Skip to main content

Meshes and nodes

What draw gives back is a BabylonJS mesh, and these functions work on it, without going back to the kernel. Moving a drawn mesh is instant, so animate and arrange with them, and rebuild geometry only when its form changes.

  • Meshes (babylon.mesh): position, rotation and scale; visibility and pickability; cloning, instancing and merging; materials; collisions and intersection tests; and reading a mesh's vertices.
  • Nodes (babylon.node): invisible transform nodes that meshes are parented to, so moving one node moves everything under it, the way a door's hinge carries the door.
  • Mesh primitives (babylon.meshBuilder): BabylonJS's own boxes, spheres, cylinders, planes and more, built straight into the scene as meshes, for helpers and effects that need no CAD kernel.
  • Matrices (babylon.transforms): transformation matrices built with BabylonJS's maths.

Every function​

Meshes​

FunctionWhat it does
cloneMakes a copy of a mesh, with its children, that shares the geometry of the original and is placed at the same spot; the copy casts and receives shadows like the original.
cloneToPositionsMakes one copy of a mesh at every given position, in the same order; the copies share the geometry of the original.
convertToFlatShadedMeshGives every triangle of a mesh its own vertices and normals, so faces show as flat facets instead of being smoothed across edges; the mesh is changed in place and given back.
createMeshInstanceCreates an instance of a mesh, a lightweight copy that shares its geometry and draws cheaply when many alike are needed, placed where the original is.
createMeshInstanceAndTransformCreates an instance of a mesh, a lightweight copy that shares its geometry and draws cheaply, and places it at the given position, rotation in degrees and scaling.
createMeshInstanceAndTransformNoReturnCreates a placed instance of a mesh, as createMeshInstanceAndTransform does, without giving it back; for scripts that only need the copy to appear.
disablePointerMoveEventsStops a mesh, and its children when includeChildren is true, from reacting to pointer moves, back to the default.
disposeRemoves a mesh from the scene and frees its GPU resources; the mesh cannot be used afterwards.
enablePointerMoveEventsLets a mesh, and its children when includeChildren is true, react to the pointer merely moving over it, which is off by default because it costs a pick on every pointer move.
getAbsolutePositionReads the position of a mesh in world coordinates, with every parent's transform applied.
getCheckCollisionsReads whether a mesh takes part in collision checking.
getChildMeshesLists the meshes parented under a mesh: all descendants, or only the direct children when directDescendantsOnly is true.
getIdReads the id of a mesh, the label set by setId or by the loader that created it.
getMaterialReads the material of a mesh, the surface description its faces are drawn with.
getMeshesOfIdFinds every mesh in the scene with exactly the given id; ids need not be unique, so several may match.
getMeshesWhereNameContainsFinds every mesh in the scene whose name contains the given text, case-sensitive, in scene order.
getMeshOfIdFinds the first mesh in the scene with exactly the given id; use getMeshesOfId when several share it.
getMeshOfUniqueIdFinds the mesh with the given unique id, the number the scene assigns to every mesh once, as getUniqueId reads it.
getNameReads the name of a mesh, as set by setName or by whatever created it.
getParentReads the node a mesh is parented to, which is what it moves with; a mesh at the top level has none.
getPickableReads whether the mesh can be picked with the pointer.
getPositionReads the position of a mesh relative to its parent, as a point.
getRotationReads the rotation of a mesh around X, Y and Z, in radians, as its rotation property holds it.
getScaleReads the scale factors of a mesh along X, Y and Z; 1 is unscaled.
getSideOrientationTurns a side orientation choice into the number the engine uses for it, for building meshes by hand.
getUniqueIdReads the unique id of a mesh, the number the scene gives every mesh once and never reuses.
getVerticesAsPolygonPointsReads the triangles of a mesh as lists of three points in the mesh's own coordinates, the form jscad.shapes.fromPolygonPoints and similar builders take.
hideHides a mesh without removing it from the scene, and its child meshes too when includeChildren is true; show brings it back.
intersectsMeshTells whether two meshes overlap, judged by their bounding boxes: axis-aligned ones by default, or boxes that follow each mesh's rotation when precise is true; includeDescendants tests their children too.
intersectsPointTells whether a point lies inside the bounding box of a mesh.
mergeMeshesJoins several meshes into one new mesh, which draws faster than many separate ones.
moveBackwardMoves a mesh against its own forward direction, the local Z axis, by distance scene units.
moveDownMoves a mesh against its own up direction, the local Y axis, by distance scene units.
moveForwardMoves a mesh along its own forward direction, the local Z axis, by distance scene units; a turned mesh moves the way it faces.
moveLeftMoves a mesh against its own right direction, the local X axis, by distance scene units.
moveRightMoves a mesh along its own right direction, the local X axis, by distance scene units.
moveUpMoves a mesh along its own up direction, the local Y axis, by distance scene units.
pitchTurns a mesh around its own X axis by rotate degrees, on top of its current rotation, the way a nose tips up or down.
rollTurns a mesh around its own Z axis by rotate degrees, on top of its current rotation, the way a wing banks.
rotateAroundAxisWithPositionTurns a mesh by angle degrees around an axis that passes through position, so the mesh orbits that point rather than spinning in place.
setCheckCollisionsTurns collision checking on or off for a mesh, and its children when includeChildren is true, so a camera or another collider with collisions enabled cannot pass through it.
setIdSets the id of a mesh, a label that getMeshOfId finds it by and that need not be unique.
setLocalScaleMultiplies the current scale of a mesh, or an instance of one, by one factor on all axes, so 2 doubles whatever size it has.
setMaterialGives a mesh a material, and its children too when includeChildren is true; the material decides the color, shininess and transparency of its surface.
setNameSets the name of a mesh, and of its children too when includeChildren is true; names are what getMeshesWhereNameContains searches.
setParentMakes one mesh the child of another, so it moves, turns and scales together with its parent from then on; its position becomes relative to the parent.
setPickableSets whether a mesh answers to pointer picking, and its children too when includeChildren is true; an unpickable mesh is skipped by clicks and rays that pick.
setPositionPlaces a mesh, or an instance of one, at a point relative to its parent.
setRotationSets the rotation of a mesh, or an instance of one, as angles in degrees around X, Y and Z, replacing its current rotation.
setScaleSets the scale factors of a mesh, or an instance of one, along X, Y and Z, replacing its current scale; 1 is unscaled.
setVisibilitySets how visible a mesh is, from 0 for fully transparent to 1 for fully shown, with the values between fading it; includeChildren applies the same value to its child meshes.
showShows a mesh that hide or a hidden draw made invisible, and its child meshes too when includeChildren is true.
updateDrawnMoves, rotates, scales and recolors a drawn mesh in place, without drawing it again, which is faster when only the placement or the color changes.
yawTurns a mesh around its own Y axis by rotate degrees, on top of its current rotation, the way a car turns left or right.

