- Home /
GrabPass twirl shader not centering correctly
I'm trying to create a shader that I can place on a material and on for example a quad, so that when I look through it, I see a twirled version of what is behind the quad. However, it doesn't seem to center correctly as can be seen in the appended photos.
The only difference between these two pictures is that I zoomed in for the second one, which I don't want to affect the center. I want the center of the twirl to always be in the center of the quad but I guess I'm not really understanding what coordinates to use and for what (I tried using unity_ObjectToWorld[3] with similar bad results). Here is the current code:`v2f vert (appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = ComputeGrabScreenPos(o.pos); return o; }
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float _Factor;
float4 _Center;
half4 frag (v2f i) : SV_Target
{
float centerX = _GrabTexture_TexelSize.z/2;
float centerY = _GrabTexture_TexelSize.w/2;
half4 pixelCol = half4(0, 0, 0, 0);
float dx = i.uv.x - centerX;
float dy = i.uv.y - centerY;
float p = _Factor;
half rmax = _GrabTexture_TexelSize.z/2;
half r = sqrt(pow(dx, 2) + pow(dy, 2));
half z = sqrt(pow(rmax, 2) - pow(r, 2));
half beta = atan2(dy, dx) + _Factor * (rmax-r)/2;
float x = i.uv.x;
float y = i.uv.y;
if(r <= rmax) {
x = centerX + r * cos(beta);
y = centerY + r * sin(beta);
}
pixelCol = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(x, y, i.uv.z, i.uv.w)));
return pixelCol;
}`
Any help would be greatly appreciated and please let me know if you feel something is missing from the description!
Your answer
![](https://koobas.hobune.stream/wayback/20220612141456im_/https://answers.unity.com/themes/thub/images/avi.jpg)