소스 검색

Merge pull request #5248 from barroij/fixLinesMeshInstancePicking

Fix linesMesh instance picking
David Catuhe 7 년 전
부모
커밋
b3916275b1
2개의 변경된 파일58개의 추가작업 그리고 45개의 파일을 삭제
  1. 11 10
      src/Math/babylon.math.ts
  2. 47 35
      src/Mesh/babylon.subMesh.ts

+ 11 - 10
src/Math/babylon.math.ts

@@ -1900,9 +1900,9 @@
          * @returns the current updated Vector3  
          * @returns the current updated Vector3  
          */
          */
         public minimizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
         public minimizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
-            this.x = Math.min(this.x, x);
-            this.y = Math.min(this.y, y);
-            this.z = Math.min(this.z, z);
+            if (x < this.x) this.x = x;
+            if (y < this.y) this.y = y;
+            if (z < this.z) this.z = z;
             return this;
             return this;
         }
         }
 
 
@@ -1914,9 +1914,9 @@
          * @returns the current updated Vector3
          * @returns the current updated Vector3
          */
          */
         public maximizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
         public maximizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
-            this.x = Math.max(this.x, x);
-            this.y = Math.max(this.y, y);
-            this.z = Math.max(this.z, z);
+            if (x > this.x) this.x = x;
+            if (y > this.y) this.y = y;
+            if (z > this.z) this.z = z;
             return this;
             return this;
         }
         }
 
 
@@ -2091,10 +2091,11 @@
          * @return the angle between vector0 and vector1
          * @return the angle between vector0 and vector1
          */
          */
         public static GetAngleBetweenVectors(vector0: Vector3, vector1: Vector3, normal: Vector3): number {
         public static GetAngleBetweenVectors(vector0: Vector3, vector1: Vector3, normal: Vector3): number {
-            var v0: Vector3 = vector0.clone().normalize();
-            var v1: Vector3 = vector1.clone().normalize();
-            var dot: number = Vector3.Dot(v0, v1);
-            var n = Vector3.Cross(v0, v1);
+            const v0: Vector3 = Tmp.Vector3[0].copyFrom(vector0).normalize();
+            const v1: Vector3 = Tmp.Vector3[1].copyFrom(vector1).normalize();
+            const dot: number = Vector3.Dot(v0, v1);
+            const n = Tmp.Vector3[2];
+            Vector3.CrossToRef(v0, v1, n);
             if (Vector3.Dot(n, normal) > 0) {
             if (Vector3.Dot(n, normal) > 0) {
                 return Math.acos(dot);
                 return Math.acos(dot);
             }
             }

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

@@ -321,8 +321,6 @@
          * @returns intersection info or null if no intersection
          * @returns intersection info or null if no intersection
          */
          */
         public intersects(ray: Ray, positions: Vector3[], indices: IndicesArray, fastCheck?: boolean): Nullable<IntersectionInfo> {
         public intersects(ray: Ray, positions: Vector3[], indices: IndicesArray, fastCheck?: boolean): Nullable<IntersectionInfo> {
-            var intersectInfo: Nullable<IntersectionInfo> = null;
-
             const material = this.getMaterial();
             const material = this.getMaterial();
             if (!material) {
             if (!material) {
                 return null;
                 return null;
@@ -339,54 +337,68 @@
             }
             }
 
 
             // LineMesh first as it's also a Mesh...
             // 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;
+        }
 
 
-                    var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
+        /** @hidden */
+        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]];
 
 
-                    if (currentIntersectInfo) {
-                        if (currentIntersectInfo.distance < 0) {
-                            continue;
-                        }
+                var currentIntersectInfo = ray.intersectsTriangle(p0, p1, p2);
 
 
-                        if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
-                            intersectInfo = currentIntersectInfo;
-                            intersectInfo.faceId = index / 3;
+                if (currentIntersectInfo) {
+                    if (currentIntersectInfo.distance < 0) {
+                        continue;
+                    }
+
+                    if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
+                        intersectInfo = currentIntersectInfo;
+                        intersectInfo.faceId = index / 3;
 
 
-                            if (fastCheck) {
-                                break;
-                            }
+                        if (fastCheck) {
+                            break;
                         }
                         }
                     }
                     }
                 }
                 }
             }
             }
-
             return intersectInfo;
             return intersectInfo;
         }
         }