VertexPositionInputs GetVertexPositionInputs(float3 positionOS)
input.positionWS = TransformObjectToWorld(positionOS);
input.positionVS = TransformWorldToView(input.positionWS);
input.positionCS = TransformWorldToHClip(input.positionWS);
float4 ndc = input.positionCS * 0.5f;
input.positionNDC.xy = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
input.positionNDC.zw = input.positionCS.zw;
NDC is not just
clipSpace.xy / clipSpace.w
. Homogeneous clip space’s x & y have a -w to w range (for what’s in view), but NDC’s x & y have a 0.0 to 1.0 range. They’re essentially screen space UVs. Dividing homogeneous clip space by its w just makes it non-homogeneous clip space, not NDC. NDC is closer to
(clipSpace.xy / clipSpace.w) * 0.5 + 0.5
. So the above code is basically solving that equation a little differently by doing:
(clipSpace.xy * 0.5 + clipSpace.w * 0.5) / clipSpace.w
Only, it’s not doing the divide by w, so it rescales the xy values to a 0.0 to w range.
But why not divide by w?
The key here is that “homogeneous” term. Note the comment for
refers to it as “Homogeneous normalized device coordinates”, and not just “normalized device coordinates”. That’s not a mistake. The term homogeneous here refers to the fact it’s a coordinate in a projective space. Essentially is the value multiplied by w, which for a perspective projection happens to be the linear depth. If you want to dig into exactly what homogeneous coordinates are, be my guest, I honestly still can’t chew through it. But the key thing is having values multiplied by the w allows those values to be correct when being linearly interpolated in a perspective projection space by dividing by w afterwards.
Basically, if you divide by w in the vertex shader, then try to use the value to sample a screen space texture, it won’t line up any more and instead will warp mid triangle. If you’re familiar with non-perspective correct texture mapping, like the original PS1, that’s the kind of thing it’ll look like.
So, if you dig deep enough in the shader code, you’ll find the few places it does actually use that float4 version of the
, it divides by w in the fragment shader, converting the value from homogeneous NDC to “regular” NDC.