This system is built using WebGPU for rendering game objects in a 3D space, utilizing multiple classes that handle the rendering pipeline, camera settings, and game objects rendering.
Below is a breakdown of the main components involved in the rendering pipeline.
This class (only one instance) manages the WebGPU-based rendering system. It is responsible for setting up the GPU device, configuring the WebGPU context, and handling rendering events. It interacts with the WebGPU API to create buffers, shaders, and pipelines for rendering.
HTMLCanvasElement (for rendering) and a GPU object (for WebGPU init). Also ensure that navigator.gpu is available before creating the component.onError) if any issues occur during the rendering process, such as device failure or unsupported WebGPU features.onRenderingReady or getter IsRenderingReady), it can creates various resources like buffers, shaders, and bind groups for rendering.frame method continuously renders the scene by invoking the render method on game objects' RenderBehavior components.The camera defines how the scene is viewed by calculating the Model-View-Projection (MVP) matrix.
It manages the field of view (FOV), aspect ratio, and near/far clipping planes.
Note : The aspect ratio should be updated whenever the canvas size changes.
getMVPMatrix method calculates the combined MVP matrix using the camera's properties and the model matrix of the object being rendered.This is an abstract class that defines the behavior of any objects that can be rendered. Each RenderBehavior contains a WebGPU pipeline, bind group layout, and shaders necessary for rendering.
asyncInit is used to create the pipeline asynchronously when the device in renderEngine is ready.A abstract method render is defined in this class, which is implemented by concrete classes to render the object.
RenderBehavior and provides a basic implementation for rendering textured objects.BasicUVTexturedRenderBehavior to render 2D sprites (textured quads).This utility class provides methods to work with transformations in 3D space, such as converting an object's transformation into a model matrix.
toModelMatrix method generates a model matrix from the position, rotation, and scale of the object, which is used to position and orient the object in the world.Initialization: The RenderGameEngineComponent is initialized with a WebGPU-capable GPU and a canvas to render on. The GPU device and context are set up asynchronously through requestResources.
Setting Up the Camera: A camera (behavior) is created by the user and attached to a GameObject. When camera is enabled, it set the camera in the RenderGameEngineComponent to be used for rendering.
Rendering a Frame: The RenderGameEngineComponent starts rendering in a loop. For each frame:
RenderBehavior.render() method.Game Object Rendering:
RenderBehavior components, which handle the actual rendering of the object using a predefined pipeline and shaders.Error Handling: If any errors occur during the rendering process (e.g., WebGPU device is lost or unavailable), the onError event is triggered, and an error message is displayed.