Stencil Buffers Pt 1 - cut a hole in an object

This is quite cool stuff. You can essentially cut out areas of other objects, by using the stencil buffer

There are two shaders below- one for the "hole" or the "window", the "cutter outer", whatever you wish to call it. And one for the wall or thing you're "cutting" into.

The main CGPROGRAM block remains simple, the changes happen at the start of the SubShader.

Firstly we ensure we draw the hole before the geometry, by giving it the Geometry-1 queue tag. The smaller the value, the sooner an object is drawn. We disable writing to the zbuffer for the whole subshader, as well as with colors (colormask 0).
The stencil has it's own sub block - in it, we define Ref with the value of 1. Comp is the comparision which compares the object's pixels with Ref. We "always" want to write to the stencil buffer using our cutter object's pixels, so it "always" passes this comparison & does what the Pass operation wants - in this case - it replaces the stencil buffer with the Ref value of 1.

In the "wall" shader, the queue tag is simply "Geometry", so it is drawn and calculated after the cutter object. The stencil block in this shader also has the Ref value set to 1 - however the comparison operation is "not equal", it only passes if the pixel value is not equal to 1. If it is 1, then that means the cutter is present. If not, then it's ok to "keep" the Wall object's pixel.

I think this is correctly explained...leave a comment if it's wrong! I think I have them enabled.

 

 

Shader "Dave/seethroughHOLE"{
    Properties{
        _MainTex("Diffuse",2D) = "white"{}
       
    }

        SubShader{
            Tags{"Queue" = "Geometry-1"}//draws before geometry
            ColorMask 0
            ZWrite off
            Stencil
            {
                Ref 1
                Comp always
                Pass replace
            }
            CGPROGRAM
            #pragma surface surf Lambert
            sampler2D _MainTex;
        struct Input {
            float2 uv_MainTex;
            };

        void surf(Input IN, inout SurfaceOutput o) {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;

        }

        ENDCG
    }

    Fallback "Diffuse"
}

 _______________________________________________________________________________________________

 Shader "Dave/wall"{
    Properties{
        _MainTex("Diffuse",2D) = "white"{}

    }

        SubShader{
            Tags{"Queue" = "Geometry"}

            Stencil
            {
                Ref 1
                Comp notequal //looks for a 1 in the stencil buffer, if not equal to 1, keep
                Pass keep
            }

            CGPROGRAM
            #pragma surface surf Lambert
            sampler2D _MainTex;
        struct Input {
            float2 uv_MainTex;
            };

        void surf(Input IN, inout SurfaceOutput o) {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;

        }

        ENDCG
    }

        Fallback "Diffuse"
}

 

There 's quite a lot of stuff going on with the stencil buffer... Here's a paste of all the info from the Unity docs...


Comparison operation values

In C#, these values are represented by the Rendering.CompareFunction enum.

Value Corresponding integer value in Rendering.CompareFunction enum Function
Never 1 Never render pixels.
Less 2 Render pixels when their reference value is less than the current value in the stencil buffer.
Equal 3 Render pixels when their reference value is equal to the current value in the stencil buffer.
LEqual 4 Render pixels when their reference value is less than or equal to the current value in the stencil buffer.
Greater 5 Render pixels when their reference value is greater than the current value in the stencil buffer.
NotEqual 6 Render pixels when their reference value differs from the current value in the stencil buffer.
GEqual 7 Render pixels when their reference value is greater than or equal to the current value in the stencil buffer.
Always 8 Always render pixels.

Stencil operation values

In C#, these values are represented by the Rendering.Rendering.StencilOp enum.

Value Corresponding integer value in Rendering.StencilOp enum Function
Keep 0 Keep the current contents of the stencil buffer.
Zero 1 Write 0 into the stencil buffer.
Replace 2 Write the reference value into the buffer.
IncrSat 3 Increment the current value in the buffer. If the value is 255 already, it stays at 255.
DecrSat 4 Decrement the current value in the buffer. If the value is 0 already, it stays at 0.
Invert 5 Negate all the bits of the current value in the buffer.
IncrWrap 7 Increment the current value in the buffer. If the value is 255 already, it becomes 0.
DecrWrap8Decrement the current value in the buffer. If the value is 0 already, it becomes 255.


