Cheap light shafts and god rays are flat cards: quads with an additive, glowing material. Seen from the front they look like soft beams, but a flat card has no thickness, so as the camera moves round to its side it gets thinner while staying just as bright. In our Godot 4.7 test the card kept a brightness of 0.52 at every angle, down to a 704 pixel sliver at 88 degrees. The script below multiplies the light by abs(dot(NORMAL, VIEW)), how much the card faces the camera: 0.37 at 45 degrees, 0.12 at 70 and nothing left at 85.
The code
Save it as shaft_card.gd and load it with const ShaftCard := preload("res://shaft_card.gd"). Then $Shaft.material_override = ShaftCard.make(Color(1.0, 0.9, 0.7), 0.6): the colour and how strong. A soft gradient texture in the mask parameter shapes the beam. The shader code is in the script, so you can also copy it into a .gdshader file.
extends RefCounted
## A light shaft card (a quad of glowing light) that fades as you look along it,
## so it never shows as a hard bright line when the camera sees it edge-on.
## const ShaftCard := preload("res://shaft_card.gd")
## $Shaft.material_override = ShaftCard.make(Color(1.0, 0.9, 0.7), 0.6)
const CODE := """
shader_type spatial;
render_mode unshaded, blend_add, cull_disabled, depth_draw_never, shadows_disabled;
uniform vec4 color : source_color = vec4(1.0, 0.9, 0.7, 1.0);
uniform float strength = 0.6;
uniform float edge_power = 1.5; // higher fades sooner as the card turns away
uniform sampler2D mask : source_color, hint_default_white; // optional soft shape
void fragment() {
// NORMAL and VIEW are both in view space here; 1 = facing the camera, 0 = edge-on
float facing = abs(dot(normalize(NORMAL), normalize(VIEW)));
ALBEDO = color.rgb * strength * pow(facing, edge_power) * texture(mask, UV).r;
}
"""
static func make(color := Color(1.0, 0.9, 0.7), strength := 0.6, edge_power := 1.5) -> ShaderMaterial:
var sh := Shader.new()
sh.code = CODE
var m := ShaderMaterial.new()
m.shader = sh
m.set_shader_parameter("color", color)
m.set_shader_parameter("strength", strength)
m.set_shader_parameter("edge_power", edge_power)
return m
What we measured
A 2 by 3 m card in front of the camera, turned 0, 45, 70, 85 and 88 degrees round its vertical axis. On the rendered screen we counted the lit pixels and read the brightest and the mean brightness above the dark background. Two runs gave the same numbers.
- Plain glow: 20,768 lit pixels facing the camera, then 14,994, 7,410, 1,818 and 704. The brightest and the mean were 0.52 every time.
- The script: 20,768 pixels at 0.52 facing the camera. At 45 degrees the brightest 0.37, mean 0.31. At 70 degrees 0.12, mean 0.10. At 85 and 88 degrees no pixel above the background.
Why it happens
An additive card adds the same light to every pixel it covers, whatever angle it is seen from, so its brightness never changes, only its width. In fragment() both NORMAL and VIEW are in view space, and their dot product is 1 when the card faces the camera and 0 when it is edge-on. abs() makes the back of the card work the same, since it is drawn from both sides. Raising it to a power, edge_power, makes the fade start sooner.
What to change
- edge_power 1.5 is what we measured. A higher value hides the card sooner, a lower one keeps it longer.
- Shafts that follow the sun: sun direction from a DirectionalLight3D. God rays without volumetric fog: light shafts in the Compatibility renderer.
Ready-made shafts: Light Shafts & God Rays VFX for Godot 4 has 37 ready-made shafts for 3D and 2D, with dust in the beam and a light pool on the floor, and they follow your sun too.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Light Shafts & God Rays VFX for Godot 4 has 37 ready-made shafts for 3D and 2D, with dust in the beam and a light pool on the floor, and they follow your sun too.
More how-tos for Light Shafts & God Rays VFX for Godot 4: Get the direction to the sun from a DirectionalLight3D, and fade sun effects out at sunset
More how-tos: Auras, rings, beams and weather
- Stretch a bolt sprite from the caster to the target
- Space lights, posts or coins evenly along a Path3D
- Stop a big spinning texture from looking grainy when it is drawn small
- God rays without volumetric fog
- Dark horror fog that works in Compatibility
- Magic aura particles that circle a character
- Charge-up particles that fly in to the middle
- Particle ring standing up instead of flat
- 2D particles from a sprite's shape
- A glowing circle on the ground
- A loot beam that never goes flat
- Chain lightning between enemies
- A tower's attack range ring
- Enemy vision cone with a "!" when it spots you
- Rain that follows the camera
- Rain falling through the roof
- Snow particles not falling
- orbit_velocity not working in 3D: use tangential_accel
- Rain falls through the player
- Snow that stays on roofs
- Emit particles along a Path3D
- Ambient particles in many places from one emitter
- Keep a buff aura on until the last buff really runs out
- Spin a magic circle forever at the same speed
- Space flying swords, shields or orbs evenly around a character for any count
- Text around a circle with even gaps