Live Demo: https://dostypes.github.io/threejs-app-template/
This project provides a reusable template demonstrating a robust and scalable architecture for building interactive Three.js applications that integrate with user interfaces (HTML/CSS/JS) and external services (simulated). It addresses the common challenge of managing communication and state between these distinct parts of an application.
Goal: To offer a clear, understandable, and extensible starting point for developers building complex 3D web experiences.
Integrating a 3D scene (Three.js), a user interface (DOM), and external data sources (APIs, WebSockets) can lead to tightly coupled and hard-to-maintain code. This template implements the AppBrain pattern to manage this complexity effectively.
- Separation of Concerns: Isolates UI logic (
UIManager), 3D rendering/interaction (ThreeManager), and external service communication (ServicesManager) into distinct, manageable modules. - Centralized Orchestration: The
AppBrainclass acts as the central coordinator, directing data flow and preventing direct dependencies between modules. This makes the application logic easier to follow and debug. - Clear Communication: Uses an event-driven approach (Node.js
EventEmitter) for modules to signal changes or actions upwards towardsAppBrain, and direct method calls forAppBrainto command modules downwards. - Extensibility: The modular design makes it straightforward to:
- Replace the simulated service (
AIHandler) with real API integrations or WebSocket connections. - Add new UI components or change the UI framework.
- Introduce more complex 3D objects and interactions.
- Scale the application with new features.
- Replace the simulated service (
The core of this template is the AppBrain, which orchestrates interactions between three main managers:
UIManager(src/ui/UIManager.js): Manages all interactions with the HTML/CSS user interface. Listens for DOM events and emits meaningfuluiActionevents toAppBrain. Receives commands fromAppBrainto update the UI (e.g., display text).ServicesManager(src/services/ServicesManager.js): Manages communication with external services. It aggregates events from specific service handlers (like the includedAIHandler) and emits a standardizedservicesChangeevent toAppBrain.ThreeManager(src/three/ThreeManager.js): Manages the Three.js scene, rendering loop, and 3D object interactions. Listens for user input within the 3D scene (e.g., object clicks viaInputHandler) and emits events likeobjectSelectedtoAppBrain. Receives commands fromAppBrainto modify the scene (e.g., change object colors, toggle visibility).
Communication Flow:
- Events In: Managers (
UIManager,ServicesManager,ThreeManager) emit events when something happens in their domain (UI click, service message, 3D object selected). - Brain Listens:
AppBrainlistens for these specific events. - Methods Out: Based on the received event,
AppBraincalls specific, public methods on the appropriate manager(s) to trigger updates or actions in other parts of the application.
-
Clone the repository:
git clone <repository-url> cd threejs-app-template
-
Install dependencies:
npm install
-
Run the development server:
npm run dev
This will start a Vite development server, typically accessible at
https://localhost:8080/. Note that it uses self-signed SSL certificates (key.pem,certificate.pem) for HTTPS, so your browser might show a security warning which you can usually bypass for local development. -
Build for production:
npm run build
This creates an optimized build in the
dist/folder.
index.html: Main HTML entry point.src/main.js: Application entry point, initializesAppBrain.src/AppBrain.js: The central orchestrator.src/ui/UIManager.js: Manages the DOM UI.src/services/ServicesManager.js: Manages external service interactions.src/services/AIHandler.js: Example service handler (replace with real integration).src/three/ThreeManager.js: Manages the Three.js scene and rendering.vite.config.js: Build tool configuration.
- Integrate Real Services: Replace or augment
src/services/AIHandler.jswith code to connect to your actual backend APIs, WebSockets, or other data sources. UpdateServicesManageraccordingly. - Expand the UI: Add more components and logic within
src/ui/and updateUIManagerto manage them. - Enhance the 3D Scene: Add more objects, lighting, controls, and interactions within
src/three/and updateThreeManager.
This project is licensed under the MIT License.