How brodbar/3d-renderer-canvas-api works
This project is a browser‐based 3D rendering engine built using the Canvas API. The overall purpose is to render 3D models (provided as OBJ and MTL files) onto a canvas without relying on any external frameworks. The project takes in 3D model data and provides various components for setting up a virtual camera, handling user input, managing the rendering canvas, and performing 3D math operations.
Main Components
• HTML & CSS – The entry point (index.html) and styling (css/style.css) for the overall UI.
• JavaScript Core – The backbone of the engine, residing in js/main.js.
• Rendering Components – Under js/components, include:
• Camera: Responsible for setting up and managing the viewpoint.
• Canvas: Governs the drawing area where 3D objects are rendered.
• Controls & Input: Manage user interaction and camera control.
• Geometric Primitives: Files like Line.js, Plane.js, Triangle (in Mesh), Vertice.js, and Mesh (folder) that define the building blocks of 3D objects.
• Timer: Likely used for animation/frame timing.
• Additional Components: E.g., any utility for camera movement or managing animations.
• Math Utilities – Under js/math (functions.js) and possibly js/components/Vector.js which handle mathematical operations crucial for 3D transformations.
• Model Loading – The js/utils/LoadModel.js which is responsible for loading external 3D model files.
• External Assets – The models directory containing OBJ and MTL files for different 3D objects.
Relationships and Interactions
• The HTML (index.html) loads the CSS and the main JS file.
• js/main.js initializes the rendering application that orchestrates the instantiation and interconnection of the different components.
• The Camera, Canvas, and Controls components interact to update views and respond to user inputs.
• Geometric components (Line, Plane, Triangle etc.) are used by the rendering pipeline to construct the 3D scene.
• Math utilities are used across components for calculating transformations, rotations, and vector operations.
• The Model Loader interacts with the file system (models folder) to fetch model data which is then rendered.
Architectural Patterns and Design Principles
• Component-Based Architecture: The separation within js/components indicates a modular design, where each file/class encapsulates a specific functionality (e.g., Camera, Controls).
• Separation of Concerns: The architecture cleanly separates rendering logic from mathematics computations and model loading.
• Lightweight Design: The avoidance of any frameworks underlines a focus on core JavaScript and manual control over rendering processes.
• Data Flow: Typically, data flows from the models and math utilities into the rendering components, and user input is captured by Controls and Input modules.
Additional Details Specific to the Project
• Minimalistic nature of the design since no frameworks are used.
• Temporal component by including the Timer component to illustrate the animation loop.
• How modularity in the js/components directory enables easy extension of functionality, e.g., additional geometric primitives or controls.