LineSegmentsGeometry.js 4.8 KB

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