فهرست منبع

avoid undesired behavior when scaling gizmo inverts a meshes scale

Trevor Baron 7 سال پیش
والد
کامیت
52e538a6b9
3فایلهای تغییر یافته به همراه27 افزوده شده و 20 حذف شده
  1. 1 15
      src/Gizmos/babylon.axisScaleGizmo.ts
  2. 3 1
      src/Gizmos/babylon.gizmo.ts
  3. 23 4
      src/Math/babylon.math.ts

+ 1 - 15
src/Gizmos/babylon.axisScaleGizmo.ts

@@ -80,21 +80,7 @@ module BABYLON {
                         }
                     }
                     
-                    var invertCount = 0;
-                    if(this.attachedMesh.scaling["x"] < 0){
-                        invertCount++;
-                    }
-                    if(this.attachedMesh.scaling["y"] < 0){
-                        invertCount++;
-                    }
-                    if(this.attachedMesh.scaling["z"] < 0){
-                        invertCount++;
-                    }
-                    if(invertCount % 2 == 0){
-                        this.attachedMesh.scaling.addInPlace(tmpVector);
-                    }else{
-                        this.attachedMesh.scaling.subtractInPlace(tmpVector);
-                    }
+                    this.attachedMesh.scaling.addInPlace(tmpVector);
 
                     if(snapped){
                         tmpSnapEvent.snapDistance = this.snapDistance*dragSteps;

+ 3 - 1
src/Gizmos/babylon.gizmo.ts

@@ -9,6 +9,7 @@ module BABYLON {
         protected _rootMesh:Mesh;
         private _attachedMesh:Nullable<AbstractMesh>;
         private _scaleFactor = 3;
+        private _tmpMatrix = new Matrix();
         /**
          * Mesh that the gizmo will be attached to. (eg. on a drag gizmo the mesh that will be dragged)
          * * When set, interactions will be enabled
@@ -52,7 +53,8 @@ module BABYLON {
                         if(!this._rootMesh.rotationQuaternion){
                             this._rootMesh.rotationQuaternion = new BABYLON.Quaternion();
                         }
-                        Quaternion.FromRotationMatrixToRef(this.attachedMesh.getWorldMatrix().getRotationMatrix(), this._rootMesh.rotationQuaternion);
+                        this.attachedMesh.getWorldMatrix().getRotationMatrixToRef(this._tmpMatrix, true);
+                        Quaternion.FromRotationMatrixToRef(this._tmpMatrix, this._rootMesh.rotationQuaternion);
                     }
                     if(this.updateGizmoPositionToMatchAttachedMesh){
                         this._rootMesh.position.copyFrom(this.attachedMesh.absolutePosition);

+ 23 - 4
src/Math/babylon.math.ts

@@ -4554,21 +4554,40 @@
 
         /**
          * Gets only rotation part of the current matrix
+         * @param ignoreScaling when set, do not let negative scale matrix values modify the rotation matrix (Default: false)
          * @returns a new matrix sets to the extracted rotation matrix from the current one
          */
-        public getRotationMatrix(): Matrix {
+        public getRotationMatrix(ignoreScaling = false): Matrix {
             var result = Matrix.Identity();
-            this.getRotationMatrixToRef(result);
+            this.getRotationMatrixToRef(result, ignoreScaling);
             return result;
         }
 
         /**
          * Extracts the rotation matrix from the current one and sets it as the given "result"  
+         * @param ignoreScaling when set, do not let negative scale matrix values modify the rotation matrix (Default: false)
          * @param result defines the target matrix to store data to
          * @returns the current matrix  
          */
-        public getRotationMatrixToRef(result: Matrix): Matrix {
-            var m = this.m;
+        public getRotationMatrixToRef(result: Matrix, ignoreScaling = false): Matrix {
+            var matrix:Matrix = this;
+
+            // By default inverting scale causes the rotation matrix, when converted to a quaternion, not be the same as when not inverted
+            if(ignoreScaling){
+                matrix = MathTmp.Matrix[0];
+                matrix.copyFrom(this);
+                matrix.m[0] = Math.abs(matrix.m[0]);
+                matrix.m[1] = Math.abs(matrix.m[1]);
+                matrix.m[2] = Math.abs(matrix.m[2]);
+                matrix.m[4] = Math.abs(matrix.m[4]);
+                matrix.m[5] = Math.abs(matrix.m[5]);
+                matrix.m[6] = Math.abs(matrix.m[6]);
+                matrix.m[8] = Math.abs(matrix.m[8]);
+                matrix.m[9] = Math.abs(matrix.m[9]);
+                matrix.m[10] = Math.abs(matrix.m[10]);
+            }
+            
+            var m = matrix.m;
 
             var sx = Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
             var sy = Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]);