Skip to main content

API Reference

Lightweight graphics library unifying WebGL/WebGPU. glre is a modern graphics library that unifies the complex APIs of WebGL2 and WebGPU, leveraging TypeScript's type safety and the benefits of reactive programming.

Initialization

Framework Integration

import { useGL } from 'glre/react'

const Component = () => {
const gl = useGL({
width: 800,
height: 600,
})

return <canvas ref={gl.ref} />
}

Configuration Options

GL Configuration

Controls basic WebGL/WebGPU settings.

PropertyTypeDefaultDescription
elHTMLCanvasElement-Canvas element
widthnumberwindow.innerWidthCanvas width
heightnumberwindow.innerHeightCanvas height
isWebGLbooleanAuto-detectedForce WebGL usage
isLoopbooleantrueAnimation loop
countnumber6Vertex count

Shader Configuration

const gl = useGL({
// Fragment shader (Node System or string)
frag: fragmentShader,
fs: fragmentShader, // Alias

// Vertex shader (optional)
vert: vertexShader,
vs: vertexShader, // Alias
})

Data Management

Uniform Variables

Set constant values for shaders.

// Numbers
gl.uniform('iTime', performance.now() / 1000)

// Vectors
gl.uniform('iResolution', [width, height])
gl.uniform('iMouse', [mouseX, mouseY])

Attribute Variables

Set vertex data.

// Vertex positions
gl.attribute('a_position', [-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1])

// Batch setting of multiple attributes
gl.attribute({
a_position: positions,
a_texCoord: uvCoordinates,
a_normal: normals,
})

Textures

Use image resources in shaders.

gl.texture('diffuse', '/path/to/texture.jpg')
gl.texture('normal', '/path/to/normal.png')

Event System

Lifecycle

const gl = useGL({
// Initialization (executed once)
init() {
console.log('GL context initialized')
},

// On mount
mount() {
console.log('Rendering started')
},

// On cleanup
clean() {
console.log('Resources released')
},
})

Frame Callbacks

const gl = useGL({
loop(deltaTime) {
// Per-frame processing
gl.uniform('iTime', performance.now() / 1000)
gl.uniform('iFrame', gl.frame.count)
},
})

Interaction

const gl = useGL({
// Mouse movement
mousemove(event) {
const x = event.clientX / gl.size[0]
const y = 1.0 - event.clientY / gl.size[1]
gl.uniform('iMouse', [x, y])
},

// Resize
resize(width, height) {
gl.uniform('iResolution', [width, height])
},

// Custom rendering
render() {
// Custom drawing logic
customRenderLogic()
},
})

Node System Integration

Basic Usage

import { vec2, vec3, vec4, sin, cos, fract } from 'glre/node'

const fragmentShader = Fn(() => {
const uv = position.xy.div(iResolution).toVar()
const time = iTime.toVar()

const color = vec3(sin(time.add(uv.x.mul(10))), cos(time.add(uv.y.mul(10))), sin(time.mul(2)))
.mul(0.5)
.add(0.5)

return vec4(color, 1.0)
})

const gl = useGL({ frag: fragmentShader })

Functions and Scope

const noiseFunction = Fn((args) => {
const [uv, time] = args
return sin(uv.x.mul(10).add(time))
.mul(sin(uv.y.mul(10).add(time)))
.mul(0.5)
.add(0.5)
})

const mainShader = Fn(() => {
const uv = position.xy.div(iResolution).toVar()
const noise = noiseFunction(uv, iTime)
return vec4(noise, noise, noise, 1.0)
})