Переглянути джерело

replace device position/rotation to be in babylon space

Trevor Baron 7 роки тому
батько
коміт
15bac0eef9

+ 30 - 18
src/Cameras/VR/babylon.webVRCamera.ts

@@ -52,13 +52,18 @@ module BABYLON {
 
         protected _descendants: Array<Node> = [];
 
+        // _device* represents device position and rotation in room space
+        private _devicePosition = Vector3.Zero();
+        private _deviceRotationQuaternion = Quaternion.Identity(); 
+
+        // device* represents device position and rotation in babylon space
         public devicePosition = Vector3.Zero();
-        public deviceRotationQuaternion: Quaternion;
+        public deviceRotationQuaternion = Quaternion.Identity();        
+
         public deviceScaleFactor: number = 1;
 
         private _deviceToWorld = Matrix.Identity();
         private _worldToDevice = Matrix.Identity();
-        private deviceWorldPosition = Vector3.Zero();
 
         public controllers: Array<WebVRController> = [];
         public onControllersAttachedObservable = new Observable<Array<WebVRController>>();
@@ -94,7 +99,6 @@ module BABYLON {
             }
 
             this.rotationQuaternion = new Quaternion();
-            this.deviceRotationQuaternion = new Quaternion();
 
             if (this.webVROptions && this.webVROptions.positionScale) {
                 this.deviceScaleFactor = this.webVROptions.positionScale;
@@ -214,16 +218,16 @@ module BABYLON {
         updateFromDevice(poseData: DevicePose) {
             if (poseData && poseData.orientation) {
                 this.rawPose = poseData;
-                this.deviceRotationQuaternion.copyFromFloats(poseData.orientation[0], poseData.orientation[1], -poseData.orientation[2], -poseData.orientation[3]);
+                this._deviceRotationQuaternion.copyFromFloats(poseData.orientation[0], poseData.orientation[1], -poseData.orientation[2], -poseData.orientation[3]);
 
                 if (this.getScene().useRightHandedSystem) {
-                    this.deviceRotationQuaternion.z *= -1;
-                    this.deviceRotationQuaternion.w *= -1;
+                    this._deviceRotationQuaternion.z *= -1;
+                    this._deviceRotationQuaternion.w *= -1;
                 }
                 if (this.webVROptions.trackPosition && this.rawPose.position) {
-                    this.devicePosition.copyFromFloats(this.rawPose.position[0], this.rawPose.position[1], -this.rawPose.position[2]);
+                    this._devicePosition.copyFromFloats(this.rawPose.position[0], this.rawPose.position[1], -this.rawPose.position[2]);
                     if (this.getScene().useRightHandedSystem) {
-                        this.devicePosition.z *= -1;
+                        this._devicePosition.z *= -1;
                     }
                 }
             }
@@ -273,11 +277,11 @@ module BABYLON {
         public _updateRigCameras() {
             var camLeft = <TargetCamera>this._rigCameras[0];
             var camRight = <TargetCamera>this._rigCameras[1];
-            camLeft.rotationQuaternion.copyFrom(this.deviceRotationQuaternion);
-            camRight.rotationQuaternion.copyFrom(this.deviceRotationQuaternion);
+            camLeft.rotationQuaternion.copyFrom(this._deviceRotationQuaternion);
+            camRight.rotationQuaternion.copyFrom(this._deviceRotationQuaternion);
 
-            camLeft.position.copyFrom(this.devicePosition);
-            camRight.position.copyFrom(this.devicePosition);
+            camLeft.position.copyFrom(this._devicePosition);
+            camRight.position.copyFrom(this._devicePosition);
         }
 
         private _workingVector = Vector3.Zero();
@@ -285,15 +289,12 @@ module BABYLON {
         private _workingMatrix = Matrix.Identity();
         public _updateCache(ignoreParentClass?: boolean): void {
             if(!this.rotationQuaternion.equals(this._cache.rotationQuaternion) || !this.position.equals(this._cache.position)){
-                // Get current device position in babylon world
-                Vector3.TransformCoordinatesToRef(this.devicePosition, this._deviceToWorld, this.deviceWorldPosition);
-
                 // Set working vector to the device position in room space rotated by the new rotation
                 this.rotationQuaternion.toRotationMatrix(this._workingMatrix);
-                Vector3.TransformCoordinatesToRef(this.devicePosition, this._workingMatrix, this._workingVector);
+                Vector3.TransformCoordinatesToRef(this._devicePosition, this._workingMatrix, this._workingVector);
 
                 // Subtract this vector from the current device position in world to get the translation for the device world matrix
-                this.deviceWorldPosition.subtractToRef(this._workingVector, this._workingVector)
+                this.devicePosition.subtractToRef(this._workingVector, this._workingVector)
                 Matrix.ComposeToRef(this._oneVector, this.rotationQuaternion, this._workingVector, this._deviceToWorld);             
                 
                 // Add translation from anchor position
@@ -310,13 +311,24 @@ module BABYLON {
                     controller._deviceToWorld = this._deviceToWorld;
                     controller.update();
                 })
+                this.update();
             }
 
             if (!ignoreParentClass) {
                 super._updateCache();
             }
         }
-        
+        public update() {
+            // Get current device position in babylon world
+            Vector3.TransformCoordinatesToRef(this._devicePosition, this._deviceToWorld, this.devicePosition);
+            
+            // Get current device rotation in babylon world
+            Matrix.FromQuaternionToRef(this._deviceRotationQuaternion, this._workingMatrix);
+            this._deviceToWorld.multiplyToRef(this._workingMatrix, this._workingMatrix);
+            Quaternion.FromRotationMatrixToRef(this._workingMatrix, this.deviceRotationQuaternion);
+
+            super.update();
+        }
         public _getViewMatrix(): Matrix {
             return Matrix.Identity();
         }

+ 20 - 20
src/Gamepad/Controllers/babylon.poseEnabledController.ts

@@ -41,8 +41,10 @@ module BABYLON {
     }
 
     export class PoseEnabledController extends Gamepad implements PoseControlled {
-        devicePosition: Vector3;
-        deviceRotationQuaternion: Quaternion;
+        public devicePosition = Vector3.Zero();
+        public deviceRotationQuaternion = new Quaternion();
+        private _devicePosition: Vector3;
+        private _deviceRotationQuaternion: Quaternion;
         deviceScaleFactor: number = 1;
 
         public position: Vector3;
@@ -67,8 +69,8 @@ module BABYLON {
             this.controllerType = PoseEnabledControllerType.GENERIC;
             this.position = Vector3.Zero();
             this.rotationQuaternion = new Quaternion();
-            this.devicePosition = Vector3.Zero();
-            this.deviceRotationQuaternion = new Quaternion();
+            this._devicePosition = Vector3.Zero();
+            this._deviceRotationQuaternion = new Quaternion();
 
             this._calculatedPosition = Vector3.Zero();
             this._calculatedRotation = new Quaternion();
@@ -76,22 +78,20 @@ module BABYLON {
         }
 
         private _workingMatrix = Matrix.Identity();
-        private _workingQuaternion = Quaternion.Identity();
-        private _workingVector = Vector3.Zero();
         public update() {
             super.update();
             var pose: GamepadPose = this.browserGamepad.pose;
             this.updateFromDevice(pose);
+
+            Vector3.TransformCoordinatesToRef(this._calculatedPosition, this._deviceToWorld, this.devicePosition)
+            this._deviceToWorld.getRotationMatrixToRef(this._workingMatrix);
+            Quaternion.FromRotationMatrixToRef(this._workingMatrix, this.deviceRotationQuaternion);
             
             if (this._mesh) {
-                Vector3.TransformCoordinatesToRef(this._calculatedPosition, this._deviceToWorld, this._workingVector)
-                this._mesh.position.copyFrom(this._workingVector);
-
+                this._mesh.position.copyFrom(this.devicePosition);
 
                 if (this._mesh.rotationQuaternion) {
-                    this._deviceToWorld.getRotationMatrixToRef(this._workingMatrix);
-                    Quaternion.FromRotationMatrixToRef(this._workingMatrix, this._workingQuaternion);
-                    this._mesh.rotationQuaternion.copyFrom(this._workingQuaternion.multiplyInPlace(this._calculatedRotation));
+                    this._mesh.rotationQuaternion.copyFrom(this.deviceRotationQuaternion.multiplyInPlace(this._calculatedRotation));
                 }
             }
         }
@@ -100,29 +100,29 @@ module BABYLON {
             if (poseData) {
                 this.rawPose = poseData;
                 if (poseData.position) {
-                    this.devicePosition.copyFromFloats(poseData.position[0], poseData.position[1], -poseData.position[2]);
+                    this._devicePosition.copyFromFloats(poseData.position[0], poseData.position[1], -poseData.position[2]);
                     if (this._mesh && this._mesh.getScene().useRightHandedSystem) {
-                        this.devicePosition.z *= -1;
+                        this._devicePosition.z *= -1;
                     }
 
-                    this.devicePosition.scaleToRef(this.deviceScaleFactor, this._calculatedPosition);
+                    this._devicePosition.scaleToRef(this.deviceScaleFactor, this._calculatedPosition);
                     this._calculatedPosition.addInPlace(this.position);
 
                 }
                 let pose = this.rawPose;
                 if (poseData.orientation && pose.orientation) {
-                    this.deviceRotationQuaternion.copyFromFloats(pose.orientation[0], pose.orientation[1], -pose.orientation[2], -pose.orientation[3]);
+                    this._deviceRotationQuaternion.copyFromFloats(pose.orientation[0], pose.orientation[1], -pose.orientation[2], -pose.orientation[3]);
                     if (this._mesh) {
                         if (this._mesh.getScene().useRightHandedSystem) {
-                            this.deviceRotationQuaternion.z *= -1;
-                            this.deviceRotationQuaternion.w *= -1;
+                            this._deviceRotationQuaternion.z *= -1;
+                            this._deviceRotationQuaternion.w *= -1;
                         } else {
-                            this.deviceRotationQuaternion.multiplyToRef(this._leftHandSystemQuaternion, this.deviceRotationQuaternion);
+                            this._deviceRotationQuaternion.multiplyToRef(this._leftHandSystemQuaternion, this._deviceRotationQuaternion);
                         }
                     }
 
                     // if the camera is set, rotate to the camera's rotation
-                    this.deviceRotationQuaternion.multiplyToRef(this.rotationQuaternion, this._calculatedRotation);
+                    this._deviceRotationQuaternion.multiplyToRef(this.rotationQuaternion, this._calculatedRotation);
                 }
             }
         }