LineSegments2.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { Matrix4, Vector3, Vector4, Line3, Math as MathUtils, Mesh, InstancedInterleavedBuffer, InterleavedBufferAttribute } from "../../../build/three.module.js";
  2. import { LineSegmentsGeometry } from "../lines/LineSegmentsGeometry.js";
  3. import { LineMaterial } from "../lines/LineMaterial.js";
  4. /**
  5. * @author WestLangley / http://github.com/WestLangley
  6. *
  7. */
  8. var LineSegments2 = function ( geometry, material ) {
  9. Mesh.call( this );
  10. this.type = 'LineSegments2';
  11. this.geometry = geometry !== undefined ? geometry : new LineSegmentsGeometry();
  12. this.material = material !== undefined ? material : new LineMaterial( { color: Math.random() * 0xffffff } );
  13. };
  14. LineSegments2.prototype = Object.assign( Object.create( Mesh.prototype ), {
  15. constructor: LineSegments2,
  16. isLineSegments2: true,
  17. computeLineDistances: ( function () { // for backwards-compatability, but could be a method of LineSegmentsGeometry...
  18. var start = new Vector3();
  19. var end = new Vector3();
  20. return function computeLineDistances() {
  21. var geometry = this.geometry;
  22. var instanceStart = geometry.attributes.instanceStart;
  23. var instanceEnd = geometry.attributes.instanceEnd;
  24. var lineDistances = new Float32Array( 2 * instanceStart.data.count );
  25. for ( var i = 0, j = 0, l = instanceStart.data.count; i < l; i ++, j += 2 ) {
  26. start.fromBufferAttribute( instanceStart, i );
  27. end.fromBufferAttribute( instanceEnd, i );
  28. lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
  29. lineDistances[ j + 1 ] = lineDistances[ j ] + start.distanceTo( end );
  30. }
  31. var instanceDistanceBuffer = new InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  32. geometry.setAttribute( 'instanceDistanceStart', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  33. geometry.setAttribute( 'instanceDistanceEnd', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  34. return this;
  35. };
  36. }() ),
  37. raycast: ( function () {
  38. var start = new Vector4();
  39. var end = new Vector4();
  40. var ssOrigin = new Vector4();
  41. var ssOrigin3 = new Vector3();
  42. var mvMatrix = new Matrix4();
  43. var line = new Line3();
  44. var closestPoint = new Vector3();
  45. return function raycast( raycaster, intersects ) {
  46. if ( raycaster.camera === null ) {
  47. console.error( 'LineSegments2: "Raycaster.camera" needs to be set in order to raycast against sprites.' );
  48. }
  49. var ray = raycaster.ray;
  50. var camera = raycaster.camera;
  51. var projectionMatrix = camera.projectionMatrix;
  52. var geometry = this.geometry;
  53. var material = this.material;
  54. var resolution = material.resolution;
  55. var lineWidth = material.linewidth;
  56. var instanceStart = geometry.attributes.instanceStart;
  57. var instanceEnd = geometry.attributes.instanceEnd;
  58. // ndc space [ - 0.5, 0.5 ]
  59. ray.at( 1, ssOrigin );
  60. ssOrigin.w = 1;
  61. ssOrigin.applyMatrix4( camera.matrixWorldInverse );
  62. ssOrigin.applyMatrix4( projectionMatrix );
  63. ssOrigin.multiplyScalar( 1 / ssOrigin.w );
  64. // screen space
  65. ssOrigin.x *= resolution.x / 2;
  66. ssOrigin.y *= resolution.y / 2;
  67. ssOrigin.z = 0;
  68. ssOrigin3.copy( ssOrigin );
  69. var matrixWorld = this.matrixWorld;
  70. mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld );
  71. for ( var i = 0, l = instanceStart.count; i < l; i ++ ) {
  72. // TODO: Maybe have to clip the line based on the camera?
  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. // segment is behind camera near
  87. if ( start.z < - 1 && end.z < - 1 ) {
  88. continue;
  89. }
  90. // segment is in front of camera far
  91. if ( start.z > 1 && end.z > 1 ) {
  92. continue;
  93. }
  94. // screen space
  95. start.x *= resolution.x / 2;
  96. start.y *= resolution.y / 2;
  97. end.x *= resolution.x / 2;
  98. end.y *= resolution.y / 2;
  99. // create 2d segment
  100. line.start.copy( start );
  101. line.start.z = 0;
  102. line.end.copy( end );
  103. line.end.z = 0;
  104. // get closest point on ray to segment
  105. var param = line.closestPointToPointParameter( ssOrigin3, true );
  106. line.at( param, closestPoint );
  107. // check if the intersection point is within clip space
  108. var zPos = MathUtils.lerp( start.z, end.z, param );
  109. var isInClipSpace = zPos >= -1 && zPos <= 1;
  110. if ( isInClipSpace && ssOrigin3.distanceTo( closestPoint ) < lineWidth * 0.5 ) {
  111. line.start.fromBufferAttribute( instanceStart, i );
  112. line.end.fromBufferAttribute( instanceEnd, i );
  113. line.start.applyMatrix4( matrixWorld );
  114. line.end.applyMatrix4( matrixWorld );
  115. var pointOnLine = new Vector3();
  116. line.at( param, pointOnLine );
  117. var point = new Vector3();
  118. ray.closestPointToPoint( pointOnLine, point );
  119. intersects.push( {
  120. point: point,
  121. pointOnLine: pointOnLine,
  122. distance: ray.origin.distanceTo( point ),
  123. object: this,
  124. face: null,
  125. faceIndex: i,
  126. uv: null,
  127. uv2: null,
  128. } );
  129. }
  130. }
  131. }
  132. } () )
  133. } );
  134. export { LineSegments2 };