Spheres and Shadows

Spheres, they're like boxes, but round.

Created by marcogomez on Mon, 07 Jun 2021 23:29:57 GMT.


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

uniform sampler2D prgm5Texture;
uniform vec2 resolution;

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm5 = texture2D(prgm5Texture, uv);
  gl_FragColor = vec4(prgm5.xyz, 1.0);
}

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

uniform float time;
uniform vec2 mouselerp;
uniform vec2 resolution;

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float INFINITY = 1.0 / 0.00001;
const vec3 matSpecularCol = vec3(1.0);

struct Camera { vec3 Position; vec3 Side; vec3 Up; vec3 View; vec2 Scale; };
struct Ray { vec3 Position; vec3 Direction; };
struct Sphere { vec3 Center; float Radius; };
struct Plane { vec3 Point; vec3 Normal; };
struct PointLight { vec3 Position; vec3 DiffuseColor; float DiffusePower; vec3 SpecularColor; float SpecularPower; };
struct Material { vec3 DiffuseColor; vec3 SpecularColor; float Shininess; };
struct Ball { Sphere Sphere; Material Material; float Velocity; };
struct LightBounce { Ray Normal; Material Material; };

const Material floorMaterial = Material(vec3(1.0), matSpecularCol, 130.0);
const Plane floorGeometry = Plane(vec3(0.0), vec3(0.0, 0.0, 1.0));

const int lightsCount = 3;
const int ballsCount = 12;
PointLight pointLights[lightsCount];
Ball balls[ballsCount];

vec2 Circle(const float time) {
  return vec2(cos(time), sin(time));
}

float IntersectSphere(const Ray ray, const Sphere sphere, inout Ray normal) {
  vec3 L = sphere.Center - ray.Position;
  float Tca = max(0.0, dot(L, ray.Direction));
  if (Tca < 0.0) { return INFINITY; }
  float d2 = dot(L, L) - Tca * Tca;
  float p2 = sphere.Radius * sphere.Radius - d2;
  if (p2 < 0.0) { return INFINITY; }
  float t = Tca - sqrt (p2);
  vec3 intersect = ray.Position + t * ray.Direction;
  normal = Ray(intersect, (intersect - sphere.Center) / sphere.Radius);
  return t;
}

float IntersectPlane(const Ray ray, const Plane plane, inout Ray normal) {
  float t = dot(plane.Point - ray.Position, plane.Normal) / dot(ray.Direction, plane.Normal);
  normal = Ray(ray.Position + t * ray.Direction, plane.Normal);
  return t;
}

vec3 Phong(PointLight light, Material material, Ray normal, vec3 eye) {
  vec3 viewDir = normalize(normal.Position - eye);
  vec3 lightVec = light.Position - normal.Position;
  float lightDistance2 = dot(lightVec, lightVec);
  vec3 lightDir = lightVec / sqrt(lightDistance2);
  float diffuse = dot(normal.Direction, lightDir);
  vec3 R = lightDir - 2.0 * diffuse * normal.Direction;
  float specular = pow(max(0.0, dot(R, viewDir)), material.Shininess);
  vec3 color = (
    max(0.0, diffuse) * light.DiffuseColor * light.DiffusePower * material.DiffuseColor +
    max(0.0, specular) * light.SpecularColor * light.SpecularPower * material.SpecularColor
  );
  return color * 110.0 / lightDistance2;
}

bool Scene(const Ray ray, out LightBounce bounce) {
  float tMatch = INFINITY;
  Ray normalMatch;
  for (int i = 0; i < ballsCount; ++i) {
    Ray normal;
    float t = max(0.0, IntersectSphere(ray, balls[i].Sphere, normal));
    if (t > 0.0 && tMatch > t) {
      tMatch = t;
      bounce = LightBounce(normal, balls[i].Material);
    }
  }
  Ray normalPlane;
  float t2 = IntersectPlane(ray, floorGeometry, normalPlane);
  if (t2 > 0.0 && t2 < tMatch) {
    vec3 pt = normalPlane.Position;
    if (length(pt) < 50.0 && (fract(pt.x) < 0.97 == fract(pt.y) < 0.97)) {
      tMatch = t2;
      bounce = LightBounce(normalPlane, floorMaterial);
    }
  }
  return tMatch < 1000.0 && tMatch > 0.0;
}

