Search Unity

Distortion Image Effect.

Discussion in 'Shaders' started by DeKac, Oct 22, 2018.

  1. DeKac

    DeKac

    Joined:
    Mar 1, 2018
    Posts:
    8
    Hello! I am completely new to shaders, been following a couple of tutorials and ran into a problem that I can't get my head around, so I hope someone is able to explain this to me.


    Code (CSharp):
    1.             float4 frag(v2f i) : SV_Target
    2.             {
    3.                 float2 disp = tex2D(_Noise, i.uv).rg;
    4.                 disp = ((disp * 2) - 1) * _Strength;
    5.                 float4 col = tex2D(_MainTex, i.uv + disp);
    6.                 return col;
    7.             }
    This is a simple shader, that takes a Noise texture, picks it's red and green values (that would control x and y), converts them to -1;1 values and then adds them to the MainTexture thus distorting it. This part works great and everything is fine. The problem happens then I start to experiment with the code. Let's say I want to distort the image by 0.1 in both x and y axis. So I wrote this :

    Code (CSharp):
    1.                 i.uv.x += 0.1;
    2.                 i.uv.y += 0.1;
    3.                 float4 col = tex2D(_MainTex, i.uv);
    and it works fine. I can see my image is distorted in both axis. So I took one step further and tried to write the code like this:

    Code (CSharp):
    1.                 float2 disp = (0.1, 0.1);
    2.                 float4 col = tex2D(_MainTex, i.uv + disp);
    3.                 return col;
    This doesn't change the result, so it's still the same. However, I found that now the first number ( that I thought was controlling x axis ) does not do anything. This is where I don't understand why. Now both axis are always the same and both are dependent on the second number. So this:

    Code (CSharp):
    1. float2 disp = (0.0, 0.2);
    is distorting both x and y axis by 0.2 for some reason.

    Thank you very much for any kind of explanation.
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    You need a constructor...

    float2 disp = float2(0.0,0.2);