The Code Therapy

Blue Marble

A rudimentary ray marcher rendering a Sphere SDF with a naive offset sampling accumulation anti-aliasing pass to create a marble with a living pattern disturbed by Perlin noise

Created by marcogomez on Fri, 22 Oct 2021 21:31:38 GMT.


// ╔═════════════╦════════════════╗
// ║ Marco Gomez ║ https://mgz.me ║
// ╚═════════════╩════════════════╝
precision highp float;

uniform sampler2D prgm1Texture;
uniform sampler2D prgm2Texture;
uniform vec2 resolution;
uniform float time;

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float SQRTAU = sqrt(TAU);

float rand(vec2 x, float t) {
  x = fract(x * vec2(5.3987, 5.4421));
  x += dot(x.yx, x.xy + vec2(21.5351, 14.3137));
  float xy = x.x * x.y;
  float r = fract(xy * 95.4307) + fract(xy * 75.04961 + t) - 1.0;
  return r * 0.5 + 0.5;
}

float gaussian(float z, float u, float o) {
  float go = 1.0 / (o * SQRTAU);
  return go * exp(-(((z - u) * (z - u)) / (2.0 * (o * o))));
}

vec3 gaussgrain(float t) {
  vec2 ps = vec2(1.0) / resolution.xy;
  vec2 uv = gl_FragCoord.xy * ps;
  float noise = rand(uv, t);
  noise = gaussian(noise, 0.0, 0.5);
  return vec3(noise);
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 texA = texture2D(prgm1Texture, uv);
  vec4 texB = texture2D(prgm2Texture, uv);
  vec3 g = gaussgrain(time);
  vec3 col = mix(texA.xyz, texB.xyz, 0.65);
  col += g * 0.07;
  gl_FragColor = vec4(col, 1.0);
}

// ╔═════════════╦════════════════╗
// ║ Marco Gomez ║ https://mgz.me ║
// ╚═════════════╩════════════════╝
precision highp float;

#define renderNaiveAA
#define aaSamples 2

uniform sampler2D cubemapTexture0PX; // https://i.imgur.com/02rSpF7.png
uniform sampler2D cubemapTexture0NX; // https://i.imgur.com/ittohSy.png
uniform sampler2D cubemapTexture0PY; // https://i.imgur.com/z2W7Em5.png
uniform sampler2D cubemapTexture0NY; // https://i.imgur.com/tjIG7sh.png
uniform sampler2D cubemapTexture0PZ; // https://i.imgur.com/0LLlzKz.png
uniform sampler2D cubemapTexture0NZ; // https://i.imgur.com/KD1glJm.png
uniform samplerCube cubemap0;
uniform vec2 resolution;
uniform vec2 mouselerp;
uniform vec2 mouse;
uniform float time;
uniform float fft;

const float PI = acos(-1.0);
const float TAU = PI * 2.0;

float acc = 0.0;

vec2 fade(vec2 t) {
  return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
}

vec4 permute(vec4 x) {
  return mod(((x * 34.0) + 1.0) * x, 289.0);
}

