-
Notifications
You must be signed in to change notification settings - Fork 1
/
HandManager.js
145 lines (117 loc) · 3.69 KB
/
HandManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { Events } from "./utils/constants";
import { gameManager } from "./GameManager";
import { landmarkStore } from "./LandmarkStore";
import * as THREE from "three";
import { unitsManager } from "./UnitsManager";
class HandManager {
constructor() {
this.landmarks = [];
this.arrowGroup = new THREE.Group();
this.rayGroup = [];
this.geometry = new THREE.SphereGeometry(0.15)
this.material = new THREE.MeshNormalMaterial()
this.handMesh = new THREE.InstancedMesh(this.geometry, this.material, 21);
}
getPositionAt(index) {
const matrix = new THREE.Matrix4();
this.handMesh.getMatrixAt(index, matrix);
return new THREE.Vector3().setFromMatrixPosition(matrix);
}
get indexFingertip() {
const indexPosition = this.getPositionAt(9);
return indexPosition;
}
get raysAdded() {
return this.rayGroup.length === 21;
}
_updateLandsMarks() {
const handle = () => {
this.landmarks = landmarkStore.landmarks;
};
window.addEventListener(Events.LandmarksUpdate, handle.bind(this));
}
_handleRaycasters() {
if (!this.raysAdded) {
for (let index = 0; index < this.landmarks.length; index++) {
const raycaster = new THREE.Raycaster();
const position = this.getPositionAt(index);
raycaster.set(position, new THREE.Vector3(0, 0, -1).normalize());
this.rayGroup.push(raycaster);
}
} else {
for (let index = 0; index < this.rayGroup.length; index++) {
const position = this.getPositionAt(index);
this.rayGroup[index].set(
position,
new THREE.Vector3(0, 0, -1).normalize()
);
}
}
}
_handleArrows() {
if (this.debugMode === false) {
this.arrowGroup.clear();
return;
}
if (this.raysAdded === false) {
return;
}
this.arrowGroup.clear();
this.rayGroup.forEach((raycaster) => {
const arrow = new THREE.ArrowHelper(
raycaster.ray.direction,
raycaster.ray.origin,
300,
0xff0000
);
this.arrowGroup.add(arrow);
});
}
_handleCollisions() {
if (!this.raysAdded) return;
this.rayGroup.forEach((ray) => {
const intersects = ray.intersectObjects(
unitsManager.activeObjects.children
);
intersects.forEach((intersection) => {
if (intersection.distance <= 4) {
window.dispatchEvent(
new CustomEvent(Events.HandCollision, {
detail: intersection.object,
})
);
}
});
});
}
_placeCubesByLandmark(camera) {
for (let index = 0; index < this.landmarks.length; index++) {
const currLndmrk = this.landmarks[index];
const vector = new THREE.Vector3(
currLndmrk.x * 2 - 1,
-currLndmrk.y * 2 + 1,
currLndmrk.z
).unproject(camera);
const dir = vector.sub(camera.position).normalize();
dir.setX(-dir.x);
const distance = -camera.position.z / dir.z; //<- this maps hands z coordinate to cameras z coordinate
const pos = camera.position.clone().add(dir.multiplyScalar(distance));
const quaternion = new THREE.Quaternion();
quaternion.setFromEuler(new THREE.Euler());
const matrix = new THREE.Matrix4();
matrix.makeRotationFromQuaternion(quaternion);
matrix.setPosition(pos);
this.handMesh.setMatrixAt(index, matrix);
this.handMesh.instanceMatrix.needsUpdate = true;
}
}
render(camera, debugMode) {
this.debugMode = debugMode;
if (!gameManager.isPlaying) return;
this._placeCubesByLandmark(camera);
this._handleCollisions();
this._handleRaycasters();
this._handleArrows();
}
}
export const handManager = new HandManager();