In this lesson, you will learn how to use Light Layers to isolate specific light sources so they only affect designated objects, giving you precise control over your scene's visual composition.
Understanding Light Layers
In a standard Unity scene, a light illuminates every object within its range and intensity. While this is physically accurate for most scenarios, you often face situations—such as a character needing a "rim light" that shouldn't bleed onto the background—where you need selective control. Light Layers allow you to assign lights and renderers to specific "channels," acting as a filter that determines which lights "see" which objects.
Configuring Your Scene
To set this up, think of it as a tagging system for illumination:
- Assigning Layers: Select your light source (e.g., a
Point LightorSpotlight). In the Inspector, locate the Light Layer dropdown. You can choose from 32 different layers. By default, it is set to "Everything." Change this to a specific layer, such asLayer 1. - Filtering Renderers: Select the
MeshRendererof the object you want to be illuminated by that light. In the Rendering section of the component, you will find the Light Layers property. Uncheck "Everything" and select only the layer you assigned to your light.
Now, that object will only respond to lights set to that specific layer, ignoring all others.
Practical Example: Scripting Light Assignment
If you are managing these layers dynamically during gameplay—for instance, changing which light hits a character based on a power-up—you can modify the lightLayer property via script:
using UnityEngine;
public class LightControl : MonoBehaviour
{
public Light dynamicLight;
void SetToLayerOne()
{
// Sets the light to only affect objects on Light Layer 1
dynamicLight.lightLayer = 2; // Bitwise: Layer 1 is index 1, bitmask 2
}
}
This approach is highly performant because it happens at the rendering pipeline level, preventing the GPU from calculating lighting interactions for objects that shouldn't be affected. It keeps your scene lighting clean and artistic, avoiding the common headache of "light spill" in complex environments.
Key Takeaway: Light Layers act as a sophisticated filtering system, allowing you to decouple lighting logic from spatial proximity and grant individual objects their own unique, isolated lighting environments.