ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
swirl_cache.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file swirl_cache.frag
5 * @brief Vulkan fragment shader: swirl cache.
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 history_head int(ext.u3.x)
18#define iResolution ext.u0.zw
19#define time_f ext.u2.y
20
21layout(location = 0) out vec4 color;
22layout(location = 0) in vec2 tc; // The default texture coordinate passed from your vertex shader
23
24// Our five input textures
25layout(set = 0, binding = 0) uniform sampler2D samp; // base
26layout(set = 0, binding = 2) uniform sampler2DArray history;
27
28#ifndef SIZE
29#define SIZE 8
30#endif
31#ifndef CACHE_HISTORY_LAYER
32#define CACHE_HISTORY_LAYER(index) ((history_head + (index)) % SIZE)
33#endif
34
35// Screen resolution and time
36
37
38
39// ---------------------------------------------------
40// A helper function to shift hue by a normalized amount [0..1]
41vec3 hueShift(vec3 color, float shift)
42{
43 // 'shift' is in [0..1]; convert it to an angle in radians
44 float angle = 6.28318530718 * shift; // 2 * PI
45 float s = sin(angle);
46 float c = cos(angle);
47
48 // Precalculate constants for a typical hue rotation matrix
49 // For reference: https://en.wikipedia.org/wiki/HSL_and_HSV#Hue_and_chroma
50 mat3 toYCbCr = mat3(
51 0.299, 0.587, 0.114,
52 0.299, 0.587, 0.114,
53 0.299, 0.587, 0.114
54 );
55 mat3 adjust = mat3(
56 0.701, -0.587, -0.114,
57 -0.299, 0.413, -0.114,
58 -0.300, -0.588, 0.886
59 );
60 mat3 rot = mat3(
61 c, -s, 0.0,
62 s, c, 0.0,
63 0.0, 0.0, 1.0
64 );
65
66 // Combine
67 return (toYCbCr + adjust * rot) * color;
68}
69
70// ---------------------------------------------------
71// A helper function to apply a swirl-like distortion around a center point.
72// radius controls the region of swirling, angle is the swirl intensity.
73vec2 swirl(vec2 uv, vec2 center, float radius, float angle)
74{
75 vec2 pos = uv - center;
76 float r = length(pos);
77
78 // Only swirl if within the swirl's radius
79 if (r < radius) {
80 float percent = (radius - r) / radius;
81 float theta = atan(pos.y, pos.x) + angle * percent;
82 pos = r * vec2(cos(theta), sin(theta));
83 }
84 return pos + center;
85}
86
87// ---------------------------------------------------
88// Main fragment shader entry point
89void main(void)
90{
91 // Normalized UVs in [0..1], making sure we have a consistent orientation
92 vec2 uv = tc;
93
94 // You can play with swirling center in NDC or in pixel space:
95 // For example, use iResolution to find the center in pixel coordinates,
96 // then normalize by iResolution.
97 // Here we simply swirl around the center (0.5, 0.5) in [0..1].
98 vec2 center = vec2(0.5, 0.5);
99
100 // The swirl radius and intensity factor
101 float radius = 0.5; // swirling radius in [0..1]
102 float swirlStrength = 0.75; // swirl angle scale
103 float swirlAngle = swirlStrength * sin(time_f * 0.8);
104
105 // Apply swirl differently for each texture
106 // Add some wave-based warping as well
107 float waveX = sin(uv.y * 6.0 + time_f * 2.0) * 0.03;
108 float waveY = cos(uv.x * 6.0 + time_f * 2.0) * 0.03;
109
110 // Distorted UVs for each sampler
111 vec2 uv0 = swirl(uv + vec2(waveX, waveY), center, radius, swirlAngle );
112 vec2 uv1 = swirl(uv + vec2(waveY, waveX), center, radius, swirlAngle * 0.5 );
113 vec2 uv2 = swirl(uv - vec2(waveX, waveY), center, radius, -swirlAngle );
114 vec2 uv3 = swirl(uv + vec2(waveY * 2.0, -waveX * 2.0), center, radius, swirlAngle * 1.5);
115 vec2 uv4 = swirl(uv, center, radius, swirlAngle * 2.0);
116
117 // Sample from each texture with the distorted coordinates
118 vec4 base = texture(samp, uv0);
119 vec4 one = texture(history, vec3(uv1, float(CACHE_HISTORY_LAYER(0))));
120 vec4 two = texture(history, vec3(uv2, float(CACHE_HISTORY_LAYER(1))));
121 vec4 three = texture(history, vec3(uv3, float(CACHE_HISTORY_LAYER(2))));
122 vec4 four = texture(history, vec3(uv4, float(CACHE_HISTORY_LAYER(3))));
123
124 // Shift each texture’s hue by different amounts
125 // to create a rainbow-like blending
126 vec3 color0 = hueShift(base.rgb, 0.00);
127 vec3 color1 = hueShift(one.rgb, 0.20);
128 vec3 color2 = hueShift(two.rgb, 0.40);
129 vec3 color3 = hueShift(three.rgb, 0.60);
130 vec3 color4 = hueShift(four.rgb, 0.80);
131
132 // Combine them. You can adjust blending weights, saturate, etc. as desired.
133 vec3 combined = color0 + color1 + color2 + color3 + color4;
134 combined /= 5.0;
135
136 // Final output color
137 color = vec4(combined, 1.0);
138}
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 time_f
Definition 3high.frag:19
#define CACHE_HISTORY_LAYER(index)
vec2 rot(vec2 v, float a)
Evaluates rot for this shader effect.
Definition air-bowl.frag:52
vec2 swirl(vec2 uv, float centerRadius, float swirlAmount)
Evaluates swirl for this shader effect.
vec3 hueShift(vec3 col, float hue)
Evaluates hueShift for this shader effect.