Shader / URP
Advanced
Unity 2021.3+ / Unity 6 (URP)
알파 블렌딩 정렬 문제 없이 섀도우 맵을 통과하는 디더링(Bayer Dither) 반투명 셰이더Bayer Dithered Transparency Shader Preserving Depth and Shadow Maps
불투명(Opaque) 큐에서 4x4 베이어 매트릭스(Bayer Matrix) 픽셀 마스킹으로 반투명을 모사하여 드로우콜 배칭과 깊이/그림자 맵을 유지하는 기법.Simulating transparency in the Opaque render queue with a 4x4 Bayer matrix pixel mask to preserve draw-call batching, depth writes, and shadow casting.
#Shader #URP #Dither #Shadows #Optimization
핵심 개념일반적인 반투명(Transparent) 셰이더는 깊이 버퍼 기록(
ZWrite Off)을 비활성화하므로 폴리곤이 겹칠 때 정렬(Sorting) 문제가 발생하고 그림자를 드리우지 못합니다. 디더링(Dithering)을 사용하면 불투명 렌더 큐에서 clip()을 통해 섀도우 맵 캐스팅과 깊이 버퍼 기록을 유지하면서 페이드 효과를 구현할 수 있습니다.Core ConceptStandard transparent shaders disable depth buffer writes (
ZWrite Off), which causes sorting artifacts on overlapping geometry and prevents shadow map casting. Ordered dithering uses clip() inside the opaque render queue to maintain depth writes and shadow casting while fading objects out.1. 4x4 베이어 매트릭스 디더 마스크 HLSL1. 4x4 Bayer Matrix Dither Mask in HLSL
static const float bayer4x4[16] = {
0.0/16.0, 8.0/16.0, 2.0/16.0, 10.0/16.0,
12.0/16.0, 4.0/16.0, 14.0/16.0, 6.0/16.0,
3.0/16.0, 11.0/16.0, 1.0/16.0, 9.0/16.0,
15.0/16.0, 7.0/16.0, 13.0/16.0, 5.0/16.0
};
// 스크린 픽셀 좌표 4x4 모듈로 인덱싱
int2 pixelPos = int2(input.positionCS.xy) % 4;
int index = pixelPos.y * 4 + pixelPos.x;
float threshold = bayer4x4[index];
clip(_Transparency - threshold); // 알파값 미만이면 픽셀 폐기
static const float bayer4x4[16] = {
0.0/16.0, 8.0/16.0, 2.0/16.0, 10.0/16.0,
12.0/16.0, 4.0/16.0, 14.0/16.0, 6.0/16.0,
3.0/16.0, 11.0/16.0, 1.0/16.0, 9.0/16.0,
15.0/16.0, 7.0/16.0, 13.0/16.0, 5.0/16.0
};
// 4x4 modulo indexing using screen pixel coordinates
int2 pixelPos = int2(input.positionCS.xy) % 4;
int index = pixelPos.y * 4 + pixelPos.x;
float threshold = bayer4x4[index];
clip(_Transparency - threshold); // Discard pixel if below alpha threshold
2. 실무 적용 및 주의사항2. Practical Considerations and Caveats
- 카메라 근접 페이드 활용: 3인칭 게임에서 카메라가 캐릭터나 벽에 가까워질 때 카메라와의 거리에 비례해
_Transparency를 낮추면 시야를 가리지 않으면서도 깊이 정렬 깨짐 없이 자연스럽게 투과시킬 수 있습니다. - TAA 및 해상도 영향: 고해상도 디스플레이나 Temporal Anti-Aliasing(TAA)이 활성화된 환경에서는 4×4 도트 격자가 시각적으로 부드럽게 섞여 보이지만, 저해상도 렌더링에서는 격자 패턴이 눈에 띌 수 있습니다.
- Camera Proximity Fade: In third-person games, reducing
_Transparencybased on the distance between the camera and the character or obstacles prevents view occlusion without causing depth-sorting glitches. - Interaction with TAA and Resolution: On high-DPI displays or when Temporal Anti-Aliasing (TAA) is enabled, the 4×4 dither pattern blends into a smooth fade, whereas on low-resolution targets the stipple grid remains visible.
베이어 매트릭스 기반 디더링은 알파 블렌딩의 오버드로우와 정렬 문제를 피하면서 불투명 파이프라인 안에서 반투명 페이드와 그림자 캐스팅을 함께 처리할 때 유용합니다.
Bayer matrix dithering avoids alpha-blending overdraw and sorting issues while supporting fade transitions and shadow casting within the opaque rendering pipeline.