-
Notifications
You must be signed in to change notification settings - Fork 1
/
webGLShaderLoader.js
190 lines (168 loc) · 6.22 KB
/
webGLShaderLoader.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
var WebGLShaderLoader = (function () {
var vertexShaderType = WebGLRenderingContext.prototype.VERTEX_SHADER;
var fragmentShaderType = WebGLRenderingContext.prototype.FRAGMENT_SHADER;
function WebGLShaderLoader (gl) {
this.errors = [];
this.vertexShader = this.fragmentShader = null;
this.gl = getContext(gl);
if (!this.gl) this.errors.push("webgl unsupported");
};
WebGLShaderLoader.prototype = {
loadFromStr: function (vertexShaderStr, fragmentShaderStr, cb) {
if (typeof vertexShaderStr !== "string" ||
typeof fragmentShaderStr !== "string" ||
typeof cb !== "function") {
this.errors.push("Usage: loaderInstance.loadFromStr('', '', function (errors, program));");
cb(this.errors, null, this.gl);
} else if (vertexShaderStr.length === 0 ||
fragmentShaderStr.length === 0) {
this.errors.push("empty shader string");
cb(this.errors, null, this.gl);
} else {
this.vertexShader = this.compile(vertexShaderType, vertexShaderStr);
this.fragmentShader = this.compile(fragmentShaderType, fragmentShaderStr);
cb(this.errors, this.link(), this.gl);
}
},
loadFromXHR: function (vertexShaderPath, fragmentShaderPath, cb) {
if (typeof vertexShaderPath !== "string" ||
typeof fragmentShaderPath !== "string" ||
typeof cb !== "function") {
this.errors.push("Usage: loaderInstance.loadFromXHR('', '', function (errors, program));");
cb(this.errors, null, this.gl);
return;
}
if (vertexShaderPath.length === 0 || fragmentShaderPath.length === 0) {
this.errors.push("empty shader path");
cb(this.errors, null, this.gl);
return;
}
var numShadersLoaded = 0;
var onload = function (shader, shaderType) {
return function (twoHundredResponse, shaderStr) {
if (!twoHundredResponse) {
this.errors.push("xhr non 200 response code");
cb(this.errors, null, this.gl);
return;
}
this[shader] = this.compile(shaderType, shaderStr);
if (++numShadersLoaded > 1) {
cb(this.errors, this.link(), this.gl);
}
}.bind(this);
}.bind(this);
this.fetch(vertexShaderPath, onload("vertexShader", vertexShaderType));
this.fetch(fragmentShaderPath, onload("fragmentShader", fragmentShaderType));
},
compile: function (type, shaderStr) {
var shader = this.gl.createShader(type);
this.gl.shaderSource(shader, shaderStr);
this.gl.compileShader(shader);
if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {
this.errors.push(this.gl.getShaderInfoLog(shader));
}
return shader;
},
link: function () {
var program = this.gl.createProgram();
this.gl.attachShader(program, this.vertexShader);
this.gl.attachShader(program, this.fragmentShader);
this.gl.linkProgram(program);
if (!this.gl.getProgramParameter(program, this.gl.LINK_STATUS)) {
this.errors.push(this.gl.getProgramInfoLog(program));
}
return program;
},
fetch: function (path, cb) {
var xhr = new XMLHttpRequest;
xhr.open("GET", path);
xhr.onload = function () {
cb(xhr.status === 200, xhr.response);
};
xhr.send();
},
};
function getContext (glOrCanvas) {
if (glOrCanvas instanceof HTMLCanvasElement) {
return glOrCanvas.getContext('webgl') || glOrCanvas.getContext('experimental-webgl');
} else {
return glOrCanvas;
}
};
function loadImages (imgSrcs, cb) {
var len = imgSrcs.length;
var images = [];
var errors = [];
var loaded = 0;
function onLoad () { if (++loaded === len) cb(errors, images); };
function onError (e) {
errors.push("Failed to load " + e.target.src);
onLoad();
};
for (var i = 0; i < len; ++i) {
images[i] = new Image;
images[i].onload = onLoad;
images[i].onerror = onError;
images[i].src = imgSrcs[i];
}
};
function getQualifiersPartial(parameter, storage) {
var getActive = "getActive" + storage;
var getLocation = "get" + storage + "Location";
return function (gl, program) {
var len = gl.getProgramParameter(program, parameter);
var qualifiers = {};
var qualifier = null;
for (var i = 0; i < len; ++i) {
qualifier = gl[getActive](program, i).name;
qualifiers[qualifier] = gl[getLocation](program, qualifier);
}
return qualifiers;
};
};
var webGLProto = WebGLRenderingContext.prototype;
var getAttributes = getQualifiersPartial(webGLProto.ACTIVE_ATTRIBUTES, "Attrib");
var getUniforms = getQualifiersPartial(webGLProto.ACTIVE_UNIFORMS, "Uniform");
WebGLShaderLoader.prototype.getAttributes = getAttributes;
WebGLShaderLoader.prototype.getUniforms = getUniforms;
// call with:
// load(gl, ['a.vert', 'b.frag', 'c.vert', 'd.frag'], ['a.jpg', 'b.jpg'],
// function (errors, gl, programs, imgs) { ... });
WebGLShaderLoader.load = function (_gl, shaderUrls, imgUrls, cb) {
imgUrls = imgUrls || [];
var programsDone = 0;
var shadersComplete = false;
var imgsComplete = !imgUrls.length;
var totalShaders = shaderUrls.length;
var errors = [];
var programs = [];
var images = [];
var gl = getContext(_gl);
if (!gl) errors.push("webgl unsupported");
if (!imgsComplete) {
loadImages(imgUrls, function (e, imgs) {
if (e.length) errors.concat(e);
images = imgs;
imgsComplete = true;
if (shadersComplete) cb(errors, gl, programs, images);
});
}
shaderUrls.forEach(function (shaderUrl, i) {
if (i % 2 === 1) return;
var loader = new WebGLShaderLoader(gl);
loader.loadFromXHR(shaderUrls[i], shaderUrls[i + 1], function (e, program, _) {
if (e.length) errors.concat(e);
programs[i / 2] = {
attributes: getAttributes(gl, program),
program: program,
uniforms: getUniforms(gl, program),
};
if (++programsDone === totalShaders / 2) {
shadersComplete = true;
if (imgsComplete) cb(errors, gl, programs, images);
}
});
});
};
return WebGLShaderLoader;
})();