LineSegments2.js 7.6 KB

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