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) opacity: f32,
  @location(3) @interpolate(flat) shape: u32,
};

// Unit quad corners (two triangles) provided as vertex_index 0..6
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) opacity: f32,
  @location(3) shape: u32,
  @location(4) color: vec4<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.opacity = opacity;
  out.shape = shape;
  return out;
}

@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
  let p = in.local;
  var inside = 0.0;
  if (in.shape == 1u) {            // square
    inside = select(0.0, 1.0, max(abs(p.x), abs(p.y)) <= 1.0);
  } else if (in.shape == 2u) {     // diamond
    inside = select(0.0, 1.0, abs(p.x) + abs(p.y) <= 1.0);
  } else {                         // circle (soft edge)
    inside = 1.0 - smoothstep(0.9, 1.0, length(p));
  }
  if (inside <= 0.0) { discard; }
  return vec4<f32>(in.color.rgb, in.color.a * in.opacity * inside * cam.fade);
}