2
0

LineSegments2.js 5.0 KB

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