← Pixel Dreamer Studio

Get the direction to the sun from a DirectionalLight3D, and fade sun effects out at sunset

The light shines along its -Z. Use +global_basis.z as the direction to the sun, and its y for how high the sun is.

A DirectionalLight3D shines along its own -Z, so sun.global_basis.z points from the scene to the sun and -sun.global_basis.z is the way the light travels. Aim light shafts and rays along -Z, and read the sun's height from the y of +Z. In our Godot 4.7 test the ground was lit only while that y was above 0. A fade built from the y of -Z by mistake stayed at 0 at every daytime angle and rose to 0.95 once the sun was 10 degrees below the horizon: sun effects that show only at night.

The code

Save it as sun_direction.gd. to_sun() and travel() return the two directions. daylight() is 1 while the sun is high and eases to 0 at the horizon: multiply the strength of light shafts, lens flares or heat haze by it. aim_along_sun() turns a Node3D so its -Z follows the sunlight, also with the sun straight overhead. For example: const Sun := preload("res://sun_direction.gd") and Sun.aim_along_sun($LightShaft, $Sun).

extends RefCounted
## Sun direction helpers for a DirectionalLight3D.
## The light shines along its own -Z, so +Z points back at the sun.


## Unit vector from the scene toward the sun. Its y is above 0 while the sun is above the horizon.
static func to_sun(sun: DirectionalLight3D) -> Vector3:
	return sun.global_basis.z.normalized()


## The way sunlight travels: aim light shafts, rays or fake shadows along this.
static func travel(sun: DirectionalLight3D) -> Vector3:
	return -sun.global_basis.z.normalized()


## 1 while the sun is high, easing to 0 as it reaches the horizon and staying 0 below it.
## Multiply sun-only effects (light shafts, lens flares, heat haze) by it.
static func daylight(sun: DirectionalLight3D, fade_height := 0.2) -> float:
	return smoothstep(0.0, fade_height, to_sun(sun).y)


## Turns a Node3D so its -Z follows the sunlight, the way a light shaft should point.
static func aim_along_sun(node: Node3D, sun: DirectionalLight3D) -> void:
	var dir := travel(sun)
	var up := Vector3.FORWARD if absf(dir.y) > 0.99 else Vector3.UP  # look_at fails when dir is parallel to up
	node.look_at(node.global_position + dir, up)

What we measured

A gray ground seen from above and a DirectionalLight3D turned 35 degrees around Y, tilted from straight down (-90 degrees on X) to 10 degrees below the horizon. We averaged the ground's screen brightness (0 to 255) and read both vectors. Two runs gave the same numbers.

Why it happens

Lights in Godot point along their local -Z, like cameras. Tilting the light down by -60 degrees on X points its -Z down into the ground, so its +Z points up and back toward the sky, where the sun is. Mixing the two up flips everything that depends on the sun's height: an effect that should fade out at sunset fades in instead.

What to change

God rays without volumetric fog: light shafts in Compatibility. A sun that moves through the day: day and night cycle.

37 ready-made shafts: Light Shafts & God Rays VFX for Godot 4. Give a 3D shaft your DirectionalLight3D as its sun and it tilts with the sun, takes its color and fades out when the sun sets.

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: God rays without volumetric fog · Light shaft cards without the bright edge-on line

More how-tos: Skies and backgrounds