three.cad/src/sketcher/pickEvents.js

102 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-03-25 16:04:13 +08:00
import * as THREE from 'three/src/Three'
export function onHover(e) {
if (this.mode || e.buttons) return
2021-03-26 12:30:35 +08:00
if (this.hovered.length) {
for (let ob of this.hovered) {
if (ob && !this.selected.has(ob)) {
ob.material.color.set(0x555555)
}
}
this.hovered = []
2021-03-26 13:38:49 +08:00
// this.dispatchEvent({ type: 'change' })
2021-03-25 16:04:13 +08:00
}
this.raycaster.setFromCamera(
new THREE.Vector2(
(e.clientX / window.innerWidth) * 2 - 1,
- (e.clientY / window.innerHeight) * 2 + 1
),
this.camera
);
const hoverPts = this.raycaster.intersectObjects(this.children)
2021-03-26 12:30:35 +08:00
// console.log(hoverPts)
2021-03-25 16:04:13 +08:00
if (hoverPts.length) {
let minDist = Infinity;
2021-03-26 12:30:35 +08:00
let idx = []
2021-03-25 16:04:13 +08:00
for (let i = 0; i < hoverPts.length; i++) {
2021-03-26 12:30:35 +08:00
if (!hoverPts[i].distanceToRay) continue;
if (hoverPts[i].distanceToRay < minDist) {
2021-03-25 16:04:13 +08:00
minDist = hoverPts[i].distanceToRay
2021-03-26 12:30:35 +08:00
idx = [i]
} else if (hoverPts[i].distanceToRay == minDist) {
idx.push(i)
2021-03-25 16:04:13 +08:00
}
}
2021-03-26 12:30:35 +08:00
// if (!idx.length) idx.push(0)
2021-03-25 16:04:13 +08:00
2021-03-26 12:30:35 +08:00
for (let i of idx) {
hoverPts[i].object.material.color.set(0x00ff00)
// hoverPts[i].object.material.color.set(0xff0000)
this.hovered.push(hoverPts[i].object)
}
2021-03-25 16:04:13 +08:00
this.dispatchEvent({ type: 'change' })
2021-03-26 12:30:35 +08:00
return
2021-03-25 16:04:13 +08:00
}
}
export function onPick(e) {
if (this.mode || e.buttons != 1) return
2021-03-26 12:30:35 +08:00
if (this.hovered.length) {
for (let h of this.hovered) {
this.selected.add(h)
}
if (this.hovered[0].type == "Points") {
2021-03-25 16:04:13 +08:00
this.domElement.addEventListener('pointermove', this.onDrag);
this.domElement.addEventListener('pointerup', this.onRelease)
}
} else {
for (let obj of this.selected) {
obj.material.color.set(0x555555)
}
this.dispatchEvent({ type: 'change' })
this.selected.clear()
}
}
export function onDrag(e) {
const mouseLoc = this.getLocation(e);
2021-03-26 12:30:35 +08:00
for (let h of this.hovered) {
this.ptsBuf.set(
mouseLoc,
this.objIdx.get(h.id) * 3
)
}
2021-03-25 16:04:13 +08:00
this.solve()
this.dispatchEvent({ type: 'change' })
}
export function onRelease() {
this.domElement.removeEventListener('pointermove', this.onDrag)
this.domElement.removeEventListener('pointerup', this.onRelease)
2021-03-26 12:30:35 +08:00
for (let ii of this.hovered) {
ii.geometry.computeBoundingSphere()
}
2021-03-25 16:04:13 +08:00
}