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() {
let count = 0;
let space = moto.Space;
let parray = [];
let mcache;
mesh.db.admin.get("camera")
.then(saved => {
if (saved) {
@ -85,7 +87,7 @@ function restore_space() {
mesh.db.admin.get("matrices")
.then(data => {
if (data) {
matrixCache = data;
mcache = data;
}
});
mesh.db.space.iterate({ map: true }).then(cached => {
@ -98,10 +100,10 @@ function restore_space() {
let models = data
.map(id => { return { id, md: cached[id] } })
.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)`);
let group = mesh.api.group.new(models, id);
let matrix = matrixCache[id];
let matrix = mcache[id];
if (matrix) {
group.applyMatrix(matrix);
} 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
$d('curtain','none');
@ -155,6 +161,8 @@ function space_init(data) {
break;
case 'KeyC':
return selection.centerXY().focus();
case 'KeyF':
return selection.floor().focus();
case 'KeyI':
return api.file.import();
case 'KeyX':
@ -188,6 +196,7 @@ function space_init(data) {
}
return;
}
let rot;
switch (code) {
case 'KeyA':
if (metaKey || ctrlKey) {
@ -209,26 +218,30 @@ function space_init(data) {
estop(evt);
break;
case 'ArrowUp':
selection.rotate(-rv,0,0).floor();
rot = selection.rotate(-rv,0,0);
break;
case 'ArrowDown':
selection.rotate(rv,0,0).floor();
rot = selection.rotate(rv,0,0);
break;
case 'ArrowLeft':
if (shiftKey) {
selection.rotate(0,-rv,0).floor();
rot = selection.rotate(0,-rv,0);
} else {
selection.rotate(0,0,rv).floor();
rot = selection.rotate(0,0,rv);
}
break;
case 'ArrowRight':
if (shiftKey) {
selection.rotate(0,rv,0).floor();
rot = selection.rotate(0,rv,0);
} else {
selection.rotate(0,0,-rv).floor();
rot = selection.rotate(0,0,-rv);
}
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;
// center vertices then restore mesh object position
// still this doesn't work. scale broken for bounding boxes
// let mid = meh.getBoundingBox().mid;
// geo.moveMesh(-mid.x, -mid.y, -mid.x);
// geo.computeBoundingBox();
// this.move(mid.x, mid.y, mid.x);
let { mid } = meh.getBoundingBox();
geo.moveMesh(-mid.x, -mid.y, -mid.z);
geo.computeBoundingBox();
this.move(mid.x, mid.y, mid.z);
// sets fallback opacity for wireframe toggle
this.opacity(1);
// this ref allows clicks to be traced to models and groups

View file

@ -66,21 +66,23 @@ mesh.object = class MeshObject {
return this;
}
floor() {
let b = this.bounds;
this.move(0, 0, -b.min.z);
floor(clazz) {
if (!clazz || this instanceof clazz) {
let b = this.bounds;
this.move(0, 0, -b.min.z);
}
return this;
}
center(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;
}
centerXY(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;
}

View file

@ -1028,7 +1028,7 @@
isHidden: () => { return !showPlatform },
isVisible: () => { return platform.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 },
onHover: (f) => { platformHover = f },
onClick: (f) => { platformClick = f},