My game has an ECS, and I'd like to render it with the wgpu crate.

How my system exactly works, is that there are two types: Object and Component. They are Rc<RefCell<T>> wrappers of the inner types.

ObjectInner contains the ID, optional parent ID, children IDs, and component IDs attached to the object.

ComponentInner contains the ID, variant enum (Transform, Brick, etc.), and optional object ID that the component may be attached to.

The Scene type contains a map of Objects and Components, with the ID as the key.

I'd like to keep the rendering code and ECS code separate from each other, so that I could for example have a server running headless without having to handle rendering.

What I have right now is context types and render types.

A context type is only created once, and contains things that can be reused for multiple objects or components. For example, BrickContext would contain the bind group layout, render pass, and mesh used for rendering every brick.

A render type is created for every instance of an object or component. For example, RenderBrick would contain the bind group and buffer, where the buffer contains the uniform, which is then passed onto the render pass's shader to render the brick.

A RenderScene would also contain a map of render types. It would go through each map, check what is missing or should be removed from Scene, and update accordingly.

Though I fear that this approach is slow or improper. Another approach I had, is to implement a notification system. The Scene would have a queue of notification, saying what got added, removed, or changed. I'm also unsure about this approach.

And how would I handle transforms? I would need to get the object to somehow get its transform from a Transform component, while also being rendered relative to its parent's transform. I would also have to figure out how to get a Brick component to render the mesh, while being affected by the shader of the Object, that manipulates the vertex position by the object's transform.

How would I approach to implement rendering with this system? Is there a better way to do this?

6 posts - 2 participants

Read full topic