bool LightScene (inout Ray ray, inout vec3 color) {
  LightBounce bounce;
  if (!Scene (ray, bounce)) { return false; }
  vec3 bouncePos = bounce.Normal.Position + bounce.Normal.Direction * 0.0001;
  LightBounce bounceShadow;
  for (int iLight = 0; iLight < lightsCount; ++iLight) {
    if (!Scene (Ray(bouncePos, normalize(pointLights[iLight].Position - bouncePos)), bounceShadow)) {
      color += Phong(pointLights[iLight], bounce.Material, bounce.Normal, ray.Position);
    }
  }
  ray = Ray(bouncePos, reflect(ray.Direction, bounce.Normal.Direction));
  return true;
}

const Sphere sphere = Sphere(vec3(0.0), 0.85);

mat2 rotate(float a) { return mat2(cos(a), sin(a), -sin(a), cos(a)); }

void hueShift(inout vec3 col, in float adjust) {
  const vec3 k = vec3(0.57735);
  float cosAngle = cos(adjust);
  col = col * cosAngle + cross(k, col) * sin(adjust) + k * dot(k, col) * (1.0 - cosAngle);
}

float ssin(float t) {
  return (2.0 / PI) * atan(sin(TAU * t * 0.5) / 0.1) * 2.0;
}

void main(void) {
  float t = time * 0.75;
  vec3 eye = vec3(Circle(time) * 7.0, 6.5);
  eye.yz += 0.5 + sin(time * 0.25) * 2.0;
  vec3 UP = vec3 (0.0, 0.0, 1.0);
  float upX = sin(time * 0.25) * 0.25;
  UP.x = upX * upX * upX * upX;
  vec3 look = normalize(UP - eye);
  vec3 u = cross(look, UP);
  vec3 v = cross(u, look);
  float FOV = PI / (1.6 + sin(time * 0.125) * 0.2);
  float TANFOV = tan(FOV / 1.8);
  vec3 dx = TANFOV * u;
  vec3 dy = TANFOV * v;
  const float vel = 1.0;
  vec2 position = (gl_FragCoord.xy - resolution.xy * 0.5) / min(resolution.x, resolution.y);
  Ray ray = Ray(eye, normalize(look + dx * position.x + dy * position.y));

  const float angle = TAU / float(ballsCount);
  vec3 ballCol = vec3(1.0, 0.2, 0.2);

  balls[ 0] = Ball(sphere, Material(ballCol, matSpecularCol, 300.0), vel); hueShift(ballCol, angle);
  balls[ 1] = Ball(sphere, Material(ballCol, matSpecularCol, 300.0), vel); hueShift(ballCol, angle);
  balls[ 2] = Ball(sphere, Material(ballCol, matSpecularCol, 300.0), vel); hueShift(ballCol, angle);
  balls[ 3] = Ball(sphere, Material(ballCol, matSpecularCol, 300.0), vel); hueShift(ballCol, angle);
  balls[ 4] = Ball(sphere, Material(ballCol, matSpecularCol, 300.0), vel); hueShift(ballCol, angle);
  balls[ 5] = Ball(sphere, Material(ballCol, matSpecularCol, 300.0), vel); hueShift(ballCol, angle);
  balls[ 6] = Ball(sphere, Material(ballCol, matSpecularCol, 300.0), vel); hueShift(ballCol, angle);
  balls[ 7] = Ball(sphere, Material(ballCol, matSpecularCol, 300.0), vel); hueShift(ballCol, angle);
  balls[ 8] = Ball(sphere, Material(ballCol, matSpecularCol, 300.0), vel); hueShift(ballCol, angle);
  balls[ 9] = Ball(sphere, Material(ballCol, matSpecularCol, 300.0), vel); hueShift(ballCol, angle);
  balls[10] = Ball(sphere, Material(ballCol, matSpecularCol, 300.0), vel); hueShift(ballCol, angle);
  balls[11] = Ball(sphere, Material(ballCol, matSpecularCol, 300.0), vel);

  float ballDist = 4.0;
  for (int i = 0; i < ballsCount; ++i) {
    float ballBounce = fract(t * balls[i].Velocity + sin(float(i) / float(ballsCount) - 0.25)) - 0.5;
    balls[i].Sphere.Center = vec3(
      cos(float(-i) * angle) * ballDist,
      sin(float(-i) * angle) * ballDist,
      balls[i].Sphere.Radius + 8.0 * (0.25 - ballBounce * ballBounce)
    );
  }
  const float lightAngle = TAU / 3.0;
  float lightDist = 0.25 + max(0.0, ssin(time * 0.05) * 4.0);
  float lightHeight = 6.0;
  vec2 wAmount = vec2(0.2);
  float cAmount = 0.6;
  vec3 lightColor = vec3(cAmount, wAmount);
  vec3 r = lightColor.xyz; vec3 g = lightColor.yxz; vec3 b = lightColor.yzx;
  vec3 pl1Pos = vec3(vec2(lightDist), lightHeight);
  vec3 pl2Pos = vec3(vec2(lightDist), lightHeight);
  vec3 pl3Pos = vec3(vec2(lightDist), lightHeight);
  pl1Pos.xy *= vec2(cos(0.0 * lightAngle), sin(0.0 * lightAngle));
  pl2Pos.xy *= vec2(cos(1.0 * lightAngle), sin(1.0 * lightAngle));
  pl3Pos.xy *= vec2(cos(2.0 * lightAngle), sin(2.0 * lightAngle));
  pl1Pos.xy *= rotate(-time * 2.0);
  pl2Pos.xy *= rotate(-time * 2.0);
  pl3Pos.xy *= rotate(-time * 2.0);
  pointLights[0] = PointLight(pl1Pos, r, 0.3, r, 0.5);
  pointLights[1] = PointLight(pl2Pos, g, 0.3, g, 0.5);
  pointLights[2] = PointLight(pl3Pos, b, 0.3, b, 0.5);
  vec3 tone = vec3(0.02, 0.03, 0.07);
  for (int i = 0; i < 2; i++) { LightScene(ray, tone); } // increase to 3 on a strong PC
  gl_FragColor = vec4(tone, 1.0);
}

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

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

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float W = 1.2;
const float T = 7.5;
const float amount = 1.0;
const float reinhardAmount = 0.9;
const float brightness = 1.2;
const float saturation = 0.6;
const float contrast = 1.0;
const vec2 vignetteSize = vec2(0.25, 0.25);
const float vignetteRoundness = 0.12;
const float vignetteMix = 1.2;
const float vignetteSmoothness = 0.42;

