Proper way to create an instance of a loaded GLB?

Hi all.
I’m getting errors when trying to instance a loaded GLB, “Instances should only be created for meshes with geometry.”

Reading up on past threads and see that we should use instantiateHierarchy on the loaded meshes array. Tried that, but the instance a) doesn’t seem to appear on screen and b) when moved to a different position using the inspector, throws the “with geometry” error.

Ideas? thx so much!

Here you are :slight_smile:

PG - https://playground.babylonjs.com/#WGZLGJ#7470

2 Likes

Those are some good looking chicks!
Thanks @labris.

I hadn’t come across this method before. Just looking it up here. Cool that it can either clone or instance.

2 Likes

Oh also interesting. Looks like there is an AssetManager task type for loading AssetContainers, although doesn’t seem to have made it to the docs..

In my PG above, I’d simplified out the AssetsManager component to focus on the problem, but was then worried after seeing your example that it’d be incompatible with AssetsManager. Looks like there’s already a fix there though. Trying it shortly.

Nice, that was easy. Here’s the version using AssetsManager:

1 Like

Hm, im not getting those artifacts with this method. Bounding boxes look good. No error messages. Added better naming, here: https://playground.babylonjs.com/#WGZLGJ#7477

Maybe post a PG of your stuff so we can see what’s happening with your models?

1 Like

The main issues are:

  • you are making things invisible
  • you changed the hierarchy before instantiating in the container

This won t and should not work…
image

You are parenting container objects to scene ones…

You could reparent in your duplicate helper

Doing:

task.loadedContainer.rootNodes[0].parent = group;

won’t work, because the instantiateModelsToScene method only deals with root objects, i.e. objects with parent === null.

If what you want is parenting to avoid having lots of objects at the root level in the inspector, set a parent as you do, before calling duplicate do task.loadedContainer.rootNodes[0].parent = null; and after it returns set task.loadedContainer.rootNodes[0].parent = group; again.

It does work for me:

Note that “house task” is the initial object that you set to invisible after loading, so it’s expected the bounding box does not show up for these invisible objects.

Actually, there’s a bug in instantiateModelsToScene, that’s why you get these warnings => you can see that the original tree is not instantiated correctly, some foliages are missing in the instances.

@RaananW It seems this PR does not handle the case where the instanced mesh is not at the root level but is a child of another mesh. Try this PG for eg:

The instance has the ground as the source mesh, instead of the sphere.

If using an empty mesh for the parent of the instance, you get the warnings @852Kerfunkle is experiencing:

And if you use a transform node as the parent, the sourceMesh property of the instance is pointing to the original sphere, not the cloned sphere:

2 Likes

Let’s fix this! :slight_smile:

i’ll open an issue for that and follow up soon.

instantiateModelsToScene doesn’t take parents of clones nodes into account · Issue #13454 · BabylonJS/Babylon.js (github.com)

2 Likes

Hm, this is not working anymore:
https://playground.babylonjs.com/#WGZLGJ#7477

Was working above. No errors, just not seeing the loaded asset. @RaananW, something change in the build regarding assetsManager.addContainerTask?

1 Like

Never mind. Mesh was set to invisible.

3 Likes

Actually, i think something did change (b/c i dont think i posted an empty pg earlier!). How can you remove or hide the original mesh without impacting the clones? See this PG. Whenever I set the isVisible property of the original loaded mesh to false, it can’t be turned on again for the clones (as far as i can tell):

It is because you set the mesh invisible but are setting only the root visible again :slight_smile: https://playground.babylonjs.com/#WGZLGJ#7924

2 Likes

Ah, i thought node there was the mesh. Urg. Thx much.