瀏覽代碼

Performance improvements

David Catuhe 10 年之前
父節點
當前提交
d03134a478
共有 37 個文件被更改,包括 3424 次插入289 次删除
  1. 21 0
      Babylon/Animations/babylon.animation.js
  2. 22 1
      Babylon/Animations/babylon.animation.ts
  3. 17 11
      Babylon/Debug/babylon.debugLayer.js
  4. 1 1
      Babylon/Debug/babylon.debugLayer.js.map
  5. 59 29
      Babylon/Debug/babylon.debugLayer.ts
  6. 3 3
      Babylon/Lights/Shadows/babylon.shadowGenerator.js
  7. 24 24
      Babylon/Lights/Shadows/babylon.shadowGenerator.ts
  8. 5 5
      Babylon/Lights/babylon.directionalLight.js
  9. 12 12
      Babylon/Lights/babylon.directionalLight.ts
  10. 13 0
      Babylon/Lights/babylon.light.ts
  11. 3 3
      Babylon/Lights/babylon.pointLight.ts
  12. 17 9
      Babylon/Lights/babylon.spotLight.js
  13. 27 18
      Babylon/Lights/babylon.spotLight.ts
  14. 136 1
      Babylon/Loading/Plugins/babylon.babylonFileLoader.js
  15. 140 1
      Babylon/Loading/Plugins/babylon.babylonFileLoader.ts
  16. 51 0
      Babylon/Math/babylon.math.js
  17. 67 9
      Babylon/Math/babylon.math.ts
  18. 3 0
      Babylon/Mesh/babylon.abstractMesh.ts
  19. 58 0
      Babylon/Mesh/babylon.mesh.js
  20. 57 3
      Babylon/Mesh/babylon.mesh.ts
  21. 572 0
      Babylon/Mesh/babylon.meshSimplification.js
  22. 577 0
      Babylon/Mesh/babylon.meshSimplification.ts
  23. 11 0
      Babylon/Particles/babylon.particle.js
  24. 16 4
      Babylon/Particles/babylon.particle.ts
  25. 10 2
      Babylon/Particles/babylon.particleSystem.js
  26. 11 3
      Babylon/Particles/babylon.particleSystem.ts
  27. 2 2
      Babylon/Physics/Plugins/babylon.oimoJSPlugin.js
  28. 2 2
      Babylon/Physics/Plugins/babylon.oimoJSPlugin.ts
  29. 102 2
      Babylon/Tools/babylon.tools.js
  30. 99 3
      Babylon/Tools/babylon.tools.ts
  31. 57 18
      Babylon/babylon.engine.js
  32. 91 49
      Babylon/babylon.engine.ts
  33. 7 0
      Babylon/babylon.mixins.ts
  34. 16 0
      Babylon/babylon.scene.js
  35. 19 3
      Babylon/babylon.scene.ts
  36. 1078 53
      babylon.2.0-alpha.debug.js
  37. 18 18
      babylon.2.0-alpha.js

+ 21 - 0
Babylon/Animations/babylon.animation.js

@@ -82,6 +82,26 @@
             return BABYLON.Color3.Lerp(startValue, endValue, gradient);
         };
 
+        Animation.prototype.matrixInterpolateFunction = function (startValue, endValue, gradient) {
+            var startScale = new BABYLON.Vector3(0, 0, 0);
+            var startRotation = new BABYLON.Quaternion();
+            var startTranslation = new BABYLON.Vector3(0, 0, 0);
+            startValue.decompose(startScale, startRotation, startTranslation);
+
+            var endScale = new BABYLON.Vector3(0, 0, 0);
+            var endRotation = new BABYLON.Quaternion();
+            var endTranslation = new BABYLON.Vector3(0, 0, 0);
+            endValue.decompose(endScale, endRotation, endTranslation);
+
+            var resultScale = this.vector3InterpolateFunction(startScale, endScale, gradient);
+            var resultRotation = this.quaternionInterpolateFunction(startRotation, endRotation, gradient);
+            var resultTranslation = this.vector3InterpolateFunction(startTranslation, endTranslation, gradient);
+
+            var result = BABYLON.Matrix.Compose(resultScale, resultRotation, resultTranslation);
+
+            return result;
+        };
+
         Animation.prototype.clone = function () {
             var clone = new Animation(this.name, this.targetPropertyPath.join("."), this.framePerSecond, this.dataType, this.loopMode);
 
@@ -181,6 +201,7 @@
                             switch (loopMode) {
                                 case Animation.ANIMATIONLOOPMODE_CYCLE:
                                 case Animation.ANIMATIONLOOPMODE_CONSTANT:
+                                    return this.matrixInterpolateFunction(startValue, endValue, gradient);
                                 case Animation.ANIMATIONLOOPMODE_RELATIVE:
                                     return startValue;
                             }

+ 22 - 1
Babylon/Animations/babylon.animation.ts

@@ -41,7 +41,7 @@
 
             mesh.animations.push(animation);
 
-            mesh.getScene().beginAnimation(mesh, 0, totalFrame, (animation.loopMode === 1));
+            mesh.getScene().beginAnimation(mesh, 0, totalFrame,(animation.loopMode === 1));
 
         }
 
@@ -88,6 +88,26 @@
             return Color3.Lerp(startValue, endValue, gradient);
         }
 
+        public matrixInterpolateFunction(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
+            var startScale = new Vector3(0, 0, 0);
+            var startRotation = new Quaternion();
+            var startTranslation = new Vector3(0, 0, 0);
+            startValue.decompose(startScale, startRotation, startTranslation);
+
+            var endScale = new Vector3(0, 0, 0);
+            var endRotation = new Quaternion();
+            var endTranslation = new Vector3(0, 0, 0);
+            endValue.decompose(endScale, endRotation, endTranslation);
+
+            var resultScale = this.vector3InterpolateFunction(startScale, endScale, gradient);
+            var resultRotation = this.quaternionInterpolateFunction(startRotation, endRotation, gradient);
+            var resultTranslation = this.vector3InterpolateFunction(startTranslation, endTranslation, gradient);
+
+            var result = Matrix.Compose(resultScale, resultRotation, resultTranslation);
+
+            return result;
+        }
+
         public clone(): Animation {
             var clone = new Animation(this.name, this.targetPropertyPath.join("."), this.framePerSecond, this.dataType, this.loopMode);
 
@@ -189,6 +209,7 @@
                             switch (loopMode) {
                                 case Animation.ANIMATIONLOOPMODE_CYCLE:
                                 case Animation.ANIMATIONLOOPMODE_CONSTANT:
+                                    return this.matrixInterpolateFunction(startValue, endValue, gradient);
                                 case Animation.ANIMATIONLOOPMODE_RELATIVE:
                                     return startValue;
                             }

文件差異過大導致無法顯示
+ 17 - 11
Babylon/Debug/babylon.debugLayer.js


文件差異過大導致無法顯示
+ 1 - 1
Babylon/Debug/babylon.debugLayer.js.map


+ 59 - 29
Babylon/Debug/babylon.debugLayer.ts

@@ -37,19 +37,21 @@
 
         public accentColor = "orange";
 
+        public customStatsFunction: () => string;
+
         constructor(scene: Scene) {
             this._scene = scene;
 
             this._syncPositions = (): void => {
                 var engine = this._scene.getEngine();
                 var canvasRect = engine.getRenderingCanvasClientRect();
-                
+
                 if (this._showUI) {
-                    this._statsDiv.style.left = (canvasRect.width - 310) + "px";
-                    this._statsDiv.style.top = (canvasRect.height - 370) + "px";
-                    this._statsDiv.style.width = "300px";
-                    this._statsDiv.style.height = "360px";
-                    this._statsSubsetDiv.style.maxHeight = (canvasRect.height - 60) + "px";
+                    this._statsDiv.style.left = (canvasRect.width - 410) + "px";
+                    this._statsDiv.style.top = (canvasRect.height - 290) + "px";
+                    this._statsDiv.style.width = "400px";
+                    this._statsDiv.style.height = "auto";
+                    this._statsSubsetDiv.style.maxHeight = "240px";
 
                     this._optionsDiv.style.left = "0px";
                     this._optionsDiv.style.top = "10px";
@@ -66,7 +68,7 @@
                     this._treeDiv.style.top = "10px";
                     this._treeDiv.style.width = "300px";
                     this._treeDiv.style.height = "auto";
-                    this._treeSubsetDiv.style.maxHeight = (canvasRect.height - 430) + "px";
+                    this._treeSubsetDiv.style.maxHeight = (canvasRect.height - 340) + "px";
                 }
 
                 this._globalDiv.style.left = canvasRect.left + "px";
@@ -187,10 +189,10 @@
 
                             if (!this.shouldDisplayLabel || this.shouldDisplayLabel(light)) {
                                 this._renderLabel(light.name, projectedPosition, -20,
-                                () => {
-                                    light.setEnabled(!light.isEnabled());
-                                },
-                                () => { return light.isEnabled() ? "orange" : "gray"; });
+                                    () => {
+                                        light.setEnabled(!light.isEnabled());
+                                    },
+                                    () => { return light.isEnabled() ? "orange" : "gray"; });
                             }
 
                         }
@@ -227,7 +229,7 @@
                 this._generateAdvancedCheckBox(this._treeSubsetDiv, mesh.name, mesh.getTotalVertices() + " verts", mesh.isVisible, (element, m) => {
                     m.isVisible = element.checked;
                 }, mesh);
-            } 
+            }
         }
 
         private _renderSingleAxis(zero: Vector3, unit: Vector3, unitText: Vector3, label: string, color: string) {
@@ -505,12 +507,11 @@
                 this._statsDiv.style.position = "absolute";
                 this._statsDiv.style.background = background;
                 this._statsDiv.style.padding = "0px 0px 0px 5px";
-                this._statsDiv.style.pointerEvents = "none";
-                this._statsDiv.style.overflowY = "auto";
                 this._generateheader(this._statsDiv, "STATISTICS");
                 this._statsSubsetDiv = document.createElement("div");
                 this._statsSubsetDiv.style.paddingTop = "5px";
                 this._statsSubsetDiv.style.paddingBottom = "5px";
+                this._statsSubsetDiv.style.overflowY = "auto";
                 this._statsDiv.appendChild(this._statsSubsetDiv);
 
                 // Tree
@@ -567,13 +568,15 @@
                 this._optionsSubsetDiv.style.maxHeight = "200px";
                 this._optionsDiv.appendChild(this._optionsSubsetDiv);
 
-                this._generateTexBox(this._optionsSubsetDiv, "<b>General:</b>", this.accentColor);
+                this._generateTexBox(this._optionsSubsetDiv, "<b>Windows:</b>", this.accentColor);
                 this._generateCheckBox(this._optionsSubsetDiv, "Statistics", this._displayStatistics, (element) => { this._displayStatistics = element.checked });
                 this._generateCheckBox(this._optionsSubsetDiv, "Logs", this._displayLogs, (element) => { this._displayLogs = element.checked });
                 this._generateCheckBox(this._optionsSubsetDiv, "Meshes tree", this._displayTree, (element) => {
                     this._displayTree = element.checked;
                     this._needToRefreshMeshesTree = true;
                 });
+                this._optionsSubsetDiv.appendChild(document.createElement("br"));
+                this._generateTexBox(this._optionsSubsetDiv, "<b>General:</b>", this.accentColor);
                 this._generateCheckBox(this._optionsSubsetDiv, "Bounding boxes", this._scene.forceShowBoundingBoxes, (element) => { this._scene.forceShowBoundingBoxes = element.checked });
                 this._generateCheckBox(this._optionsSubsetDiv, "Clickable labels", this._labelsEnabled, (element) => {
                     this._labelsEnabled = element.checked;
@@ -581,7 +584,7 @@
                         this._clearLabels();
                     }
                 });
