-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
309 lines (283 loc) · 10.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
<!DOCTYPE html>
<title>Julia set interactive visualization : WebGL</title>
<meta charset="UTF-8">
<meta name='viewport' content='width=device-width, initial-scale=1'>
<meta name='description' content='Online and interactive Julia set visualization by WebGL.'>
<!-- <script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script> -->
<script src='vue.min.js'></script>
<script src='three.min.js'></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-E9ZG1KHTVT"></script>
<script src='/scripts/google.js'></script>
<style>
#stage {
position: relative;
}
#canvas-container {
position: absolute;
z-index: 1;
}
#ui {
position: absolute;
z-index: 2;
user-select: none;
top: 0;
left: 0;
color: white;
padding: 1rem;
background-color: #0008
}
#canvas {
width: 100%;
}
</style>
<div id='app'>
<div id="stage">
<div id="canvas-conainer">
<canvas id='canvas' v-on:touchmove='touchMove' v-on:mousemove='mouseMove' v-on:mousedown='mouseDown'></canvas>
</div>
<div id="ui">
<input type='range' v-model='uniforms.range.value.x' min='0.1' max='4' step='0.1'>
rx {{uniforms.range.value.x}}<br>
<input type='range' v-model='uniforms.range.value.y' min='0.1' max='4' step='0.1'>
ry {{uniforms.range.value.y}}<br>
<input type='range' v-model='uniforms.mouseRange.value' min='0.1' max='2' step='0.1'>
mouseRange {{uniforms.mouseRange.value}}<br>
<input type='range' v-model='uniforms.maxItr.value' min='1' max='1024' step='1'>
maxItr {{uniforms.maxItr.value}}<br>
<input type='range' v-model='uniforms.shadow.value' min='0.01' max='4' step='0.01'>
shadow {{uniforms.shadow.value}}<br>
<input type='range' v-model='uniforms.d.value' min='1' max='64' step='1'>
d {{uniforms.d.value}}<br>
<div v-for='c in color'>
<input type='range' v-model='c.a' min='0' max='1' step='0.01'>
<input type='color' v-model='c.rgb'>
<input type='text' v-model='c.rgb' size='7'><br>
</div>
<button type='button' v-on:click='save'>Save</button><br>
</div>
</div>
<div>
<h2>Description</h2>
<p>Online and interactive Julia set visualization by WebGL.</p>
<h2>How to use</h2>
<ul>
<li>Mouse move for changing parameter <i>c</i></li>
<li>Mouse click for pausing</li>
<li>'Save' button for saving current image</li>
</ul>
<h2>Used technologies</h2>
<ul>
<li>HTML / Javascript</li>
<li>Vue.js</li>
<li>WebGL2</li>
</ul>
<p>
<a href="https://github.com/monman53/juliaset">Source codes (GitHub)</a>
Any suggestions and issues are welcome.
</p>
<h2>Future works</h2>
<ul>
<li>Mobile support</li>
<li>More friendly UI</li>
</ul>
<h2>References</h2>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Julia_set#Quadratic_polynomials">Wikipedia: Julia set #Quadratic
polynomials</a></li>
</ul>
<h2>Contacts</h2>
<ul>
<li><a href="https://github.com/monman53/juliaset">GitHub (This project)</a></li>
<li><a href="https://monman53.github.io/">Profile</a></li>
<li><a href="https://twitter.com/monman53">Twitter</a></li>
</ul>
</div>
</div>
<script type="x-shader/x-fragment" id="fs">
uniform vec2 size, mouse, range;
uniform float mouseRange, shadow, d;
uniform int maxItr;
uniform vec4 c[5];
float julia(vec2 z, vec2 c) {
vec2 nz;
int i;
float x_tmp;
for(int itr=0;itr<1024;itr++){
i = itr;
if(dot(z, z) >= 65536. || itr >= maxItr) break;
x_tmp = z.x*z.x - z.y*z.y + c.x;
z.y = 2.*z.x*z.y + c.y;
z.x = x_tmp;
}
float nu = 0.0;
if(i < maxItr){
float log_zn = log(dot(z, z)) * 0.5;
nu = log(log_zn / log(2.0))/log(2.0);
}
return (float(i+1)-nu)/float(maxItr);
}
float smooth(float x) {
return smoothstep(0., 1., x);
}
vec4 color(float x) {
if(x < c[1].a){
return mix(c[0], c[1], smooth((x-c[0].a)/(c[1].a-c[0].a)));
}else if(x < c[2].a){
return mix(c[1], c[2], smooth((x-c[1].a)/(c[2].a-c[1].a)));
}else if(x < c[3].a){
return mix(c[2], c[3], smooth((x-c[2].a)/(c[3].a-c[2].a)));
}else if(x < c[4].a){
return mix(c[3], c[4], smooth((x-c[3].a)/(c[4].a-c[3].a)));
}else{
return mix(c[4], c[0], smooth((x-c[4].a)/(1.0-c[4].a)));
}
}
vec4 pattern(float f) {
f = pow(f, shadow);
// return color(mod(f, 1.0/d)*d);
return color(mod(f, 1./d)*d);
}
void main() {
float r = size.x*range.y > range.x*size.y ? range.y/size.y : range.x/size.x;
vec2 z = (gl_FragCoord.xy - 0.5*size)*r;
vec2 c = (mouse - 0.5*size)*r*mouseRange;
// antialiasing
float dl = r * 0.5;
vec4 col = vec4(0.);
col += pattern(julia(z+vec2(dl*0., dl*0.), c));
col += pattern(julia(z+vec2(dl*0., dl*1.), c));
col += pattern(julia(z+vec2(dl*1., dl*0.), c));
col += pattern(julia(z+vec2(dl*1., dl*1.), c));
col *= 0.25;
// output
gl_FragColor = col;
}
</script>
<script>
var app = new Vue({
el: "#app",
data: {
canvas: null,
renderer: null,
scene: null,
camera: null,
mouse: true,
uniforms: {
range: { type: 'v2', value: new THREE.Vector2(3.5, 2.5) },
size: { type: 'v2', value: new THREE.Vector2(0., 0.) },
mouse: { type: 'v2', value: new THREE.Vector2(0., 0.) },
maxItr: { type: 'int', value: 128 },
mouseRange: { type: 'float', value: 0.7 },
shadow: { type: 'float', value: 1.0 },
d: { type: 'float', value: 5.0 },
c: {
type: 'v4v', value: [
new THREE.Vector4(0. / 255., 7. / 255., 30. / 255., 0.0),
new THREE.Vector4(32. / 255., 107. / 255., 203. / 255., 0.16),
new THREE.Vector4(237. / 255., 255. / 255., 255. / 255., 0.42),
new THREE.Vector4(255. / 255., 140. / 255., 0. / 255., 0.6425),
new THREE.Vector4(0. / 255., 2. / 255., 0. / 255., 0.8575),
]
},
},
color: [
{ rgb: "#00071e", a: 0. },
{ rgb: "#206bcb", a: 0.16 },
{ rgb: "#edffff", a: 0.42 },
{ rgb: "#ff8c00", a: 0.6425 },
{ rgb: "#000200", a: 0.8575 },
],
},
watch: {
uniforms: {
handler: function () {
window.requestAnimationFrame(this.draw);
},
deep: true,
},
color: {
handler: function () {
// window.requestAnimationFrame(this.draw);
for (var i = 0; i < 5; i++) {
var r = parseInt("0x" + this.color[i].rgb.substring(1, 3)) / 255.;
var g = parseInt("0x" + this.color[i].rgb.substring(3, 5)) / 255.;
var b = parseInt("0x" + this.color[i].rgb.substring(5, 7)) / 255.;
this.uniforms.c.value[i] = new THREE.Vector4(r, g, b, this.color[i].a);
}
window.requestAnimationFrame(this.draw);
},
deep: true,
},
},
mounted: function () {
this.canvas = document.getElementById('canvas');
var w = this.canvas.getBoundingClientRect().width;
// var w = canvas.innerWidth;
var h = w * 0.5;
this.canvas.width = w;
this.canvas.height = h;
// prepare webgl with Three.js
this.renderer = new THREE.WebGLRenderer({ canvas: this.canvas });
this.scene = new THREE.Scene();
this.camera = new THREE.OrthographicCamera(-0.5, 0.5, 0.5, -0.5, -1, 1);
this.camera.position.z = 1;
this.scene.add(this.camera)
// uniforms for shader
this.uniforms.size.value = new THREE.Vector2(w, h);
this.uniforms.mouse.value = new THREE.Vector2(w * 0.5, h * 0.5);
// prepare shaders
var screenShaderMaterial = new THREE.ShaderMaterial({
fragmentShader: document.getElementById('fs').textContent,
uniforms: this.uniforms,
});
// set plane
var plane = new THREE.PlaneGeometry(1.0, 1.0);
var mesh = new THREE.Mesh(plane, screenShaderMaterial);
this.scene.add(mesh);
// add resize handler
window.addEventListener('resize', this.resize)
window.addEventListener('fullscreenchange', this.resize)
this.draw();
},
methods: {
draw: function () {
this.renderer.render(this.scene, this.camera);
},
mouseMove: function (event) {
if (this.mouse) {
this.uniforms.mouse.value.x = event.offsetX;
this.uniforms.mouse.value.y = event.offsetY;
}
},
touchMove: function () {
this.uniforms.mouse.value.x = event.targetTouches[0].clientX;
this.uniforms.mouse.value.y = event.targetTouches[0].clientY;
},
mouseDown: function () {
this.mouse = !this.mouse;
},
resize: function () {
var w = window.innerWidth;
var h = window.innerHeight;
this.uniforms.size.value.x = w;
this.uniforms.size.value.y = h;
this.canvas.width = w;
this.canvas.height = h;
this.canvas.style.width = w;
this.canvas.style.height = h;
this.renderer = new THREE.WebGLRenderer({ canvas: this.canvas });
window.requestAnimationFrame(this.draw);
},
save: function () {
this.renderer.render(this.scene, this.camera);
var screenshot = this.renderer.domElement.toDataURL();
var link = document.createElement("a");
link.href = screenshot;
link.download = 'out.png';
link.click();
delete link;
},
},
});
</script>