From 9f19ed9fde9b604078b576293c51e0878424ba07 Mon Sep 17 00:00:00 2001 From: howard Date: Sat, 3 Apr 2021 21:54:27 -0700 Subject: [PATCH] not idea --- src/Scene.js | 31 ++++---- src/extrude.js | 2 +- src/sketcher/Sketch.js | 16 ++--- src/sketcher/drawDimension.js | 130 +++++++++++++++++----------------- src/utils/shared.js | 10 +-- 5 files changed, 92 insertions(+), 97 deletions(-) diff --git a/src/Scene.js b/src/Scene.js index 1df6b30..a0baa1e 100644 --- a/src/Scene.js +++ b/src/Scene.js @@ -13,7 +13,7 @@ import { extrude } from './extrude' import { onHover, onPick } from './utils/mouseEvents'; import { _vec2, _vec3, color, awaitPts } from './utils/shared' import { Vector3 } from 'three/src/Three'; -import {AxesHelper} from './utils/axes' +import { AxesHelper } from './utils/axes' import CSG from "./utils/three-csg.js" @@ -152,7 +152,7 @@ export class Scene { const entries = state.treeEntries.byNid for (let k in entries) { - if (k[0] == 's') { + if (k[0] == 's') { entries[k].obj3d = loader.parse(entries[k].obj3d) this.obj3d.add(entries[k].obj3d) @@ -190,32 +190,33 @@ function render() { async function addSketch() { - let references = await this.awaitPts(3) + let sketch; - const sketch = new Sketch(this.camera, this.canvas, this.store) - - if (references.length == 1 && references[0].name[0] == 'd') { - this.obj3d.add(sketch.obj3d) - sketch.obj3d.matrix = references[0].matrix + if (this.selected[0].name[0] == 'd') { + sketch = new Sketch(this.camera, this.canvas, this.store) + sketch.obj3d.matrix = this.selected[0].matrix sketch.plane.applyMatrix4(sketch.obj3d.matrix) sketch.obj3d.inverse = sketch.obj3d.matrix.clone().invert() - - } else if (references.length == 3) { - this.obj3d.add(sketch.obj3d) + } else { + const references = await this.awaitPts(3); + if (references.length != 3) return; + sketch = new Sketch(this.camera, this.canvas, this.store) sketch.align( ...references.map( el => new Vector3(...el.geometry.attributes.position.array).applyMatrix4(el.matrixWorld) ) ) - - } else { - console.log('cancelled') - return; } + this.obj3d.add(sketch.obj3d) + + + sketch.activate() + sketch.obj3d.addEventListener('change', this.render); this.render() + console.log('render') this.store.dispatch({ type: 'rx-sketch', obj: sketch }) } diff --git a/src/extrude.js b/src/extrude.js index 6e6b734..6d6eb91 100644 --- a/src/extrude.js +++ b/src/extrude.js @@ -64,7 +64,7 @@ export function extrude(sketch) { } - findPair(children[1]) + findPair(children[3]) //??? need fixing const shape = new THREE.Shape(v2s); const extrudeSettings = { depth: 8, bevelEnabled: false }; diff --git a/src/sketcher/Sketch.js b/src/sketcher/Sketch.js index 2703fa2..cd17b41 100644 --- a/src/sketcher/Sketch.js +++ b/src/sketcher/Sketch.js @@ -9,7 +9,7 @@ import { get3PtArc } from './drawArc' import { _vec2, _vec3, raycaster, awaitPts } from '../utils/shared' import { replacer, reviver } from '../utils/mapJSONReplacer' import { AxesHelper } from '../utils/axes' -import { drawDimension } from './drawDimension'; +import { drawDimension, _onMoveDimension } from './drawDimension'; @@ -45,11 +45,9 @@ class Sketch { this.constraints = new Map() this.c_id = 0; - this.sub = new THREE.Group(); - this.obj3d.add(this.sub); - const axesHelper = new AxesHelper(2); - this.sub.add(axesHelper); - + this.obj3d.add(new THREE.Group().add(new AxesHelper(2))); + this.obj3d.add(new THREE.Group()); + this.obj3d.add(new THREE.Group()); } else { @@ -108,6 +106,7 @@ class Sketch { this.drawPreClick2 = drawPreClick2.bind(this); this.drawOnClick2 = drawOnClick2.bind(this); this.drawDimension = drawDimension.bind(this) + this._onMoveDimension = _onMoveDimension.bind(this) this.awaitPts = awaitPts.bind(this); @@ -117,6 +116,7 @@ class Sketch { this.onRelease = onRelease.bind(this); this.onKeyPress = this.onKeyPress.bind(this); + } @@ -335,11 +335,11 @@ class Sketch { /* - loop to update all the children that are points - - we skip first triplet because it refers to a non-geometry child + - why +6? we skip first two triplets because it refers to a non-geometry children - we also sneak in updating lines children as well, by checking when ptsBuf[ptr] is NaN */ - for (let i = 1, ptr = (pts_buffer >> 2) + 3; i < this.obj3d.children.length; i += 1, ptr += 3) { + for (let i = 3, ptr = (pts_buffer >> 2) + 9; i < this.obj3d.children.length; i += 1, ptr += 3) { // for (let i = 0, ptr = (pts_buffer >> 2) + 3; i < this.obj3d.children.length; i += 1, ptr += 3) { const pos = this.obj3d.children[i].geometry.attributes.position; diff --git a/src/sketcher/drawDimension.js b/src/sketcher/drawDimension.js index 66fc915..22b7bfc 100644 --- a/src/sketcher/drawDimension.js +++ b/src/sketcher/drawDimension.js @@ -17,9 +17,6 @@ export async function drawDimension() { if (pts.length != 2) return; - - - const line = new THREE.LineSegments( new THREE.BufferGeometry().setAttribute('position', new THREE.Float32BufferAttribute(3 * 8, 3) @@ -34,21 +31,72 @@ export async function drawDimension() { pointMaterial.clone() ) - const group = this.obj3d.children[0] - group.add(line) - group.add(point) + line.userData.construction = true + point.userData.construction = true + + const groupLines = this.obj3d.children[1] + const groupPts = this.obj3d.children[2] + groupLines.add(line) + groupPts.add(point) + + const onMove = this._onMoveDimension(...pts, point, line) + + let onEnd, onKey; + + let add = await new Promise((res) => { + onEnd = (e) => res(true) + onKey = (e) => e.key == 'Escape' && res(false) + + this.canvas.addEventListener('pointermove', onMove) + this.canvas.addEventListener('pointerdown', onEnd) + window.addEventListener('keydown', onKey) + }) + + this.canvas.removeEventListener('pointermove', onMove) + this.canvas.removeEventListener('pointerdown', onEnd) + window.removeEventListener('keydown', onKey) + + if (add) { + this.constraints.set(++this.c_id, //??? + [ + 'pt_pt_distance', 10, + [pts[0].name, pts[1].name, -1, -1] + ] + ) + pts[0].userData.constraints.push(this.c_id) + pts[1].userData.constraints.push(this.c_id) + + this.updateOtherBuffers() + + line.name = this.c_id + point.name = this.c_id + + } else { + + [groupLines.splice(groupLines.length - 1), + groupPts.splice(groupLines.length - 1)].forEach( + e => { + e.geometry.dispose() + e.material.dispose() + } + ) + sc.render() + } + + return +} + + +export function _onMoveDimension(_p1, _p2, point, line) { + + const p1 = new THREE.Vector2(..._p1.geometry.attributes.position.array.slice(0, 2)) + const p2 = new THREE.Vector2(..._p2.geometry.attributes.position.array.slice(0, 2)) + const p3 = new THREE.Vector2() let dir, hyp, proj, perp, p1e, p2e, loc; - const p1 = new THREE.Vector2(...pts[0].geometry.attributes.position.array.slice(0, 2)) - const p2 = new THREE.Vector2(...pts[1].geometry.attributes.position.array.slice(0, 2)) - const p3 = new THREE.Vector2() - - const onMove = (e) => { - - - + return (e) => { loc = this.getLocation(e) p3.set(loc.x, loc.y) @@ -60,7 +108,7 @@ export async function drawDimension() { p1e = p1.clone().add(perp).toArray() p2e = p2.clone().add(perp).toArray() - const linegeom = line.geometry.attributes.position.array + const linegeom = line.geometry.attributes.position.array linegeom.set(p1.toArray(), 0) linegeom.set(p1e, 3) @@ -79,54 +127,4 @@ export async function drawDimension() { point.geometry.attributes.position.needsUpdate = true; sc.render() } - - let onEnd, onKey; - - let add = await new Promise((res) => { - onEnd = (e) => { - res(true) - this.updateOtherBuffers() - } - onKey = (e) => { - if (e.key == 'Escape') res(false) - } - this.canvas.addEventListener('pointermove', onMove) - this.canvas.addEventListener('pointerdown', onEnd) - window.addEventListener('keydown', onKey) - }) - - this.canvas.removeEventListener('pointermove', onMove) - this.canvas.removeEventListener('pointerdown', onEnd) - this.canvas.removeEventListener('keydown', onKey) - - if (add) { - this.constraints.set(++this.c_id, //??? - [ - 'pt_pt_distance', 10, - [pts[0].name, pts[1].name, -1, -1] - ] - ) - pts[0].userData.constraints.push(this.c_id) - pts[1].userData.constraints.push(this.c_id) - - this.updateOtherBuffers() - - line.name = this.c_id - point.name = this.c_id - - } else { - - group.children.splice(group.children.length - 2).forEach( - e => { - e.geometry.dispose() - e.material.dispose() - } - ) - sc.render() - } - - - - return -} - +} \ No newline at end of file diff --git a/src/utils/shared.js b/src/utils/shared.js index 0b4bdd9..70bf3c2 100644 --- a/src/utils/shared.js +++ b/src/utils/shared.js @@ -58,7 +58,6 @@ const lineObj = (n = 1) => { async function awaitPts(n) { let references = this.selected.slice() - let end = false; while (references.length < n && !end) { let pt; @@ -68,16 +67,13 @@ async function awaitPts(n) { onKey = (e) => e.key == 'Escape' && rej() onEnd = (e) => res(this.hovered[0]) - this.canvas.addEventListener('pointerdown', onEnd) - window.addEventListener('keydown', onKey) + this.canvas.addEventListener('pointerdown', onEnd,{ once: true }) + window.addEventListener('keydown', onKey,{ once: true }) }) if (pt.name[0] == 'p') { references.push(pt) - } else if (pt.name[0] == 'd') { - references = [pt] - end = true; - } + } } catch (e) { end = true;