Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by trasher258 · Apr 21, 2017 at 03:25 PM · scripting problemshaderanimation script

Using time.deltatime to control an animated shader

Hey fellas, I got a shader that animates over time which just offsets the uvs of a given texture. At the moment I'm utilizing the global time variable available in Unity which works well... until the game runs long enough to where the shader starts to slow down. Needless to say the time variable gets rather large and float precision becomes a problem in this scenario.

So I like to swap out the Time.time variable with Time.deltatime instead to increment the animation but I'm not certian how to do this in a shader. I know I need to make use of an If statement for this to work so I can wrap around the overall animation time variable so it doesn't exceed past 0-1. Ideally, it would be setup like this:

 currentTime = 0.0
 currentTime += time.deltaTime
 if currentTime < 1.0 then (currentTime - 1.0)

I know that using If statements are usually frown upon when used in shaders but this is the only one used in my case so it shouldn't cause a major performance hit. So is it possible to get this working in a shader and if so how? The idea is there, it's just the matter of getting it to work... hopefully without resorting to relying on a seperate script component if at all possible.

Thanks in advance.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by FortisVenaliter · Apr 21, 2017 at 03:51 PM

The only way I know how would require a script component, but a small one. You can define your own float as a parameter in the shader. Handle the timing update logic on the CPU in the script and pass the float of time to the shader each frame. That would give you full control.

Edit (in response to comment below): You can still do this with only one script, it looks like... Check out the Shader.SetGlobalFloat function. With one script, you should be able to set a global float to handle the time. This would allow you to wrap it around on the CPU side, but it would require all materials to loop at the same point to maintain continuity.

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 trasher258 · Apr 21, 2017 at 04:51 PM 0
Share

That is possible but the only concern I have with that is if I have lots of materials running around in the scene using this shader, I have to do this to all of them. Potentially I could target any materials, both when starting the game and when they're spawned in, to be controlled via this one script but I'm not certian how that will affect performance. That and I may not be able to control the speed of it per material this way if needed. Hmm.

It's worth a shot at least so I'll let you know if it works well enough.

avatar image FortisVenaliter trasher258 · Apr 21, 2017 at 08:23 PM 0
Share

See my edit above. I haven't actually tried using this function before, but it ought to do what you want.

avatar image
0

Answer by AtGfx · Apr 21, 2017 at 04:43 PM

Hi !

Making an if statement in a shader is not a really good idea since your piece of code will not be optimised on the gpu cores. Nevertheless, if the statement is really simple like in your example, and do not rely on variables, the gpu can still make a good work. Generally avoid doing it for a statement that rely on calculated variables in the shader.

For your example, why don't you simply calculate the delta time on your CPU and then set a shader parameter corresponding to this delta time ?

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 trasher258 · Apr 21, 2017 at 05:06 PM 0
Share

That's doable, my only concern is having many materials requiring this variable to be set both at game start and when new ones are instanced. That's why I ideally like it to be done in the shader itself so it's self-contained and not needing an external script outside of the shader to calculate it.

Like I mentioned before to another person who answered, it could be possible to manage any materials using this shader with a single script component with the only downside being no way to control speed per material. It's worth a shot but I still prefer doing this in the shader itself if possible.

avatar image AtGfx trasher258 · Apr 21, 2017 at 10:36 PM 0
Share

I don't think it is possible to get ti$$anonymous$$g info in the shader. You can have a look to the hlsl/glsl documentation to verify it.

But if you attach a script to a game object you can set a speed factor to control the speed parameter passed to the shader, which can be different per object...

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

121 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Separating Procedurally Generated Mesh, Based On Triangle Height 1 Answer

How to make transparent shader (2D)? 1 Answer

Outline Shader not working correctly on blender models 0 Answers

Texture2D Array Appears Grainy 0 Answers

Highlight where the player clicks 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