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 checkedUniforms,Geometry,Meshes in a scene graph ofObjs, perspective and orthographicCameras,Textures andRenderTargets. - Geometry. Primitives such as
Box,Sphere,TorusandCapsule;PathandCurvefor drawing in 3D;Tube,ExtrudeandLatheto 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 sharedLights, withShadowmaps, and the same lighting as GLSL chunks for your own shaders. - Loading.
GLTFLoaderfor models from Blender and other tools, andTexture.fromURL. - Interaction.
Raycastfor picking meshes and instances. - Simulation.
InstancedMeshfor many copies of a mesh, andTransformFeedbackto simulate on the GPU. - Recipes. Ready-made setups:
FragmentShaderfor a full-screen shader,Stagefor a 3D scene with an orbiting camera, lights and shadows,GPUSimulationfor particles and other simulations in float textures, andScrollRendererto draw many scenes, each fitted to an element, on one canvas. - React.
@wethegit/react-wtc-glwrapsScrollRendererin 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.
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.