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


