effects

Node shader layers

createNodeShaderLayer turns a fragment shader into a LayerRenderer that draws one billboard quad per node — in a single draw call — without you writing any geometry, buffer, or disposal code. It is the fastest way to give nodes a custom look: a pulsing halo, a glow, a ring, or a completely custom shape that replaces the built-in dot.

import { createNodeShaderLayer } from '@kineviz/gl';

The factory owns the merged quad geometry, the per-frame position/size/dim sync, and cleanup. You supply only a shader (and optionally per-node attributes and uniforms). Mount it through the layer registry:

api.layers.mount('node-halo', () => createNodeShaderLayer({
  sizing: 'screen',
  sizeFactor: 3.5,
  animate: true,
  attributes: [{ name: 'phase', size: 1, value: (_n, i) => i * 2.39996 }],
  uniforms: { uColor: new THREE.Color(0.3, 0.85, 1), uSpeed: 0.7 },
  fragmentShader: `
    void main() {
      vec2 p = vUv * 2.0 - 1.0; float r = length(p);
      float pulse = 0.5 + 0.5 * sin(uTime * uSpeed * 6.2831853 + vPhase);
      float mid = mix(0.60, 0.80, pulse), hw = 0.16;
      float ring = smoothstep(mid - hw, mid, r) * (1.0 - smoothstep(mid, mid + hw, r));
      float a = ring * (0.5 + 0.5 * pulse) * vDim;
      if (a <= 0.002) discard;
      gl_FragColor = vec4(uColor, a);
    }`,
}), {}, { frame: true });

// later
api.layers.unmount('node-halo');

mount(name, factory, config, opts): the factory (arg 2) returns the renderer; config (arg 3, here {}) is passed to renderer.update(); opts (arg 4) — { frame: true } registers the per-frame tick that an animated shader needs. See it live in the custom node halo scenario.

The shader contract

The engine writes the vertex shader and a fragment-shader preamble for you. Your fragmentShader is just void main(){…} (plus any helper functions). Do not redeclare the symbols the engine already provides — it will inject the declarations:

Symbol Kind Available Meaning
vUv varying vec2 always Quad coordinate, 0..1 (center 0.5, 0.5).
vDim varying float when followDim (default) Live opacity of this node, 0..1 — multiply your alpha by it so the effect dims with the node.
uTime uniform float when animate: true Seconds since mount.
uResolution uniform vec2 always Drawing-buffer size in px.
v<Name> varying per declared attribute Your per-node attribute (see below).
your uniforms uniform per declared uniform Declared automatically from uniforms.

So in the example above, vUv, vDim, uTime are engine-provided; vPhase comes from the phase attribute; uColor and uSpeed are your uniforms.

Spec options

interface NodeShaderLayerSpec {
  fragmentShader: string;               // your void main(){…}
  sizing?: 'screen' | 'world';          // default 'screen'
  sizeFactor?: number;                  // default 1 — scales the quad
  size?: (node, index) => number;       // per-node size override
  filter?: (node, index) => boolean;    // target a subset of nodes
  attributes?: NodeShaderAttribute[];   // per-node varyings
  uniforms?: Record<string, UniformValue>;
  animate?: boolean;                    // default false — enables uTime + frame ticks
  band?: 'underlay' | 'overlay' | 'top';// default 'overlay'
  bandOffset?: number;                  // fine render-order nudge within the band
  followDim?: boolean;                  // default true — expose vDim
  hideBuiltinNode?: boolean;            // default false — suppress the built-in dot
  blending?: 'additive' | 'normal';    // default 'additive'
  maxNodes?: number;                    // capacity cap
  hitArea?: 'none' | 'quad' | 'circle' | 'shape';  // default 'none'
  hitMask?: { prelude?: string; sdf: string };     // required by hitArea: 'shape'
}
  • sizing — 'screen' keeps the quad a constant pixel size regardless of zoom (good for halos/HUD); 'world' scales it with the scene (good for shapes that should grow/shrink with the graph).
  • blending — 'additive' (default) makes overlapping glows brighten, ideal for halos; switch to 'normal' for opaque shapes.
  • band / bandOffset — choose the render band (see the layer model).

