LineSegments2.js 9.4 KB

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