Hey,
What do you advise for a method do spawn new meshes in an area? Basically I have an array of cells which contain a list of meshes to spawn. As the player moves around, entering a new cell will require that cell creates (instantiates) a bunch meshes. Similar to Battlefield 2’s vegetation approach but probably with a bit more freedom in placement.
I’m wondering what the best code shape for this is? Do I create a parallel ECB and pass in a native array of objects to spawn? What if I have many cells that suddenly need to spawn all their contents?
There’s a lot of ways to go about this so I’m really interested.
Essentially I’m doing lots of little mesh objects so I don’t want them to persist in the world (it’s a vast open world) but spawn as I reach them. Despawn them when far away. I’m looking for DOTS-friendly patterns and I’m open to more modern ways of doing it or DOTS-centric thinking.
I’d like this to be great performance. Spawning bullets from a gun is one thing, but spawning specific meshes (and despawning) with specific transforms is important and currently overlooked in samples. We’re talking at least a million objects in this world, in specific places, like grass, vegetation etc. There’s very few different kinds so the problem space is managing how many are instantiated at a given time, and where.
Advice and thoughts?
I would create pool system.
Entities that you can reuse, just change their positions.
This way you avoid instancing and syncing for these objects.
You can clear pool, or reduce number of elements in a pool for specific type of meshes, if they are no more in the area. Probably radius of few cells. Or estimate the direction of player movement.
With that, your instantiating system will focus on creating entities, which are required and not stashed in the pool.
I guess I’m not familiar with Battlefield 2, but is this procedural vegetation or fixed?
If it is fixed, I believe subscenes would be the right answer, although they generate internal fatal errors for me among other issues. One option I’ve considered (to potentially work around that issue) is to store prefabs in a dynamic buffer and then have a blob asset that contains arrays of indices into the prefabs as well as component values to initialize. Then it is just a matter of instantiating and initializing those values in a parallel job (I would use my custom ICB over ECB as it is way faster at this, but that’s beside the point).
Regardless, I would likely have a queue of cells which need to be loaded, and limit to dequeuing one per frame (or however many fit within frame budget).
In our game we have this pattern (not switched to DOTS yet), take the diff of 2d camera view bounds of previous frame and this frame, calculate which cells are in now from the diff, for each cell collect List<(id, position, rotation, scale)> for objects in that cell, send it to renderer which finds the mesh for that object by the id, then constructs its matrix and appends to the (RenderMesh, List) for instanced rendering, then we iterate by 1023 slices and Instanced Draw the mesh. It scales really well when the objects are too many but there aren’t many unique objects, there’s no transform hierarchy, and the objects are static