I write my own camera distortion effect like following:
sampler2D _MainTex, _DistortionMap;
struct VertexData {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct Interpolators {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
Interpolators VertexProgram (VertexData v) {
Interpolators i;
i.pos = UnityObjectToClipPos(v.vertex);
i.uv = v.uv;
return i;
}
float4 FragmentProgram (Interpolators i) : SV_Target {
float2 distort = tex2D(_DistortionMap, i.uv).rg;
distort = distort * 2 - 1;
i.uv += distort;
float3 sourceColor = tex2D(_MainTex, i.uv).rgb;
return float4(sourceColor, 1);
}
ENDCG
While this is attached to the camera by
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
Graphics.Blit(src, dest, material);
}
As you can notice I use a texture map which contain the red and green channel to change the x,y position. The map I used is this, which will squeeze everything a little bit to the middle.
However, the result comes out to be
It looks like the image is distorted slice by slice but not pixel by pixel, how to improve it?
Thank you.