LineSegmentsGeometry.js 4.8 KB

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