LineGeometry.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry.js';
  2. /**
  3. * A chain of vertices, forming a polyline.
  4. *
  5. * This is used in {@link Line2} to describe the shape.
  6. *
  7. * ```js
  8. * const points = [
  9. * new THREE.Vector3( - 10, 0, 0 ),
  10. * new THREE.Vector3( 0, 5, 0 ),
  11. * new THREE.Vector3( 10, 0, 0 ),
  12. * ];
  13. *
  14. * const geometry = new LineGeometry();
  15. * geometry.setFromPoints( points );
  16. * ```
  17. *
  18. * @augments LineSegmentsGeometry
  19. * @three_import import { LineLineGeometry2 } from 'three/addons/lines/LineGeometry.js';
  20. */
  21. class LineGeometry extends LineSegmentsGeometry {
  22. /**
  23. * Constructs a new line geometry.
  24. */
  25. constructor() {
  26. super();
  27. /**
  28. * This flag can be used for type testing.
  29. *
  30. * @type {boolean}
  31. * @readonly
  32. * @default true
  33. */
  34. this.isLineGeometry = true;
  35. this.type = 'LineGeometry';
  36. }
  37. /**
  38. * Sets the given line positions for this geometry.
  39. *
  40. * @param {Float32Array|Array<number>} array - The position data to set.
  41. * @return {LineGeometry} A reference to this geometry.
  42. */
  43. setPositions( array ) {
  44. // converts [ x1, y1, z1, x2, y2, z2, ... ] to pairs format
  45. const length = array.length - 3;
  46. const points = new Float32Array( 2 * length );
  47. for ( let i = 0; i < length; i += 3 ) {
  48. points[ 2 * i ] = array[ i ];
  49. points[ 2 * i + 1 ] = array[ i + 1 ];
  50. points[ 2 * i + 2 ] = array[ i + 2 ];
  51. points[ 2 * i + 3 ] = array[ i + 3 ];
  52. points[ 2 * i + 4 ] = array[ i + 4 ];
  53. points[ 2 * i + 5 ] = array[ i + 5 ];
  54. }
  55. super.setPositions( points );
  56. return this;
  57. }
  58. /**
  59. * Sets the given line colors for this geometry.
  60. *
  61. * @param {Float32Array|Array<number>} array - The position data to set.
  62. * @return {LineGeometry} A reference to this geometry.
  63. */
  64. setColors( array ) {
  65. // converts [ r1, g1, b1, r2, g2, b2, ... ] to pairs format
  66. const length = array.length - 3;
  67. const colors = new Float32Array( 2 * length );
  68. for ( let i = 0; i < length; i += 3 ) {
  69. colors[ 2 * i ] = array[ i ];
  70. colors[ 2 * i + 1 ] = array[ i + 1 ];
  71. colors[ 2 * i + 2 ] = array[ i + 2 ];
  72. colors[ 2 * i + 3 ] = array[ i + 3 ];
  73. colors[ 2 * i + 4 ] = array[ i + 4 ];
  74. colors[ 2 * i + 5 ] = array[ i + 5 ];
  75. }
  76. super.setColors( colors );
  77. return this;
  78. }
  79. /**
  80. * Setups this line segments geometry from the given sequence of points.
  81. *
  82. * @param {Array<Vector3|Vector2>} points - An array of points in 2D or 3D space.
  83. * @return {LineGeometry} A reference to this geometry.
  84. */
  85. setFromPoints( points ) {
  86. // converts a vector3 or vector2 array to pairs format
  87. const length = points.length - 1;
  88. const positions = new Float32Array( 6 * length );
  89. for ( let i = 0; i < length; i ++ ) {
  90. positions[ 6 * i ] = points[ i ].x;
  91. positions[ 6 * i + 1 ] = points[ i ].y;
  92. positions[ 6 * i + 2 ] = points[ i ].z || 0;
  93. positions[ 6 * i + 3 ] = points[ i + 1 ].x;
  94. positions[ 6 * i + 4 ] = points[ i + 1 ].y;
  95. positions[ 6 * i + 5 ] = points[ i + 1 ].z || 0;
  96. }
  97. super.setPositions( positions );
  98. return this;
  99. }
  100. /**
  101. * Setups this line segments geometry from the given line.
  102. *
  103. * @param {Line} line - The line that should be used as a data source for this geometry.
  104. * @return {LineGeometry} A reference to this geometry.
  105. */
  106. fromLine( line ) {
  107. const geometry = line.geometry;
  108. this.setPositions( geometry.attributes.position.array ); // assumes non-indexed
  109. // set colors, maybe
  110. return this;
  111. }
  112. }
  113. export { LineGeometry };