LineSegments2.js 7.8 KB

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