preserve object visibility

This commit is contained in:
Stewart Allen 2024-08-02 10:17:40 -04:00
commit b5ed7b1a4a
2 changed files with 32 additions and 15 deletions

View file

@ -104,7 +104,7 @@ async function restore_space() {
space.view.setFocus(saved.focus); space.view.setFocus(saved.focus);
} }
}); });
const mcache = await db_admin.get("matrices") || {}; const mcache = await db_admin.get("meta") || {};
let count = 0; let count = 0;
await db_space.iterate({ map: true }).then(cached => { await db_space.iterate({ map: true }).then(cached => {
const keys = []; const keys = [];
@ -126,10 +126,12 @@ async function restore_space() {
return { id, md: cached[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(mcache[r.id])); .map(r => new mesh.model(r.md, r.id)
.applyMatrix(mcache[r.id]?.matrix)
.visible(mcache[r.id]?.visible ?? true));
if (models.length) { if (models.length) {
log(`restored ${models.length} model(s)`); log(`restored ${models.length} model(s)`);
mesh.api.group.new(models, id).applyMatrix(mcache[id]); mesh.api.group.new(models, id).applyMatrix(mcache[id]?.matrix)
} else { } else {
log(`removed empty group ${id}`); log(`removed empty group ${id}`);
db_space.remove(id); db_space.remove(id);
@ -145,15 +147,15 @@ async function restore_space() {
if (keys.length) { if (keys.length) {
log(`removing ${keys.length} unclaimed meshes`); log(`removing ${keys.length} unclaimed meshes`);
} }
// clear out meshes left in the space db along with their matrices // clear out meshes left in the space db along with their meta-data
for (let id of keys) { for (let id of keys) {
db_space.remove(id); db_space.remove(id);
delete mcache[id]; delete mcache[id];
} }
// restore global cache only after objects are restored // restore global cache only after objects are restored
// otherwise their setup will corrupt the cache for other restores // otherwise their setup will corrupt the cache for other restores
matrixCache = mcache; metaCache = mcache;
store_matrices(); store_meta();
}).then(() => { }).then(() => {
// restore preferences after models are restored // restore preferences after models are restored
return api.prefs.load().then(() => { return api.prefs.load().then(() => {
@ -589,23 +591,33 @@ function space_load(data) {
.focus(); .focus();
} }
let matrixCache = {}; let metaCache = {};
// todo deferred with util function store_meta() {
function store_matrices() { mesh.db.admin.put("meta", metaCache);
mesh.db.admin.put("matrices", matrixCache); }
function update_meta(id, data) {
let meta = metaCache[id] = metaCache[id] || {};
Object.assign(meta, data);
store_meta();
}
// cache model visibility for page restores
function object_visible(data) {
let { id, visible } = data;
update_meta(id, { visible });
} }
// cache model matrices for page restores // cache model matrices for page restores
function object_matrix(data) { function object_matrix(data) {
let { id, matrix } = data; let { id, matrix } = data;
matrixCache[id] = matrix.elements; update_meta(id, { matrix: matrix.elements });
store_matrices();
} }
function object_destroy(id) { function object_destroy(id) {
delete matrixCache[id]; delete metaCache[id];
store_matrices(); store_meta();
} }
// listen for changes like dark mode toggle // listen for changes like dark mode toggle
@ -696,6 +708,7 @@ broker.listeners({
load_files, load_files,
object_matrix, object_matrix,
object_destroy, object_destroy,
object_visible,
space_init, space_init,
space_load, space_load,
set_darkmode, set_darkmode,

View file

@ -19,6 +19,7 @@ const worker = moto.client.fn;
let publish = { let publish = {
matrix: gapp.broker.bind("object_matrix"), matrix: gapp.broker.bind("object_matrix"),
destroy: gapp.broker.bind("object_destroy"), destroy: gapp.broker.bind("object_destroy"),
visible: gapp.broker.bind("object_visible"),
}; };
mesh.object = class MeshObject { mesh.object = class MeshObject {
@ -55,8 +56,10 @@ mesh.object = class MeshObject {
applyMatrix(elements) { applyMatrix(elements) {
if (elements) { if (elements) {
this.object.applyMatrix4(new Matrix4().fromArray(elements)); this.object.applyMatrix4(new Matrix4().fromArray(elements));
return this.matrixChanged();
} else {
return this;
} }
return this.matrixChanged();
} }
applyMatrix4(matrix) { applyMatrix4(matrix) {
@ -85,6 +88,7 @@ mesh.object = class MeshObject {
} else { } else {
this.object.visible = opt; this.object.visible = opt;
} }
publish.visible({ id: this.id, visible: this.object.visible });
return this; return this;
} }