Shader / URP
Advanced
Unity 2021.3+ / Unity 6 (URP)
깊이(Depth) 및 노멀 텍스처 기반 URP 풀스크린 렌더 피처 아웃라인URP Full-Screen Render Feature Outline Using Depth and Normal Buffers
소벨 필터(Sobel Filter)를 카메라 Depth/Normal 텍스처에 적용해 메시 지오메트리 변형 없이 외곽선을 추출하는 URP 스크린 스페이스 셰이더.Extracting clean outlines without mesh duplication by applying a Sobel filter to the camera Depth and Normal textures in a URP screen-space shader.
#Shader #URP #RenderFeature #Outline #PostProcessing
핵심 개념지오메트리를 뒤집어 확장하는 역노멀 아웃라인(Inverted Hull)은 하드 엣지(Hard Edge) 모서리가 갈라지거나 폴리곤 수에 비례해 정점 비용이 증가하는 한계가 있습니다. 화면 전체의 카메라 깊이 버퍼(
_CameraDepthTexture)와 뷰 공간 노멀(_CameraNormalsTexture)에 소벨 필터(Sobel Filter)를 적용하면 메시와 배경의 경계면에 일관된 두께의 선명한 외곽선이 생성됩니다.Core ConceptTraditional inverted-hull outlines duplicate and extrude geometry along vertex normals, which causes gaps at hard edges and increases vertex count. Applying a Sobel filter across the full-screen camera depth buffer (
_CameraDepthTexture) and view-space normals (_CameraNormalsTexture) produces crisp outlines of uniform width along object silhouettes and creases.1. 소벨 필터 깊이/노멀 델타 추출 HLSL1. Sobel Filter Depth/Normal Delta Extraction in HLSL
float SampleDepthDiff(float2 uv, float2 offset)
{
float d0 = SampleSceneDepth(uv + float2(-offset.x, -offset.y));
float d1 = SampleSceneDepth(uv + float2( offset.x, -offset.y));
float d2 = SampleSceneDepth(uv + float2(-offset.x, offset.y));
float d3 = SampleSceneDepth(uv + float2( offset.x, offset.y));
float edgeX = d1 - d0;
float edgeY = d3 - d2;
return sqrt(edgeX * edgeX + edgeY * edgeY);
}
2. 실무 적용 및 주의사항2. Practical Considerations and Caveats
- 뷰 각도에 따른 깊이 보정(Grazing Angle): 카메라 시선과 거의 평행한 바닥이나 벽면에서는 인접 픽셀 간 깊이 차이가 급격히 커져 면 전체가 검은 외곽선으로 덮일 수 있으므로,
dot(viewNormal, viewDir)에 반비례하도록 깊이 임계값(Threshold)을 보정해야 합니다. - 렌더 피처 입력 요구사항 설정: URP
ScriptableRenderPass초기화 시ConfigureInput(ScriptableRenderPassInput.Depth | ScriptableRenderPassInput.Normal)을 명시해야 셰이더에서_CameraDepthTexture와_CameraNormalsTexture를 정상 샘플링할 수 있습니다.
- Grazing Angle Depth Compensation: Surfaces viewed at steep grazing angles exhibit large depth differences between adjacent pixels, which can cause false outline fills; scale the depth threshold inversely with
dot(viewNormal, viewDir)to suppress artifacts. - Configuring Render Feature Inputs: Call
ConfigureInput(ScriptableRenderPassInput.Depth | ScriptableRenderPassInput.Normal)inside your URPScriptableRenderPasssetup so that URP generates and binds_CameraDepthTextureand_CameraNormalsTexture.
깊이와 노멀 버퍼 기반의 스크린 스페이스 아웃라인은 씬 내 폴리곤 수와 무관하게 일정한 후처리 비용으로 실루엣과 내부 주름선을 동시에 검출할 수 있습니다.
Screen-space outlines driven by depth and normal buffers detect both external silhouettes and internal surface creases at a fixed post-processing cost independent of scene polygon count.