three.cad/src/sketcher/drawDimension.js

129 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-04-03 03:33:09 +08:00
import * as THREE from '../../node_modules/three/src/Three';
2021-04-03 12:05:28 +08:00
const lineMaterial = new THREE.LineBasicMaterial({
linewidth: 2,
2021-04-04 08:59:14 +08:00
color: 0x156289,
2021-04-03 12:05:28 +08:00
})
const pointMaterial = new THREE.PointsMaterial({
2021-04-04 08:59:14 +08:00
color: 0x156289,
2021-04-03 12:05:28 +08:00
size: 4,
})
2021-04-04 08:59:14 +08:00
export async function drawDimension() {
let pts;
// try {
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
// pts = await this.awaitPts(2)
// } catch {
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
// }
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
pts = await this.awaitPts(2)
console.log('here', pts)
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
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()
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
const lineGeom = new THREE.Float32BufferAttribute(3 * 8, 3)
const line = new THREE.LineSegments(
new THREE.BufferGeometry().setAttribute('position',
lineGeom
),
lineMaterial.clone()
);
const ptGeom = new THREE.Float32BufferAttribute(3, 3)
const point = new THREE.Points(
new THREE.BufferGeometry().setAttribute('position',
ptGeom
),
pointMaterial.clone()
2021-04-03 03:33:09 +08:00
)
2021-04-04 08:59:14 +08:00
this.obj3d.children[0].add(line)
this.obj3d.children[0].add(point)
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
let dir, hyp, proj, perp, p1e, p2e, loc;
const onMove = (e) => {
loc = this.getLocation(e)
p3.set(loc.x, loc.y)
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
dir = p2.clone().sub(p1).normalize()
hyp = p3.clone().sub(p1)
proj = dir.multiplyScalar(hyp.dot(dir))
perp = hyp.sub(proj)
2021-04-03 12:05:28 +08:00
2021-04-04 08:59:14 +08:00
p1e = p1.clone().add(perp).toArray()
p2e = p2.clone().add(perp).toArray()
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
lineGeom.set(p1.toArray(), 0)
lineGeom.set(p1e, 3)
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
lineGeom.set(p1e, 6)
lineGeom.set(p2e, 9)
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
lineGeom.set(p2e, 12)
lineGeom.set(p2.toArray(), 15)
2021-04-03 12:05:28 +08:00
2021-04-04 08:59:14 +08:00
lineGeom.set(p1e, 18)
lineGeom.set(p3.toArray(), 21)
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
ptGeom.set(p3.toArray())
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
line.geometry.attributes.position.needsUpdate = true;
point.geometry.attributes.position.needsUpdate = true;
sc.render()
2021-04-03 03:33:09 +08:00
}
2021-04-03 12:05:28 +08:00
2021-04-04 08:59:14 +08:00
let onEnd, onKey;
2021-04-03 12:05:28 +08:00
2021-04-04 08:59:14 +08:00
let ret = await new Promise((res, rej) => {
2021-04-03 12:05:28 +08:00
2021-04-04 08:59:14 +08:00
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)
})
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
console.log(ret, 'here')
this.canvas.removeEventListener('pointermove', onMove)
this.canvas.removeEventListener('pointerdown', onEnd)
this.canvas.removeEventListener('keydown', onKey)
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
// this.constraints.set(++this.c_id, //???
// [
// 'pt_pt_distance', 10,
// [_p1.name, _p2.name, -1, -1]
// ]
// )
// _p1.userData.constraints.push(this.c_id)
// _p2.userData.constraints.push(this.c_id)
2021-04-03 03:33:09 +08:00
2021-04-03 12:05:28 +08:00
return
2021-04-03 03:33:09 +08:00
}