Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Nightre/Rapid.js/llms.txt
Use this file to discover all available pages before exploring further.
GLShader wraps a compiled WebGL2 program. It auto-parses attribute and uniform names and types from GLSL source at construction time, so you can set them by name without ever querying locations manually. Each instance owns a dedicated VAO and exposes a clean API for uploading uniforms, binding vertex attributes, and managing GPU lifecycle.
Constructor
The WebGL2 rendering context used to compile and link the shader program.
Vertex shader GLSL source. Parsed for
in and uniform declarations to populate attributeLoc and uniformLoc.Fragment shader GLSL source. Parsed the same way as the vertex source; uniform declarations in either stage are merged into the same map.
Optional array of attribute descriptors. When provided,
setAttributes is called immediately after the VAO is created so the binding state is recorded in the VAO.Public Properties
| Property | Type | Description |
|---|---|---|
program | WebGLProgram | The linked WebGL program object. |
attributeLoc | Record<string, number> | Map of GLSL attribute name → gl.getAttribLocation result, populated automatically by the GLSL parser. |
uniformLoc | Record<string, WebGLUniformLocation> | Map of GLSL uniform name → gl.getUniformLocation result, populated automatically. |
isCustom | boolean | true when this shader was produced by CustomGlShader; used internally by the render pipeline. |
padding | number | Extra pixels this shader needs outside the sprite quad boundary (e.g. for glow or outline effects). |
shaderId | number | Monotonically-increasing debug identifier assigned at construction. |
vao | WebGLVertexArrayObject | The vertex array object that records all attribute pointer bindings for this program. |
Methods
use()
Activates this shader program on the GL context (gl.useProgram). Call before issuing draw calls that should use this shader.
bindVAO()
Binds the shader’s VAO (gl.bindVertexArray). Call before recording new attribute bindings or before a draw call that replays recorded bindings.
unbindVAO()
Unbinds the current VAO by binding null. Call after recording attribute bindings to prevent accidental mutations.
setUniform(name, value)
Set a single uniform by name. The correct gl.uniform* overload is chosen automatically based on the GLSL type parsed from the source. If the name is not found in uniformLoc the call is silently ignored.
setUniforms(map)
Batch-set uniforms from a plain object. Equivalent to calling setUniform once per key.
setAttribute(attr)
Bind a single vertex attribute pointer, enable the array, and set the instancing divisor. The VBO that supplies data for this attribute must already be bound before calling this method.
setAttributes(attrs)
Bind multiple attribute pointers at once. Calls setAttribute for each entry.
destroy()
Delete the underlying WebGLProgram and release GPU memory. After calling destroy, the GLShader instance must not be used again.
IAttribute Interface
Describes one vertex attribute binding passed tosetAttribute or setAttributes.
The GLSL attribute variable name exactly as it appears in the vertex shader source (e.g.
"aPosition").Number of scalar components per vertex:
1, 2, 3, or 4.WebGL type constant for individual components (e.g.
gl.FLOAT, gl.UNSIGNED_BYTE). Defaults to gl.FLOAT.When
true, integer values are mapped to the [0, 1] (unsigned) or [-1, 1] (signed) floating-point range. Defaults to false.Byte distance between the start of one vertex’s data and the start of the next vertex’s data in the buffer.
Byte offset from the start of the stride block to the first component of this attribute. Defaults to
0.Instancing divisor passed to
gl.vertexAttribDivisor. 0 means the attribute advances once per vertex (default). 1 means once per instance.Example
UniformValue Type
UniformValue is the union of all value types accepted by setUniform and setUniforms:
gl.uniform* variant is selected at runtime based on the GLSL type stored in the internal uniformType map:
| GLSL type | Dispatch |
|---|---|
float | gl.uniform1f |
int, bool, sampler2D, samplerCube | gl.uniform1i / gl.uniform1iv (array) |
vec2 | gl.uniform2fv |
vec3 | gl.uniform3fv |
vec4 | gl.uniform4fv |
ivec2, bvec2 | gl.uniform2iv |
ivec3, bvec3 | gl.uniform3iv |
ivec4, bvec4 | gl.uniform4iv |
mat2 | gl.uniformMatrix2fv |
mat3 | gl.uniformMatrix3fv |
mat4 | gl.uniformMatrix4fv |
Auto-Parsing from GLSL
The constructor calls an internalparseShader method on both the vertex and fragment source strings. It extracts all declarations matching:
- Attributes —
in <type> <name>(GLSL ES 3.0) orattribute <type> <name>(GLSL ES 1.0) - Uniforms —
uniform <type> <name>oruniform <type> <name>[N](array form)
gl.getAttribLocation / gl.getUniformLocation is looked up once and cached in attributeLoc / uniformLoc. The GLSL type string is stored internally so setUniform can dispatch without any additional introspection at call time.
Array uniforms (e.g. uniform sampler2D uTextures[8]) are flagged internally so gl.uniform1iv is used instead of gl.uniform1i.