createBufferData

View sourcecreateBufferData(gl: WebGLRenderingContext, type: number, data: TypeArray, bufferUsage: number): WebGLBuffer

Creates a WebGL buffer, uploads typed-array data to it, and leaves it bound to the supplied target.

The function calls WebGLRenderingContext#createBuffer on gl and asserts that WebGL returned a buffer. It then passes type to WebGLRenderingContext#bindBuffer and uploads data with WebGLRenderingContext#bufferData, passing bufferUsage through as WebGL’s storage-usage hint. The target remains bound to the new buffer when the function returns.

Examples

import { BufferType, DrawUsage, createBufferData } from '@wimaengine/render-webgl'

// `gl` is an existing WebGLRenderingContext.
const positions = new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0])
const vertexBuffer = createBufferData(
  gl,
  BufferType.Array,
  positions,
  DrawUsage.Static
)
// `vertexBuffer` is still bound to BufferType.Array on `gl`.

Parameters

NameDescriptionDefault
gl

WebGL context that owns the buffer.

—
type

Buffer target from BufferType.

—
data

Typed-array data to upload to the target.

—
bufferUsage

Storage-usage hint from DrawUsage.

—

Throws

If WebGLRenderingContext#createBuffer returns null or undefined.

Returns

The created buffer containing the uploaded data.