Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Unscaled time on shadergraph

Discussion in 'Shaders' started by pineapuru, Nov 26, 2020.

  1. pineapuru

    pineapuru

    Joined:
    Mar 21, 2018
    Posts:
    46
    Hey, I've been trying out shadergraph and shader programming in general and noticed shadergraph doesn't have any unscaled time options, also I haven't found many solutions on using unscaled time on shader codes, I searched on the forum and found something from 2015 saying it isn't possible to use unscaled time on shadergraph, is that still true? what would be a good method to make a shader run on unscaled time easily?

    Edit: Just to inform everyone, I animate a lot of my pause scenes with shaders, that's why I need them to act even when time is freezed, they're like background scrolling textures etc.
     
    Last edited: Nov 26, 2020
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Why can't you just set value from C# side and use that to drive your shader animations.
     
    adamgolden likes this.
  3. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,464
    Indeed. To elaborate on that approach, add something like public Renderer ren; to a component and drag whichever to it. Add a Float property to your Shader Graph, call it something like "Unscaled Time" with a reference name like "_UnscaledTime". Then in Shader Graph, connect your Unscaled Time node to whatever, and finally in Update of the component you added ren to:
    Code (CSharp):
    1. ren.sharedMaterial.SetFloat("_UnscaledTime", Time.unscaledTime);
    Note the above expects the renderer is using a single material. If not, use .sharedMaterials[index] instead.
     
    Braza, Kshesho and pineapuru like this.
  4. pineapuru

    pineapuru

    Joined:
    Mar 21, 2018
    Posts:
    46
    Thanks for teaching me on how to do it! Just had to do some changes as UI images doesn't have renderers aparently, but just had to change the code to:
    Code (CSharp):
    1. img.material.SetFloat("_UnscaledTime", Time.unscaledTime);
    Thank you so much again. :)
     
    adamgolden likes this.
  5. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    700
    I need my scaled time for seasonal and day/night synchronization, and am using GPUI, (a GPU instancer) so I cant set the parameters outside of the GPUI prefab, because it all gets cached on the GPU, but I still want my trees to sway in the wind when paused. Not a huge deal, btu I think I might want it for other things in the future.
     
  6. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,873
    Did you try using Shader.SetGlobalFloat instead?
     
    adamgolden likes this.
  7. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    700
    Magic words is what I seek! Thank you! Did not know the phrase existed. I will try it out. If it works, A whole new set of options just opened up! If I am reading it right, I can set all types of global floats for anything... ...wow. I did not even consider it possible when designing.