ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
trail-cache-blur.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file trail-cache-blur.frag
5 * @brief Vulkan fragment shader: trail-cache-blur.
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_smooth ext.u3.w
22#define history_head int(ext.u3.x)
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
29// An audio-reactive, dynamically scaling glitch trail.
30#define EFFECT_ID 24
31layout(location = 0) in vec2 tc;
32layout(location = 0) out vec4 color;
33layout(set = 0, binding = 0) uniform sampler2D samp;
34layout(set = 0, binding = 2) uniform sampler2DArray history;
35
36
37#ifndef SIZE
38#define SIZE 8
39#endif
40
41#ifndef CACHE_HISTORY_LAYER
42#define CACHE_HISTORY_LAYER(index) ((history_head + (index)) % SIZE)
43#endif
44layout(set = 0, binding = 3) uniform sampler1D spectrum0;
45layout(set = 0, binding = 4) uniform sampler1DArray spectrum_history;
46
47
48
49#ifndef SPECTRUM_HISTORY_LAYER
50#define SPECTRUM_HISTORY_LAYER(index) ((spectrum_history_head - ((index) % max(spectrum_history_size, 1)) + max(spectrum_history_size, 1)) % max(spectrum_history_size, 1))
51#endif
52
53
54
55
56
57
58
59
60
61const float PI = 3.14159265359;
62const float TAU = 6.28318530718;
63
64// --- UTILITIES ---
65
66/**
67 * @brief Evaluates rotate 2d for this shader effect.
68 * @param angle Input value for the calculation.
69 * @return Computed mat2 value.
70 */
71mat2 rotate_2d(float angle) {
72 float c = cos(angle);
73 float s = sin(angle);
74 return mat2(c, -s, s, c);
75}
76
77/**
78 * @brief Evaluates mirror repeat for this shader effect.
79 * @param point Input value for the calculation.
80 * @return Computed vec2 value.
81 */
82vec2 mirror_repeat(vec2 point) {
83 return 1.0 - abs(mod(point, 2.0) - 1.0);
84}
85
86/**
87 * @brief Evaluates palette for this shader effect.
88 * @param phase Input value for the calculation.
89 * @return Computed vec3 value.
90 */
91vec3 palette(float phase) {
92 return 0.5 + 0.5 * cos(TAU * (phase + vec3(0.02, 0.35, 0.69)));
93}
94
95// Pseudo-random noise for glitch artifacts
96/**
97 * @brief Evaluates hash 12 for this shader effect.
98 * @param p Input value for the calculation.
99 * @return Computed float value.
100 */
101float hash_12(vec2 p) {
102 vec3 p3 = fract(vec3(p.xyx) * 0.1031);
103 p3 += dot(p3, p3.yzx + 33.33);
104 return fract((p3.x + p3.y) * p3.z);
105}
106
107// --- OPTIMIZED SAMPLERS ---
108
109/**
110 * @brief Evaluates sample cache for this shader effect.
111 * @param index Input value for the calculation.
112 * @param uv Input value for the calculation.
113 * @return Computed vec4 value.
114 */
115vec4 sample_cache(int index, vec2 uv) {
116 uv = mirror_repeat(uv);
117 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(index))));
118}
119
120/**
121 * @brief Evaluates sample history for this shader effect.
122 * @param index Input value for the calculation.
123 * @param frequency Input value for the calculation.
124 * @return Computed float value.
125 */
126float sample_history(int index, float frequency) {
127 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(index + 1)))).r;
128}
129
130// --- GLITCH TRAIL TRANSFORM ---
131
132/**
133 * @brief Evaluates dynamic trail transform for this shader effect.
134 * @param uv Input value for the calculation.
135 * @param center Input value for the calculation.
136 * @param index Input value for the calculation.
137 * @param total_layers Input value for the calculation.
138 * @param bass Input value for the calculation.
139 * @param treble Input value for the calculation.
140 * @param time Input value for the calculation.
141 * @param peak Input value for the calculation.
142 * @return Computed vec2 value.
143 */
144vec2 dynamic_trail_transform(vec2 uv, vec2 center, int index, int total_layers, float bass, float treble, float time, float peak) {
145 float progress = float(index) / float(max(total_layers - 1, 1));
146
147 // 1. SCANLINE TEARING: Horizontal block displacement on high audio peaks
148 float strip_y = floor(uv.y * (20.0 + bass * 30.0) + time * 2.0);
149 float tear_offset = (hash_12(vec2(strip_y, time)) - 0.5) * 2.0;
150
151 // Activate tear only on loud peaks and push it deeper into the trail
152 float tear_amount = step(0.65, peak) * progress * (treble * 0.15);
153 vec2 point = uv - center;
154 point.x += tear_offset * tear_amount;
155
156 // 2. SCALE: Add slight noise to the zoom to make it breathe organically
157 float max_zoom = 1.0 + (bass * 1.5) + (hash_12(uv * 0.1 + time) * 0.1 * progress);
158 float current_scale = mix(1.0, max_zoom, progress);
159
160 // 3. STUTTER TWIST: Quantize the rotation for a robotic/VHS feel
161 float raw_angle = progress * (treble * PI) * sin(time * 0.5);
162 float quantize_steps = mix(40.0, 4.0, bass); // Heavy bass reduces steps, making it stutter more
163 float stutter_angle = floor(raw_angle * quantize_steps) / quantize_steps;
164
165 mat2 rot = rotate_2d(stutter_angle);
166 point = rot * (point / current_scale);
167
168 return mirror_repeat(point + center);
169}
170
171/**
172 * @brief Executes the fragment shader entry point.
173 */
174void main() {
175 vec2 resolution = max(iResolution, vec2(1.0));
176 float aspect = resolution.x / resolution.y;
177 vec2 mouse_uv = iMouse.xy / resolution;
178 vec2 center = iMouse.z > 0.0 ? mouse_uv : vec2(0.5);
179
180 // Fetch live audio
181 float bass = texture(spectrum0, 0.03).r;
182 float mid = texture(spectrum0, 0.22).r;
183 float treble = texture(spectrum0, 0.58).r;
184
185 vec3 live = texture(samp, tc).rgb;
186 vec3 accumulated = live;
187 float total_weight = 1.0;
188
189 for (int index = 0; index < SIZE; ++index) {
190
191 float old_bass = sample_history(index, 0.03);
192 float old_mid = sample_history(index, 0.22);
193 float old_treble = sample_history(index, 0.58);
194
195 float progress = float(index) / float(max(SIZE - 1, 1));
196
197 // 4. TEMPORAL CHROMATIC ABERRATION: Split UVs based on treble and age
198 float ca_spread = old_treble * progress * 0.04;
199
200 vec2 uv_r = dynamic_trail_transform(tc + vec2(ca_spread, ca_spread * 0.5), center, index, SIZE, old_bass, old_treble, time_f, amp_peak);
201 vec2 uv_g = dynamic_trail_transform(tc, center, index, SIZE, old_bass, old_treble, time_f, amp_peak);
202 vec2 uv_b = dynamic_trail_transform(tc - vec2(ca_spread, ca_spread * 0.5), center, index, SIZE, old_bass, old_treble, time_f, amp_peak);
203
204 // Sample split channels
205 float r = sample_cache(index, uv_r).r;
206 float g = sample_cache(index, uv_g).g;
207 float b = sample_cache(index, uv_b).b;
208
209 vec3 cached = vec3(r, g, b);
210
211 // 5. SIGNAL DEGRADATION: Bitcrush the older layers based on mid frequencies
212 float crush_steps = mix(64.0, 4.0, progress * old_mid);
213 cached = floor(cached * crush_steps) / crush_steps;
214
215 float weight = exp(-progress * 3.5);
216
217 vec3 trail_tint = mix(vec3(1.0), palette(time_f * 0.2 + progress + old_bass), progress * 0.8);
218
219 accumulated += (cached * trail_tint) * weight;
220 total_weight += weight;
221 }
222
223 vec3 result = accumulated / total_weight;
224
225 // Final contrast and inversion on heavy beats
226 result = (result - 0.5) * (1.1 + amp_smooth * 0.2) + 0.5;
227
228 // Violent invert flash when the beat peaks
229 result = mix(result, vec3(1.0) - result, smoothstep(0.85, 1.0, amp_peak));
230
231 color = vec4(clamp(result, 0.0, 1.0), texture(samp, tc).a);
232}
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_smooth
#define CACHE_HISTORY_LAYER(index)
mat2 rotate_2d(float angle)
Evaluates rotate 2d for this shader effect.
float sample_history(int index, float frequency)
Evaluates sample history for this shader effect.
#define SIZE
vec3 palette(float phase)
Evaluates palette for this shader effect.
#define SPECTRUM_HISTORY_LAYER(index)
vec2 mirror_repeat(vec2 point)
Evaluates mirror repeat for this shader effect.
vec4 sample_cache(int index, vec2 uv)
Evaluates sample cache for this shader effect.
#define amp_peak
vec2 rot(vec2 v, float a)
Evaluates rot for this shader effect.
Definition air-bowl.frag:52
vec2 dynamic_trail_transform(vec2 uv, vec2 center, int index, int total_layers, float bass, float treble, float time)
Evaluates dynamic trail transform for this shader effect.
#define TAU
#define PI
#define frequency
Definition ol.frag:13
float hash_12(vec2 p)
Evaluates hash 12 for this shader effect.