← Pixel Dreamer Studio

Light shaft cards without the bright edge-on line

Multiply the card's light by how much it faces the camera, so it fades out before it turns into a line.

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.

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

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