Built-in Primitive Meshes
Babylon.js includes a variety of parametric shapes through the MeshBuilder class:
// Create a sphere
const sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {
diameter: 2,
segments: 32,
updatable: true // Allow geometry to be modified later
}, scene);
// Create a box/cube
const box = BABYLON.MeshBuilder.CreateBox("box", {
size: 2, // Size in all dimensions
width: 3, // Override specific dimensions
height: 1,
depth: 2,
faceColors: [ // Color each face differently
new BABYLON.Color4(1,0,0,1), // red front
new BABYLON.Color4(0,1,0,1), // green back
BABYLON.Color4.Blue(), // blue top
BABYLON.Color4.White(), // white bottom
BABYLON.Color4.Yellow(), // yellow right
BABYLON.Color4.Cyan() // cyan left
]
}, scene);
// Create a plane
const plane = BABYLON.MeshBuilder.CreatePlane("plane", {
width: 10,
height: 5,
sideOrientation: BABYLON.Mesh.DOUBLESIDE // Visible from both sides
}, scene);
// Create a cylinder
const cylinder = BABYLON.MeshBuilder.CreateCylinder("cylinder", {
height: 3,
diameterTop: 1,
diameterBottom: 2,
tessellation: 24, // Number of segments around circumference
subdivisions: 1 // Number of height segments
}, scene);
// Create a torus (donut)
const torus = BABYLON.MeshBuilder.CreateTorus("torus", {
diameter: 5,
thickness: 1,
tessellation: 32
}, scene);
// Create a ground mesh
const ground = BABYLON.MeshBuilder.CreateGround("ground", {
width: 100,
height: 100,
subdivisions: 20, // Increases detail for height maps
updatable: false
}, scene);