瀏覽代碼

Merge pull request #1516 from RaananW/position-offset-webvr

Adding position offset to the WebVR camera
David Catuhe 8 年之前
父節點
當前提交
8082b148b4
共有 1 個文件被更改,包括 26 次插入1 次删除
  1. 26 1
      src/Cameras/VR/babylon.webVRCamera.ts

+ 26 - 1
src/Cameras/VR/babylon.webVRCamera.ts

@@ -23,9 +23,14 @@ module BABYLON {
 
         private _quaternionCache: Quaternion;
 
+        private _positionOffset: Vector3;
+
         constructor(name: string, position: Vector3, scene: Scene, compensateDistortion = false, private webVROptions: WebVROptions = {}) {
             super(name, position, scene);
 
+            //using the position provided as the current position offset
+            this._positionOffset = position;
+
             //enable VR
             this.getEngine().initWebVR();
 
@@ -81,7 +86,10 @@ module BABYLON {
                     this.rotationQuaternion.copyFromFloats(this._cacheState.orientation[0], this._cacheState.orientation[1], -this._cacheState.orientation[2], -this._cacheState.orientation[3]);
                     if (this.webVROptions.trackPosition && this._cacheState.position) {
                         this.position.copyFromFloats(this._cacheState.position[0], this._cacheState.position[1], -this._cacheState.position[2]);
-                        this.webVROptions.positionScale && this.position.scaleInPlace(this.webVROptions.positionScale)
+                        //scale the position accordingly
+                        this.webVROptions.positionScale && this.position.scaleInPlace(this.webVROptions.positionScale);
+                        //add the position offset
+                        this.position.addInPlace(this._positionOffset);
                     }
                 }
             }
@@ -122,6 +130,23 @@ module BABYLON {
             //pitch and roll won't be affected.
             this._vrDevice.resetPose();
         }
+
+        /**
+         * 
+         * Set the position offset of the VR camera
+         * The offset will be added to the WebVR pose, after scaling it (if set).
+         * 
+         * @param {Vector3} [newPosition] an optional new position. if not provided, the current camera position will be used.
+         * 
+         * @memberOf WebVRFreeCamera
+         */
+        public setPositionOffset(newPosition?: Vector3) {
+            if(newPosition) {
+                this._positionOffset = newPosition;
+            } else {
+                this._positionOffset.copyFrom(this.position);
+            }
+        }
     }
 }