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 the local origin that runs along the path during the extrusion.
When you need the appearance of a solid shape then 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 that there are profile shapes that cause caps to not correctly fit the profile shape. In this case you can used 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 set to true.
MeshBuilder
Usage :
const options = { shape: myPoints, //vec3 array with z = 0, path: myPath, //vec3 array updatable: true}
let extruded = BABYLON.MeshBuilder.ExtrudeShape("ext", options, scene); //scene is optional and defaults to the current scene
// Updateoptions.shape = newShape;options.path = newPath;options.instance = extruded;extruded = BABYLON.MeshBuilder.ExtrudeShape("ext", options); //No scene parameter when using instance
option | value | default value |
---|---|---|
option shape | value (Vector3[]) array of Vector3, the shape you want to extrude REQUIRED | default value |
option path | value (Vector3[]) array of Vector3, the extrusion axis REQUIRED | default value |
option scale | value (number) the value to scale the shape | default value 1 |
option rotation | value (number) the value to rotate the shape each step along the path | default value 0 |
option cap | value (number) extrusion cap : NO_CAP, CAP_START, CAP_END, CAP_ALL | default value NO_CAP |
option closeShape | value (boolean) closes the shape, no need to push shape[0] to shape array | default value false |
option closePath | value (boolean) closes the path, no need to push path[0] to path array | default value false |
option updatable | value (boolean) true if the mesh is updatable | default value false |
option sideOrientation | value (number) side orientation | default value DEFAULTSIDE |
option frontUVs | value (Vector4) ONLY WHEN sideOrientation:BABYLON.Mesh.DOUBLESIDE is an option | default value Vector4(0,0, 1,1) |
option backUVs | value (Vector4) ONLY WHEN sideOrientation:BABYLON.Mesh.DOUBLESIDE is an option | default value Vector4(0,0, 1,1) |
option instance | value (LineMesh) an instance of an extruded shape to be updated | default value null |
option invertUV | value (boolean) to swap the U and V coordinates at geometry construction time (texture rotation of 90°) | default value false |
option firstNormal | value (Vector3) path normal of first point of path | default value null |
option adjustFrame | value (boolean) apply heuristic to adjust tangents of paths that reverse direction | default value 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:
let extrusion = BABYLON.Mesh.ExtrudeShape(name, shape, path, scale, rotation, cap, scene);let extrusion = BABYLON.Mesh.ExtrudeShape(name, shape, path, scale, rotation, cap, scene, updatable, sideOrientation, instance); //optional parameters after scene