float filmicReinhardCurve(float x) {
  float q = (T * T + 1.0) * x * x;
  return q / (q + x + T * T);
}

vec3 filmicReinhard(vec3 c) {
  float w = filmicReinhardCurve(W);
  return vec3(
    filmicReinhardCurve(c.r),
    filmicReinhardCurve(c.g),
    filmicReinhardCurve(c.b)
  ) / w;
}

vec3 ContrastSaturationBrightness(vec3 color, float brt, float sat, float con) {
  const float AvgLumR = 0.5;
  const float AvgLumG = 0.5;
  const float AvgLumB = 0.5;
  const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721);
  vec3 AvgLumin = vec3(AvgLumR, AvgLumG, AvgLumB);
  vec3 brtColor = color * brt;
  vec3 intensity = vec3(dot(brtColor, LumCoeff));
  vec3 satColor = mix(intensity, brtColor, sat);
  vec3 conColor = mix(AvgLumin, satColor, con);
  return conColor;
}

float sdSquare(vec2 point, float width) {
  vec2 d = abs(point) - width;
  return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}

float vignette(vec2 uv, vec2 size, float roundness, float smoothness) {
  uv -= 0.5;
  float minWidth = min(size.x, size.y);
  uv.x = sign(uv.x) * clamp(abs(uv.x) - abs(minWidth - size.x), 0.0, 1.0);
  uv.y = sign(uv.y) * clamp(abs(uv.y) - abs(minWidth - size.y), 0.0, 1.0);
  float boxSize = minWidth * (1.0 - roundness);
  float dist = sdSquare(uv, boxSize) - (minWidth * roundness);
  return 1.0 - smoothstep(0.0, smoothness, dist);
}

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 seed = dot(uv, vec2(12.9898, 78.233));
  float noise = fract(sin(seed) * 43758.5453123 + t);
  noise = gaussian(noise, 0.0, 0.5);
  return vec3(noise);
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm1 = texture2D(prgm1Texture, uv);
  vec3 reinhard = filmicReinhard(prgm1.rgb);
  vec3 color = prgm1.rgb;
  color = mix(prgm1.rgb, reinhard, reinhardAmount);
  color = ContrastSaturationBrightness(color, brightness, saturation, contrast);
  float v = vignette(uv, vignetteSize, vignetteRoundness, vignetteSmoothness);
  vec3 vig = color * v;
  vec3 g = gaussgrain(time) * 0.03;
  color = mix(color, vig, vignetteMix);
  color = mix(prgm1.rgb, color, amount) - g;
  color = clamp(color, 0.0, 1.0);
  gl_FragColor = vec4(color, 1.0);
}

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

