graph-explorer-render 0.1.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. `fract(d*RINGS - phase*RINGS)` gives each fragment
  // its position within the nearest crest; 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.
  let RINGS = 2.4;
  let front = fract(d * RINGS - in.phase * RINGS);
  let crest = pow(1.0 - front, 5.0 - in.softness * 3.0);

  // 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);
}