update worker/db sync model

This commit is contained in:
Stewart Allen 2024-08-10 13:24:01 -04:00
commit 08f67a0ea6
5 changed files with 68 additions and 67 deletions

View file

@ -943,23 +943,22 @@ const tool = {
return log('nothing to union');
}
log(`union ${models.length} models`).pin();
worker.model_union(models.map(m => {
return { id: m.id, matrix: m.matrix }
}))
.then(data => {
api.selection.visible(false);
let group = api.group.new([new mesh.model({
file: `union`,
mesh: data
})]).promote();
api.selection.set([group]);
})
.catch(error => {
log(`union error: ${error}`);
})
.finally(() => {
log('union complete').unpin();
});
worker
.model_union(models.map(m => m.id))
.then(data => {
api.selection.visible(false);
let group = api.group.new([new mesh.model({
file: `union`,
mesh: data
})]).promote();
api.selection.set([ group ]);
})
.catch(error => {
log(`union error: ${error}`);
})
.finally(() => {
log('union complete').unpin();
});
},
difference(models) {
@ -968,9 +967,7 @@ const tool = {
return log('nothing to diff');
}
log(`diff ${models.length} models`).pin();
worker.model_difference(models.map(m => {
return { id: m.id, matrix: m.matrix }
}))
worker.model_difference(models.map(m => m.id))
.then(data => {
api.selection.visible(false);
let group = api.group.new([new mesh.model({
@ -993,9 +990,7 @@ const tool = {
return log('nothing to intersect');
}
log(`intersect ${models.length} models`).pin();
worker.model_intersect(models.map(m => {
return { id: m.id, matrix: m.matrix }
}))
worker.model_intersect(models.map(m => m.id))
.then(data => {
let group = api.group.new([new mesh.model({
file: `intersect`,
@ -1019,7 +1014,7 @@ const tool = {
}
log(`subtract ${models.length} models`).pin();
worker.model_subtract(models.map(m => {
return { id: m.id, matrix: m.matrix, tool: m.tool() }
return { id: m.id, tool: m.tool() }
}))
.then(data => {
let group = api.group.new([new mesh.model({

View file

@ -94,7 +94,6 @@ mesh.group = class MeshGroup extends mesh.object {
}
promote() {
this.centerModels();
let { dim, mid } = this.bounds;
let { x, y, z } = dim;
let max = Math.max(x, y, z);

View file

@ -140,6 +140,10 @@ mesh.model = class MeshModel extends mesh.object {
.attributes.position.array;
}
get positions() {
return this.geometry.attributes.position.array;
}
get matrix() {
return this.mesh.matrixWorld.elements;
}
@ -188,8 +192,6 @@ mesh.model = class MeshModel extends mesh.object {
this.log('model-rotate', quaternion.toArray());
this.geometry.applyQuaternion(quaternion);
this.updateBounds();
this.persist();
moto.space.update();
}
scale(x = 1, y = 1, z = 1) {
@ -197,8 +199,6 @@ mesh.model = class MeshModel extends mesh.object {
if (x === 1 && y === 1 && z === 1) return;
this.geometry.scale(x, y, z);
this.updateBounds();
this.persist();
moto.space.update();
}
translate(x = 0, y = 0, z = 0) {
@ -206,7 +206,6 @@ mesh.model = class MeshModel extends mesh.object {
if (!(x || y || z)) return;
this.geometry.translate(x, y, z);
this.updateBounds();
this.persist();
}
move(x = 0, y = 0, z = 0) {
@ -222,7 +221,7 @@ mesh.model = class MeshModel extends mesh.object {
return pos;
}
pos.set(...arguments);
this.metaChanged();
this.metaChanged({ pos: pos.toArray() });
return this;
}
@ -264,6 +263,7 @@ mesh.model = class MeshModel extends mesh.object {
this.log('update-bounds', this.geometry.boundingBox);
this.updateBoundsBox();
moto.space.update();
this.sync();
}
updateBoundsBox() {
@ -315,11 +315,7 @@ mesh.model = class MeshModel extends mesh.object {
this.opacity(1);
// this ref allows clicks to be traced to models and groups
meh.model = this;
// persist in db so it can be restored on page load
this.persist();
// sync data to worker
worker.model_load({ id: this.id, name: this.file, vertices });
// update bounds
// sync worker, allows raycasting to work
this.updateBounds();
}
@ -331,29 +327,25 @@ mesh.model = class MeshModel extends mesh.object {
// signal util.box3expand that geometry changed
geo._model_invalid = true;
geo.computeVertexNormals();
// allows raycasting to work
// geo.computeBoundingSphere();
// persist in db so it can be restored on page load
this.persist();
// sync data to worker
worker.model_load({id: this.id, name: this.name, vertices });
// restore wireframe state
this.wireframe(was);
// fixup normals
this.normals({refresh: true});
// sync worker, allows raycasting to work
this.updateBounds();
// re-gen face index in surface mode
mesh.api.mode.check();
// allows raycasting to work
// update bounds
this.updateBounds();
}
rename(file) {
this.file = file;
this.persist();
this.sync();
}
persist() {
// sync to worker and indexeddb for page restoration or worker ops
sync() {
// sync to worker
worker.model_load({ id: this.id, name: this.file, vertices: this.positions });
// persist in db so it can be restored on page load
mapp.db.space.put(this.id, {
file: this.file,
@ -556,7 +548,7 @@ mesh.model = class MeshModel extends mesh.object {
// extract axes from plane and split when present (only z for now)
let { z } = plane;
return new Promise((resolve,reject) => {
worker.model_split({ pos: this.world_positions, z }).then(data => {
worker.model_split({ id: this.id, z }).then(data => {
let { o1, o2 } = data;
let model;
if (o1.length)

View file

@ -34,7 +34,7 @@ mesh.object = class MeshObject {
}
log() {
console.log(this.id, this.type, ...arguments);
// mesh.api.log.emit(this.id, this.type, ...arguments);
}
get type() {
@ -65,6 +65,7 @@ mesh.object = class MeshObject {
this.log('apply-meta', meta);
this.applyMatrix(meta.matrix);
this.visible(meta.visible ?? true);
this.metaChanged(meta);
return this;
}
@ -73,10 +74,8 @@ mesh.object = class MeshObject {
if (elements) {
this.log('apply-matrix');
this.object.applyMatrix4(new Matrix4().fromArray(elements));
return this.metaChanged();
} else {
return this;
}
return this;
}
// used during model splitting
@ -195,11 +194,14 @@ mesh.object = class MeshObject {
}
}
metaChanged() {
metaChanged(values = {}) {
this.object.updateMatrix();
this.updateBoundsBox();
space.update();
this.meta.matrix = this.object.matrix.elements;
Object.assign(this.meta, values, {
matrix: this.object.matrix.elements
});
worker.object_meta({ id: this.id, meta: this.meta });
publish.meta({ id: this.id, meta: this.meta });
return this;
}

View file

@ -21,7 +21,7 @@
gapp.main("mesh.work", [], (root) => {
const { Matrix4, Vector3, BufferGeometry, BufferAttribute, computeFaceNormal } = THREE;
const { Quaternion } = THREE;
const { Quaternion, } = THREE;
const { base, mesh, moto } = root;
const { client, worker } = moto;
const { util } = mesh;
@ -48,13 +48,19 @@ function cacheUpdate(id, data) {
}
// translate original mesh vertices into UI world view (PI/2 rotation on X)
function translate_encode(id, matrix) {
function translate_encode(id) {
let rec = cache[id];
let { pos, matrix } = rec;
let mkey = matrix.map(v => v.round(6)).join('-');
// re-translate on missing cache or changed matrix
if (!rec || !rec.trans || rec.mkey !== mkey) {
let geo = rec.geo.clone();
let mat = core_matrix.clone().multiply(new Matrix4().fromArray(matrix));
geo.applyMatrix4(mat);
if (pos) {
geo.translate(new Vector3().fromArray(pos));
} else {
let mat = core_matrix.clone().multiply(new Matrix4().fromArray(matrix));
geo.applyMatrix4(mat);
}
rec.mkey = mkey;
rec.trans = geo.attributes.position.array
}
@ -122,22 +128,22 @@ let model = {
return data;
},
union(recs) {
let arrays = recs.map(rec => translate_encode(rec.id, rec.matrix));
union(ids) {
let arrays = ids.map(id => translate_encode(id));
let solids = arrays.map(a => CSG.fromPositionArray(a));
let union = CSG.union(...solids);
return CSG.toPositionArray(union);
},
difference(recs) {
let arrays = recs.map(rec => translate_encode(rec.id, rec.matrix));
difference(ids) {
let arrays = ids.map(id => translate_encode(id));
let solids = arrays.map(a => CSG.fromPositionArray(a));
let diff = CSG.difference(...solids);
return CSG.toPositionArray(diff);
},
intersect(recs) {
let arrays = recs.map(rec => translate_encode(rec.id, rec.matrix));
intersect(ids) {
let arrays = ids.map(id => translate_encode(id));
let solids = arrays.map(a => CSG.fromPositionArray(a));
let union = CSG.intersect(...solids);
return CSG.toPositionArray(union);
@ -145,10 +151,10 @@ let model = {
subtract(recs) {
let bases = recs.filter(rec => !rec.tool)
.map(rec => translate_encode(rec.id, rec.matrix))
.map(rec => translate_encode(rec.id))
.map(a => CSG.fromPositionArray(a));
let tools = recs.filter(rec => rec.tool)
.map(rec => translate_encode(rec.id, rec.matrix))
.map(rec => translate_encode(rec.id))
.map(a => CSG.fromPositionArray(a));
let subs = [];
for (let obj of bases) {
@ -161,7 +167,8 @@ let model = {
// split a model along an axis at a given point
// return two arrays of vertices for each resulting object
split(data) {
let { pos, z } = data;
let { id, z } = data;
let pos = translate_encode(id);
let o1 = []; // new bottom
let o2 = []; // new top
let on = [];
@ -372,6 +379,12 @@ let group = {
};
let object = {
meta(data) {
let { id, meta } = data;
cacheUpdate(id, meta);
console.log('object_meta', data);
},
create(data) {
let { id, type } = data;
cache[id] = { id, type };