forked from USTC-Hackergame/hackergame2022-writeups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender-main.js
184 lines (147 loc) · 5.67 KB
/
render-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
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
"use strict";
// Some of the code are derived from https://webglfundamentals.org/webgl/lessons/webgl-shadertoy.html
// which is licensed under MIT License.
function main(vsSource, fsSource) {
// Get A WebGL context
/** @type {HTMLCanvasElement} */
const canvas = document.querySelector("#canvas");
const gl = canvas.getContext("webgl");
if (!gl) {
return;
}
// setup GLSL program
const program = webglUtils.createProgramFromSources(gl, [vsSource, fsSource]);
// look up where the vertex data needs to go.
const positionAttributeLocation = gl.getAttribLocation(program, "a_position");
// look up uniform locations
const resolutionLocation = gl.getUniformLocation(program, "iResolution");
const mouseLocation = gl.getUniformLocation(program, "iMouse");
const timeLocation = gl.getUniformLocation(program, "iTime");
// Create a buffer to put three 2d clip space points in
const positionBuffer = gl.createBuffer();
// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// fill it with a 2 triangles that cover clipspace
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
-1, -1, // first triangle
1, -1,
-1, 1,
-1, 1, // second triangle
1, -1,
1, 1,
]), gl.STATIC_DRAW);
const inputElem = document.querySelector('.divcanvas');
const playResetButton = document.getElementById('playResetButton');
const playPauseButton = document.getElementById('playPauseButton');
const playerTimeSpan = document.getElementById('playerTime');
const playerFramerateSpan = document.getElementById('playerFramerate');
const playerResolutionSpan = document.getElementById('playerResolution');
const playerStatusSpan = document.getElementById('playerStatus');
playResetButton.addEventListener('click', resetTime);
playPauseButton.addEventListener('click', togglePlayPauseState);
let isPlaying = false;
function togglePlayPauseState() {
isPlaying = !isPlaying;
if (isPlaying) {
playPauseButton.innerHTML = "PAUSE";
requestFrame();
} else {
playPauseButton.innerHTML = "PLAY";
cancelFrame();
}
}
let mouseX = 0;
let mouseY = 0;
function setMousePosition(e) {
const rect = inputElem.getBoundingClientRect();
mouseX = e.clientX - rect.left;
mouseY = rect.height - (e.clientY - rect.top) - 1; // bottom is 0 in WebGL
}
inputElem.addEventListener('mousemove', setMousePosition);
inputElem.addEventListener('touchmove', (e) => {
e.preventDefault();
setMousePosition(e.touches[0]);
}, {passive: false});
let requestId;
function requestFrame() {
if (!requestId) {
requestId = requestAnimationFrame(render);
}
}
function cancelFrame() {
if (requestId) {
cancelAnimationFrame(requestId);
requestId = undefined;
}
}
let then = 0;
let time = 0;
// Previous status update time
let prevUpdateTime = 0;
// Cumulative frome rendered for this period
let cumuFrameRendered = 0;
function resetTime() {
time = 0;
prevUpdateTime = 0;
}
function render(now) {
requestId = undefined;
now *= 0.001; // convert to seconds
const elapsedTime = Math.min(now - then, 0.1);
time += elapsedTime;
then = now;
cumuFrameRendered += 1;
// update player status bar
if (time - prevUpdateTime > 0.1) {
playerTimeSpan.innerHTML = time.toFixed(2) + " sec";
playerFramerateSpan.innerHTML = (cumuFrameRendered / (time - prevUpdateTime)).toFixed(2) + " FPS";
playerResolutionSpan.innerHTML = gl.canvas.width.toString() + " x " + gl.canvas.height.toString();
prevUpdateTime = time;
cumuFrameRendered = 0;
}
webglUtils.resizeCanvasToDisplaySize(gl.canvas);
// Tell WebGL how to convert from clip space to pixels
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
// Tell it to use our program (pair of shaders)
gl.useProgram(program);
playerStatusSpan.innerHTML = "Program: Initialized";
// Turn on the attribute
gl.enableVertexAttribArray(positionAttributeLocation);
// Bind the position buffer.
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
gl.vertexAttribPointer(
positionAttributeLocation,
2, // 2 components per iteration
gl.FLOAT, // the data is 32bit floats
false, // don't normalize the data
0, // 0 = move forward size * sizeof(type) each iteration to get the next position
0, // start at the beginning of the buffer
);
gl.uniform2f(resolutionLocation, gl.canvas.width, gl.canvas.height);
gl.uniform2f(mouseLocation, mouseX, mouseY);
gl.uniform1f(timeLocation, time);
gl.drawArrays(
gl.TRIANGLES,
0, // offset
6, // num vertices to process
);
requestFrame();
}
requestFrame();
requestAnimationFrame(cancelFrame);
}
window.onload = function () {
const playerStatusSpan = document.getElementById('playerStatus');
playerStatusSpan.innerHTML = "Program: Initializing...";
const canvasObj = document.getElementById('canvas');
canvasObj.addEventListener("webglcontextlost", (e) => {
const playerStatusSpan = document.getElementById('playerStatus');
const extraInfoSpan = document.getElementById('extraInfo');
playerStatusSpan.innerHTML = "Program: Context Lost";
extraInfoSpan.innerHTML = "Your WebGL context has lost. This might've been caused by a browser or driver bug, and we recommend you to try again with another browser or graphics driver installed.";
});
setTimeout(function () {
main(window.vertexShader, window.fragmentShader);
}, 100);
}