Are there any performance benefits of Entity’s clones?
Currently at the beginning, I create one entity with all required components (models/elements/…) and set it’s enabled property to false. Then every time I want to create a new entity, I do it like this:
let newEntity = baseEntity.clone(); app.root.addChild(newEntity);
It works fine, but I’m curious if there are some performance benefits (for example for entities with the same model) of doing it this way or if the outcome is the same as If I do it like this:
let newEntity = new pc.Entity(); newEntity.addComponent(...); newEntity.addComponent(...); ...
That’s a good system that you use, I do something similar to mimic templates/prefabs in my games. The Playcanvas team is actively working in templates so at some point we might get more candy on this.
Regarding performance, using clone() has a runtime overhead since it allocates a new object in memory each time you call it. So it makes sense to use it when loading/preparing the level and not during game play, if you are cloning a lot of objects.
A good practice is to prepare a pool of objects, by cloning them while the game loads, have them disabled and when your game requires a game entity of that type, grab the next available from the list and enable it. So no cloning will be happening during gameplay. Do a search on game/object pooling and you will get plenty of articles to study on the matter.
Now, regarding models and rendering performance, normally Playcanvas will push a draw call for each entity that has a model component, even if the model/materials are the same. So with many models in scene this will have a performance hit.
Luckily Playcanvas provides a robust and very easy to use batching system which you can enable directly from the editor. It requires some thinking on how to prepare your batch groups, as soon as you have them in place you will see a big performance gain in scenes with many models. That’s important especially on mobile.
Hi Leonidas. Thank you for the answer. I already use pooling, I create entities in advance and I also use batching. I’m new to 3D and Playcanvas. I spent one week with Babylon JS where is a possibility to create clones of existing meshes which leads to performance benefits, and that was the reason why I asked about cloning entities in Playcanvas. Now, after you mentioned batching, I realized that batching in Playcanvas is probably something very similar to Babylon’s cloned meshes.
Yes indeed it is something similar, though the Playcanvas implementation doesn’t take, for the moment, advantage of hardware instancing (if supported by the device).