Skip to content

Instantly share code, notes, and snippets.

@vagmi
Created March 27, 2026 18:26
Show Gist options
  • Select an option

  • Save vagmi/8d17c98d5a507fc42031ac48176ce019 to your computer and use it in GitHub Desktop.

Select an option

Save vagmi/8d17c98d5a507fc42031ac48176ce019 to your computer and use it in GitHub Desktop.
// Emerald Multidimensional Fractal - ShaderToy Fragment
// Created by Vagmi
#define MAX_STEPS 200
#define SURFACE_DIST 0.004
#define MAX_DIST 10.0
#define ITERATIONS 12
// Rotation matrix for fractal complexity
mat2 rot(float a) {
float s = sin(a), c = cos(a);
return mat2(c, -s, s, c);
}
// Distance Function (The Fractal)
float map(vec3 p) {
float scale = 1.0;
float time = iTime * 0.1;
// Initial rotation to move through the fractal
p.xz *= rot(time * 0.3);
p.xy *= rot(time * 0.2);
float dist = 100.0;
for (int i = 0; i < ITERATIONS; i++) {
// Fold space (The magic of fractals)
p = abs(p) - vec3(1.2, 1.5, 1.1);
// Rotate and scale in "higher dimensions"
if (p.x < p.y) p.xy = p.yx;
if (p.x < p.z) p.xz = p.zx;
if (p.y < p.z) p.yz = p.zy;
float s = 1.6; // Scale factor
p *= s;
p -= vec3(0.5, 2.5, 0.8) * (s - 1.0);
scale *= s;
}
// Distance to a sphere-like structure within the folded space
return length(p) / scale;
}
// Basic Raymarching loop
float rayMarch(vec3 ro, vec3 rd) {
float dO = 0.0;
for (int i = 0; i < MAX_STEPS; i++) {
vec3 p = ro + rd * dO;
float dS = map(p);
dO += dS;
if (dO > MAX_DIST || dS < SURFACE_DIST) break;
}
return dO;
}
// Calculate normal for lighting
vec3 getNormal(vec3 p) {
float d = map(p);
vec2 e = vec2(0.01, 0);
vec3 n = d - vec3(map(p - e.xyy), map(p - e.yxy), map(p - e.yyx));
return normalize(n);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Center coordinates
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
// Camera setup
vec3 ro = vec3(0, 0, -4); // Ray origin
vec3 rd = normalize(vec3(uv, 1.2)); // Ray direction
// March
float d = rayMarch(ro, rd);
// Emerald Color Palette
vec3 deepGreen = vec3(0.01, 0.1, 0.05);
vec3 emerald = vec3(0.31, 0.78, 0.47);
vec3 brightSeafoam = vec3(0.6, 1.0, 0.8);
vec3 col = vec3(0);
if (d < MAX_DIST) {
vec3 p = ro + rd * d;
vec3 n = getNormal(p);
// Simple Diffuse Lighting
float diff = dot(n, normalize(vec3(1, 2, -3))) * 0.5 + 0.5;
// Use depth and normal to blend greens
col = mix(deepGreen, emerald, diff);
// Specular highlight for a "crystalline" look
float spec = pow(max(0.0, dot(reflect(rd, n), normalize(vec3(1, 2, -3)))), 20.0);
col += brightSeafoam * spec * 0.5;
// Add "Emerald" glow based on distance
col *= 1.2 / (1.0 + d * 0.1);
} else {
// Background: subtle dark green gradient
col = deepGreen * (1.0 - length(uv));
}
// Post processing: Subtle bloom / gamma correction
col = pow(col, vec3(0.8));
fragColor = vec4(col, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment