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

use existing rotation if rotation quaternion doesnt exist, move scaling removal logic outside of Math class

Trevor Baron 7 лет назад
Родитель
Сommit
6faffa14ba
3 измененных файлов с 30 добавлено и 27 удалено
  1. 16 2
      src/Gizmos/babylon.gizmo.ts
  2. 10 4
      src/Gizmos/babylon.planeRotationGizmo.ts
  3. 4 21
      src/Math/babylon.math.ts

+ 16 - 2
src/Gizmos/babylon.gizmo.ts

@@ -51,9 +51,23 @@ module BABYLON {
                 if(this.attachedMesh){
                     if(this.updateGizmoRotationToMatchAttachedMesh){
                         if(!this._rootMesh.rotationQuaternion){
-                            this._rootMesh.rotationQuaternion = new BABYLON.Quaternion();
+                            this._rootMesh.rotationQuaternion = Quaternion.RotationYawPitchRoll(this._rootMesh.rotation.y, this._rootMesh.rotation.x, this._rootMesh.rotation.z);
                         }
-                        this.attachedMesh.getWorldMatrix().getRotationMatrixToRef(this._tmpMatrix, true);
+
+                        // Remove scaling before getting rotation matrix to get rotation matrix unmodified by scale
+                        tempVector.copyFrom(this.attachedMesh.scaling);
+                        if(this.attachedMesh.scaling.x < 0){
+                            this.attachedMesh.scaling.x *= -1;
+                        }
+                        if(this.attachedMesh.scaling.y < 0){
+                            this.attachedMesh.scaling.y *= -1;
+                        }
+                        if(this.attachedMesh.scaling.z < 0){
+                            this.attachedMesh.scaling.z *= -1;
+                        }
+                        this.attachedMesh.computeWorldMatrix().getRotationMatrixToRef(this._tmpMatrix);
+                        this.attachedMesh.scaling.copyFrom(tempVector);
+                        this.attachedMesh.computeWorldMatrix();
                         Quaternion.FromRotationMatrixToRef(this._tmpMatrix, this._rootMesh.rotationQuaternion);
                     }
                     if(this.updateGizmoPositionToMatchAttachedMesh){

+ 10 - 4
src/Gizmos/babylon.planeRotationGizmo.ts

@@ -52,7 +52,7 @@ module BABYLON {
             // Add drag behavior to handle events when the gizmo is dragged
             this.dragBehavior = new PointerDragBehavior({dragPlaneNormal: planeNormal});
             this.dragBehavior.moveAttached = false;
-            this.dragBehavior.maxDragAngle =  Math.PI*4/10;
+            this.dragBehavior.maxDragAngle =  Math.PI*9/20;
             this.dragBehavior._useAlternatePickedPointAboveMaxDragAngle = true;
             this._rootMesh.addBehavior(this.dragBehavior);
 
@@ -73,7 +73,7 @@ module BABYLON {
             this.dragBehavior.onDragObservable.add((event)=>{
                 if(this.attachedMesh){
                     if(!this.attachedMesh.rotationQuaternion){
-                        this.attachedMesh.rotationQuaternion = new BABYLON.Quaternion();
+                        this.attachedMesh.rotationQuaternion = Quaternion.RotationYawPitchRoll(this.attachedMesh.rotation.y, this.attachedMesh.rotation.x, this.attachedMesh.rotation.z);
                     }
                     // Calc angle over full 360 degree (https://stackoverflow.com/questions/43493711/the-angle-between-two-3d-vectors-with-a-result-range-0-360)
                     var newVector = event.dragPlanePoint.subtract(this.attachedMesh.position).normalize();
@@ -115,8 +115,14 @@ module BABYLON {
                      var quaternionCoefficient = Math.sin(angle/2)
                      var amountToRotate = new BABYLON.Quaternion(planeNormalTowardsCamera.x*quaternionCoefficient,planeNormalTowardsCamera.y*quaternionCoefficient,planeNormalTowardsCamera.z*quaternionCoefficient,Math.cos(angle/2));
 
-                     // Rotate selected mesh quaternion over fixed axis
-                     this.attachedMesh.rotationQuaternion.multiplyToRef(amountToRotate,this.attachedMesh.rotationQuaternion);
+                     if(this.updateGizmoRotationToMatchAttachedMesh){
+                        // Rotate selected mesh quaternion over fixed axis
+                        this.attachedMesh.rotationQuaternion.multiplyToRef(amountToRotate,this.attachedMesh.rotationQuaternion);
+                     }else{
+                         // Rotate selected mesh quaternion over rotated axis
+                        amountToRotate.multiplyToRef(this.attachedMesh.rotationQuaternion,this.attachedMesh.rotationQuaternion);
+                     }
+                     
 
                     lastDragPosition.copyFrom(event.dragPlanePoint);
                     if(snapped){

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

@@ -4554,12 +4554,11 @@
 
         /**
          * 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(ignoreScaling = false): Matrix {
+        public getRotationMatrix(): Matrix {
             var result = Matrix.Identity();
-            this.getRotationMatrixToRef(result, ignoreScaling);
+            this.getRotationMatrixToRef(result);
             return result;
         }
 
@@ -4569,24 +4568,8 @@
          * @param result defines the target matrix to store data to
          * @returns the current matrix  
          */
-        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]);
-            }
-            
+        public getRotationMatrixToRef(result: Matrix): Matrix {
+            var matrix:Matrix = this;            
             var m = matrix.m;
 
             var sx = Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);