luma.gl

VertexArray

The VertexArray class (like its lower level counterpart, the VertexArrayObject) manages an “array” of values (“buffers”) that will be made available as input data to shaders during a draw call. For each WebGL Buffer, the VertexArray also stores some additional information about how that data in the buffer should be accessed, such as offsets, strides, etc, and whether the attribute is instanced.

The VertexArray class provides the following features on top of the lower level VertexArrayObject class:

The VertexArray is a wrapper class around the VertexArrayObject class which encapsulates the underlying WebGL object. The VertexArrayObject class has a number of complications that the VertexArray takes care of.

It is usually not necessary to create VertexArray instances in luma.gl applications. The application can just supply a map of attributes to the Model class, and rely on that class to automatically manage the vertex attributes array and supply it to any draw calls (e.g. when rendering, picking etc). Still, it can be useful to review this documentation to better understand how attributes are handled.

For more information on the WebGL VertexArrayObject, see the OpenGL Wiki.

Usage

Import the VertexArray class so that your app can use it:

import {VertexArray} from '@luma.gl/core';

Create a new VertexArray

const vao = new VertexArray(gl);
}

Deleting a VertexArray

vertexArrayObject.delete();

Adding attributes to a VertexArray: without program metadata, buffers must be specified using location indices

const vertexArray2 = new VertexArray(gl);
vertexArray2.setBuffers({
  0: new Buffer({data: new Float32Array([...]), ...})
});

Adding attributes to a VertexArray: adding a program configuration enables setting attributes by name

// Register attribute info extracted from program shaders
const program = new Program(gl, ...);
const vertexArray = new VertexArray(gl, {program});

// Now it is possible to set buffers using attribute names
vertexArray.setAttributes({
  aColor: new Buffer(gl, new Uint8Array([...]))
});

Setting a set of attributes and an elements array

```js
const vertexArray = new VertexArray(gl, {
  attributes: {
    elements: new Buffer(gl, {target: GL.ELEMENT_ARRAY_BUFFER, data: new Uint32Array([...])}),
  	positions: new Buffer(gl, {data: new Float32Array([...])})
  }
}

Setting a constant vertex attribute

```js
import {VertexArray} from '@luma.gl/core';
const vao = new VertexArray(gl);
vao.setConstant(0, [0, 0, 0]);

Constructor

VertexArray(gl : WebGLRenderingContext, props : Object)

Creates a new VertexArray

Methods

initialize(props : Object) : VertexArray

Reinitializes a VertexArray.

setAttributes(attributes : Object) : VertexArray

Sets named uniforms from a map.

program.setAttributes(attributes : Object);

Attributes is an object with key-value pairs: {nameOrLocation: value, ....}.

Each value can be an a Buffer, an Array starting with a Buffer or a typed array.

setConstant(value : Array [, accessor : Object]) : VertexArray

Sets a constant value for a vertex attribute. When this VertexArray is used in a Program.draw() call, all Vertex Shader invocations will get the same value.

VertexArray.setConstant(location, array);

WebGL APIs: vertexAttrib4[u]{f,i}v

setBuffer(nameOrLocation, buffer : Buffer [, accessor : Object]) : VertexArray

Binds the specified attribute in this vertex array to the supplied buffer

setBuffer(location, buffer); setBuffer(location, buffer, {offset = 0, stride = 0, normalized = false, integer = false});

gl.vertexAttrib{I}Pointer, gl.vertexAttribDivisor

setElementBuffer(buffer : Buffer [, accessor : Object]) : VertexArray

Binds the supplied buffer as index buffer (GL.ELEMENT_ARRAY_BUFFER).

Attribute Accessors

When setting Buffer attributes, additional data can be provided to specify how the buffer should be accessed. This data can be stored directly on the Buffer accessor or supplied to .setBuffer.

Notes about Integer Attributes

Notes about Instanced Rendering

Notes about setting mat type attributes