UNPKG

@jonobr1/force-directed-graph

Version:

GPU supercharged attraction-graph visualizations for the web built on top of Three.js

171 lines (134 loc) 3.63 kB
import { getPosition, getVelocity, getIndex, random, jiggle, link, charge, center } from "./partials.js"; export const types = ["simplex", "nested"]; /** * Calculate the next frame's velocity for all nodes. */ export const simplex = ` uniform float alpha; uniform float is2D; uniform float size; uniform float time; uniform float nodeRadius; uniform float nodeAmount; uniform float edgeAmount; uniform float maxSpeed; uniform float timeStep; uniform float damping; uniform float repulsion; uniform float springLength; uniform float stiffness; uniform float gravity; uniform sampler2D textureLinks; ${getPosition} ${getVelocity} ${getIndex} ${random} ${jiggle} ${link} ${charge} ${center} void main() { vec2 uv = gl_FragCoord.xy / resolution.xy; int id1 = getIndex( uv ); vec3 p1 = getPosition( uv ); vec3 v1 = getVelocity( uv ); vec3 a = vec3( 0.0 ), b = vec3( 0.0 ), c = vec3( 0.0 ); for ( float i = 0.0; i < max( nodeAmount, edgeAmount ); i += 1.0 ) { float uvx = mod( i, size ) / size; float uvy = floor( i / size ) / size; vec2 uv2 = vec2( uvx, uvy ); int id2 = getIndex( uv2 ); vec3 v2 = getVelocity( uv2 ); vec3 p2 = getPosition( uv2 ); if ( i < edgeAmount ) { b += link( i, id1, p1, v1, uv2 ); } if ( i < nodeAmount) { c += charge( i, id1, p1, v1, id2, p2, v2 ); } } b *= 1.0 - step( edgeAmount, float( id1 ) ); c *= 1.0 - step( nodeAmount, float( id1 ) ); // 4. vec3 d = center( p1 ); vec3 acceleration = a + b + c + d; // Calculate Velocity vec3 velocity = ( v1 + ( acceleration * timeStep ) ) * damping * alpha; velocity = clamp( velocity, - maxSpeed, maxSpeed ); velocity.z *= 1.0 - is2D; gl_FragColor = vec4( velocity, 0.0 ); } `; export const nested = ` uniform float alpha; uniform float is2D; uniform float size; uniform float time; uniform float nodeRadius; uniform float nodeAmount; uniform float edgeAmount; uniform float maxSpeed; uniform float timeStep; uniform float damping; uniform float repulsion; uniform float springLength; uniform float stiffness; uniform float gravity; uniform sampler2D textureLinks; uniform sampler2D textureLinksLookUp; ${getPosition} ${getVelocity} ${getIndex} ${random} ${jiggle} ${link} ${charge} ${center} void main() { vec2 uv = gl_FragCoord.xy / resolution.xy; int id1 = getIndex( uv ); vec3 p1 = getPosition( uv ); vec3 v1 = getVelocity( uv ); vec3 a = vec3( 0.0 ), b = vec3( 0.0 ), c = vec3( 0.0 ); /* for ( float i = 0.0; i < linkAmount; i += 1.0 ) { // TODO: get all edges and link them b += link( i, id1, p1, v1, uv2 ); } */ for ( float i = 0.0; i < nodeAmount; i += 1.0 ) { float uvx = mod( i, size ) / size; float uvy = floor( i / size ) / size; vec2 uv2 = vec2( uvx, uvy ); int id2 = getIndex( uv2 ); vec3 v2 = getVelocity( uv2 ); vec3 p2 = getPosition( uv2 ); if ( i < nodeAmount) { c += charge( i, id1, p1, v1, id2, p2, v2 ); } } b *= 1.0 - step( edgeAmount, float( id1 ) ); c *= 1.0 - step( nodeAmount, float( id1 ) ); // 4. vec3 d = center( p1 ); vec3 acceleration = a + b + c + d; // Calculate Velocity vec3 velocity = ( v1 + ( acceleration * timeStep ) ) * damping * alpha; velocity = clamp( velocity, - maxSpeed, maxSpeed ); velocity.z *= 1.0 - is2D; gl_FragColor = vec4( velocity, 0.0 ); } `;