Custom Extrusion
Custom Extruded Shapes
A custom extruded shape replaces the rotation and scale options with rotationFunction or scaleFunction. These allow you to vary the rotation and scale of the mesh as it extrudes by defining them in terms of a path index or a distance along the path.
On creation, the local origin of a ribbon 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.
MeshBuilder
Usage :
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";const options = { shape: myPoints, //vec3 array with z = 0, path: myPath, //vec3 array rotationFunction: rotFn, scaleFunction: scaleFn, updatable: true,};let extruded = MeshBuilder.ExtrudeShapeCustom("ext", options, scene); //scene is optional and defaults to the current scene// Updateoptions.shape = newShape;options.path = newPath;options.instance = extruded;options.rotationFunction = newRotFn;options.scaleFunction = newScaleFn;extruded = MeshBuilder.ExtrudeShapeCustom("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 | |
| scaleFunction | ( function(i, distance) ) a function returning a scale value from (i, distance) parameters | {return 1;} |
| rotationFunction | ( function(i, distance) ) a function returning a rotation value from (i, distance) parameters | {return 0;} |
| closeShape | (boolean) closes the shape, replaces ribbonClosePath | false |
| closePath | (boolean) closes the path, replaces ribbonCloseArray | false |
| ribbonClosePath | (boolean) the underlying ribbon closePath parameter value deprecated | false |
| ribbonCloseArray | (boolean) the underlying ribbon closeArray parameter value deprecated | false |
| cap | (number) extrusion cap : NO_CAP, CAP_START, CAP_END, CAP_ALL | NO_CAP |
| 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 | null |
| adjustFrame | (boolean) apply heuristic to adjust tangents of paths that reverse direction | false |
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 rotationFunction or scaleFunction options.
The scaleFunction and rotationFunction are called on each path point and require two parameters: index and distance.
- index refers to the path point position in the path array
- distance is the current point distance from the beginning of the path.
Examples
Closed Shape Extrusionclosed shape extrusion
Updatable Extrusionupdate of extrusion scaleFunction and rotationFunction
Offset Using Trigonometryoffset open profile shape path defined by trigonometry
Sine Wavesine wave by alternately scaling positive/negative
Rotation Evolving With The Distancescale constant and rotation changing with the distance
Non-Linear Rotation Functionnon-linear rotation function
Offset Open Profile Shapeoffset open profile shape
Open Extrusion pathopen extrusion path
Extrusion With Constant ScaleExtrusion with constant scale 1 and no rotation
Custom Extrusion With path closedclosePath set to true
closeShape set to true
closeShape and closePath both set to true
Using firstNormal and adjustFrameusing firstNormal and adjustFrame options
Strange Shapes With Custom Extrusiongenerate strange shapes
Mesh
Usage:
import { Mesh } from "@babylonjs/core/Meshes/mesh";let extrusion = Mesh.ExtrudeShapeCustom("extrusion", shape, path, scaleFunction, rotateFunction, ribbonCloseArray, ribbonClosePath, cap, scene);let extrusion = Mesh.ExtrudeShapeCustom("extrusion", shape, path, scaleFunction, rotateFunction, ribbonCloseArray, ribbonClosePath, cap, scene, updatable, sideOrientation, instance); //optional parameters after scene// fixed unit scale and zero rotationlet extrusion = Mesh.ExtrudeShapeCustom("extrusion", shape, path, () => { return 1;}, () => { return 0;}, ribbonCloseArray, ribbonClosePath, cap, scene);