-                this._generateCheckBox(this._optionsSubsetDiv, "Generate user marks", Tools.PerformanceLogLevel === Tools.PerformanceUserMarkLogLevel,
+                this._generateCheckBox(this._optionsSubsetDiv, "Generate user marks (F12)", Tools.PerformanceLogLevel === Tools.PerformanceUserMarkLogLevel,
                     (element) => {
                         if (element.checked) {
                             Tools.PerformanceLogLevel = Tools.PerformanceUserMarkLogLevel;
@@ -645,21 +648,48 @@
         private _displayStats() {
             var scene = this._scene;
             var engine = scene.getEngine();
+            var glInfo = engine.getGlInfo();
 
             this._statsSubsetDiv.innerHTML = "Babylon.js v" + Engine.Version + " - <b>" + Tools.Format(engine.getFps(), 0) + " fps</b><br><br>"
-            + "Total meshes: " + scene.meshes.length + "<br>"
-            + "Total vertices: " + scene.getTotalVertices() + "<br>"
-            + "Active meshes: " + scene.getActiveMeshes().length + "<br>"
-            + "Active vertices: " + scene.getActiveVertices() + "<br>"
-            + "Active bones: " + scene.getActiveBones() + "<br>"
-            + "Active particles: " + scene.getActiveParticles() + "<br><br>"
-            + "Frame duration: " + Tools.Format(scene.getLastFrameDuration()) + " ms<br>"
-            + "<b>Draw calls: " + engine.drawCalls + "</b><br><br>"
-            + "<i>Evaluate Active Meshes duration:</i> " + Tools.Format(scene.getEvaluateActiveMeshesDuration()) + " ms<br>"
-            + "<i>Render Targets duration:</i> " + Tools.Format(scene.getRenderTargetsDuration()) + " ms<br>"
-            + "<i>Particles duration:</i> " + Tools.Format(scene.getParticlesDuration()) + " ms<br>"
-            + "<i>Sprites duration:</i> " + Tools.Format(scene.getSpritesDuration()) + " ms<br>"
-            + "<i>Render duration:</i> <b>" + Tools.Format(scene.getRenderDuration()) + " ms</b>";
+                + "<div style='column-count: 2;-moz-column-count:2;-webkit-column-count:2'>"
+                + "<b>Count</b><br>"
+                + "Total meshes: " + scene.meshes.length + "<br>"
+                + "Total vertices: " + scene.getTotalVertices() + "<br>"
+                + "Total materials: " + scene.materials.length + "<br>"
+                + "Total textures: " + scene.textures.length + "<br>"
+                + "Active meshes: " + scene.getActiveMeshes().length + "<br>"
+                + "Active vertices: " + scene.getActiveVertices() + "<br>"
+                + "Active bones: " + scene.getActiveBones() + "<br>"
+                + "Active particles: " + scene.getActiveParticles() + "<br>"
+                + "<b>Draw calls: " + engine.drawCalls + "</b><br><br>"
+                + "<b>Duration</b><br>"
+                + "Meshes selection:</i> " + Tools.Format(scene.getEvaluateActiveMeshesDuration()) + " ms<br>"
+                + "Render Targets: " + Tools.Format(scene.getRenderTargetsDuration()) + " ms<br>"
+                + "Particles: " + Tools.Format(scene.getParticlesDuration()) + " ms<br>"
+                + "Sprites: " + Tools.Format(scene.getSpritesDuration()) + " ms<br><br>"
+                + "Render: <b>" + Tools.Format(scene.getRenderDuration()) + " ms</b><br>"
+                + "Frame: " + Tools.Format(scene.getLastFrameDuration()) + " ms<br>"
+                + "Potential FPS: " + Tools.Format(1000.0 / scene.getLastFrameDuration(), 0) + "<br><br>"
+                + "</div>"
+                + "<div style='column-count: 2;-moz-column-count:2;-webkit-column-count:2'>"
+                + "<b>Extensions</b><br>"
+                + "Std derivatives: " + (engine.getCaps().standardDerivatives ? "Yes" : "No") + "<br>"
+                + "Compressed textures: " + (engine.getCaps().s3tc ? "Yes" : "No") + "<br>"
+                + "Hardware instances: " + (engine.getCaps().instancedArrays ? "Yes" : "No") + "<br>"
+                + "Texture float: " + (engine.getCaps().textureFloat ? "Yes" : "No") + "<br>"
+                + "32bits indices: " + (engine.getCaps().uintIndices ? "Yes" : "No") + "<br>"
+                + "<b>Caps.</b><br>"
+                + "Max textures units: " + engine.getCaps().maxTexturesImageUnits + "<br>"
+                + "Max textures size: " + engine.getCaps().maxTextureSize + "<br>"
+                + "Max anisotropy: " + engine.getCaps().maxAnisotropy + "<br><br><br>"
+                + "</div><br>"
+                + "<b>Info</b><br>"
+                + glInfo.version + "<br>"
+                + glInfo.renderer + "<br>";
+
+            if (this.customStatsFunction) {
+                this._statsSubsetDiv.innerHTML += this._statsSubsetDiv.innerHTML;
+            }
         }
     }
 }

+ 3 - 3
Babylon/Lights/Shadows/babylon.shadowGenerator.js

@@ -198,7 +198,7 @@
 
             // Get correct effect
             var join = defines.join("\n");
-            if (this._cachedDefines != join) {
+            if (this._cachedDefines !== join) {
                 this._cachedDefines = join;
                 this._effect = this._scene.getEngine().createEffect("shadowMap", attribs, ["world", "mBones", "viewProjection", "diffuseMatrix"], ["diffuseSampler"], join);
             }
@@ -219,8 +219,8 @@
             var lightPosition = this._light.position;
             var lightDirection = this._light.direction;
 
-            if (this._light._computeTransformedPosition()) {
-                lightPosition = this._light._transformedPosition;
+            if (this._light.computeTransformedPosition()) {
+                lightPosition = this._light.transformedPosition;
             }
 
             if (!this._cachedPosition || !this._cachedDirection || !lightPosition.equals(this._cachedPosition) || !lightDirection.equals(this._cachedDirection)) {

+ 24 - 24
Babylon/Lights/Shadows/babylon.shadowGenerator.ts

@@ -34,31 +34,31 @@
             this.filter = (value ? ShadowGenerator.FILTER_POISSONSAMPLING : ShadowGenerator.FILTER_NONE);
         }
 
-        private _light: DirectionalLight;
+        private _light: IShadowLight;
         private _scene: Scene;
         private _shadowMap: RenderTargetTexture;
         private _darkness = 0;
         private _transparencyShadow = false;
         private _effect: Effect;
 
-        private _viewMatrix = BABYLON.Matrix.Zero();
-        private _projectionMatrix = BABYLON.Matrix.Zero();
-        private _transformMatrix = BABYLON.Matrix.Zero();
-        private _worldViewProjection = BABYLON.Matrix.Zero();
+        private _viewMatrix = Matrix.Zero();
+        private _projectionMatrix = Matrix.Zero();
+        private _transformMatrix = Matrix.Zero();
+        private _worldViewProjection = Matrix.Zero();
         private _cachedPosition: Vector3;
         private _cachedDirection: Vector3;
         private _cachedDefines: string;
 
-        constructor(mapSize: number, light: DirectionalLight) {
+        constructor(mapSize: number, light: IShadowLight) {
             this._light = light;
             this._scene = light.getScene();
 
             light._shadowGenerator = this;
 
             // Render target
-            this._shadowMap = new BABYLON.RenderTargetTexture(light.name + "_shadowMap", mapSize, this._scene, false);
-            this._shadowMap.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
-            this._shadowMap.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
+            this._shadowMap = new RenderTargetTexture(light.name + "_shadowMap", mapSize, this._scene, false);
+            this._shadowMap.wrapU = Texture.CLAMP_ADDRESSMODE;
+            this._shadowMap.wrapV = Texture.CLAMP_ADDRESSMODE;
             this._shadowMap.renderParticles = false;
 
             // Custom render function
@@ -94,7 +94,7 @@
                     }
 
                     // Bones
-                    var useBones = mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind);
+                    var useBones = mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind);
 
                     if (useBones) {
                         this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
@@ -154,7 +154,7 @@
                 defines.push("#define VSM");
             }
 
-            var attribs = [BABYLON.VertexBuffer.PositionKind];
+            var attribs = [VertexBuffer.PositionKind];
 
             var mesh = subMesh.getMesh();
             var scene = mesh.getScene();
@@ -163,20 +163,20 @@
             // Alpha test
             if (material && material.needAlphaTesting()) {
                 defines.push("#define ALPHATEST");
-                if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
-                    attribs.push(BABYLON.VertexBuffer.UVKind);
+                if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
+                    attribs.push(VertexBuffer.UVKind);
                     defines.push("#define UV1");
                 }
-                if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
-                    attribs.push(BABYLON.VertexBuffer.UV2Kind);
+                if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
+                    attribs.push(VertexBuffer.UV2Kind);
                     defines.push("#define UV2");
                 }
             }
 
             // Bones
-            if (mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
-                attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
-                attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
+            if (mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind)) {
+                attribs.push(VertexBuffer.MatricesIndicesKind);
+                attribs.push(VertexBuffer.MatricesWeightsKind);
                 defines.push("#define BONES");
                 defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
             }
@@ -192,7 +192,7 @@
 
             // Get correct effect      
             var join = defines.join("\n");
-            if (this._cachedDefines != join) {
+            if (this._cachedDefines !== join) {
                 this._cachedDefines = join;
                 this._effect = this._scene.getEngine().createEffect("shadowMap",
                     attribs,
@@ -207,7 +207,7 @@
             return this._shadowMap;
         }
 
-        public getLight(): DirectionalLight {
+        public getLight(): IShadowLight {
             return this._light;
         }
 
@@ -216,8 +216,8 @@
             var lightPosition = this._light.position;
             var lightDirection = this._light.direction;
 
-            if (this._light._computeTransformedPosition()) {
-                lightPosition = this._light._transformedPosition;
+            if (this._light.computeTransformedPosition()) {
+                lightPosition = this._light.transformedPosition;
             }
 
             if (!this._cachedPosition || !this._cachedDirection || !lightPosition.equals(this._cachedPosition) || !lightDirection.equals(this._cachedDirection)) {
@@ -227,8 +227,8 @@
 
                 var activeCamera = this._scene.activeCamera;
 
-                BABYLON.Matrix.LookAtLHToRef(lightPosition, this._light.position.add(lightDirection), BABYLON.Vector3.Up(), this._viewMatrix);
-                BABYLON.Matrix.PerspectiveFovLHToRef(Math.PI / 2.0, 1.0, activeCamera.minZ, activeCamera.maxZ, this._projectionMatrix);
+                Matrix.LookAtLHToRef(lightPosition, this._light.position.add(lightDirection), Vector3.Up(), this._viewMatrix);
+                Matrix.PerspectiveFovLHToRef(Math.PI / 2.0, 1.0, activeCamera.minZ, activeCamera.maxZ, this._projectionMatrix);
 
                 this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
             }

+ 5 - 5
Babylon/Lights/babylon.directionalLight.js

@@ -15,7 +15,7 @@ var BABYLON;
             this.position = direction.scale(-1);
         }
         DirectionalLight.prototype.getAbsolutePosition = function () {
-            return this._transformedPosition ? this._transformedPosition : this.position;
+            return this.transformedPosition ? this.transformedPosition : this.position;
         };
 
         DirectionalLight.prototype.setDirectionToTarget = function (target) {
@@ -23,13 +23,13 @@ var BABYLON;
             return this.direction;
         };
 
-        DirectionalLight.prototype._computeTransformedPosition = function () {
+        DirectionalLight.prototype.computeTransformedPosition = function () {
             if (this.parent && this.parent.getWorldMatrix) {
-                if (!this._transformedPosition) {
-                    this._transformedPosition = BABYLON.Vector3.Zero();
+                if (!this.transformedPosition) {
+                    this.transformedPosition = BABYLON.Vector3.Zero();
                 }
 
-                BABYLON.Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this._transformedPosition);
+                BABYLON.Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this.transformedPosition);
                 return true;
             }
 

+ 12 - 12
Babylon/Lights/babylon.directionalLight.ts

@@ -1,9 +1,9 @@
 module BABYLON {
-    export class DirectionalLight extends Light {
+    export class DirectionalLight extends Light implements IShadowLight {
         public position: Vector3;
 
         private _transformedDirection: Vector3;
-        public _transformedPosition: Vector3;
+        public transformedPosition: Vector3;
         private _worldMatrix: Matrix;
 
         constructor(name: string, public direction: Vector3, scene: Scene) {
@@ -13,21 +13,21 @@
         }
 
         public getAbsolutePosition(): Vector3 {
-            return this._transformedPosition ? this._transformedPosition : this.position;
+            return this.transformedPosition ? this.transformedPosition : this.position;
         }
 
         public setDirectionToTarget(target: Vector3): Vector3 {
-            this.direction = BABYLON.Vector3.Normalize(target.subtract(this.position));
+            this.direction = Vector3.Normalize(target.subtract(this.position));
             return this.direction;
         }
 
-        public _computeTransformedPosition(): boolean {
+        public computeTransformedPosition(): boolean {
             if (this.parent && this.parent.getWorldMatrix) {
-                if (!this._transformedPosition) {
-                    this._transformedPosition = BABYLON.Vector3.Zero();
+                if (!this.transformedPosition) {
+                    this.transformedPosition = Vector3.Zero();
                 }
 
-                BABYLON.Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this._transformedPosition);
+                Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this.transformedPosition);
                 return true;
             }
 
@@ -37,10 +37,10 @@
         public transferToEffect(effect: Effect, directionUniformName: string): void {
             if (this.parent && this.parent.getWorldMatrix) {
                 if (!this._transformedDirection) {
-                    this._transformedDirection = BABYLON.Vector3.Zero();
+                    this._transformedDirection = Vector3.Zero();
                 }
 
-                BABYLON.Vector3.TransformNormalToRef(this.direction, this.parent.getWorldMatrix(), this._transformedDirection);
+                Vector3.TransformNormalToRef(this.direction, this.parent.getWorldMatrix(), this._transformedDirection);
                 effect.setFloat4(directionUniformName, this._transformedDirection.x, this._transformedDirection.y, this._transformedDirection.z, 1);
 
                 return;
@@ -51,10 +51,10 @@
 
         public _getWorldMatrix(): Matrix {
             if (!this._worldMatrix) {
-                this._worldMatrix = BABYLON.Matrix.Identity();
+                this._worldMatrix = Matrix.Identity();
             }
 
-            BABYLON.Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
+            Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
 
             return this._worldMatrix;
         }

+ 13 - 0
Babylon/Lights/babylon.light.ts

@@ -1,4 +1,17 @@
 module BABYLON {
+
+    export interface IShadowLight {
+        position: Vector3;
+        direction: Vector3;
+        transformedPosition: Vector3;
+        name: string;
+
+        computeTransformedPosition(): boolean;
+        getScene(): Scene;
+
+        _shadowGenerator: ShadowGenerator;
+    }
+
     export class Light extends Node {
         public diffuse = new Color3(1.0, 1.0, 1.0);
         public specular = new Color3(1.0, 1.0, 1.0);

+ 3 - 3
Babylon/Lights/babylon.pointLight.ts

@@ -14,10 +14,10 @@
         public transferToEffect(effect: Effect, positionUniformName: string): void {
             if (this.parent && this.parent.getWorldMatrix) {
                 if (!this._transformedPosition) {
-                    this._transformedPosition = BABYLON.Vector3.Zero();
+                    this._transformedPosition = Vector3.Zero();
                 }
 
-                BABYLON.Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this._transformedPosition);
+                Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this._transformedPosition);
                 effect.setFloat4(positionUniformName, this._transformedPosition.x, this._transformedPosition.y, this._transformedPosition.z, 0);
 
                 return;
@@ -32,7 +32,7 @@
 
         public _getWorldMatrix(): Matrix {
             if (!this._worldMatrix) {
-                this._worldMatrix = BABYLON.Matrix.Identity();
+                this._worldMatrix = Matrix.Identity();
             }
 
             Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);

+ 17 - 9
Babylon/Lights/babylon.spotLight.js

@@ -16,7 +16,7 @@ var BABYLON;
             this.exponent = exponent;
         }
         SpotLight.prototype.getAbsolutePosition = function () {
-            return this._transformedPosition ? this._transformedPosition : this.position;
+            return this.transformedPosition ? this.transformedPosition : this.position;
         };
 
         SpotLight.prototype.setDirectionToTarget = function (target) {
@@ -24,6 +24,19 @@ var BABYLON;
             return this.direction;
         };
 
+        SpotLight.prototype.computeTransformedPosition = function () {
+            if (this.parent && this.parent.getWorldMatrix) {
+                if (!this.transformedPosition) {
+                    this.transformedPosition = BABYLON.Vector3.Zero();
+                }
+
+                BABYLON.Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this.transformedPosition);
+                return true;
+            }
+
+            return false;
+        };
+
         SpotLight.prototype.transferToEffect = function (effect, positionUniformName, directionUniformName) {
             var normalizeDirection;
 
@@ -32,16 +45,11 @@ var BABYLON;
                     this._transformedDirection = BABYLON.Vector3.Zero();
                 }
 
-                if (!this._transformedPosition) {
-                    this._transformedPosition = BABYLON.Vector3.Zero();
-                }
-
-                var parentWorldMatrix = this.parent.getWorldMatrix();
+                this.computeTransformedPosition();
 
-                BABYLON.Vector3.TransformCoordinatesToRef(this.position, parentWorldMatrix, this._transformedPosition);
-                BABYLON.Vector3.TransformNormalToRef(this.direction, parentWorldMatrix, this._transformedDirection);
+                BABYLON.Vector3.TransformNormalToRef(this.direction, this.parent.getWorldMatrix(), this._transformedDirection);
 
-                effect.setFloat4(positionUniformName, this._transformedPosition.x, this._transformedPosition.y, this._transformedPosition.z, this.exponent);
+                effect.setFloat4(positionUniformName, this.transformedPosition.x, this.transformedPosition.y, this.transformedPosition.z, this.exponent);
                 normalizeDirection = BABYLON.Vector3.Normalize(this._transformedDirection);
             } else {
                 effect.setFloat4(positionUniformName, this.position.x, this.position.y, this.position.z, this.exponent);

+ 27 - 18
Babylon/Lights/babylon.spotLight.ts

@@ -1,8 +1,9 @@
 module BABYLON {
-    export class SpotLight extends Light {
+    export class SpotLight extends Light implements IShadowLight {
+
+        public transformedPosition: Vector3;
 
         private _transformedDirection: Vector3;
-        private _transformedPosition: Vector3;
         private _worldMatrix: Matrix;
 
         constructor(name: string, public position: Vector3, public direction: Vector3, public angle: number, public exponent: number, scene: Scene) {
@@ -10,36 +11,44 @@
         }
 
         public getAbsolutePosition(): Vector3 {
-            return this._transformedPosition ? this._transformedPosition : this.position;
+            return this.transformedPosition ? this.transformedPosition : this.position;
         }
 
         public setDirectionToTarget(target: Vector3): Vector3 {
-            this.direction = BABYLON.Vector3.Normalize(target.subtract(this.position));
+            this.direction = Vector3.Normalize(target.subtract(this.position));
             return this.direction;
         }
 
+        public computeTransformedPosition(): boolean {
+            if (this.parent && this.parent.getWorldMatrix) {
+                if (!this.transformedPosition) {
+                    this.transformedPosition = Vector3.Zero();
+                }
+
+                Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this.transformedPosition);
+                return true;
+            }
+
+            return false;
+        }
+
         public transferToEffect(effect: Effect, positionUniformName: string, directionUniformName: string): void {
             var normalizeDirection;
 
             if (this.parent && this.parent.getWorldMatrix) {
                 if (!this._transformedDirection) {
-                    this._transformedDirection = BABYLON.Vector3.Zero();
+                    this._transformedDirection = Vector3.Zero();
                 }
 
-                if (!this._transformedPosition) {
-                    this._transformedPosition = BABYLON.Vector3.Zero();
-                }
-
-                var parentWorldMatrix = this.parent.getWorldMatrix();
-
-                BABYLON.Vector3.TransformCoordinatesToRef(this.position, parentWorldMatrix, this._transformedPosition);
-                BABYLON.Vector3.TransformNormalToRef(this.direction, parentWorldMatrix, this._transformedDirection);
+                this.computeTransformedPosition();
+                
+                Vector3.TransformNormalToRef(this.direction, this.parent.getWorldMatrix(), this._transformedDirection);
 
-                effect.setFloat4(positionUniformName, this._transformedPosition.x, this._transformedPosition.y, this._transformedPosition.z, this.exponent);
-                normalizeDirection = BABYLON.Vector3.Normalize(this._transformedDirection);
+                effect.setFloat4(positionUniformName, this.transformedPosition.x, this.transformedPosition.y, this.transformedPosition.z, this.exponent);
+                normalizeDirection = Vector3.Normalize(this._transformedDirection);
             } else {
                 effect.setFloat4(positionUniformName, this.position.x, this.position.y, this.position.z, this.exponent);
-                normalizeDirection = BABYLON.Vector3.Normalize(this.direction);
+                normalizeDirection = Vector3.Normalize(this.direction);
             }
 
             effect.setFloat4(directionUniformName, normalizeDirection.x, normalizeDirection.y, normalizeDirection.z, Math.cos(this.angle * 0.5));
@@ -47,10 +56,10 @@
 
         public _getWorldMatrix(): Matrix {
             if (!this._worldMatrix) {
-                this._worldMatrix = BABYLON.Matrix.Identity();
+                this._worldMatrix = Matrix.Identity();
             }
 
-            BABYLON.Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
+            Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
 
             return this._worldMatrix;
         }

+ 136 - 1
Babylon/Loading/Plugins/babylon.babylonFileLoader.js

@@ -362,6 +362,11 @@
                 light._includedOnlyMeshesIds = parsedLight.includedOnlyMeshesIds;
             }
 
+            // Actions
+            if (parsedLight.actions !== undefined) {
+                light._waitingActions = parsedLight.actions;
+            }
+
             // Animations
             if (parsedLight.animations) {
                 for (var animationIndex = 0; animationIndex < parsedLight.animations.length; animationIndex++) {
@@ -392,7 +397,7 @@
                     camera = new BABYLON.ArcRotateCamera(parsedCamera.name, alpha, beta, radius, lockedTargetMesh, scene);
                 }
             } else if (parsedCamera.type === "AnaglyphFreeCamera") {
-                var eye_space = parsedCamera.eye_space;
+                eye_space = parsedCamera.eye_space;
                 camera = new BABYLON.AnaglyphFreeCamera(parsedCamera.name, position, eye_space, scene);
             } else if (parsedCamera.type === "DeviceOrientationCamera") {
                 camera = new BABYLON.DeviceOrientationCamera(parsedCamera.name, position, scene);
@@ -436,6 +441,11 @@
                 camera._waitingParentId = parsedCamera.parentId;
             }
 
+            // Actions
+            if (parsedCamera.actions !== undefined) {
+                camera._waitingActions = parsedCamera.actions;
+            }
+
             // Target
             if (parsedCamera.target) {
                 camera.setTarget(BABYLON.Vector3.FromArray(parsedCamera.target));
@@ -677,6 +687,11 @@
                 mesh._waitingParentId = parsedMesh.parentId;
             }
 
+            // Actions
+            if (parsedMesh.actions !== undefined) {
+                mesh._waitingActions = parsedMesh.actions;
+            }
+
             // Geometry
             mesh.hasVertexAlpha = parsedMesh.hasVertexAlpha;
 
@@ -793,6 +808,117 @@
             return mesh;
         };
 
+        var parseActions = function (parsedActions, object, scene) {
+            object.actionManager = new BABYLON.ActionManager(scene);
+
+            // instanciate a new object
+            var instanciate = function (name, params) {
+                var newInstance = Object.create(BABYLON[name].prototype);
+                newInstance.constructor.apply(newInstance, params);
+                return newInstance;
+            };
+
+            var parseParameter = function (name, value, target, propertyPath) {
+                var split = value.split(",");
+
+                if (split.length == 1) {
+                    var num = parseFloat(split[0]);
+                    if (isNaN(num))
+                        return split[0];
+                    else
+                        return num;
+                }
+
+                var effectiveTarget = propertyPath.split(".");
+                for (var i = 0; i < effectiveTarget.length; i++) {
+                    target = target[effectiveTarget[i]];
+                }
+
+                if (split.length == 3) {
+                    var values = [parseFloat(split[0]), parseFloat(split[1]), parseFloat(split[2])];
+                    if (target instanceof BABYLON.Vector3)
+                        return BABYLON.Vector3.FromArray(values);
+                    else
+                        return BABYLON.Color3.FromArray(values);
+                }
+
+                if (split.length == 4) {
+                    var values = [parseFloat(split[0]), parseFloat(split[1]), parseFloat(split[2]), parseFloat(split[3])];
+                    if (target instanceof BABYLON.Vector4)
+                        return BABYLON.Vector4.FromArray(values);
+                    else
+                        return BABYLON.Color4.FromArray(values);
+                }
+            };
+
+            // traverse graph per trigger
+            var traverse = function (parsedAction, trigger, condition, action, actionManager) {
+                var parameters = new Array();
+                var target = null;
+                var propertyPath = null;
+
+                // Parameters
+                if (parsedAction.type == 2)
+                    parameters.push(actionManager);
+                else
+                    parameters.push(trigger);
+
+                for (var i = 0; i < parsedAction.properties.length; i++) {
+                    var value = parsedAction.properties[i].value;
+                    if (parsedAction.properties[i].name === "target") {
+                        value = target = scene.getNodeByName(value);
+                    } else if (parsedAction.properties[i].name != "propertyPath") {
+                        if (value === "false" || value === "true")
+                            value = value === "true";
+                        else if (parsedAction.type === 2 && parsedAction.properties[i].name === "operator")
+                            value = BABYLON.ValueCondition[value];
+                        else
+                            value = parseParameter(parsedAction.properties[i].name, value, target, propertyPath);
+                    } else {
+                        propertyPath = value;
+                    }
+                    parameters.push(value);
+                }
+                parameters.push(condition);
+
+                // If interpolate value action
+                if (parsedAction.name === "InterpolateValueAction") {
+                    var param = parameters[parameters.length - 2];
+                    parameters[parameters.length - 1] = param;
+                    parameters[parameters.length - 2] = condition;
+                }
+
+                // Action or condition
+                var newAction = instanciate(parsedAction.name, parameters);
+                if (newAction instanceof BABYLON.Condition) {
+                    condition = newAction;
+                    newAction = action;
+                } else {
+                    condition = null;
+                    if (action)
+                        action.then(newAction);
+                    else
+                        actionManager.registerAction(newAction);
+                }
+
+                for (var i = 0; i < parsedAction.children.length; i++)
+                    traverse(parsedAction.children[i], trigger, condition, newAction, actionManager);
+            };
+
+            for (var i = 0; i < parsedActions.children.length; i++) {
+                var triggerParams;
+                var trigger = parsedActions.children[i];
+
+                if (trigger.properties.length > 0) {
+                    triggerParams = { trigger: BABYLON.ActionManager[trigger.name], parameter: scene.getMeshByName(trigger.properties[0].value) };
+                } else
+                    triggerParams = BABYLON.ActionManager[trigger.name];
+
+                for (var j = 0; j < trigger.children.length; j++)
+                    traverse(trigger.children[j], triggerParams, null, null, object.actionManager);
+            }
+        };
+
         var isDescendantOf = function (mesh, names, hierarchyIds) {
             names = (names instanceof Array) ? names : [names];
             for (var i in names) {
@@ -1245,6 +1371,10 @@
                         mesh.parent = scene.getLastEntryByID(mesh._waitingParentId);
                         mesh._waitingParentId = undefined;
                     }
+                    if (mesh._waitingActions) {
+                        parseActions(mesh._waitingActions, mesh, scene);
+                        mesh._waitingActions = undefined;
+                    }
                 }
 
                 // Particles Systems
@@ -1272,6 +1402,11 @@
                     }
                 }
 
+                // Actions (scene)
+                if (parsedData.actions) {
+                    parseActions(parsedData.actions, scene, scene);
+                }
+
                 // Finish
                 return true;
             }

+ 140 - 1
Babylon/Loading/Plugins/babylon.babylonFileLoader.ts

@@ -361,6 +361,11 @@
             light._includedOnlyMeshesIds = parsedLight.includedOnlyMeshesIds;
         }
 
+        // Actions
+        if (parsedLight.actions !== undefined) {
+            light._waitingActions = parsedLight.actions;
+        }
+
         // Animations
         if (parsedLight.animations) {
             for (var animationIndex = 0; animationIndex < parsedLight.animations.length; animationIndex++) {
@@ -392,7 +397,7 @@
             }
 
         } else if (parsedCamera.type === "AnaglyphFreeCamera") {
-            var eye_space = parsedCamera.eye_space;
+            eye_space = parsedCamera.eye_space;
             camera = new AnaglyphFreeCamera(parsedCamera.name, position, eye_space, scene);
 
         } else if (parsedCamera.type === "DeviceOrientationCamera") {
@@ -446,6 +451,11 @@
             camera._waitingParentId = parsedCamera.parentId;
         }
 
+        // Actions
+        if (parsedCamera.actions !== undefined) {
+            camera._waitingActions = parsedCamera.actions;
+        }
+
         // Target
         if (parsedCamera.target) {
             camera.setTarget(BABYLON.Vector3.FromArray(parsedCamera.target));
@@ -687,6 +697,11 @@
             mesh._waitingParentId = parsedMesh.parentId;
         }
 
+        // Actions
+        if (parsedMesh.actions !== undefined) {
+            mesh._waitingActions = parsedMesh.actions;
+        }
+
         // Geometry
         mesh.hasVertexAlpha = parsedMesh.hasVertexAlpha;
 
@@ -804,6 +819,121 @@
         return mesh;
     };
 
+    var parseActions = (parsedActions: any, object: any, scene: Scene) => {
+        object.actionManager = new BABYLON.ActionManager(scene);
+
+        // instanciate a new object
+        var instanciate = (name: any, params: Array<any>): any => {
+            var newInstance: Object = Object.create(BABYLON[name].prototype);
+            newInstance.constructor.apply(newInstance, params);
+            return newInstance;
+        };
+
+        var parseParameter = (name: String, value: String, target: any, propertyPath: String): any => {
+            var split = value.split(",");
+
+            if (split.length == 1) {
+                var num = parseFloat(split[0]);
+                if (isNaN(num))
+                    return split[0];
+                else
+                    return num;
+            }
+
+            var effectiveTarget = propertyPath.split(".");
+            for (var i = 0; i < effectiveTarget.length; i++) {
+                target = target[effectiveTarget[i]];
+            }
+
+            if (split.length == 3) {
+                var values = [parseFloat(split[0]), parseFloat(split[1]), parseFloat(split[2])];
+                if (target instanceof BABYLON.Vector3)
+                    return BABYLON.Vector3.FromArray(values);
+                else
+                    return BABYLON.Color3.FromArray(values);
+            }
+
+            if (split.length == 4) {
+                var values = [parseFloat(split[0]), parseFloat(split[1]), parseFloat(split[2]), parseFloat(split[3])];
+                if (target instanceof Vector4)
+                    return BABYLON.Vector4.FromArray(values);
+                else
+                    return BABYLON.Color4.FromArray(values);
+            }
+        };
+
+        // traverse graph per trigger
+        var traverse = (parsedAction: any, trigger: any, condition: Condition, action: Action, actionManager: ActionManager) => {
+            var parameters = new Array();
+            var target: any = null;
+            var propertyPath = null;
+
+            // Parameters
+            if (parsedAction.type == 2)
+                parameters.push(actionManager);
+            else
+                parameters.push(trigger);
+
+            for (var i = 0; i < parsedAction.properties.length; i++) {
+                var value = parsedAction.properties[i].value;
+                if (parsedAction.properties[i].name === "target") {
+                    value = target = scene.getNodeByName(value);
+                }
+                else if (parsedAction.properties[i].name != "propertyPath") {
+                    if (value === "false" || value === "true")
+                        value = value === "true";
+                    else if (parsedAction.type === 2 && parsedAction.properties[i].name === "operator")
+                        value = BABYLON.ValueCondition[value];
+                    else
+                        value = parseParameter(parsedAction.properties[i].name, value, target, propertyPath);
+                }
+                else {
+                    propertyPath = value;
+                }
+                parameters.push(value);
+            }
+            parameters.push(condition);
+
+            // If interpolate value action
+            if (parsedAction.name === "InterpolateValueAction") {
+                var param = parameters[parameters.length - 2];
+                parameters[parameters.length - 1] = param;
+                parameters[parameters.length - 2] = condition;
+            }
+
+            // Action or condition
+            var newAction = instanciate(parsedAction.name, parameters);
+            if (newAction instanceof BABYLON.Condition) {
+                condition = newAction;
+                newAction = action;
+            } else {
+                condition = null;
+                if (action)
+                    action.then(newAction);
+                else
+                    actionManager.registerAction(newAction);
+            }
+
+            for (var i = 0; i < parsedAction.children.length; i++)
+                traverse(parsedAction.children[i], trigger, condition, newAction, actionManager);
+        };
+
+        // triggers
+        for (var i = 0; i < parsedActions.children.length; i++) {
+            var triggerParams: any;
+            var trigger = parsedActions.children[i];
+
+            if (trigger.properties.length > 0) {
+                triggerParams = { trigger: BABYLON.ActionManager[trigger.name], parameter: scene.getMeshByName(trigger.properties[0].value) };
+            }
+            else
+                triggerParams = BABYLON.ActionManager[trigger.name];
+
+            for (var j = 0; j < trigger.children.length; j++)
+                traverse(trigger.children[j], triggerParams, null, null, object.actionManager);
+        }
+
+    };
 
     var isDescendantOf = (mesh, names, hierarchyIds) => {
         names = (names instanceof Array) ? names : [names];
@@ -1263,6 +1393,10 @@
                     mesh.parent = scene.getLastEntryByID(mesh._waitingParentId);
                     mesh._waitingParentId = undefined;
                 }
+                if (mesh._waitingActions) {
+                    parseActions(mesh._waitingActions, mesh, scene);
+                    mesh._waitingActions = undefined;
+                }
             }
 
             // Particles Systems
@@ -1290,6 +1424,11 @@
                 }
             }
 
+            // Actions (scene)
+            if (parsedData.actions) {
+                parseActions(parsedData.actions, scene, scene);
+            }
+
             // Finish
             return true;
         }

+ 51 - 0
Babylon/Math/babylon.math.js

@@ -216,6 +216,13 @@
             return new Color4(this.r, this.g, this.b, this.a);
         };
 
+        Color4.prototype.copyFrom = function (source) {
+            this.r = source.r;
+            this.g = source.g;
+            this.b = source.b;
+            this.a = source.a;
+        };
+
         // Statics
         Color4.Lerp = function (left, right, amount) {
             var result = new Color4(0, 0, 0, 0);
@@ -1410,6 +1417,10 @@
             return new Quaternion(-q.x, -q.y, -q.z, q.w);
         };
 
+        Quaternion.Identity = function () {
+            return new Quaternion(0, 0, 0, 1);
+        };
+
         Quaternion.RotationAxis = function (axis, angle) {
             var result = new Quaternion();
             var sin = Math.sin(angle / 2);
@@ -1677,6 +1688,34 @@
             return Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5], this.m[6], this.m[7], this.m[8], this.m[9], this.m[10], this.m[11], this.m[12], this.m[13], this.m[14], this.m[15]);
         };
 
