A WebGL dither shader in one triangle

How a full-screen triangle can draw a flat procedural effect, why the fragment shader does the visible work and where raw WebGL adds maintenance.

On this page

A hero effect can be a flat procedural pattern. Its appearance is calculated in a fragment shader, and it doesn’t need a camera moving around a set of 3D objects.

That makes it a reasonable candidate for plain WebGL. One implementation draws a single triangle covering the canvas and lets the shader calculate the visible pixels. That’s a narrower job than a general-purpose 3D scene.

Why the triangle extends beyond the screen

WebGL’s clip-space viewport runs from -1 to 1 along each axis. These three vertices cover it:

const vertices = new Float32Array([
  -1, -1,
   3, -1,
  -1,  3,
]);

The part of the triangle outside the viewport gets clipped. One oversized triangle avoids needing two triangles to form a rectangle for this pass.

The triangle isn’t the decorative picture. It supplies the fragments the pixel calculation runs on. Drawing it once doesn’t make the shader’s work negligible, because every covered pixel still has a cost.

The vertex shader can stay simple

For a WebGL2 implementation with an attribute named position, the vertex stage can simply place each vertex:

#version 300 es
in vec2 position;

void main() {
  gl_Position = vec4(position, 0.0, 1.0);
}

This snippet assumes the buffer and attribute have been configured. It’s one part of a renderer, not a complete effect you can paste in and publish.

The fragment stage then uses pixel coordinates, resolution and any time input to produce the pattern. A dithered result compares an underlying brightness value with a repeating threshold pattern, which turns smooth values into a spatial arrangement of light and dark pixels.

Noise and thresholding are separate choices. You can change the animation without changing the pattern scale, or change the dither scale without making the geometry more complicated.

Resolution is part of the design

Canvas dimensions and CSS dimensions are different things. Rendering at the full device-pixel ratio can sharpen an effect, but it increases the number of fragments considerably.

For a deliberately pixelated band, a lower rendering resolution may suit the design. That kind of hero implementation uses its CSS dimensions as the drawing-buffer dimensions, which keeps its chosen dither scale.

Test that choice visually. It isn’t a universal recommendation for charts, crisp text or canvas work in general. Resize handling also has to update the viewport and resolution uniform together.

Animation needs a lifecycle

Use an elapsed-time input rather than advancing by a fixed amount each frame. Different refresh rates shouldn’t change the intended speed.

For reduced motion, draw a suitable still frame. Pause decorative work when it isn’t useful, and resume it when the canvas returns to view. Avoid creating a new animation loop every time an observer fires. The motion article covers the broader reasoning behind those choices.

Check shader compilation and program linking. If WebGL is unavailable or initialisation fails, the page should still be complete. A decorative band suits a quiet static fallback; an essential visualisation needs a more informative alternative.

Raw WebGL is a trade-off

A small specialised renderer can avoid loading a broader library for one effect. It also makes your code responsible for buffers, shader errors, resizing, context loss and cleanup.

If the page already needs three.js for real scene objects, materials and assets, replacing it may create more work than it removes. Let the project’s requirements and measured output decide.

This article doesn’t claim a byte saving or a faster render, because those need a build comparison under stated conditions. The design point is simpler: when the visible work is entirely a flat shader, the renderer can be correspondingly small in scope. The three.js lazy-loading article covers the case where a full scene is involved.

References

  • Interaction design
  • Performance