Three.js Vertex Animation Textures (Buffer Textures Emulated)

Large per-vertex datasets in textures in Three.js, where a verlet cloth simulation is baked into a float data texture and replayed on 400+ instanced flags with texelFetch by vertex index and frame, with no CPU work per frame.

Key topics: three.js vertex animation texture, vat, data texture, texelfetch, gl_vertexid, baked animation

What this code does

- Buffer Textures: WebGL2 lacks texture buffers, so a float `DataTexture` with nearest filtering plays the same role.
- Bake: a pinned verlet cloth with periodic wind is simulated on the CPU once, then 90 frames of positions and normals are written to texture rows.
- Playback: the vertex shader reads `texelFetch(uVat, ivec2(gl_VertexID, frame), 0)` for its own data each frame.
- Interpolation: blending adjacent frames keeps motion smooth at any playback speed.
- Instancing: hundreds of flags share the data with per-instance phase and colour, in one draw call.
- Controls: flag count, playback speed and frame interpolation.

Continue Learning

JavaScript Implementation

ES6+
// "Buffer texture" in WebGL2: put large per-vertex data in a float texture and texelFetch it
const vat = new THREE.DataTexture(bakedFloats, vertexCount, frames, THREE.RGBAFormat, THREE.FloatType)
vat.minFilter = vat.magFilter = THREE.NearestFilter
vat.needsUpdate = true
const material = new THREE.ShaderMaterial({
  glslVersion: THREE.GLSL3,
  uniforms: { uVat: { value: vat }, uFrame: { value: 0 } },
  vertexShader: `
    uniform sampler2D uVat; uniform int uFrame;
    void main () {
      vec3 p = texelFetch(uVat, ivec2(gl_VertexID, uFrame), 0).xyz; // this vertex, this frame
      gl_Position = projectionMatrix * modelViewMatrix * vec4(p, 1.0);
    }`,
  fragmentShader: 'out vec4 c; void main () { c = vec4(1.0); }'
})