ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
inthesky_cache_astral_tessellation.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file inthesky_cache_astral_tessellation.frag
5 * @brief Vulkan fragment shader: inthesky cache astral tessellation.
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 ext.u1.y
18#define amp_peak ext.u2.w
19#define amp_smooth ext.u3.w
20#define history_head int(ext.u3.x)
21#define iMouse ext.mouse
22#define iResolution ext.u0.zw
23#define iTime ext.u0.y
24#define spectrum_history_head int(ext.audio_history.x)
25#define spectrum_history_size int(ext.audio_history.y)
26#define time_f ext.u2.y
27
28
29// History-manipulated in-the-sky effect: astral tessellation.
30layout(location = 0) in vec2 tc;
31layout(location = 0) out vec4 color;
32layout(set = 0, binding = 0) uniform sampler2D samp;
33layout(set = 0, binding = 2) uniform sampler2DArray history;
34
35#ifndef SIZE
36#define SIZE 8
37#endif
38#ifndef CACHE_HISTORY_LAYER
39#define CACHE_HISTORY_LAYER(index) ((history_head + (index)) % SIZE)
40#endif
41layout(set = 0, binding = 3) uniform sampler1D spectrum0;
42layout(set = 0, binding = 4) uniform sampler1DArray spectrum_history;
43
44
45#ifndef SPECTRUM_HISTORY_LAYER
46#define SPECTRUM_HISTORY_LAYER(index) ((spectrum_history_head - ((index) % max(spectrum_history_size, 1)) + max(spectrum_history_size, 1)) % max(spectrum_history_size, 1))
47#endif
48
49
50
51
52
53
54
55
56const float TAU = 6.28318530718;
57const float SEGMENTS = 16.0;
58const float HUE_OFFSET = 0.44;
59const float INTENSITY = 1.42;
60const float PERSISTENCE = 0.82;
61
62/**
63 * @brief Evaluates clockTime for this shader effect.
64 * @return Computed float value.
65 */
66float clockTime() {
67 return max(time_f, iTime);
68}
69
70/**
71 * @brief Evaluates hash21 for this shader effect.
72 * @param p Input value for the calculation.
73 * @return Computed float value.
74 */
75float hash21(vec2 p) {
76 return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
77}
78
79/**
80 * @brief Evaluates hash22 for this shader effect.
81 * @param p Input value for the calculation.
82 * @return Computed vec2 value.
83 */
84vec2 hash22(vec2 p) {
85 return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453123);
86}
87
88/**
89 * @brief Evaluates noise21 for this shader effect.
90 * @param p Input value for the calculation.
91 * @return Computed float value.
92 */
93float noise21(vec2 p) {
94 vec2 cell = floor(p);
95 vec2 local = fract(p);
96 local = local * local * (3.0 - 2.0 * local);
97 return mix(mix(hash21(cell), hash21(cell + vec2(1.0, 0.0)), local.x),
98 mix(hash21(cell + vec2(0.0, 1.0)), hash21(cell + 1.0), local.x), local.y);
99}
100
101/**
102 * @brief Evaluates fbm for this shader effect.
103 * @param p Input value for the calculation.
104 * @return Computed float value.
105 */
106float fbm(vec2 p) {
107 float value = 0.0;
108 float weight = 0.5;
109 mat2 basis = mat2(0.80, -0.60, 0.60, 0.80);
110 for (int octave = 0; octave < 5; ++octave) {
111 value += noise21(p) * weight;
112 p = basis * p * 2.04 + vec2(1.8, 2.4);
113 weight *= 0.5;
114 }
115 return value;
116}
117
118/**
119 * @brief Evaluates rotate2 for this shader effect.
120 * @param p Input value for the calculation.
121 * @param angle Input value for the calculation.
122 * @return Computed vec2 value.
123 */
124vec2 rotate2(vec2 p, float angle) {
125 float cosine = cos(angle);
126 float sine = sin(angle);
127 return mat2(cosine, -sine, sine, cosine) * p;
128}
129
130/**
131 * @brief Evaluates mirrorUV for this shader effect.
132 * @param uv Input value for the calculation.
133 * @return Computed vec2 value.
134 */
135vec2 mirrorUV(vec2 uv) {
136 return 1.0 - abs(mod(uv, 2.0) - 1.0);
137}
138
139/**
140 * @brief Evaluates kaleidoUV for this shader effect.
141 * @param uv Input value for the calculation.
142 * @param segments Input value for the calculation.
143 * @param center Input value for the calculation.
144 * @param aspect Input value for the calculation.
145 * @return Computed vec2 value.
146 */
147vec2 kaleidoUV(vec2 uv, float segments, vec2 center, float aspect) {
148 vec2 p = (uv - center) * vec2(aspect, 1.0);
149 float angle = atan(p.y, p.x);
150 float radius = length(p);
151 float sector = TAU / segments;
152 angle = abs(mod(angle + sector * 0.5, sector) - sector * 0.5);
153 vec2 folded = vec2(cos(angle), sin(angle)) * radius;
154 return folded / vec2(aspect, 1.0) + center;
155}
156
157/**
158 * @brief Evaluates fractalSkyFold for this shader effect.
159 * @param uv Input value for the calculation.
160 * @param zoom Input value for the calculation.
161 * @param rotation Input value for the calculation.
162 * @param center Input value for the calculation.
163 * @param aspect Input value for the calculation.
164 * @return Computed vec2 value.
165 */
166vec2 fractalSkyFold(vec2 uv, float zoom, float rotation, vec2 center, float aspect) {
167 vec2 p = uv;
168 for (int iteration = 0; iteration < 5; ++iteration) {
169 float fi = float(iteration);
170 p = abs((p - center) * (zoom + fi * 0.035)) - 0.5 + center;
171 vec2 scaled = (p - center) * vec2(aspect, 1.0);
172 scaled = rotate2(scaled, rotation + fi * 0.075);
173 p = scaled / vec2(aspect, 1.0) + center;
174 }
175 return p;
176}
177
178/**
179 * @brief Evaluates skyPalette for this shader effect.
180 * @param phase Input value for the calculation.
181 * @return Computed vec3 value.
182 */
183vec3 skyPalette(float phase) {
184 vec3 wave = 0.5 + 0.5 * cos(TAU * (phase + HUE_OFFSET + vec3(0.00, 0.31, 0.67)));
185 return clamp((wave - 0.08) * 1.18, 0.0, 1.0);
186}
187
188/**
189 * @brief Evaluates toneMap for this shader effect.
190 * @param value Input value for the calculation.
191 * @return Computed vec3 value.
192 */
193vec3 toneMap(vec3 value) {
194 value = max(value, 0.0);
195 return clamp((value * (2.51 * value + 0.03)) / (value * (2.43 * value + 0.59) + 0.14), 0.0,
196 1.0);
197}
198
199/**
200 * @brief Evaluates cacheFrame for this shader effect.
201 * @param index Input value for the calculation.
202 * @param uv Input value for the calculation.
203 * @return Computed vec4 value.
204 */
205vec4 cacheFrame(int index, vec2 uv) {
206 if (index == 0)
207 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(0))));
208 if (index == 1)
209 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(1))));
210 if (index == 2)
211 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(2))));
212 if (index == 3)
213 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(3))));
214 if (index == 4)
215 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(4))));
216 if (index == 5)
217 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(5))));
218 if (index == 6)
219 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(6))));
220 return texture(history, vec3(uv, float(CACHE_HISTORY_LAYER(7))));
221}
222
223/**
224 * @brief Evaluates spectrumHistory for this shader effect.
225 * @param index Input value for the calculation.
226 * @param frequency Input value for the calculation.
227 * @return Computed float value.
228 */
229float spectrumHistory(int index, float frequency) {
230 if (index == 0)
231 return texture(spectrum0, frequency).r;
232 if (index == 1)
233 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(1)))).r;
234 if (index == 2)
235 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(2)))).r;
236 if (index == 3)
237 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(3)))).r;
238 if (index == 4)
239 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(4)))).r;
240 if (index == 5)
241 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(5)))).r;
242 if (index == 6)
243 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(6)))).r;
244 return texture(spectrum_history, vec2(frequency, float(SPECTRUM_HISTORY_LAYER(7)))).r;
245}
246
247/**
248 * @brief Evaluates historyBands for this shader effect.
249 * @param index Input value for the calculation.
250 * @return Computed vec4 value.
251 */
252vec4 historyBands(int index) {
253 return vec4(spectrumHistory(index, 0.04), spectrumHistory(index, 0.22),
254 spectrumHistory(index, 0.58), spectrumHistory(index, 0.86));
255}
256
257/**
258 * @brief Evaluates skyField for this shader effect.
259 * @param p Input value for the calculation.
260 * @param age Input value for the calculation.
261 * @param band Input value for the calculation.
262 * @return Computed vec3 value.
263 */
264vec3 skyField(vec2 p, float age, vec4 band) {
265 float t = clockTime();
266 vec2 grid = p * (5.0 + band.y * 4.0);
267 vec2 id = floor(grid);
268 vec2 local = fract(grid);
269 float nearest = 9.0;
270 float second = 9.0;
271 vec2 direction = vec2(0.0);
272 for (int y = -1; y <= 1; ++y) {
273 for (int x = -1; x <= 1; ++x) {
274 vec2 neighbor = vec2(float(x), float(y));
275 vec2 point = 0.5 + 0.38 * sin(TAU * hash22(id + neighbor) + t * 0.16 + age);
276 vec2 delta = neighbor + point - local;
277 float distanceToPoint = length(delta);
278 if (distanceToPoint < nearest) {
279 second = nearest;
280 nearest = distanceToPoint;
281 direction = delta;
282 } else if (distanceToPoint < second) {
283 second = distanceToPoint;
284 }
285 }
286 }
287 float edge = exp(-(second - nearest) * (20.0 + band.z * 36.0));
288 float star = exp(-nearest * (14.0 + band.w * 24.0));
289 return vec3(edge * 0.75 + star * 0.35, normalize(direction + vec2(0.001)) * edge);
290}
291
292/**
293 * @brief Executes the fragment shader entry point.
294 */
295void main(void) {
296 float aspect = max(iResolution.x, 1.0) / max(iResolution.y, 1.0);
297 vec2 aspectScale = vec2(aspect, 1.0);
298 vec2 mouseCenter = iMouse.z > 0.5 ? iMouse.xy / max(iResolution, vec2(1.0)) : vec2(0.5);
299 vec2 center = vec2(0.5) + (mouseCenter - 0.5) * (0.7 + amp * 0.25);
300 vec2 p = (tc - center) * aspectScale;
301 vec3 live = texture(samp, tc).rgb;
302
303 vec3 accumulation = live * 0.28;
304 float totalWeight = 0.28;
305 vec4 newest = historyBands(0);
306 vec4 oldest = historyBands(7);
307 float temporalChange = length(newest - oldest);
308
309 for (int index = 0; index < 8; ++index) {
310 float age = float(index);
311 vec4 bands = historyBands(index);
312 vec3 field = skyField(p, age, bands);
313 float energy = clamp(abs(field.x), 0.0, 2.2);
314 vec2 flow = clamp(field.yz, vec2(-2.0), vec2(2.0));
315
316 float segmentCount = SEGMENTS + 2.0 * floor(bands.z * 4.0);
317 vec2 historyUV = kaleidoUV(tc, segmentCount, center, aspect);
318 float zoom =
319 1.28 + bands.x * 0.42 + energy * 0.08 + 0.08 * sin(clockTime() * 0.22 - age * 0.18);
320 float rotation = clockTime() * (0.025 + bands.y * 0.08) + age * 0.035 + energy * 0.04;
321 historyUV = fractalSkyFold(historyUV, zoom, rotation, center, aspect);
322 historyUV += flow / aspectScale * (0.006 + bands.z * 0.018) * INTENSITY;
323 historyUV = mirrorUV(historyUV);
324
325 vec3 cached = cacheFrame(index, historyUV).rgb;
326 vec3 tint = skyPalette(energy * 0.22 + age * 0.081 + bands.w * 0.32 + clockTime() * 0.013);
327 float emission = pow(clamp(energy, 0.0, 1.0), 2.3);
328 vec3 layer = mix(cached, cached * tint * 1.32, 0.50);
329 layer += tint * emission * (0.16 + bands.z * 0.72);
330
331 float weight = pow(PERSISTENCE, age + 1.0) * (0.52 + bands.x * 0.76 + bands.y * 0.38);
332 accumulation += layer * weight;
333 totalWeight += weight;
334 }
335
336 vec3 result = accumulation / max(totalWeight, 0.001);
337 vec3 currentField = skyField(p, 0.0, newest);
338 float currentEnergy = clamp(abs(currentField.x), 0.0, 1.5);
339 vec3 energyColor =
340 skyPalette(currentEnergy * 0.28 + temporalChange * 0.24 + clockTime() * 0.015);
341 float crown = pow(clamp(currentEnergy, 0.0, 1.0), 2.6);
342 float edge = fwidth(currentField.x);
343
344 result = 1.0 - (1.0 - result) * (1.0 - clamp(energyColor * crown * 0.48, 0.0, 0.88));
345 result += energyColor * edge * (1.8 + newest.w * 3.2);
346 result *= 1.0 + clamp(amp_smooth + amp * 0.25, 0.0, 1.5) * 0.24;
347 result = toneMap(result);
348
349 float inversion =
350 smoothstep(0.72, 0.98, amp_peak) * smoothstep(0.06, 0.50, temporalChange) * 0.68;
351 vec3 inverted = mix(vec3(1.0) - result, (vec3(1.0) - result).gbr, 0.26);
352 result = mix(result, inverted, inversion);
353 color = vec4(clamp(result, 0.0, 1.0), texture(samp, tc).a);
354}
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
Definition abc.frag:17
float fbm(vec2 point)
Evaluates fbm for this shader effect.
#define amp_smooth
#define CACHE_HISTORY_LAYER(index)
#define SPECTRUM_HISTORY_LAYER(index)
#define amp_peak
vec3 wave(ivec2 p, ivec2 size)
Evaluates wave for this shader effect.
float energy()
Evaluates energy for this shader effect.
vec2 mirrorUV(vec2 uv)
Evaluates mirrorUV for this shader effect.
const float SEGMENTS
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 noise21(vec2 p)
Evaluates noise21 for this shader effect.
vec2 cell(vec2 p)
Evaluates cell for this shader effect.
#define TAU
vec3 skyPalette(float phase)
Evaluates skyPalette for this shader effect.
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.
vec2 fractalSkyFold(vec2 uv, float zoom, float rotation, vec2 center, float aspect)
Evaluates fractalSkyFold for this shader effect.
float spectrumHistory(int index, float frequency)
Evaluates spectrumHistory for this shader effect.
vec2 kaleidoUV(vec2 uv, float segments, vec2 center, float aspect)
Evaluates kaleidoUV for this shader effect.
vec3 toneMap(vec3 value)
Evaluates toneMap for this shader effect.
vec3 skyField(vec2 p, float age, vec4 band)
Evaluates skyField for this shader effect.
float clockTime()
Evaluates clockTime for this shader effect.
#define frequency
Definition ol.frag:13
mat2 rotation(float angle)
Evaluates rotation for this shader effect.