I have a quick question regarding the use and best practice for selecting meshes in a scene.
Or better said, groups of meshes.
My use case is the following: With 2K+ nodes in my scene, I have a logic where a group of meshes/instances/clones is parented to a transformNode to form a group of objects. These groups of objects are setEnabled depending on context/camera position. All meshes are enabled/disabled from the parent dynamically.
QUESTION IS: What is the method/order, when I ask for scene.get*By?
Can I assume that it runs a loop through all nodes in the order of the hierarchy? When it hits the first corresponding, the function runs and ignores the ones coming after? In which case, is the order the one I can see in ‘the Inspector’? In other words, if my transformNode is named something like “__my group” (as opposed to say “zz_mygroup”), will it be found earlier since it’s higher in the hierarchy (and from there, can I expect it to trigger faster and remove load on performance?)
Thanks in advance for clarifying this for me and meanwhile, have a great day
The function itself is very simple
When it hits the first corresponding, the function returns the result, if there is no result at the end of iteration it will return null.
Inspector shows nodes in the alphabet order. It doesn’t matter what is the name of your node; the function will go through array like
scene.transformNodes from element with index=0 till the end.
I believe there is no reliable way to control how your nodes are ordered inside scene arrays if you import models and dispose some meshes. The common way is to reduce the length of array which should be iterated, for example with Asset containers or with some custom code.
Thanks a lot for the answer and insight.
I’d like to bounce on the quote above. My object groups and the parent transformNode I want to pick is not from a single import of assets. In fact, the transformNode itself is created in the BJS script and the meshes (clones and instances) parented to it come from a number of imports. So, say I create my parent transformNode at the beginning of my script/scene and before importing assets or creating meshes or instances, can I assume it will be higher in the hierarchy of the scene index?
It will be ‘higher’ (if you count backwards, since it will have lesser index) in the
scene.transformNodes array. I am not aware of the ‘scene index’
Also, I don’t think you’ll get a lot of performance optimization here. One million elements array iteration takes about 50 ms on average computer, so for 2K nodes iteration will take 0.1 ms. There is always a lot of space for performance optimization in other parts of the scene.
haha, thanks. I didn’t see the numbers. You are right; there’s much more and better to do to gain performance. I might just disregard this as of just now. It was only that, when I enter the trigger area, I get a tiny-mini freeze of a few ms. It’s very little but you can still feel it. But then, from the answer you gave me, it’s likely not because of this. May be is when I make setEnabled on the 200 descendants? May be it’s just for a couple of frames and draw calls. May be I should try to run the function at a different time on render? Anyways, nothing essential. I’m gonna focus on some other parts for now and again, thanks (as always) for your support and expertise. Meanwhile, have a great sunday
There is a great tool for this - Coroutines | Babylon.js Documentation
Looks promising, thanks a lot. I’m gonna need some time to dig into this. Have a good one