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. How can we improve our URP documentation to cover your needs? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

Get _Time from shader for script

Discussion in 'Shaders' started by antonbursch, Sep 28, 2010.

  1. antonbursch

    antonbursch

    Joined:
    Mar 30, 2008
    Posts:
    39
    I have an ocean shader that uses a vertex sine wave to make waves on a grid mesh.

    I have an object that I want to float in the ocean. I can use the exact same sine wave to get a y position for the floating object. I have done this in a shader for the floating object and it works perfect.

    I have a water vehicle that I want to float in the ocean. I can use the exact same sine wave to get a y position for the floating object. But I can't do it in a shader because I need the collision working and that gets all messed up. Therefore I need to do this using a script. And that's simple. Except I can't figure out how to sync up the time in the script to the time in the shader.

    Script time is Time.time.

    Shader time is _Time.

    They do not appear to be in sync.

    I tried to use the following:

    float timeY = renderer.material.GetVector("_Time");

    But it only returns (0, 0, 0, 0).

    Any suggestions on how to sync my times between script and shaders?

    Thanks

    Anton
     
    Last edited: Sep 28, 2010
  2. reissgrant

    reissgrant

    Joined:
    Aug 20, 2009
    Posts:
    726
    Pass the Time.time variable to the shader with:

    Code (csharp):
    1.  
    2. On Update(){
    3.   float theUnityTime = Time.time;
    4.   myCustomMaterial.SetFloat("unityTime",theUnityTime);
    5. }
    6.  
    Then declare this in your shader to access it:
    Code (csharp):
    1.  
    2. float unityTime;
    3.  
     
    Last edited: Sep 29, 2010
  3. antonbursch

    antonbursch

    Joined:
    Mar 30, 2008
    Posts:
    39
    yeah, that is what i did. it was right in front of my face too. ;P

     
  4. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    132
    You can get the shader's _Time value from a C# script by calling this:

    Code (CSharp):
    1. Shader.GetGlobalVector("_Time")
    This way, you don't have to modify your shaders to use a custom time variable.
     
  5. DearUnityPleaseAddSerializableDictionaries

    DearUnityPleaseAddSerializableDictionaries

    Joined:
    Sep 12, 2014
    Posts:
    126
    Shader.GetGlobalVector("_Time")[1] or Shader.GetGlobalVector("_Time")[0]? Seems to be out of sync for me
     
  6. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    1,828
    It depends if you are in play mode or not, otherwise EditorApplication.timeSinceStartup is what's being fed to the _Time parameter in shaderland. While in play mode it's Time.time. This what I use in my buoyancy script and it matches the _Time.x value used in shaders:

    Code (CSharp):
    1. private static float _Time
    2.         {
    3.             get
    4.             {
    5. #if UNITY_EDITOR
    6.                 return Application.isPlaying ? Time.time : Shader.GetGlobalVector("_Time").x;
    7. #else
    8.                 return Time.time;
    9. #endif
    10.             }
    11.         }
     
unityunity