Extruding Shapes
Extruded Shape
An extruded shape is created by defining a shape profile using Vector3 coordinates in the xy plane and providing a path along which the profile will be extruded. You must set at least the shape and path options. On update, you must set the shape, path, and instance options, and you can set the scale and rotation options.
On creation, the local origin of an extrusion is coincident with the world origin. It is not possible to give a position relative to the constructed shape, as this depends on the data sets used.
The profile shape's local origin is (0, 0, 0) relative to its defining coordinates, and it is this local origin that runs along the path during the extrusion.
When you need the appearance of a solid shape, there is an option to cap the ends. The caps are drawn by creating triangles from the barycenter of the shape profile to the profile vertices, so there are profile shapes for which the caps will not fit correctly. In this case, you can use CreatePolygon for the caps; however, you do need to position and rotate these caps in addition to creating them.
When you need sharp mitred corners, there is a utility function available: Extruded Shape with Mitred Corners.
Because the extrusion converts the path points to a Path3D, there are two anomalies that can occur for a given set of path points. The first is that the orientation of the normal to the path is undefined when the path is a straight line. The Path3D constructor will pick a first normal, which may not be the one needed. Use the firstNormal option to set the path normal at the first point. The second anomaly occurs when the path reverses itself from one point to the next; this causes the tangent at that point to become undefined. To apply a heuristic fix for this, use the adjustFrame option and set it to true.
MeshBuilder
Usage :
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";const options = { shape: myPoints, //vec3 array with z = 0, path: myPath, //vec3 array updatable: true};let extruded = MeshBuilder.ExtrudeShape("ext", options, scene); //scene is optional and defaults to the current scene// Updateoptions.shape = newShape;options.path = newPath;options.instance = extruded;extruded = MeshBuilder.ExtrudeShape("ext", options); //No scene parameter when using instance| option | value | default value |
|---|---|---|
| shape | (Vector3[]) array of Vector3, the shape you want to extrude REQUIRED | |
| path | (Vector3[]) array of Vector3, the extrusion axis REQUIRED | |
| scale | (number) the value to scale the shape | 1 |
| rotation | (number) the value to rotate the shape each step along the path | 0 |
| cap | (number) extrusion cap : NO_CAP, CAP_START, CAP_END, CAP_ALL | NO_CAP |
| closeShape | (boolean) closes the shape, no need to push shape[0] to shape array | false |
| closePath | (boolean) closes the path, no need to push path[0] to path array | false |
| updatable | (boolean) true if the mesh is updatable | false |
| sideOrientation | (number) side orientation | DEFAULTSIDE |
| frontUVs | (Vector4) ONLY WHEN sideOrientation:BABYLON.Mesh.DOUBLESIDE is an option | Vector4(0,0, 1,1) |
| backUVs | (Vector4) ONLY WHEN sideOrientation:BABYLON.Mesh.DOUBLESIDE is an option | Vector4(0,0, 1,1) |
| instance | (LineMesh) an instance of an extruded shape to be updated | null |
| invertUV | (boolean) to swap the U and V coordinates at geometry construction time (texture rotation of 90°) | false |
| firstNormal | (Vector3) path normal of first point of path | null |
| adjustFrame | (boolean) apply heuristic to adjust tangents of paths that reverse direction | false |
Examples
Close Shape by Pushclosed using shape.push(shape[0])
CloseShape is Trueclosed using closeShape: true
Updatable Extrusionupdate of extrusion including, shape, path, scale and rotation
Extrusion With Open Shapeextrusion with open shape
Spiral Extrusionspiral extrusion with straight path and rotation set:
Capped Extrusioncapped extrusion
Incorrectly Capped Extrusionshape profile that does not cap correctly
Extrusion firstNormalsetting first normal to control orientation
Extrusion path reverses directionextrusion path reverses causing artifacts
Mesh
Usage:
import { Mesh } from "@babylonjs/core/Meshes/mesh";let extrusion = Mesh.ExtrudeShape(name, shape, path, scale, rotation, cap, scene);let extrusion = Mesh.ExtrudeShape(name, shape, path, scale, rotation, cap, scene, updatable, sideOrientation, instance); //optional parameters after scene