fix matrix cache corruption during reload. center models on import

This commit is contained in:
Stewart Allen 2022-01-11 10:54:31 -05:00
commit f53a9752f8
4 changed files with 34 additions and 19 deletions

View file

@ -75,6 +75,8 @@ function init() {
function restore_space() { function restore_space() {
let count = 0; let count = 0;
let space = moto.Space; let space = moto.Space;
let parray = [];
let mcache;
mesh.db.admin.get("camera") mesh.db.admin.get("camera")
.then(saved => { .then(saved => {
if (saved) { if (saved) {
@ -85,7 +87,7 @@ function restore_space() {
mesh.db.admin.get("matrices") mesh.db.admin.get("matrices")
.then(data => { .then(data => {
if (data) { if (data) {
matrixCache = data; mcache = data;
} }
}); });
mesh.db.space.iterate({ map: true }).then(cached => { mesh.db.space.iterate({ map: true }).then(cached => {
@ -98,10 +100,10 @@ function restore_space() {
let models = data let models = data
.map(id => { return { id, md: cached[id] } }) .map(id => { return { id, md: cached[id] } })
.filter(r => r.md) // filter cache misses .filter(r => r.md) // filter cache misses
.map(r => new mesh.model(r.md, r.id).applyMatrix(matrixCache[r.id])); .map(r => new mesh.model(r.md, r.id).applyMatrix(mcache[r.id]));
mesh.api.log.emit(`restored ${models.length} model(s)`); mesh.api.log.emit(`restored ${models.length} model(s)`);
let group = mesh.api.group.new(models, id); let group = mesh.api.group.new(models, id);
let matrix = matrixCache[id]; let matrix = mcache[id];
if (matrix) { if (matrix) {
group.applyMatrix(matrix); group.applyMatrix(matrix);
} else { } else {
@ -112,6 +114,10 @@ function restore_space() {
} }
} }
} }
// restore global cache only after objects are restored
// otherwise their setup will corrupt the cache for other restores
matrixCache = mcache;
store_matrices();
}) })
// hide loading curtain // hide loading curtain
$d('curtain','none'); $d('curtain','none');
@ -155,6 +161,8 @@ function space_init(data) {
break; break;
case 'KeyC': case 'KeyC':
return selection.centerXY().focus(); return selection.centerXY().focus();
case 'KeyF':
return selection.floor().focus();
case 'KeyI': case 'KeyI':
return api.file.import(); return api.file.import();
case 'KeyX': case 'KeyX':
@ -188,6 +196,7 @@ function space_init(data) {
} }
return; return;
} }
let rot;
switch (code) { switch (code) {
case 'KeyA': case 'KeyA':
if (metaKey || ctrlKey) { if (metaKey || ctrlKey) {
@ -209,26 +218,30 @@ function space_init(data) {
estop(evt); estop(evt);
break; break;
case 'ArrowUp': case 'ArrowUp':
selection.rotate(-rv,0,0).floor(); rot = selection.rotate(-rv,0,0);
break; break;
case 'ArrowDown': case 'ArrowDown':
selection.rotate(rv,0,0).floor(); rot = selection.rotate(rv,0,0);
break; break;
case 'ArrowLeft': case 'ArrowLeft':
if (shiftKey) { if (shiftKey) {
selection.rotate(0,-rv,0).floor(); rot = selection.rotate(0,-rv,0);
} else { } else {
selection.rotate(0,0,rv).floor(); rot = selection.rotate(0,0,rv);
} }
break; break;
case 'ArrowRight': case 'ArrowRight':
if (shiftKey) { if (shiftKey) {
selection.rotate(0,rv,0).floor(); rot = selection.rotate(0,rv,0);
} else { } else {
selection.rotate(0,0,-rv).floor(); rot = selection.rotate(0,0,-rv);
} }
break; break;
} }
if (rot && true) {
// todo future pref to auto-floor or not
rot.floor(mesh.group);
}
} }
]); ]);

View file

@ -103,10 +103,10 @@ mesh.model = class MeshModel extends mesh.object {
meh.renderOrder = 1; meh.renderOrder = 1;
// center vertices then restore mesh object position // center vertices then restore mesh object position
// still this doesn't work. scale broken for bounding boxes // still this doesn't work. scale broken for bounding boxes
// let mid = meh.getBoundingBox().mid; let { mid } = meh.getBoundingBox();
// geo.moveMesh(-mid.x, -mid.y, -mid.x); geo.moveMesh(-mid.x, -mid.y, -mid.z);
// geo.computeBoundingBox(); geo.computeBoundingBox();
// this.move(mid.x, mid.y, mid.x); this.move(mid.x, mid.y, mid.z);
// sets fallback opacity for wireframe toggle // sets fallback opacity for wireframe toggle
this.opacity(1); this.opacity(1);
// this ref allows clicks to be traced to models and groups // this ref allows clicks to be traced to models and groups

View file

@ -66,21 +66,23 @@ mesh.object = class MeshObject {
return this; return this;
} }
floor() { floor(clazz) {
let b = this.bounds; if (!clazz || this instanceof clazz) {
this.move(0, 0, -b.min.z); let b = this.bounds;
this.move(0, 0, -b.min.z);
}
return this; return this;
} }
center(bounds) { center(bounds) {
let b = bounds || this.bounds; let b = bounds || this.bounds;
this.move(-b.center.x, -b.center.y, -b.center.z); this.move(-b.mid.x, -b.mid.y, -b.mid.z);
return this; return this;
} }
centerXY(bounds) { centerXY(bounds) {
let b = bounds || this.bounds; let b = bounds || this.bounds;
this.move(-b.center.x, -b.center.y, 0); this.move(-b.mid.x, -b.mid.y, 0);
return this; return this;
} }

View file

@ -1028,7 +1028,7 @@
isHidden: () => { return !showPlatform }, isHidden: () => { return !showPlatform },
isVisible: () => { return platform.visible }, isVisible: () => { return platform.visible },
isGridVisible() { return grid.view.visible }, isGridVisible() { return grid.view.visible },
opacity: (o) => { platform.material.opacity = o }, opacity: (o) => { platform.material.opacity = o; Space.platform.setVisible(o > 0) },
onMove: (f,t) => { platformOnMove = f, platformOnMoveTime = t || platformOnMoveTime }, onMove: (f,t) => { platformOnMove = f, platformOnMoveTime = t || platformOnMoveTime },
onHover: (f) => { platformHover = f }, onHover: (f) => { platformHover = f },
onClick: (f) => { platformClick = f}, onClick: (f) => { platformClick = f},