LineGeometry.js 3.3 KB

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