Skip to content
cvializPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

ECS 3D Demo

A demonstration of Entity Component System (ECS) architecture combined with Three.js for 3D rendering.

ECS 3D Demo Screenshot

Overview

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.

Features

  • 🎮 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

Project Structure

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)

Getting Started

Prerequisites

  • Node.js (v18 or higher)
  • npm or yarn

Installation

npm install

Development Server

npm run dev

The application will be available at http://localhost:5173/.

Build for Production

npm run build

Preview the production build:

npm run preview

Available Scripts

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)

ECS Architecture

Components

  • 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

Systems

  • worldInitSystem: Initializes the ECS world with entities
  • sceneInitSystem: Sets up the Three.js scene
  • cameraMovementSystem: Handles camera movement via keyboard
  • velocitySystem: Applies velocity to entity positions
  • rotationSystem: Handles entity rotation
  • sceneSyncSystem: Syncs ECS world state to Three.js scene

Development Guidelines

World Directory Rules

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, or THREE.Mesh
  • ✅ Pure data structures and world logic
  • ✅ Easy to test without rendering context

Import Style

Use absolute imports instead of relative imports:

import { Component } from "@/components/Component";

Function Syntax

Use arrow functions instead of the function keyword:

const myFunction = (param: string): void => {
  // implementation
};

Validation

Before submitting any changes, run the validation command:

npm run validate

This ensures code quality and catches issues early.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages