2024-07-23 22:45:27 -04:00
|
|
|
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
// dep: add.array
|
|
|
|
|
// dep: add.three
|
|
|
|
|
// dep: moto.license
|
|
|
|
|
// dep: moto.client
|
|
|
|
|
// dep: mesh.object
|
|
|
|
|
// use: mesh.api
|
|
|
|
|
// use: mesh.util
|
|
|
|
|
// use: mesh.group
|
|
|
|
|
gapp.register("mesh.sketch", [], (root, exports) => {
|
|
|
|
|
|
|
|
|
|
const { MeshBasicMaterial, LineBasicMaterial, LineSegments, DoubleSide } = THREE;
|
2024-07-24 15:57:32 -04:00
|
|
|
const { PlaneGeometry, EdgesGeometry, SphereGeometry, Vector3, Mesh, Group } = THREE;
|
2024-07-23 22:45:27 -04:00
|
|
|
const { mesh, moto } = root;
|
|
|
|
|
const { space } = moto;
|
|
|
|
|
|
|
|
|
|
const mapp = mesh;
|
|
|
|
|
const worker = moto.client.fn;
|
|
|
|
|
|
|
|
|
|
const material = {
|
2024-07-24 15:57:32 -04:00
|
|
|
normal: new MeshBasicMaterial({ color: 0x888888, side: DoubleSide, transparent: true, opacity: 0.25 }),
|
|
|
|
|
selected: new MeshBasicMaterial({ color: 0x889988, side: DoubleSide, transparent: true, opacity: 0.25 }),
|
|
|
|
|
highlight: new LineBasicMaterial({ color: 0x88ffdd, side: DoubleSide, transparent: true, opacity: 0.50 }),
|
2024-07-23 22:45:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 2D plane containing open and closed polygons which can be extruded **/
|
|
|
|
|
mesh.sketch = class MeshSketch extends mesh.object {
|
2024-07-24 12:29:55 -04:00
|
|
|
constructor(opt = {}) {
|
|
|
|
|
super(opt.id);
|
2024-07-23 22:45:27 -04:00
|
|
|
|
2024-07-24 12:29:55 -04:00
|
|
|
this.file = opt.file || this.id;
|
2024-07-24 15:57:32 -04:00
|
|
|
this.scale = opt.scale || { x: 10, y: 10, z: 0 };
|
2024-07-24 12:29:55 -04:00
|
|
|
this.center = opt.center || { x: 0, y: 0, z: 0 };
|
|
|
|
|
this.normal = opt.normal || { x: 0, y: 0, z: 1 };
|
|
|
|
|
|
2024-07-24 15:57:32 -04:00
|
|
|
const group = this.group = new Group();
|
|
|
|
|
group.sketch = this;
|
|
|
|
|
|
2024-07-24 12:29:55 -04:00
|
|
|
const planeGeometry = new PlaneGeometry(1, 1);
|
2024-07-24 15:57:32 -04:00
|
|
|
const planeMaterial = material.normal;
|
2024-07-23 22:45:27 -04:00
|
|
|
const plane = this.plane = new Mesh(planeGeometry, planeMaterial);
|
2024-07-24 15:57:32 -04:00
|
|
|
plane.sketch = this;
|
2024-07-23 22:45:27 -04:00
|
|
|
|
|
|
|
|
const outlineGeometry = new EdgesGeometry(planeGeometry);
|
2024-07-24 15:57:32 -04:00
|
|
|
const outlineMaterial = material.normal;
|
2024-07-23 22:45:27 -04:00
|
|
|
const outline = this.outline = new LineSegments(outlineGeometry, outlineMaterial);
|
|
|
|
|
|
2024-07-24 15:57:32 -04:00
|
|
|
const handleGeometry = new SphereGeometry(0.5, 16, 16);
|
|
|
|
|
const handleMaterial = material.normal;
|
2024-07-23 22:45:27 -04:00
|
|
|
|
2024-07-24 15:57:32 -04:00
|
|
|
const handles = this.handles = [];
|
|
|
|
|
const corners = this.corners = [ [-0.5, 0.5, 0], [0.5, 0.5, 0], [-0.5, -0.5, 0], [0.5, -0.5, 0] ];
|
2024-07-23 22:45:27 -04:00
|
|
|
|
|
|
|
|
for (let corner of corners) {
|
|
|
|
|
const handle = new Mesh(handleGeometry, handleMaterial);
|
|
|
|
|
handle.position.set(...corner);
|
|
|
|
|
handles.push(handle);
|
2024-07-24 15:57:32 -04:00
|
|
|
group.add(handle);
|
|
|
|
|
handle.sketch = this;
|
2024-07-23 22:45:27 -04:00
|
|
|
}
|
2024-07-24 12:29:55 -04:00
|
|
|
|
2024-07-24 15:57:32 -04:00
|
|
|
group.add(outline);
|
|
|
|
|
group.add(plane);
|
|
|
|
|
|
2024-07-24 12:29:55 -04:00
|
|
|
this.update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update() {
|
2024-07-24 15:57:32 -04:00
|
|
|
const { plane, outline, center, handles, corners, normal, scale } = this;
|
2024-07-24 12:29:55 -04:00
|
|
|
|
2024-07-24 15:57:32 -04:00
|
|
|
// group.rotation.x = -Math.PI/2;
|
2024-07-24 12:29:55 -04:00
|
|
|
plane.scale.set(scale.x, scale.y, 1);
|
|
|
|
|
plane.position.set(center.x, center.y, center.z);
|
2024-07-24 15:57:32 -04:00
|
|
|
outline.scale.set(scale.x, scale.y, 1);
|
|
|
|
|
outline.position.set(center.x, center.y, center.z);
|
|
|
|
|
|
|
|
|
|
for (let i=0; i<4; i++) {
|
|
|
|
|
const handle = handles[i];
|
|
|
|
|
const corner = corners[i];
|
|
|
|
|
handle.position.set(
|
|
|
|
|
(corner[0] * scale.x) + center.x,
|
|
|
|
|
(corner[1] * scale.y) + center.y,
|
|
|
|
|
(corner[2] * scale.z) + center.z
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-07-24 12:29:55 -04:00
|
|
|
|
2024-07-24 15:57:32 -04:00
|
|
|
// const nv = new Vector3(normal.x, normal.y, normal.z);
|
|
|
|
|
// const tp = new Vector3().addVectors(group.position, nv);
|
|
|
|
|
// group.lookAt(tp);
|
2024-07-24 12:29:55 -04:00
|
|
|
|
|
|
|
|
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);
|
2024-07-23 22:45:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get type() {
|
|
|
|
|
return "sketch";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get object() {
|
2024-07-24 15:57:32 -04:00
|
|
|
return this.group;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get meshes() {
|
|
|
|
|
return this.group.children.filter(c => c.sketch ? c : undefined);
|
2024-07-23 22:45:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rename(newname) {
|
|
|
|
|
this.file = newname;
|
2024-07-24 12:29:55 -04:00
|
|
|
this.#db_save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remove() {
|
|
|
|
|
this.#db_remove();
|
2024-07-23 22:45:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
highlight() {
|
|
|
|
|
this.outline.material = material.highlight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unhighlight() {
|
2024-07-24 15:57:32 -04:00
|
|
|
this.outline.material = material.normal;
|
2024-07-23 22:45:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
select(bool) {
|
2024-07-24 15:57:32 -04:00
|
|
|
const { plane, handles } = this;
|
2024-07-23 22:45:27 -04:00
|
|
|
if (bool === undefined) {
|
2024-07-24 15:57:32 -04:00
|
|
|
return plane.material === material.selected;
|
2024-07-23 22:45:27 -04:00
|
|
|
}
|
|
|
|
|
if (bool.toggle) {
|
|
|
|
|
return this.select(!this.select());
|
|
|
|
|
}
|
2024-07-24 15:57:32 -04:00
|
|
|
plane.material = (bool ? material.selected : material.normal);
|
|
|
|
|
for (let handle of handles) {
|
|
|
|
|
handle.material = bool ? material.highlight : plane.material;
|
|
|
|
|
}
|
|
|
|
|
return bool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
move(x, y, z = 0) {
|
|
|
|
|
const { center, scale, plane, handles, dragging } = this;
|
|
|
|
|
const handle = handles.indexOf(dragging);
|
|
|
|
|
if (dragging === plane) {
|
|
|
|
|
center.x += x;
|
|
|
|
|
center.y += y;
|
|
|
|
|
center.z += z;
|
|
|
|
|
this.update();
|
|
|
|
|
} else if (handle >= 0) {
|
|
|
|
|
const sf = [
|
|
|
|
|
[-1, 1, 1],
|
|
|
|
|
[ 1, 1, 1],
|
|
|
|
|
[-1,-1, 1],
|
|
|
|
|
[ 1,-1, 1],
|
|
|
|
|
][handle];
|
|
|
|
|
center.x += x / 2;
|
|
|
|
|
center.y += y / 2;
|
|
|
|
|
center.z += z / 2;
|
|
|
|
|
scale.x += x * sf[0];
|
|
|
|
|
scale.y += y * sf[1];
|
|
|
|
|
scale.z += z * sf[2];
|
|
|
|
|
this.update();
|
|
|
|
|
} else {
|
|
|
|
|
console.log({ sketch_move: [...arguments] });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
drag(opt = {}) {
|
|
|
|
|
const { plane, handles, dragging } = this;
|
|
|
|
|
if (opt.start) {
|
|
|
|
|
// console.log({ drag_start: opt.start, plane, same: opt.start === plane });
|
|
|
|
|
this.dragging = opt.start;
|
|
|
|
|
} else if (opt.end) {
|
|
|
|
|
console.log({ drag_end: opt.end });
|
|
|
|
|
} else {
|
|
|
|
|
console.log({ invalid_sketch_drag: opt });
|
|
|
|
|
}
|
2024-07-23 22:45:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|