Search Unity

Question 2D sway shader, with an offset

Discussion in 'Shaders' started by Yamski, Nov 19, 2020.

  1. Yamski

    Yamski

    Joined:
    Sep 7, 2019
    Posts:
    5
    My result:
    https://www.reddit.com/r/Unity3D/co.../?utm_source=share&utm_medium=web2x&context=3

    My code:

    fixed4 frag(v2f IN) : SV_Target{

    float offset = _Offset;

    fixed4 color;

    float validTime = _Time * _WaveSpeed;



    if (IN.texcoord.y > offset){

    float v1 = IN.texcoord.y / _WaveLength + _WavePhase + validTime;

    float vSin = -_WaveAmplitude * sin(v1*6.2832f);

    color = tex2D(_MainTex, float2(IN.texcoord.x + vSin, IN.texcoord.y));

    }

    else {

    color = tex2D(_MainTex, float2(IN.texcoord.x, IN.texcoord.y));

    }



    return color;

    }

    Basically, I want to get rid of the if statement, since I've heard that it's not a good approach when writing shaders.

    Also, I'd like to make the offset line less visible.

    Thanks :)
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi and welcome.
    Could you please edit your post to use code tags, so that it's possible to view your code.
    That if statement should be least of your worries in my opinion.
    But what exactly you want to achieve? Make the tree not sway at the bottom edge? Or what do you mean with "offset line". I can guess but I'd rather not. And it's easier to help when the question is clear.
     
    Yamski likes this.