More shit here-


ParameterValueFunction
ref An integer. Range 0 through 255. Default is 0. The reference value.

The GPU compares the current contents of the stencil buffer against this value, using the operation defined in comparisonOperation.

This value is masked with readMask or writeMask, depending on whether a read or a write operation is occurring.

The GPU can also write this value to the stencil buffer, if Pass, Fail or ZFail have a value of Replace.
readMask An integer. Range 0 through 255. Default is 255. The GPU uses this value as a mask when it performs the stencil test.

See above for the stencil test equation.
writeMask An integer. Range 0 through 255. Default is 255. The GPU uses this value as a mask when it writes to the stencil buffer.

Note that, like other masks, it specifies which bits are included in the operation. For example, a value of 0 means that no bits are included in the write operation, not that the stencil buffer receives a value of 0.
comparisonOperation A comparison operation. See Comparison operation values for valid values. Default is Always. The operation that the GPU performs for the stencil test for all pixels.

This defines the operation for all pixels, regardless of facing. If this is defined in addition to comparisonOperationBack and comparisonOperationFront, this value overrides them.
passOperation A stencil operation. See Stencil operation values for valid values. Default is Keep. The operation that the GPU performs on the stencil buffer when a pixel pases both the stencil test and the depth test.

This defines the operation for all pixels, regardless of facing. If this is defined in addition to passOperationBack and passOperationFront, this value overrides them.
failOperation A stencil operation. See Stencil operation values for valid values. Default is Keep. The operation that the GPU performs on the stencil buffer when a pixel fails the stencil test.

This defines the operation for all pixels, regardless of facing. If this is defined in addition to failOperationBack and failOperationFront, this value overrides them.
zFailOperation A stencil operation. See Stencil operation values for valid values. Default is Keep. The operation that the GPU performs on the stencil buffer when a pixel passes the stencil test, but fails the depth test.

This defines the operation for all pixels, regardless of facing. If this is defined in addition to zFailOperation and zFailOperation, this value overrides them.
comparisonOperationBack A comparison operation. See Comparison operation values for valid values. Default is Always. The operation that the GPU performs for the stencil test.

This defines the operation for back-facing pixels only. If comparisonOperation is defined, that value overrides this one.
passOperationBack A stencil operation. See Stencil operation values for valid values. Default is Keep. The operation that the GPU performs on the stencil buffer when a pixel pases both the stencil test and the depth test.

This defines the operation for back-facing pixels only. If passOperation is defined, that value overrides this one.
failOperationBack A stencil operation. See Stencil operation values for valid values. Default is Keep. The operation that the GPU performs on the stencil buffer when a pixel fails the stencil test.

This defines the operation for back-facing pixels only. If failOperation is defined, that value overrides this one.
zFailOperationBack A stencil operation. See Stencil operation values for valid values. Default is Keep. The operation that the GPU performs on the stencil buffer when a pixel passes the stencil test, but fails the depth test.

This defines the operation for back-facing pixels only. If zFailOperation is defined, that value overrides this one.
comparisonOperationFront A comparison operation. See Comparison operation values for valid values. Default is Always. The operation that the GPU performs for the stencil test.

This defines the operation for front-facing pixels only. If comparisonOperation is defined, that value overrides this one.
passOperationFront A stencil operation. See Stencil operation values for valid values. Default is Keep. The operation that the GPU performs on the stencil buffer when a pixel pases both the stencil test and the depth test.

This defines the operation for front-facing pixels only. If passOperation is defined, that value overrides this one.
failOperationFront A stencil operation. See Stencil operation values for valid values. Default is Keep. The operation that the GPU performs on the stencil buffer when a pixel fails the stencil test.

This defines the operation for front-facing pixels only. If failOperation is defined, that value overrides this one.
zFailOperationFrontA stencil operation. See Stencil operation values for valid values. Default is Keep.The operation that the GPU performs on the stencil buffer when a pixel passes the stencil test, but fails the depth test.

This defines the operation for front-facing pixels only. If zFailOperation is defined, that value overrides this one.
 

Comments

Popular posts from this blog

setting VFX graph properties using C#

scripting custom render texture creation, assignment, shaders