Nodes​

FunctionWhat it does
createNodeFromRotationCreates a node at origin turned by the three rotation angles in degrees around X, Y and Z, inside the coordinate system of parent when one is given.
createWorldNodeCreates a node parented to the root node of the scene, a fresh starting point for building a hierarchy at the world origin.
drawNodeDraws the three axes of a node as colored lines of the given length, parented to it, so its position and orientation can be seen; the default colors are red for X, green for Y and blue for Z.
drawNodesDraws the three axes of several nodes as colored lines of the given length, each set parented to its node, as drawNode does for one.
getAbsoluteForwardVectorReads the direction a node's local Z axis points in world space, with every parent's rotation applied.
getAbsolutePositionReads where a node's origin is in world space, with every parent's transform applied.
getAbsoluteRightVectorReads the direction a node's local X axis points in world space, with every parent's rotation applied.
getAbsoluteRotationTransformationReads the rotation of a node in world space, with every parent's rotation applied, as a 4x4 matrix of 16 numbers.
getAbsoluteUpVectorReads the direction a node's local Y axis points in world space, with every parent's rotation applied.
getChildrenLists the nodes and meshes parented directly under a node, the ones that move with it.
getParentReads the node a node is parented to, the one it moves with; a top-level node has none.
getPositionExpressedInLocalSpaceReads a node's position measured in its own local axes rather than its parent's, which differs once the node is rotated.
getRootNodeGives the root node of the scene, the top of the hierarchy that createWorldNode parents to.
getRotationReads a node's rotation relative to its parent as three angles in degrees around X, Y and Z.
getRotationTransformationReads the rotation of a node relative to its parent as a 4x4 matrix of 16 numbers; the node must carry a rotation quaternion, which rotate and setDirection give it.
rotateTurns a node by angle degrees around an axis through its own origin, on top of its current rotation; its children follow.
rotateAroundAxisWithPositionTurns a node by angle degrees around an axis that passes through position, so the node orbits that point rather than spinning in place; its children follow.
setAbsolutePositionMoves a node to a point in world space, whatever its parents are; its children follow.
setDirectionTurns a node so its local Z axis points along direction; its children follow.
setParentParents a node to another so it moves with it from then on, keeping its current place in the world; a null parent detaches it.
translateMoves a node by distance scene units along direction, given in the node's own local axes; its children follow.

