About PositionNDC In URP

基础知识 同时被 3 个专栏收录
56 篇文章 0 订阅
87 篇文章 1 订阅
47 篇文章 0 订阅

VertexPositionInputs GetVertexPositionInputs(float3 positionOS)
    VertexPositionInputs input;
    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;

    return input;

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:

Code (csharp):

  1. (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.

From:Confused on NDC space - Unity Forum

  • 0
  • 0
  • 0
  • 一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
钱包余额 0