Преглед изворни кода

Auto-Rotation update

if quaternion is set, rotation can still be used (only as a setter,
getting it won't work, probably.).
Raanan Weber пре 9 година
родитељ
комит
1db4607c7f
4 измењених фајлова са 53 додато и 13 уклоњено
  1. 13 0
      src/Mesh/babylon.abstractMesh.js
  2. 20 11
      src/Mesh/babylon.abstractMesh.ts
  3. 10 2
      src/Mesh/babylon.mesh.js
  4. 10 0
      src/Mesh/babylon.mesh.ts

+ 13 - 0
src/Mesh/babylon.abstractMesh.js

@@ -177,6 +177,19 @@ var BABYLON;
             enumerable: true,
             configurable: true
         });
+        Object.defineProperty(AbstractMesh.prototype, "rotationQuaternion", {
+            get: function () {
+                return this._rotationQuaternion;
+            },
+            set: function (quaternion) {
+                this._rotationQuaternion = quaternion;
+                if (this.rotation.length()) {
+                    this.rotation.copyFromFloats(0, 0, 0);
+                }
+            },
+            enumerable: true,
+            configurable: true
+        });
         // Methods
         AbstractMesh.prototype.updatePoseMatrix = function (matrix) {
             this._poseMatrix.copyFrom(matrix);

+ 20 - 11
src/Mesh/babylon.abstractMesh.ts

@@ -31,7 +31,7 @@
         public definedFacingForward = true; // orientation for POV movement & rotation
         public position = new Vector3(0, 0, 0);
         private _rotation = new Vector3(0, 0, 0);
-        public rotationQuaternion: Quaternion;
+        public _rotationQuaternion: Quaternion;
         private _scaling = new Vector3(1, 1, 1);
         public billboardMode = AbstractMesh.BILLBOARDMODE_NONE;
         public visibility = 1.0;
@@ -156,23 +156,20 @@
             scene.addMesh(this);
         }
 
+        /**
+         * Getting the rotation object. 
+         * If rotation quaternion is set, the euler angels of the quaternion will be sent back.
+         * This is not 100% accurate. Avoid using rotation if the wuaternion is set.
+         */
         public get rotation(): Vector3 {
             if (this.rotationQuaternion) {
-                Tools.Warn("Quaternion rotation is used, the rotation value is probably wrong");
+                return this._rotationQuaternion.toEulerAngles();
             }
             return this._rotation;
         }
-
+        
         public set rotation(newRotation: Vector3) {
             this._rotation = newRotation;
-            //check if rotationQuaternion exists, and if it does - update it!
-            if (this.rotationQuaternion) {
-                var len = this._rotation.length();
-                if (len) {
-                    this.rotationQuaternion.multiplyInPlace(BABYLON.Quaternion.RotationYawPitchRoll(this._rotation.y, this._rotation.x, this._rotation.z))
-                    this._rotation.copyFromFloats(0, 0, 0);
-                }
-            }
         }
 
         public get scaling(): Vector3 {
@@ -185,6 +182,18 @@
                 this.physicImpostor.forceUpdate();
             }
         }
+        
+        public get rotationQuaternion() {
+            return this._rotationQuaternion;
+        }
+        
+        public set rotationQuaternion(quaternion: Quaternion) {
+            this._rotationQuaternion = quaternion;
+            //reset the rotation vector.
+            if(this.rotation.length()) {
+                this.rotation.copyFromFloats(0,0,0);
+            }
+        }
 
         // Methods
         public updatePoseMatrix(matrix: Matrix) {

+ 10 - 2
src/Mesh/babylon.mesh.js

@@ -12,7 +12,7 @@ var BABYLON;
             this.renderSelf = new Array();
         }
         return _InstancesBatch;
-    })();
+    }());
     BABYLON._InstancesBatch = _InstancesBatch;
     var Mesh = (function (_super) {
         __extends(Mesh, _super);
@@ -636,6 +636,14 @@ var BABYLON;
             if (!this._geometry || !this._geometry.getVertexBuffers() || !this._geometry.getIndexBuffer()) {
                 return;
             }
+            //rotate, if quaternion is set and rotation was used
+            if (this.rotationQuaternion) {
+                var len = this.rotation.length();
+                if (len) {
+                    this.rotationQuaternion.multiplyInPlace(BABYLON.Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z));
+                    this.rotation.copyFromFloats(0, 0, 0);
+                }
+            }
             var callbackIndex;
             for (callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
                 this._onBeforeRenderCallbacks[callbackIndex](this);
@@ -1707,6 +1715,6 @@ var BABYLON;
         Mesh._CAP_END = 2;
         Mesh._CAP_ALL = 3;
         return Mesh;
-    })(BABYLON.AbstractMesh);
+    }(BABYLON.AbstractMesh));
     BABYLON.Mesh = Mesh;
 })(BABYLON || (BABYLON = {}));

+ 10 - 0
src/Mesh/babylon.mesh.ts

@@ -721,6 +721,16 @@
             if (!this._geometry || !this._geometry.getVertexBuffers() || !this._geometry.getIndexBuffer()) {
                 return;
             }
+            
+            //rotate, if quaternion is set and rotation was used
+            if (this.rotationQuaternion) {
+                var len = this.rotation.length();
+                if (len) {
+                    this.rotationQuaternion.multiplyInPlace(BABYLON.Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z))
+                    this.rotation.copyFromFloats(0, 0, 0);
+                }
+            }
+
             var callbackIndex: number;
             for (callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
                 this._onBeforeRenderCallbacks[callbackIndex](this);