LineSegments2.js 7.8 KB

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