Browse Source

Plugin Injection

sebavan 6 years ago
parent
commit
1291d4e988

+ 9 - 3
src/Meshes/polygonMesh.ts

@@ -165,12 +165,18 @@ import { Mesh } from "../Meshes/mesh";
         }
 
         /**
+         * Babylon reference to the earcut plugin.
+         */
+        public bjsEarcut: any;
+
+        /**
          * Creates a PolygonMeshBuilder
          * @param name name of the builder
          * @param contours Path of the polygon
          * @param scene scene to add to
          */
-        constructor(name: string, contours: Path2 | Vector2[] | any, scene: Scene) {
+        constructor(name: string, contours: Path2 | Vector2[] | any, scene: Scene, earcutInjection = earcut) {
+            this.bjsEarcut = earcutInjection;
             this._name = name;
             this._scene = scene;
 
@@ -186,7 +192,7 @@ import { Mesh } from "../Meshes/mesh";
             this._points.add(points);
             this._outlinepoints.add(points);
 
-            if (typeof earcut === 'undefined') {
+            if (typeof this.bjsEarcut === 'undefined') {
                 Logger.Warn("Earcut was not found, the polygon will not be built.");
             }
         }
@@ -230,7 +236,7 @@ import { Mesh } from "../Meshes/mesh";
 
             var indices = new Array<number>();
 
-            let res = earcut(this._epoints, this._eholes, 2);
+            let res = this.bjsEarcut(this._epoints, this._eholes, 2);
 
             for (let i = 0; i < res.length; i++) {
                 indices.push(res[i]);

+ 5 - 4
src/Physics/Plugins/ammoJSPlugin.ts

@@ -51,12 +51,13 @@ import { AbstractMesh } from "../../Meshes/abstractMesh";
         /**
          * Initializes the ammoJS plugin
          * @param _useDeltaForWorldStep if the time between frames should be used when calculating physics steps (Default: true)
+         * @param ammoInjection can be used to inject your own ammo reference
          */
-        public constructor(private _useDeltaForWorldStep: boolean = true) {
-            if (typeof Ammo === "function") {
-                Ammo();
+        public constructor(private _useDeltaForWorldStep: boolean = true, ammoInjection: any = Ammo) {
+            if (typeof ammoInjection === "function") {
+                ammoInjection();
             }
-            this.bjsAMMO = Ammo;
+            this.bjsAMMO = ammoInjection;
             if (!this.isSupported()) {
                 Logger.Error("AmmoJS is not available. Please make sure you included the js file.");
                 return;

+ 3 - 2
src/Physics/Plugins/cannonJSPlugin.ts

@@ -19,9 +19,10 @@ import { PhysicsEngine } from "../../Physics/physicsEngine";
         private _physicsMaterials = new Array();
         private _fixedTimeStep: number = 1 / 60;
         //See https://github.com/schteppe/CANNON.js/blob/gh-pages/demos/collisionFilter.html
-        public BJSCANNON = CANNON;
+        public BJSCANNON: any;
 
-        public constructor(private _useDeltaForWorldStep: boolean = true, iterations: number = 10) {
+        public constructor(private _useDeltaForWorldStep: boolean = true, iterations: number = 10, cannonInjection = CANNON) {
+            this.BJSCANNON = cannonInjection;
             if (!this.isSupported()) {
                 Logger.Error("CannonJS is not available. Please make sure you included the js file.");
                 return;

+ 2 - 2
src/Physics/Plugins/oimoJSPlugin.ts

@@ -16,8 +16,8 @@ import { Logger } from "../../Misc/logger";
         public name: string = "OimoJSPlugin";
         public BJSOIMO: any;
 
-        constructor(iterations?: number) {
-            this.BJSOIMO = OIMO;
+        constructor(iterations?: number, oimoInjection = OIMO) {
+            this.BJSOIMO = oimoInjection;
             this.world = new this.BJSOIMO.World({
                 iterations: iterations
             });