-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
143 lines (110 loc) · 3.87 KB
/
main.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
gl = initGL("canvas");
let program = createProgram("vts", "fts");
gl.useProgram(program);
let posL = gl.getAttribLocation(program, "position");
let posBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, posBuffer);
let positions = [
-1, -1,
+1, -1,
-1, +1,
-1, +1,
+1, -1,
+1, +1
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
resize(gl.canvas, false, false, true);
clearColor();
gl.enableVertexAttribArray(posL);
gl.vertexAttribPointer(posL, 2, gl.FLOAT, false, 0, 0);
let config = {
fractal: 3,
specular_exp: 16,
light_angle: 0.0,
fps: 60,
};
let resL = gl.getUniformLocation(program, "res");
gl.uniform2f(resL, 2.0/gl.drawingBufferWidth, 2.0/gl.drawingBufferHeight);
let timeL = gl.getUniformLocation(program, "time");
let fractalL = gl.getUniformLocation(program, "fractal");
let camZL = gl.getUniformLocation(program, "camZ");
let THICCL = gl.getUniformLocation(program, "THICC");
let specularExpL = gl.getUniformLocation(program, "specularExp");
let lightAngleL = gl.getUniformLocation(program, "lightAngle");
function updateFractal(){
// let fractalN = document.getElementById("fractals").value;
let camZ, THICC;
switch(+config.fractal){
case 3:
camZ = -3.0;
THICC = 0.001;
break;
case 2:
camZ = -2.0;
THICC = 0.001;
break;
case 1:
camZ = -16.0;
THICC = 0.03;
break;
case 0: default:
camZ = -6.0;
THICC = 0.001;
}
gl.uniform1i(fractalL, config.fractal);
gl.uniform1f(camZL, camZ);
gl.uniform1f(THICCL, THICC);
gl.uniform1f(specularExpL, config.specular_exp);
gl.uniform1f(lightAngleL, config.light_angle);
}
updateFractal();
let gui = new dat.GUI({ width: 300 });
gui.add(config, 'fractal', {
'Menger Sponge': 3,
'Sierpenski Pyramid': 0,
'Mandelbox': 1,
'Mandelbulb': 2,
}).name('Fractal').onFinishChange(updateFractal);
gui.add(config, 'specular_exp', 0.5, 16, 0.5).name('Specular Exponent').onFinishChange(updateFractal);
gui.add(config, 'light_angle', 0.0, 1, 0.1).name('Lighting Angle').onFinishChange(updateFractal);
//let perfFolder = gui.addFolder("Performance");
//perfFolder.add(config, 'fps', 0, 120).name('Target FPS').step(1);
// Choose best requestAnimationFrame
window.reqAnimationFrame = (function(callback){
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / config.fps);
};
})();
let stats = new Stats();
stats.showPanel(0); // 0: fps, 1: ms, 2: mb, 3+: custom
document.body.appendChild(stats.dom);
let startTime = performance.now();
function draw(){
let time = performance.now();
stats.begin();
gl.drawArrays(gl.TRIANGLES, 0, 6);
gl.uniform1f(timeL, (time - startTime)/2000);
stats.end();
window.reqAnimationFrame(draw, 1000/ config.fps);
}
/**
function animate(newtime) {
// request another frame
window.reqAnimationFrame(animate);
// elapsed time since last loop
now = newtime;
elapsed = now - then;
// if enough time has elapsed, draw the next frame
if (elapsed > fpsInterval) {
// Get ready for next frame by setting then=now, but...
// Also, adjust for fpsInterval not being multiple of 16.67
then = now - (elapsed % fpsInterval);
// draw stuff here
// TESTING...Report #seconds since start and achieved fps.
var sinceStart = now - startTime;
var currentFps = Math.round(1000 / (sinceStart / ++frameCount) * 100) / 100;
$results.text("Elapsed time= " + Math.round(sinceStart / 1000 * 100) / 100 + " secs @ " + currentFps + " fps.");
}
}**/
window.addEventListener("load", draw, false);