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.
- TIME at 0.123 UV per second: normal step 0.00205, biggest step 0.20205, at 3600 s. At 0.333: normal 0.00555, biggest 0.20555, at 3600 s.
- TIME at 0.1, 0.25, 0.37 and 0.13: no step bigger than normal.
- The script, all six speeds: no step bigger than normal.
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
- Two layers at different speeds: wrap at a time where both make whole loops, for example 20 s for 0.1 and 0.05.
- Or pick speeds where speed times 3600 is a whole number, like 0.125 instead of 0.123.
- A bigger time_rollover_secs in Project Settings moves the jump later. It does not remove it.
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
- A 3D outline that stays the same width on screen
- Find and fix the seam in a repeating texture
- PS1 vertex wobble that is the same near and far
- Snow that stays on the roofs
- Packed ORM maps on a StandardMaterial3D
- Neon signs that share a material but not a colour
- A MatCap that stays the right way up from every side
- Scroll or tile a 2D texture without stretched edge lines
- Keep floors, roads and roofs sharp when the camera looks along them
- Make WorldEnvironment color grading apply to a 2D game
- Get a hard cel-shaded light edge from StandardMaterial3D's Toon mode
- Make metallic materials stop looking black or flat
- Make a glowing sign or screen actually light the wall around it
- Use a LUT strip for color grading in Godot 4
- Tiling textures that keep their size on any mesh
- A sharp low-resolution PS1 look
- Only 8 lights show on a long wall in Compatibility
- Hit flash shader for 2D sprites
- Flash a 3D enemy white when it takes a hit
- Dissolve shader for 3D models
- 2D outline shader that is not cut off
- 3D outline shader without gaps at the corners
- A force field shield with a hit ripple
- Normal map inverted: DirectX vs OpenGL
- Full-screen post-process shader in 3D