graph-explorer-render 0.3.0

WebGL2/wgpu renderer for graph-explorer — nodes, edges, halos and labels.
Documentation
struct Camera {
  center: vec2<f32>,
  zoom: f32,
  fade: f32,
  viewport: vec2<f32>,
  _pad1: vec2<f32>,
};
@group(0) @binding(0) var<uniform> cam: Camera;

struct VsOut {
  @builtin(position) clip: vec4<f32>,
  @location(0) local: vec2<f32>,
  @location(1) color: vec4<f32>,
  @location(2) softness: f32,
  @location(3) phase: f32,
};

var<private> QUAD: array<vec2<f32>, 6> = array<vec2<f32>, 6>(
  vec2<f32>(-1.0, -1.0), vec2<f32>(1.0, -1.0), vec2<f32>(1.0, 1.0),
  vec2<f32>(-1.0, -1.0), vec2<f32>(1.0, 1.0), vec2<f32>(-1.0, 1.0),
);

@vertex
fn vs_main(
  @builtin(vertex_index) vi: u32,
  @location(0) pos: vec2<f32>,
  @location(1) radius: f32,
  @location(2) color: vec4<f32>,
  @location(3) softness: f32,
  @location(4) phase: f32,
) -> VsOut {
  let corner = QUAD[vi];
  let world = pos + corner * radius;
  let rel = (world - cam.center) * cam.zoom;
  let clip = vec2<f32>(rel.x / (cam.viewport.x * 0.5), rel.y / (cam.viewport.y * 0.5));
  var out: VsOut;
  out.clip = vec4<f32>(clip, 0.0, 1.0);
  out.local = corner;
  out.color = color;
  out.softness = softness;
  out.phase = phase;
  return out;
}

@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
  let d = length(in.local);        // 0 at center .. 1 at the quad edge
  if (d > 1.0) { discard; }

  // Rolling waves: concentric crests that emanate OUTWARD from the node as
  // `phase` advances 0->1. Subtracting the growing phase moves crests toward
  // larger `d` (outward) over time. A sharp power shapes each crest into a
  // distinct ripple rather than a smear, and `softness` widens it.
  //
  // The wave is a raised cosine rather than the more obvious `fract`, because
  // `fract` snaps from 1 straight back to 0 at every ring boundary. That
  // discontinuity is a hard edge, and a hard edge in a radial function draws a
  // visibly stair-stepped circle. A raised cosine peaks at the same radii and
  // is continuous everywhere, so the ring boundaries antialias themselves.
  // Raising a smooth 0..1 function to a power keeps it smooth, so the
  // `softness` knob still sharpens the crest without reintroducing a step.
  let RINGS = 2.4;
  let TAU = 6.2831855;
  // Both arguments to `pow` are guarded, because it is undefined for a
  // negative base and returns infinity for a zero base with a negative
  // exponent — and either result poisons the pixel to NaN rather than merely
  // looking wrong. `cos` is only required to be accurate to within a few ULP,
  // so `0.5 + 0.5*cos(x)` can land a hair below zero at a trough, and
  // `softness` arrives from host JSON with no upper bound.
  let wave = max(0.5 + 0.5 * cos(TAU * (d * RINGS - in.phase * RINGS)), 0.0);
  let crest = pow(wave, max(5.0 - in.softness * 3.0, 0.5));

  // Envelope: the node covers the center, so fade the wave in past the node,
  // and fade it out at the rim so the halo is a bounded ring band, not a disc.
  let inner = smoothstep(0.15, 0.45, d);
  let outer = 1.0 - smoothstep(0.65, 1.0, d);
  let env = inner * outer;

  // A faint steady base glow under the ripples keeps the selection legible even
  // at a wave trough, so the halo never fully disappears between crests.
  let base = 0.18 * outer;

  let intensity = clamp(crest * env + base, 0.0, 1.0);
  return vec4<f32>(in.color.rgb, in.color.a * intensity * cam.fade);
}