Specialized components
Physics and spatial systems
Rendering system
19 min
overview the rendering system is responsible for visualizing 3d objects on screen it bridges the gap between the abstract entity component representation of objects and their visual appearance this system leverages three js, a powerful webgl based 3d graphics library, to handle the complex process of transforming 3d data into 2d images on the user's display core components the rendering system consists of several specialized components that work together to create the visual output renderercomponent the renderercomponent serves as the central hub for rendering operations it manages the webgl rendering context via three js webglrenderer the html canvas element where the scene is drawn the main three js scene object that contains all visible elements the effectcomposer for post processing effects // simplified from src/renderer/components/renderercomponent ts export const renderercomponent = definecomponent({ name 'renderercomponent', schema s object({ canvas s type\<htmlcanvaselement | null>(), renderer s type\<webglrenderer | null>(), effectcomposer s type\<effectcomposer | null>(), scene s class(() => new scene()), // additional properties }), // implementation details }); cameracomponent the cameracomponent defines the viewpoint from which the scene is observed it encapsulates field of view (fov) settings aspect ratio near and far clipping planes camera type (perspective or orthographic) // simplified from src/camera/components/cameracomponent ts export const cameracomponent = definecomponent({ name 'cameracomponent', schema s object({ fov s number({ default 60 }), aspect s number({ default 1 }), near s number({ default 0 1 }), far s number({ default 1000 }), // additional properties }), oninit (initial) => new arraycamera(\[/ camera initialization /]), // implementation details }); objectcomponent the objectcomponent serves as the bridge between the ecs architecture and three js objects it links a three js object3d to an entity synchronizes the entity's transformcomponent with the three js object's transform handles matrix updates and parent child relationships // simplified from src/renderer/components/objectcomponent ts export const objectcomponent = definecomponent({ name 'objectcomponent', schema s type\<object3d>({ required true }), onset(entity, component, obj object3d) { // ensure entity has a transformcomponent setcomponent(entity, transformcomponent); const transform = getcomponent(entity, transformcomponent); // link three js object's transform to ecs transformcomponent obj matrix = transform matrix; obj matrixworld = transform matrixworld; obj matrixautoupdate = false; // ecs handles updates // additional setup component set(obj); }, // additional methods }); meshcomponent the meshcomponent is a specialized version of objectcomponent specifically for 3d models (meshes) it takes a three js mesh object (geometry + material) delegates to objectcomponent for transform synchronization provides a convenient interface for visible 3d objects // simplified from src/renderer/components/meshcomponent ts export const meshcomponent = definecomponent({ name 'meshcomponent', schema s type\<mesh>({ required true }), onset(entity, component, mesh mesh) { setcomponent(entity, objectcomponent, mesh); component set(mesh); }, // additional methods }); scenecomponent the scenecomponent helps organize entities into logical groups for rendering it defines which entities belong to which scene allows for selective rendering of different parts of the world facilitates scene management for complex environments postprocessingcomponent the postprocessingcomponent enables visual effects to be applied after the main rendering pass it configures post processing effects like bloom, depth of field, or color correction integrates with the effectcomposer in the renderercomponent provides a framework for extending the visual capabilities of the engine webglrenderersystem the webglrenderersystem is the system that orchestrates the rendering process each frame its responsibilities include finding active renderer and camera entities collecting all visible objects from the scene updating the three js scene with these objects executing the rendering process applying post processing effects if configured // simplified concept from src/renderer/webglrenderersystem tsx function execute() { // find entities with both renderercomponent and cameracomponent const rendererentities = rendererquery(); for (const entity of rendererentities) { const camera = getcomponent(entity, cameracomponent); const renderercomponent = getcomponent(entity, renderercomponent); const threejsscene = renderercomponent scene; // collect all visible objects const visibleobjects = collectvisibleobject3ds( renderercomponent scenes, entity ); // update scene contents threejsscene children = visibleobjects; // set scene properties (background, environment, fog) // perform the actual render render(renderercomponent, threejsscene, camera, deltatime); } } rendering process the rendering process follows a specific sequence of operations 1\ setup phase before rendering can occur, the necessary components must be set up // create and configure a camera entity const cameraentity = createentity(); setcomponent(cameraentity, transformcomponent, { position { x 0, y 2, z 10 } }); setcomponent(cameraentity, cameracomponent, { fov 75, aspect window\ innerwidth / window\ innerheight, near 0 1, far 1000 }); // create and configure a renderer entity (often the same as the camera entity) setcomponent(cameraentity, renderercomponent, { canvas document getelementbyid('rendercanvas'), // additional configuration }); // create a visible object const cubeentity = createentity(); setcomponent(cubeentity, transformcomponent, { position { x 5, y 0, z 0 } }); setcomponent(cubeentity, meshcomponent, createcubemesh()); 2\ per frame execution during each frame of the game loop the transform system updates all entity transforms the webglrenderersystem executes it identifies the active renderer and camera it collects all visible objects it updates the three js scene it triggers the actual rendering sequencediagram participant usercode participant ecsengine participant transformsys as transform system participant webglrendersys as webglrenderersystem participant threejs participant screen usercode >>ecsengine set meshcomponent on cube entity usercode >>ecsengine set cameracomponent on camera entity usercode >>ecsengine set renderercomponent on camera entity note over ecsengine game loop tick ecsengine >>transformsys execute transformsys >>ecsengine update transformcomponent matrixworld for all entities ecsengine >>webglrendersys execute webglrendersys >>ecsengine get renderer, camera, visible objects ecsengine >>webglrendersys components data webglrendersys >>threejs populate scene with visible object3ds webglrendersys >>threejs renderer render(scene, camera) threejs >>screen pixels drawn 3\ transform synchronization a key aspect of the rendering system is how it synchronizes entity transforms with three js objects the objectcomponent links a three js object to an entity's transformcomponent it makes the three js object's matrices point directly to the transformcomponent 's matrices when the transform system updates the transformcomponent , the three js object's transform is automatically updated this ensures that visual objects appear at the correct position, rotation, and scale post processing the rendering system supports post processing effects through the postprocessingcomponent and three js effectcomposer the renderercomponent initializes an effectcomposer the postprocessingcomponent configures which effects to apply during rendering, instead of directly rendering to the screen, the scene is first rendered to a buffer the configured effects are then applied to this buffer the final result is presented on screen common post processing effects include bloom (glow around bright objects) depth of field (focus/blur based on distance) color grading (adjusting colors for artistic effect) ambient occlusion (simulating soft shadows in crevices) integration with other systems the rendering system integrates with several other engine systems transform system provides spatial information for positioning objects physics system may provide visual debugging representations input system may highlight objects under the cursor animation system updates visual representations of animated entities next steps with an understanding of how entities are visualized on screen, the next chapter explores the input system, which handles user interactions with the 3d environment next input system docid\ gaammfydn5tltewfrx3jn