master
howard 2021-04-03 21:54:27 -07:00
parent 1d450bab1c
commit 9f19ed9fde
5 changed files with 92 additions and 97 deletions

View File

@ -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 })
}

View File

@ -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 };

View File

@ -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;

View File

@ -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)
@ -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
}

View File

@ -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,15 +67,12 @@ 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) {