Skip to content

Commit

Permalink
Allow earcut injection through meshbuilder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sebavan committed Jan 14, 2019
1 parent 04145df commit 3e27bcb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/Meshes/mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ declare type LinesMesh = import("./linesMesh").LinesMesh;
declare type InstancedMesh = import("./instancedMesh").InstancedMesh;
declare type GroundMesh = import("./groundMesh").GroundMesh;

declare var earcut: any;

/**
* Class used to represent a specific level of detail of a mesh
* @see http://doc.babylonjs.com/how_to/how_to_use_lod
Expand Down Expand Up @@ -3119,9 +3121,10 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
* @param holes is a required array of arrays of successive Vector3 used to defines holes in the polygon
* @param updatable defines if the mesh must be flagged as updatable
* @param sideOrientation defines the mesh side orientation (http://doc.babylonjs.com/babylon101/discover_basic_elements#side-orientation)
* @param earcutInjection can be used to inject your own earcut reference
* @returns a new Mesh
*/
public static CreatePolygon(name: string, shape: Vector3[], scene: Scene, holes?: Vector3[][], updatable?: boolean, sideOrientation?: number): Mesh {
public static CreatePolygon(name: string, shape: Vector3[], scene: Scene, holes?: Vector3[][], updatable?: boolean, sideOrientation?: number, earcutInjection = earcut): Mesh {
throw "Import MeshBuilder before creating meshes.";
}

Expand All @@ -3135,9 +3138,10 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
* @param holes is a required array of arrays of successive Vector3 used to defines holes in the polygon
* @param updatable defines if the mesh must be flagged as updatable
* @param sideOrientation defines the mesh side orientation (http://doc.babylonjs.com/babylon101/discover_basic_elements#side-orientation)
* @param earcutInjection can be used to inject your own earcut reference
* @returns a new Mesh
*/
public static ExtrudePolygon(name: string, shape: Vector3[], depth: number, scene: Scene, holes?: Vector3[][], updatable?: boolean, sideOrientation?: number): Mesh {
public static ExtrudePolygon(name: string, shape: Vector3[], depth: number, scene: Scene, holes?: Vector3[][], updatable?: boolean, sideOrientation?: number, earcutInjection = earcut): Mesh {
throw "Import MeshBuilder before creating meshes.";
}

Expand Down
21 changes: 13 additions & 8 deletions src/Meshes/meshBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { GroundMesh } from "./groundMesh";
import { PolygonMeshBuilder } from "./polygonMesh";
import { BoundingInfo } from "../Culling/boundingInfo";

declare var earcut: any;

Mesh.CreateRibbon = (name: string, pathArray: Vector3[][], closeArray: boolean = false, closePath: boolean, offset: number, scene?: Scene, updatable: boolean = false, sideOrientation?: number, instance?: Mesh) => {
return MeshBuilder.CreateRibbon(name, {
pathArray: pathArray,
Expand Down Expand Up @@ -129,25 +131,25 @@ Mesh.CreateDashedLines = (name: string, points: Vector3[], dashSize: number, gap
return MeshBuilder.CreateDashedLines(name, options, scene);
};

Mesh.CreatePolygon = (name: string, shape: Vector3[], scene: Scene, holes?: Vector3[][], updatable?: boolean, sideOrientation?: number): Mesh => {
Mesh.CreatePolygon = (name: string, shape: Vector3[], scene: Scene, holes?: Vector3[][], updatable?: boolean, sideOrientation?: number, earcutInjection = earcut): Mesh => {
var options = {
shape: shape,
holes: holes,
updatable: updatable,
sideOrientation: sideOrientation
};
return MeshBuilder.CreatePolygon(name, options, scene);
return MeshBuilder.CreatePolygon(name, options, scene, earcutInjection);
};

Mesh.ExtrudePolygon = (name: string, shape: Vector3[], depth: number, scene: Scene, holes?: Vector3[][], updatable?: boolean, sideOrientation?: number): Mesh => {
Mesh.ExtrudePolygon = (name: string, shape: Vector3[], depth: number, scene: Scene, holes?: Vector3[][], updatable?: boolean, sideOrientation?: number, earcutInjection = earcut): Mesh => {
var options = {
shape: shape,
holes: holes,
depth: depth,
updatable: updatable,
sideOrientation: sideOrientation
};
return MeshBuilder.ExtrudePolygon(name, options, scene);
return MeshBuilder.ExtrudePolygon(name, options, scene, earcutInjection);
};

Mesh.ExtrudeShape = (name: string, shape: Vector3[], path: Vector3[], scale: number, rotation: number, cap: number, scene: Nullable<Scene> = null, updatable?: boolean, sideOrientation?: number, instance?: Mesh): Mesh => {
Expand Down Expand Up @@ -1135,9 +1137,10 @@ export class MeshBuilder {
* @param name defines the name of the mesh
* @param options defines the options used to create the mesh
* @param scene defines the hosting scene
* @param earcutInjection can be used to inject your own earcut reference
* @returns the polygon mesh
*/
public static CreatePolygon(name: string, options: { shape: Vector3[], holes?: Vector3[][], depth?: number, faceUV?: Vector4[], faceColors?: Color4[], updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }, scene: Scene): Mesh {
public static CreatePolygon(name: string, options: { shape: Vector3[], holes?: Vector3[][], depth?: number, faceUV?: Vector4[], faceColors?: Color4[], updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, }, scene: Scene, earcutInjection = earcut): Mesh {
options.sideOrientation = MeshBuilder._UpdateSideOrientation(options.sideOrientation);
var shape = options.shape;
var holes = options.holes || [];
Expand All @@ -1153,7 +1156,7 @@ export class MeshBuilder {
contours.pop();
}

var polygonTriangulation = new PolygonMeshBuilder(name, contours, scene);
var polygonTriangulation = new PolygonMeshBuilder(name, contours, scene, earcutInjection);
for (var hNb = 0; hNb < holes.length; hNb++) {
hole = [];
for (var hPoint = 0; hPoint < holes[hNb].length; hPoint++) {
Expand All @@ -1176,10 +1179,12 @@ export class MeshBuilder {
* @param name defines the name of the mesh
* @param options defines the options used to create the mesh
* @param scene defines the hosting scene
* @param earcutInjection can be used to inject your own earcut reference
* @returns the polygon mesh
*/
public static ExtrudePolygon(name: string, options: { shape: Vector3[], holes?: Vector3[][], depth?: number, faceUV?: Vector4[], faceColors?: Color4[], updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }, scene: Scene): Mesh {
return MeshBuilder.CreatePolygon(name, options, scene);
public static ExtrudePolygon(name: string, options: { shape: Vector3[], holes?: Vector3[][], depth?: number, faceUV?: Vector4[], faceColors?: Color4[], updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }, scene: Scene
, earcutInjection = earcut): Mesh {
return MeshBuilder.CreatePolygon(name, options, scene, earcutInjection);
}

/**
Expand Down

0 comments on commit 3e27bcb

Please sign in to comment.