Instances vs. Clones

Hello everyone!

For my project, I currently have hundreds of cloned meshes. Understandably, this is creating a performance issue with respect to frame rate.

I’ve been experimenting to see if I can substitute some of these meshes with instances to improve performance. This mostly works, but there is one issue. Unlike clones, instances don’t follow their cloned parents. I did a forum search, and supposedly adding parentMesh.setEnabled(false) is supposed to fix this, but it doesn’t seem to work.

Here’s the PG:
https://www.babylonjs-playground.com/#8L50Q3#13

2 Likes

Each time you create an instance, you should just set its parent. That’s how I do it and it is just as simple as that. Try myInstance.parent = someParentMesh, it will probably solve your problem. Also if you inspect the scene graph you will see that each instance is indeed separated from its initial parent, that’s just how things work with instance :smiley:

Hello @Djani_Daud!

Thanks for the suggestion, that makes sense. However, it seems that would have to be done manually for each parent. If there are 100 or more clones, for instance, I’m not sure how you could make the new instances match the correct parent with a function (i.e. clone3 + instance 3, clone 4 + instance 4) :thinking:

I am sure I am missing something crucial to understand the issue, but I will ask anyhow -

You have a single mesh (sphere), and you create N clones out of it. Then you create another mesh (box) and you create N-1 instances out of it.
Now, you want to match instance[index] with clone[index]. You have the clone mesh array, you are iterating in a for loop, what stops you from setting the instance parent to be the N position of the clone army?

Lines 49 - 51 places all the box instances at the same position. Line 53 makes them all children of the same sphere and line 58 just rotates that same sphere. Moving position of box instances gives https://www.babylonjs-playground.com/#8L50Q3#15

2 Likes

Hello @RaananW and @JohnK :smiley: :+1:

Each instance (box) should follow its cloned parent (sphere). So, each rotating sphere clone should have a box rotating with it. If everything is just cloned, the cloned meshes know to keep the proper hierarchy (cloneSphere1 will be the parent of cloneBox1 and so on).

However, with instances, you have to tell each instance what the parent is. This can be done manually, but with hundreds of parent clones, I’m wondering if there is a way to do this automatically.

Thanks for the help!

I don’t see how it is expected to work automatically :slight_smile:

When you iterate over the boxes, assign a parent to them. it is one line of code that will get you where you wanted

You seem to fall into the same trap each time, perhaps you need to rethink your expectations of how instances work. When I started with Babylon.js I also had certain expectations of how thing would behave, which were wrong. Just a question of re-focusing.

If you re-read the post where you found this you will see that this is not a reference to instances not being applied to children but to a separate issue of hiding both a parent and its children. In DK’s reply you should note that he is answering these two separate questions but in reverse order to the way they were asked.

Do not let any of my comments stop you asking questions as this is often the only way to get clarity. Think about what it is you now know that you did not know before and be glad you are learning.

1 Like

With only about three weeks of programming and working with BabylonJS, I have no expectations for how anything works! :rofl: Most of my questions/issues stem from not knowing enough about the fundamentals of JavaScript, so I’ve definitely been working on learning that.

@RaananW asked what I mean by “automatically,” so I made a PG showing that (HERE). I made a box, and told it that its parent is sphere. Then, when you clone spheres, the boxes clone as well. If you check the Inspector, you’ll see that the box names are automatically assigned.

CloneSphere1

CloneSphere1.box

In this case, you don’t have to assign a box to a sphere clone, it just happens. I believe this is referred to as maintaining the proper hierarchy. With instances, it seems you would have to create code that would assign each individual instance to a parent. I know how to do that, but if you have 100 spheres (sphere1, sphere2, sphere3,…), it seems like the wrong approach to write this 100 times:

box1.parent = sphere1;
box2.parent = sphere2;
box3.parent = sphere3:

1 Like

Yes it would be wrong, you do it using a loop as in the PG I posted in my previous post. Do you have an issue with this method?

1 Like

Apologies! I completely overlooked that you posted a new PG. Once again, you’ve been amazingly helpful, thanks! :smiley: :+1:

2 Likes