Using RectAreaLight in Three.js for Realistic Lighting
This article explains how to implement and configure the
RectAreaLight in Three.js to create realistic studio and
window lighting effects. You will learn the essential setup steps,
including the initialization of required libraries, code implementation,
material compatibility, and tips for optimizing performance when using
this physically-based light source.
What is RectAreaLight?
In 3D graphics, a RectAreaLight emits light from a
rectangular plane rather than a single point or infinitely distant
source. This makes it ideal for simulating real-world light sources like
bright rectangular windows, softbox studio lights, and overhead
fluorescent panels. Unlike point lights, rectangular lights produce
realistic, soft-edged specular reflections on shiny surfaces.
Step 1: Initialize the RectAreaLight Library
Unlike standard lights in Three.js, RectAreaLight
requires an external helper library to render correctly on materials.
Without this initialization, the light will not interact with surfaces
properly.
You must import and initialize the
RectAreaLightUniformsLib before creating your scene:
import * as THREE from 'three';
import { RectAreaLightUniformsLib } from 'three/examples/jsm/lights/RectAreaLightUniformsLib.js';
// Initialize the uniforms library
RectAreaLightUniformsLib.init();Step 2: Create and Position the Light
Once the library is initialized, you can create the light by defining its color, intensity, width, and height.
// Parameters: Color, Intensity, Width, Height
const rectLight = new THREE.RectAreaLight(0xffffff, 5, 4, 2);
// Position the light in the scene
rectLight.position.set(5, 5, 0);
// Point the light toward the center of the scene or a specific object
rectLight.lookAt(0, 0, 0);
// Add the light to your scene
scene.add(rectLight);The intensity of a RectAreaLight is
measured in lumens or nits, meaning you may need to set higher intensity
values compared to standard directional lights to achieve the desired
brightness.
Step 3: Use Compatible Materials
RectAreaLight only works with materials that support
complex physical lighting calculations. You must use either
MeshStandardMaterial or MeshPhysicalMaterial
on the 3D models in your scene.
const material = new THREE.MeshStandardMaterial({
color: 0x808080,
roughness: 0.2,
metalness: 0.8
});Using materials like MeshBasicMaterial or
MeshLambertMaterial will result in the light having no
visual effect on those objects.
Step 4: Visualize the Light Source with a Helper
To make positioning easier during development, you can add a
RectAreaLightHelper to visualize the boundaries and
direction of the light plane.
import { RectAreaLightHelper } from 'three/examples/jsm/helpers/RectAreaLightHelper.js';
const rectLightHelper = new RectAreaLightHelper(rectLight);
rectLight.add(rectLightHelper);Limitations and Performance
While RectAreaLight produces highly realistic
reflections and illumination, there are two key limitations to keep in
mind:
- No Shadow Maps: In Three.js,
RectAreaLightdoes not support real-time shadow casting (shadow maps). To achieve shadows with this light, you must combine it with a subtle directional light or bake ambient occlusion maps into your 3D models. - Performance Cost: These lights are computationally expensive because they require complex pixel-by-pixel shader calculations. Limit the number of active rectangular lights in your scene to maintain a smooth frame rate, especially on mobile devices.