- Home /
Plane depth in shader
Hello,
I am trying to clip mesh by plane. To fill clipped pixels I want to rewrite depth buffer by this plane's distance. But I have troubles to calculate right depth for each pixel.
White area it is my computation for depth, versus how it should be on real plane (with N = up, O = zero). Right cube is just primitive to show that is depth render.
half4 frag(g2f IN, out float outDepth : SV_Depth) : COLOR
{
half4 solid = _Color;
bool clipIt = IN.worldPos.y > 0;
float3 normal = IN.normal;
float z = IN.pos.w; // Good Depth
if (clipIt)
{
normal = normalize(_ClipMatrix[1].xyz); // Up vector from Mat
float3 pN, pO, rd, ro;
pN = float4(0, 1, 0, 0.0); // Plane Normal - UP
pO = float4(0, 0, 0, 1.0); // Plane Origin - Zero
ro = float4(_WorldSpaceCameraPos, 1); // Ray Origin - Cam
rd = normalize(IN.worldPos - _WorldSpaceCameraPos) * z; // Ray Direction
float dpth = dot(pN, pO - ro) / dot(pN, rd); // Calc depth
z = z* dpth;
}
//outDepth = (1.0 - z * _ZBufferParams.w) / (z * _ZBufferParams.z); // Good Depth writing
z /= 40;
z -= frac(z * 128) / 128;
solid = float4(z, z, z, 1); // Visualize depth
return solid;
}
How I have to compute depth, to achieve same values in left cube like in a floor plane?
I was trying to transfer all points and vectors to projection coordinates, but it didn't succeed.
editor.png
(35.0 kB)
Comment