Instance Meshes and Animations as a Set?

Hello everyone!

I’m finding great examples for how to create instances for individual objects, but I’m wondering if there is a way to do this for multiple meshes, textures, and animations that are are part of a group.

I have a character that consists of three different meshes. Each mesh has its own animation and material properties. The goal is create instances of the complete character with accompanying animations.

I’m currently working with the example below, but it only applies to individual boxes.
https://www.babylonjs-playground.com/#8L50Q3#1

Thanks in advance!

Unfortunately you have to instantiate all the different part separately at the moment but I wounder if a tiny helper could help with that.

I see. That definitely works, but I’m not sure how to keep the instances paired correctly.

For example:

“box” is the parent of “sphere.”

let instanceCount = 20;

for (var index = 0; index < instanceCount - 1; index++) {
    let instance = box.createInstance("box" + index);
    instance.position.x = 200 - Math.random() * 400;
    instance.position.z = 200 - Math.random() * 400;
    instance.alwaysSelectAsActiveMesh = true;
};



for (var index = 0; index < instanceCount - 1; index++) {
    let instance = sphere.createInstance("sphere" + index);
    instance.position.x = 200 - Math.random() * 400;
    instance.position.z = 200 - Math.random() * 400;
    instance.alwaysSelectAsActiveMesh = true;
    
};

Here are examples of what I’ve been trying, but nothing has worked so far…

instance.parent = box;
instance.parent = “box”;
instance.parent = (“box + index”);
instance.setParent(“box + index”);

I suppose I’m not sure how to reference the array of instances. I assume they’re named like so:

sphere0
sphere1
sphere2

Ohhhhh I see I am totally not sure this would work with all cause parent of instances can not be instances themselves if I recall.

I see. Would something like this work with clones?
I just cloned the parent mesh, and all the other meshes appeared with it, but no animations.

In your case it might be easier to rely on loading your model in an asset container and use instantiateModelsToScene which create a fresh new version of it respecting the hierarchy.

OK, I’ll try that.

Thanks!

Hello again @sebavan.

Here is one more question to help me determine if what I’m attempting is possible, or if I should abandon the idea and move on to some other game design.

  1. With the asset container, does that only allow for the duplication of the exact same mesh/animations, or is it possible to duplicate a new version with random values (i.e. scaling, material, animation parameters, etc.)? I know this is possible with clones, but the parameters have to be manually added.

The goal is to have characters spawn based on a template character (from the asset container), but the new characters can have random attributes. I suppose what I’m attempting would be a type of procedural generation. I thought instances would be the solution, but as you pointed out, they only work for individual objects.

Thanks again for the help!

This would behave like clone so it should be all good.

Actually it would be better suited to your case due to the restrictions related to instances properties: the visual one aside vertex colors are not tweakable without custom materials and shaders.

Just so I understand, what I’m trying to achieve is possible with the asset container (the randomization part)?

Yup you would need to randomize values after instantiating.

Great, thanks! :smiley: :+1: