-
Notifications
You must be signed in to change notification settings - Fork 0
/
display-webgl-shader.js
291 lines (241 loc) · 9.66 KB
/
display-webgl-shader.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'use strict';
/**
* Display WebGL Shader WordPress Block.
*
* https://mrzebra.co.uk/
*
* Copyright (c) Zebra North, 2021
*/
/**
* Create a <canvas> element and display a shader in it.
*
* @param {String} fragmentShaderSource The source code for the fragment shader.
*/
function webglShader(fragmentShaderSource)
{
// This is used to assign each shader on the page a unique ID.
let id = 1;
/**
* Create a WebGL2 rendering context on the given canvas.
*
* @param {DOMElement} canvas The <canvas> element.
*
* @returns {WebGL2RenderingContext} Returns the rendering context.
*
* @throws {Error} Thrown if the canvas element does not exist or WebGL2 is not supported.
*/
const createContext = function (canvas)
{
if (!canvas)
throw new Error('Could not find canvas element');
const context = canvas.getContext('webgl2');
if (!context)
throw new Error('Failed to get webgl context, webgl is not supported by the browser');
return context;
};
/**
* Build a compiled shader from source code.
*
* @param {WebGL2RenderingContext} gl The WebGL context.
* @param {Number} type The type of shader, either gl.VERTEX_SHADER or gl.FRAGMENT_SHADER.
* @param {String} id The ID attribute of the <script> element containing the shader's source code.
*
* @returns {WebGLShader} Returns the shader.
*
* @throws {Error} Thrown if compilation fails, eg due to a syntax error in the source code.
*/
const createShader = function (gl, type, source)
{
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
{
const msg = 'Failed to compile shader ' + id + ': ' + gl.getShaderInfoLog(shader);
gl.deleteShader(shader);
throw new Error(msg);
}
return shader;
};
/**
* Get the source code for the vertex shader.
* This multiplies the UVs by iResolution for compatibility
* with Shadertoy.
*
* @returns {String} Returns shader source.
*/
const getVertexShaderSource = function ()
{
return `#version 300 es
precision mediump float;
in vec4 vertex;
in vec2 uv;
uniform mat4 modelView;
uniform mat4 projection;
uniform vec2 iResolution;
out vec2 vUv;
out vec4 vSurface;
void main()
{
gl_Position = projection * modelView * vertex;
gl_PointSize = 2.0;
vUv = uv * iResolution;
vSurface = vertex;
}`;
}
/**
* Get the source code for the fragment shader.
* This takes the source code from the input block and adds
* the necessary boilerplate around it.
*
* @param {String} source The source code from the input block.
*
* @returns {String} Returns shader source.
*/
const getFragmentShaderSource = function (source)
{
return `#version 300 es
precision mediump float;
in vec2 vUv;
out vec4 fragColor;
uniform float iTime;
uniform vec2 iResolution;
` + source + `
void main()
{
vec4 colour = vec4(1.0, 0.0, 0.0, 1.0);
mainImage(colour, vUv);
fragColor = colour;
}`;
}
/**
* Link shaders together into a program.
*
* @param {WebGL2RenderingContext} gl The WebGL context.
* @param {Array} shaders An array of WebGLShader.
*
* @returns {WebGLProgram} Returns a set of linked shaders.
*
* @throws {Error} Thrown if linking fails.
*/
const createProgram = function (gl, shaders)
{
const program = gl.createProgram();
for (const shader of shaders)
gl.attachShader(program, shader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS))
{
const msg = 'Failed to create GL program: ' + gl.getProgramInfoLog(program);
gl.deleteProgram(program);
throw new Error(msg);
}
return program;
};
/**
* Render the scene for a single frame.
*
* @param {WebGL2RenderingContext} gl The WebGL2 render context.
* @param {Canvas} canvas The <canvas> DOM element to which the scene is rendered.
* @param {Scene} scene The scene to draw.
*/
const draw = function (gl, canvas, scene)
{
// Clear the screen.
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.disable(gl.DEPTH_TEST);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Set up an orthographic camera.
const projectionMatrix = glMatrix.mat4.create();
glMatrix.mat4.ortho(projectionMatrix, 0, 1, 0, 1, 0.01, 200.0);
let modelView = glMatrix.mat4.create();
glMatrix.mat4.translate(modelView, modelView, [1, 0, 0]);
let target = glMatrix.vec3.create();
let up = glMatrix.vec3.create();
up[1] = 1;
glMatrix.mat4.lookAt(modelView, [0, 0, -1], target, up);
gl.useProgram(scene.shader.program);
// Set the shader program uniforms: the projection matrix, modelview matrix, time, and resolution.
gl.uniformMatrix4fv(scene.shader.uniforms.projection, false, projectionMatrix);
gl.uniformMatrix4fv(scene.shader.uniforms.modelView, false, modelView);
gl.uniform1f(scene.shader.uniforms.time, (Date.now() % 100000) / 1000);
gl.uniform2f(scene.shader.uniforms.resolution, canvas.clientWidth, canvas.clientHeight);
// Load the quad.
gl.enableVertexAttribArray(scene.shader.attributes.vertex);
gl.bindBuffer(gl.ARRAY_BUFFER, scene.buffers.vertex);
gl.vertexAttribPointer(scene.shader.attributes.vertex, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(scene.shader.attributes.uv);
gl.bindBuffer(gl.ARRAY_BUFFER, scene.buffers.uv);
gl.vertexAttribPointer(scene.shader.attributes.uv, 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, scene.buffers.index);
// Draw the quad.
gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_SHORT, 0);
};
/**
* Run the shader.
*
* @param {String} canvasId The ID attribute of the <canvas> tag that will display the shader.
* @param {String} vertexShaderSource The source code for the vertex shader.
* @param {String} fragmentShaderSource The source code for the fragment shader.
*/
function main(canvasId, vertexShaderSource, fragmentShaderSource)
{
const glCanvas = document.getElementById(canvasId);
const gl = createContext(glCanvas);
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
const program = createProgram(gl, [vertexShader, fragmentShader]);
const scene = {
shader: {
program: program,
uniforms: {
projection: gl.getUniformLocation(program, 'projection'),
modelView: gl.getUniformLocation(program, 'modelView'),
time: gl.getUniformLocation(program, 'iTime'),
resolution: gl.getUniformLocation(program, 'iResolution'),
},
attributes: {
vertex: gl.getAttribLocation(program, 'vertex'),
uv: gl.getAttribLocation(program, 'uv'),
},
},
buffers: {
vertex: gl.createBuffer(),
uv: gl.createBuffer(),
index: gl.createBuffer(),
},
geometry: {
vertices: [],
uvs: [],
indices: [],
}
}
// Create a mesh containing a single quad in the XY plane.
scene.geometry.vertices.push(0, 0, 1);
scene.geometry.vertices.push(0, 1, 1);
scene.geometry.vertices.push(-1, 0, 1);
scene.geometry.vertices.push(-1, 1, 1);
scene.geometry.uvs.push(0, 0);
scene.geometry.uvs.push(0, 1);
scene.geometry.uvs.push(1, 0);
scene.geometry.uvs.push(1, 1);
scene.geometry.indices.push(0);
scene.geometry.indices.push(1);
scene.geometry.indices.push(2);
scene.geometry.indices.push(3);
// Load the geometry into the WebGL buffers.
gl.bindBuffer(gl.ARRAY_BUFFER, scene.buffers.vertex);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(scene.geometry.vertices), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, scene.buffers.uv);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(scene.geometry.uvs), gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, scene.buffers.index);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(scene.geometry.indices), gl.STATIC_DRAW);
// Draw the frame.
window.setInterval(() => draw(gl, glCanvas, scene), 16.66);
}
// Create a <canvas> element on which to draw the shader.
const canvasId = 'display-webgl-shader-' + id++;
document.write('<canvas id="' + canvasId + '" class="zebra-north-display-webgl-shader" width="640" height="360"></canvas>')
// Initialise the shader when the page has finished loading.
window.addEventListener('DOMContentLoaded', () => main(canvasId, getVertexShaderSource(), getFragmentShaderSource(fragmentShaderSource)));
}