I’m using Unity 2022.3.28f1, URP 14.0.11
I’m writing a shader (for URP for Forward Path) that uses the Stencil buffer.
For the object itself, which is inside the Stencil mask, everything works correctly - only that part of the object that is inside the mask is visible.
But how can I do the same for the shadow it casts?
I need to write a shader (for the green plane) that will draw a cast shadow only in the part that is inside the Stencil mask.
How can I do it? Is it possible?
Shader for Stencil mask (StencilMask.shader):
Shader "GK/StencilMask" {
Properties {
}
SubShader {
Tags { "RenderPipeline" = "UniversalPipeline" "RenderType" = "Opaque" "Queue" = "Geometry-1" }
Blend Zero One // Zero One - Full Transparent
ZTest Always // Always - No depth testing occurs. Draw all geometry, regardless of distance.
ZWrite Off
ColorMask 0 // 0 - Disables color writes to the R, G, B, and A channels.
Stencil {
Ref 1
Comp Always
Pass Replace // Replace - Записать Ref значение в буфер
}
Pass {
}
} // SubShader
} // Shader
Shader for objects using the Stencil buffer (URPLit_Stencil.shader)
This is a standard URP Lit shader with the addition of a Stencil buffer.
Shader "GK/URP_Lit_Stencil" {
Properties {
...
}
SubShader {
Tags {
"RenderType" = "TransparentCutout"
"RenderPipeline" = "UniversalPipeline"
"UniversalMaterialType" = "Lit"
"IgnoreProjector" = "True"
"ShaderModel" = "4.5" }
Stencil {
Ref 1
Comp Equal
}
Pass { // Forward pass
...
}
Pass { // ShadowCaster
...
}
Pass { // UniversalGBuffer ( used for deferred rendering: Rendering Path = Deferred )
...
}
Pass { // DepthOnly
...
}
Pass { // DepthNormals : This pass is used when drawing to a _CameraNormalsTexture texture
...
}
Pass { // Meta : This pass it not used during regular rendering, only for lightmap baking.
...
}
Pass { // Universal2D
...
}
} // SubShader
...
} // Shader