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. While 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 :
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";const polygon = MeshBuilder.CreatePolygon("polygon", options, scene); //scene is optional and defaults to the current scene| option | value | default value |
|---|---|---|
| shape | (Vector3[]) array of Vector3, the shape you want to build REQUIRED | |
| holes | (Vector3[][]) array of holes, each hole being an array of successive Vector3 | [] |
| 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) |
All vectors for the shape and holes are Vector3s and should be in the xz plane, i.e. 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
import { Mesh } from "@babylonjs/core/Meshes/mesh";let polygon = Mesh.CreatePolygon("polygon", shape, scene);let polygon = Mesh.CreatePolygon("polygon", shape, scene, holes, updatable, sideOrientation); //optional parameters after scene