Skip to content

Getting started

Introduction ​

wtc-gl is a small WebGL2 library for the shader-driven parts of websites: full-screen effects, scenes synced to DOM elements as the page scrolls, and the occasional 3D model.

It's descended from OGL, with names familiar from three.js. You write the GLSL yourself, and wtc-gl handles the WebGL around it: programs and uniforms, geometry and buffers, the scene graph, cameras, render targets, and context loss.

What's in it ​

  • The core. A Renderer, Programs with checked Uniforms, Geometry, Meshes in a scene graph of Objs, perspective and orthographic Cameras, Textures and RenderTargets.
  • Geometry. Primitives such as Box, Sphere, Torus and Capsule; Path and Curve for drawing in 3D; Tube, Extrude and Lathe to turn them into solids; CPU tools to merge, weld and transform geometry; and CSG through manifold-3d.
  • Lines. Polyline, lines with real width: tapers, dashes, flat ribbons and trails.
  • Lighting. LitProgram, a Blinn-Phong material lit by a shared Lights, with Shadow maps, and the same lighting as GLSL chunks for your own shaders.
  • Loading. GLTFLoader for models from Blender and other tools, and Texture.fromURL.
  • Interaction. Raycast for picking meshes and instances.
  • Simulation. InstancedMesh for many copies of a mesh, and TransformFeedback to simulate on the GPU.
  • Recipes. Ready-made setups: FragmentShader for a full-screen shader, Stage for a 3D scene with an orbiting camera, lights and shadows, GPUSimulation for particles and other simulations in float textures, and ScrollRenderer to draw many scenes, each fitted to an element, on one canvas.
  • React. @wethegit/react-wtc-gl wraps ScrollRenderer in a provider and hooks.

It also re-exports wtc-math, so vectors and matrices come from the same import.

When to use it ​

wtc-gl suits sites where the WebGL is mostly yours: custom shaders, effects on images and sections, generative graphics, and a few models. It stays small and close to WebGL, so it's easy to see what each frame draws.

For a full 3D engine, with physically based materials, animation, post-processing stacks and a large ecosystem of loaders, use three.js instead.

A first look ​

Every scene follows the same shape: make a renderer, put meshes in a scene, and render it with a camera each frame.

js
import {
  Renderer,
  Camera,
  Obj,
  Mesh,
  Box,
  LitProgram,
  Lights,
  Vec2
} from 'wtc-gl'

const renderer = new Renderer({ clearColour: [0.95, 0.96, 0.97, 1] })
const { gl } = renderer
// The renderer sizes the drawing buffer; CSS sizes the canvas on the page
Object.assign(gl.canvas.style, {
  position: 'fixed',
  inset: '0',
  width: '100%',
  height: '100%'
})
document.body.appendChild(gl.canvas)

const camera = new Camera({ fov: 35 })
camera.position.z = 5

const scene = new Obj()
const lights = new Lights()
const box = new Mesh(gl, {
  geometry: new Box(gl),
  program: new LitProgram(gl, { lights, colour: [0.29, 0.62, 1] })
})
box.setParent(scene)

const resize = () => {
  renderer.dimensions = new Vec2(innerWidth, innerHeight)
  camera.perspective({ aspect: innerWidth / innerHeight })
}
addEventListener('resize', resize)
resize()

requestAnimationFrame(function frame(t) {
  requestAnimationFrame(frame)
  box.rotation.y = t * 0.001
  box.updateRotation()
  renderer.render({ scene, camera })
})

Start with installation, then your first shader.