Per-node attributes

Pass per-node data into the shader as a NodeShaderAttribute. Each becomes a varying named v<Name> (capitalized):

attributes: [
  { name: 'phase',  size: 1, value: (_n, i) => i * 2.39996 },           // → vPhase  (float)
  { name: 'tint',   size: 3, value: (n) => categoryRgb(n.category) },   // → vTint   (vec3)
]
interface NodeShaderAttribute {
  name: string;              // GLSL name; exposed as varying v<Name>
  size: 1 | 2 | 3 | 4;       // float | vec2 | vec3 | vec4
  value: (node, index) => number | number[];
  dynamic?: boolean;         // default false — re-evaluate every frame
}

By default attributes refill only when membership changes. Set dynamic: true to re-read the value every frame (e.g. a value derived from a changing property).

Uniforms

uniforms accepts numbers, number arrays, and Three.js types — the GLSL declaration type is inferred:

JS value GLSL type
number float
[a, b] / [a, b, c] / [a, b, c, d] vec2 / vec3 / vec4
THREE.Color vec3
THREE.Vector2/3/4 vec2/3/4
THREE.Texture sampler2D

Making a layer clickable

The engine's picking pass rasterises only the built-in node quad, sized from the node's own size — it knows nothing about your sizeFactor. So a layer drawing at sizeFactor: 10 paints a panel ten times larger than its hitbox, and clicks land on the background everywhere except a small patch at the node centre. This is the single most reported surprise about custom layers.

hitArea fixes it: the layer registers a second mesh in the engine's node picking scene, sharing this layer's geometry, so the hitbox is exactly the quad it draws.

hitArea Clickable region
'none' (default) built-in hitbox only — the legacy behaviour
'quad' the whole billboard, corners included
'circle' the inscribed circle — right for a round panel
'shape' exactly where hitMask says you drew something

Clicks resolve to the same payload a built-in node click produces, so selection, hover, context menu and drag all follow with no extra wiring.

createNodeShaderLayer({
  sizeFactor: 10,
  hitArea: 'circle',
  fragmentShader: /* … */,
});

For any other outline, hitMask.sdf is a GLSL expression in vec2 q (= vUv - 0.5, y up) that is negative where you drew something; hitMask.prelude is emitted above main for any helper it calls.

createNodeShaderLayer({
  hitArea: 'shape',
  hitMask: { sdf: 'max(length(q) - 0.5, 0.18 - length(q))' },   // a thick ring
  fragmentShader: /* draws the same ring */,
});

Two outlines written twice will drift. Generate the mask from the same code that drew the pixels — that is exactly what the media and card layers do, and why a click in a hexagon panel's corner falls through to whatever is behind.

Any varying the expression names (vFoo for attribute foo) is forwarded into the pick program automatically; nothing else is, so the pick pass stays as small as the mask needs.

Replacing the node shape

Set hideBuiltinNode: true to suppress the engine's built-in node fill so your shader is the node. The node stays pickable and edge-connected — only its fill is replaced. This is how the custom node shape scenario draws each node as an SDF diamond:

api.layers.mount('node-shape', () => createNodeShaderLayer({
  hideBuiltinNode: true,
  blending: 'normal',
  uniforms: { uColor: new THREE.Color(0.45, 0.8, 1) },
  fragmentShader: `
    void main() {
      vec2 p = vUv * 2.0 - 1.0;
      float d = abs(p.x) + abs(p.y);          // diamond SDF: |x| + |y|
      float edge = fwidth(d);
      float fill = 1.0 - smoothstep(0.85 - edge, 0.85, d);
      if (fill <= 0.002) discard;
      gl_FragColor = vec4(uColor, fill * vDim);
    }`,
}), {}, {});

For a shape that is not animated you can omit { frame: true } — the layer only needs a per-frame tick when animate: true (or when an attribute is dynamic).

See also