Your first shader
Draw a full-screen fragment shader with the FragmentShader recipe.
FragmentShader sets up everything a single shader needs: a renderer and canvas, a triangle that covers it, a render loop, and two uniforms kept up to date, u_time and u_resolution. You supply the fragment shader.
import { FragmentShader, Vec2 } from 'wtc-gl'
// A full-screen fragment shader: FragmentShader makes the renderer, a
// triangle that covers the canvas and the render loop, and keeps u_time and
// u_resolution up to date.
const vertex = /* glsl */ `#version 300 es
in vec3 position;
in vec2 uv;
out vec2 v_uv;
void main() {
v_uv = uv;
gl_Position = vec4(position, 1.0);
}`
const fragment = /* glsl */ `#version 300 es
precision highp float;
uniform vec2 u_resolution;
uniform float u_time;
in vec2 v_uv;
out vec4 colour;
void main() {
// Centred, aspect-correct coordinates
vec2 p = (gl_FragCoord.xy - 0.5 * u_resolution) / u_resolution.y;
float t = u_time * 20.0;
float rings = sin(length(p) * 24.0 - t * 3.0);
vec3 a = vec3(0.29, 0.62, 1.0);
vec3 b = vec3(0.95, 0.42, 0.26);
colour = vec4(mix(a, b, smoothstep(-0.3, 0.3, rings + p.x)), 1.0);
}`
export default function (container) {
const shader = new FragmentShader({
vertex,
fragment,
container,
// Sized to the container below, rather than the window
autoResize: false
})
// The page drives the frames, so it can pause off screen
shader.playing = false
return {
frame(time) {
shader.render(time * 1000)
},
resize(width, height) {
shader.dimensions = new Vec2(width, height)
shader.renderer.dimensions = shader.dimensions
shader.u_resolution.value = shader.dimensions.scaleNew(
shader.renderer.dpr
).array
},
dispose() {
shader.destroy()
}
}
}On a page of its own, the shader can fill the window. Leave out container and autoResize, and FragmentShader appends its canvas to the body and resizes with the window. It sizes the drawing buffer, so give the canvas its size on the page with CSS, for example canvas { position: fixed; inset: 0; width: 100%; height: 100% }:
import { FragmentShader } from 'wtc-gl'
import fragment from './effect.frag'
new FragmentShader({ fragment })The shaders
The fragment shader runs once for every pixel of the canvas.
u_resolutionis the canvas size in device pixels, sogl_FragCoord.xy / u_resolutionruns from 0 to 1 across it.u_timegrows steadily while the shader plays.
The default vertex shader is GLSL 1.00. To write the fragment shader in GLSL 3.00 (#version 300 es, with in, out and a named output), pass a matching vertex shader too, as the example does.
Your own uniforms
Pass more uniforms with uniforms, and change their values in onBeforeRender:
import { FragmentShader, Uniform } from 'wtc-gl'
const pointer = new Uniform({
name: 'pointer',
value: [0, 0],
kind: 'float_vec2'
})
const shader = new FragmentShader({
fragment,
uniforms: { u_pointer: pointer }
})
addEventListener('pointermove', (e) => {
pointer.value = [e.clientX / innerWidth, 1 - e.clientY / innerHeight]
})onBeforeRender(time) is called before each frame with the requestAnimationFrame timestamp in milliseconds, for values that change every frame rather than on an event.
Playing and stopping
shader.playing starts and stops the render loop. shader.destroy() stops it for good, and frees the canvas and its WebGL context.
The FragmentShader recipe covers every option, including post for feedback effects.