-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
828 lines (732 loc) · 39 KB
/
Copy pathindex.html
File metadata and controls
828 lines (732 loc) · 39 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>灵动粒子 By Haemorr</title>
<link rel="icon" href='data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><circle cx="12" cy="16" r="7" fill="%236c5ce7"/><circle cx="24" cy="10" r="4" fill="%2300f3ff"/><circle cx="22" cy="24" r="3" fill="%23ff7675"/></svg>'>
<style>
:root {
--glass-bg: rgba(255, 255, 255, 0.75);
--glass-border: rgba(255, 255, 255, 0.8);
--text-main: #2d3436;
--text-sub: #636e72;
--primary: #6c5ce7;
--sidebar-width: 280px;
--sidebar-collapsed-width: 72px;
--shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.1);
--item-hover: rgba(0,0,0,0.06);
--divider-color: rgba(0,0,0,0.08);
--active-text: #ffffff;
}
body.dark-mode {
--glass-bg: rgba(30, 30, 30, 0.6);
--glass-border: rgba(255, 255, 255, 0.1);
--text-main: #f1f2f6;
--text-sub: #a4b0be;
--primary: #00f3ff;
--shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.6);
--item-hover: rgba(255,255,255,0.1);
--divider-color: rgba(255,255,255,0.15);
--active-text: #000000;
}
body {
margin: 0; overflow: hidden;
font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
color: var(--text-main);
transition: color 0.5s ease;
touch-action: none;
}
.bg-layer {
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
z-index: 0; transition: opacity 0.8s ease-in-out;
}
#bg-light { background: linear-gradient(135deg, #fdfbfb 0%, #ebedee 100%); opacity: 1; }
#bg-dark { background: linear-gradient(135deg, #1a1a1a 0%, #2c2c2c 50%, #1a1a1a 100%); opacity: 0; }
body.dark-mode #bg-light { opacity: 0; }
body.dark-mode #bg-dark { opacity: 1; }
#canvas-container { position: absolute; top: 0; left: 0; width: 100vw; height: 100vh; z-index: 1; }
#sidebar {
position: fixed; top: 20px; bottom: 20px; left: 20px;
width: var(--sidebar-width);
background: var(--glass-bg);
backdrop-filter: blur(24px); -webkit-backdrop-filter: blur(24px);
border: 1px solid var(--glass-border);
border-radius: 24px;
z-index: 100;
box-shadow: var(--shadow);
display: flex; flex-direction: column;
transition: width 0.4s cubic-bezier(0.25, 1, 0.5, 1);
overflow: hidden;
}
#sidebar.collapsed { width: var(--sidebar-collapsed-width); }
.sidebar-header {
padding: 20px; display: flex; align-items: center; justify-content: space-between;
min-height: 44px; border-bottom: 1px solid var(--divider-color);
}
.app-title {
font-size: 1.1rem; font-weight: 800; color: var(--primary);
white-space: nowrap; transition: opacity 0.2s; margin-left: 5px;
}
#sidebar.collapsed .app-title { opacity: 0; pointer-events: none; position: absolute; }
.toggle-btn {
background: none; border: none; color: var(--text-main);
cursor: pointer; font-size: 1.2rem; width: 36px; height: 36px;
border-radius: 8px; display: flex; align-items: center; justify-content: center;
transition: background 0.2s; margin-left: auto;
}
#sidebar.collapsed .toggle-btn { margin: 0 auto; }
.toggle-btn:hover { background: var(--item-hover); }
.sidebar-content {
flex: 1; padding: 15px; overflow-y: auto; overflow-x: hidden;
display: flex; flex-direction: column; gap: 0;
}
.sidebar-content::-webkit-scrollbar { width: 0; }
.divider {
width: 100%; height: 1px; background: var(--divider-color);
margin: 15px 0; flex-shrink: 0; transition: all 0.3s;
}
#sidebar.collapsed .divider { width: 24px; margin: 15px auto; }
.control-group { display: flex; flex-direction: column; gap: 10px; }
.group-header {
display: flex; align-items: center;
color: var(--text-sub); font-size: 0.85rem; font-weight: 600;
height: 32px; transition: all 0.3s; cursor: pointer; border-radius: 8px;
}
#sidebar.collapsed .group-header { justify-content: center; width: 100%; }
#sidebar.collapsed .group-header:hover { background: var(--item-hover); }
.group-label { white-space: nowrap; transition: opacity 0.2s; }
.icon-box {
width: 32px; display: flex; justify-content: center; align-items: center;
font-size: 1rem; flex-shrink: 0; margin-right: 4px;
}
#sidebar.collapsed .icon-box { margin-right: 0; }
#sidebar.collapsed .group-label { display: none; }
.cam-switch-btn {
width: 100%; height: 42px; padding: 0 12px;
border-radius: 12px; border: 1px solid var(--glass-border);
background: var(--item-hover); color: var(--text-main);
cursor: pointer; display: flex; align-items: center;
transition: all 0.2s; font-size: 0.9rem; font-weight: 600;
box-sizing: border-box;
}
.cam-switch-btn.active { background: rgba(0, 184, 148, 0.15); color: #00b894; border-color: #00b894; }
.cam-switch-btn:hover { transform: translateY(-1px); }
.cam-icon-wrapper {
width: 32px; display: flex; justify-content: center; align-items: center;
font-size: 1.1rem; flex-shrink: 0; margin-right: 4px;
}
.cam-text { line-height: 1; padding-top: 1px; white-space: nowrap; }
#sidebar.collapsed .cam-switch-btn { width: 42px; padding: 0; justify-content: center; margin: 0 auto; }
#sidebar.collapsed .cam-icon-wrapper { margin-right: 0; }
#sidebar.collapsed .cam-text { display: none; }
.shape-list { display: flex; flex-direction: column; gap: 6px; }
.shape-btn {
display: flex; align-items: center; height: 42px; padding: 0 12px;
border: none; background: transparent; color: var(--text-main);
border-radius: 12px; cursor: pointer; transition: all 0.2s;
font-size: 0.95rem; overflow: hidden; text-align: left; box-sizing: border-box;
}
.shape-btn:hover { background: var(--item-hover); }
.shape-btn.active {
background: var(--primary); color: var(--active-text);
box-shadow: 0 4px 12px rgba(0,0,0,0.15); font-weight: 600;
}
.shape-icon {
width: 32px; display: flex; justify-content: center; align-items: center;
font-size: 1.2rem; flex-shrink: 0; margin-right: 4px;
}
.shape-text { white-space: nowrap; line-height: 1; padding-top: 1px;}
#sidebar.collapsed .shape-btn { width: 42px; padding: 0; justify-content: center; margin: 0 auto; }
#sidebar.collapsed .shape-icon { margin-right: 0; }
#sidebar.collapsed .shape-text { display: none; }
.color-picker-wrapper {
width: 100%; height: 40px; border-radius: 12px; position: relative;
background: var(--item-hover); border: 1px solid var(--glass-border);
display: flex; align-items: center; padding: 4px; box-sizing: border-box;
cursor: pointer; transition: all 0.2s;
}
.color-picker-wrapper:hover { background: rgba(150,150,150, 0.1); }
#current-color-display {
flex: 1; height: 100%; border-radius: 8px; background-color: var(--primary);
box-shadow: 0 2px 6px rgba(0,0,0,0.1); transition: background-color 0.2s; position: relative;
}
#native-color-input { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; z-index: 2; }
.color-tip { font-size: 0.8rem; color: var(--text-sub); margin-left: 10px; margin-right: 5px; white-space: nowrap; }
#sidebar.collapsed .color-picker-wrapper { width: 42px; height: 42px; padding: 0; justify-content: center; margin: 0 auto; }
#sidebar.collapsed #current-color-display { width: 24px; height: 24px; flex: none; border-radius: 50%; }
#sidebar.collapsed .color-tip { display: none; }
.instruction-box {
font-size: 0.8rem; color: var(--text-sub); line-height: 1.6;
background: var(--item-hover); padding: 12px; border-radius: 12px;
}
#sidebar.collapsed .instruction-box { display: none; }
.theme-wrapper { padding: 15px; border-top: 1px solid var(--divider-color); }
.theme-btn {
width: 100%; height: 44px; border-radius: 12px;
border: 1px solid var(--glass-border); background: var(--item-hover);
color: var(--text-main); cursor: pointer;
display: flex; align-items: center; justify-content: center; gap: 10px;
font-size: 0.9rem; transition: all 0.2s;
}
.theme-btn:hover { background: rgba(150,150,150, 0.15); }
#sidebar.collapsed .theme-btn { padding: 0; }
#sidebar.collapsed .theme-text { display: none; }
#video-container { position: fixed; bottom: 20px; right: 20px; width: 160px; height: 120px; z-index: 10; }
#video-feed {
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
border-radius: 16px; object-fit: cover; transform: scaleX(-1);
border: 3px solid var(--primary); box-shadow: var(--shadow);
opacity: 0.9; display: none; background: #000;
}
#output-canvas {
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
border-radius: 16px; object-fit: cover; transform: scaleX(-1);
pointer-events: none; z-index: 11; display: none;
}
#camera-placeholder {
width: 100%; height: 100%;
border-radius: 16px; background: rgba(0,0,0,0.1);
backdrop-filter: blur(5px); border: 2px dashed var(--text-sub);
display: flex; align-items: center; justify-content: center; flex-direction: column;
color: var(--text-sub); font-size: 0.8rem; box-sizing: border-box;
}
.cam-off-icon { font-size: 1.5rem; opacity: 0.5; margin-bottom: 5px; }
#loader {
position: fixed; top: 0; left: 0; width: 100%; height: 100%;
background: #fff; z-index: 999; display: flex; flex-direction: column;
justify-content: center; align-items: center; transition: opacity 0.5s; color: #6c5ce7;
}
.spinner {
width: 40px; height: 40px; border: 4px solid rgba(108, 92, 231, 0.2);
border-top: 4px solid #6c5ce7; border-radius: 50%;
animation: spin 1s linear infinite; margin-bottom: 20px;
}
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
</style>
</head>
<body>
<div id="bg-light" class="bg-layer"></div>
<div id="bg-dark" class="bg-layer"></div>
<div id="loader">
<div class="spinner"></div>
<div>Engine Start...</div>
</div>
<!-- 侧边栏 -->
<nav id="sidebar">
<div class="sidebar-header">
<div class="app-title">✨ 灵动粒子</div>
<button class="toggle-btn" id="sidebar-toggle" title="折叠/展开">☰</button>
</div>
<div class="sidebar-content">
<div class="control-group">
<button class="cam-switch-btn" id="cam-toggle-btn" title="开启/关闭手势识别">
<div class="cam-icon-wrapper">📷</div>
<span class="cam-text" id="cam-btn-text">开启摄像头控制</span>
</button>
</div>
<div class="divider"></div>
<div class="control-group">
<div class="group-header" title="点击展开">
<div class="icon-box">💠</div>
<span class="group-label">模型形态</span>
</div>
<div class="shape-list">
<button class="shape-btn active" data-shape="heart" id="btn-heart">
<div class="shape-icon">❤️</div><span class="shape-text">挚爱之心</span>
</button>
<button class="shape-btn" data-shape="galaxy" id="btn-galaxy">
<div class="shape-icon">🌌</div><span class="shape-text">浩瀚银河</span>
</button>
<button class="shape-btn" data-shape="flower" id="btn-flower">
<div class="shape-icon">🌹</div><span class="shape-text">繁花盛宴</span>
</button>
<button class="shape-btn" data-shape="forest" id="btn-forest">
<div class="shape-icon">🌲</div><span class="shape-text">迷雾森林</span>
</button>
<button class="shape-btn" data-shape="dna" id="btn-dna">
<div class="shape-icon">🧬</div><span class="shape-text">生命螺旋</span>
</button>
</div>
</div>
<div class="divider"></div>
<div class="control-group">
<div class="group-header" title="点击展开">
<div class="icon-box">🎨</div>
<span class="group-label">粒子色彩</span>
</div>
<div class="color-picker-wrapper" title="点击选择任意颜色">
<div id="current-color-display"></div>
<div class="color-tip">点击切换</div>
<input type="color" id="native-color-input" value="#6c5ce7">
</div>
</div>
<div class="divider"></div>
<div class="control-group">
<div class="group-header" title="点击展开">
<div class="icon-box">ℹ️</div>
<span class="group-label">操作指南</span>
</div>
<div class="instruction-box">
👋 <b>缩放</b>: 张开/捏合手掌<br>
✋ <b>旋转</b>: 移动手腕位置<br>
✊ <b>锁定</b>: 右手握拳锁旋转<br>
🙌 <b>重置</b>: 双手同时握拳
</div>
</div>
</div>
<div class="theme-wrapper">
<button class="theme-btn" id="theme-btn" title="切换显示模式">
<span id="theme-icon" style="font-size: 1.1rem">☀</span>
<span class="theme-text">当前:浅色模式</span>
</button>
</div>
</nav>
<div id="video-container">
<div id="camera-placeholder">
<div class="cam-off-icon">📷⃠</div>
<div>已关闭</div>
</div>
<video id="video-feed" playsinline muted autoplay></video>
<canvas id="output-canvas"></canvas>
</div>
<div id="canvas-container"></div>
<script type="module">
import * as THREE from 'https://cdn.skypack.dev/three@0.136.0';
import { OrbitControls } from 'https://cdn.skypack.dev/three@0.136.0/examples/jsm/controls/OrbitControls.js';
const loadScript = (src) => new Promise((resolve, reject) => {
const s = document.createElement('script');
s.src = src; s.onload = resolve; s.onerror = reject;
document.body.appendChild(s);
});
let scene, camera, renderer, particles, geometry, material;
const isMobileDevice = window.innerWidth < 768;
const particleCount = isMobileDevice ? 8000 : 25000;
let isDarkMode = false;
let currentShape = 'heart';
let targetScale = 1; let currentScale = 1;
let targetRotationX = 0, targetRotationY = 0;
let currentRotationX = 0, currentRotationY = 0;
let clock = new THREE.Clock();
let cameraUtils = null;
let isCameraActive = false;
let handDetected = false;
let sidebarAutoCloseTimer = null; // [新增] 侧边栏自动收起定时器
const Shapes = {
heart: (i) => {
const u = Math.random() * Math.PI * 2;
const v = Math.random() * Math.PI;
const x = 16 * Math.pow(Math.sin(u), 3) * Math.sin(v);
const y = (13 * Math.cos(u) - 5 * Math.cos(2*u) - 2 * Math.cos(3*u) - Math.cos(4*u)) * Math.sin(v);
const z = 6 * Math.cos(v);
return new THREE.Vector3(x * 0.22, y * 0.22, z * 0.22);
},
galaxy: (i) => {
const arms = 3; const armWidth = 0.8;
const isCore = Math.random() < 0.25;
if (isCore) {
const theta = Math.random() * Math.PI * 2;
const phi = Math.acos(2 * Math.random() - 1);
const r = Math.pow(Math.random(), 1/3) * 1.5;
return new THREE.Vector3(r * Math.sin(phi) * Math.cos(theta), r * Math.cos(phi) * 0.4, r * Math.sin(phi) * Math.sin(theta));
}
const spin = i * 0.0002;
const armIndex = i % arms;
const armAngle = (armIndex / arms) * Math.PI * 2;
const dist = Math.pow(Math.random(), 2) * 9 + 0.5;
const angle = armAngle + dist * 0.5 + (Math.random() - 0.5) * armWidth;
const x = Math.cos(angle) * dist;
const z = Math.sin(angle) * dist;
const height = (Math.random() - 0.5) * (2.5 / (dist * 0.5 + 0.1));
return new THREE.Vector3(x, height, z);
},
flower: (i) => {
const layerCount = 6;
const t = Math.pow(Math.random(), 0.6);
const layerIndex = Math.floor(t * layerCount);
const progress = (layerIndex + Math.random()) / layerCount;
const baseR = 0.5 + progress * 3.5;
const u = Math.random() * Math.PI * 2;
const petals = 6;
const waveAmp = 0.2 + progress * 0.4;
const offset = layerIndex * (Math.PI / petals);
const r = baseR * (1 + waveAmp * Math.sin(petals * u + offset));
let y = (1 - progress) * 2.0;
const curl = Math.sin(petals * u + offset);
if (progress > 0.6) y -= curl * 0.3 * progress; else y += curl * 0.1;
y += (Math.random() - 0.5) * 0.15;
const x = r * Math.cos(u); const z = r * Math.sin(u);
return new THREE.Vector3(x, y - 1, z);
},
forest: (i) => {
const treeCount = 60; const bushCount = 40;
const rand = Math.random();
if (rand < 0.65) {
const treeIdx = i % treeCount;
const angle = treeIdx * 2.39996; const dist = 4.0 * Math.sqrt(treeIdx + 1);
const tx = Math.cos(angle) * dist; const tz = Math.sin(angle) * dist;
const pseudoRand = Math.sin(treeIdx * 999);
const heightScale = 1.0 + 0.5 * pseudoRand;
if (Math.random() < 0.25) {
const h = Math.random() * 2.0 * heightScale;
const r = 0.2 + (2.0 - h) * 0.05;
const theta = Math.random() * Math.PI * 2;
return new THREE.Vector3(tx + r*Math.cos(theta), h - 4, tz + r*Math.sin(theta));
} else {
const crownY = -2.5 + 2.0 * heightScale;
const r = Math.pow(Math.random(), 1/3);
const theta = Math.random() * Math.PI * 2;
const phi = Math.acos(2 * Math.random() - 1);
const crownRad = 1.3 + 0.3 * pseudoRand;
return new THREE.Vector3(tx + r * crownRad * Math.sin(phi) * Math.cos(theta), crownY + r * crownRad * Math.cos(phi) * 1.3, tz + r * crownRad * Math.sin(phi) * Math.sin(theta));
}
} else if (rand < 0.90) {
const bushIdx = (i * 17) % bushCount;
const seed = bushIdx * 654.321;
const bx = (Math.sin(seed) * 22); const bz = (Math.cos(seed * 0.9) * 22);
const br = 0.7 + 0.3 * Math.sin(seed);
const r = Math.pow(Math.random(), 1/3) * br;
const theta = Math.random() * Math.PI * 2;
const phi = Math.random() * Math.PI / 2;
return new THREE.Vector3(bx + r * Math.sin(phi) * Math.cos(theta), -4 + r * Math.cos(phi) * 0.7, bz + r * Math.sin(phi) * Math.sin(theta));
} else {
const x = (Math.random() - 0.5) * 60; const z = (Math.random() - 0.5) * 60;
return new THREE.Vector3(x, -4, z);
}
},
dna: (i) => {
const len = 18; const t = Math.random() * len - len/2;
const angle = t * 2.5; const type = Math.random();
if (type < 0.7) {
const strand = Math.random() > 0.5 ? 0 : Math.PI;
const x = 2.0 * Math.cos(angle + strand) + (Math.random()-0.5)*0.4;
const z = 2.0 * Math.sin(angle + strand) + (Math.random()-0.5)*0.4;
return new THREE.Vector3(x, t, z);
} else {
const segment = Math.floor((t + len/2) * 2);
if (segment % 3 === 0) {
const alpha = Math.random();
const x1 = 2.0 * Math.cos(angle); const z1 = 2.0 * Math.sin(angle);
const x2 = 2.0 * Math.cos(angle + Math.PI); const z2 = 2.0 * Math.sin(angle + Math.PI);
return new THREE.Vector3(x1 + (x2-x1)*alpha, t, z1 + (z2-z1)*alpha);
} else return new THREE.Vector3(0,0,0);
}
}
};
function createParticleTexture() {
const canvas = document.createElement('canvas');
canvas.width = 32; canvas.height = 32;
const ctx = canvas.getContext('2d');
const grad = ctx.createRadialGradient(16, 16, 0, 16, 16, 16);
grad.addColorStop(0, 'rgba(255, 255, 255, 1)');
grad.addColorStop(1, 'rgba(255, 255, 255, 0)');
ctx.fillStyle = grad;
ctx.fillRect(0, 0, 32, 32);
const texture = new THREE.Texture(canvas);
texture.needsUpdate = true;
return texture;
}
function initScene() {
const container = document.getElementById('canvas-container');
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0xffffff, 0.035);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 12;
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
container.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.autoRotate = true;
controls.autoRotateSpeed = 2.0;
window.controls = controls;
geometry = new THREE.BufferGeometry();
const initialPos = new Float32Array(particleCount * 3);
const targetPos = new Float32Array(particleCount * 3);
const colors = new Float32Array(particleCount * 3);
const baseColor = new THREE.Color('#6c5ce7');
for (let i = 0; i < particleCount; i++) {
initialPos[i * 3] = (Math.random() - 0.5) * 50;
initialPos[i * 3 + 1] = (Math.random() - 0.5) * 50;
initialPos[i * 3 + 2] = (Math.random() - 0.5) * 50;
const c = baseColor.clone();
c.offsetHSL(0, 0, (Math.random() - 0.5) * 0.2);
colors[i * 3] = c.r; colors[i * 3 + 1] = c.g; colors[i * 3 + 2] = c.b;
}
geometry.setAttribute('position', new THREE.BufferAttribute(initialPos, 3));
geometry.setAttribute('targetPosition', new THREE.BufferAttribute(targetPos, 3));
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));
material = new THREE.PointsMaterial({
size: 0.12, vertexColors: true, blending: THREE.NormalBlending,
depthWrite: false, transparent: true, opacity: 0.8,
map: createParticleTexture()
});
particles = new THREE.Points(geometry, material);
scene.add(particles);
generateTargetPositions('heart');
window.addEventListener('resize', onWindowResize);
}
function generateTargetPositions(shapeName) {
const posAttr = geometry.attributes.targetPosition;
const generator = Shapes[shapeName];
for (let i = 0; i < particleCount; i++) {
const v = generator(i);
posAttr.setXYZ(i, v.x, v.y, v.z);
}
posAttr.needsUpdate = true;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
currentScale = THREE.MathUtils.lerp(currentScale, targetScale, 0.05);
particles.scale.setScalar(currentScale);
if (isCameraActive && handDetected) {
window.controls.autoRotate = false;
currentRotationY = THREE.MathUtils.lerp(currentRotationY, targetRotationY, 0.06);
currentRotationX = THREE.MathUtils.lerp(currentRotationX, targetRotationX, 0.06);
particles.rotation.y = currentRotationY;
particles.rotation.x = currentRotationX;
} else {
window.controls.autoRotate = true;
particles.rotation.x = THREE.MathUtils.lerp(particles.rotation.x, 0, 0.05);
currentRotationY = particles.rotation.y;
}
window.controls.update();
const pos = geometry.attributes.position.array;
const target = geometry.attributes.targetPosition.array;
const speed = 4.0 * delta;
for (let i = 0; i < particleCount; i++) {
const ix = i * 3;
pos[ix] += (target[ix] - pos[ix]) * speed;
pos[ix+1] += (target[ix+1] - pos[ix+1]) * speed;
pos[ix+2] += (target[ix+2] - pos[ix+2]) * speed;
}
geometry.attributes.position.needsUpdate = true;
renderer.render(scene, camera);
}
function initUI() {
const sidebar = document.getElementById('sidebar');
const toggleBtn = document.getElementById('sidebar-toggle');
toggleBtn.addEventListener('click', () => sidebar.classList.toggle('collapsed'));
// 点击外部自动收起
document.addEventListener('click', (e) => {
if (!sidebar.classList.contains('collapsed') &&
!sidebar.contains(e.target) &&
!toggleBtn.contains(e.target)) {
sidebar.classList.add('collapsed');
}
});
const shapeBtns = document.querySelectorAll('.shape-btn');
shapeBtns.forEach(btn => {
btn.addEventListener('click', (e) => {
shapeBtns.forEach(b => b.classList.remove('active'));
const target = e.target.closest('.shape-btn');
target.classList.add('active');
const newShape = target.dataset.shape;
if (newShape !== currentShape) {
currentShape = newShape;
generateTargetPositions(newShape);
}
});
});
const groupHeaders = document.querySelectorAll('.group-header');
groupHeaders.forEach(header => {
header.addEventListener('click', () => {
if (sidebar.classList.contains('collapsed')) {
sidebar.classList.remove('collapsed');
}
});
});
const colorInput = document.getElementById('native-color-input');
const colorDisplay = document.getElementById('current-color-display');
colorInput.addEventListener('input', (e) => {
const hex = e.target.value;
colorDisplay.style.backgroundColor = hex;
document.documentElement.style.setProperty('--primary', hex);
const c = new THREE.Color(hex);
const hsl = {}; c.getHSL(hsl); const isLight = hsl.l > 0.8;
const colors = geometry.attributes.color;
for(let i=0; i<particleCount; i++) {
const temp = c.clone();
const offset = isLight ? (Math.random() * 0.1) : (Math.random()-0.5)*0.2;
temp.offsetHSL(0, 0, offset);
colors.setXYZ(i, temp.r, temp.g, temp.b);
}
colors.needsUpdate = true;
});
const themeBtn = document.getElementById('theme-btn');
const themeIcon = document.getElementById('theme-icon');
const themeText = themeBtn.querySelector('.theme-text');
themeBtn.addEventListener('click', () => {
isDarkMode = !isDarkMode;
document.body.classList.toggle('dark-mode');
if (isDarkMode) {
themeIcon.innerText = '🌙'; themeText.innerText = '当前:深色模式';
scene.fog.color.set(0x1a1a1a);
material.blending = THREE.AdditiveBlending; material.opacity = 0.6;
} else {
themeIcon.innerText = '☀'; themeText.innerText = '当前:浅色模式';
scene.fog.color.set(0xffffff);
material.blending = THREE.NormalBlending; material.opacity = 0.8;
}
material.needsUpdate = true;
});
}
async function initAI() {
await loadScript('https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js');
await loadScript('https://cdn.jsdelivr.net/npm/@mediapipe/control_utils/control_utils.js');
await loadScript('https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js');
await loadScript('https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js');
const videoElement = document.getElementById('video-feed');
const canvasElement = document.getElementById('output-canvas');
const canvasCtx = canvasElement.getContext('2d');
const placeholder = document.getElementById('camera-placeholder');
const camBtn = document.getElementById('cam-toggle-btn');
const camText = document.getElementById('cam-btn-text');
const sidebar = document.getElementById('sidebar');
const hands = new Hands({locateFile: (file) => `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`});
hands.setOptions({
maxNumHands: 2, // 开启双手支持
modelComplexity: 1,
minDetectionConfidence: 0.4,
minTrackingConfidence: 0.5
});
// 简单的握拳检测函数
function isFist(landmarks) {
// 判断四个手指指尖是否低于指根关节(y坐标更大)
// 坐标系:y向下为正
// 注意:这适用于手掌向着摄像头(常规操作)
// 食指: 8 tip, 6 pip
// 中指: 12 tip, 10 pip
// 无名指: 16 tip, 14 pip
// 小指: 20 tip, 18 pip
let closedFingers = 0;
if (landmarks[8].y > landmarks[6].y) closedFingers++;
if (landmarks[12].y > landmarks[10].y) closedFingers++;
if (landmarks[16].y > landmarks[14].y) closedFingers++;
if (landmarks[20].y > landmarks[18].y) closedFingers++;
// 拇指判断比较复杂,简化为判断拇指尖是否靠近小指根部
// 或者直接忽略拇指,只看四指
return closedFingers >= 4;
}
hands.onResults((results) => {
if(videoElement.videoWidth) {
canvasElement.width = videoElement.videoWidth;
canvasElement.height = videoElement.videoHeight;
canvasCtx.save();
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
let activeHands = 0;
if (results.multiHandLandmarks) activeHands = results.multiHandLandmarks.length;
if (activeHands > 0) {
handDetected = true;
// [新增] 自动收起侧边栏逻辑
if (!sidebar.classList.contains('collapsed') && !sidebarAutoCloseTimer) {
sidebarAutoCloseTimer = setTimeout(() => {
sidebar.classList.add('collapsed');
sidebarAutoCloseTimer = null;
}, 3000); // 3秒后收起
}
// 绘制骨骼
for (const landmarks of results.multiHandLandmarks) {
drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS, {color: '#00FF00', lineWidth: 4});
drawLandmarks(canvasCtx, landmarks, {color: '#FF0000', lineWidth: 2});
}
// --- 双手握拳重置检测 ---
let fistCount = 0;
let rotationLocked = false;
for (let i = 0; i < activeHands; i++) {
const landmarks = results.multiHandLandmarks[i];
const isHandFist = isFist(landmarks);
if (isHandFist) {
fistCount++;
// [新增] 左手握拳检测 (用于锁定旋转)
// MediaPipe handedness label "Left" 通常指用户的左手
// 注意:在 mirrored 模式下,左手出现在屏幕右侧,label 仍为 "Left"
if (results.multiHandedness && results.multiHandedness[i].label === 'Left') {
rotationLocked = true;
}
}
}
if (fistCount === 2) {
// 双手握拳 -> 重置状态
targetScale = 1.0;
targetRotationX = 0;
targetRotationY = 0;
} else {
// 单手或双手非握拳交互
// 取第一只手作为主控手
const lm = results.multiHandLandmarks[0];
// 缩放 (始终响应)
const p1 = lm[4], p2 = lm[8];
const dist = Math.hypot(p1.x-p2.x, p1.y-p2.y);
targetScale = Math.min(Math.max((dist - 0.03) * 18, 0.5), 3.0);
// 旋转 (左手握拳时不响应)
if (!rotationLocked) {
const wrist = lm[0];
targetRotationY = (wrist.x - 0.5) * 6.0;
targetRotationX = (wrist.y - 0.5) * 4.0;
}
}
} else {
handDetected = false;
targetScale = 1.0;
// 如果手消失了,是否需要清除定时器?
// 逻辑上:手消失了 -> 下次手出现重新计时
if (sidebarAutoCloseTimer) {
clearTimeout(sidebarAutoCloseTimer);
sidebarAutoCloseTimer = null;
}
}
canvasCtx.restore();
}
});
cameraUtils = new Camera(videoElement, {
onFrame: async () => { await hands.send({image: videoElement}); },
width: { ideal: 1280 },
height: { ideal: 720 },
facingMode: 'user'
});
camBtn.addEventListener('click', async () => {
if (!isCameraActive) {
try {
await cameraUtils.start();
isCameraActive = true;
videoElement.style.display = 'block';
canvasElement.style.display = 'block';
placeholder.style.display = 'none';
camBtn.classList.add('active');
camText.innerText = '关闭摄像头';
} catch(e) {
alert("摄像头启动失败:\n1.请确保使用 HTTPS\n2.请允许浏览器访问相机");
console.error(e);
}
} else {
await cameraUtils.stop();
const stream = videoElement.srcObject;
if (stream) { stream.getTracks().forEach(track => track.stop()); videoElement.srcObject = null; }
isCameraActive = false; handDetected = false; targetScale = 1.0;
videoElement.style.display = 'none';
canvasElement.style.display = 'none';
placeholder.style.display = 'flex';
camBtn.classList.remove('active');
camText.innerText = '开启摄像头控制';
}
});
document.getElementById('loader').style.opacity = 0;
setTimeout(() => document.getElementById('loader').style.display = 'none', 500);
}
window.onload = () => {
initScene();
initUI();
animate();
initAI();
};
</script>
</body>
</html>