|
| 1 | +# Write a depth-only pass in a Universal Render Pipeline shader |
| 2 | + |
| 3 | +To create a depth prepass that writes the depth of objects before the opaque and transparent passes, add a shader pass that has the `DepthOnly` tag. |
| 4 | + |
| 5 | +Add this pass if you enable a setting that requires a depth prepass, otherwise Unity might render incorrectly. For example, if you enable **Depth Priming** in the [Universal Render Pipleine (URP) Asset](urp-universal-renderer.md), opaque objects are invisible. |
| 6 | + |
| 7 | +**Important**: To render correctly, make sure that every shader pass renders the same number of fragments in the same positions. For example, use an include or a macro to share the same code between passes, especially if you change the positions of vertices, use alpha clipping, or use effects like dithering. |
| 8 | + |
| 9 | +## Add a depth-only pass |
| 10 | + |
| 11 | +The following example shows how to add a depth-only pass to a shader: |
| 12 | + |
| 13 | +```lang-hlsl |
| 14 | +Shader "Example/CustomShader" |
| 15 | +{ |
| 16 | + SubShader |
| 17 | + { |
| 18 | +
|
| 19 | + Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" } |
| 20 | +
|
| 21 | + // Add a depth-only pass |
| 22 | + Pass |
| 23 | + { |
| 24 | + Name "DepthOnlyPass" |
| 25 | + Tags { "LightMode" = "DepthOnly" } |
| 26 | +
|
| 27 | + // Write depth to the depth buffer |
| 28 | + ZWrite On |
| 29 | +
|
| 30 | + // Don't write to the color buffer |
| 31 | + ColorMask 0 |
| 32 | +
|
| 33 | + ... |
| 34 | + } |
| 35 | +
|
| 36 | + // The forward pass. This pass also writes depth by default. |
| 37 | + Pass |
| 38 | + { |
| 39 | + Name "ForwardPass" |
| 40 | + Tags { "LightMode" = "UniversalForward" } |
| 41 | +
|
| 42 | + ... |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## Add a depth and normals pass |
| 49 | + |
| 50 | +The recommended best practice is to also add a pass that has the `DepthNormals` tag, to write both the depth and normals of objects. Otherwise, if you enable features like screen space ambient occlusion (SSAO), Unity might not render objects correctly. |
| 51 | + |
| 52 | +For more information about outputting normals, refer to [Visualize normal vectors in a shader in URP](writing-shaders-urp-unlit-normals.md). |
| 53 | + |
| 54 | +For example: |
| 55 | + |
| 56 | +```lang-hlsl |
| 57 | +Pass |
| 58 | +{ |
| 59 | + Name "DepthNormalsPass" |
| 60 | + Tags { "LightMode" = "DepthNormals" } |
| 61 | +
|
| 62 | + // Write depth to the depth buffer |
| 63 | + ZWrite On |
| 64 | +
|
| 65 | + ... |
| 66 | +
|
| 67 | + float4 frag(Varyings input) : SV_TARGET |
| 68 | + { |
| 69 | + // Return the normal as a value between 0 and 1 |
| 70 | + float3 normalWS = normalize(input.normalWS); |
| 71 | + return float4(normalWS * 0.5 + 0.5, 1); |
| 72 | + }} |
| 73 | +``` |
| 74 | + |
| 75 | +## Additional resources |
| 76 | + |
| 77 | +- [ShaderLab Pass tags](urp-shaders/urp-shaderlab-pass-tags.md) |
| 78 | +- [Shader methods](use-built-in-shader-methods.md) |
| 79 | +- [Universal Render Pipleine (URP) Asset](urp-universal-renderer.md) |
0 commit comments