Documentation

TSL Material Graph is a visual node editor for Three.js TSL materials and a small runtime that turns the saved graphs into NodeMaterials.

Getting started

1. Install

npm install tsl-material-graph three

2. Add the Vite plugin

// vite.config.ts
import { defineConfig } from 'vite';
import { shaderGraph } from 'tsl-material-graph/vite';

export default defineConfig({
  plugins: [shaderGraph({ dir: 'src/shaders' })],
});

3. Open the editor

Run your usual npm run dev. The terminal prints the editor link right below Vite's own:

  ➜  Local:   http://localhost:5173/
  ➜  Material Graph: http://localhost:5173/__shadergraph/

Create a shader and press Save (Ctrl+S): the JSON is written to src/shaders/.

4. Use it in your scene

import * as THREE from 'three/webgpu';
import { createShaderGraphMaterial } from 'tsl-material-graph';
import graph from './shaders/Hologram.json';

const shader = createShaderGraphMaterial(graph);
mesh.material = shader.material;

Requirements

Vite plugin

The plugin adds the editor to your dev server at /__shadergraph/ and a small file API that reads and writes the graphs in your project. It only runs in dev (vite serve): nothing is added to your production build.

OptionDefaultDescription
dir'src/shaders'Folder with the shader graph JSON files, relative to the project root. Created on the first save.
rootVite rootProject root used to resolve dir.

The plugin also sets resolve.dedupe: ['three'], so the library and your project always share one copy of Three.js. The file API only accepts .json files inside dir.

CLI (without Vite)

For projects that don't use Vite, run the editor on its own. It saves into the same kind of folder and opens your browser:

npx tsl-material-graph --dir src/shaders
FlagDefaultDescription
--dirsrc/shadersFolder with the JSON files.
--port5199Port, or the next free one.
--no-openDon't open the browser.

Hot reload in your scene

Importing the JSON as a module lets Vite reload it when the editor saves. Accept the update to swap the material without reloading the page:

import graph from './shaders/Hologram.json';

let shader = createShaderGraphMaterial(graph);
mesh.material = shader.material;

if (import.meta.hot) {
  import.meta.hot.accept('./shaders/Hologram.json', (mod) => {
    const old = shader;
    shader = createShaderGraphMaterial(mod.default);
    mesh.material = shader.material;
    old.dispose();
  });
}

Loading a graph

Import the JSON (bundled with your code) or fetch it at runtime:

import { createShaderGraphMaterial, loadShaderGraph } from 'tsl-material-graph';

// Bundled
const a = createShaderGraphMaterial(graph);

// Fetched (relative texture URLs resolve against the JSON's URL)
const b = await loadShaderGraph('/shaders/Lava.json');

One compiled shader can be shared by many meshes. The Usage panel in the editor always shows the exact snippet for the current graph.

Usage panel

Properties

Blackboard properties become uniforms: change them at runtime without recompiling the shader.

shader.set('Tint', '#ff5a2b');      // color: hex string, number or THREE.Color
shader.set('Speed', 1.5);          // float
shader.set('Offset', [0.5, 0.2]);   // vec2 / vec3 / vec4: array or THREE.Vector*
shader.set('Albedo', texture);      // texture

shader.properties;           // { Tint: 'color', Speed: 'float', ... }
shader.get('Speed');           // current value

Textures

Images uploaded in the editor are embedded in the JSON as data URLs, which is convenient but makes files larger. For production, use a URL in the property (relative URLs resolve against the JSON when using loadShaderGraph) or pass textures in code:

const shader = createShaderGraphMaterial(graph, {
  textures: { Albedo: new THREE.TextureLoader().load('/albedo.png') },
});

API reference

createShaderGraphMaterial(graph, options?)

Compiles a graph and returns a CompiledShader. Options: textures (by property name), baseUrl (for relative texture URLs), textureLoader.

loadShaderGraph(url, options?)

Fetches a JSON graph and calls createShaderGraphMaterial. Returns a promise.

CompiledShader

