Aliasing issues with a depth-buffer color correction

Hi there !

In our project we’re using a color correction that takes the depth buffer and multiply it by a ramp before applying the result to the final image. It allows us to control the overall color of the scene depending on how far the objects are from the camera.
But we’re experiencing some aliasing issues when there’s a high contrast between two objects in the depth buffer.


You can see the strange red borders on the two objects in the center of the image (the red comes from the lava river).


Here’s the depth buffer after using a pow function to tune it.


And this is the result when we use the gradient texture to remap the depth buffer into a color that will be multiplied to the camera render. We can clearly see the odd white aliasing around some objects. I can understand the aliasing issue on high contrasts, but I can’t see why it’s white instead of being of the color of the object in front/back.

Here’s the code of the shader :

Shader "Hidden/Depth Color Correction Effect" {
Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _RampNear ("Near Ramp", 2D) = "grayscaleRamp" {}
    _Exponent ("Exponent", float) = 100
    _Saturation ("Saturation", float) = 0.64
}

SubShader {
    Pass {
        ZTest Always Cull Off ZWrite Off
        Fog { Mode off }

CGPROGRAM
    #pragma vertex vert_img
    #pragma fragment frag
    #pragma fragmentoption ARB_precision_hint_fastest
    #include "UnityCG.cginc"

    uniform sampler2D _MainTex;
    uniform sampler2D _RampNear;
    uniform sampler2D _CameraDepthTexture;
    uniform float _Exponent;
    uniform float _Saturation;

    fixed4 frag (v2f_img i) : COLOR
    {
        //sample depth and raise to exponent
        fixed4 depth = pow((UNITY_SAMPLE_DEPTH( tex2D (_CameraDepthTexture, i.uv ) )) , _Exponent);
        //return depth ;
        fixed4 orig = tex2D(_MainTex, i.uv);
        //remap depth with gradient
        fixed rr = tex2D(_RampNear, depth.rr).r + 0.00001; // numbers to workaround Cg's bug at D3D code generation :(
        fixed gg = tex2D(_RampNear, depth.gg).g + 0.00002;
        fixed bb = tex2D(_RampNear, depth.bb).b + 0.00003;
        fixed4 depthremap = fixed4(rr, gg, bb, orig.a);
        return depthremap ;

        //multiply depth with image
        fixed4 color = (orig * depthremap);

        //saturation
        fixed4 lum = Luminance(color.rgb);
        return lerp(lum, color, _Saturation);
    }
ENDCG
    }
}

Fallback off

}

Thanks in advance !
Robin

Hello Robin!
3 years late) I probably have the same problem. I tried many ways to solve the problem, but I still have not found a solution. Have you found a way to fix this bug?

Not a bug per se, but rather an unexpected consequence of using a texture as UVs for another texture.

Short version: Disable mip maps on your ramp texture.

Long version: Real time rendering determines the mip map of a texture by using the pixel derivatives of the UVs. That’s a fancy way of saying how much the UVs change between one on screen pixel and the one beside it. When the value doesn’t change much, this means it should use a lower mip level, and when the value changes a lot it should use a higher mip. When you’re using a texture like a depth texture as the UVs, sometimes the values between two pixels change drastically, and the GPU thinks it should use a higher mip level. You can override that by supplying your own derivatives with tex2Dgrad, or choosing the mip level manually with tex2Dlod, or just disabling the mip maps on the texture if they’re not needed.

Thank you for your answer. If I understand correctly, the problem is only related to the mip-map.
I tried the “Short version: Disable mip maps on your ramp texture” for the experiment.
Result: nothing has changed.

I tried to use a shader without texture, result has not changed.
Are there any other ideas or am I doing something wrong?

If my solution didn’t work, then your problem is different than the one described in the post above. Can you post pictures of the issue you’re having? And can you describe your setup more?

I created a new thread: Screen space fog aliasing issues