Explorar o código

Small modifications

Camera's attach control will start sending frames to the vr device.
detach will stop.
full screen will make the canvas full screen.
Raanan Weber %!s(int64=9) %!d(string=hai) anos
pai
achega
a01ca1fc19
Modificáronse 3 ficheiros con 39 adicións e 52 borrados
  1. 27 24
      src/Cameras/VR/babylon.webVRCamera.ts
  2. 6 15
      src/Tools/babylon.tools.ts
  3. 6 13
      src/babylon.engine.ts

+ 27 - 24
src/Cameras/VR/babylon.webVRCamera.ts

@@ -5,6 +5,7 @@ module BABYLON {
 
     export interface WebVROptions {
         trackPosition?: boolean; //update the camera's position
+        positionScale?: number;
         displayName?: string; //if there are more than one VRDisplays.
     }
 
@@ -28,6 +29,28 @@ module BABYLON {
             //this._getWebVRDevices = this._getWebVRDevices.bind(this);
             if (!this.getEngine().vrDisplaysPromise) {
                 Tools.Error("WebVR is not enabled on your browser");
+            } else {
+                //TODO get the metrics updated using the device's eye parameters!
+                this.getEngine().vrDisplaysPromise.then((devices) => {
+                    if (devices.length > 0) {
+                        this._vrEnabled = true;
+                        if (this.webVROptions.displayName) {
+                            devices.some(device => {
+                                if (device.displayName === this.webVROptions.displayName) {
+                                    this._vrDevice = device;
+                                    return true;
+                                } else {
+                                    return false;
+                                }
+                            })
+                        } else {
+                            //choose the first one
+                            this._vrDevice = devices[0];
+                        }
+                    } else {
+                        Tools.Error("No WebVR devices found!");
+                    }
+                })
             }
 
             this.rotationQuaternion = new Quaternion();
@@ -40,6 +63,7 @@ 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.position.copyFromFloats(this._cacheState.position[0], this._cacheState.position[1], -this._cacheState.position[2]);
+                    this.webVROptions.positionScale && this.position.scaleInPlace(this.webVROptions.positionScale)
                 }
                 //Flip in XY plane
                 this.rotationQuaternion.z *= -1;
@@ -58,30 +82,9 @@ module BABYLON {
 
             noPreventDefault = Camera.ForceAttachControlToAlwaysPreventDefault ? false : noPreventDefault;
 
-            //TODO get the metrics updated using the device's eye parameters!
-
-            //sanity check. if no WebVR enabled.
-            this.getEngine().vrDisplaysPromise && this.getEngine().vrDisplaysPromise.then((devices) => {
-                if (devices.length > 0) {
-                    this._vrEnabled = true;
-                    if (this.webVROptions.displayName) {
-                        devices.some(device => {
-                            if (device.displayName === this.webVROptions.displayName) {
-                                this._vrDevice = device;
-                                return true;
-                            } else {
-                                return false;
-                            }
-                        })
-                    } else {
-                        //choose the first one
-                        this._vrDevice = devices[0];
-                    }
-                    this.getEngine().enableVR(this._vrDevice)
-                } else {
-                    Tools.Error("No WebVR devices found!");
-                }
-            })
+            if (this._vrEnabled) {
+                this.getEngine().enableVR(this._vrDevice)
+            }
         }
 
         public detachControl(element: HTMLElement): void {

+ 6 - 15
src/Tools/babylon.tools.ts

@@ -271,23 +271,14 @@
             }
         }
 
-        public static RequestFullscreen(element, vrDisplay?): void {
-            //WebVR?
-            if (vrDisplay) {
-                vrDisplay.requestPresent([{ source: element }]);
-            } else {
-                var requestFunction = element.requestFullscreen || element.msRequestFullscreen || element.webkitRequestFullscreen || element.mozRequestFullScreen;
-                if (!requestFunction) return;
-                requestFunction.call(element);
-            }
+        public static RequestFullscreen(element): void {
+            var requestFunction = element.requestFullscreen || element.msRequestFullscreen || element.webkitRequestFullscreen || element.mozRequestFullScreen;
+            if (!requestFunction) return;
+            requestFunction.call(element);
         }
 
-        public static ExitFullscreen(vrDisplay?): void {
-            //WebVR?
-            if (vrDisplay) {
-                vrDisplay.exitPresent();
-            }
-            else if (document.exitFullscreen) {
+        public static ExitFullscreen(): void {
+            if (document.exitFullscreen) {
                 document.exitFullscreen();
             }
             else if (document.mozCancelFullScreen) {

+ 6 - 13
src/babylon.engine.ts

@@ -784,7 +784,7 @@
 
             if (this._activeRenderLoops.length > 0) {
                 // Register new frame
-                Tools.QueueNewFrame(this._bindedRenderFunction, this._getFrameRequester());
+                Tools.QueueNewFrame(this._bindedRenderFunction, this._vrDisplayEnabled);
             } else {
                 this._renderingQueueLaunched = false;
             }
@@ -819,10 +819,10 @@
          */
         public switchFullscreen(requestPointerLock: boolean): void {
             if (this.isFullscreen) {
-                Tools.ExitFullscreen(this._vrDisplayEnabled);
+                Tools.ExitFullscreen();
             } else {
                 this._pointerLockRequested = requestPointerLock;
-                Tools.RequestFullscreen(this._renderingCanvas, this._vrDisplayEnabled);
+                Tools.RequestFullscreen(this._renderingCanvas);
             }
         }
 
@@ -959,13 +959,14 @@
 
         public enableVR(vrDevice) {
             this._vrDisplayEnabled = vrDevice;
-            window.addEventListener('vrdisplaypresentchange', this._onVRFullScreenTriggered, false);
+            this._vrDisplayEnabled.requestPresent([{ source: this.getRenderingCanvas() }]).then(this._onVRFullScreenTriggered);
         }
 
         public disableVR() {
             if (this._vrDisplayEnabled) {
+                this._vrDisplayEnabled.exitPresent();
                 this._vrDisplayEnabled = null;
-                window.removeEventListener('vrdisplaypresentchange', this._onVRFullScreenTriggered, false);
+                this._onVRFullScreenTriggered()
             }
         }
 
@@ -987,14 +988,6 @@
             }
         }
 
-        private _getFrameRequester() {
-            if (this._vrDisplayEnabled && this._vrDisplayEnabled.isPresenting) {
-                return this._vrDisplayEnabled;
-            } else {
-                return window;
-            }
-        }
-
         private _getVRDisplays() {
             var getWebVRDevices = (devices: Array<any>) => {
                 var size = devices.length;