소스 검색

fix Picking on instanced LinesMesh

Julien Barrois 7 년 전
부모
커밋
8063d49884
1개의 변경된 파일45개의 추가작업 그리고 35개의 파일을 삭제
  1. 45 35
      src/Mesh/babylon.subMesh.ts

+ 45 - 35
src/Mesh/babylon.subMesh.ts

@@ -321,8 +321,6 @@
          * @returns intersection info or null if no intersection
          */
         public intersects(ray: Ray, positions: Vector3[], indices: IndicesArray, fastCheck?: boolean): Nullable<IntersectionInfo> {
-            var intersectInfo: Nullable<IntersectionInfo> = null;
-
             const material = this.getMaterial();
             if (!material) {
                 return null;
@@ -339,54 +337,66 @@
             }
 
             // LineMesh first as it's also a Mesh...
-            if (LinesMesh && this._mesh instanceof LinesMesh) {
-                var lineMesh = <LinesMesh>this._mesh;
+            if (LinesMesh) {
+                const mesh = this._mesh instanceof InstancedMesh ? (<InstancedMesh>this._mesh).sourceMesh : this._mesh
+                if (mesh instanceof LinesMesh) {
+                    const linesMesh = <LinesMesh>mesh
+                    return this._intersectLines(ray, positions, indices, linesMesh.intersectionThreshold, fastCheck);
+                }
+            }
 
-                // Line test
-                for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 2) {
-                    var p0 = positions[indices[index]];
-                    var p1 = positions[indices[index + 1]];
+            return this._intersectTriangles(ray, positions, indices, fastCheck);
+        }
 
-                    var length = ray.intersectionSegment(p0, p1, lineMesh.intersectionThreshold);
-                    if (length < 0) {
-                        continue;
-                    }
+        /** @hidden */
+        private _intersectLines(ray: Ray, positions: Vector3[], indices: IndicesArray, intersectionThreshold:number, fastCheck?: boolean): Nullable<IntersectionInfo> {
+            var intersectInfo: Nullable<IntersectionInfo> = null;
 
-                    if (fastCheck || !intersectInfo || length < intersectInfo.distance) {
-                        intersectInfo = new IntersectionInfo(null, null, length);
+            // Line test
+            for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 2) {
+                var p0 = positions[indices[index]];
+                var p1 = positions[indices[index + 1]];
 
-                        if (fastCheck) {
-                            break;
-                        }
+                var length = ray.intersectionSegment(p0, p1, intersectionThreshold);
+                if (length < 0) {
+                    continue;
+                }
+
+                if (fastCheck || !intersectInfo || length < intersectInfo.distance) {
+                    intersectInfo = new IntersectionInfo(null, null, length);
+
+                    if (fastCheck) {
+                        break;
                     }
                 }
             }
-            else {
-                // Triangles test
-                for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
-                    var p0 = positions[indices[index]];
-                    var p1 = positions[indices[index + 1]];
-                    var p2 = positions[indices[index + 2]];
+            return intersectInfo;
+        }
+        private _intersectTriangles(ray: Ray, positions: Vector3[], indices: IndicesArray, fastCheck?: boolean): Nullable<IntersectionInfo> {
+            var intersectInfo: Nullable<IntersectionInfo> = null;
+            // Triangles test
+            for (var index = this.indexStart; index < this.indexStart + this.indexCount; index += 3) {
+                var p0 = positions[indices[index]];
+                var p1 = positions[indices[index + 1]];
+                var p2 = positions[indices[index + 2]];
 
-                    var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
+                var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
 
-                    if (currentIntersectInfo) {
-                        if (currentIntersectInfo.distance < 0) {
-                            continue;
-                        }
+                if (currentIntersectInfo) {
+                    if (currentIntersectInfo.distance < 0) {
+                        continue;
+                    }
 
-                        if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
-                            intersectInfo = currentIntersectInfo;
-                            intersectInfo.faceId = index / 3;
+                    if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
+                        intersectInfo = currentIntersectInfo;
+                        intersectInfo.faceId = index / 3;
 
-                            if (fastCheck) {
-                                break;
-                            }
+                        if (fastCheck) {
+                            break;
                         }
                     }
                 }
             }
-
             return intersectInfo;
         }