-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
320 lines (256 loc) · 11.7 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
<!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="#00FFFF" id="backgroundColorInput">
</div>
<button id="helpButton">Help</button>
<a href="https://github.com/arifr1234/implicit-equation-plotter" 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>VAL R(VAL3 s)</code> - the implicit equation is: <code>s(x, y, z) == 0</code></li>
</ul>
<h2>Flags and globals</h2>
<ul>
<li><code>time</code> : time in seconds</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");
const backgroundColorInput = document.getElementById("backgroundColorInput");
canvasContainer.style.backgroundColor = backgroundColorInput.value;
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="seifertSurface" data-name="Seifert Surface">
#define DISABLE_STOCHASTIC
VAL dc_atan2(VAL2 v)
{
return VAL(atan(v[1].x, v[0].x), determinant(v) / dc_absSq(v));
}
VAL2 dc_pow(VAL2 z, float p)
{
VAL angle = dc_atan2(z);
VAL r = d_sqrt(dc_absSq(z));
angle = p * angle;
return mul(d_exp(d_log(r) * p), VAL2(d_cos(angle), d_sin(angle)));
}
VAL2 dc_exp(VAL2 z)
{
return mul(d_exp(z[0]), VAL2(d_cos(z[1]), d_sin(z[1])));
}
VAL seifertR(VAL2 z, VAL2 w)
{
// arg(z) = -i * ln(z / |z|)
// arg(z) = c
// -i * ln(z / |z|) = c
// ln(z / |z|) = ic
// z / |z| = e^(ic)
VAL2 sum = dc_pow(z, 4.) + dc_pow(w, 5.);
float theta = (0.7 * 0.5 * cos(time) + 1.) * 3.14;
VAL2 target = VAL2(c(cos(theta)), c(sin(theta)));
return dc_absSq(div(sum, d_sqrt(dc_absSq(sum))) - target);
}
VAL R(VAL3 s) // == 0
{
VAL sqS = d_sq(s[0]) + d_sq(s[1]) + d_sq(s[2]);
VAL denom = sqS + c(1);
return seifertR(VAL2(div(sqS - c(1), denom), div(2.*s[0], denom)), VAL2(div(2.*s[1], denom), div(2.*s[2], denom)));
}
</script>
<script type="x-shader/fragment" id="sphere" data-name="Sphere">
#define DISABLE_STOCHASTIC
VAL R(VAL3 s) // == 0
{
// (X^2 + Y^2 + Z^2 - 1) == 0
return d_sq(s[0]) + d_sq(s[1]) + d_sq(s[2]) - c(1);
}
</script>
<script type="x-shader/fragment" id="torus" data-name="Torus">
#define DISABLE_STOCHASTIC
VAL R(VAL3 s) // == 0
{
return d_sq(d_sqrt(d_sq(s[0]) + d_sq(s[1])) - c(2.)) + d_sq(s[2]) - c(1);
}
</script>
<script>
function setCode(code)
{
editor.setValue(code);
setImplicitEquation(code);
}
loadShaders.then(() => {
const surfaceToLoad = "seifertSurface";
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>