Unlocking the Power of Lighting in Unity
Understanding Direct and Indirect Lighting
Before we dive into the different types of lights in Unity, it’s essential to understand the concept of direct and indirect lighting. Direct lighting refers to light that has zero or one bounce, while indirect lighting has two or more bounces before hitting the observer’s eyes. In Unity, indirect light with four bounces and above is known as Global Illumination (GI).
Types of Lights in Unity
Unity offers four types of lights: Directional Light, Point Light, Spot Light, and Area Light. Each light type has its unique properties and effects, and understanding when to use each one is crucial for achieving the desired lighting effect.
- Directional Light: Represents large, distant sources that come from a position outside the range of the game world. Used to simulate the effect of the sun on a scene.
- Point Light: Located at a point in space and equally sends light out in all directions. Used to create streetlights, campfires, or lamps.
- Spot Light: Emits light in one direction and is constrained to a specific angle, resulting in a cone-shaped region of light. Used to create headlight of a car or spotlight effects.
- Area Light: Emitted in a rectangular shape and needs to be baked before taking effect. Used to create realistic lighting for scenes.
using UnityEngine;
public class LightExample : MonoBehaviour
{
// Create a directional light
public Light directionalLight = new Light(LightType.Directional);
// Create a point light
public Light pointLight = new Light(LightType.Point);
// Create a spot light
public Light spotLight = new Light(LightType.Spot);
// Create an area light
public Light areaLight = new Light(LightType.Area);
}
Light Modes in Unity
Unity offers three main light modes: Real-time, Baked, and Mixed. Understanding the differences between these modes is crucial for achieving the desired lighting effect.
- Real-time Lighting: Calculates and updates lighting in each frame. Used for illuminating characters or movable geometry.
- Baked Lighting: Performs lighting calculations in the editor and stores the data as a texture. Used for simulating realistic lighting for scenes while reducing CPU and GPU costs.
- Mixed Lighting: Combines Real-time and Baked Lighting, allowing a single light source to provide both direct and indirect light.
using UnityEngine;
public class LightModeExample : MonoBehaviour
{
// Set the light mode to real-time
public Light light = new Light();
light.lightmapMode = LightMapMode.Realtime;
// Set the light mode to baked
light.lightmapMode = LightMapMode.Baked;
// Set the light mode to mixed
light.lightmapMode = LightMapMode.Mixed;
}