three.cad/src/sketcher/drawDimension.js

197 lines
4.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() {
2021-04-04 17:36:41 +08:00
let pts = await this.awaitPts({ p: 2 })
2021-04-03 03:33:09 +08:00
2021-04-04 17:36:41 +08:00
if (pts == null) return;
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
const line = new THREE.LineSegments(
new THREE.BufferGeometry().setAttribute('position',
2021-04-04 10:08:19 +08:00
new THREE.Float32BufferAttribute(3 * 8, 3)
2021-04-04 08:59:14 +08:00
),
lineMaterial.clone()
);
const point = new THREE.Points(
new THREE.BufferGeometry().setAttribute('position',
2021-04-04 10:08:19 +08:00
new THREE.Float32BufferAttribute(3, 3)
2021-04-04 08:59:14 +08:00
),
pointMaterial.clone()
2021-04-03 03:33:09 +08:00
)
2021-04-04 17:36:41 +08:00
line.userData.nids = pts.map(e => e.name)
2021-04-04 10:08:19 +08:00
2021-04-04 12:54:27 +08:00
const groupLines = this.obj3d.children[1]
const groupPts = this.obj3d.children[2]
groupLines.add(line)
groupPts.add(point)
2021-04-04 10:08:19 +08:00
2021-04-04 12:54:27 +08:00
const onMove = this._onMoveDimension(...pts, point, line)
2021-04-03 03:33:09 +08:00
2021-04-04 08:59:14 +08:00
let onEnd, onKey;
2021-04-03 12:05:28 +08:00
2021-04-04 10:08:19 +08:00
let add = await new Promise((res) => {
2021-04-04 12:54:27 +08:00
onEnd = (e) => res(true)
onKey = (e) => e.key == 'Escape' && res(false)
2021-04-04 08:59:14 +08:00
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
this.canvas.removeEventListener('pointermove', onMove)
this.canvas.removeEventListener('pointerdown', onEnd)
2021-04-04 12:54:27 +08:00
window.removeEventListener('keydown', onKey)
2021-04-03 03:33:09 +08:00
2021-04-04 10:08:19 +08:00
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)
2021-04-04 12:54:27 +08:00
2021-04-04 10:08:19 +08:00
this.updateOtherBuffers()
line.name = this.c_id
point.name = this.c_id
} else {
2021-04-04 12:54:27 +08:00
[groupLines.splice(groupLines.length - 1),
groupPts.splice(groupLines.length - 1)].forEach(
2021-04-04 10:08:19 +08:00
e => {
e.geometry.dispose()
e.material.dispose()
}
)
sc.render()
}
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
}
2021-04-04 12:54:27 +08:00
export function _onMoveDimension(_p1, _p2, point, line) {
2021-04-04 17:36:41 +08:00
p1.set(..._p1.geometry.attributes.position.array.slice(0, 2))
p2.set(..._p2.geometry.attributes.position.array.slice(0, 2))
2021-04-04 12:54:27 +08:00
let dir, hyp, proj, perp, p1e, p2e, loc;
return (e) => {
loc = this.getLocation(e)
2021-04-04 17:36:41 +08:00
2021-04-04 12:54:27 +08:00
p3.set(loc.x, loc.y)
2021-04-04 17:36:41 +08:00
const linegeom = line.geometry.attributes.position
const pointgeom = point.geometry.attributes.position
2021-04-04 12:54:27 +08:00
dir = p2.clone().sub(p1).normalize()
hyp = p3.clone().sub(p1)
proj = dir.multiplyScalar(hyp.dot(dir))
2021-04-04 17:36:41 +08:00
perp = hyp.clone().sub(proj)
2021-04-04 12:54:27 +08:00
p1e = p1.clone().add(perp).toArray()
p2e = p2.clone().add(perp).toArray()
2021-04-04 17:36:41 +08:00
linegeom.array.set(p1.toArray(), 0)
linegeom.array.set(p1e, 3)
2021-04-04 12:54:27 +08:00
2021-04-04 17:36:41 +08:00
linegeom.array.set(p1e, 6)
linegeom.array.set(p2e, 9)
2021-04-04 12:54:27 +08:00
2021-04-04 17:36:41 +08:00
linegeom.array.set(p2e, 12)
linegeom.array.set(p2.toArray(), 15)
2021-04-04 12:54:27 +08:00
2021-04-04 17:36:41 +08:00
linegeom.array.set(p1e, 18)
linegeom.array.set(p3.toArray(), 21)
2021-04-04 12:54:27 +08:00
2021-04-04 17:36:41 +08:00
linegeom.needsUpdate = true;
pointgeom.array.set(p3.toArray())
pointgeom.needsUpdate = true;
point.userData.offset = hyp.toArray()
2021-04-04 12:54:27 +08:00
sc.render()
}
2021-04-04 17:36:41 +08:00
}
const p1 = new THREE.Vector2()
const p2 = new THREE.Vector2()
const p3 = new THREE.Vector2()
export function updateDimLines() {
let dir, hyp, proj, perp, p1e, p2e;
let nids, _p1, _p2, _p3, offset;
const groupLines = this.obj3d.children[1].children
const groupPts = this.obj3d.children[2].children
for (let i = 0; i < groupLines.length; i++) {
nids = groupLines[i].userData.nids
// console.log(sketcher.objIdx.get(nid[0]), 'heeeeeeee')
_p1 = this.obj3d.children[sketcher.objIdx.get(nids[0])].geometry.attributes.position.array
_p2 = this.obj3d.children[sketcher.objIdx.get(nids[1])].geometry.attributes.position.array
_p3 = groupPts[i].geometry.attributes.position.array
offset = groupPts[i].userData.offset
p1.set(_p1[0], _p1[1])
p2.set(_p2[0], _p2[1])
p3.set(_p1[0] + offset[0], _p1[1] + offset[1])
const linegeom = groupLines[i].geometry.attributes.position
const pointgeom = groupPts[i].geometry.attributes.position
dir = p2.clone().sub(p1).normalize()
hyp = p3.clone().sub(p1)
proj = dir.multiplyScalar(hyp.dot(dir))
perp = hyp.sub(proj)
p1e = p1.clone().add(perp).toArray()
p2e = p2.clone().add(perp).toArray()
linegeom.array.set(p1.toArray(), 0)
linegeom.array.set(p1e, 3)
linegeom.array.set(p1e, 6)
linegeom.array.set(p2e, 9)
linegeom.array.set(p2e, 12)
linegeom.array.set(p2.toArray(), 15)
linegeom.array.set(p1e, 18)
linegeom.array.set(p3.toArray(), 21)
linegeom.needsUpdate = true;
pointgeom.array.set(p3.toArray())
pointgeom.needsUpdate = true;
}
}