createTexture

View sourcecreateTexture(gl: WebGLRenderingContext, img: TexImageSource, flipY: number): WebGLTexture

Creates a 2D WebGL texture from an image source and uploads its pixels.

The helper calls WebGLRenderingContext#createTexture, optionally enables WebGLRenderingContext#pixelStorei with UNPACK_FLIP_Y_WEBGL, and binds the result with WebGLRenderingContext#bindTexture. It uploads img with WebGLRenderingContext#texImage2D using RGBA and UNSIGNED_BYTE, sets linear magnification and linear-mipmap-nearest minification filters, and generates mipmaps. When flipY is truthy, the unpack flag is set back to false after the upload; the previous context value is not saved or restored. The created texture remains bound to TEXTURE_2D when the function completes.

Examples

import { createTexture } from '@wimaengine/render-webgl'

// `gl` is an existing WebGLRenderingContext and `image` is a TexImageSource.
const texture = createTexture(gl, image, 1) // truthy flipY flips the upload vertically

Parameters

NameDescriptionDefault
gl

WebGL context that owns the texture.

—
img

Image source passed to texImage2D.

—
flipY

Truthy to enable vertical unpack flipping during the upload; falsy to leave the unpack flag unchanged.

—

Throws

If a WebGL operation rejects the image or texture state; such errors propagate without restoring context state in a finally block.

Returns

The allocated texture, or null if WebGL could not allocate one.