Create a directional light

Godot Version

4.5

Question

I’m creating camera and a directional light via gdscript.. but I never get any shadow effect and looks like everything just stays on full bright all the time. Now if I build this same scene in the editor he lighting is proper.

My code:

func CreateDirectionalLight(node):
	var new_light_3d = DirectionalLight3D.new()
	node.add_child(new_light_3d)
	new_light_3d.name = "Directional Light"

	# Basic setup
	new_light_3d.light_energy = 1.5      		  # Brightness
	new_light_3d.light_color = Color(1, 1, 1)	  # White light
	new_light_3d.shadow_enabled = true   		  # Enable shadows

	# Orient the light (pointing down at an angle)
	new_light_3d.rotation_degrees = Vector3(45, 0, 0)
	return new_light_3d
func CreateCamera(node):
	var new_camera_3d = Camera3D.new()
	node.add_child(new_camera_3d)
	new_camera_3d.name = "Camera"
	return new_camera_3d

Whats wrong ?

This would be pointing up at an angle, try -45?

Yah I tried that before posting, was just trying different things.

Ok to reproduce this, I made a new scene. Added a 3d Scene Node to it. then attached this script.

func _ready():
	# --- Directional Light ---
	var light = DirectionalLight3D.new()
	light.rotation_degrees = Vector3(-45, -45, 0)
	node.add_child(light)

	# --- Plane (Ground) ---
	var plane_mesh = MeshInstance3D.new()
	plane_mesh.mesh = PlaneMesh.new()
	plane_mesh.scale = Vector3(10, 1, 10) # make it larger

	# Add material (green ground)
	var plane_material = StandardMaterial3D.new()
	plane_material.albedo_color = Color(0.2, 0.8, 0.2) # green
	plane_mesh.material_override = plane_material
	node.add_child(plane_mesh)

	# --- Cube ---
	var cube_mesh = MeshInstance3D.new()
	cube_mesh.mesh = BoxMesh.new()
	cube_mesh.transform.origin  = Vector3(0, 1, 0) # above the plane

	# Add material (blue cube)
	var cube_material = StandardMaterial3D.new()
	cube_material.albedo_color = Color(0.2, 0.4, 1.0) # blue
	cube_mesh.material_override = cube_material
	node.add_child(cube_mesh)

	# --- Camera ---
	camera = Camera3D.new()
	camera.transform.origin  = Vector3(5, 5, 10)
	camera.look_at(Vector3(0, 1, 0), Vector3.UP)
	node.add_child(camera)

As you can see there is no lights or shading at all in this scene.

With this script you omitted enabling shadows

# --- Directional Light ---
var light = DirectionalLight3D.new()
light.rotation_degrees = Vector3(-45, -45, 0)
light.shadow_enabled = true
node.add_child(light)

With this change I see the cube’s shadow.

2 Likes