LineSegments2.js 9.0 KB

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