Skip to content

Sky

The Sky module provides two ways to create sky backgrounds: procedural atmospheric sky and cubemap skyboxes.

Import

javascript
import { Sky, Skybox } from 'mage-engine';

Procedural Sky

The Sky class creates a realistic atmospheric sky with dynamic sun positioning.

Constructor

javascript
const sky = new Sky(options);

Options

ParameterTypeDefaultDescription
scalenumber10000Sky sphere scale
turbiditynumber10Atmospheric turbidity (haziness)
rayleighnumber0.8Rayleigh scattering coefficient
mieCoefficientnumber0.005Mie scattering coefficient
mieDirectionalGnumber0.8Mie directional scattering
sunInclinationnumber0.49Sun vertical position (0-1)
sunAzimuthnumber0.205Sun horizontal position (0-1)
sunDistancenumber100Distance to sun

Methods

javascript
sky.setTurbidity(value)           // Set haziness (2-10)
sky.setRayleigh(value)            // Set scattering (0-4)
sky.setLuminance(value)           // Set brightness
sky.setMieCoefficient(value)      // Set Mie coefficient
sky.setMieDirectionalG(value)     // Set Mie directionality
sky.setSun(inclination, azimuth, distance)  // Position sun

Example: Basic Sky

javascript
import { Sky, Level } from 'mage-engine';

class OutdoorLevel extends Level {
    onCreate() {
        const sky = new Sky({
            turbidity: 10,
            rayleigh: 2,
            sunInclination: 0.4,
            sunAzimuth: 0.25
        });
    }
}

Example: Day/Night Cycle

javascript
class DayNightLevel extends Level {
    onCreate() {
        this.sky = new Sky();
        this.timeOfDay = 0.3;  // Start at morning
    }
    
    update(dt) {
        // Advance time
        this.timeOfDay += dt * 0.01;
        if (this.timeOfDay > 1) this.timeOfDay = 0;
        
        // Update sun position
        const inclination = Math.sin(this.timeOfDay * Math.PI);
        this.sky.setSun(inclination, 0.25, 100);
        
        // Adjust atmosphere based on time
        if (this.timeOfDay < 0.2 || this.timeOfDay > 0.8) {
            // Dawn/dusk - more colorful
            this.sky.setRayleigh(3);
            this.sky.setTurbidity(10);
        } else {
            // Midday - clearer
            this.sky.setRayleigh(1);
            this.sky.setTurbidity(5);
        }
    }
}

Sun Position Guide

TimeInclinationEffect
0.0NightBelow horizon
0.25SunriseGolden hour
0.5NoonHigh sun
0.75SunsetGolden hour
1.0NightBelow horizon

Skybox

The Skybox class creates a sky using a cubemap texture (6 images).

Constructor

javascript
const skybox = new Skybox(options);

Options

ParameterTypeDefaultDescription
namestringauto-generatedUnique identifier
texturestring'skybox'Cubemap texture name from assets

Loading Skybox Textures

Cubemap textures require 6 images for each face:

javascript
// In your assets configuration
const assets = {
    cubetextures: {
        'skybox': [
            'skybox/px.jpg',  // Positive X (right)
            'skybox/nx.jpg',  // Negative X (left)
            'skybox/py.jpg',  // Positive Y (top)
            'skybox/ny.jpg',  // Negative Y (bottom)
            'skybox/pz.jpg',  // Positive Z (front)
            'skybox/nz.jpg'   // Negative Z (back)
        ]
    }
};

Example: Basic Skybox

javascript
import { Skybox, Level } from 'mage-engine';

class SpaceLevel extends Level {
    onCreate() {
        const skybox = new Skybox({ 
            texture: 'spaceSkybox' 
        });
    }
}

Example: Multiple Skyboxes

javascript
const assets = {
    cubetextures: {
        'daySky': ['day/px.jpg', 'day/nx.jpg', 'day/py.jpg', 'day/ny.jpg', 'day/pz.jpg', 'day/nz.jpg'],
        'nightSky': ['night/px.jpg', 'night/nx.jpg', 'night/py.jpg', 'night/ny.jpg', 'night/pz.jpg', 'night/nz.jpg']
    }
};

class EnvironmentLevel extends Level {
    onCreate() {
        // Start with day skybox
        this.currentSkybox = new Skybox({ texture: 'daySky' });
    }
    
    switchToNight() {
        this.currentSkybox.dispose();
        this.currentSkybox = new Skybox({ texture: 'nightSky' });
    }
}

Choosing Between Sky Types

FeatureProcedural SkySkybox
Dynamic sun
Day/night cycleManual swap
PerformanceSlightly heavierLighter
CustomizationParametersFixed image
RealismAtmosphericDepends on art
Best forOutdoor, realisticSpace, stylized

Complete Example

javascript
import { Level, Sky, SunLight, AmbientLight } from 'mage-engine';

class RealisticOutdoor extends Level {
    onCreate() {
        // Create procedural sky
        this.sky = new Sky({
            turbidity: 8,
            rayleigh: 2.5,
            sunInclination: 0.4,
            sunAzimuth: 0.25
        });
        
        // Add matching sun light
        const sun = new SunLight({
            color: 0xffffcc,
            intensity: 1.0,
            position: { x: 100, y: 100, z: 50 },
            castShadow: true
        });
        
        // Add ambient for fill
        const ambient = new AmbientLight({
            color: 0x404080,
            intensity: 0.3
        });
    }
}

See Also