{
  "license": "CC0-1.0",
  "shader": {
    "id": "3q9m0six",
    "title": "Coiling Fibration (optimized)",
    "license": "CC0-1.0",
    "provenance_type": "bred",
    "made_with": "glslop",
    "status": "active",
    "compile_ok": true,
    "render_hash": "00003e3f7e190000",
    "tags": [
      "mathy"
    ],
    "parents": [
      "5cj5jdtf"
    ],
    "author": "7tvg46yh8qfu",
    "author_name": "hawkkn",
    "author_url": "/u/hawkkn",
    "created_at": "2026-12-05T05:44:11.394Z",
    "poster_url": "/posters/3q9m0six.avif",
    "stats": {
      "slops": 0,
      "views": 19,
      "forks": 0
    },
    "self": "/api/v1/shaders/3q9m0six",
    "isf": "/*{\n  \"DESCRIPTION\": \"Hopf fibration: coiled fibers of S3 stereographically projected to R3, with orange, red and blue comet lights spiralling along them. Speeds are integrated in a persistent clock buffer, so knobs never make anything jump.\",\n  \"CREDIT\": \"Hopf Fibration VJ\",\n  \"CATEGORIES\": [\"Generator\", \"Geometry\", \"VJ\"],\n  \"ISFVSN\": \"2\",\n  \"INPUTS\": [\n    { \"NAME\": \"size\",        \"TYPE\": \"float\", \"DEFAULT\": 1.0,  \"MIN\": 0.4,    \"MAX\": 2.0 },\n    { \"NAME\": \"amount\",      \"TYPE\": \"float\", \"DEFAULT\": 30.0, \"MIN\": 4.0,    \"MAX\": 36.0 },\n    { \"NAME\": \"warp\",        \"TYPE\": \"float\", \"DEFAULT\": 1.0,  \"MIN\": 0.0,    \"MAX\": 2.0 },\n    { \"NAME\": \"warpSpeed\",   \"TYPE\": \"float\", \"DEFAULT\": 1.0,  \"MIN\": 0.0,    \"MAX\": 4.0 },\n    { \"NAME\": \"cometSpeed\",  \"TYPE\": \"float\", \"DEFAULT\": 1.0,  \"MIN\": 0.0,    \"MAX\": 4.0 },\n    { \"NAME\": \"hue\",         \"TYPE\": \"float\", \"DEFAULT\": 0.0,  \"MIN\": -180.0, \"MAX\": 180.0 },\n    { \"NAME\": \"glow\",        \"TYPE\": \"float\", \"DEFAULT\": 1.0,  \"MIN\": 0.0,    \"MAX\": 1.0 },\n    { \"NAME\": \"zoom\",        \"TYPE\": \"float\", \"DEFAULT\": 1.0,  \"MIN\": 0.4,    \"MAX\": 2.5 },\n    { \"NAME\": \"orbitSpeed\",  \"TYPE\": \"float\", \"DEFAULT\": 1.0,  \"MIN\": 0.0,    \"MAX\": 4.0 },\n    { \"NAME\": \"cameraYaw\",   \"TYPE\": \"float\", \"DEFAULT\": 0.0,  \"MIN\": -3.1416,\"MAX\": 3.1416 },\n    { \"NAME\": \"cameraPitch\", \"TYPE\": \"float\", \"DEFAULT\": 0.0,  \"MIN\": -1.1,   \"MAX\": 1.1 },\n    { \"NAME\": \"quality\",     \"TYPE\": \"float\", \"DEFAULT\": 0.4,  \"MIN\": 0.3,    \"MAX\": 1.0 }\n  ],\n  \"PASSES\": [\n    { \"TARGET\": \"clockBuf\", \"PERSISTENT\": true, \"FLOAT\": true, \"FILTER\": \"NEAREST\" },\n    { \"TARGET\": \"fiberBuf\", \"FLOAT\": true, \"FILTER\": \"NEAREST\" },\n    { \"TARGET\": \"sceneBuf\", \"FLOAT\": true },\n    { }\n  ]\n}*/\n\n// Pass 0  clockBuf  texels (0..2,0): comet, warp and orbit clocks, each stored as\n//                   (256 s blocks, quarter-seconds, remainder) so 16-bit floats stay precise for hours.\n// Pass 1  fiberBuf  texels (0..35, 0..3): one column per fiber; column 48 = camera target + fiber count.\n// Pass 2  sceneBuf  HDR raymarch of the coiled fibers into the bottom-left `quality` fraction of the buffer.\n// Pass 3  screen    upscale, bloom, hue, tone map, vignette, grain.\n\n#define NF 36\n#define COILS 26.0\n#define TAU 6.2831853\n\nfloat hsh(float i) { return fract(sin(i * 127.1 + 11.7) * 43758.5453); }\nfloat rnd(vec2 p) { return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); }\n\nvec3 pal(float c) {                                  // 0 orange, 1 red, 2 blue\n  return c < 0.5 ? vec3(1.0, 0.40, 0.05) : (c < 1.5 ? vec3(1.0, 0.06, 0.09) : vec3(0.08, 0.36, 1.0));\n}\nfloat ringN(float r) { return r < 0.5 ? 6.0 : (r < 1.5 ? 9.0 : (r < 2.5 ? 10.0 : 11.0)); }\nfloat ringA(float r) { return r < 0.5 ? 0.38 : (r < 1.5 ? 0.85 : (r < 2.5 ? 1.32 : 1.8)); }\nfloat ringC(float r) { return r < 1.5 ? 2.0 : (r < 2.5 ? 1.0 : 0.0); }\nfloat ringCount(float r) { return max(1.0, floor(ringN(r) * amount / 36.0 + 0.5)); }\n\nvec4 clockAdvance(vec4 s, float d) {\n  float lo = s.z + d, q = floor(lo * 4.0);\n  lo -= q * 0.25;\n  float mid = s.y + q, hb = floor(mid / 1024.0);\n  return vec4(s.x + hb, mid - hb * 1024.0, lo, 0.0);\n}\nfloat clockValue(vec4 s) { return s.x * 256.0 + s.y * 0.25 + s.z; }\n\n// Fiber idx as a circle in R3, through three points of its S3 great circle after projection.\nbool fiber(float idx, float WT, out vec4 C, out vec4 N, out vec4 U, out vec4 K) {\n  float start = 0.0, ri = -1.0, j = 0.0, cnt = 1.0;\n  for (int r = 0; r < 4; r++) {\n    float fr = float(r), c = ringCount(fr);\n    if (ri < 0.0 && idx < start + c) { ri = fr; j = idx - start; cnt = c; }\n    start += c;\n  }\n  C = vec4(1e4, 0.0, 0.0, 1.0); N = vec4(0.0, 0.0, 1.0, 0.0); U = vec4(1.0, 0.0, 0.0, 0.0); K = vec4(0.0);\n  if (ri < 0.0) return false;\n\n  float r1 = warp * (0.55 * sin(WT * 0.11) + 0.15 * sin(WT * 0.037)) + 0.12 * warp;\n  float r2 = WT * 0.09;\n  float a = ringA(ri) + 0.06 * warp * sin(WT * 0.07) * (ri - 1.5);\n  float b = j / cnt * TAU + ri * 0.5 + WT * 0.035 * (mod(ri, 2.0) > 0.5 ? 1.0 : -1.0);\n  float ca = cos(a * 0.5), sa = sin(a * 0.5);\n\n  vec3 P0 = vec3(0.0), P1 = vec3(0.0), P2 = vec3(0.0);\n  for (int k = 0; k < 3; k++) {                       // one inlined copy instead of three\n    float t = float(k) * 2.0944;\n    float x1 = ca * cos(t), x2 = ca * sin(t), x3 = sa * cos(t + b), x4 = sa * sin(t + b);\n    float y1 = x1 * cos(r1) - x4 * sin(r1), y4 = x1 * sin(r1) + x4 * cos(r1);  // x1-x4 rotation: the warp\n    float y2 = x2 * cos(r2) - x3 * sin(r2), y3 = x2 * sin(r2) + x3 * cos(r2);\n    vec3 P = vec3(y1, y3, y2) / max(1.0 - y4, 1e-3);\n    if (k == 0) P0 = P; else if (k == 1) P1 = P; else P2 = P;\n  }\n  vec3 p = P0 - P2, q = P1 - P2, n = cross(p, q);\n  float nn = max(dot(n, n), 1e-9);\n  vec3 cen = P2 + cross(q * dot(p, p) - p * dot(q, q), n) / (2.0 * nn);\n  float rad = length(P0 - cen);\n  float w = rad > 25.0 ? 0.0 : min(1.0, rad / 1.6) / (1.0 + 0.1 * rad);\n\n  float rc = ringC(ri);\n  C = vec4(cen * size, rad * size);\n  N = vec4(normalize(n), w);\n  U = vec4(normalize(P0 - cen), mod(rc + 1.0 + mod(j + ri, 2.0), 3.0) + hsh(idx));\n  K = vec4(pal(rc) * 0.55, (0.07 + 0.06 * hsh(idx + 40.0)) * (mod(idx, 3.0) > 0.5 ? 1.0 : -1.0));\n  return true;\n}\n\nvec3 aces(vec3 x) { return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0); }\nvec3 hueRot(vec3 c, float a) {\n  vec3 k = vec3(0.57735);\n  float ca = cos(a);\n  return c * ca + cross(k, c) * sin(a) + k * dot(k, c) * (1.0 - ca);\n}\n\nvoid main() {\n  vec2 fc = gl_FragCoord.xy;\n\n  // ---------- pass 0: clocks (3 texels) ----------\n  if (PASSINDEX == 0) {\n    if (fc.y > 1.0 || fc.x > 3.0) { gl_FragColor = vec4(0.0); return; }\n    float d = clamp(TIMEDELTA, 0.0, 0.1);\n    float sp = fc.x < 1.0 ? cometSpeed : (fc.x < 2.0 ? warpSpeed : orbitSpeed);\n    gl_FragColor = clockAdvance(IMG_PIXEL(clockBuf, fc), d * sp);\n    return;\n  }\n\n  vec4 ck0 = IMG_PIXEL(clockBuf, vec2(0.5, 0.5));\n  vec4 ck1 = IMG_PIXEL(clockBuf, vec2(1.5, 0.5));\n  vec4 ck2 = IMG_PIXEL(clockBuf, vec2(2.5, 0.5));\n  float CT = clockValue(ck0), WT = clockValue(ck1), OT = clockValue(ck2);\n\n  // ---------- pass 1: fiber table (49 x 4 texels) ----------\n  if (PASSINDEX == 1) {\n    if (fc.y > 4.0 || fc.x > 49.0) { gl_FragColor = vec4(0.0); return; }\n    float col = floor(fc.x), row = floor(fc.y);\n    vec4 res = vec4(0.0), C, N, U, K;\n    vec3 s = vec3(0.0);\n    float ws = 0.0, total = 0.0;\n    for (int i = 0; i < NF; i++) {\n      float fi = float(i);\n      bool ok = fiber(fi, WT, C, N, U, K);\n      if (ok) total += 1.0;\n      if (ok && N.w > 0.0 && C.w < 6.0 * size) { s += C.xyz * N.w; ws += N.w; }\n      if (fi == col) res = row < 0.5 ? C : (row < 1.5 ? N : (row < 2.5 ? U : K));\n      if (col < 47.5 && fi >= col) break;       // fiber columns stop early; column 48 sums all\n    }\n    gl_FragColor = col > 47.5 ? vec4(ws > 0.0 ? s / ws : vec3(0.0), total) : res;\n    return;\n  }\n\n  // ---------- pass 2: raymarch at `quality` resolution ----------\n  if (PASSINDEX == 2) {\n    vec2 rs = floor(RENDERSIZE * quality);\n    if (fc.x > rs.x || fc.y > rs.y) { gl_FragColor = vec4(0.0); return; }\n\n    vec4 tg = IMG_PIXEL(fiberBuf, vec2(48.5, 0.5));\n    vec3 tgt = tg.xyz;\n    float cnt = tg.w;\n\n    float yaw = OT * 0.06 + cameraYaw, pit = 0.32 + 0.22 * sin(OT * 0.045) + cameraPitch;\n    vec3 ro = 7.2 * zoom * vec3(cos(pit) * sin(yaw), sin(pit), cos(pit) * cos(yaw));\n    vec3 f = normalize(-ro);\n    float roll = 0.08 * sin(OT * 0.13);\n    vec3 r = normalize(cross(f, vec3(sin(roll), cos(roll), 0.0))), u = cross(r, f);\n    vec2 p = (fc * 2.0 - rs) / rs.y;\n    vec3 rd = normalize(mat3(r, u, f) * vec3(p, 1.9));\n\n    // per-fiber ray interval: bounding sphere intersected with the slab around the circle's plane\n    vec2 iv[NF];\n    float tS = 1e9, tE = -1e9;\n    for (int i = 0; i < NF; i++) {\n      float fx = float(i) + 0.5;\n      vec4 Ci = IMG_PIXEL(fiberBuf, vec2(fx, 0.5));\n      vec4 Ni = IMG_PIXEL(fiberBuf, vec2(fx, 1.5));\n      vec3 oc = ro - (Ci.xyz - tgt);\n      float Rb = Ci.w + 0.95, bb = dot(oc, rd), h = bb * bb - dot(oc, oc) + Rb * Rb;\n      vec2 seg = vec2(1e9, -1e9);\n      if (float(i) < cnt && Ni.w > 0.0 && h > 0.0) {\n        h = sqrt(h);\n        seg = vec2(-bb - h, -bb + h);\n        float dn = dot(rd, Ni.xyz), on = dot(oc, Ni.xyz);\n        if (abs(dn) > 1e-4) {\n          float ta = (-0.95 - on) / dn, tb = (0.95 - on) / dn;\n          seg = vec2(max(seg.x, min(ta, tb)), min(seg.y, max(ta, tb)));\n        } else if (abs(on) > 0.95) seg = vec2(1e9, -1e9);\n        seg.x = max(seg.x, 0.0);\n        if (seg.x > seg.y) seg = vec2(1e9, -1e9);\n      }\n      iv[i] = seg;\n      tS = min(tS, seg.x); tE = max(tE, seg.y);\n    }\n\n    float gk = mix(0.3, 1.0, glow), coil = 0.038 * size, minStep = mix(0.009, 0.013, glow);\n    vec3 col = vec3(0.0);\n    float t = tS + rnd(fc + fract(TIME) * 17.0) * 0.06;\n    for (int s = 0; s < 140; s++) {\n      if (t > tE) break;\n      vec3 q0 = ro + rd * t, acc = vec3(0.0);\n      float md = 1e9, nextE = 1e9;\n      bool hit = false;\n      for (int i = 0; i < NF; i++) {\n        vec2 seg = iv[i];\n        if (t < seg.x) { nextE = min(nextE, seg.x); continue; }\n        if (t > seg.y) continue;\n        hit = true;\n        float fx = float(i) + 0.5;\n        vec4 Ci = IMG_PIXEL(fiberBuf, vec2(fx, 0.5));\n        vec4 Ni = IMG_PIXEL(fiberBuf, vec2(fx, 1.5));\n        vec3 q = q0 - (Ci.xyz - tgt), n = Ni.xyz;\n        float hh = dot(q, n);\n        vec3 rr = q - hh * n;\n        float e = length(rr) - Ci.w;\n        float tb2 = hh * hh + e * e, rc = clamp(0.04 * Ci.w, 0.25 * coil, 1.4 * coil);\n        if (tb2 > 0.8) { float q1 = max(sqrt(tb2) - rc, 0.0); md = min(md, q1 * q1); continue; }\n        vec4 Ui = IMG_PIXEL(fiberBuf, vec2(fx, 2.5));\n        vec4 Ki = IMG_PIXEL(fiberBuf, vec2(fx, 3.5));\n        vec3 ua = Ui.xyz, va = cross(n, ua);\n        float ang = atan(dot(rr, va), dot(rr, ua));\n        float th = ang * (Ki.w > 0.0 ? COILS : -COILS) + CT * 0.9 + fract(Ui.w) * TAU;\n        vec2 cd = vec2(e, hh) - rc * vec2(cos(th), sin(th));\n        float d2 = dot(cd, cd);\n        md = min(md, d2);\n        float w = Ni.w;\n        acc += Ki.rgb * w * (0.00034 * gk / (d2 + 0.0004 * gk));                        // coiled line\n        float x = fract(ang * 0.31831 + CT * Ki.w + fract(Ui.w));                         // two comets per fiber\n        float com = x < 0.045 ? smoothstep(0.0, 0.045, x) : exp(-(x - 0.045) * 6.5);     // soft front, long tail\n        float hc = mod(floor(Ui.w) + fract(Ui.w) * 0.6 + TIME * 0.015, 3.0);\n        vec3 cc = mix(pal(floor(hc)), pal(mod(floor(hc) + 1.0, 3.0)), smoothstep(0.0, 1.0, fract(hc)));\n        acc += cc * w * com * (0.0028 * gk / (d2 + 0.0004 * gk));\n        acc += vec3(1.0, 0.92, 0.82) * w * com * com * com * (0.00035 / (d2 + 0.00015)); // hot core\n      }\n      if (!hit) {                                     // empty space: jump to the next fiber's bound\n        if (nextE > 1e8) break;\n        t = nextE + 0.002;\n        continue;\n      }\n      float dt = clamp(sqrt(md) * 0.45, minStep, 0.32);\n      col += acc * dt;\n      t += min(dt, max(nextE - t, minStep));\n    }\n    gl_FragColor = vec4(min(col, vec3(60000.0)), 1.0);\n    return;\n  }\n\n  // ---------- pass 3: composite ----------\n  vec2 uv = isf_FragNormCoord.xy, dc = uv - 0.5;\n  vec2 rs = floor(RENDERSIZE * quality), sc = rs / RENDERSIZE, px = 1.0 / RENDERSIZE;\n  vec2 lo = px * 0.5, hi = sc - px * 1.5;\n  vec2 su = uv * sc;\n  float ca = 0.006;\n  vec3 c = vec3(IMG_NORM_PIXEL(sceneBuf, clamp(su - dc * ca * sc, lo, hi)).r,\n                IMG_NORM_PIXEL(sceneBuf, clamp(su, lo, hi)).g,\n                IMG_NORM_PIXEL(sceneBuf, clamp(su + dc * ca * sc, lo, hi)).b);\n\n  vec2 asp = vec2(RENDERSIZE.y / RENDERSIZE.x, 1.0) * sc;\n  float rot = rnd(fc) * TAU;\n  vec3 bs = vec3(0.0), bl = vec3(0.0);\n  float wsum = 0.0;\n  for (int k = 0; k < 16; k++) {                        // two-radius spiral bloom, rotated per pixel\n    float fk = float(k) + 0.5, rr = sqrt(fk / 16.0), th = fk * 2.39996 + rot;\n    vec2 o = vec2(cos(th), sin(th)) * rr * asp;\n    float wk = exp(-rr * rr * 2.5);\n    bs += IMG_NORM_PIXEL(sceneBuf, clamp(su + o * 0.018, lo, hi)).rgb * wk;\n    bl += IMG_NORM_PIXEL(sceneBuf, clamp(su + o * 0.07, lo, hi)).rgb * wk;\n    wsum += wk;\n  }\n  vec3 bloom = (bs * 1.1 + bl * 1.4) / wsum;\n\n  c = hueRot(max(c + bloom * 0.14 * glow * glow, 0.0), hue * 0.0174533);\n  c = aces(c * 0.9);\n  c *= 1.0 - smoothstep(0.35, 1.05, length(dc * vec2(1.15, 1.0)));\n  c = max(c - 0.002, 0.0) * 1.002;                                  // true black floor\n  c = pow(c, vec3(1.0 / 2.2));\n  c *= 1.0 + (rnd(fc + TIME * 61.0) - 0.5) * 0.08;                  // grain on lit pixels only\n  gl_FragColor = vec4(clamp(c, 0.0, 1.0), 1.0);\n}"
  }
}