LineSegmentsGeometry.js 4.9 KB

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