Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make a stencil shader only affect a certain object?

Discussion in 'Shaders' started by dbonham, Apr 10, 2021.

  1. dbonham

    dbonham

    Joined:
    Dec 13, 2013
    Posts:
    2
    I'm working on a building designer and I'm using two planes with stencil shaders to cut holes in the walls for each window, one back and one front facing. The problem is that the stencils affect wall sections other than the ones the window is attached to, causing this x-ray effect on the corners. Is there a property I can set when I create the object to tell it to only affect the wall section in its parent?

    I appreciate any insight, shaders are forbidden magic to me.


    Shader on the stencil planes:
    Code (CSharp):
    1.  
    2. Shader "Mask"
    3. {
    4.    //show values to edit in inspector
    5.    Properties{
    6.        [IntRange] _StencilRef("Stencil Reference Value", Range(0,255)) = 0
    7.    }
    8.  
    9.        SubShader{
    10.        //the material is completely non-transparent and is rendered at the same time as the other opaque geometry
    11.        Tags{ "RenderType" = "Opaque" "Queue" = "Geometry-1"}
    12.  
    13.        //stencil operation
    14.        Stencil{
    15.            Ref[_StencilRef]
    16.            Comp Always
    17.            Pass Replace
    18.        }
    19.  
    20.        Pass{
    21.                //don't draw color or depth
    22.                Blend Zero One
    23.                ZWrite Off
    24.  
    25.                CGPROGRAM
    26.                #include "UnityCG.cginc"
    27.  
    28.                #pragma vertex vert
    29.                #pragma fragment frag
    30.  
    31.                struct appdata {
    32.                    float4 vertex : POSITION;
    33.                };
    34.  
    35.                struct v2f {
    36.                    float4 position : SV_POSITION;
    37.                };
    38.  
    39.                v2f vert(appdata v) {
    40.                    v2f o;
    41.                    //calculate the position in clip space to render the object
    42.                    o.position = UnityObjectToClipPos(v.vertex);
    43.                    return o;
    44.                }
    45.  
    46.                fixed4 frag(v2f i) : SV_TARGET{
    47.                    return 0;
    48.                }
    49.  
    50.                ENDCG
    51.            }
    52.    }
    53. }
    Shader on the wall sections:
    Code (CSharp):
    1.  
    2. Shader "WallShaderDefault" {
    3.    Properties{
    4.        _Color("Tint", Color) = (0, 0, 0, 1)
    5.        _MainTex("Texture", 2D) = "white" {}
    6.        _Smoothness("Smoothness", Range(0, 1)) = 0
    7.        _Metallic("Metalness", Range(0, 1)) = 0
    8.        [HDR] _Emission("Emission", color) = (0,0,0)
    9.  
    10.        [IntRange] _StencilRef("Stencil Reference Value", Range(0,255)) = 0
    11.    }
    12.        SubShader{
    13.            Tags{ "RenderType" = "Opaque" "Queue" = "Geometry"}
    14.  
    15.            //stencil operation
    16.            Stencil{
    17.                Ref[_StencilRef]
    18.                Comp Equal
    19.            }
    20.  
    21.            CGPROGRAM
    22.  
    23.            #pragma surface surf Standard fullforwardshadows
    24.            #pragma target 3.0
    25.  
    26.            sampler2D _MainTex;
    27.            fixed4 _Color;
    28.  
    29.            half _Smoothness;
    30.            half _Metallic;
    31.            half3 _Emission;
    32.  
    33.            struct Input {
    34.                float2 uv_MainTex;
    35.            };
    36.  
    37.            void surf(Input i, inout SurfaceOutputStandard o) {
    38.                fixed4 col = tex2D(_MainTex, i.uv_MainTex);
    39.                col *= _Color;
    40.                o.Albedo = col.rgb;
    41.                o.Metallic = _Metallic;
    42.                o.Smoothness = _Smoothness;
    43.                o.Emission = _Emission;
    44.            }
    45.            ENDCG
    46.        }
    47.            FallBack "Standard"
    48. }
     
  2. sebastianfeistl

    sebastianfeistl

    Joined:
    Sep 29, 2015
    Posts:
    4
    I'm having exactly the same problem. Did you find a solution?