Shader / URP
Advanced
Unity 2021.3+ / Unity 6 (URP)
카메라 깊이 텍스처(_CameraDepthTexture) 페이드 기반 해안선 수면 폼(Foam) 셰이더Shoreline Water Foam Shader Using Camera Depth Texture Fade
수면 지오메트리의 스크린 깊이와 바닥 지형 깊이의 차이를 계산하여 해안선이나 바위 주변에 수면 거품(Foam)을 표현하는 URP 물 셰이더.A URP water shader that computes the difference between water surface depth and underlying scene geometry depth to generate shoreline and obstacle foam.
#Shader #URP #Water #DepthTexture #VFX
핵심 개념URP의
_CameraDepthTexture를 샘플링하면 반투명 수면 메시 뒤에 그려진 불투명 해저 지형의 선형 깊이($SceneDepth$)를 얻을 수 있습니다. 여기서 현재 수면 정점의 뷰 공간 깊이($SurfaceDepth$)를 뺀 깊이 차이($\Delta Depth$)가 작을수록 물이 얕은 경계면이므로, 이를 기준으로 해안가 거품(Foam) 색상을 블렌딩합니다.Core ConceptSampling URP's
_CameraDepthTexture provides the linear eye-space depth ($SceneDepth$) of opaque terrain behind the translucent water mesh. Subtracting the water surface's own view-space depth ($SurfaceDepth$) yields the vertical depth difference ($\Delta Depth$); smaller values indicate shallow intersections where shoreline foam can be blended.1. 깊이 페이드 연산 HLSL1. Depth Fade Calculation in HLSL
float2 screenUV = input.positionCS.xy / _ScaledScreenParams.xy;
float rawDepth = SampleSceneDepth(screenUV);
float sceneEyeDepth = LinearEyeDepth(rawDepth, _ZBufferParams);
float surfaceEyeDepth = input.positionCS.w;
float depthDiff = sceneEyeDepth - surfaceEyeDepth;
float foamFactor = 1.0 - saturate(depthDiff / _FoamDistance);
half4 foamColor = _FoamColor * foamFactor;
2. 실무 적용 및 주의사항2. Practical Considerations and Caveats
- URP Depth Texture 활성화 및 렌더 큐 설정:
_CameraDepthTexture에는 불투명(Opaque, Queue <= 2500) 오브젝트의 깊이만 기록되므로, URP 에셋에서Depth Texture옵션을 켜고 수면 머티리얼은 반드시Transparent(Queue 3000) 큐에서 렌더링해야 합니다. - 오소그래픽(Orthographic) 카메라 호환:
LinearEyeDepth()와positionCS.w는 원근(Perspective) 투영 행렬을 기준으로 동작하므로, 직교 투영(Orthographic) 카메라를 사용하는 게임에서는unity_OrthoParams.w분기를 통해 선형 깊이를 별도로 계산해야 합니다.
- Enable Depth Texture and Use Transparent Queue:
_CameraDepthTextureonly captures objects rendered in theOpaquequeue (<= 2500). EnableDepth Textureon the URP Asset and set the water material toTransparent(Queue 3000) so the seabed is already written to the depth buffer. - Orthographic Camera Compatibility:
LinearEyeDepth()andpositionCS.wassume perspective projection; when using an orthographic camera, checkunity_OrthoParams.wand interpolate between the camera's near and far clip planes instead.
깊이 버퍼 차이를 이용한 Depth Fade 기법은 별도의 교차 마스크 텍스처를 제작하지 않고도 지형이나 바위가 물에 잠기는 모든 경계면에 자연스러운 수면 거품과 수심 색상을 표현할 수 있습니다.
Depth-fade intersection calculations automatically generate shoreline foam and depth-based water tinting around any submerged terrain or prop without manual mask painting.