-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
444 lines (351 loc) · 15.1 KB
/
index.html
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
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0;
}
#rightBar
{
position: relative;
height: 100vh;
overflow: auto;
}
#canvasContainer
{
display: flex;
align-items: center;
height: 100%;
}
canvas {
flex-shrink: 1;
flex-grow: 1;
width: 100%;
height: 100%;
object-fit: contain;
}
#editor {
height: 80vh;
}
#controls
{
padding: 5px;
display: flex;
}
#controls > *
{
margin: 5px;
}
</style>
</head>
<body>
<div style="width: 100vw; height: 100vh; overflow: hidden;">
<div id="leftBar" style="width: 400px; height: 100vh; float: left; display: flex;">
<div style="height: 100vh; flex-grow: 1; width: 0px;">
<div id="editor">
vec2 func(int a, foo b)
{
return a(abs(a), dot(vec2(1, 1), vec2(5, 2)));
}
</div>
<div id="controls" style="overflow-x: auto;">
<button id="plotButton" style="width: 50px; height: 50px;">Plot!</button>
<select id="exampleSelect">
<option value="">Examples</option>
</select>
<div>
<span style="float: left;">background color: </span>
<input type="color" value="#FFFFFF" id="backgroundColorInput">
</div>
<button id="helpButton">Help</button>
<a href="https://github.com/arifr1234/webglParametricSurfacePlotter" target="_blank">GitHub</a>
</div>
</div>
<a id="dragBar" style="cursor:col-resize; width: 5px; height: 100vh; background-color: gray; display: block;"></a>
</div>
<div id="rightBar">
<div id="canvasContainer">
<canvas id="c"></canvas>
</div>
<pre id="GlslErrors" style="position: absolute; top: 0px; left: 0px; margin: 10px; padding: 3px; background-color: white; color: red; border-style: solid; visibility: hidden;"></pre>
<div id="help" style="position: absolute; top: 0px; left: 0px; margin: 10px; padding: 3px; border-style: solid; background-color: white; display: none;">
<h1>Help</h1>
<li>Language: GLSL 3.00</li>
<h2>Simple operations</h2>
<ul>
<!-- ace_support ace_type -->
<li><code>c(a)</code> : constant (for instance: <code>c(1.5)</code>, <code>c(time)</code>)</li>
<li><code>a + b</code> : addition</li>
<li><code>a - b</code> : subtraction</li>
<li><code>mul(a, b)</code> : multiplication (element wise)</li>
<li><code>div(a, b)</code> : division (element wise)</li>
<li><code>recip(b)</code> : reciprocal (1/b)</li>
</ul>
<h2>Other functions</h2>
<ul>
<li><code>d_sq(a)</code> : squared (a*a)</li>
<li><code>d_sqrt(a)</code> : square root</li>
<li><code>d_sin(a)</code></li>
<li><code>d_cos(a)</code></li>
<li><code>d_exp(a)</code></li>
<li><code>d_log(a)</code></li>
</ul>
<h2>Complex functions</h2>
<ul>
<li><code>dc_mul(a, b)</code></li>
<li><code>dc_sq(z)</code></li>
<li><code>dc_conj(z)</code> : conjugate</li>
<li><code>dc_absSq(z)</code> : Re(z)^2 + Im(z)^2</li>
<li><code>dc_recip(z)</code> : 1/z</li>
</ul>
<h2>Vector functions</h2>
<ul>
<li><code>d_lenSq(v)</code> : x^2 + y*2 + z^2</li>
<li><code>d_cross(a, b)</code> : cross product</li>
</ul>
<h2>Types and variables</h2>
<ul>
<li><code>VAL</code> : number (replaces float)</li>
<li><code>VAL2</code> : 2 dimensional vector (replaces vec2)</li>
<li><code>VAL3</code> : 3 dimensional vector (replaces vec3)</li>
</ul>
<h2>Main function</h2>
<ul>
<li><code>VAL3 surface(VAL2 ts)</code> - the parametric function</li>
<li><code>bool inRange(vec2 ts)</code> - return true if inside the parameter domain</li>
<li><code>vec3 shading(vec3 normal, vec2 ts, vec3 pos)</code></li>
</ul>
<h2>Flags and globals</h2>
<ul>
<li><code>time</code> : time in seconds</li>
<li><code>#define DEFAULT_SHADING</code> - the shading function will be <pre>vec3 shading(vec3 normal, vec2 ts, vec3 pos)
{
return vec3(dot(normal, vec3(1, 0, 0)) / 2. + 0.5);
}</pre></li>
<li><code>#define DISABLE_STOCHASTIC</code> : runs slower but more accurately</li>
</ul>
<p><b>NOTE: don't add <code>float</code>s with <code>VAL</code>s.</b></p>
<p>You can however multiply <code>float</code>s with <code>VAL</code>s (or with <code>VAL2</code> or <code>VAL3</code>)</p>
<h2>Advanced use/Under The Hood</h2>
<p>The <code>VAL</code> type and the custom functions are used in order to calculate the surface function derivatives (Jacobian) efficiently and accurately, using AutoDiff.</p>
<p>AutoDiff requires every variable to contain the value and the derivatives, and each operation/function to calculate both the value and the derivatives based on the previous ones.</p>
<p>Therefore <code>VAL</code> is a glsl <code>vec3</code> under the hood, where the x component is the value, the y component is the derivative in terms of the first parameter (parameter to the <code>surface</code> function), and z component is the derivative in terms of the second parameter.</p>
<p><code>VAL2</code> and <code>VAL3</code> are <code>mat2x3</code> and <code>mat3x3</code> respectively.</p>
<h2>Custom function definition example</h2>
<pre>VAL d_cosh(VAL a) { return VAL(cosh(a.x), a.yz * sinh(a.x)); }
VAL d_sinh(VAL a) { return VAL(sinh(a.x), a.yz * cosh(a.x)); }
</pre>
</div>
<script>
const helpButton = document.getElementById("helpButton");
const help = document.getElementById("help");
let visibility = false;
helpButton.addEventListener("click", (e) => {
help.style.display = visibility ? "none" : "block";
visibility = !visibility;
});
</script>
</div>
<script>
const left = document.getElementById('leftBar');
const bar = document.getElementById('dragBar');
//script assuming leftBar doesn't have padding.
function dragB(e) {
let width = e.pageX + bar.offsetWidth / 2;
left.style.width = width + 'px';
editor.resize();
}
bar.addEventListener('mousedown', () => {
document.addEventListener('mousemove', dragB);
});
document.addEventListener('mouseup', () => {
document.removeEventListener('mousemove', dragB);
});
const canvasContainer = document.getElementById("canvasContainer");
document.getElementById("backgroundColorInput").addEventListener("change", e => {
canvasContainer.style.backgroundColor = e.target.value;
});
</script>
</div>
<label id="fps" style="position: absolute; top: 30px; right: 50%; color: white; background-color: black; padding: 5px; visibility: hidden;">fps</label>
<script src="https://ajaxorg.github.io/ace-builds/src-noconflict/ace.js"></script>
<script src="embeddedEditor.js"></script>
<script src="https://twgljs.org/dist/4.x/twgl-full.min.js"></script>
<script id="vs" type="x-shader/vertex">#version 300 es
in vec3 position;
void main() {
gl_Position = vec4( position, 1.0 );
}
</script>
<script src="graphics.js"></script>
<script type="x-shader/fragment" id="boySurface" data-name="Boy's surface">
VAL3 surface(VAL2 s)
{
const float sqrt5 = sqrt(5.);
VAL2 s2 = dc_sq(s);
VAL2 s3 = dc_mul(s2, s);
VAL2 s4 = dc_sq(s2);
VAL2 s6 = dc_sq(s3);
const VAL2 one = VAL2(c(1), c(0));
VAL2 denominator = dc_recip(s6 + sqrt5*s3 - one);
VAL3 g = VAL3(
-1.5 * dc_mul(dc_mul(s, one - s4), denominator)[1],
-1.5 * dc_mul(dc_mul(s, one + s4), denominator)[0],
dc_mul(one + s6, denominator)[1] - c(0.5)
);
VAL normalFactor = recip(d_sq(g[0]) + d_sq(g[1]) + d_sq(g[2]));
return VAL3(mul(g[0], normalFactor), mul(g[1], normalFactor), mul(g[2], normalFactor) + c(0.75));
}
bool inRange(vec2 ts)
{
return sq(ts.x) + sq(ts.y) <= 1.;
}
#define DEFAULT_SHADING
</script>
<script type="x-shader/fragment" id="torusSurface" data-name="Torus">
VAL3 surface(VAL2 s)
{
return VAL3(mul(c(1) + 0.5 * d_cos(s[1]), d_cos(s[0])),
mul(c(1) + 0.5 * d_cos(s[1]), d_sin(s[0])),
0.5 * d_sin(s[1]));
}
bool inRange(vec2 ts)
{
// return 0. <= ts.x && ts.x < TAU && 0. <= ts.y && ts.y < TAU;
return true;
}
#define DEFAULT_SHADING
</script>
<script type="x-shader/fragment" id="torus2Surface" data-name="Dancing torus">
VAL3 surface(VAL2 s)
{
float t = 0.6 * time;
VAL r0 = c(1. - 0.3*sin(t));
float r1 = 0.5 + 0.25*cos(t);
return VAL3(mul(r0 + r1 * d_cos(s[1]), d_cos(s[0])),
mul(r0 + r1 * d_cos(s[1]), d_sin(s[0])),
0.5 * d_sin(s[1]));
}
bool inRange(vec2 ts)
{
// return 0. <= ts.x && ts.x < TAU && 0. <= ts.y && ts.y < TAU;
return true;
}
#define DEFAULT_SHADING
</script>
<script type="x-shader/fragment" id="torusknotSurface" data-name="Torus knot">
VAL3 surface(VAL2 s)
{
VAL r0 = c(1.);
float r1 = 0.5;
float p = 1.;
float q = 1.;
switch((int(time) / 4) % 3)
{
case 0:
p = 3.;
q = 5.;
break;
case 1:
p = 9.;
q = 7.;
break;
case 2:
p = 2.;
q = 3.;
break;
}
// s[0] += 0.1 * c(time);
VAL t = 0.2 * c(time);
VAL R = r0 + r1 * d_cos(q * s[0] + t);
VAL2 pv = VAL2(d_cos(p * s[0]),
d_sin(p * s[0]));
VAL2 R_pv = mul(R, pv);
VAL3 curv = VAL3(R_pv[0], R_pv[1],
r1 * d_sin(q * s[0] + t));
VAL R_der = -r1 * q * d_sin(q * s[0] + t);
VAL2 pv_der = VAL2(-p * d_sin(p * s[0]),
p * d_cos(p * s[0]));
VAL2 pv_der_R = mul(pv_der, R);
VAL2 R_pv_der = mul(R_der, pv) + pv_der_R;
VAL3 curv_der = VAL3(R_pv_der[0], R_pv_der[1],
r1 * q * d_cos(q * s[0] + t));
curv_der = div(curv_der, d_sqrt(d_lenSq(curv_der)));
VAL3 torus_der = VAL3(pv_der_R[0], pv_der_R[0], c(0.));
// torus_der = div(torus_der, d_sqrt(d_lenSq(torus_der)));
VAL3 right = d_cross(torus_der, curv_der);
right = div(right, d_sqrt(d_lenSq(right)));
VAL3 up = d_cross(right, curv_der);
// up = div(up, d_sqrt(d_lenSq(up)));
return curv + 0.1 * (mul(d_cos(s[1]), right) + mul(d_sin(s[1]), up));
}
bool inRange(vec2 ts)
{
// return 0. <= ts.x && ts.x < TAU && 0. <= ts.y && ts.y < TAU;
return true;
}
vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
vec3 shading(vec3 normal, vec2 ts, vec3 pos)
{
float light = 0.5 * dot(normal, vec3(1, 0, 0)) + 0.5;
return light * hsv2rgb(vec3(fract(5. * ts.x / TAU - 0.2 * time), 1, 1));
}
</script>
<script type="x-shader/fragment" id="BreatherSurface" data-name="Breather surface">
#define DISABLE_STOCHASTIC
VAL d_cosh(VAL a) { return VAL(cosh(a.x), a.yz * sinh(a.x)); }
VAL d_sinh(VAL a) { return VAL(sinh(a.x), a.yz * cosh(a.x)); }
VAL3 surface(VAL2 s)
{
float aa = (mod(floor(time / 5.), 3.) + 2.) / 5.;
float wsqr = 1. - sq(aa);
float w = sqrt(wsqr);
VAL v = s[0];
VAL u = s[1];
VAL denom = aa * (d_sq(w * d_cosh(aa * u)) + d_sq(aa * d_sin(w * v)));
return VAL3(-u, c(0), c(0)) +
div(VAL3(
2. * wsqr * mul(d_cosh(aa * u), d_sinh(aa * u)),
2. * w * mul(d_cosh(aa * u), -(w * mul(d_cos(v), d_cos(w * v))) - mul(d_sin(v), d_sin(w * v))),
2. * w * mul(d_cosh(aa * u), -(w * mul(d_sin(v), d_cos(w * v))) + mul(d_cos(v), d_sin(w * v)))), denom);
}
bool inRange(vec2 ts)
{
return 0. <= ts.x && ts.x < 150. && -14. <= ts.y && ts.y < 14.;
}
#define DEFAULT_SHADING
</script>
<script>
function setCode(code)
{
editor.setValue(code);
setParametricEquation(code);
}
loadShaders.then(() => {
const surfaceToLoad = "BreatherSurface"; // "boySurface"
setCode(document.getElementById(surfaceToLoad).innerHTML);
});
document.getElementById("plotButton").addEventListener("click", function(e)
{
setCode(editor.getValue());
});
exampleSelect = document.getElementById("exampleSelect");
document.querySelectorAll("script[data-name]").forEach(e => {
let op = document.createElement("option");
op.value = e.id;
op.innerHTML = e.dataset.name;
exampleSelect.appendChild(op);
});
exampleSelect.addEventListener("change", e =>
{
setCode(document.getElementById(e.target.value).innerHTML);
e.target.value = "";
});
</script>
</body>
</html>