Search Unity

Slowly increase from 1 to 0

Discussion in 'Shader Graph' started by MarkusKer, Nov 5, 2019.

  1. MarkusKer

    MarkusKer

    Joined:
    Jun 7, 2017
    Posts:
    15
    Hello

    i was wondering how i would be able to achieve an certain effect.
    I have a float and as soon as i turn on the float to be 1 i want it to slowly increase inside the shader graph to achieve some sort of smooth transition. I played with the "SineTime" component but this one is fading out after an certain amount of time. I need the value to stay 1 after it has reached this value.

    Any ideas?

    Thanks,
    Markus
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Your explanation seems a bit unclear to me. You want some smooth transition inside Shader Graph material to begin when you set a value to 1?

    I think you should try to do it the normal way if you want a smooth transition:
    Expose a float value in your shader graph and then use C# code to set the parameter smoothly over time using Lerp or some other interpolation, so that you get a gradual change in the material. Then you can control your value to be anything you want.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Sine time is literally the sine of the current time value. And the time is the script side Time.timeSinceLevelLoad. If you want a value to increase slowly over time, you need to do it by setting a start time and duration (or end time) and calculate that from the current time.

    So, for example, you could have a StartTime and Duration property and do something like this:
    float value = saturate((Time - StartTime) / Duration);

    If you want it to make the transition a little smoother with a ease in and out from 0.0 to 1.0, you can put that through a smooth step node.
     
  4. seffles

    seffles

    Joined:
    Oct 2, 2013
    Posts:
    32
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Those examples don’t have a duration, so they’re always 1 second long. Also the code is using Time.time which only works if you never load a new scene.
     
  6. seffles

    seffles

    Joined:
    Oct 2, 2013
    Posts:
    32
    What they DO have is an excellent example of how to achieve something like you were describing in Shader Graph. It has the desired effect of starting from 0 at a given time, and rising to 1 where it will stay. It's really not hard to add a duration on top of that using a multiply node.

    But pardon me, next time I'll only post if I have something the OP can copy and paste in to do exactly what he needs without reading or learning anything. Didn't realise I'd stumbled into Stack Overflow. :p
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    It’s a good example, not discounting that. Just pointing out the limitations, and a correction. I posted example HLSL code in the Shader Graph forum, so I’m hardly innocent here. ;)
     
    seffles likes this.