LineSegments2.js 9.0 KB

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