Three.js Vertex Array Objects (VAO vs Re-Binding, Raw WebGL2)
Measure what vertex array objects save in raw WebGL2 alongside Three.js, drawing up to 20,000 meshes either with one bindVertexArray per draw or by re-binding buffers and attribute pointers every time.
Key topics: webgl2 vao, vertex array object, draw call overhead, vertexattribpointer, raw webgl, three.js internals
Game Over
What this code does
- VAO: records every attribute pointer, enable flag and index-buffer binding once, at setup.
- Per-Draw Cost: without VAOs each draw rebinds buffers and calls vertexAttribPointer and enableVertexAttribArray per attribute, about 8 calls.
- With VAOs: a draw is bindVertexArray, a uniform and drawElements, about 3 calls.
- Measurement: CPU submit time per frame shows the driver overhead difference at thousands of draws.
- three.js: the renderer creates and caches VAOs automatically (bindingStates), which is one reason it's efficient.
- Controls: binding mode, draw calls and distinct shapes.
Related Demos
Continue Learning
JavaScript Implementation
// A vertex array object records buffer bindings + attribute layout once
const vao = gl.createVertexArray()
gl.bindVertexArray(vao)
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer)
gl.enableVertexAttribArray(0)
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0)
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer) // the index binding is stored too
gl.bindVertexArray(null)
// per draw: one bind instead of bindBuffer + enableVertexAttribArray + vertexAttribPointer per attribute
gl.bindVertexArray(vao)
gl.drawElements(gl.TRIANGLES, count, gl.UNSIGNED_SHORT, 0)
renderer.resetState() // after raw GL, let three.js resync its cached state