Browse Source

expose winding order for xr meshes

Chris Barth 4 years ago
parent
commit
e2f715307e
2 changed files with 14 additions and 10 deletions
  1. 1 0
      src/LibDeclarations/webxr.nativeextensions.d.ts
  2. 13 10
      src/XR/features/WebXRMeshDetector.ts

+ 1 - 0
src/LibDeclarations/webxr.nativeextensions.d.ts

@@ -30,6 +30,7 @@ interface XRMesh {
     meshSpace: XRSpace;
     positions: Float32Array;
     indices: Uint32Array;
+    isClockwiseWindingOrder: boolean;
     normals?: Float32Array;
     lastChangedTime: number;
     parentSceneObject?: XRSceneObject;

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

@@ -223,13 +223,6 @@ export class WebXRMeshDetector extends WebXRAbstractFeature {
                     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) {
@@ -238,13 +231,23 @@ export class WebXRMeshDetector extends WebXRAbstractFeature {
                         mesh.normals[i + 2] = -1 * xrMesh.normals[i + 2];
                     }
                 }
-            }
-            else {
+            } else {
                 mesh.positions = xrMesh.positions;
-                mesh.indices = xrMesh.indices;
                 mesh.normals = xrMesh.normals;
             }
 
+            if (xrMesh.isClockwiseWindingOrder) {
+                // Babylon.js expects counter clockwise winding order for meshes
+                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];
+                }
+            } else {
+                mesh.indices = xrMesh.indices;
+            }
+
             // matrix
             const pose = xrFrame.getPose(xrMesh.meshSpace, this._xrSessionManager.referenceSpace);
             if (pose) {