UNPKG

@spearwolf/twopoint5d

Version:

Create 2.5D realtime graphics and pixelart with WebGL and three.js

83 lines 2.97 kB
import { VertexObjectBuffer } from './VertexObjectBuffer.js'; import { VertexObjectDescriptor } from './VertexObjectDescriptor.js'; export class VOBufferPool { #usedCount = 0; #disposed = false; constructor(descriptor, capacityOrData) { this.descriptor = descriptor instanceof VertexObjectDescriptor ? descriptor : new VertexObjectDescriptor(descriptor); if (typeof capacityOrData === 'number') { const capacity = capacityOrData; this.capacity = capacity; this.buffer = new VertexObjectBuffer(this.descriptor, capacity); } else { const buffersData = capacityOrData; this.capacity = buffersData.capacity; this.fromBuffersData(buffersData); } } get usedCount() { return this.#usedCount; } set usedCount(value) { this.#usedCount = value < this.capacity ? value : this.capacity; } get availableCount() { return this.capacity - this.#usedCount; } get isDisposed() { return this.#disposed; } clear() { this.usedCount = 0; } dispose() { if (this.#disposed) return; this.#disposed = true; this.#usedCount = 0; if (this.buffer != null) { for (const buffer of this.buffer.buffers.values()) { buffer.typedArray = undefined; } this.buffer.buffers.clear(); } } createFromAttributes(attributes) { const firstObjectIndex = this.#usedCount; const objectCount = this.buffer.copyAttributes(attributes, firstObjectIndex); this.#usedCount += objectCount; return [objectCount, firstObjectIndex]; } toBuffersData() { return { capacity: this.capacity, usedCount: this.usedCount, buffers: Object.fromEntries(Array.from(this.buffer.buffers.values()).map((buffer) => [buffer.bufferName, buffer.typedArray])), }; } fromBuffersData(buffersData, copyTypedArrays = false) { if (buffersData.capacity !== this.capacity) { throw new Error('Invalid buffersData capacity'); } this.#usedCount = buffersData.usedCount; if (this.buffer == null) { this.buffer = new VertexObjectBuffer(this.descriptor, buffersData); } else { for (const [bufferName, typedArray] of Object.entries(buffersData.buffers)) { const buffer = this.buffer.buffers.get(bufferName); if (buffer) { if (copyTypedArrays || typedArray.length < buffer.typedArray.length) { buffer.typedArray.set(typedArray); } else { buffer.typedArray = typedArray; } buffer.serial++; } } } } } //# sourceMappingURL=VOBufferPool.js.map