ソースを参照

Allow earcut injection through meshbuilder methods

sebavan 6 年 前
コミット
3e27bcb9fd
2 ファイル変更19 行追加10 行削除
  1. 6 2
      src/Meshes/mesh.ts
  2. 13 8
      src/Meshes/meshBuilder.ts

+ 6 - 2
src/Meshes/mesh.ts

@@ -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
@@ -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.";
     }
 
@@ -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.";
     }
 

+ 13 - 8
src/Meshes/meshBuilder.ts

@@ -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,
@@ -129,17 +131,17 @@ 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,
@@ -147,7 +149,7 @@ Mesh.ExtrudePolygon = (name: string, shape: Vector3[], depth: number, scene: Sce
         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 => {
@@ -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 || [];
@@ -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++) {
@@ -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);
     }
 
     /**