2.1.0 • Published 7 years ago
@react-vertex/texture-hooks v2.1.0
@react-vertex/texture-hooks
Documentation and Examples
React hooks for working with WebGL textures. More info on using textures in WebGL.
Install via npm:
npm install @react-vertex/texture-hooksImporting:
import {
useTexture2d
} from '@react-vertex/texture-hooks'useTexture2d(gl, url, [getOptions]) => [WebGLTexture, isLoaded]
React hook for 2d WebGL textures. It returns a texture immediately with a placeholder pixel and updates it when the image loads.
Arguments:
gl: A WebGL context.
url: The URL of the texture image to load.
getOptions (optional): A function that will be called with the context (gl) that returns an object with the options you wish to override.
Valid keys in object returned by getOptions:
formatdefualts to gl.RGBAmipMapsBoolean defaults to true. More on mipmaps.typedefaults to gl.UNSIGNED_BYTEwrapdefaults to null (sets both wrapS and wrapT)wrapSdefaults to gl.REPEAT (will be overridden bywrapif used)wrapTdefaults to gl.REPEAT (will be overridden bywrapif used)minMagdefaults to null (sets both minFilter and magFilter)minFilterdefaults to gl.NEAREST_MIPMAP_LINEAR (will be overridden byminMagif used)magFilterdefaults to gl.LINEAR (will be overridden byminMagif used)placeholderdefaults to a black pixel (new Uint8Array([0, 0, 0, 1]))
Returns:
[texture, isLoaded] : An array with a WebGLTexture as the first item and a boolean isLoaded indicating whether the full image has loaded yet.
Example Usage
import { useProgram } from '@react-vertex/shader-hooks'
import { useUniformSampler2d } from '@react-vertex/uniform-hooks'
import { useTexture2d } from '@react-vertex/texture-hooks'
const textureOptions = gl => ({
placeholder: new Uint8Array([0, 0, 1, 1]) // blue
})
...
const program = useProgram(gl, vert, frag)
gl.useProgram(program)
const [texDiff] = useTexture2d(gl, tilesDiffUrl, textureOptions)
useUniformSampler2d(gl, program, 'texDiff', texDiff, 0) // setup a sampler uniform to read unit 0
...