LineSegments2.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { Matrix4, Vector3, Line, Math as MathUtils } from "../../../build/three.module.js";
  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 Vector4();
  37. var end = new Vector4();
  38. var ssOrigin = new Vector4();
  39. var mvMatrix = new Matrix4();
  40. var line = new Line();
  41. var closestPoint = new Vector3();
  42. return function raycast( raycaster, intersects ) {
  43. if ( raycaster.camera === null ) {
  44. console.error( 'THREE.LineSegments2: "Raycaster.camera" needs to be set in order to raycast against sprites.' );
  45. }
  46. var ray = raycaster.ray;
  47. var camera = raycaster.camera;
  48. var projectionMatrix = camera.projectionMatrix;
  49. var geometry = this.geometry;
  50. var material = this.material;
  51. var resolution = material.resolution;
  52. var lineWidth = material.lineWidth;
  53. var instanceStart = geometry.attributes.instanceStart;
  54. var instanceEnd = geometry.attributes.instanceEnd;
  55. // ndc space [ - 0.5, 0.5 ]
  56. ssOrigin.copy( ray.origin );
  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;
  63. ssOrigin.y *= resolution.y;
  64. ssOrigin.z = 0;
  65. var matrixWorld = this.matrixWorld;
  66. mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld );
  67. for ( var i = 0, l = instanceStart.count; i < l; i ++ ) {
  68. // TODO: Maybe have to clip the line based on the camera?
  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. // segment is behind camera near
  80. if ( start.z > 0 && end.z > 0 ) {
  81. continue;
  82. }
  83. // segment is in front of camera far
  84. if ( start.z < - 1 && end.z < - 1 ) {
  85. continue;
  86. }
  87. // ndc space [ - 0.5, 0.5 ]
  88. start.multiplyScalar( 1 / start.w );
  89. end.multiplyScalar( 1 / end.w );
  90. // screen space
  91. start.x *= resolution.x;
  92. start.y *= resolution.y;
  93. end.y *= resolution.y;
  94. end.y *= resolution.y;
  95. // create 2d segment
  96. line.start.copy( start );
  97. line.start.z = 0;
  98. line.end.copy( end );
  99. line.end.z = 0;
  100. // get closest point on ray to segment
  101. var param = line.closestPointToPointParameter( ssOrigin, true );
  102. line.at( param, closestPoint );
  103. // check if the intersection point is within clip space
  104. var zPos = MathUtils.lerp( start.z, end.z, param );
  105. var isInClipSpace = zPos < 0 && zPos > - 1;
  106. if ( isInClipSpace && ssOrigin.distanceTo( closestPoint ) < lineWidth * 0.5 ) {
  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 Vector3();
  112. line.at( param, pointOnLine );
  113. var point = new Vector3();
  114. ray.closestPointToPoint( pointOnLine, point );
  115. intersects.push( {
  116. point: point,
  117. pointOnLine: pointOnLine,
  118. distance: ray.origin.distanceTo( point ),
  119. object: this,
  120. face: null,
  121. faceIndex: i,
  122. uv: null,
  123. uv2: null,
  124. } );
  125. }
  126. }
  127. }
  128. } () )
  129. } );