agentleFS
bullish0xbullish0x/GameStudio/.agents/skills/threejs-lighting/SKILL.md

threejs-lighting

Skill29 starsChanged 6 months agoMITView on GitHub

Comprehensive lighting systems for Three.js including ambient, directional, point, spot, and hemisphere lights with optimization strategies

25 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use threejs-performance, threejs-asset-pipeline and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • alton47Skill

    threejs-performance

    Optimizes Three.js performance — instancing, draw call reduction, LOD, frustum culling, geometry merging, texture compression, Stats.js profiling, and GPU debugging. Use when the user asks about performance, FPS, draw calls, instancing, optimization, lag, stuttering, or profiling a Three.js scene. Trigger keywords: performance, fps, draw calls, instancing, InstancedMesh, LOD, profiling, optimization, Stats.

    Also in 7 projects that use this

  • CloudAI-XSkill

    threejs-asset-pipeline

    Shrinking glTF assets for three.js with gltf-transform: inspect, validate and optimize commands, Draco, meshopt and quantization, KTX2, WebP and AVIF textures, the loader setup each output needs, and gltfjsx for React. Use when compressing, optimizing or validating 3D models before loading them.

    Also in 4 projects that use this

  • CloudAI-XSkill

    threejs-react-three-fiber

    Building three.js scenes from React with React Three Fiber and drei: renderer setup, the scene graph as JSX, frame-loop, state and loading hooks, pointer events, and adaptive performance, on WebGL or with WebGPU and TSL. Use when writing React components that render three.js.

    Also in 4 projects that use this

  • LinHao-citySkill

    bfl-api

    BFL FLUX API integration guide covering endpoints, async polling patterns, rate limiting, error handling, webhooks, and regional endpoints with Python and TypeScript code examples.

    Also in 3 projects that use this

  • LinHao-citySkill

    flux-best-practices

    Comprehensive guide for BFL FLUX image generation models. Covers prompting, T2I, I2I, structured JSON, hex colors, typography, multi-reference editing, and model-specific best practices for FLUX.2 and FLUX.1 families.

    Also in 3 projects that use this

  • jaron-goh-centrSkill

    website-to-video

    Capture a general website/URL and turn it into a video OF the site — tour, showcase, or social clip built from captured screenshots and the site's own brand assets. Use for portfolio / blog / docs / landing-page showcases. Not a product launch or promo, even from a URL (/product-launch-video). Unclear → /hyperframes.

    Also in 4 projects that use this

  • +6 more from LinHao-city

The file

We split this skill into the rules it gives an agent

8 rules. 0 are common, 8 are distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 24 edited versions we read keep these.

  • 8

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use this skill when:

    line 12
  2. Distinctive:

    Realism: Use physically-based lighting values

    line 24
  3. Distinctive:

    Color Temperature: Use realistic color temperatures

    line 26
  4. Distinctive:

    Use ambient/hemisphere for fill instead of multiple lights

    line 614
  5. Distinctive:

    Use light pools for dynamic lights

    line 616
  6. Distinctive:

    Use lower shadow map sizes for point lights (512-1024)

    line 618
  7. Distinctive:

    Use directional lights for sun (one shadow map vs 6 for point)

    line 619
  8. Distinctive:

    Use hemisphere light instead of ambient + directional

    line 621

“Common” is measured: kept by at least 6 in 10 of the 24 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Three.js Lighting

When to Use

Use this skill when:

  • Setting up scene lighting
  • Creating realistic lighting environments
  • Implementing dynamic lighting
  • Optimizing light performance
  • Creating day/night cycles
  • Implementing volumetric lighting

Core Principles

  1. Layered Lighting: Combine multiple light types
  2. Performance: Limit active lights (<8 for mobile)
  3. Realism: Use physically-based lighting values
  4. Optimization: Bake static lights when possible
  5. Color Temperature: Use realistic color temperatures
  6. Intensity: Scale with inverse square law for realism

Implementation

1. Light Manager

// lights/LightManager.ts
import * as THREE from 'three';

