buffer.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. import { DataBuffer } from "./dataBuffer.js";
  2. import { Logger } from "../Misc/logger.js";
  3. /**
  4. * Class used to store data that will be store in GPU memory
  5. */
  6. export class Buffer {
  7. /**
  8. * Gets a boolean indicating if the Buffer is disposed
  9. */
  10. get isDisposed() {
  11. return this._isDisposed;
  12. }
  13. /**
  14. * Constructor
  15. * @param engine the engine
  16. * @param data the data to use for this buffer
  17. * @param updatable whether the data is updatable
  18. * @param stride the stride (optional)
  19. * @param postponeInternalCreation whether to postpone creating the internal WebGL buffer (optional)
  20. * @param instanced whether the buffer is instanced (optional)
  21. * @param useBytes set to true if the stride in in bytes (optional)
  22. * @param divisor sets an optional divisor for instances (1 by default)
  23. * @param label defines the label of the buffer (for debug purpose)
  24. */
  25. constructor(engine, data, updatable, stride = 0, postponeInternalCreation = false, instanced = false, useBytes = false, divisor, label) {
  26. this._isAlreadyOwned = false;
  27. this._isDisposed = false;
  28. if (engine && engine.getScene) {
  29. // old versions of VertexBuffer accepted 'mesh' instead of 'engine'
  30. this._engine = engine.getScene().getEngine();
  31. }
  32. else {
  33. this._engine = engine;
  34. }
  35. this._updatable = updatable;
  36. this._instanced = instanced;
  37. this._divisor = divisor || 1;
  38. this._label = label;
  39. if (data instanceof DataBuffer) {
  40. this._data = null;
  41. this._buffer = data;
  42. }
  43. else {
  44. this._data = data;
  45. this._buffer = null;
  46. }
  47. this.byteStride = useBytes ? stride : stride * Float32Array.BYTES_PER_ELEMENT;
  48. if (!postponeInternalCreation) {
  49. // by default
  50. this.create();
  51. }
  52. }
  53. /**
  54. * Create a new VertexBuffer based on the current buffer
  55. * @param kind defines the vertex buffer kind (position, normal, etc.)
  56. * @param offset defines offset in the buffer (0 by default)
  57. * @param size defines the size in floats of attributes (position is 3 for instance)
  58. * @param stride defines the stride size in floats in the buffer (the offset to apply to reach next value when data is interleaved)
  59. * @param instanced defines if the vertex buffer contains indexed data
  60. * @param useBytes defines if the offset and stride are in bytes *
  61. * @param divisor sets an optional divisor for instances (1 by default)
  62. * @returns the new vertex buffer
  63. */
  64. createVertexBuffer(kind, offset, size, stride, instanced, useBytes = false, divisor) {
  65. const byteOffset = useBytes ? offset : offset * Float32Array.BYTES_PER_ELEMENT;
  66. const byteStride = stride ? (useBytes ? stride : stride * Float32Array.BYTES_PER_ELEMENT) : this.byteStride;
  67. // a lot of these parameters are ignored as they are overridden by the buffer
  68. return new VertexBuffer(this._engine, this, kind, this._updatable, true, byteStride, instanced === undefined ? this._instanced : instanced, byteOffset, size, undefined, undefined, true, this._divisor || divisor);
  69. }
  70. // Properties
  71. /**
  72. * Gets a boolean indicating if the Buffer is updatable?
  73. * @returns true if the buffer is updatable
  74. */
  75. isUpdatable() {
  76. return this._updatable;
  77. }
  78. /**
  79. * Gets current buffer's data
  80. * @returns a DataArray or null
  81. */
  82. getData() {
  83. return this._data;
  84. }
  85. /**
  86. * Gets underlying native buffer
  87. * @returns underlying native buffer
  88. */
  89. getBuffer() {
  90. return this._buffer;
  91. }
  92. /**
  93. * Gets the stride in float32 units (i.e. byte stride / 4).
  94. * May not be an integer if the byte stride is not divisible by 4.
  95. * @returns the stride in float32 units
  96. * @deprecated Please use byteStride instead.
  97. */
  98. getStrideSize() {
  99. return this.byteStride / Float32Array.BYTES_PER_ELEMENT;
  100. }
  101. // Methods
  102. /**
  103. * Store data into the buffer. Creates the buffer if not used already.
  104. * If the buffer was already used, it will be updated only if it is updatable, otherwise it will do nothing.
  105. * @param data defines the data to store
  106. */
  107. create(data = null) {
  108. if (!data && this._buffer) {
  109. return; // nothing to do
  110. }
  111. data = data || this._data;
  112. if (!data) {
  113. return;
  114. }
  115. if (!this._buffer) {
  116. // create buffer
  117. if (this._updatable) {
  118. this._buffer = this._engine.createDynamicVertexBuffer(data, this._label);
  119. this._data = data;
  120. }
  121. else {
  122. this._buffer = this._engine.createVertexBuffer(data, undefined, this._label);
  123. }
  124. }
  125. else if (this._updatable) {
  126. // update buffer
  127. this._engine.updateDynamicVertexBuffer(this._buffer, data);
  128. this._data = data;
  129. }
  130. }
  131. /** @internal */
  132. _rebuild() {
  133. if (!this._data) {
  134. if (!this._buffer) {
  135. // Buffer was not yet created, nothing to do
  136. return;
  137. }
  138. if (this._buffer.capacity > 0) {
  139. // We can at least recreate the buffer with the right size, even if we don't have the data
  140. if (this._updatable) {
  141. this._buffer = this._engine.createDynamicVertexBuffer(this._buffer.capacity, this._label);
  142. }
  143. else {
  144. this._buffer = this._engine.createVertexBuffer(this._buffer.capacity, undefined, this._label);
  145. }
  146. return;
  147. }
  148. Logger.Warn(`Missing data for buffer "${this._label}" ${this._buffer ? "(uniqueId: " + this._buffer.uniqueId + ")" : ""}. Buffer reconstruction failed.`);
  149. this._buffer = null;
  150. }
  151. else {
  152. this._buffer = null;
  153. this.create(this._data);
  154. }
  155. }
  156. /**
  157. * Update current buffer data
  158. * @param data defines the data to store
  159. */
  160. update(data) {
  161. this.create(data);
  162. }
  163. /**
  164. * Updates the data directly.
  165. * @param data the new data
  166. * @param offset the new offset
  167. * @param vertexCount the vertex count (optional)
  168. * @param useBytes set to true if the offset is in bytes
  169. */
  170. updateDirectly(data, offset, vertexCount, useBytes = false) {
  171. if (!this._buffer) {
  172. return;
  173. }
  174. if (this._updatable) {
  175. // update buffer
  176. this._engine.updateDynamicVertexBuffer(this._buffer, data, useBytes ? offset : offset * Float32Array.BYTES_PER_ELEMENT, vertexCount ? vertexCount * this.byteStride : undefined);
  177. if (offset === 0 && vertexCount === undefined) {
  178. // Keep the data if we easily can
  179. this._data = data;
  180. }
  181. else {
  182. this._data = null;
  183. }
  184. }
  185. }
  186. /** @internal */
  187. _increaseReferences() {
  188. if (!this._buffer) {
  189. return;
  190. }
  191. if (!this._isAlreadyOwned) {
  192. this._isAlreadyOwned = true;
  193. return;
  194. }
  195. this._buffer.references++;
  196. }
  197. /**
  198. * Release all resources
  199. */
  200. dispose() {
  201. if (!this._buffer) {
  202. return;
  203. }
  204. // The data buffer has an internal counter as this buffer can be used by several VertexBuffer objects
  205. // This means that we only flag it as disposed when all references are released (when _releaseBuffer will return true)
  206. if (this._engine._releaseBuffer(this._buffer)) {
  207. this._isDisposed = true;
  208. this._data = null;
  209. this._buffer = null;
  210. }
  211. }
  212. }
  213. /**
  214. * Specialized buffer used to store vertex data
  215. */
  216. export class VertexBuffer {
  217. /**
  218. * Gets a boolean indicating if the Buffer is disposed
  219. */
  220. get isDisposed() {
  221. return this._isDisposed;
  222. }
  223. /**
  224. * Gets or sets the instance divisor when in instanced mode
  225. */
  226. get instanceDivisor() {
  227. return this._instanceDivisor;
  228. }
  229. set instanceDivisor(value) {
  230. const isInstanced = value != 0;
  231. this._instanceDivisor = value;
  232. if (isInstanced !== this._instanced) {
  233. this._instanced = isInstanced;
  234. this._computeHashCode();
  235. }
  236. }
  237. /**
  238. * Gets the max possible amount of vertices stored within the current vertex buffer.
  239. * We do not have the end offset or count so this will be too big for concatenated vertex buffers.
  240. * @internal
  241. */
  242. get _maxVerticesCount() {
  243. const data = this.getData();
  244. if (!data) {
  245. return 0;
  246. }
  247. if (Array.isArray(data)) {
  248. // data is a regular number[] with float values
  249. return data.length / (this.byteStride / 4) - this.byteOffset / 4;
  250. }
  251. return (data.byteLength - this.byteOffset) / this.byteStride;
  252. }
  253. /** @internal */
  254. constructor(engine, data, kind, updatableOrOptions, postponeInternalCreation, stride, instanced, offset, size, type, normalized = false, useBytes = false, divisor = 1, takeBufferOwnership = false) {
  255. /** @internal */
  256. this._isDisposed = false;
  257. let updatable = false;
  258. this.engine = engine;
  259. if (typeof updatableOrOptions === "object" && updatableOrOptions !== null) {
  260. updatable = updatableOrOptions.updatable ?? false;
  261. postponeInternalCreation = updatableOrOptions.postponeInternalCreation;
  262. stride = updatableOrOptions.stride;
  263. instanced = updatableOrOptions.instanced;
  264. offset = updatableOrOptions.offset;
  265. size = updatableOrOptions.size;
  266. type = updatableOrOptions.type;
  267. normalized = updatableOrOptions.normalized ?? false;
  268. useBytes = updatableOrOptions.useBytes ?? false;
  269. divisor = updatableOrOptions.divisor ?? 1;
  270. takeBufferOwnership = updatableOrOptions.takeBufferOwnership ?? false;
  271. this._label = updatableOrOptions.label;
  272. }
  273. else {
  274. updatable = !!updatableOrOptions;
  275. }
  276. if (data instanceof Buffer) {
  277. this._buffer = data;
  278. this._ownsBuffer = takeBufferOwnership;
  279. }
  280. else {
  281. this._buffer = new Buffer(engine, data, updatable, stride, postponeInternalCreation, instanced, useBytes, divisor, this._label);
  282. this._ownsBuffer = true;
  283. }
  284. this.uniqueId = VertexBuffer._Counter++;
  285. this._kind = kind;
  286. if (type === undefined) {
  287. const vertexData = this.getData();
  288. this.type = vertexData ? VertexBuffer.GetDataType(vertexData) : VertexBuffer.FLOAT;
  289. }
  290. else {
  291. this.type = type;
  292. }
  293. const typeByteLength = VertexBuffer.GetTypeByteLength(this.type);
  294. if (useBytes) {
  295. this._size = size || (stride ? stride / typeByteLength : VertexBuffer.DeduceStride(kind));
  296. this.byteStride = stride || this._buffer.byteStride || this._size * typeByteLength;
  297. this.byteOffset = offset || 0;
  298. }
  299. else {
  300. this._size = size || stride || VertexBuffer.DeduceStride(kind);
  301. this.byteStride = stride ? stride * typeByteLength : this._buffer.byteStride || this._size * typeByteLength;
  302. this.byteOffset = (offset || 0) * typeByteLength;
  303. }
  304. this.normalized = normalized;
  305. this._instanced = instanced !== undefined ? instanced : false;
  306. this._instanceDivisor = instanced ? divisor : 0;
  307. this._alignBuffer();
  308. this._computeHashCode();
  309. }
  310. _computeHashCode() {
  311. // note: cast to any because the property is declared readonly
  312. this.hashCode =
  313. ((this.type - 5120) << 0) +
  314. ((this.normalized ? 1 : 0) << 3) +
  315. (this._size << 4) +
  316. ((this._instanced ? 1 : 0) << 6) +
  317. /* keep 5 bits free */
  318. (this.byteStride << 12);
  319. }
  320. /** @internal */
  321. _rebuild() {
  322. this._buffer?._rebuild();
  323. }
  324. /**
  325. * Returns the kind of the VertexBuffer (string)
  326. * @returns a string
  327. */
  328. getKind() {
  329. return this._kind;
  330. }
  331. // Properties
  332. /**
  333. * Gets a boolean indicating if the VertexBuffer is updatable?
  334. * @returns true if the buffer is updatable
  335. */
  336. isUpdatable() {
  337. return this._buffer.isUpdatable();
  338. }
  339. /**
  340. * Gets current buffer's data
  341. * @returns a DataArray or null
  342. */
  343. getData() {
  344. return this._buffer.getData();
  345. }
  346. /**
  347. * Gets current buffer's data as a float array. Float data is constructed if the vertex buffer data cannot be returned directly.
  348. * @param totalVertices number of vertices in the buffer to take into account
  349. * @param forceCopy defines a boolean indicating that the returned array must be cloned upon returning it
  350. * @returns a float array containing vertex data
  351. */
  352. getFloatData(totalVertices, forceCopy) {
  353. const data = this.getData();
  354. if (!data) {
  355. return null;
  356. }
  357. return VertexBuffer.GetFloatData(data, this._size, this.type, this.byteOffset, this.byteStride, this.normalized, totalVertices, forceCopy);
  358. }
  359. /**
  360. * Gets underlying native buffer
  361. * @returns underlying native buffer
  362. */
  363. getBuffer() {
  364. return this._buffer.getBuffer();
  365. }
  366. /**
  367. * Gets the Buffer instance that wraps the native GPU buffer
  368. * @returns the wrapper buffer
  369. */
  370. getWrapperBuffer() {
  371. return this._buffer;
  372. }
  373. /**
  374. * Gets the stride in float32 units (i.e. byte stride / 4).
  375. * May not be an integer if the byte stride is not divisible by 4.
  376. * @returns the stride in float32 units
  377. * @deprecated Please use byteStride instead.
  378. */
  379. getStrideSize() {
  380. return this.byteStride / VertexBuffer.GetTypeByteLength(this.type);
  381. }
  382. /**
  383. * Returns the offset as a multiple of the type byte length.
  384. * @returns the offset in bytes
  385. * @deprecated Please use byteOffset instead.
  386. */
  387. getOffset() {
  388. return this.byteOffset / VertexBuffer.GetTypeByteLength(this.type);
  389. }
  390. /**
  391. * Returns the number of components or the byte size per vertex attribute
  392. * @param sizeInBytes If true, returns the size in bytes or else the size in number of components of the vertex attribute (default: false)
  393. * @returns the number of components
  394. */
  395. getSize(sizeInBytes = false) {
  396. return sizeInBytes ? this._size * VertexBuffer.GetTypeByteLength(this.type) : this._size;
  397. }
  398. /**
  399. * Gets a boolean indicating is the internal buffer of the VertexBuffer is instanced
  400. * @returns true if this buffer is instanced
  401. */
  402. getIsInstanced() {
  403. return this._instanced;
  404. }
  405. /**
  406. * Returns the instancing divisor, zero for non-instanced (integer).
  407. * @returns a number
  408. */
  409. getInstanceDivisor() {
  410. return this._instanceDivisor;
  411. }
  412. // Methods
  413. /**
  414. * Store data into the buffer. If the buffer was already used it will be either recreated or updated depending on isUpdatable property
  415. * @param data defines the data to store
  416. */
  417. create(data) {
  418. this._buffer.create(data);
  419. this._alignBuffer();
  420. }
  421. /**
  422. * Updates the underlying buffer according to the passed numeric array or Float32Array.
  423. * This function will create a new buffer if the current one is not updatable
  424. * @param data defines the data to store
  425. */
  426. update(data) {
  427. this._buffer.update(data);
  428. this._alignBuffer();
  429. }
  430. /**
  431. * Updates directly the underlying WebGLBuffer according to the passed numeric array or Float32Array.
  432. * Returns the directly updated WebGLBuffer.
  433. * @param data the new data
  434. * @param offset the new offset
  435. * @param useBytes set to true if the offset is in bytes
  436. */
  437. updateDirectly(data, offset, useBytes = false) {
  438. this._buffer.updateDirectly(data, offset, undefined, useBytes);
  439. this._alignBuffer();
  440. }
  441. /**
  442. * Disposes the VertexBuffer and the underlying WebGLBuffer.
  443. */
  444. dispose() {
  445. if (this._ownsBuffer) {
  446. this._buffer.dispose();
  447. }
  448. this._isDisposed = true;
  449. }
  450. /**
  451. * Enumerates each value of this vertex buffer as numbers.
  452. * @param count the number of values to enumerate
  453. * @param callback the callback function called for each value
  454. */
  455. forEach(count, callback) {
  456. VertexBuffer.ForEach(this._buffer.getData(), this.byteOffset, this.byteStride, this._size, this.type, count, this.normalized, callback);
  457. }
  458. /** @internal */
  459. _alignBuffer() { }
  460. /**
  461. * Deduces the stride given a kind.
  462. * @param kind The kind string to deduce
  463. * @returns The deduced stride
  464. */
  465. static DeduceStride(kind) {
  466. switch (kind) {
  467. case VertexBuffer.UVKind:
  468. case VertexBuffer.UV2Kind:
  469. case VertexBuffer.UV3Kind:
  470. case VertexBuffer.UV4Kind:
  471. case VertexBuffer.UV5Kind:
  472. case VertexBuffer.UV6Kind:
  473. return 2;
  474. case VertexBuffer.NormalKind:
  475. case VertexBuffer.PositionKind:
  476. return 3;
  477. case VertexBuffer.ColorKind:
  478. case VertexBuffer.ColorInstanceKind:
  479. case VertexBuffer.MatricesIndicesKind:
  480. case VertexBuffer.MatricesIndicesExtraKind:
  481. case VertexBuffer.MatricesWeightsKind:
  482. case VertexBuffer.MatricesWeightsExtraKind:
  483. case VertexBuffer.TangentKind:
  484. return 4;
  485. default:
  486. throw new Error("Invalid kind '" + kind + "'");
  487. }
  488. }
  489. /**
  490. * Gets the vertex buffer type of the given data array.
  491. * @param data the data array
  492. * @returns the vertex buffer type
  493. */
  494. static GetDataType(data) {
  495. if (data instanceof Int8Array) {
  496. return VertexBuffer.BYTE;
  497. }
  498. else if (data instanceof Uint8Array) {
  499. return VertexBuffer.UNSIGNED_BYTE;
  500. }
  501. else if (data instanceof Int16Array) {
  502. return VertexBuffer.SHORT;
  503. }
  504. else if (data instanceof Uint16Array) {
  505. return VertexBuffer.UNSIGNED_SHORT;
  506. }
  507. else if (data instanceof Int32Array) {
  508. return VertexBuffer.INT;
  509. }
  510. else if (data instanceof Uint32Array) {
  511. return VertexBuffer.UNSIGNED_INT;
  512. }
  513. else {
  514. return VertexBuffer.FLOAT;
  515. }
  516. }
  517. /**
  518. * Gets the byte length of the given type.
  519. * @param type the type
  520. * @returns the number of bytes
  521. */
  522. static GetTypeByteLength(type) {
  523. switch (type) {
  524. case VertexBuffer.BYTE:
  525. case VertexBuffer.UNSIGNED_BYTE:
  526. return 1;
  527. case VertexBuffer.SHORT:
  528. case VertexBuffer.UNSIGNED_SHORT:
  529. return 2;
  530. case VertexBuffer.INT:
  531. case VertexBuffer.UNSIGNED_INT:
  532. case VertexBuffer.FLOAT:
  533. return 4;
  534. default:
  535. throw new Error(`Invalid type '${type}'`);
  536. }
  537. }
  538. /**
  539. * Enumerates each value of the given parameters as numbers.
  540. * @param data the data to enumerate
  541. * @param byteOffset the byte offset of the data
  542. * @param byteStride the byte stride of the data
  543. * @param componentCount the number of components per element
  544. * @param componentType the type of the component
  545. * @param count the number of values to enumerate
  546. * @param normalized whether the data is normalized
  547. * @param callback the callback function called for each value
  548. */
  549. static ForEach(data, byteOffset, byteStride, componentCount, componentType, count, normalized, callback) {
  550. if (data instanceof Array) {
  551. let offset = byteOffset / 4;
  552. const stride = byteStride / 4;
  553. for (let index = 0; index < count; index += componentCount) {
  554. for (let componentIndex = 0; componentIndex < componentCount; componentIndex++) {
  555. callback(data[offset + componentIndex], index + componentIndex);
  556. }
  557. offset += stride;
  558. }
  559. }
  560. else {
  561. const dataView = data instanceof ArrayBuffer ? new DataView(data) : new DataView(data.buffer, data.byteOffset, data.byteLength);
  562. const componentByteLength = VertexBuffer.GetTypeByteLength(componentType);
  563. for (let index = 0; index < count; index += componentCount) {
  564. let componentByteOffset = byteOffset;
  565. for (let componentIndex = 0; componentIndex < componentCount; componentIndex++) {
  566. const value = VertexBuffer._GetFloatValue(dataView, componentType, componentByteOffset, normalized);
  567. callback(value, index + componentIndex);
  568. componentByteOffset += componentByteLength;
  569. }
  570. byteOffset += byteStride;
  571. }
  572. }
  573. }
  574. static _GetFloatValue(dataView, type, byteOffset, normalized) {
  575. switch (type) {
  576. case VertexBuffer.BYTE: {
  577. let value = dataView.getInt8(byteOffset);
  578. if (normalized) {
  579. value = Math.max(value / 127, -1);
  580. }
  581. return value;
  582. }
  583. case VertexBuffer.UNSIGNED_BYTE: {
  584. let value = dataView.getUint8(byteOffset);
  585. if (normalized) {
  586. value = value / 255;
  587. }
  588. return value;
  589. }
  590. case VertexBuffer.SHORT: {
  591. let value = dataView.getInt16(byteOffset, true);
  592. if (normalized) {
  593. value = Math.max(value / 32767, -1);
  594. }
  595. return value;
  596. }
  597. case VertexBuffer.UNSIGNED_SHORT: {
  598. let value = dataView.getUint16(byteOffset, true);
  599. if (normalized) {
  600. value = value / 65535;
  601. }
  602. return value;
  603. }
  604. case VertexBuffer.INT: {
  605. return dataView.getInt32(byteOffset, true);
  606. }
  607. case VertexBuffer.UNSIGNED_INT: {
  608. return dataView.getUint32(byteOffset, true);
  609. }
  610. case VertexBuffer.FLOAT: {
  611. return dataView.getFloat32(byteOffset, true);
  612. }
  613. default: {
  614. throw new Error(`Invalid component type ${type}`);
  615. }
  616. }
  617. }
  618. /**
  619. * Gets the given data array as a float array. Float data is constructed if the data array cannot be returned directly.
  620. * @param data the input data array
  621. * @param size the number of components
  622. * @param type the component type
  623. * @param byteOffset the byte offset of the data
  624. * @param byteStride the byte stride of the data
  625. * @param normalized whether the data is normalized
  626. * @param totalVertices number of vertices in the buffer to take into account
  627. * @param forceCopy defines a boolean indicating that the returned array must be cloned upon returning it
  628. * @returns a float array containing vertex data
  629. */
  630. static GetFloatData(data, size, type, byteOffset, byteStride, normalized, totalVertices, forceCopy) {
  631. const tightlyPackedByteStride = size * VertexBuffer.GetTypeByteLength(type);
  632. const count = totalVertices * size;
  633. if (type !== VertexBuffer.FLOAT || byteStride !== tightlyPackedByteStride) {
  634. const copy = new Float32Array(count);
  635. VertexBuffer.ForEach(data, byteOffset, byteStride, size, type, count, normalized, (value, index) => (copy[index] = value));
  636. return copy;
  637. }
  638. if (!(data instanceof Array || data instanceof Float32Array) || byteOffset !== 0 || data.length !== count) {
  639. if (data instanceof Array) {
  640. const offset = byteOffset / 4;
  641. return data.slice(offset, offset + count);
  642. }
  643. else if (data instanceof ArrayBuffer) {
  644. return new Float32Array(data, byteOffset, count);
  645. }
  646. else {
  647. let offset = data.byteOffset + byteOffset;
  648. if (forceCopy) {
  649. const result = new Float32Array(count);
  650. const source = new Float32Array(data.buffer, offset, count);
  651. result.set(source);
  652. return result;
  653. }
  654. // Protect against bad data
  655. const remainder = offset % 4;
  656. if (remainder) {
  657. offset = Math.max(0, offset - remainder);
  658. }
  659. return new Float32Array(data.buffer, offset, count);
  660. }
  661. }
  662. if (forceCopy) {
  663. return data.slice();
  664. }
  665. return data;
  666. }
  667. }
  668. VertexBuffer._Counter = 0;
  669. /**
  670. * The byte type.
  671. */
  672. VertexBuffer.BYTE = 5120;
  673. /**
  674. * The unsigned byte type.
  675. */
  676. VertexBuffer.UNSIGNED_BYTE = 5121;
  677. /**
  678. * The short type.
  679. */
  680. VertexBuffer.SHORT = 5122;
  681. /**
  682. * The unsigned short type.
  683. */
  684. VertexBuffer.UNSIGNED_SHORT = 5123;
  685. /**
  686. * The integer type.
  687. */
  688. VertexBuffer.INT = 5124;
  689. /**
  690. * The unsigned integer type.
  691. */
  692. VertexBuffer.UNSIGNED_INT = 5125;
  693. /**
  694. * The float type.
  695. */
  696. VertexBuffer.FLOAT = 5126;
  697. // Enums
  698. /**
  699. * Positions
  700. */
  701. VertexBuffer.PositionKind = `position`;
  702. /**
  703. * Normals
  704. */
  705. VertexBuffer.NormalKind = `normal`;
  706. /**
  707. * Tangents
  708. */
  709. VertexBuffer.TangentKind = `tangent`;
  710. /**
  711. * Texture coordinates
  712. */
  713. VertexBuffer.UVKind = `uv`;
  714. /**
  715. * Texture coordinates 2
  716. */
  717. VertexBuffer.UV2Kind = `uv2`;
  718. /**
  719. * Texture coordinates 3
  720. */
  721. VertexBuffer.UV3Kind = `uv3`;
  722. /**
  723. * Texture coordinates 4
  724. */
  725. VertexBuffer.UV4Kind = `uv4`;
  726. /**
  727. * Texture coordinates 5
  728. */
  729. VertexBuffer.UV5Kind = `uv5`;
  730. /**
  731. * Texture coordinates 6
  732. */
  733. VertexBuffer.UV6Kind = `uv6`;
  734. /**
  735. * Colors
  736. */
  737. VertexBuffer.ColorKind = `color`;
  738. /**
  739. * Instance Colors
  740. */
  741. VertexBuffer.ColorInstanceKind = `instanceColor`;
  742. /**
  743. * Matrix indices (for bones)
  744. */
  745. VertexBuffer.MatricesIndicesKind = `matricesIndices`;
  746. /**
  747. * Matrix weights (for bones)
  748. */
  749. VertexBuffer.MatricesWeightsKind = `matricesWeights`;
  750. /**
  751. * Additional matrix indices (for bones)
  752. */
  753. VertexBuffer.MatricesIndicesExtraKind = `matricesIndicesExtra`;
  754. /**
  755. * Additional matrix weights (for bones)
  756. */
  757. VertexBuffer.MatricesWeightsExtraKind = `matricesWeightsExtra`;
  758. //# sourceMappingURL=buffer.js.map