1
0

BufferAttribute.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. import { Vector3 } from '../math/Vector3.js';
  2. import { Vector2 } from '../math/Vector2.js';
  3. import { denormalize, normalize } from '../math/MathUtils.js';
  4. import { StaticDrawUsage, FloatType } from '../constants.js';
  5. import { fromHalfFloat, toHalfFloat } from '../extras/DataUtils.js';
  6. const _vector = /*@__PURE__*/ new Vector3();
  7. const _vector2 = /*@__PURE__*/ new Vector2();
  8. let _id = 0;
  9. /**
  10. * This class stores data for an attribute (such as vertex positions, face
  11. * indices, normals, colors, UVs, and any custom attributes ) associated with
  12. * a geometry, which allows for more efficient passing of data to the GPU.
  13. *
  14. * When working with vector-like data, the `fromBufferAttribute( attribute, index )`
  15. * helper methods on vector and color class might be helpful. E.g. {@link Vector3#fromBufferAttribute}.
  16. */
  17. class BufferAttribute {
  18. /**
  19. * Constructs a new buffer attribute.
  20. *
  21. * @param {TypedArray} array - The array holding the attribute data.
  22. * @param {number} itemSize - The item size.
  23. * @param {boolean} [normalized=false] - Whether the data are normalized or not.
  24. */
  25. constructor( array, itemSize, normalized = false ) {
  26. if ( Array.isArray( array ) ) {
  27. throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
  28. }
  29. /**
  30. * This flag can be used for type testing.
  31. *
  32. * @type {boolean}
  33. * @readonly
  34. * @default true
  35. */
  36. this.isBufferAttribute = true;
  37. /**
  38. * The ID of the buffer attribute.
  39. *
  40. * @name BufferAttribute#id
  41. * @type {number}
  42. * @readonly
  43. */
  44. Object.defineProperty( this, 'id', { value: _id ++ } );
  45. /**
  46. * The name of the buffer attribute.
  47. *
  48. * @type {string}
  49. */
  50. this.name = '';
  51. /**
  52. * The array holding the attribute data. It should have `itemSize * numVertices`
  53. * elements, where `numVertices` is the number of vertices in the associated geometry.
  54. *
  55. * @type {TypedArray}
  56. */
  57. this.array = array;
  58. /**
  59. * The number of values of the array that should be associated with a particular vertex.
  60. * For instance, if this attribute is storing a 3-component vector (such as a position,
  61. * normal, or color), then the value should be `3`.
  62. *
  63. * @type {number}
  64. */
  65. this.itemSize = itemSize;
  66. /**
  67. * Represents the number of items this buffer attribute stores. It is internally computed
  68. * by dividing the `array` length by the `itemSize`.
  69. *
  70. * @type {number}
  71. * @readonly
  72. */
  73. this.count = array !== undefined ? array.length / itemSize : 0;
  74. /**
  75. * Applies to integer data only. Indicates how the underlying data in the buffer maps to
  76. * the values in the GLSL code. For instance, if `array` is an instance of `UInt16Array`,
  77. * and `normalized` is `true`, the values `0 - +65535` in the array data will be mapped to
  78. * `0.0f - +1.0f` in the GLSL attribute. If `normalized` is `false`, the values will be converted
  79. * to floats unmodified, i.e. `65535` becomes `65535.0f`.
  80. *
  81. * @type {boolean}
  82. */
  83. this.normalized = normalized;
  84. /**
  85. * Defines the intended usage pattern of the data store for optimization purposes.
  86. *
  87. * Note: After the initial use of a buffer, its usage cannot be changed. Instead,
  88. * instantiate a new one and set the desired usage before the next render.
  89. *
  90. * @type {(StaticDrawUsage|DynamicDrawUsage|StreamDrawUsage|StaticReadUsage|DynamicReadUsage|StreamReadUsage|StaticCopyUsage|DynamicCopyUsage|StreamCopyUsage)}
  91. * @default StaticDrawUsage
  92. */
  93. this.usage = StaticDrawUsage;
  94. /**
  95. * This can be used to only update some components of stored vectors (for example, just the
  96. * component related to color). Use the `addUpdateRange()` function to add ranges to this array.
  97. *
  98. * @type {Array<Object>}
  99. */
  100. this.updateRanges = [];
  101. /**
  102. * Configures the bound GPU type for use in shaders.
  103. *
  104. * Note: this only has an effect for integer arrays and is not configurable for float arrays.
  105. * For lower precision float types, use `Float16BufferAttribute`.
  106. *
  107. * @type {(FloatType|IntType)}
  108. * @default FloatType
  109. */
  110. this.gpuType = FloatType;
  111. /**
  112. * A version number, incremented every time the `needsUpdate` is set to `true`.
  113. *
  114. * @type {number}
  115. */
  116. this.version = 0;
  117. }
  118. /**
  119. * A callback function that is executed after the renderer has transferred the attribute
  120. * array data to the GPU.
  121. */
  122. onUploadCallback() {}
  123. /**
  124. * Flag to indicate that this attribute has changed and should be re-sent to
  125. * the GPU. Set this to `true` when you modify the value of the array.
  126. *
  127. * @type {number}
  128. * @default false
  129. * @param {boolean} value
  130. */
  131. set needsUpdate( value ) {
  132. if ( value === true ) this.version ++;
  133. }
  134. /**
  135. * Sets the usage of this buffer attribute.
  136. *
  137. * @param {(StaticDrawUsage|DynamicDrawUsage|StreamDrawUsage|StaticReadUsage|DynamicReadUsage|StreamReadUsage|StaticCopyUsage|DynamicCopyUsage|StreamCopyUsage)} value - The usage to set.
  138. * @return {BufferAttribute} A reference to this buffer attribute.
  139. */
  140. setUsage( value ) {
  141. this.usage = value;
  142. return this;
  143. }
  144. /**
  145. * Adds a range of data in the data array to be updated on the GPU.
  146. *
  147. * @param {number} start - Position at which to start update.
  148. * @param {number} count - The number of components to update.
  149. */
  150. addUpdateRange( start, count ) {
  151. this.updateRanges.push( { start, count } );
  152. }
  153. /**
  154. * Clears the update ranges.
  155. */
  156. clearUpdateRanges() {
  157. this.updateRanges.length = 0;
  158. }
  159. /**
  160. * Copies the values of the given buffer attribute to this instance.
  161. *
  162. * @param {BufferAttribute} source - The buffer attribute to copy.
  163. * @return {BufferAttribute} A reference to this instance.
  164. */
  165. copy( source ) {
  166. this.name = source.name;
  167. this.array = new source.array.constructor( source.array );
  168. this.itemSize = source.itemSize;
  169. this.count = source.count;
  170. this.normalized = source.normalized;
  171. this.usage = source.usage;
  172. this.gpuType = source.gpuType;
  173. return this;
  174. }
  175. /**
  176. * Copies a vector from the given buffer attribute to this one. The start
  177. * and destination position in the attribute buffers are represented by the
  178. * given indices.
  179. *
  180. * @param {number} index1 - The destination index into this buffer attribute.
  181. * @param {BufferAttribute} attribute - The buffer attribute to copy from.
  182. * @param {number} index2 - The source index into the given buffer attribute.
  183. * @return {BufferAttribute} A reference to this instance.
  184. */
  185. copyAt( index1, attribute, index2 ) {
  186. index1 *= this.itemSize;
  187. index2 *= attribute.itemSize;
  188. for ( let i = 0, l = this.itemSize; i < l; i ++ ) {
  189. this.array[ index1 + i ] = attribute.array[ index2 + i ];
  190. }
  191. return this;
  192. }
  193. /**
  194. * Copies the given array data into this buffer attribute.
  195. *
  196. * @param {(TypedArray|Array)} array - The array to copy.
  197. * @return {BufferAttribute} A reference to this instance.
  198. */
  199. copyArray( array ) {
  200. this.array.set( array );
  201. return this;
  202. }
  203. /**
  204. * Applies the given 3x3 matrix to the given attribute. Works with
  205. * item size `2` and `3`.
  206. *
  207. * @param {Matrix3} m - The matrix to apply.
  208. * @return {BufferAttribute} A reference to this instance.
  209. */
  210. applyMatrix3( m ) {
  211. if ( this.itemSize === 2 ) {
  212. for ( let i = 0, l = this.count; i < l; i ++ ) {
  213. _vector2.fromBufferAttribute( this, i );
  214. _vector2.applyMatrix3( m );
  215. this.setXY( i, _vector2.x, _vector2.y );
  216. }
  217. } else if ( this.itemSize === 3 ) {
  218. for ( let i = 0, l = this.count; i < l; i ++ ) {
  219. _vector.fromBufferAttribute( this, i );
  220. _vector.applyMatrix3( m );
  221. this.setXYZ( i, _vector.x, _vector.y, _vector.z );
  222. }
  223. }
  224. return this;
  225. }
  226. /**
  227. * Applies the given 4x4 matrix to the given attribute. Only works with
  228. * item size `3`.
  229. *
  230. * @param {Matrix4} m - The matrix to apply.
  231. * @return {BufferAttribute} A reference to this instance.
  232. */
  233. applyMatrix4( m ) {
  234. for ( let i = 0, l = this.count; i < l; i ++ ) {
  235. _vector.fromBufferAttribute( this, i );
  236. _vector.applyMatrix4( m );
  237. this.setXYZ( i, _vector.x, _vector.y, _vector.z );
  238. }
  239. return this;
  240. }
  241. /**
  242. * Applies the given 3x3 normal matrix to the given attribute. Only works with
  243. * item size `3`.
  244. *
  245. * @param {Matrix3} m - The normal matrix to apply.
  246. * @return {BufferAttribute} A reference to this instance.
  247. */
  248. applyNormalMatrix( m ) {
  249. for ( let i = 0, l = this.count; i < l; i ++ ) {
  250. _vector.fromBufferAttribute( this, i );
  251. _vector.applyNormalMatrix( m );
  252. this.setXYZ( i, _vector.x, _vector.y, _vector.z );
  253. }
  254. return this;
  255. }
  256. /**
  257. * Applies the given 4x4 matrix to the given attribute. Only works with
  258. * item size `3` and with direction vectors.
  259. *
  260. * @param {Matrix4} m - The matrix to apply.
  261. * @return {BufferAttribute} A reference to this instance.
  262. */
  263. transformDirection( m ) {
  264. for ( let i = 0, l = this.count; i < l; i ++ ) {
  265. _vector.fromBufferAttribute( this, i );
  266. _vector.transformDirection( m );
  267. this.setXYZ( i, _vector.x, _vector.y, _vector.z );
  268. }
  269. return this;
  270. }
  271. /**
  272. * Sets the given array data in the buffer attribute.
  273. *
  274. * @param {(TypedArray|Array)} value - The array data to set.
  275. * @param {number} [offset=0] - The offset in this buffer attribute's array.
  276. * @return {BufferAttribute} A reference to this instance.
  277. */
  278. set( value, offset = 0 ) {
  279. // Matching BufferAttribute constructor, do not normalize the array.
  280. this.array.set( value, offset );
  281. return this;
  282. }
  283. /**
  284. * Returns the given component of the vector at the given index.
  285. *
  286. * @param {number} index - The index into the buffer attribute.
  287. * @param {number} component - The component index.
  288. * @return {number} The returned value.
  289. */
  290. getComponent( index, component ) {
  291. let value = this.array[ index * this.itemSize + component ];
  292. if ( this.normalized ) value = denormalize( value, this.array );
  293. return value;
  294. }
  295. /**
  296. * Sets the given value to the given component of the vector at the given index.
  297. *
  298. * @param {number} index - The index into the buffer attribute.
  299. * @param {number} component - The component index.
  300. * @param {number} value - The value to set.
  301. * @return {BufferAttribute} A reference to this instance.
  302. */
  303. setComponent( index, component, value ) {
  304. if ( this.normalized ) value = normalize( value, this.array );
  305. this.array[ index * this.itemSize + component ] = value;
  306. return this;
  307. }
  308. /**
  309. * Returns the x component of the vector at the given index.
  310. *
  311. * @param {number} index - The index into the buffer attribute.
  312. * @return {number} The x component.
  313. */
  314. getX( index ) {
  315. let x = this.array[ index * this.itemSize ];
  316. if ( this.normalized ) x = denormalize( x, this.array );
  317. return x;
  318. }
  319. /**
  320. * Sets the x component of the vector at the given index.
  321. *
  322. * @param {number} index - The index into the buffer attribute.
  323. * @param {number} x - The value to set.
  324. * @return {BufferAttribute} A reference to this instance.
  325. */
  326. setX( index, x ) {
  327. if ( this.normalized ) x = normalize( x, this.array );
  328. this.array[ index * this.itemSize ] = x;
  329. return this;
  330. }
  331. /**
  332. * Returns the y component of the vector at the given index.
  333. *
  334. * @param {number} index - The index into the buffer attribute.
  335. * @return {number} The y component.
  336. */
  337. getY( index ) {
  338. let y = this.array[ index * this.itemSize + 1 ];
  339. if ( this.normalized ) y = denormalize( y, this.array );
  340. return y;
  341. }
  342. /**
  343. * Sets the y component of the vector at the given index.
  344. *
  345. * @param {number} index - The index into the buffer attribute.
  346. * @param {number} y - The value to set.
  347. * @return {BufferAttribute} A reference to this instance.
  348. */
  349. setY( index, y ) {
  350. if ( this.normalized ) y = normalize( y, this.array );
  351. this.array[ index * this.itemSize + 1 ] = y;
  352. return this;
  353. }
  354. /**
  355. * Returns the z component of the vector at the given index.
  356. *
  357. * @param {number} index - The index into the buffer attribute.
  358. * @return {number} The z component.
  359. */
  360. getZ( index ) {
  361. let z = this.array[ index * this.itemSize + 2 ];
  362. if ( this.normalized ) z = denormalize( z, this.array );
  363. return z;
  364. }
  365. /**
  366. * Sets the z component of the vector at the given index.
  367. *
  368. * @param {number} index - The index into the buffer attribute.
  369. * @param {number} z - The value to set.
  370. * @return {BufferAttribute} A reference to this instance.
  371. */
  372. setZ( index, z ) {
  373. if ( this.normalized ) z = normalize( z, this.array );
  374. this.array[ index * this.itemSize + 2 ] = z;
  375. return this;
  376. }
  377. /**
  378. * Returns the w component of the vector at the given index.
  379. *
  380. * @param {number} index - The index into the buffer attribute.
  381. * @return {number} The w component.
  382. */
  383. getW( index ) {
  384. let w = this.array[ index * this.itemSize + 3 ];
  385. if ( this.normalized ) w = denormalize( w, this.array );
  386. return w;
  387. }
  388. /**
  389. * Sets the w component of the vector at the given index.
  390. *
  391. * @param {number} index - The index into the buffer attribute.
  392. * @param {number} w - The value to set.
  393. * @return {BufferAttribute} A reference to this instance.
  394. */
  395. setW( index, w ) {
  396. if ( this.normalized ) w = normalize( w, this.array );
  397. this.array[ index * this.itemSize + 3 ] = w;
  398. return this;
  399. }
  400. /**
  401. * Sets the x and y component of the vector at the given index.
  402. *
  403. * @param {number} index - The index into the buffer attribute.
  404. * @param {number} x - The value for the x component to set.
  405. * @param {number} y - The value for the y component to set.
  406. * @return {BufferAttribute} A reference to this instance.
  407. */
  408. setXY( index, x, y ) {
  409. index *= this.itemSize;
  410. if ( this.normalized ) {
  411. x = normalize( x, this.array );
  412. y = normalize( y, this.array );
  413. }
  414. this.array[ index + 0 ] = x;
  415. this.array[ index + 1 ] = y;
  416. return this;
  417. }
  418. /**
  419. * Sets the x, y and z component of the vector at the given index.
  420. *
  421. * @param {number} index - The index into the buffer attribute.
  422. * @param {number} x - The value for the x component to set.
  423. * @param {number} y - The value for the y component to set.
  424. * @param {number} z - The value for the z component to set.
  425. * @return {BufferAttribute} A reference to this instance.
  426. */
  427. setXYZ( index, x, y, z ) {
  428. index *= this.itemSize;
  429. if ( this.normalized ) {
  430. x = normalize( x, this.array );
  431. y = normalize( y, this.array );
  432. z = normalize( z, this.array );
  433. }
  434. this.array[ index + 0 ] = x;
  435. this.array[ index + 1 ] = y;
  436. this.array[ index + 2 ] = z;
  437. return this;
  438. }
  439. /**
  440. * Sets the x, y, z and w component of the vector at the given index.
  441. *
  442. * @param {number} index - The index into the buffer attribute.
  443. * @param {number} x - The value for the x component to set.
  444. * @param {number} y - The value for the y component to set.
  445. * @param {number} z - The value for the z component to set.
  446. * @param {number} w - The value for the w component to set.
  447. * @return {BufferAttribute} A reference to this instance.
  448. */
  449. setXYZW( index, x, y, z, w ) {
  450. index *= this.itemSize;
  451. if ( this.normalized ) {
  452. x = normalize( x, this.array );
  453. y = normalize( y, this.array );
  454. z = normalize( z, this.array );
  455. w = normalize( w, this.array );
  456. }
  457. this.array[ index + 0 ] = x;
  458. this.array[ index + 1 ] = y;
  459. this.array[ index + 2 ] = z;
  460. this.array[ index + 3 ] = w;
  461. return this;
  462. }
  463. /**
  464. * Sets the given callback function that is executed after the Renderer has transferred
  465. * the attribute array data to the GPU. Can be used to perform clean-up operations after
  466. * the upload when attribute data are not needed anymore on the CPU side.
  467. *
  468. * @param {Function} callback - The `onUpload()` callback.
  469. * @return {BufferAttribute} A reference to this instance.
  470. */
  471. onUpload( callback ) {
  472. this.onUploadCallback = callback;
  473. return this;
  474. }
  475. /**
  476. * Returns a new buffer attribute with copied values from this instance.
  477. *
  478. * @return {BufferAttribute} A clone of this instance.
  479. */
  480. clone() {
  481. return new this.constructor( this.array, this.itemSize ).copy( this );
  482. }
  483. /**
  484. * Serializes the buffer attribute into JSON.
  485. *
  486. * @return {Object} A JSON object representing the serialized buffer attribute.
  487. */
  488. toJSON() {
  489. const data = {
  490. itemSize: this.itemSize,
  491. type: this.array.constructor.name,
  492. array: Array.from( this.array ),
  493. normalized: this.normalized
  494. };
  495. if ( this.name !== '' ) data.name = this.name;
  496. if ( this.usage !== StaticDrawUsage ) data.usage = this.usage;
  497. return data;
  498. }
  499. }
  500. /**
  501. * Convenient class that can be used when creating a `Int8` buffer attribute with
  502. * a plain `Array` instance.
  503. *
  504. * @augments BufferAttribute
  505. */
  506. class Int8BufferAttribute extends BufferAttribute {
  507. /**
  508. * Constructs a new buffer attribute.
  509. *
  510. * @param {(Array<number>|Int8Array)} array - The array holding the attribute data.
  511. * @param {number} itemSize - The item size.
  512. * @param {boolean} [normalized=false] - Whether the data are normalized or not.
  513. */
  514. constructor( array, itemSize, normalized ) {
  515. super( new Int8Array( array ), itemSize, normalized );
  516. }
  517. }
  518. /**
  519. * Convenient class that can be used when creating a `UInt8` buffer attribute with
  520. * a plain `Array` instance.
  521. *
  522. * @augments BufferAttribute
  523. */
  524. class Uint8BufferAttribute extends BufferAttribute {
  525. /**
  526. * Constructs a new buffer attribute.
  527. *
  528. * @param {(Array<number>|Uint8Array)} array - The array holding the attribute data.
  529. * @param {number} itemSize - The item size.
  530. * @param {boolean} [normalized=false] - Whether the data are normalized or not.
  531. */
  532. constructor( array, itemSize, normalized ) {
  533. super( new Uint8Array( array ), itemSize, normalized );
  534. }
  535. }
  536. /**
  537. * Convenient class that can be used when creating a `UInt8Clamped` buffer attribute with
  538. * a plain `Array` instance.
  539. *
  540. * @augments BufferAttribute
  541. */
  542. class Uint8ClampedBufferAttribute extends BufferAttribute {
  543. /**
  544. * Constructs a new buffer attribute.
  545. *
  546. * @param {(Array<number>|Uint8ClampedArray)} array - The array holding the attribute data.
  547. * @param {number} itemSize - The item size.
  548. * @param {boolean} [normalized=false] - Whether the data are normalized or not.
  549. */
  550. constructor( array, itemSize, normalized ) {
  551. super( new Uint8ClampedArray( array ), itemSize, normalized );
  552. }
  553. }
  554. /**
  555. * Convenient class that can be used when creating a `Int16` buffer attribute with
  556. * a plain `Array` instance.
  557. *
  558. * @augments BufferAttribute
  559. */
  560. class Int16BufferAttribute extends BufferAttribute {
  561. /**
  562. * Constructs a new buffer attribute.
  563. *
  564. * @param {(Array<number>|Int16Array)} array - The array holding the attribute data.
  565. * @param {number} itemSize - The item size.
  566. * @param {boolean} [normalized=false] - Whether the data are normalized or not.
  567. */
  568. constructor( array, itemSize, normalized ) {
  569. super( new Int16Array( array ), itemSize, normalized );
  570. }
  571. }
  572. /**
  573. * Convenient class that can be used when creating a `UInt16` buffer attribute with
  574. * a plain `Array` instance.
  575. *
  576. * @augments BufferAttribute
  577. */
  578. class Uint16BufferAttribute extends BufferAttribute {
  579. /**
  580. * Constructs a new buffer attribute.
  581. *
  582. * @param {(Array<number>|Uint16Array)} array - The array holding the attribute data.
  583. * @param {number} itemSize - The item size.
  584. * @param {boolean} [normalized=false] - Whether the data are normalized or not.
  585. */
  586. constructor( array, itemSize, normalized ) {
  587. super( new Uint16Array( array ), itemSize, normalized );
  588. }
  589. }
  590. /**
  591. * Convenient class that can be used when creating a `Int32` buffer attribute with
  592. * a plain `Array` instance.
  593. *
  594. * @augments BufferAttribute
  595. */
  596. class Int32BufferAttribute extends BufferAttribute {
  597. /**
  598. * Constructs a new buffer attribute.
  599. *
  600. * @param {(Array<number>|Int32Array)} array - The array holding the attribute data.
  601. * @param {number} itemSize - The item size.
  602. * @param {boolean} [normalized=false] - Whether the data are normalized or not.
  603. */
  604. constructor( array, itemSize, normalized ) {
  605. super( new Int32Array( array ), itemSize, normalized );
  606. }
  607. }
  608. /**
  609. * Convenient class that can be used when creating a `UInt32` buffer attribute with
  610. * a plain `Array` instance.
  611. *
  612. * @augments BufferAttribute
  613. */
  614. class Uint32BufferAttribute extends BufferAttribute {
  615. /**
  616. * Constructs a new buffer attribute.
  617. *
  618. * @param {(Array<number>|Uint32Array)} array - The array holding the attribute data.
  619. * @param {number} itemSize - The item size.
  620. * @param {boolean} [normalized=false] - Whether the data are normalized or not.
  621. */
  622. constructor( array, itemSize, normalized ) {
  623. super( new Uint32Array( array ), itemSize, normalized );
  624. }
  625. }
  626. /**
  627. * Convenient class that can be used when creating a `Float16` buffer attribute with
  628. * a plain `Array` instance.
  629. *
  630. * This class automatically converts to and from FP16 via `Uint16Array` since `Float16Array`
  631. * browser support is still problematic.
  632. *
  633. * @augments BufferAttribute
  634. */
  635. class Float16BufferAttribute extends BufferAttribute {
  636. /**
  637. * Constructs a new buffer attribute.
  638. *
  639. * @param {(Array<number>|Uint16Array)} array - The array holding the attribute data.
  640. * @param {number} itemSize - The item size.
  641. * @param {boolean} [normalized=false] - Whether the data are normalized or not.
  642. */
  643. constructor( array, itemSize, normalized ) {
  644. super( new Uint16Array( array ), itemSize, normalized );
  645. this.isFloat16BufferAttribute = true;
  646. }
  647. getX( index ) {
  648. let x = fromHalfFloat( this.array[ index * this.itemSize ] );
  649. if ( this.normalized ) x = denormalize( x, this.array );
  650. return x;
  651. }
  652. setX( index, x ) {
  653. if ( this.normalized ) x = normalize( x, this.array );
  654. this.array[ index * this.itemSize ] = toHalfFloat( x );
  655. return this;
  656. }
  657. getY( index ) {
  658. let y = fromHalfFloat( this.array[ index * this.itemSize + 1 ] );
  659. if ( this.normalized ) y = denormalize( y, this.array );
  660. return y;
  661. }
  662. setY( index, y ) {
  663. if ( this.normalized ) y = normalize( y, this.array );
  664. this.array[ index * this.itemSize + 1 ] = toHalfFloat( y );
  665. return this;
  666. }
  667. getZ( index ) {
  668. let z = fromHalfFloat( this.array[ index * this.itemSize + 2 ] );
  669. if ( this.normalized ) z = denormalize( z, this.array );
  670. return z;
  671. }
  672. setZ( index, z ) {
  673. if ( this.normalized ) z = normalize( z, this.array );
  674. this.array[ index * this.itemSize + 2 ] = toHalfFloat( z );
  675. return this;
  676. }
  677. getW( index ) {
  678. let w = fromHalfFloat( this.array[ index * this.itemSize + 3 ] );
  679. if ( this.normalized ) w = denormalize( w, this.array );
  680. return w;
  681. }
  682. setW( index, w ) {
  683. if ( this.normalized ) w = normalize( w, this.array );
  684. this.array[ index * this.itemSize + 3 ] = toHalfFloat( w );
  685. return this;
  686. }
  687. setXY( index, x, y ) {
  688. index *= this.itemSize;
  689. if ( this.normalized ) {
  690. x = normalize( x, this.array );
  691. y = normalize( y, this.array );
  692. }
  693. this.array[ index + 0 ] = toHalfFloat( x );
  694. this.array[ index + 1 ] = toHalfFloat( y );
  695. return this;
  696. }
  697. setXYZ( index, x, y, z ) {
  698. index *= this.itemSize;
  699. if ( this.normalized ) {
  700. x = normalize( x, this.array );
  701. y = normalize( y, this.array );
  702. z = normalize( z, this.array );
  703. }
  704. this.array[ index + 0 ] = toHalfFloat( x );
  705. this.array[ index + 1 ] = toHalfFloat( y );
  706. this.array[ index + 2 ] = toHalfFloat( z );
  707. return this;
  708. }
  709. setXYZW( index, x, y, z, w ) {
  710. index *= this.itemSize;
  711. if ( this.normalized ) {
  712. x = normalize( x, this.array );
  713. y = normalize( y, this.array );
  714. z = normalize( z, this.array );
  715. w = normalize( w, this.array );
  716. }
  717. this.array[ index + 0 ] = toHalfFloat( x );
  718. this.array[ index + 1 ] = toHalfFloat( y );
  719. this.array[ index + 2 ] = toHalfFloat( z );
  720. this.array[ index + 3 ] = toHalfFloat( w );
  721. return this;
  722. }
  723. }
  724. /**
  725. * Convenient class that can be used when creating a `Float32` buffer attribute with
  726. * a plain `Array` instance.
  727. *
  728. * @augments BufferAttribute
  729. */
  730. class Float32BufferAttribute extends BufferAttribute {
  731. /**
  732. * Constructs a new buffer attribute.
  733. *
  734. * @param {(Array<number>|Float32Array)} array - The array holding the attribute data.
  735. * @param {number} itemSize - The item size.
  736. * @param {boolean} [normalized=false] - Whether the data are normalized or not.
  737. */
  738. constructor( array, itemSize, normalized ) {
  739. super( new Float32Array( array ), itemSize, normalized );
  740. }
  741. }
  742. //
  743. export {
  744. Float32BufferAttribute,
  745. Float16BufferAttribute,
  746. Uint32BufferAttribute,
  747. Int32BufferAttribute,
  748. Uint16BufferAttribute,
  749. Int16BufferAttribute,
  750. Uint8ClampedBufferAttribute,
  751. Uint8BufferAttribute,
  752. Int8BufferAttribute,
  753. BufferAttribute
  754. };
粤ICP备19079148号