ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
Liquid_Rainbow.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file Liquid_Rainbow.frag
5 * @brief Vulkan fragment shader: Liquid Rainbow.
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 iMouse ext.mouse
18#define iResolution ext.u0.zw
19#define time_f ext.u2.y
20
21layout(set = 0, binding = 0) uniform sampler2D samp;
22
23
24
25layout(location = 0) out vec4 color;
26layout(location = 0) in vec2 tc;
27
28// Controls
29const float iAmplitude = 1.0;
30const float iFrequency = 1.0;
31const float iBrightness = 1.2; // Slightly boosted for light effect
32const float iContrast = 1.1;
33const float iSaturation = 1.2;
34const float iHueShift = 0.0;
35const float iZoom = 1.0;
36const float iRotation = 0.0;
37
38// --- COLOR HELPER FUNCTIONS ---
39
40/**
41 * @brief Evaluates adjustBrightness for this shader effect.
42 * @param col Input value for the calculation.
43 * @param b Input value for the calculation.
44 * @return Computed vec3 value.
45 */
46vec3 adjustBrightness(vec3 col, float b) { return col * b; }
47/**
48 * @brief Evaluates adjustContrast for this shader effect.
49 * @param col Input value for the calculation.
50 * @param c Input value for the calculation.
51 * @return Computed vec3 value.
52 */
53vec3 adjustContrast(vec3 col, float c) { return (col - 0.5) * c + 0.5; }
54/**
55 * @brief Evaluates adjustSaturation for this shader effect.
56 * @param col Input value for the calculation.
57 * @param s Input value for the calculation.
58 * @return Computed vec3 value.
59 */
60vec3 adjustSaturation(vec3 col, float s) {
61 float gray = dot(col, vec3(0.299, 0.587, 0.114));
62 return mix(vec3(gray), col, s);
63}
64
65// IQ Cosine Palette - Tweaked for Full Rainbow
66/**
67 * @brief Evaluates palette for this shader effect.
68 * @param t Input value for the calculation.
69 * @return Computed vec3 value.
70 */
71vec3 palette(float t) {
72 vec3 a = vec3(0.5, 0.5, 0.5);
73 vec3 b = vec3(0.5, 0.5, 0.5);
74 vec3 c = vec3(1.0, 1.0, 1.0);
75 // This offset vector creates the rainbow spectrum
76 vec3 d = vec3(0.00, 0.33, 0.67);
77 return a + b * cos(6.28318 * (c * t + d + iHueShift));
78}
79
80// --- COORDINATE HELPERS ---
81
82/**
83 * @brief Evaluates wrapUV for this shader effect.
84 * @param tc Input value for the calculation.
85 * @return Computed vec2 value.
86 */
87vec2 wrapUV(vec2 tc) {
88 return 1.0 - abs(1.0 - 2.0 * fract(tc * 0.5));
89}
90
91/**
92 * @brief Evaluates mxTexture for this shader effect.
93 * @param tex Input value for the calculation.
94 * @param tc Input value for the calculation.
95 * @return Computed vec4 value.
96 */
97vec4 mxTexture(sampler2D tex, vec2 tc) {
98 vec2 ts = vec2(textureSize(tex, 0));
99 vec2 eps = 0.5 / ts;
100 vec2 uv = wrapUV(tc);
101 // Smooth wrapping prevents hard edges in the liquid
102 vec2 sampleUV = clamp(uv, eps, 1.0 - eps);
103 return texture(tex, sampleUV);
104}
105
106/**
107 * @brief Evaluates rotate2D for this shader effect.
108 * @param p Input value for the calculation.
109 * @param a Input value for the calculation.
110 * @return Computed vec2 value.
111 */
112vec2 rotate2D(vec2 p, float a) {
113 float c = cos(a); float s = sin(a);
114 return mat2(c, -s, s, c) * p;
115}
116
117// --- NOISE ---
118
119/**
120 * @brief Evaluates hash for this shader effect.
121 * @param p Input value for the calculation.
122 * @return Computed float value.
123 */
124float hash(vec2 p) {
125 return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
126}
127
128/**
129 * @brief Evaluates noise for this shader effect.
130 * @param p Input value for the calculation.
131 * @return Computed float value.
132 */
133float noise(vec2 p) {
134 vec2 i = floor(p);
135 vec2 f = fract(p);
136 vec2 u = f * f * (3.0 - 2.0 * f);
137 float a = hash(i + vec2(0.0, 0.0));
138 float b = hash(i + vec2(1.0, 0.0));
139 float c = hash(i + vec2(0.0, 1.0));
140 float d = hash(i + vec2(1.0, 1.0));
141 return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
142}
143
144// Standard FBM (Smooth, not ridged, for liquid feel)
145/**
146 * @brief Evaluates fbm for this shader effect.
147 * @param p Input value for the calculation.
148 * @return Computed float value.
149 */
150float fbm(vec2 p) {
151 float v = 0.0;
152 float a = 0.5;
153 mat2 rot = mat2(cos(0.5), sin(0.5), -sin(0.5), cos(0.50));
154 for (int i = 0; i < 5; i++) {
155 v += a * noise(p);
156 p = rot * p * 2.0 + vec2(2.0); // Rotate to prevent axis artifacts
157 a *= 0.5;
158 }
159 return v;
160}
161
162// --- LIQUID LIGHT LOGIC ---
163
164/**
165 * @brief Evaluates sampleLiquid for this shader effect.
166 * @param uv Input value for the calculation.
167 * @param t Input value for the calculation.
168 * @param strength Input value for the calculation.
169 * @param center Input value for the calculation.
170 * @param res Input value for the calculation.
171 * @return Computed vec3 value.
172 */
173vec3 sampleLiquid(vec2 uv, float t, float strength, vec2 center, vec2 res) {
174 float aspect = res.x / res.y;
175 vec2 p = (uv - center) * vec2(aspect, 1.0);
176
177 // 1. Initial Transform
178 p = rotate2D(p, iRotation);
179 float zoom = max(iZoom, 0.1);
180 p /= zoom;
181
182 // 2. RECURSIVE DOMAIN WARPING (The Liquid Core)
183 // We warp the coordinates (q), then warp the warp (r)
184
185 vec2 q = vec2(0.0);
186 q.x = fbm(p + vec2(0.0, 0.0) + 0.05*t);
187 q.y = fbm(p + vec2(5.2, 1.3) + 0.05*t);
188
189 vec2 r = vec2(0.0);
190 // Warp 'r' using 'q'
191 r.x = fbm(p + 4.0*q + vec2(t * 0.15));
192 r.y = fbm(p + 4.0*q + vec2(t * 0.05, 2.8));
193
194 // 'f' is the final noise value used for coloring intensity
195 float f = fbm(p + 4.0*r);
196
197 // 3. TEXTURE SAMPLING
198 // We use the warped vector 'r' to displace the texture UVs.
199 // This makes the image look like it's reflecting in flowing water.
200 vec2 fluidUV = uv + (r * 0.2 * strength * iAmplitude);
201 vec3 texCol = mxTexture(samp, fluidUV).rgb;
202
203 // 4. COLORING (Rainbow Light)
204
205 // Create the rainbow based on the noise magnitude and time
206 // length(q) gives us the "bands" of the oil spill
207 vec3 rainbow = palette(length(q) + f + t * 0.2);
208
209 // 5. COMPOSITION
210
211 // Mix the texture with the rainbow
212 // The texture acts as the "base matter", the rainbow is the "light"
213 vec3 col = mix(texCol, texCol * rainbow * 1.5, 0.6 * iSaturation);
214
215 // Add specular highlights (The "Liquid Light" shine)
216 // When the curvature (f) is high, we add pure light
217 float shine = f * f * f * 1.5 * iContrast;
218 col += shine * rainbow * strength;
219
220 // Deepen the darks to make the lights pop
221 col *= 0.8 + 0.5 * f;
222
223 return col;
224}
225
226/**
227 * @brief Executes the fragment shader entry point.
228 */
229void main() {
230 vec2 uv = tc;
231 uv = wrapUV(uv);
232
233 // Slow down time for viscous liquid feel
234 float t = time_f * (0.1 + iFrequency * 0.1);
235
236 float ampControl = clamp(iAmplitude, 0.0, 2.0);
237 float strength = 1.0 + (ampControl * 0.5);
238
239 vec2 center = vec2(0.5);
240 if (iMouse.z > 0.0) {
241 center = iMouse.xy / iResolution;
242 }
243
244 // Chromatic Aberration
245 // We separate the channels slightly based on the noise intensity
246 vec2 offset = vec2(0.005 * strength, 0.0);
247
248 vec3 col;
249 col.r = sampleLiquid(uv + offset, t, strength, center, iResolution).r;
250 col.g = sampleLiquid(uv, t, strength, center, iResolution).g;
251 col.b = sampleLiquid(uv - offset, t, strength, center, iResolution).b;
252
253 // Post Processing
254 col = adjustBrightness(col, iBrightness);
255 col = adjustContrast(col, iContrast);
256 col = adjustSaturation(col, iSaturation);
257
258 // Vignette
259 vec2 vUV = uv * (1.0 - uv.yx);
260 float vig = vUV.x * vUV.y * 15.0;
261 vig = pow(vig, 0.15);
262 col *= vig;
263
264 color = vec4(col, 1.0);
265}
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
vec4 mxTexture(sampler2D tex, vec2 tc)
Evaluates mxTexture for this shader effect.
const float iFrequency
const float iRotation
vec3 adjustBrightness(vec3 col, float b)
Evaluates adjustBrightness for this shader effect.
const float iBrightness
vec2 wrapUV(vec2 tc)
Evaluates wrapUV for this shader effect.
vec3 adjustSaturation(vec3 col, float s)
Evaluates adjustSaturation for this shader effect.
vec3 adjustContrast(vec3 col, float c)
Evaluates adjustContrast for this shader effect.
const float iAmplitude
const float iSaturation
const float iContrast
const float iHueShift
const float iZoom
vec3 sampleLiquid(vec2 uv, float t, float strength, vec2 center, vec2 res)
Evaluates sampleLiquid for this shader effect.
float fbm(vec2 point)
Evaluates fbm for this shader effect.
vec3 palette(float phase)
Evaluates palette for this shader effect.
vec2 rot(vec2 v, float a)
Evaluates rot for this shader effect.
Definition air-bowl.frag:52
float hash(vec2 p)
Evaluates hash for this shader effect.
float noise(vec2 p)
Evaluates noise for this shader effect.
vec3 rainbow(float t)
Evaluates rainbow for this shader effect.
mat2 rotate2D(float a)
Evaluates rotate2D for this shader effect.
#define strength
Definition ol.frag:16