LineSegmentsGeometry.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ( function () {
  2. var LineSegmentsGeometry = function () {
  3. THREE.InstancedBufferGeometry.call( this );
  4. this.type = 'LineSegmentsGeometry';
  5. 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 ];
  6. var uvs = [ - 1, 2, 1, 2, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 2, 1, - 2 ];
  7. var index = [ 0, 2, 1, 2, 3, 1, 2, 4, 3, 4, 5, 3, 4, 6, 5, 6, 7, 5 ];
  8. this.setIndex( index );
  9. this.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  10. this.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  11. };
  12. LineSegmentsGeometry.prototype = Object.assign( Object.create( THREE.InstancedBufferGeometry.prototype ), {
  13. constructor: LineSegmentsGeometry,
  14. isLineSegmentsGeometry: true,
  15. applyMatrix4: function ( matrix ) {
  16. var start = this.attributes.instanceStart;
  17. var 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: function ( array ) {
  32. var lineSegments;
  33. if ( array instanceof Float32Array ) {
  34. lineSegments = array;
  35. } else if ( Array.isArray( array ) ) {
  36. lineSegments = new Float32Array( array );
  37. }
  38. var 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: function ( array ) {
  47. var colors;
  48. if ( array instanceof Float32Array ) {
  49. colors = array;
  50. } else if ( Array.isArray( array ) ) {
  51. colors = new Float32Array( array );
  52. }
  53. var 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: function ( geometry ) {
  59. this.setPositions( geometry.attributes.position.array );
  60. return this;
  61. },
  62. fromEdgesGeometry: function ( geometry ) {
  63. this.setPositions( geometry.attributes.position.array );
  64. return this;
  65. },
  66. fromMesh: function ( mesh ) {
  67. this.fromWireframeGeometry( new THREE.WireframeGeometry( mesh.geometry ) ); // set colors, maybe
  68. return this;
  69. },
  70. fromLineSegments: function ( lineSegments ) {
  71. var 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: 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 () { // todo
  124. },
  125. applyMatrix: function ( matrix ) {
  126. console.warn( 'THREE.LineSegmentsGeometry: applyMatrix() has been renamed to applyMatrix4().' );
  127. return this.applyMatrix4( matrix );
  128. }
  129. } );
  130. THREE.LineSegmentsGeometry = LineSegmentsGeometry;
  131. } )();