← Pixel Dreamer Studio

Scroll a shader texture for hours without the once-an-hour jump

Give the shader your own time that wraps after a whole number of loops, or use a speed that loops evenly in 3600 s.

Godot's shader TIME goes back to 0 after time_rollover_secs, 3600 seconds by default in our Godot 4.7 project. A texture scrolling at speed times TIME then jumps back by whatever part of a loop it had covered. Over 2 hours at 60 fps, 0.123 and 0.333 UV per second jumped 0.2 of the texture at 3600 s, about 100 times a normal frame's step. 0.1, 0.25, 0.37 and 0.13 did not, because 3600 times each of them is a whole number. A time that wraps after one whole loop never jumped.

The code

Put the script on any node and give it the ShaderMaterial and the speed the shader scrolls at. In the shader add uniform float scroll_time; and scroll with UV + vec2(scroll_time * speed, 0.0) instead of TIME.

extends Node
## Gives a scrolling shader its own time that wraps after whole loops of the texture,
## so it never jumps when Godot's TIME rolls over (every 3600 s by default).
## In the shader: uniform float scroll_time; then use scroll_time where you used TIME.

@export var material: ShaderMaterial
@export var speed := 0.123  # UV per second, the same speed the shader scrolls at
var _t := 0.0


func _process(delta: float) -> void:
	_t = fposmod(_t + delta, 1.0 / absf(speed))  # 1 / speed seconds = exactly one loop
	material.set_shader_parameter("scroll_time", _t)

What we measured

time_rollover_secs read from the project: 3600. Two hours at 60 fps worked out in GDScript for six speeds, with the offset as the fraction of TIME times speed. Two runs with the same numbers.

Why it happens

At 3600 s a speed of 0.123 has scrolled 442.8 loops. TIME going back to 0 throws the 0.8 away at once, which shows as a jump of 0.2 the other way. Wrapping your own time after exactly one loop, 1 / speed seconds, makes the restart land where the texture already is.

What to change

Noise in an effect shader: dissolve shader in 3D.

Seamless noise to scroll: 60 VFX Textures has 60 grayscale VFX textures: 20 seamless noise, 26 shape masks and 14 8x8 flipbooks, for Unity, Unreal and Godot.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: 60 VFX Textures has 60 grayscale VFX textures: 20 seamless noise, 26 shape masks and 14 8x8 flipbooks, for Unity, Unreal and Godot.

More how-tos: Shaders, materials and lights