+        Matrix.prototype.decompose = function (scale, rotation, translation) {
+            translation.x = this.m[12];
+            translation.y = this.m[13];
+            translation.z = this.m[14];
+
+            var xs = BABYLON.Tools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
+            var ys = BABYLON.Tools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
+            var zs = BABYLON.Tools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
+
+            scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
+            scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
+            scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
+
+            if (scale.x == 0 || scale.y == 0 || scale.z == 0) {
+                rotation.x = 0;
+                rotation.y = 0;
+                rotation.z = 0;
+                rotation.w = 1;
+                return false;
+            }
+
+            var rotationMatrix = BABYLON.Matrix.FromValues(this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0, this.m[4] / scale.y, this.m[5] / scale.y, this.m[6] / scale.y, 0, this.m[8] / scale.z, this.m[9] / scale.z, this.m[10] / scale.z, 0, 0, 0, 0, 1);
+
+            rotation.fromRotationMatrix(rotationMatrix);
+
+            return true;
+        };
+
         // Statics
         Matrix.FromArray = function (array, offset) {
             var result = new Matrix();
@@ -1738,6 +1777,18 @@
             return result;
         };
 
+        Matrix.Compose = function (scale, rotation, translation) {
+            var result = Matrix.FromValues(scale.x, 0, 0, 0, 0, scale.y, 0, 0, 0, 0, scale.z, 0, 0, 0, 0, 1);
+
+            var rotationMatrix = Matrix.Identity();
+            rotation.toRotationMatrix(rotationMatrix);
+            result = result.multiply(rotationMatrix);
+
+            result.setTranslation(translation);
+
+            return result;
+        };
+
         Matrix.Identity = function () {
             return Matrix.FromValues(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0);
         };

+ 67 - 9
Babylon/Math/babylon.math.ts

@@ -186,6 +186,13 @@
             return new Color4(this.r, this.g, this.b, this.a);
         }
 
+        public copyFrom(source: Color4): void {
+            this.r = source.r;
+            this.g = source.g;
+            this.b = source.b;
+            this.a = source.a;
+        }
+
         // Statics
         public static Lerp(left: Color4, right: Color4, amount: number): Color4 {
             var result = new Color4(0, 0, 0, 0);
@@ -1371,6 +1378,10 @@
             return new Quaternion(-q.x, -q.y, -q.z, q.w);
         }
 
+        public static Identity(): Quaternion {
+            return new Quaternion(0, 0, 0, 1);
+        }
+
         public static RotationAxis(axis: Vector3, angle: number): Quaternion {
             var result = new Quaternion();
             var sin = Math.sin(angle / 2);
@@ -1440,7 +1451,7 @@
                 num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
             }
 
-            return new Quaternion((num3 * left.x) + (num2 * right.x), (num3 * left.y) + (num2 * right.y), (num3 * left.z) + (num2 * right.z), (num3 * left.w) + (num2 * right.w));
+            return new Quaternion((num3 * left.x) + (num2 * right.x),(num3 * left.y) + (num2 * right.y),(num3 * left.z) + (num2 * right.z),(num3 * left.w) + (num2 * right.w));
         }
     }
 
@@ -1641,9 +1652,9 @@
         public equals(value: Matrix): boolean {
             return value &&
                 (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
-                this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
-                this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
-                this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
+                    this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
+                    this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
+                    this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
         }
 
         public clone(): Matrix {
@@ -1653,6 +1664,38 @@
                 this.m[12], this.m[13], this.m[14], this.m[15]);
         }
 
+        public decompose(scale: Vector3, rotation: Quaternion, translation: Vector3) {
+            translation.x = this.m[12];
+            translation.y = this.m[13];
+            translation.z = this.m[14];
+
+            var xs = Tools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
+            var ys = Tools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
+            var zs = Tools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
+
+            scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
+            scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
+            scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
+
+            if (scale.x == 0 || scale.y == 0 || scale.z == 0) {
+                rotation.x = 0;
+                rotation.y = 0;
+                rotation.z = 0;
+                rotation.w = 1;
+                return false;
+            }
+
+            var rotationMatrix = BABYLON.Matrix.FromValues(
+                this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0,
+                this.m[4] / scale.y, this.m[5] / scale.y, this.m[6] / scale.y, 0,
+                this.m[8] / scale.z, this.m[9] / scale.z, this.m[10] / scale.z, 0,
+                0, 0, 0, 1);
+
+            rotation.fromRotationMatrix(rotationMatrix);
+
+            return true;
+        }
+
         // Statics
         public static FromArray(array: number[], offset?: number): Matrix {
             var result = new Matrix();
@@ -1723,6 +1766,21 @@
             return result;
         }
 
