ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
codex-gen-cache-asteroid-sonar.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file codex-gen-cache-asteroid-sonar.frag
5 * @brief Vulkan fragment shader: codex-gen-cache-asteroid-sonar.
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// Codex cache concept 023: asteroid sonar.
30// Remixes repository cache, spectrum-history, coordinate-fold, and palette idioms.
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#ifndef SIZE
37#define SIZE 8
38#endif
39#ifndef CACHE_HISTORY_LAYER
40#define CACHE_HISTORY_LAYER(index) ((history_head + (index)) % SIZE)
41#endif
42layout(set = 0, binding = 3) uniform sampler1D spectrum0;
43layout(set = 0, binding = 4) uniform sampler1DArray spectrum_history;
44
45
46#ifndef SPECTRUM_HISTORY_LAYER
47#define SPECTRUM_HISTORY_LAYER(index) ((spectrum_history_head - ((index) % max(spectrum_history_size, 1)) + max(spectrum_history_size, 1)) % max(spectrum_history_size, 1))
48#endif
49
50
51
52
53
54
55
56
57
58const float TAU = 6.28318530718;
59
60/**
61 * @brief Evaluates rotate 2d for this shader effect.
62 * @param angle Input value for the calculation.
63 * @return Computed mat2 value.
64 */
65mat2 rotate_2d(float angle) {
66 float cosine = cos(angle);
67 float sine = sin(angle);
68 return mat2(cosine, -sine, sine, cosine);
69}
70
71/**
72 * @brief Evaluates mirror repeat for this shader effect.
73 * @param point Input value for the calculation.
74 * @return Computed vec2 value.
75 */
76vec2 mirror_repeat(vec2 point) {
77 return 1.0 - abs(mod(point, 2.0) - 1.0);
78}
79
80/**
81 * @brief Evaluates hash 21 for this shader effect.
82 * @param point Input value for the calculation.
83 * @return Computed float value.
84 */
85float hash_21(vec2 point) {
86 return fract(sin(dot(point, vec2(127.1, 311.7))) * 43758.5453123);
87}
88
89/**
90 * @brief Evaluates noise 2d for this shader effect.
91 * @param point Input value for the calculation.
92 * @return Computed float value.
93 */
94float noise_2d(vec2 point) {
95 vec2 cell = floor(point);
96 vec2 part = fract(point);
97 part = part * part * (3.0 - 2.0 * part);
98 return mix(mix(hash_21(cell), hash_21(cell + vec2(1.0, 0.0)), part.x),
99 mix(hash_21(cell + vec2(0.0, 1.0)), hash_21(cell + vec2(1.0)), part.x),
100 part.y);
101}
102
103/**
104 * @brief Evaluates voronoi data for this shader effect.
105 * @param point Input value for the calculation.
106 * @return Computed vec3 value.
107 */
108vec3 voronoi_data(vec2 point) {
109 vec2 cell = floor(point);
110 vec2 part = fract(point);
111 float nearest = 4.0;
112 float second_nearest = 4.0;
113 float identity = 0.0;
114 for (int y = -1; y <= 1; ++y) {
115 for (int x = -1; x <= 1; ++x) {
116 vec2 offset = vec2(float(x), float(y));
117 vec2 seed = vec2(hash_21(cell + offset), hash_21(cell + offset + 19.17));
118 float distance_to_seed = length(offset + seed - part);
119 if (distance_to_seed < nearest) {
120 second_nearest = nearest;
121 nearest = distance_to_seed;
122 identity = hash_21(cell + offset + 7.31);
123 } else if (distance_to_seed < second_nearest) {
124 second_nearest = distance_to_seed;
125 }
126 }
127 }
128 return vec3(nearest, second_nearest, identity);
129}
130
131/**
132 * @brief Evaluates hex coord for this shader effect.
133 * @param point Input value for the calculation.
134 * @return Computed vec2 value.
135 */
136vec2 hex_coord(vec2 point) {
137 const vec2 SCALE = vec2(1.0, 1.7320508);
138 vec2 first = mod(point, SCALE) - SCALE * 0.5;
139 vec2 second = mod(point - SCALE * 0.5, SCALE) - SCALE * 0.5;
140 return dot(first, first) < dot(second, second) ? first : second;
141}
142
143/**
144 * @brief Evaluates palette for this shader effect.
145 * @param phase Input value for the calculation.
146 * @return Computed vec3 value.
147 */
148vec3 palette(float phase) {
149 return 0.5 + 0.5 * cos(TAU * (phase + 0.014 + vec3(0.04, 0.37, 0.69)));
150}
151
152/**
153 * @brief Evaluates sample cache for this shader effect.
154 * @param index Input value for the calculation.
155 * @param uv Input value for the calculation.
156 * @return Computed vec3 value.
157 */
158vec3 sample_cache(int index, vec2 uv) {
159 return texture(history, vec3(mirror_repeat(uv),
160 float(CACHE_HISTORY_LAYER(index)))).rgb;
161}
162
163/**
164 * @brief Evaluates history fft for this shader effect.
165 * @param index Input value for the calculation.
166 * @param frequency Input value for the calculation.
167 * @return Computed float value.
168 */
169float history_fft(int index, float frequency) {
170 int maximum_age = max(min(SIZE, spectrum_history_size) - 1, 0);
171 int spectrum_age = min(index + 1, maximum_age);
172 return texture(spectrum_history,
173 vec2(frequency, float(SPECTRUM_HISTORY_LAYER(spectrum_age)))).r;
174}
175
176vec2 concept_space(vec2 point, float age, vec3 old_audio,
177 float bass, float middle, float high) {
178 vec2 q = point;
179 float old_bass = old_audio.x;
180 float old_mid = old_audio.y;
181 float old_high = old_audio.z;
182 vec2 cell = floor((q + 1.0) * 4.0);
183 vec2 local = fract((q + 1.0) * 4.0) - 0.5;
184 local *= 1.0 + dot(local, local) * (0.8 + old_bass);
185 q = (cell + local + 0.5) / 4.0 - 1.0;
186 return q;
187}
188
189/**
190 * @brief Evaluates concept field for this shader effect.
191 * @param q Input value for the calculation.
192 * @param age Input value for the calculation.
193 * @param bass Input value for the calculation.
194 * @param middle Input value for the calculation.
195 * @param high Input value for the calculation.
196 * @return Computed float value.
197 */
198float concept_field(vec2 q, float age, float bass, float middle, float high) {
199 vec2 diamond = abs(fract(q * (7.0 + middle * 4.0)) - 0.5);
200 return exp(-abs(diamond.x + diamond.y - 0.33) * (36.0 + high * 18.0));
201}
202
203/**
204 * @brief Evaluates blend memory for this shader effect.
205 * @param memory Input value for the calculation.
206 * @param live Input value for the calculation.
207 * @param field Input value for the calculation.
208 * @param age Input value for the calculation.
209 * @param old_audio Input value for the calculation.
210 * @return Computed vec3 value.
211 */
212vec3 blend_memory(vec3 memory, vec3 live, float field, float age, vec3 old_audio) {
213 vec3 split = vec3(memory.r, live.g, memory.b);
214 return mix(memory, split * palette(field + age), 0.45 + field * 0.3);
215}
216
217/**
218 * @brief Executes the fragment shader entry point.
219 */
220void main() {
221 vec2 resolution = max(iResolution, vec2(1.0));
222 vec2 aspect_scale = vec2(resolution.x / resolution.y, 1.0);
223 vec2 center = iMouse.z > 0.0 ? iMouse.xy / resolution : vec2(0.5);
224 vec2 point = (tc - center) * aspect_scale;
225 float bass = max(texture(spectrum0, 0.045).r, amp_low);
226 float middle = max(texture(spectrum0, 0.32).r, amp_mid);
227 float high = max(texture(spectrum0, 0.82).r, amp_high);
228 vec3 current_audio = vec3(bass, middle, high);
229
230 vec2 live_q = concept_space(point, 0.0, current_audio, bass, middle, high);
231 float live_field = concept_field(live_q, 0.0, bass, middle, high);
232 vec3 live = texture(samp, mirror_repeat(live_q / aspect_scale + center)).rgb;
233 vec3 accumulated = live * 1.2;
234 float total_weight = 1.2;
235
236 for (int index = 0; index < SIZE; ++index) {
237 float age = float(index + 1) / float(max(SIZE, 1));
238 vec3 old_audio = vec3(history_fft(index, 0.045),
239 history_fft(index, 0.32),
240 history_fft(index, 0.82));
241 vec2 cache_q = concept_space(point, age, old_audio, bass, middle, high);
242 float field = concept_field(cache_q, age, bass, middle, high);
243 vec3 memory = sample_cache(index, cache_q / aspect_scale + center);
244 vec3 treated = blend_memory(memory, live, field, age, old_audio);
245 float weight = pow(1.0 - age * 0.8, 1.5) * (0.35 + field * 0.75);
246 accumulated += treated * weight;
247 total_weight += weight;
248 }
249
250 vec3 result = accumulated / max(total_weight, 0.001);
251 result += palette(length(live_q) + time_f * 0.04) * pow(live_field, 2.0) * 0.22;
252 result = (result - 0.5) * (1.04 + amp_smooth * 0.18) + 0.5;
253 color = vec4(clamp(result, 0.0, 1.0), texture(samp, tc).a);
254}
255
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)
#define amp_mid
mat2 rotate_2d(float angle)
Evaluates rotate 2d for this shader effect.
#define SIZE
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)
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 spectrum_history_size
#define seed
Definition air-bowl.frag:20
float field(vec2 p, float tm)
Evaluates field for this shader effect.
vec2 hex_coord(vec2 point)
Evaluates hex coord for this shader effect.
float concept_field(vec2 q, float age, float bass, float middle, float high)
Evaluates concept field for this shader effect.
vec3 blend_memory(vec3 memory, vec3 live, float field, float age, vec3 old_audio)
Evaluates blend memory for this shader effect.
float noise_2d(vec2 point)
Evaluates noise 2d for this shader effect.
vec2 concept_space(vec2 point, float age, vec3 old_audio, float bass, float middle, float high)
vec3 voronoi_data(vec2 point)
Evaluates voronoi data for this shader effect.
float history_fft(int index, float frequency)
Evaluates history fft for this shader effect.
vec2 cell(vec2 p)
Evaluates cell for this shader effect.
#define TAU
float diamond(vec2 p)
Evaluates diamond for this shader effect.
Definition dmd_gpt.frag:29
#define frequency
Definition ol.frag:13