Browse Source

Adding support for equirectangular not fixed

David Catuhe 9 years ago
parent
commit
97179d04da

File diff suppressed because it is too large
+ 17 - 16
dist/preview release/babylon.core.js


File diff suppressed because it is too large
+ 2184 - 2182
dist/preview release/babylon.d.ts


File diff suppressed because it is too large
+ 24 - 24
dist/preview release/babylon.js


File diff suppressed because it is too large
+ 115 - 137
dist/preview release/babylon.max.js


File diff suppressed because it is too large
+ 24 - 24
dist/preview release/babylon.noworker.js


+ 1 - 1
dist/preview release/what's new.md

@@ -22,7 +22,7 @@
     - Added `Animatable.goToFrame()` ([deltakosh](https://github.com/deltakosh))   
     - Fixed behavior or `Animation.CreateAndStartAnimation` and added `Animation.CreateMergeAndStartAnimation` to reproduce previous behavior ([deltakosh](https://github.com/deltakosh))
     - Adding `StandardMaterial.linkEmissiveWithDiffuse` to, well, link emissive with diffuse value. (With)[http://www.babylonjs-playground.com/#2FPUCS#2] and (without)[http://www.babylonjs-playground.com/#2FPUCS#1] ([deltakosh](https://github.com/deltakosh))
-    - Adding support for equi-rectangular mapping. See [demo here](http://www.babylonjs-playground.com/#27FN5R#8) ([deltakosh](https://github.com/deltakosh))
+    - Adding support for equi-rectangular mapping. See [demo here](http://www.babylonjs-playground.com/#27FN5R#12) ([deltakosh](https://github.com/deltakosh))
     - Sprites and particles scheduler updated to be resolved before transparent objects ([deltakosh](https://github.com/deltakosh))
     - Added ability to deactivate ArcRotateCamera panning mechanism (by setting panningSensibility to 0) ([vouskprod](http://www.github.com/vousk))    
     - Added `DirectionalLight.autoUpdateExtends` to prevent directional lights to adapt to scene extends ([deltakosh](https://github.com/deltakosh))

+ 5 - 4
src/Cameras/babylon.arcRotateCamera.js

@@ -73,7 +73,7 @@ var BABYLON;
                 _this.position.copyFrom(_this._newPosition);
                 var up = _this.upVector;
                 if (_this.allowUpsideDown && _this.beta < 0) {
-                    var up = up.clone();
+                    up = up.clone();
                     up = up.negate();
                 }
                 BABYLON.Matrix.LookAtLHToRef(_this.position, target, up, _this._viewMatrix);
@@ -148,7 +148,7 @@ var BABYLON;
             if (this._onPointerDown === undefined) {
                 this._onPointerDown = function (evt) {
                     // Manage panning with right click
-                    _this._isRightClick = evt.button === 2 ? true : false;
+                    _this._isRightClick = evt.button === 2;
                     // manage pointers
                     pointers.add(evt.pointerId, { x: evt.clientX, y: evt.clientY, type: evt.pointerType });
                     cacheSoloPointer = pointers.item(evt.pointerId);
@@ -376,7 +376,7 @@ var BABYLON;
                 }
             }
             // Inertia
-            if (this.inertialAlphaOffset !== 0 || this.inertialBetaOffset !== 0 || this.inertialRadiusOffset != 0) {
+            if (this.inertialAlphaOffset !== 0 || this.inertialBetaOffset !== 0 || this.inertialRadiusOffset !== 0) {
                 this.alpha += this.beta <= 0 ? -this.inertialAlphaOffset : this.inertialAlphaOffset;
                 this.beta += this.inertialBetaOffset;
                 this.radius -= this.inertialRadiusOffset;
@@ -481,7 +481,7 @@ var BABYLON;
                 this.position.copyFrom(this._newPosition);
                 var up = this.upVector;
                 if (this.allowUpsideDown && this.beta < 0) {
-                    var up = up.clone();
+                    up = up.clone();
                     up = up.negate();
                 }
                 BABYLON.Matrix.LookAtLHToRef(this.position, target, up, this._viewMatrix);
@@ -530,6 +530,7 @@ var BABYLON;
                     var alphaShift = this._cameraRigParams.stereoHalfAngle * (cameraIndex === 0 ? 1 : -1);
                     return new ArcRotateCamera(name, this.alpha + alphaShift, this.beta, this.radius, this.target, this.getScene());
             }
+            return null;
         };
         /**
          * @override

+ 5 - 4
src/Cameras/babylon.arcRotateCamera.ts

@@ -142,7 +142,7 @@
             if (this._onPointerDown === undefined) {
                 this._onPointerDown = evt => {
                     // Manage panning with right click
-                    this._isRightClick = evt.button === 2 ? true : false;
+                    this._isRightClick = evt.button === 2;
 
                     // manage pointers
                     pointers.add(evt.pointerId, { x: evt.clientX, y: evt.clientY, type: evt.pointerType });
@@ -408,7 +408,7 @@
             }			
 			
             // Inertia
-            if (this.inertialAlphaOffset !== 0 || this.inertialBetaOffset !== 0 || this.inertialRadiusOffset != 0) {
+            if (this.inertialAlphaOffset !== 0 || this.inertialBetaOffset !== 0 || this.inertialRadiusOffset !== 0) {
                 this.alpha += this.beta <= 0 ? -this.inertialAlphaOffset : this.inertialAlphaOffset;
                 this.beta += this.inertialBetaOffset;
                 this.radius -= this.inertialRadiusOffset;
@@ -529,7 +529,7 @@
 
                 var up = this.upVector;
                 if (this.allowUpsideDown && this.beta < 0) {
-                    var up = up.clone();
+                    up = up.clone();
                     up = up.negate();
                 }
 
@@ -567,7 +567,7 @@
 
             var up = this.upVector;
             if (this.allowUpsideDown && this.beta < 0) {
-                var up = up.clone();
+                up = up.clone();
                 up = up.negate();
             }
 
@@ -624,6 +624,7 @@
                     var alphaShift = this._cameraRigParams.stereoHalfAngle * (cameraIndex === 0 ? 1 : -1);
                     return new ArcRotateCamera(name, this.alpha + alphaShift, this.beta, this.radius, this.target, this.getScene());
             }
+            return null;
         }
         
         /**

+ 41 - 41
src/Debug/babylon.debugLayer.ts

@@ -393,7 +393,7 @@
             this._rootElement.appendChild(this._globalDiv);
 
             this._generateDOMelements();
-            
+
             engine.getRenderingCanvas().addEventListener("click", this._onCanvasClick);
 
             this._syncPositions();
@@ -489,10 +489,10 @@
             button.style.width = "150px";
             button.style.marginBottom = "5px";
             button.style.color = "#444444";
-            button.style.border = "1px solid white"; 
+            button.style.border = "1px solid white";
             button.className = "debugLayerButton";
 
-            button.addEventListener("click",(evt: Event) => {
+            button.addEventListener("click", (evt: Event) => {
                 task(evt.target, tag);
             });
 
@@ -708,7 +708,7 @@
                     }
                 });
                 this._optionsSubsetDiv.appendChild(document.createElement("br"));
-  
+
                 this._globalDiv.appendChild(this._statsDiv);
                 this._globalDiv.appendChild(this._logDiv);
                 this._globalDiv.appendChild(this._optionsDiv);
@@ -722,46 +722,46 @@
             var glInfo = engine.getGlInfo();
 
             this._statsSubsetDiv.innerHTML = "Babylon.js v" + Engine.Version + " - <b>" + Tools.Format(engine.getFps(), 0) + " fps</b><br><br>"
-                + "<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 indices: " + scene.getActiveIndices() + "<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>"
-                + "Fragment depth: " + (engine.getCaps().fragmentDepthSupported ? "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>";
+            + "<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 indices: " + scene.getActiveIndices() + "<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>"
+            + "Fragment depth: " + (engine.getCaps().fragmentDepthSupported ? "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;
             }
         }
     }
-}
+}

+ 2 - 1
src/Layer/babylon.layer.js

@@ -3,6 +3,7 @@ var BABYLON;
     var Layer = (function () {
         function Layer(name, imgUrl, scene, isBackground, color) {
             this.name = name;
+            this.alphaBlendingMode = BABYLON.Engine.ALPHA_COMBINE;
             this._vertexDeclaration = [2];
             this._vertexStrideSize = 2 * 4;
             this.texture = imgUrl ? new BABYLON.Texture(imgUrl, scene, true) : null;
@@ -45,7 +46,7 @@ var BABYLON;
             // VBOs
             engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
             // Draw order
-            engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
+            engine.setAlphaMode(this.alphaBlendingMode);
             engine.draw(true, 0, 6);
             engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
         };

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

@@ -4,6 +4,7 @@
         public isBackground: boolean;
         public color: Color4;
         public onDispose: () => void;
+        public alphaBlendingMode = Engine.ALPHA_COMBINE;
 
         private _scene: Scene;
         private _vertexDeclaration = [2];
@@ -70,7 +71,7 @@
             engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
 
             // Draw order
-            engine.setAlphaMode(Engine.ALPHA_COMBINE);
+            engine.setAlphaMode(this.alphaBlendingMode);
             engine.draw(true, 0, 6);
             engine.setAlphaMode(Engine.ALPHA_DISABLE);
         }

+ 1 - 0
src/Materials/Textures/babylon.texture.js

@@ -199,6 +199,7 @@ var BABYLON;
         Texture.SKYBOX_MODE = 5;
         Texture.INVCUBIC_MODE = 6;
         Texture.EQUIRECTANGULAR_MODE = 7;
+        Texture.FIXED_EQUIRECTANGULAR_MODE = 8;
         Texture.CLAMP_ADDRESSMODE = 0;
         Texture.WRAP_ADDRESSMODE = 1;
         Texture.MIRROR_ADDRESSMODE = 2;

+ 1 - 0
src/Materials/Textures/babylon.texture.ts

@@ -13,6 +13,7 @@
         public static SKYBOX_MODE = 5;
         public static INVCUBIC_MODE = 6;
         public static EQUIRECTANGULAR_MODE = 7;
+        public static FIXED_EQUIRECTANGULAR_MODE = 8;
 
         public static CLAMP_ADDRESSMODE = 0;
         public static WRAP_ADDRESSMODE = 1;

+ 4 - 0
src/Materials/babylon.standardMaterial.js

@@ -102,6 +102,7 @@ var BABYLON;
             this.REFLECTIONMAP_SKYBOX = false;
             this.REFLECTIONMAP_EXPLICIT = false;
             this.REFLECTIONMAP_EQUIRECTANGULAR = false;
+            this.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
             this.INVERTCUBICMAP = false;
             this.LOGARITHMICDEPTH = false;
             this._keys = Object.keys(this);
@@ -382,6 +383,9 @@ var BABYLON;
                             case BABYLON.Texture.EQUIRECTANGULAR_MODE:
                                 this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
                                 break;
+                            case BABYLON.Texture.FIXED_EQUIRECTANGULAR_MODE:
+                                this._defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = true;
+                                break;
                         }
                     }
                 }

+ 4 - 0
src/Materials/babylon.standardMaterial.ts

@@ -94,6 +94,7 @@
         public REFLECTIONMAP_SKYBOX = false;
         public REFLECTIONMAP_EXPLICIT = false;
         public REFLECTIONMAP_EQUIRECTANGULAR = false;
+        public REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
         public INVERTCUBICMAP = false;
         public LOGARITHMICDEPTH = false;
 
@@ -440,6 +441,9 @@
                             case Texture.EQUIRECTANGULAR_MODE:
                                 this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
                                 break;
+                            case Texture.FIXED_EQUIRECTANGULAR_MODE:
+                                this._defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = true;
+                                break;
                         }
                     }
                 }

+ 85 - 123
src/Math/babylon.math.js

@@ -1323,27 +1323,31 @@ var BABYLON;
             return this;
         };
         Quaternion.prototype.toRotationMatrix = function (result) {
-            var x = this.x, y = this.y, z = this.z, w = this.w;
-            var x2 = x + x, y2 = y + y, z2 = z + z;
-            var xx = x * x2, xy = x * y2, xz = x * z2;
-            var yy = y * y2, yz = y * z2, zz = z * z2;
-            var wx = w * x2, wy = w * y2, wz = w * z2;
-            result.m[0] = 1 - (yy + zz);
-            result.m[4] = xy - wz;
-            result.m[8] = xz + wy;
-            result.m[1] = xy + wz;
-            result.m[5] = 1 - (xx + zz);
-            result.m[9] = yz - wx;
-            result.m[2] = xz - wy;
-            result.m[6] = yz + wx;
-            result.m[10] = 1 - (xx + yy);
+            var xx = this.x * this.x;
+            var yy = this.y * this.y;
+            var zz = this.z * this.z;
+            var xy = this.x * this.y;
+            var zw = this.z * this.w;
+            var zx = this.z * this.x;
+            var yw = this.y * this.w;
+            var yz = this.y * this.z;
+            var xw = this.x * this.w;
+            result.m[0] = 1.0 - (2.0 * (yy + zz));
+            result.m[1] = 2.0 * (xy + zw);
+            result.m[2] = 2.0 * (zx - yw);
             result.m[3] = 0;
+            result.m[4] = 2.0 * (xy - zw);
+            result.m[5] = 1.0 - (2.0 * (zz + xx));
+            result.m[6] = 2.0 * (yz + xw);
             result.m[7] = 0;
+            result.m[8] = 2.0 * (zx + yw);
+            result.m[9] = 2.0 * (yz - xw);
+            result.m[10] = 1.0 - (2.0 * (yy + xx));
             result.m[11] = 0;
             result.m[12] = 0;
             result.m[13] = 0;
             result.m[14] = 0;
-            result.m[15] = 1;
+            result.m[15] = 1.0;
             return this;
         };
         Quaternion.prototype.fromRotationMatrix = function (matrix) {
@@ -1357,46 +1361,40 @@ var BABYLON;
             return result;
         };
         Quaternion.FromRotationMatrixToRef = function (matrix, result) {
-            var absQ = Math.pow(matrix.determinant(), 1.0 / 3.0);
-            result.w = Math.sqrt(Math.max(0, absQ + matrix.m[0] + matrix.m[5] + matrix.m[10])) / 2;
-            result.x = Math.sqrt(Math.max(0, absQ + matrix.m[0] - matrix.m[5] - matrix.m[10])) / 2;
-            result.y = Math.sqrt(Math.max(0, absQ - matrix.m[0] + matrix.m[5] - matrix.m[10])) / 2;
-            result.z = Math.sqrt(Math.max(0, absQ - matrix.m[0] - matrix.m[5] + matrix.m[10])) / 2;
-            result.x = (matrix.m[6] - matrix.m[9]) < 0 ? -Math.abs(result.x) : Math.abs(result.x);
-            result.y = (matrix.m[8] - matrix.m[2]) < 0 ? -Math.abs(result.y) : Math.abs(result.y);
-            result.z = (matrix.m[1] - matrix.m[4]) < 0 ? -Math.abs(result.z) : Math.abs(result.z);
-            result.normalize();
-            //var data = matrix.m;
-            //var m11 = data[0], m12 = data[4], m13 = data[8];
-            //var m21 = data[1], m22 = data[5], m23 = data[9];
-            //var m31 = data[2], m32 = data[6], m33 = data[10];
-            //var trace = m11 + m22 + m33;
-            //var s;
-            //if (trace > 0) {
-            //    s = 0.5 / Math.sqrt(trace + 1.0);
-            //    result.w = 0.25 / s;
-            //    result.x = (m32 - m23) * s;
-            //    result.y = (m13 - m31) * s;
-            //    result.z = (m21 - m12) * s;
-            //} else if (m11 > m22 && m11 > m33) {
-            //    s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
-            //    result.w = (m32 - m23) / s;
-            //    result.x = 0.25 * s;
-            //    result.y = (m12 + m21) / s;
-            //    result.z = (m13 + m31) / s;
-            //} else if (m22 > m33) {
-            //    s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
-            //    result.w = (m13 - m31) / s;
-            //    result.x = (m12 + m21) / s;
-            //    result.y = 0.25 * s;
-            //    result.z = (m23 + m32) / s;
-            //} else {
-            //    s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
-            //    result.w = (m21 - m12) / s;
-            //    result.x = (m13 + m31) / s;
-            //    result.y = (m23 + m32) / s;
-            //    result.z = 0.25 * s;
-            //}
+            var data = matrix.m;
+            var m11 = data[0], m12 = data[4], m13 = data[8];
+            var m21 = data[1], m22 = data[5], m23 = data[9];
+            var m31 = data[2], m32 = data[6], m33 = data[10];
+            var trace = m11 + m22 + m33;
+            var s;
+            if (trace > 0) {
+                s = 0.5 / Math.sqrt(trace + 1.0);
+                result.w = 0.25 / s;
+                result.x = (m32 - m23) * s;
+                result.y = (m13 - m31) * s;
+                result.z = (m21 - m12) * s;
+            }
+            else if (m11 > m22 && m11 > m33) {
+                s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
+                result.w = (m32 - m23) / s;
+                result.x = 0.25 * s;
+                result.y = (m12 + m21) / s;
+                result.z = (m13 + m31) / s;
+            }
+            else if (m22 > m33) {
+                s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
+                result.w = (m13 - m31) / s;
+                result.x = (m12 + m21) / s;
+                result.y = 0.25 * s;
+                result.z = (m23 + m32) / s;
+            }
+            else {
+                s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
+                result.w = (m21 - m12) / s;
+                result.x = (m13 + m31) / s;
+                result.y = (m23 + m32) / s;
+                result.z = 0.25 * s;
+            }
         };
         Quaternion.Inverse = function (q) {
             return new Quaternion(-q.x, -q.y, -q.z, q.w);
@@ -1456,63 +1454,27 @@ var BABYLON;
             result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
             result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
         };
-        // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
         Quaternion.Slerp = function (left, right, amount) {
-            var cosHalfTheta = left.w * right.w + left.x * right.x + left.y * right.y + left.z * right.z;
-            var result = new Quaternion();
-            if (cosHalfTheta < 0) {
-                result.w = -right.w;
-                result.x = -right.x;
-                result.y = -right.y;
-                result.z = -right.z;
-                cosHalfTheta = -cosHalfTheta;
+            var num2;
+            var num3;
+            var num = amount;
+            var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
+            var flag = false;
+            if (num4 < 0) {
+                flag = true;
+                num4 = -num4;
+            }
+            if (num4 > 0.999999) {
+                num3 = 1 - num;
+                num2 = flag ? -num : num;
             }
             else {
-                result.copyFrom(right);
-            }
-            if (Math.abs(cosHalfTheta) >= 1.0) {
-                result.w = left.w;
-                result.x = left.x;
-                result.y = left.y;
-                result.z = left.z;
-                return result;
-            }
-            var halfTheta = Math.acos(cosHalfTheta);
-            var sinHalfTheta = Math.sqrt(1.0 - cosHalfTheta * cosHalfTheta);
-            if (Math.abs(sinHalfTheta) < 0.001) {
-                result.w = 0.5 * (left.w + result.w);
-                result.x = 0.5 * (left.x + result.x);
-                result.y = 0.5 * (left.y + result.y);
-                result.z = 0.5 * (left.z + result.z);
-                return result;
-            }
-            var ratioA = Math.sin((1 - amount) * halfTheta) / sinHalfTheta;
-            var ratioB = Math.sin(amount * halfTheta) / sinHalfTheta;
-            result.w = (left.w * ratioA + result.w * ratioB);
-            result.x = (left.x * ratioA + result.x * ratioB);
-            result.y = (left.y * ratioA + result.y * ratioB);
-            result.z = (left.z * ratioA + result.z * ratioB);
-            return result;
-            //var num2;
-            //var num3;
-            //var num = amount;
-            //var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
-            //var flag = false;
-            //if (num4 < 0) {
-            //    flag = true;
-            //    num4 = -num4;
-            //}
-            //if (num4 > 0.999999) {
-            //    num3 = 1 - num;
-            //    num2 = flag ? -num : num;
-            //}
-            //else {
-            //    var num5 = Math.acos(num4);
-            //    var num6 = (1.0 / Math.sin(num5));
-            //    num3 = (Math.sin((1.0 - num) * num5)) * num6;
-            //    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));
+                var num5 = Math.acos(num4);
+                var num6 = (1.0 / Math.sin(num5));
+                num3 = (Math.sin((1.0 - num) * num5)) * num6;
+                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 Quaternion;
     })();
@@ -1963,6 +1925,20 @@ var BABYLON;
         Matrix.TranslationToRef = function (x, y, z, result) {
             Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, x, y, z, 1.0, result);
         };
+        Matrix.Lerp = function (startValue, endValue, gradient) {
+            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 = Vector3.Lerp(startScale, endScale, gradient);
+            var resultRotation = Quaternion.Slerp(startRotation, endRotation, gradient);
+            var resultTranslation = Vector3.Lerp(startTranslation, endTranslation, gradient);
+            return Matrix.Compose(resultScale, resultRotation, resultTranslation);
+        };
         Matrix.LookAtLH = function (eye, target, up) {
             var result = Matrix.Zero();
             Matrix.LookAtLHToRef(eye, target, up, result);
@@ -2131,20 +2107,6 @@ var BABYLON;
             result.m[14] = temp3 * plane.d;
             result.m[15] = 1.0;
         };
-        Matrix.Lerp = function (startValue, endValue, gradient) {
-            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 = Vector3.Lerp(startScale, endScale, gradient);
-            var resultRotation = Quaternion.Slerp(startRotation, endRotation, gradient);
-            var resultTranslation = Vector3.Lerp(startTranslation, endTranslation, gradient);
-            return Matrix.Compose(resultScale, resultRotation, resultTranslation);
-        };
         Matrix._tempQuaternion = new Quaternion();
         Matrix._xAxis = Vector3.Zero();
         Matrix._yAxis = Vector3.Zero();

+ 86 - 144
src/Math/babylon.math.ts

@@ -1637,32 +1637,32 @@
         }
 
         public toRotationMatrix(result: Matrix): Quaternion {
-            var x = this.x, y = this.y, z = this.z, w = this.w;
-            var x2 = x + x, y2 = y + y, z2 = z + z;
-            var xx = x * x2, xy = x * y2, xz = x * z2;
-            var yy = y * y2, yz = y * z2, zz = z * z2;
-            var wx = w * x2, wy = w * y2, wz = w * z2; 
- 
-            result.m[0] = 1 - (yy + zz);
-            result.m[4] = xy - wz;
-            result.m[8] = xz + wy;
-            
-            result.m[1] = xy + wz;
-            result.m[5] = 1 - (xx + zz);
-            result.m[9] = yz - wx;
-            
-            result.m[2] = xz - wy;
-            result.m[6] = yz + wx;
-            result.m[10] = 1 - (xx + yy);
-            
+            var xx = this.x * this.x;
+            var yy = this.y * this.y;
+            var zz = this.z * this.z;
+            var xy = this.x * this.y;
+            var zw = this.z * this.w;
+            var zx = this.z * this.x;
+            var yw = this.y * this.w;
+            var yz = this.y * this.z;
+            var xw = this.x * this.w;
+
+            result.m[0] = 1.0 - (2.0 * (yy + zz));
+            result.m[1] = 2.0 * (xy + zw);
+            result.m[2] = 2.0 * (zx - yw);
             result.m[3] = 0;
+            result.m[4] = 2.0 * (xy - zw);
+            result.m[5] = 1.0 - (2.0 * (zz + xx));
+            result.m[6] = 2.0 * (yz + xw);
             result.m[7] = 0;
+            result.m[8] = 2.0 * (zx + yw);
+            result.m[9] = 2.0 * (yz - xw);
+            result.m[10] = 1.0 - (2.0 * (yy + xx));
             result.m[11] = 0;
-            
             result.m[12] = 0;
             result.m[13] = 0;
             result.m[14] = 0;
-            result.m[15] = 1; 
+            result.m[15] = 1.0;
 
             return this;
         }
@@ -1681,57 +1681,46 @@
         }
 
         public static FromRotationMatrixToRef(matrix: Matrix, result: Quaternion): void {
+            var data = matrix.m;
+            var m11 = data[0], m12 = data[4], m13 = data[8];
+            var m21 = data[1], m22 = data[5], m23 = data[9];
+            var m31 = data[2], m32 = data[6], m33 = data[10];
+            var trace = m11 + m22 + m33;
+            var s;
 
-            var absQ = Math.pow(matrix.determinant(), 1.0 / 3.0);
-            result.w = Math.sqrt(Math.max(0, absQ + matrix.m[0] + matrix.m[5] +matrix.m[10])) / 2;
-            result.x = Math.sqrt(Math.max(0, absQ + matrix.m[0] - matrix.m[5] -matrix.m[10])) / 2;
-            result.y = Math.sqrt(Math.max(0, absQ - matrix.m[0] + matrix.m[5] -matrix.m[10])) / 2;
-            result.z = Math.sqrt(Math.max(0, absQ - matrix.m[0] - matrix.m[5] + matrix.m[10])) / 2;
-            result.x = (matrix.m[6] - matrix.m[9]) < 0 ? -Math.abs(result.x) : Math.abs(result.x);
-            result.y = (matrix.m[8] - matrix.m[2]) < 0 ? -Math.abs(result.y) : Math.abs(result.y);
-            result.z = (matrix.m[1] - matrix.m[4]) < 0 ? -Math.abs(result.z) : Math.abs(result.z);
-            result.normalize(); 
-
-            //var data = matrix.m;
-            //var m11 = data[0], m12 = data[4], m13 = data[8];
-            //var m21 = data[1], m22 = data[5], m23 = data[9];
-            //var m31 = data[2], m32 = data[6], m33 = data[10];
-            //var trace = m11 + m22 + m33;
-            //var s;
+            if (trace > 0) {
 
-            //if (trace > 0) {
+                s = 0.5 / Math.sqrt(trace + 1.0);
 
-            //    s = 0.5 / Math.sqrt(trace + 1.0);
+                result.w = 0.25 / s;
+                result.x = (m32 - m23) * s;
+                result.y = (m13 - m31) * s;
+                result.z = (m21 - m12) * s;
+            } else if (m11 > m22 && m11 > m33) {
 
-            //    result.w = 0.25 / s;
-            //    result.x = (m32 - m23) * s;
-            //    result.y = (m13 - m31) * s;
-            //    result.z = (m21 - m12) * s;
-            //} else if (m11 > m22 && m11 > m33) {
+                s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
 
-            //    s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
+                result.w = (m32 - m23) / s;
+                result.x = 0.25 * s;
+                result.y = (m12 + m21) / s;
+                result.z = (m13 + m31) / s;
+            } else if (m22 > m33) {
 
-            //    result.w = (m32 - m23) / s;
-            //    result.x = 0.25 * s;
-            //    result.y = (m12 + m21) / s;
-            //    result.z = (m13 + m31) / s;
-            //} else if (m22 > m33) {
+                s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
 
-            //    s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
-
-            //    result.w = (m13 - m31) / s;
-            //    result.x = (m12 + m21) / s;
-            //    result.y = 0.25 * s;
-            //    result.z = (m23 + m32) / s;
-            //} else {
+                result.w = (m13 - m31) / s;
+                result.x = (m12 + m21) / s;
+                result.y = 0.25 * s;
+                result.z = (m23 + m32) / s;
+            } else {
 
-            //    s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
+                s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
 
-            //    result.w = (m21 - m12) / s;
-            //    result.x = (m13 + m31) / s;
-            //    result.y = (m23 + m32) / s;
-            //    result.z = 0.25 * s;
-            //}
+                result.w = (m21 - m12) / s;
+                result.x = (m13 + m31) / s;
+                result.y = (m23 + m32) / s;
+                result.z = 0.25 * s;
+            }
         }
 
         public static Inverse(q: Quaternion): Quaternion {
@@ -1809,77 +1798,30 @@
             result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
         }
 
-        // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
         public static Slerp(left: Quaternion, right: Quaternion, amount: number): Quaternion {
-            var cosHalfTheta = left.w * right.w + left.x * right.x + left.y * right.y + left.z * right.z;
-            var result = new Quaternion();
+            var num2;
+            var num3;
+            var num = amount;
+            var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
+            var flag = false;
 
-            if (cosHalfTheta < 0) {
-                result.w = -right.w;
-                result.x = -right.x;
-                result.y = -right.y;
-                result.z = -right.z;
-
-                cosHalfTheta = -cosHalfTheta;
-            } else {
-                result.copyFrom(right);
+            if (num4 < 0) {
+                flag = true;
+                num4 = -num4;
             }
 
-           	if (Math.abs(cosHalfTheta) >= 1.0) {
-                result.w = left.w;
-                result.x = left.x;
-                result.y = left.y;
-                result.z = left.z;
-
-                return result;
+            if (num4 > 0.999999) {
+                num3 = 1 - num;
+                num2 = flag ? -num : num;
             }
-
-            var halfTheta = Math.acos(cosHalfTheta);
-            var sinHalfTheta = Math.sqrt(1.0 - cosHalfTheta * cosHalfTheta);
-
-            if (Math.abs(sinHalfTheta) < 0.001) {
-                result.w = 0.5 * (left.w + result.w);
-                result.x = 0.5 * (left.x + result.x);
-                result.y = 0.5 * (left.y + result.y);
-                result.z = 0.5 * (left.z + result.z);
-
-                return result;
+            else {
+                var num5 = Math.acos(num4);
+                var num6 = (1.0 / Math.sin(num5));
+                num3 = (Math.sin((1.0 - num) * num5)) * num6;
+                num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
             }
 
-            var ratioA = Math.sin((1 - amount) * halfTheta) / sinHalfTheta;
-            var ratioB = Math.sin(amount * halfTheta) / sinHalfTheta;
-
-            result.w = (left.w * ratioA + result.w * ratioB);
-            result.x = (left.x * ratioA + result.x * ratioB);
-            result.y = (left.y * ratioA + result.y * ratioB);
-            result.z = (left.z * ratioA + result.z * ratioB);
-
-
-            return result;
-
-            //var num2;
-            //var num3;
-            //var num = amount;
-            //var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
-            //var flag = false;
-
-            //if (num4 < 0) {
-            //    flag = true;
-            //    num4 = -num4;
-            //}
-
-            //if (num4 > 0.999999) {
-            //    num3 = 1 - num;
-            //    num2 = flag ? -num : num;
-            //}
-            //else {
-            //    var num5 = Math.acos(num4);
-            //    var num6 = (1.0 / Math.sin(num5));
-            //    num3 = (Math.sin((1.0 - num) * num5)) * num6;
-            //    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));
         }
     }
 
@@ -2471,6 +2413,24 @@
                 x, y, z, 1.0, result);
         }
 
+        public static Lerp(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 = Vector3.Lerp(startScale, endScale, gradient);
+            var resultRotation = Quaternion.Slerp(startRotation, endRotation, gradient);
+            var resultTranslation = Vector3.Lerp(startTranslation, endTranslation, gradient);
+
+            return Matrix.Compose(resultScale, resultRotation, resultTranslation);
+        }    
+
         public static LookAtLH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
             var result = Matrix.Zero();
 
@@ -2690,24 +2650,6 @@
             result.m[14] = temp3 * plane.d;
             result.m[15] = 1.0;
         }
-
-        public static Lerp(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 = Vector3.Lerp(startScale, endScale, gradient);
-            var resultRotation = Quaternion.Slerp(startRotation, endRotation, gradient);
-            var resultTranslation = Vector3.Lerp(startTranslation, endTranslation, gradient);
-
-            return Matrix.Compose(resultScale, resultRotation, resultTranslation);
-        }
     }
 
     export class Plane {

+ 1 - 1
src/Mesh/babylon.mesh.vertexData.ts

@@ -2099,4 +2099,4 @@
             }
         }
     }
-}
+}

+ 12 - 2
src/Shaders/default.fragment.fx

@@ -196,7 +196,7 @@ uniform sampler2D reflection2DSampler;
 #ifdef REFLECTIONMAP_SKYBOX
 varying vec3 vPositionUVW;
 #else
-#ifdef REFLECTIONMAP_EQUIRECTANGULAR
+#ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED
 varying vec3 vDirectionW;
 #endif
 
@@ -210,7 +210,7 @@ uniform mat4 view;
 
 vec3 computeReflectionCoords(vec4 worldPos, vec3 worldNormal)
 {
-#ifdef REFLECTIONMAP_EQUIRECTANGULAR
+#ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED
 	vec3 direction = normalize(vDirectionW);
 
 	float t = clamp(direction.y * -0.5 + 0.5, 0., 1.0);
@@ -219,6 +219,16 @@ vec3 computeReflectionCoords(vec4 worldPos, vec3 worldNormal)
 	return vec3(s, t, 0);
 #endif
 
+#ifdef REFLECTIONMAP_EQUIRECTANGULAR
+
+	vec3 cameraToVertex = normalize(worldPos.xyz - vEyePosition);
+	vec3 r = reflect(cameraToVertex, worldNormal);
+	float t = clamp(r.y * -0.5 + 0.5, 0., 1.0);
+	float s = atan(r.z, r.x) * RECIPROCAL_PI2 + 0.5;
+
+	return vec3(s, t, 0);
+#endif
+
 #ifdef REFLECTIONMAP_SPHERICAL
 	vec3 viewDir = normalize(vec3(view * worldPos));
 	vec3 viewNormal = normalize(vec3(view * vec4(worldNormal, 0.0)));

+ 2 - 2
src/Shaders/default.vertex.fx

@@ -128,7 +128,7 @@ varying vec4 vPositionFromLight3;
 varying vec3 vPositionUVW;
 #endif
 
-#ifdef REFLECTIONMAP_EQUIRECTANGULAR
+#ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED
 varying vec3 vDirectionW;
 #endif
 
@@ -186,7 +186,7 @@ void main(void) {
 	vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
 #endif
 
-#ifdef REFLECTIONMAP_EQUIRECTANGULAR
+#ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED
 	vDirectionW = normalize(vec3(finalWorld * vec4(position, 0.0)));
 #endif