#Writing custom depth pass using Shader Graph?

1 messages · Page 1 of 1 (latest)

ember lintel
#

Is it possible to overwrite a camera depth texture using a custom pre baked depth pass texture inside shader graph?
I have achieved to do this already using HLSL & a renderer feature to queue it with the shader & baked depth texture as input. It is working fine. It basically injects the texture before opaque objects are rendered. For context I am trying to make a pre rendered background setup.
Was just curious if I can do the same in Shader Graph? A bit unfamiliar with most of its nodes.

true shard
#

that depends entirely on what your shader is doing!

ember lintel
# true shard that depends entirely on what your shader is doing!

[Shader]

  • the input shader takes is a texture 2d called "_DepthTex". (And we assign a pre baked Raw depth texture here)
  • we set clip space
  • then we take red channel of _DepthTex, i say it should be used for depth buffer and return it.
    [Render Feature]
  • we take this shader & create a material
  • use its output and write it to cameras depth texture using render feature, making sure we queue it before opaques are drawn
#
{
    Properties
    {
        _DepthTex("Depth", 2D) = "black" {}
    }
    SubShader
    {
        Tags { "RenderPipeline" = "UniversalPipeline" }
        Pass
        {
            Name "InjectDepth"
            ZTest Always ZWrite On Cull Off

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv : TEXCOORD0;
            };

            TEXTURE2D(_DepthTex); SAMPLER(sampler_DepthTex);

            Varyings vert(uint vertexID : SV_VertexID)
            {
                Varyings output;
                float2 uv = float2((vertexID << 1) & 2, vertexID & 2);
                output.positionCS = float4(uv * 2.0 - 1.0, UNITY_NEAR_CLIP_VALUE, 1.0);

                #if UNITY_UV_STARTS_AT_TOP
                    output.positionCS.y *= -1;
                #endif

                output.uv = uv;
                return output;

            }

            struct FragmentOutput
            {      
                float depth : SV_Depth;
            };

            FragmentOutput frag(Varyings input)
            {
                FragmentOutput outData;
                float d = SAMPLE_TEXTURE2D(_DepthTex, sampler_DepthTex, input.uv).r;
                outData.depth = d;
                return outData;
            }
            ENDHLSL

        }

    }

}```
#

this works perfectly fine on Unity Editor but its an experiment and i wish to know more about shader graph if its possible to recreate this shader.

meager wren
ember lintel
#

also big fan