Sfoglia il codice sorgente

initWebVRAsync, useStandingMatrixAsync

Trevor Baron 7 anni fa
parent
commit
87badbb527

+ 1 - 0
dist/preview release/what's new.md

@@ -62,6 +62,7 @@
 - Added promise-based async functions to the SceneLoader, Scene.whenReadyAsync, and material.forceCompilationAsync. ([bghgary](https://github.com/bghgary)]
 - Added checks to VertexData.merge to ensure data is valid before merging. ([bghgary](https://github.com/bghgary)]
 - Ability to set a mesh to customize the webVR gaze tracker ([trevordev](https://github.com/trevordev))
+- Added promise-based async functions for initWebVRAsync and useStandingMatrixAsync ([trevordev](https://github.com/trevordev))
 
 ## Bug fixes
 

+ 3 - 4
src/Cameras/VR/babylon.vrExperienceHelper.ts

@@ -207,15 +207,14 @@ module BABYLON {
         }
 
         /**
-         * The mesh used to display where the user is selecting.
+         * The mesh used to display where the user is selecting, 
+         * when set bakeCurrentTransformIntoVertices will be called on the mesh.
+         * See http://doc.babylonjs.com/resources/baking_transformations 
          */
         public get gazeTrackerMesh(): Mesh {
             return this._gazeTracker;
         }
 
-        /**
-         * Sets the mesh to be used to display where the user is selecting.
-         */
         public set gazeTrackerMesh(value: Mesh) {
             if (value) {
                 this._gazeTracker = value;

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

@@ -301,26 +301,34 @@ module BABYLON {
          */
         public useStandingMatrix(callback = (bool: boolean) => { }) {
             // Use standing matrix if available
-            if (!navigator || !navigator.getVRDisplays) {
-                callback(false);
-            } else {
-                navigator.getVRDisplays().then((displays: any) => {
-                    if (!displays || !displays[0] || !displays[0].stageParameters || !displays[0].stageParameters.sittingToStandingTransform) {
-                        callback(false);
-                    } else {
-                        this._standingMatrix = new Matrix();
-                        Matrix.FromFloat32ArrayToRefScaled(displays[0].stageParameters.sittingToStandingTransform, 0, 1, this._standingMatrix);
-                        if (!this.getScene().useRightHandedSystem) {
-                            [2, 6, 8, 9, 14].forEach((num) => {
-                                if (this._standingMatrix) {
-                                    this._standingMatrix.m[num] *= -1;
-                                }
-                            });
-                        }
-                        callback(true);
+            this.getEngine().initWebVRAsync().then((result)=>{
+                if (!result.vrDisplay || !result.vrDisplay.stageParameters || !result.vrDisplay.stageParameters.sittingToStandingTransform) {
+                    callback(false);
+                } else {
+                    this._standingMatrix = new Matrix();
+                    Matrix.FromFloat32ArrayToRefScaled(result.vrDisplay.stageParameters.sittingToStandingTransform, 0, 1, this._standingMatrix);
+                    if (!this.getScene().useRightHandedSystem) {
+                        [2, 6, 8, 9, 14].forEach((num) => {
+                            if (this._standingMatrix) {
+                                this._standingMatrix.m[num] *= -1;
+                            }
+                        });
                     }
+                    callback(true);
+                }
+            });
+        }
+
+        /**
+         * Enables the standing matrix when supported. This can be used to position the user's view the correct height from the ground.
+         * @returns A promise with a boolean set to if the standing matrix is supported.
+         */
+        public useStandingMatrixAsync():Promise<boolean> {
+            return new Promise((res, rej)=>{
+                this.useStandingMatrix((supported)=>{
+                    res(supported);
                 });
-            }
+            });
         }
 
         /**

+ 49 - 28
src/Engine/babylon.engine.ts

@@ -1784,37 +1784,58 @@
             return this._vrDisplay;
         }
 
+        /**
+         * Initializes a webVR display and starts listening to display change events.
+         * The onVRDisplayChangedObservable will be notified upon these changes.
+         * @returns The onVRDisplayChangedObservable.
+         */
         public initWebVR(): Observable<{ vrDisplay: any, vrSupported: any }> {
-            var notifyObservers = () => {
-                var eventArgs = {
-                    vrDisplay: this._vrDisplay,
-                    vrSupported: this._vrSupported
-                };
-                this.onVRDisplayChangedObservable.notifyObservers(eventArgs);
-            }
+            this.initWebVRAsync();
+            return this.onVRDisplayChangedObservable;
+        }
 
-            if (!this._onVrDisplayConnect) {
-                this._onVrDisplayConnect = (event) => {
-                    this._vrDisplay = event.display;
-                    notifyObservers();
-                };
-                this._onVrDisplayDisconnect = () => {
-                    this._vrDisplay.cancelAnimationFrame(this._frameHandler);
-                    this._vrDisplay = undefined;
-                    this._frameHandler = Tools.QueueNewFrame(this._bindedRenderFunction);
-                    notifyObservers();
-                };
-                this._onVrDisplayPresentChange = () => {
-                    this._vrExclusivePointerMode = this._vrDisplay && this._vrDisplay.isPresenting;
+        /**
+         * Initializes a webVR display and starts listening to display change events.
+         * The onVRDisplayChangedObservable will be notified upon these changes.
+         * @returns A promise containing a VRDisplay and if vr is supported.
+         */
+        public initWebVRAsync(): Promise<{ vrDisplay: Nullable<any>, vrSupported: boolean }> {
+            return new Promise((res, rej)=>{
+                var notifyObservers = () => {
+                    var eventArgs = {
+                        vrDisplay: this._vrDisplay,
+                        vrSupported: this._vrSupported
+                    };
+                    this.onVRDisplayChangedObservable.notifyObservers(eventArgs);
                 }
-                window.addEventListener('vrdisplayconnect', this._onVrDisplayConnect);
-                window.addEventListener('vrdisplaydisconnect', this._onVrDisplayDisconnect);
-                window.addEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
-            }
-
-            this._getVRDisplays(notifyObservers);
-
-            return this.onVRDisplayChangedObservable;
+    
+                if (!this._onVrDisplayConnect) {
+                    this._onVrDisplayConnect = (event) => {
+                        this._vrDisplay = event.display;
+                        notifyObservers();
+                    };
+                    this._onVrDisplayDisconnect = () => {
+                        this._vrDisplay.cancelAnimationFrame(this._frameHandler);
+                        this._vrDisplay = undefined;
+                        this._frameHandler = Tools.QueueNewFrame(this._bindedRenderFunction);
+                        notifyObservers();
+                    };
+                    this._onVrDisplayPresentChange = () => {
+                        this._vrExclusivePointerMode = this._vrDisplay && this._vrDisplay.isPresenting;
+                    }
+                    window.addEventListener('vrdisplayconnect', this._onVrDisplayConnect);
+                    window.addEventListener('vrdisplaydisconnect', this._onVrDisplayDisconnect);
+                    window.addEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
+                }
+    
+                this._getVRDisplays(()=>{
+                    notifyObservers();
+                    res({
+                        vrDisplay: this._vrDisplay,
+                        vrSupported: this._vrSupported
+                    });
+                });
+            });
         }
 
         public enableVR() {