nanogl-gltf
Version:
84 lines (83 loc) • 3.29 kB
JavaScript
import GltfTypes from '../types/GltfTypes';
/**
* The BufferView element is a view into a Buffer, representing a slice of it,
* as a Buffer is often used to contain large quantities of different datas not related to each other.
* A BufferView allows to access only a part of a Buffer and to simplify its access.
*/
export default class BufferView {
constructor() {
this.gltftype = GltfTypes.BUFFERVIEW;
/**
* Offset of this BufferView in the associated Buffer, in bytes
*/
this.byteOffset = 0;
/**
* Length of this BufferView in the associated Buffer, in bytes
*/
this.byteLength = 0;
/**
* Stride of this BufferView in the associated Buffer, in bytes
*/
this.byteStride = 0;
/**
* Target of this BufferView, may be used to infer the type of the data,
* either 34962 (used for vertex attributes) or 34963 (used for vertex indices).
*/
this.target = 0;
/**
* WebGLBuffer created from the associated BufferView data, used when calling getWebGLBuffer()
*/
this.glBuffer = null;
/**
* WebGLBufferTarget stored when calling getWebGLBuffer()
*/
this.glBufferTarget = 0;
}
/**
* Parse the BufferView data, load the associated Buffer element and sets the byteOffset, byteLength, byteStride and target attributes.
*
* Is async as it needs to wait for the buffer data to be loaded.
* @param gltfLoader GLTFLoader to use
* @param data Data to parse
*/
async parse(gltfLoader, data) {
const { byteLength, byteOffset = 0, byteStride = 0, target = 0 } = data;
this.byteLength = byteLength;
this.byteOffset = byteOffset;
this.byteStride = byteStride;
this.target = target;
this.buffer = await gltfLoader.getElement(GltfTypes.BUFFER, data.buffer);
}
/**
* Get the offset of this BufferView in the associated Buffer, in bytes.
* Adds the byteOffset of the Buffer if any.
*/
getByteOffset() {
return this.byteOffset + this.buffer._byteOffset;
}
/**
* Create a WebGLBuffer from the associated BufferView data.
* @param gl GL context to create the WebGLBuffer
* @param inferedTarget Target to use for the WebGLBuffer
*/
getWebGLBuffer(gl, inferedTarget) {
if (this.target !== 0 && this.target !== inferedTarget) {
throw new Error(`BufferView's target ${this.target} doesn't match infered one ${inferedTarget}`);
}
if (this.glBuffer !== null) {
if (this.glBufferTarget !== inferedTarget) {
// Is this really an error?
throw new Error(`WebglBuffers with different target requested on BufferView`);
}
}
else {
const data = new Uint8Array(this.buffer._bytes, this.getByteOffset(), this.byteLength);
this.glBufferTarget = inferedTarget;
this.glBuffer = gl.createBuffer();
gl.bindBuffer(inferedTarget, this.glBuffer);
gl.bufferData(inferedTarget, data, gl.STATIC_DRAW);
gl.bindBuffer(inferedTarget, null);
}
return this.glBuffer;
}
}