LineSegments2.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @author WestLangley / http://github.com/WestLangley
  3. *
  4. */
  5. import {
  6. InstancedInterleavedBuffer,
  7. InterleavedBufferAttribute,
  8. Line3,
  9. Math as _Math,
  10. Matrix4,
  11. Mesh,
  12. Vector3,
  13. Vector4
  14. } from "../../../build/three.module.js";
  15. import { LineSegmentsGeometry } from "../lines/LineSegmentsGeometry.js";
  16. import { LineMaterial } from "../lines/LineMaterial.js";
  17. var LineSegments2 = function ( geometry, material ) {
  18. Mesh.call( this );
  19. this.type = 'LineSegments2';
  20. this.geometry = geometry !== undefined ? geometry : new LineSegmentsGeometry();
  21. this.material = material !== undefined ? material : new LineMaterial( { color: Math.random() * 0xffffff } );
  22. };
  23. LineSegments2.prototype = Object.assign( Object.create( Mesh.prototype ), {
  24. constructor: LineSegments2,
  25. isLineSegments2: true,
  26. computeLineDistances: ( function () { // for backwards-compatability, but could be a method of LineSegmentsGeometry...
  27. var start = new Vector3();
  28. var end = new Vector3();
  29. return function computeLineDistances() {
  30. var geometry = this.geometry;
  31. var instanceStart = geometry.attributes.instanceStart;
  32. var instanceEnd = geometry.attributes.instanceEnd;
  33. var lineDistances = new Float32Array( 2 * instanceStart.data.count );
  34. for ( var i = 0, j = 0, l = instanceStart.data.count; i < l; i ++, j += 2 ) {
  35. start.fromBufferAttribute( instanceStart, i );
  36. end.fromBufferAttribute( instanceEnd, i );
  37. lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
  38. lineDistances[ j + 1 ] = lineDistances[ j ] + start.distanceTo( end );
  39. }
  40. var instanceDistanceBuffer = new InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  41. geometry.setAttribute( 'instanceDistanceStart', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  42. geometry.setAttribute( 'instanceDistanceEnd', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  43. return this;
  44. };
  45. }() ),
  46. raycast: ( function () {
  47. var start = new Vector4();
  48. var end = new Vector4();
  49. var ssOrigin = new Vector4();
  50. var ssOrigin3 = new Vector3();
  51. var mvMatrix = new Matrix4();
  52. var line = new Line3();
  53. var closestPoint = new Vector3();
  54. return function raycast( raycaster, intersects ) {
  55. if ( raycaster.camera === null ) {
  56. console.error( 'LineSegments2: "Raycaster.camera" needs to be set in order to raycast against LineSegments2.' );
  57. }
  58. var ray = raycaster.ray;
  59. var camera = raycaster.camera;
  60. var projectionMatrix = camera.projectionMatrix;
  61. var geometry = this.geometry;
  62. var material = this.material;
  63. var resolution = material.resolution;
  64. var lineWidth = material.linewidth;
  65. var instanceStart = geometry.attributes.instanceStart;
  66. var instanceEnd = geometry.attributes.instanceEnd;
  67. // ndc space [ - 1.0, 1.0 ]
  68. ray.at( 1, ssOrigin );
  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 = _Math.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 };