LineSegments2.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 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;
  50. var instanceStart = geometry.attributes.instanceStart;
  51. var instanceEnd = geometry.attributes.instanceEnd;
  52. // pick a point 1 unit out along the ray to avoid the ray origin
  53. // sitting at the camera origin which will cause "w" to be 0 when
  54. // applying the projection matrix.
  55. ray.at( 1, ssOrigin );
  56. // ndc space [ - 1.0, 1.0 ]
  57. ssOrigin.w = 1;
  58. ssOrigin.applyMatrix4( camera.matrixWorldInverse );
  59. ssOrigin.applyMatrix4( projectionMatrix );
  60. ssOrigin.multiplyScalar( 1 / ssOrigin.w );
  61. // screen space
  62. ssOrigin.x *= resolution.x / 2;
  63. ssOrigin.y *= resolution.y / 2;
  64. ssOrigin.z = 0;
  65. ssOrigin3.copy( ssOrigin );
  66. var matrixWorld = this.matrixWorld;
  67. mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld );
  68. for ( var i = 0, l = instanceStart.count; i < l; i ++ ) {
  69. start.fromBufferAttribute( instanceStart, i );
  70. end.fromBufferAttribute( instanceEnd, i );
  71. start.w = 1;
  72. end.w = 1;
  73. // camera space
  74. start.applyMatrix4( mvMatrix );
  75. end.applyMatrix4( mvMatrix );
  76. // clip space
  77. start.applyMatrix4( projectionMatrix );
  78. end.applyMatrix4( projectionMatrix );
  79. // ndc space [ - 1.0, 1.0 ]
  80. start.multiplyScalar( 1 / start.w );
  81. end.multiplyScalar( 1 / end.w );
  82. // skip the segment if it's outside the camera near and far planes
  83. var isBehindCameraNear = start.z < - 1 && end.z < - 1;
  84. var isPastCameraFar = start.z > 1 && end.z > 1;
  85. if ( isBehindCameraNear || isPastCameraFar ) {
  86. continue;
  87. }
  88. // screen space
  89. start.x *= resolution.x / 2;
  90. start.y *= resolution.y / 2;
  91. end.x *= resolution.x / 2;
  92. end.y *= resolution.y / 2;
  93. // create 2d segment
  94. line.start.copy( start );
  95. line.start.z = 0;
  96. line.end.copy( end );
  97. line.end.z = 0;
  98. // get closest point on ray to segment
  99. var param = line.closestPointToPointParameter( ssOrigin3, true );
  100. line.at( param, closestPoint );
  101. // check if the intersection point is within clip space
  102. var zPos = THREE.MathUtils.lerp( start.z, end.z, param );
  103. var isInClipSpace = zPos >= - 1 && zPos <= 1;
  104. var isInside = ssOrigin3.distanceTo( closestPoint ) < lineWidth * 0.5;
  105. if ( isInClipSpace && isInside ) {
  106. line.start.fromBufferAttribute( instanceStart, i );
  107. line.end.fromBufferAttribute( instanceEnd, i );
  108. line.start.applyMatrix4( matrixWorld );
  109. line.end.applyMatrix4( matrixWorld );
  110. var pointOnLine = new THREE.Vector3();
  111. var point = new THREE.Vector3();
  112. ray.distanceSqToSegment( line.start, line.end, point, pointOnLine );
  113. intersects.push( {
  114. point: point,
  115. pointOnLine: pointOnLine,
  116. distance: ray.origin.distanceTo( point ),
  117. object: this,
  118. face: null,
  119. faceIndex: i,
  120. uv: null,
  121. uv2: null,
  122. } );
  123. }
  124. }
  125. };
  126. }() )
  127. } );