// Astonishing Animated 3D Cosmic Scene
// Inspired by demoparty aesthetics: Orbiting planet with moons, fog, glow, and color cycling.
// --- Helper Functions ---
float sdSphere(vec3 p, float r) { // Signed distance to a sphere
return length(p) - r;
}
float sdPlane(vec3 p, vec3 n, float h) { // Signed distance to a plane
return dot(p, n) + h;
}
float sceneSDF(vec3 p, float time) { // Combined scene SDF
// Large central planet (pulsing radius for glow effect)
float planetRadius = 1.0 + 0.1 * sin(time * 2.0); // Pulsing animation
float planet = sdSphere(p, planetRadius);
// Moon 1: Orbiting at a moderate distance
vec3 moon1Pos = vec3(1.5 * sin(time * 0.5), 0.5 * cos(time * 0.5), 1.5 * cos(time * 0.5));
float moon1 = sdSphere(p - moon1Pos, 0.2);
// Moon 2: Orbiting at a farther distance with faster speed
vec3 moon2Pos = vec3(2.5 * sin(time * 1.0), 0.3 * sin(time * 0.7), 2.5 * cos(time * 1.0));
float moon2 = sdSphere(p - moon2Pos, 0.15);
// Ground plane for added depth
float ground = sdPlane(p, vec3(0,1,0), 1.0); // Plane at y = -1
// Union of all objects
return min(min(min(planet, moon1), moon2), ground);
}
vec3 getNormal(vec3 p, float time) { // Compute normal via finite differences
float eps = 0.001;
vec2 e = vec2(eps, 0);
return normalize(vec3(
sceneSDF(p + e.xyz, time) - sceneSDF(p - e.xyz, time),
sceneSDF(p + e.yxz, time) - sceneSDF(p - e.yxz, time),
sceneSDF(p + e.zxy, time) - sceneSDF(p - e.zxy, time)
));
}
float rayMarch(vec3 ro, vec3 rd, float time) { // Ray marching loop
float t = 0.0; // Distance along ray
for(int i = 0; i < 100; i++) { // Limit to 100 steps for performance
vec3 p = ro + rd * t;
float d = sceneSDF(p, time);
if(d < 0.001) return t; // Hit something
t += d; // Step forward
if(t > 50.0) break; // Max distance
}
return -1.0; // No hit
}
float softShadow(vec3 ro, vec3 rd, float time, float k) { // Simple soft shadows
float t = 0.01;
float res = 1.0;
for(int i = 0; i < 50; i++) {
float d = sceneSDF(ro + rd * t, time);
res = min(res, k * d / t);
t += d;
if(t > 20.0) break;
}
return clamp(res, 0.0, 1.0);
}
// --- Main Function ---
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Normalized and aspect-corrected UV coordinates
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y; // Center and correct aspect ratio
uv *= 1.0 + 0.1 * sin(iTime * 0.1); // Slight anti-aliasing via time-varying zoom
float time = iTime; // Global time for animation
// Camera setup: Orbiting around the scene
vec3 ro = vec3(3.0 * sin(time * 0.3), 1.5 + sin(time * 0.2), 3.0 * cos(time * 0.3)); // Camera position
vec3 lookAt = vec3(0.0, 0.0, 0.0); // Look at origin (planet center)
vec3 fwd = normalize(lookAt - ro);
vec3 right = normalize(cross(vec3(0,1,0), fwd));
vec3 up = cross(fwd, right);
vec3 rd = normalize(fwd * 1.0 + right * uv.x + up * uv.y); // Ray direction
// Ray march the scene
float t = rayMarch(ro, rd, time);
vec3 col; // Final color
if(t > 0.0) { // Hit something
vec3 p = ro + rd * t; // Hit point
vec3 normal = getNormal(p, time); // Compute normal
// Lighting: Directional light with some ambient
vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0));
float diffuse = max(dot(normal, lightDir), 0.0);
float specular = pow(max(dot(reflect(-lightDir, normal), rd), 0.0), 32.0); // Specular highlight
float shadow = softShadow(p + normal * 0.01, lightDir, time, 10.0); // Soft shadows
// Object-based coloring with time-varying hues
float dPlanet = sdSphere(p, 1.0); // Check distance to planet
float dMoon1 = sdSphere(p - vec3(1.5 * sin(time * 0.5), 0.5 * cos(time * 0.5), 1.5 * cos(time * 0.5)), 0.2);
float dMoon2 = sdSphere(p - vec3(2.5 * sin(time * 1.0), 0.3 * sin(time * 0.7), 2.5 * cos(time * 1.0)), 0.15);
float dGround = sdPlane(p, vec3(0,1,0), 1.0);
vec3 baseColor;
if(abs(dPlanet) < 0.01) baseColor = vec3(0.8, 0.2, 0.5) + 0.3 * sin(time + vec3(0,2,4)); // Glowing planet
else if(abs(dMoon1) < 0.01) baseColor = vec3(0.1, 0.5, 0.8) + 0.2 * cos(time * 0.5 + vec3(1,3,5)); // Blue moon
else if(abs(dMoon2) < 0.01) baseColor = vec3(0.5, 0.8, 0.1) + 0.2 * sin(time * 0.7 + vec3(2,4,6)); // Green moon
else baseColor = vec3(0.2, 0.1, 0.5) + 0.1 * cos(time + vec3(3,5,7)); // Ground
col = baseColor * (0.2 + 0.8 * diffuse * shadow) + vec3(0.5 * specular); // Apply lighting
} else { // Background: Starfield gradient
col = vec3(0.05, 0.1, 0.2) + 0.5 * pow(vec3(1.0), vec3(1.0 / (1.0 + dot(rd, vec3(0,1,0))))); // Simple sky gradient
col += 0.1 * vec3(sin(rd.x * 10.0 + time), cos(rd.y * 15.0 + time), sin(rd.z * 20.0)); // Starfield noise
}
// Post-processing: Fog and chromatic aberration
float fogAmount = 1.0 / (1.0 + exp(-t * 0.1)); // Exponential fog based on distance
col = mix(col, vec3(0.0, 0.0, 0.1), fogAmount); // Fog color
vec2 chromAb = uv * 0.01 * sin(time + length(uv)); // Subtle chromatic aberration
col.r += chromAb.x;
col.b -= chromAb.y;
col = clamp(col, 0.0, 1.0); // Final clamp
fragColor = vec4(col, 1.0); // Output
}