depth shader : Invert it

Hi there, I am using the supplied Shader for depth:

Shader "Hidden/Render Depth" {
SubShader {
    Tags { "RenderType"="Opaque" }
    Pass {
        Fog { Mode Off }
       
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

struct v2f {
    float4 pos : POSITION;
    float depth : TEXCOORD0;
};

v2f vert( appdata_base v ) {
    v2f o;
    o.pos = mul(glstate.matrix.mvp, v.vertex);
    TRANSFER_EYEDEPTH(o.depth);
    return o;
}

half4 frag(v2f i) : COLOR {
    OUTPUT_EYEDEPTH(i.depth);
}

ENDCG

    }
}

Fallback Off

}

I’m stuck at inverting the effect to get white near, and black far.

please help me!! I’m so stuck at shader things!!

Once done will post the end result which should be amazing. :smile:

Ok, here’s a slightly more generic shader: it fades from one color at near plane to another color at far plane. Tweak the colors as you want in the material.

Shader "Fade Two Colors" {
Properties {
    _ColorNear ("Color Near", Color) = (0,0,0,0)
    _ColorFar ("Color Far", Color) = (1,1,1,1)
}
SubShader {

    Pass {
CGPROGRAM
#pragma vertex vert
#include "UnityCG.cginc"

struct appdata {
    float4 vertex : POSITION;
};
struct v2f {
    float4 pos : POSITION;
    float4 color : COLOR;
};

uniform float4 _ColorNear;
uniform float4 _ColorFar;

v2f vert(appdata v) {
    v2f o;
    o.pos = mul(glstate.matrix.mvp, v.vertex);
    float depth;
    COMPUTE_EYEDEPTH(depth);
    float factor = depth * _ProjectionParams.w;
    o.color = lerp(_ColorNear, _ColorFar, factor);
    return o;
}
ENDCG

        SetTexture[_MainTex] { combine primary }
    }
} 
}