Search Unity

Lerp interpolation help

Discussion in 'Shaders' started by unity_5fpsmegasupergiperprogramer, May 22, 2019.

  1. unity_5fpsmegasupergiperprogramer

    unity_5fpsmegasupergiperprogramer

    Joined:
    Dec 1, 2017
    Posts:
    101
    I need help.

    There are two textures.

    We need to make a quick transition in two places.

    If you just use lerp(tex1, tex2, amout) then it constantly overlays the textures on each other with the transition. I need to be active one of the textures, BUT with a smooth transition to the other.

    upload_2019-5-22_18-1-10.png
     
  2. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    119
    Yeah, the third parameter isn't speed, is the amount of blend between the two values.
    So, if "speed" is 0.5 then that's the amount of blend you'll have between the two values.
    In other words, is a variable that you have to change yourself.
    You can do this by making speed a variable that you change externally to the shader using a script.
    Or if you want it to animate without any external script driving it, then you can use _Time, _SinTime or similar.
     
    Last edited: May 22, 2019
  3. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    119
    Weird, I thought "amout" was "speed" when I first read your question.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,442
    if you are setting the amount from script, could take value from AnimationCurve,
    with some curve similar to
    upload_2019-5-22_14-58-23.png
     
  5. Deleted User

    Deleted User

    Guest

    Interesting problem. In fact you need the first texture to fade out while the second fades in. Maybe try material.lerp?

    https://docs.unity3d.com/ScriptReference/Material.Lerp.html
     
  6. Sh-Shahrabi

    Sh-Shahrabi

    Joined:
    Sep 28, 2018
    Posts:
    56
    I am not sure I understand your question 100 percent. So you mean if you use lerp(tex1, tex2, lerpFactpr), your code is always overlaying the tex2 on tex1 after transition is done? Is it a performance concern you have? Or do you mean you literally see the one texture being effected by the other at all time?

    At any case typical way of doing this, if it is only two textures, drive the lerp factor from CPU, and switch it smoothly from 0 to 1 when you want to show tex2, let it stay on 1 the entire time, like this you should be seeing only tex2, lerp back to 0 to get tex1. If there are more than two textures, lets say you wanna do a slideshow, then instead of tex1 and tex2, you should have a texture called current another called nextInLine (I change the naming so that it becomes more obvious what they do). Then you always have your current and nextInLine set by your CPU, when it is transition time, count the lerpFactor to 1 over time, it will switch to the next texutre, then set the lerp factor back to 0 and set current to NextInLine and update the nextInLine to the next texture. You can loop like this over an array of textures in a slideshow of some sort.