-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
1629 lines (1412 loc) · 63 KB
/
Copy pathindex.html
File metadata and controls
1629 lines (1412 loc) · 63 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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>A Small Firework</title>
<link rel="icon" type="image/png" href="favicon.png">
<link rel="apple-touch-icon" href="favicon.png">
<!-- Import Cuprum Font -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Cuprum:ital,wght@0,400..700;1,400..700&display=swap" rel="stylesheet">
<style>
/* Base Reset */
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
font-family: 'Cuprum', sans-serif;
}
/* Night Sky Background */
body {
background: radial-gradient(ellipse at bottom, #1B2735 0%, #090A0F 100%);
color: white;
user-select: none;
-webkit-user-select: none;
}
canvas {
display: block;
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
z-index: 1;
}
/* Interaction Zone (Left Edge) */
#trigger-zone {
position: absolute;
top: 0;
left: 0;
width: 30px;
height: 100%;
z-index: 100;
background: transparent;
}
/* Sidebar Menu */
#sidebar {
position: absolute;
top: 0;
left: -220px;
width: 220px;
height: 100%;
background: rgba(255, 255, 255, 0.05);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
border-right: 1px solid rgba(255, 255, 255, 0.1);
transition: left 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
z-index: 99;
padding-top: 60px;
box-sizing: border-box;
display: flex;
flex-direction: column;
gap: 15px;
overflow-y: auto;
overflow-x: hidden;
scrollbar-width: thin; /* Firefox */
scrollbar-color: rgba(255, 255, 255, 0.3) transparent; /* Firefox */
scrollbar-gutter: stable;
}
/* Night-sky themed thin scrollbar (WebKit) */
#sidebar::-webkit-scrollbar {
width: 4px;
}
#sidebar::-webkit-scrollbar-track {
background: transparent;
}
#sidebar::-webkit-scrollbar-thumb {
background-color: rgba(255, 255, 255, 0.2);
border-radius: 4px;
}
#trigger-zone:hover ~ #sidebar,
#sidebar:hover {
left: 0;
}
.menu-title {
text-align: right;
font-size: 1.3rem;
letter-spacing: 1px;
color: rgba(255, 255, 255, 0.9);
margin: 0 20px 5px 20px;
text-transform: uppercase;
font-weight: 600;
}
.menu-tagline {
text-align: right;
font-size: 1.0rem;
color: rgba(255, 255, 255, 0.5);
margin: 0 20px 25px 20px;
font-style: italic;
font-family: 'Cuprum', sans-serif;
padding: 0;
}
/* Transparent Buttons */
.fw-option {
background: transparent;
border: 1px solid rgba(255, 255, 255, 0.2);
color: rgba(255, 255, 255, 0.7);
padding: 15px 20px;
margin: 0 20px;
border-radius: 8px;
cursor: pointer;
text-align: left;
transition: all 0.3s ease;
font-size: 1.1rem;
position: relative;
overflow: hidden;
font-family: 'Cuprum', sans-serif;
display: flex;
align-items: center;
}
.fw-option:hover {
background: rgba(255, 255, 255, 0.1);
border-color: rgba(255, 255, 255, 0.5);
color: #fff;
padding-left: 25px;
}
.fw-option.active {
background: rgba(255, 255, 255, 0.15);
border-color: #a8c0ff;
color: #fff;
box-shadow: 0 0 15px rgba(168, 192, 255, 0.2);
}
/* Special style for the auto-play button */
#btn-autoplay {
margin-top: 25px;
border-color: rgba(255, 220, 100, 0.3);
color: rgba(255, 235, 180, 0.9);
font-family: 'Cuprum', sans-serif;
font-style: italic;
transition: all 0.5s ease;
}
#btn-autoplay:hover {
background: rgba(255, 220, 100, 0.1);
border-color: rgba(255, 220, 100, 0.6);
color: #fff;
padding-left: 25px;
}
#btn-autoplay.active {
background: rgba(255, 220, 100, 0.15);
border-color: rgba(255, 220, 100, 1);
color: #fff;
box-shadow: 0 0 20px rgba(255, 200, 50, 0.15);
text-shadow: 0 0 5px rgba(255, 200, 50, 0.5);
}
/* Help Text */
#hint {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
text-align: center;
color: rgba(255, 240, 220, 0.9);
pointer-events: none;
z-index: 5;
font-family: 'Cuprum', sans-serif;
font-size: 2rem;
font-style: italic;
letter-spacing: 1px;
text-shadow: 0 0 15px rgba(255, 200, 100, 0.2);
opacity: 0;
animation: warmFade 6s ease-out forwards;
}
#hint span {
display: block;
margin-top: 10px;
font-size: 1.2rem;
font-family: 'Cuprum', sans-serif;
opacity: 0.7;
font-style: normal;
letter-spacing: 0.5px;
}
@keyframes warmFade {
0% { opacity: 0; transform: translate(-50%, -40%); }
15% { opacity: 1; transform: translate(-50%, -50%); }
70% { opacity: 1; transform: translate(-50%, -50%); }
100% { opacity: 0; transform: translate(-50%, -60%); }
}
/* Volume Control Styles */
.volume-control {
margin: 20px 20px 0 20px;
padding-top: 20px;
border-top: 1px solid rgba(255, 255, 255, 0.1);
}
.volume-label {
font-size: 0.95rem;
letter-spacing: 0.5px;
color: rgba(255, 255, 255, 0.75);
text-transform: uppercase;
}
.volume-control input[type=range] {
-webkit-appearance: none;
appearance: none;
width: 100%;
height: 4px;
background: rgba(255, 255, 255, 0.15);
border-radius: 2px;
outline: none;
margin-top: 10px;
}
.volume-control input[type=range]::-webkit-slider-runnable-track {
height: 4px;
background: rgba(255, 255, 255, 0.2);
border-radius: 2px;
}
.volume-control input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 14px;
height: 14px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.75);
border: 1px solid rgba(255, 255, 255, 0.75);
box-shadow: 0 0 8px rgba(255, 255, 255, 0.4);
margin-top: -5px;
cursor: pointer;
}
.volume-control input[type=range]::-moz-range-track {
height: 4px;
background: rgba(255, 255, 255, 0.2);
border: none;
border-radius: 2px;
}
.volume-control input[type=range]::-moz-range-thumb {
width: 14px;
height: 14px;
border-radius: 50%;
background: rgba(255, 255, 255, 0.75);
border: 1px solid rgba(255, 255, 255, 0.75);
box-shadow: 0 0 8px rgba(255, 255, 255, 0.4);
cursor: pointer;
}
.volume-control input[type=range]:focus {
outline: none;
}
.volume-control input[type=range]:hover::-webkit-slider-thumb {
box-shadow: 0 0 10px rgba(255, 255, 255, 0.6);
}
.volume-control input[type=range]:hover::-moz-range-thumb {
box-shadow: 0 0 10px rgba(255, 255, 255, 0.6);
}
/* Subtle footer at the bottom of sidebar */
.sidebar-footer {
margin: 10px 20px 18px 20px;
margin-top: auto;
font-size: 0.85rem;
color: rgba(255, 255, 255, 0.35);
text-align: right;
}
.sidebar-footer a {
color: inherit;
text-decoration: none;
}
.sidebar-footer a:hover {
color: rgba(255, 255, 255, 0.6);
text-decoration: underline;
text-underline-offset: 2px;
}
</style>
</head>
<body>
<!-- Three.js Container -->
<div id="canvas-container"></div>
<!-- UI Overlay -->
<div id="trigger-zone"></div>
<div id="sidebar">
<div class="menu-title">A Small Firework</div>
<div class="menu-tagline">For quiet nights like this.</div>
<!-- Manual Options -->
<button class="fw-option" id="opt-spark" onclick="setFireworkType('spark')">One Spark</button>
<button class="fw-option active" id="opt-bloom" onclick="setFireworkType('bloom')">Bloom</button>
<button class="fw-option" id="opt-drift" onclick="setFireworkType('drift')">Drift</button>
<button class="fw-option" id="opt-scatter" onclick="setFireworkType('scatter')">Scatter</button>
<!-- New Sparkler Option -->
<button class="fw-option" id="opt-sparkler" onclick="launchSparkler()">Sparkler</button>
<!-- Auto Play Option -->
<button class="fw-option" id="btn-autoplay" onclick="toggleAutoPlay()">Stay and watch</button>
<!-- Volume Control Component -->
<div class="volume-control">
<div class="volume-label">Volume</div>
<input type="range" id="volume-slider" min="0" max="1" step="0.01" value="0.5">
</div>
<div class="sidebar-footer">
<a href="https://github.com/Ahhhh2016" target="_blank" rel="noopener noreferrer">Made by Ahhhh2016</a>
</div>
</div>
<div id="hint">
Light one anywhere.
<span>The menu is on the left.</span>
</div>
<!-- Import Three.js from CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// --- 1. Global Variables & Setup ---
let scene, camera, renderer;
let fireworks = [];
let particles = [];
let raycaster, mouse;
// Systems
let trailSystem;
let sparklerSystem = null;
let audioCtx;
let masterGainNode;
let currentVolume = 0.5;
// Configuration
let currentType = 'bloom';
let isAutoPlay = false;
let autoPlayTimer = 0;
let sparklerActive = false;
// Texture cache
let particleTexture;
// Sparkler constants (mirror shader values)
const SPARKLER_BURN_START_Y = 2.2;
const SPARKLER_BURN_SPEED = 0.25;
const SPARKLER_TIME_SCALE = 1.5;
// --- 2. Shader Code for Sparkler ---
// UPDATED: Visible Paper Texture & Fixed FBM overloading
const sparklerFragmentShader = `
uniform float iTime;
uniform vec2 iResolution;
varying vec2 vUv;
// Int constants for loops
#define NUM_SPARKLES 260
#define NUM_SUB_SPARKLES 4
#define SUB_SPARKLE_CHANCE .6
// Splatter jets
#define NUM_SPLATTER_EVENTS 3
#define SPLATTER_PER_EVENT 14
// Needle-like bright jets
#define NUM_NEEDLE_EVENTS 2
#define NEEDLES_PER_EVENT 6
#define NEEDLE_SAMPLES 12
#define PRIMARY_PARTICLE_COLOR vec3(1., 0.8, 0.5)
#define SECONDARY_PARTICLE_COLOR vec3(1., 0.5, 0.3)
#define DOF vec2(1., 1.5)
// Global size multiplier for sparkler particles
#define SPARK_SIZE_MULT 1.0
// Camera Config
#define MIN_CAM_DISTANCE 1.5
#define MAX_CAM_DISTANCE 7.
// Speed Config: Starts at Y=2.2, moves down at 0.25 units/sec
#define BURN_START_Y 2.2
#define BURN_SPEED 0.25
#define PI 3.1415
#define saturate(x) clamp(x,0.,1.)
float DistSqr(vec3 a, vec3 b) { vec3 D=a-b; return dot(D, D); }
const vec3 up = vec3(0.,1.,0.);
#define MOD3 vec3(.1031,.11369,.13787)
vec3 hash31(float p) {
vec3 p3 = fract(vec3(p) * MOD3);
p3 += dot(p3, p3.yzx + 19.19);
return fract(vec3((p3.x + p3.y)*p3.z, (p3.x+p3.z)*p3.y, (p3.y+p3.z)*p3.x));
}
// 3D Value Noise
float hash(vec3 p) {
p = fract( p*0.3183099+.1 );
p *= 17.0;
return fract( p.x*p.y*p.z*(p.x+p.y+p.z) );
}
float noise(vec3 x) {
vec3 i = floor(x);
vec3 f = fract(x);
f = f*f*(3.0-2.0*f);
return mix(mix(mix( hash(i+vec3(0,0,0)), hash(i+vec3(1,0,0)),f.x),
mix( hash(i+vec3(0,1,0)), hash(i+vec3(1,1,0)),f.x),f.y),
mix(mix( hash(i+vec3(0,0,1)), hash(i+vec3(1,0,1)),f.x),
mix( hash(i+vec3(0,1,1)), hash(i+vec3(1,1,1)),f.x),f.y),f.z);
}
// FBM for vec3 (renamed from fbm to fbm3)
float fbm3(vec3 p) {
float v = 0.0;
float a = 0.5;
for (int i = 0; i < 4; i++) {
v += a * noise(p);
p *= 2.0;
a *= 0.5;
}
return v;
}
// FBM for vec2 (renamed from fbm to fbm2)
float fbm2(vec2 p) {
float v = 0.0;
float a = 0.5;
for (int i = 0; i < 4; i++) {
v += a * noise(vec3(p, 0.0));
p *= 2.0;
a *= 0.5;
}
return v;
}
// Procedural Triplanar Texture Simulation
// Simulates tex3D(pos, normal, sampler) using noise
float proceduralTex3D(vec3 pos, vec3 normal, float scale) {
// Use fbm2 for planar mapping
float nX = fbm2(pos.yz * scale);
float nY = fbm2(pos.xz * scale);
float nZ = fbm2(pos.xy * scale);
return nX * abs(normal.x) + nY * abs(normal.y) + nZ * abs(normal.z);
}
struct ray { vec3 o; vec3 d; };
struct camera {
vec3 p; vec3 forward; vec3 left; vec3 up;
vec3 center; vec3 i; ray ray; vec3 lookAt; float zoom;
};
camera cam;
void CameraSetup(vec2 uv, vec3 position, vec3 lookAt, float zoom) {
cam.p = position;
cam.lookAt = lookAt;
cam.forward = normalize(cam.lookAt-cam.p);
cam.left = cross(up, cam.forward);
cam.up = cross(cam.forward, cam.left);
cam.zoom = zoom;
cam.center = cam.p+cam.forward*cam.zoom;
cam.i = cam.center+cam.left*uv.x+cam.up*uv.y;
cam.ray.o = cam.p;
cam.ray.d = normalize(cam.i-cam.p);
}
float within(vec2 v, float t) { return (t-v.x) / (v.y-v.x); }
float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) {
vec3 pa = p-a, ba = b-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h ) - r;
}
// --- SDF & Map ---
// Returns vec4(distance, bumpValue, burnState, matID)
vec4 map( in vec3 p) {
// Calculate Current Burn Y
float scaledTime = iTime * 1.5;
float currentBurnY = BURN_START_Y - scaledTime * BURN_SPEED;
// Texture Projection Normal
vec3 normalApprox = normalize(vec3(p.x, 0.0, p.z));
// 1. New Paper Bump (Fine grain)
float newBump = proceduralTex3D(p, normalApprox, 60.0) * 0.005;
// 2. Burned Ash Bump (Coarse) - unused now as we discard ash
float burnedBump = proceduralTex3D(p, normalApprox, 20.0) * 0.06;
// Transition zone logic
float transition = smoothstep(currentBurnY - 0.2, currentBurnY + 0.2, p.y);
float bump = mix(burnedBump, newBump, transition);
// Apply bump to SDF radius
// Further reduced radius for a thinner stick
float d = sdCapsule(p, vec3(0., -4., 0.), vec3(0., 4., 0.), 0.02 + bump);
return vec4(d, bump, transition, 1.0);
}
// --- Raymarching ---
vec4 castRay( in vec3 ro, in vec3 rd ) {
float dmin = 1.0;
float dmax = 20.0;
float precis = 0.002;
float d = dmin;
float m = -1.0;
float bumpVal = 0.0;
float transVal = 0.0;
for( int i=0; i<60; i++ ) {
vec4 res = map( ro+rd*d );
if( res.x<precis || d>dmax ) {
// Hit!
bumpVal = res.y;
transVal = res.z;
m = res.w;
break;
}
d += res.x;
}
if( d>dmax ) m=-1.0;
// Return d, bump, transition, id
return vec4( d, bumpVal, transVal, m );
}
vec3 calcNormal( in vec3 pos ) {
vec3 eps = vec3( 0.001, 0.0, 0.0 );
vec3 nor = vec3(
map(pos+eps.xyy).x - map(pos-eps.xyy).x,
map(pos+eps.yxy).x - map(pos-eps.yxy).x,
map(pos+eps.yyx).x - map(pos-eps.yyx).x );
return normalize(nor);
}
vec3 ClosestPoint(ray r, vec3 p) {
return r.o + max(0., dot(p-r.o, r.d))*r.d;
}
// --- Rendering ---
vec4 render( in vec3 ro, in vec3 rd, out float d, float currentBurnY ) {
vec3 col = vec3(0.);
vec4 res = castRay(ro,rd);
d = res.x;
float bumpVal = res.y;
float transition = res.z;
float m = res.w;
float alpha = 0.0;
if( m>0.5 ) {
vec3 pos = ro + d*rd;
// Cutoff for disappeared ash
if (pos.y < currentBurnY + 0.15) {
alpha = 1.0;
vec3 nor = calcNormal( pos );
// --- Material Colors ---
// Enhanced Paper Texture using Triplanar noise
float tex = proceduralTex3D(pos, nor, 60.0);
// Deep gray base for the stick
vec3 paperCol = vec3(0.22, 0.22, 0.24);
// Fibrous texture: subtle darker strands
float fibers = smoothstep(0.4, 0.7, tex);
paperCol = mix(paperCol, vec3(0.16, 0.16, 0.18), fibers * 0.4);
// Add micro grain
float grain = noise(pos * 150.0);
paperCol *= (0.95 + 0.1 * grain);
// --- Lighting ---
vec3 lig = normalize( vec3(-0.6, 0.7, -0.5) );
float diff = clamp( dot( nor, lig ), 0.0, 1.0 );
float lighting = pow(diff, 0.6);
// Basic lighting
col = paperCol * (0.4 + 0.6 * lighting);
// --- Burning Glow Effect (Additive) ---
float distToBurn = currentBurnY - pos.y; // Positive if below line
// Narrower band for smaller central flame region
if (abs(distToBurn) < 0.025) {
float heat = exp(-abs(distToBurn) * 80.0);
// Flicker noise
float flicker = noise(vec3(0.0, iTime * 10.0, 0.0));
heat *= (0.8 + 0.2 * flicker);
// Cracks glow brighter
float crack = smoothstep(0.4, 0.7, proceduralTex3D(pos, nor, 30.0));
vec3 emberColor = vec3(1.0, 0.3, 0.05); // Orange-Red
vec3 brightColor = vec3(1.0, 0.8, 0.4); // Yellow-White
vec3 glow = mix(emberColor, brightColor, crack * 0.5);
col += glow * heat * 0.9;
}
}
}
return vec4( saturate(col), alpha );
}
vec3 sparkle(ray r, vec3 p, float size, vec3 color) {
float CAMERA_DISTANCE = length(cam.p);
float camDist = length(cam.p-p);
float focus = smoothstep(DOF.y, DOF.x, abs(camDist-CAMERA_DISTANCE));
vec3 closestPoint = ClosestPoint(r, p);
float dist = DistSqr(closestPoint, p)*10000.;
size = mix(size*5., size, focus);
float brightness = size / (dist * dist);
brightness = clamp(brightness,0., 3.0);
float bokeh = smoothstep(.01, .04, brightness)*saturate(dist*.005+.4)*.15;
brightness = mix(bokeh, brightness, focus);
return color * brightness;
}
vec3 sparkles(ray r, float time) {
vec3 col = vec3(0.);
for(int i=0; i<NUM_SPARKLES; i++) {
float fi = float(i);
float t = time + (fi/float(NUM_SPARKLES));
float ft = floor(t);
float life = fract(t);
float emissionBurnY = BURN_START_Y - (time - life) * BURN_SPEED;
vec3 n = hash31(fi + ft*123.324);
// Start close to the burn line with slight jitter
vec3 pStart = vec3(0., emissionBurnY, 0.) + (n - 0.5) * vec3(0.05, 0.02, 0.05);
// 360° uniform emission (isotropic) blended from an initial ring-like ignition
float phi = n.x * 6.28318530718; // 2 * PI
float cosTheta = 2.0 * n.y - 1.0;
float sinTheta = sqrt(max(0.0, 1.0 - cosTheta * cosTheta));
vec3 dirIso = normalize(vec3(cos(phi) * sinTheta, cosTheta, sin(phi) * sinTheta));
// Ring-directed early ignition (slight upward-to-downward bias like before)
float vtilt = mix(0.12, -0.4, n.y);
vec3 dirRing = normalize(vec3(cos(phi), vtilt, sin(phi)));
// Blend factor: 0 at ignition -> 1 as life grows (keep early ignition look)
float f = smoothstep(0.0, 0.10, life);
vec3 dir = normalize(mix(dirRing, dirIso, f));
float speed = mix(1.6, 4.0, n.z);
vec3 v0 = dir * speed;
float drag = exp(-life * 1.2);
vec3 gravity = vec3(0.0, -1.8, 0.0);
vec3 p = pStart + v0 * life * drag + 0.5 * gravity * (life * life);
if (emissionBurnY > -3.0) {
// Lifespan sizing and flicker
float size = mix(8., .5, smoothstep(0., .2, life));
size *= smoothstep(1., .2, life);
size *= smoothstep(0.0, 0.08, life);
float flick = noise(p * 6.0 + vec3(0.0, 0.0, iTime * 24.0));
size *= mix(0.85, 1.25, flick);
size *= SPARK_SIZE_MULT;
// Color temperature progression (white-hot -> warm -> cool)
vec3 hot = vec3(1.0, 0.96, 0.88);
vec3 warm = vec3(1.0, 0.72, 0.28);
vec3 cool = vec3(0.82, 0.32, 0.08);
vec3 c1 = mix(warm, hot, smoothstep(0.0, 0.18, 1.0 - life));
vec3 c2 = mix(c1, cool, smoothstep(0.5, 1.0, life));
// Occasional micro-bursts of sub-sparks
if(life > n.z && abs(n.z-.55) < SUB_SPARKLE_CHANCE) {
for(int x=0; x<NUM_SUB_SPARKLES; x++) {
float fx = float(x);
vec3 ns = hash31(fx+fi);
// Sub-spark ballistic path (jet-matching cone)
float st = saturate(within(vec2(n.z, 1.), life));
float phiS = ns.x * 6.28318530718;
float cosThetaS = 2.0 * ns.y - 1.0;
float sinThetaS = sqrt(max(0.0, 1.0 - cosThetaS * cosThetaS));
vec3 dirS = normalize(vec3(cos(phiS) * sinThetaS, cosThetaS, sin(phiS) * sinThetaS));
float spdS = mix(0.8, 2.2, ns.x);
vec3 vS = dirS * spdS;
vec3 sp = p + vS * st * exp(-st * 2.2) + 0.5 * gravity * (st * st);
float sSize = mix(6., 0.4, smoothstep(0., .12, st));
sSize *= smoothstep(1., .88, st);
sSize *= SPARK_SIZE_MULT;
vec3 sc = mix(warm, cool, st);
col += sparkle(r, sp, sSize, sc);
}
} else {
col += sparkle(r, p, size, c2);
}
}
}
return col;
}
// Splatter jets: short, wide-angle bursts around the burn line
vec3 splatterSparks(ray r, float time) {
vec3 col = vec3(0.);
for (int i = 0; i < NUM_SPLATTER_EVENTS; i++) {
float fi = float(i);
float t = time * 0.8 + fi * 0.27;
float ft = floor(t);
float life = fract(t);
float emissionBurnY = BURN_START_Y - (time - life) * BURN_SPEED;
vec3 n = hash31(fi + ft * 67.19);
vec3 pStart = vec3(0., emissionBurnY, 0.) + (n - 0.5) * vec3(0.04, 0.02, 0.04);
vec3 gravity = vec3(0.0, -1.8, 0.0);
for (int j = 0; j < SPLATTER_PER_EVENT; j++) {
float fj = float(j);
vec3 n2 = hash31(fi * 31.0 + fj * 13.0 + ft * 19.0);
// Wide horizontal cone, slight downward bias (splatter)
vec3 dir = normalize(vec3(n2.x * 2.0 - 1.0, mix(0.25, -0.45, n2.y), n2.z * 2.0 - 1.0) * vec3(1.8, 1.0, 1.8));
float spd = mix(1.2, 3.2, n2.z);
float drag = exp(-life * 1.6);
vec3 p = pStart + dir * spd * life * drag + 0.5 * gravity * (life * life);
// Size and color progression
float s = mix(5., 0.4, smoothstep(0., 0.22, life));
s *= smoothstep(1., .2, life);
s *= SPARK_SIZE_MULT;
vec3 warm = vec3(1.0, 0.72, 0.28);
vec3 cool = vec3(0.82, 0.32, 0.08);
vec3 c = mix(warm, cool, life);
col += sparkle(r, p, s, c);
}
}
return col;
}
// Subtle smoke rising from the burn line (low intensity, soft)
vec3 smokeClouds(ray r, float time, float currentBurnY) {
vec3 col = vec3(0.);
for (int i = 0; i < 12; i++) {
float fi = float(i);
float t = time * 0.5 + fi * 0.173;
float ft = floor(t);
float life = fract(t);
vec3 n = hash31(fi + ft * 41.31);
float rise = mix(0.2, 1.2, life) + n.x * 0.2;
vec3 jitter = (n - 0.5) * vec3(0.25, 0.15, 0.25);
vec3 p = vec3(0., currentBurnY + rise, 0.) + jitter;
float size = mix(40., 12., life);
float fade = (1.0 - life) * (0.35 + 0.15 * noise(p * 3.0 + vec3(0.0, 0.0, time * 1.3)));
vec3 smokeCol = vec3(0.34, 0.36, 0.4) * fade;
col += sparkle(r, p, size, smokeCol);
}
return col;
}
// Needle-like bright jets: short, thin, high-brightness streaks
vec3 needleJets(ray r, float time, float currentBurnY) {
vec3 col = vec3(0.);
for (int i = 0; i < NUM_NEEDLE_EVENTS; i++) {
float fi = float(i);
float t = time * 1.1 + fi * 0.41;
float ft = floor(t);
float life = fract(t);
vec3 n = hash31(fi + ft * 91.73);
vec3 pStart = vec3(0., currentBurnY, 0.) + (n - 0.5) * vec3(0.03, 0.02, 0.03);
// Narrow ring-directed cone
float phi = n.x * 6.28318530718;
float vtilt = mix(0.02, -0.28, n.y);
vec3 dir = normalize(vec3(cos(phi), vtilt, sin(phi)));
float spd = mix(2.0, 4.5, n.z);
float drag = exp(-life * 1.0);
vec3 gravity = vec3(0.0, -1.8, 0.0);
vec3 p0 = pStart + dir * spd * life * drag + 0.5 * gravity * (life * life);
for (int j = 0; j < NEEDLES_PER_EVENT; j++) {
float fj = float(j);
vec3 n2 = hash31(fi * 37.0 + fj * 17.0 + ft * 23.0);
// Slight spread per-needle
float spread = mix(0.06, 0.18, n2.y);
vec3 d = normalize(dir + (n2 - 0.5) * vec3(spread, spread * 0.5, spread));
// Draw a short segment along direction as multiple bright points
for (int s = 0; s < NEEDLE_SAMPLES; s++) {
float fs = float(s) / float(NEEDLE_SAMPLES - 1);
float seg = mix(0.0, 0.28, fs);
vec3 ps = p0 + d * seg;
float sSize = mix(4.8, 1.0, fs) * SPARK_SIZE_MULT;
// Hot white-y color
vec3 c = vec3(1.0, 0.95, 0.82);
// Slight extra boost for needles
col += sparkle(r, ps, sSize, c) * 1.15;
}
}
}
return col;
}
vec3 Rainbow(vec3 c) {
float t=iTime;
c += sin(vec3(.4, .3, .3)*t + vec3(1.1244,3.43215,6.435))*vec3(.4, .1, .5);
return c;
}
void main() {
vec2 uv = vUv - 0.5;
uv.y *= iResolution.y/iResolution.x;
float scaledTime = iTime * 1.5;
float currentBurnY = BURN_START_Y - scaledTime * BURN_SPEED;
float turn = iTime * 0.1;
float s = sin(turn);
float c = cos(turn);
mat3 rot = mat3(c, 0., s, 0., 1., 0., s, 0., -c);
float CAMERA_DISTANCE = mix(MIN_CAM_DISTANCE, MAX_CAM_DISTANCE, sin(iTime*.0765)*.5+.5);
vec3 pos = vec3(0., 0.0, -CAMERA_DISTANCE) * rot;
float lookY = clamp(currentBurnY - 1.0, -1.0, 1.0);
CameraSetup(uv, pos, vec3(0., lookY, 0.), 1.);
float dist;
vec4 stick = render(cam.ray.o, cam.ray.d, dist, currentBurnY);
dist += .08;
vec3 col = vec3(0.);
if (stick.w > 0.0) {
col = stick.rgb;
}
vec3 spk = sparkles(cam.ray, scaledTime);
vec3 spl = splatterSparks(cam.ray, scaledTime);
vec3 ndl = needleJets(cam.ray, scaledTime, currentBurnY);
vec3 smk = smokeClouds(cam.ray, scaledTime, currentBurnY);
col += spk + spl + ndl + smk;
float alpha = max(stick.w, min(1.0, length(spk + spl + ndl + smk) * 1.5));
gl_FragColor = vec4(col, alpha);
}
`;
const sparklerVertexShader = `
varying vec2 vUv;
void main() {
vUv = uv;
// Full Screen Quad
gl_Position = vec4(position.xy, 0.0, 1.0);
}
`;
// --- 3. Class Definitions ---
class FireworkTrailSystem {
constructor(scene, texture) {
this.maxParticles = 10000;
this.geometry = new THREE.BufferGeometry();
this.positions = new Float32Array(this.maxParticles * 3);
this.colors = new Float32Array(this.maxParticles * 3);
this.opacities = new Float32Array(this.maxParticles);
this.sizes = new Float32Array(this.maxParticles);
this.count = 0;
this.index = 0;
this.geometry.setAttribute('position', new THREE.BufferAttribute(this.positions, 3));
this.geometry.setAttribute('color', new THREE.BufferAttribute(this.colors, 3));
this.geometry.setAttribute('alpha', new THREE.BufferAttribute(this.opacities, 1));
this.geometry.setAttribute('size', new THREE.BufferAttribute(this.sizes, 1));
const material = new THREE.ShaderMaterial({
uniforms: { pointTexture: { value: texture } },
vertexShader: `
attribute float alpha;
attribute float size;
varying vec3 vColor;
varying float vAlpha;
void main() {
vColor = color;
vAlpha = alpha;
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
gl_PointSize = size * (300.0 / -mvPosition.z);
gl_Position = projectionMatrix * mvPosition;
}
`,
fragmentShader: `
uniform sampler2D pointTexture;
varying vec3 vColor;
varying float vAlpha;
void main() {
gl_FragColor = vec4(vColor, vAlpha);
gl_FragColor = gl_FragColor * texture2D(pointTexture, gl_PointCoord);
}
`,
blending: THREE.AdditiveBlending,
depthWrite: false,
transparent: true,
vertexColors: true
});
this.mesh = new THREE.Points(this.geometry, material);
this.mesh.frustumCulled = false;
scene.add(this.mesh);
}
add(position, color, size = 1.0) {
const i = this.index;
const idx3 = i * 3;
this.positions[idx3] = position.x;
this.positions[idx3 + 1] = position.y;
this.positions[idx3 + 2] = position.z;
this.colors[idx3] = color.r;
this.colors[idx3 + 1] = color.g;
this.colors[idx3 + 2] = color.b;
this.opacities[i] = 1.0;
this.sizes[i] = size;
this.index = (this.index + 1) % this.maxParticles;
if (this.count < this.maxParticles) this.count++;
}
update() {
for (let i = 0; i < this.maxParticles; i++) {
if (this.opacities[i] > 0) {
this.opacities[i] *= 0.94;
if (this.opacities[i] < 0.01) this.opacities[i] = 0;
}
}
this.geometry.attributes.position.needsUpdate = true;
this.geometry.attributes.color.needsUpdate = true;
this.geometry.attributes.alpha.needsUpdate = true;
this.geometry.attributes.size.needsUpdate = true;
}
}
class SparklerSystem {
constructor(scene, camera) {
this.scene = scene;
this.camera = camera;
this.mesh = null;
this.active = false;
this.startTime = 0;
// Duration roughly calculated:
// Start Y=2.2, Speed 0.25 (scaled by 1.5 in shader = 0.375 eff).
// Distance to cover ~5 units. Time ~ 14s. Safe 16s.
this.duration = 16000;
this.soundInterval = null;
this.burnSound = null;
const geometry = new THREE.PlaneGeometry(2, 2);
const material = new THREE.ShaderMaterial({
uniforms: {
iTime: { value: 0 },
iResolution: { value: new THREE.Vector2(window.innerWidth, window.innerHeight) }
},
vertexShader: sparklerVertexShader,
fragmentShader: sparklerFragmentShader,
transparent: true,
depthTest: false,
depthWrite: false,
blending: THREE.AdditiveBlending
});
this.mesh = new THREE.Mesh(geometry, material);
this.mesh.renderOrder = 9999;
this.mesh.frustumCulled = false;
this.mesh.visible = false;
scene.add(this.mesh);
}
start() {
if (this.active) return;
this.active = true;
this.mesh.visible = true;
this.startTime = performance.now();
this.mesh.material.uniforms.iTime.value = 0;
// Lower pixel ratio during sparkler to reduce GPU load
if (typeof renderer !== 'undefined' && renderer) {
this._prevPixelRatio = (typeof renderer.getPixelRatio === 'function') ? renderer.getPixelRatio() : Math.min(window.devicePixelRatio, 1);
renderer.setPixelRatio(0.75);
}