LineSegmentsGeometry.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. console.warn( "THREE.LineSegmentsGeometry: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. THREE.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. THREE.LineSegmentsGeometry.prototype = Object.assign( Object.create( THREE.InstancedBufferGeometry.prototype ), {
  13. constructor: THREE.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 ) );
  68. // set colors, maybe
  69. return this;
  70. },
  71. fromLineSegments: function ( lineSegments ) {
  72. var geometry = lineSegments.geometry;
  73. if ( geometry.isGeometry ) {
  74. this.setPositions( geometry.vertices );
  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. } );