-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.js
executable file
·148 lines (129 loc) · 4.86 KB
/
util.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
let programs = {};
let frameBuffers = {};
let textures = {};
let glBoilerplate = initBoilerPlate();
function createProgram(programName, vertexShader, fragmentShader) {
let program = glBoilerplate.programFromSources(gl, vertexShader, fragmentShader);
gl.useProgram(program);
glBoilerplate.bindScreenVerts(gl, program);
programs[programName] = {
program: program,
uniforms: {}
};
}
function resetTexture(name, width, height) {
textures[name] = glBoilerplate.createTexture(gl, width, height, ext.HALF_FLOAT_OES, null);
}
function setTexture(name, texture) {
textures[name] = textures[texture];
}
function resetTextureImage(name, url) {
textures[name] = loadTexture(gl, "assets/" + url)
}
function resetFrameBufferForTexture(textureName) {
let texture = textures[textureName];
let framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
gl.checkFramebufferStatus(gl.FRAMEBUFFER);
frameBuffers[textureName] = framebuffer;
}
function setUniformForProgram(programName, name, val, type) {
let uniforms = programs[programName].uniforms;
let location = uniforms[name];
if (!location) {
location = gl.getUniformLocation(programs[programName].program, name);
uniforms[name] = location;
}
switch(type) {
case "1i":
gl.uniform1i(location, val);
break;
case "1f":
gl.uniform1f(location, val);
break;
case "2f":
gl.uniform2f(location, val[0], val[1]);
break;
case "3f":
gl.uniform3f(location, val[0], val[1], val[2]);
break;
case "4f":
gl.uniform4f(location, val[0], val[1], val[2], val[3]);
break;
}
}
function updateUniformForProgram(programName, name, val, type) {
setProgram(programName);
setUniformForProgram(programName, name, val, type)
}
function setSize(width, height) {
gl.viewport(0, 0, width, height);
}
function setProgram(programName) {
gl.useProgram(programs[programName].program);
}
function executeShader(programName, inputTextures, outputTexture) {
gl.useProgram(programs[programName].program);
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffers[outputTexture]);
for (let i = 0; i < inputTextures.length; i++) {
gl.activeTexture(gl.TEXTURE0 + i);
gl.bindTexture(gl.TEXTURE_2D, textures[inputTextures[i]]);
}
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
}
function swapTextures(texture1Name, texture2Name) {
let temp = textures[texture1Name];
textures[texture1Name] = textures[texture2Name];
textures[texture2Name] = temp;
temp = frameBuffers[texture1Name];
frameBuffers[texture1Name] = frameBuffers[texture2Name];
frameBuffers[texture2Name] = temp;
}
//
// Initialize a texture and load an image.
// When the image finishes loading, copy it into the texture.
//
// https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Using_textures_in_WebGL
//
function loadTexture(gl, url) {
function isPowerOf2(value) {
return (value & (value - 1)) === 0;
}
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
// Because images have to be download over the internet
// they might take a moment until they are ready.
// Until then put a single pixel in the texture so we can
// use it immediately. When the image has finished downloading
// we'll update the texture with the contents of the image.
const level = 0;
const internalFormat = gl.RGBA;
const width = 1;
const height = 1;
const border = 0;
const srcFormat = gl.RGBA;
const srcType = gl.UNSIGNED_BYTE;
const pixel = new Uint8Array([0, 0, 255, 255]); // opaque blue
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, srcFormat, srcType, pixel);
const image = new Image();
image.onload = function() {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, srcFormat, srcType, image);
// WebGL1 has different requirements for power of 2 images
// vs non power of 2 images so check if the image is a
// power of 2 in both dimensions.
if (isPowerOf2(image.width) && isPowerOf2(image.height)) {
// Yes, it's a power of 2. Generate mips.
gl.generateMipmap(gl.TEXTURE_2D);
} else {
// No, it's not a power of 2. Turn off mips and set
// wrapping to clamp to edge
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
}
};
image.src = url;
return texture;
}