|
| 1 | +import { ShaderVariant } from './index'; |
| 2 | + |
| 3 | +// Concept #3: True multipass feedback trails (portal / disk) |
| 4 | +// Implements a dissipative advection–diffusion update in a feedback buffer. |
| 5 | +export const feedbackPortal: ShaderVariant = { |
| 6 | + id: 'feedback-portal', |
| 7 | + name: 'Feedback Portal', |
| 8 | + description: |
| 9 | + 'True feedback trails in a soft central portal; mouse injects faint dye into a slow vortex.', |
| 10 | + mathStory: |
| 11 | + 'We evolve a dye field in a disk using a dissipative advection–diffusion step: the previous frame is back-traced along a gentle spiral (a stable focus) and lightly blurred to mimic diffusion, then exponentially faded. A small continuous source plus mouse injection perturbs the flow; because the buffer feeds itself (ping-pong), paths leave real temporal trails instead of redrawing “fake” streaks.', |
| 12 | + pipeline: { |
| 13 | + // Keep insertion order: feedback first (offscreen), image second (screen). |
| 14 | + sources: { |
| 15 | + feedback: ` |
| 16 | +precision mediump float; |
| 17 | +
|
| 18 | +uniform float iTime; |
| 19 | +uniform vec2 iResolution; |
| 20 | +uniform vec2 iMouse; |
| 21 | +uniform sampler2D iChannel0; |
| 22 | +
|
| 23 | +vec2 rot90(vec2 p) { return vec2(-p.y, p.x); } |
| 24 | +
|
| 25 | +float hash12(vec2 p) { |
| 26 | + return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123); |
| 27 | +} |
| 28 | +
|
| 29 | +float portalMask(vec2 p, float radius) { |
| 30 | + float r = length(p); |
| 31 | + // 1.0 inside, 0.0 outside with a soft edge |
| 32 | + return smoothstep(radius, radius - 0.02, r); |
| 33 | +} |
| 34 | +
|
| 35 | +void main() { |
| 36 | + vec2 frag = gl_FragCoord.xy; |
| 37 | + vec2 uv = frag / iResolution.xy; |
| 38 | +
|
| 39 | + float minRes = min(iResolution.x, iResolution.y); |
| 40 | + vec2 p = (frag - 0.5 * iResolution.xy) / minRes; |
| 41 | +
|
| 42 | + const float portalR = 0.58; |
| 43 | + float portal = portalMask(p, portalR); |
| 44 | + float r = length(p); |
| 45 | +
|
| 46 | + // --- Velocity field (in "p" units per frame) --- |
| 47 | + // A stable spiral (damped rotation) + small time wobble. |
| 48 | + vec2 v = vec2(0.0); |
| 49 | + float swirl = (0.010 * exp(-2.0 * r * r) + 0.002); |
| 50 | + v += rot90(p) * swirl; |
| 51 | + v += -p * 0.006; |
| 52 | + v += 0.0016 * vec2( |
| 53 | + sin(0.35 * iTime + 6.0 * p.y), |
| 54 | + cos(0.30 * iTime + 6.0 * p.x) |
| 55 | + ); |
| 56 | +
|
| 57 | + // Mouse perturbation (very subtle): local swirl + drift. |
| 58 | + vec2 mP = (iMouse - 0.5 * iResolution.xy) / minRes; |
| 59 | + vec2 dm = p - mP; |
| 60 | + float md2 = dot(dm, dm); |
| 61 | + float mInflu = exp(-md2 / 0.010); |
| 62 | + v += rot90(dm) * (0.0030 * mInflu); |
| 63 | + v += dm * (0.0015 * mInflu); |
| 64 | +
|
| 65 | + v *= portal; |
| 66 | +
|
| 67 | + // Convert displacement in p-space to UV-space. |
| 68 | + vec2 toUv = vec2(minRes / iResolution.x, minRes / iResolution.y); |
| 69 | + vec2 uvBack = uv - v * toUv; |
| 70 | +
|
| 71 | + // --- Advection + diffusion (5 taps) --- |
| 72 | + vec2 px = 1.0 / iResolution.xy; |
| 73 | + vec4 c0 = texture2D(iChannel0, uvBack); |
| 74 | + vec4 c1 = texture2D(iChannel0, uvBack + vec2(px.x, 0.0)); |
| 75 | + vec4 c2 = texture2D(iChannel0, uvBack - vec2(px.x, 0.0)); |
| 76 | + vec4 c3 = texture2D(iChannel0, uvBack + vec2(0.0, px.y)); |
| 77 | + vec4 c4 = texture2D(iChannel0, uvBack - vec2(0.0, px.y)); |
| 78 | + vec4 state = c0 * 0.60 + (c1 + c2 + c3 + c4) * 0.10; |
| 79 | +
|
| 80 | + // Exponential fade (stronger outside the portal). |
| 81 | + float fade = mix(0.88, 0.988, portal); |
| 82 | + state *= fade; |
| 83 | + state *= portal; |
| 84 | +
|
| 85 | + // --- Continuous faint source to keep motion visible even without mouse --- |
| 86 | + vec2 emitter = 0.22 * vec2(cos(0.12 * iTime), sin(0.10 * iTime)); |
| 87 | + float eInk = exp(-dot(p - emitter, p - emitter) / (0.030 * 0.030)); |
| 88 | + vec3 eCol = vec3(0.15, 0.3, 0.4); |
| 89 | + state.rgb += eCol * (0.04 * eInk) * portal; |
| 90 | + state.a += (0.05 * eInk) * portal; |
| 91 | +
|
| 92 | + // Mouse dye injection (visible and persistent). |
| 93 | + float mInk = exp(-md2 / (0.018 * 0.018)); |
| 94 | + vec3 mCol = vec3(0.2, 0.35, 0.5); |
| 95 | + state.rgb += mCol * (0.1 * mInk) * portal; |
| 96 | + state.a += (0.12 * mInk) * portal; |
| 97 | +
|
| 98 | + // Visible rim to sell the "portal" boundary. |
| 99 | + float rim = exp(-pow((r - portalR) / 0.018, 2.0)); |
| 100 | + state.rgb += vec3(0.1, 0.15, 0.2) * (0.02 * rim); |
| 101 | + state.a += 0.03 * rim; |
| 102 | +
|
| 103 | + // Tiny dither prevents banding in slow fades. |
| 104 | + float d = (hash12(frag + fract(iTime)) - 0.5) * 0.002; |
| 105 | + state.rgb += d; |
| 106 | +
|
| 107 | + gl_FragColor = clamp(state, 0.0, 4.0); |
| 108 | +} |
| 109 | +`, |
| 110 | + |
| 111 | + image: ` |
| 112 | +precision mediump float; |
| 113 | +
|
| 114 | +uniform float iTime; |
| 115 | +uniform vec2 iResolution; |
| 116 | +uniform vec2 iMouse; |
| 117 | +uniform sampler2D iChannel0; |
| 118 | +
|
| 119 | +float hash12(vec2 p) { |
| 120 | + return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123); |
| 121 | +} |
| 122 | +
|
| 123 | +float portalMask(vec2 p, float radius) { |
| 124 | + float r = length(p); |
| 125 | + return smoothstep(radius, radius - 0.02, r); |
| 126 | +} |
| 127 | +
|
| 128 | +vec3 toneMap(vec3 x) { |
| 129 | + // Soft filmic-ish curve using an exponential rolloff. |
| 130 | + x = 1.0 - exp(-2.2 * x); |
| 131 | + return pow(clamp(x, 0.0, 1.0), vec3(0.4545)); |
| 132 | +} |
| 133 | +
|
| 134 | +void main() { |
| 135 | + vec2 frag = gl_FragCoord.xy; |
| 136 | + vec2 uv = frag / iResolution.xy; |
| 137 | +
|
| 138 | + float minRes = min(iResolution.x, iResolution.y); |
| 139 | + vec2 p = (frag - 0.5 * iResolution.xy) / minRes; |
| 140 | + float r = length(p); |
| 141 | +
|
| 142 | + const float portalR = 0.58; |
| 143 | + float portal = portalMask(p, portalR); |
| 144 | + float rim = exp(-pow((r - portalR) / 0.022, 2.0)); |
| 145 | +
|
| 146 | + vec4 state = texture2D(iChannel0, uv); |
| 147 | + vec3 dye = state.rgb; |
| 148 | + float dens = state.a; |
| 149 | +
|
| 150 | + // Subtle chroma drift based on density and time. |
| 151 | + float h = 0.07 * iTime + 2.0 * dens; |
| 152 | + vec3 pal = 0.55 + 0.45 * cos(6.28318 * (h + vec3(0.0, 0.33, 0.67))); |
| 153 | + pal = mix(vec3(dot(pal, vec3(0.299, 0.587, 0.114))), pal, 0.65); |
| 154 | +
|
| 155 | + vec3 glow = dye * pal * 2.5; |
| 156 | + glow += (dens * dens) * vec3(0.2, 0.3, 0.4); |
| 157 | + glow = toneMap(glow); |
| 158 | +
|
| 159 | + // Background (dark but not too dark). |
| 160 | + vec3 bg = vec3(0.02, 0.03, 0.045); |
| 161 | + bg += 0.025 * vec3(0.0, 0.04, 0.08) * smoothstep(0.8, 0.0, r); |
| 162 | +
|
| 163 | + // Compose: keep the effect mostly inside the portal. |
| 164 | + vec3 col = bg; |
| 165 | + col = mix(col, bg + glow, portal); |
| 166 | +
|
| 167 | + // Rim highlight and a very soft vignette. |
| 168 | + col += rim * vec3(0.12, 0.18, 0.25) * 0.5; |
| 169 | + col *= 1.0 - 0.2 * r * r; |
| 170 | +
|
| 171 | + // Mouse proximity brightens the rim. |
| 172 | + vec2 mP = (iMouse - 0.5 * iResolution.xy) / minRes; |
| 173 | + float md = length(p - mP); |
| 174 | + float mRim = exp(-md * 8.0) * rim; |
| 175 | + col += mRim * vec3(0.15, 0.2, 0.25) * 0.3; |
| 176 | +
|
| 177 | + // Dither. |
| 178 | + col += (hash12(frag + iTime) - 0.5) * 0.003; |
| 179 | +
|
| 180 | + gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0); |
| 181 | +} |
| 182 | +`, |
| 183 | + }, |
| 184 | + channels: { |
| 185 | + // feedback reads its own previous frame (true feedback loop) |
| 186 | + feedback: { iChannel0: 'feedback' }, |
| 187 | + // final image reads from feedback buffer |
| 188 | + image: { iChannel0: 'feedback' }, |
| 189 | + }, |
| 190 | + }, |
| 191 | +}; |
0 commit comments