2021-04-07 03:46:16 +08:00
|
|
|
import * as THREE from '../node_modules/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();
|
2021-04-08 12:05:54 +08:00
|
|
|
raycaster.params.Line.threshold = 0.1;
|
|
|
|
raycaster.params.Points.threshold = 0.1;
|
2021-03-31 07:20:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
const color = {
|
2021-04-07 02:31:14 +08:00
|
|
|
background:0xbbbbbb,
|
2021-03-31 16:07:34 +08:00
|
|
|
lighting: 0xFFFFFF,
|
|
|
|
emissive: 0x072534,
|
2021-04-07 02:31:14 +08:00
|
|
|
|
|
|
|
hover: 0x00ff00,
|
2021-04-05 11:52:17 +08:00
|
|
|
point: 0x555555, //points
|
|
|
|
line: 0x555555, //lines
|
|
|
|
mesh: 0x156289, //mesh:
|
|
|
|
dimension: 0x891d15, //
|
2021-04-07 02:31:14 +08:00
|
|
|
|
|
|
|
plane: 0xdaacac, //
|
|
|
|
planeBorder: 0xc59797, //
|
2021-03-31 07:20:24 +08:00
|
|
|
}
|
|
|
|
|
2021-04-07 02:31:14 +08:00
|
|
|
const hoverColor = {
|
|
|
|
hover: 0x00ff00,
|
|
|
|
point: 0x00ff00, //points
|
|
|
|
line: 0x00ff00, //lines
|
|
|
|
mesh: 0x00ff00, //mesh:
|
|
|
|
dimension: 0x00ff00, //
|
|
|
|
plane: 0xff0000, //
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-29 18:27:34 +08:00
|
|
|
const lineMaterial = new THREE.LineBasicMaterial({
|
2021-04-08 15:33:18 +08:00
|
|
|
linewidth: 1,
|
2021-04-05 11:52:17 +08:00
|
|
|
color: color.line,
|
2021-03-29 18:27:34 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const pointMaterial = new THREE.PointsMaterial({
|
2021-04-05 11:52:17 +08:00
|
|
|
color: color.point,
|
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-07 05:10:07 +08:00
|
|
|
ret.name = "p" + id++
|
2021-04-05 11:52:17 +08:00
|
|
|
ret.userData.type = 'point'
|
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-07 05:10:07 +08:00
|
|
|
ret.name = 'l' + id++
|
2021-04-05 11:52:17 +08:00
|
|
|
ret.userData.type = 'line'
|
2021-03-31 16:07:34 +08:00
|
|
|
return ret
|
|
|
|
}
|
2021-03-29 18:27:34 +08:00
|
|
|
|
|
|
|
|
2021-04-04 17:36:41 +08:00
|
|
|
async function awaitPts(...criteria) {
|
|
|
|
|
|
|
|
function fullfilled() {
|
|
|
|
for (let i = criteria.length - 1; i >= 0; i--) {
|
|
|
|
const crit = criteria[i]
|
|
|
|
let nfilled = 0;
|
|
|
|
for (let k in counter) {
|
|
|
|
if (!crit[k] || counter[k] > crit[k]) {
|
|
|
|
criteria.splice(i, 1)
|
|
|
|
break;
|
|
|
|
} else if (counter[k] == crit[k]) {
|
|
|
|
nfilled += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nfilled == Object.keys(crit).length) return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const counter = {}
|
|
|
|
|
2021-04-03 03:33:09 +08:00
|
|
|
let references = this.selected.slice()
|
2021-04-04 17:36:41 +08:00
|
|
|
|
|
|
|
for (let ob of references) {
|
2021-04-05 11:52:17 +08:00
|
|
|
const type = ob.userData.type
|
2021-04-04 17:36:41 +08:00
|
|
|
if (counter[type]) {
|
|
|
|
counter[type] += 1;
|
|
|
|
} else {
|
|
|
|
counter[type] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fullfilled()) return references
|
|
|
|
|
2021-04-04 08:59:14 +08:00
|
|
|
let end = false;
|
2021-04-04 17:36:41 +08:00
|
|
|
while (criteria.length && !end) {
|
2021-04-04 08:59:14 +08:00
|
|
|
let pt;
|
|
|
|
let onEnd, onKey;
|
|
|
|
try {
|
2021-04-04 17:36:41 +08:00
|
|
|
|
2021-04-04 08:59:14 +08:00
|
|
|
pt = await new Promise((res, rej) => {
|
2021-04-04 10:08:19 +08:00
|
|
|
onKey = (e) => e.key == 'Escape' && rej()
|
2021-04-04 17:36:41 +08:00
|
|
|
onEnd = (e) => this.hovered.length && res(this.hovered[0])
|
|
|
|
this.canvas.addEventListener('pointerdown', onEnd)
|
|
|
|
window.addEventListener('keydown', onKey)
|
2021-04-04 08:59:14 +08:00
|
|
|
})
|
|
|
|
|
2021-04-04 17:36:41 +08:00
|
|
|
references.push(pt)
|
2021-04-05 11:52:17 +08:00
|
|
|
const type = pt.userData.type
|
2021-04-04 17:36:41 +08:00
|
|
|
if (counter[type]) {
|
|
|
|
counter[type] += 1;
|
|
|
|
} else {
|
|
|
|
counter[type] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fullfilled()) return references
|
2021-04-04 08:59:14 +08:00
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
end = true;
|
2021-04-03 03:33:09 +08:00
|
|
|
}
|
2021-04-04 08:59:14 +08:00
|
|
|
|
|
|
|
this.canvas.removeEventListener('pointerdown', onEnd)
|
2021-04-04 10:08:19 +08:00
|
|
|
window.removeEventListener('keydown', onKey)
|
2021-04-03 03:33:09 +08:00
|
|
|
}
|
2021-04-05 11:52:17 +08:00
|
|
|
|
2021-04-04 17:36:41 +08:00
|
|
|
console.log('fail')
|
|
|
|
return null
|
2021-04-03 03:33:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-07 02:31:14 +08:00
|
|
|
export { lineMaterial, pointMaterial, _vec2, _vec3, raycaster, color, hoverColor, ptObj, lineObj, awaitPts }
|