Hi. I have a compute shader that takes GameObject’s world position ,and I want to get the screen space position of it for further operations. (Suppose there’s only 1 camera in the scene)
However I find it frustrating to do it.
some includes
...
RWStructuredBuffer<float4> _TestData;
float4 _WorldPos;
void CalculateScreenSpacePosition()
{
float4 clipPos = ComputeClipSpacePosition(_WorldPos, UNITY_MATRIX_VP);
_TestData[0] = clipPos;
float3 ndcPos = ComputeNormalizedDeviceCoordinatesWithZ(_WorldPos, UNITY_MATRIX_VP);
_TestData[1] = float4(ndcPos, 1);
float4 screenPos = ComputeScreenPos(clipPos, _ProjectionParams.x);
_TestData[2] = screenPos;
}
[numthreads(1,1,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
CalculateScreenSpacePosition();
}
I did some tests and use a vector buffer to retrieve the results .
However the positions don’t look correct.

the elements in the array of the pic: clip pos, ndc pos, screen pos
And I try to get the native matrices it doesn’t feel very correct.

_MatrixData[0] = UNITY_MATRIX_V;
Above is the UNITY_MATRIX_V matrix I got from compute shader. It sort of lacks of position infomrations.
However I manually log the Camera’s worldToCameraMatrix in c# code, it gives correct result with positions informations.
I can’t find any related resources or guide in google/GPT/source code in HDRP to go further.
So I need help with how to translate a world pos into screen space pos correctly in compute shader.