Skip to main content
Fixing copy-paste error
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401
Pass
{
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma target 3.0
    #include "UnityCG.cginc"
                    
    struct appdata
    {
        float4 vertex : POSITION;
    };

    sampler2D _MainTex;
    // Request camera depth buffer as a texture.
    // Incurs extra cost in forward rendering, "just there" in deferred.
    sampler2D _CameraDepthTexture;
        
    void vert (
        float4 vertex : POSITION,
        out float4 outpos : SV_POSITION)
    {
        outpos = UnityObjectToClipPos(v.vertex);
    }
        
    fixed4 frag (UNITY_VPOS_TYPE screenPos : VPOS) : SV_Target
    {
        // Convert pixel coordinates into screen UVs.
        float2 uv = screenPos.xy * (_ScreenParams.zw - 1.0f);
        // Depending on setup/platform, you may need to invert uv.y
    
        // Sample depth buffer, linearized into the 0...1 range.
        float depth = Linear01Depth(
              UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, uv)));

        // Compressing the range, so we get more colour 
        // variation close to the camera.
        depth = saturate(2.0f * depth);
        depth = 1.0f - depth;
        depth *= depth;             
        depth = 1.0f - depth;               

        // Use depth value as a lookup into a colour
        // ramp texture of your choosing.
        fixed4 colour = tex2D(_MainTex, depth);
                            
        return colour;
    }
    ENDCG
}
Pass
{
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma target 3.0
    #include "UnityCG.cginc"
                    
    struct appdata
    {
        float4 vertex : POSITION;
    };

    sampler2D _MainTex;
    // Request camera depth buffer as a texture.
    // Incurs extra cost in forward rendering, "just there" in deferred.
    sampler2D _CameraDepthTexture;
        
    void vert (
        float4 vertex : POSITION,
        out float4 outpos : SV_POSITION)
    {
        outpos = UnityObjectToClipPos(v.vertex);
    }
        
    fixed4 frag (UNITY_VPOS_TYPE screenPos : VPOS) : SV_Target
    {
        // Convert pixel coordinates into screen UVs.
        float2 uv = screenPos.xy * (_ScreenParams.zw - 1.0f);
        // Depending on setup/platform, you may need to invert uv.y
    
        // Sample depth buffer, linearized into the 0...1 range.
        float depth = Linear01Depth(
              UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, uv)));

        // Compressing the range, so we get more colour 
        // variation close to the camera.
        depth = saturate(2.0f * depth);
        depth = 1.0f - depth;
        depth *= depth;             
        depth = 1.0f - depth;               

        // Use depth value as a lookup into a colour
        // ramp texture of your choosing.
        fixed4 colour = tex2D(_MainTex, depth);
                            
        return colour;
    }
    ENDCG
}
Pass
{
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma target 3.0
    #include "UnityCG.cginc"
                    
    struct appdata
    {
        float4 vertex : POSITION;
    };

    sampler2D _MainTex;
    // Request camera depth buffer as a texture.
    // Incurs extra cost in forward rendering, "just there" in deferred.
    sampler2D _CameraDepthTexture;
        
    void vert (
        float4 vertex : POSITION,
        out float4 outpos : SV_POSITION)
    {
        outpos = UnityObjectToClipPos(vertex);
    }
        
    fixed4 frag (UNITY_VPOS_TYPE screenPos : VPOS) : SV_Target
    {
        // Convert pixel coordinates into screen UVs.
        float2 uv = screenPos.xy * (_ScreenParams.zw - 1.0f);
        // Depending on setup/platform, you may need to invert uv.y
    
        // Sample depth buffer, linearized into the 0...1 range.
        float depth = Linear01Depth(
              UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, uv)));

        // Compressing the range, so we get more colour 
        // variation close to the camera.
        depth = saturate(2.0f * depth);
        depth = 1.0f - depth;
        depth *= depth;             
        depth = 1.0f - depth;               

        // Use depth value as a lookup into a colour
        // ramp texture of your choosing.
        fixed4 colour = tex2D(_MainTex, depth);
                            
        return colour;
    }
    ENDCG
}
Adding another example of what can be done with this kind of shader.
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Here's me playing around with this a bit more... Animation of a green sonar wave passing over a dark scene and illuminating it

Here's me playing around with this a bit more... Animation of a green sonar wave passing over a dark scene and illuminating it

Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Here's a simple version which should work both as a fullscreen post effect or as an object in the world, like a magic lens you can look through.

Animation showing a camera panning in & out of a scene with a sliding window rendering the scene's depth in a white-yellow-red-purple spectrum

Pass
{
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma target 3.0
    #include "UnityCG.cginc"
                    
    struct appdata
    {
        float4 vertex : POSITION;
    };

    sampler2D _MainTex;
    // Request camera depth buffer as a texture.
    // Incurs extra cost in forward rendering, "just there" in deferred.
    sampler2D _CameraDepthTexture;
        
    void vert (
        float4 vertex : POSITION,
        out float4 outpos : SV_POSITION)
    {
        outpos = UnityObjectToClipPos(v.vertex);
    }
        
    fixed4 frag (UNITY_VPOS_TYPE screenPos : VPOS) : SV_Target
    {
        // Convert pixel coordinates into screen UVs.
        float2 uv = screenPos.xy * (_ScreenParams.zw - 1.0f);
        // Depending on setup/platform, you may need to invert uv.y
    
        // Sample depth buffer, linearized into the 0...1 range.
        float depth = Linear01Depth(
              UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, uv)));

        // Compressing the range, so we get more colour 
        // variation close to the camera.
        depth = saturate(2.0f * depth);
        depth = 1.0f - depth;
        depth *= depth;             
        depth = 1.0f - depth;               

        // Use depth value as a lookup into a colour
        // ramp texture of your choosing.
        fixed4 colour = tex2D(_MainTex, depth);
                            
        return colour;
    }
    ENDCG
}

Here I'm using the VPOS semantic to simplify calculating the position of the fragment on the screen. If you need to support shader models below 3 there are other ways to do this, they're just a little messier. ;)