- Home /
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!
Answer by Egad_McDad · May 05, 2020 at 02:09 AM
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.
@Egad_$$anonymous$$cDad Thanks for the reply, I'm not sure to be able to do this.. but I'll try :)
I've put more information about my problem on this thread, in case you're able to address me to some solution! Thanks! https://forum.unity.com/threads/customrendertexture-ignores-computescreenpos.881785
Hey, I popped into that thread, added my own thoughts on how I would do it
Your answer
Follow this Question
Related Questions
Custom Render Texture, globalTexcoord not on range 0 to 1? 0 Answers
Creating a shader with "local" screenspace uv? 0 Answers
How can I convert the texture coordinates of the multi-spite to 0-1? 0 Answers
How do I get mesh data into post-processing shader (or render a mesh shader to a texture)? 0 Answers
How to scale and offset UV coordinates from the center of the texture? 1 Answer