ngraph.leiden
Version:
Leiden/Louvain community detection for ngraph.graph (JS)
50 lines (48 loc) • 1.92 kB
JavaScript
import { GLCollection, defineProgram, ColorAttribute, InstancedAttribute } from 'w-gl';
export default class SimpleLineCollection extends GLCollection {
constructor(gl, options = {}) {
const program = defineProgram({
gl,
vertex: `
uniform mat4 modelViewProjection;
uniform float width;
uniform vec2 resolution;
attribute vec4 color;
attribute vec3 from, to;
attribute vec2 point;
varying vec4 vColor;
void main() {
vec4 clip0 = modelViewProjection * vec4(from, 1.0);
vec4 clip1 = modelViewProjection * vec4(to, 1.0);
vec2 screen0 = resolution * (0.5 * clip0.xy/clip0.w + 0.5);
vec2 screen1 = resolution * (0.5 * clip1.xy/clip1.w + 0.5);
vec2 xBasis = normalize(screen1 - screen0);
vec2 yBasis = vec2(-xBasis.y, xBasis.x);
vec2 pt0 = screen0 + width * point.x * yBasis;
vec2 pt1 = screen1 + width * point.x * yBasis;
vec2 pt = mix(pt0, pt1, point.y);
vec4 clip = mix(clip0, clip1, point.y);
gl_Position = vec4(clip.w * (2.0 * pt/resolution - 1.0), clip.z, clip.w);
vColor = color.abgr;
}
`,
fragment: `
precision highp float;
varying vec4 vColor;
void main() { gl_FragColor = vColor; }
`,
attributes: { color: new ColorAttribute() },
instanced: { point: new InstancedAttribute([ -0.5, 0, -0.5, 1, 0.5, 1, -0.5, 0, 0.5, 1, 0.5, 0 ]) }
});
super(program);
this.width = options.width || 2;
}
draw(_, drawContext) {
if (!this.uniforms) {
this.uniforms = { modelViewProjection: this.modelViewProjection, width: this.width, resolution: [drawContext.width, drawContext.height] };
}
this.uniforms.resolution[0] = drawContext.width;
this.uniforms.resolution[1] = drawContext.height;
this.program.draw(this.uniforms);
}
}