LineSegments2.js 5.6 KB

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