- Home /
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.
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.
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.
See my edit above. I haven't actually tried using this function before, but it ought to do what you want.
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 ?
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.
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
![](https://koobas.hobune.stream/wayback/20220612120347im_/https://answers.unity.com/themes/thub/images/avi.jpg)