瀏覽代碼

adding a tracking-state observable for the XR camera

fixing #9076
Raanan Weber 4 年之前
父節點
當前提交
96a4b0dfab
共有 2 個文件被更改,包括 42 次插入0 次删除
  1. 24 0
      src/XR/webXRCamera.ts
  2. 18 0
      src/XR/webXRTypes.ts

+ 24 - 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
      */
@@ -74,6 +81,17 @@ export class WebXRCamera extends FreeCamera {
         );
     }
 
+    public get trackingState(): WebXRTrackingState {
+        return this._trackingState;
+    }
+
+    public set trackingState(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 +151,15 @@ export class WebXRCamera extends FreeCamera {
         const pose = this._xrSessionManager.currentFrame && this._xrSessionManager.currentFrame.getViewerPose(this._xrSessionManager.referenceSpace);
 
         if (!pose) {
+            this.trackingState = 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;
+        if (trackingState !== this._trackingState) {
+            this.trackingState = 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 {