Cutout depth normals texture problem

I made a simple cutout surface shader that uses addshadow for the shadowcaster pass.
Everything works really well (also depth), except of the depthnormals Texture.

I’m using Keijiros beautiful Kino Obscurance with forward rendering. When set to depth textures everything looks good, but when I switch to depthnormals texture apparently the shader writes its normal into the buffer.

So in the end it is not really a problem for me, but I’m wondering: How could I control what is written in the DetphNormals Texture? Would I need to input the WorldNormal just to save it out again when I’m not clipping? (My clippling needs to be in the surface shader, it is clipped through the world position).

The depth normals texture is generated with a replacement shader pass. You would need to modify the depth normal shader to add a new pass with a unique render type tag.

1 Like

Hi Ben,

Thanks a lot for your answer!
This gives me an idea on where to start. I sadly really have to do this I noticed that the obscurance plugin always uses depthNormals. (DepthTexture only mode · Issue #17 · keijiro/KinoObscurance · GitHub).

I will give an update, once managed to get it working.

3270583--252657--upload_2017-10-30_10-4-54.png

Awesome, I got it all working, was really not as hard as I feared!

Just for documentation purposes:
In my shader there is a custom RenderType:

SubShader {
    Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="HeightCutout"}

And in the internal depthnormal shader (found in the unity shader package under Builtinshaders/DefaultResourcesExtra/Internal-DepthNormalsTexture.shader I added this:

SubShader {
        Tags { "RenderType"="HeightCutout" }
        // Change culling if needed
        Cull Off
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            struct v2f {
                float4 pos : SV_POSITION;
                float3 worldPos : TEXCOORD0;
                float4 nz : TEXCOORD1;
                UNITY_VERTEX_OUTPUT_STEREO
            };
           
            //your variables
            float CutoutHeight;
           
            v2f vert( appdata_base v ) {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
                o.pos = UnityObjectToClipPos(v.vertex);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                o.nz.xyz = COMPUTE_VIEW_NORMAL;
                o.nz.w = COMPUTE_DEPTH_01;
                return o;
            }
           
            fixed4 frag(v2f i) : SV_Target {
                clip(CutoutHeight - i.worldPos.y);
                return EncodeDepthNormal (i.nz.w, i.nz.xyz);
            }
            ENDCG
        }
    }

After modifying you will need to set the depthnormals texture in the graphics settings (edit->Project settings->Graphics).
3270890--252693--upload_2017-10-30_15-14-35.png

As easy as that. :slight_smile:
Thanks again ben for pointing me in the right direction.

1 Like