LineSegmentsGeometry.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. this.instanceCount = this.attributes.instanceStart.count;
  52. //
  53. this.computeBoundingBox();
  54. this.computeBoundingSphere();
  55. return this;
  56. }
  57. setColors( array ) {
  58. let colors;
  59. if ( array instanceof Float32Array ) {
  60. colors = array;
  61. } else if ( Array.isArray( array ) ) {
  62. colors = new Float32Array( array );
  63. }
  64. const instanceColorBuffer = new InstancedInterleavedBuffer( colors, 6, 1 ); // rgb, rgb
  65. this.setAttribute( 'instanceColorStart', new InterleavedBufferAttribute( instanceColorBuffer, 3, 0 ) ); // rgb
  66. this.setAttribute( 'instanceColorEnd', new InterleavedBufferAttribute( instanceColorBuffer, 3, 3 ) ); // rgb
  67. return this;
  68. }
  69. fromWireframeGeometry( geometry ) {
  70. this.setPositions( geometry.attributes.position.array );
  71. return this;
  72. }
  73. fromEdgesGeometry( geometry ) {
  74. this.setPositions( geometry.attributes.position.array );
  75. return this;
  76. }
  77. fromMesh( mesh ) {
  78. this.fromWireframeGeometry( new WireframeGeometry( mesh.geometry ) );
  79. // set colors, maybe
  80. return this;
  81. }
  82. fromLineSegments( lineSegments ) {
  83. const geometry = lineSegments.geometry;
  84. this.setPositions( geometry.attributes.position.array ); // assumes non-indexed
  85. // set colors, maybe
  86. return this;
  87. }
  88. computeBoundingBox() {
  89. if ( this.boundingBox === null ) {
  90. this.boundingBox = new Box3();
  91. }
  92. const start = this.attributes.instanceStart;
  93. const end = this.attributes.instanceEnd;
  94. if ( start !== undefined && end !== undefined ) {
  95. this.boundingBox.setFromBufferAttribute( start );
  96. _box.setFromBufferAttribute( end );
  97. this.boundingBox.union( _box );
  98. }
  99. }
  100. computeBoundingSphere() {
  101. if ( this.boundingSphere === null ) {
  102. this.boundingSphere = new Sphere();
  103. }
  104. if ( this.boundingBox === null ) {
  105. this.computeBoundingBox();
  106. }
  107. const start = this.attributes.instanceStart;
  108. const end = this.attributes.instanceEnd;
  109. if ( start !== undefined && end !== undefined ) {
  110. const center = this.boundingSphere.center;
  111. this.boundingBox.getCenter( center );
  112. let maxRadiusSq = 0;
  113. for ( let i = 0, il = start.count; i < il; i ++ ) {
  114. _vector.fromBufferAttribute( start, i );
  115. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
  116. _vector.fromBufferAttribute( end, i );
  117. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
  118. }
  119. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  120. if ( isNaN( this.boundingSphere.radius ) ) {
  121. console.error( 'THREE.LineSegmentsGeometry.computeBoundingSphere(): Computed radius is NaN. The instanced position data is likely to have NaN values.', this );
  122. }
  123. }
  124. }
  125. toJSON() {
  126. // todo
  127. }
  128. applyMatrix( matrix ) {
  129. console.warn( 'THREE.LineSegmentsGeometry: applyMatrix() has been renamed to applyMatrix4().' );
  130. return this.applyMatrix4( matrix );
  131. }
  132. }
  133. export { LineSegmentsGeometry };
粤ICP备19079148号