+        public static Compose(scale: Vector3, rotation: Quaternion, translation: Vector3): Matrix {
+            var result = Matrix.FromValues(scale.x, 0, 0, 0,
+                0, scale.y, 0, 0,
+                0, 0, scale.z, 0,
+                0, 0, 0, 1);
+
+            var rotationMatrix = Matrix.Identity();
+            rotation.toRotationMatrix(rotationMatrix);
+            result = result.multiply(rotationMatrix);
+
+            result.setTranslation(translation);
+
+            return result;
+        }
+
         public static Identity(): Matrix {
             return Matrix.FromValues(1.0, 0, 0, 0,
                 0, 1.0, 0, 0,
@@ -2612,7 +2670,7 @@
     }
 
     export class PathCursor {
-        private _onchange = new Array <(cursor: PathCursor) => void>();
+        private _onchange = new Array<(cursor: PathCursor) => void>();
 
         value: number = 0;
         animations = new Array<Animation>();
@@ -2620,14 +2678,14 @@
         constructor(private path: Path2) {
         }
 
-        getPoint() : Vector3 {
+        getPoint(): Vector3 {
             var point = this.path.getPointAtLengthPosition(this.value);
             return new Vector3(point.x, 0, point.y);
         }
 
         moveAhead(step: number = 0.002) {
             this.move(step);
-            
+
         }
 
         moveBack(step: number = 0.002) {
@@ -2635,7 +2693,7 @@
         }
 
         move(step: number) {
-            
+
             if (Math.abs(step) > 1) {
                 throw "step size should be less than 1.";
             }
@@ -2648,7 +2706,7 @@
         private ensureLimits() {
             while (this.value > 1) {
                 this.value -= 1;
-            } 
+            }
             while (this.value < 0) {
                 this.value += 1;
             }

+ 3 - 0
Babylon/Mesh/babylon.abstractMesh.ts

@@ -106,6 +106,9 @@
 
         private _onAfterWorldMatrixUpdate = new Array<(mesh: BABYLON.AbstractMesh) => void>();
 
+        // Loading properties
+        public _waitingActions: any;
+
         constructor(name: string, scene: Scene) {
             super(name, scene);
 

+ 58 - 0
Babylon/Mesh/babylon.mesh.js

@@ -996,6 +996,64 @@ var BABYLON;
             }
         };
 
+        /**
+        * Simplify the mesh according to the given array of settings.
+        * Function will return immediately and will simplify async.
+        * @param settings a collection of simplification settings.
+        * @param parallelProcessing should all levels calculate parallel or one after the other.
+        * @param type the type of simplification to run.
+        * successCallback optional success callback to be called after the simplification finished processing all settings.
+        */
+        Mesh.prototype.simplify = function (settings, parallelProcessing, type, successCallback) {
+            var _this = this;
+            if (typeof parallelProcessing === "undefined") { parallelProcessing = true; }
+            if (typeof type === "undefined") { type = 0 /* QUADRATIC */; }
+            var getSimplifier = function () {
+                switch (type) {
+                    case 0 /* QUADRATIC */:
+                    default:
+                        return new BABYLON.QuadraticErrorSimplification(_this);
+                }
+            };
+
+            if (parallelProcessing) {
+                //parallel simplifier
+                settings.forEach(function (setting) {
+                    var simplifier = getSimplifier();
+                    simplifier.simplify(setting, function (newMesh) {
+                        _this.addLODLevel(setting.distance, newMesh);
+
+                        //check if it is the last
+                        if (setting.quality == settings[settings.length - 1].quality && successCallback) {
+                            //all done, run the success callback.
+                            successCallback();
+                        }
+                    });
+                });
+            } else {
+                //single simplifier.
+                var simplifier = getSimplifier();
+
+                var runDecimation = function (setting, callback) {
+                    simplifier.simplify(setting, function (newMesh) {
+                        _this.addLODLevel(setting.distance, newMesh);
+
+                        //run the next quality level
+                        callback();
+                    });
+                };
+
+                BABYLON.AsyncLoop.Run(settings.length, function (loop) {
+                    runDecimation(settings[loop.index], function () {
+                        loop.executeNext();
+                    });
+                }, function () {
+                    //execution ended, run the success callback.
+                    successCallback();
+                });
+            }
+        };
+
         // Statics
         Mesh.CreateBox = function (name, size, scene, updatable) {
             var box = new BABYLON.Mesh(name, scene);

+ 57 - 3
Babylon/Mesh/babylon.mesh.ts

@@ -39,8 +39,8 @@
          */
         constructor(name: string, scene: Scene, parent: Node = null, source?: Mesh, doNotCloneChildren?: boolean) {
             super(name, scene);
-            
-            if (source){
+
+            if (source) {
                 // Geometry
                 if (source._geometry) {
                     source._geometry.applyToMesh(this);
@@ -59,7 +59,7 @@
 
                         if (mesh.parent === source) {
                             // doNotCloneChildren is always going to be False
-                            var newChild = mesh.clone(name + "." + mesh.name, this, doNotCloneChildren); 
+                            var newChild = mesh.clone(name + "." + mesh.name, this, doNotCloneChildren);
                         }
                     }
                 }
@@ -1004,6 +1004,60 @@
             }
         }
 
+        /**
+         * Simplify the mesh according to the given array of settings.
+         * Function will return immediately and will simplify async.
+         * @param settings a collection of simplification settings.
+         * @param parallelProcessing should all levels calculate parallel or one after the other.
+         * @param type the type of simplification to run.
+         * successCallback optional success callback to be called after the simplification finished processing all settings.
+         */
+        public simplify(settings: Array<ISimplificationSettings>, parallelProcessing: boolean = true, type: SimplificationType = SimplificationType.QUADRATIC, successCallback?: () => void) {
+
+            var getSimplifier = (): ISimplifier => {
+                switch (type) {
+                    case SimplificationType.QUADRATIC:
+                    default:
+                        return new QuadraticErrorSimplification(this);
+                }
+            }
+
+            if (parallelProcessing) {
+                //parallel simplifier
+                settings.forEach((setting) => {
+                    var simplifier = getSimplifier();
+                    simplifier.simplify(setting, (newMesh) => {
+                        this.addLODLevel(setting.distance, newMesh);
+                        //check if it is the last
+                        if (setting.quality == settings[settings.length - 1].quality && successCallback) {
+                            //all done, run the success callback.
+                            successCallback();
+                        }
+                    });
+                });
+            } else {
+                //single simplifier.
+                var simplifier = getSimplifier();
+
+                var runDecimation = (setting: ISimplificationSettings, callback: () => void) => {
+                    simplifier.simplify(setting, (newMesh) => {
+                        this.addLODLevel(setting.distance, newMesh);
+                        //run the next quality level
+                        callback();
+                    });
+                }
+
+                AsyncLoop.Run(settings.length, (loop: AsyncLoop) => {
+                    runDecimation(settings[loop.index], () => {
+                        loop.executeNext();
+                    });
+                }, () => {
+                        //execution ended, run the success callback.
+                        successCallback();
+                    });
+            }
+        }
+
         // Statics
         public static CreateBox(name: string, size: number, scene: Scene, updatable?: boolean): Mesh {
             var box = new BABYLON.Mesh(name, scene);

+ 572 - 0
Babylon/Mesh/babylon.meshSimplification.js

@@ -0,0 +1,572 @@
+var BABYLON;
+(function (BABYLON) {
+    
+
+    
+
+    var SimplificationSettings = (function () {
+        function SimplificationSettings(quality, distance) {
+            this.quality = quality;
+            this.distance = distance;
+        }
+        return SimplificationSettings;
+    })();
+    BABYLON.SimplificationSettings = SimplificationSettings;
+
+    /**
+    * The implemented types of simplification.
+    * At the moment only Quadratic Error Decimation is implemented.
+    */
+    (function (SimplificationType) {
+        SimplificationType[SimplificationType["QUADRATIC"] = 0] = "QUADRATIC";
+    })(BABYLON.SimplificationType || (BABYLON.SimplificationType = {}));
+    var SimplificationType = BABYLON.SimplificationType;
+
+    var DecimationTriangle = (function () {
+        function DecimationTriangle(vertices) {
+            this.vertices = vertices;
+            this.error = new Array(4);
+            this.deleted = false;
+            this.isDirty = false;
+            this.borderFactor = 0;
+        }
+        return DecimationTriangle;
+    })();
+    BABYLON.DecimationTriangle = DecimationTriangle;
+
+    var DecimationVertex = (function () {
+        function DecimationVertex(position, normal, uv, id) {
+            this.position = position;
+            this.normal = normal;
+            this.uv = uv;
+            this.id = id;
+            this.isBorder = true;
+            this.q = new QuadraticMatrix();
+            this.triangleCount = 0;
+            this.triangleStart = 0;
+        }
+        return DecimationVertex;
+    })();
+    BABYLON.DecimationVertex = DecimationVertex;
+
+    var QuadraticMatrix = (function () {
+        function QuadraticMatrix(data) {
+            this.data = new Array(10);
+            for (var i = 0; i < 10; ++i) {
+                if (data && data[i]) {
+                    this.data[i] = data[i];
+                } else {
+                    this.data[i] = 0;
+                }
+            }
+        }
+        QuadraticMatrix.prototype.det = function (a11, a12, a13, a21, a22, a23, a31, a32, a33) {
+            var det = this.data[a11] * this.data[a22] * this.data[a33] + this.data[a13] * this.data[a21] * this.data[a32] + this.data[a12] * this.data[a23] * this.data[a31] - this.data[a13] * this.data[a22] * this.data[a31] - this.data[a11] * this.data[a23] * this.data[a32] - this.data[a12] * this.data[a21] * this.data[a33];
+            return det;
+        };
+
+        QuadraticMatrix.prototype.addInPlace = function (matrix) {
+            for (var i = 0; i < 10; ++i) {
+                this.data[i] += matrix.data[i];
+            }
+        };
+
+        QuadraticMatrix.prototype.addArrayInPlace = function (data) {
+            for (var i = 0; i < 10; ++i) {
+                this.data[i] += data[i];
+            }
+        };
+
+        QuadraticMatrix.prototype.add = function (matrix) {
+            var m = new QuadraticMatrix();
+            for (var i = 0; i < 10; ++i) {
+                m.data[i] = this.data[i] + matrix.data[i];
+            }
+            return m;
+        };
+
+        QuadraticMatrix.FromData = function (a, b, c, d) {
+            return new QuadraticMatrix(QuadraticMatrix.DataFromNumbers(a, b, c, d));
+        };
+
+        //returning an array to avoid garbage collection
+        QuadraticMatrix.DataFromNumbers = function (a, b, c, d) {
+            return [a * a, a * b, a * c, a * d, b * b, b * c, b * d, c * c, c * d, d * d];
+        };
+        return QuadraticMatrix;
+    })();
+    BABYLON.QuadraticMatrix = QuadraticMatrix;
+
+    var Reference = (function () {
+        function Reference(vertexId, triangleId) {
+            this.vertexId = vertexId;
+            this.triangleId = triangleId;
+        }
+        return Reference;
+    })();
+    BABYLON.Reference = Reference;
+
+    /**
+    * An implementation of the Quadratic Error simplification algorithm.
+    * Original paper : http://www1.cs.columbia.edu/~cs4162/html05s/garland97.pdf
+    * Ported mostly from QSlim and http://voxels.blogspot.de/2014/05/quadric-mesh-simplification-with-source.html to babylon JS
+    * @author RaananW
+    */
+    var QuadraticErrorSimplification = (function () {
+        function QuadraticErrorSimplification(_mesh) {
+            this._mesh = _mesh;
+            this.initialised = false;
+            this.syncIterations = 5000;
+            this.aggressiveness = 7;
+            this.decimationIterations = 100;
+        }
+        QuadraticErrorSimplification.prototype.simplify = function (settings, successCallback) {
+            var _this = this;
+            this.initWithMesh(this._mesh, function () {
+                _this.runDecimation(settings, successCallback);
+            });
+        };
+
+        QuadraticErrorSimplification.prototype.runDecimation = function (settings, successCallback) {
+            var _this = this;
+            var targetCount = ~~(this.triangles.length * settings.quality);
+            var deletedTriangles = 0;
+
+            var triangleCount = this.triangles.length;
+
+            var iterationFunction = function (iteration, callback) {
+                setTimeout(function () {
+                    if (iteration % 5 === 0) {
+                        _this.updateMesh(iteration === 0);
+                    }
+
+                    for (var i = 0; i < _this.triangles.length; ++i) {
+                        _this.triangles[i].isDirty = false;
+                    }
+
+                    var threshold = 0.000000001 * Math.pow((iteration + 3), _this.aggressiveness);
+
+                    var trianglesIterator = function (i) {
+                        var t = _this.triangles[i];
+                        if (!t)
+                            return;
+                        if (t.error[3] > threshold) {
+                            return;
+                        }
+                        if (t.deleted) {
+                            return;
+                        }
+                        if (t.isDirty) {
+                            return;
+                        }
+                        for (var j = 0; j < 3; ++j) {
+                            if (t.error[j] < threshold) {
+                                var deleted0 = [];
+                                var deleted1 = [];
+
+                                var i0 = t.vertices[j];
+                                var i1 = t.vertices[(j + 1) % 3];
+                                var v0 = _this.vertices[i0];
+                                var v1 = _this.vertices[i1];
+
+                                if (v0.isBorder !== v1.isBorder)
+                                    continue;
+
+                                var p = BABYLON.Vector3.Zero();
+                                var n = BABYLON.Vector3.Zero();
+                                var uv = BABYLON.Vector2.Zero();
+
+                                _this.calculateError(v0, v1, p, n, uv);
+
+                                if (_this.isFlipped(v0, i1, p, deleted0, t.borderFactor))
+                                    continue;
+                                if (_this.isFlipped(v1, i0, p, deleted1, t.borderFactor))
+                                    continue;
+
+                                v0.position = p;
+                                v0.normal = n;
+                                v0.uv = uv;
+                                v0.q = v1.q.add(v0.q);
+                                var tStart = _this.references.length;
+
+                                deletedTriangles = _this.updateTriangles(v0.id, v0, deleted0, deletedTriangles);
+                                deletedTriangles = _this.updateTriangles(v0.id, v1, deleted1, deletedTriangles);
+
+                                var tCount = _this.references.length - tStart;
+
+                                if (tCount <= v0.triangleCount) {
+                                    if (tCount) {
+                                        for (var c = 0; c < tCount; c++) {
+                                            _this.references[v0.triangleStart + c] = _this.references[tStart + c];
+                                        }
+                                    }
+                                } else {
+                                    v0.triangleStart = tStart;
+                                }
+
+                                v0.triangleCount = tCount;
+                                break;
+                            }
+                        }
+                    };
+                    BABYLON.AsyncLoop.SyncAsyncForLoop(_this.triangles.length, _this.syncIterations, trianglesIterator, callback, function () {
+                        return (triangleCount - deletedTriangles <= targetCount);
+                    });
+                }, 0);
+            };
+
+            BABYLON.AsyncLoop.Run(this.decimationIterations, function (loop) {
+                if (triangleCount - deletedTriangles <= targetCount)
+                    loop.breakLoop();
+                else {
+                    iterationFunction(loop.index, function () {
+                        loop.executeNext();
+                    });
+                }
+            }, function () {
+                setTimeout(function () {
+                    successCallback(_this.reconstructMesh());
+                }, 0);
+            });
+        };
+
+        QuadraticErrorSimplification.prototype.initWithMesh = function (mesh, callback) {
+            var _this = this;
+            if (!mesh)
+                return;
+
+            this.vertices = [];
+            this.triangles = [];
+
+            this._mesh = mesh;
+            var positionData = this._mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
+            var normalData = this._mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
+            var uvs = this._mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
+            var indices = mesh.getIndices();
+
+            var vertexInit = function (i) {
+                var vertex = new DecimationVertex(BABYLON.Vector3.FromArray(positionData, i * 3), BABYLON.Vector3.FromArray(normalData, i * 3), BABYLON.Vector2.FromArray(uvs, i * 2), i);
+                _this.vertices.push(vertex);
+            };
+            var totalVertices = mesh.getTotalVertices();
+            BABYLON.AsyncLoop.SyncAsyncForLoop(totalVertices, this.syncIterations, vertexInit, function () {
+                var indicesInit = function (i) {
+                    var pos = i * 3;
+                    var i0 = indices[pos + 0];
+                    var i1 = indices[pos + 1];
+                    var i2 = indices[pos + 2];
+                    var triangle = new DecimationTriangle([_this.vertices[i0].id, _this.vertices[i1].id, _this.vertices[i2].id]);
+                    _this.triangles.push(triangle);
+                };
+                BABYLON.AsyncLoop.SyncAsyncForLoop(indices.length / 3, _this.syncIterations, indicesInit, function () {
+                    _this.init(callback);
+                });
+            });
+        };
+
+        QuadraticErrorSimplification.prototype.init = function (callback) {
+            var _this = this;
+            var triangleInit1 = function (i) {
+                var t = _this.triangles[i];
+                t.normal = BABYLON.Vector3.Cross(_this.vertices[t.vertices[1]].position.subtract(_this.vertices[t.vertices[0]].position), _this.vertices[t.vertices[2]].position.subtract(_this.vertices[t.vertices[0]].position)).normalize();
+                for (var j = 0; j < 3; j++) {
+                    _this.vertices[t.vertices[j]].q.addArrayInPlace(QuadraticMatrix.DataFromNumbers(t.normal.x, t.normal.y, t.normal.z, -(BABYLON.Vector3.Dot(t.normal, _this.vertices[t.vertices[0]].position))));
+                }
+            };
+            BABYLON.AsyncLoop.SyncAsyncForLoop(this.triangles.length, this.syncIterations, triangleInit1, function () {
+                var triangleInit2 = function (i) {
+                    var t = _this.triangles[i];
+                    for (var j = 0; j < 3; ++j) {
+                        t.error[j] = _this.calculateError(_this.vertices[t.vertices[j]], _this.vertices[t.vertices[(j + 1) % 3]]);
+                    }
+                    t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
+                };
+                BABYLON.AsyncLoop.SyncAsyncForLoop(_this.triangles.length, _this.syncIterations, triangleInit2, function () {
+                    _this.initialised = true;
+                    callback();
+                });
+            });
+        };
+
+        QuadraticErrorSimplification.prototype.reconstructMesh = function () {
+            var newTriangles = [];
+            var i;
+            for (i = 0; i < this.vertices.length; ++i) {
+                this.vertices[i].triangleCount = 0;
+            }
+            var t;
+            var j;
+            for (i = 0; i < this.triangles.length; ++i) {
+                if (!this.triangles[i].deleted) {
+                    t = this.triangles[i];
+                    for (j = 0; j < 3; ++j) {
+                        this.vertices[t.vertices[j]].triangleCount = 1;
+                    }
+                    newTriangles.push(t);
+                }
+            }
+
+            var newVerticesOrder = [];
+
+            //compact vertices, get the IDs of the vertices used.
+            var dst = 0;
+            for (i = 0; i < this.vertices.length; ++i) {
+                if (this.vertices[i].triangleCount) {
+                    this.vertices[i].triangleStart = dst;
+                    this.vertices[dst].position = this.vertices[i].position;
+                    this.vertices[dst].normal = this.vertices[i].normal;
+                    this.vertices[dst].uv = this.vertices[i].uv;
+                    newVerticesOrder.push(i);
+                    dst++;
+                }
+            }
+
+            for (i = 0; i < newTriangles.length; ++i) {
+                t = newTriangles[i];
+                for (j = 0; j < 3; ++j) {
+                    t.vertices[j] = this.vertices[t.vertices[j]].triangleStart;
+                }
+            }
+            this.vertices = this.vertices.slice(0, dst);
+
+            var newPositionData = [];
+            var newNormalData = [];
+            var newUVsData = [];
+
+            for (i = 0; i < newVerticesOrder.length; ++i) {
+                newPositionData.push(this.vertices[i].position.x);
+                newPositionData.push(this.vertices[i].position.y);
+                newPositionData.push(this.vertices[i].position.z);
+                newNormalData.push(this.vertices[i].normal.x);
+                newNormalData.push(this.vertices[i].normal.y);
+                newNormalData.push(this.vertices[i].normal.z);
+                newUVsData.push(this.vertices[i].uv.x);
+                newUVsData.push(this.vertices[i].uv.y);
+            }
+
+            var newIndicesArray = [];
+            for (i = 0; i < newTriangles.length; ++i) {
+                newIndicesArray.push(newTriangles[i].vertices[0]);
+                newIndicesArray.push(newTriangles[i].vertices[1]);
+                newIndicesArray.push(newTriangles[i].vertices[2]);
+            }
+
+            var newMesh = new BABYLON.Mesh(this._mesh + "Decimated", this._mesh.getScene());
+            newMesh.material = this._mesh.material;
+            newMesh.parent = this._mesh.parent;
+            newMesh.setIndices(newIndicesArray);
+            newMesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, newPositionData);
+            newMesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, newNormalData);
+            newMesh.setVerticesData(BABYLON.VertexBuffer.UVKind, newUVsData);
+
+            //preparing the skeleton support
+            if (this._mesh.skeleton) {
+                //newMesh.skeleton = this._mesh.skeleton.clone("", "");
+                //newMesh.getScene().beginAnimation(newMesh.skeleton, 0, 100, true, 1.0);
+            }
+
+            return newMesh;
+        };
+
+        QuadraticErrorSimplification.prototype.isFlipped = function (vertex1, index2, point, deletedArray, borderFactor) {
+            for (var i = 0; i < vertex1.triangleCount; ++i) {
+                var t = this.triangles[this.references[vertex1.triangleStart + i].triangleId];
+                if (t.deleted)
+                    continue;
+
+                var s = this.references[vertex1.triangleStart + i].vertexId;
+
+                var id1 = t.vertices[(s + 1) % 3];
+                var id2 = t.vertices[(s + 2) % 3];
+
+                if ((id1 === index2 || id2 === index2) && borderFactor < 2) {
+                    deletedArray[i] = true;
+                    continue;
+                }
+
+                var d1 = this.vertices[id1].position.subtract(point);
+                d1 = d1.normalize();
+                var d2 = this.vertices[id2].position.subtract(point);
+                d2 = d2.normalize();
+                if (Math.abs(BABYLON.Vector3.Dot(d1, d2)) > 0.999)
+                    return true;
+                var normal = BABYLON.Vector3.Cross(d1, d2).normalize();
+                deletedArray[i] = false;
+                if (BABYLON.Vector3.Dot(normal, t.normal) < 0.2)
+                    return true;
+            }
+
+            return false;
+        };
+
+        QuadraticErrorSimplification.prototype.updateTriangles = function (vertexId, vertex, deletedArray, deletedTriangles) {
+            var newDeleted = deletedTriangles;
+            for (var i = 0; i < vertex.triangleCount; ++i) {
+                var ref = this.references[vertex.triangleStart + i];
+                var t = this.triangles[ref.triangleId];
+                if (t.deleted)
+                    continue;
+                if (deletedArray[i]) {
+                    t.deleted = true;
+                    newDeleted++;
+                    continue;
+                }
+                t.vertices[ref.vertexId] = vertexId;
+                t.isDirty = true;
+                t.error[0] = this.calculateError(this.vertices[t.vertices[0]], this.vertices[t.vertices[1]]) + (t.borderFactor / 2);
+                t.error[1] = this.calculateError(this.vertices[t.vertices[1]], this.vertices[t.vertices[2]]) + (t.borderFactor / 2);
+                t.error[2] = this.calculateError(this.vertices[t.vertices[2]], this.vertices[t.vertices[0]]) + (t.borderFactor / 2);
+                t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
+                this.references.push(ref);
+            }
+            return newDeleted;
+        };
+
+        QuadraticErrorSimplification.prototype.identifyBorder = function () {
+            for (var i = 0; i < this.vertices.length; ++i) {
+                var vCount = [];
+                var vId = [];
+                var v = this.vertices[i];
+                var j;
+                for (j = 0; j < v.triangleCount; ++j) {
+                    var triangle = this.triangles[this.references[v.triangleStart + j].triangleId];
+                    for (var ii = 0; ii < 3; ii++) {
+                        var ofs = 0;
+                        var id = triangle.vertices[ii];
+                        while (ofs < vCount.length) {
+                            if (vId[ofs] === id)
+                                break;
+                            ++ofs;
+                        }
+                        if (ofs === vCount.length) {
+                            vCount.push(1);
+                            vId.push(id);
+                        } else {
+                            vCount[ofs]++;
+                        }
+                    }
+                }
+
+                for (j = 0; j < vCount.length; ++j) {
+                    if (vCount[j] === 1) {
+                        this.vertices[vId[j]].isBorder = true;
+                    } else {
+                        this.vertices[vId[j]].isBorder = false;
+                    }
+                }
+            }
+        };
+
+        QuadraticErrorSimplification.prototype.updateMesh = function (identifyBorders) {
+            if (typeof identifyBorders === "undefined") { identifyBorders = false; }
+            var i;
+            if (!identifyBorders) {
+                var newTrianglesVector = [];
+                for (i = 0; i < this.triangles.length; ++i) {
+                    if (!this.triangles[i].deleted) {
+                        newTrianglesVector.push(this.triangles[i]);
+                    }
+                }
+                this.triangles = newTrianglesVector;
+            }
+
+            for (i = 0; i < this.vertices.length; ++i) {
+                this.vertices[i].triangleCount = 0;
+                this.vertices[i].triangleStart = 0;
+            }
+            var t;
+            var j;
+            var v;
+            for (i = 0; i < this.triangles.length; ++i) {
+                t = this.triangles[i];
+                for (j = 0; j < 3; ++j) {
+                    v = this.vertices[t.vertices[j]];
+                    v.triangleCount++;
+                }
+            }
+
+            var tStart = 0;
+
+            for (i = 0; i < this.vertices.length; ++i) {
+                this.vertices[i].triangleStart = tStart;
+                tStart += this.vertices[i].triangleCount;
+                this.vertices[i].triangleCount = 0;
+            }
+
+            var newReferences = new Array(this.triangles.length * 3);
+            for (i = 0; i < this.triangles.length; ++i) {
+                t = this.triangles[i];
+                for (j = 0; j < 3; ++j) {
+                    v = this.vertices[t.vertices[j]];
+                    newReferences[v.triangleStart + v.triangleCount] = new Reference(j, i);
+                    v.triangleCount++;
+                }
+            }
+            this.references = newReferences;
+
+            if (identifyBorders) {
+                this.identifyBorder();
+            }
+        };
+
+        QuadraticErrorSimplification.prototype.vertexError = function (q, point) {
+            var x = point.x;
+            var y = point.y;
+            var z = point.z;
+            return q.data[0] * x * x + 2 * q.data[1] * x * y + 2 * q.data[2] * x * z + 2 * q.data[3] * x + q.data[4] * y * y + 2 * q.data[5] * y * z + 2 * q.data[6] * y + q.data[7] * z * z + 2 * q.data[8] * z + q.data[9];
+        };
+
+        QuadraticErrorSimplification.prototype.calculateError = function (vertex1, vertex2, pointResult, normalResult, uvResult) {
+            var q = vertex1.q.add(vertex2.q);
+            var border = vertex1.isBorder && vertex2.isBorder;
+            var error = 0;
+            var qDet = q.det(0, 1, 2, 1, 4, 5, 2, 5, 7);
+
+            if (qDet !== 0 && !border) {
+                if (!pointResult) {
+                    pointResult = BABYLON.Vector3.Zero();
+                }
+                pointResult.x = -1 / qDet * (q.det(1, 2, 3, 4, 5, 6, 5, 7, 8));
+                pointResult.y = 1 / qDet * (q.det(0, 2, 3, 1, 5, 6, 2, 7, 8));
+                pointResult.z = -1 / qDet * (q.det(0, 1, 3, 1, 4, 6, 2, 5, 8));
+                error = this.vertexError(q, pointResult);
+
+                //TODO this should be correctly calculated
+                if (normalResult) {
+                    normalResult.copyFrom(vertex1.normal);
+                    uvResult.copyFrom(vertex1.uv);
+                }
+            } else {
+                var p3 = (vertex1.position.add(vertex2.position)).divide(new BABYLON.Vector3(2, 2, 2));
+                var norm3 = (vertex1.normal.add(vertex2.normal)).divide(new BABYLON.Vector3(2, 2, 2)).normalize();
+                var error1 = this.vertexError(q, vertex1.position);
+                var error2 = this.vertexError(q, vertex2.position);
+                var error3 = this.vertexError(q, p3);
+                error = Math.min(error1, error2, error3);
+                if (error === error1) {
+                    if (pointResult) {
+                        pointResult.copyFrom(vertex1.position);
+                        normalResult.copyFrom(vertex1.normal);
+                        uvResult.copyFrom(vertex1.uv);
+                    }
+                } else if (error === error2) {
+                    if (pointResult) {
+                        pointResult.copyFrom(vertex2.position);
+                        normalResult.copyFrom(vertex2.normal);
+                        uvResult.copyFrom(vertex2.uv);
+                    }
+                } else {
+                    if (pointResult) {
+                        pointResult.copyFrom(p3);
+                        normalResult.copyFrom(norm3);
+                        uvResult.copyFrom(vertex1.uv);
+                    }
+                }
+            }
+            return error;
+        };
+        return QuadraticErrorSimplification;
+    })();
+    BABYLON.QuadraticErrorSimplification = QuadraticErrorSimplification;
+})(BABYLON || (BABYLON = {}));
+//# sourceMappingURL=babylon.meshSimplification.js.map

+ 577 - 0
Babylon/Mesh/babylon.meshSimplification.ts

@@ -0,0 +1,577 @@
+module BABYLON {
+
+    /**
+     * A simplifier interface for future simplification implementations.
+     */
+    export interface ISimplifier {
+        /**
+         * Simplification of a given mesh according to the given settings.
+         * Since this requires computation, it is assumed that the function runs async.
+         * @param settings The settings of the simplification, including quality and distance
+         * @param successCallback A callback that will be called after the mesh was simplified.
+         * @param errorCallback in case of an error, this callback will be called. optional.
+         */
+        simplify(settings: ISimplificationSettings, successCallback: (simplifiedMeshes: Mesh) => void, errorCallback?: () => void):void ;
+    }
+
+
+    /**
+     * Expected simplification settings.
+     * Quality should be between 0 and 1 (1 being 100%, 0 being 0%);
+     */
+    export interface ISimplificationSettings {
+        quality: number;
+        distance: number;
+    }
+
+    export class SimplificationSettings implements ISimplificationSettings {
+        constructor(public quality: number, public distance: number) {
+        }
+    }
+
+    /**
+     * The implemented types of simplification.
+     * At the moment only Quadratic Error Decimation is implemented.
+     */
+    export enum SimplificationType {
+        QUADRATIC
+    }
+
+    export class DecimationTriangle {
+        public normal: Vector3;
+        public error: Array<number>;
+        public deleted: boolean;
+        public isDirty: boolean;
+        public borderFactor: number;
+
+        constructor(public vertices: Array<number>) {
+            this.error = new Array<number>(4);
+            this.deleted = false;
+            this.isDirty = false;
+            this.borderFactor = 0;
+        }
+    }
+
+    export class DecimationVertex {
+        public q: QuadraticMatrix;
+        public isBorder: boolean;
+
+        public triangleStart: number;
+        public triangleCount: number;
+
+        constructor(public position: Vector3, public normal: Vector3, public uv: Vector2, public id) {
+            this.isBorder = true;
+            this.q = new QuadraticMatrix();
+            this.triangleCount = 0;
+            this.triangleStart = 0;
+        }
+    }
+
+    export class QuadraticMatrix {
+        public data: Array<number>;
+
+        constructor(data?: Array<number>) {
+            this.data = new Array(10);
+            for (var i = 0; i < 10; ++i) {
+                if (data && data[i]) {
+                    this.data[i] = data[i];
+                } else {
+                    this.data[i] = 0;
+                }
+            }
+        }
+
+        public det(a11, a12, a13, a21, a22, a23, a31, a32, a33) {
+            var det = this.data[a11] * this.data[a22] * this.data[a33] + this.data[a13] * this.data[a21] * this.data[a32] +
+                this.data[a12] * this.data[a23] * this.data[a31] - this.data[a13] * this.data[a22] * this.data[a31] -
+                this.data[a11] * this.data[a23] * this.data[a32] - this.data[a12] * this.data[a21] * this.data[a33];
+            return det;
+        }
+
+        public addInPlace(matrix: QuadraticMatrix) {
+            for (var i = 0; i < 10; ++i) {
+                this.data[i] += matrix.data[i];
+            }
+        }
+
+        public addArrayInPlace(data: Array<number>) {
+            for (var i = 0; i < 10; ++i) {
+                this.data[i] += data[i];
+            }
+        }
+
+        public add(matrix: QuadraticMatrix): QuadraticMatrix {
+            var m = new QuadraticMatrix();
+            for (var i = 0; i < 10; ++i) {
+                m.data[i] = this.data[i] + matrix.data[i];
+            }
+            return m;
+        }
+
+        public static FromData(a: number, b: number, c: number, d: number): QuadraticMatrix {
+            return new QuadraticMatrix(QuadraticMatrix.DataFromNumbers(a, b, c, d));
+        }
+
+        //returning an array to avoid garbage collection
+        public static DataFromNumbers(a: number, b: number, c: number, d: number) {
+            return [a * a, a * b, a * c, a * d, b * b, b * c, b * d, c * c, c * d, d * d];
+        }
+    }
+
+    export class Reference {
+        constructor(public vertexId: number, public triangleId: number) { }
+    }
+
+    /**
+     * An implementation of the Quadratic Error simplification algorithm.
+     * Original paper : http://www1.cs.columbia.edu/~cs4162/html05s/garland97.pdf
+     * Ported mostly from QSlim and http://voxels.blogspot.de/2014/05/quadric-mesh-simplification-with-source.html to babylon JS
+     * @author RaananW
+     */
+    export class QuadraticErrorSimplification implements ISimplifier {
+
+        private triangles: Array<DecimationTriangle>;
+        private vertices: Array<DecimationVertex>;
+        private references: Array<Reference>;
+
+        private initialised: boolean = false;
+        
+        public syncIterations = 5000;
+
+        public aggressiveness : number;
+        public decimationIterations: number;
+        
+        constructor(private _mesh: Mesh) {
+            this.aggressiveness  = 7;
+            this.decimationIterations = 100;
+        }
+
+        public simplify(settings: ISimplificationSettings, successCallback: (simplifiedMeshes: Mesh) => void) {
+            this.initWithMesh(this._mesh, () => {
+                this.runDecimation(settings, successCallback);
+            });
+        }
+
+        private runDecimation(settings: ISimplificationSettings, successCallback: (simplifiedMeshes: Mesh) => void) {
+            var targetCount = ~~(this.triangles.length * settings.quality);
+            var deletedTriangles = 0;
+
+            var triangleCount = this.triangles.length;
+
+            var iterationFunction = (iteration: number, callback) => {
+                setTimeout(() => {
+                    if (iteration % 5 === 0) {
+                        this.updateMesh(iteration === 0);
+                    }
+
+                    for (var i = 0; i < this.triangles.length; ++i) {
+                        this.triangles[i].isDirty = false;
+                    }
+
+                    var threshold = 0.000000001 * Math.pow((iteration + 3), this.aggressiveness);
+
+                    var trianglesIterator = (i) => {
+                        var t = this.triangles[i];
+                        if (!t) return;
+                        if (t.error[3] > threshold) { return }
+                        if (t.deleted) { return }
+                        if (t.isDirty) {  return }
+                        for (var j = 0; j < 3; ++j) {
+                            if (t.error[j] < threshold) {
+                                var deleted0: Array<boolean> = [];
+                                var deleted1: Array<boolean> = [];
+
+                                var i0 = t.vertices[j];
+                                var i1 = t.vertices[(j + 1) % 3];
+                                var v0 = this.vertices[i0];
+                                var v1 = this.vertices[i1];
+
+                                if (v0.isBorder !== v1.isBorder) continue;
+
+                                var p = Vector3.Zero();
+                                var n = Vector3.Zero();
+                                var uv = Vector2.Zero();
+
+                                this.calculateError(v0, v1, p, n, uv);
+
+                                if (this.isFlipped(v0, i1, p, deleted0, t.borderFactor)) continue;
+                                if (this.isFlipped(v1, i0, p, deleted1, t.borderFactor)) continue;
+
+                                v0.position = p;
+                                v0.normal = n;
+                                v0.uv = uv;
+                                v0.q = v1.q.add(v0.q);
+                                var tStart = this.references.length;
+
+                                deletedTriangles = this.updateTriangles(v0.id, v0, deleted0, deletedTriangles);
+                                deletedTriangles = this.updateTriangles(v0.id, v1, deleted1, deletedTriangles);
+
+                                var tCount = this.references.length - tStart;
+
+                                if (tCount <= v0.triangleCount) {
+                                    if (tCount) {
+                                        for (var c = 0; c < tCount; c++) {
+                                            this.references[v0.triangleStart + c] = this.references[tStart + c];
+                                        }
+                                    }
+                                } else {
+                                    v0.triangleStart = tStart;
+                                }
+
+                                v0.triangleCount = tCount;
+                                break;
+                            }
+                        }
+                    };
+                    AsyncLoop.SyncAsyncForLoop(this.triangles.length, this.syncIterations, trianglesIterator, callback, () => { return (triangleCount - deletedTriangles <= targetCount) });
+                }, 0);
+            };
+
+            AsyncLoop.Run(this.decimationIterations, (loop: AsyncLoop) => {
+                if (triangleCount - deletedTriangles <= targetCount) loop.breakLoop();
+                else {
+                    iterationFunction(loop.index, () => {
+                        loop.executeNext();
+                    });
+                }
+            }, () => {
+                    setTimeout(() => {
+                        successCallback(this.reconstructMesh());
+                    }, 0);
+                });
+        }
+
+        private initWithMesh(mesh: Mesh, callback: Function) {
+            if (!mesh) return;
+
+            this.vertices = [];
+            this.triangles = [];
+
+            this._mesh = mesh;
+            var positionData = this._mesh.getVerticesData(VertexBuffer.PositionKind);
+            var normalData = this._mesh.getVerticesData(VertexBuffer.NormalKind);
+            var uvs = this._mesh.getVerticesData(VertexBuffer.UVKind);
+            var indices = mesh.getIndices();
+
+            var vertexInit = (i) => {
+                var vertex = new DecimationVertex(Vector3.FromArray(positionData, i * 3), Vector3.FromArray(normalData, i * 3), Vector2.FromArray(uvs, i * 2), i);
+                this.vertices.push(vertex);
+            };
+            var totalVertices = mesh.getTotalVertices();
+            AsyncLoop.SyncAsyncForLoop(totalVertices, this.syncIterations, vertexInit, () => {
+
+                var indicesInit = (i) => {
+                    var pos = i * 3;
+                    var i0 = indices[pos + 0];
+                    var i1 = indices[pos + 1];
+                    var i2 = indices[pos + 2];
+                    var triangle = new DecimationTriangle([this.vertices[i0].id, this.vertices[i1].id, this.vertices[i2].id]);
+                    this.triangles.push(triangle);
+                };
+                AsyncLoop.SyncAsyncForLoop(indices.length / 3, this.syncIterations, indicesInit, () => {
+                    this.init(callback);
+                });
+            });
+        }
+
+        private init(callback: Function) {
+            var triangleInit1 = (i) => {
+                var t = this.triangles[i];
+                t.normal = Vector3.Cross(this.vertices[t.vertices[1]].position.subtract(this.vertices[t.vertices[0]].position), this.vertices[t.vertices[2]].position.subtract(this.vertices[t.vertices[0]].position)).normalize();
+                for (var j = 0; j < 3; j++) {
+                    this.vertices[t.vertices[j]].q.addArrayInPlace(QuadraticMatrix.DataFromNumbers(t.normal.x, t.normal.y, t.normal.z, -(Vector3.Dot(t.normal, this.vertices[t.vertices[0]].position))));
+                }
+            };
+            AsyncLoop.SyncAsyncForLoop(this.triangles.length, this.syncIterations, triangleInit1, () => {
+
+                var triangleInit2 = (i) => {
+                    var t = this.triangles[i];
+                    for (var j = 0; j < 3; ++j) {
+                        t.error[j] = this.calculateError(this.vertices[t.vertices[j]], this.vertices[t.vertices[(j + 1) % 3]]);
+                    }
+                    t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
+                };
+                AsyncLoop.SyncAsyncForLoop(this.triangles.length, this.syncIterations, triangleInit2, () => {
+                    this.initialised = true;
+                    callback();
+                });
+            });
+        }
+        
+        private reconstructMesh(): Mesh {
+
+            var newTriangles: Array<DecimationTriangle> = [];
+            var i: number;
+            for (i = 0; i < this.vertices.length; ++i) {
+                this.vertices[i].triangleCount = 0;
+            }
+            var t: DecimationTriangle;
+            var j: number;
+            for (i = 0; i < this.triangles.length; ++i) {
+                if (!this.triangles[i].deleted) {
+                    t = this.triangles[i];
+                    for (j = 0; j < 3; ++j) {
+                        this.vertices[t.vertices[j]].triangleCount = 1;
+                    }
+                    newTriangles.push(t);
+                }
+            }
+
+            var newVerticesOrder = [];
+
+            //compact vertices, get the IDs of the vertices used.
+            var dst = 0;
+            for (i = 0; i < this.vertices.length; ++i) {
+                if (this.vertices[i].triangleCount) {
+                    this.vertices[i].triangleStart = dst;
+                    this.vertices[dst].position = this.vertices[i].position;
+                    this.vertices[dst].normal = this.vertices[i].normal;
+                    this.vertices[dst].uv = this.vertices[i].uv;
+                    newVerticesOrder.push(i);
+                    dst++;
+                }
+            }
+
+            for (i = 0; i < newTriangles.length; ++i) {
+                t = newTriangles[i];
+                for (j = 0; j < 3; ++j) {
+                    t.vertices[j] = this.vertices[t.vertices[j]].triangleStart;
+                }
+            }
+            this.vertices = this.vertices.slice(0, dst);
+
+            var newPositionData = [];
+            var newNormalData = [];
+            var newUVsData = [];
+
+            for (i = 0; i < newVerticesOrder.length; ++i) {
+                newPositionData.push(this.vertices[i].position.x);
+                newPositionData.push(this.vertices[i].position.y);
+                newPositionData.push(this.vertices[i].position.z);
+                newNormalData.push(this.vertices[i].normal.x);
+                newNormalData.push(this.vertices[i].normal.y);
+                newNormalData.push(this.vertices[i].normal.z);
+                newUVsData.push(this.vertices[i].uv.x);
+                newUVsData.push(this.vertices[i].uv.y);
+            }
+
+            var newIndicesArray: Array<number> = [];
+            for (i = 0; i < newTriangles.length; ++i) {
+                newIndicesArray.push(newTriangles[i].vertices[0]);
+                newIndicesArray.push(newTriangles[i].vertices[1]);
+                newIndicesArray.push(newTriangles[i].vertices[2]);
+            }
+
+            var newMesh = new Mesh(this._mesh + "Decimated", this._mesh.getScene());
+            newMesh.material = this._mesh.material;
+            newMesh.parent = this._mesh.parent;
+            newMesh.setIndices(newIndicesArray);
+            newMesh.setVerticesData(VertexBuffer.PositionKind, newPositionData);
+            newMesh.setVerticesData(VertexBuffer.NormalKind, newNormalData);
+            newMesh.setVerticesData(VertexBuffer.UVKind, newUVsData);
+            //preparing the skeleton support
+            if (this._mesh.skeleton) {
+                //newMesh.skeleton = this._mesh.skeleton.clone("", "");
+                //newMesh.getScene().beginAnimation(newMesh.skeleton, 0, 100, true, 1.0);
+            }
+
+            return newMesh;
+        }
+
+        private isFlipped(vertex1: DecimationVertex, index2: number, point: Vector3, deletedArray: Array<boolean>, borderFactor: number): boolean {
+
+            for (var i = 0; i < vertex1.triangleCount; ++i) {
+                var t = this.triangles[this.references[vertex1.triangleStart + i].triangleId];
+                if (t.deleted) continue;
+
+                var s = this.references[vertex1.triangleStart + i].vertexId;
+
+                var id1 = t.vertices[(s + 1) % 3];
+                var id2 = t.vertices[(s + 2) % 3];
+
+                if ((id1 === index2 || id2 === index2) && borderFactor < 2) {
+                    deletedArray[i] = true;
+                    continue;
+                }
+
+                var d1 = this.vertices[id1].position.subtract(point);
+                d1 = d1.normalize();
+                var d2 = this.vertices[id2].position.subtract(point);
+                d2 = d2.normalize();
+                if (Math.abs(Vector3.Dot(d1, d2)) > 0.999) return true;
+                var normal = Vector3.Cross(d1, d2).normalize();
+                deletedArray[i] = false;
+                if (Vector3.Dot(normal, t.normal) < 0.2) return true;
+            }
+
+            return false;
+        }
+
+        private updateTriangles(vertexId: number, vertex: DecimationVertex, deletedArray: Array<boolean>, deletedTriangles: number): number {
+            var newDeleted = deletedTriangles;
+            for (var i = 0; i < vertex.triangleCount; ++i) {
+                var ref = this.references[vertex.triangleStart + i];
+                var t = this.triangles[ref.triangleId];
+                if (t.deleted) continue;
+                if (deletedArray[i]) {
+                    t.deleted = true;
+                    newDeleted++;
+                    continue;
+                }
+                t.vertices[ref.vertexId] = vertexId;
+                t.isDirty = true;
+                t.error[0] = this.calculateError(this.vertices[t.vertices[0]], this.vertices[t.vertices[1]]) + (t.borderFactor / 2);
+                t.error[1] = this.calculateError(this.vertices[t.vertices[1]], this.vertices[t.vertices[2]]) + (t.borderFactor / 2);
+                t.error[2] = this.calculateError(this.vertices[t.vertices[2]], this.vertices[t.vertices[0]]) + (t.borderFactor / 2);
+                t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
+                this.references.push(ref);
+            }
+            return newDeleted;
+        }
+
+        private identifyBorder() {
+            
+            for (var i = 0; i < this.vertices.length; ++i) {
+                var vCount: Array<number> = [];
+                var vId: Array<number> = [];
+                var v = this.vertices[i];
+                var j: number;
+                for (j = 0; j < v.triangleCount; ++j) {
+                    var triangle = this.triangles[this.references[v.triangleStart + j].triangleId];
+                    for (var ii = 0; ii < 3; ii++) {
+                        var ofs = 0;
+                        var id = triangle.vertices[ii];
+                        while (ofs < vCount.length) {
+                            if (vId[ofs] === id) break;
+                            ++ofs;
+                        }
+                        if (ofs === vCount.length) {
+                            vCount.push(1);
+                            vId.push(id);
+                        } else {
+                            vCount[ofs]++;
+                        }
+                    }
+                }
+
+                for (j = 0; j < vCount.length; ++j) {
+                    if (vCount[j] === 1) {
+                        this.vertices[vId[j]].isBorder = true;
+                    } else {
+                        this.vertices[vId[j]].isBorder = false;
+                    }
+                }
+
+            }
+        }
+
+        private updateMesh(identifyBorders: boolean = false) {
+            var i: number;
+            if (!identifyBorders) {
+                var newTrianglesVector: Array<DecimationTriangle> = [];
+                for (i = 0; i < this.triangles.length; ++i) {
+                    if (!this.triangles[i].deleted) {
+                        newTrianglesVector.push(this.triangles[i]);
+                    }
+                }
+                this.triangles = newTrianglesVector;
+            }
+
+            for (i = 0; i < this.vertices.length; ++i) {
+                this.vertices[i].triangleCount = 0;
+                this.vertices[i].triangleStart = 0;
+            }
+            var t: DecimationTriangle;
+            var j: number;
+            var v: DecimationVertex;
+            for (i = 0; i < this.triangles.length; ++i) {
+                t = this.triangles[i];
+                for (j = 0; j < 3; ++j) {
+                    v = this.vertices[t.vertices[j]];
+                    v.triangleCount++;
+                }
+            }
+
+            var tStart = 0;
+
+            for (i = 0; i < this.vertices.length; ++i) {
+                this.vertices[i].triangleStart = tStart;
+                tStart += this.vertices[i].triangleCount;
+                this.vertices[i].triangleCount = 0;
+            }
+
+            var newReferences: Array<Reference> = new Array(this.triangles.length * 3);
+            for (i = 0; i < this.triangles.length; ++i) {
+                t = this.triangles[i];
+                for (j = 0; j < 3; ++j) {
+                    v = this.vertices[t.vertices[j]];
+                    newReferences[v.triangleStart + v.triangleCount] = new Reference(j, i);
+                    v.triangleCount++;
+                }
+            }
+            this.references = newReferences;
+
+            if (identifyBorders) {
+                this.identifyBorder();
+            }
+        }
+
+
+        private vertexError(q: QuadraticMatrix, point: Vector3): number {
+            var x = point.x;
+            var y = point.y;
+            var z = point.z;
+            return q.data[0] * x * x + 2 * q.data[1] * x * y + 2 * q.data[2] * x * z + 2 * q.data[3] * x + q.data[4] * y * y
+                + 2 * q.data[5] * y * z + 2 * q.data[6] * y + q.data[7] * z * z + 2 * q.data[8] * z + q.data[9];
+        }
+
+        private calculateError(vertex1: DecimationVertex, vertex2: DecimationVertex, pointResult?: Vector3, normalResult?: Vector3, uvResult?: Vector2): number {
+            var q = vertex1.q.add(vertex2.q);
+            var border = vertex1.isBorder && vertex2.isBorder;
+            var error: number = 0;
+            var qDet = q.det(0, 1, 2, 1, 4, 5, 2, 5, 7);
+
+            if (qDet !== 0 && !border) {
+                if (!pointResult) {
+                    pointResult = Vector3.Zero();
+                }
+                pointResult.x = -1 / qDet * (q.det(1, 2, 3, 4, 5, 6, 5, 7, 8));
+                pointResult.y = 1 / qDet * (q.det(0, 2, 3, 1, 5, 6, 2, 7, 8));
+                pointResult.z = -1 / qDet * (q.det(0, 1, 3, 1, 4, 6, 2, 5, 8));
+                error = this.vertexError(q, pointResult);
+                //TODO this should be correctly calculated
+                if (normalResult) {
+                    normalResult.copyFrom(vertex1.normal);
+                    uvResult.copyFrom(vertex1.uv);
+                }
+            } else {
+                var p3 = (vertex1.position.add(vertex2.position)).divide(new Vector3(2, 2, 2));
+                var norm3 = (vertex1.normal.add(vertex2.normal)).divide(new Vector3(2, 2, 2)).normalize();
+                var error1 = this.vertexError(q, vertex1.position);
+                var error2 = this.vertexError(q, vertex2.position);
+                var error3 = this.vertexError(q, p3);
+                error = Math.min(error1, error2, error3);
+                if (error === error1) {
+                    if (pointResult) {
+                        pointResult.copyFrom(vertex1.position);
+                        normalResult.copyFrom(vertex1.normal);
+                        uvResult.copyFrom(vertex1.uv);
+                    }
+                } else if (error === error2) {
+                    if (pointResult) {
+                        pointResult.copyFrom(vertex2.position);
+                        normalResult.copyFrom(vertex2.normal);
+                        uvResult.copyFrom(vertex2.uv);
+                    }
+                } else {
+                    if (pointResult) {
+                        pointResult.copyFrom(p3);
+                        normalResult.copyFrom(norm3);
+                        uvResult.copyFrom(vertex1.uv);
+                    }
+                }
+            }
+            return error;
+        }
+    }
+} 

+ 11 - 0
Babylon/Particles/babylon.particle.js

@@ -12,6 +12,17 @@
             this.angle = 0;
             this.angularSpeed = 0;
         }
+        Particle.prototype.copyTo = function (other) {
+            other.position.copyFrom(this.position);
+            other.direction.copyFrom(this.direction);
+            other.color.copyFrom(this.color);
+            other.colorStep.copyFrom(this.colorStep);
+            other.lifeTime = this.lifeTime;
+            other.age = this.age;
+            other.size = this.size;
+            other.angle = this.angle;
+            other.angularSpeed = this.angularSpeed;
+        };
         return Particle;
     })();
     BABYLON.Particle = Particle;

