Cloning With the Solid Particle System
Using the Solid Particle System to Copy Meshes
There is more to the Solid Particle System (SPS) than just producing multiple copies of a mesh, and these features are covered in full in the Particles section. The SPS places multiple copies of a mesh together into a single mesh. This means that instead of multiple draw calls, there is just one draw call for the single mesh.
The SPS is a single updatable mesh with the same properties as any other Babylon.js mesh.
To produce multiple copies of a mesh, use the following script.
Example:
import { Scalar } from "@babylonjs/core/Maths/math.scalar";import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";import { SolidParticleSystem } from "@babylonjs/core/Particles/solidParticleSystem";SPS = new SolidParticleSystem("SPS", scene); //create the SPSconst tetra = MeshBuilder.CreatePolyhedron("tetra", {}); //create the meshSPS.addShape(tetra, 1500); // add as many copies as you want to the SPStetra.dispose(); //dispose of the original meshconst spsMesh = SPS.buildMesh(); //builds the SPS mesh//Set the function to initialise the particle propertiesSPS.initParticles = () => { for (let p = 0; p < SPS.nbParticles; p++) { const particle = SPS.particles[p]; particle.position.x = Scalar.RandomRange(-50, 50); particle.position.y = Scalar.RandomRange(-50, 50); particle.position.z = Scalar.RandomRange(-50, 50); const scale = Scalar.RandomRange(0.5, 1.5); particle.scale.x = scale; particle.scale.y = scale; particle.scale.z = scale; particle.rotation.x = Scalar.RandomRange(0, Math.PI); particle.rotation.y = Scalar.RandomRange(0, Math.PI); particle.rotation.z = Scalar.RandomRange(0, Math.PI); }};SPS.initParticles(); //call the initialising functionSPS.setParticles(); //apply the properties and display the mesh