LineSegments2.js 5.6 KB

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