0
\$\begingroup\$

I forgot to write this C# code to enable depth from my camera:

private void OnEnable()
{
    GetComponent<Camera>().depthTextureMode = DepthTextureMode.Depth;
}

Also, I specified camera render path is Forward. However, my shader below still works and the depth from _CameraDepthTexture is correct. All of this I tested in PC, I'm confused why it's working.

Properties
{
    _TopColor("TopColor",Color) = (1,1,1,1)
    _BottomColor("BottomColor",Color) = (1,1,1,1)

    _NoiseTex("NoiseTex", 2D) = "white" {}
    _NoiseScale("Noise Scale", Range(0.01,2)) = 1
    _Height("Height",float) = 1
    _Speed("Speed",float) = 1
    _DepthBiasFactor("DepthBiasFactor",float) =1
    _DcurvatureRadius("DcurvatureRadius",float) = 30
}
SubShader
{
    Tags { "RenderType"="Transparent" "Queue" = "Transparent" "IgnoreProjector" = "True"}
    LOD 100
    
    ZWrite On
    ZTest On
    Blend SrcAlpha OneMinusSrcAlpha

    Pass
    {
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        // make fog work
        #pragma multi_compile_fog

        #include "UnityCG.cginc"
        #include "Lighting.cginc"

        struct appdata
        {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
            float3 normal : NORMAL;

        };

        struct v2f
        {
            float2 uv : TEXCOORD0;
            float2 CloudUV01 : TEXCOORD2;
            float2 CloudUV02 : TEXCOORD3;
            float4 worldPos : TEXCOORD4;
            float2 noisePos:TEXCOORD5;
            float4 screenPos : TEXCOORD6;
            float3 worldNormal : TEXCOORD7;
            UNITY_FOG_COORDS(1)
            float4 vertex : SV_POSITION;
        };

        
        float4 _TopColor;
        float4 _BottomColor;
        sampler2D _NoiseTex;
        float _Height;
        float _Speed;
        float _DepthBiasFactor;
        sampler2D _CameraDepthTexture;
        float _DcurvatureRadius;
        float _NoiseScale;

        v2f vert (appdata v)
        {
            v2f o;
            o.vertex = UnityObjectToClipPos(v.vertex);

            o.CloudUV01 = v.uv + _Time.x * _Speed;
            o.CloudUV02 = v.uv - _Time.x *_Speed;
            o.noisePos.x = tex2Dlod(_NoiseTex, float4(o.CloudUV01,0,0)).r * _NoiseScale;
            o.noisePos.y = tex2Dlod(_NoiseTex, float4(o.CloudUV02,0,0)).r * _NoiseScale;
            o.worldPos = mul(unity_ObjectToWorld, v.vertex);
            o.worldNormal = mul(v.normal,(float3x3)unity_WorldToObject);

            o.vertex.y += o.noisePos.x * o.noisePos.y * _Height;

            o.vertex.y -= pow(distance(float2(0,0), o.worldPos.xz) / _DcurvatureRadius, 3);

            o.screenPos = ComputeScreenPos(o.vertex);

            UNITY_TRANSFER_FOG(o,o.vertex);
            return o;
        }

        fixed4 frag (v2f i) : SV_Target
        {
            float3 worldNormal = normalize(i.worldNormal);
            float3 worldLight = normalize(_WorldSpaceLightPos0.xyz);

            float4 depthSample = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, i.screenPos);
            float depth = LinearEyeDepth(depthSample);
            float borderLine = saturate((depth - i.screenPos.w) / 2 - _DepthBiasFactor);

            float mask = saturate(i.noisePos.x * i.noisePos.y);

            float4 col = _TopColor * mask + _BottomColor * (1 - mask);
            
            col.a = borderLine;

            float3 diffuse = _LightColor0.rgb * col.rgb * (dot(worldNormal,worldLight) * 0.5 + 0.5);
            
            // apply fog
            UNITY_APPLY_FOG(i.fogCoord, diffuse);
            return float4(diffuse,col.a);
            // return float4(borderLine,borderLine,borderLine,1);
        }
        ENDCG
    }
}
\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

I changed the platform to Android and the _CameraDepthTexture can't be fetched anymore if I failed to specify the camera's depth mode by code.

So, on the PC platform, no matter what render path, it seems there is no need to specify the camera's depth mode, but on other platforms, depending on the render path, the C# code might be needed.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .