Repository navigation
Expand file tree
/
Copy pathcaustics.html
More file actions
219 lines (194 loc) · 6.39 KB
/
Copy pathcaustics.html
File metadata and controls
219 lines (194 loc) · 6.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Caustics</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
background: #111;
display: flex;
flex-direction: column;
align-items: center;
padding: 24px;
gap: 16px;
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
min-height: 100vh;
}
canvas {
max-width: 100%;
border-radius: 10px;
box-shadow: 0 12px 48px rgba(0,0,0,0.5);
}
.controls {
display: flex;
gap: 10px;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
button {
padding: 8px 16px;
border-radius: 8px;
border: none;
background: #007AFF;
color: #fff;
font-size: 13px;
font-weight: 500;
cursor: pointer;
transition: background 0.1s;
}
button:hover { background: #0066dd; }
button.ghost { background: #2c2c2e; color: #ccc; }
button.ghost:hover { background: #3a3a3c; }
.seg {
display: flex;
background: #2c2c2e;
border-radius: 8px;
padding: 3px;
gap: 2px;
}
.seg button {
background: transparent;
color: #666;
border-radius: 6px;
padding: 6px 14px;
font-size: 12px;
font-weight: 500;
}
.seg button.active { background: #444; color: #fff; }
label {
color: #888;
font-size: 12px;
display: flex;
align-items: center;
gap: 8px;
}
input[type=range] { accent-color: #007AFF; width: 100px; }
.code {
font-family: "SF Mono", monospace;
font-size: 11px;
color: #555;
background: #1a1a1a;
padding: 10px 14px;
border-radius: 8px;
max-width: 700px;
word-break: break-all;
text-align: center;
}
</style>
</head>
<body>
<canvas id="c" width="800" height="500"></canvas>
<div class="controls">
<div class="seg" id="colorSeg">
<button class="active" data-color="white">white</button>
<button data-color="cyan">cyan</button>
<button data-color="gold">gold</button>
<button data-color="blue">blue</button>
</div>
<label>opacity <input type="range" id="opacity" min="0" max="1" step="0.01" value="1"></label>
<label>speed <input type="range" id="speed" min="0.1" max="3" step="0.05" value="1"></label>
<button class="ghost" id="pauseBtn">pause</button>
</div>
<div class="code">vec2 p=FC.xy/r.y*2e1+t;for(float i;i++<8.;)p+=sin(p+t/.2+i)*.4,p*=mat2(6,-8,8,6)/9.;o=vec4(tanh(length(fwidth(sin(p*.3)/.1))),texture(b,FC.xy/r));</div>
<script>
const canvas = document.getElementById('c');
const gl = canvas.getContext('webgl2');
if (!gl) { canvas.replaceWith(Object.assign(document.createElement('p'), { textContent: 'WebGL2 not supported.', style: 'color:#f66' })); }
// ── shaders ───────────────────────────────────────────────────────────────────
const VERT = `#version 300 es
in vec2 a_pos;
void main() { gl_Position = vec4(a_pos, 0.0, 1.0); }`;
const FRAG = `#version 300 es
precision highp float;
uniform vec2 u_res;
uniform float u_time;
uniform vec3 u_tint;
uniform float u_opacity;
out vec4 fragColor;
void main() {
vec2 p = gl_FragCoord.xy / u_res.y * 20.0 + u_time;
for (float i = 0.0; i++ < 8.0;) {
p += sin(p + u_time / 0.2 + i) * 0.4;
p *= mat2(6.0, -8.0, 8.0, 6.0) / 9.0;
}
float L = tanh(length(fwidth(sin(p * 0.3) / 0.1)));
fragColor = vec4(u_tint * L, u_opacity);
}`;
function compile(type, src) {
const s = gl.createShader(type);
gl.shaderSource(s, src);
gl.compileShader(s);
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS))
console.error(gl.getShaderInfoLog(s));
return s;
}
const prog = gl.createProgram();
gl.attachShader(prog, compile(gl.VERTEX_SHADER, VERT));
gl.attachShader(prog, compile(gl.FRAGMENT_SHADER, FRAG));
gl.linkProgram(prog);
gl.useProgram(prog);
// fullscreen quad
const buf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1, 1,-1, -1,1, 1,1]), gl.STATIC_DRAW);
const loc = gl.getAttribLocation(prog, 'a_pos');
gl.enableVertexAttribArray(loc);
gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
const uRes = gl.getUniformLocation(prog, 'u_res');
const uTime = gl.getUniformLocation(prog, 'u_time');
const uTint = gl.getUniformLocation(prog, 'u_tint');
const uOpacity = gl.getUniformLocation(prog, 'u_opacity');
// ── state ─────────────────────────────────────────────────────────────────────
const TINTS = {
white: [1.0, 1.0, 1.0],
cyan: [0.2, 0.9, 1.0],
gold: [1.0, 0.82, 0.3],
blue: [0.3, 0.55, 1.0],
};
let tint = TINTS.white;
let opacity = 1.0;
let speed = 1.0;
let paused = false;
let elapsed = 0.0;
let lastTS = null;
// ── controls ──────────────────────────────────────────────────────────────────
document.getElementById('colorSeg').addEventListener('click', e => {
const btn = e.target.closest('button[data-color]');
if (!btn) return;
document.querySelectorAll('#colorSeg button').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
tint = TINTS[btn.dataset.color];
});
document.getElementById('opacity').addEventListener('input', e => {
opacity = parseFloat(e.target.value);
});
document.getElementById('speed').addEventListener('input', e => {
speed = parseFloat(e.target.value);
});
document.getElementById('pauseBtn').addEventListener('click', e => {
paused = !paused;
e.target.textContent = paused ? 'resume' : 'pause';
if (!paused) lastTS = null;
});
// ── render loop ───────────────────────────────────────────────────────────────
function frame(ts) {
if (!paused) {
if (lastTS !== null) elapsed += (ts - lastTS) * 0.001 * speed;
lastTS = ts;
} else {
lastTS = null;
}
gl.viewport(0, 0, canvas.width, canvas.height);
gl.uniform2f(uRes, canvas.width, canvas.height);
gl.uniform1f(uTime, elapsed);
gl.uniform3fv(uTint, tint);
gl.uniform1f(uOpacity, opacity);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
</script>
</body>
</html>