- Home /
Question by
nguyenhuumanh · Nov 11, 2020 at 10:54 AM ·
depth-buffer
Manually calculate depth buffer value?
I want to manually calculate the z value that will be stored in the depth buffer using the world space position in the fragment shader. It must be as if Unity is calculating the depth from for me.
What I have seems to work so far, but I am not 100% sure it is correct.
float depthToZVal(float depth) {
return (1.0 / depth - _ZBufferParams.w) / _ZBufferParams.z;
}
fragout frag(g2f i) {
float4 worldPos = // calculate worldPos from param i
g2f o;
float4 viewPos = mul(UNITY_MATRIX_V, worldPos);
o.depth = depthToZVal(-viewPos.z);
o.color = // calculate color
return o;
}
Is my approach correct?
Comment