Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Can I tween in shader graph?

Discussion in 'Shader Graph' started by MartinLukavec, Aug 31, 2020.

  1. MartinLukavec

    MartinLukavec

    Joined:
    Aug 31, 2020
    Posts:
    10
    Hi,
    can I make effect that shader will smoothly change when I change parametr (for example it will change transparency from 0 to 1 smoothly in 2 seconds)? Is this possible via shader graph or do I need to write my own shader?
    THX
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,549
    Yes it is. There's a Time node with various outputs (total elapsed, delta etc.), all the usual math nodes including what you would use for whatever traditional tween equation if Lerp wasn't enough (sine, arcsine, powers and so on). Clamping, branching, no reason you couldn't set things up how you want. You could just set an "elapsed" param to 0 for whatever it is, have a duration param, have a node adding delta time to elapsed, do whatever you want with that :)
     
  3. MartinLukavec

    MartinLukavec

    Joined:
    Aug 31, 2020
    Posts:
    10
    Ok, but problem is, that IDK how to create tween effect without variables (and from what I know, shader graph doesn't have any variables).
    And the time node does not have option to reset time, that is the only solution IK without variables.
    This is just from what I know and can be wrong.
     
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,549
    I see what you mean now about doing it within the shader. You could just set a tween progress property via material.SetFloat("_AlphaTweenProgress", 0.5f) and pass it the progress? That's how I've been doing it. But now I'm also curious how this could be done.. there's got to be a way (because if not, there definitely should be). If I can work one out I'll post again.
     
  5. MartinLukavec

    MartinLukavec

    Joined:
    Aug 31, 2020
    Posts:
    10
    Instead of writing script to control shader, it will be maybe easier to write whole shader instead of using shader graph. (or at least easier to use xD)
     
  6. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,549
    You might be able to set a Time offset for resetting a shader graph-based program just by calling once material.SetFloat("_StartTime", Time.time). That's assuming both Time and Shader Graph Time node's output have corresponding values.

    Edit: I'm gonna throw together something to see if that works. I'll report back if it is, along with solution (or that it's not one). If it does, that'll be a great way to optimize a few things I'm doing.
     
    Last edited: Aug 31, 2020
  7. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,549
    Yes it does!
    Code (CSharp):
    1. using UnityEngine;
    2. public class AlphaTweenTest : MonoBehaviour
    3. {
    4.   public Renderer ren;
    5.   private void Update()
    6.   {
    7.     if (Input.GetKeyDown(KeyCode.Return)) ResetTween();
    8.   }
    9.   private void ResetTween()
    10.   {
    11.     ren.sharedMaterial.SetFloat("_StartTime", Time.time);
    12.   }
    13. }
    shader_graph_tween.jpg
    Edit: Note that _StartTime was my reference name (hence no underscore in image)
    Edit: I did this with default version of URP and Shader Graph, in URP Template of 2020.2.0a21