2
0

LineSegments2.js 5.2 KB

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