LineSegments2.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { Matrix4, Vector3, Vector2, PositionalAudio, Line } from "../../../build/three.module.js";
  2. /**
  3. * @author WestLangley / http://github.com/WestLangley
  4. *
  5. */
  6. THREE.LineSegments2 = function ( geometry, material ) {
  7. THREE.Mesh.call( this );
  8. this.type = 'LineSegments2';
  9. this.geometry = geometry !== undefined ? geometry : new THREE.LineSegmentsGeometry();
  10. this.material = material !== undefined ? material : new THREE.LineMaterial( { color: Math.random() * 0xffffff } );
  11. };
  12. THREE.LineSegments2.prototype = Object.assign( Object.create( THREE.Mesh.prototype ), {
  13. constructor: THREE.LineSegments2,
  14. isLineSegments2: true,
  15. computeLineDistances: ( function () { // for backwards-compatability, but could be a method of LineSegmentsGeometry...
  16. var start = new THREE.Vector3();
  17. var end = new THREE.Vector3();
  18. return function computeLineDistances() {
  19. var geometry = this.geometry;
  20. var instanceStart = geometry.attributes.instanceStart;
  21. var instanceEnd = geometry.attributes.instanceEnd;
  22. var lineDistances = new Float32Array( 2 * instanceStart.data.count );
  23. for ( var i = 0, j = 0, l = instanceStart.data.count; i < l; i ++, j += 2 ) {
  24. start.fromBufferAttribute( instanceStart, i );
  25. end.fromBufferAttribute( instanceEnd, i );
  26. lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
  27. lineDistances[ j + 1 ] = lineDistances[ j ] + start.distanceTo( end );
  28. }
  29. var instanceDistanceBuffer = new THREE.InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  30. geometry.setAttribute( 'instanceDistanceStart', new THREE.InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  31. geometry.setAttribute( 'instanceDistanceEnd', new THREE.InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  32. return this;
  33. };
  34. }() ),
  35. raycast: ( function () {
  36. var start = new Vector4();
  37. var end = new Vector4();
  38. var origin = new Vector4();
  39. var mvMatrix = new Matrix4();
  40. var line = new Line();
  41. var closestPoint = new Vector3();
  42. return function raycast( raycaster, insterescts ) {
  43. if ( raycaster.camera === null ) {
  44. console.error( 'THREE.LineSegments2: "Raycaster.camera" needs to be set in order to raycast against sprites.' );
  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. var isPerspective = projectionMatrix[ 2 ][ 3 ] === -1;
  56. var aspect = resolution.x / resolution.y;
  57. // ndc space [ - 0.5, 0.5 ]
  58. origin.copy( ray.origin );
  59. origin.w = 1;
  60. origin.applyMatrix4( camera.matrixWorldInverse );
  61. origin.applyMatrix4( projectionMatrix );
  62. origin.multiplyScalar( 1 / origin.w );
  63. // screen space
  64. origin.x *= resolution.x;
  65. origin.y *= resolution.y;
  66. origin.z = 0;
  67. mvMatrix.multiplyMatrices( camera.matrixWorldInverse, this.matrixWorld );
  68. for ( var i = 0, l = instanceStart.count; i < l; i ++ ) {
  69. // TODO: Maybe have to clip the line based on the camera?
  70. start.fromBufferAttribute( instanceStart, i );
  71. end.fromBufferAttribute( instanceEnd, i );
  72. start.w = 1;
  73. end.w = 1;
  74. // camera space
  75. start.applyMatrix4( mvMatrix );
  76. end.applyMatrix4( mvMatrix );
  77. // clip space
  78. start.applyMatrix4( projectionMatrix );
  79. end.applyMatrix4( projectionMatrix );
  80. // ndc space [ - 0.5, 0.5 ]
  81. start.multiplyScalar( 1 / start.w );
  82. end.multiplyScalar( 1 / end.w );
  83. // screen space
  84. start.x *= resolution.x;
  85. start.y *= resolution.y;
  86. end.y *= resolution.y;
  87. end.y *= resolution.y;
  88. line.start.copy( start );
  89. line.start.z = 0;
  90. line.end.copy( end );
  91. line.end.z = 0;
  92. var param = line.closestPointToPointParameter( origin, true );
  93. line.at( param, closestPoint );
  94. if ( origin.distanceTo( closestPoint ) < lineWidth * 0.5 ) {
  95. // intersected! output a hit
  96. }
  97. }
  98. }
  99. } () )
  100. } );