Triode 2D signal graph
Import createSignalFactory and transformDefinitions from triode/graph to build Triode chains in Node or a browser. This entry point uses JavaScript only. The rendering host owns textures, clock, frame order and shader emission.
import { createSignalFactory, transformDefinitions, type SignalGraph } from 'triode/graph';
let graph: SignalGraph<string | null> | undefined;
const o0 = {
id: 'o0',
setGraph(candidate: SignalGraph<string | null>) {
graph = candidate;
},
};
const { generators: { osc, src } } = createSignalFactory(transformDefinitions, {
defaultOutput: o0,
textureReference: slot => slot === o0 ? o0.id : null,
});
osc(8).blend(src(o0), 0.85).out(o0);
Each graph has version: 1, an ordered operations array and an optional final swizzle. Each operation has a transform name, an effective type, and ordered inputs with name, type and a tagged value. Combine and coordinate-combine transforms take the second signal as their first input. Overloaded transforms use the coordinate form at the start of a chain and the color form later in the chain.
| Input kind | Host responsibility |
|---|---|
constant | Read value. Vector literals use arrays. |
signal | Recursively compile its graph. |
texture | Bind its reference at draw time. textureReference controls the reference type. |
dynamic | Call evaluate(frame) at draw time and apply fallback when needed. |
sequence | Resolve values at the current time and BPM using settings and fallback. |
toGraph() snapshots chain structure and literal arrays. It preserves live functions and texture references. Pass a textureReference adapter that returns stable identifiers to store graphs in JSON. A direct output operand such as blend(o0) and the texture inside blend(src(o0)) both use that adapter. Hosts that call external shaders need to supply their own bindings for live functions and easing callbacks.
Apply coordinate operations to the input coordinates of the preceding chain. Apply color operations to the preceding color. Apply combine operations to both signals at the same coordinates. For feedback, resolve a reference to the active destination against its prior frame. The host decides when other outputs have drawn their next frame.
Emit a shader function in Node
Import emitSignalFunction from triode/tsl to compile a SignalGraph to GLSL or WGSL without creating a renderer. Pass language, a layout with a function name, the vec4 return type and ordered float or vec2 inputs, and a bindings callback. Use distinct ASCII identifiers that are valid in the target language. Triode reserves names starting with nodeVar, nodeConst, nodeCode, triode or tsl_. An empty input list works when the callback supplies constant bindings.
import type { SignalGraph } from 'triode/graph';
import { emitSignalFunction } from 'triode/tsl';
export function emitOscillator(graph: SignalGraph) {
return emitSignalFunction(graph, {
language: 'wgsl',
layout: {
name: 'shade',
type: 'vec4',
inputs: [
{ name: 'uv', type: 'vec2' },
{ name: 'clock', type: 'float' },
{ name: 'size', type: 'vec2' },
],
},
bindings: ({ uv, clock, size }) => ({
coordinates: uv,
frame: { time: clock, resolution: size },
}),
});
}
The callback receives named TSL parameter nodes. Use sampleReference to bind a graph's texture references to functions that take coordinates and return RGBA TSL nodes. Set previousOutput to a non-null reference when prev() occurs in a nested graph. A top-level prev() passes undefined to the sampler if the reference is omitted. Use resolveConstant to bind a host-defined value, such as a named control, to a TSL node. Return undefined to keep ordinary literal handling. The optional registry accepts a src override such as new Map([['src', { node: (st, sample) => sample(st) }]]). This passes unwrapped coordinates to the sampler. Other registry entries must use the stock implementations and return types.
The emitted text contains the named fragment function and its helpers. Supply entry points, fragment outputs, resources, directives and module declarations in the host shader. A sampler can call one named trusted glslFn or wgslFn with a vec2 value input and a vec4 result. Match the target language, use an empty include list, and choose a name distinct from the emitted function and its parameters. Pass source for a single sampler function. Triode checks its signature and treats the rest of the source as opaque, including any prelude or trailing text the Three parser retains. Compile the composed shader to verify its syntax and semantics.
The emitter supports built-in graph operations with literal inputs and host-bound fragment values. It rejects dynamic values, sequences, TSL structs, renderer resources and inputs, raw TSL shader code, explicitly named TSL locals, custom named TSL helpers and compute or subgroup operations. Use a valid, trusted graph and trusted binding callbacks.