Randomly Generated Meshes

Hello!

I’m trying to create meshes with random values, including names, materials, animations, etc. I modified the instance example from the documentation, and I managed to create new meshes (not instances) and new materials with unique names (according to the inspector anyway). However, when I try to include the new meshes in a function (i.e. “box1”), it doesn’t work. So, it seems I’m missing code to let the scene know that the new meshes exist, or this method of creating new random meshes simply doesn’t work.

https://www.babylonjs-playground.com/#BSX7A4#1

By the way, I’ve attempted to use clones, duplicating asset containers, instances, and the solid particle system to achieve the game mechanic with no success. It’s possible what I’m trying to achieve isn’t currently possible with BabylonJS.

What do you mean by this? Can you update your PG to show the problem?

You can get the mesh with name box1 from the scene by doing scene.getMeshByName("box1").

Hello @Evgeni_Popov!

Here’s an updated PG where I try to apply “material1” to “box1.” I added the getMeshByName lines with (“box” + index) and (“material” + index) hoping that would reference all the newly created objects. I’m getting “not defined” errors.

https://www.babylonjs-playground.com/#WNLWRL

box1.material = new BABYLON.StandardMaterial("material1");
You did not create an object box1, so this variable is not defined and it fails at this line…

Re-reading your first post, you can’t create instances of the same mesh with different materials: all instances of a mesh can’t have their own material, they will inherit the material from their master mesh. If you want to have a different material for each mesh, you must clone the master mesh instead.

I see, that makes sense, thanks for the explanation. Is it possible then to create a function that would generate clones with random parameters?

For example (obviously not code below, just a concept):

Number of clones to generate: 3

clone 1: (random scaling and animations): random material (#7)
clone 2: (random scaling and animations): random material (#4)
clone 3: (random scaling and animations): random material (#1)

I know this can be done manually, but I’m hoping to create a type of procedural generation for multiple meshes.

Thanks again for the help!

You can try something like this:

https://www.babylonjs-playground.com/#BSX7A4#3

This is exactly right, thanks! Being new to programming, I’m still unsure about how to reference each clone by its specific name. In the PG below, I used an animation sequence from the documentation to try and animate only box1. You explained earlier that scene.getMeshByName was needed, so I added that too. Because I most likely assembled the code incorrectly, I still get the box 1 undefined error.

https://www.babylonjs-playground.com/#BSX7A4#4

You must put the result of scene.getMeshByName("box1") in a variable before being able to use it:

https://www.babylonjs-playground.com/#BSX7A4#5

Excellent! Thanks again–I definitely appreciate the help! :smiley: :+1: