ソースを参照

scene and engine can now be omitted :)

David Catuhe 8 年 前
コミット
cf996b2bff

ファイルの差分が大きいため隠しています
+ 22 - 22
dist/preview release/babylon.core.js


ファイルの差分が大きいため隠しています
+ 1654 - 1651
dist/preview release/babylon.d.ts


ファイルの差分が大きいため隠しています
+ 31 - 31
dist/preview release/babylon.js


+ 47 - 16
dist/preview release/babylon.max.js

@@ -7831,6 +7831,7 @@ var BABYLON;
                 }
             };
             this._renderingCanvas = canvas;
+            Engine.Instances.push(this);
             this._externalData = new BABYLON.StringDictionary();
             options = options || {};
             if (antialias != null) {
@@ -8037,6 +8038,30 @@ var BABYLON;
             this._badOS = regexp.test(navigator.userAgent);
             BABYLON.Tools.Log("Babylon.js engine (v" + Engine.Version + ") launched");
         }
+        Object.defineProperty(Engine, "LastCreatedEngine", {
+            get: function () {
+                if (Engine.Instances.length === 0) {
+                    return null;
+                }
+                return Engine.Instances[Engine.Instances.length - 1];
+            },
+            enumerable: true,
+            configurable: true
+        });
+        Object.defineProperty(Engine, "LastCreatedScene", {
+            get: function () {
+                var lastCreatedEngine = Engine.LastCreatedEngine;
+                if (!lastCreatedEngine) {
+                    return null;
+                }
+                if (lastCreatedEngine.scenes.length === 0) {
+                    return null;
+                }
+                return lastCreatedEngine.scenes[lastCreatedEngine.scenes.length - 1];
+            },
+            enumerable: true,
+            configurable: true
+        });
         Object.defineProperty(Engine, "NEVER", {
             get: function () {
                 return Engine._NEVER;
@@ -10344,6 +10369,11 @@ var BABYLON;
             document.removeEventListener("mspointerlockchange", this._onPointerLockChange);
             document.removeEventListener("mozpointerlockchange", this._onPointerLockChange);
             document.removeEventListener("webkitpointerlockchange", this._onPointerLockChange);
+            // Remove from Instances
+            var index = Engine.Instances.indexOf(this);
+            if (index >= 0) {
+                Engine.Instances.splice(index, 1);
+            }
         };
         // Loading screen
         Engine.prototype.displayLoadingUI = function () {
@@ -10508,6 +10538,7 @@ var BABYLON;
         };
         return Engine;
     }());
+    Engine.Instances = new Array();
     // Const statics
     Engine._ALPHA_DISABLE = 0;
     Engine._ALPHA_ADD = 1;
@@ -10589,7 +10620,7 @@ var BABYLON;
             this.onDisposeObservable = new BABYLON.Observable();
             this.name = name;
             this.id = name;
-            this._scene = scene;
+            this._scene = scene || BABYLON.Engine.LastCreatedScene;
             this._initCache();
         }
         Object.defineProperty(Node.prototype, "parent", {
@@ -11965,7 +11996,7 @@ var BABYLON;
                 }
                 _this.onCollisionPositionChangeObservable.notifyObservers(_this.position);
             };
-            scene.addMesh(_this);
+            _this.getScene().addMesh(_this);
             return _this;
         }
         Object.defineProperty(AbstractMesh, "BILLBOARDMODE_NONE", {
@@ -13694,7 +13725,7 @@ var BABYLON;
             _this.radius = 0.00001;
             _this._excludedMeshesIds = new Array();
             _this._includedOnlyMeshesIds = new Array();
-            scene.addLight(_this);
+            _this.getScene().addLight(_this);
             return _this;
         }
         Object.defineProperty(Light, "LIGHTMAP_DEFAULT", {
@@ -15571,9 +15602,9 @@ var BABYLON;
             _this._activeMeshes = new BABYLON.SmartArray(256);
             _this._globalPosition = BABYLON.Vector3.Zero();
             _this._refreshFrustumPlanes = true;
-            scene.addCamera(_this);
-            if (!scene.activeCamera) {
-                scene.activeCamera = _this;
+            _this.getScene().addCamera(_this);
+            if (!_this.getScene().activeCamera) {
+                _this.getScene().activeCamera = _this;
             }
             _this.position = position;
             return _this;
@@ -19531,8 +19562,8 @@ var BABYLON;
             this._transformMatrix = BABYLON.Matrix.Zero();
             this._edgesRenderers = new BABYLON.SmartArray(16);
             this._uniqueIdCounter = 0;
-            this._engine = engine;
-            engine.scenes.push(this);
+            this._engine = engine || BABYLON.Engine.LastCreatedEngine;
+            this._engine.scenes.push(this);
             this._externalData = new BABYLON.StringDictionary();
             this._uid = null;
             this._renderingManager = new BABYLON.RenderingManager(this);
@@ -26329,7 +26360,7 @@ var BABYLON;
             */
             this.onDisposeObservable = new BABYLON.Observable();
             this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NONE;
-            this._scene = scene;
+            this._scene = scene || BABYLON.Engine.LastCreatedScene;
             this._scene.textures.push(this);
             this._uid = null;
         }
@@ -29200,7 +29231,7 @@ var BABYLON;
             this._fillMode = Material.TriangleFillMode;
             this.name = name;
             this.id = name;
-            this._scene = scene;
+            this._scene = scene || BABYLON.Engine.LastCreatedScene;
             if (scene.useRightHandedSystem) {
                 this.sideOrientation = Material.ClockWiseSideOrientation;
             }
@@ -31614,7 +31645,7 @@ var BABYLON;
             this.texture = imgUrl ? new BABYLON.Texture(imgUrl, scene, true) : null;
             this.isBackground = isBackground === undefined ? true : isBackground;
             this.color = color === undefined ? new BABYLON.Color4(1, 1, 1, 1) : color;
-            this._scene = scene;
+            this._scene = scene || BABYLON.Engine.LastCreatedScene;
             this._scene.layers.push(this);
             var engine = scene.getEngine();
             // VBO
@@ -31824,7 +31855,7 @@ var BABYLON;
             this._actualFrame = 0;
             this.id = name;
             this._capacity = capacity;
-            this._scene = scene;
+            this._scene = scene || BABYLON.Engine.LastCreatedScene;
             this._customEffect = customEffect;
             scene.particleSystems.push(this);
             var indices = [];
@@ -34495,7 +34526,7 @@ var BABYLON;
              */
             this.onBeforeComputeObservable = new BABYLON.Observable();
             this.bones = [];
-            this._scene = scene;
+            this._scene = scene || BABYLON.Engine.LastCreatedScene;
             scene.skeletons.push(this);
             //make sure it will recalculate the matrix next time prepare is called.
             this._isDirty = true;
@@ -42501,7 +42532,7 @@ var BABYLON;
              * @type {BABYLON.Observable}
              */
             this.onSizeChangedObservable = new BABYLON.Observable();
-            this._scene = scene;
+            this._scene = scene || BABYLON.Engine.LastCreatedScene;
             var engine = scene.getEngine();
             this._engine = engine;
             this._maxSize = this._engine.getCaps().maxTextureSize;
@@ -49752,7 +49783,7 @@ var BABYLON;
             this.layerMask = 0x0FFFFFFF;
             this._vertexBuffers = {};
             this._isEnabled = true;
-            this._scene = scene;
+            this._scene = scene || BABYLON.Engine.LastCreatedScene;
             this._emitter = emitter;
             this.id = name;
             scene.lensFlareSystems.push(this);
@@ -53017,7 +53048,7 @@ var BABYLON;
             this._bSphereOnly = false;
             this._bSphereRadiusFactor = 1.0;
             this.name = name;
-            this._scene = scene;
+            this._scene = scene || BABYLON.Engine.LastCreatedScene;
             this._camera = scene.activeCamera;
             this._pickable = options ? options.isPickable : false;
             this._particlesIntersect = options ? options.particleIntersection : false;

ファイルの差分が大きいため隠しています
+ 1654 - 1651
dist/preview release/babylon.module.d.ts


ファイルの差分が大きいため隠しています
+ 31 - 31
dist/preview release/babylon.noworker.js


+ 1 - 1
src/Bones/babylon.skeleton.ts

@@ -26,7 +26,7 @@
         constructor(public name: string, public id: string, scene: Scene) {
             this.bones = [];
 
-            this._scene = scene;
+            this._scene = scene || Engine.LastCreatedScene;
 
             scene.skeletons.push(this);
 

+ 3 - 3
src/Cameras/babylon.camera.ts

@@ -138,10 +138,10 @@
         constructor(name: string, position: Vector3, scene: Scene) {
             super(name, scene);
 
-            scene.addCamera(this);
+            this.getScene().addCamera(this);
 
-            if (!scene.activeCamera) {
-                scene.activeCamera = this;
+            if (!this.getScene().activeCamera) {
+                this.getScene().activeCamera = this;
             }
 
             this.position = position;

+ 1 - 1
src/Layer/babylon.highlightlayer.ts

@@ -249,7 +249,7 @@ module BABYLON {
          * @param options Sets of none mandatory options to use with the layer (see IHighlightLayerOptions for more information)
          */
         constructor(name: string, scene: Scene, options?: IHighlightLayerOptions) {
-            this._scene = scene;
+            this._scene = scene || Engine.LastCreatedScene;
             var engine = scene.getEngine();
             this._engine = engine;
             this._maxSize = this._engine.getCaps().maxTextureSize;

+ 1 - 1
src/Layer/babylon.layer.ts

@@ -64,7 +64,7 @@
             this.isBackground = isBackground === undefined ? true : isBackground;
             this.color = color === undefined ? new Color4(1, 1, 1, 1) : color;
 
-            this._scene = scene;
+            this._scene = scene || Engine.LastCreatedScene;
             this._scene.layers.push(this);
 
             var engine = scene.getEngine();

+ 1 - 1
src/LensFlare/babylon.lensFlareSystem.ts

@@ -18,7 +18,7 @@
 
         constructor(public name: string, emitter: any, scene: Scene) {
 
-            this._scene = scene;
+            this._scene = scene || Engine.LastCreatedScene;
             this._emitter = emitter;
             this.id = name;
             scene.lensFlareSystems.push(this);

+ 1 - 1
src/Lights/babylon.light.ts

@@ -94,7 +94,7 @@
          */
         constructor(name: string, scene: Scene) {
             super(name, scene);
-            scene.addLight(this);
+            this.getScene().addLight(this);
         }
         /**
          * Returns the string "Light".  

+ 1 - 1
src/Materials/Textures/babylon.baseTexture.ts

@@ -69,7 +69,7 @@
         private _uid: string;
 
         constructor(scene: Scene) {
-            this._scene = scene;
+            this._scene = scene || Engine.LastCreatedScene;
             this._scene.textures.push(this);
             this._uid = null;
         }

+ 1 - 1
src/Materials/babylon.material.ts

@@ -213,7 +213,7 @@
             this.name = name;
             this.id = name;
 
-            this._scene = scene;
+            this._scene = scene || Engine.LastCreatedScene;
 
             if (scene.useRightHandedSystem) {
                 this.sideOrientation = Material.ClockWiseSideOrientation;

+ 1 - 1
src/Mesh/babylon.abstractMesh.ts

@@ -258,7 +258,7 @@
         constructor(name: string, scene: Scene) {
             super(name, scene);
 
-            scene.addMesh(this);
+            this.getScene().addMesh(this);
         }
 
         /**

+ 1 - 1
src/Particles/babylon.particleSystem.ts

@@ -106,7 +106,7 @@
             this.id = name;
             this._capacity = capacity;
 
-            this._scene = scene;
+            this._scene = scene || Engine.LastCreatedScene;
 
             this._customEffect = customEffect;
 

+ 1 - 1
src/Particles/babylon.solidParticleSystem.ts

@@ -129,7 +129,7 @@
         */
         constructor(name: string, scene: Scene, options?: { updatable?: boolean; isPickable?: boolean; particleIntersection?: boolean; boundingSphereOnly?: boolean; bSphereRadiusFactor?: number }) {
             this.name = name;
-            this._scene = scene;
+            this._scene = scene || Engine.LastCreatedScene;
             this._camera = <TargetCamera>scene.activeCamera;
             this._pickable = options ? options.isPickable : false;
             this._particlesIntersect = options ? options.particleIntersection : false;

+ 32 - 0
src/babylon.engine.ts

@@ -196,6 +196,29 @@
      * The engine class is responsible for interfacing with all lower-level APIs such as WebGL and Audio.
      */
     export class Engine {
+        public static Instances = new Array<Engine>();
+
+        public static get LastCreatedEngine(): Engine {
+            if (Engine.Instances.length === 0) {
+                return null;
+            }
+
+            return Engine.Instances[Engine.Instances.length - 1];
+        }
+
+        public static get LastCreatedScene(): Scene {
+            var lastCreatedEngine = Engine.LastCreatedEngine;
+            if (!lastCreatedEngine) {
+                return null;
+            }
+            
+            if (lastCreatedEngine.scenes.length === 0) {
+                return null;
+            }
+
+            return lastCreatedEngine.scenes[lastCreatedEngine.scenes.length - 1];
+        }
+
         // Const statics
         private static _ALPHA_DISABLE = 0;
         private static _ALPHA_ADD = 1;
@@ -513,6 +536,8 @@
         constructor(canvas: HTMLCanvasElement, antialias?: boolean, options?: EngineOptions, adaptToDeviceRatio = false) {
             this._renderingCanvas = canvas;
 
+            Engine.Instances.push(this);
+
             this._externalData = new StringDictionary<Object>();
 
             options = options || {};
@@ -3211,6 +3236,13 @@
             document.removeEventListener("mspointerlockchange", this._onPointerLockChange);
             document.removeEventListener("mozpointerlockchange", this._onPointerLockChange);
             document.removeEventListener("webkitpointerlockchange", this._onPointerLockChange);
+
+            // Remove from Instances
+            var index = Engine.Instances.indexOf(this);
+
+            if (index >= 0) {
+                Engine.Instances.splice(index, 1);
+            }
         }
 
         // Loading screen

+ 1 - 1
src/babylon.node.ts

@@ -94,7 +94,7 @@ module BABYLON {
         constructor(name: string, scene: Scene) {
             this.name = name;
             this.id = name;
-            this._scene = scene;
+            this._scene = scene || Engine.LastCreatedScene;
             this._initCache();
         }
 

+ 2 - 2
src/babylon.scene.ts

@@ -570,9 +570,9 @@
          * @param {BABYLON.Engine} engine - the engine to be used to render this scene.
          */
         constructor(engine: Engine) {
-            this._engine = engine;
+            this._engine = engine || Engine.LastCreatedEngine;
 
-            engine.scenes.push(this);
+            this._engine.scenes.push(this);
 
             this._externalData = new StringDictionary<Object>();
             this._uid = null;