Search Unity

Barrel distortion shader

Discussion in 'Shaders' started by rxxxxxx, Apr 4, 2021.

  1. rxxxxxx

    rxxxxxx

    Joined:
    Apr 4, 2021
    Posts:
    1
    Hi,

    I am trying to implement barrel distortion in a vertex shader. I'm using a simplified version of Brown's model.
    Here's my vertex shader code:

    Code (CSharp):
    1.             v2f vert(appdata_full v) {
    2.                 v2f o;
    3.  
    4.                 o.vertex = UnityObjectToClipPos(v.vertex);
    5.  
    6.                 float2 h = o.vertex.xy / o.vertex.w;
    7.              
    8.                 float r = sqrt(h.x * h.x + h.y * h.y);
    9.                 float f = r + _c1 * pow(r, 3) + _c2 * pow(r, 5);
    10.  
    11.                 h = f * _scale * h;
    12.  
    13.                 o.vertex.xy = h.xy * o.vertex.w;
    14.  
    15.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    16.  
    17.                 return o;
    18.             }
    However this doesn’t seem to work properly, as I'm getting something that looks like a pincushion distortion, regardless what values I use for _c1 and _c2.

    Any suggestions welcome, thanks :)