@spearwolf/twopoint5d
Version:
a library to create 2.5d realtime graphics and pixelart with three.js
67 lines • 2.55 kB
JavaScript
import { VertexObjectBuffer } from './VertexObjectBuffer.js';
import { VertexObjectDescriptor } from './VertexObjectDescriptor.js';
export class VOBufferPool {
#usedCount = 0;
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;
}
clear() {
this.usedCount = 0;
}
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