LineSegmentsGeometry.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ( function () {
  2. const _box = new THREE.Box3();
  3. const _vector = new THREE.Vector3();
  4. class LineSegmentsGeometry extends THREE.InstancedBufferGeometry {
  5. constructor() {
  6. super();
  7. this.isLineSegmentsGeometry = true;
  8. this.type = 'LineSegmentsGeometry';
  9. const positions = [ - 1, 2, 0, 1, 2, 0, - 1, 1, 0, 1, 1, 0, - 1, 0, 0, 1, 0, 0, - 1, - 1, 0, 1, - 1, 0 ];
  10. const uvs = [ - 1, 2, 1, 2, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 2, 1, - 2 ];
  11. const index = [ 0, 2, 1, 2, 3, 1, 2, 4, 3, 4, 5, 3, 4, 6, 5, 6, 7, 5 ];
  12. this.setIndex( index );
  13. this.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  14. this.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  15. }
  16. applyMatrix4( matrix ) {
  17. const start = this.attributes.instanceStart;
  18. const end = this.attributes.instanceEnd;
  19. if ( start !== undefined ) {
  20. start.applyMatrix4( matrix );
  21. end.applyMatrix4( matrix );
  22. start.needsUpdate = true;
  23. }
  24. if ( this.boundingBox !== null ) {
  25. this.computeBoundingBox();
  26. }
  27. if ( this.boundingSphere !== null ) {
  28. this.computeBoundingSphere();
  29. }
  30. return this;
  31. }
  32. setPositions( array ) {
  33. let lineSegments;
  34. if ( array instanceof Float32Array ) {
  35. lineSegments = array;
  36. } else if ( Array.isArray( array ) ) {
  37. lineSegments = new Float32Array( array );
  38. }
  39. const instanceBuffer = new THREE.InstancedInterleavedBuffer( lineSegments, 6, 1 ); // xyz, xyz
  40. this.setAttribute( 'instanceStart', new THREE.InterleavedBufferAttribute( instanceBuffer, 3, 0 ) ); // xyz
  41. this.setAttribute( 'instanceEnd', new THREE.InterleavedBufferAttribute( instanceBuffer, 3, 3 ) ); // xyz
  42. //
  43. this.computeBoundingBox();
  44. this.computeBoundingSphere();
  45. return this;
  46. }
  47. setColors( array ) {
  48. let colors;
  49. if ( array instanceof Float32Array ) {
  50. colors = array;
  51. } else if ( Array.isArray( array ) ) {
  52. colors = new Float32Array( array );
  53. }
  54. const instanceColorBuffer = new THREE.InstancedInterleavedBuffer( colors, 6, 1 ); // rgb, rgb
  55. this.setAttribute( 'instanceColorStart', new THREE.InterleavedBufferAttribute( instanceColorBuffer, 3, 0 ) ); // rgb
  56. this.setAttribute( 'instanceColorEnd', new THREE.InterleavedBufferAttribute( instanceColorBuffer, 3, 3 ) ); // rgb
  57. return this;
  58. }
  59. fromWireframeGeometry( geometry ) {
  60. this.setPositions( geometry.attributes.position.array );
  61. return this;
  62. }
  63. fromEdgesGeometry( geometry ) {
  64. this.setPositions( geometry.attributes.position.array );
  65. return this;
  66. }
  67. fromMesh( mesh ) {
  68. this.fromWireframeGeometry( new THREE.WireframeGeometry( mesh.geometry ) ); // set colors, maybe
  69. return this;
  70. }
  71. fromLineSegments( lineSegments ) {
  72. const geometry = lineSegments.geometry;
  73. this.setPositions( geometry.attributes.position.array ); // assumes non-indexed
  74. // set colors, maybe
  75. return this;
  76. }
  77. computeBoundingBox() {
  78. if ( this.boundingBox === null ) {
  79. this.boundingBox = new THREE.Box3();
  80. }
  81. const start = this.attributes.instanceStart;
  82. const end = this.attributes.instanceEnd;
  83. if ( start !== undefined && end !== undefined ) {
  84. this.boundingBox.setFromBufferAttribute( start );
  85. _box.setFromBufferAttribute( end );
  86. this.boundingBox.union( _box );
  87. }
  88. }
  89. computeBoundingSphere() {
  90. if ( this.boundingSphere === null ) {
  91. this.boundingSphere = new THREE.Sphere();
  92. }
  93. if ( this.boundingBox === null ) {
  94. this.computeBoundingBox();
  95. }
  96. const start = this.attributes.instanceStart;
  97. const end = this.attributes.instanceEnd;
  98. if ( start !== undefined && end !== undefined ) {
  99. const center = this.boundingSphere.center;
  100. this.boundingBox.getCenter( center );
  101. let maxRadiusSq = 0;
  102. for ( let i = 0, il = start.count; i < il; i ++ ) {
  103. _vector.fromBufferAttribute( start, i );
  104. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
  105. _vector.fromBufferAttribute( end, i );
  106. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
  107. }
  108. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  109. if ( isNaN( this.boundingSphere.radius ) ) {
  110. console.error( 'THREE.LineSegmentsGeometry.computeBoundingSphere(): Computed radius is NaN. The instanced position data is likely to have NaN values.', this );
  111. }
  112. }
  113. }
  114. toJSON() { // todo
  115. }
  116. applyMatrix( matrix ) {
  117. console.warn( 'THREE.LineSegmentsGeometry: applyMatrix() has been renamed to applyMatrix4().' );
  118. return this.applyMatrix4( matrix );
  119. }
  120. }
  121. THREE.LineSegmentsGeometry = LineSegmentsGeometry;
  122. } )();