API

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
// Update
options.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
optionvaluedefault 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 Extrusion

closed shape extrusion

Updatable Extrusion

update of extrusion scaleFunction and rotationFunction

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:

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 rotation
let extrusion = Mesh.ExtrudeShapeCustom("extrusion", shape, path, () => {
return 1;
}, () => {
return 0;
}, ribbonCloseArray, ribbonClosePath, cap, scene);