ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
inversion_code_cache_resonance_cavity.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file inversion_code_cache_resonance_cavity.frag
5 * @brief Vulkan fragment shader: inversion code cache resonance cavity.
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: resonance cavity.
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.67;
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 float modeX = 3.0 + floor(band.x * 8.0);
235 float modeY = 4.0 + floor(band.y * 9.0);
236 float standingX = sin((p.x + 0.8) * PI * modeX + age * 0.2);
237 float standingY = sin((p.y + 0.6) * PI * modeY - age * 0.17);
238 float harmonic = cos((p.x - p.y) * PI * (2.0 + band.z * 5.0) - t * 1.4);
239 float energy = abs(standingX * standingY * harmonic);
240 vec2 flow = vec2(standingY * harmonic, -standingX * harmonic);
241 return vec3(energy, flow);
242}
243
244/**
245 * @brief Executes the fragment shader entry point.
246 */
247void main(void) {
248 float aspect = max(iResolution.x, 1.0) / max(iResolution.y, 1.0);
249 vec2 aspectScale = vec2(aspect, 1.0);
250 vec2 p = (tc - 0.5) * aspectScale;
251 vec3 live = texture(samp, tc).rgb;
252
253 vec3 accumulation = live * 0.10;
254 float totalWeight = 0.10;
255 vec4 newestBands = historyBands(0);
256 vec4 oldestBands = historyBands(7);
257 float temporalChange = length(newestBands - oldestBands);
258
259 for (int index = 0; index < 8; ++index) {
260 float age = float(index);
261 vec4 bands = historyBands(index);
262 vec3 field = energyField(p, age, bands);
263 float energy = clamp(abs(field.x) * 1.65 + bands.x * 0.30, 0.0, 3.5);
264 vec2 vectorField = clamp(field.yz * 1.35, vec2(-2.8), vec2(2.8));
265
266 // The field itself drives a layered geometric transform.
267 vec2 geometry = p;
268 float transformAngle =
269 age * 0.16 + bands.z * 1.35 + field.x * 0.24 + sin(clockTime() * 0.19 - age) * 0.12;
270 geometry = rotate2(geometry, transformAngle);
271
272 float shear = sin(clockTime() * 0.27 + age * 0.71 + bands.y * 4.0) * (0.16 + energy * 0.09);
273 geometry = mat2(1.0, shear, -shear * 0.72, 1.0) * geometry;
274
275 float bendFrequency = 4.5 + bands.z * 10.0 + age * 0.55;
276 vec2 bend = vec2(sin(geometry.y * bendFrequency + clockTime() * 0.8 + age),
277 cos(geometry.x * (bendFrequency * 0.87) - clockTime() * 0.67 + age * 0.6));
278 geometry += bend * (0.045 + energy * 0.035 + bands.y * 0.075);
279
280 float recursiveScale =
281 1.0 + sin(clockTime() * 0.31 - age * 0.38) * 0.12 - age * 0.026 - bands.x * 0.10;
282 geometry *= recursiveScale;
283
284 float foldMix = smoothstep(0.28, 1.10, energy);
285 vec2 folded = abs(geometry) - vec2(0.24 + bands.x * 0.10, 0.20 + bands.y * 0.08);
286 folded = rotate2(folded, -transformAngle * 0.63 + field.x * 0.18);
287 geometry = mix(geometry, folded, foldMix * 0.58);
288
289 vec2 historyUV = geometry / aspectScale + 0.5;
290 historyUV += vectorField / aspectScale * (0.024 + bands.y * 0.060 + bands.z * 0.035);
291 historyUV = mirrorUV(historyUV);
292
293 vec2 echoOffset = rotate2(vectorField, transformAngle + 1.5707963) / aspectScale *
294 (0.008 + energy * 0.012 + bands.w * 0.018);
295 vec3 cachedPrimary = cacheFrame(index, historyUV).rgb;
296 vec3 cachedEcho = cacheFrame(index, mirrorUV(historyUV + echoOffset)).bgr;
297 vec3 cached = mix(cachedPrimary, cachedEcho, 0.20 + bands.z * 0.24);
298
299 vec3 fieldColor =
300 palette(energy * 0.31 + age * 0.103 + bands.w * 0.42 + clockTime() * 0.018);
301 float emissive = pow(clamp(energy, 0.0, 1.35), 1.45);
302 float geometryLine =
303 pow(0.5 + 0.5 * sin(dot(geometry, vec2(9.0 + age, 7.0 + bands.y * 8.0)) + energy * TAU -
304 clockTime() * 1.3),
305 7.0);
306 vec3 layer = mix(cached, cached * fieldColor * 1.62, 0.76);
307 layer += fieldColor * emissive * (0.48 + bands.z * 1.35);
308 layer += palette(fieldColor.r + age * 0.07) * geometryLine * (0.18 + energy * 0.30);
309
310 float weight = pow(0.84, age) * (0.70 + bands.x * 1.05 + bands.y * 0.62 + energy * 0.12);
311 accumulation += layer * weight;
312 totalWeight += weight;
313 }
314
315 vec3 result = accumulation / max(totalWeight, 0.001);
316 vec3 currentField = energyField(p, 0.0, newestBands);
317 float currentEnergy = clamp(abs(currentField.x) * 1.75 + newestBands.x * 0.25, 0.0, 2.0);
318 float edgeEnergy = fwidth(currentField.x);
319 vec3 currentColor = palette(currentEnergy * 0.39 + temporalChange * 0.34 + clockTime() * 0.020);
320
321 float aura = pow(clamp(currentEnergy, 0.0, 1.0), 1.65);
322 result = 1.0 - (1.0 - result) * (1.0 - clamp(currentColor * aura * 0.78, 0.0, 0.94));
323 result += currentColor * aura * (0.32 + newestBands.y * 0.48);
324 result += currentColor * edgeEnergy * (3.2 + newestBands.w * 5.0);
325
326 float pulseGeometry =
327 pow(0.5 + 0.5 * sin(length(p) * 32.0 + currentField.x * 9.0 - clockTime() * 2.1), 8.0);
328 result += palette(currentEnergy + 0.23) * pulseGeometry * (0.14 + newestBands.z * 0.42);
329
330 float luminance = dot(result, vec3(0.299, 0.587, 0.114));
331 result = mix(vec3(luminance), result, 1.28);
332 result *= 1.12 + clamp(amp_smooth, 0.0, 1.0) * 0.42;
333
334 result = toneMap(result);
335 float inversion =
336 smoothstep(0.48, 0.88, amp_peak) * (0.34 + 0.66 * smoothstep(0.04, 0.42, temporalChange));
337 vec3 inverted = 1.0 - result;
338 inverted = mix(inverted, inverted.bgr, 0.28 + newestBands.z * 0.42);
339 result = mix(result, inverted, inversion * 0.94);
340 color = vec4(clamp(result, 0.0, 1.0), texture(samp, tc).a);
341}
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.
vec3 harmonic(float t)
Evaluates harmonic 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