Evolve Stunning Visual Effects Through Genetic Algorithms
ShaderForge is an interactive WebGL shader playground that combines real-time visual programming with evolutionary computation. Write GLSL shaders, see instant results, and use genetic algorithms to evolve mesmerizing visual effects.
Each evolved shader has genetic traits:
# Clone the repository
git clone https://github.com/yourusername/shaderforge.git
cd shaderforge
# Open in browser
open shaderforge.html
# or
python -m http.server 8000
# Then visit http://localhost:8000
Simply open shaderforge.html in any modern browser - no installation required!
Browser Requirements:
Must support: WebGL 2.0 (or WebGL 1.0 fallback)
Ctrl+Enter or click βCompileβ to see changesprecision mediump float;
uniform vec2 u_resolution;
uniform float u_time;
uniform vec2 u_mouse;
void main() {
// Normalized coordinates (0.0 to 1.0)
vec2 st = gl_FragCoord.xy / u_resolution;
// Mouse interaction
vec2 mouse = u_mouse / u_resolution;
float dist = distance(st, mouse);
// Animated color
vec3 color = vec3(st.x, st.y, abs(sin(u_time)));
color *= 1.0 - dist;
gl_FragColor = vec4(color, 1.0);
}
u_time - Seconds since start (float)u_resolution - Canvas width/height (vec2)u_mouse - Mouse X/Y position (vec2)u_frame - Frame counter (int, optional)1. Start with preset you like (e.g., "plasma")
2. Click "Evolve From Current"
3. Rate variations (Like/Skip)
4. After 3-5 generations, patterns emerge
5. Save your favorite evolutions
6. Use as starting point for new evolutions
// Combine multiple techniques:
// 1. Fractal noise
float noise(vec2 st) {
return fract(sin(dot(st, vec2(12.9898, 78.233))) * 43758.5453);
}
// 2. Domain warping
vec2 warp(vec2 st) {
st += vec2(sin(st.y * 3.0 + u_time), cos(st.x * 3.0 - u_time)) * 0.1;
return st;
}
// 3. Color palette
vec3 palette(float t) {
vec3 a = vec3(0.5, 0.5, 0.5);
vec3 b = vec3(0.5, 0.5, 0.5);
vec3 c = vec3(1.0, 1.0, 1.0);
vec3 d = vec3(0.0, 0.33, 0.67);
return a + b * cos(6.28318 * (c * t + d));
}
void main() {
vec2 st = gl_FragCoord.xy / u_resolution;
st = warp(st);
float n = noise(st * 10.0 + u_time);
vec3 color = palette(n);
gl_FragColor = vec4(color, 1.0);
}
Data Types:
float x = 1.0; // Single number
vec2 pos = vec2(0.5, 0.5); // 2D vector
vec3 color = vec3(1.0, 0.0, 0.0); // RGB
vec4 rgba = vec4(1.0, 0.0, 0.0, 1.0); // RGBA
Built-in Functions:
sin(x), cos(x), tan(x) // Trigonometry
abs(x), sign(x) // Math
length(v), distance(a,b) // Vector
mix(a, b, t) // Linear interpolation
smoothstep(e0, e1, x) // Smooth interpolation
fract(x) // Fractional part
floor(x), ceil(x) // Rounding
Vector Operations:
vec2 a = vec2(1.0, 2.0);
vec2 b = vec2(3.0, 4.0);
vec2 sum = a + b; // (4.0, 6.0)
vec2 scaled = a * 2.0; // (2.0, 4.0)
float dot = dot(a, b); // 11.0
1. Gradient:
vec2 st = gl_FragCoord.xy / u_resolution;
vec3 color = vec3(st.x, st.y, 0.5);
gl_FragColor = vec4(color, 1.0);
2. Circle:
vec2 st = gl_FragCoord.xy / u_resolution;
vec2 center = vec2(0.5);
float dist = distance(st, center);
float circle = smoothstep(0.3, 0.29, dist);
gl_FragColor = vec4(vec3(circle), 1.0);
3. Animation:
vec2 st = gl_FragCoord.xy / u_resolution;
float wave = sin(st.x * 10.0 + u_time * 2.0);
vec3 color = vec3(wave * 0.5 + 0.5);
gl_FragColor = vec4(color, 1.0);
4. Noise:
float random(vec2 st) {
return fract(sin(dot(st, vec2(12.9898, 78.233))) * 43758.5453);
}
void main() {
vec2 st = gl_FragCoord.xy / u_resolution;
float n = random(st);
gl_FragColor = vec4(vec3(n), 1.0);
}
βββββββββββββββββββββββββββββββββββββββ
β ShaderForge System β
βββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββββββββββββββ β
β β WebGL Rendering Engine β β
β β - Shader Compilation β β
β β - Uniform Management β β
β β - Render Loop (60fps) β β
β βββββββββββββββββββββββββββββββ β
β β
β βββββββββββββββββββββββββββββββ β
β β Genetic Algorithm Engine β β
β β - Population Management β β
β β - Fitness Evaluation β β
β β - Selection & Breeding β β
β β - Mutation Operators β β
β βββββββββββββββββββββββββββββββ β
β β
β βββββββββββββββββββββββββββββββ β
β β Shader DNA System β β
β β - Gene Encoding β β
β β - Code Generation β β
β β - Parameter Evolution β β
β βββββββββββββββββββββββββββββββ β
β β
β βββββββββββββββββββββββββββββββ β
β β User Interface β β
β β - Code Editor β β
β β - Controls Panel β β
β β - Library Manager β β
β βββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββ
// Evolution Process
1. INITIALIZATION
- Create 20 random shader genomes
- Each has 8 genetic traits
2. EVALUATION
- User rates shaders (Like/Skip)
- Fitness = likes - (skips * 0.5)
3. SELECTION
- Tournament selection (size 4)
- Top 30% elite preservation
4. CROSSOVER
- Select two parents
- Randomly inherit traits
- Create offspring
5. MUTATION
- 15% chance per gene
- Gaussian perturbation
- Clamp to valid ranges
6. REPLACEMENT
- New generation replaces old
- Repeat from step 2
| Gene | Range | Effect |
|---|---|---|
| colorSpeed | 0.0 - 2.0 | Color animation rate |
| colorShift | [0-6, 0-6, 0-6] | RGB phase offsets |
| timeScale | 0.1 - 2.0 | Global time multiplier |
| distortion | 0.0 - 1.0 | Noise amount |
| frequency | 1.0 - 20.0 | Pattern repetition |
| octaves | 1 - 5 | Fractal layers |
| symmetry | 0 - 4 | Mirror modes |
| blendMode | 0 - 2 | Compositing |
Problem: Red error message appears Solution:
Common Errors:
// β Wrong
vec2 st = gl_FragCoord.xy / u_resolution
gl_FragColor = vec4(color, 1.0)
// β
Correct
vec2 st = gl_FragCoord.xy / u_resolution; // Need semicolon
gl_FragColor = vec4(color, 1.0); // Need semicolon
Problem: Canvas shows nothing Solution:
Problem: Shaders donβt change after evolution Solution:
Problem: Low FPS or stuttering Solution:
Problem: Save button doesnβt work Solution:
β Do:
const for loop bounds when possibleβ Donβt:
For Beautiful Results:
1. Start with "plasma" or "rainbow"
2. Use low mutation rate (0.1)
3. Evolve slowly (rate 10+ shaders)
4. Save intermediate results
5. Cross-breed favorites
For Wild Experiments:
1. Start with "experimental" preset
2. Use high mutation rate (0.3)
3. Skip often to increase diversity
4. Don't save until gen 5+
5. Embrace chaos
Time-based Animation:
float t = u_time * 0.5; // Slower animation
Mouse Interaction:
vec2 mouse = u_mouse / u_resolution;
float dist = distance(st, mouse);
color *= 1.0 - smoothstep(0.0, 0.5, dist);
Aspect Ratio Correction:
vec2 st = gl_FragCoord.xy / u_resolution;
st.x *= u_resolution.x / u_resolution.y;
Contributions are welcome! Please feel free to submit a Pull Request.
git clone https://github.com/yourusername/shaderforge.git
cd shaderforge
# No build process - pure HTML/JS/WebGL
# Just open shaderforge.html in browser
MIT License
Copyright (c) 2025 ShaderForge
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the βSoftwareβ), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED βAS ISβ, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
If you find ShaderForge useful, please consider: