-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
195 lines (162 loc) · 5.86 KB
/
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
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
185
186
187
188
189
190
191
192
193
194
195
function resizeCanvasToDisplaySize(canvas) {
const displayWidth = canvas.clientWidth;
const displayHeight = canvas.clientHeight;
const needResize = canvas.width !== displayWidth ||
canvas.height !== displayHeight;
if (needResize) {
canvas.width = displayWidth;
canvas.height = displayHeight;
}
return needResize;
}
function createShader(gl, type, source) {
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (success)
return shader;
var shaderType = "vertex"
if (type === gl.FRAGMENT_SHADER)
shaderType = "fragment"
console.log(source)
console.log(shaderType, gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
}
function createProgram(gl, vertexShader, fragmentShader) {
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
var success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (success)
return program;
console.log(gl.getProgramInfoLog(program));
gl.deleteProgram(program);
}
var shaders = {};
var programs = {};
function buildProgramsFromArray(gl, array) {
Object.keys(array).forEach((name) => {
if (!shaders[array[name]["vertex"]]) {
var source = document.querySelector("#" + array[name]["vertex"]).text;
shaders[array[name]["vertex"]] = createShader(gl, gl.VERTEX_SHADER, source);
}
if (!shaders[array[name]["fragment"]]) {
var source = document.querySelector("#" + array[name]["fragment"]).text;
shaders[array[name]["fragment"]] = createShader(gl, gl.FRAGMENT_SHADER, source);
}
programs[name] = createProgram(gl, shaders[array[name]["vertex"]], shaders[array[name]["fragment"]]);
})
}
function useProgram(gl, program) {
gl.useProgram(getProgram(program));
}
function getProgram(program) {
return programs[program];
}
var attribLocations = {};
function setAttribs(gl, program, attribs) {
if (!attribLocations[program])
attribLocations[program] = {};
Object.keys(attribs).forEach((attr) => {
if (!attribLocations[program][attr]) {
attribLocations[program][attr] = gl.getAttribLocation(getProgram(program), attr);
}
if (attribLocations[program][attr] !== -1) {
gl.bindBuffer(gl.ARRAY_BUFFER, attribs[attr]["buffer"]);
gl.enableVertexAttribArray(attribLocations[program][attr]);
gl.vertexAttribPointer(
attribLocations[program][attr],
attribs[attr]["numComponents"], gl.FLOAT, false, 0, 0
);
}
});
}
var uniformLocations = {};
function setUniforms(gl, program, uniforms) {
if (!uniformLocations[program]) {
uniformLocations[program] = {};
}
useProgram(gl, program);
Object.keys(uniforms).forEach((uni) => {
if (!uniformLocations[program][uni]) {
uniformLocations[program][uni] = gl.getUniformLocation(getProgram(program), uni);
}
switch (uniforms[uni]["type"]) {
case "int1":
gl.uniform1i(uniformLocations[program][uni],
uniforms[uni].data);
break;
case "float1":
gl.uniform1f(uniformLocations[program][uni],
uniforms[uni].data);
break;
case "vec3":
gl.uniform3fv(uniformLocations[program][uni],
uniforms[uni].data)
break;
case "mat4":
gl.uniformMatrix4fv(uniformLocations[program][uni],
false, uniforms[uni].data)
break;
}
});
}
function guessNumComponentsFromName(name, length) {
let numComponents;
if (name.indexOf('coord') >= 0) {
numComponents = 2;
} else if (name.indexOf('color') >= 0) {
numComponents = 4;
} else {
numComponents = 3; // position, normals, indices ...
}
if (length % numComponents > 0) {
throw 'can not guess numComponents. You should specify it.';
}
return numComponents;
}
function getNumComponents(array, arrayName) {
return array.numComponents || array.size || guessNumComponentsFromName(arrayName, getArray(array).length);
}
function getArray(array) {
return array.length ? array : array.data;
}
function getNumElements(arrays) {
let key;
const positionKeys = ['position', 'positions', 'a_position'];
for (const k of positionKeys) {
if (k in arrays) {
key = k;
break;
}
}
key = key || Object.keys(arrays)[0];
const array = arrays[key];
const length = getArray(array).length;
const numComponents = getNumComponents(array, key);
const numElements = length / numComponents;
if (length % numComponents > 0) {
throw new Error(`numComponents ${numComponents} not correct for length ${length}`);
}
return numElements;
}
function createBufferInfo(gl, data) {
var bufferInfo = {};
bufferInfo.numElements = getNumElements(data);
bufferInfo.attribs = {}
Object.keys(data).forEach((key) => {
bufferInfo.attribs["a_" + key] = {};
bufferInfo.attribs["a_" + key].buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.attribs["a_" + key].buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data[key]), gl.STATIC_DRAW);
bufferInfo.attribs["a_" + key].numComponents = 3;
})
bufferInfo.attribs["a_texcoord"].numComponents = 2;
return bufferInfo;
}
function setBufferAttribs(gl, program, bufferInfo) {
setAttribs(gl, program, bufferInfo.attribs);
}
export { resizeCanvasToDisplaySize, buildProgramsFromArray, createBufferInfo, setBufferAttribs, setUniforms };