#version 300 es
precision highp float;
out vec4 fragColor;
uniform float iTime;
uniform vec2 iResolution;
const int MAX_MARCH = 128; // Max ray march steps for performance
const float MAX_DIST = 20.0; // Max ray distance
const float SURF_DIST = 0.001; // Surface hit threshold
const vec3 LIGHT_POS = vec3(2.0, 2.0, 2.0); // Base light position (animated)
// Signed Distance Field (SDF) for a deformed sphere (tunnel wall element)
float sdSphere(vec3 p, float r) {
return length(p) - r;
}
// Main scene SDF with symmetry folding and deformation
float map(vec3 p) {
// Apply kaleidoscopic symmetry: fold space across axes for infinite reflections
vec3 q = abs(mod(p, 2.0) - 1.0); // Mirror symmetry with tiling for efficiency
q = abs(q); // Additional folding for more complexity (8-fold symmetry)
// Deform the object with time and position for fractal-like animation
float anim = sin(iTime * 0.5) * 0.2; // Pulsating animation factor
vec3 deform = vec3(sin(p.x * 2.0 + iTime), cos(p.y * 2.0 + iTime), sin(p.z * 2.0 + iTime)) * 0.1; // Add wavy distortion
float d = sdSphere(q + deform, 0.5 + 0.2 * sin(iTime + length(p) * 0.5)); // Sphere with size pulsation and distance-based variation
return d;
}
// Compute normal using finite differences
vec3 calcNormal(vec3 p) {
vec2 e = vec2(0.001, 0.0);
return normalize(vec3(
map(p + e.xyy) - map(p - e.xyy),
map(p + e.yxy) - map(p - e.yxy),
map(p + e.yyx) - map(p - e.yyx)
));
}
// Soft shadow calculation using ray marching
float softShadow(vec3 ro, vec3 rd, float mint, float maxt) {
float res = 1.0;
float t = mint;
float ph = 1e10; // Large initial value for shadow penumbra
for(int i = 0; i < 32; i++) { // Fewer steps for shadow to save performance
float h = map(ro + rd * t);
float y = h * h / (2.0 * ph); // Parallax approximation for soft shadows
res = min(res, 10.0 * y / t); // Attenuate shadow
ph = h;
t += clamp(h, 0.02, 0.2); // Adaptive step size
if(h < 0.001) return 0.0; // Hard shadow if hit
if(t > maxt) break;
}
return clamp(res, 0.0, 1.0);
}
// Ambient occlusion approximation
float ambientOcclusion(vec3 p, vec3 n) {
float occ = 0.0;
float sca = 1.0;
for(int i = 0; i < 5; i++) {
float hr = 0.01 + 0.12 * float(i) / 4.0;
vec3 dir = n * hr; // Sample along normal
float dd = map(p + dir);
occ += (hr - dd) * sca;
sca *= 0.95; // Reduce influence with distance
}
return clamp(1.0 - 3.0 * occ, 0.0, 1.0);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Aspect-corrected UV coordinates
vec2 uv = (2.0 * fragCoord - iResolution.xy) / iResolution.y;
// Time and animation setup
float time = iTime * 0.5; // Slow down animation for smoother motion
// Camera setup: flying through the tunnel with acceleration
vec3 ro = vec3(0.0, 0.0, -5.0) + vec3(sin(time) * 3.0, cos(time * 0.7) * 2.0, time * 2.0); // Camera position with forward motion
vec3 ta = vec3(0.0, 0.0, time * 2.0 + 5.0); // Look-ahead target for tunnel effect
vec3 cw = normalize(ta - ro);
vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
vec3 cv = cross(cu, cw);
vec3 rd = normalize(uv.x * cu + uv.y * cv + 2.0 * cw); // Ray direction with FOV control
// Ray marching
float t = 0.0; // Distance along ray
vec3 p; // Hit position
bool hit = false;
for(int i = 0; i < MAX_MARCH; i++) {
p = ro + rd * t;
float d = map(p);
if(d < SURF_DIST * t) { // Numerical stability with distance scaling
hit = true;
break;
}
t += d; // Step along the distance field
if(t > MAX_DIST) break; // Early exit if too far
}
vec3 col; // Final color
if(hit) {
// Compute normal, lighting, and shading
vec3 n = calcNormal(p);
// Animated light position
vec3 lightPos = LIGHT_POS + vec3(sin(time * 2.0), cos(time * 2.0), sin(time)) * 2.0; // Moving light for dynamic shadows
vec3 lightDir = normalize(lightPos - p);
// Diffuse and specular lighting
float diff = max(dot(n, lightDir), 0.0);
vec3 viewDir = normalize(ro - p);
vec3 halfVec = normalize(lightDir + viewDir);
float spec = pow(max(dot(n, halfVec), 0.0), 32.0) * 0.5; // Specular highlight
// Shadow and ambient occlusion
float shadow = softShadow(p + n * 0.01, lightDir, 0.01, length(lightPos - p)); // Offset to avoid self-intersection
float ao = ambientOcclusion(p, n);
// Base color with animation and fractal variation
vec3 baseCol = 0.5 + 0.5 * cos(time * 2.0 + length(p) * 2.0 + vec3(0.0, 2.0, 4.0)); // Vibrant, shifting colors based on position and time
// Combine lighting
col = baseCol * (0.2 + diff * 0.8 * shadow) + spec * vec3(1.0); // Add specular highlight
col *= ao; // Apply ambient occlusion for depth
// Fog for depth and atmosphere
float fogFactor = exp(-0.05 * t); // Exponential fog falloff
vec3 bg = vec3(0.01, 0.02, 0.05) * (1.0 + rd.y * 0.5); // Dark background gradient
col = mix(bg, col, fogFactor);
} else {
// Background: starry sky with simple noise for stars
vec3 bg = vec3(0.01, 0.02, 0.05) * (1.0 + rd.y * 0.5); // Blueish gradient
float star = smoothstep(0.99, 1.0, fract(sin(dot(rd.xy, vec2(12.9898, 78.233))) * 43758.5453)); // Simple hash-based stars
col = bg + vec3(star) * 0.8; // Add white stars
}
// Post-processing: vignette and color grading for polish
vec2 vignetteUV = uv * (0.5 + 0.5 * sin(iTime * 0.1)) * 0.9; // Slight animated vignette
float vignette = smoothstep(1.0, 0.3, dot(vignetteUV, vignetteUV)); // Darken edges
col *= vignette;
col = clamp(col, 0.0, 1.0); // Clamp to avoid overbright areas
col = pow(col, vec3(1.2)) * vec3(1.1, 1.0, 0.9); // Simple tone mapping and warm color grading
// Output
fragColor = vec4(col, 1.0);
}