+ 16 - 4
Babylon/Particles/babylon.particle.ts

@@ -1,13 +1,25 @@
 module BABYLON {
     export class Particle {
-        public position = BABYLON.Vector3.Zero();
-        public direction = BABYLON.Vector3.Zero();
-        public color = new BABYLON.Color4(0, 0, 0, 0);
-        public colorStep = new BABYLON.Color4(0, 0, 0, 0);
+        public position = Vector3.Zero();
+        public direction = Vector3.Zero();
+        public color = new Color4(0, 0, 0, 0);
+        public colorStep = new Color4(0, 0, 0, 0);
         public lifeTime = 1.0;
         public age = 0;
         public size = 0;
         public angle = 0;
         public angularSpeed = 0;
+
+        public copyTo(other: Particle) {
+            other.position.copyFrom(this.position);
+            other.direction.copyFrom(this.direction);
+            other.color.copyFrom(this.color);
+            other.colorStep.copyFrom(this.colorStep);
+            other.lifeTime = this.lifeTime;
+            other.age = this.age;
+            other.size = this.size;
+            other.angle = this.angle;
+            other.angularSpeed = this.angularSpeed;
+        }
     }
 } 

+ 10 - 2
Babylon/Particles/babylon.particleSystem.js

@@ -104,8 +104,7 @@
                     particle.age += _this._scaledUpdateSpeed;
 
                     if (particle.age >= particle.lifeTime) {
-                        particles.splice(index, 1);
-                        _this._stockParticles.push(particle);
+                        _this.recycleParticle(particle);
                         index--;
                         continue;
                     } else {
@@ -126,6 +125,15 @@
                 }
             };
         }
