2
0

LineSegmentsGeometry.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. console.error( 'THREE.LineSegmentsGeometry no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
  74. return;
  75. } else if ( geometry.isBufferGeometry ) {
  76. this.setPositions( geometry.attributes.position.array ); // assumes non-indexed
  77. }
  78. // set colors, maybe
  79. return this;
  80. },
  81. computeBoundingBox: function () {
  82. var box = new THREE.Box3();
  83. return function computeBoundingBox() {
  84. if ( this.boundingBox === null ) {
  85. this.boundingBox = new THREE.Box3();
  86. }
  87. var start = this.attributes.instanceStart;
  88. var end = this.attributes.instanceEnd;
  89. if ( start !== undefined && end !== undefined ) {
  90. this.boundingBox.setFromBufferAttribute( start );
  91. box.setFromBufferAttribute( end );
  92. this.boundingBox.union( box );
  93. }
  94. };
  95. }(),
  96. computeBoundingSphere: function () {
  97. var vector = new THREE.Vector3();
  98. return function computeBoundingSphere() {
  99. if ( this.boundingSphere === null ) {
  100. this.boundingSphere = new THREE.Sphere();
  101. }
  102. if ( this.boundingBox === null ) {
  103. this.computeBoundingBox();
  104. }
  105. var start = this.attributes.instanceStart;
  106. var end = this.attributes.instanceEnd;
  107. if ( start !== undefined && end !== undefined ) {
  108. var center = this.boundingSphere.center;
  109. this.boundingBox.getCenter( center );
  110. var maxRadiusSq = 0;
  111. for ( var i = 0, il = start.count; i < il; i ++ ) {
  112. vector.fromBufferAttribute( start, i );
  113. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
  114. vector.fromBufferAttribute( end, i );
  115. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
  116. }
  117. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  118. if ( isNaN( this.boundingSphere.radius ) ) {
  119. console.error( 'THREE.LineSegmentsGeometry.computeBoundingSphere(): Computed radius is NaN. The instanced position data is likely to have NaN values.', this );
  120. }
  121. }
  122. };
  123. }(),
  124. toJSON: function () {
  125. // todo
  126. },
  127. applyMatrix: function ( matrix ) {
  128. console.warn( 'THREE.LineSegmentsGeometry: applyMatrix() has been renamed to applyMatrix4().' );
  129. return this.applyMatrix4( matrix );
  130. }
  131. } );