Garrett Johnson 5 лет назад
Родитель
Сommit
76b55e4e71
1 измененных файлов с 60 добавлено и 18 удалено
  1. 60 18
      examples/js/lines/LineSegments2.js

+ 60 - 18
examples/js/lines/LineSegments2.js

@@ -1,4 +1,4 @@
-import { Matrix4, Vector3, Vector2, PositionalAudio, Line } from "../../../build/three.module.js";
+import { Matrix4, Vector3, Line, Math as MathUtils } from "../../../build/three.module.js";
 
 /**
  * @author WestLangley / http://github.com/WestLangley
@@ -61,12 +61,12 @@ THREE.LineSegments2.prototype = Object.assign( Object.create( THREE.Mesh.prototy
 		var start = new Vector4();
 		var end = new Vector4();
 
-		var origin = new Vector4();
+		var ssOrigin = new Vector4();
 		var mvMatrix = new Matrix4();
 		var line = new Line();
 		var closestPoint = new Vector3();
 
-		return function raycast( raycaster, insterescts ) {
+		return function raycast( raycaster, intersects ) {
 
 			if ( raycaster.camera === null ) {
 
@@ -86,22 +86,20 @@ THREE.LineSegments2.prototype = Object.assign( Object.create( THREE.Mesh.prototy
 			var instanceStart = geometry.attributes.instanceStart;
 			var instanceEnd = geometry.attributes.instanceEnd;
 
-			var isPerspective = projectionMatrix[ 2 ][ 3 ] === -1;
-			var aspect = resolution.x / resolution.y;
-
 			// ndc space [ - 0.5, 0.5 ]
-			origin.copy( ray.origin );
-			origin.w = 1;
-			origin.applyMatrix4( camera.matrixWorldInverse );
-			origin.applyMatrix4( projectionMatrix );
-			origin.multiplyScalar( 1 / origin.w );
+			ssOrigin.copy( ray.origin );
+			ssOrigin.w = 1;
+			ssOrigin.applyMatrix4( camera.matrixWorldInverse );
+			ssOrigin.applyMatrix4( projectionMatrix );
+			ssOrigin.multiplyScalar( 1 / ssOrigin.w );
 
 			// screen space
-			origin.x *= resolution.x;
-			origin.y *= resolution.y;
-			origin.z = 0;
+			ssOrigin.x *= resolution.x;
+			ssOrigin.y *= resolution.y;
+			ssOrigin.z = 0;
 
-			mvMatrix.multiplyMatrices( camera.matrixWorldInverse, this.matrixWorld );
+			var matrixWorld = this.matrixWorld;
+			mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld );
 
 			for ( var i = 0, l = instanceStart.count; i < l; i ++ ) {
 
@@ -121,6 +119,20 @@ THREE.LineSegments2.prototype = Object.assign( Object.create( THREE.Mesh.prototy
 				start.applyMatrix4( projectionMatrix );
 				end.applyMatrix4( projectionMatrix );
 
+				// segment is behind camera near
+				if ( start.z > 0 && end.z > 0 ) {
+
+					continue;
+
+				}
+
+				// segment is in front of camera far
+				if ( start.z < - 1 && end.z < - 1 ) {
+
+					continue;
+
+				}
+
 				// ndc space [ - 0.5, 0.5 ]
 				start.multiplyScalar( 1 / start.w );
 				end.multiplyScalar( 1 / end.w );
@@ -132,18 +144,48 @@ THREE.LineSegments2.prototype = Object.assign( Object.create( THREE.Mesh.prototy
 				end.y *= resolution.y;
 				end.y *= resolution.y;
 
+				// create 2d segment
 				line.start.copy( start );
 				line.start.z = 0;
 
 				line.end.copy( end );
 				line.end.z = 0;
 
-				var param = line.closestPointToPointParameter( origin, true );
+				// get closest point on ray to segment
+				var param = line.closestPointToPointParameter( ssOrigin, true );
 				line.at( param, closestPoint );
 
-				if ( origin.distanceTo( closestPoint ) < lineWidth * 0.5 ) {
+				// check if the intersection point is within clip space
+				var zPos = MathUtils.lerp( start.z, end.z, param );
+				var isInClipSpace = zPos < 0 && zPos > - 1;
+
+				if ( isInClipSpace && ssOrigin.distanceTo( closestPoint ) < lineWidth * 0.5 ) {
+
+					line.start.fromBufferAttribute( instanceStart, i );
+					line.end.fromBufferAttribute( instanceEnd, i );
+
+					line.start.applyMatrix4( matrixWorld );
+					line.end.applyMatrix4( matrixWorld );
+
+					var pointOnLine = new Vector3();
+					line.at( param, pointOnLine );
+
+					var point = new Vector3();
+					ray.closestPointToPoint( pointOnLine, point );
+
+					intersects.push( {
+
+						point: point,
+						pointOnLine: pointOnLine,
+						distance: ray.origin.distanceTo( point ),
+
+						object: this,
+						face: null,
+						faceIndex: i,
+						uv: null,
+						uv2: null,
 
-					// intersected! output a hit
+					} );
 
 				}