-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
804 lines (716 loc) · 33.6 KB
/
Copy pathindex.html
File metadata and controls
804 lines (716 loc) · 33.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Coloring Book Sky</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; background: #1a1040; }
canvas { display: block; width: 100%; height: 100%; }
/* Debug panel — toggle with 'D' key */
#debug-panel {
display: none; position: fixed; top: 12px; right: 12px; width: 320px;
background: rgba(10, 10, 25, 0.92); border: 1px solid #333; border-radius: 8px;
padding: 14px; z-index: 1000; font: 12px/1.6 'SF Mono', 'Menlo', monospace; color: #aaa;
backdrop-filter: blur(8px); max-height: 90vh; overflow-y: auto;
}
#debug-panel.visible { display: block; }
#debug-panel h3 { color: #ff6b9d; font-size: 11px; text-transform: uppercase; letter-spacing: 1px; margin: 12px 0 6px; }
#debug-panel h3:first-child { margin-top: 0; }
#debug-panel .row { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
#debug-panel .row label { width: 120px; font-size: 11px; color: #888; }
#debug-panel .row input[type="range"] { flex: 1; accent-color: #ff6b9d; }
#debug-panel .row .val { width: 40px; text-align: right; color: #fff; font-size: 11px; }
#debug-panel .hint { font-size: 10px; color: #555; margin-top: 8px; }
</style>
</head>
<body>
<!-- Debug panel (press D to toggle) -->
<div id="debug-panel">
<h3>Vibrancy</h3>
<div class="row"><label>Saturation</label><input type="range" id="d-vibrance" min="50" max="180" value="117"><span class="val" id="v-vibrance">1.17</span></div>
<h3>Color Temperature</h3>
<div class="row"><label>Warm R</label><input type="range" id="d-warmR" min="90" max="115" value="96"><span class="val" id="v-warmR">0.96</span></div>
<div class="row"><label>Cool R</label><input type="range" id="d-coolR" min="80" max="105" value="89"><span class="val" id="v-coolR">0.89</span></div>
<div class="row"><label>Warm B</label><input type="range" id="d-warmB" min="75" max="105" value="87"><span class="val" id="v-warmB">0.87</span></div>
<div class="row"><label>Cool B</label><input type="range" id="d-coolB" min="90" max="120" value="103"><span class="val" id="v-coolB">1.03</span></div>
<h3>Glow Pulse</h3>
<div class="row"><label>Max Intensity</label><input type="range" id="d-glowMax" min="0" max="20" value="16"><span class="val" id="v-glowMax">0.16</span></div>
<div class="row"><label>Center X</label><input type="range" id="d-glowX" min="0" max="100" value="61"><span class="val" id="v-glowX">0.61</span></div>
<div class="row"><label>Center Y</label><input type="range" id="d-glowY" min="0" max="100" value="62"><span class="val" id="v-glowY">0.62</span></div>
<div class="row"><label>Spread</label><input type="range" id="d-glowSpread" min="10" max="80" value="52"><span class="val" id="v-glowSpread">5.2</span></div>
<h3>Noise Color Mod</h3>
<div class="row"><label>Intensity</label><input type="range" id="d-noiseAmt" min="0" max="10" value="7"><span class="val" id="v-noiseAmt">0.070</span></div>
<h3>God Rays</h3>
<div class="row"><label>Intensity</label><input type="range" id="d-rayInt" min="0" max="50" value="29"><span class="val" id="v-rayInt">0.29</span></div>
<div class="row"><label>Center X</label><input type="range" id="d-rayX" min="0" max="100" value="56"><span class="val" id="v-rayX">0.56</span></div>
<div class="row"><label>Center Y</label><input type="range" id="d-rayY" min="0" max="100" value="18"><span class="val" id="v-rayY">0.18</span></div>
<div class="row"><label>Orbit Radius X</label><input type="range" id="d-rayOrbitX" min="0" max="30" value="6"><span class="val" id="v-rayOrbitX">0.06</span></div>
<div class="row"><label>Orbit Radius Y</label><input type="range" id="d-rayOrbitY" min="0" max="20" value="5"><span class="val" id="v-rayOrbitY">0.05</span></div>
<h3>Vignette</h3>
<div class="row"><label>Min Opacity</label><input type="range" id="d-vigMin" min="0" max="30" value="6"><span class="val" id="v-vigMin">0.06</span></div>
<div class="row"><label>Max Opacity</label><input type="range" id="d-vigMax" min="0" max="40" value="12"><span class="val" id="v-vigMax">0.12</span></div>
<h3>Cloud Morph</h3>
<div class="row"><label>Amplitude</label><input type="range" id="d-morphAmp" min="0" max="30" value="16"><span class="val" id="v-morphAmp">0.016</span></div>
<div class="row"><label>Speed</label><input type="range" id="d-morphSpd" min="1" max="300" value="59"><span class="val" id="v-morphSpd">0.059</span></div>
<div class="row"><label>Scale</label><input type="range" id="d-morphScl" min="10" max="80" value="30"><span class="val" id="v-morphScl">3.0</span></div>
<h3>Shooting Stars</h3>
<div class="row"><label>Brightness</label><input type="range" id="d-ssOpacity" min="0" max="100" value="70"><span class="val" id="v-ssOpacity">0.70</span></div>
<h3>Star Twinkle</h3>
<div class="row"><label>Speed Min</label><input type="range" id="d-twinkMin" min="1" max="30" value="5"><span class="val" id="v-twinkMin">0.5</span></div>
<div class="row"><label>Speed Max</label><input type="range" id="d-twinkMax" min="5" max="50" value="23"><span class="val" id="v-twinkMax">2.3</span></div>
<div class="hint">Press D to close. Values update live.</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// ============================================================
// STAR DETECTION PARAMS (tuned via star-debug.html)
// ============================================================
const STAR_PARAMS = {
minLuminance: 0.52,
maxSaturation: 0.63,
minLocalContrast: 0.03,
sampleRadius: 20,
scoreThreshold: 0.35,
maskPadding: 10,
};
// ============================================================
// HELPERS
// ============================================================
function smoothstep(edge0, edge1, x) {
const t = Math.max(0, Math.min(1, (x - edge0) / (edge1 - edge0)));
return t * t * (3 - 2 * t);
}
// ============================================================
// STAR DETECTION → BINARY MASK
// ============================================================
function detectStarMask(img) {
const W = img.width, H = img.height, N = W * H;
const canvas = document.createElement('canvas');
canvas.width = W; canvas.height = H;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, W, H);
const px = imageData.data;
const { minLuminance, maxSaturation, minLocalContrast, sampleRadius, scoreThreshold, maskPadding } = STAR_PARAMS;
const r = sampleRadius;
// Luminance + saturation maps
const lumMap = new Float32Array(N);
const satMap = new Float32Array(N);
for (let i = 0; i < N; i++) {
const idx = i * 4;
const rr = px[idx] / 255, gg = px[idx + 1] / 255, bb = px[idx + 2] / 255;
lumMap[i] = 0.2126 * rr + 0.7152 * gg + 0.0722 * bb;
const maxC = Math.max(rr, gg, bb), minC = Math.min(rr, gg, bb);
satMap[i] = maxC > 0 ? (maxC - minC) / maxC : 0;
}
// Integral image
const integral = new Float64Array(N);
for (let y = 0; y < H; y++) {
for (let x = 0; x < W; x++) {
const i = y * W + x;
integral[i] = lumMap[i]
+ (x > 0 ? integral[i - 1] : 0)
+ (y > 0 ? integral[i - W] : 0)
- (x > 0 && y > 0 ? integral[i - W - 1] : 0);
}
}
function boxSum(x0, y0, x1, y1) {
let s = integral[y1 * W + x1];
if (x0 > 0) s -= integral[y1 * W + x0 - 1];
if (y0 > 0) s -= integral[(y0 - 1) * W + x1];
if (x0 > 0 && y0 > 0) s += integral[(y0 - 1) * W + x0 - 1];
return s;
}
// Detect
const rawMask = new Uint8Array(N);
const innerR = Math.max(1, Math.floor(r * 0.3));
for (let y = 0; y < H; y++) {
for (let x = 0; x < W; x++) {
const i = y * W + x;
const lum = lumMap[i];
const lumScore = smoothstep(minLuminance, minLuminance + 0.2, lum);
if (lumScore < 0.01) continue;
const satScore = 1.0 - smoothstep(0.0, maxSaturation, satMap[i]);
if (satScore < 0.01) continue;
const ox0 = Math.max(0, x - r), oy0 = Math.max(0, y - r);
const ox1 = Math.min(W - 1, x + r), oy1 = Math.min(H - 1, y + r);
const ix0 = Math.max(0, x - innerR), iy0 = Math.max(0, y - innerR);
const ix1 = Math.min(W - 1, x + innerR), iy1 = Math.min(H - 1, y + innerR);
const outerSum = boxSum(ox0, oy0, ox1, oy1);
const innerSum = boxSum(ix0, iy0, ix1, iy1);
const outerArea = (ox1 - ox0 + 1) * (oy1 - oy0 + 1);
const innerArea = (ix1 - ix0 + 1) * (iy1 - iy0 + 1);
const ringAvg = (outerArea - innerArea) > 0 ? (outerSum - innerSum) / (outerArea - innerArea) : lum;
const conScore = smoothstep(minLocalContrast, minLocalContrast + 0.1, lum - ringAvg);
if (lumScore * satScore * conScore >= scoreThreshold) rawMask[i] = 1;
}
}
// Dilate
const mask = new Uint8Array(N);
const pad = maskPadding;
if (pad === 0) {
mask.set(rawMask);
} else {
for (let y = 0; y < H; y++) {
for (let x = 0; x < W; x++) {
if (!rawMask[y * W + x]) continue;
const y0 = Math.max(0, y - pad), y1 = Math.min(H - 1, y + pad);
const x0 = Math.max(0, x - pad), x1 = Math.min(W - 1, x + pad);
for (let py = y0; py <= y1; py++) {
for (let px2 = x0; px2 <= x1; px2++) {
const dx = px2 - x, dy = py - y;
if (dx * dx + dy * dy <= pad * pad) mask[py * W + px2] = 1;
}
}
}
}
}
return { mask, px, W, H };
}
// ============================================================
// CONNECTED-COMPONENT LABELING (BFS flood fill)
// Assigns each star blob a unique random phase + speed,
// encoded into a texture: R=phase, G=speed, B=starness
// ============================================================
function buildStarDataTexture(mask, W, H) {
const N = W * H;
const canvas = document.createElement('canvas');
canvas.width = W; canvas.height = H;
const ctx = canvas.getContext('2d');
const imgData = ctx.createImageData(W, H);
const data = imgData.data;
// Fill alpha to 255
for (let i = 0; i < N; i++) data[i * 4 + 3] = 255;
const visited = new Uint8Array(N);
const queue = [];
let starCount = 0;
for (let y = 0; y < H; y++) {
for (let x = 0; x < W; x++) {
const i = y * W + x;
if (!mask[i] || visited[i]) continue;
// New star blob — assign unique random phase and speed
starCount++;
const phase = Math.floor(Math.random() * 256); // 0-255 → maps to 0-2π in shader
const speed = Math.floor(Math.random() * 256); // 0-255 → maps to speed range in shader
// BFS
queue.length = 0;
queue.push(i);
visited[i] = 1;
let head = 0;
while (head < queue.length) {
const ci = queue[head++];
const cx = ci % W;
const cy = (ci - cx) / W;
// Write star data
const oi = ci * 4;
data[oi] = phase; // R = phase
data[oi + 1] = speed; // G = speed
data[oi + 2] = 255; // B = starness (is a star)
// 8-connected neighbors
for (let dy = -1; dy <= 1; dy++) {
for (let dx = -1; dx <= 1; dx++) {
if (dx === 0 && dy === 0) continue;
const nx = cx + dx, ny = cy + dy;
if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
const ni = ny * W + nx;
if (!mask[ni] || visited[ni]) continue;
visited[ni] = 1;
queue.push(ni);
}
}
}
}
}
console.log('[Sky] Connected components:', starCount, 'individual stars found');
ctx.putImageData(imgData, 0, 0);
return canvas;
}
// ============================================================
// BUILD CLEAN SKY (stars replaced with background color)
// ============================================================
function buildCleanSky(mask, px, W, H) {
const N = W * H;
const canvas = document.createElement('canvas');
canvas.width = W; canvas.height = H;
const ctx = canvas.getContext('2d');
const imgData = ctx.createImageData(W, H);
const cd = imgData.data;
// Copy original pixels
for (let i = 0; i < px.length; i++) cd[i] = px[i];
// Replace masked pixels with ring-average from non-masked neighbors
const outerR = STAR_PARAMS.sampleRadius + STAR_PARAMS.maskPadding;
for (let i = 0; i < N; i++) {
if (!mask[i]) continue;
const x = i % W, y = (i - x) / W;
let sumR = 0, sumG = 0, sumB = 0, cnt = 0;
for (let dy = -outerR; dy <= outerR; dy++) {
for (let dx = -outerR; dx <= outerR; dx++) {
const nx = x + dx, ny = y + dy;
if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
const ni = ny * W + nx;
if (mask[ni]) continue; // skip star pixels
sumR += px[ni * 4]; sumG += px[ni * 4 + 1]; sumB += px[ni * 4 + 2];
cnt++;
}
}
if (cnt > 0) {
cd[i * 4] = Math.round(sumR / cnt);
cd[i * 4 + 1] = Math.round(sumG / cnt);
cd[i * 4 + 2] = Math.round(sumB / cnt);
}
}
ctx.putImageData(imgData, 0, 0);
return canvas;
}
// ============================================================
// MAIN
// ============================================================
function main() {
const renderer = new THREE.WebGLRenderer({ antialias: false });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
const skyImg = new Image();
skyImg.onload = () => {
const skyTexture = new THREE.Texture(skyImg);
skyTexture.minFilter = THREE.LinearFilter;
skyTexture.magFilter = THREE.LinearFilter;
skyTexture.needsUpdate = true;
const screenAspect = window.innerWidth / window.innerHeight;
const skyAspect = skyImg.width / skyImg.height;
const skyGeo = new THREE.PlaneGeometry(2, 2);
const skyMat = new THREE.ShaderMaterial({
uniforms: {
uSky: { value: skyTexture },
uSkyClean: { value: skyTexture },
uStarData: { value: null },
uTime: { value: 0.0 },
uScreenAspect: { value: screenAspect },
uImageAspect: { value: skyAspect },
uTwinkleActive: { value: 0.0 },
// Living sky uniforms (computed in JS animate loop)
uTempTint: { value: new THREE.Vector3(1, 1, 1) },
uGlowIntensity: { value: 0.0 },
uVignetteOpacity: { value: 0.09 },
uLightPos: { value: new THREE.Vector2(0.5, 0.25) },
uGodRayIntensity: { value: 0.29 },
uGlowCenter: { value: new THREE.Vector2(0.61, 0.62) },
uGlowSpread: { value: 5.2 },
uNoiseAmount: { value: 0.07 },
uTwinkleSpeedMin: { value: 0.5 },
uTwinkleSpeedRange: { value: 1.8 },
uMorphAmp: { value: 0.017 },
uMorphSpeed: { value: 0.052 },
uMorphScale: { value: 3.0 },
uVibrance: { value: 1.17 },
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = vec4(position.xy, 0.0, 1.0);
}
`,
fragmentShader: `
// === UNIFORMS ===
uniform sampler2D uSky;
uniform sampler2D uSkyClean;
uniform sampler2D uStarData;
uniform float uTime;
uniform float uScreenAspect;
uniform float uImageAspect;
uniform float uTwinkleActive;
uniform vec3 uTempTint;
uniform float uGlowIntensity;
uniform float uVignetteOpacity;
uniform vec2 uLightPos;
uniform float uGodRayIntensity;
uniform vec2 uGlowCenter;
uniform float uGlowSpread;
uniform float uNoiseAmount;
uniform float uTwinkleSpeedMin;
uniform float uTwinkleSpeedRange;
uniform float uMorphAmp;
uniform float uMorphSpeed;
uniform float uMorphScale;
uniform float uVibrance;
varying vec2 vUv;
// === ASHIMA 2D SIMPLEX NOISE ===
vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec2 mod289(vec2 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec3 permute(vec3 x) { return mod289(((x * 34.0) + 1.0) * x); }
float snoise(vec2 v) {
const vec4 C = vec4(0.211324865405187, 0.366025403784439,
-0.577350269189626, 0.024390243902439);
vec2 i = floor(v + dot(v, C.yy));
vec2 x0 = v - i + dot(i, C.xx);
vec2 i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
vec4 x12 = x0.xyxy + C.xxzz;
x12.xy -= i1;
i = mod289(i);
vec3 p = permute(permute(i.y + vec3(0.0, i1.y, 1.0)) + i.x + vec3(0.0, i1.x, 1.0));
vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
m = m * m; m = m * m;
vec3 x = 2.0 * fract(p * C.www) - 1.0;
vec3 h = abs(x) - 0.5;
vec3 ox = floor(x + 0.5);
vec3 a0 = x - ox;
m *= 1.79284291400159 - 0.85373472095314 * (a0*a0 + h*h);
vec3 g;
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * x12.xz + h.yz * x12.yw;
return 130.0 * dot(m, g);
}
// === GOD RAYS ===
vec3 computeGodRays(vec2 uv, vec2 lightPos) {
float t = mod(uTime, 10000.0);
vec2 delta = uv - lightPos;
float dist = length(delta);
vec2 rayDir = delta / max(dist, 0.001);
// Noise mask (once per pixel, not per sample)
float angle = atan(delta.y, delta.x);
float noiseMask = snoise(vec2(angle * 5.0 + t * 0.008, 1.5));
noiseMask = smoothstep(0.2, 0.8, noiseMask * 0.5 + 0.5);
// Radial march toward light (40 samples)
float rayAccum = 0.0;
vec2 sampleUV = uv;
float stepLen = 0.3 / 40.0;
for (int i = 0; i < 40; i++) {
sampleUV -= rayDir * stepLen;
vec2 clamped = clamp(sampleUV, 0.001, 0.999);
vec3 samp = texture2D(uSky, clamped).rgb;
float lum = dot(samp, vec3(0.2126, 0.7152, 0.0722));
// Bright areas pass light, dark areas block
float occlusion = smoothstep(0.42, 0.65, lum);
rayAccum += occlusion;
}
rayAccum /= 40.0;
// Distance falloff
float falloff = 1.0 - smoothstep(0.0, 0.85, dist);
falloff *= falloff;
vec3 rayColor = vec3(1.0, 0.94, 0.85);
return rayColor * rayAccum * noiseMask * falloff * uGodRayIntensity;
}
// === MAIN ===
void main() {
float t = mod(uTime, 10000.0);
// 1. Cover mode UV
vec2 uv = vUv;
if (uScreenAspect > uImageAspect) {
float scale = uImageAspect / uScreenAspect;
uv.y = uv.y * scale + (1.0 - scale) * 0.5;
} else {
float scale = uScreenAspect / uImageAspect;
uv.x = uv.x * scale + (1.0 - scale) * 0.5;
}
// 2. Cloud morph (UV distortion via noise)
vec2 morphUV = uv; // save original for star data lookup
if (uMorphAmp > 0.0) {
float mt = t * uMorphSpeed;
float dx = snoise(uv * uMorphScale + vec2(mt, 0.0));
float dy = snoise(uv * uMorphScale + vec2(0.0, mt) + 50.0);
// Edge fade: reduce distortion near image edges to prevent clamping artifacts
float edgeFade = smoothstep(0.0, 0.05, uv.x) * smoothstep(1.0, 0.95, uv.x)
* smoothstep(0.0, 0.05, uv.y) * smoothstep(1.0, 0.95, uv.y);
uv += vec2(dx, dy) * uMorphAmp * edgeFade;
}
// 3. Sample clean sky at MORPHED UV (star-free sky morphs freely)
vec3 color = texture2D(uSkyClean, uv).rgb;
// 4. Star twinkle — stars composited from ORIGINAL UV (pinned in place)
if (uTwinkleActive > 0.5) {
vec4 sd = texture2D(uStarData, morphUV);
if (sd.b > 0.5) {
float phase = sd.r * 6.2831;
float speed = uTwinkleSpeedMin + sd.g * uTwinkleSpeedRange;
float wave = sin(t * speed + phase);
float twinkle = smoothstep(-0.3, 0.3, wave);
vec3 star = texture2D(uSky, morphUV).rgb; // star from original position
color = mix(color, star, twinkle);
}
}
// 4. Color temperature breathing (uniform from JS)
color *= uTempTint;
// 4b. Vibrancy (saturation adjustment)
float luma = dot(color, vec3(0.2126, 0.7152, 0.0722));
color = mix(vec3(luma), color, uVibrance);
// 5. Noise-based color modulation (spatially varying)
float nt = t * 0.01;
float n1 = snoise(uv * 1.5 + vec2(nt * 0.3, 0.0));
float n2 = snoise(uv * 1.5 + vec2(0.0, nt * 0.4) + 100.0);
float n3 = snoise(uv * 1.5 + vec2(nt * 0.35, nt * 0.2) + 200.0);
color *= vec3(1.0 + n1 * uNoiseAmount, 1.0 + n2 * uNoiseAmount, 1.0 + n3 * uNoiseAmount);
// 6. Atmospheric glow pulse
float glowDist = length(uv - uGlowCenter);
float glowMask = exp(-glowDist * glowDist * uGlowSpread);
color += vec3(1.0, 0.85, 0.72) * glowMask * uGlowIntensity;
// 7. God rays (screen blend)
vec3 rays = computeGodRays(uv, uLightPos);
color = color + rays - color * rays;
// 8. Breathing vignette
vec2 vigUv = vUv - 0.5;
float vigDist = length(vigUv);
float vig = smoothstep(0.6, 1.1, vigDist);
vec3 vigColor = vec3(0.15, 0.08, 0.18);
color = mix(color, vigColor, vig * uVignetteOpacity);
gl_FragColor = vec4(color, 1.0);
}
`,
depthWrite: false,
depthTest: false,
});
const skyMesh = new THREE.Mesh(skyGeo, skyMat);
skyMesh.renderOrder = 0;
scene.add(skyMesh);
// Foreground: Chance cutout
const loader = new THREE.TextureLoader();
const chanceTexture = loader.load('chance-cutout.png', () => {
const chanceAspect = chanceTexture.image.width / chanceTexture.image.height;
const chanceHeight = 1.7;
const chanceWidth = chanceHeight * chanceAspect / screenAspect;
const chanceGeo = new THREE.PlaneGeometry(chanceWidth, chanceHeight);
const chanceMat = new THREE.MeshBasicMaterial({
map: chanceTexture,
transparent: true,
depthWrite: false,
depthTest: false,
});
const chanceMesh = new THREE.Mesh(chanceGeo, chanceMat);
chanceMesh.position.set(0, -1.0 + chanceHeight * 0.5, 0);
chanceMesh.renderOrder = 1;
scene.add(chanceMesh);
// --- Shooting star mesh ---
const SHOOTING_STARS = [
{ startTime: 8, startX: -0.8, startY: 0.2, endX: -0.1, endY: 0.35 },
{ startTime: 28, startX: 0.1, startY: 0.4, endX: 0.85, endY: 0.55 },
{ startTime: 48, startX: -0.5, startY: 0.5, endX: 0.25, endY: 0.65 },
];
const ssGeo = new THREE.PlaneGeometry(0.3, 0.008);
const ssMat = new THREE.ShaderMaterial({
uniforms: {
uOpacity: { value: 0.0 },
uTrailFade: { value: 0.0 },
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform float uOpacity;
uniform float uTrailFade;
varying vec2 vUv;
void main() {
// Taper: bright at right (head), fading left (tail)
float head = smoothstep(0.0, 1.0, vUv.x);
// Soft glow across width
float width = 1.0 - abs(vUv.y - 0.5) * 2.0;
width = pow(max(width, 0.0), 0.5);
// Trail dissolve during linger phase
float trail = smoothstep(uTrailFade, uTrailFade + 0.3, vUv.x);
// Combine
float alpha = head * width * trail * uOpacity;
vec3 color = vec3(1.0, 0.96, 0.90);
gl_FragColor = vec4(color * alpha, alpha);
}
`,
transparent: true,
blending: THREE.AdditiveBlending,
depthWrite: false,
depthTest: false,
});
const ssMesh = new THREE.Mesh(ssGeo, ssMat);
ssMesh.renderOrder = 0.5;
scene.add(ssMesh);
// Render static frame immediately
renderer.render(scene, camera);
console.log('[Sky] Static sky loaded. Computing star detection...');
// Star detection (deferred so page renders first)
setTimeout(() => {
// Step 1: Detect star mask
const { mask, px, W, H } = detectStarMask(skyImg);
// Step 2: Connected-component labeling → per-star phase/speed texture
const starDataCanvas = buildStarDataTexture(mask, W, H);
const starDataTexture = new THREE.Texture(starDataCanvas);
starDataTexture.minFilter = THREE.NearestFilter; // CRITICAL: no interpolation
starDataTexture.magFilter = THREE.NearestFilter; // categorical data, not visual
starDataTexture.needsUpdate = true;
// Step 3: Build clean sky (stars removed)
const cleanSkyCanvas = buildCleanSky(mask, px, W, H);
const cleanTexture = new THREE.Texture(cleanSkyCanvas);
cleanTexture.minFilter = THREE.LinearFilter;
cleanTexture.magFilter = THREE.LinearFilter;
cleanTexture.needsUpdate = true;
// Activate twinkle
skyMat.uniforms.uSkyClean.value = cleanTexture;
skyMat.uniforms.uStarData.value = starDataTexture;
skyMat.uniforms.uTwinkleActive.value = 1.0;
console.log('[Sky] Twinkle active!');
// Debug panel helpers
const dv = (id) => parseInt(document.getElementById(id).value);
const dp = document.getElementById('debug-panel');
window.addEventListener('keydown', (e) => {
if (e.key === 'd' || e.key === 'D') dp.classList.toggle('visible');
});
// Update display values on slider input
dp.querySelectorAll('input[type="range"]').forEach(input => {
input.addEventListener('input', () => {
const id = input.id.replace('d-', 'v-');
const el = document.getElementById(id);
if (!el) return;
// Format based on which slider
if (input.id.includes('warmR') || input.id.includes('coolR') || input.id.includes('warmB') || input.id.includes('coolB'))
el.textContent = (parseInt(input.value) / 100).toFixed(2);
else if (input.id.includes('glowMax'))
el.textContent = (parseInt(input.value) / 100).toFixed(2);
else if (input.id.includes('glowX') || input.id.includes('glowY'))
el.textContent = (parseInt(input.value) / 100).toFixed(2);
else if (input.id.includes('glowSpread'))
el.textContent = (parseInt(input.value) / 10).toFixed(1);
else if (input.id.includes('noiseAmt'))
el.textContent = (parseInt(input.value) / 100).toFixed(3);
else if (input.id.includes('rayInt'))
el.textContent = (parseInt(input.value) / 100).toFixed(2);
else if (input.id.includes('rayX') || input.id.includes('rayY'))
el.textContent = (parseInt(input.value) / 100).toFixed(2);
else if (input.id.includes('rayOrbit'))
el.textContent = (parseInt(input.value) / 100).toFixed(2);
else if (input.id.includes('vigMin') || input.id.includes('vigMax'))
el.textContent = (parseInt(input.value) / 100).toFixed(2);
else if (input.id.includes('twinkMin'))
el.textContent = (parseInt(input.value) / 10).toFixed(1);
else if (input.id.includes('twinkMax'))
el.textContent = (parseInt(input.value) / 10).toFixed(1);
else if (input.id.includes('ssOpacity'))
el.textContent = (parseInt(input.value) / 100).toFixed(2);
else if (input.id.includes('vibrance'))
el.textContent = (parseInt(input.value) / 100).toFixed(2);
else if (input.id.includes('morphAmp'))
el.textContent = (parseInt(input.value) / 1000).toFixed(3);
else if (input.id.includes('morphSpd'))
el.textContent = (parseInt(input.value) / 1000).toFixed(3);
else if (input.id.includes('morphScl'))
el.textContent = (parseInt(input.value) / 10).toFixed(1);
});
});
// Animation loop — timing designed for 60-second seamless loop
function animate(timestamp) {
requestAnimationFrame(animate);
const t = timestamp * 0.001;
skyMat.uniforms.uTime.value = t;
// Read debug values (cheap — just parseInt on cached DOM elements)
const warmR = dv('d-warmR') / 100;
const coolR = dv('d-coolR') / 100;
const warmB = dv('d-warmB') / 100;
const coolB = dv('d-coolB') / 100;
const glowMax = dv('d-glowMax') / 100;
const rayInt = dv('d-rayInt') / 100;
const rayCX = dv('d-rayX') / 100;
const rayCY = dv('d-rayY') / 100;
const rayOrbX = dv('d-rayOrbitX') / 100;
const rayOrbY = dv('d-rayOrbitY') / 100;
const vigMin = dv('d-vigMin') / 100;
const vigMax = dv('d-vigMax') / 100;
// Color temperature breathing (1 full cycle in 60s)
const tempOsc = Math.sin(t * 0.1047) * 0.5 + 0.5;
const tint = skyMat.uniforms.uTempTint.value;
tint.x = warmR + tempOsc * (coolR - warmR);
tint.y = 0.95;
tint.z = warmB + tempOsc * (coolB - warmB);
// Atmospheric glow pulse (2 full cycles in 60s)
const glowOsc = Math.sin(t * 0.2094) * 0.5 + 0.5;
skyMat.uniforms.uGlowIntensity.value = glowOsc * glowMax;
// Breathing vignette (3 full cycles in 60s)
const vigOsc = Math.sin(t * 0.3142) * 0.5 + 0.5;
skyMat.uniforms.uVignetteOpacity.value = vigMin + vigOsc * (vigMax - vigMin);
// God ray light — orbits behind Chance's head
const orbitT = t * 0.1047; // 1 orbit in 60s
const lp = skyMat.uniforms.uLightPos.value;
lp.x = rayCX + Math.cos(orbitT) * rayOrbX;
lp.y = rayCY + Math.sin(orbitT) * rayOrbY;
skyMat.uniforms.uGodRayIntensity.value = rayInt;
// Glow center + spread from debug panel
skyMat.uniforms.uGlowCenter.value.x = dv('d-glowX') / 100;
skyMat.uniforms.uGlowCenter.value.y = dv('d-glowY') / 100;
skyMat.uniforms.uGlowSpread.value = dv('d-glowSpread') / 10;
skyMat.uniforms.uNoiseAmount.value = dv('d-noiseAmt') / 100;
skyMat.uniforms.uTwinkleSpeedMin.value = dv('d-twinkMin') / 10;
skyMat.uniforms.uTwinkleSpeedRange.value = (dv('d-twinkMax') - dv('d-twinkMin')) / 10;
// Vibrancy
skyMat.uniforms.uVibrance.value = dv('d-vibrance') / 100;
// Cloud morph
skyMat.uniforms.uMorphAmp.value = dv('d-morphAmp') / 1000;
skyMat.uniforms.uMorphSpeed.value = dv('d-morphSpd') / 1000;
skyMat.uniforms.uMorphScale.value = dv('d-morphScl') / 10;
// Shooting stars
const loopT = t % 60;
const ssMaxOpacity = dv('d-ssOpacity') / 100;
let ssActive = false;
for (const ss of SHOOTING_STARS) {
const elapsed = loopT - ss.startTime;
if (elapsed < 0 || elapsed > 3.0) continue;
ssActive = true;
const travelDur = 1.5;
const angle = Math.atan2(ss.endY - ss.startY, ss.endX - ss.startX);
if (elapsed < travelDur) {
// Phase 1: Head traveling
const progress = elapsed / travelDur;
const fadeIn = Math.min(1.0, elapsed / 0.3);
const headX = ss.startX + (ss.endX - ss.startX) * progress;
const headY = ss.startY + (ss.endY - ss.startY) * progress;
const cx = (ss.startX + headX) / 2;
const cy = (ss.startY + headY) / 2;
const dx = headX - ss.startX;
const dy = headY - ss.startY;
const len = Math.sqrt(dx * dx + dy * dy);
ssMesh.scale.set(Math.max(0.01, len / 0.3), 1, 1);
ssMesh.position.set(cx, cy, 0);
ssMesh.rotation.z = angle;
ssMat.uniforms.uOpacity.value = fadeIn * ssMaxOpacity;
ssMat.uniforms.uTrailFade.value = 0.0;
} else {
// Phase 2: Trail lingering
const lingerProgress = (elapsed - travelDur) / 1.5;
const cx = (ss.startX + ss.endX) / 2;
const cy = (ss.startY + ss.endY) / 2;
const dx = ss.endX - ss.startX;
const dy = ss.endY - ss.startY;
const fullLen = Math.sqrt(dx * dx + dy * dy);
ssMesh.scale.set(fullLen / 0.3, 1, 1);
ssMesh.position.set(cx, cy, 0);
ssMesh.rotation.z = angle;
ssMat.uniforms.uOpacity.value = ssMaxOpacity * (1.0 - lingerProgress);
ssMat.uniforms.uTrailFade.value = lingerProgress;
}
break;
}
if (!ssActive) ssMat.uniforms.uOpacity.value = 0.0;
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
window.skyReady = true;
}, 50);
// Resize
window.addEventListener('resize', () => {
const newAspect = window.innerWidth / window.innerHeight;
renderer.setSize(window.innerWidth, window.innerHeight);
skyMat.uniforms.uScreenAspect.value = newAspect;
const newChanceWidth = chanceHeight * chanceAspect / newAspect;
chanceMesh.geometry.dispose();
chanceMesh.geometry = new THREE.PlaneGeometry(newChanceWidth, chanceHeight);
});
});
chanceTexture.minFilter = THREE.LinearFilter;
chanceTexture.magFilter = THREE.LinearFilter;
};
skyImg.src = 'sky-panorama.jpg';
}
main();
</script>
</body>
</html>