LineSegments2.js 5.5 KB

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