瀏覽代碼

Merge pull request #9084 from RaananW/xrTrackingState

[XR] Tracking state for the WebXR Camera
David Catuhe 4 年之前
父節點
當前提交
e4543e597f
共有 3 個文件被更改,包括 45 次插入0 次删除
  1. 1 0
      dist/preview release/what's new.md
  2. 26 0
      src/XR/webXRCamera.ts
  3. 18 0
      src/XR/webXRTypes.ts

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

@@ -207,6 +207,7 @@
 - Fixed an issue with moving backwards in XR ([#8854](https://github.com/BabylonJS/Babylon.js/issues/8854)) ([RaananW](https://github.com/RaananW))
 - Hit-Test results can be an empty array ([#8887](https://github.com/BabylonJS/Babylon.js/issues/8887)) ([RaananW](https://github.com/RaananW))
 - XR's main camera uses the first eye's projection matrix ([#8944](https://github.com/BabylonJS/Babylon.js/issues/8944)) ([RaananW](https://github.com/RaananW))
+- XR tracking state was added to the camera ([#9076](https://github.com/BabylonJS/Babylon.js/issues/9076)) ([RaananW](https://github.com/RaananW))
 
 ### Collisions
 

+ 26 - 0
src/XR/webXRCamera.ts

@@ -6,6 +6,7 @@ import { TargetCamera } from "../Cameras/targetCamera";
 import { WebXRSessionManager } from "./webXRSessionManager";
 import { Viewport } from "../Maths/math.viewport";
 import { Observable } from "../Misc/observable";
+import { WebXRTrackingState } from "./webXRTypes";
 
 /**
  * WebXR Camera which holds the views for the xrSession
@@ -17,6 +18,7 @@ export class WebXRCamera extends FreeCamera {
     private _referencedPosition: Vector3 = new Vector3();
     private _xrInvPositionCache: Vector3 = new Vector3();
     private _xrInvQuaternionCache = Quaternion.Identity();
+    private _trackingState: WebXRTrackingState = WebXRTrackingState.NOT_TRACKING;
 
     /**
      * Observable raised before camera teleportation
@@ -29,6 +31,11 @@ export class WebXRCamera extends FreeCamera {
     public onAfterCameraTeleport = new Observable<Vector3>();
 
     /**
+     * Notifies when the camera's tracking state has changed.
+     * Notice - will also be triggered when tracking has started (at the beginning of the session)
+     */
+    public onTrackingStateChanged = new Observable<WebXRTrackingState>();
+    /**
      * Should position compensation execute on first frame.
      * This is used when copying the position from a native (non XR) camera
      */
@@ -75,6 +82,20 @@ export class WebXRCamera extends FreeCamera {
     }
 
     /**
+     * Get the current XR tracking state of the camera
+     */
+    public get trackingState(): WebXRTrackingState {
+        return this._trackingState;
+    }
+
+    private _setTrackingState(newState: WebXRTrackingState) {
+        if (this._trackingState !== newState) {
+            this._trackingState = newState;
+            this.onTrackingStateChanged.notifyObservers(newState);
+        }
+    }
+
+    /**
      * Return the user's height, unrelated to the current ground.
      * This will be the y position of this camera, when ground level is 0.
      */
@@ -133,9 +154,14 @@ export class WebXRCamera extends FreeCamera {
         const pose = this._xrSessionManager.currentFrame && this._xrSessionManager.currentFrame.getViewerPose(this._xrSessionManager.referenceSpace);
 
         if (!pose) {
+            this._setTrackingState(WebXRTrackingState.NOT_TRACKING);
             return;
         }
 
+        // Set the tracking state. if it didn't change it is a no-op
+        const trackingState = pose.emulatedPosition ? WebXRTrackingState.TRACKING_LOST : WebXRTrackingState.TRACKING;
+        this._setTrackingState(trackingState);
+
         if (pose.transform) {
             const pos = pose.transform.position;
             this._referencedPosition.set(pos.x, pos.y, pos.z);

+ 18 - 0
src/XR/webXRTypes.ts

@@ -24,6 +24,24 @@ export enum WebXRState {
 }
 
 /**
+ * The state of the XR camera's tracking
+ */
+export enum WebXRTrackingState {
+    /**
+     * No transformation received, device is not being tracked
+     */
+    NOT_TRACKING,
+    /**
+     * Tracking lost - using emulated position
+     */
+    TRACKING_LOST,
+    /**
+     * Transformation tracking works normally
+     */
+    TRACKING
+}
+
+/**
  * Abstraction of the XR render target
  */
 export interface WebXRRenderTarget extends IDisposable {