Mesh primitives​

FunctionWhat it does
createBoxBuilds a box centered on the origin with width along X, height along Y and depth along Z.
createCapsuleBuilds a capsule, a cylinder with rounded ends, of the given height and radius along orientation; radiusTop and radiusBottom size the two ends separately.
createCubeBuilds a cube centered on the origin with every edge size long.
createCylinderBuilds a cylinder standing along Y, centered on the origin; different top and bottom diameters make a cone or a taper, and 0 closes an end to a point.
createDiscBuilds a flat disc of the given radius centered on the origin in the XY plane; arc below 1 leaves a pie slice, so 0.5 is a half disc.
createExtrudedSahpeSweeps a profile along a path: shape is the profile as points in the XY plane and path the points it travels through, with scale and rotation in radians applied step by step along the way.
createGeodesicBuilds a geodesic sphere, an icosahedron whose faces are subdivided into triangles as m and n say, so the surface is made of near-equal triangles.
createGoldbergBuilds a Goldberg polyhedron, a ball made of hexagons and twelve pentagons like a football, with m and n setting how many hexagons there are.
createGroundBuilds a flat ground plane centered on the origin in the XZ plane, width along X and height along Z, divided into a grid of subdivisionsX by subdivisionsY cells.
createIcoSphereBuilds a sphere from evenly sized triangles, subdivided from an icosahedron, which shades more evenly than createSphere.
createLatheRevolves a profile around the Y axis, the way a lathe turns wood: shape is the profile as points in the XY plane with X as the distance from the axis, radius pushes it outward, and arc below 1 leaves the revolution open.
createPolygonBuilds a flat filled polygon from its outline points, with optional holes, lying in the XZ plane; a depth above 0 gives it thickness downward.
createPolyhedronBuilds one of the fifteen built-in polyhedra by type, from tetrahedron (0) to elongated pentagonal cupola (14), or your own from custom data.
createRectanglePlaneBuilds a flat rectangle centered on the origin in the XY plane, width along X and height along Y, facing the Z axis.
createRibbonBuilds a surface through several paths of points, joining neighboring paths with triangles, like cloth stretched over a set of lines.
createSphereBuilds a sphere of the given diameter centered on the origin; segments sets how finely it is divided, more being rounder and heavier.
createSquarePlaneBuilds a flat square of side size centered on the origin in the XY plane, facing the Z axis; a single-sided plane is invisible from behind unless sideOrientation is double-sided.
createTorusBuilds a ring lying in the XZ plane around the origin: diameter is the ring's overall diameter and thickness the diameter of its tube.
createTorusKnotBuilds a tube wound into a knot around the origin: p and q are how many times it winds around the ring and through its hole, so 2 and 3 give the classic trefoil.
createTubeBuilds a tube of the given radius along a path of points; cap closes neither, one or both ends, and arc below 1 leaves the tube open along its length.
extrudePolygonBuilds a solid by extruding a flat polygon, given by its outline points in the XZ plane with optional holes, downward by depth.

Matrices​

FunctionWhat it does
rotationCenterAxisBuilds a rotation about an axis that passes through a center point, as three matrices applied in order: move the center to the origin, rotate, move back.
rotationCenterXBuilds a rotation about a line parallel to the X axis through a center point, as three matrices applied in order: move the center to the origin, rotate, move back.
rotationCenterYBuilds a rotation about a line parallel to the Y axis through a center point, as three matrices applied in order: move the center to the origin, rotate, move back.
rotationCenterYawPitchRollBuilds a rotation from three angles about a center point: yaw turns about Y, pitch about X and roll about Z, in degrees.
rotationCenterZBuilds a rotation about a line parallel to the Z axis through a center point, as three matrices applied in order: move the center to the origin, rotate, move back.
scaleCenterXYZBuilds a scale with its own factor per axis, measured from a center point that stays in place, as three matrices applied in order: move the center to the origin, scale, move back.
scaleXYZBuilds a scale with its own factor per axis, measured from the origin; [2, 3, 1] doubles X, triples Y and keeps Z.
translationsXYZBuilds one move per vector, for transforming many objects each by its own vector, in the same order.
translationXYZBuilds a move by a vector; [10, 5, 0] moves 10 along X, 5 along Y and nothing along Z.
uniformScaleBuilds a scale by the same factor on every axis, measured from the origin, so 2 makes everything twice as big and twice as far from the origin.
uniformScaleFromCenterBuilds a scale by the same factor on every axis, measured from a center point that stays in place, as three matrices applied in order: move the center to the origin, scale, move back.