2021-04-03 03:33:09 +08:00
|
|
|
import * as THREE from 'three/src/Three';
|
2021-03-29 18:27:34 +08:00
|
|
|
|
2021-03-31 07:20:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
const _vec2 = new THREE.Vector2()
|
|
|
|
const _vec3 = new THREE.Vector3()
|
|
|
|
|
|
|
|
|
|
|
|
const raycaster = new THREE.Raycaster();
|
|
|
|
raycaster.params.Line.threshold = 0.8;
|
2021-03-31 16:07:34 +08:00
|
|
|
raycaster.params.Points.threshold = 0.6;
|
2021-03-31 07:20:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
const color = {
|
|
|
|
hover: 0x00ff00,
|
2021-03-31 16:07:34 +08:00
|
|
|
lighting: 0xFFFFFF,
|
|
|
|
emissive: 0x072534,
|
2021-04-01 06:03:35 +08:00
|
|
|
d: 0xf5bc42, //datums: planes
|
|
|
|
p: 0x555555, //points
|
|
|
|
l: 0x555555, //lines
|
|
|
|
m: 0x156289, //mesh: extrude
|
2021-03-31 07:20:24 +08:00
|
|
|
}
|
|
|
|
|
2021-03-29 18:27:34 +08:00
|
|
|
const lineMaterial = new THREE.LineBasicMaterial({
|
|
|
|
linewidth: 2,
|
2021-04-01 06:03:35 +08:00
|
|
|
color: color.l,
|
2021-03-29 18:27:34 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const pointMaterial = new THREE.PointsMaterial({
|
2021-04-01 06:03:35 +08:00
|
|
|
color: color.p,
|
2021-03-29 18:27:34 +08:00
|
|
|
size: 4,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2021-03-31 16:07:34 +08:00
|
|
|
const ptObj = (n) => {
|
|
|
|
const ret = new THREE.Points(
|
|
|
|
new THREE.BufferGeometry().setAttribute('position',
|
|
|
|
new THREE.Float32BufferAttribute(n || 3, 3)
|
|
|
|
),
|
|
|
|
pointMaterial.clone()
|
|
|
|
);
|
2021-04-01 06:03:35 +08:00
|
|
|
ret.name = 'p' + nid++
|
2021-03-31 16:07:34 +08:00
|
|
|
return ret
|
|
|
|
}
|
2021-03-29 18:27:34 +08:00
|
|
|
|
2021-03-31 16:07:34 +08:00
|
|
|
const lineObj = (n = 1) => {
|
|
|
|
const ret = new THREE.Line(
|
|
|
|
new THREE.BufferGeometry().setAttribute('position',
|
|
|
|
new THREE.Float32BufferAttribute(3 * (n + 1), 3)
|
|
|
|
),
|
|
|
|
lineMaterial.clone()
|
|
|
|
);
|
2021-04-01 06:03:35 +08:00
|
|
|
ret.name = 'l' + nid++
|
2021-03-31 16:07:34 +08:00
|
|
|
return ret
|
|
|
|
}
|
2021-03-29 18:27:34 +08:00
|
|
|
|
|
|
|
|
2021-04-03 03:33:09 +08:00
|
|
|
async function awaitPts(n) {
|
|
|
|
let references = this.selected.slice()
|
|
|
|
|
2021-04-04 08:59:14 +08:00
|
|
|
let end = false;
|
|
|
|
while (references.length < n && !end) {
|
|
|
|
let pt;
|
|
|
|
let onEnd, onKey;
|
|
|
|
try {
|
|
|
|
pt = await new Promise((res, rej) => {
|
|
|
|
onKey = (e) => {
|
|
|
|
if (e.key != 'Escape') return
|
|
|
|
console.log(e.key, 'key')
|
|
|
|
rej()
|
2021-04-03 03:33:09 +08:00
|
|
|
}
|
2021-04-04 08:59:14 +08:00
|
|
|
onEnd = (e) => {
|
|
|
|
res(this.hovered[0])
|
2021-04-03 03:33:09 +08:00
|
|
|
}
|
2021-04-04 08:59:14 +08:00
|
|
|
|
|
|
|
this.canvas.addEventListener('pointerdown', onEnd)
|
|
|
|
window.addEventListener('keydown', onKey)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (pt.name[0] == 'p') {
|
|
|
|
references.push(pt)
|
|
|
|
} else if (pt.name[0] == 'd') {
|
|
|
|
references = [pt]
|
|
|
|
end = true;
|
2021-04-03 03:33:09 +08:00
|
|
|
}
|
2021-04-04 08:59:14 +08:00
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
console.log('cancelled')
|
|
|
|
end = true;
|
2021-04-03 03:33:09 +08:00
|
|
|
}
|
2021-04-04 08:59:14 +08:00
|
|
|
|
|
|
|
window.removeEventListener('keydown', onKey)
|
|
|
|
this.canvas.removeEventListener('pointerdown', onEnd)
|
2021-04-03 03:33:09 +08:00
|
|
|
}
|
2021-04-04 08:59:14 +08:00
|
|
|
|
2021-04-03 03:33:09 +08:00
|
|
|
return references
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export { lineMaterial, pointMaterial, _vec2, _vec3, raycaster, color, ptObj, lineObj, awaitPts }
|