Absolutely! Let's create a visually stunning, animated 3D shader suitable for a demoparty shader competition. I'll first outline the concept and approach, then provide the full Shadertoy code.
Concept and Planning
Goal:
Create a mesmerizing, animated 3D fractal-like scene with smooth camera movement, intricate geometry, and vibrant colors that evolve over time.
Key Features:
- 3D fractal geometry: Use a Mandelbulb or similar fractal distance estimator for raymarching.
- Smooth camera animation: Orbiting and zooming to reveal different parts of the fractal.
- Dynamic lighting: Phong or Blinn-Phong lighting with a moving light source.
- Color cycling: Use HSV or cosine-based color cycling for psychedelic effects.
- Performance: Optimize raymarching steps and distance estimation for smooth animation.
Step-by-step plan:
- Setup raymarching basics: Ray origin, direction, marching loop.
- Implement Mandelbulb distance estimator: Classic fractal formula.
- Calculate normal for lighting: Gradient approximation.
- Lighting model: Diffuse + specular with a moving light.
- Camera animation: Orbit around fractal center with time.
- Color palette: Use smooth color cycling based on iteration count and time.
- Final composition: Output color with gamma correction.
Code
// Shadertoy shader: Animated Mandelbulb fractal with dynamic lighting and color cycling
// Constants
#define MAX_MARCHING_STEPS 100
#define MAX_DISTANCE 100.0
#define SURFACE_DIST 0.001
#define POWER 8.0
// Distance estimator for Mandelbulb fractal
float mandelbulbDE(vec3 pos) {
vec3 z = pos;
float dr = 1.0;
float r = 0.0;
for (int i = 0; i < 10; i++) {
r = length(z);
if (r > 2.0) break;
// Convert to polar coordinates
float theta = acos(z.z / r);
float phi = atan(z.y, z.x);
dr = pow(r, POWER - 1.0) * POWER * dr + 1.0;
// Scale and rotate the point
float zr = pow(r, POWER);
theta = theta * POWER;
phi = phi * POWER;
// Convert back to cartesian coordinates
z = zr * vec3(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta));
z += pos;
}
return 0.5 * log(r) * r / dr;
}
// Calculate normal by gradient approximation
vec3 getNormal(vec3 p) {
float eps = 0.001;
vec3 e = vec3(eps, 0.0, 0.0);
return normalize(vec3(
mandelbulbDE(p + e.xyy) - mandelbulbDE(p - e.xyy),
mandelbulbDE(p + e.yxy) - mandelbulbDE(p - e.yxy),
mandelbulbDE(p + e.yyx) - mandelbulbDE(p - e.yyx)
));
}
// Phong lighting model
vec3 phongLighting(vec3 pos, vec3 normal, vec3 lightPos, vec3 viewDir) {
vec3 lightDir = normalize(lightPos - pos);
float diff = max(dot(normal, lightDir), 0.0);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
vec3 ambient = vec3(0.1);
vec3 diffuse = vec3(1.0, 0.8, 0.6) * diff;
vec3 specular = vec3(1.0) * spec;
return ambient + diffuse + specular;
}
// HSV to RGB conversion
vec3 hsv2rgb(vec3 c) {
vec3 rgb = clamp( abs(mod(c.x*6.0 + vec3(0.0,4.0,2.0),
6.0) - 3.0) - 1.0,
0.0,
1.0 );
rgb = rgb*rgb*(3.0-2.0*rgb);
return c.z * mix(vec3(1.0), rgb, c.y);
}
// Main raymarch function
float rayMarch(vec3 ro, vec3 rd, out vec3 p) {
float dist = 0.0;
for (int i = 0; i < MAX_MARCHING_STEPS; i++) {
p = ro + rd * dist;
float d = mandelbulbDE(p);
if (d < SURFACE_DIST) return dist;
if (dist > MAX_DISTANCE) break;
dist += d;
}
return -1.0;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Normalized pixel coordinates (from -1 to 1)
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
// Camera setup
float time = iTime * 0.3;
vec3 camPos = vec3(3.5 * cos(time), 2.0 * sin(time * 0.7), 3.5 * sin(time));
vec3 target = vec3(0.0);
vec3 up = vec3(0.0, 1.0, 0.0);
// Camera basis
vec3 forward = normalize(target - camPos);
vec3 right = normalize(cross(forward, up));
vec3 camUp = cross(right, forward);
// Ray direction
vec3 rd = normalize(uv.x * right + uv.y * camUp + 1.5 * forward);
// Raymarch
vec3 p;
float dist = rayMarch(camPos, rd, p);
vec3 col = vec3(0.0);
if (dist > 0.0) {
// Calculate normal
vec3 normal = getNormal(p);
// Light position moving around
vec3 lightPos = vec3(5.0 * cos(time * 1.3), 5.0, 5.0 * sin(time * 1.3));
// View direction
vec3 viewDir = normalize(camPos - p);
// Lighting
vec3 light = phongLighting(p, normal, lightPos, viewDir);
// Color cycling based on position and time
float hue = mod(atan(normal.z, normal.x) / 6.2831 + iTime * 0.1, 1.0);
float saturation = 0.8;
float value = clamp(light.r + light.g + light.b, 0.0, 1.0);
col = hsv2rgb(vec3(hue, saturation, value));
}
// Gamma correction
col = pow(col, vec3(0.4545));
fragColor = vec4(col, 1.0);
}
Explanation
- Mandelbulb fractal: The
mandelbulbDE function implements the distance estimator for the Mandelbulb fractal with power 8.
- Raymarching: We march rays from the camera until we hit the fractal surface or exceed max distance.
- Normal calculation: Approximated by sampling the distance field around the hit point.
- Lighting: Phong lighting with a moving light source to create dynamic highlights.
- Camera: Orbits around the fractal, giving a constantly changing perspective.
- Color: Uses HSV color cycling based on the normal's angle and time for psychedelic effects.
- Gamma correction: Applied for better color appearance on screen.
How to use:
- Copy-paste this code into Shadertoy.
- Run and watch the mesmerizing Mandelbulb fractal animate and glow.
- Adjust parameters like
POWER, MAX_MARCHING_STEPS, or colors for your own twist.
If you want, I can help you further polish or add sound-reactive elements, or optimize for performance! Would you like that?