Synchronising time between gdscript and shader

Godot Version



I am trying to trigger animations in the shader frm GdScript, sending through a ‘start_time’, as a shader parameter, set as ‘Time.get_ticks_msec() * 1000.0’, then calculating elapsed time in the shader with ‘t = TIME - start_time’.

I expected these times to be synchronised, but TIME (in gdscript) is bit ( around half a second) behind.

To calculate the offset I evenually found I could set a global shader parameter ‘offset_time’ in a physics process running on the first frame, then when I need synchronised time in a shader I do ‘actual_time = TIME + offset_time’.

Is this expected behaviour, or am I missing something? Is there a cleaner way to do this? I haven’t yet tested whether the times stay in sync over longer periods.

Thanks in advance!

Two options I’ve found:

If you’re using a particle shader you can look at the VERTEX position in the spatial shader and use that to make adjustments.

Otherwise, when needed, pass the shader elapsed (sum(delta)) via your gdscript func process(delta) and set_shader_parameter (or whatever that function is).

There’s no easy way to track time elapsed in shaders I’m afraid (at least that I’ve found)

Edit: To be clear, the timing should be handled entirely by your gdscript through the process func and updating the shader parameter manually. TIME is only really useful for sinusoids and random seeds in my experience.

1 Like

Thanks. I was hoing to avoid having to explicitly set an ‘actual_time’ in the shader every frame, but I guess it’s not the end of the world if it comes to that! So far setting a single offset at the game seems stable, though, atleast in my current case on a multimesh present frm initialisation.