LineSegments2.js 9.0 KB

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