Three.js 3D Sankey Diagram (Energy Flow)
An extruded 3D Sankey diagram in Three.js tracing an illustrative energy system from primary sources through electricity and fuels to end-use sectors and useful energy versus losses, with flowing gradient ribbons and node tracing.
Key topics: sankey diagram, flow visualization, energy flow, three.js sankey, alluvial diagram, ribbon chart
Game Over
What this code does
- Layout: nodes stand in columns by stage, with height ∝ throughput (max of in and out); links stack within each node's in and out slots.
- Ribbons: top and bottom edges are cubic Béziers between source and target slots, extruded into a closed band.
- Shading: a ShaderMaterial blends the source colour into the target colour along the ribbon, with travelling pulses for direction.
- Tracing: choosing a node walks links upstream and downstream and dims the rest; hover a ribbon for its share.
- Efficiency: the HUD compares primary supply with useful energy, using illustrative numbers rather than statistics.
- Controls: trace node, animated flow, ribbon depth, curvature and node gap.
Related Demos
Continue Learning
JavaScript Implementation
// Sankey ribbon: two cubic Bézier edges (top & bottom) from source slot to target slot
const bez = (t, a, b, c, d) => (1 - t) ** 3 * a + 3 * (1 - t) ** 2 * t * b + 3 * (1 - t) * t * t * c + t ** 3 * d
for (let i = 0; i <= 40; i++) {
const t = i / 40
const x = bez(t, x0, x0 + k, x1 - k, x1)
const yTop = bez(t, y0, y0, y1, y1), yBot = yTop - linkValue * unit
positions.push(x, yTop, 0, x, yBot, 0) // triangle strip → ribbon
}
// Stack links at each node: consume the node's out/in slot by the link's thickness