ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
liquid_code_iridescent_whirlpool.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file liquid_code_iridescent_whirlpool.frag
5 * @brief Vulkan fragment shader: liquid code iridescent whirlpool.
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 iResolution ext.u0.zw
23#define time_f ext.u2.y
24
25layout(location = 0) in vec2 tc;
26layout(location = 0) out vec4 color;
27layout(set = 0, binding = 0) uniform sampler2D samp;
28
29
30
31
32
33
34
35
36const float PI = 3.14159265359;
37const float TAU = 6.28318530718;
38
39/**
40 * @brief Evaluates hash21 for this shader effect.
41 * @param p Input value for the calculation.
42 * @return Computed float value.
43 */
44float hash21(vec2 p) {
45 return fract(sin(dot(p, vec2(269.5, 183.3))) * 43758.5453);
46}
47
48/**
49 * @brief Evaluates noise21 for this shader effect.
50 * @param p Input value for the calculation.
51 * @return Computed float value.
52 */
53float noise21(vec2 p) {
54 vec2 i = floor(p);
55 vec2 f = fract(p);
56 vec2 u = f * f * (3.0 - 2.0 * f);
57 return mix(mix(hash21(i), hash21(i + vec2(1.0, 0.0)), u.x),
58 mix(hash21(i + vec2(0.0, 1.0)), hash21(i + vec2(1.0)), u.x), u.y);
59}
60
61/**
62 * @brief Evaluates turbulentFbm for this shader effect.
63 * @param p Input value for the calculation.
64 * @return Computed float value.
65 */
66float turbulentFbm(vec2 p) {
67 float value = 0.0;
68 float amplitude = 0.52;
69 mat2 rotation = mat2(0.58, -0.82, 0.82, 0.58);
70 for (int i = 0; i < 6; ++i) {
71 value += abs(noise21(p) * 2.0 - 1.0) * amplitude;
72 p = rotation * p * 2.07 + vec2(3.13, -2.41);
73 amplitude *= 0.48;
74 }
75 return value;
76}
77
78/**
79 * @brief Evaluates rotate2 for this shader effect.
80 * @param p Input value for the calculation.
81 * @param angle Input value for the calculation.
82 * @return Computed vec2 value.
83 */
84vec2 rotate2(vec2 p, float angle) {
85 float c = cos(angle);
86 float s = sin(angle);
87 return mat2(c, -s, s, c) * p;
88}
89
90/**
91 * @brief Evaluates mirrorUV for this shader effect.
92 * @param uv Input value for the calculation.
93 * @return Computed vec2 value.
94 */
95vec2 mirrorUV(vec2 uv) {
96 return 1.0 - abs(mod(uv, 2.0) - 1.0);
97}
98
99/**
100 * @brief Evaluates interference for this shader effect.
101 * @param phase Input value for the calculation.
102 * @param thickness Input value for the calculation.
103 * @return Computed vec3 value.
104 */
105vec3 interference(float phase, float thickness) {
106 vec3 wavelengths = vec3(1.0, 0.78, 0.58);
107 vec3 bands = 0.5 + 0.5 * cos(TAU * (phase + thickness * wavelengths) +
108 vec3(0.0, 0.9, 1.7));
109 return pow(bands, vec3(1.15));
110}
111
112/**
113 * @brief Evaluates whirlpoolHeight for this shader effect.
114 * @param p Input value for the calculation.
115 * @return Computed float value.
116 */
117float whirlpoolHeight(vec2 p) {
118 float t = time_f;
119 float radius = max(length(p), 0.018);
120 float angle = atan(p.y, p.x);
121 float tightening = log(radius + 0.035) * (7.8 + amp_mid * 0.9);
122 float phase = angle * 6.0 + tightening - t * 1.8;
123
124 vec2 q = rotate2(p, -log(radius + 0.05) * 0.55 + t * 0.08);
125 vec2 warp = vec2(turbulentFbm(q * 3.2 + vec2(t * 0.1, 0.0)),
126 turbulentFbm(q * 3.2 + vec2(0.0, -t * 0.12) + 5.8));
127 q += (warp - 0.48) * 0.19;
128 float filaments = turbulentFbm(q * 7.3 + vec2(t * 0.14, -t * 0.09));
129 float arms = sin(phase + filaments * 5.5) * 0.31;
130 float wake = sin(radius * 58.0 - angle * 3.0 - t * 5.1 + warp.x * 4.0) * 0.1;
131 float cross = cos(q.x * 36.0 + q.y * 19.0 + t * 3.8) * 0.055;
132 float funnel = -exp(-radius * 6.5) * 0.62;
133 return arms + wake + cross + filaments * 0.56 + funnel;
134}
135
136/**
137 * @brief Evaluates whirlpoolNormal for this shader effect.
138 * @param p Input value for the calculation.
139 * @param e Input value for the calculation.
140 * @return Computed vec3 value.
141 */
142vec3 whirlpoolNormal(vec2 p, float e) {
143 float left = whirlpoolHeight(p - vec2(e, 0.0));
144 float right = whirlpoolHeight(p + vec2(e, 0.0));
145 float down = whirlpoolHeight(p - vec2(0.0, e));
146 float up = whirlpoolHeight(p + vec2(0.0, e));
147 vec2 gradient = vec2(right - left, up - down) / (2.0 * e);
148 return normalize(vec3(-gradient * 0.105, 1.0));
149}
150
151/**
152 * @brief Evaluates refractedTexture for this shader effect.
153 * @param uv Input value for the calculation.
154 * @param tangent Input value for the calculation.
155 * @param normalFlow Input value for the calculation.
156 * @param split Input value for the calculation.
157 * @return Computed vec3 value.
158 */
159vec3 refractedTexture(vec2 uv, vec2 tangent, vec2 normalFlow, float split) {
160 vec2 direction = normalize(tangent + normalFlow * 0.7 + vec2(0.0001));
161 vec2 across = vec2(-direction.y, direction.x);
162 vec3 source = texture(samp, mirrorUV(uv)).rgb * 0.38;
163 source += texture(samp, mirrorUV(uv + direction * 0.003)).rgb * 0.2;
164 source += texture(samp, mirrorUV(uv - direction * 0.003)).rgb * 0.2;
165 source += texture(samp, mirrorUV(uv + across * 0.0045)).rgb * 0.11;
166 source += texture(samp, mirrorUV(uv - across * 0.0045)).rgb * 0.11;
167 return vec3(texture(samp, mirrorUV(uv + normalFlow * split + across * split)).r,
168 source.g,
169 texture(samp, mirrorUV(uv - normalFlow * split - across * split)).b);
170}
171
172/**
173 * @brief Evaluates anisotropicSpecular for this shader effect.
174 * @param n Input value for the calculation.
175 * @param v Input value for the calculation.
176 * @param l Input value for the calculation.
177 * @param tangent Input value for the calculation.
178 * @param roughness Input value for the calculation.
179 * @return Computed float value.
180 */
181float anisotropicSpecular(vec3 n, vec3 v, vec3 l, vec3 tangent, float roughness) {
182 vec3 halfVector = normalize(v + l);
183 vec3 bitangent = normalize(cross(n, tangent));
184 float th = dot(tangent, halfVector) / max(roughness, 0.04);
185 float bh = dot(bitangent, halfVector) / max(roughness * 2.4, 0.04);
186 float nh = max(dot(n, halfVector), 0.0);
187 return exp(-(th * th + bh * bh) / max(nh * nh, 0.015)) /
188 max(4.0 * PI * roughness * roughness, 0.01);
189}
190
191/**
192 * @brief Evaluates iridescentMetal for this shader effect.
193 * @param base Input value for the calculation.
194 * @param n Input value for the calculation.
195 * @param p Input value for the calculation.
196 * @param thickness Input value for the calculation.
197 * @param roughness Input value for the calculation.
198 * @return Computed vec3 value.
199 */
200vec3 iridescentMetal(vec3 base, vec3 n, vec2 p, float thickness, float roughness) {
201 vec3 view = normalize(vec3(-p * 0.13, 1.45));
202 vec3 light0 = normalize(vec3(-0.63, 0.51, 0.69));
203 vec3 light1 = normalize(vec3(0.72, 0.24, 0.65));
204 vec3 light2 = normalize(vec3(0.04, -0.76, 0.65));
205 vec3 tangent = normalize(vec3(-n.y, n.x, 0.08));
206 float nv = max(dot(n, view), 0.0);
207 vec3 f0 = mix(vec3(0.67), base, 0.5);
208 vec3 fresnel = f0 + (1.0 - f0) * pow(1.0 - nv, 5.0);
209 vec3 film = interference((1.0 - nv) * 0.72 + time_f * 0.012, thickness);
210
211 vec3 result = base * 0.04;
212 result += base * max(dot(n, light0), 0.0) * 0.07;
213 result += anisotropicSpecular(n, view, light0, tangent, roughness) *
214 vec3(1.0, 0.74, 0.5) * 0.72;
215 result += anisotropicSpecular(n, view, light1, tangent, roughness * 1.25) *
216 vec3(0.28, 0.58, 1.0) * 0.62;
217 result += anisotropicSpecular(n, view, light2, tangent, roughness * 1.7) *
218 vec3(0.74, 0.24, 1.0) * 0.38;
219
220 vec3 reflected = reflect(-view, n);
221 vec3 environment = mix(vec3(0.02, 0.03, 0.062), film,
222 smoothstep(-0.6, 0.82, reflected.z));
223 return result + environment * fresnel * 1.45;
224}
225
226/**
227 * @brief Evaluates toneMap for this shader effect.
228 * @param value Input value for the calculation.
229 * @return Computed vec3 value.
230 */
231vec3 toneMap(vec3 value) {
232 value = max(value, 0.0);
233 return 1.0 - exp(-value * (1.06 + value * 0.11));
234}
235
236/**
237 * @brief Executes the fragment shader entry point.
238 */
239void main(void) {
240 float aspect = max(iResolution.x, 1.0) / max(iResolution.y, 1.0);
241 vec2 p = (tc - 0.5) * vec2(aspect, 1.0);
242 float e = 1.5 / max(max(iResolution.x, iResolution.y), 320.0);
243 float radius = max(length(p), 0.018);
244 float angle = atan(p.y, p.x);
245 float phase = angle * 6.0 + log(radius + 0.035) * 7.8 - time_f * 1.8;
246 float height = whirlpoolHeight(p);
247 vec3 n = whirlpoolNormal(p, e);
248
249 vec2 radial = p / radius;
250 vec2 tangent = vec2(-radial.y, radial.x);
251 float core = exp(-radius * 3.2);
252 vec2 spiralFlow = tangent * (0.022 + core * 0.058 + amp_low * 0.024);
253 spiralFlow -= radial * (0.006 + core * 0.018);
254 spiralFlow += n.xy * (0.017 + amp_smooth * 0.017);
255 vec2 uv = mirrorUV(tc + spiralFlow / vec2(aspect, 1.0));
256
257 float split = 0.0018 + amp_high * 0.0065 + core * 0.0015;
258 vec3 source = refractedTexture(uv, tangent, n.xy, split);
259 float luminance = dot(source, vec3(0.299, 0.587, 0.114));
260 vec3 alloy = mix(vec3(luminance) * vec3(0.78, 0.86, 0.98), source, 0.4);
261 float thickness = turbulentFbm(p * 5.1 + time_f * 0.035) + height * 0.12;
262 float roughness = mix(0.08, 0.26, turbulentFbm(p * 3.8 - time_f * 0.025));
263 vec3 result = iridescentMetal(alloy, n, p, thickness, roughness);
264
265 float filament = pow(0.5 + 0.5 * sin(phase + height * 8.0), 15.0);
266 result += interference(height * 0.08 + time_f * 0.018, thickness) * filament *
267 (0.38 + amp_peak * 1.35);
268 result = mix(result, result * (0.58 + source * 1.08), 0.22);
269 color = vec4(toneMap(result * (1.0 + amp_smooth * 0.3)), texture(samp, uv).a);
270}
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
#define amp_smooth
#define amp_mid
#define amp_low
#define amp_high
#define amp_peak
vec2 mirrorUV(vec2 uv)
Evaluates mirrorUV for this shader effect.
float hash21(vec2 p)
Evaluates hash21 for this shader effect.
float n(vec2 p)
Evaluates n for this shader effect.
vec2 warp(vec2 p, vec2 o, float k)
Evaluates warp for this shader effect.
float luminance(vec3 c)
Evaluates luminance for this shader effect.
float noise21(vec2 p)
Evaluates noise21 for this shader effect.
vec3 film(float x, float v)
Evaluates film for this shader effect.
#define TAU
#define PI
vec2 rotate2(vec2 p, float angle)
Evaluates rotate2 for this shader effect.
vec3 toneMap(vec3 value)
Evaluates toneMap for this shader effect.
vec3 refractedTexture(vec2 uv, vec2 direction, float amount)
Evaluates refractedTexture for this shader effect.
float whirlpoolHeight(vec2 p)
Evaluates whirlpoolHeight for this shader effect.
float anisotropicSpecular(vec3 n, vec3 v, vec3 l, vec3 tangent, float roughness)
Evaluates anisotropicSpecular for this shader effect.
vec3 whirlpoolNormal(vec2 p, float e)
Evaluates whirlpoolNormal for this shader effect.
float turbulentFbm(vec2 p)
Evaluates turbulentFbm for this shader effect.
vec3 iridescentMetal(vec3 base, vec3 n, vec2 p, float thickness, float roughness)
Evaluates iridescentMetal for this shader effect.
vec3 interference(float phase, float thickness)
Evaluates interference for this shader effect.
float alloy(vec2 p)
Evaluates alloy for this shader effect.
mat2 rotation(float angle)
Evaluates rotation for this shader effect.