Proper way to use View.TranslatedWorldToClip in custom shader code node

Hello everyone. I want to convert world-space object position into screen-space position in the material editor. I’m following [SOLVED]Object Screen Position Within Material Editor - #6 by IronicParadox to do this.

I added a custom shader code node “PositionWSToSS”, and use it like the following.

The content of “PositionWSToSS” is as follows:

float4 Clip = mul(float4(PositionWS.rgb,1),View.TranslatedWorldToClip);
Clip = Clip.rgba / Clip.a; 
return Clip;

I expected the output of “PositionWSToSS” to be a float4 where x,y,z are each in the range of [-1,1].
To my surprise, I got the following:

What am I doing wrong here?

Hey, does this suffice?

return float2(ScreenCoords.x, 1.0 - ScreenCoords.y);


OR

You can change the ViewportUV to PixelPosition to get the Screen pixel coordinates of the world actor.

Hi, I’m afraid this doesn’t suffice, since ScreenPosition is different for each vertex/pixel. What I’m aiming to get is the result of converting ObjectPosition to screen-space, and ObjectPosition is the same for all vertices/pixels.

For anyone still wondering, I’ve got it.

The content of PositionVSToSS is as follows:

float4 ClipSpace = mul(float4(ViewSpaceCoords.rgb,1),View.ViewToClip);
ClipSpace = ClipSpace.rgba / ClipSpace.a; //normalize
float2 ScreenSpace = ClipSpace.rg; 
ScreenSpace.xy = ScreenSpace.xy * 0.5 + 0.5;
ScreenSpace.y = 1 - ScreenSpace.y;
return ScreenSpace;
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.