← Pixel Dreamer Studio

Neon signs that share a material but not a colour

Keep one ShaderMaterial for every sign and give each sign its own colour and flicker seed as instance uniforms.

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.

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

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