Component now reuses its canvas. regl implementation reuses its regl instance, and caches resources that already loaded.

This commit is contained in:
Rezmason
2025-05-05 18:59:18 -07:00
parent 237990b44c
commit 664f484723
6 changed files with 115 additions and 87 deletions

View File

@@ -28,10 +28,16 @@ const makeDoubleBuffer = (regl, props) => {
const isPowerOfTwo = (x) => Math.log2(x) % 1 == 0;
const loadImage = (regl, url, mipmap) => {
const loadImage = (cache, regl, url, mipmap) => {
const key = `${url}_${mipmap}`;
if (cache.has(key)) {
return cache.get(key);
}
let texture = regl.texture([[0]]);
let loaded = false;
return {
const resource = {
texture: () => {
if (!loaded && url != null) {
console.warn(`texture still loading: ${url}`);
@@ -72,12 +78,18 @@ const loadImage = (regl, url, mipmap) => {
}
})(),
};
cache.set(key, resource);
return resource;
};
const loadText = (url) => {
const loadText = (cache, url) => {
const key = url;
if (cache.has(key)) {
return cache.get(key);
}
let text = "";
let loaded = false;
return {
const resource = {
text: () => {
if (!loaded) {
console.warn(`text still loading: ${url}`);
@@ -91,6 +103,8 @@ const loadText = (url) => {
}
})(),
};
cache.set(key, resource);
return resource;
};
const makeFullScreenQuad = (regl, uniforms = {}, context = {}) =>