In this lesson, you will learn how to use Light Layers to restrict specific light sources to illuminate only designated groups of game objects, giving you granular control over your scene's lighting hierarchy.
The Concept of Light Layers
By default, every light in your scene illuminates every object within its range. However, complex scenes often require "artistic lighting"—where a light should highlight a character without spilling onto the background, or where specific glowing elements need their own dedicated light source that ignores the environment.
Light Layers act as a filter. By assigning both a Light component and a Renderer component to the same bitmasked layer, you instruct the Universal Render Pipeline (URP) to evaluate lighting calculations only for that specific pair.
Implementation Workflow
- Define your Layers: Navigate to the URP Asset settings. Under the Lighting section, ensure Light Layers is enabled.
- Assign the Layer: Select a Light (e.g., a Spot Light). In the Inspector, locate the Light Layer dropdown and select the specific layer (e.g., "Layer 1").
- Filter the Renderer: Select the
MeshRendererof the target game object. In the Additional Settings foldout, locate the Light Layer dropdown and select that same "Layer 1."
If you have a character on "Layer 1" and a wall on "Default," a Light set to "Layer 1" will illuminate the character perfectly while the wall remains completely unaffected by that light source.
Scripting Light Layers
You can also toggle these layers dynamically during gameplay to create dramatic effects, such as a spotlight that "finds" a player by switching its mask:
using UnityEngine.Rendering.Universal;
public class LightMaskSwitcher : MonoBehaviour
{
public Light targetLight;
void SetLightLayer(int layerBitmask)
{
// LightLayerEnum is a bitmask; use bitwise shifts (1 << layerIndex)
targetLight.lightLayers = (LightLayerEnum)layerBitmask;
}
}
This technique is invaluable for optimizing performance and achieving specific cinematic looks without needing to move light sources or adjust complex attenuation values.
Key Takeaway: Light Layers allow you to decouple lighting from physical proximity, enabling precise artistic control by masking which lights affect which specific meshes.