Browse Source

address review feedback

Chris Barth 4 years ago
parent
commit
98287beaad

+ 6 - 4
src/LibDeclarations/webxr.d.ts

@@ -124,6 +124,10 @@ interface XRPose {
     readonly emulatedPosition: boolean;
 }
 
+interface XRWorldInformation {
+    detectedPlanes?: XRPlaneSet;
+}
+
 interface XRFrame {
     readonly session: XRSession;
     getPose(space: XRSpace, baseSpace: XRSpace): XRPose | undefined;
@@ -135,10 +139,8 @@ interface XRFrame {
     // Anchors
     trackedAnchors?: XRAnchorSet;
     createAnchor?(pose: XRRigidTransform, space: XRSpace): Promise<XRAnchor>;
-    // Planes
-    worldInformation?: {
-        detectedPlanes?: XRPlaneSet;
-    };
+    // World geometries
+    worldInformation?: XRWorldInformation;
     // Hand tracking
     getJointPose?(joint: XRJointSpace, baseSpace: XRSpace): XRJointPose;
 }

+ 31 - 33
src/LibDeclarations/webxr.nativeextensions.d.ts

@@ -2,21 +2,6 @@
 // They are intended for use with either Babylon Native https://github.com/BabylonJS/BabylonNative or
 // Babylon React Native: https://github.com/BabylonJS/BabylonReactNative
 
-// Open question: Do better types exist to use for vectors/quaternions?
-interface XRVector {
-    x: number;
-    y: number;
-    z: number;
-}
-
-interface XRQuaternion {
-    x: number;
-    y: number;
-    z: number;
-    w: number;
-}
-
-// Open question: Should XRGeometryType be defined or should all types be strings?
 type XRGeometryType = "unknown" | "background" | "wall" | "floor" | "ceiling" | "platform";
 
 interface XRFieldOfView {
@@ -27,38 +12,49 @@ interface XRFieldOfView {
 }
 
 interface XRFrustum {
-    position: XRVector;
-    rotation: XRQuaternion;
+    position: DOMPointReadOnly;
+    orientation: DOMPointReadOnly;
     fieldOfView: XRFieldOfView;
     farDistance: number;
 }
 
 interface XRPlane {
-    // Open question: Should geometry id's and types be declared on XRPlanes or queried through other means?
-    geometryId?: number;
-    geometryType?: XRGeometryType | string;
+    // Open question: Should parent geometry id's and types be declared on XRPlanes or queried through other means?
+    parentGeometryId?: number;
+    parentGeometryType?: XRWorldGeometryType;
 }
 
-// Open question: Should XRMesh have a concept of a XRReferenceSpace similar to XRPlane?
 interface XRMesh {
+    meshSpace: XRSpace;
     positions: Float32Array;
     indices: Uint32Array;
     normals?: Float32Array;
-    // Open question: Do we need lastChangedTime declared for XRMeshes?
     lastChangedTime: number;
-    // Open question: Should geometry id's and types be declared on XRMeshes or queried through other means?
-    geometryId?: number;
-    geometryType?: XRGeometryType | string;
+
+    // Open question: Should parent geometry id's and types be declared on XRMeshes or queried through other means?
+    parentGeometryId?: number;
+    parentGeometryType?: XRGeometryType;
 }
 
 type XRDetectionBoundaryType = "frustum" | "sphere" | "box";
 
 interface XRDetectionBoundary {
-    isStationary?: boolean;
-    type?: XRDetectionBoundaryType;
-    frustum?: XRFrustum;
-    sphereRadius?: number;
-    boxDimensions?: XRVector;
+    type: XRDetectionBoundaryType;
+}
+
+interface XRFrustumDetectionBoundary extends XRDetectionBoundary {
+    type: XRDetectionBoundaryType = "frustum"
+    frustum: XRFrustum;
+}
+
+interface XRSphereDetectionBoundary extends XRDetectionBoundary {
+    type: XRDetectionBoundaryType = "sphere";
+    radius: number;
+}
+
+interface XRBoxDetectionBoundary extends XRDetectionBoundary {
+    type: XRDetectionBoundaryType = "box";
+    extent: DOMPointReadOnly;
 }
 
 interface XRGeometryDetectorOptions {
@@ -73,10 +69,12 @@ interface XRSession {
     trySetPreferredMeshDetectorOptions(preferredOptions: XRGeometryDetectorOptions): boolean;
 }
 
-type XRMeshSet = Set<XRMesh>;
-
 interface XRFrame {
     featurePointCloud? : Array<number>;
-    // Open question: Should meshes be declared on the XRFrame or queried for the session?
+}
+
+type XRMeshSet = Set<XRMesh>;
+
+interface XRWorldInformation {
     detectedMeshes? : XRMeshSet;
 }

+ 24 - 10
src/XR/features/WebXRMeshDetector.ts

@@ -93,11 +93,12 @@ export class WebXRMeshDetector extends WebXRAbstractFeature {
     constructor(_xrSessionManager: WebXRSessionManager, private _options: IWebXRMeshDetectorOptions = {}) {
         super(_xrSessionManager);
         this.xrNativeFeatureName = "mesh-detection";
-        this._initMeshDetector = this._initMeshDetector.bind(this);
         if (this._xrSessionManager.session) {
-            this._initMeshDetector(this._xrSessionManager.session);
+            this._init();
         } else {
-            this._xrSessionManager.onXRSessionInit.addOnce(this._initMeshDetector);
+            this._xrSessionManager.onXRSessionInit.addOnce(() => {
+                this._init();
+            });
         }
     }
 
@@ -134,7 +135,7 @@ export class WebXRMeshDetector extends WebXRAbstractFeature {
             return;
         }
 
-        const detectedMeshes = frame.detectedMeshes;
+        const detectedMeshes = frame.worldInformation?.detectedMeshes;
         if (!!detectedMeshes) {
             const toRemove = this._detectedMeshes
                 .filter((mesh) => !detectedMeshes.has(mesh.xrMesh))
@@ -171,14 +172,14 @@ export class WebXRMeshDetector extends WebXRAbstractFeature {
         }
     }
 
-    private _initMeshDetector(session: XRSession) {
-        if (!!session.trySetMeshDetectorEnabled) {
-            session.trySetMeshDetectorEnabled(true);
+    private _init() {
+        if (!!this._xrSessionManager.session.trySetMeshDetectorEnabled) {
+            this._xrSessionManager.session.trySetMeshDetectorEnabled(true);
         }
 
         if (!!this._options.preferredDetectorOptions &&
-            !!session.trySetPreferredMeshDetectorOptions) {
-            session.trySetPreferredMeshDetectorOptions(this._options.preferredDetectorOptions);
+            !!this._xrSessionManager.session.trySetPreferredMeshDetectorOptions) {
+            this._xrSessionManager.session.trySetPreferredMeshDetectorOptions(this._options.preferredDetectorOptions);
         }
     }
 
@@ -214,7 +215,20 @@ export class WebXRMeshDetector extends WebXRAbstractFeature {
             mesh.normals = xrMesh.normals;
         }
 
-        mesh.transformationMatrix = this._options.worldParentNode?.getWorldMatrix() ?? Matrix.Identity();
+        // matrix
+        const pose = xrFrame.getPose(xrMesh.meshSpace, this._xrSessionManager.referenceSpace);
+        if (pose) {
+            const mat = mesh.transformationMatrix || new Matrix();
+            Matrix.FromArrayToRef(pose.transform.matrix, 0, mat);
+            if (!this._xrSessionManager.scene.useRightHandedSystem) {
+                mat.toggleModelMatrixHandInPlace();
+            }
+            mesh.transformationMatrix = mat;
+            if (this._options.worldParentNode) {
+                mat.multiplyToRef(this._options.worldParentNode.getWorldMatrix(), mat);
+            }
+        }
+        
         return <IWebXRMesh>mesh;
     }
 

+ 10 - 9
src/XR/features/WebXRPlaneDetector.ts

@@ -96,11 +96,12 @@ export class WebXRPlaneDetector extends WebXRAbstractFeature {
     constructor(_xrSessionManager: WebXRSessionManager, private _options: IWebXRPlaneDetectorOptions = {}) {
         super(_xrSessionManager);
         this.xrNativeFeatureName = "plane-detection";
-        this._initPlaneDetector = this._initPlaneDetector.bind(this);
         if (this._xrSessionManager.session) {
-            this._initPlaneDetector(this._xrSessionManager.session);
+            this._init();
         } else {
-            this._xrSessionManager.onXRSessionInit.addOnce(this._initPlaneDetector);
+            this._xrSessionManager.onXRSessionInit.addOnce(() => {
+                this._init();
+            });
         }
     }
 
@@ -189,7 +190,7 @@ export class WebXRPlaneDetector extends WebXRAbstractFeature {
         }
     }
 
-    private _initPlaneDetector(session: XRSession) {
+    private _init() {
         const internalInit = () => {
             this._enabled = true;
             if (this._detectedPlanes.length) {
@@ -198,20 +199,20 @@ export class WebXRPlaneDetector extends WebXRAbstractFeature {
         };
 
         if (!!this._options.preferredDetectorOptions &&
-            !!session.trySetPreferredPlaneDetectorOptions) {
-                session.trySetPreferredPlaneDetectorOptions(this._options.preferredDetectorOptions);
+            !!this._xrSessionManager.session.trySetPreferredPlaneDetectorOptions) {
+                this._xrSessionManager.session.trySetPreferredPlaneDetectorOptions(this._options.preferredDetectorOptions);
         }
 
-        if (!session.updateWorldTrackingState) {
+        if (!this._xrSessionManager.session.updateWorldTrackingState) {
             // check if this was enabled by a flag
-            const alreadyEnabled = (session as any).worldTrackingState?.planeDetectionState?.enabled;
+            const alreadyEnabled = (this._xrSessionManager.session as any).worldTrackingState?.planeDetectionState?.enabled;
             if (alreadyEnabled) {
                 internalInit();
             }
             // fail silently
             return;
         }
-        session.updateWorldTrackingState({ planeDetectionState: { enabled: true } });
+        this._xrSessionManager.session.updateWorldTrackingState({ planeDetectionState: { enabled: true } });
         internalInit();
     }