LineSegments2.js 5.5 KB

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