A material is a resource, so every sign that uses it reads the same parameters. In our Godot 4.7 test, set_shader_parameter() on the material of one of three neon signs turned all three green, and a flicker made from TIME dimmed all three at the same moments: the same state in 60 of 60 samples. Duplicating the material for each sign fixes the colour but makes a copy per sign. The script below keeps one material and declares the colour and a flicker seed as instance uniform, set per sign with set_instance_shader_parameter(): one sign green, the other two pink, and the three signs in the same state in only 36 and 39 of 60 samples in two runs.
The code
Save it as neon_sign.gd and load it with const Neon := preload("res://neon_sign.gd"). Make the material once with Neon.material(), set it as the material_override of every sign, then call Neon.setup(sign, Color.HOT_PINK) for each one. Leave the seed out for a random flicker, or give two signs the same seed to make them flicker together, like two halves of one sign.
extends RefCounted
## One shared material for every neon sign, with its own colour and its own flicker per sign.
## const Neon := preload("res://neon_sign.gd")
## var mat := Neon.material() # make it once
## for s in $Signs.get_children():
## s.material_override = mat # every sign uses the same material
## Neon.setup(s, Color.HOT_PINK) # but gets its own colour and flicker
const CODE := """
shader_type spatial;
render_mode unshaded;
instance uniform vec4 glow_color : source_color = vec4(1.0, 0.2, 0.6, 1.0);
instance uniform float seed = 0.0;
uniform float flicker = 0.15; // share of the time a sign is dimmed
uniform float steps_per_second = 12.0;
void fragment() {
float t = floor(TIME * steps_per_second);
float n = fract(sin(t * 12.9898 + seed * 78.233) * 43758.5453);
ALBEDO = glow_color.rgb * (n < flicker ? 0.25 : 1.0);
}
"""
static func material(flicker := 0.15) -> ShaderMaterial:
var sh := Shader.new()
sh.code = CODE
var m := ShaderMaterial.new()
m.shader = sh
m.set_shader_parameter("flicker", flicker)
return m
static func setup(node: GeometryInstance3D, color: Color, seed := -1.0) -> void:
## seed: leave it out for a random flicker, or give two signs the same seed to flicker together.
node.set_instance_shader_parameter("glow_color", color)
node.set_instance_shader_parameter("seed", randf() * 1000.0 if seed < 0.0 else seed)
What we measured
Three flat signs side by side with one ShaderMaterial, unshaded, pink by default. We read the colour in the middle of each sign on the rendered screen. For the flicker, 15% of the time dimmed, we read all three 60 times, 0.09 s apart.
- Plain uniform, set_shader_parameter() to make sign 1 green: green, green, green.
- Plain uniform, sign 1 given a duplicate of the material: the change stayed on that sign only, as expected, with a second material.
- The script, one material on all three (we checked it was the same object): sign 1 green, the others pink.
- Plain flicker from TIME: all three in the same state in 60 of 60 samples in both runs, each dimmed 9 times in the first and 8 in the second.
- The script with seeds 10, 17 and 24: the same state in 36 of 60 samples in the first run and 39 in the second. Each sign was dimmed 5, 10 and 11 times, then 9, 6 and 8. The flicker runs on real time, so the counts change a little from run to run.
The colour results were the same in both runs.
Why it happens
A uniform belongs to the material, and a material is shared by every node that uses it, so one change shows on all of them. A flicker built only from TIME gives every sign the same number at the same moment. An instance uniform belongs to each node instead: the material and shader stay shared, and each sign stores its own value. Adding the seed to the random number gives each sign its own on and off pattern.
What to change
- Instance uniforms are set on the node, not the material, with
set_instance_shader_parameter(), and the shader has to declare them with instance uniform. - A slower or faster buzz: steps_per_second in the material. More dimmed time: the flicker value, 0.15 here.
- The same problem on other shared resources: change the colour of one particle effect and one growing zone grows every copy.
Ready-made signs: Neon Signs & Area Lights for Godot 4.7 has 41 ready-made 3D neon signs and light fixtures that light the walls and wet ground around them, including OPEN, BAR, HOTEL and RAMEN signs, paper lanterns and string lights.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Neon Signs & Area Lights for Godot 4.7 has 41 ready-made 3D neon signs and light fixtures that light the walls and wet ground around them, including OPEN, BAR, HOTEL and RAMEN signs, paper lanterns and string lights.
More how-tos for Neon Signs & Area Lights for Godot 4.7: A swing that settles the same at any frame rate
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
- 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
- Scroll a shader texture for hours without the once-an-hour jump