CustomRenderTexture ignores "ComputeScreenPos"

Hi everyone!

I wrote a pretty simple shader, it draws an ellipse based on given uv screen coordinates of the mouse position.

It works well in a material applied to a gameObject, the coordinates are updated via script following the input screen position and the ellipse is displayed in a right way.

Now the problem

I’ve created a CustomRenderTexture, applied a material with that shader and I’ve noticed that the ellipse is now distorted since the screen position computated by ComputeScreenPos(o.vertex) is now converted in texture’s uv space.

So for example, the bottom of the screen coordinates (0,0)screen are converted to (0,0) local uv space [the ellipse in the left bottom of the texture], another example the center of the screen let say (540,960) are now (0.5,0.5) [the ellipse is now in the middle of the texture not of the screen!]

I would like to know if CustomRenderTexture can read screen space and how.

Let me know if you need more details

Thank you!

Hey @Voodoomedia , the best way to accomplish what you are trying to do is to first calculate the clip space in the vertex shader and then pass that value to the fragment shader. Once there, you can divide the clipspace.x by your screen width and clipspace.y by your screen height.

The reason this works is because clip space will give you the position of where that fragment/pixel is on the screen. One thing to note is that clip space is different from platform to platform, so for a more robust calculation you should look into ComputeScreenPos() (its near the bottom of the page). I couldn’t get that function working myself, but its there.