export interface LightConfig {
  color?: number | string;
  intensity?: number;
  position?: THREE.Vector3;
  target?: THREE.Vector3;
  castShadow?: boolean;
}

export class LightManager {
  private scene: THREE.Scene;
  private lights = new Map<string, THREE.Light>();

  constructor(scene: THREE.Scene) {
    this.scene = scene;
  }

  addAmbientLight(id: string, config: LightConfig = {}): THREE.AmbientLight {
    const light = new THREE.AmbientLight(
      config.color ?? 0xffffff,
      config.intensity ?? 0.5
    );

    this.lights.set(id, light);
    this.scene.add(light);

    return light;
  }

  addDirectionalLight(id: string, config: LightConfig = {}): THREE.DirectionalLight {
    const light = new THREE.DirectionalLight(
      config.color ?? 0xffffff,
      config.intensity ?? 1
    );

    if (config.position) {
      light.position.copy(config.position);
    }

    if (config.target) {
      light.target.position.copy(config.target);
      this.scene.add(light.target);
    }

    if (config.castShadow) {
      light.castShadow = true;
      light.shadow.mapSize.width = 2048;
      light.shadow.mapSize.height = 2048;
      light.shadow.camera.near = 0.5;
      light.shadow.camera.far = 500;
      light.shadow.camera.left = -50;
      light.shadow.camera.right = 50;
      light.shadow.camera.top = 50;
      light.shadow.camera.bottom = -50;
    }

    this.lights.set(id, light);
    this.scene.add(light);

    return light;
  }

  addPointLight(id: string, config: LightConfig & { distance?: number; decay?: number } = {}): THREE.PointLight {
    const light = new THREE.PointLight(
      config.color ?? 0xffffff,
      config.intensity ?? 1,
      config.distance ?? 0,
      config.decay ?? 2
    );

    if (config.position) {
      light.position.copy(config.position);
    }

    if (config.castShadow) {
      light.castShadow = true;
      light.shadow.mapSize.width = 1024;
      light.shadow.mapSize.height = 1024;
      light.shadow.camera.near = 0.5;
      light.shadow.camera.far = 500;
    }

    this.lights.set(id, light);
    this.scene.add(light);

    return light;
  }

  addSpotLight(
    id: string,
    config: LightConfig & {
      distance?: number;
      angle?: number;
      penumbra?: number;
      decay?: number;
    } = {}
  ): THREE.SpotLight {
    const light = new THREE.SpotLight(
      config.color ?? 0xffffff,
      config.intensity ?? 1,
      config.distance ?? 0,
      config.angle ?? Math.PI / 3,
      config.penumbra ?? 0,
      config.decay ?? 2
    );

    if (config.position) {
      light.position.copy(config.position);
    }

    if (config.target) {
      light.target.position.copy(config.target);
      this.scene.add(light.target);
    }

    if (config.castShadow) {
      light.castShadow = true;
      light.shadow.mapSize.width = 1024;
      light.shadow.mapSize.height = 1024;
      light.shadow.camera.near = 0.5;
      light.shadow.camera.far = 500;
    }

    this.lights.set(id, light);
    this.scene.add(light);

    return light;
  }

  addHemisphereLight(
    id: string,
    skyColor: number | string = 0x87ceeb,
    groundColor: number | string = 0x654321,
    intensity: number = 1
  ): THREE.HemisphereLight {
    const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);

    this.lights.set(id, light);
    this.scene.add(light);

    return light;
  }

  getLight(id: string): THREE.Light | undefined {
    return this.lights.get(id);
  }

  removeLight(id: string): void {
    const light = this.lights.get(id);
    if (light) {
      this.scene.remove(light);
      this.lights.delete(id);
      light.dispose();
    }
  }

  setLightIntensity(id: string, intensity: number): void {
    const light = this.lights.get(id);
    if (light) {
      light.intensity = intensity;
    }
  }

  setLightColor(id: string, color: number | string): void {
    const light = this.lights.get(id);
    if (light) {
      light.color.set(color);
    }
  }

  dispose(): void {
    this.lights.forEach((light) => {
      this.scene.remove(light);
      light.dispose();
    });
    this.lights.clear();
  }
}

