- Home /
uv-space to screenSpace in surface shader
Hello, I'm trying to get the screenSpace-Coordinates from a Point in uv-space (for an not-tiling texture).
I found screenPos which seems to give me the position for the current pixel, but I need also the ability to calculate it for other pixels, which correspond to certain points like the center (0.5, 0.5) and so on.
I tried this:
float4 p = mul (UNITY_MATRIX_MVP, float4(IN.uv_MainTex.x, IN.uv_MainTex.y, 0, 1));
p.xy /= p.w;
and expect it to be equivalent to
float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
Which is not the case.
the matrix UNITY_$$anonymous$$ATRIX_$$anonymous$$VP
is the $$anonymous$$odelViewProjection matrix for converting from model-space to projection/clip space (basically, vertex to screen). So multiplying it with "uv-space" will produce the wrong results.
When you say "uv-space" do you mean, a coordinate on the triangle? or a point in the view (which goes from [-1,-1] to [1,1]) into pixel coordinates?
What you might want is
float2 viewCenter = float2( 0, 0 );
float4 screenCenterPx4 = mul( UNITY_$$anonymous$$ATRIX_P, float4( viewCenter.x, viewCenter.y, 0, 1 ) );
float2 screenCenterPx = (screenCenterPx4.xyz / screenCenterPx4.w).xy;
to convert view space to screen coordinates.
Your answer
Follow this Question
Related Questions
Get local position in surface shader 2 Answers
Z value shader 1 Answer
Alpha Diffuse Surface Shader issue 1 Answer
Surface Shader Layer only visible in Sprite Mask view 0 Answers