ANGLE_instanced_arrays
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since June 2016.
TheANGLE_instanced_arrays extension is part of theWebGL API and allows to draw the same object, or groups of similar objects multiple times, if they share the same vertex data, primitive count and type.
WebGL extensions are available using theWebGLRenderingContext.getExtension() method. For more information, see alsoUsing Extensions in theWebGL tutorial.
Note:This extension is only available toWebGL1 contexts. InWebGL2, the functionality of this extension is available on the WebGL2 context by default and the constants and methods are available without theANGLE_ suffix.
Despite the name "ANGLE", this extension works on any device if the hardware supports it and not just on Windows when using the ANGLE library. "ANGLE" just indicates that this extension has been written by the ANGLE library authors.
In this article
Constants
This extension exposes one new constant, which can be used in thegl.getVertexAttrib() method:
ext.VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLEReturns a
GLintdescribing the frequency divisor used for instanced rendering when used in thegl.getVertexAttrib()as thepnameparameter.
Instance methods
This extension exposes three new methods.
ext.drawArraysInstancedANGLE()Behaves identically to
gl.drawArrays()except that multiple instances of the range of elements are executed, and the instance advances for each iteration.ext.drawElementsInstancedANGLE()Behaves identically to
gl.drawElements()except that multiple instances of the set of elements are executed and the instance advances between each set.ext.vertexAttribDivisorANGLE()Modifies the rate at which generic vertex attributes advance when rendering multiple instances of primitives with
ext.drawArraysInstancedANGLE()andext.drawElementsInstancedANGLE().
Examples
The following example shows how to draw a given geometry multiple times with a single draw call.
Warning:The following is educational, not production level code. It should generally be avoided to construct data / buffers within the rendering loop or right before use.
// enable the extensionconst ext = gl.getExtension("ANGLE_instanced_arrays");// binding the geometry buffer as usualgl.bindBuffer(gl.ARRAY_BUFFER, geometryVertexBuffer);gl.enableVertexAttribArray(vertexPositionAttributeLocation);gl.vertexAttribPointer( vertexPositionAttributeLocation, 3, gl.FLOAT, false, 0, 0,);// build position bufferconst instancePositions = [];for (const instance of instances) { instancePositions.push( instance.position.x, instance.position.y, instance.position.z, );}const instancePositionBuffer = createWebGLBufferFromData(instancePositions);// binding the instance position buffer as you would with any attributegl.bindBuffer(gl.ARRAY_BUFFER, instancePositionBuffer);gl.enableVertexAttribArray(instancePositionAttributeLocation);gl.vertexAttribPointer( instancePositionAttributeLocation, 3, gl.FLOAT, false, 0, 0,);// mark the attribute as instanced and advance it every single(1) instance rather than every vertexext.vertexAttribDivisorANGLE(instancePositionAttributeLocation, 1);// draw geometry for each instanceext.drawArraysInstancedANGLE( gl.TRIANGLES, 0, numGeometryVertices, instances.length,);Specifications
| Specification |
|---|
| WebGL ANGLE_instanced_arrays Khronos Ratified Extension Specification> |