2. Day/Night Cycle

// lights/DayNightCycle.ts
import * as THREE from 'three';

export interface TimeOfDay {
  hour: number; // 0-24
  sunColor: THREE.Color;
  skyColor: THREE.Color;
  groundColor: THREE.Color;
  ambientIntensity: number;
  sunIntensity: number;
  sunPosition: THREE.Vector3;
}

export class DayNightCycle {
  private currentTime = 12; // Noon
  private timeSpeed = 1; // Hours per second
  private cycleLength = 24; // Hours

  private sunLight: THREE.DirectionalLight;
  private hemisphereLight: THREE.HemisphereLight;
  private ambientLight: THREE.AmbientLight;

  private readonly timeSteps: TimeOfDay[] = [
    // Midnight
    {
      hour: 0,
      sunColor: new THREE.Color(0x0c1445),
      skyColor: new THREE.Color(0x0a0a1e),
      groundColor: new THREE.Color(0x050508),
      ambientIntensity: 0.1,
      sunIntensity: 0,
      sunPosition: new THREE.Vector3(0, -1, 0),
    },
    // Dawn
    {
      hour: 6,
      sunColor: new THREE.Color(0xff6347),
      skyColor: new THREE.Color(0xff7f50),
      groundColor: new THREE.Color(0x2f1810),
      ambientIntensity: 0.3,
      sunIntensity: 0.5,
      sunPosition: new THREE.Vector3(1, 0, 0),
    },
    // Noon
    {
      hour: 12,
      sunColor: new THREE.Color(0xfffaf0),
      skyColor: new THREE.Color(0x87ceeb),
      groundColor: new THREE.Color(0x654321),
      ambientIntensity: 0.5,
      sunIntensity: 1,
      sunPosition: new THREE.Vector3(0, 1, 0),
    },
    // Dusk
    {
      hour: 18,
      sunColor: new THREE.Color(0xff4500),
      skyColor: new THREE.Color(0xff6347),
      groundColor: new THREE.Color(0x1a0f08),
      ambientIntensity: 0.3,
      sunIntensity: 0.5,
      sunPosition: new THREE.Vector3(-1, 0, 0),
    },
    // Midnight (wrap)
    {
      hour: 24,
      sunColor: new THREE.Color(0x0c1445),
      skyColor: new THREE.Color(0x0a0a1e),
      groundColor: new THREE.Color(0x050508),
      ambientIntensity: 0.1,
      sunIntensity: 0,
      sunPosition: new THREE.Vector3(0, -1, 0),
    },
  ];

  constructor(
    sunLight: THREE.DirectionalLight,
    hemisphereLight: THREE.HemisphereLight,
    ambientLight: THREE.AmbientLight
  ) {
    this.sunLight = sunLight;
    this.hemisphereLight = hemisphereLight;
    this.ambientLight = ambientLight;
  }

  update(deltaTime: number): void {
    this.currentTime += this.timeSpeed * deltaTime;

    if (this.currentTime >= this.cycleLength) {
      this.currentTime = 0;
    }

    this.applyTimeOfDay(this.currentTime);
  }

  private applyTimeOfDay(hour: number): void {
    // Find surrounding time steps
    let prevStep = this.timeSteps[0];
    let nextStep = this.timeSteps[1];

    for (let i = 0; i < this.timeSteps.length - 1; i++) {
      if (hour >= this.timeSteps[i].hour && hour < this.timeSteps[i + 1].hour) {
        prevStep = this.timeSteps[i];
        nextStep = this.timeSteps[i + 1];
        break;
      }
    }

    // Interpolate
    const duration = nextStep.hour - prevStep.hour;
    const elapsed = hour - prevStep.hour;
    const t = elapsed / duration;

    // Colors
    this.sunLight.color.lerpColors(prevStep.sunColor, nextStep.sunColor, t);
    this.hemisphereLight.color.lerpColors(prevStep.skyColor, nextStep.skyColor, t);
    this.hemisphereLight.groundColor.lerpColors(
      prevStep.groundColor,
      nextStep.groundColor,
      t
    );

    // Intensities
    this.ambientLight.intensity = THREE.MathUtils.lerp(
      prevStep.ambientIntensity,
      nextStep.ambientIntensity,
      t
    );
    this.sunLight.intensity = THREE.MathUtils.lerp(
      prevStep.sunIntensity,
      nextStep.sunIntensity,
      t
    );

    // Position
    this.sunLight.position.lerpVectors(prevStep.sunPosition, nextStep.sunPosition, t);
    this.sunLight.position.multiplyScalar(50);
  }

