-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.js
78 lines (64 loc) · 2.53 KB
/
common.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
/**
* Sphere segments count
*/
const SEGMENTS_COUNT = 16;
/**
* Light source position
*/
const LIGHT_SOURCE = [-2, -4, -5];
/**
* @param initFn (WebGLContext) => (screenRatio) => void, initializes resources, returns draw function that will be called in the loop
*/
function drawLoop(canvasId, initFn) {
const canvas = document.getElementById(canvasId),
context = canvas.getContext("webgl", { antialias: true, depth: false, premultipliedAlpha: false });
let resizeRequested = true,
width = 0,
height = 0;
function resize() {
if (resizeRequested) {
const w = canvas.clientWidth,
h = canvas.clientHeight,
px = window.devicePixelRatio || 1; // use real device pixels on high dpi devices
width = w * px;
height = h * px;
canvas.setAttribute("width", width.toFixed(0));
canvas.setAttribute("height", height.toFixed(0));
context.viewport(0, 0, width, height);
resizeRequested = false;
}
}
window.addEventListener("resize", () => resizeRequested = true);
const draw = initFn(context);
(function nextFrame() {
requestAnimationFrame(() => {
resize();
draw(width / height);
nextFrame();
});
})();
}
function buildShaderProgram(gl, vertexSource, fragmentSource) {
let vshader = gl.createShader(gl.VERTEX_SHADER),
fshader = gl.createShader(gl.FRAGMENT_SHADER),
program = gl.createProgram();
gl.shaderSource(vshader, vertexSource);
gl.compileShader(vshader);
gl.shaderSource(fshader, fragmentSource);
gl.compileShader(fshader);
gl.attachShader(program, vshader);
gl.attachShader(program, fshader);
gl.linkProgram(program);
if (!gl.getShaderParameter(vshader, gl.COMPILE_STATUS)) {
let i = 0;
throw new Error("Error compiling vertex shader: " + gl.getShaderInfoLog(vshader) + "\n" + vertexSource.replace(/^/mg, () => `${i++}: `));
}
if (!gl.getShaderParameter(fshader, gl.COMPILE_STATUS)) {
let i = 0;
throw new Error("Error compiling fragment shader: " + gl.getShaderInfoLog(fshader) + "\n" + fragmentSource.replace(/^/mg, () => `${i++}: `));
}
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw new Error("Error linking shaders: " + gl.getProgramInfoLog(program) + "\n" + vertexSource + "\n---\n" + fragmentSource);
}
return program;
}