Просмотр исходного кода

detect if a device orientation sensor is availible on a device

Trevor Baron 6 лет назад
Родитель
Сommit
2bec57cc5a

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

@@ -17,6 +17,7 @@
 - Unify preparation of instance attributes. Added `MaterialHelper.PushAttributesForInstances` ([MarkusBillharz](https://github.com/MarkusBillharz))
 - Added support for PBR [irradiance map](https://doc.babylonjs.com/how_to/physically_based_rendering_master#irradiance-map)
 - Ability to set render camera on utility layer instead of using the latest active camera ([TrevorDev](https://github.com/TrevorDev))
+- Method to check if device orientation is available ([TrevorDev](https://github.com/TrevorDev))
 
 ### Engine
 - Added preprocessors for shaders to improve how shaders are compiled for WebGL1/2 or WebGPU ([Deltakosh](https://github.com/deltakosh/))

+ 27 - 0
src/Cameras/Inputs/freeCameraDeviceOrientationInput.ts

@@ -52,6 +52,33 @@ export class FreeCameraDeviceOrientationInput implements ICameraInput<FreeCamera
     private _gamma: number = 0;
 
     /**
+     * Can be used to detect if a device orientation sensor is availible on a device
+     * @param timeout amount of time in milliseconds to wait for a response from the sensor (default: infinite)
+     */
+    public static WaitForOrientationChangeAsync(timeout?: number) {
+        return new Promise((res, rej) => {
+            var gotValue = false;
+            var eventHandler = () => {
+                window.removeEventListener("deviceorientation", eventHandler);
+                gotValue = true;
+                res();
+            };
+
+            // If timeout is pupulated reject the promise
+            if (timeout) {
+                setTimeout(() => {
+                    if (!gotValue) {
+                        window.removeEventListener("deviceorientation", eventHandler);
+                        rej("WaitForOrientationChangeAsync timed out");
+                    }
+                }, timeout);
+            }
+
+            window.addEventListener("deviceorientation", eventHandler);
+        });
+    }
+
+    /**
      * @hidden
      */
     public _onDeviceOrientationChangedObservable = new Observable<void>();