Skip to content

feat: Level of Detail (LOD) helper in @webgamekit/threejs #121

Description

@cnotv

Parent epic

#19

Problem

Distant objects are rendered at full polygon count even when individual triangles are sub-pixel. This wastes GPU vertex throughput. Three.js provides THREE.LOD but there is no shared helper to configure it consistently across scenes.

Goal

A composable LOD factory in packages/threejs/src/ that makes distance-based mesh swapping trivial to adopt, including an "impostor" tier (billboard sprite) for maximum-distance objects.

Plan

1. packages/threejs/src/lod.ts

/**
 * Creates a THREE.LOD object from an ordered array of mesh+distance pairs.
 * @param levels - array of { mesh, distance } sorted near → far
 * @returns THREE.LOD ready to add to the scene
 */
export const createLOD = (levels: LODLevel[]): THREE.LOD => {  }

/**
 * Creates a billboard impostor sprite from a texture for the lowest LOD tier.
 */
export const createImpostorSprite = (texture: THREE.Texture, size: number): THREE.Sprite => {  }
  • Validates that levels are sorted ascending by distance
  • JSDoc on all exports (required by jsdoc/require-jsdoc rule)

2. Export from packages/threejs/src/index.ts

3. Apply in one existing scene

  • Target: ContinuousWorld or ForestGame — both have distant repeated objects
  • Replace furthest-tier objects with impostor sprites
  • Verify triangle count drops in the stats panel at distance

4. Tests (packages/threejs/src/lod.test.ts)

  • createLOD assigns the correct distances to each level
  • Levels added in wrong order throw or warn
  • createImpostorSprite returns a THREE.Sprite

Acceptance criteria

  • createLOD and createImpostorSprite exported from @webgamekit/threejs
  • At least one scene uses LOD; triangle count reduces at distance in stats panel
  • Unit tests pass
  • All exports have JSDoc

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions