Просмотр исходного кода

Added support for right handed system

David Catuhe 9 лет назад
Родитель
Сommit
dea49de07a

Разница между файлами не показана из-за своего большого размера
+ 22 - 22
dist/preview release/babylon.core.js


Разница между файлами не показана из-за своего большого размера
+ 1492 - 1490
dist/preview release/babylon.d.ts


Разница между файлами не показана из-за своего большого размера
+ 39 - 39
dist/preview release/babylon.js


Разница между файлами не показана из-за своего большого размера
+ 47 - 5
dist/preview release/babylon.max.js


Разница между файлами не показана из-за своего большого размера
+ 38 - 38
dist/preview release/babylon.noworker.js


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

@@ -1,7 +1,7 @@
 # 2.5.0:
 
 ### Major updates
-    
+- Babylon.js now supports right handed system with ```scene.useRightHandedSystem = true``` ([deltakosh](https://github.com/deltakosh))    
 ### Updates
 - Cube textures are now cached by texture cache ([deltakosh](https://github.com/deltakosh)) 
 - Added onAnimationEnd callback for `sprite.playAnimation` ([deltakosh](https://github.com/deltakosh)) 

+ 6 - 1
src/Cameras/babylon.arcRotateCamera.js

@@ -407,7 +407,12 @@ var BABYLON;
                     up = up.clone();
                     up = up.negate();
                 }
-                BABYLON.Matrix.LookAtLHToRef(this.position, target, up, this._viewMatrix);
+                if (this.getScene().useRightHandedSystem) {
+                    BABYLON.Matrix.LookAtRHToRef(this.position, target, up, this._viewMatrix);
+                }
+                else {
+                    BABYLON.Matrix.LookAtLHToRef(this.position, target, up, this._viewMatrix);
+                }
                 this._viewMatrix.m[12] += this.targetScreenOffset.x;
                 this._viewMatrix.m[13] += this.targetScreenOffset.y;
             }

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

@@ -431,7 +431,11 @@
                     up = up.negate();
                 }
 
-                Matrix.LookAtLHToRef(this.position, target, up, this._viewMatrix);
+                if (this.getScene().useRightHandedSystem) {
+                    Matrix.LookAtRHToRef(this.position, target, up, this._viewMatrix);
+                } else {
+                    Matrix.LookAtLHToRef(this.position, target, up, this._viewMatrix);
+                }
                 this._viewMatrix.m[12] += this.targetScreenOffset.x;
                 this._viewMatrix.m[13] += this.targetScreenOffset.y;
             }

+ 6 - 1
src/Cameras/babylon.targetCamera.js

@@ -185,7 +185,12 @@ var BABYLON;
             else {
                 this._currentTarget.copyFrom(this._getLockedTargetPosition());
             }
-            BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
+            if (this.getScene().useRightHandedSystem) {
+                BABYLON.Matrix.LookAtRHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
+            }
+            else {
+                BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
+            }
             return this._viewMatrix;
         };
         /**

+ 6 - 1
src/Cameras/babylon.targetCamera.ts

@@ -227,7 +227,12 @@
                 this._currentTarget.copyFrom(this._getLockedTargetPosition());
             }
 
-            Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
+            if (this.getScene().useRightHandedSystem) {
+                Matrix.LookAtRHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
+            } else {
+                Matrix.LookAtLHToRef(this.position, this._currentTarget, this.upVector, this._viewMatrix);
+            }
+            
             return this._viewMatrix;
         }
 

+ 8 - 2
src/Materials/babylon.material.js

@@ -71,7 +71,6 @@ var BABYLON;
             this.state = "";
             this.alpha = 1.0;
             this.backFaceCulling = true;
-            this.sideOrientation = Material.CounterClockWiseSideOrientation;
             /**
             * An event triggered when the material is disposed.
             * @type {BABYLON.Observable}
@@ -96,6 +95,12 @@ var BABYLON;
             this._fillMode = Material.TriangleFillMode;
             this.id = name;
             this._scene = scene;
+            if (scene.useRightHandedSystem) {
+                this.sideOrientation = Material.ClockWiseSideOrientation;
+            }
+            else {
+                this.sideOrientation = Material.CounterClockWiseSideOrientation;
+            }
             if (!doNotAdd) {
                 scene.materials.push(this);
             }
@@ -231,8 +236,9 @@ var BABYLON;
         };
         Material.prototype._preBind = function () {
             var engine = this._scene.getEngine();
+            var reverse = this.sideOrientation === Material.ClockWiseSideOrientation;
             engine.enableEffect(this._effect);
-            engine.setState(this.backFaceCulling, this.zOffset, false, this.sideOrientation === Material.ClockWiseSideOrientation);
+            engine.setState(this.backFaceCulling, this.zOffset, false, reverse);
         };
         Material.prototype.bind = function (world, mesh) {
             this._scene._cachedMaterial = this;

+ 9 - 2
src/Materials/babylon.material.ts

@@ -114,7 +114,7 @@
         public backFaceCulling = true;
 
         @serialize()
-        public sideOrientation = Material.CounterClockWiseSideOrientation;
+        public sideOrientation: number;
 
         public onCompiled: (effect: Effect) => void;
         public onError: (effect: Effect, errors: string) => void;
@@ -209,6 +209,12 @@
 
             this._scene = scene;
 
+            if (scene.useRightHandedSystem) {
+                this.sideOrientation = Material.ClockWiseSideOrientation;
+            } else {
+                this.sideOrientation = Material.CounterClockWiseSideOrientation;
+            }
+
             if (!doNotAdd) {
                 scene.materials.push(this);
             }
@@ -267,9 +273,10 @@
 
         public _preBind(): void {
             var engine = this._scene.getEngine();
+            var reverse = this.sideOrientation === Material.ClockWiseSideOrientation;
 
             engine.enableEffect(this._effect);
-            engine.setState(this.backFaceCulling, this.zOffset, false, this.sideOrientation === Material.ClockWiseSideOrientation);
+            engine.setState(this.backFaceCulling, this.zOffset, false, reverse);
         }
 
         public bind(world: Matrix, mesh?: Mesh): void {

+ 26 - 0
src/Math/babylon.math.js

@@ -2258,6 +2258,32 @@ var BABYLON;
             var ez = -Vector3.Dot(this._zAxis, eye);
             return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0, this._xAxis.y, this._yAxis.y, this._zAxis.y, 0, this._xAxis.z, this._yAxis.z, this._zAxis.z, 0, ex, ey, ez, 1, result);
         };
+        Matrix.LookAtRH = function (eye, target, up) {
+            var result = Matrix.Zero();
+            Matrix.LookAtRHToRef(eye, target, up, result);
+            return result;
+        };
+        Matrix.LookAtRHToRef = function (eye, target, up, result) {
+            // Z axis
+            eye.subtractToRef(target, this._zAxis);
+            this._zAxis.normalize();
+            // X axis
+            Vector3.CrossToRef(up, this._zAxis, this._xAxis);
+            if (this._xAxis.lengthSquared() === 0) {
+                this._xAxis.x = 1.0;
+            }
+            else {
+                this._xAxis.normalize();
+            }
+            // Y axis
+            Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
+            this._yAxis.normalize();
+            // Eye angles
+            var ex = -Vector3.Dot(this._xAxis, eye);
+            var ey = -Vector3.Dot(this._yAxis, eye);
+            var ez = -Vector3.Dot(this._zAxis, eye);
+            return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0, this._xAxis.y, this._yAxis.y, this._zAxis.y, 0, this._xAxis.z, this._yAxis.z, this._zAxis.z, 0, ex, ey, ez, 1, result);
+        };
         Matrix.OrthoLH = function (width, height, znear, zfar) {
             var matrix = Matrix.Zero();
             Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);

+ 37 - 0
src/Math/babylon.math.ts

@@ -2827,6 +2827,43 @@
                 ex, ey, ez, 1, result);
         }
 
+        public static LookAtRH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
+            var result = Matrix.Zero();
+
+            Matrix.LookAtRHToRef(eye, target, up, result);
+
+            return result;
+        }
+
+        public static LookAtRHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
+            // Z axis
+            eye.subtractToRef(target, this._zAxis);
+            this._zAxis.normalize();
+
+            // X axis
+            Vector3.CrossToRef(up, this._zAxis, this._xAxis);
+
+            if (this._xAxis.lengthSquared() === 0) {
+                this._xAxis.x = 1.0;
+            } else {
+                this._xAxis.normalize();
+            }
+
+            // Y axis
+            Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
+            this._yAxis.normalize();
+
+            // Eye angles
+            var ex = -Vector3.Dot(this._xAxis, eye);
+            var ey = -Vector3.Dot(this._yAxis, eye);
+            var ez = -Vector3.Dot(this._zAxis, eye);
+
+            return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0,
+                this._xAxis.y, this._yAxis.y, this._zAxis.y, 0,
+                this._xAxis.z, this._yAxis.z, this._zAxis.z, 0,
+                ex, ey, ez, 1, result);
+        }
+
         public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
             var matrix = Matrix.Zero();
 

+ 1 - 1
src/babylon.engine.ts

@@ -88,7 +88,7 @@
         engine.resetTextureCache();
         scene._removePendingData(texture);
 
-        texture.onLoadedCallbacks.forEach(function (callback) {
+        texture.onLoadedCallbacks.forEach(callback => {
             callback();
         });
         texture.onLoadedCallbacks = [];