MemberDescription
materialThe NodeMaterial (MeshStandardNodeMaterial for Lit, MeshBasicNodeMaterial for Unlit).
propertiesExposed property names and their types.
set(name, value)Updates a property without recompiling.
get(name)Current value of a property.
dispose()Disposes the material.

compileGraph(graph, options?)

Lower-level compiler used by the functions above. Options: resolveTexture(prop). Throws GraphCompileError (with the offending nodeId) when the graph is invalid.

validateMaterial(renderer, material, geometry, camera, scene)

Compiles the material on the GPU and rejects if the generated WGSL/GLSL is invalid. Useful in tools and tests.

defineNodes(defs), getNodeDef(type), allNodeDefs()

The node registry. See Custom nodes.

The editor

Graph Inspector and Blackboard on the left, the graph in the middle, the 3D preview and the Usage snippet on the right. Hover the ? icon in the top-right corner for every shortcut.

The editor

Try it now, nothing to install: open the editor (in the browser, Save downloads the JSON).

Building a graph

Node previews

Every node shows what it outputs: on a quad, or on a sphere when it depends on position or normals. Nodes with Time upstream animate. Toggle a preview with the bar at the bottom of the node, or all of them with Previews in the toolbar.

Node previews

Blackboard

Properties exposed to your code: Float (with a min/max slider), Vector2/3/4, Color and Texture2D. Drag one onto the canvas to read it in the graph.

Blackboard

Arrange, groups, collapse

Project mode

Opened through the Vite plugin or the CLI, the editor works on your project's files: Open lists the shaders in the folder, Save writes them back, and the file you're editing is shown in the top-right corner (a dot means unsaved changes).

Editor in project mode

Keyboard shortcuts

ActionShortcut
Create nodeRight-click, Space
Delete selectionDelete, Backspace
Copy · Paste · DuplicateCtrl+C Ctrl+V Ctrl+D
Undo · RedoCtrl+Z Ctrl+Y
SaveCtrl+S
Arrange nodesCtrl+L
Group · UngroupCtrl+G Ctrl+Shift+G
Add to selectionCtrl+click Shift+click
Pan viewMiddle mouse, Alt+drag

JSON format

{
  "version": 1,
  "name": "Hologram",
  "settings": { "surface": "lit", "transparent": false, "side": "front", "alphaTest": 0, "wireframe": false },
  "properties": [{ "id": "p_tint", "name": "Tint", "type": "color", "value": "#2f8cff" }],
  "nodes": [{ "id": "lerp", "type": "math/lerp", "position": { "x": 440, "y": -60 }, "values": { "t": 0.5 } }],
  "edges": [{ "id": "e9", "source": "lerp", "sourceHandle": "out", "target": "master", "targetHandle": "baseColor" }]
}

The types are exported by the package (ShaderGraphJSON, GraphNode, GraphEdge, GraphProperty...). The groups, preview, collapsed and route fields are used by the editor only and ignored by the runtime.

Custom nodes

A node is a definition with ports and a build function that returns TSL nodes. Register it before compiling a graph that uses it:

import { defineNodes } from 'tsl-material-graph';
import { sin } from 'three/tsl';

defineNodes([{
  type: 'custom/wave',
  label: 'Wave',
  category: 'Procedural',
  inputs: [
    { id: 'in', label: 'In', type: 'float', default: 0 },
    { id: 'freq', label: 'Frequency', type: 'float', default: 4 },
  ],
  outputs: [{ id: 'out', label: 'Out', type: 'float' }],
  build: (i) => ({ out: sin(i.in.mul(i.freq)).mul(0.5).add(0.5) }),
}]);

Node reference

Generated from the library's node registry.

Loading the node list…

FAQ

Does it work without WebGPU?

Yes. WebGPURenderer falls back to WebGL2, and every node is tested on both backends.

Does the editor end up in my production build?

No. The Vite plugin only runs in dev, and your code only imports the runtime (about 27 KB, plus Three.js).

Can I use it with React Three Fiber?

Yes: the runtime returns a regular Three.js material. Assign shader.material to your mesh.