Hi everyone,
I have implemented hardware instancing of some props in our game that works fine (for now only for one mesh instance per ModelComponent). The way I’m doing it is like this:
Go through all prop entities
For each, take the ModelComponent and get the first MeshInstance
create a Key from Mesh id and Material id and add the MeshInstance to a Map<Key, MeshInstance>
If this is the first mesh for this key then create the required instances vertex buffer and add the transform
If we found a similar MeshInstance, take the transform, add it to the appropriate vertex buffer
(the one MeshInstance used for instancing) then destroy the ModelComponent on that entity.
So in the end we end-up with one entity that has the ModelComponent and original MeshInstance per prop type that holds the instance buffer for the rest.
Now the problem is that we support an in-game editor that allows us to edit those props, move them around, rotate them, etc. In order to be able to pick them and edit them, we need to “de-instance” them and recreate the original entity. Basically I need to recreate the ModelComponent for each entity that it was removed from based on the original (first reference) that I kept and the transform information I have.
One more piece of information I keep from each original prop is the GraphNode of the MeshInstance that is used for the final transform. So if I move entities around I update the instance buffer with new transform. (Somehow when I remove the ModelComponent from the root entity of the prop, that model graph hierarchy is still being kept on the entity).
My question is what’s the best approach for cloning the reference ModelComponent that I kept and re-attach it to all the “instance” entities that it was removed from and set the proper “node” property on the MeshInstances?