Stencil Buffers pt2 - object that only appears behind certain "glass"

More or less the same as what happened in the last post about stencil buffers - the few lines with Enums are useful - they "enumerate" - put all the functions into a drop down list in the Inspector for us. So we store these in variables - _SComp and _SOp. Notice the values are just numbers, as they are just entries in a list/array.

A trick in the tutorial i watched was having an object only appear behind a certain "pane of glass" - or quad. The quad holds the stencil window shader and faces the object (so it faces away from the camera), and the SComp is set to always, the SOp is set to Replace. The trick is that both the object and glass have the same ref numbers, so if you had another pane of glass with a different ref number, the object does not appear behind it.

The object's shader beneath, has a bump and albedo in it. Set the SComp to Equal (different to the last example we did) and the SOp to Keep.

 

Shader "Dave/stencilwindow"
{
    Properties{
        _Color("Main Colour",color) = (1,1,1,1)
        _SRef("Stencil Ref",Float)=1
        [Enum(UnityEngine.Rendering.CompareFunction)] _SComp("Stencil Comp",Float) = 8
        [Enum(UnityEngine.Rendering.StencilOp)] _SOp("Stencil Op", Float) = 2

    }

        SubShader{
            Tags{"Queue" = "Geometry-1"}
            ZWrite off
            ColorMask 0
            Stencil{
                Ref[_SRef]
                Comp[_SComp]
                Pass[_SOp]

            }
            CGPROGRAM
            #pragma surface surf Lambert
            sampler2D _myDiffuse;
    struct Input {
        float2 uv_myDiffuse;
            };
    void surf(Input IN, inout SurfaceOutput o) {

        o.Albedo = tex2D(_myDiffuse, IN.uv_myDiffuse).rgb;
        }
    ENDCG
    }

}

_____________________________________________________________________________

 

 Shader "Dave/BumpDiffuseStencil"{

    Properties{
        _Color("Color",color) = (1,1,1,1)
        _myDiffuse("Diffuse Texture",2D) = "white"{}
        _myBump("Bump Texture",2D) = "bump"{}
        _mySlider("Bump Amount",Range(0,10)) = 1

        _SRef("Stencil Ref",float) = 1
        [Enum(UnityEngine.Rendering.CompareFunction)] _SComp("Stencil Comp",Float) = 8
        [Enum(UnityEngine.Rendering.StencilOp)] _SOp("Stencil Op",Float) = 2


    }

        SubShader{
            Stencil{
                Ref[_SRef]
                Comp[_SComp]
                Pass[_SOp]

            }
        CGPROGRAM
            #pragma surface surf Lambert
            sampler2D _myDiffuse;
        sampler2D _myBump;
            half _mySlider;
        float4 _Color;

        struct Input {
            float2 uv_myDiffuse;
            float2 uv_myBump;

        };

        void surf(Input IN, inout SurfaceOutput o) {
            o.Albedo = tex2D(_myDiffuse, IN.uv_myDiffuse).rgb*_Color.rgb;
            o.Normal = UnpackNormal(tex2D(_myBump, IN.uv_myBump));
            o.Normal *= float3(_mySlider, _mySlider, 1);

        }


        ENDCG
        }
            Fallback "Diffuse"
}

Comments

Popular posts from this blog

setting VFX graph properties using C#

scripting custom render texture creation, assignment, shaders