- Home /
Image Effect - Screen coordinates
Hey everyone, I am creating an image effect and I am wondering what is the difference between those ways to calculate coordinates.
half2 uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, v.texcoord);
o.uv = uv;
#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0)
{ uv.y = 1 - uv.y; }
#endif
o.screenPos = uv;
and
o.uv = v.texcoord;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.screenPos = ComputeScreenPos(o.pos);
Thank you !
Answer by zach-r-d · Aug 12, 2015 at 03:45 PM
The first way only works in image effect/screen-space shaders, but is more accurate. Basically, the uv coordinate of a fragment in the screen-space plane will be equal to its normalized viewport coordinate. So that value can be trivially manipulated to get the screen-space coordinate (although UV_STARTS_AT_TOP needs to be accounted for). In that code sample I think to get the screen position in pixel coordinates requires the last line to be "o.screenPos = uv*_ScreenParams.xy" though.
The second way works in any kind of shader, but will not be quite as accurate, and potentially more expensive to compute. It takes a world-space position, multiplies it by the MVP matrix to get it into viewport coordinates, then translates that into pixel coordinates.
tl;dr use the first one when making an image effect, second when making a normal (object) shader.
Thank you very much for your answer. Could you explain your last line a bit more ? o.screenPos = uv *_ScreenParams.xy;
Sure. UV coordinates go from (0,0) to (1,1) whereas screen coordinates go from (0,0) to (screenWidth, screenHeight). So multiplying by _ScreenParams.xy is necessary to get the pixel coordinates.
Your answer
Follow this Question
Related Questions
CommandBuffer's CameraEvent ordering not in sync with shader rendering queue? 1 Answer
How can I make the most efficient SDF Shader in unity 1 Answer
Render everything in black, but the enemies in red 2 Answers
screen flipped upside down [image effect] 0 Answers
Unity - blur on one camera effects the other as well 0 Answers