LineSegmentsGeometry.js 4.5 KB

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