add sketch persistence

This commit is contained in:
Stewart Allen 2024-07-24 12:29:55 -04:00
commit 2efd26a2cc
3 changed files with 51 additions and 10 deletions

View file

@ -106,11 +106,14 @@ async function restore_space() {
const keys = [];
const claimed = [];
for (let [id, data] of Object.entries(cached)) {
// console.log({ id, data });
keys.push(id);
if (count++ === 0) {
mesh.api.log.emit(`restoring workspace`);
}
// restore group
// restore object based on type
// group arrays load models they contain
// sketches are loaded by type since they're not grouped
if (Array.isArray(data)) {
claimed.push(id);
let models = data
@ -127,6 +130,9 @@ async function restore_space() {
mesh.api.log.emit(`removed empty group ${id}`);
db_space.remove(id);
}
} else if (data.type === 'sketch') {
claimed.push(data);
mesh.api.add.sketch(data);
}
}
for (let id of claimed) {

View file

@ -395,7 +395,7 @@ let sketch = {
remove(sk) {
sketches.remove(sk);
space.world.remove(sk.object);
space.update();s
space.update();
},
list() {
@ -404,8 +404,8 @@ let sketch = {
};
let add = {
sketch() {
sketch.add(new mesh.sketch());
sketch(opt) {
sketch.add(new mesh.sketch(opt));
},
input() {

View file

@ -13,7 +13,7 @@
gapp.register("mesh.sketch", [], (root, exports) => {
const { MeshBasicMaterial, LineBasicMaterial, LineSegments, DoubleSide } = THREE;
const { PlaneGeometry, EdgesGeometry, SphereGeometry, Group, Mesh } = THREE;
const { PlaneGeometry, EdgesGeometry, SphereGeometry, Vector3, Mesh } = THREE;
const { mesh, moto } = root;
const { space } = moto;
@ -30,10 +30,15 @@ const material = {
/** 2D plane containing open and closed polygons which can be extruded **/
mesh.sketch = class MeshSketch extends mesh.object {
constructor(model, face, id) {
super(id);
constructor(opt = {}) {
super(opt.id);
const planeGeometry = new PlaneGeometry(10, 10);
this.file = opt.file || this.id;
this.scale = opt.scale || { x: 10, y: 10 };
this.center = opt.center || { x: 0, y: 0, z: 0 };
this.normal = opt.normal || { x: 0, y: 0, z: 1 };
const planeGeometry = new PlaneGeometry(1, 1);
const planeMaterial = material.plane;
const plane = this.plane = new Mesh(planeGeometry, planeMaterial);
@ -42,11 +47,11 @@ mesh.sketch = class MeshSketch extends mesh.object {
const outline = this.outline = new LineSegments(outlineGeometry, outlineMaterial);
plane.add(outline);
const handleGeometry = new SphereGeometry(0.2, 16, 16);
const handleGeometry = new SphereGeometry(0.05, 16, 16);
const handleMaterial = material.handle;
const handles = [];
const corners = [ [-5, 5, 0], [5, 5, 0], [-5, -5, 0], [5, -5, 0] ];
const corners = [ [-0.5, 0.5, 0], [0.5, 0.5, 0], [-0.5, -0.5, 0], [0.5, -0.5, 0] ];
for (let corner of corners) {
const handle = new Mesh(handleGeometry, handleMaterial);
@ -54,6 +59,31 @@ mesh.sketch = class MeshSketch extends mesh.object {
plane.add(handle);
handles.push(handle);
}
this.update();
}
update() {
const { plane, center, normal, scale, type } = this;
plane.scale.set(scale.x, scale.y, 1);
plane.position.set(center.x, center.y, center.z);
const normalVector = new Vector3(normal.x, normal.y, normal.z);
const targetPoint = new Vector3().addVectors(plane.position, normalVector);
plane.lookAt(targetPoint);
this.#db_save();
}
#db_save() {
const { center, normal, scale, type, file } = this;
mapp.db.space.put(this.id, { center, normal, scale, type, file });
}
#db_remove() {
mapp.api.sketch.remove(this);
mapp.db.space.remove(this.id);
}
get type() {
@ -66,6 +96,11 @@ mesh.sketch = class MeshSketch extends mesh.object {
rename(newname) {
this.file = newname;
this.#db_save();
}
remove() {
this.#db_remove();
}
highlight() {