LineSegments2.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. THREE.LineSegments2 = function ( geometry, material ) {
  2. if ( geometry === undefined ) geometry = new THREE.LineSegmentsGeometry();
  3. if ( material === undefined ) material = new THREE.LineMaterial( { color: Math.random() * 0xffffff } );
  4. THREE.Mesh.call( this, geometry, material );
  5. this.type = 'LineSegments2';
  6. };
  7. THREE.LineSegments2.prototype = Object.assign( Object.create( THREE.Mesh.prototype ), {
  8. constructor: THREE.LineSegments2,
  9. isLineSegments2: true,
  10. computeLineDistances: ( function () { // for backwards-compatability, but could be a method of LineSegmentsGeometry...
  11. var start = new THREE.Vector3();
  12. var end = new THREE.Vector3();
  13. return function computeLineDistances() {
  14. var geometry = this.geometry;
  15. var instanceStart = geometry.attributes.instanceStart;
  16. var instanceEnd = geometry.attributes.instanceEnd;
  17. var lineDistances = new Float32Array( 2 * instanceStart.count );
  18. for ( var i = 0, j = 0, l = instanceStart.count; i < l; i ++, j += 2 ) {
  19. start.fromBufferAttribute( instanceStart, i );
  20. end.fromBufferAttribute( instanceEnd, i );
  21. lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
  22. lineDistances[ j + 1 ] = lineDistances[ j ] + start.distanceTo( end );
  23. }
  24. var instanceDistanceBuffer = new THREE.InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  25. geometry.setAttribute( 'instanceDistanceStart', new THREE.InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  26. geometry.setAttribute( 'instanceDistanceEnd', new THREE.InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  27. return this;
  28. };
  29. }() ),
  30. raycast: ( function () {
  31. var start = new THREE.Vector4();
  32. var end = new THREE.Vector4();
  33. var ssOrigin = new THREE.Vector4();
  34. var ssOrigin3 = new THREE.Vector3();
  35. var mvMatrix = new THREE.Matrix4();
  36. var line = new THREE.Line3();
  37. var closestPoint = new THREE.Vector3();
  38. var box = new THREE.Box3();
  39. var sphere = new THREE.Sphere();
  40. var clipToWorldVector = new THREE.Vector4();
  41. return function raycast( raycaster, intersects ) {
  42. if ( raycaster.camera === null ) {
  43. console.error( 'LineSegments2: "Raycaster.camera" needs to be set in order to raycast against LineSegments2.' );
  44. }
  45. var threshold = ( raycaster.params.Line2 !== undefined ) ? raycaster.params.Line2.threshold || 0 : 0;
  46. var ray = raycaster.ray;
  47. var camera = raycaster.camera;
  48. var projectionMatrix = camera.projectionMatrix;
  49. var matrixWorld = this.matrixWorld;
  50. var geometry = this.geometry;
  51. var material = this.material;
  52. var resolution = material.resolution;
  53. var lineWidth = material.linewidth + threshold;
  54. var instanceStart = geometry.attributes.instanceStart;
  55. var instanceEnd = geometry.attributes.instanceEnd;
  56. // camera forward is negative
  57. var near = - camera.near;
  58. // clip space is [ - 1, 1 ] so multiply by two to get the full
  59. // width in clip space
  60. var ssMaxWidth = 2.0 * Math.max( lineWidth / resolution.width, lineWidth / resolution.height );
  61. //
  62. // check if we intersect the sphere bounds
  63. if ( geometry.boundingSphere === null ) {
  64. geometry.computeBoundingSphere();
  65. }
  66. sphere.copy( geometry.boundingSphere ).applyMatrix4( matrixWorld );
  67. var distanceToSphere = Math.max( camera.near, sphere.distanceToPoint( ray.origin ) );
  68. // get the w component to scale the world space line width
  69. clipToWorldVector.set( 0, 0, - distanceToSphere, 1.0 ).applyMatrix4( camera.projectionMatrix );
  70. clipToWorldVector.multiplyScalar( 1.0 / clipToWorldVector.w );
  71. clipToWorldVector.applyMatrix4( camera.projectionMatrixInverse );
  72. // increase the sphere bounds by the worst case line screen space width
  73. var sphereMargin = Math.abs( ssMaxWidth / clipToWorldVector.w ) * 0.5;
  74. sphere.radius += sphereMargin;
  75. if ( raycaster.ray.intersectsSphere( sphere ) === false ) {
  76. return;
  77. }
  78. //
  79. // check if we intersect the box bounds
  80. if ( geometry.boundingBox === null ) {
  81. geometry.computeBoundingBox();
  82. }
  83. box.copy( geometry.boundingBox ).applyMatrix4( matrixWorld );
  84. var distanceToBox = Math.max( camera.near, box.distanceToPoint( ray.origin ) );
  85. // get the w component to scale the world space line width
  86. clipToWorldVector.set( 0, 0, - distanceToBox, 1.0 ).applyMatrix4( camera.projectionMatrix );
  87. clipToWorldVector.multiplyScalar( 1.0 / clipToWorldVector.w );
  88. clipToWorldVector.applyMatrix4( camera.projectionMatrixInverse );
  89. // increase the sphere bounds by the worst case line screen space width
  90. var boxMargin = Math.abs( ssMaxWidth / clipToWorldVector.w ) * 0.5;
  91. box.max.x += boxMargin;
  92. box.max.y += boxMargin;
  93. box.max.z += boxMargin;
  94. box.min.x -= boxMargin;
  95. box.min.y -= boxMargin;
  96. box.min.z -= boxMargin;
  97. if ( raycaster.ray.intersectsBox( box ) === false ) {
  98. return;
  99. }
  100. //
  101. // pick a point 1 unit out along the ray to avoid the ray origin
  102. // sitting at the camera origin which will cause "w" to be 0 when
  103. // applying the projection matrix.
  104. ray.at( 1, ssOrigin );
  105. // ndc space [ - 1.0, 1.0 ]
  106. ssOrigin.w = 1;
  107. ssOrigin.applyMatrix4( camera.matrixWorldInverse );
  108. ssOrigin.applyMatrix4( projectionMatrix );
  109. ssOrigin.multiplyScalar( 1 / ssOrigin.w );
  110. // screen space
  111. ssOrigin.x *= resolution.x / 2;
  112. ssOrigin.y *= resolution.y / 2;
  113. ssOrigin.z = 0;
  114. ssOrigin3.copy( ssOrigin );
  115. mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld );
  116. for ( var i = 0, l = instanceStart.count; i < l; i ++ ) {
  117. start.fromBufferAttribute( instanceStart, i );
  118. end.fromBufferAttribute( instanceEnd, i );
  119. start.w = 1;
  120. end.w = 1;
  121. // camera space
  122. start.applyMatrix4( mvMatrix );
  123. end.applyMatrix4( mvMatrix );
  124. // skip the segment if it's entirely behind the camera
  125. var isBehindCameraNear = start.z > near && end.z > near;
  126. if ( isBehindCameraNear ) {
  127. continue;
  128. }
  129. // trim the segment if it extends behind camera near
  130. if ( start.z > near ) {
  131. const deltaDist = start.z - end.z;
  132. const t = ( start.z - near ) / deltaDist;
  133. start.lerp( end, t );
  134. } else if ( end.z > near ) {
  135. const deltaDist = end.z - start.z;
  136. const t = ( end.z - near ) / deltaDist;
  137. end.lerp( start, t );
  138. }
  139. // clip space
  140. start.applyMatrix4( projectionMatrix );
  141. end.applyMatrix4( projectionMatrix );
  142. // ndc space [ - 1.0, 1.0 ]
  143. start.multiplyScalar( 1 / start.w );
  144. end.multiplyScalar( 1 / end.w );
  145. // screen space
  146. start.x *= resolution.x / 2;
  147. start.y *= resolution.y / 2;
  148. end.x *= resolution.x / 2;
  149. end.y *= resolution.y / 2;
  150. // create 2d segment
  151. line.start.copy( start );
  152. line.start.z = 0;
  153. line.end.copy( end );
  154. line.end.z = 0;
  155. // get closest point on ray to segment
  156. var param = line.closestPointToPointParameter( ssOrigin3, true );
  157. line.at( param, closestPoint );
  158. // check if the intersection point is within clip space
  159. var zPos = THREE.MathUtils.lerp( start.z, end.z, param );
  160. var isInClipSpace = zPos >= - 1 && zPos <= 1;
  161. var isInside = ssOrigin3.distanceTo( closestPoint ) < lineWidth * 0.5;
  162. if ( isInClipSpace && isInside ) {
  163. line.start.fromBufferAttribute( instanceStart, i );
  164. line.end.fromBufferAttribute( instanceEnd, i );
  165. line.start.applyMatrix4( matrixWorld );
  166. line.end.applyMatrix4( matrixWorld );
  167. var pointOnLine = new THREE.Vector3();
  168. var point = new THREE.Vector3();
  169. ray.distanceSqToSegment( line.start, line.end, point, pointOnLine );
  170. intersects.push( {
  171. point: point,
  172. pointOnLine: pointOnLine,
  173. distance: ray.origin.distanceTo( point ),
  174. object: this,
  175. face: null,
  176. faceIndex: i,
  177. uv: null,
  178. uv2: null,
  179. } );
  180. }
  181. }
  182. };
  183. }() )
  184. } );