--- name: r3f-lighting description: React Three Fiber lighting - light types, shadows, Environment component, IBL. Use when adding lights, configuring shadows, setting up environment lighting, or optimizing lighting performance. --- # React Three Fiber Lighting ## Quick Start ```tsx import { Canvas } from '@react-three/fiber' function Scene() { return ( {/* Ambient fill */} {/* Main light with shadows */} {/* Objects */} {/* Ground */} ) } ``` ## Light Types Overview | Light | Description | Shadow Support | Cost | |-------|-------------|----------------|------| | ambientLight | Uniform everywhere | No | Very Low | | hemisphereLight | Sky/ground gradient | No | Very Low | | directionalLight | Parallel rays (sun) | Yes | Low | | pointLight | Omnidirectional (bulb) | Yes | Medium | | spotLight | Cone-shaped | Yes | Medium | | rectAreaLight | Area light (window) | No* | High | ## ambientLight Illuminates all objects equally. No direction, no shadows. ```tsx ``` ## hemisphereLight Gradient from sky to ground. Good for outdoor scenes. ```tsx ``` ## directionalLight Parallel light rays. Simulates distant light (sun). ```tsx // With target (light points at target) function DirectionalWithTarget() { const lightRef = useRef() return ( <> ) } ``` ## pointLight Emits light in all directions. Like a light bulb. ```tsx ``` ## spotLight Cone-shaped light. Like a flashlight. ```tsx // SpotLight helper import { useHelper } from '@react-three/drei' import { SpotLightHelper } from 'three' function SpotLightWithHelper() { const lightRef = useRef() useHelper(lightRef, SpotLightHelper, 'cyan') return } ``` ## rectAreaLight Rectangular area light. Great for soft, realistic lighting. ```tsx import { RectAreaLightHelper } from 'three/examples/jsm/helpers/RectAreaLightHelper' function AreaLight() { const lightRef = useRef() return ( <> ) } // Note: RectAreaLight only works with MeshStandardMaterial and MeshPhysicalMaterial // Does not cast shadows natively ``` ## Shadow Setup ### Enable Shadows on Canvas ```tsx ``` ### Shadow Types ```tsx // Basic shadows (fastest, hard edges) // PCF shadows (default, filtered) // Soft shadows (PCFSoft, softer edges) // VSM shadows (variance shadow map) ``` ### Configure Shadow-Casting Objects ```tsx // Light must cast shadows // Objects must cast and/or receive shadows // Ground typically only receives ``` ### Shadow Camera Helper ```tsx import { useHelper } from '@react-three/drei' import { CameraHelper } from 'three' function LightWithShadowHelper() { const lightRef = useRef() // Visualize shadow camera frustum useHelper(lightRef.current?.shadow.camera, CameraHelper) return ( ) } ``` ## Drei Lighting Helpers ### Environment HDR environment lighting with presets or custom files. ```tsx import { Environment } from '@react-three/drei' // Preset environments // Custom HDR file // Cube map (6 images) // Ground projection ``` ### Lightformer Create custom light shapes inside Environment. ```tsx import { Environment, Lightformer } from '@react-three/drei' ``` ### Sky Procedural sky with sun. ```tsx import { Sky } from '@react-three/drei' ``` ### Stars Starfield background. ```tsx import { Stars } from '@react-three/drei' ``` ### Stage Quick lighting setup for product showcase. ```tsx import { Stage } from '@react-three/drei' ``` ### ContactShadows Fast fake shadows without shadow mapping. ```tsx import { ContactShadows } from '@react-three/drei' // For animated scenes ``` ### AccumulativeShadows Soft, accumulated shadows. ```tsx import { AccumulativeShadows, RandomizedLight } from '@react-three/drei' ``` ### SoftShadows Enable PCF soft shadows globally. ```tsx import { SoftShadows } from '@react-three/drei' ``` ### BakeShadows Bake shadows for static scenes. ```tsx import { BakeShadows } from '@react-three/drei' {/* Bakes shadows once on mount */} ``` ## Common Lighting Setups ### Three-Point Lighting ```tsx function ThreePointLighting() { return ( <> {/* Key light (main) */} {/* Fill light (softer, opposite side) */} {/* Back light (rim lighting) */} {/* Ambient fill */} ) } ``` ### Outdoor Daylight ```tsx import { Sky, Environment } from '@react-three/drei' function OutdoorLighting() { return ( <> ) } ``` ### Studio Lighting ```tsx import { Environment, Lightformer, ContactShadows } from '@react-three/drei' function StudioLighting() { return ( <> {/* Key light */} {/* Fill light */} {/* Rim light */} ) } ``` ## Animated Lighting ```tsx import { useFrame } from '@react-three/fiber' import { useRef } from 'react' function AnimatedLight() { const lightRef = useRef() useFrame(({ clock }) => { const t = clock.elapsedTime // Orbit around scene lightRef.current.position.x = Math.cos(t) * 5 lightRef.current.position.z = Math.sin(t) * 5 // Pulsing intensity lightRef.current.intensity = 1 + Math.sin(t * 2) * 0.5 // Color cycling lightRef.current.color.setHSL((t * 0.1) % 1, 1, 0.5) }) return ( ) } ``` ## Light Helpers ```tsx import { useHelper } from '@react-three/drei' import { DirectionalLightHelper, PointLightHelper, SpotLightHelper, HemisphereLightHelper, } from 'three' function LightWithHelpers() { const dirLightRef = useRef() const pointLightRef = useRef() const spotLightRef = useRef() const hemiLightRef = useRef() useHelper(dirLightRef, DirectionalLightHelper, 5, 'red') useHelper(pointLightRef, PointLightHelper, 1, 'green') useHelper(spotLightRef, SpotLightHelper, 'blue') useHelper(hemiLightRef, HemisphereLightHelper, 5, 'yellow', 'brown') return ( <> ) } ``` ## Performance Tips 1. **Limit light count**: Each light adds shader complexity 2. **Use baked lighting**: For static scenes 3. **Smaller shadow maps**: 512-1024 often sufficient 4. **Tight shadow frustums**: Only cover needed area 5. **Disable unused shadows**: Not all lights need shadows 6. **Use Environment**: More efficient than many lights ```tsx // Selective shadows // Only update shadows when needed // Use layers to exclude objects from lights {/* Affected by light */} {/* Not affected */} ``` ## See Also - `r3f-materials` - Material light response - `r3f-textures` - Environment maps - `r3f-postprocessing` - Bloom and light effects