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. Since the custom extrusion is based on the Babylon.js ribbon there are two options, ribbonClosePath which closes the profile shape and ribbonCloseArray, which closes the extrusion path array.

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 :

const options = {
shape: myPoints, //vec3 array with z = 0,
path: myPath, //vec3 array
rotationFunction: rotFn,
scaleFunction: scaleFn,
updatable: true
}
let extruded = BABYLON.MeshBuilder.ExtrudeShapeCustom("ext", options, scene); //scene is optional and defaults to the current scene
// Update
options.shape = newShape;
options.path = newPath;
options.instance = extruded;
options.rotationFunction = newRotFn;
options.scaleFunction = newScaleFn;
extruded = BABYLON.MeshBuilder.ExtrudeShapeCustom("ext", options); //No scene parameter when using instance
optionvaluedefault 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
scaleFunction
value
( function(i, distance) ) a function returning a scale value from (i, distance) parameters
default value
{return 1;}
option
rotationFunction
value
( function(i, distance) ) a function returning a rotation value from (i, distance) parameters
default value
{return 0;}
option
ribbonClosePath
value
(boolean) the underlying ribbon closePath parameter value
default value
false
option
ribbonCloseArray
value
(boolean) the underlying ribbon closeArray parameter value
default value
false
option
cap
value
(number) extrusion cap : NO_CAP, CAP_START, CAP_END, CAP_ALL
default value
NO_CAP
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

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

non updatable extrusion: Non Updatable Extrusion update of extrusion scaleFunction and rotation Function: Updatable Extrusion offset open profile shape path defined by trigonometry: Offset Open Profile Shape Path Defined By Trigonometry sine wave by alternately scaling positive/negative: Sine Wave By Alternately Scaling Positive/Negative example of constant scale and rotation evolving with the distance: Example Of Constant Scale And Rotation Evolving With The Distance non-linear rotation function: Non-Linear Rotation Function

offset open profile shape: Offset Open Profile Shape
open extrusion path: Open Extrusion path Extrusion with constant scale 1 and no rotation: Extrusion With Constant Scale ribbonCloseArray to true: Custom Extrusion With ribbonCloseArray ribbonClosePath to true instead: Custom Extrusion With ribbonClosePath Both true: Custom Extrusion With ribbonClosePath and ribbonCloseArray

  generate strange shapes: Strange Shapes With Custom Extrusion

Mesh

Usage:

let extrusion = BABYLON.Mesh.ExtrudeShapeCustom("extrusion", shape, path, scaleFunction, rotateFunction, ribbonCloseArray, ribbonClosePath, cap, scene);
let extrusion = BABYLON.Mesh.ExtrudeShapeCustom("extrusion", shape, path, scaleFunction, rotateFunction, ribbonCloseArray, ribbonClosePath, cap, scene, updatable, sideOrientation, instance); //optional parameters after scene
// fixed unit scale and zero rotation
let extrusion = BABYLON.Mesh.ExtrudeShapeCustom("extrusion", shape, path, () => {return 1}, () => {return 0}, ribbonCloseArray, ribbonClosePath, cap, scene);