Irregular Polygons

Non Regular Polygon

A polygon of any type, regular or irregular, convex or concave, is created in the horizontal xz plane. Since it can be concave it requires a procedure to fill the polygon with triangles and an Earcut method is needed. Whilst an Earcut script is pre-loaded in the Playground you will have to add a reference to such a script in your own projects. One is available at cdn or via a npm package.

On creation the local origin of the polygon is coincident with the world origin. It is not possible to give a position relative to the polygon as this depends on the data sets used.

CreatePolygon calls PolygonMeshBuilder. There is no instance option. You must set at least the shape option.

MeshBuilder

Usage :

const polygon = BABYLON.MeshBuilder.CreatePolygon("polygon", options, scene); //scene is optional and defaults to the current scene
optionvaluedefault value
option
shape
value
(Vector3[]) array of Vector3, the shape you want to build REQUIRED
default value
 
option
holes
value
(Vector3[][]) array of holes, each hole being an array of successive Vector3
default value
[]
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)

All vectors for shape and holes are Vector3 and should be in the xz plane, ie of the form BABYLON.Vector3(x, 0, z) and in counter clockwise order. You just list all the vertices of the polygon and CreatePolygon closes the shape.

Example

polygon with holes: Create a Polygon With Holes

Mesh

let polygon = BABYLON.Mesh.CreatePolygon("polygon", shape, scene);
let polygon = BABYLON.Mesh.CreatePolygon("polygon", shape, scene, holes, updatable, sideOrientation); //optional parameters after scene