ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
af-scale-cache-code-crown-4D.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file af-scale-cache-code-crown-4D.frag
5 * @brief Vulkan fragment shader: af-scale-cache-code-crown-4D.
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// af_scale-cache-code-acid-crown-4D
30// A crown of sharpened acid rays and 4D scale-pulsed reflections.
31#define EFFECT_ID 24
32layout(location = 0) in vec2 tc;
33layout(location = 0) out vec4 color;
34layout(set = 0, binding = 0) uniform sampler2D samp;
35layout(set = 0, binding = 2) uniform sampler2DArray history;
36
37#ifndef SIZE
38#define SIZE 8
39#endif
40#ifndef CACHE_HISTORY_LAYER
41#define CACHE_HISTORY_LAYER(index) ((history_head + (index)) % SIZE)
42#endif
43layout(set = 0, binding = 3) uniform sampler1D spectrum0;
44layout(set = 0, binding = 4) uniform sampler1DArray spectrum_history;
45
46
47#ifndef SPECTRUM_HISTORY_LAYER
48#define SPECTRUM_HISTORY_LAYER(index) ((spectrum_history_head - ((index) % max(spectrum_history_size, 1)) + max(spectrum_history_size, 1)) % max(spectrum_history_size, 1))
49#endif
50
51
52
53
54
55
56
57
58
59const float PI = 3.14159265359;
60const float TAU = 6.28318530718;
61
62// --- 4D GEOMETRY OPERATIONS ---
63
64// Rotates a vec4 in the XW plane
65/**
66 * @brief Evaluates rotate 4d xw for this shader effect.
67 * @param v Input value for the calculation.
68 * @param angle Input value for the calculation.
69 * @return Computed vec4 value.
70 */
71vec4 rotate_4d_xw(vec4 v, float angle) {
72 float cosine = cos(angle);
73 float sine = sin(angle);
74 return vec4(v.x * cosine - v.w * sine, v.y, v.z, v.x * sine + v.w * cosine);
75}
76
77// Rotates a vec4 in the YW plane
78/**
79 * @brief Evaluates rotate 4d yw for this shader effect.
80 * @param v Input value for the calculation.
81 * @param angle Input value for the calculation.
82 * @return Computed vec4 value.
83 */
84vec4 rotate_4d_yw(vec4 v, float angle) {
85 float cosine = cos(angle);
86 float sine = sin(angle);
87 return vec4(v.x, v.y * cosine - v.w * sine, v.z, v.y * sine + v.w * cosine);
88}
89
90// Rotates a vec4 in the ZW plane
91/**
92 * @brief Evaluates rotate 4d zw for this shader effect.
93 * @param v Input value for the calculation.
94 * @param angle Input value for the calculation.
95 * @return Computed vec4 value.
96 */
97vec4 rotate_4d_zw(vec4 v, float angle) {
98 float cosine = cos(angle);
99 float sine = sin(angle);
100 return vec4(v.x, v.y, v.z * cosine - v.w * sine, v.z * sine + v.w * cosine);
101}
102
103// Projects 4D geometry back to 2D for texture sampling
104/**
105 * @brief Evaluates project 4d to 2d for this shader effect.
106 * @param v Input value for the calculation.
107 * @param distance_z Input value for the calculation.
108 * @param distance_w Input value for the calculation.
109 * @return Computed vec2 value.
110 */
111vec2 project_4d_to_2d(vec4 v, float distance_z, float distance_w) {
112 // Prevent division by zero with small offsets
113 float w_proj = 1.0 / (distance_w - v.w + 0.001);
114 float z_proj = 1.0 / (distance_z - (v.z * w_proj) + 0.001);
115 return vec2(v.x * z_proj * w_proj, v.y * z_proj * w_proj);
116}
117
118/**
119 * @brief Evaluates rotate 2d for this shader effect.
120 * @param angle Input value for the calculation.
121 * @return Computed mat2 value.
122 */
123mat2 rotate_2d(float angle) {
124 float cosine = cos(angle);
125 float sine = sin(angle);
126 return mat2(cosine, -sine, sine, cosine);
127}
128
129/**
130 * @brief Evaluates mirror repeat for this shader effect.
131 * @param point Input value for the calculation.
132 * @return Computed vec2 value.
133 */
134vec2 mirror_repeat(vec2 point) {
135 return 1.0 - abs(mod(point, 2.0) - 1.0);
136}
137
138/**
139 * @brief Evaluates ping pong for this shader effect.
140 * @param value Input value for the calculation.
141 * @param size Input value for the calculation.
142 * @return Computed float value.
143 */
144float ping_pong(float value, float size) {
145 float wrapped = mod(value, size * 2.0);
146 return wrapped <= size ? wrapped : size * 2.0 - wrapped;
147}
148
149/**
150 * @brief Evaluates hash 21 for this shader effect.
151 * @param point Input value for the calculation.
152 * @return Computed float value.
153 */
154float hash_21(vec2 point) {
155 return fract(sin(dot(point, vec2(127.1, 311.7))) * 43758.5453);
156}
157
158/**
159 * @brief Evaluates value noise for this shader effect.
160 * @param point Input value for the calculation.
161 * @return Computed float value.
162 */
163float value_noise(vec2 point) {
164 vec2 cell = floor(point);
165 vec2 part = fract(point);
166 part = part * part * (3.0 - 2.0 * part);
167 float lower = mix(hash_21(cell), hash_21(cell + vec2(1.0, 0.0)), part.x);
168 float upper = mix(hash_21(cell + vec2(0.0, 1.0)), hash_21(cell + 1.0), part.x);
169 return mix(lower, upper, part.y);
170}
171
172/**
173 * @brief Evaluates fbm for this shader effect.
174 * @param point Input value for the calculation.
175 * @return Computed float value.
176 */
177float fbm(vec2 point) {
178 float result = 0.0;
179 float weight = 0.5;
180 for (int octave = 0; octave < 4; ++octave) {
181 result += value_noise(point) * weight;
182 point = rotate_2d(0.71) * point * 2.03 + 1.37;
183 weight *= 0.5;
184 }
185 return result;
186}
187
188/**
189 * @brief Evaluates palette for this shader effect.
190 * @param phase Input value for the calculation.
191 * @return Computed vec3 value.
192 */
193vec3 palette(float phase) {
194 float hue = float(EFFECT_ID) * 0.073;
195 return 0.5 + 0.5 * cos(TAU * (phase + hue + vec3(0.02, 0.35, 0.69)));
196}
197
198/**
199 * @brief Evaluates tone map for this shader effect.
200 * @param value Input value for the calculation.
201 * @return Computed vec3 value.
202 */
203vec3 tone_map(vec3 value) {
204 value = max(value, 0.0);
205 return clamp((value * (2.51 * value + 0.03)) /
206 (value * (2.43 * value + 0.59) + 0.14),
207 0.0, 1.0);
208}
209
210/**
211 * @brief Evaluates sample cache for this shader effect.
212 * @param index Input value for the calculation.
213 * @param uv Input value for the calculation.
214 * @return Computed vec4 value.
215 */
216vec4 sample_cache(int index, vec2 uv) {
217 uv = mirror_repeat(uv);
218 if (index == 0) return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(0))));
219 if (index == 1) return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(1))));
220 if (index == 2) return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(2))));
221 if (index == 3) return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(3))));
222 if (index == 4) return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(4))));
223 if (index == 5) return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(5))));
224 if (index == 6) return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(6))));
225 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(7))));
226}
227
228/**
229 * @brief Evaluates sample history for this shader effect.
230 * @param index Input value for the calculation.
231 * @param frequency Input value for the calculation.
232 * @return Computed float value.
233 */
234float sample_history(int index, float frequency) {
235 if (index == 0) return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(1)))).r;
236 if (index == 1) return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(2)))).r;
237 if (index == 2) return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(3)))).r;
238 if (index == 3) return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(4)))).r;
239 if (index == 4) return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(5)))).r;
240 if (index == 5) return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(6)))).r;
241 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(7)))).r;
242}
243
244/**
245 * @brief Evaluates soft sample for this shader effect.
246 * @param image Input value for the calculation.
247 * @param uv Input value for the calculation.
248 * @param texel Input value for the calculation.
249 * @param spread Input value for the calculation.
250 * @return Computed vec4 value.
251 */
252vec4 soft_sample(sampler2D image, vec2 uv, vec2 texel, float spread) {
253 vec4 result = texture(image, mirror_repeat(uv)) * 4.0;
254 result += texture(image, mirror_repeat(uv + vec2(texel.x, 0.0) * spread)) * 2.0;
255 result += texture(image, mirror_repeat(uv - vec2(texel.x, 0.0) * spread)) * 2.0;
256 result += texture(image, mirror_repeat(uv + vec2(0.0, texel.y) * spread)) * 2.0;
257 result += texture(image, mirror_repeat(uv - vec2(0.0, texel.y) * spread)) * 2.0;
258 result += texture(image, mirror_repeat(uv + texel * spread));
259 result += texture(image, mirror_repeat(uv - texel * spread));
260 result += texture(image, mirror_repeat(uv + vec2(texel.x, -texel.y) * spread));
261 result += texture(image, mirror_repeat(uv + vec2(-texel.x, texel.y) * spread));
262 return result / 16.0;
263}
264
265/**
266 * @brief Evaluates effect field for this shader effect.
267 * @param point Input value for the calculation.
268 * @param bass Input value for the calculation.
269 * @param middle Input value for the calculation.
270 * @param treble Input value for the calculation.
271 * @return Computed float value.
272 */
273float effect_field(vec2 point, float bass, float middle, float treble) {
274 float radius = length(point) + 0.0001;
275 float angle = atan(point.y, point.x);
276
277 // Construct a 4D spatial coordinate using the audio spectrum for Z and W
278 vec4 geo_4d = vec4(point.x, point.y, bass - middle, treble + sin(time_f * 0.2));
279
280 // Rotate the 4D object based on time and audio intensity
281 geo_4d = rotate_4d_xw(geo_4d, time_f * 0.6 + bass * 2.0);
282 geo_4d = rotate_4d_yw(geo_4d, time_f * 0.4 - middle * 1.5);
283 geo_4d = rotate_4d_zw(geo_4d, time_f * 0.2 + treble);
284
285 // Use the 4D transformed vector to calculate the field intensity
286 float crown = pow(abs(cos(angle * (13.0 + floor(treble * 7.0)) + time_f + geo_4d.w * 5.0)), 12.0);
287 float ring = sin(radius * (37.0 + bass * 24.0) - time_f * 9.0 + geo_4d.z * 10.0);
288
289 float field = crown * exp(-radius * 1.7) * 0.72 + ring * 0.32;
290 return field + (fbm(vec2(geo_4d.x, geo_4d.y) * 3.1 - vec2(0.0, time_f * 0.12)) - 0.5) * 0.12;
291}
292
293/**
294 * @brief Evaluates cache transform 4d for this shader effect.
295 * @param uv Input value for the calculation.
296 * @param center Input value for the calculation.
297 * @param age Input value for the calculation.
298 * @param bass Input value for the calculation.
299 * @param middle Input value for the calculation.
300 * @param treble Input value for the calculation.
301 * @param flow Input value for the calculation.
302 * @return Computed vec2 value.
303 */
304vec2 cache_transform_4d(vec2 uv, vec2 center, float age, float bass, float middle, float treble, vec2 flow) {
305 vec2 point = uv - center;
306
307 // Embed the 2D cache coordinate into 4D space
308 // Z axis maps to spectral energy, W axis maps to the layer's age in the cache
309 vec4 cache_4d = vec4(point.x, point.y, bass - treble, age * 0.1);
310
311 // Perform 4D hyper-rotations to warp the previous frames
312 cache_4d = rotate_4d_xw(cache_4d, time_f * 0.3 + bass);
313 cache_4d = rotate_4d_yw(cache_4d, -time_f * 0.2 + middle);
314
315 // Add the surface normal flow into the 4D geometry
316 cache_4d.x += flow.x * age * (0.007 + middle * 0.012);
317 cache_4d.y += flow.y * age * (0.007 + middle * 0.012);
318
319 // Project the 4D volume back down to 2D for the actual texture lookup
320 vec2 projected = project_4d_to_2d(cache_4d, 2.5, 2.0);
321
322 float scale = pow(max(0.982 - bass * 0.065, 0.45), age);
323 projected *= scale;
324
325 return mirror_repeat(projected + center);
326}
327
328/**
329 * @brief Executes the fragment shader entry point.
330 */
331void main() {
332 vec2 resolution = max(iResolution, vec2(1.0));
333 vec2 texel = 1.0 / resolution;
334 float aspect = resolution.x / resolution.y;
335 vec2 mouse_uv = iMouse.xy / resolution;
336 vec2 center = iMouse.z > 0.0 ? mouse_uv : vec2(0.5);
337 vec2 point = (tc - center) * vec2(aspect, 1.0);
338
339 float bass = texture(spectrum0, 0.03).r;
340 float middle = texture(spectrum0, 0.22).r;
341 float treble = texture(spectrum0, 0.58).r;
342 float air = texture(spectrum0, 0.82).r;
343
344 float field = effect_field(point, bass, middle, treble);
345
346 float pixel = 2.0 / max(max(resolution.x, resolution.y), 360.0);
347 vec2 gradient = vec2(effect_field(point + vec2(pixel, 0.0), bass, middle, treble) - field,
348 effect_field(point + vec2(0.0, pixel), bass, middle, treble) - field) /
349 pixel;
350 vec3 surface_normal = normalize(vec3(-gradient * (0.16 + amp_high * 0.05), 1.0));
351
352 float ripple = sin(point.x * 10.0 + time_f * (4.0 + bass * 3.0));
353 ripple += sin(point.y * 11.0 + time_f * (3.5 + middle * 2.0));
354 vec2 radial = normalize(point + vec2(0.001));
355
356 vec2 ripple_uv = tc + radial * ripple * (0.009 + bass * 0.018);
357 vec2 live_uv = mirror_repeat(ripple_uv + surface_normal.xy * (0.012 + amp_low * 0.025));
358
359 vec3 sharp = texture(samp, live_uv).rgb;
360 vec3 softened = soft_sample(samp, live_uv, texel, 1.0 + middle * 3.0).rgb;
361 float dispersion = 0.003 + treble * 0.014;
362 vec3 refracted = vec3(texture(samp, mirror_repeat(live_uv + surface_normal.xy * dispersion)).r,
363 softened.g,
364 texture(samp, mirror_repeat(live_uv - surface_normal.xy * dispersion)).b);
365 vec3 live = mix(sharp, refracted, 0.48 + amp_smooth * 0.15);
366
367 float luminance = dot(live, vec3(0.299, 0.587, 0.114));
368
369 // Map the 4D geometry directly to the color palette
370 // We construct a vector from the field, spectrum, and time, rotate it, and use the projected value
371 vec4 color_4d = vec4(field, bass, middle, time_f * 0.1);
372 color_4d = rotate_4d_xw(color_4d, treble * PI);
373
374 vec3 mapped = 0.5 + 0.5 * cos(clamp(live, 0.0, 1.0) * PI * 0.5 +
375 palette(color_4d.x + color_4d.w));
376
377 vec3 accumulated = mix(live, mapped * (0.45 + luminance), 0.44);
378 accumulated += palette(field * 0.55 + time_f * 0.04) *
379 pow(max(0.0, 1.0 - surface_normal.z), 2.0) * 0.7;
380 float total_weight = 1.0;
381
382 for (int index = 0; index < 8; ++index) {
383 float age = float(index + 1);
384 float old_bass = sample_history(index, 0.03);
385 float old_middle = sample_history(index, 0.22);
386 float old_treble = sample_history(index, 0.58);
387
388 // Use the new 4D transform for the cache history lookup
389 vec2 history_uv = cache_transform_4d(tc, center, age, old_bass, old_middle, old_treble, surface_normal.xy);
390
391 vec3 history_color = sample_cache(index, history_uv).rgb;
392 float old_energy = old_bass * 0.5 + old_middle * 0.3 + old_treble * 0.2;
393
394 // Shift the tint utilizing the 4D geometry mapping from earlier
395 vec3 history_tint = palette(color_4d.z * 0.12 + age * 0.061 + old_energy * 0.7);
396
397 float weight = pow(0.76, age);
398 accumulated += mix(history_color, history_color * history_tint, 0.62) * weight;
399 total_weight += weight;
400 }
401
402 accumulated /= total_weight;
403 float crest = pow(max(sin(field * 19.0 - time_f * 2.4), 0.0), 7.0);
404 accumulated += palette(field + time_f * 0.09) * crest * (0.25 + air * 1.35);
405 accumulated = (accumulated - 0.5) * (1.08 + amp_smooth * 0.28) + 0.5;
406 accumulated = mix(accumulated, vec3(1.0) - accumulated, smoothstep(0.91, 1.0, amp_peak));
407
408 color = vec4(tone_map(accumulated), texture(samp, live_uv).a);
409}
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
vec4 soft_sample(sampler2D image, vec2 uv, vec2 texel, float spread)
Evaluates soft sample for this shader effect.
float fbm(vec2 point)
Evaluates fbm for this shader effect.
#define amp_smooth
#define CACHE_HISTORY_LAYER(index)
float value_noise(vec2 point)
Evaluates value noise for this shader effect.
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.
float hash_21(vec2 point)
Evaluates hash 21 for this shader effect.
vec3 palette(float phase)
Evaluates palette for this shader effect.
#define amp_low
#define amp_high
#define SPECTRUM_HISTORY_LAYER(index)
float ping_pong(float value, float size)
Evaluates ping pong for this shader effect.
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.
float effect_field(vec2 point, float bass, float middle, float treble)
Evaluates effect field for this shader effect.
#define EFFECT_ID
#define amp_peak
vec3 tone_map(vec3 value)
Evaluates tone map for this shader effect.
vec3 ripple(ivec2 p, ivec2 size)
Evaluates ripple for this shader effect.
vec2 project_4d_to_2d(vec4 v, float distance_z, float distance_w)
Evaluates project 4d to 2d for this shader effect.
vec4 rotate_4d_xw(vec4 v, float angle)
Evaluates rotate 4d xw for this shader effect.
vec4 rotate_4d_yw(vec4 v, float angle)
Evaluates rotate 4d yw for this shader effect.
vec4 rotate_4d_zw(vec4 v, float angle)
Evaluates rotate 4d zw for this shader effect.
vec2 cache_transform_4d(vec2 uv, vec2 center, float age, float bass, float middle, float treble, vec2 flow)
Evaluates cache transform 4d for this shader effect.
vec3 hue(float t)
Evaluates hue for this shader effect.
Definition auraXi1.frag:65
float field(vec2 p, float tm)
Evaluates field for this shader effect.
float luminance(vec3 c)
Evaluates luminance for this shader effect.
vec2 cell(vec2 p)
Evaluates cell for this shader effect.
#define TAU
#define PI
#define frequency
Definition ol.frag:13