LineSegmentsGeometry.js 4.9 KB

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