ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
frac_shader02_dmd6i_spectrum16.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file frac_shader02_dmd6i_spectrum16.frag
5 * @brief Vulkan fragment shader: frac shader02 dmd6i spectrum16.
6 */
7layout(set = 0, binding = 1, std140) uniform SpriteExtended {
8 vec4 mouse;
9 vec4 u0;
10 vec4 u1;
11 vec4 u2;
12 vec4 u3;
13 vec4 custom_uniforms[16];
14 vec4 audio_bands;
15 vec4 audio_history;
17#define amp_high ext.audio_bands.z
18#define amp_low ext.audio_bands.x
19#define amp_mid ext.audio_bands.y
20#define amp_peak ext.u2.w
21#define amp_rms ext.u3.z
22#define amp_smooth ext.u3.w
23#define iMouse ext.mouse
24#define iResolution ext.u0.zw
25#define spectrum_history_head int(ext.audio_history.x)
26#define spectrum_history_size int(ext.audio_history.y)
27#define time_f ext.u2.y
28
29layout(location = 0) out vec4 color;
30layout(location = 0) in vec2 tc;
31layout(set = 0, binding = 0) uniform sampler2D samp;
32
33
34
35layout(set = 0, binding = 3) uniform sampler1D spectrum; // live FFT (always bound when audio is enabled)
36
37// Audio variables
38
39
40
41
42
43
44
45// 16 Temporal Buffers
46layout(set = 0, binding = 3) uniform sampler1D spectrum0; // T=0 (Now)
47layout(set = 0, binding = 4) uniform sampler1DArray spectrum_history;
48
49
50#ifndef SPECTRUM_HISTORY_LAYER
51#define SPECTRUM_HISTORY_LAYER(index) ((spectrum_history_head - ((index) % max(spectrum_history_size, 1)) + max(spectrum_history_size, 1)) % max(spectrum_history_size, 1))
52#endif
53
54const float PI = 3.14159265358979323846;
55
56// Helper to sample the 16-stack
57/**
58 * @brief Evaluates sample16 for this shader effect.
59 * @param index Input value for the calculation.
60 * @param freq Input value for the calculation.
61 * @return Computed float value.
62 */
63float sample16(int index, float freq) {
64 switch(index) {
65 case 0: return texture(spectrum0, freq).r;
66 case 1: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(1)))).r;
67 case 2: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(2)))).r;
68 case 3: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(3)))).r;
69 case 4: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(4)))).r;
70 case 5: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(5)))).r;
71 case 6: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(6)))).r;
72 case 7: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(7)))).r;
73 case 8: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(8)))).r;
74 case 9: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(9)))).r;
75 case 10: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(10)))).r;
76 case 11: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(11)))).r;
77 case 12: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(12)))).r;
78 case 13: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(13)))).r;
79 case 14: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(14)))).r;
80 default: return texture(spectrum_history, vec2(freq, float(SPECTRUM_HISTORY_LAYER(15)))).r;
81 }
82}
83
84/**
85 * @brief Evaluates pingPong for this shader effect.
86 * @param x Input value for the calculation.
87 * @param length Input value for the calculation.
88 * @return Computed float value.
89 */
90float pingPong(float x, float length) {
91 float m = mod(x, length * 2.0);
92 return m <= length ? m : length * 2.0 - m;
93}
94
95/**
96 * @brief Evaluates rotateUV for this shader effect.
97 * @param uv Input value for the calculation.
98 * @param angle Input value for the calculation.
99 * @param c Input value for the calculation.
100 * @param aspect Input value for the calculation.
101 * @return Computed vec2 value.
102 */
103vec2 rotateUV(vec2 uv, float angle, vec2 c, float aspect) {
104 float s = sin(angle), cc = cos(angle);
105 vec2 p = uv - c;
106 p.x *= aspect;
107 p = mat2(cc, -s, s, cc) * p;
108 p.x /= aspect;
109 return p + c;
110}
111
112/**
113 * @brief Evaluates reflectUV for this shader effect.
114 * @param uv Input value for the calculation.
115 * @param segments Input value for the calculation.
116 * @param c Input value for the calculation.
117 * @param aspect Input value for the calculation.
118 * @return Computed vec2 value.
119 */
120vec2 reflectUV(vec2 uv, float segments, vec2 c, float aspect) {
121 vec2 p = uv - c;
122 p.x *= aspect;
123 float ang = atan(p.y, p.x);
124 float rad = length(p);
125 float stepA = 6.2831853 / segments;
126 ang = mod(ang, stepA);
127 ang = abs(ang - stepA * 0.5);
128 vec2 r = vec2(cos(ang), sin(ang)) * rad;
129 r.x /= aspect;
130 return r + c;
131}
132
133// FRACTAL FOLD: Each iteration uses a different history buffer.
134// `histPhase` is the smooth, monotonically advancing audio-driven offset
135// (continuous across frames; never wraps), so the fold motion never snaps.
136/**
137 * @brief Evaluates fractalFold for this shader effect.
138 * @param uv Input value for the calculation.
139 * @param zoom Input value for the calculation.
140 * @param t Input value for the calculation.
141 * @param histPhase Input value for the calculation.
142 * @param c Input value for the calculation.
143 * @param aspect Input value for the calculation.
144 * @return Computed vec2 value.
145 */
146vec2 fractalFold(vec2 uv, float zoom, float t, float histPhase, vec2 c, float aspect) {
147 vec2 p = uv;
148 for (int i = 0; i < 6; i++) {
149 // Use history buffers 0-5 for the 6 iterations
150 float fft = sample16(i, 0.5);
151 // Phase is histPhase + per-iteration offset; *no* (zoom + fft*..) sin-of-time
152 // multiplier (that caused phase jumps when fft changed). fft now only
153 // adds a small continuous bias to the per-iteration phase.
154 float phase = histPhase * (0.35 + 0.05 * float(i)) + float(i) * 0.7;
155 float z = zoom + 0.15 * sin(phase) + fft * 0.10;
156 p = abs((p - c) * z) - 0.5 + c;
157 p = rotateUV(p, histPhase * 0.12 + float(i) * 0.07 + fft * 0.05, c, aspect);
158 }
159 return p;
160}
161
162/**
163 * @brief Evaluates neonPalette for this shader effect.
164 * @param t Input value for the calculation.
165 * @return Computed vec3 value.
166 */
167vec3 neonPalette(float t) {
168 vec3 pink = vec3(1.0, 0.15, 0.75);
169 vec3 blue = vec3(0.10, 0.55, 1.0);
170 vec3 green = vec3(0.10, 1.00, 0.45);
171 float ph = fract(t * 0.08);
172 vec3 k1 = mix(pink, blue, smoothstep(0.00, 0.33, ph));
173 vec3 k2 = mix(blue, green, smoothstep(0.33, 0.66, ph));
174 vec3 k3 = mix(green, pink, smoothstep(0.66, 1.00, ph));
175 return normalize(mix(mix(k1, k2, step(0.33, ph)), k3, step(0.66, ph))) * 1.05;
176}
177
178/**
179 * @brief Evaluates hash1 for this shader effect.
180 * @param n Input value for the calculation.
181 * @return Computed float value.
182 */
183float hash1(float n) {
184 return fract(sin(n * 127.1 + 311.7) * 43758.5453123);
185}
186
187/**
188 * @brief Evaluates diamondFold for this shader effect.
189 * @param uv Input value for the calculation.
190 * @param c Input value for the calculation.
191 * @param aspect Input value for the calculation.
192 * @return Computed vec2 value.
193 */
194vec2 diamondFold(vec2 uv, vec2 c, float aspect) {
195 vec2 p = (uv - c) * vec2(aspect, 1.0);
196 p = abs(p);
197 if (p.y > p.x) p = p.yx;
198 p.x /= aspect;
199 return p + c;
200}
201
202// Map unbounded warped coordinates to [0,1] continuously (no periodic wrap).
203// This avoids the visible "jump back" from fract() when the zoom crosses an
204// integer boundary in texture space.
205/**
206 * @brief Evaluates continuousUv for this shader effect.
207 * @param pwrap Input value for the calculation.
208 * @param ar Input value for the calculation.
209 * @param center Input value for the calculation.
210 * @return Computed vec2 value.
211 */
212vec2 continuousUv(vec2 pwrap, vec2 ar, vec2 center) {
213 vec2 n = pwrap / ar;
214 vec2 bounded = n / (1.0 + abs(n)); // (-inf, inf) -> (-1, 1)
215 vec2 uv = center + 0.5 * bounded;
216 return clamp(uv, vec2(0.001), vec2(0.999));
217}
218
219/**
220 * @brief Executes the fragment shader entry point.
221 */
222void main(void) {
223 vec4 baseTex = texture(samp, tc);
224 vec2 uv = tc * 2.0 - 1.0;
225 float aspect = iResolution.x / iResolution.y;
226 vec2 ar = vec2(aspect, 1.0);
227 vec2 m = (iMouse.z > 0.5) ? (iMouse.xy / iResolution) : vec2(0.5);
228
229 // 1. Radial Audio Sampling (Temporal Echo)
230 float distFromCenter = length((tc - m) * ar);
231 float historySelect = clamp(distFromCenter * 15.0, 0.0, 15.0);
232 int hIdx = int(historySelect);
233 float hFrac = fract(historySelect);
234
235 // Smoothly blend between 16 buffers based on radius
236 float fftCurrent = sample16(hIdx, clamp(distFromCenter * 0.5, 0.0, 1.0));
237 float fftNext = sample16(min(hIdx + 1, 15), clamp(distFromCenter * 0.5, 0.0, 1.0));
238 float fft = mix(fftCurrent, fftNext, hFrac);
239
240 // Live FFT fallback channel (for cases where history buffers are not
241 // configured/populated even though the shader declares spectrum0..15).
242 float liveFft = texture(spectrum, clamp(distFromCenter * 0.5, 0.0, 1.0)).r;
243
244 // ------------------------------------------------------------------
245 // SMOOTH AUDIO-DRIVEN PHASE.
246 // We sum a broadband sample from every shell to get total energy stored
247 // across the 16-frame ring buffer. This sum is continuous frame-to-frame
248 // (each shell shifts down one slot per frame), so multiplying it into a
249 // motion phase produces fluid drift that never snaps when audio changes.
250 // It plays the role of an integrator without needing shader state.
251 float spectrumSum = 0.0;
252 for (int i = 0; i < 16; i++) {
253 spectrumSum += sample16(i, 0.25) + sample16(i, 0.75);
254 }
255 spectrumSum *= 0.03125; // 1/(16*2)
256
257 // Detect whether history is actually evolving. If all spectrumN are nearly
258 // identical/flat, use live FFT + amp bands to avoid fixed time-loop motion.
259 float histDelta = 0.0;
260 for (int i = 0; i < 15; i++) {
261 histDelta += abs(sample16(i, 0.25) - sample16(i + 1, 0.25));
262 }
263 histDelta *= (1.0 / 15.0);
264 float historyValid = smoothstep(0.002, 0.02, histDelta + spectrumSum * 0.2);
265
266 // historyEnergy = energy of the LOCAL shell only (drives this pixel)
267 float histEnergy = 0.0;
268 histEnergy += sample16(hIdx, 0.10);
269 histEnergy += sample16(hIdx, 0.40);
270 histEnergy += sample16(hIdx, 0.80);
271 histEnergy *= 0.333;
272
273 // Blend local history with live FFT so movement remains audio-driven even
274 // when history buffers are unavailable or only partially configured.
275 float audioEnergy = mix(liveFft, histEnergy, historyValid);
276 audioEnergy = max(audioEnergy, 0.35 * (amp_low + amp_mid + amp_high));
277
278 // CRITICAL: phase MUST be a function of time_f *alone* so it accumulates
279 // monotonically. Audio is only allowed to ADD a bounded offset (size O(1)),
280 // never to SCALE time_f - that would multiply Ξ”rate by all of t and
281 // produce huge phase jumps after the first minute.
282 float basePhase = time_f * 0.6;
283 float audioOffset = mix(liveFft, spectrumSum, historyValid) * 1.2; // bounded
284 float audioPhase = basePhase + audioOffset; // ALWAYS continuous
285
286 // Monotonic evolving hash driver: non-repeating evolution over time.
287 float evo = audioPhase * (0.22 + audioEnergy * 0.6) + spectrumSum * 3.7;
288 float evoIdx = floor(evo);
289 float evoFrac = fract(evo);
290 float evoHash = mix(hash1(evoIdx + 5.0), hash1(evoIdx + 6.0), evoFrac);
291 // ------------------------------------------------------------------
292
293 // 2. Modified Kaleidoscope Reflection.
294 // Use the smooth audio phase instead of raw time_f, and use historyEnergy
295 // (continuous, per-shell) instead of the radius-blended fft so segments
296 // don't visibly snap when fft changes per frame.
297 float seg = 4.0 + (2.0 + audioEnergy * 4.0) * (0.35 + 0.65 * evoHash);
298 vec2 kUV = reflectUV(tc, seg, m, aspect);
299 kUV = diamondFold(kUV, m, aspect);
300
301 // 3. Fractal Folding driven by smooth audioPhase (no time_f-vs-fft jumps).
302 float foldZoom = 1.25 + 0.9 * audioEnergy + 0.35 * (evoHash - 0.5);
303 kUV = fractalFold(kUV, foldZoom, time_f, audioPhase, m, aspect);
304 kUV = rotateUV(kUV, audioPhase * (0.18 + audioEnergy * 0.45), m, aspect);
305 kUV = diamondFold(kUV, m, aspect);
306
307 // 4. Continuous Audio Drift Warp (non-periodic, no orbital wrap).
308 vec2 p = (kUV - m) * ar;
309 float rD = max(length(p), 1e-6);
310
311 // Monotonic drift from audio/history. This is linear in time and audio
312 // energy; no angle wrap terms (atan/cos/sin orbit) so no fixed cycle point.
313 float drift = audioPhase * (0.22 + audioEnergy * 0.65 + spectrumSum * 0.15);
314
315 // Long-evolving pseudo-random bias from drift index, blended continuously.
316 float dIdx = floor(drift * 0.07);
317 float dFrac = fract(drift * 0.07);
318 vec2 dA = vec2(hash1(dIdx + 19.0), hash1(dIdx + 43.0)) - 0.5;
319 vec2 dB = vec2(hash1(dIdx + 20.0), hash1(dIdx + 44.0)) - 0.5;
320 vec2 driftBias = mix(dA, dB, dFrac) * (0.35 + audioEnergy * 0.45);
321
322 // Non-periodic radial terms (log/sqrt/polynomial), audio-reactive.
323 float radialGain = 1.2 + audioEnergy * 3.0 + spectrumSum * 1.1;
324 float logTerm = log(1.0 + rD * radialGain);
325 float sqrtTerm = sqrt(rD) * (0.35 + audioEnergy * 0.4);
326 float polyTerm = rD * rD * (0.03 + 0.08 * evoHash);
327
328 // Affine + drift warp: no circular phase reset because there is no periodic
329 // angular parameter in the main transport path.
330 vec2 pwrap;
331 pwrap.x = p.x * (1.0 + 0.55 * audioEnergy) + logTerm + drift * 0.11 + driftBias.x;
332 pwrap.y = p.y * (1.0 + 0.40 * audioEnergy) + sqrtTerm + drift * 0.073 + polyTerm + driftBias.y;
333
334 // Chromatic Split using the temporal shift
335 float chromaShift = (0.0015 + audioEnergy * 0.005) * mix(0.5, 2.0, amp_high);
336
337 vec2 off = normalize(pwrap + 1e-6) * chromaShift;
338 vec2 u0 = continuousUv(pwrap, ar, m);
339
340 vec3 finalRGB;
341 finalRGB.r = texture(samp, u0 + off).r;
342 finalRGB.g = texture(samp, u0).g;
343 finalRGB.b = texture(samp, u0 - off).b;
344 // ------------------------------------------------------------------------
345
346 // 5. Final Glow & Vignette
347 float pulse = 0.5 + 0.5 * clamp(
348 log(1.0 + abs(drift) * 0.08 + rD * (5.0 + audioEnergy * 6.0)) * 0.33
349 + evoHash * 0.55,
350 0.0,
351 1.0
352 );
353 finalRGB *= (0.85 + 0.15 * pulse);
354
355 float vign = 1.0 - smoothstep(0.5, 1.5, distFromCenter);
356 finalRGB *= (vign + audioEnergy * 0.3);
357
358 // Audio-driven Final Color Grading
359 finalRGB *= 1.0 + audioEnergy * 0.8;
360 finalRGB = mix(finalRGB, finalRGB.zyx, audioEnergy * 0.4);
361
362 color = vec4(clamp(finalRGB, 0.0, 1.0), baseTex.a);
363}
layout(set=0, binding=1, std140) uniform SpriteExtended
Definition 3high.frag:7
#define iMouse
Definition 3high.frag:17
ext
Definition 3high.frag:16
void main(void)
Executes the fragment shader entry point.
Definition 3high.frag:47
#define iResolution
Definition 3high.frag:18
#define time_f
Definition 3high.frag:19
#define amp_mid
#define amp_low
#define amp_high
#define SPECTRUM_HISTORY_LAYER(index)
float pingPong(float x, float length)
Evaluates pingPong for this shader effect.
Definition af_scale.frag:32
vec2 fractalFold(vec2 p, float zoom, float t, int i)
Evaluates fractalFold for this shader effect.
float histEnergy(float f)
Evaluates histEnergy for this shader effect.
vec2 diamondFold(vec2 p)
Evaluates diamondFold for this shader effect.
vec2 rotateUV(vec2 uv, float angle, vec2 c, float aspect)
Evaluates rotateUV for this shader effect.
Definition aura2.frag:59
vec3 neonPalette(float t)
Evaluates neonPalette for this shader effect.
Definition aura2.frag:112
vec2 reflectUV(vec2 uv, float segments, vec2 c, float aspect)
Evaluates reflectUV for this shader effect.
Definition aura2.frag:76
float n(vec2 p)
Evaluates n for this shader effect.
#define PI
float sample16(int index, float freq)
Evaluates sample16 for this shader effect.
vec2 continuousUv(vec2 pwrap, vec2 ar, vec2 center)
Evaluates continuousUv for this shader effect.
float hash1(float n)
Evaluates hash1 for this shader effect.
vec3 spectrum(float t)
Evaluates spectrum for this shader effect.