Web Development
Three.js WebGPU and TSL: Your First Animated Scene Without a Bundler
Your first 3D scene with WebGPU does not require learning GLSL or setting up a bundler. With three.js r186, two files are enough: an index.html with an import map and a main.js. In this tutorial we build a sphere that ripples and shifts color, with the whole shader written in JavaScript.
Two pieces make this possible: WebGPURenderer and TSL. The renderer uses WebGPU when the browser has it and drops to WebGL 2 on its own when it does not. The official manual puts it this way: "If a device/browser doesn't support WebGPU, the renderer can automatically fall back to using a WebGL 2 backend." TSL is the three.js shading language: you write nodes in JavaScript and the library turns them into WGSL or GLSL depending on the active backend. One codebase runs in both worlds. We will go step by step through the exact version, the file layout, the local server, the scene, every TSL node and the mistakes that waste the most time. It is written for developers who know JavaScript and have never touched a shader.
What WebGPURenderer and TSL are
WebGPURenderer is the three.js renderer built for WebGPU. According to the r186 manual, "WebGPURenderer uses a WebGPU backend by default and a WebGL 2 backend as a fallback." You do not detect browser support or maintain two code paths: the renderer picks the backend when it starts.
TSL (Three.js Shading Language) is what keeps that backend switch out of your shader code. The manual describes it like this: "With TSL, developers can write shader code with JavaScript in a platform-independent manner. Shader code written in TSL can be transpiled to WGSL or GLSL depending on the available backend." In practice, instead of writing a shader as a string in another language, you compose JavaScript functions such as sin() or mix() and assign the result to a material.
Before going further, see where this leads. The animation below runs live in your browser and uses the same building blocks (three.js r186, WebGPURenderer and TSL): a vase on a potter's wheel whose shape and glaze are computed on the GPU with TSL nodes. Move your pointer over it and the clay dents where you touch it. The label in the corner shows which backend your browser used.
The version: three.js r186, pinned
This tutorial uses three.js r186, published on npm as three@0.186.1 on September 24, 2026. Every URL in the example carries that exact version. Avoid unversioned URLs: a new release can deprecate an API without you touching your code, as happened to Clock in r183.
A bit of history explains the import layout. Since r171 (November 2024), WebGPURenderer has had its own entry point, three/webgpu, and TSL its own, three/tsl. The renderer uses WebGPU when available and WebGL 2 otherwise, with no separate code. The r171 release notes sum it up in one line: "Codesplit WebGL/WebGPU entrypoints."
That split does require one change on your side. The r186 manual asks you to import from three/webgpu instead of three:
- import * as THREE from "three";
+ import * as THREE from "three/webgpu";
The layout: two files and an import map
Without a bundler, the browser needs to know which URL each imported name points to. That is what an import map is for: a JSON block inside the HTML that maps "three/webgpu" to a concrete address. Here is the full index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>First WebGPU scene</title>
<style>body { margin: 0; background: #0e1b3a; }</style>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.186.1/build/three.webgpu.js",
"three/webgpu": "https://unpkg.com/three@0.186.1/build/three.webgpu.js",
"three/tsl": "https://unpkg.com/three@0.186.1/build/three.tsl.js"
}
}
</script>
</head>
<body>
<script type="module" src="./main.js"></script>
</body>
</html>
| Import map entry | File | Purpose |
|---|---|---|
three/webgpu | three.webgpu.js | The renderer, node materials and the rest of three.js |
three/tsl | three.tsl.js | TSL functions: sin, mix, time and the rest |
three | three.webgpu.js | Any import of "three" gets the same copy |
The three/webgpu entry is required even if your own code only imported from three/tsl. In version 0.186.1, three.tsl.js starts with import { TSL } from 'three/webgpu';: it imports that name itself, and if the import map cannot resolve it, the module fails to load. All three entries point to the same version and the same CDN for a reason the installation manual spells out: "Import all dependencies from the same version of three.js, and from the same CDN. Mixing files from different sources may cause duplicate code to be included, or even break the application in unexpected ways."
For this tutorial, unpkg or jsDelivr both work as long as you do not mix them. If the project grows, a bundler such as Vite handles all of this through npm, but you do not need one to learn.
Opening it: you need a local server
A common first mistake is double-clicking index.html. Over file:// the example does not work. The installation manual warns about it: "While it's technically possible to double-click an HTML file and open it in your browser, important features that we'll later implement, do not work when the page is opened this way, for security reasons."
The fix is to serve the folder over HTTP. The manual suggests installing Node.js and running this inside the project folder:
npx serve .
Then open the local address the console prints. Any other local HTTP server works just as well.
Scene, camera and renderer
Here is the complete main.js. We take it apart below:
import * as THREE from "three/webgpu";
import { positionLocal, normalLocal, time, sin, mix, color, uniform } from "three/tsl";
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(50, innerWidth / innerHeight, 0.1, 100);
camera.position.z = 4;
const renderer = new THREE.WebGPURenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
await renderer.init();
console.log("Backend:", renderer.backend.isWebGPUBackend ? "WebGPU" : "WebGL 2");
const amplitude = uniform(0.15);
const wave = sin(positionLocal.y.mul(6).add(time.mul(2)));
const material = new THREE.MeshStandardNodeMaterial();
material.positionNode = positionLocal.add(normalLocal.mul(wave.mul(amplitude)));
material.colorNode = mix(color("#3fa9d6"), color("#6cbf91"), wave.mul(0.5).add(0.5));
scene.add(new THREE.Mesh(new THREE.SphereGeometry(1, 64, 64), material));
scene.add(new THREE.HemisphereLight(0xffffff, 0x333333, 2));
const timer = new THREE.Timer();
renderer.setAnimationLoop((timestamp) => {
timer.update(timestamp);
renderer.render(scene, camera);
});
The scene and camera are standard three.js. What is new lives in the renderer, and one detail deserves attention: WebGPU initializes asynchronously. The manual explains that setAnimationLoop() "will automatically ensure the renderer is initialized when rendering the first frame". If you only draw inside that loop, nothing else is needed.
If you drive the loop with window.requestAnimationFrame(), or use the renderer before the first frame, you need one extra line: await renderer.init();. Our example includes it because we want to log the backend before drawing anything. Without it, that log line would query a renderer that has not finished starting up.
TSL, node by node
In TSL every expression is a node, and nodes chain through methods such as .mul() (multiply) and .add() (add). Nothing is computed in JavaScript: you are describing a calculation that the GPU will run for every vertex and every pixel.
positionLocal: each vertex position in the object's own space, avec3attribute.normalLocal: the vertex normal, the direction pointing "out" of the surface.time: per the TSL docs, "Represents the elapsed time in seconds."sin(x): "Returns the sine of the parameter." It stays between -1 and 1.mix(a, b, t): blendsaandbbyt: 0 givesa, 1 givesb.color(): turns a hex color into a node.uniform(value): "TSL function for creating a uniform node." A uniform is a value the shader receives from JavaScript, the same for every vertex.
Now read the wave line from the inside out. positionLocal.y takes each vertex's height. .mul(6) sets how many ripples fit on the sphere. .add(time.mul(2)) shifts the ripple over time, with 2 as the speed. sin() turns all of that into a value between -1 and 1 that rises and falls.
That wave feeds both outputs of the material. positionNode gets the shape: each vertex moves along its normal by the wave times the amplitude, so the surface swells and sinks. colorNode gets the color: wave.mul(0.5).add(0.5) maps the wave from the -1 to 1 range into 0 to 1, which is what mix() expects, so crests and troughs get different colors. Both outputs belong to MeshStandardNodeMaterial, the node version of the standard material, which still reacts to the scene lighting.
One performance point worth absorbing early: all of this runs on the GPU, in parallel for every vertex. The sphere has 64 by 64 segments and there is no JavaScript loop walking through them. If you raise the geometry resolution tomorrow, the per-vertex work lands on the GPU, not on the page's main thread.
Animation: time and Timer
Notice that the loop never touches the material. The ripple moves on its own because it uses the time node, the elapsed time in seconds. That is the biggest mindset shift when moving to shaders: you do not animate vertices from JavaScript, you describe how they depend on time.
For logic that does live in JavaScript, such as moving the camera or reacting to input, use Timer. Its docs say that "Timer has an update() method that updates its internal state", and that it can use the Page Visibility API "to avoid large time delta values when the app is inactive". Skip Clock: since r183 its source says "This module has been deprecated. Please use THREE.Timer instead." In the example the Timer is already updated in the loop; when you add your own logic, read the frame time with timer.getDelta(), which the same Timer docs describe alongside update().
Which backend is running
The console.log line tells you: renderer.backend.isWebGPUBackend is true on WebGPU and false on WebGL 2. To check that your scene also works without WebGPU, force the fallback when creating the renderer:
const renderer = new THREE.WebGPURenderer({ antialias: true, forceWebGL: true });
The manual presents this option "for testing purposes or if you want to exclude the usage of WebGPU for certain reasons". In the team's test with Edge, this same example used WebGPU when available and WebGL 2 with WebGPU disabled, without changing a line of the shader. The console shows Backend: WebGPU or Backend: WebGL 2.
Common mistakes
If something breaks, check this list in order:
- Mixing
threewiththree/webgpu. If part of your code imports from"three"and another part from"three/webgpu", and the import map does not point both at the same file, you end up with two copies of the library. That is why thethreeentry also points tothree.webgpu.js. - Using different versions or CDNs.
three.webgpu.jsfrom one version withthree.tsl.jsfrom another, or one from unpkg and the other from jsDelivr: the manual warns this can duplicate code "or even break the application in unexpected ways". - Opening over
file://. Serve the folder withnpx serve .. - Forgetting
init()outsidesetAnimationLoop. If you use the renderer before the first frame or withrequestAnimationFrame, you needawait renderer.init();.
Frequently asked questions
Do I need to know GLSL to use TSL?
No. TSL is written in JavaScript and three.js translates it to WGSL or GLSL depending on the backend. Knowing what a vertex and a normal are helps more than knowing another language's syntax.
What happens if the browser has no WebGPU?
WebGPURenderer falls back to WebGL 2 automatically and your TSL code is translated to GLSL. You can test it with forceWebGL: true.
Why does it not work when I double-click the HTML file?
Because over file:// the browser blocks features the example needs, for security reasons. Serve the folder with npx serve ..
Conclusion
With two files, a correct import map and a pinned version, you get a WebGPU scene with a WebGL 2 fallback and a shader written in JavaScript. The API is not the hard part. The hard part is the mental switch: in TSL you describe calculations that run on the GPU, and time is just another node.
Copy the two files, serve them with npx serve . and tweak the wave numbers until you understand what each one does. Then try other nodes from the TSL docs. We will keep publishing browser graphics examples on the Nippysoft blog.
References
- three.js — WebGPURenderer manual (r186): github.com/mrdoob/three.js/blob/r186/manual/pages/webgpurenderer.html
- three.js — TSL documentation (r186): github.com/mrdoob/three.js/blob/r186/docs/pages/TSL.html.md
- three.js — Installation manual (r186): github.com/mrdoob/three.js/blob/r186/manual/pages/installation.html
- three.js — r186 release: github.com/mrdoob/three.js/releases/tag/r186
- three.js — r171 release, separate WebGL and WebGPU entry points: github.com/mrdoob/three.js/releases/tag/r171
- three.js — PR #29948, introducing three.tsl.js: github.com/mrdoob/three.js/pull/29948
- three.js — Timer source (r186): github.com/mrdoob/three.js/blob/r186/src/core/Timer.js
- three.js — MeshStandardNodeMaterial source (r186): github.com/mrdoob/three.js/blob/r186/src/materials/nodes/MeshStandardNodeMaterial.js
Tags
Share this post
Subscribe
Get the latest posts delivered right to your inbox.
Leave a comment