A demonstration of Entity Component System (ECS) architecture combined with Three.js for 3D rendering.
This project showcases a clean separation between ECS world logic and rendering concerns. It uses:
- bitECS: A lightweight, zero-dependency Entity Component System for JavaScript
- Three.js: For 3D rendering and visualization
- Vite: For fast development and builds
The application features a simple 3D scene with entities that can move and rotate, demonstrating ECS principles in practice.
- 🎮 Entity Component System: Pure ECS implementation with BiTECS
- 🎨 3D Visualization: Real-time rendering with Three.js
- 💾 State Persistence: Save and load ECS world state to localStorage
- 🎯 Component-based Architecture: Clean separation of concerns between world logic and rendering
src/
├── components.ts # ECS component definitions (Transform, Position, Velocity, MeshRef)
├── index.ts # Application entry point
├── renderer/ # Three.js rendering logic
│ └── resources.ts # Renderer and scene resource management
├── scene/ # Scene initialization and synchronization
│ ├── initSystem.ts # Scene setup
│ ├── resources.ts # Scene resource management
│ └── syncSystem.ts # Sync world state to scene
├── storage.ts # LocalStorage persistence for ECS world
├── style.css # Application styles
└── world/ # World logic (no Three.js dependencies)
├── factories/ # Entity creation utilities
└── systems/ # ECS systems (rotation, velocity, camera movement)
- Node.js (v18 or higher)
- npm or yarn
npm installnpm run devThe application will be available at http://localhost:5173/.
npm run buildPreview the production build:
npm run preview| Command | Description |
|---|---|
npm run dev |
Start the development server |
npm test |
Run Playwright tests |
npm run build |
Build the application for production |
npm run preview |
Preview the production build |
npm run typecheck |
Run TypeScript type checking |
npm run knip |
Analyze for unused files, dependencies, etc. |
npm run lint |
Run Oxlint for code quality checks |
npm run format |
Format code with Oxfmt |
npm run validate |
Run all validation checks (format, lint, knip, typecheck, test, build) |
- Transform: Position and rotation data for entities
- Position: Position-only component (for cameras, etc.)
- Velocity: Movement vector for entities
- MeshRef: Reference to Three.js Mesh objects
- Camera: Tag component for camera entities
- Cube: Tag component for cube entities
worldInitSystem: Initializes the ECS world with entitiessceneInitSystem: Sets up the Three.js scenecameraMovementSystem: Handles camera movement via keyboardvelocitySystem: Applies velocity to entity positionsrotationSystem: Handles entity rotationsceneSyncSystem: Syncs ECS world state to Three.js scene
The src/world/ directory contains pure ECS logic that should be completely decoupled from rendering:
- ❌ No Three.js dependencies
- ❌ No references to
THREE.Scene,THREE.Camera, orTHREE.Mesh - ✅ Pure data structures and world logic
- ✅ Easy to test without rendering context
Use absolute imports instead of relative imports:
import { Component } from "@/components/Component";Use arrow functions instead of the function keyword:
const myFunction = (param: string): void => {
// implementation
};Before submitting any changes, run the validation command:
npm run validateThis ensures code quality and catches issues early.