UNAVI Docs unavi.xyz
Build · Scripting API

wired:shading

Node-based shader graphs attached to prims in place of a standard material.

Graphs are directed acyclic lists of fixed-arity nodes with no loops, branches, or raw shader source, keeping GPU cost bounded.

Limits: 128 nodes per network, 16 public inputs, 4 texture samples.

Interface graph

Uses vec2, vec3, color, prim-id, error, document, layer.

graph-value

variant

Value type for public shader inputs and per-prim overrides.

float(f32)
vec2(vec2)
vec3(vec3)
color(color)

value-kind

enum
float
vec2
vec3
color

port

variant

Input operand to a shader node.

const(graph-value)
input(u16)Index into public-inputs.
node(u16)Index of an earlier node in the same network (strictly less than the referencing node's index to prevent cycles; cannot cross between surface and displacement networks).

binary-op

record
a: port
b: port

lerp-op

record
a: port
b: port
t: port

pow-op

record
x: port
y: port

atan2-op

record
y: port
x: port

select-op

record
cond: port
a: port
b: port

clamp-op

record
x: port
low: port
high: port

step-op

record
edge: port
x: port

smoothstep-op

record
low: port
high: port
x: port

texture-sample-op

record
uv: port
slot: u8Texture slot index (0..=3).

extract-op

record
v: port
channel: u8

combine2-op

record
x: port
y: port

combine3-op

record
x: port
y: port
z: port

combine4-op

record
x: port
y: port
z: port
w: port

convert-op

record
v: port
to: value-kind

remap-op

record
x: port
from-low: port
from-high: port
to-low: port
to-high: port

polar-coords-op

record
uv: port
center: port

rotate-uv-op

record
uv: port
center: port
radians: port

dither-op

record
coord: port
level: port

node

variant

Single operation in a shading network.

Stage availability for zero-arity inputs:

uv
world-normal
world-position
vertex-color
local-position
local-normal
time
instance-randomPer-instance pseudo-random scalar.
object-position
object-scale
view-direction
screen-uvFragment screen coordinates (0..1 from top-left).
add(binary-op)
sub(binary-op)
mul(binary-op)
div(binary-op)
modulo(binary-op)
min(binary-op)
max(binary-op)
dot(binary-op)
cross(binary-op)
distance(binary-op)
pow(pow-op)
atan2(atan2-op)
lerp(lerp-op)
clamp(clamp-op)
step(step-op)
smoothstep(smoothstep-op)
remap(remap-op)
select(select-op)
sin(port)
cos(port)
one-minus(port)
abs(port)
floor(port)
fract(port)
saturate(port)
sqrt(port)
length(port)
normalize(port)
triangle-wave(port)
luminance(port)
fresnel(port)Fresnel rim term raised to the given power exponent.
noise(port)
texture-sample(texture-sample-op)
scene-color(port)Samples the opaque scene color already rendered behind this surface at the given screen UV (used for refraction). Runs in a later pass and does not see other scene-color or later-drawn surfaces.
extract(extract-op)
combine2(combine2-op)
combine3(combine3-op)
combine4(combine4-op)
convert(convert-op)
polar-coords(polar-coords-op)
rotate-uv(rotate-uv-op)
screen-dotsFragment position in viewport pixels from top-left (surface only).
dither(dither-op)4x4 Bayer ordered dither: returns 1.0 where level exceeds the threshold at coord (vec2), otherwise 0.0.

lit-output

record

Physically based lit output; unset ports fall back to renderer defaults.

base-color: option<port>
emissive: option<port>
metallic: option<port>
roughness: option<port>
normal: option<port>
alpha: option<port>
alpha-clip-threshold: option<port>Fragments with alpha below this threshold are discarded.
specular-transmission: option<port>Specular transmission (0..1), tinted by base-color. Values above 0 render in the transmissive pass using thickness and ior.
diffuse-transmission: option<port>Diffuse (Lambertian) transmission from rear lighting.
thickness: option<port>Refracted ray travel thickness in meters.
ior: option<port>Index of refraction (1.0 air, 1.33 water, 1.5 glass).

unlit-output

record

Direct unlit color output bypassing lighting.

color: port
alpha-clip-threshold: option<port>

surface-output

variant
lit(lit-output)
unlit(unlit-output)

blend-mode

enum

Alpha/color compositing mode.

opaque
blend
add
multiply

cull-mode

enum
back
front
none

surface-graph

record

Fragment-stage network.

nodes: list<node>
output: surface-output
blend: blend-mode
cull: cull-mode
cast-shadows: boolWhether this surface casts shadows.

displacement-graph

record

Vertex-stage network. position-offset applies in prim-local space; world-position-offset applies after the prim transform.

nodes: list<node>
position-offset: option<port>
normal-override: option<port>
world-position-offset: option<port>

shader-graph

record

Complete shader graph with default public-inputs, a fragment network, and an optional vertex displacement network.

public-inputs: list<graph-value>
surface: surface-graph
displacement: option<displacement-graph>

set-graph

funcset-graph: func(document: borrow<document>, prim: prim-id, layer: layer, graph: option<shader-graph>) -> result<_, error>

Attaches graph to prim in place of its material, or clears the graph (and its overrides) when none. Identical graphs share one compiled pipeline. Prims can also reference a graph on another prim via the shader-binding relation.

overrides

funcoverrides: func(document: borrow<document>, prim: prim-id) -> list<tuple<u16, graph-value>>

Returns active public-input overrides on prim by index.

set-overrides

funcset-overrides: func(document: borrow<document>, prim: prim-id, layer: layer, values: list<tuple<u16, graph-value>>) -> result<_, error>

Replaces all public-input overrides on prim in layer without recompiling the shared shader pipeline.

Source

/// Node-based shader graphs attached to prims in place of a standard `material`.
///
/// Graphs are directed acyclic lists of fixed-arity nodes with no loops,
/// branches, or raw shader source, keeping GPU cost bounded.
///
/// Limits: 128 nodes per network, 16 public inputs, 4 texture samples.
package wired:shading;

interface graph {
  use wired:core/math.{vec2, vec3, color};
  use wired:core/ids.{prim-id};
  use wired:core/error.{error};
  use wired:scene/document.{document, layer};

  /// Value type for public shader inputs and per-prim overrides.
  variant graph-value {
    float(f32),
    vec2(vec2),
    vec3(vec3),
    color(color),
  }

  enum value-kind {
    float,
    vec2,
    vec3,
    color,
  }

  /// Input operand to a shader node.
  variant port {
    const(graph-value),
    /// Index into `public-inputs`.
    input(u16),
    /// Index of an earlier node in the same network (strictly less than the
    /// referencing node's index to prevent cycles; cannot cross between surface
    /// and displacement networks).
    node(u16),
  }

  record binary-op { a: port, b: port }
  record lerp-op { a: port, b: port, t: port }
  record pow-op { x: port, y: port }
  record atan2-op { y: port, x: port }
  record select-op { cond: port, a: port, b: port }
  record clamp-op { x: port, low: port, high: port }
  record step-op { edge: port, x: port }
  record smoothstep-op { low: port, high: port, x: port }
  record texture-sample-op {
    uv: port,
    /// Texture slot index (`0..=3`).
    slot: u8,
  }
  record extract-op { v: port, channel: u8 }
  record combine2-op { x: port, y: port }
  record combine3-op { x: port, y: port, z: port }
  record combine4-op { x: port, y: port, z: port, w: port }
  record convert-op { v: port, to: value-kind }
  record remap-op {
    x: port,
    from-low: port,
    from-high: port,
    to-low: port,
    to-high: port,
  }
  record polar-coords-op { uv: port, center: port }
  record rotate-uv-op { uv: port, center: port, radians: port }
  record dither-op { coord: port, level: port }

  /// Single operation in a shading network.
  ///
  /// Stage availability for zero-arity inputs:
  /// - Surface only: `uv`, `world-normal`, `world-position`, `vertex-color`,
  ///   `view-direction`, `screen-uv`, `screen-dots`.
  /// - Displacement only: `local-position`, `local-normal`.
  /// - Both stages: `time`, `instance-random`, `object-position`,
  ///   `object-scale`.
  variant node {
    uv,
    world-normal,
    world-position,
    vertex-color,
    local-position,
    local-normal,
    time,
    /// Per-instance pseudo-random scalar.
    instance-random,
    object-position,
    object-scale,
    view-direction,
    /// Fragment screen coordinates (`0..1` from top-left).
    screen-uv,

    add(binary-op),
    sub(binary-op),
    mul(binary-op),
    div(binary-op),
    modulo(binary-op),
    min(binary-op),
    max(binary-op),
    dot(binary-op),
    cross(binary-op),
    distance(binary-op),
    pow(pow-op),
    atan2(atan2-op),
    lerp(lerp-op),
    clamp(clamp-op),
    step(step-op),
    smoothstep(smoothstep-op),
    remap(remap-op),
    select(select-op),

    sin(port),
    cos(port),
    one-minus(port),
    abs(port),
    floor(port),
    fract(port),
    saturate(port),
    sqrt(port),
    length(port),
    normalize(port),
    triangle-wave(port),
    luminance(port),

    /// Fresnel rim term raised to the given power exponent.
    fresnel(port),
    noise(port),
    texture-sample(texture-sample-op),
    /// Samples the opaque scene color already rendered behind this surface at
    /// the given screen UV (used for refraction). Runs in a later pass and does
    /// not see other `scene-color` or later-drawn surfaces.
    scene-color(port),

    extract(extract-op),
    combine2(combine2-op),
    combine3(combine3-op),
    combine4(combine4-op),
    convert(convert-op),

    polar-coords(polar-coords-op),
    rotate-uv(rotate-uv-op),

    /// Fragment position in viewport pixels from top-left (surface only).
    screen-dots,
    /// 4x4 Bayer ordered dither: returns `1.0` where `level` exceeds the
    /// threshold at `coord` (`vec2`), otherwise `0.0`.
    dither(dither-op),
  }

  /// Physically based lit output; unset ports fall back to renderer defaults.
  record lit-output {
    base-color:           option<port>,
    emissive:             option<port>,
    metallic:             option<port>,
    roughness:            option<port>,
    normal:               option<port>,
    alpha:                option<port>,
    /// Fragments with alpha below this threshold are discarded.
    alpha-clip-threshold: option<port>,
    /// Specular transmission (`0..1`), tinted by `base-color`. Values above `0`
    /// render in the transmissive pass using `thickness` and `ior`.
    specular-transmission: option<port>,
    /// Diffuse (Lambertian) transmission from rear lighting.
    diffuse-transmission:  option<port>,
    /// Refracted ray travel thickness in meters.
    thickness:             option<port>,
    /// Index of refraction (`1.0` air, `1.33` water, `1.5` glass).
    ior:                   option<port>,
  }

  /// Direct unlit color output bypassing lighting.
  record unlit-output {
    color:                port,
    alpha-clip-threshold: option<port>,
  }

  variant surface-output {
    lit(lit-output),
    unlit(unlit-output),
  }

  /// Alpha/color compositing mode.
  enum blend-mode {
    opaque,
    blend,
    add,
    multiply,
  }

  enum cull-mode {
    back,
    front,
    none,
  }

  /// Fragment-stage network.
  record surface-graph {
    nodes:        list<node>,
    output:       surface-output,
    blend:        blend-mode,
    cull:         cull-mode,
    /// Whether this surface casts shadows.
    cast-shadows: bool,
  }

  /// Vertex-stage network. `position-offset` applies in prim-local space;
  /// `world-position-offset` applies after the prim transform.
  record displacement-graph {
    nodes:                 list<node>,
    position-offset:       option<port>,
    normal-override:       option<port>,
    world-position-offset: option<port>,
  }

  /// Complete shader graph with default `public-inputs`, a fragment network,
  /// and an optional vertex displacement network.
  record shader-graph {
    public-inputs: list<graph-value>,
    surface:       surface-graph,
    displacement:  option<displacement-graph>,
  }

  /// Attaches `graph` to `prim` in place of its material, or clears the graph
  /// (and its overrides) when `none`. Identical graphs share one compiled
  /// pipeline. Prims can also reference a graph on another prim via the
  /// `shader-binding` relation.
  set-graph: func(
    document: borrow<document>,
    prim:     prim-id,
    layer:    layer,
    graph:    option<shader-graph>,
  ) -> result<_, error>;

  /// Returns active public-input overrides on `prim` by index.
  overrides: func(document: borrow<document>, prim: prim-id) -> list<tuple<u16, graph-value>>;

  /// Replaces all public-input overrides on `prim` in `layer` without
  /// recompiling the shared shader pipeline.
  set-overrides: func(
    document: borrow<document>,
    prim:     prim-id,
    layer:    layer,
    values:   list<tuple<u16, graph-value>>,
  ) -> result<_, error>;
}