summaryrefslogtreecommitdiffstats
path: root/DepthBlit.shader
blob: b2a512fc0a1a75f55c7b6ca8cb329c82f4ccd0fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Shader "Hidden/yum_food/DepthBlit"
{
    // Reads from depth texture and outputs linearized depth to color
    Properties
    {
        _MainTex ("", 2D) = "white" {}
        _Near ("Near Plane", Float) = 0.01
        _Far ("Far Plane", Float) = 1.0
    }

    SubShader
    {
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            UNITY_DECLARE_DEPTH_TEXTURE(_DepthTex);
            float _Near;
            float _Far;

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
            };

            v2f vert(float4 vertex : POSITION, float2 uv : TEXCOORD0)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(vertex);
                o.uv = uv;
                return o;
            }

            float4 frag(v2f i) : SV_Target
            {
                // 0 = near clip plane, 1 = far clip plane.
                float rawDepth = SAMPLE_DEPTH_TEXTURE(_DepthTex, i.uv);
                // Unity uses reversed-Z on most platforms: 1 at near, 0 at far.
                // Flip to get linear 0-1 (0=near, 1=far) for orthographic cameras.
                #if defined(UNITY_REVERSED_Z)
                    return 1.0 - rawDepth;
                #else
                    return rawDepth;
                #endif
            }
            ENDCG
        }
    }
}