add better gyroid tile caching

This commit is contained in:
Stewart Allen 2021-06-10 21:31:38 -04:00
commit 45416b39d9
2 changed files with 25 additions and 4 deletions

View file

@ -6,17 +6,36 @@
if (self.base.gyroid) return;
const base = self.base,
PI2 = Math.PI * 2;
const base = self.base;
const PI2 = Math.PI * 2;
let cache = {};
let lastVal;
let lastRes = 0;
let lastSlice = 0;
/**
* @param off {number} z offset value from 0-1
* @param res {number} resolution (pixels/slices per side)
*/
function slice(off, res, val) {
// auto clear cach if it hasn't been hit in the last 20 seconds
// or the requested resolution or tip values have changed
let now = Date.now();
if (res !== lastRes || val !== lastVal || now - lastSlice > 20000) {
// console.log({clear_cache: now});
cache = {};
}
lastVal = val;
lastRes = res;
lastSlice = now;
let rez = parseInt(res || 200);
let inc = PI2 / rez;
let z = PI2 * off;
let key = (z % PI2).round(3);
let hit = cache[key];
if (hit) {
return hit;
}
let tip = val || 0;
let edge = [];
let vals = [];
@ -146,7 +165,9 @@
.map(poly => filter(poly, 0))
.map(poly => filter(poly, inc));
return {edge, points, dir, polys: psimple};
let slice = {edge, points, dir, polys: psimple};
cache[key] = slice;
return slice;
}
// merge co-linear and distance threshold

View file

@ -1333,7 +1333,7 @@
let size = process.sliceSupportSize;
let s4 = size / 4;
let s2 = size * 0.45;
let min = 0.01;//process.sliceSupportArea || 1;
let min = 0.01;
let geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(widget.vertices, 3));
let mat = new THREE.MeshBasicMaterial();