  setTime(hour: number): void {
    this.currentTime = hour % this.cycleLength;
  }

  setTimeSpeed(speed: number): void {
    this.timeSpeed = speed;
  }

  getCurrentTime(): number {
    return this.currentTime;
  }
}

3. Light Probe System

// lights/LightProbeSystem.ts
import * as THREE from 'three';

export class LightProbeSystem {
  private scene: THREE.Scene;
  private lightProbe: THREE.LightProbe;

  constructor(scene: THREE.Scene) {
    this.scene = scene;
    this.lightProbe = new THREE.LightProbe();
    this.scene.add(this.lightProbe);
  }

  async fromCubeRenderTarget(cubeRenderTarget: THREE.WebGLCubeRenderTarget): Promise<void> {
    const generator = new THREE.PMREMGenerator(cubeRenderTarget.texture.source.data);
    const cubeUV = generator.fromCubemap(cubeRenderTarget.texture);

    this.lightProbe.copy(THREE.LightProbeGenerator.fromCubeRenderTarget(cubeRenderTarget));

    generator.dispose();
  }

  async fromEquirectangular(texture: THREE.Texture, renderer: THREE.WebGLRenderer): Promise<void> {
    const generator = new THREE.PMREMGenerator(renderer);
    const renderTarget = generator.fromEquirectangular(texture);

    this.lightProbe.copy(THREE.LightProbeGenerator.fromCubeRenderTarget(renderTarget));

    generator.dispose();
    renderTarget.dispose();
  }

  setIntensity(intensity: number): void {
    this.lightProbe.intensity = intensity;
  }

  dispose(): void {
    this.scene.remove(this.lightProbe);
  }
}

4. Dynamic Light Pool

// lights/DynamicLightPool.ts
import * as THREE from 'three';

export class DynamicLightPool {
  private scene: THREE.Scene;
  private availableLights: THREE.PointLight[] = [];
  private activeLights = new Map<string, THREE.PointLight>();
  private maxLights: number;

  constructor(scene: THREE.Scene, maxLights: number = 8) {
    this.scene = scene;
    this.maxLights = maxLights;

    // Pre-create lights
    for (let i = 0; i < maxLights; i++) {
      const light = new THREE.PointLight(0xffffff, 0, 10, 2);
      light.visible = false;
      this.scene.add(light);
      this.availableLights.push(light);
    }
  }

  requestLight(
    id: string,
    position: THREE.Vector3,
    color: number | string,
    intensity: number,
    distance: number = 10
  ): THREE.PointLight | null {
    // If light already active, update it
    if (this.activeLights.has(id)) {
      const light = this.activeLights.get(id)!;
      light.position.copy(position);
      light.color.set(color);
      light.intensity = intensity;
      light.distance = distance;
      return light;
    }

    // Get available light
    const light = this.availableLights.pop();
    if (!light) {
      console.warn('No available lights in pool');
      return null;
    }

    // Configure light
    light.position.copy(position);
    light.color.set(color);
    light.intensity = intensity;
    light.distance = distance;
    light.visible = true;

    this.activeLights.set(id, light);

    return light;
  }

  releaseLight(id: string): void {
    const light = this.activeLights.get(id);
    if (light) {
      light.visible = false;
      light.intensity = 0;
      this.activeLights.delete(id);
      this.availableLights.push(light);
    }
  }

