Creating the shader viewer
This was my example to see if could get audio input into the shader viewer.
Free to use for anything. No credit needed.
// P1 angle: 0 or 1
// P2 speed
// P3 sound reaction
// P4 size planets
// P5 zoom in
// P6 trajactory lines
// P7 sun glow
// P8 stars(0 = aan, 1 = uit)
#define PI 3.14159265
float hash31(vec3 p) { p = fract(p * 0.1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); }
float hash21(vec2 p) { vec3 p3 = fract(vec3(p.xyx) * 0.1031); p3 += dot(p3, p3.yzx + 33.33); return fract((p3.x + p3.y) * p3.z); }
float noise3(vec3 p)
{
vec3 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
return mix(mix(mix(hash31(i), hash31(i + vec3(1, 0, 0)), f.x),
mix(hash31(i + vec3(0, 1, 0)), hash31(i + vec3(1, 1, 0)), f.x), f.y),
mix(mix(hash31(i + vec3(0, 0, 1)), hash31(i + vec3(1, 0, 1)), f.x),
mix(hash31(i + vec3(0, 1, 1)), hash31(i + vec3(1, 1, 1)), f.x), f.y), f.z);
}
float fbm(vec3 p, int oct)
{
float v = 0.0, a = 0.5;
for (int i = 0; i < 7; i++)
{
if (i >= oct) break;
v += a * noise3(p);
p = p * 2.02 + vec3(1.7, 9.2, 3.1);
a *= 0.5;
}
return v;
}
float spec(float x) { return texture(iChannel0, vec2(x, 0.25)).x; }
mat3 rotY(float a) { float c = cos(a), s = sin(a); return mat3(c, 0, -s, 0, 1, 0, s, 0, c); }
mat3 rotZ(float a) { float c = cos(a), s = sin(a); return mat3(c, s, 0, -s, c, 0, 0, 0, 1); }
// ---- gegevens per planeet: 0 Mercurius .. 7 Neptunus ----
const float ORB[8] = float[8](0.20, 0.27, 0.37, 0.47, 0.60, 0.73, 0.84, 0.94);
const float SIZE[8] = float[8](0.011, 0.018, 0.034, 0.014, 0.046, 0.038, 0.025, 0.024);
const float SPD[8] = float[8](2.6, 1.6, 1.0, 0.62, 0.26, 0.16, 0.09, 0.065);
const float PH[8] = float[8](0.3, 2.1, 4.0, 5.2, 1.0, 3.3, 0.6, 2.7);
const float BAND[8] = float[8](0.75, 0.55, 0.40, 0.30, 0.03, 0.07, 0.13, 0.19);
float T_tilt; // kijkhoek
float gTime;
// positie in beeld en diepte (groter = verder weg) van een punt in het baanvlak
vec3 project(vec2 xz)
{
return vec3(xz.x, xz.y * sin(T_tilt), xz.y * cos(T_tilt));
}
vec3 planetPos(int i)
{
float a = PH[i] + gTime * SPD[i] * 0.35;
return project(ORB[i] * vec2(cos(a), sin(a)));
}
// ---- oppervlakken ----
vec3 earthSurface(vec3 n, vec3 L, out float alphaCloud)
{
float spin = gTime * 0.25;
vec3 s = rotY(spin) * (rotZ(0.41) * n);
float lat = asin(clamp(s.y, -1.0, 1.0));
float c = fbm(s * 1.6 + vec3(3.7, 1.2, 8.1), 6);
c += 0.18 * (fbm(s * 5.0 + 11.0, 4) - 0.5);
float land = smoothstep(0.535, 0.55, c);
vec3 deep = vec3(0.02, 0.10, 0.32);
vec3 shallow = vec3(0.06, 0.32, 0.55);
vec3 ocean = mix(deep, shallow, smoothstep(0.44, 0.535, c));
float dry = smoothstep(0.15, 0.45, abs(lat)) * (1.0 - smoothstep(0.55, 0.75, abs(lat)));
dry *= smoothstep(0.45, 0.6, fbm(s * 3.0 + 4.0, 4));
vec3 green = mix(vec3(0.10, 0.36, 0.10), vec3(0.20, 0.42, 0.14), fbm(s * 8.0, 3));
vec3 desert = vec3(0.70, 0.58, 0.36);
vec3 north = vec3(0.22, 0.30, 0.16);
vec3 ground = mix(green, desert, dry);
ground = mix(ground, north, smoothstep(0.8, 1.0, abs(lat)));
float mount = smoothstep(0.6, 0.75, c + 0.1 * fbm(s * 12.0, 3));
ground = mix(ground, vec3(0.45, 0.40, 0.36), mount * 0.6);
vec3 col = mix(ocean, ground, land);
float ice = smoothstep(1.12, 1.22, abs(lat) + 0.08 * (fbm(s * 6.0, 3) - 0.5));
col = mix(col, vec3(0.92, 0.95, 1.0), ice);
float diff = max(dot(n, L), 0.0);
vec3 H = normalize(L + vec3(0, 0, -1));
float sp = pow(max(dot(n, H), 0.0), 40.0) * (1.0 - land) * (1.0 - ice) * 0.6;
vec3 lit = col * (0.03 + 1.05 * diff) + vec3(1.0, 0.95, 0.85) * sp * step(0.0, dot(n, L));
// stadslichtjes op de nachtkant
float night = smoothstep(0.08, -0.08, dot(n, L));
float city = step(0.86, hash31(floor(s * 160.0))) * land * (1.0 - ice) * (1.0 - dry * 0.8);
city *= smoothstep(0.45, 0.6, fbm(s * 7.0 + 2.0, 3));
lit += vec3(1.0, 0.65, 0.25) * city * night * 0.9;
// wolken
vec3 sc = rotY(spin * 1.15) * (rotZ(0.41) * n);
float cl = fbm(sc * 2.6 + vec3(gTime * 0.02, 0.0, 0.0), 6);
cl = smoothstep(0.56, 0.72, cl + 0.05 * sin(asin(clamp(sc.y, -1.0, 1.0)) * 6.0));
alphaCloud = cl;
vec3 cloud = vec3(1.0) * (0.04 + 1.0 * diff);
lit = mix(lit, cloud, cl * 0.85);
return lit;
}
vec3 planetSurface(int i, vec3 n, vec3 L)
{
vec3 s = rotY(gTime * (0.1 + 0.05 * float(i))) * n;
float y = n.y;
vec3 col;
if (i == 0) col = mix(vec3(0.45, 0.42, 0.40), vec3(0.62, 0.58, 0.55), fbm(s * 6.0, 5));
else if (i == 1) col = mix(vec3(0.85, 0.72, 0.45), vec3(0.95, 0.88, 0.68), fbm(s * 3.0 + vec3(gTime * 0.05), 4));
else if (i == 3)
{
col = mix(vec3(0.72, 0.30, 0.14), vec3(0.45, 0.18, 0.10), smoothstep(0.45, 0.65, fbm(s * 3.5, 5)));
col = mix(col, vec3(0.95), smoothstep(0.88, 0.94, abs(y)));
}
else if (i == 4)
{
float b = sin(y * 16.0 + 2.0 * fbm(s * vec3(2.0, 8.0, 2.0), 4));
col = mix(vec3(0.85, 0.75, 0.60), vec3(0.62, 0.40, 0.25), 0.5 + 0.5 * b);
vec2 spot = vec2(atan(s.z, s.x) - 0.6, y + 0.35);
col = mix(col, vec3(0.75, 0.30, 0.18), smoothstep(1.0, 0.6, length(spot * vec2(3.0, 9.0))));
}
else if (i == 5)
{
float b = sin(y * 22.0 + fbm(s * vec3(2.0, 6.0, 2.0), 3));
col = mix(vec3(0.90, 0.80, 0.58), vec3(0.75, 0.62, 0.40), 0.5 + 0.5 * b);
}
else if (i == 6) col = mix(vec3(0.62, 0.86, 0.90), vec3(0.70, 0.92, 0.95), 0.5 + 0.5 * sin(y * 10.0));
else col = mix(vec3(0.18, 0.32, 0.80), vec3(0.30, 0.48, 0.92), 0.5 + 0.5 * sin(y * 12.0 + fbm(s * 4.0, 3)));
float diff = max(dot(n, L), 0.0);
return col * (0.04 + 1.0 * diff);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord)
{
vec2 uv = (2.0 * fragCoord - iResolution.xy) / min(iResolution.x, iResolution.y);
gTime = iTime * (0.3 + 2.0 * P2);
T_tilt = 0.42 + 1.0 * clamp(P1, 0.0, 1.0);
float amt = 0.5 + 1.5 * P3;
float sizeK = 1.0 + 1.2 * P4;
// inzoomen op de aarde
vec3 ePos = planetPos(2);
float zf = smoothstep(0.0, 1.0, clamp(P5, 0.0, 1.0));
float zoom = mix(1.0, 9.0, zf * zf);
vec2 w = mix(vec2(0.0), ePos.xy, zf) + uv / zoom;
float px = 2.0 / (min(iResolution.x, iResolution.y) * zoom); // pixelgrootte in wereld
float bass = (spec(0.01) + spec(0.03) + spec(0.05)) / 3.0;
// ---- achtergrond ----
vec3 col = vec3(0.0);
float starAmt = 1.0 - clamp(P8, 0.0, 1.0);
if (starAmt > 0.0)
{
vec2 sp = uv * 0.9 + w * 0.1; // sterren bewegen nauwelijks mee met de zoom
float milky = exp(-pow((sp.y - sp.x * 0.35 + 0.2) * 2.2, 2.0)) * fbm(vec3(sp * 3.0, 1.0), 4);
col += vec3(0.10, 0.09, 0.14) * milky * starAmt;
for (int k = 0; k < 2; k++)
{
float sc = k == 0 ? 60.0 : 140.0;
vec2 g = sp * sc, id = floor(g), f = fract(g) - 0.5;
float r = hash21(id + float(k) * 17.0);
if (r > 0.93)
{
vec2 off = vec2(hash21(id + 3.1), hash21(id + 7.7)) - 0.5;
float d = length(f - off * 0.6);
float tw = 0.6 + 0.4 * sin(iTime * (1.0 + r * 4.0) + r * 50.0);
col += smoothstep(0.08, 0.0, d) * tw * (r - 0.93) * 14.0 * starAmt * vec3(0.85, 0.9, 1.0);
}
}
}
// ---- zon ----
float Rs = 0.075 * (1.0 + 0.12 * bass * amt);
float rs = length(w);
float glow = (0.6 + 1.2 * P7) * (0.6 + 0.8 * bass * amt);
col += vec3(1.0, 0.55, 0.15) * exp(-max(rs - Rs, 0.0) * 9.0) * 0.45 * glow;
col += vec3(1.0, 0.75, 0.40) * exp(-max(rs - Rs, 0.0) * 30.0) * 0.6 * glow;
float hitDepth = 1e9;
vec3 hitCol = vec3(0.0);
if (rs < Rs)
{
vec2 q = w / Rs;
float z = sqrt(1.0 - dot(q, q));
float gran = fbm(vec3(q * 6.0, gTime * 0.3), 5);
vec3 sun = mix(vec3(1.0, 0.45, 0.08), vec3(1.0, 0.92, 0.65), pow(z, 0.6));
sun *= 0.85 + 0.35 * gran;
hitCol = sun * (1.2 + 0.4 * bass * amt);
hitDepth = -z * Rs;
}
// ---- planeten ----
float lvl[8];
vec3 pos[8];
float rad[8];
for (int i = 0; i < 8; i++)
{
lvl[i] = spec(BAND[i]);
pos[i] = planetPos(i);
rad[i] = SIZE[i] * sizeK * (1.0 + 0.25 * lvl[i] * amt);
}
for (int i = 0; i < 8; i++)
{
vec2 d = w - pos[i].xy;
float R = rad[i];
float r2 = dot(d, d) / (R * R);
if (r2 < 1.0)
{
vec2 q = d / R;
vec3 n = vec3(q, -sqrt(1.0 - r2));
float depth = pos[i].z + n.z * R;
if (depth < hitDepth)
{
// licht van de zon, met een beetje 'camera-licht' erbij zodat je nooit alleen de nachtkant ziet
vec3 L = normalize(normalize(-pos[i]) + vec3(0.0, 0.0, -0.9));
vec3 c;
if (i == 2)
{
float ca;
c = earthSurface(n, L, ca);
float rim = pow(1.0 + n.z, 3.0);
c += vec3(0.25, 0.55, 1.0) * rim * (0.25 + 0.75 * max(dot(n, L) + 0.3, 0.0));
}
else c = planetSurface(i, n, L);
// edge-antialias
float edge = smoothstep(1.0, 1.0 - 2.5 * px / R, sqrt(r2));
hitCol = mix(col, c, edge);
hitDepth = depth;
}
}
}
// ---- maan ----
{
float ma = gTime * 1.3;
float mr = rad[2] * 2.3;
vec3 mp = ePos + vec3(mr * cos(ma), mr * sin(ma) * sin(T_tilt), mr * sin(ma) * cos(T_tilt));
float R = rad[2] * 0.28;
vec2 d = w - mp.xy;
float r2 = dot(d, d) / (R * R);
if (r2 < 1.0)
{
vec3 n = vec3(d / R, -sqrt(1.0 - r2));
float depth = mp.z + n.z * R;
if (depth < hitDepth)
{
vec3 L = normalize(normalize(-mp) + vec3(0.0, 0.0, -0.9));
vec3 c = mix(vec3(0.42), vec3(0.75), fbm(n * 5.0 + 3.0, 5)) * (0.03 + max(dot(n, L), 0.0));
hitCol = mix(col, c, smoothstep(1.0, 1.0 - 2.5 * px / R, sqrt(r2)));
hitDepth = depth;
}
}
}
// ---- baanlijnen en planeetgloed (onder de objecten als die ervoor zitten) ----
vec3 over = vec3(0.0);
float sT = sin(T_tilt), cT = cos(T_tilt);
vec2 cw = vec2(w.x, w.y / sT);
float lcw = length(cw);
vec2 grad = vec2(cw.x, cw.y / sT) / max(lcw, 1e-4);
float lineDepth = cw.y * cT;
float baseLine = 0.10 + 0.6 * P6;
for (int i = 0; i < 8; i++)
{
float dd = abs(lcw - ORB[i]) / max(length(grad), 1e-4);
float line = smoothstep(1.6 * px, 0.0, dd);
vec3 lc = (i == 2) ? vec3(0.35, 0.65, 1.0) : vec3(0.55, 0.6, 0.8);
float b = baseLine * (i == 2 ? 1.6 : 1.0) + 0.5 * lvl[i] * amt;
if (lineDepth < hitDepth) over += lc * line * b;
// gloed rond de planeet op de maat van zijn toon
float pd = length(w - pos[i].xy) - rad[i];
vec3 gc = (i == 2) ? vec3(0.3, 0.6, 1.0) : vec3(1.0, 0.9, 0.7);
float hal = exp(-max(pd, 0.0) / (rad[i] * 0.6)) * (pd > 0.0 ? 1.0 : 0.0);
over += gc * hal * (0.08 + 0.6 * lvl[i] * amt) * (i == 2 ? 1.5 : 1.0);
}
// ---- planetoïdengordel ----
{
float rot = gTime * 0.04;
float a = atan(cw.y, cw.x) - rot;
vec2 g = vec2(a * 60.0, lcw * 120.0);
vec2 id = floor(g);
float h = hash21(id);
float rc = (id.y + 0.5) / 120.0;
if (h > 0.88 && abs(rc - 0.535) < 0.03)
{
float ac = (id.x + 0.3 + 0.4 * hash21(id + 5.0)) / 60.0 + rot;
vec3 rp = project(rc * vec2(cos(ac), sin(ac)));
float rr = 0.0008 * sizeK * (0.6 + h);
float dot_ = smoothstep(rr + px, rr, length(w - rp.xy));
if (rp.z < hitDepth) over += vec3(0.6, 0.55, 0.5) * dot_ * 0.55;
}
}
// ---- ringen van Saturnus ----
{
vec3 sp = pos[5];
vec2 d = w - sp.xy;
vec2 c = vec2(d.x, d.y / sT);
float rr = length(c) / rad[5];
if (rr > 1.25 && rr < 2.35)
{
float rd = sp.z + c.y * cT;
float band = 0.55 + 0.45 * sin(rr * 40.0) * sin(rr * 13.0);
float gap = smoothstep(0.02, 0.05, abs(rr - 1.95));
float a = 0.75 * band * gap * smoothstep(1.25, 1.32, rr) * smoothstep(2.35, 2.25, rr);
vec3 rc = vec3(0.85, 0.76, 0.58) * (0.5 + 0.6 * band) * (0.8 + 0.6 * lvl[5] * amt);
if (rd < hitDepth) { hitCol = mix(hitDepth < 1e8 ? hitCol : col, rc, a); hitDepth = min(hitDepth, rd); }
}
}
vec3 outc = (hitDepth < 1e8) ? hitCol : col;
outc += over;
outc = 1.0 - exp(-outc * 1.3); // zachte tonemapping
fragColor = vec4(pow(outc, vec3(0.95)), 1.0);
}
Made for MNodes: paste it in a Shader Code node and wire it to a Shader or Scene. The P values are the sliders above the code.
Comments
No comments yet.
Log in or create an account to place a comment.