Browse Source

Merge pull request #6234 from aWeirdo/patch-25

Picking support for UnIndexed meshes
David Catuhe 6 years ago
parent
commit
9c04870cf1
1 changed files with 40 additions and 1 deletions
  1. 40 1
      src/Meshes/subMesh.ts

+ 40 - 1
src/Meshes/subMesh.ts

@@ -372,6 +372,11 @@ export class SubMesh extends BaseSubMesh implements ICullable {
             return this._intersectLines(ray, positions, indices, (this._mesh as any).intersectionThreshold, fastCheck);
         }
 
+        // Check if mesh is unindexed
+        if (!indices.length) {
+            return this._intersectUnIndexedTriangles(ray, positions, indices, fastCheck, trianglePredicate);
+        }
+
         return this._intersectTriangles(ray, positions, indices, fastCheck, trianglePredicate);
     }
 
@@ -435,6 +440,40 @@ export class SubMesh extends BaseSubMesh implements ICullable {
     }
 
     /** @hidden */
+    private _intersectUnIndexedTriangles(ray: Ray, positions: Vector3[], indices: IndicesArray,
+        fastCheck?: boolean, trianglePredicate?: TrianglePickingPredicate): Nullable<IntersectionInfo> {
+        var intersectInfo: Nullable<IntersectionInfo> = null;
+        // Triangles test
+        for (var index = this.verticesStart; index < this.verticesStart + this.verticesCount; index += 3) {
+            var p0 = positions[index];
+            var p1 = positions[index + 1];
+            var p2 = positions[index + 2];
+
+            if (trianglePredicate && !trianglePredicate(p0, p1, p2, ray)) {
+                continue;
+            }
+
+            var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
+
+            if (currentIntersectInfo) {
+                if (currentIntersectInfo.distance < 0) {
+                    continue;
+                }
+
+                if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
+                    intersectInfo = currentIntersectInfo;
+                    intersectInfo.faceId = index / 3;
+
+                    if (fastCheck) {
+                        break;
+                    }
+                }
+            }
+        }
+        return intersectInfo;
+    }
+
+    /** @hidden */
     public _rebuild(): void {
         if (this._linesIndexBuffer) {
             this._linesIndexBuffer = null;
@@ -518,4 +557,4 @@ export class SubMesh extends BaseSubMesh implements ICullable {
 
         return new SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex + 1, startIndex, indexCount, mesh, renderingMesh);
     }
-}
+}