-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
327 lines (283 loc) · 8.58 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import * as THREE from "./res/js/three.module.js";
import { DRACOLoader } from "./res/js/DRACOLoader.js";
import { GLTFLoader } from "./res/js/GLTFLoader.js";
const videoElement = document.getElementsByClassName("input_video")[0];
const canvasElement = document.getElementsByClassName("output_canvas")[0];
const btnRun = document.getElementById("run");
const handMesh = new Hands({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
},
});
handMesh.setOptions({
maxNumHands: 1,
modelComplexity: 1,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5,
});
let ring;
function initThreeApp(canvas, w, h) {
const renderer = new THREE.WebGLRenderer({
canvas,
alpha: false,
antialias: true,
});
const fov = 75;
const near = 0.01;
const far = 1000;
const camera = new THREE.PerspectiveCamera(fov, 800 / 720, near, far);
camera.position.z = 2;
const scene = new THREE.Scene();
function resize() {
const width = w || window.innerWidth;
const height = h || window.innerHeight;
renderer.setSize(width, height);
renderer.setPixelRatio(window.pixelRatio);
if (camera.isPerspectiveCamera) {
camera.aspect = width / height;
}
camera.updateProjectionMatrix();
}
function render() {
renderer.render(scene, camera);
}
// initial resize and render
resize();
render();
// add a light
const color = 0xfefefe;
const intensity = 4;
const dirLight = new THREE.DirectionalLight(color, intensity);
dirLight.position.set(-1, 2, 4);
scene.add(dirLight);
const spotLight = new THREE.SpotLight(color, 2);
spotLight.position.copy(camera.position);
scene.add(spotLight);
spotLight.onAfterRender(() => {
spotLight.lookAt(ring.position);
})
// add a box
const boxWidth = 0.2;
const boxHeight = 0.2;
const boxDepth = 0.2;
const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
const material = new THREE.MeshPhongMaterial({
color: 0x44aa88,
//transparent: true,
//opacity: 0.8,
});
const cube = new THREE.Mesh(geometry, material);
//scene.add(cube);
const plane = new THREE.Mesh(
new THREE.PlaneBufferGeometry(3.1, 3.1),
new THREE.MeshBasicMaterial({
color: 0xffffff,
//opacity: 0.95,
//transparent: true,
map: new THREE.VideoTexture(videoElement),
})
);
scene.add(plane);
const textureLoader = new THREE.TextureLoader();
const textureEquirec = textureLoader.load( './res/textures/envMap.jpeg' );
textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
textureEquirec.encoding = THREE.sRGBEncoding;
scene.background = textureEquirec;
scene.environment = textureEquirec;
const gltfLoader = new GLTFLoader();
// Optional: Provide a DRACOLoader instance to decode compressed mesh data
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath("./res/js/draco/");
gltfLoader.setDRACOLoader(dracoLoader);
// Load a glTF resource
gltfLoader.load(
// resource URL
"./res/models/Pomellato.glb",
// called when the resource is loaded
(gltf) => {
ring = gltf.scene;
ring.scale.set(4, 4, 4);
ring.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
child.material.side = THREE.DoubleSide;
child.material.needsUpdate = true;
child.position.z = -0.8;
child.updateMatrixWorld();
if(child?.userData?.name === "pomellatoNudo_gem49"){
//child.material.map = null;
child.material.transmission = 0.8;
child.material.sheen = 0.5;
child.material.needsUpdate = true;
}
console.log(child);
}
});
ring.visible = false;
scene.add(ring);
gltf.animations; // Array<THREE.AnimationClip>
gltf.scene; // THREE.Group
gltf.scenes; // Array<THREE.Group>
gltf.cameras; // Array<THREE.Camera>
gltf.asset; // Object
},
// called while loading is progressing
function (xhr) {
console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
},
// called when loading has errors
function (error) {
console.log("An error happened");
}
);
return {
renderer,
camera,
scene,
resize,
render,
cube,
};
}
initVideo(videoElement, 800, 800);
const threeApp = initThreeApp(canvasElement, 800, 800);
const onResults = function (res) {
btnRun.style.display = "none";
const landmarks = res.multiHandLandmarks;
if (landmarks?.length > 0) {
//console.log(res);
//console.log(landmarks);
}
if (
!ring ||
!landmarks ||
!landmarks[0] ||
!landmarks[0][14] ||
!landmarks[0][13] ||
!landmarks[0][0] ||
!landmarks[0][9]
){
ring.visible = false;
return;
}
ring.visible = true;
//const { x, y, z } = landmarks[0][13];
//console.log(landmarks[0][14]);
const wrist = new THREE.Vector3(
landmarks[0][0].x,
landmarks[0][0].y,
landmarks[0][0].z
);
const pointA = new THREE.Vector3(
landmarks[0][13].x,
landmarks[0][13].y,
landmarks[0][13].z
);
const pointB = new THREE.Vector3(
landmarks[0][14].x,
landmarks[0][14].y,
landmarks[0][14].z
);
const pointN = new THREE.Vector3(
landmarks[0][9].x,
landmarks[0][9].y,
landmarks[0][9].z
);
const percentage = 0.65;
var dir = pointB.clone().sub(pointA);
var length = dir.length();
dir = dir.normalize().multiplyScalar(length * percentage);
const point = pointA.clone().add(dir);
const rotateZ = Math.atan(
(landmarks[0][14].y - landmarks[0][13].y) /
(landmarks[0][14].x - landmarks[0][13].x)
);
const rotateX = Math.atan(
(landmarks[0][14].z - landmarks[0][0].z) /
(landmarks[0][14].y - landmarks[0][0].y)
);
const rotateY = Math.atan(
(landmarks[0][9].z - landmarks[0][13].z) /
(landmarks[0][9].x - landmarks[0][13].x)
);
const scale_x = landmarks[0][13].x - landmarks[0][14].x;
const scale_y = landmarks[0][13].y - landmarks[0][14].y;
const scale_z = landmarks[0][13].z - landmarks[0][14].z;
//calculate the distance between landmarks[13] and [14]
const scale = Math.sqrt(
(landmarks[0][14].x - landmarks[0][13].x) ** 2 +
(landmarks[0][14].y - landmarks[0][13].y) ** 2 +
(landmarks[0][14].z - landmarks[0][13].z) ** 2
);
// landmarks[0][1] == nose position(face center point)
// use landmarks xy value to calculate the screen xy
let vec = new THREE.Vector3();
let pos = new THREE.Vector3();
vec.set(point.x * 2 - 1, -point.y * 2 + 1, point.z);
vec.unproject(threeApp.camera);
vec.sub(threeApp.camera.position).normalize();
let distance = -threeApp.camera.position.z / vec.z;
pos.copy(threeApp.camera.position).add(vec.multiplyScalar(distance));
threeApp.cube.position.x = pos.x;
threeApp.cube.position.y = pos.y;
ring.position.x = pos.x;
ring.position.y = pos.y;
//ring.position.z = pos.z - 0.1;
//ring.scale.x = scale*45;
//ring.scale.y = scale*45;
//ring.scale.z = scale*45;
ring.scale.x = scale * 1.5;
ring.scale.y = scale * 1.5;
ring.scale.z = scale * 1.5;
const hand_info = res.multiHandedness[0].label;
// ring.rotation.x = rotateX + Math.PI / 2;
ring.rotation.z = -rotateZ + Math.PI / 2;
if (hand_info === "Left") {
if (rotateY < 0) {
ring.rotation.y = rotateY + Math.PI / 16;
} else {
ring.rotation.y = rotateY + Math.PI + Math.PI / 16;
}
} else {
if (rotateY > 0) {
ring.rotation.y = rotateY - Math.PI / 16;
} else {
ring.rotation.y = rotateY + Math.PI - Math.PI / 16;
}
}
// ring.lookAt(pointA);
// todo
// got the cube xy then how to get the z value?
};
handMesh.onResults(onResults);
const run = async function () {
threeApp.render();
await handMesh.send({ image: videoElement });
requestAnimationFrame(run);
//ring.rotation.z += 0.1;
};
btnRun.addEventListener("click", (evt) => {
console.log(evt);
evt.target.innerText = 'WAITING...';
run();
});
function initVideo(video, w, h) {
const width = w || window.innerWidth;
const height = h || window.innerHeight;
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
const constraints = { video: { width, height, facingMode: "environment" } };
navigator.mediaDevices
.getUserMedia(constraints)
.then(function (stream) {
// apply the stream to the video element used in the texture
video.srcObject = stream;
video.play();
})
.catch(function (error) {
console.error("Unable to access the camera/webcam.", error);
});
} else {
console.error("MediaDevices interface not available.");
}
}