From f53a9752f8c45767b8d67ce34e0a241e3e59b604 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Tue, 11 Jan 2022 10:54:31 -0500 Subject: [PATCH] fix matrix cache corruption during reload. center models on import --- src/main/mesh.js | 31 ++++++++++++++++++++++--------- src/mesh/model.js | 8 ++++---- src/mesh/object.js | 12 +++++++----- src/moto/space.js | 2 +- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/main/mesh.js b/src/main/mesh.js index 5b864d6b..9bd34859 100644 --- a/src/main/mesh.js +++ b/src/main/mesh.js @@ -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); + } } ]); diff --git a/src/mesh/model.js b/src/mesh/model.js index e2d8a967..6c47b9c0 100644 --- a/src/mesh/model.js +++ b/src/mesh/model.js @@ -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 diff --git a/src/mesh/object.js b/src/mesh/object.js index 2dc8ce6e..da53de0c 100644 --- a/src/mesh/object.js +++ b/src/mesh/object.js @@ -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; } diff --git a/src/moto/space.js b/src/moto/space.js index 9e06098c..f57338d8 100644 --- a/src/moto/space.js +++ b/src/moto/space.js @@ -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},