ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
inversion_code_cache_dipole_choir.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file inversion_code_cache_dipole_choir.frag
5 * @brief Vulkan fragment shader: inversion code cache dipole choir.
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_peak ext.u2.w
18#define amp_smooth ext.u3.w
19#define history_head int(ext.u3.x)
20#define iResolution ext.u0.zw
21#define iTime ext.u0.y
22#define spectrum_history_head int(ext.audio_history.x)
23#define spectrum_history_size int(ext.audio_history.y)
24#define time_f ext.u2.y
25
26
27// Temporal inversion energy field: dipole choir.
28layout(location = 0) in vec2 tc;
29layout(location = 0) out vec4 color;
30layout(set = 0, binding = 0) uniform sampler2D samp;
31layout(set = 0, binding = 2) uniform sampler2DArray history;
32
33#ifndef SIZE
34#define SIZE 8
35#endif
36#ifndef CACHE_HISTORY_LAYER
37#define CACHE_HISTORY_LAYER(index) ((history_head + (index)) % SIZE)
38#endif
39layout(set = 0, binding = 3) uniform sampler1D spectrum0;
40layout(set = 0, binding = 4) uniform sampler1DArray spectrum_history;
41
42
43#ifndef SPECTRUM_HISTORY_LAYER
44#define SPECTRUM_HISTORY_LAYER(index) ((spectrum_history_head - ((index) % max(spectrum_history_size, 1)) + max(spectrum_history_size, 1)) % max(spectrum_history_size, 1))
45#endif
46
47
48
49
50
51
52const float PI = 3.14159265359;
53const float TAU = 6.28318530718;
54const float HUE_OFFSET = 0.11;
55
56/**
57 * @brief Evaluates clockTime for this shader effect.
58 * @return Computed float value.
59 */
60float clockTime() {
61 return max(time_f, iTime);
62}
63
64/**
65 * @brief Evaluates hash21 for this shader effect.
66 * @param p Input value for the calculation.
67 * @return Computed float value.
68 */
69float hash21(vec2 p) {
70 return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
71}
72
73/**
74 * @brief Evaluates hash22 for this shader effect.
75 * @param p Input value for the calculation.
76 * @return Computed vec2 value.
77 */
78vec2 hash22(vec2 p) {
79 return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453123);
80}
81
82/**
83 * @brief Evaluates noise21 for this shader effect.
84 * @param p Input value for the calculation.
85 * @return Computed float value.
86 */
87float noise21(vec2 p) {
88 vec2 cell = floor(p);
89 vec2 local = fract(p);
90 local = local * local * (3.0 - 2.0 * local);
91 return mix(mix(hash21(cell), hash21(cell + vec2(1.0, 0.0)), local.x),
92 mix(hash21(cell + vec2(0.0, 1.0)), hash21(cell + 1.0), local.x), local.y);
93}
94
95/**
96 * @brief Evaluates fbm for this shader effect.
97 * @param p Input value for the calculation.
98 * @return Computed float value.
99 */
100float fbm(vec2 p) {
101 float value = 0.0;
102 float weight = 0.5;
103 mat2 basis = mat2(0.80, -0.60, 0.60, 0.80);
104 for (int octave = 0; octave < 5; ++octave) {
105 value += noise21(p) * weight;
106 p = basis * p * 2.03 + vec2(1.7, 2.4);
107 weight *= 0.5;
108 }
109 return value;
110}
111
112/**
113 * @brief Evaluates rotate2 for this shader effect.
114 * @param p Input value for the calculation.
115 * @param angle Input value for the calculation.
116 * @return Computed vec2 value.
117 */
118vec2 rotate2(vec2 p, float angle) {
119 float cosine = cos(angle);
120 float sine = sin(angle);
121 return mat2(cosine, -sine, sine, cosine) * p;
122}
123
124/**
125 * @brief Evaluates segmentDistance for this shader effect.
126 * @param p Input value for the calculation.
127 * @param a Input value for the calculation.
128 * @param b Input value for the calculation.
129 * @return Computed float value.
130 */
131float segmentDistance(vec2 p, vec2 a, vec2 b) {
132 vec2 segment = b - a;
133 float position = clamp(dot(p - a, segment) / max(dot(segment, segment), 0.0001), 0.0, 1.0);
134 return length(p - a - segment * position);
135}
136
137/**
138 * @brief Evaluates mirrorUV for this shader effect.
139 * @param uv Input value for the calculation.
140 * @return Computed vec2 value.
141 */
142vec2 mirrorUV(vec2 uv) {
143 return 1.0 - abs(mod(uv, 2.0) - 1.0);
144}
145
146/**
147 * @brief Evaluates palette for this shader effect.
148 * @param phase Input value for the calculation.
149 * @return Computed vec3 value.
150 */
151vec3 palette(float phase) {
152 vec3 colorWave = 0.5 + 0.5 * cos(TAU * (phase + HUE_OFFSET + vec3(0.00, 0.28, 0.63)));
153 return clamp((colorWave - 0.08) * 1.16, 0.0, 1.0);
154}
155
156/**
157 * @brief Evaluates toneMap for this shader effect.
158 * @param value Input value for the calculation.
159 * @return Computed vec3 value.
160 */
161vec3 toneMap(vec3 value) {
162 value = max(value, 0.0);
163 return clamp((value * (2.51 * value + 0.03)) / (value * (2.43 * value + 0.59) + 0.14), 0.0,
164 1.0);
165}
166
167/**
168 * @brief Evaluates cacheFrame for this shader effect.
169 * @param index Input value for the calculation.
170 * @param uv Input value for the calculation.
171 * @return Computed vec4 value.
172 */
173vec4 cacheFrame(int index, vec2 uv) {
174 if (index == 0)
175 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(0))));
176 if (index == 1)
177 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(1))));
178 if (index == 2)
179 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(2))));
180 if (index == 3)
181 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(3))));
182 if (index == 4)
183 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(4))));
184 if (index == 5)
185 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(5))));
186 if (index == 6)
187 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(6))));
188 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(7))));
189}
190
191/**
192 * @brief Evaluates spectrumHistory for this shader effect.
193 * @param index Input value for the calculation.
194 * @param frequency Input value for the calculation.
195 * @return Computed float value.
196 */
197float spectrumHistory(int index, float frequency) {
198 if (index == 0)
199 return texture(spectrum0, frequency).r;
200 if (index == 1)
201 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(1)))).r;
202 if (index == 2)
203 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(2)))).r;
204 if (index == 3)
205 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(3)))).r;
206 if (index == 4)
207 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(4)))).r;
208 if (index == 5)
209 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(5)))).r;
210 if (index == 6)
211 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(6)))).r;
212 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(7)))).r;
213}
214
215/**
216 * @brief Evaluates historyBands for this shader effect.
217 * @param index Input value for the calculation.
218 * @return Computed vec4 value.
219 */
220vec4 historyBands(int index) {
221 return vec4(spectrumHistory(index, 0.04), spectrumHistory(index, 0.22),
222 spectrumHistory(index, 0.58), spectrumHistory(index, 0.86));
223}
224
225/**
226 * @brief Evaluates energyField for this shader effect.
227 * @param p Input value for the calculation.
228 * @param age Input value for the calculation.
229 * @param band Input value for the calculation.
230 * @return Computed vec3 value.
231 */
232vec3 energyField(vec2 p, float age, vec4 band) {
233 float t = clockTime();
234 vec2 poleA = vec2(cos(t * 0.37 + age), sin(t * 0.29 + age * 0.7)) * 0.34;
235 vec2 poleB = -poleA;
236 vec2 da = p - poleA;
237 vec2 db = p - poleB;
238 float ra = length(da) + 0.055;
239 float rb = length(db) + 0.055;
240 float potential = (1.0 + band.x) / ra - (1.0 + band.y) / rb;
241 float energy = 0.5 + 0.5 * sin(potential * 2.6 + t - age * 0.8);
242 vec2 flow = normalize(da) / ra - normalize(db) / rb;
243 return vec3(energy, flow * 0.18);
244}
245
246/**
247 * @brief Executes the fragment shader entry point.
248 */
249void main(void) {
250 float aspect = max(iResolution.x, 1.0) / max(iResolution.y, 1.0);
251 vec2 aspectScale = vec2(aspect, 1.0);
252 vec2 p = (tc - 0.5) * aspectScale;
253 vec3 live = texture(samp, tc).rgb;
254
255 vec3 accumulation = live * 0.10;
256 float totalWeight = 0.10;
257 vec4 newestBands = historyBands(0);
258 vec4 oldestBands = historyBands(7);
259 float temporalChange = length(newestBands - oldestBands);
260
261 for (int index = 0; index < 8; ++index) {
262 float age = float(index);
263 vec4 bands = historyBands(index);
264 vec3 field = energyField(p, age, bands);
265 float energy = clamp(abs(field.x) * 1.65 + bands.x * 0.30, 0.0, 3.5);
266 vec2 vectorField = clamp(field.yz * 1.35, vec2(-2.8), vec2(2.8));
267
268 // The field itself drives a layered geometric transform.
269 vec2 geometry = p;
270 float transformAngle =
271 age * 0.16 + bands.z * 1.35 + field.x * 0.24 + sin(clockTime() * 0.19 - age) * 0.12;
272 geometry = rotate2(geometry, transformAngle);
273
274 float shear = sin(clockTime() * 0.27 + age * 0.71 + bands.y * 4.0) * (0.16 + energy * 0.09);
275 geometry = mat2(1.0, shear, -shear * 0.72, 1.0) * geometry;
276
277 float bendFrequency = 4.5 + bands.z * 10.0 + age * 0.55;
278 vec2 bend = vec2(sin(geometry.y * bendFrequency + clockTime() * 0.8 + age),
279 cos(geometry.x * (bendFrequency * 0.87) - clockTime() * 0.67 + age * 0.6));
280 geometry += bend * (0.045 + energy * 0.035 + bands.y * 0.075);
281
282 float recursiveScale =
283 1.0 + sin(clockTime() * 0.31 - age * 0.38) * 0.12 - age * 0.026 - bands.x * 0.10;
284 geometry *= recursiveScale;
285
286 float foldMix = smoothstep(0.28, 1.10, energy);
287 vec2 folded = abs(geometry) - vec2(0.24 + bands.x * 0.10, 0.20 + bands.y * 0.08);
288 folded = rotate2(folded, -transformAngle * 0.63 + field.x * 0.18);
289 geometry = mix(geometry, folded, foldMix * 0.58);
290
291 vec2 historyUV = geometry / aspectScale + 0.5;
292 historyUV += vectorField / aspectScale * (0.024 + bands.y * 0.060 + bands.z * 0.035);
293 historyUV = mirrorUV(historyUV);
294
295 vec2 echoOffset = rotate2(vectorField, transformAngle + 1.5707963) / aspectScale *
296 (0.008 + energy * 0.012 + bands.w * 0.018);
297 vec3 cachedPrimary = cacheFrame(index, historyUV).rgb;
298 vec3 cachedEcho = cacheFrame(index, mirrorUV(historyUV + echoOffset)).bgr;
299 vec3 cached = mix(cachedPrimary, cachedEcho, 0.20 + bands.z * 0.24);
300
301 vec3 fieldColor =
302 palette(energy * 0.31 + age * 0.103 + bands.w * 0.42 + clockTime() * 0.018);
303 float emissive = pow(clamp(energy, 0.0, 1.35), 1.45);
304 float geometryLine =
305 pow(0.5 + 0.5 * sin(dot(geometry, vec2(9.0 + age, 7.0 + bands.y * 8.0)) + energy * TAU -
306 clockTime() * 1.3),
307 7.0);
308 vec3 layer = mix(cached, cached * fieldColor * 1.62, 0.76);
309 layer += fieldColor * emissive * (0.48 + bands.z * 1.35);
310 layer += palette(fieldColor.r + age * 0.07) * geometryLine * (0.18 + energy * 0.30);
311
312 float weight = pow(0.84, age) * (0.70 + bands.x * 1.05 + bands.y * 0.62 + energy * 0.12);
313 accumulation += layer * weight;
314 totalWeight += weight;
315 }
316
317 vec3 result = accumulation / max(totalWeight, 0.001);
318 vec3 currentField = energyField(p, 0.0, newestBands);
319 float currentEnergy = clamp(abs(currentField.x) * 1.75 + newestBands.x * 0.25, 0.0, 2.0);
320 float edgeEnergy = fwidth(currentField.x);
321 vec3 currentColor = palette(currentEnergy * 0.39 + temporalChange * 0.34 + clockTime() * 0.020);
322
323 float aura = pow(clamp(currentEnergy, 0.0, 1.0), 1.65);
324 result = 1.0 - (1.0 - result) * (1.0 - clamp(currentColor * aura * 0.78, 0.0, 0.94));
325 result += currentColor * aura * (0.32 + newestBands.y * 0.48);
326 result += currentColor * edgeEnergy * (3.2 + newestBands.w * 5.0);
327
328 float pulseGeometry =
329 pow(0.5 + 0.5 * sin(length(p) * 32.0 + currentField.x * 9.0 - clockTime() * 2.1), 8.0);
330 result += palette(currentEnergy + 0.23) * pulseGeometry * (0.14 + newestBands.z * 0.42);
331
332 float luminance = dot(result, vec3(0.299, 0.587, 0.114));
333 result = mix(vec3(luminance), result, 1.28);
334 result *= 1.12 + clamp(amp_smooth, 0.0, 1.0) * 0.42;
335
336 result = toneMap(result);
337 float inversion =
338 smoothstep(0.48, 0.88, amp_peak) * (0.34 + 0.66 * smoothstep(0.04, 0.42, temporalChange));
339 vec3 inverted = 1.0 - result;
340 inverted = mix(inverted, inverted.bgr, 0.28 + newestBands.z * 0.42);
341 result = mix(result, inverted, inversion * 0.94);
342 color = vec4(clamp(result, 0.0, 1.0), texture(samp, tc).a);
343}
layout(set=0, binding=1, std140) uniform SpriteExtended
Definition 3high.frag:7
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
float fbm(vec2 point)
Evaluates fbm for this shader effect.
#define amp_smooth
#define CACHE_HISTORY_LAYER(index)
vec3 palette(float phase)
Evaluates palette for this shader effect.
#define SPECTRUM_HISTORY_LAYER(index)
#define amp_peak
float energy()
Evaluates energy for this shader effect.
vec2 mirrorUV(vec2 uv)
Evaluates mirrorUV for this shader effect.
vec2 hash22(vec2 p)
Evaluates hash22 for this shader effect.
float hash21(vec2 p)
Evaluates hash21 for this shader effect.
float field(vec2 p, float tm)
Evaluates field for this shader effect.
float luminance(vec3 c)
Evaluates luminance for this shader effect.
float noise21(vec2 p)
Evaluates noise21 for this shader effect.
vec2 cell(vec2 p)
Evaluates cell for this shader effect.
#define TAU
#define PI
vec4 historyBands(int index)
Evaluates historyBands for this shader effect.
vec4 cacheFrame(int index, vec2 uv)
Evaluates cacheFrame for this shader effect.
vec2 rotate2(vec2 p, float angle)
Evaluates rotate2 for this shader effect.
float spectrumHistory(int index, float frequency)
Evaluates spectrumHistory for this shader effect.
vec3 toneMap(vec3 value)
Evaluates toneMap for this shader effect.
float clockTime()
Evaluates clockTime for this shader effect.
float segmentDistance(vec2 p, vec2 a, vec2 b)
Evaluates segmentDistance for this shader effect.
vec3 energyField(vec2 p, float age, vec4 band)
Evaluates energyField for this shader effect.
#define frequency
Definition ol.frag:13