UNPKG

2 kBJavaScriptView Raw
1import { BUFFER_TYPE } from "@pixi/constants";
2import { Runner } from "@pixi/runner";
3let UID = 0;
4class Buffer {
5 /**
6 * @param {PIXI.IArrayBuffer} data - the data to store in the buffer.
7 * @param _static - `true` for static buffer
8 * @param index - `true` for index buffer
9 */
10 constructor(data, _static = !0, index = !1) {
11 this.data = data || new Float32Array(1), this._glBuffers = {}, this._updateID = 0, this.index = index, this.static = _static, this.id = UID++, this.disposeRunner = new Runner("disposeBuffer");
12 }
13 // TODO could explore flagging only a partial upload?
14 /**
15 * Flags this buffer as requiring an upload to the GPU.
16 * @param {PIXI.IArrayBuffer|number[]} [data] - the data to update in the buffer.
17 */
18 update(data) {
19 data instanceof Array && (data = new Float32Array(data)), this.data = data || this.data, this._updateID++;
20 }
21 /** Disposes WebGL resources that are connected to this geometry. */
22 dispose() {
23 this.disposeRunner.emit(this, !1);
24 }
25 /** Destroys the buffer. */
26 destroy() {
27 this.dispose(), this.data = null;
28 }
29 /**
30 * Flags whether this is an index buffer.
31 *
32 * Index buffers are of type `ELEMENT_ARRAY_BUFFER`. Note that setting this property to false will make
33 * the buffer of type `ARRAY_BUFFER`.
34 *
35 * For backwards compatibility.
36 */
37 set index(value) {
38 this.type = value ? BUFFER_TYPE.ELEMENT_ARRAY_BUFFER : BUFFER_TYPE.ARRAY_BUFFER;
39 }
40 get index() {
41 return this.type === BUFFER_TYPE.ELEMENT_ARRAY_BUFFER;
42 }
43 /**
44 * Helper function that creates a buffer based on an array or TypedArray
45 * @param {ArrayBufferView | number[]} data - the TypedArray that the buffer will store. If this is a regular Array it will be converted to a Float32Array.
46 * @returns - A new Buffer based on the data provided.
47 */
48 static from(data) {
49 return data instanceof Array && (data = new Float32Array(data)), new Buffer(data);
50 }
51}
52export {
53 Buffer
54};
55//# sourceMappingURL=Buffer.mjs.map