float perlin(vec2 uv) {
  vec4 pi = floor(uv.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
  pi = mod(pi, 289.0);
  vec4 pf = fract(uv.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
  vec4 ix = pi.xzxz, iy = pi.yyww, fx = pf.xzxz, fy = pf.yyww;
  vec4 i = permute(permute(ix) + iy);
  vec4 gx = 2.0 * fract(i * 0.0243902439) - 1.0;
  vec4 gy = abs(gx) - 0.5;
  vec4 tx = floor(gx + 0.5);
  gx = gx - tx;
  vec2 g00 = vec2(gx.x, gy.x);
  vec2 g01 = vec2(gx.z, gy.z);
  vec2 g10 = vec2(gx.y, gy.y);
  vec2 g11 = vec2(gx.w, gy.w);
  vec4 n = 1.79284291400159 - 0.85373472095314 * vec4(
    dot(g00, g00), dot(g01, g01),
    dot(g10, g10), dot(g11, g11)
  );
  g00 *= n.x; g01 *= n.y; g10 *= n.z; g11 *= n.w;
  float n00 = dot(g00, vec2(fx.x, fy.x));
  float n01 = dot(g01, vec2(fx.z, fy.z));
  float n10 = dot(g10, vec2(fx.y, fy.y));
  float n11 = dot(g11, vec2(fx.w, fy.w));
  vec2 fxy = fade(pf.xy);
  vec2 nx = mix(vec2(n00, n01), vec2(n10, n11), fxy.x);
  float nxy = mix(nx.x, nx.y, fxy.y);
  return nxy;
}

vec2 cmul(vec2 a, vec2 b) {
  return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x );
}

vec2 csqr(vec2 a)  {
  return vec2(a.x * a.x - a.y * a.y, 2.0 * a.x * a.y);
}

mat3 rotate(mat3 mat, vec3 theta) {
  float sx = sin(theta.x), sy = sin(theta.y), sz = sin(theta.z);
  float cx = cos(theta.x), cy = cos(theta.y), cz = cos(theta.z);
  return (
    mat *
    mat3(1.0, 0.0, 0.0, 0.0,  cx, -sin(theta.x), 0.0, sx,   cx) *
    mat3( cy, 0.0,  sy, 0.0, 1.0,           0.0, -sy, 0.0,  cy) *
    mat3( cz, -sz, 0.0,  sz,  cz,           0.0, 0.0, 0.0, 1.0)
  );
}

vec2 rotate(vec2 p, vec2 c, float theta) {
  float co = cos(theta), si = sin(theta);
  return (p - c) * mat2(co, -si, si, co);
}


vec2 sphere(vec3 ro, vec3 rd, vec4 sph) {
  vec3 oc = ro - sph.xyz;
  float b = dot(oc, rd);
  float c = dot(oc, oc) - sph.w * sph.w;
  float h = b * b - c;
  if (h < 0.0) { return vec2(-1.0); }
  h = sqrt(h);
  return vec2(-b - h, -b + h);
}

float map(vec3 p) {
  float res = 0.0;
  vec3 c = p;
  float pn = perlin(2.0 * -mouselerp.x + p.zy + time * 0.125) * 0.95;
  for (int i = 0; i < 12; ++i) {
    p = abs(pn * 0.5 + 2.5) * abs(p) / (dot(p, p)) - 0.75;
    p.xy = rotate(p.xy, vec2(0.0), float(i) * PI * 0.25);
    p += pn;
    p.yz = mix(cmul(p.yz, p.zx), csqr(p.yz), 0.25);
    p = p.zxy;
    res += exp(-25.0 * abs(dot(p, c))) * (0.35 + fft * 0.75);
  }
  return res / 2.0;
}

vec3 raymarch(vec3 ro, vec3 rd, vec2 tminmax) {
  float t = tminmax.x;
  float dt = 0.1;
  vec3 col = vec3(0.0);
  float c = 0.0;
  for (int i = 0; i < 30; i++) {
    t += dt * exp(-5.0 * c);
    if (t > tminmax.y) { break; }
    vec3 pos = ro + t * rd;
    c = map(ro + t * rd);
    col = 0.99 * col + 0.5 * vec3(c * c * c, c * c, c);
  }
  return col;
}

mat3 calcLookAtMatrix(vec3 origin, vec3 target, float roll) {
  vec3 rr = vec3(sin(roll), cos(roll), 0.0);
  vec3 ww = normalize(target - origin);
  vec3 uu = normalize(cross(ww, rr));
  vec3 vv = normalize(cross(uu, ww));
  return mat3(uu, vv, ww);
}

vec3 render(vec3 ro, vec3 rd) {
  vec2 minMax = sphere(ro, rd, vec4(0.0, 0.0, 0.0, 2.5));
  vec3 col = raymarch(ro, rd, minMax);
  if (minMax.x < 0.0) {
    col = textureCube(cubemap0, rd).xyz * 2.0;
  } else {
    vec3 nor = (ro + minMax.x * rd) / 2.0;
    nor = reflect(rd, nor);
    float fre = pow(0.5 + clamp(dot(nor, rd), 0.0, 1.0), 3.0) * 0.8;
    vec3 freN = fre * 1.25 + mix(fre * col, nor, 0.5) * 2.0;
    vec3 cubeMap = textureCube(cubemap0, nor).xyz;
    vec3 norPlusFres = mix(cubeMap, (cubeMap * cubeMap) + normalize(freN) * 0.6, 0.65);
    col += norPlusFres * 0.75;
    col = mix(col, col * norPlusFres, 0.42);
  }
  col = 0.5 * (log(1.0 + col));
  col = clamp(col, 0.0, 1.0);
  return col;
}

vec3 naiveAA(vec3 ro, vec3 rd) {
  const int samples = aaSamples;
  vec3 c = vec3(0.0);
  vec2 o = vec2(6.0, 0.0);
  o = rotate(o, vec2(0.0), PI * 0.125);
  for (int i = 0; i < samples; i++) {
    c += render(ro + o.x / resolution.x, rd) / float(samples);
    o = rotate(o, vec2(0.0), TAU / float(samples));
  }
  return c;
}

void main(void) {
  vec2 uv = (gl_FragCoord.xy - resolution.xy * 0.5) / resolution.y;
  if (length(mouse) > 2.0) { acc += time * 0.25; }
  float camRadius = 4.0;
  float fov = 0.5;
  float roXRot = sin(mouselerp.x * PI) * camRadius;
  float roZRot = cos(mouselerp.x * PI) * camRadius;
  vec3 ro = vec3(roXRot, mouselerp.y * PI, roZRot);
  ro.xz = rotate(ro.xz, vec2(0.0), acc);
  vec3 camTarget = vec3(0.0, 0.25, 0.0);
  float camRoll = 0.0;
  mat3 camMatrix = calcLookAtMatrix(ro, camTarget, camRoll);
  vec3 rd = normalize(camMatrix * vec3(uv.x, uv.y, fov));
  #ifndef renderNaiveAA
    vec3 col = render(ro, rd);
  #else
    vec3 col = naiveAA(ro, rd);
  #endif
  gl_FragColor = vec4(col, 1.0);
}

// ╔═════════════╦════════════════╗
// ║ Marco Gomez ║ https://mgz.me ║
// ╚═════════════╩════════════════╝
precision highp float;

uniform sampler2D prgm1Texture;
uniform vec2 resolution;
uniform float time;

#define inputTexture prgm1Texture

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float hardscan = -8.0;
const float hardPix = -2.0;
const float maskDark = 0.5;
const float maskLight = 1.5;

float toLinear(float c) {
  return (c <= 0.04045) ? c / 12.92 : pow(abs((c + 0.055) / 1.055), 2.4);
}

vec3 toLinear(vec3 c) {
  return vec3(toLinear(c.r), toLinear(c.g), toLinear(c.b));
}

float toSRGB(float c) {
  return(c < 0.0031308 ? c * 12.92 : 1.055 * pow(abs(c), 0.41666) - 0.055);
}

vec3 toSRGB(vec3 c) {
  return vec3(toSRGB(c.r), toSRGB(c.g), toSRGB(c.b));
}

vec3 fetch(vec2 pos, vec2 off, vec2 res) {
  pos = floor(pos * res + off) / res;
  if (max(abs(pos.x - 0.5), abs(pos.y - 0.5)) > 0.5) {
    return vec3(0.0);
  }
  return toLinear(texture2D(inputTexture, pos.xy, -16.0).xyz);
}

vec2 dist(vec2 pos, vec2 res) {
  pos = pos * res;
  return -((pos - floor(pos)) - vec2(0.5));
}

float gauss(float pos, float scale) {
  return exp2(scale * pos * pos);
}

vec3 horz3(vec2 pos, float off, vec2 res) {
  vec3 b = fetch(pos, vec2(-1.0, off), res);
  vec3 c = fetch(pos, vec2(+0.0, off), res);
  vec3 d = fetch(pos, vec2(+1.0, off), res);
  float dst = dist(pos, res).x;
  float scale = hardPix;
  float wb = gauss(dst - 1.0, scale);
  float wc = gauss(dst + 0.0, scale);
  float wd = gauss(dst + 1.0, scale);
  return (b * wb + c * wc + d * wd) / (wb + wc + wd);
}

vec3 horz5(vec2 pos, float off, vec2 res) {
  vec3 a = fetch(pos, vec2(-2.0, off), res);
  vec3 b = fetch(pos, vec2(-1.0, off), res);
  vec3 c = fetch(pos, vec2(+0.0, off), res);
  vec3 d = fetch(pos, vec2(+1.0, off), res);
  vec3 e = fetch(pos, vec2(+2.0, off), res);
  float dst = dist(pos, res).x;
  float scale = hardPix;
  float wa = gauss(dst - 2.0, scale);
  float wb = gauss(dst - 1.0, scale);
  float wc = gauss(dst + 0.0, scale);
  float wd = gauss(dst + 1.0, scale);
  float we = gauss(dst + 2.0, scale);
  return (a * wa + b * wb + c * wc + d * wd + e * we) / (wa + wb + wc + wd + we);
}

float scan(vec2 pos, float off, vec2 res) {
  float dst = dist(pos, res).y;
  return gauss(dst + off, hardscan);
}

vec3 tri(vec2 pos, vec2 res) {
  vec3 a = horz3(pos, -1.0, res);
  vec3 b = horz5(pos, +0.0, res);
  vec3 c = horz3(pos, +1.0, res);
  float wa = scan(pos, -1.0, res);
  float wb = scan(pos, +0.0, res);
  float wc = scan(pos, +1.0, res);
  return a * wa + b * wb + c * wc;
}

vec3 mask(vec2 pos) {
  pos.x += pos.y * 3.0;
  vec3 m = vec3(maskDark, maskDark, maskDark);
  pos.x = fract(pos.x / 6.0);
  if (pos.x < 0.333) {
    m.r = maskLight;
  } else if (pos.x < 0.666) {
    m.g = maskLight;
  } else {
    m.b = maskLight;
  }
  return m;
}

float bar(float pos, float bar) {
  pos -= bar;
  return pos * pos < 4.0 ? 0.0 : 1.0;
}

float rand(vec2 uv, float t) {
  float seed = dot(uv, vec2(12.9898, 78.233));
  return fract(sin(seed) * 43758.5453123 + t);
}

float gaussian(float z, float u, float o) {
  return (
    (1.0 / (o * sqrt(TAU))) *
    (exp(-(((z - u) * (z - u)) / (2.0 * (o * o)))))
  );
}

vec3 gaussgrain(float t) {
  vec2 ps = vec2(1.0) / resolution.xy;
  vec2 uv = gl_FragCoord.xy * ps;
  float noise = rand(uv, t);
  noise = gaussian(noise, 0.0, 0.5);
  return vec3(noise);
}

void drawVig(inout vec3 color, vec2 uv) {
  float vignette = uv.x * uv.y * (1.0 - uv.x) * (1.0 - uv.y);
  vignette = clamp(pow(abs(16.0 * vignette), 0.1), 0.0, 1.0);
  color = mix(color, color * vignette, 0.7);
}

void main(void) {
  vec2 res = vec2(1024.0, 768.0);
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  float vig = (0.0 + 1.0 * 21.0 * uv.x * uv.y * (1.0 - uv.x) * (1.0 - uv.y));
  float v = exp(-0.01 * length(uv)) * vig;
  float frameScale = 29.97;
  float frameTime = floor(time * frameScale) / frameScale;
  vec3 grainA = gaussgrain(frameTime * 1.0);
  vec3 grainB = gaussgrain(frameTime * 1.3);
  vec3 g = grainA * grainB * 0.12;
  float s = clamp(0.35 + 0.35 * sin(3.0 * time + uv.y * res.y * 3.0), 0.0, 1.0);
  float scanLines = pow(s, 1.33);
  vec4 color = vec4(tri(uv, res) * mask(gl_FragCoord.xy), 1.0);
  color.xyz = toSRGB(color.xyz * 2.0);
  color = mix(color, color * v, 0.7);
  drawVig(color.xyz, uv);
  color = mix(color, color - s, 0.125);
  color.xyz -= g;
  gl_FragColor = color;
}