createVAO

View sourcecreateVAO(gl: WebGL2RenderingContext, mesh: Mesh, attributemap: Readonly<Map<string, MeshAttribute>>): WebGLVertexArrayObject

Creates a WebGL 2 vertex-array object from a Mesh.

The helper calls WebGL2RenderingContext#createVertexArray, reads the mesh’s indices and named attribute data, and binds the new object with WebGL2RenderingContext#bindVertexArray. When Mesh#getIndices returns anything other than undefined, it uploads that typed array with BufferType.ElementArray and DrawUsage.Static. For each mesh attribute, it looks up a matching MeshAttribute in attributemap, uploads data.data with BufferType.Array and DrawUsage.Static, enables the descriptor’s location with WebGLRenderingContext#enableVertexAttribArray, and records it with WebGLRenderingContext#vertexAttribPointer using (attribute.id, attribute.size, attribute.type, false, 0, 0).

attributemap is read by name; its keys do not need to be iterated in the same order as the mesh. A missing descriptor causes an error identifying the attribute name. The value passed to bindVertexArray remains bound when the function returns. When present, the index buffer remains bound to ELEMENT_ARRAY_BUFFER; the last attribute buffer remains bound to ARRAY_BUFFER. The helper does not restore bindings or delete any created buffers. It does not assert the result of createVertexArray, so WebGL’s null result is returned as-is.

Examples

import { Mesh } from '@wimaengine/render-core'
import { AttributeMap, createVAO } from '@wimaengine/render-webgl'

const vao = createVAO(gl, Mesh.quad2D(1, 1), world.getResource(AttributeMap))
// `vao` is bound to `gl` and ready for the matching vertex program.

Parameters

NameDescriptionDefault
gl

WebGL 2 context that owns the VAO and buffers.

—
mesh

Geometry whose index and attribute arrays are uploaded.

—
attributemap

Descriptors, normally the AttributeMap resource.

—

Returns

The created VAO, or null when WebGL cannot allocate one.