DirectionalLight3D
- Game Development course
- Intermediate
- Lesson 63/83
- 4 min
Learn sun-like lighting with DirectionalLight3D.
DirectionalLight3D and Sunlight
To simulate parallel light rays originating from an infinitely distant source in outdoor scenes, the DirectionalLight3D node is used. This light represents the sun or moon; its position is irrelevant, only its rotation determines the incident direction of light rays.
DirectionalLight3D sun = new DirectionalLight3D();
sun.LightEnergy = 1.2f;Enabling Shadows
By default, shadow casting is disabled on lights to preserve performance. To create realistic shadows, set the ShadowEnabled property to true. The brightness and intensity emitted into the scene is adjusted via the LightEnergy property.
sun.ShadowEnabled = true;
GD.Print("Shadows enabled: " + sun.ShadowEnabled);Practice Configure Light Energy and Shadows
Set sun.ShadowEnabled to true and print the energy value with GD.Print("Enerji: " + sun.LightEnergy);.
Starter code
using System;
public class DirectionalLight3D
{
public bool ShadowEnabled { get; set; } = false;
public float LightEnergy { get; set; } = 1.5f;
}
public static class GD
{
public static void Print(object message) => Console.WriteLine(message);
}
public class Program
{
public static void Main()
{
DirectionalLight3D sun = new DirectionalLight3D();
// Golgeyi ac ve enerji degerini yazdir
}
}Sample solution
using System;
public class DirectionalLight3D
{
public bool ShadowEnabled { get; set; } = false;
public float LightEnergy { get; set; } = 1.5f;
}
public static class GD
{
public static void Print(object message) => Console.WriteLine(message);
}
public class Program
{
public static void Main()
{
DirectionalLight3D sun = new DirectionalLight3D();
sun.ShadowEnabled = true;
GD.Print("Enerji: " + sun.LightEnergy);
}
}Expected output
Enerji: 1.5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which of the following determines the direction light strikes in the scene for a DirectionalLight3D?
- The node's rotation angle Correct answer
- The light's X and Y coordinate position
- The light's scale
- The light's Fov value
Explanation: Because directional light emits parallel rays, its position does not matter; only its rotation determines the incident light angle.
2 Enable Shadows
isik.____ = true;
isik.ShadowEnabled = true;
Explanation: The ShadowEnabled property is set to true to allow light to cast shadows.
3 Light Components Matching
| Term | Meaning |
|---|---|
DirectionalLight3D |
Emits parallel light like the sun |
ShadowEnabled |
Setting to enable light shadows |
LightEnergy |
Brightness and energy of light |
Rotation |
Incident angle of light on scene |