- Home /
What do these lines in the refraction shader actually do? (UV calculation)
Hello. I've been studying the refraction shader and I am not exactly sure what the following lines do :
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
this value is later used in
half4 col = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
I am assuming that those lines calculate the position of the vertex on the Grab Texture, transforming it back into projection (or view? not sure) space. So getting the x and y position, then using the w property, which stores projection displacement, to get the vertex back to the near clipping plane, then multiplying by 0.5 to convert it from a coordinate system with it's origin in the center to the screen space coordinate system?
It would be amazing if somebody could clear this up for me, since everyone seems to just use this and never comment on what exactly it does. (probably because it's really basic...)
Thanks in advance!
Ok so you've picked my interest. Please chime in if you figure it out, this is killing me lol.
To get the projected space, you would divide by w. Simply adding the w component is very strange, it does not seem to have any mathematical sense what-so-ever. I'm guessing this is a "trick" to achieve a visual effect at low cost.
Thats all for now, I can't wrap my head around that manipulation -___-
I've looked into the UnityCG.cginc file after finding out that you can substitute that line for ComputeGrabScreenPos(float4 pos)
This is defined as follows (together with ComputeScreenPos, sort of relevant)
// Projected screen position helpers
#define V2F_SCREEN_TYPE float4
inline float4 ComputeScreenPos (float4 pos) {
float4 o = pos * 0.5f;
#if defined(UNITY_HALF_TEXEL_OFFSET)
o.xy = float2(o.x, o.y*_ProjectionParams.x) + o.w * _ScreenParams.zw;
#else
o.xy = float2(o.x, o.y*_ProjectionParams.x) + o.w;
#endif
o.zw = pos.zw;
return o;
}
inline float4 ComputeGrabScreenPos (float4 pos) {
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
float4 o = pos * 0.5f;
o.xy = float2(o.x, o.y*scale) + o.w;
o.zw = pos.zw;
return o;
}
Note that ComputeScreenPos does pretty much the same thing (apart from taking into account the texel offset define and not flipping,the image) So appearently it is really a world position to screen position calculation, but I still have no clue why it works the way it does.
Your answer
![](https://koobas.hobune.stream/wayback/20220612103351im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Reuse Shader Pass output as input for next Pass 0 Answers
Distort shader not showing sprites 2 Answers
how to change progection matrix for some objects 0 Answers
Painted Vertices are Blockly, Looking to smooth them Out 1 Answer
Cubemap Projection Shader 0 Answers