0
0

LineSegments2.js 7.8 KB

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