Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Shadows in holes using stencil buffer

Discussion in 'Shaders' started by MrCHICK00, May 5, 2022.

  1. MrCHICK00

    MrCHICK00

    Joined:
    May 7, 2019
    Posts:
    2
    Hey. I'm using the stencil buffer to make a hole in plane. It work perfect but the plane receive shadows on all surface. I want that only the non transparent plane to receive shadows and that transparent hole to not receive shadows. For plane I am using a default SurfaceShader nothing special. I turned the "receive shadows" and "cast shadows" to off for both objects (hole and plane) and nothing happened. Someone can help me to solve the problem? Thanks!

    Here is the hole shader code (the object who make the hole):
    Code (CSharp):
    1. Shader "Custom/Hole"{
    2.     SubShader{
    3.         Tags { "RenderType" = "Transparent" "Queue" = "Geometry-1" }
    4.         ColorMask 0
    5.         ZWrite Off
    6.         Stencil{
    7.             Ref 1
    8.             Comp always
    9.             Pass replace
    10.         }
    11.         Pass{}
    12.     }
    13. }
    Here is the shader code for plane:
    Code (CSharp):
    1. Shader "Custom/Plane"{
    2.     Properties{
    3.         _MainTex("Albedo (RGB)", 2D) = "white" {}
    4.     }
    5.     SubShader{
    6.         Tags{ "RenderType" = "Transparent"  "Queue" = "Geometry+0" }
    7.         ZWrite On
    8.         ZTest LEqual
    9.         Cull Back
    10.         Stencil{
    11.             Ref 1
    12.             Comp notequal
    13.             Pass keep
    14.         }
    15.         CGPROGRAM
    16.         #pragma surface surf Lambert
    17.         #pragma target 3.0
    18.         sampler2D _MainTex;
    19.         struct Input{
    20.             float2 uv_MainTex;
    21.         };
    22.         void surf(Input IN, inout SurfaceOutput o){
    23.             o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
    24.         }
    25.         ENDCG
    26.     }
    27.     FallBack "Diffuse"
    28. }
    Here is the result: Screenshot_18.jpg Screenshot_22.jpg