Shader / URP
Advanced
Unity 2022.3+ / Unity 6 (URP)
3D 픽셀 아트용 컬러 팔레트 양자화(LUT Posterization) 및 1px 도트 외곽선 셰이더Color Palette Quantization (Posterization) and 1px Pixel-Art Outline Shader
3D 씬의 연속적인 라이팅 그라데이션을 제한된 팔레트로 양자화하는 포스터라이제이션과 Depth/Normal 버퍼를 이용한 1px 단위 외곽선 검출 기법.Quantizing smooth 3D lighting gradients into discrete retro color steps and extracting 1px pixel-art outlines from Depth and Normal buffers in URP.
#Shader #URP #PixelArt #LUT #Posterization #Outline
핵심 개념3D 씬을 저해상도로 렌더링하더라도 연속적인 3D 셰이딩 그라데이션이 그대로 남아있으면 픽셀 아트보다는 저화질 3D 화면처럼 보이기 쉽습니다. 색상을 일정 단계로 나누어 제한된 컬러 팔레트로 매핑하는 컬러 양자화(Color Quantization)와, 물체 간 경계에 1픽셀 도트 선을 그리는 Normal/Depth Edge Outline을 결합하면 일관된 픽셀 아트 외곽선과 색감을 구현할 수 있습니다.
Core ConceptRendering a 3D scene at low resolution still leaves smooth shading gradients intact, which can look like low-resolution 3D rather than pixel art. Combining Color Quantization (posterizing colors into discrete steps or a fixed LUT palette) with a Normal/Depth Edge Outline (drawing 1-pixel borders along object boundaries) produces consistent pixel-art outlines and shading.
1. 3D 픽셀 아트 종합 포스트 프로세싱 셰이더1. Combined 3D Pixel Art Post-Processing Shader
// URP Custom Post-Process Pass Fragment Shader
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareNormalsTexture.hlsl"
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex_Point);
float4 _MainTex_TexelSize;
float _ColorSteps; // 색상 양자화 단계 (예: 4.0 ~ 8.0)
float _DepthThreshold; // 외곽선 깊이 민감도 (예: 0.2)
float _NormalThreshold; // 외곽선 노멀 민감도 (예: 0.4)
// 1픽셀 십자(Cross) 외곽선 검출
float DetectOutline(float2 uv)
{
float2 offset = _MainTex_TexelSize.xy;
// 중앙 및 상하좌우 샘플링
float dCenter = SampleSceneDepth(uv);
float dTop = SampleSceneDepth(uv + float2(0, offset.y));
float dBottom = SampleSceneDepth(uv - float2(0, offset.y));
float dLeft = SampleSceneDepth(uv - float2(offset.x, 0));
float dRight = SampleSceneDepth(uv + float2(offset.x, 0));
// 깊이 차이 계산
float depthDiff = (abs(dCenter - dTop) + abs(dCenter - dBottom) +
abs(dCenter - dLeft) + abs(dCenter - dRight));
// 노멀 차이 계산
float3 nCenter = SampleSceneNormals(uv);
float3 nTop = SampleSceneNormals(uv + float2(0, offset.y));
float3 nRight = SampleSceneNormals(uv + float2(offset.x, 0));
float normalDiff = distance(nCenter, nTop) + distance(nCenter, nRight);
// 임계값 초과 시 1.0 (외곽선 검정)
return (depthDiff > _DepthThreshold || normalDiff > _NormalThreshold) ? 1.0 : 0.0;
}
half4 Frag(Varyings input) : SV_Target
{
// 1. 원본 컬러 샘플링
half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex_Point, input.uv);
// 2. 색상 단계 양자화 (Posterization)
col.rgb = floor(col.rgb * _ColorSteps) / _ColorSteps;
// 3. 1픽셀 도트 외곽선 합성
float isEdge = DetectOutline(input.uv);
if (isEdge > 0.5)
{
col.rgb = half3(0.05, 0.05, 0.08); // 도트 검은색 아웃라인
}
return col;
}
// URP Custom Post-Process Pass Fragment Shader
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareNormalsTexture.hlsl"
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex_Point);
float4 _MainTex_TexelSize;
float _ColorSteps; // Color quantization steps (e.g., 4.0 to 8.0)
float _DepthThreshold; // Outline depth sensitivity (e.g., 0.2)
float _NormalThreshold; // Outline normal sensitivity (e.g., 0.4)
// 1-pixel cross neighborhood outline detection
float DetectOutline(float2 uv)
{
float2 offset = _MainTex_TexelSize.xy;
// Sample center and 4-connected neighbors
float dCenter = SampleSceneDepth(uv);
float dTop = SampleSceneDepth(uv + float2(0, offset.y));
float dBottom = SampleSceneDepth(uv - float2(0, offset.y));
float dLeft = SampleSceneDepth(uv - float2(offset.x, 0));
float dRight = SampleSceneDepth(uv + float2(offset.x, 0));
// Compute depth delta
float depthDiff = (abs(dCenter - dTop) + abs(dCenter - dBottom) +
abs(dCenter - dLeft) + abs(dCenter - dRight));
// Compute normal delta
float3 nCenter = SampleSceneNormals(uv);
float3 nTop = SampleSceneNormals(uv + float2(0, offset.y));
float3 nRight = SampleSceneNormals(uv + float2(offset.x, 0));
float normalDiff = distance(nCenter, nTop) + distance(nCenter, nRight);
// Output 1.0 when exceeding threshold (dark outline)
return (depthDiff > _DepthThreshold || normalDiff > _NormalThreshold) ? 1.0 : 0.0;
}
half4 Frag(Varyings input) : SV_Target
{
// 1. Sample source color
half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex_Point, input.uv);
// 2. Quantize color steps (Posterization)
col.rgb = floor(col.rgb * _ColorSteps) / _ColorSteps;
// 3. Composite 1-pixel outline
float isEdge = DetectOutline(input.uv);
if (isEdge > 0.5)
{
col.rgb = half3(0.05, 0.05, 0.08); // Dark pixel outline color
}
return col;
}
2. 레트로 무드 연출 팁2. Retro Visual Styling Tips
- 감마 코렉션 일치:
_ColorSteps로 색상을 나눌 때 선형(Linear) 공간에서 계산하면 어두운 영역의 계조가 좁아질 수 있으므로, sRGB 공간에서 양자화한 뒤 선형으로 변환하는 방식이 유리합니다. - 디더링(Dithering) 추가 결합: 포스터라이제이션으로 분리된 명암 경계에 4×4 베이어 디더링 패턴을 함께 적용하면 고전 도트 게임 스타일의 점묘 명암이 재현됩니다.
- Gamma Space Consistency: Quantizing colors with
_ColorStepsdirectly in Linear space can compress dark shadow tones; converting to sRGB before quantization and back to Linear afterward preserves more even tonal steps. - Combining with Ordered Dithering: Applying a subtle 4×4 Bayer dither offset along posterized shading boundaries reproduces the stippled shading transitions characteristic of classic low-color games.
깊이·노멀 기반의 십자 샘플링 외곽선 검출과 컬러 포스터라이제이션을 후처리 패스로 묶으면 3D 지오메트리를 픽셀 아트 톤으로 정돈할 수 있습니다.
Combining cross-sampled depth/normal edge detection with color posterization in a post-process pass converts continuous 3D geometry into a cohesive pixel-art look.