Fisheye Pivot Point

Hi guys,

I’m using this standard Fisheye shader (as a camera image effect) with great results.

However it always starts the distortion from the central point of the image. I really need to be able to move/offset that point (center of the distortion) around the screen. Do you have any ideas how to modify the shader in order to accomplish this task? Thanks!

Shader "Hidden/FisheyeShader" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "" {}
    }
 
    // Shader code pasted into all further CGPROGRAM blocks
    CGINCLUDE
 
    #include "UnityCG.cginc"
 
    struct v2f {
        float4 pos : SV_POSITION;
        float2 uv : TEXCOORD0;
    };
 
    sampler2D _MainTex;
 
    float2 intensity;
 
    v2f vert( appdata_img v )
    {
        v2f o;
        o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
        o.uv = v.texcoord.xy;
        return o;
    }
 
    half4 frag(v2f i) : SV_Target
    {
        half2 coords = i.uv;
        coords = (coords - 0.5) * 2.0;

     
        half2 realCoordOffs;
        realCoordOffs.x = (1-coords.y * coords.y) * intensity.y * (coords.x);
        realCoordOffs.y = (1-coords.x * coords.x) * intensity.x * (coords.y);
     
        half4 color = tex2D (_MainTex, i.uv - realCoordOffs);  
     
        return color;
    }

    ENDCG
 
Subshader {
Pass {
      ZTest Always Cull Off ZWrite Off
      Fog { Mode off }   

      CGPROGRAM
      #pragma fragmentoption ARB_precision_hint_fastest
      #pragma vertex vert
      #pragma fragment frag
      ENDCG
  }

}

Fallback off
 
} // shader

change this line:
coords =(coords -0.5)*2.0;
into
coords =(coords -float2(pivotx, pivoty))*2.0; (didnt test it but it should work)

For awesome image effects to use in your game or to learn from, check out TOZ Image Effects for Unity 5.x and Aubergines PostprocessEffects for Unity 4.x & 3.x

1 Like

This indeed works. Thank you so much! I will check the packages you mentioned.