API

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 SPS
const tetra = MeshBuilder.CreatePolyhedron("tetra", {}); //create the mesh
SPS.addShape(tetra, 1500); // add as many copies as you want to the SPS
tetra.dispose(); //dispose of the original mesh
const spsMesh = SPS.buildMesh(); //builds the SPS mesh
//Set the function to initialise the particle properties
SPS.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 function
SPS.setParticles(); //apply the properties and display the mesh

Example

Copying a tetrahedron: Copying a Tetrahedron