buffer.ts 25 KB

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