RTGLSL
RTGLSL A JavaScript library for creating and editing GLSL simulations with minimal JavaScript across multiple pages.
Features
- Simple API - One-line shader registration
- Real-time updates - Automatic parameter synchronization
- Minimal footprint - Lightweight and optimized
- JSON configuration - Support for both objects and JSON strings
- HTML controls - Zero-JavaScript parameter controls via data attributes
- High performance - Buffer caching, uniform optimization, frame rate control
- Auto animation - Built-in time-based animation system
- Multiple simulations - Manage multiple shaders simultaneously
- Render Modes - Support for triangles, lines, points, strips, fans, and more
- Blend Modes - 13+ blend modes including add, multiply, screen, overlay
- 3D Geometry - Vertex/index buffer support with utility functions
- Particle Systems - Built-in particle rendering with physics simulation
Quick Start
Basic Usage
<script src="rtglsl.js"></script>
<script>
// Register a shader simulation - that's it!
RTGLSL.register('myShader', canvas, {
id: 'myShader',
vertex: `precision mediump float; attribute vec2 position; varying vec2 vUv; void main() { vUv = position * 0.5 + 0.5; gl_Position = vec4(position, 0.0, 1.0); }`,
fragment: `precision mediump float; uniform float uTime; uniform vec3 uColor; varying vec2 vUv; void main() { vec3 color = uColor * (sin(uTime) * 0.5 + 0.5); gl_FragColor = vec4(color, 1.0); }`,
uniforms: { uTime: 0, uColor: [1, 0, 0] },
attributes: { position: 0 },
time: { uniform: 'uTime', speed: 2, max: 10, loop: true },
renderMode: 'triangles',
blendMode: 'normal'
});
</script>
HTML Controls (Zero JavaScript!)
<!-- Automatic parameter control via data attributes -->
<input type="range" data-rtglsl-sim="myShader" data-rtglsl-param="uTime" min="0" max="10" step="0.1" value="0">
<input type="range" data-rtglsl-sim="myShader" data-rtglsl-param="uColorR" min="0" max="1" step="0.1" value="1">
<input type="color" data-rtglsl-sim="myShader" data-rtglsl-param="uColor" value="#ff0000">
<!-- Render mode and blend mode controls -->
<select data-rtglsl-sim="myShader" data-rtglsl-param="renderMode">
<option value="triangles">Triangles</option>
<option value="lines">Lines</option>
<option value="points">Points</option>
</select>
<select data-rtglsl-sim="myShader" data-rtglsl-param="blendMode">
<option value="normal">Normal</option>
<option value="add">Add</option>
<option value="multiply">Multiply</option>
</select>
Auto-Generated Controls
// Register a shader first
RTGLSL.register('myShader', canvas, {
id: 'myShader',
vertex: '/* vertex shader code */',
fragment: '/* fragment shader code */',
uniforms: { uTime: 0, uColor: [1, 0, 0], uScale: 1.0 },
attributes: { position: 0 },
time: { uniform: 'uTime', speed: 2, max: 10, loop: true },
renderMode: 'triangles',
blendMode: 'normal'
});
// Generate controls automatically
RTGLSL.generateControls('myShader', 'controlsContainer', {
title: 'Shader Controls',
layout: 'flex',
controlStyle: 'default',
excludeParams: ['uTime'], // Exclude time parameter
customLabels: { uScale: 'Scale Factor' },
customRanges: { uScale: { min: 0.1, max: 3.0, step: 0.1 } }
});
<!-- HTML container for generated controls -->
<div id="controlsContainer"></div>
JSON String Configuration
const jsonConfig = `{
"id": "myShader",
"vertex": "attribute vec2 position; varying vec2 vUv; void main() { vUv = position * 0.5 + 0.5; gl_Position = vec4(position, 0.0, 1.0); }",
"fragment": "precision mediump float; uniform float uTime; uniform vec3 uColor; varying vec2 vUv; void main() { vec3 color = uColor * (sin(uTime) * 0.5 + 0.5); gl_FragColor = vec4(color, 1.0); }",
"uniforms": { "uTime": 0, "uColor": [1, 0, 0] },
"attributes": { "position": 0 },
"time": { "uniform": "uTime", "speed": 2, "max": 10, "loop": true }
}`;
RTGLSL.register('myShader', canvas, jsonConfig);
API Reference
Static Methods (Global Management)
RTGLSL.register(name, canvas, config)
Register a new shader simulation.
Parameters:
name (string): Unique simulation identifier
canvas (HTMLCanvasElement): Canvas element for rendering
config (object|string): Shader configuration object or JSON string
Configuration Object:
{
id: 'shaderId', // Unique shader identifier
vertex: 'vertex shader code', // Vertex shader source
fragment: 'fragment shader code', // Fragment shader source
uniforms: { // Initial uniform values
uTime: 0,
uColor: [1, 0, 0],
uScale: 1.0
},
attributes: { // Attribute locations
position: 0
},
time: { // Optional: Auto animation
uniform: 'uTime', // Uniform to animate
speed: 2, // Animation speed
max: 10, // Maximum value
loop: true // Loop animation
},
renderMode: 'triangles', // Optional: Render mode
blendMode: 'normal', // Optional: Blend mode
geometry: { // Optional: Custom geometry
vertices: [...], // Vertex data
indices: [...], // Index data (optional)
vertexSize: 2 // Components per vertex
},
targetFPS: 60 // Optional: Frame rate limit
}
RTGLSL.update(name, params)
Update simulation parameters in real-time.
RTGLSL.update('myShader', {
uTime: 1.5,
uColor: [0, 1, 0],
uScale: 2.0
});
RTGLSL.get(name)
Get simulation instance by name.
RTGLSL.remove(name)
Remove simulation and cleanup resources.
RTGLSL.cleanup()
Cleanup all simulations and resources.
RTGLSL.play(name)
Start animation for a specific simulation.
RTGLSL.pause(name)
Pause animation for a specific simulation.
RTGLSL.toggle(name)
Toggle play/pause state for a specific simulation.
RTGLSL.clearFrame(name)
Clear the canvas frame for a specific simulation.
RTGLSL.generateControls(simName, containerId, options)
Auto-generate HTML controls for simulation parameters.
Parameters:
simName (string): Simulation name
containerId (string): HTML element ID for controls container
options (object): Control generation options
Options:
{
title: 'Controls Title',
showTitle: true,
layout: 'flex', // 'flex', 'grid', 'vertical'
columns: 4,
controlStyle: 'default', // 'default', 'compact', 'wide'
excludeParams: [],
includeParams: null,
customLabels: {},
customRanges: {},
customSteps: {}
}
Instance Methods (Direct Shader Control)
new RTGLSL().init(canvas)
Initialize WebGL context.
create(config)
Create shader from configuration.
update(id, params)
Update shader parameters.
render(id, geometry?, renderMode?, blendMode?)
Render the shader with optional geometry, render mode, and blend mode.
onUpdate(id, callback)
Set up parameter change listener.
get(id)
Get shader instance by ID.
remove(id)
Remove shader and cleanup resources.
Render Modes
RTGLSL supports comprehensive render modes for different visual effects:
Available Render Modes
triangles - Standard triangle rendering (default)
triangleStrip - Connected triangles for efficient rendering
triangleFan - Fan-shaped triangle pattern
lines - Individual line segments
lineStrip - Connected line segments
lineLoop - Closed line loop
points - Point/particle rendering
Usage