2
0

LineSegments2.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // camera forward is negative
  65. var near = - camera.near;
  66. // pick a point 1 unit out along the ray to avoid the ray origin
  67. // sitting at the camera origin which will cause "w" to be 0 when
  68. // applying the projection matrix.
  69. ray.at( 1, ssOrigin );
  70. // ndc space [ - 1.0, 1.0 ]
  71. ssOrigin.w = 1;
  72. ssOrigin.applyMatrix4( camera.matrixWorldInverse );
  73. ssOrigin.applyMatrix4( projectionMatrix );
  74. ssOrigin.multiplyScalar( 1 / ssOrigin.w );
  75. // screen space
  76. ssOrigin.x *= resolution.x / 2;
  77. ssOrigin.y *= resolution.y / 2;
  78. ssOrigin.z = 0;
  79. ssOrigin3.copy( ssOrigin );
  80. var matrixWorld = this.matrixWorld;
  81. mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld );
  82. for ( var i = 0, l = instanceStart.count; i < l; i ++ ) {
  83. start.fromBufferAttribute( instanceStart, i );
  84. end.fromBufferAttribute( instanceEnd, i );
  85. start.w = 1;
  86. end.w = 1;
  87. // camera space
  88. start.applyMatrix4( mvMatrix );
  89. end.applyMatrix4( mvMatrix );
  90. // skip the segment if it's entirely behind the camera
  91. var isBehindCameraNear = start.z > near && end.z > near;
  92. if ( isBehindCameraNear ) {
  93. continue;
  94. }
  95. // trim the segment if it extends behind camera near
  96. if ( start.z > near ) {
  97. const deltaDist = start.z - end.z;
  98. const t = ( start.z - near ) / deltaDist;
  99. start.lerp( end, t );
  100. } else if ( end.z > near ) {
  101. const deltaDist = end.z - start.z;
  102. const t = ( end.z - near ) / deltaDist;
  103. end.lerp( start, t );
  104. }
  105. // clip space
  106. start.applyMatrix4( projectionMatrix );
  107. end.applyMatrix4( projectionMatrix );
  108. // ndc space [ - 1.0, 1.0 ]
  109. start.multiplyScalar( 1 / start.w );
  110. end.multiplyScalar( 1 / end.w );
  111. // screen space
  112. start.x *= resolution.x / 2;
  113. start.y *= resolution.y / 2;
  114. end.x *= resolution.x / 2;
  115. end.y *= resolution.y / 2;
  116. // create 2d segment
  117. line.start.copy( start );
  118. line.start.z = 0;
  119. line.end.copy( end );
  120. line.end.z = 0;
  121. // get closest point on ray to segment
  122. var param = line.closestPointToPointParameter( ssOrigin3, true );
  123. line.at( param, closestPoint );
  124. // check if the intersection point is within clip space
  125. var zPos = MathUtils.lerp( start.z, end.z, param );
  126. var isInClipSpace = zPos >= - 1 && zPos <= 1;
  127. var isInside = ssOrigin3.distanceTo( closestPoint ) < lineWidth * 0.5;
  128. if ( isInClipSpace && isInside ) {
  129. line.start.fromBufferAttribute( instanceStart, i );
  130. line.end.fromBufferAttribute( instanceEnd, i );
  131. line.start.applyMatrix4( matrixWorld );
  132. line.end.applyMatrix4( matrixWorld );
  133. var pointOnLine = new Vector3();
  134. var point = new Vector3();
  135. ray.distanceSqToSegment( line.start, line.end, point, pointOnLine );
  136. intersects.push( {
  137. point: point,
  138. pointOnLine: pointOnLine,
  139. distance: ray.origin.distanceTo( point ),
  140. object: this,
  141. face: null,
  142. faceIndex: i,
  143. uv: null,
  144. uv2: null,
  145. } );
  146. }
  147. }
  148. };
  149. }() )
  150. } );
  151. export { LineSegments2 };