# Polygon Mesh Builder

## Polygon Mesh Builder

*CreatePolygon* and *ExtrudePolygon* which follow the same form as other mesh creations call *PolygonMeshBuilder* which can be used directly. It has some advantages but cannot be used with *faceUVs*. The data used in describing the polygon is given in 2D vectors, (x, y) rather than (x 0, z). Additionally, an array of vector2s as well as the polygon data can be passed as Path2 object. The advantage of using Path2 is that curves can be described using simple methods rather than your own functions. The data used to describe the polygon shape must be in counter clockwise order.

The way *PolygonMeshBuilder* works is to construct and return the triangulation of the polygon and then create the mesh from this triangulation using its *build* method. The triangulation requires an Earcut script. 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.

The build function takes two optional parameters: the first is a Boolean and is true if the mesh is to be updatable and the seconds is the depth of the extrusion when required.

## Constraints

The polygon and any holes inside it must be simple, that is no overlapping sides. Holes should be wholly inside the polygon and should not be too close to the sides of the polygon or to each other otherwise the mesh will be malformed. This algorithm is one you can use to check if a polygon is simple.

## Usage

`const polygon_triangulation = new BABYLON.PolygonMeshBuilder("name", vector2 array, scene);const polygon = polygon_triangulation.build();`

`var polygon_triangulation = new BABYLON.PolygonMeshBuilder("name", Path2, scene);var polygon = polygon_triangulation.build(false, 3);`

### Holes

A hole can only be given as an array of vector2, representing the corners of the hole in consecutive counter clockwise order around the hole.

Holes are added to the polygon triangulation using the addHole function.

`polygon_triangulation.addHole(hole1);polygon_triangulation.addHole(hole2);polygon_triangulation.addHole(hole3);var polygon = polygon_triangulation.build(true, 1.4);`

## Examples

Each example contains polygons described with vector2s and with Path2 Simple Polygons: Simple Polygons Simple Extruded Polygons: Simple Extruded Polygons Polygons with holes: Polygons With Holes Extruded Polygons with holes: Extruded Polygons With Holes