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 :

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
closeShape
value
(boolean) closes the shape, replaces ribbonClosePath
default value
false
option
closePath
value
(boolean) closes the path, replaces ribbonCloseArray
default value
false
option
ribbonClosePath
value
(boolean) the underlying ribbon closePath parameter value depreceated
default value
false
option
ribbonCloseArray
value
(boolean) the underlying ribbon closeArray parameter value depreceated
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
option
firstNormal
value
(Vector3) path normal of first point
default value
null
option
adjustFrame
value
(boolean) apply heuristic to adjust tangents of paths that reverse direction
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

Closed Shape Extrusion

closed shape extrusion

Updatable Extrusion

update of extrusion scaleFunction and rotation Function

Offset Using Trigonometry

offset open profile shape path defined by trigonometry

Sine Wave

sine wave by alternately scaling positive/negative

Rotation Evolving With The Distance

scale constant and rotation changing 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

Extrusion with constant scale 1 and no rotation

Custom Extrusion With path closed

closePath set to true

Custom Extrusion With shape closed

closeShape set to true

Closed Shape and Path

closeShape and closePath both set to true

Using firstNormal and adjustFrame

using firstNormal and adjustFrame options

Strange Shapes With Custom Extrusion

generate strange shapes

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,
);