forked from iapafoto/WebGLShader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLCanvas.js
More file actions
255 lines (218 loc) · 8.3 KB
/
GLCanvas.js
File metadata and controls
255 lines (218 loc) · 8.3 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Sebastien Durand 2023
// ----------------------------------------------------------------
// License: Creative Commons Attribution-ShareAlike 3.0 Unported License
//-----------------------------------------------------------------
// Associates a glsl shader to a canvas
// Enables animation with pause and start (iTime - shadertoy like)
// Manage canvas resize (iResolution - shadertoy like)
// ----------------------------------------------------------------
class GLCanvas {
#paramCallback;
#animTime0;
#time0;
#isPause;
constructor(canvas, fragmentShader, initCallback, renderCallback) {
if (typeof canvas === 'string' || canvas instanceof String) {
this.canvas = document.getElementById(canvas);
} else {
this.canvas = canvas;
}
this.gl = this.canvas.getContext('webgl2');
this.#paramCallback = renderCallback;
this.#isPause = false;
if (fragmentShader.endsWith(".frag") || fragmentShader.endsWith(".gl") || fragmentShader.endsWith(".glsl")) {
loadShader(fragmentShader, (src) => { // load file form url
this.#init(src, initCallback);
});
} else {
this.#init(fragmentShader, initCallback); // already source code
}
}
#init(fragmentShaderSrc, initCallback) {
this.program = this.#createProgram(fragmentShaderSrc);
this.setAnimTime(0); // Begining of the animation
const self = this;
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.addEventListener('resize', function() {
self.#resizeMe();
});
if (initCallback != null && typeof initCallback !== 'undefined') {
initCallback(this);
}
this.#resizeMe();
this.#render();
}
#resizeMe() {
// Update canvas size to real size to avoid stretch effects
const k = window.devicePixelRatio || 1;
if (this.canvas.width != k*this.canvas.clientWidth ||
this.canvas.height != k*this.canvas.clientHeight) {
this.canvas.width = k*this.canvas.clientWidth;
this.canvas.height = k*this.canvas.clientHeight;
this.update();
}
}
#compile(shader, src) {
this.gl.shaderSource(shader, src);
this.gl.compileShader(shader);
// log de compilation
let compiled = this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS);
console.log('Shader compiled successfully: ' + compiled);
let compilationLog = this.gl.getShaderInfoLog(shader);
console.log('Shader compiler log: ' + compilationLog);
}
#createProgram(fragmentShaderSrc) {
let vertexShaderSrc = `#version 300 es
in vec4 position;void main() {gl_Position = position;}`;
const vertexShader = this.gl.createShader(this.gl.VERTEX_SHADER);
const fragmentShader = this.gl.createShader(this.gl.FRAGMENT_SHADER);
this.#compile(vertexShader, vertexShaderSrc)
this.#compile(fragmentShader, fragmentShaderSrc)
const program = this.gl.createProgram();
this.gl.attachShader(program, vertexShader);
this.gl.attachShader(program, fragmentShader);
this.gl.linkProgram(program);
this.gl.useProgram(program);
return program;
}
#render() {
this.gl.viewport(0, 0, this.canvas.width, this.canvas.height);
const positionAttributeLocation = this.gl.getAttribLocation(this.program, 'position');
const positionBuffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, positionBuffer);
this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array([-1,-1,1,-1,-1,1,1,1]), this.gl.STATIC_DRAW);
this.gl.enableVertexAttribArray(positionAttributeLocation);
this.gl.vertexAttribPointer(positionAttributeLocation, 2, this.gl.FLOAT, false, 0, 0);
// Charge les paramètres du shader
this.gl.useProgram(this.program);
// par defaut
this.set1f('iTime', this.getAnimTime());
this.set2f('iResolution', this.canvas.width, this.canvas.height);
// Appel de la fonction de rendu de votre fragment shader
if (this.#paramCallback != null && typeof this.#paramCallback !== 'undefined') {
this.#paramCallback(this);
}
this.gl.drawArrays(this.gl.TRIANGLE_STRIP, 0, 4);
if (!this.#isPause) {
window.requestAnimationFrame(() => this.#render());
}
}
// Control of animation -------------------------
isPaused() {
return this.#isPause;
}
start() {
this.#isPause = false;
this.setAnimTime(this.#animTime0);
this.#render();
}
pause() {
if (!this.#isPause) {
this.#animTime0 = this.#animTime0 + (performance.now()/1000. - this.#time0);
this.#isPause = true;
}
}
update() {
if (this.#isPause) { // update is automatic if anim
this.#render();
}
}
setAnimTime(t) { // indirect set anim time
this.#animTime0 = t;
this.#time0 = performance.now()/1000.;
}
getAnimTime() { // indirect get anim time
return this.#isPause ? this.#animTime0 : this.#animTime0 + (performance.now() / 1000 - this.#time0);
}
// Modest simplification of function calls ------------------------
set1f(param, v) {
let p = this.gl.getUniformLocation(this.program, param);
if (p != null) this.gl.uniform1f(p, v);
}
set1i(param, v) {
let p = this.gl.getUniformLocation(this.program, param);
if (p != null) this.gl.uniform1i(p, v);
}
set2f(param, v1, v2) {
let p = this.gl.getUniformLocation(this.program, param);
if (p != null) this.gl.uniform2f(p, v1, v2);
}
setTexture(param, txt, id) {
this.gl.activeTexture(this.gl.TEXTURE0 + id);
this.gl.bindTexture(this.gl.TEXTURE_2D, txt);
this.set1i(param, id);
}
}
// ------------------------------------------------------------------------------
// Initialize a texture and load an image.
// When the image finished loading copy it into the texture.
// The texture is create froma canvas but could be use in all canvas
function loadTexture(canvas, url) {
var gl = canvas.getContext("webgl2");
const texture = gl.createTexture();
// Default init blue texture
gl.bindTexture(gl.TEXTURE_2D, texture);
const pixel = new Uint8Array([0, 0, 255, 255]); // opaque blue
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1,1,0, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
// Get real picture ad update texture with it
const image = new Image();
image.onload = () => updateTexture(gl, texture, image);
image.src = url;
return texture;
}
function isPowerOf2(value) {
return (value & (value - 1)) === 0;
}
function updateTexture(gl, texture, image) {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
// WebGL1 has different requirements for power of 2 images
if (isPowerOf2(image.width) && isPowerOf2(image.height)) {
gl.generateMipmap(gl.TEXTURE_2D); // Generate mips.
} else {
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);
}
}
// ------------------------------------------------------------------------------
// Initialize a texture from the rendering of a canvas
function createTextureFromCanvas(canvasGL, canvasImg) {
const gl = canvasGL.getContext("webgl2");
const texture = gl.createTexture();
updateTexture(gl, texture, canvasImg);
return texture;
}
// ------------------------------------------------------------------------------------
function loadShader(url, callback) {
var xhr = new XMLHttpRequest();
// xhr.overrideMimeType("x-shader/x-fragment"); // not usefull
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
callback(xhr.responseText);
}
};
xhr.open("GET", url, true);
xhr.send();
}
/* Multi loading (not used)
function loadShaders(urls, callback) {
let results = [];
let loadedCount = 0;
for (let i = 0; i < urls.length; i++) {
let xhr = new XMLHttpRequest();
xhr.overrideMimeType("x-shader/x-fragment");
xhr.open("GET", urls[i], true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
results[i] = JSON.parse(xhr.responseText);
loadedCount++;
if (loadedCount === urls.length) {
callback(results);
}
}
};
xhr.send();
}
}
*/