-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl-utils.js
108 lines (93 loc) · 3.18 KB
/
webgl-utils.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
/**
* @typedef {(0x8B30|0x8B31)} WebGLShaderType
*/
/**
* Creates and compiles a shader.
*
* @param {WebGLRenderingContext} gl The WebGL Context.
* @param {string} shaderSource The GLSL source code for the shader.
* @param {WebGLShaderType} shaderType The type of shader, VERTEX_SHADER or
* FRAGMENT_SHADER.
* @return {WebGLShader} The shader.
*/
function compileShader(gl, shaderSource, shaderType) {
// Create the shader object
const shader = gl.createShader(shaderType);
// Set the shader source code.
gl.shaderSource(shader, shaderSource);
// Compile the shader
gl.compileShader(shader);
// Check if it compiled
const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (!success) {
// Something went wrong during compilation; get the error
throw "could not compile shader:" + gl.getShaderInfoLog(shader);
}
return shader;
}
/**
* Creates a program from 2 shaders.
*
* @param {WebGLRenderingContext} gl The WebGL context.
* @param {WebGLShader} vertexShader A vertex shader.
* @param {WebGLShader} fragmentShader A fragment shader.
* @return {WebGLProgram} A program.
*/
function createProgram(gl, vertexShader, fragmentShader) {
// create a program.
const program = gl.createProgram();
// attach the shaders.
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
// link the program.
gl.linkProgram(program);
// Check if it linked.
const success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (!success) {
// something went wrong with the link
throw ("program failed to link:" + gl.getProgramInfoLog (program));
}
return program;
}
/**
*
* @param {WebGLRenderingContext} gl
* @param {string} fileSource
* @param {WebGLShaderType} shaderType
*/
async function createShaderFromFile(gl, fileSource, shaderType) {
const response = await fetch(fileSource);
if (!response.ok) {
throw("Shader: " + fileSource + " not found!");
}
const shaderText = await response.text();
console.log(shaderText);
return compileShader(gl, shaderText, shaderType)
}
/**
* Creates a program from 2 shader files.
*
* @param {WebGLRenderingContext} gl The WebGL Context.
* @param {string[]} shaderURLs Array of ids of the script
* tags for the shaders. The first is assumed to be the
* vertex shader, the second the fragment shader.
* @return {WebGLProgram} A program
*/
async function createProgramFromFiles(
gl, shaderURLs) {
const vertexShader = await createShaderFromFile(gl, shaderURLs[0], gl.VERTEX_SHADER);
const fragmentShader = await createShaderFromFile(gl, shaderURLs[1], gl.FRAGMENT_SHADER);
return createProgram(gl, vertexShader, fragmentShader);
}
function resizeCanvasToDisplaySize(canvas, multiplier) {
multiplier = multiplier || 1;
const width = canvas.clientWidth * multiplier | 0;
const height = canvas.clientHeight * multiplier | 0;
if (canvas.width !== width || canvas.height !== height) {
canvas.width = width;
canvas.height = height;
return true;
}
return false;
}
export { createProgramFromFiles, resizeCanvasToDisplaySize }