-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
195 lines (152 loc) · 5.78 KB
/
script.js
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
import * as THREE from 'https://threejs.org/build/three.module.js' //
var root = 'https://gre-v-el.github.io/Mandelbrot-and-Julia-sets-viewer/'
// three.js setups
var canvasHTML = document.getElementById('canvas');
var sceneThree = new THREE.Scene();
var rendererThree = new THREE.WebGLRenderer({ canvas: canvasHTML, preserveDrawingBuffer: true});
var cameraThree = new THREE.PerspectiveCamera(45, canvasHTML.clientWidth / canvasHTML.clientWidth, 1, 1000);
var clock = new THREE.Clock();
var loader = new THREE.FileLoader();
// fps view
var script = document.createElement('script');
var stats;
script.onload = function () {
stats = new Stats();
document.body.appendChild(stats.dom);
};
script.src = ''// 'mrdoob.github.io/stats.js/build/stats.min.js';
document.head.appendChild(script);
// uniforms
var mandelbrotUniforms = {
aspect: { value: cameraThree.aspect },
center: { value: new THREE.Vector2(-0.6, 0) },
scale: { value: 5 },
xDisplacement: { value: -0.5 },
picked: { value: new THREE.Vector2(-1, 0) },
juliaInterpolation: { value: 0 },
isJulia: { value: false },
iterations: { value: 500 },
power: {value: 2}
};
var juliaUniforms = {
aspect: { value: cameraThree.aspect },
center: { value: new THREE.Vector2(0, 0) },
scale: { value: 5 },
xDisplacement: { value: 0.5 },
picked: { value: new THREE.Vector2(-1, 0) },
juliaInterpolation: { value: 1 },
isJulia: { value: true },
iterations: { value: 500 },
power: {value: 2}
};
var targetPicked = new THREE.Vector2(-1, 0);
var smoothPicked = new THREE.Vector2(-1, 0);
var smoothScaleJ = 5;
var targetScaleJ = 5;
var smoothScaleM = 5;
var targetScaleM = 5;
// load shaders and create the scene
var quadVertex = '';
var fractalFragment = '';
var juliaFragment = '';
loader.load(root + '/shaders/fractal_fragment.glsl', function (data) { fractalFragment = data; countLoads(); })
loader.load(root + '/shaders/quad_vertex.glsl', function (data) { quadVertex = data; countLoads(); })
var loadsLeft = 2;
function countLoads() {
loadsLeft--;
if (loadsLeft == 0) {
// main display setup
var mandelbrotDisplay = new THREE.Mesh(
new THREE.PlaneGeometry(1, 2),
new THREE.ShaderMaterial({
vertexShader: quadVertex,
fragmentShader: fractalFragment,
uniforms: mandelbrotUniforms,
depthWrite: false,
depthTest: false
})
);
sceneThree.add(mandelbrotDisplay);
var juliaDisplay = new THREE.Mesh(
new THREE.PlaneGeometry(1, 2),
new THREE.ShaderMaterial({
vertexShader: quadVertex,
fragmentShader: fractalFragment,
uniforms: juliaUniforms,
depthWrite: false,
depthTest: false
})
);
sceneThree.add(juliaDisplay);
}
}
function tick() {
var dt = clock.getDelta();
if (controls.doUpdate) {
var impacted = controls.mouseX > canvasHTML.width / 2 ? juliaUniforms : mandelbrotUniforms;
if (controls.mouseRight) {
impacted.center.value.x -=
controls.mouseDX / canvasHTML.width * 2 * impacted.scale.value;
impacted.center.value.y +=
controls.mouseDY / canvasHTML.height * impacted.scale.value;
}
if (controls.mouseLeft && impacted == mandelbrotUniforms) {
targetPicked = new THREE.Vector2(
(controls.mouseX / canvasHTML.width * 2 - 0.5) * mandelbrotUniforms.aspect.value * mandelbrotUniforms.scale.value + mandelbrotUniforms.center.value.x,
-(controls.mouseY / canvasHTML.height - 0.5) * mandelbrotUniforms.scale.value + mandelbrotUniforms.center.value.y
);
}
if(impacted == mandelbrotUniforms)
targetScaleM*= Math.pow(1.002, controls.mouseScroll);
else
targetScaleJ*= Math.pow(1.002, controls.mouseScroll);
mandelbrotUniforms.iterations.value = inspector.iterations;
juliaUniforms.iterations.value = inspector.iterations;
mandelbrotUniforms.power.value = inspector.power;
juliaUniforms.power.value = inspector.power;
juliaUniforms.juliaInterpolation.value = inspector.interpolation;
}
smoothPicked.lerp(targetPicked, 0.2);
smoothScaleM += (targetScaleM - smoothScaleM)*0.2;
smoothScaleJ += (targetScaleJ - smoothScaleJ)*0.2;
juliaUniforms.picked.value = smoothPicked;
mandelbrotUniforms.picked.value = smoothPicked;
juliaUniforms.scale.value = smoothScaleJ;
mandelbrotUniforms.scale.value = smoothScaleM;
updateControls();
render();
if (stats != undefined) stats.update();
requestAnimationFrame(tick);
}
function render() {
if (canvasHTML.width !== canvasHTML.clientWidth || canvasHTML.height !== canvasHTML.clientHeight) {
rendererThree.setSize(canvasHTML.clientWidth, canvasHTML.clientHeight, false);
cameraThree.aspect = canvasHTML.clientWidth / canvasHTML.clientHeight;
cameraThree.updateProjectionMatrix();
mandelbrotUniforms.aspect.value = cameraThree.aspect / 2;
juliaUniforms.aspect.value = cameraThree.aspect / 2;
}
rendererThree.render(sceneThree, cameraThree);
}
tick();
document.getElementById("dropdown-button").addEventListener('click', function() {
document.getElementById("download-options").style.height =
document.getElementById("download-options").style.height == "150px"? "0px" : "150px";
})
document.getElementById("screenshot-button").addEventListener('click', function() {
canvasHTML.width = document.getElementById("render-width-input").value;
canvasHTML.height = document.getElementById("render-height-input").value;
rendererThree.setSize(canvasHTML.width, canvasHTML.height, false);
cameraThree.aspect = canvasHTML.width / canvasHTML.height;
cameraThree.updateProjectionMatrix();
mandelbrotUniforms.aspect.value = cameraThree.aspect / 2;
juliaUniforms.aspect.value = cameraThree.aspect / 2;
rendererThree.render(sceneThree, cameraThree);
var link = document.createElement('a');
document.body.appendChild(link);
link.download = "mandelbrot_julia.png";
link.href = canvasHTML.toDataURL("image/png");
link.click();
document.body.removeChild(link);
document.getElementById("download-options").style.height = "0px";
})