+        ParticleSystem.prototype.recycleParticle = function (particle) {
+            var lastParticle = this.particles.pop();
+
+            if (lastParticle !== particle) {
+                lastParticle.copyTo(particle);
+                this._stockParticles.push(lastParticle);
+            }
+        };
+
         ParticleSystem.prototype.getCapacity = function () {
             return this._capacity;
         };

+ 11 - 3
Babylon/Particles/babylon.particleSystem.ts

@@ -134,9 +134,8 @@
                     var particle = particles[index];
                     particle.age += this._scaledUpdateSpeed;
 
-                    if (particle.age >= particle.lifeTime) { // Recycle
-                        particles.splice(index, 1);
-                        this._stockParticles.push(particle);
+                    if (particle.age >= particle.lifeTime) { // Recycle by swapping with last particle
+                        this.recycleParticle(particle);
                         index--;
                         continue;
                     }
@@ -159,6 +158,15 @@
             }
         }
 
+        public recycleParticle(particle: Particle): void {
+            var lastParticle = this.particles.pop();
+
+            if (lastParticle !== particle) {
+                lastParticle.copyTo(particle);
+                this._stockParticles.push(lastParticle);
+            }
+        }
+
         public getCapacity(): number {
             return this._capacity;
         }

+ 2 - 2
Babylon/Physics/Plugins/babylon.oimoJSPlugin.js

@@ -16,7 +16,7 @@ var BABYLON;
 
                         var center = mesh.getBoundingInfo().boundingBox.center;
                         body.setPosition(center.x, center.y, center.z);
-                        body.setOrientation(mesh.rotation.x, mesh.rotation.y, mesh.rotation.z);
+                        body.setRotation(mesh.rotation.x, mesh.rotation.y, mesh.rotation.z);
                         return;
                     }
 
@@ -30,7 +30,7 @@ var BABYLON;
 
                         body = registeredMesh.body.body;
                         body.setPosition(absolutePosition.x, absolutePosition.y, absolutePosition.z);
-                        body.setOrientation(absoluteRotation.x, absoluteRotation.y, absoluteRotation.z);
+                        body.setRotation(absoluteRotation.x, absoluteRotation.y, absoluteRotation.z);
                         return;
                     }
                 }

+ 2 - 2
Babylon/Physics/Plugins/babylon.oimoJSPlugin.ts

@@ -247,7 +247,7 @@ module BABYLON {
 
                     var center = mesh.getBoundingInfo().boundingBox.center;
                     body.setPosition(center.x, center.y, center.z);
-                    body.setOrientation(mesh.rotation.x, mesh.rotation.y, mesh.rotation.z);
+                    body.setRotation(mesh.rotation.x, mesh.rotation.y, mesh.rotation.z);
                     return;
                 }
                 // Case where the parent has been updated
@@ -260,7 +260,7 @@ module BABYLON {
 
                     body = registeredMesh.body.body;
                     body.setPosition(absolutePosition.x, absolutePosition.y, absolutePosition.z);
-                    body.setOrientation(absoluteRotation.x, absoluteRotation.y, absoluteRotation.z);
+                    body.setRotation(absoluteRotation.x, absoluteRotation.y, absoluteRotation.z);
                     return;
                 }
             }

+ 102 - 2
Babylon/Tools/babylon.tools.js

@@ -280,6 +280,17 @@
             return Math.min(max, Math.max(min, value));
         };
 
+        // Returns -1 when value is a negative number and
+        // +1 when value is a positive number.
+        Tools.Sign = function (value) {
+            value = +value; // convert to a number
+
+            if (value === 0 || isNaN(value))
+                return value;
+
+            return value > 0 ? 1 : -1;
+        };
+
         Tools.Format = function (value, decimals) {
             if (typeof decimals === "undefined") { decimals = 2; }
             return value.toFixed(decimals);
@@ -423,8 +434,8 @@
             }
 
             //At this point size can be a number, or an object (according to engine.prototype.createRenderTargetTexture method)
-            var texture = new BABYLON.RenderTargetTexture("screenShot", size, engine.scenes[0], false, false);
-            texture.renderList = engine.scenes[0].meshes;
+            var texture = new BABYLON.RenderTargetTexture("screenShot", size, scene, false, false);
+            texture.renderList = scene.meshes;
 
             texture.onAfterRender = function () {
                 // Read the contents of the framebuffer
@@ -803,5 +814,94 @@
         return Tools;
     })();
     BABYLON.Tools = Tools;
+
+    /**
+    * An implementation of a loop for asynchronous functions.
+    */
+    var AsyncLoop = (function () {
+        /**
+        * Constroctor.
+        * @param iterations the number of iterations.
+        * @param _fn the function to run each iteration
+        * @param _successCallback the callback that will be called upon succesful execution
+        * @param offset starting offset.
+        */
+        function AsyncLoop(iterations, _fn, _successCallback, offset) {
+            if (typeof offset === "undefined") { offset = 0; }
+            this.iterations = iterations;
+            this._fn = _fn;
+            this._successCallback = _successCallback;
+            this.index = offset - 1;
+            this._done = false;
+        }
+        /**
+        * Execute the next iteration. Must be called after the last iteration was finished.
+        */
+        AsyncLoop.prototype.executeNext = function () {
+            if (!this._done) {
+                if (this.index < this.iterations) {
+                    ++this.index;
+                    this._fn(this);
+                } else {
+                    this.breakLoop();
+                }
+            }
+        };
+
+        /**
+        * Break the loop and run the success callback.
+        */
+        AsyncLoop.prototype.breakLoop = function () {
+            this._done = true;
+            this._successCallback();
+        };
+
+        /**
+        * Helper function
+        */
+        AsyncLoop.Run = function (iterations, _fn, _successCallback, offset) {
+            if (typeof offset === "undefined") { offset = 0; }
+            var loop = new AsyncLoop(iterations, _fn, _successCallback, offset);
+
+            loop.executeNext();
+
+            return loop;
+        };
+
+        /**
+        * A for-loop that will run a given number of iterations synchronous and the rest async.
+        * @param iterations total number of iterations
+        * @param syncedIterations number of synchronous iterations in each async iteration.
+        * @param fn the function to call each iteration.
+        * @param callback a success call back that will be called when iterating stops.
+        * @param breakFunction a break condition (optional)
+        * @param timeout timeout settings for the setTimeout function. default - 0.
+        * @constructor
+        */
+        AsyncLoop.SyncAsyncForLoop = function (iterations, syncedIterations, fn, callback, breakFunction, timeout) {
+            if (typeof timeout === "undefined") { timeout = 0; }
+            AsyncLoop.Run(Math.ceil(iterations / syncedIterations), function (loop) {
+                if (breakFunction && breakFunction())
+                    loop.breakLoop();
+                else {
+                    setTimeout(function () {
+                        for (var i = 0; i < syncedIterations; ++i) {
+                            var iteration = (loop.index * syncedIterations) + i;
+                            if (iteration >= iterations)
+                                break;
+                            fn(iteration);
+                            if (breakFunction && breakFunction()) {
+                                loop.breakLoop();
+                                break;
+                            }
+                        }
+                        loop.executeNext();
+                    }, timeout);
+                }
+            }, callback);
+        };
+        return AsyncLoop;
+    })();
+    BABYLON.AsyncLoop = AsyncLoop;
 })(BABYLON || (BABYLON = {}));
 //# sourceMappingURL=babylon.tools.js.map

+ 99 - 3
Babylon/Tools/babylon.tools.ts

@@ -311,6 +311,17 @@
             return Math.min(max, Math.max(min, value));
         }
 
+        // Returns -1 when value is a negative number and
+        // +1 when value is a positive number. 
+        public static Sign(value: number): number {
+            value = +value; // convert to a number
+
+            if (value === 0 || isNaN(value))
+                return value;
+
+            return value > 0 ? 1 : -1
+        }
+
         public static Format(value: number, decimals: number = 2): string {
             return value.toFixed(decimals);
         }
@@ -461,8 +472,8 @@
             }
 
             //At this point size can be a number, or an object (according to engine.prototype.createRenderTargetTexture method)
-            var texture = new RenderTargetTexture("screenShot", size, engine.scenes[0], false, false);
-            texture.renderList = engine.scenes[0].meshes;
+            var texture = new RenderTargetTexture("screenShot", size, scene, false, false);
+            texture.renderList = scene.meshes;
 
             texture.onAfterRender = () => {
                 // Read the contents of the framebuffer
@@ -511,7 +522,7 @@
 
                     window.document.body.appendChild(a);
 
-                    a.addEventListener("click", () => {
+                    a.addEventListener("click",() => {
                         a.parentElement.removeChild(a);
                     });
                     a.click();
@@ -785,5 +796,90 @@
             return 0;
         }
 
+
+    }
+
+    /**
+     * An implementation of a loop for asynchronous functions.
+     */
+    export class AsyncLoop {
+        public index: number;
+        private _done: boolean;
+
+        /**
+         * Constroctor.
+         * @param iterations the number of iterations.
+         * @param _fn the function to run each iteration
+         * @param _successCallback the callback that will be called upon succesful execution
+         * @param offset starting offset.
+         */
+        constructor(public iterations: number, private _fn: (asyncLoop: AsyncLoop) => void, private _successCallback: () => void, offset: number = 0) {
+            this.index = offset - 1;
+            this._done = false;
+        }
+
+        /**
+         * Execute the next iteration. Must be called after the last iteration was finished.
+         */
+        public executeNext(): void {
+            if (!this._done) {
+                if (this.index < this.iterations) {
+                    ++this.index;
+                    this._fn(this);
+                } else {
+                    this.breakLoop();
+                }
+            }
+        }
+
+        /**
+         * Break the loop and run the success callback.
+         */
+        public breakLoop(): void {
+            this._done = true;
+            this._successCallback();
+        }
+
+        /**
+         * Helper function
+         */
+        public static Run(iterations: number, _fn: (asyncLoop: AsyncLoop) => void, _successCallback: () => void, offset: number = 0): AsyncLoop {
+            var loop = new AsyncLoop(iterations, _fn, _successCallback, offset);
+
+            loop.executeNext();
+
+            return loop;
+        }
+
+
+        /**
+         * A for-loop that will run a given number of iterations synchronous and the rest async.
+         * @param iterations total number of iterations
+         * @param syncedIterations number of synchronous iterations in each async iteration.
+         * @param fn the function to call each iteration.
+         * @param callback a success call back that will be called when iterating stops.
+         * @param breakFunction a break condition (optional)
+         * @param timeout timeout settings for the setTimeout function. default - 0.
+         * @constructor
+         */
+        public static SyncAsyncForLoop(iterations: number, syncedIterations: number, fn: (iteration: number) => void, callback: () => void, breakFunction?: () => boolean, timeout: number = 0) {
+            AsyncLoop.Run(Math.ceil(iterations / syncedIterations),(loop: AsyncLoop) => {
+                if (breakFunction && breakFunction()) loop.breakLoop();
+                else {
+                    setTimeout(() => {
+                        for (var i = 0; i < syncedIterations; ++i) {
+                            var iteration = (loop.index * syncedIterations) + i;
+                            if (iteration >= iterations) break;
+                            fn(iteration);
+                            if (breakFunction && breakFunction()) {
+                                loop.breakLoop();
+                                break;
+                            }
+                        }
+                        loop.executeNext();
+                    }, timeout);
+                }
+            }, callback);
+        }
     }
 } 

+ 57 - 18
Babylon/babylon.engine.js

@@ -122,9 +122,9 @@
 
             // Cull
             if (this._isCullDirty) {
-                if (this.cull === true) {
+                if (this.cull) {
                     gl.enable(gl.CULL_FACE);
-                } else if (this.cull === false) {
+                } else {
                     gl.disable(gl.CULL_FACE);
                 }
 
@@ -145,9 +145,9 @@
 
             // Depth test
             if (this._isDepthTestDirty) {
-                if (this.depthTest === true) {
+                if (this.depthTest) {
                     gl.enable(gl.DEPTH_TEST);
-                } else if (this.depthTest === false) {
+                } else {
                     gl.disable(gl.DEPTH_TEST);
                 }
                 this._isDepthTestDirty = false;
@@ -226,9 +226,9 @@
 
             // Alpha blend
             if (this._isAlphaBlendDirty) {
-                if (this._alphaBlend === true) {
+                if (this._alphaBlend) {
                     gl.enable(gl.BLEND);
-                } else if (this._alphaBlend === false) {
+                } else {
                     gl.disable(gl.BLEND);
                 }
 
@@ -322,15 +322,13 @@
     };
 
     var partialLoad = function (url, index, loadedImages, scene, onfinish) {
-        var img;
-
         var onload = function () {
             loadedImages[index] = img;
             loadedImages._internalCount++;
 
             scene._removePendingData(img);
 
-            if (loadedImages._internalCount == 6) {
+            if (loadedImages._internalCount === 6) {
                 onfinish(loadedImages);
             }
         };
@@ -339,7 +337,7 @@
             scene._removePendingData(img);
         };
 
-        img = BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
+        var img = BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
         scene._addPendingData(img);
     };
 
@@ -429,6 +427,23 @@
             this._caps.maxCubemapTextureSize = this._gl.getParameter(this._gl.MAX_CUBE_MAP_TEXTURE_SIZE);
             this._caps.maxRenderTextureSize = this._gl.getParameter(this._gl.MAX_RENDERBUFFER_SIZE);
 
+            // Infos
+            this._glVersion = this._gl.getParameter(this._gl.VERSION);
+
+            var rendererInfo = this._gl.getExtension("WEBGL_debug_renderer_info");
+            if (rendererInfo != null) {
+                this._glRenderer = this._gl.getParameter(rendererInfo.UNMASKED_RENDERER_WEBGL);
+                this._glVendor = this._gl.getParameter(rendererInfo.UNMASKED_VENDOR_WEBGL);
+            }
+
+            if (!this._glVendor) {
+                this._glVendor = "Unknown vendor";
+            }
+
+            if (!this._glRenderer) {
+                this._glRenderer = "Unknown renderer";
+            }
+
             // Extensions
             this._caps.standardDerivatives = (this._gl.getExtension('OES_standard_derivatives') !== null);
             this._caps.s3tc = this._gl.getExtension('WEBGL_compressed_texture_s3tc');
@@ -588,6 +603,14 @@
             configurable: true
         });
 
+        Engine.prototype.getGlInfo = function () {
+            return {
+                vendor: this._glVendor,
+                renderer: this._glRenderer,
+                version: this._glVersion
+            };
+        };
+
         Engine.prototype.getAudioEngine = function () {
             return this._audioEngine;
         };
@@ -1256,16 +1279,16 @@
 
         Engine.prototype.setAlphaMode = function (mode) {
             switch (mode) {
-                case BABYLON.Engine.ALPHA_DISABLE:
+                case Engine.ALPHA_DISABLE:
                     this.setDepthWrite(true);
                     this._alphaState.alphaBlend = false;
                     break;
-                case BABYLON.Engine.ALPHA_COMBINE:
+                case Engine.ALPHA_COMBINE:
                     this.setDepthWrite(false);
                     this._alphaState.setAlphaBlendFunctionParameters(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE);
                     this._alphaState.alphaBlend = true;
                     break;
-                case BABYLON.Engine.ALPHA_ADD:
+                case Engine.ALPHA_ADD:
                     this.setDepthWrite(false);
                     this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
                     this._alphaState.alphaBlend = true;
@@ -1389,7 +1412,7 @@
                 callback = function (data) {
                     var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
 
-                    var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap && ((info.width >> (info.mipmapCount - 1)) == 1);
+                    var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap && ((info.width >> (info.mipmapCount - 1)) === 1);
                     prepareWebGLTexture(texture, _this._gl, scene, info.width, info.height, invertY, !loadMipmap, info.isFourCC, function () {
                         BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 1);
 
@@ -1408,12 +1431,28 @@
             } else {
                 var onload = function (img) {
                     prepareWebGLTexture(texture, _this._gl, scene, img.width, img.height, invertY, noMipmap, false, function (potWidth, potHeight) {
-                        var isPot = (img.width == potWidth && img.height == potHeight);
+                        var isPot = (img.width === potWidth && img.height === potHeight);
                         if (!isPot) {
                             _this._workingCanvas.width = potWidth;
                             _this._workingCanvas.height = potHeight;
 
+                            if (samplingMode === BABYLON.Texture.NEAREST_SAMPLINGMODE) {
+                                _this._workingContext.imageSmoothingEnabled = false;
+                                _this._workingContext.mozImageSmoothingEnabled = false;
+                                _this._workingContext.oImageSmoothingEnabled = false;
+                                _this._workingContext.webkitImageSmoothingEnabled = false;
+                                _this._workingContext.msImageSmoothingEnabled = false;
+                            }
+
                             _this._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, potWidth, potHeight);
+
+                            if (samplingMode === BABYLON.Texture.NEAREST_SAMPLINGMODE) {
+                                _this._workingContext.imageSmoothingEnabled = true;
+                                _this._workingContext.mozImageSmoothingEnabled = true;
+                                _this._workingContext.oImageSmoothingEnabled = true;
+                                _this._workingContext.webkitImageSmoothingEnabled = true;
+                                _this._workingContext.msImageSmoothingEnabled = true;
+                            }
                         }
 
                         _this._gl.texImage2D(_this._gl.TEXTURE_2D, 0, _this._gl.RGBA, _this._gl.RGBA, _this._gl.UNSIGNED_BYTE, isPot ? img : _this._workingCanvas);
@@ -1649,7 +1688,7 @@
 
                     BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 6);
 
-                    if (!noMipmap && !info.isFourCC && info.mipmapCount == 1) {
+                    if (!noMipmap && !info.isFourCC && info.mipmapCount === 1) {
                         gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
                     }
 
@@ -1777,12 +1816,12 @@
                 if (texture.update()) {
                     this._activeTexturesCache[channel] = null;
                 }
-            } else if (texture.delayLoadState == BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
+            } else if (texture.delayLoadState === Engine.DELAYLOADSTATE_NOTLOADED) {
                 texture.delayLoad();
                 return;
             }
 
-            if (this._activeTexturesCache[channel] == texture) {
+            if (this._activeTexturesCache[channel] === texture) {
                 return;
             }
             this._activeTexturesCache[channel] = texture;

+ 91 - 49
Babylon/babylon.engine.ts

@@ -103,9 +103,9 @@
 
             // Cull
             if (this._isCullDirty) {
-                if (this.cull === true) {
+                if (this.cull) {
                     gl.enable(gl.CULL_FACE);
-                } else if (this.cull === false) {
+                } else {
                     gl.disable(gl.CULL_FACE);
                 }
 
@@ -126,9 +126,9 @@
 
             // Depth test
             if (this._isDepthTestDirty) {
-                if (this.depthTest === true) {
+                if (this.depthTest) {
                     gl.enable(gl.DEPTH_TEST);
-                } else if (this.depthTest === false) {
+                } else {
                     gl.disable(gl.DEPTH_TEST);
                 }
                 this._isDepthTestDirty = false;
@@ -202,9 +202,9 @@
 
             // Alpha blend
             if (this._isAlphaBlendDirty) {
-                if (this._alphaBlend === true) {
+                if (this._alphaBlend) {
                     gl.enable(gl.BLEND);
-                } else if (this._alphaBlend === false) {
+                } else {
                     gl.disable(gl.BLEND);
                 }
 
@@ -234,21 +234,21 @@
     var getSamplingParameters = (samplingMode: number, generateMipMaps: boolean, gl: WebGLRenderingContext): { min: number; mag: number } => {
         var magFilter = gl.NEAREST;
         var minFilter = gl.NEAREST;
-        if (samplingMode === BABYLON.Texture.BILINEAR_SAMPLINGMODE) {
+        if (samplingMode === Texture.BILINEAR_SAMPLINGMODE) {
             magFilter = gl.LINEAR;
             if (generateMipMaps) {
                 minFilter = gl.LINEAR_MIPMAP_NEAREST;
             } else {
                 minFilter = gl.LINEAR;
             }
-        } else if (samplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
+        } else if (samplingMode === Texture.TRILINEAR_SAMPLINGMODE) {
             magFilter = gl.LINEAR;
             if (generateMipMaps) {
                 minFilter = gl.LINEAR_MIPMAP_LINEAR;
             } else {
                 minFilter = gl.LINEAR;
             }
-        } else if (samplingMode === BABYLON.Texture.NEAREST_SAMPLINGMODE) {
+        } else if (samplingMode === Texture.NEAREST_SAMPLINGMODE) {
             magFilter = gl.NEAREST;
             if (generateMipMaps) {
                 minFilter = gl.NEAREST_MIPMAP_LINEAR;
@@ -298,15 +298,13 @@
     var partialLoad = (url: string, index: number, loadedImages: any, scene,
         onfinish: (images: HTMLImageElement[]) => void) => {
 
-        var img: HTMLImageElement;
-
         var onload = () => {
             loadedImages[index] = img;
             loadedImages._internalCount++;
 
             scene._removePendingData(img);
 
-            if (loadedImages._internalCount == 6) {
+            if (loadedImages._internalCount === 6) {
                 onfinish(loadedImages);
             }
         };
@@ -315,7 +313,7 @@
             scene._removePendingData(img);
         };
 
-        img = BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
+        var img = Tools.LoadImage(url, onload, onerror, scene.database);
         scene._addPendingData(img);
     }
 
@@ -426,11 +424,11 @@
         public scenes = new Array<Scene>();
 
         // Private Members
-        private _gl: WebGLRenderingContext;
+        public _gl: WebGLRenderingContext;
         private _renderingCanvas: HTMLCanvasElement;
         private _windowIsBackground = false;
 
-        private _audioEngine: BABYLON.AudioEngine;
+        private _audioEngine: AudioEngine;
 
         private _onBlur: () => void;
         private _onFocus: () => void;
@@ -449,6 +447,10 @@
 
         private _drawCalls = 0;
 
+        private _glVersion: string;
+        private _glRenderer: string;
+        private _glVendor: string;
+
         private _renderingQueueLaunched = false;
         private _activeRenderLoops = [];
 
@@ -525,6 +527,23 @@
             this._caps.maxCubemapTextureSize = this._gl.getParameter(this._gl.MAX_CUBE_MAP_TEXTURE_SIZE);
             this._caps.maxRenderTextureSize = this._gl.getParameter(this._gl.MAX_RENDERBUFFER_SIZE);
 
+            // Infos
+            this._glVersion = this._gl.getParameter(this._gl.VERSION);
+
+            var rendererInfo: any = this._gl.getExtension("WEBGL_debug_renderer_info");
+            if (rendererInfo != null) {
+                this._glRenderer = this._gl.getParameter(rendererInfo.UNMASKED_RENDERER_WEBGL);
+                this._glVendor = this._gl.getParameter(rendererInfo.UNMASKED_VENDOR_WEBGL);
+            }
+
+            if (!this._glVendor) {
+                this._glVendor = "Unknown vendor";
+            }
+
+            if (!this._glRenderer) {
+                this._glRenderer = "Unknown renderer";
+            }
+
             // Extensions
             this._caps.standardDerivatives = (this._gl.getExtension('OES_standard_derivatives') !== null);
             this._caps.s3tc = this._gl.getExtension('WEBGL_compressed_texture_s3tc');
@@ -583,11 +602,19 @@
             document.addEventListener("mozpointerlockchange", this._onPointerLockChange, false);
             document.addEventListener("webkitpointerlockchange", this._onPointerLockChange, false);
 
-            this._audioEngine = new BABYLON.AudioEngine();
+            this._audioEngine = new AudioEngine();
 
             Tools.Log("Babylon.js engine (v" + Engine.Version + ") launched");
         }
 
+        public getGlInfo() {
+            return {
+                vendor: this._glVendor,
+                renderer: this._glRenderer,
+                version: this._glVersion
+            }
+        }
+
         public getAudioEngine(): AudioEngine {
             return this._audioEngine;
         }
@@ -698,7 +725,7 @@
 
             if (this._activeRenderLoops.length > 0) {
                 // Register new frame
-                BABYLON.Tools.QueueNewFrame(() => {
+                Tools.QueueNewFrame(() => {
                     this._renderLoop();
                 });
             } else {
@@ -715,7 +742,7 @@
 
             if (!this._renderingQueueLaunched) {
                 this._renderingQueueLaunched = true;
-                BABYLON.Tools.QueueNewFrame(() => {
+                Tools.QueueNewFrame(() => {
                     this._renderLoop();
                 });
             }
@@ -723,10 +750,10 @@
 
         public switchFullscreen(requestPointerLock: boolean): void {
             if (this.isFullscreen) {
-                BABYLON.Tools.ExitFullscreen();
+                Tools.ExitFullscreen();
             } else {
                 this._pointerLockRequested = requestPointerLock;
-                BABYLON.Tools.RequestFullscreen(this._renderingCanvas);
+                Tools.RequestFullscreen(this._renderingCanvas);
             }
         }
 
@@ -1047,7 +1074,7 @@
                 return this._compiledEffects[name];
             }
 
-            var effect = new BABYLON.Effect(baseName, attributesNames, uniformsNames, samplers, this, defines, fallbacks, onCompiled, onError);
+            var effect = new Effect(baseName, attributesNames, uniformsNames, samplers, this, defines, fallbacks, onCompiled, onError);
             effect._key = name;
             this._compiledEffects[name] = effect;
 
@@ -1256,16 +1283,16 @@
 
         public setAlphaMode(mode: number): void {
             switch (mode) {
-                case BABYLON.Engine.ALPHA_DISABLE:
+                case Engine.ALPHA_DISABLE:
                     this.setDepthWrite(true);
                     this._alphaState.alphaBlend = false;
                     break;
-                case BABYLON.Engine.ALPHA_COMBINE:
+                case Engine.ALPHA_COMBINE:
                     this.setDepthWrite(false);
                     this._alphaState.setAlphaBlendFunctionParameters(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE);
                     this._alphaState.alphaBlend = true;
                     break;
-                case BABYLON.Engine.ALPHA_ADD:
+                case Engine.ALPHA_ADD:
                     this.setDepthWrite(false);
                     this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
                     this._alphaState.alphaBlend = true;
@@ -1308,10 +1335,10 @@
             var magFilter = gl.NEAREST;
             var minFilter = gl.NEAREST;
 
-            if (samplingMode === BABYLON.Texture.BILINEAR_SAMPLINGMODE) {
+            if (samplingMode === Texture.BILINEAR_SAMPLINGMODE) {
                 magFilter = gl.LINEAR;
                 minFilter = gl.LINEAR;
-            } else if (samplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
+            } else if (samplingMode === Texture.TRILINEAR_SAMPLINGMODE) {
                 magFilter = gl.LINEAR;
                 minFilter = gl.LINEAR_MIPMAP_LINEAR;
             }
@@ -1325,7 +1352,6 @@
         }
 
         public createTexture(url: string, noMipmap: boolean, invertY: boolean, scene: Scene, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: () => void = null, onError: () => void = null, buffer: any = null): WebGLTexture {
-
             var texture = this._gl.createTexture();
 
             var extension: string;
@@ -1364,7 +1390,7 @@
                 var callback = (arrayBuffer) => {
                     var data = new Uint8Array(arrayBuffer);
 
-                    var header = BABYLON.Internals.TGATools.GetTGAHeader(data);
+                    var header = Internals.TGATools.GetTGAHeader(data);
 
                     prepareWebGLTexture(texture, this._gl, scene, header.width, header.height, invertY, noMipmap, false, () => {
                         Internals.TGATools.UploadContent(this._gl, data);
@@ -1376,7 +1402,7 @@
                 };
 
                 if (!(fromData instanceof Array))
-                    BABYLON.Tools.LoadFile(url, arrayBuffer => {
+                    Tools.LoadFile(url, arrayBuffer => {
                         callback(arrayBuffer);
                     }, onerror, scene.database, true);
                 else
@@ -1384,9 +1410,9 @@
 
             } else if (isDDS) {
                 callback = (data) => {
-                    var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
+                    var info = Internals.DDSTools.GetDDSInfo(data);
 
-                    var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap && ((info.width >> (info.mipmapCount - 1)) == 1);
+                    var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap && ((info.width >> (info.mipmapCount - 1)) === 1);
                     prepareWebGLTexture(texture, this._gl, scene, info.width, info.height, invertY, !loadMipmap, info.isFourCC, () => {
 
                         Internals.DDSTools.UploadDDSLevels(this._gl, this.getCaps().s3tc, data, info, loadMipmap, 1);
@@ -1398,7 +1424,7 @@
                 };
 
                 if (!(fromData instanceof Array))
-                    BABYLON.Tools.LoadFile(url, data => {
+                    Tools.LoadFile(url, data => {
                         callback(data);
                     }, onerror, scene.database, true);
                 else
@@ -1407,12 +1433,28 @@
             } else {
                 var onload = (img) => {
                     prepareWebGLTexture(texture, this._gl, scene, img.width, img.height, invertY, noMipmap, false, (potWidth, potHeight) => {
-                        var isPot = (img.width == potWidth && img.height == potHeight);
+                        var isPot = (img.width === potWidth && img.height === potHeight);
                         if (!isPot) {
                             this._workingCanvas.width = potWidth;
                             this._workingCanvas.height = potHeight;
 
+                            if (samplingMode === Texture.NEAREST_SAMPLINGMODE) {
+                                this._workingContext.imageSmoothingEnabled = false;
+                                this._workingContext.mozImageSmoothingEnabled = false;
+                                this._workingContext.oImageSmoothingEnabled = false;
+                                this._workingContext.webkitImageSmoothingEnabled = false;
+                                this._workingContext.msImageSmoothingEnabled = false;
+                            }
+
                             this._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, potWidth, potHeight);
+
+                            if (samplingMode === Texture.NEAREST_SAMPLINGMODE) {
+                                this._workingContext.imageSmoothingEnabled = true;
+                                this._workingContext.mozImageSmoothingEnabled = true;
+                                this._workingContext.oImageSmoothingEnabled = true;
+                                this._workingContext.webkitImageSmoothingEnabled = true;
+                                this._workingContext.msImageSmoothingEnabled = true;
+                            }
                         }
 
                         this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, isPot ? img : this._workingCanvas);
@@ -1425,9 +1467,9 @@
 
 
                 if (!(fromData instanceof Array))
-                    BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
+                    Tools.LoadImage(url, onload, onerror, scene.database);
                 else
-                    BABYLON.Tools.LoadImage(buffer, onload, onerror, scene.database);
+                    Tools.LoadImage(buffer, onload, onerror, scene.database);
             }
 
             return texture;
@@ -1563,7 +1605,7 @@
             // in the same way, generateDepthBuffer is defaulted to true
             var generateMipMaps = false;
             var generateDepthBuffer = true;
-            var samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
+            var samplingMode = Texture.TRILINEAR_SAMPLINGMODE;
             if (options !== undefined) {
                 generateMipMaps = options.generateMipMaps === undefined ? options : options.generateMipmaps;
                 generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
@@ -1637,8 +1679,8 @@
             var isDDS = this.getCaps().s3tc && (extension === ".dds");
 
             if (isDDS) {
-                BABYLON.Tools.LoadFile(rootUrl, data => {
-                    var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
+                Tools.LoadFile(rootUrl, data => {
+                    var info = Internals.DDSTools.GetDDSInfo(data);
 
                     var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap;
 
@@ -1647,7 +1689,7 @@
 
                     Internals.DDSTools.UploadDDSLevels(this._gl, this.getCaps().s3tc, data, info, loadMipmap, 6);
 
-                    if (!noMipmap && !info.isFourCC && info.mipmapCount == 1) {
+                    if (!noMipmap && !info.isFourCC && info.mipmapCount === 1) {
                         gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
                     }
 
@@ -1772,16 +1814,16 @@
             }
 
             // Video
-            if (texture instanceof BABYLON.VideoTexture) {
+            if (texture instanceof VideoTexture) {
                 if ((<VideoTexture>texture).update()) {
                     this._activeTexturesCache[channel] = null;
                 }
-            } else if (texture.delayLoadState == BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) { // Delay loading
+            } else if (texture.delayLoadState === Engine.DELAYLOADSTATE_NOTLOADED) { // Delay loading
                 texture.delayLoad();
                 return;
             }
 
-            if (this._activeTexturesCache[channel] == texture) {
+            if (this._activeTexturesCache[channel] === texture) {
                 return;
             }
             this._activeTexturesCache[channel] = texture;
@@ -1795,7 +1837,7 @@
                 if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
                     internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
                     // CUBIC_MODE and SKYBOX_MODE both require CLAMP_TO_EDGE.  All other modes use REPEAT.
-                    var textureWrapMode = (texture.coordinatesMode !== BABYLON.Texture.CUBIC_MODE && texture.coordinatesMode !== BABYLON.Texture.SKYBOX_MODE) ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE;
+                    var textureWrapMode = (texture.coordinatesMode !== Texture.CUBIC_MODE && texture.coordinatesMode !== Texture.SKYBOX_MODE) ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE;
                     this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_S, textureWrapMode);
                     this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_T, textureWrapMode);
                 }
@@ -1808,13 +1850,13 @@
                     internalTexture._cachedWrapU = texture.wrapU;
 
                     switch (texture.wrapU) {
-                        case BABYLON.Texture.WRAP_ADDRESSMODE:
+                        case Texture.WRAP_ADDRESSMODE:
                             this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.REPEAT);
                             break;
-                        case BABYLON.Texture.CLAMP_ADDRESSMODE:
+                        case Texture.CLAMP_ADDRESSMODE:
                             this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
                             break;
-                        case BABYLON.Texture.MIRROR_ADDRESSMODE:
+                        case Texture.MIRROR_ADDRESSMODE:
                             this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.MIRRORED_REPEAT);
                             break;
                     }
@@ -1823,13 +1865,13 @@
                 if (internalTexture._cachedWrapV !== texture.wrapV) {
                     internalTexture._cachedWrapV = texture.wrapV;
                     switch (texture.wrapV) {
-                        case BABYLON.Texture.WRAP_ADDRESSMODE:
+                        case Texture.WRAP_ADDRESSMODE:
                             this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.REPEAT);
                             break;
-                        case BABYLON.Texture.CLAMP_ADDRESSMODE:
+                        case Texture.CLAMP_ADDRESSMODE:
                             this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
                             break;
-                        case BABYLON.Texture.MIRROR_ADDRESSMODE:
+                        case Texture.MIRROR_ADDRESSMODE:
                             this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.MIRRORED_REPEAT);
                             break;
                     }

+ 7 - 0
Babylon/babylon.mixins.ts

@@ -43,6 +43,13 @@ interface HTMLCanvasElement {
     webkitRequestPointerLock(): void;
 }
 
+interface CanvasRenderingContext2D {
+    imageSmoothingEnabled: boolean;
+    mozImageSmoothingEnabled: boolean;
+    oImageSmoothingEnabled: boolean;
+    webkitImageSmoothingEnabled: boolean;
+}
+
 interface WebGLTexture {
     isReady: boolean;
     isCube:boolean;

+ 16 - 0
Babylon/babylon.scene.js

@@ -664,6 +664,22 @@
             return null;
         };
 
+        Scene.prototype.getNodeByName = function (name) {
+            var mesh = this.getMeshByName(name);
+
+            if (mesh) {
+                return mesh;
+            }
+
+            var light = this.getLightByName(name);
+
+            if (light) {
+                return light;
+            }
+
+            return this.getCameraByName(name);
+        };
+
         Scene.prototype.getMeshByName = function (name) {
             for (var index = 0; index < this.meshes.length; index++) {
                 if (this.meshes[index].name === name) {

+ 19 - 3
Babylon/babylon.scene.ts

@@ -350,10 +350,10 @@
                                 pickResult.pickedMesh.actionManager.processTrigger(ActionManager.OnCenterPickTrigger, ActionEvent.CreateNew(pickResult.pickedMesh, evt));
                                 break;
                             case 2:
-                                pickResult.pickedMesh.actionManager.processTrigger(ActionManager.OnRightPickTrigger, ActionEvent.CreateNew(pickResult.pickedMesh,evt));
+                                pickResult.pickedMesh.actionManager.processTrigger(ActionManager.OnRightPickTrigger, ActionEvent.CreateNew(pickResult.pickedMesh, evt));
                                 break;
                         }
-                        pickResult.pickedMesh.actionManager.processTrigger(ActionManager.OnPickTrigger, ActionEvent.CreateNew(pickResult.pickedMesh,evt));
+                        pickResult.pickedMesh.actionManager.processTrigger(ActionManager.OnPickTrigger, ActionEvent.CreateNew(pickResult.pickedMesh, evt));
                     }
                 }
 
@@ -742,6 +742,22 @@
             return null;
         }
 
+        public getNodeByName(name: string): Node {
+            var mesh = this.getMeshByName(name);
+
+            if (mesh) {
+                return mesh;
+            }
+
+            var light = this.getLightByName(name);
+
+            if (light) {
+                return light;
+            }
+
+            return this.getCameraByName(name);
+        }
+
         public getMeshByName(name: string): AbstractMesh {
             for (var index = 0; index < this.meshes.length; index++) {
                 if (this.meshes[index].name === name) {
@@ -903,7 +919,7 @@
 
             if (mesh.showBoundingBox || this.forceShowBoundingBoxes) {
                 this._boundingBoxRenderer.renderList.push(mesh.getBoundingInfo().boundingBox);
-            }            
+            }
 
             if (mesh && mesh.subMeshes) {
                 // Submeshes Octrees

文件差異過大導致無法顯示
+ 1078 - 53
babylon.2.0-alpha.debug.js


文件差異過大導致無法顯示
+ 18 - 18
babylon.2.0-alpha.js