How to properly "de-instantiate" hardware instancing

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?

Hi @heretique,

I think the easiest way to do that is to keep the original model component disabled, instead of destroying it, while enabling HW instancing.

Then when time comes, you can just enable it, after disabling HW instancing on each affected material. And of course remove the instanced mesh instance from any layers rendering it.

2 Likes

Yeah, hehe :sweat_smile:, in hindsight I think this solution is probably the best too. I think I’m complicating things by removing the original model component.
Thanks a lot @Leonidas for opening my eyes :smiley:

1 Like

I’ve been there! HW instancing can be a bit complex given it’s a code only solution. Wherever you can simplify it, it definitely helps in long term maintenance.

1 Like