LineSegments2.js 5.4 KB

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