Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by bad3p · Aug 15, 2012 at 04:47 PM · shadertime

Built-in ShaderLab "_Time.y" and "Time.time" are not equal?

Hi!

I am trying to make an animation in shader using the built-in "_Time" property. Lets say the animation is fade-out of output color (white to black) based on values of current T0="Time.time" and some future T1="Time.time + 10". In shader I do simple linear interpolation:

 fixed f = clamp( (_Time.y - T0)/(T1 - T0) );    
 fixed4 color = fixed4(1,1,1,1) * (1-f) + fixed4(0,0,0,0) * f;

From my first impression it all works right, but some time ago I did notice that "_Time.y " is not equal to Time.time. The animation is always playing with delay and I think the longer the scene is playing the longer is delay occured.

So, the question is how the "_Time" value corresponds to Time.time.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ScroodgeM · Aug 15, 2012 at 05:57 PM 1
Share

try to reproduce some great delta with _Time and Time.y and tell how we can do it too 8)

i often use these values and AFAI$$anonymous$$ it's the same value.

5 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by bad3p · Aug 21, 2012 at 03:04 PM

_Time.y = Time.timeSinceLevelLoad

Solution:

 material.SetFloat( "_Time0", Time.timeSinceLevelLoad );
 material.SetFloat( "_Time1", Time.timeSinceLevelLoad + FadeTime );
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ferretnt · Jan 09, 2018 at 07:47 PM 1
Share

Thank you. After hours of debugging, moving things from fragment to vertex shaders, wondering why iOS and Android differ, etc. Another strike for information that should be in the docs (I mean, if you have Unity source code, this is going to be pretty obvious...) but isn't and ins$$anonymous$$d is buried somewhere on the web.

From the docs: "Time (t/20, t, t*2, t*3), use to animate things inside the shaders.".

Useless.

avatar image
3

Answer by aldonaletto · Aug 15, 2012 at 06:13 PM

I would not mix Time.time and _Time.y: the docs don't say they are the same, thus this feels unreliable to me - they may be independent variables, and accumulate some difference over time. I would pass Time.time to a shader variable in Update with material.SetFloat(), as you probably is already doing with the variables T0 and T1, or use _Time.y to set T0 and T1 (not always possible, depending on your logic).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by monotoan · Oct 19, 2017 at 07:39 PM

A slightly stronger solution to this issue is to run one script that passes Time.time (or whichever time value you need) into a GLOBAL shader variable. This solution enables you to access game time from any shader or material without having to worry about individually setting time variables on any of those materials.


For example:

    Shader.SetGlobalFloat("_GameTime",Time.time);


Then, in any shader, you can declare and use

 float _GameTime;


Just remember, global shader variables won't be updated or work properly if you add them to the "Properties { }" section of a shader, so make sure you just put this declaration in the SubShader { } / CGPROGRAM section.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chris-Cooper · Jan 29, 2018 at 11:24 PM 0
Share

Just to confirm, this works great. Thanks! The two are definitely not always equal, for example I've found that the Unity splash screen (or something else in builds) delays the shader time start but about 5 seconds compared to Time.time

avatar image Knugke · Nov 29, 2021 at 07:52 PM 0
Share

I've also tried this approach and, so far so good. I wanted to be able to adjust the rate at which time passes, but then I had to recalculate an "offset" to make the transition smooth, else the new multiplier lets _Time * _Speed jump at each change. I now calculate on offset each time I change the _Speed, and send the calculated time to the global shader variable, keeping a handful of shaders aligned.

avatar image
0

Answer by adamgryu · Sep 10, 2018 at 08:56 PM

You can also get the shader's _Time value from a C# script by calling this:

     Shader.GetGlobalVector("_Time")

This way, you don't have to modify your shaders to use a custom time variable. You can just use the shader time in your C# script.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by GrayedFox · Jan 03, 2021 at 02:46 AM

Rather than using a time variable I would put that logic into a coroutine which drives the animation property of your shader. I go into more detail in this answer but I think the same logic should apply: http://answers.unity.com/answers/1801454/view.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to change a range within a shader over time, using a script. 1 Answer

Change input values based on Time 0 Answers

Changinge the value of a shader over time 1 Answer

Shader Graph not updating time related nodes in preview 6 Answers

Shader time related questions 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges