← Pixel Dreamer Studio

God rays without volumetric fog

One additive quad per shaft, pointed along your sun and turned toward the camera.

The Compatibility renderer, the one Godot uses for web exports, does not draw volumetric fog: in our Godot 4.7 test, turning it on with a density of 0.08 brightened 0 pixels. Draw the shaft yourself instead: an additive, unshaded quad that hangs from the window along the sun's direction and turns around its own axis to face the camera. In the same scene the script below brightened 1,907 of the pixels we checked, stayed lined up with the sun after it moved (0.00 degrees off) and hid itself when the sun went below the horizon.

Left: a dark room with a grey floor and nothing in the air. Right: three warm light shafts slanting down from above
Godot 4.7 with the Compatibility renderer and volumetric fog turned on. Left: the fog alone. Right: three shafts from the script, following the sun.

The code

Add a MeshInstance3D where the light comes in, such as a window or a hole in the roof, attach this script and drag your DirectionalLight3D into sun. length and width are in metres. intensity is how bright the middle of the shaft gets. It takes its color from the sun's light_color.

extends MeshInstance3D
## A soft light shaft (god ray) that follows your sun and fades out when the sun sets.
## No volumetric fog, so it also works in the Compatibility renderer.
## Put this node where the light comes in (a window, a hole in the roof) and drag your DirectionalLight3D into "sun".

@export var sun: DirectionalLight3D
@export var length := 6.0:  ## metres from the opening to where the shaft has faded out
	set(v):
		length = v
		_build()
@export var width := 1.2:  ## metres across
	set(v):
		width = v
		_build()
@export var intensity := 0.5

const SHADER := """
shader_type spatial;
render_mode unshaded, blend_add, cull_disabled, depth_draw_never, shadows_disabled, fog_disabled;

uniform vec3 shaft_color : source_color = vec3(1.0);
uniform float intensity = 0.5;

void vertex() {
	// turn the quad around the shaft's long axis so it always faces the camera
	vec3 axis_point = (MODEL_MATRIX * vec4(0.0, VERTEX.y, 0.0, 1.0)).xyz;
	vec3 axis = normalize(MODEL_MATRIX[1].xyz);
	vec3 side = normalize(cross(axis, CAMERA_POSITION_WORLD - axis_point));
	VERTEX = (inverse(MODEL_MATRIX) * vec4(axis_point + side * VERTEX.x, 1.0)).xyz;
}

void fragment() {
	float across = 1.0 - abs(UV.x * 2.0 - 1.0);  // 0 at the sides, 1 in the middle
	float along = UV.y;  // 0 at the opening, 1 at the far end
	float a = smoothstep(0.0, 0.8, across) * smoothstep(0.0, 0.08, along) * (1.0 - smoothstep(0.35, 1.0, along));
	ALBEDO = shaft_color * intensity * a;
}
"""

var _mat: ShaderMaterial


func _ready() -> void:
	_mat = ShaderMaterial.new()
	_mat.shader = Shader.new()
	_mat.shader.code = SHADER
	material_override = _mat
	cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
	_build()


func _build() -> void:
	var q := QuadMesh.new()
	q.size = Vector2(width, length)
	q.center_offset = Vector3(0, -length * 0.5, 0)  # the top edge sits on this node, the shaft hangs below it
	mesh = q
	extra_cull_margin = width  # the shader turns the quad toward the camera


func _process(_delta: float) -> void:
	if not _mat:
		return
	var up := Vector3.UP
	var color := Color.WHITE
	if sun:
		up = sun.global_basis.z.normalized()  # from the floor back toward the sun
		color = sun.light_color
	var side := up.cross(Vector3.FORWARD if absf(up.z) < 0.99 else Vector3.RIGHT).normalized()
	global_basis = Basis(side, up, side.cross(up))
	var fade := smoothstep(0.0, 0.2, up.y)  # 0 once the sun is at or below the horizon
	_mat.set_shader_parameter("shaft_color", color)
	_mat.set_shader_parameter("intensity", intensity * fade)
	visible = fade > 0.0

What we measured

A dark scene with a grey floor and a sun 55 degrees above the horizon. We compared each frame with the same frame without the shaft and counted the pixels that got brighter, checking every second pixel in each direction. Brightness is the average of red, green and blue, from 0 to 1. Two runs gave the same numbers.

Why the quad turns toward the camera

A flat quad disappears when you look at it edge-on. The vertex() function rebuilds each corner from a point on the shaft's long axis plus a step sideways, at right angles to both the axis and the direction to the camera. The quad only spins around its own axis, so the shaft keeps its slant from the sun. Because the corners move away from where Godot thinks the mesh is, the script sets extra_cull_margin to the width so the shaft is not culled at the edge of the screen.

What to change

Ready-made shafts: Light Shafts & God Rays VFX for Godot 4 has 25 3D presets (windows, blinds, stained glass, forest canopy, caves, underwater, stage lights) and 12 for 2D, each 3D one with dust in the beam and a light pool on the floor, following the sun the same way.

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: Auras, rings, beams and weather