Lookups in a data texture
Send an array of numbers to a shader as a texture, update it every frame, and read exact values back with texelFetch.
Uniform arrays are limited to a few hundred values, and their size is fixed in the shader. A texture can hold millions, and the shader can loop over as many as you tell it. Here 40 points move around, and the shader colours each pixel by its nearest point and draws lines where two cells meet.
import { createContext, createRenderer, createTexture, updateTexture } from 'wtc-simplegl'
// Cells around 40 moving points. The points' positions are uploaded into a
// float texture every frame, and the shader reads them all with texelFetch to
// find the nearest two.
const COUNT = 40
const vert = /* glsl */ `#version 300 es
in vec2 a_position;
out vec2 v_uv;
void main() {
v_uv = a_position * 0.5 + 0.5;
gl_Position = vec4(a_position, 0.0, 1.0);
}`
const frag = /* glsl */ `#version 300 es
precision highp float;
uniform sampler2D u_points; // COUNT × 1, RG32F: each texel is a point, 0–1
uniform int u_count;
uniform float u_aspect;
in vec2 v_uv;
out vec4 colour;
void main() {
vec2 p = v_uv * vec2(u_aspect, 1.0);
float nearest = 1e9, second = 1e9;
int which = 0;
for (int i = 0; i < u_count; i++) {
vec2 point = texelFetch(u_points, ivec2(i, 0), 0).xy * vec2(u_aspect, 1.0);
float d = distance(p, point);
if (d < nearest) { second = nearest; nearest = d; which = i; }
else if (d < second) { second = d; }
}
// Lines where two cells meet, and a dot at each point
float edge = 1.0 - smoothstep(0.0, 0.006, second - nearest);
float centre = 1.0 - smoothstep(0.006, 0.01, nearest);
float shade = fract(float(which) * 0.618);
vec3 rgb = mix(vec3(0.29, 0.62, 1.0), vec3(1.0, 0.45, 0.7), shade);
float alpha = max(max(edge, centre), 0.12);
colour = vec4(rgb * alpha, alpha);
}`
export default function (container) {
const ac = new AbortController()
const canvas = document.createElement('canvas')
container.appendChild(canvas)
const gl = createContext({
canvas,
signal: ac.signal,
attributes: { alpha: true, premultipliedAlpha: true }
})
// Two floats per point: x and y
const points = new Float32Array(COUNT * 2)
const seeds = Array.from({ length: COUNT }, () => [Math.random(), Math.random(), 0.2 + Math.random()])
const options = { width: COUNT, height: 1, format: gl.RG, type: gl.FLOAT }
const texture = createTexture(gl, { ...options, data: points })
const cells = createRenderer(gl, {
vert,
frag,
count: 3,
attributes: {
a_position: { data: new Float32Array([-1, -1, 3, -1, -1, 3]), size: 2 }
}
})
cells.setUniforms({ u_count: COUNT })
return {
frame(time) {
// Move each point in a slow loop about where it started
seeds.forEach(([x, y, speed], i) => {
points[i * 2] = x + 0.08 * Math.sin(time * speed + i)
points[i * 2 + 1] = y + 0.08 * Math.cos(time * speed * 0.8 + i * 2)
})
updateTexture(gl, texture, points, options)
cells.bindVAOs()
cells.setUniforms({ u_points: texture, u_aspect: gl.canvas.width / gl.canvas.height })
cells.render()
},
dispose() {
ac.abort()
cells.delete()
gl.deleteTexture(texture)
gl.getExtension('WEBGL_lose_context')?.loseContext()
canvas.remove()
}
}
}Packing the data
Each point is two floats, x and y, so the texture is gl.RG with gl.FLOAT: one texel per point, in a row.
const points = new Float32Array(COUNT * 2)
const options = { width: COUNT, height: 1, format: gl.RG, type: gl.FLOAT }
const texture = createTexture(gl, { ...options, data: points })createTexture picks the internal format, gl.RG32F, and leaves out mipmaps, which float textures can't have by default. Use gl.RGBA for four values per item, or gl.RED for one.
More than a few thousand items won't fit in one row; textures are limited to gl.MAX_TEXTURE_SIZE wide, at least 2048. Wrap them onto more rows, and work out the texel from the index in the shader:
ivec2 texel = ivec2(i % 1024, i / 1024);Reading it
texelFetch takes whole texel coordinates and a mip level, and returns the stored value with no filtering or wrapping:
vec2 point = texelFetch(u_points, ivec2(i, 0), 0).xy;texture(), with coordinates from 0 to 1, would blend neighbouring texels unless the filter is gl.NEAREST. For data, texelFetch is clearer and always exact.
Updating it
updateTexture(gl, texture, points, options)Pass the same width, height, format and type as when it was made. The size can change: upload more points with a bigger width, and update the count uniform to match.
Uploading a few thousand floats a frame is cheap. For data that's calculated from the last frame's data, it's usually better to do the calculation on the GPU, with transform feedback or ping-pong framebuffers.