LineSegmentsGeometry.js 4.9 KB

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