Lab

WebGL Shader Playground

A live GLSL fragment shader editor on raw WebGL, with presets, three controls, and compile errors that point at the line.

A fragment shader editor that runs your GLSL on the GPU as you type. Pick a preset, move the controls, or write your own.

What is in it

  • Four presets: ripples that follow the pointer, a spiral in polar coordinates, a plasma field made of summed sine waves, and moving Voronoi cells.
  • Live editing: the shader recompiles a quarter of a second after you stop typing.
  • Errors that point at the line: a compile error is listed with its line number, and clicking it selects that line in the editor. The last version that compiled keeps running in the meantime.
  • Three controls: intensity, speed and colour. Every preset uses all three.

The uniforms

Your shader can read five values:

uniform float uTime;       // seconds, scaled by the Speed slider, stopped by Pause
uniform vec2  uResolution; // canvas size in device pixels
uniform vec2  uMouse;      // pointer position in device pixels, origin bottom-left
uniform float uIntensity;  // the Intensity slider
uniform vec3  uColor;      // the Colour picker

How it works

The playground is raw WebGL, with no Three.js. One quad covering the canvas is drawn every frame, and all the visual work happens in the fragment shader.

  • No leaks on failed compiles. With a compile after most pauses in typing, most compiles fail. Every path out of the compile step deletes the shaders and program it created. The quad's vertex buffer is created once, not per compile.
  • A clock that does not jump. Time is accumulated frame by frame at the current speed. Pausing, changing the speed or recompiling leaves the animation where it was.
  • Sharp on high-density screens. The canvas buffer is sized to the screen's pixel density, capped at twice the CSS size so a phone is not asked for nine times the pixels.
  • Context loss. If the browser takes the WebGL context away, the playground rebuilds its buffer and recompiles the last good shader when the context returns.
  • Idle when unseen. Nothing is drawn while the canvas is scrolled off screen or the tab is in the background. With reduced motion requested, it starts paused on a still frame.

Things to try

  • Change the 40.0 in the Ripples preset. Small constants make large visual differences.
  • Break the shader on purpose to see the error list.
  • In Voronoi, replace 1.0 - nearest with nearest to invert the cells.
  • Write a shape from a signed distance function. uTime and uMouse are already there to animate it.

WebGL · GLSL · Shaders · Real-time