8 minutes
Lighting Component System
Let's walk through building a lighting component system that adds ambient and directional lighting to your scene. These will be your work lights and are usually all someone needs to simulate the outdoors.
Understanding Babylon.js Lighting Classes
Before building our ECS lighting system, let's explore the different lighting options available in Babylon.js. Each light type serves different purposes and creates different visual effects in your 3D scenes.
HemisphericLight (Ambient Lighting)
HemisphericLight provides soft, even lighting that simulates light coming from all directions - like outdoor ambient light or light bouncing off surfaces. It's perfect for general scene illumination without harsh shadows.
Key Properties:
intensity: Controls brightness (0-10+)diffuse: Main light color affecting object surfacesspecular: Color of reflective highlightsgroundColor: Color of light coming from below
DirectionalLight (Sun-like Lighting)
DirectionalLight simulates parallel light rays coming from a specific direction - like sunlight. It's essential for realistic outdoor scenes and casting directional shadows.
Key Properties:
direction: Vector pointing in the light directionposition: Used for shadow calculations (not light direction)autoCalcShadowZBounds: Automatically optimizes shadow renderingshadowMinZ/shadowMaxZ: Control shadow rendering distance
SpotLight (Focused Cone Lighting)
SpotLight creates a cone of light from a specific position - like a flashlight, stage light, or car headlight. It's perfect for focused illumination and dramatic effects.
Key Properties:
position: Where the light is locateddirection: Which way the light pointsangle: Width of the light cone (in radians)exponent: How sharply the light fades at edgesrange: Maximum distance the light travels
PointLight (Omnidirectional Lighting)
PointLight radiates light in all directions from a single point - like a light bulb, candle, or lamp. It's ideal for interior lighting and local illumination.
Key Properties:
position: Location of the light sourcerange: Maximum distance the light reachesdiffuse/specular: Color properties like other lights- Perfect for creating multiple colored lights in a scene
Light Animation and Control
All lights can be animated and controlled dynamically:
Light Performance Considerations
Light Limits: Most devices support 4-8 dynamic lights per material. Use light limiting or light baking for scenes with many lights.
Shadow Performance: Only DirectionalLight and SpotLight support shadows efficiently. Limit shadow-casting lights for better performance.
Light Culling: Lights outside the camera view or object influence range are automatically culled by Babylon.js.
Let's start by creating the new Component file.
Lighting in Babylon.js
First, we need to import everything we'll use from Babylon.js and our ECS framework:
We're pulling in our ECS building blocks and two key lighting types: HemisphericLight for ambient lighting and DirectionalLight for shadows and directional illumination.
Lighting Component Options
Next, we define what configuration our lighting component needs:
We specify which light types to create and the position offset for our lights.
Creating the Lighting Component
Now we build the component class that holds our lighting configuration:
The component stores the light types we want, an array to hold the created lights, and the offset position. We provide sensible defaults for a typical lighting setup.
Setting Up the Lighting System
The system handles the lighting logic. Let's start with the constructor:
This tells the system to process any entity that has a LightingComponent attached.
Loading the Lighting Entity
When an entity with a lighting component gets added, this method creates the actual lights:
We grab the lighting component, set the loading flag, and convert our offset array into a proper Babylon.js Vector3 for positioning.
Creating Each Light Type
Now we iterate through the requested light types and create them:
For ambient lights, we create a HemisphericLight at our offset position. The intensity is adjusted based on whether we have multiple lights - lower intensity when combined with other lights to avoid over-brightening.
Adding Directional Lighting
Next, we handle directional lights for shadows and realistic lighting:
Directional lights provide parallel light rays (like sunlight) and support shadow casting. The autoCalcShadowZBounds setting automatically optimizes shadow rendering boundaries.
Finishing the Loading Process
Finally, we mark the lighting as loaded and provide confirmation:
We clear the loading flag, set the loaded flag, and log confirmation that our lighting system is ready.
In Sum
This lighting system provides flexible illumination for your 3D scenes. It supports both ambient lighting (for general scene brightness) and directional lighting (for realistic shadows and depth). We'll get to the shadows later.
The system automatically adjusts light intensities when multiple lights are used together, preventing over-exposure while maintaining good visual quality.