Advertisement Jump to content
Sign in to follow this  

World Space to Screen Space Conversions

This topic is 2237 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I'm trying to work with transformed quads, that is to say, quads that have there vertices defined in screen coordinates. Basically im trying to shift those quads to different points on the screen. The trouble is, those points are defined in world coordinates.

I'm attempting to write a shader that will accept transformed vertices (from a 2D quad), and shift those vertices to a point on the screen (lets call the point 'p') which is defined in world coordinates. Point 'p' is passed to the shader along with the quad vertex.

So basically, i need to convert my point 'p' to screen coordinates using the shader, and then translate my quad coordinates (defined in screen space) to 'p'.

I have a base quad that has the vertex coordinates: (0,1,0,1), (0,-1,0,1), (1,-1,0,1), (1,1,0,1). I then scale and shift the quad in the shader as appropriate. For this example, i scaled the x component of the vertices by 100, and the y component by 2. So you basically end up with a 4pixel high quad x 100pixels wide. The quad is built around the screen origin (top-left corner) so all i have to do is shift all the vertices is the quad by 'p' to place the quad in the right spot.

I can get my quads to move around to roughly the right relative positions from each other, but not the correct positions on the screen. It seems the magnitude of the translations is not right (only guessing).

Main shader code is posted below. Any help would be much appreciated.

struct VSOutput
float4 Pos_ws;
float4 Pos_ps;
float4 Color;

VSOutput ComputeVSOutput2DShift(float4 position, float4 col, float4 p)

VSOutput vout;

// convert point to clip coordinates
float4 pCLIP = mul(p, WVP);

// convert point to screen space
float3 pNDC = / pCLIP.w;

// adjust height
position.y *= 2;

// adjust width
position.x *= 100;

// convert vertex to screen space
position.xy /= ViewportSize;
position.xy *= float2(2, -2);
position.xy -= float2(1, -1);

// translate vertex
position.x += pNDC.x;
position.y += pNDC.y;

// set output data
vout.Pos_ps = position;
vout.Color = col;
return vout;

Edited by porters

Share this post

Link to post
Share on other sites
Well to anyone who was interested, I figured out what the problem was. I just needed to make a minor adjustment to my translate vertex code (see below). The problem was that i had not taken into account that the origin of the NDC system is the centre of the viewport. Since the NDC system ranges from -1 to 1, i just had to add/subtract one in the vertex translation code.

// translate vertex
position.x += spNDC.x + 1;
position.y += spNDC.y - 1;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!