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

Bug Time Node yields Time.time instead of Time.timeSinceLevelLoad, per documentation. Or am I confused?

Discussion in 'Shader Graph' started by dgoyette, Jul 19, 2020.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,117
    There appears to be either a bug with the Time node, or some inconsistency in the implementation or documentation.

    The documentation for the Time node shows the following (https://docs.unity3d.com/Packages/com.unity.shadergraph@7.3/manual/Time-Node.html):

    The right-hand values for all of these appears to be coming from the "Built-in shader variables" (https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html), which states the following (emphasis mine):

    So, this is telling me that the Time value coming out of the Time node should be equal to Time.timeSinceLevelLoad. However, it's easy to confirm that the value is actually equal to Time.time, not Time.timeSinceLevelLoad.

    This isn't the end of the world; I can adjust my shader usage to account for this. But I'd like to know if this is considered a bug, or incorrect documentation, or whether I just don't understand what's going on here.

    This is easy to verify using a shader like this:

    upload_2020-7-18_22-37-28.png

    And then use a script like this in your scene:

    Code (CSharp):
    1.     // Update is called once per frame
    2.     void Update()
    3.     {
    4.         if (Input.GetKeyDown(KeyCode.KeypadEnter))
    5.         {
    6.             Debug.Log("Reloading scene.");
    7.             SceneManager.LoadScene("SampleScene");
    8.         }
    9.  
    10.         Debug.Log($"timeSinceLevelLoad: {Time.timeSinceLevelLoad}; time: {Time.time}; unscaledTime: {Time.unscaledTime}");
    11.     }
    The object you put this shader on should appear to gradually rise. But if you reload the scene (Enter key), such that timeSinceLevelLoad resets, the object's vertex position won't reset. It will keep going up. And it will go up by an amount consistent with Time.time. For example, the nearer cube is rising up 2 additional meters compared to its proper position, because Time.time is 20. (The cubes behind it are just there to show the measurement)

    upload_2020-7-18_22-36-26.png
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,117
    In looking a little deeper into this, it turns out that this might be render pipeline specific. If I manually create a shader and use Time.y in HDRP, that's appears to be yielding Time.time, not Time.timeSinceLevelLoad. So, perhaps Shader Graph is returning _Time.y, but _Time.y has been changed in HDRP to return Time.time, not Time.timeSinceLevelLoad.
     
  3. KYL3R

    KYL3R

    Joined:
    Nov 16, 2012
    Posts:
    128
    Hmm, I just wanted to compare times to calculate age from script to shader.

    Time.time returns the time in seconds since the start of the application
    _Time contains Time since level load
    but there is also Time.timeSinceLevelLoad which returns the time in seconds since the last non-additive scene has finished loading

    I load my levels additively, so Time.timeSinceLevelLoad should not reset here, but I didn't try if _Time behaves the same or if it resets on every level load (additive & non-additive). I think I will just pass in a global variable, I already had to do that for unscaled time (for pause menu).
     
  4. lgarczyn

    lgarczyn

    Joined:
    Nov 23, 2014
    Posts:
    41
    _Time.x (didn't check for the others) doesn't reset on level load on 2022.2.16f1