-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandel.html
343 lines (283 loc) · 8.44 KB
/
mandel.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
<html>
<head>
<style type="text/css">
html, body {margin: 0;}
canvas {width: 100%; height: 100%}
</style>
</head>
<body>
<canvas id="main" width="800" height="450" />
<script language="javascript">
var oversample = 2;
var canvas = document.getElementById("main");
canvas.width = window.innerWidth * oversample;
canvas.height = window.innerHeight * oversample;
var gl = canvas.getContext('webgl');
var WIDTH = canvas.width;
var HEIGHT = canvas.height;
var ITERATIONS = 200;
var THRESHOLD = 3.1;
var INITIAL_VIEW = {x: -2.5, y:-1.5, width:4.0, height:3.0};
var MOUSE_LEFT = 0;
var MOUSE_MIDDLE = 1;
var MOUSE_RIGHT = 2;
/**
* Shaders
*/
// Utility to fail loudly on shader compilation failure
function compileShader(shaderSource, shaderType) {
var shader = gl.createShader(shaderType);
gl.shaderSource(shader, shaderSource);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw "Shader compile failed with: " + gl.getShaderInfoLog(shader);
}
return shader;
}
var vertexShader = compileShader(`
attribute vec2 position;
void main() {
// position specifies only x and y.
// We set z to be 0.0, and w to be 1.0
gl_Position = vec4(position, 0.0, 1.0);
}
`, gl.VERTEX_SHADER);
var fragmentShader = compileShader(`
precision highp float;
uniform vec4 viewport[2];
const float WIDTH = ` + WIDTH + `.0;
const float HEIGHT = ` + HEIGHT + `.0;
const float THRESHOLD = ` + THRESHOLD + `;
const int ITERATIONS = ` + ITERATIONS + `;
vec2 pow2(vec2 z) {
return vec2(z.x*z.x - z.y*z.y, 2.0*z.x*z.y);
}
vec2 iterate(vec2 z, vec2 c) {
return pow2(z) + c;
}
vec2 value(vec2 c) {
vec2 z = vec2(0, 0);
for (int i=1; i <= ITERATIONS; i++) {
z = iterate(z, c);
if (length(z) > THRESHOLD) {
return vec2(i, length(z));
}
}
return vec2(ITERATIONS, length(z));
}
vec2 map_coord(float x, float y) {
float X = viewport[0].x;
float Y = viewport[0].y;
float W = viewport[0].z;
float H = viewport[0].w;
vec2 normalized = vec2(x/WIDTH, y/HEIGHT);
vec2 scaled = vec2(X + W*normalized.x, Y + H*normalized.y);
return scaled;
}
float hue2rgb(float p, float q, float t) {
if(t < 0.0) t += 1.0;
if(t > 1.0) t -= 1.0;
if(t < 1.0/6.0) return p + (q - p) * 6.0 * t;
if(t < 1.0/2.0) return q;
if(t < 2.0/3.0) return p + (q - p) * (2.0/3.0 - t) * 6.0;
return p;
}
vec4 hslToRgb(vec3 hsl) {
// Converted from http://stackoverflow.com/a/9493060/98057
float h = hsl.x;
float s = hsl.y;
float l = hsl.z;
float r;
float g;
float b;
if (s == 0.0) {
r = l;
g = l;
b = l;
} else {
float q = l * (1.0 + s);
if (l >= 0.5) {
q = l + s - l * s;
}
float p = 2.0 * l - q;
r = hue2rgb(p, q, h + 1.0/3.0);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1.0/3.0);
}
return vec4(r, g, b, 1.0);
}
vec4 map_color(vec2 result) {
float escape = result.x;
float len = result.y;
if (escape >= float(ITERATIONS)-0.1) {
return vec4(0.0, 0.0, 0.0, 1.0);
}
float frac_result = escape + 1.0 - log(log(float(len)))/log(2.0);
float val = frac_result / float(ITERATIONS);
return hslToRgb(vec3(val, 1.0, 0.5));
}
vec4 map_grayscale(vec2 result) {
float escape = result.x;
float len = result.y;
if (escape >= float(ITERATIONS)-0.1) {
return vec4(0.0, 0.0, 0.0, 1.0);
}
float frac_result = escape + 1.0 - log(log(float(len)))/log(2.0);
float val = frac_result / float(ITERATIONS);
return vec4(val, val, val, 1.0);
}
vec4 map_cyan(vec2 result) {
float escape = result.x;
float len = result.y;
if (escape >= float(ITERATIONS)-0.1) {
return hslToRgb(vec3(0.6, .9, 0.05));
}
float frac_result = escape + 1.0 - log(log(float(len)))/log(2.0);
float val = frac_result / float(ITERATIONS);
//return hslToRgb(vec3(0.6, .9, 0.05 + .95*val));
return hslToRgb(vec3(0.6, .9, min(1.0, 0.05 + 2.5*val)));
}
void main(){
vec2 test = vec2(1.0,1.0) + vec2(1.0,1.0);
float x = gl_FragCoord.x;
float y = gl_FragCoord.y;
float v = 0.0;
vec2 result = value(map_coord(x, y));
//gl_FragColor = map_grayscale(result);
//gl_FragColor = map_color(result);
gl_FragColor = map_cyan(result);
}
`, gl.FRAGMENT_SHADER);
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
gl.useProgram(program);
/**
* Geometry setup
*/
// Set up 4 vertices, which we'll draw as a rectangle
// via 2 triangles
//
// A---C
// | /|
// | / |
// |/ |
// B---D
//
// We order them like so, so that when we draw with
// gl.TRIANGLE_STRIP, we draw triangle ABC and BCD.
var vertexData = new Float32Array([
-1.0, 1.0, // top left
-1.0, -1.0, // bottom left
1.0, 1.0, // top right
1.0, -1.0, // bottom right
]);
var vertexDataBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexDataBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertexData, gl.STATIC_DRAW);
/**
* Attribute setup
*/
// Utility to complain loudly if we fail to find the attribute
function getAttribLocation(program, name) {
var attributeLocation = gl.getAttribLocation(program, name);
if (attributeLocation === -1) {
throw 'Can not find attribute ' + name + '.';
}
return attributeLocation;
}
// To make the geometry information available in the shader as attributes, we
// need to tell WebGL what the layout of our data in the vertex buffer is.
var positionHandle = getAttribLocation(program, 'position');
gl.enableVertexAttribArray(positionHandle);
gl.vertexAttribPointer(positionHandle,
2, // position is a vec2
gl.FLOAT, // each component is a float
gl.FALSE, // don't normalize values
2 * 4, // two 4 byte float components per vertex
0 // offset into each span of vertex data
);
/**
* Simulation setup
*/
var view = INITIAL_VIEW;
/**
* Uniform setup
*/
// Utility to complain loudly if we fail to find the uniform
function getUniformLocation(program, name) {
var uniformLocation = gl.getUniformLocation(program, name);
if (uniformLocation === -1) {
throw 'Can not find uniform ' + name + '.';
}
return uniformLocation;
}
var mandelHandle = getUniformLocation(program, 'viewport');
/**
* Simulation step, data transfer, and drawing
*/
var step = function() {
var baseIndex = 0;
var dataToSendToGPU = new Float32Array(4);
dataToSendToGPU[baseIndex + 0] = view.x;
dataToSendToGPU[baseIndex + 1] = view.y;
dataToSendToGPU[baseIndex + 2] = view.width;
dataToSendToGPU[baseIndex + 3] = view.height;
console.log("data to send", dataToSendToGPU, view);
gl.uniform4fv(mandelHandle, dataToSendToGPU);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};
step();
function rescale(view, new_view, factor) {
// Scale viewport
new_view.width = factor*view.width;
new_view.height = factor*view.height;
// Move coords according to the scaling
new_view.x += (1-factor)*view.width/2;
new_view.y += (1-factor)*view.height/2;
}
function clone_view() {
return {x: view.x, y: view.y, width: view.width, height: view.height};
}
/**
* Click handler
*/
document.body.addEventListener('mousedown', function (e){
console.log(e);
var x = e.x / window.innerWidth;
var y = -1 * e.y / window.innerHeight;
console.log("click coords", x, y);
var new_view = clone_view();
// Move coord to re-center the view
new_view.x = view.x + x*view.width - view.width/2;
new_view.y = view.y + y*view.height + view.height/2;
var scale = 1;
if (e.button == MOUSE_LEFT) {
var scale = 0.75;
} else if (e.button == MOUSE_MIDDLE) {
var scale = 1.2;
}
if (e.button != MOUSE_RIGHT) {
rescale(view, new_view, scale);
view = new_view;
requestAnimationFrame(step);
return false;
}
});
document.addEventListener('mousewheel', function(e) {
if (e.deltaY == 0) return;
var step_size = 53;
var new_view = clone_view();
var scale = Math.pow(1.1, e.deltaY / step_size);
if (e.deltaY < 0) {
scale = Math.pow(0.90, -1 * e.deltaY / step_size);
}
console.log(scale, e);
rescale(view, new_view, scale);
view = new_view;
requestAnimationFrame(step);
});
</script>
</body>
</html>