I’ll cut right to the chase:
Is there any significant performance difference between having all your entities defined in the root of your scene with model components already applied, and then enabling / disabling the ones you need to display while the application is running.
Compared to having nothing in your root, and then creating / destroying entities and adding them to the root when you need them during runtime.
Or perhaps having all your entities in the scene defined and then adding / removing just the model component.
I’m guessing these might not have a significant impact when the models are simplistic and the entity count is low.
But lets say theoretically that each model is a 4 million+ polygon complex model with complex materials/textures, and the scene has 20+ of these entities.
Is any of the three approaches I mentioned superior? And I’m only speaking in terms of performance. Or are they the same, or is there a fourth approach thats better than my examples?
Having the entities created upfront, and then enabling / disabling is the fastest, as that does the least amount of work.
If you at least have glb loaded, then meshes are already created … so calling instantiateModelEntity (or even better instantiateRenderEntity) - is pretty fast, as that only creates entities and links them to existing meshes.
The other way is to instantial those one time … and then clone them (example here: PlayCanvas Examples ) - the advantage of the clone is that you can for example modify your source hierarchy (add extra entities / scripts), and clone this. Which is in a way similar to what templates do for you.
Oh okay, maby I should’ve specified a bit more what I meant when I said performance.
I ment performance as in the solution that produces the least amount of memory and processing power while interacting.
If it takes a second or two everytime i swap entities/models that is fine. My main focus with my application is that camera interactions and animations run smoothly without fps drops. And I assume if I have all my entities and models loaded into the scene root from start, there might be significant memory usage compared to if I load and destroy them on demand?
I want the app to run smoothly on both mobile and desktop devices. And the swapping of entities wont happen that often so its more important that the app runs smoothly in between swaps.
Creating/destroying entities, and in general JS objects, on the fly can indeed produces frame drops due to garbage collection, if you are doing that in large numbers.
Usually in games we use pooling to avoid that, that’s quite close to what you are thinking. Pre-create a number of entities that you may potentially need later on, on start time, keep disabled and use them only when required.
As soon as an entity isn’t needed anymore, instead of destroying it, you disable it and send it back to the pool for future use.