ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
Electric_Fold.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file Electric_Fold.frag
5 * @brief Vulkan fragment shader: Electric Fold.
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.0;
32const float iContrast = 1.0;
33const float iSaturation = 1.0;
34const float iHueShift = 0.0;
35const float iZoom = 1.0;
36const float iRotation = 0.0;
37
38// --- COLOR HELPERS ---
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 * @brief Evaluates rotateHue for this shader effect.
66 * @param col Input value for the calculation.
67 * @param angle Input value for the calculation.
68 * @return Computed vec3 value.
69 */
70vec3 rotateHue(vec3 col, float angle) {
71 float U = cos(angle); float W = sin(angle);
72 mat3 R = mat3(
73 0.299+0.701*U+0.168*W, 0.587-0.587*U+0.330*W, 0.114-0.114*U-0.497*W,
74 0.299-0.299*U-0.328*W, 0.587+0.413*U+0.035*W, 0.114-0.114*U+0.292*W,
75 0.299-0.300*U+1.250*W, 0.587-0.588*U-1.050*W, 0.114+0.886*U-0.203*W
76 );
77 return clamp(R * col, 0.0, 1.0);
78}
79/**
80 * @brief Evaluates applyColorAdjustments for this shader effect.
81 * @param col Input value for the calculation.
82 * @return Computed vec3 value.
83 */
84vec3 applyColorAdjustments(vec3 col) {
85 col = adjustBrightness(col, iBrightness);
86 col = adjustContrast(col, iContrast);
87 col = adjustSaturation(col, iSaturation);
88 col = rotateHue(col, iHueShift);
89 return clamp(col, 0.0, 1.0);
90}
91
92// Cosine Palette for the "Electric" glow
93/**
94 * @brief Evaluates palette for this shader effect.
95 * @param t Input value for the calculation.
96 * @return Computed vec3 value.
97 */
98vec3 palette(float t) {
99 vec3 a = vec3(0.5, 0.5, 0.5);
100 vec3 b = vec3(0.5, 0.5, 0.5);
101 vec3 c = vec3(1.0, 1.0, 1.0);
102 vec3 d = vec3(0.263, 0.416, 0.557);
103 return a + b * cos(6.28318 * (c * t + d + iHueShift));
104}
105
106// --- COORDINATE HELPERS ---
107
108/**
109 * @brief Evaluates rotate2D for this shader effect.
110 * @param p Input value for the calculation.
111 * @param a Input value for the calculation.
112 * @return Computed vec2 value.
113 */
114vec2 rotate2D(vec2 p, float a) {
115 float c = cos(a); float s = sin(a);
116 return mat2(c, -s, s, c) * p;
117}
118
119/**
120 * @brief Evaluates wrapUV for this shader effect.
121 * @param tc Input value for the calculation.
122 * @return Computed vec2 value.
123 */
124vec2 wrapUV(vec2 tc) {
125 return 1.0 - abs(1.0 - 2.0 * fract(tc * 0.5));
126}
127
128/**
129 * @brief Evaluates mxTexture for this shader effect.
130 * @param tex Input value for the calculation.
131 * @param tc Input value for the calculation.
132 * @return Computed vec4 value.
133 */
134vec4 mxTexture(sampler2D tex, vec2 tc) {
135 vec2 ts = vec2(textureSize(tex, 0));
136 vec2 eps = 0.5 / ts;
137 vec2 uv = wrapUV(tc);
138 vec2 sampleUV = clamp(uv, eps, 1.0 - eps);
139 float lod = 0.0;
140 return textureLod(tex, sampleUV, lod);
141}
142
143// --- NOISE & FBM ---
144
145/**
146 * @brief Evaluates hash for this shader effect.
147 * @param p Input value for the calculation.
148 * @return Computed float value.
149 */
150float hash(vec2 p) {
151 return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
152}
153
154/**
155 * @brief Evaluates noise for this shader effect.
156 * @param p Input value for the calculation.
157 * @return Computed float value.
158 */
159float noise(vec2 p) {
160 vec2 i = floor(p);
161 vec2 f = fract(p);
162 vec2 u = f * f * (3.0 - 2.0 * f);
163 float a = hash(i + vec2(0.0, 0.0));
164 float b = hash(i + vec2(1.0, 0.0));
165 float c = hash(i + vec2(0.0, 1.0));
166 float d = hash(i + vec2(1.0, 1.0));
167 return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
168}
169
170// Combined FBM: 'ridges' true = Electricity, 'ridges' false = Soft Clouds
171/**
172 * @brief Evaluates fbm for this shader effect.
173 * @param p Input value for the calculation.
174 * @param ridges Input value for the calculation.
175 * @return Computed float value.
176 */
177float fbm(vec2 p, bool ridges) {
178 float v = 0.0;
179 float a = 0.5;
180 for (int i = 0; i < 4; i++) {
181 float n = noise(p);
182 if (ridges) n = 1.0 - abs(n * 2.0 - 1.0);
183 v += a * n;
184 p *= 2.1;
185 a *= 0.55;
186 }
187 return v;
188}
189
190/**
191 * @brief Evaluates kaleido for this shader effect.
192 * @param p Input value for the calculation.
193 * @param slices Input value for the calculation.
194 * @return Computed vec2 value.
195 */
196vec2 kaleido(vec2 p, float slices) {
197 float pi = 3.14159265359;
198 float r = length(p);
199 float a = atan(p.y, p.x);
200 float sector = pi * 2.0 / slices;
201 a = mod(a, sector);
202 a = abs(a - sector * 0.5);
203 return vec2(cos(a), sin(a)) * r;
204}
205
206// --- MERGED LOGIC ---
207
208/**
209 * @brief Evaluates sampleMerged for this shader effect.
210 * @param uv Input value for the calculation.
211 * @param t Input value for the calculation.
212 * @param strength Input value for the calculation.
213 * @param center Input value for the calculation.
214 * @param res Input value for the calculation.
215 * @return Computed vec3 value.
216 */
217vec3 sampleMerged(vec2 uv, float t, float strength, vec2 center, vec2 res) {
218 float aspect = res.x / res.y;
219 vec2 p = (uv - center) * vec2(aspect, 1.0);
220
221 // Initial Rotation from controls
222 p = rotate2D(p, iRotation);
223 float zoom = max(iZoom, 0.001);
224 p /= zoom;
225
226 // --- STAGE 1: FLUID SWIRL (From Your Code) ---
227 // We distort 'p' using a rotational noise field
228 float f1 = fbm(p * 2.0 + t * 0.2, false);
229 float f2 = fbm(p * 2.5 - t * 0.3, false);
230
231 // Convert to polar, twist angle based on noise, convert back
232 float rLen = length(p);
233 float ang = atan(p.y, p.x);
234 ang += (f1 * 3.0 + f2) * strength * 0.5; // The "Swirl" amount
235 p = vec2(cos(ang), sin(ang)) * rLen;
236
237 // --- STAGE 2: ELECTRIC DOMAIN WARP (From Previous Shader) ---
238 // Now we take that swirled coordinate and "melt" it with ridges
239 vec2 q = vec2(0.0);
240 q.x = fbm(p + vec2(0.0, 0.0) + t * 0.1, false);
241 q.y = fbm(p + vec2(5.2, 1.3) - t * 0.1, false);
242
243 vec2 r = vec2(0.0);
244 // Use 'ridges=true' here for the sharp electric look
245 r.x = fbm(p + 4.0 * q + vec2(t * 0.2, 9.2), true);
246 r.y = fbm(p + 4.0 * q + vec2(8.3, 2.8), false);
247
248 // Apply the warp
249 p += r * (0.15 * strength);
250
251 // --- STAGE 3: FRACTAL FOLDING (Geometric Loop) ---
252
253 // Kaleidoscope
254 float slices = 8.0 + 4.0 * sin(t * 0.2);
255 p = kaleido(p, slices);
256
257 // Box Folding Loop
258 int iterations = 4 + int(iAmplitude * 1.5);
259 float scale = 1.2 + (iFrequency * 0.2);
260 float shift = 0.1 * strength;
261 float rotAng = t * 0.2 + (f1 * 0.5); // Use the fluid noise 'f1' to modulate rotation
262
263 for(int i = 0; i < iterations; i++) {
264 p = abs(p);
265 p -= shift;
266 p *= scale;
267 p = rotate2D(p, rotAng + float(i)*0.4);
268 }
269
270 // --- COMPOSITION ---
271
272 // Map back to texture space
273 vec2 finalUV = p * 0.5 + center;
274
275 // Add chromatic aberration based on the swirl noise
276 vec2 chroma = vec2(0.005 * strength * f2, 0.0);
277
278 float red = mxTexture(samp, finalUV + chroma).r;
279 float grn = mxTexture(samp, finalUV).g;
280 float blu = mxTexture(samp, finalUV - chroma).b;
281 vec3 texCol = vec3(red, grn, blu);
282
283 // Generate Glow/Energy
284 // Combine the "Swirl" intensity (f1) with the "Electric" intensity (r)
285 float energy = (length(r) * 0.5 + f1 * 0.5);
286 float glow = pow(energy, 3.0) * iContrast;
287
288 vec3 pal = palette(length(p) + t * 0.5);
289
290 // Mix Texture + Palette + Glow
291 vec3 finalCol = mix(texCol, pal, 0.3 * iSaturation);
292 finalCol += pal * glow * strength;
293
294 return finalCol;
295}
296
297/**
298 * @brief Executes the fragment shader entry point.
299 */
300void main() {
301 vec2 uv = tc;
302 uv = wrapUV(uv);
303
304 float ampControl = clamp(iAmplitude, 0.0, 2.0);
305 float freqControl = clamp(iFrequency, 0.0, 2.0);
306
307 float t = time_f * (0.2 + freqControl * 0.3);
308 float strength = 0.5 + (ampControl * 1.0);
309
310 vec2 center = vec2(0.5);
311 if (iMouse.z > 0.0) {
312 center = iMouse.xy / iResolution;
313 }
314
315 // We blend two passes slightly offset in time for extra motion blur/smoothness
316 vec3 colA = sampleMerged(uv, t, strength, center, iResolution);
317
318 // Optional: Second pass for complexity (can remove for speed)
319 // vec3 colB = sampleMerged(uv + vec2(0.005), t + 0.5, strength, center, iResolution);
320 // colA = mix(colA, colB, 0.5);
321
322 color.rgb = applyColorAdjustments(colA);
323 color.a = 1.0;
324}
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.
vec3 applyColorAdjustments(vec3 col)
Evaluates applyColorAdjustments 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.
vec3 sampleMerged(vec2 uv, float t, float strength, vec2 center, vec2 res)
Evaluates sampleMerged for this shader effect.
const float iAmplitude
const float iSaturation
vec3 rotateHue(vec3 col, float angle)
Evaluates rotateHue for this shader effect.
const float iContrast
const float iHueShift
const float iZoom
float fbm(vec2 point)
Evaluates fbm for this shader effect.
vec3 palette(float phase)
Evaluates palette for this shader effect.
float hash(vec2 p)
Evaluates hash for this shader effect.
float energy()
Evaluates energy for this shader effect.
float noise(vec2 p)
Evaluates noise for this shader effect.
vec2 kaleido(vec2 p, float seg)
Evaluates kaleido for this shader effect.
vec3 pal(float t)
Evaluates pal for this shader effect.
mat2 R(float a)
Evaluates R for this shader effect.
float n(vec2 p)
Evaluates n for this shader effect.
float W(vec2 p)
Evaluates W for this shader effect.
mat2 rotate2D(float a)
Evaluates rotate2D for this shader effect.
#define strength
Definition ol.frag:16