Search Unity

Have a depth based post shader kind of working, but need help with blending with scene

Discussion in 'Shaders' started by fivearchers, Apr 22, 2015.

  1. fivearchers

    fivearchers

    Joined:
    Apr 17, 2009
    Posts:
    716
    Below is a falloff post fx shader i'm work on - it works in scene sort of - the far away objects do fall off to blue - however the shader is completely taking over the scene - I'd like it to ideally multiply the color with the existing colors in the scene. How would I do this? (I'm new at post shaders - previously i had built a material shader that does exactly what I want - more or less mapping a gradient to a distance - but I'd like to be able to do it in post, and not have to change every shader in the game)

    Shader "Custom/LightFalloffPost"
    {
    SubShader
    {
    Tags { "RenderType"="Opague" }

    Pass
    {

    CGPROGRAM
    #pragma target 3.0
    #pragma vertex vert
    #pragma fragment frag
    #include "UnityCG.cginc"

    uniform sampler2D _CameraDepthTexture; //the depth texture

    struct v2f
    {
    float4 pos : SV_POSITION;
    float4 projPos : TEXCOORD1; //Screen position of pos
    };

    v2f vert(appdata_base v)
    {
    v2f o;
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    o.projPos = ComputeScreenPos(o.pos);

    return o;
    }

    half4 frag(v2f i) : COLOR
    {

    //Grab the depth value from the depth texture
    //Linear01Depth restricts this value to [0, 1]
    float depth = Linear01Depth (tex2Dproj(_CameraDepthTexture,
    UNITY_PROJ_COORD(i.projPos)).r);

    half4 c;
    c.r = (1-depth)*0.3f;
    c.g = (1-depth)*0.6f;
    c.b = 1-depth;
    c.a = 1-depth;

    return c;
    }

    ENDCG
    }
    }
    FallBack "VertexLit"
    }