  updateLight(id: string, position: THREE.Vector3, intensity?: number): void {
    const light = this.activeLights.get(id);
    if (light) {
      light.position.copy(position);
      if (intensity !== undefined) {
        light.intensity = intensity;
      }
    }
  }

  dispose(): void {
    this.activeLights.forEach((light) => {
      this.scene.remove(light);
      light.dispose();
    });
    this.availableLights.forEach((light) => {
      this.scene.remove(light);
      light.dispose();
    });
    this.activeLights.clear();
    this.availableLights = [];
  }
}

Usage Examples

// Example 1: Basic three-point lighting
import { LightManager } from './lights/LightManager';

const lightManager = new LightManager(scene);

// Key light (main light)
lightManager.addDirectionalLight('key', {
  color: 0xffffff,
  intensity: 1,
  position: new THREE.Vector3(5, 10, 5),
  castShadow: true,
});

// Fill light (soften shadows)
lightManager.addDirectionalLight('fill', {
  color: 0x8888ff,
  intensity: 0.4,
  position: new THREE.Vector3(-5, 5, 5),
});

// Back light (rim light)
lightManager.addDirectionalLight('back', {
  color: 0xffffff,
  intensity: 0.3,
  position: new THREE.Vector3(0, 5, -10),
});

// Ambient
lightManager.addAmbientLight('ambient', {
  color: 0x404040,
  intensity: 0.2,
});

// Example 2: Day/night cycle
import { DayNightCycle } from './lights/DayNightCycle';

const sunLight = lightManager.getLight('sun') as THREE.DirectionalLight;
const hemiLight = lightManager.getLight('hemi') as THREE.HemisphereLight;
const ambientLight = lightManager.getLight('ambient') as THREE.AmbientLight;

const dayNight = new DayNightCycle(sunLight, hemiLight, ambientLight);
dayNight.setTime(12); // Start at noon
dayNight.setTimeSpeed(0.1); // 0.1 hours per second

function animate() {
  dayNight.update(deltaTime);
  renderer.render(scene, camera);
}

// Example 3: Dynamic light pool
import { DynamicLightPool } from './lights/DynamicLightPool';

const lightPool = new DynamicLightPool(scene, 8);

// Spawn light
lightPool.requestLight(
  'torch_1',
  new THREE.Vector3(5, 2, 5),
  0xff6600,
  2,
  10
);

// Update light position
lightPool.updateLight('torch_1', playerPosition);

// Remove light
lightPool.releaseLight('torch_1');

Checklist

  • Set up ambient light for base illumination
  • Add directional light for sun/moon
  • Configure shadow casting for main lights
  • Add point lights for localized lighting
  • Implement hemisphere light for outdoor scenes
  • Limit total active lights (< 8 for mobile)
  • Use light pools for dynamic lights
  • Bake static lighting when possible
  • Test lighting on target devices
  • Optimize shadow map sizes
  • Use realistic color temperatures
  • Profile light performance

Common Pitfalls

  1. Too many lights: Performance drops rapidly
  2. No ambient light: Pure black shadows
  3. Wrong color temperatures: Unrealistic look
  4. High shadow map sizes: Memory/performance issues
  5. Not limiting light distance: Affects unnecessary objects
  6. Forgetting to dispose: Memory leaks
  7. Static lights in update loop: Wasted CPU

Performance Tips

  • Limit total lights to 4-8 (mobile) or 8-16 (desktop)
  • Use ambient/hemisphere for fill instead of multiple lights
  • Bake static lighting into lightmaps
  • Use light pools for dynamic lights
  • Disable shadows on distant lights
  • Use lower shadow map sizes for point lights (512-1024)
  • Use directional lights for sun (one shadow map vs 6 for point)
  • Turn off lights that are out of view
  • Use hemisphere light instead of ambient + directional

Related Skills

  • threejs-scene-setup - Scene initialization
  • threejs-shadows - Shadow configuration
  • threejs-material-systems - Material lighting response
  • mobile-performance - Mobile optimization

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.