uniform sampler2D eTexture0; // https://i.imgur.com/N9yUvGf.png
uniform sampler2D prgm2Texture;
uniform vec2 resolution;
uniform float time;

vec4 rgbShift(vec2 p , vec4 shift) {
  shift *= 2.0 * shift.w - 1.0;
  vec2 rs = vec2(shift.x, -shift.y);
  vec2 gs = vec2(shift.y, -shift.z);
  vec2 bs = vec2(shift.z, -shift.x);
  float r = texture2D(prgm2Texture, p + rs, 0.0).x;
  float g = texture2D(prgm2Texture, p + gs, 0.0).y;
  float b = texture2D(prgm2Texture, p + bs, 0.0).z;
  return vec4(r,g,b,1.0);
}

vec4 noise(vec2 uv) {
  return texture2D(eTexture0, uv, 0.0);
}

vec4 vec4pow(vec4 v, float p) {
  return vec4(
    pow(v.x, p),
    pow(v.y, p),
    pow(v.z, p),
    v.w
  );
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec2 mo = uv * 2.0 - 1.0;
  mo *= 0.01;
  vec3 chromaticAberration;
  chromaticAberration.r = texture2D(prgm2Texture, uv - mo * 0.05, 0.0).r;
  chromaticAberration.g = texture2D(prgm2Texture, uv - mo * 0.15, 0.0).g;
  chromaticAberration.b = texture2D(prgm2Texture, uv - mo * 0.25, 0.0).b;
  vec4 color = vec4(vec3(0.0), 1.0);
  color.xyz = mix(color.xyz, chromaticAberration, 0.3);
  const float speed = 0.01;
  const float amplitude = 0.01;
  vec4 shift = vec4pow(
    noise(
      vec2(speed * time, speed * time / 25.0 )
    ), 8.0
  ) * vec4(vec3(amplitude), 1.0);
  color += rgbShift(uv, shift);
  gl_FragColor = color;
}

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

uniform sampler2D prgm3Texture;
uniform sampler2D prgm4Texture;
uniform vec2 resolution;
uniform float time;

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

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm3 = texture2D(prgm3Texture, uv);
  vec4 prgm4 = texture2D(prgm4Texture, uv);
  gl_FragColor = prgm3 * 0.3 + prgm4 * 0.7;
  gl_FragColor += smoothstep(prgm4, prgm4 + 0.01, vec4(0.8, 0.2, 0.5, 1.0)) * 0.03;
}

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

uniform sampler2D prgm4Texture;
uniform vec2 resolution;

void main() {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 color = texture2D(prgm4Texture, uv);
  float pb = 0.4;
  vec4 lcdColor = vec4(pb, pb, pb, 1.0);
  int px = int(mod(gl_FragCoord.x, 3.0));
  if (px == 1) lcdColor.r = 1.0;
  else if (px == 2) lcdColor.g = 1.0;
  else lcdColor.b = 1.0;
  float sclV = 0.25;
  if (int(mod(gl_FragCoord.y, 3.0)) == 0) lcdColor.rgb = vec3(sclV, sclV, sclV);
  gl_FragColor = mix(color, color * lcdColor, 1.0);
}