KEMBAR78
"Graphical fun With WebGL shaders", Martin Splitt | PDF
@g33konaut
Creative experiments
With WebGL
@g33konaut
Our plan for today...
1. What is WebGL?
@g33konaut
Our plan for today...
1. What is WebGL?
2. Shaders ftw
@g33konaut
Our plan for today...
1. What is WebGL?
2. Shaders ftw
3. Let’s write some shaders!
@g33konaut
What is WebGL?
@g33konaut
Why do we need a GPU?
CPU GPU
@g33konaut
Why do we need a GPU?
CPU GPU
for(var i=0; i<10; i++) {
if(data[i] == MAGIC_VALUE) {
data[i+offset] = data[i] / data[i+1]
}
}
@g33konaut
Why do we need a GPU?
CPU GPU
@g33konaut
This is what WebGL can do...
@g33konaut
What do we need for our WebGL laboratory?
1. A <canvas>
@g33konaut
What do we need for our WebGL laboratory?
1. A <canvas>
2. A WebGL context
canvas.getContext(‘webgl’);
@g33konaut
What do we need for our WebGL laboratory?
1. A <canvas>
2. A WebGL context
canvas.getContext(‘webgl’);
3. Vertices, Shaders, ...
@g33konaut
@g33konaut
This is how WebGL works
Vertices GLBuffers
Vertex
Shader
Fragment
Shader
Pixels
GLSL code
@g33konaut
Vertices? Buffers? Shaders? GLSL? WAT?!
@g33konaut
Breathe. It will be fine. I promise.
@g33konaut
Breathe. It will be fine. I promise.
@g33konaut
Breathe. It will be fine. I promise.
@g33konaut
Okay, let’s talk about vertices
@g33konaut
Vertices are points in 3D space.
@g33konaut
We can also use 2D coordinates.
(1,1)
(-1,1)
(-1,-1) (1,-1)
@g33konaut
(1,1)
(-1,1)
(-1,-1) (1,-1)
We put them into a TypedArray
const vertices = new Float32Array([
-1.0, 1.0,
1.0, 1.0,
1.0, -1.0,
-1.0, -1.0
]);
@g33konaut
A buffer moves the data to the GPU memory
// create an empty buffer
const vBuf = gl.createBuffer();
@g33konaut
A buffer moves the data to the GPU memory
// create an empty buffer
const vBuf = gl.createBuffer();
// select vBuf as the current ARRAY_BUFFER
gl.bindBuffer(gl.ARRAY_BUFFER, vBuf);
@g33konaut
A buffer moves the data to the GPU memory
// create an empty buffer
const vBuf = gl.createBuffer();
// select vBuf as the current ARRAY_BUFFER
gl.bindBuffer(gl.ARRAY_BUFFER, vBuf);
// copy the TypedArray into the buffer
gl.bufferData(gl.ARRAY_BUFFER, vrtx, gl.STATIC_DRAW);
@g33konaut
So what happens here?
A.x
A.y
B.x
B.y
...
...
...
GL
Buffer
(Vertices)
@g33konaut
So what happens here?
A.x
A.y
B.x
B.y
...
...
...
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
@g33konaut
So what happens here?
A.x
A.y
B.x
B.y
...
...
...
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Fragment
shader
Fragment
shader
Fragment
shader
Fragment
shader
@g33konaut
So what happens here?
A.x
A.y
B.x
B.y
...
...
...
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Vertex
shader
Fragment
shader
Fragment
shader
Fragment
shader
Fragment
shader
@g33konaut
Let’s make some shaders!
@g33konaut
A shader is a function, run on the GPU
function (x, y, p1, p2, ...) {
}
@g33konaut
A vertex shader returns coordinates to draw at
function (x, y, p1, p2, ...) {
// do stuff
return [x, y];
}
@g33konaut
A fragment shader returns the colour to draw
function (x, y, p1, p2, ...) {
// do stuff
return [red, green, blue, alpha];
}
@g33konaut
The 4 types of inputs for shaders
● const A constant
@g33konaut
The 4 types of inputs for shaders
● const A constant
● uniform A “global” variable
@g33konaut
The 4 types of inputs for shaders
● const A constant
● uniform A “global” variable
● attribute A per-vertex variable
@g33konaut
The 4 types of inputs for shaders
● const A constant
● uniform A “global” variable
● attribute A per-vertex variable
● varying Interpolated variable
(vertex write, fragment read)
@g33konaut
Enough talking, time to code!
avgp.github.io/shaderpad
@g33konaut
Go and play with shaders!

"Graphical fun With WebGL shaders", Martin Splitt