-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
444 lines (372 loc) · 16 KB
/
index.html
File metadata and controls
444 lines (372 loc) · 16 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Gesture Controlled 3D Particles</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #000;
font-family: 'Courier New', Courier, monospace;
}
#canvas-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
#video-element {
position: absolute;
top: 0;
left: 0;
opacity: 0;
pointer-events: none;
z-index: 0;
}
#ui {
position: absolute;
top: 20px;
left: 20px;
color: #00ffcc;
z-index: 10;
background: rgba(0, 0, 0, 0.6);
padding: 15px;
border: 1px solid #00ffcc;
border-radius: 8px;
pointer-events: none;
}
.status-dot {
height: 10px;
width: 10px;
background-color: #ff0000;
border-radius: 50%;
display: inline-block;
margin-right: 5px;
}
.active {
background-color: #00ff00;
box-shadow: 0 0 10px #00ff00;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/control_utils/control_utils.js"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js" crossorigin="anonymous"></script>
</head>
<body>
<div id="ui">
<h3>Gesture Control System</h3>
<div><span id="cam-status" class="status-dot"></span> Camera: <span id="cam-text">Initializing...</span></div>
<p>🖐 <b>Open Hand:</b> Rotate & Color</p>
<p>🤏 <b>Pinch:</b> Collapse/Expand</p>
<p>✊ <b>Fist (Hold):</b> Switch Shape</p>
<p>Current Shape: <b id="shape-name">SPHERE</b></p>
</div>
<video id="video-element"></video>
<div id="canvas-container"></div>
<script>
/**
* CONFIGURATION & GLOBALS
*/
const PARTICLE_COUNT = 15000;
const PARTICLE_SIZE = 0.08;
let scene, camera, renderer, particles, geometry, material;
let currentPositions = [];
let targetPositions = [];
let originalPositions = []; // To store the 'base' shape for morphing
let isFistDetected = false;
let fistHoldTimer = 0;
// Interaction State
const interaction = {
x: 0,
y: 0,
pinchDistance: 1.0, // 1.0 is normal, <1 is contraction
shapeIndex: 0
};
const SHAPES = ['SPHERE', 'HEART', 'SATURN', 'FLOWER', 'DNA'];
/**
* 1. THREE.JS SETUP
*/
function initThree() {
const container = document.getElementById('canvas-container');
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x000000, 0.03);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 10;
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
container.appendChild(renderer.domElement);
// Initialize Particles
geometry = new THREE.BufferGeometry();
const positions = new Float32Array(PARTICLE_COUNT * 3);
const colors = new Float32Array(PARTICLE_COUNT * 3);
// Initial shape: Sphere
const sphere = getSpherePositions();
for (let i = 0; i < PARTICLE_COUNT * 3; i++) {
positions[i] = sphere[i];
currentPositions[i] = sphere[i];
targetPositions[i] = sphere[i];
colors[i] = 1.0; // Start white
}
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));
// Shader Material for better glow performance
const sprite = new THREE.TextureLoader().load('https://threejs.org/examples/textures/sprites/disc.png');
material = new THREE.PointsMaterial({
size: PARTICLE_SIZE,
map: sprite,
vertexColors: true,
blending: THREE.AdditiveBlending,
depthTest: false,
transparent: true,
opacity: 0.8
});
particles = new THREE.Points(geometry, material);
scene.add(particles);
// Initial fill of shapes
switchShape(0);
animate();
}
/**
* 2. SHAPE GENERATORS (Maths)
*/
function getSpherePositions() {
const arr = new Float32Array(PARTICLE_COUNT * 3);
for (let i = 0; i < PARTICLE_COUNT; i++) {
const r = 4;
const theta = Math.random() * Math.PI * 2;
const phi = Math.acos((Math.random() * 2) - 1);
arr[i * 3] = r * Math.sin(phi) * Math.cos(theta);
arr[i * 3 + 1] = r * Math.sin(phi) * Math.sin(theta);
arr[i * 3 + 2] = r * Math.cos(phi);
}
return arr;
}
function getHeartPositions() {
const arr = new Float32Array(PARTICLE_COUNT * 3);
for (let i = 0; i < PARTICLE_COUNT; i++) {
// Parametric Heart 3D
let x = (Math.random() - 0.5) * 6;
let y = (Math.random() - 0.5) * 6;
let z = (Math.random() - 0.5) * 6;
// Rejection sampling to fill volume
let tries = 0;
while (tries < 10) {
const hx = Math.random() * 2 * Math.PI;
const hz = Math.random() * 2 - 1;
// Simplified surface distribution
// Using standard parametric equation for better visuals
const t = Math.random() * Math.PI * 2;
const u = Math.random() * Math.PI;
// Heart formula
x = 16 * Math.pow(Math.sin(t), 3);
y = 13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t);
z = 4 * Math.cos(u) * Math.sin(t); // add thickness
// Scale down
arr[i * 3] = x * 0.2;
arr[i * 3 + 1] = y * 0.2;
arr[i * 3 + 2] = z * 0.2;
tries++;
}
}
return arr;
}
function getSaturnPositions() {
const arr = new Float32Array(PARTICLE_COUNT * 3);
const ringCount = Math.floor(PARTICLE_COUNT * 0.6);
const planetCount = PARTICLE_COUNT - ringCount;
// Planet
for (let i = 0; i < planetCount; i++) {
const r = 2.5;
const theta = Math.random() * Math.PI * 2;
const phi = Math.acos((Math.random() * 2) - 1);
arr[i * 3] = r * Math.sin(phi) * Math.cos(theta);
arr[i * 3 + 1] = r * Math.sin(phi) * Math.sin(theta);
arr[i * 3 + 2] = r * Math.cos(phi);
}
// Rings
for (let i = planetCount; i < PARTICLE_COUNT; i++) {
const angle = Math.random() * Math.PI * 2;
const dist = 3.5 + Math.random() * 2.5;
arr[i * 3] = Math.cos(angle) * dist;
arr[i * 3 + 1] = (Math.random() - 0.5) * 0.2; // thin y
arr[i * 3 + 2] = Math.sin(angle) * dist;
// Tilt the ring
const x = arr[i * 3];
const y = arr[i * 3 + 1];
const tilt = 0.4;
arr[i * 3] = x * Math.cos(tilt) - y * Math.sin(tilt);
arr[i * 3 + 1] = x * Math.sin(tilt) + y * Math.cos(tilt);
}
return arr;
}
function getFlowerPositions() {
const arr = new Float32Array(PARTICLE_COUNT * 3);
for (let i = 0; i < PARTICLE_COUNT; i++) {
const u = Math.random() * Math.PI * 2;
const v = Math.random() * Math.PI;
// Rose/Flower polar mathematics
const k = 5; // petals
const r = 3 * Math.cos(k * u) + 1;
arr[i * 3] = r * Math.cos(u) * Math.sin(v);
arr[i * 3 + 1] = r * Math.sin(u) * Math.sin(v);
arr[i * 3 + 2] = r * Math.cos(v) * 0.5; // flatten z
}
return arr;
}
function getDNAPositions() {
const arr = new Float32Array(PARTICLE_COUNT * 3);
for (let i = 0; i < PARTICLE_COUNT; i++) {
const t = (i / PARTICLE_COUNT) * 20 * Math.PI;
const radius = 2;
const height = (i / PARTICLE_COUNT) * 10 - 5;
// Double Helix offset
const strand = i % 2 === 0 ? 0 : Math.PI;
arr[i * 3] = radius * Math.cos(t + strand);
arr[i * 3 + 1] = height;
arr[i * 3 + 2] = radius * Math.sin(t + strand);
}
return arr;
}
function switchShape(index) {
let positions;
const name = SHAPES[index % SHAPES.length];
document.getElementById('shape-name').innerText = name;
switch (name) {
case 'SPHERE': positions = getSpherePositions(); break;
case 'HEART': positions = getHeartPositions(); break;
case 'SATURN': positions = getSaturnPositions(); break;
case 'FLOWER': positions = getFlowerPositions(); break;
case 'DNA': positions = getDNAPositions(); break;
}
// Set target positions
for (let i = 0; i < positions.length; i++) {
targetPositions[i] = positions[i];
}
}
/**
* 3. MEDIAPIPE HANDS SETUP
*/
const videoElement = document.getElementById('video-element');
function onResults(results) {
document.getElementById('cam-status').classList.add('active');
document.getElementById('cam-text').innerText = "Active";
if (results.multiHandLandmarks && results.multiHandLandmarks.length > 0) {
const landmarks = results.multiHandLandmarks[0];
// 1. Position Tracking (Index Finger Tip - Landmark 8)
// Map 0-1 coord to -1 to 1 for 3D space
const indexTip = landmarks[8];
interaction.x = (indexTip.x - 0.5) * 2;
interaction.y = -(indexTip.y - 0.5) * 2; // Invert Y
// 2. Pinch Detection (Index Tip 8 to Thumb Tip 4)
const thumbTip = landmarks[4];
const distance = Math.sqrt(
Math.pow(indexTip.x - thumbTip.x, 2) +
Math.pow(indexTip.y - thumbTip.y, 2)
);
// Map distance: approx 0.02 (touching) to 0.2 (open)
interaction.pinchDistance = THREE.MathUtils.mapLinear(distance, 0.02, 0.2, 0.2, 1.5);
interaction.pinchDistance = Math.max(0.1, Math.min(interaction.pinchDistance, 2.0));
// 3. Fist Detection (Simple heuristic: Fingertips close to palm)
// Check if fingertips (8, 12, 16, 20) are below PIP joints (6, 10, 14, 18) in Y axis
// Note: Coordinates are normalized. 0 is top, 1 is bottom.
const isFist = (landmarks[8].y > landmarks[6].y) &&
(landmarks[12].y > landmarks[10].y) &&
(landmarks[16].y > landmarks[14].y) &&
(landmarks[20].y > landmarks[18].y);
if (isFist) {
fistHoldTimer++;
if (fistHoldTimer > 30) { // Approx 1 second hold
interaction.shapeIndex++;
switchShape(interaction.shapeIndex);
fistHoldTimer = 0; // Reset
}
} else {
fistHoldTimer = 0;
}
}
}
const hands = new Hands({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
}
});
hands.setOptions({
maxNumHands: 1,
modelComplexity: 1,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5
});
hands.onResults(onResults);
const cameraUtils = new Camera(videoElement, {
onFrame: async () => {
await hands.send({ image: videoElement });
},
width: 640,
height: 480
});
cameraUtils.start();
/**
* 4. ANIMATION LOOP
*/
function animate() {
requestAnimationFrame(animate);
const positions = particles.geometry.attributes.position.array;
const colors = particles.geometry.attributes.color.array;
// Morphing Speed
const lerpFactor = 0.05;
// Time for animations
const time = Date.now() * 0.001;
for (let i = 0; i < PARTICLE_COUNT; i++) {
const i3 = i * 3;
// 1. MORPHING LOGIC: Lerp current pos to target pos
currentPositions[i3] += (targetPositions[i3] - currentPositions[i3]) * lerpFactor;
currentPositions[i3 + 1] += (targetPositions[i3 + 1] - currentPositions[i3 + 1]) * lerpFactor;
currentPositions[i3 + 2] += (targetPositions[i3 + 2] - currentPositions[i3 + 2]) * lerpFactor;
// 2. GESTURE INTERACTION: EXPANSION (Pinch)
// We multiply positions by pinch factor
let x = currentPositions[i3] * interaction.pinchDistance;
let y = currentPositions[i3 + 1] * interaction.pinchDistance;
let z = currentPositions[i3 + 2] * interaction.pinchDistance;
// 3. NOISE/MOVEMENT (Alive feeling)
// Add subtle sine wave movement based on index
x += Math.sin(time + x) * 0.02;
y += Math.cos(time + y) * 0.02;
positions[i3] = x;
positions[i3 + 1] = y;
positions[i3 + 2] = z;
// 4. DYNAMIC COLOR
// Change color based on Hand X/Y Position
// Base color + hand offset
const r = 0.5 + Math.sin(time + x * 0.5) * 0.5 + (interaction.x * 0.5);
const g = 0.5 + Math.cos(time + y * 0.5) * 0.5 + (interaction.y * 0.5);
const b = 0.8; // High Blue base
colors[i3] = Math.max(0, Math.min(1, r));
colors[i3 + 1] = Math.max(0, Math.min(1, g));
colors[i3 + 2] = b;
}
particles.geometry.attributes.position.needsUpdate = true;
particles.geometry.attributes.color.needsUpdate = true;
// Global Rotation based on Hand X
particles.rotation.y += 0.002 + (interaction.x * 0.05);
particles.rotation.x += (interaction.y * 0.05);
renderer.render(scene, camera);
}
// Window Resize Handling
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Boot
initThree();
</script>
</body>
</html>