Hi there, I am in need of help to understand how to do the following. In the vertex shader I would like to set the Z position of each vertex, in ScreenSpace, to 10.0f (i.e. at 10f distance along the forward direction from the screen). This means, in abstract terms, that all vertices will be placed at a plane that is parallel to the screen and lays at distance 10.0f from the screen.
Outside shaders, one could do it with the following:
//wPos is the WorldSpace position of the vertex
Vector3 sPos = Camera.main.WorldToScreenPoint(wPos); //sPos is the ScreenSpace position
sPos = new Vector3(sPos.x, sPos.y, 10f); //set the new Z position in ScreenSpace
wPos = Camera.main.ScreenToWorldPoint(sPos); //convert back from Screen to World Space
Now, how to accomplish the samething within a vertex shader?
I tried the following, but it does not work:
VS_INPUT VS_Main(entershader v)
{
VS_INPUT output = (GS_INPUT)0;
v.vertex.z = 10.0f;
output.pos = mul(_Object2World, v.vertex);
return output;
}
Also, this does not work:
VS_INPUT VS_Main(entershader v)
{
VS_INPUT output = (GS_INPUT)0;
output.pos = mul(_Object2World, v.vertex);
output.pos.z = 10.0f;
return output;
}
In both cases, the vertex Z position is set to 10f in WorldSpace, not ScreenSpace. How can I solve this simple issue? I am sure this is has to be simple, but I am struggling with getting it right.
I appreciate any assistance.