LineSegments2.js 8.9 KB

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