GrabPass for local distortion effect problem
Hello, I'm quite new to shaders and currently I want to use GrabPass to create a local distortion effect in my game but I faced a very strange behavior. Distortion is camera-dependent and not object-dependent. I don't know whether it makes any sense, so here is a video demonstration. I want to have the same effect while moving the object with distortion material when my camera doesn't move. What's wrong with my shader? Here is the source code:
Shader "Custom/LocalDistortionShader"
{
Properties
{
_MainTex ("Main Texture", 2D) = "" {}
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
}
Cull Off
Lighting Off
ZWrite Off
GrabPass
{
"_BackgroundTexture"
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 grabPos : TEXCOORD0;
};
v2f vert (appdata IN)
{
v2f OUT;
OUT.pos = UnityObjectToClipPos(IN.vertex);
OUT.grabPos = ComputeGrabScreenPos(OUT.pos);
return OUT;
}
sampler2D _BackgroundTexture;
fixed4 frag (v2f IN) : SV_Target
{
float2 fromCenterVec = float2(IN.grabPos.x - 0.5f, IN.grabPos.y - 0.5f);
float distToCenterSqr = dot(fromCenterVec, fromCenterVec);
float offset = smoothstep(2, 100 , 1 / distToCenterSqr);
fixed4 col = tex2D(_BackgroundTexture, IN.grabPos + fromCenterVec * offset);
return col;
}
ENDCG
}
}
}
Your answer
Follow this Question
Related Questions
How to set a render-to-texture camera to be a VR camera 0 Answers
How can I apply a 9-slice scaling to a mash object using a shader? 0 Answers
Get a Color from the Sprite Renderer in the Shader Graph using a Vector Sprite. Unity 2019.4.0f1. 1 Answer
Shadows on alpha clipping/vertex offset shaders misaligned when using screen space texture 0 Answers
My camera is making like straight vertical pixel gaps when is moving 0 Answers