// TODO: A similar function should be already available in SRP lib on master. Use that instead
float4 ComputeScreenPos(float4 positionCS)
{
float4 o = positionCS * 0.5f;
o.xy = float2(o.x, o.y * _ProjectionParams.x) + o.w;
o.zw = positionCS.zw;
return o;
}
I found this code snippet In Unity shader of URP.
I just want to know the principle of this line [o.xy = float2(o.x, o.y * _ProjectionParams.x) + o.w];
Clip space has a visible range of -w to +w for the x and y. And by w I mean literally the w value of that clip space position. The w is either 1.0 for orthographic projections, or the view (aka “eye”) space depth for perspective cameras. The reason why has to do with how linear projection math and perspective correction works. You can look up homogeneous coordinates if you want to dig into it more. The TLDR is without this, perspective projections would have weird warping problems similar to PS1 graphics. So multiplying the value by 0.5 and then adding the half w changes it from a -w to +w range to a 0.0 to +w range. In the fragment shader, the xy values are divided by w to get your screen space 0.0 to 1.0 screen space UV range.
The _ProjectionParams.x flips the vertical screen position, and exists because Unity does some weird extra stuff to make all APIs act like OpenGL. Which sometimes involves some weird flipping of the screen in various different ways.
understand! thank you! and i didn’t expect meet you here.
It’s very lucky today! Because I’m living in the same era with genius who share his knowledge. it’s you.
I’m your huge fan. whenever i found a thread or post of unity shader that helped me, it was always yours.
Always appreciatng your effort of sharing knowledge!!!