From f36808c16004e7bedc4f55443c7949e067743795 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Sat, 27 Jul 2024 00:39:23 -0400 Subject: [PATCH] fix sketch normal, extrude to match sketch normal --- src/main/mesh.js | 6 ++++-- src/mesh/sketch.js | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/mesh.js b/src/main/mesh.js index 756ef75a..073f602d 100644 --- a/src/main/mesh.js +++ b/src/main/mesh.js @@ -408,9 +408,11 @@ function space_init(data) { // set focus on intersected face const { x, y, z } = int.point; // rotate normal using group's matrix - const normal = shiftKey ? int.face.normal : undefined; + const normal = shiftKey ? int.face.normal.clone(): undefined; + // ransform the normal to align with sketch group + normal?.transformDirection(int.object.sketch.group.matrix); // y,z swap due to world rotation for orbit controls - api.focus({center: { x, y:-z, z:y }, normal}); + api.focus({ center: { x, y:-z, z:y }, normal }); } else { selection.toggle(sketch); } diff --git a/src/mesh/sketch.js b/src/mesh/sketch.js index 8e6b3bdd..085ea315 100644 --- a/src/mesh/sketch.js +++ b/src/mesh/sketch.js @@ -14,7 +14,7 @@ // use: mesh.group gapp.register("mesh.sketch", [], (root, exports) => { -const { BufferGeometry, BufferAttribute } = THREE; +const { BufferGeometry, BufferAttribute, Quaternion } = THREE; const { MeshBasicMaterial, LineBasicMaterial, LineSegments, DoubleSide } = THREE; const { PlaneGeometry, EdgesGeometry, SphereGeometry, Vector3, Mesh, Group } = THREE; const { base, mesh, moto } = root; @@ -85,7 +85,7 @@ mesh.sketch = class MeshSketch extends mesh.object { } update() { - const { group, plane, outline, center, handles, corners, normal, scale } = this; + let { group, plane, outline, center, handles, corners, normal, scale } = this; plane.scale.set(scale.x, scale.y, 1); outline.scale.set(scale.x, scale.y, 1); @@ -101,12 +101,8 @@ mesh.sketch = class MeshSketch extends mesh.object { ); } - const { position } = group; - group.lookAt(new Vector3( - normal.x + position.x, - normal.z + position.z, - normal.y - position.y - )); + normal = new THREE.Vector3(normal.x, normal.y, normal.z).normalize(); + group.quaternion.copy(new Quaternion().setFromUnitVectors(new Vector3(0, 0, 1), normal)); this.#db_save(); } @@ -117,7 +113,7 @@ mesh.sketch = class MeshSketch extends mesh.object { } #db_save() { - const { center, normal, scale, type, file, items } = this; + let { center, normal, scale, type, file, items } = this; mapp.db.space.put(this.id, { center, normal, scale, type, file, items }); } @@ -456,7 +452,6 @@ mesh.sketch = class MeshSketch extends mesh.object { } extrude(opt = {}) { - console.log('extrude',opt); let { selection, height, chamfer, chamfer_top, chamfer_bottom } = opt; let models = []; let items = this.group.children @@ -477,6 +472,11 @@ mesh.sketch = class MeshSketch extends mesh.object { let ngrp = api.group.new(models); ngrp.floor(); ngrp.move(center.x, center.y, center.z); + // align extrusion with sketch plane + const euler = this.group.rotation; + const quaternion = new THREE.Quaternion(); + quaternion.setFromEuler(euler); + ngrp.qrotation(quaternion); } } }