← Pixel Dreamer Studio

Custom particle shader ignores the color ramp

The ramp arrives in COLOR. Your shader has to use it.

The color_ramp of a ParticleProcessMaterial gives each particle its color in COLOR. A StandardMaterial3D picks it up with a checkbox, a ShaderMaterial does not: write ALBEDO = COLOR.rgb for the colors and multiply ALPHA by COLOR.a for the fade. A ShaderMaterial also has no Billboard Mode, so turn the quad to the camera in vertex().

Two rising streams of soft round dots: all white on the left, yellow turning orange, red and fading on the right
The script below in Godot 4.7 with the Compatibility renderer. Left: a shader that does not read COLOR. Right: the script as written.

The code

Attach this to a GPUParticles3D for embers drawn with your own shader: soft round dots that go from yellow to red and fade out. The two lines in fragment() that use COLOR are the fix. The line in vertex() makes the dots face the camera.

extends GPUParticles3D
## Embers drawn with your own spatial shader that still follow the Color Ramp:
## yellow, then red, then a fade out. A custom shader has to read COLOR itself.

const SHADER := '''
shader_type spatial;
render_mode unshaded, depth_draw_never, cull_disabled;

void vertex() {
	// face the camera, like Billboard Mode in a StandardMaterial3D
	MODELVIEW_MATRIX = VIEW_MATRIX * mat4(INV_VIEW_MATRIX[0], INV_VIEW_MATRIX[1], INV_VIEW_MATRIX[2], MODEL_MATRIX[3]);
}

void fragment() {
	float d = length(UV - vec2(0.5)) * 2.0;
	ALBEDO = COLOR.rgb; // the Color Ramp
	ALPHA = (1.0 - smoothstep(0.5, 1.0, d)) * COLOR.a; // a soft round dot, and the fade in the ramp
}
'''


func _ready() -> void:
	amount = 24
	lifetime = 2.0
	var ramp := Gradient.new()
	ramp.set_color(0, Color(1.0, 0.85, 0.3, 1.0))
	ramp.set_color(1, Color(0.6, 0.05, 0.0, 0.0))
	ramp.add_point(0.45, Color(1.0, 0.3, 0.05, 1.0))
	var rt := GradientTexture1D.new()
	rt.gradient = ramp
	var m := ParticleProcessMaterial.new()
	m.color_ramp = rt
	m.direction = Vector3.UP
	m.spread = 20.0
	m.initial_velocity_min = 0.8
	m.initial_velocity_max = 1.2
	m.gravity = Vector3.ZERO
	process_material = m
	var sh := Shader.new()
	sh.code = SHADER
	var mat := ShaderMaterial.new()
	mat.shader = sh
	var q := QuadMesh.new()
	q.size = Vector2(0.2, 0.2)
	q.material = mat
	draw_pass_1 = q

What we measured

We let out one particle that does not move, in the middle of the screen, with a lifetime of 2 seconds, and read the pixel color (red/green/blue from 0 to 1) at three points of its life. The first run after a fresh import started late while the shader compiled. The next two runs gave the same numbers.

Billboarding is gone too

Billboard Mode is a setting of StandardMaterial3D. A ShaderMaterial draws the quad as it is, flat and facing one way. We looked at the same particle from the side, 3 meters away.

What to change

The same problem with a StandardMaterial3D: particle color ramp not working. Particles that do not show at all: GPUParticles3D not showing.

Tested in Godot 4.7 with the Compatibility renderer.

More how-tos: When particles do not show up