LineSegments2.js 7.9 KB

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