p5
Version:
[](https://www.npmjs.com/package/p5)
48 lines (47 loc) • 1.31 kB
JavaScript
//#region src/webgl/GeometryBufferCache.js
var GeometryBufferCache = class {
constructor(renderer) {
this.renderer = renderer;
this.cache = {};
}
numCached() {
return Object.keys(this.cache).length;
}
isCached(gid) {
return this.cache[gid] !== void 0;
}
getGeometryByID(gid) {
return this.cache[gid]?.geometry;
}
getCached(model) {
return this.getCachedID(model.gid);
}
getCachedID(gid) {
return this.cache[gid];
}
ensureCached(geometry) {
const gid = geometry.gid;
if (!gid) throw new Error("The p5.Geometry you passed in has no gid property!");
if (this.isCached(gid)) return this.getCached(geometry);
this.freeBuffers(gid);
if (Object.keys(this.cache).length > 1e3) {
const key = Object.keys(this.cache)[0];
this.freeBuffers(key);
}
const buffers = { geometry };
this.cache[gid] = buffers;
const indices = geometry.faces.length ? geometry.faces.flat() : null;
let indexType = null;
if (indices) indexType = indices.some((i) => i > 65535) ? Uint32Array : Uint16Array;
this.renderer._ensureGeometryBuffers(buffers, indices, indexType);
return buffers;
}
freeBuffers(gid) {
const buffers = this.cache[gid];
if (!buffers) return;
delete this.cache[gid];
this.renderer._freeBuffers(buffers);
}
};
//#endregion
export { GeometryBufferCache };