Authoring custom layers
When createNodeShaderLayer and the
built-in layers don't cover what you need, drop down to the layer standard: a
LayerRenderer you write, mounted on a live <GraphCanvas> through the
api.layers registry — with no JSX or engine-core changes.
This is the same contract the built-in coverage and marker layers implement, so anything they can do, a custom layer can too.
Reach for a full LayerRenderer only when you need custom geometry (lines,
ribbons, meshes), multiple draw passes, or non-node-anchored visuals. For
a custom node look, createNodeShaderLayer
is far less code.
The LayerRenderer contract
import type { LayerRenderer, LayerContext } from '@kineviz/gl';
interface LayerRenderer<Config = unknown> {
mount(ctx: LayerContext): void; // create objects, add to ctx.cloudScene
update?(config: Config): void; // apply new config (MUST be idempotent)
frame?(dt: number, ctx: LayerContext): boolean | void; // per-frame; return true to keep ticking
onMapMode?(enabled: boolean, sphereRadius: number): void; // flat ↔ globe toggled
dispose(): void; // free geometry / materials / textures
}
A layer only ever touches the narrow LayerContext — never engine internals:
| Field | Use |
|---|---|
cloudScene |
The group all your objects must be added to (never scene). |
camera |
The engine camera (read-only). |
store |
Graph data + selection / visibility / position state. |
invalidate() |
Request a render after any visual change. |
isMapMode / sphereRadius |
Whether the map is active, and the globe radius (0 = flat). |
renderOrder(slot, offset?) |
A non-colliding renderOrder for the underlay / overlay / top band. |
getDrawingBufferSize() |
Device-pixel buffer size — for sizing screen-space quads. Read it each frame. |
getNodeOpacity(id) / getEdgeOpacity(id) |
Live dim state — read each frame so your visuals fade with focus/context, groups, filters. |
iconAtlasManager |
Icon atlas (may be null early — always guard). |
getHitTest() |
Optional. The picking hook that makes your own geometry clickable — see below. |
Two contract rules worth repeating:
updatemust be idempotent. It may be called more than once with an equivalent config (the imperative mount path applies the initial config right aftermount). Don't accumulate state in it.- Read opacity each frame. Pull
getNodeOpacity/getEdgeOpacityinsideframeso the layer follows the same dim state as the core clouds.
Making your geometry clickable
A layer that draws its own geometry is not clickable by default: the engine's picking pass only rasterises the built-in node quad. Register a hit mesh and the hitbox becomes exactly the geometry you drew.
interface LayerHitTest {
encodeNodeId(nodeIndex: number): [number, number, number];
addNodeHitMesh(mesh: THREE.Object3D): void;
removeNodeHitMesh(mesh: THREE.Object3D): void;
}
Read ctx.getHitTest?.() lazily — inside mount or frame — and never cache
the result across a renderer re-init. It returns null when the engine has no
picker yet (very early mount) or when the active render path owns picking
itself.
Three rules for the mesh you register:
- its material writes
gl_FragColor = vec4(idColour, 1.0)anddiscards wherever the layer draws nothing — otherwise the quad corners steal clicks; frustumCulledmust befalse(the picking scenes have no frustum to cull against);- it must not be parented to
cloudScene— the engine appliescloudScene's transform to it before every pick.
A hit resolves to the same payload a built-in node click produces, so selection, hover, context menu and drag all follow for free.
If your layer is one quad per node, you almost certainly want
createNodeShaderLayer's
hitArea / hitMask instead — it does all of the above for you.
Render bands
renderOrder(slot, offset) keeps your objects from colliding with the core
clouds (which own the slots between the bands):
| Band | Range | For |
|---|---|---|
underlay |
-50 … -10 |
map tiles, ground glow, axis walls |
overlay |
2 … 98 |
halos, badges, markers |
top |
100 … |
always-on-top HUD / gizmos |
mesh.renderOrder = ctx.renderOrder('overlay', 5);
The layer registry
api.layers mounts, updates, and unmounts any
LayerRenderer at runtime:
api.layers.mount(name, factory, config, opts?); // factory: () => LayerRenderer
api.layers.update(name, config); // re-config a mounted (or pending) layer
api.layers.unmount(name); // dispose + remove
api.layers.has(name); // mounted or pending?
api.layers.list(); // all layer names
opts: { frame: true }registers the per-frameframe()tick. Omit it for a static layer that only redraws onupdate.- Mounting is idempotent by name — mounting an existing name replaces it.
- Mounts issued before the canvas is wired are queued and hydrate
automatically once the engine is up, so you can safely call
api.layers.mount(orapi.coverage.set, etc.) right after creating the api ref.
React: useEngineLayer
Inside a component, the useEngineLayer hook ties a layer's
lifetime to the component and re-creates it when deps change:
import { useEngineLayer } from '@kineviz/gl';
useEngineLayer(
'my-layer',
() => new MyLayerRenderer(), // factory — called once per construct
config, // re-applied via update() when its identity changes
[/* deps — re-create the renderer when these change */],
{ frame: true },
);
A minimal layer
A skeleton that draws something, follows dim state, and cleans up:
import * as THREE from 'three';
import type { LayerRenderer, LayerContext } from '@kineviz/gl';
class MyLayerRenderer implements LayerRenderer<{ color: number }> {
private mesh!: THREE.Mesh;
private mat!: THREE.MeshBasicMaterial;
mount(ctx: LayerContext) {
this.mat = new THREE.MeshBasicMaterial({ transparent: true });
this.mesh = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), this.mat);
this.mesh.renderOrder = ctx.renderOrder('overlay');
ctx.cloudScene.add(this.mesh);
}
update(config: { color: number }) {
this.mat.color.setHex(config.color); // idempotent: only assigns
}
frame(_dt: number, ctx: LayerContext): boolean {
// follow a node's live opacity, then ask for another frame if animating
this.mat.opacity = ctx.getNodeOpacity('n1');
return false;
}
dispose() {
this.mesh.geometry.dispose();
this.mat.dispose();
this.mesh.removeFromParent();
}
}
api.layers.mount('my-layer', () => new MyLayerRenderer(), { color: 0x33ccff }, { frame: true });
See also
- Node shader layers — the shortcut for custom node visuals.
- API:
api.layers. - The built-in layers are reference implementations — coverage and markers are
LayerRenderers mounted through this exact registry.