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.
CustomGlShader injects user-provided GLSL vertex() and fragment() hook functions into the engine’s built-in sprite and graphic shaders, without replacing the full program. Rather than writing a complete shader from scratch, you supply two small GLSL snippets that are spliced into the engine’s template at compile time. Compilation is lazy — a distinct GLShader is compiled the first time each rendering region encounters the shader, so there is no upfront cost for regions that never draw with it.
Constructor
The active
Rapid renderer instance. Required so that setUniforms can call rapid.flush() before mutating uniform state.Vertex hook GLSL source. Must define the function
void vertex(vec2 position, vec4 vRegion) {}. The body is called from the engine’s vertex shader immediately after the built-in transform.Fragment hook GLSL source. Must define the function
void fragment(inout vec4 fragColor) {}. The body is called from the engine’s fragment shader with the fully-composited sprite colour as input; modify fragColor in-place to change the final pixel output.Number of additional texture units to reserve for your own samplers (default
0). The engine occupies the first N units for sprite atlases; your textures are bound starting at unit N.Initial uniform values applied when the first
GLShader is compiled for a region. Equivalent to calling setUniforms immediately after construction.GLSL Hook Signatures
Both hooks must be present in their respective source strings. The engine splices each hook into its shader template and calls it at the appropriate stage.Public Properties
| Property | Type | Description |
|---|---|---|
vs | string | The vertex hook source provided at construction. |
fs | string | The fragment hook source provided at construction. |
glshader | Map<string, GLShader> | Map of compiled GLShader instances keyed by region key. Populated lazily on the first draw call per region. |
uniforms | Record<string, UniformValue> | Current uniform values. Updated by setUniforms. |
uniformDirty | Set<string> | Set of region keys whose compiled GLShader instances need uniform re-upload on the next draw. |
uniformTextures | Record<string, WebGLTexture> | Map of texture uniform names to WebGLTexture objects to bind when this shader is active. |
usedTextureUnitNum | number | Number of additional texture units reserved for user-supplied textures. |
padding | number | Extra pixels expanded on each side of the sprite quad. Set via setPadding. |
rapid | Rapid | The Rapid renderer instance passed at construction. Used to call rapid.flush() before mutating uniform state. |
Methods
setUniforms(map)
Set uniform values or bind WebGL textures. Internally calls rapid.flush() first to ensure that any in-progress draw batch is submitted with the previous uniform state before the new values take effect.
Plain UniformValue entries update the uniforms map; WebGLTexture entries update uniformTextures. All currently-compiled per-region GLShader instances are marked dirty so they pick up the new values on their next draw call.
setPadding(pixels)
Expand the sprite quad by pixels on every side. This is necessary for effects such as outlines, glows, or blurs that produce visible output outside the original sprite boundary. Returns this for chaining.
Propagates immediately to all already-compiled GLShader instances managed by this CustomGlShader.
applyUniform(key, textureUniforms) (internal)
Applies pending uniform and texture-unit assignments to the compiled GLShader for the given region key. Called automatically by the render pipeline; you do not need to call this directly.
getGLShader(region, key, baseVS?, baseFS?) (internal)
Retrieves the cached GLShader for a region, or compiles a new one by splicing the hook source into the engine’s base shader templates. Called automatically by the render pipeline; you do not need to call this directly.