Project Dusk #3 - Testing scene graph webbuild 1

I am progressing with my scene graph implementation. I have a simple test project running in the browser now. You can control the plane using arrow keys:

It's a simple start. The plane is a scene object with a few children and components attached - and unlike previously, this is all done in C without any Lua scripting. Not even C++.

The scene graph API is simple and straightforward. It's similar to how it works in Unity. Let's look at some code pieces:

  1 #include "../plane_sim_g.h"
  2 
  3 // inserting here the typedef struct from plane_sim_g.h:
  4 typedef struct MeshRendererComponent {
  5     Material material;
  6     Mesh* mesh;
  7 } MeshRendererComponent;
  8 
  9 static void MeshRendererDraw(Camera3D camera, SceneObject* node, SceneComponentId sceneComponentId, void* component, void* userdata)
 10 {
 11     MeshRendererComponent* meshRenderer = (MeshRendererComponent*)component;
 12     Matrix m = SceneObject_getWorldMatrix(node);
 13     DrawMesh(*meshRenderer->mesh, meshRenderer->material, m);
 14 }
 15 
 16 void MeshRendererComponentRegister()
 17 {
 18     psg.meshRendererComponentId = SceneGraph_registerComponentType(psg.sceneGraph, "MeshRenderer", sizeof(MeshRendererComponent),
 19         (SceneComponentTypeMethods) {
 20             .draw = MeshRendererDraw,
 21         });
 22 }

The code above is a simple mesh renderer. It is a bit of boilerplate code, but in general, it's not much. The advantage of this programming style is, that it's very easily usable in different contexts.

Using this component is very simple:

  1     ...
  2     SceneObjectId plane = SceneGraph_createObject(psg.sceneGraph, "plane");
  3     SceneGraph_setLocalPosition(psg.sceneGraph, plane, position);
  4     SceneGraph_addComponent(psg.sceneGraph, plane, psg.meshRendererComponentId,
  5     &(MeshRendererComponent) {
  6         .material = psg.model.materials[1],
  7         .mesh = psg.meshPlane,
  8     });
  9     ...

Adding the mesh and initializing it's memory - that's all what's done here. A init method can be provided in case things require initialization, but for most stuff, this is really sufficient.

I made a load test with instantiating 1000 planes and it ran acceptably well. The biggest drain is drawing the meshes individually instead of batching them. While this sounds like a nice thing to try out, my priority is to battle prove the scene graph first by making a simple arcade game with this plane you can see there. It would be easier with a scene editor, but this is what I am going for once I have the foundations ready, which are: Scene graph and asset management. Lua scripting will maybe even come earlier ... though I am not sure. The type system I intend to make for the asset management could, if the binding works as I imagine, make the Lua binding much easier, so taking care of that first could be more efficient coding wise to avoid having to rewrite things later.

So much for now, hope to keep adding things soon. Here's a rough plan what I want to implement with this demo:

It would be nice for comparison to implement the game in parallel using pico ECS - though I guess based on my preeliminary tests, that this won't show much of a difference. It could be however a good way to learn more about working with ECS and to see if it's really not a good fit for my project.

đŸĒ