Browse Source

working mesh detector

Chris Barth 4 years ago
parent
commit
83bf83da3d

+ 18 - 12
src/LibDeclarations/webxr.nativeextensions.d.ts

@@ -2,6 +2,7 @@
 // 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;
@@ -15,6 +16,9 @@ interface XRQuaternion {
     w: number;
 }
 
+// Open question: Should XRGeometryType be defined or should all types be strings?
+type XRGeometryType = "unknown" | "background" | "wall" | "floor" | "ceiling" | "platform";
+
 interface XRFieldOfView {
     angleLeft: number;
     angleRight: number;
@@ -29,16 +33,24 @@ interface XRFrustum {
     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 XRMesh have a concept of a XRReferenceSpace similar to XRPlane?
 interface XRMesh {
     positions: Float32Array;
     indices: Uint32Array;
     normals?: Float32Array;
-    transformationMatrix?: Matrix;
+    // 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;
 }
 
-type XRGeometryType = "unknown" | "background" | "wall" | "floor" | "ceiling" | "platform";
-
 type XRDetectionBoundaryType = "frustum" | "sphere" | "box";
 
 interface XRDetectionBoundary {
@@ -49,28 +61,22 @@ interface XRDetectionBoundary {
     boxDimensions?: XRVector;
 }
 
-type XRGeometryLevelOfDetail = "coarse" | "medium" | "fine" | "custom";
-
 interface XRGeometryDetectorOptions {
     detectionBoundary?: XRDetectionBoundary;
-    levelOfDetail?: XRGeometryLevelOfDetail | string;
     updateInterval?: number;
 }
 
 interface XRSession {
     trySetFeaturePointCloudEnabled(enabled: boolean): boolean;
-    trySetPlaneDetectorOptions(options: XRGeometryDetectorOptions): boolean;
-    tryGetPlaneGeometryId(xrPlane: XRPlane): number | undefined;
-    tryGetPlaneGeometryType(xrPlane: XRPlane): XRGeometryType | string | undefined;
+    trySetPreferredPlaneDetectorOptions(preferredOptions: XRGeometryDetectorOptions): boolean;
     trySetMeshDetectorEnabled(enabled: boolean): boolean;
-    trySetMeshDetectorOptions(options: XRGeometryDetectorOptions): boolean;
-    tryGetMeshGeometryId(xrMesh: XRMesh): number | undefined;
-    tryGetMeshGeometryType(xrMesh: XRMesh): XRGeometryType | string | undefined;
+    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?
     detectedMeshes? : XRMeshSet;
 }

+ 46 - 12
src/XR/features/WebXRMeshDetector.ts

@@ -35,6 +35,18 @@ export interface IWebXRMesh {
      */
     id: number;
     /**
+     * an array of vertex positions in babylon space. right/left hand system is taken into account.
+     */
+    positions: Float32Array;
+    /**
+     * an array of indices in babylon space. right/left hand system is taken into account.
+     */
+    indices: Uint32Array;
+    /**
+     * an array of vertex normals in babylon space. right/left hand system is taken into account.
+     */
+    normals?: Float32Array;
+    /**
      * data required for construction a mesh in Babylon.js
      */
     xrMesh: XRMesh;
@@ -43,14 +55,6 @@ export interface IWebXRMesh {
      * Local vs. World are decided if worldParentNode was provided or not in the options when constructing the module
      */
     transformationMatrix: Matrix;
-    /**
-     * if the mesh is a part of a more complex geometry, this id will represent the geometry
-     */
-    geometryId?: number;
-    /**
-     * if the mesh is a part of a more complex geometry, this type will represent the type of the geometry
-     */
-    geometryType?: XRGeometryType | string;
 }
 
 let meshIdProvider = 0;
@@ -174,14 +178,44 @@ export class WebXRMeshDetector extends WebXRAbstractFeature {
         }
 
         if (!!this._options.preferredDetectorOptions &&
-            !!this._xrSessionManager.session.trySetMeshDetectorOptions) {
-            this._xrSessionManager.session.trySetMeshDetectorOptions(this._options.preferredDetectorOptions);
+            !!this._xrSessionManager.session.trySetPreferredMeshDetectorOptions) {
+            this._xrSessionManager.session.trySetPreferredMeshDetectorOptions(this._options.preferredDetectorOptions);
         }
     }
 
     private _updateMeshWithXRMesh(xrMesh: XRMesh, mesh: Partial<IWebXRMesh>, xrFrame: XRFrame): IWebXRMesh {
-        mesh.geometryId = this._xrSessionManager.session.tryGetMeshGeometryId(xrMesh);
-        mesh.geometryType = this._xrSessionManager.session.tryGetMeshGeometryType(xrMesh);
+        mesh.xrMesh = xrMesh;
+        if (!this._xrSessionManager.scene.useRightHandedSystem) {
+            mesh.positions = new Float32Array(xrMesh.positions.length);
+            for (let i = 0; i < xrMesh.positions.length; i += 3) {
+                mesh.positions[i] = xrMesh.positions[i];
+                mesh.positions[i + 1] = xrMesh.positions[i + 1];
+                mesh.positions[i + 2] = -1 * xrMesh.positions[i + 2];
+            }
+
+            mesh.indices = new Uint32Array(xrMesh.indices.length);
+            for (let i = 0; i < xrMesh.indices.length; i += 3) {
+                mesh.indices[i] = xrMesh.indices[i];
+                mesh.indices[i + 1] = xrMesh.indices[i + 2];
+                mesh.indices[i + 2] = xrMesh.indices[i + 1];
+            }
+
+            if (!!xrMesh.normals) {
+                mesh.normals = new Float32Array(xrMesh.normals.length);
+                for (let i = 0; i < xrMesh.normals.length; i += 3) {
+                    mesh.normals[i] = xrMesh.normals[i];
+                    mesh.normals[i + 1] = xrMesh.normals[i + 1];
+                    mesh.normals[i + 2] = -1 * xrMesh.normals[i + 2];
+                }
+            }
+        }
+        else {
+            mesh.positions = xrMesh.positions;
+            mesh.indices = xrMesh.indices;
+            mesh.normals = xrMesh.normals;
+        }
+
+        mesh.transformationMatrix = this._options.worldParentNode?.getWorldMatrix() ?? Matrix.Identity();
         return <IWebXRMesh>mesh;
     }
 

+ 2 - 15
src/XR/features/WebXRPlaneDetector.ts

@@ -50,16 +50,6 @@ export interface IWebXRPlane {
      * the native xr-plane object
      */
     xrPlane: XRPlane;
-    /**
-     * if the plane is a part of a more complex geometry, geometryId will be populated with the geometry's id
-     * note: this functionality is currently only available with BabylonNative
-     */
-    geometryId?: number;
-    /**
-     * if the mesh is a part of a more complex geometry, geometryType will be populated by the type of the geometry
-     * note: this functionality is currently only available with BabylonNative
-     */
-    geometryType?: XRGeometryType | string;
 }
 
 let planeIdProvider = 0;
@@ -209,8 +199,8 @@ export class WebXRPlaneDetector extends WebXRAbstractFeature {
         };
 
         if (!!this._options.preferredDetectorOptions &&
-            !!this._xrSessionManager.session.trySetPlaneDetectorOptions) {
-            this._xrSessionManager.session.trySetPlaneDetectorOptions(this._options.preferredDetectorOptions);
+            !!this._xrSessionManager.session.trySetPreferredPlaneDetectorOptions) {
+            this._xrSessionManager.session.trySetPreferredPlaneDetectorOptions(this._options.preferredDetectorOptions);
         }
 
         if (!this._xrSessionManager.session.updateWorldTrackingState) {
@@ -245,9 +235,6 @@ export class WebXRPlaneDetector extends WebXRAbstractFeature {
             }
         }
 
-        plane.geometryId = this._xrSessionManager.session.tryGetPlaneGeometryId(xrPlane);
-        plane.geometryType = this._xrSessionManager.session.tryGetPlaneGeometryType(xrPlane);
-
         return <IWebXRPlane>plane;
     }