ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
halluc_gem.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file halluc_gem.frag
5 * @brief Vulkan fragment shader: halluc gem.
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; // Input UVs from the 3D Mesh
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// --- 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) {
47 return col * b;
48}
49
50/**
51 * @brief Evaluates adjustContrast for this shader effect.
52 * @param col Input value for the calculation.
53 * @param c Input value for the calculation.
54 * @return Computed vec3 value.
55 */
56vec3 adjustContrast(vec3 col, float c) {
57 return (col - 0.5) * c + 0.5;
58}
59
60/**
61 * @brief Evaluates adjustSaturation for this shader effect.
62 * @param col Input value for the calculation.
63 * @param s Input value for the calculation.
64 * @return Computed vec3 value.
65 */
66vec3 adjustSaturation(vec3 col, float s) {
67 float gray = dot(col, vec3(0.299, 0.587, 0.114));
68 return mix(vec3(gray), col, s);
69}
70
71/**
72 * @brief Evaluates rotateHue for this shader effect.
73 * @param col Input value for the calculation.
74 * @param angle Input value for the calculation.
75 * @return Computed vec3 value.
76 */
77vec3 rotateHue(vec3 col, float angle) {
78 float U = cos(angle);
79 float W = sin(angle);
80 mat3 R = mat3(
81 0.299 + 0.701*U + 0.168*W,
82 0.587 - 0.587*U + 0.330*W,
83 0.114 - 0.114*U - 0.497*W,
84 0.299 - 0.299*U - 0.328*W,
85 0.587 + 0.413*U + 0.035*W,
86 0.114 - 0.114*U + 0.292*W,
87 0.299 - 0.300*U + 1.250*W,
88 0.587 - 0.588*U - 1.050*W,
89 0.114 + 0.886*U - 0.203*W
90 );
91 return clamp(R * col, 0.0, 1.0);
92}
93
94/**
95 * @brief Evaluates applyColorAdjustments for this shader effect.
96 * @param col Input value for the calculation.
97 * @return Computed vec3 value.
98 */
99vec3 applyColorAdjustments(vec3 col) {
100 col = adjustBrightness(col, iBrightness);
101 col = adjustContrast(col, iContrast);
102 col = adjustSaturation(col, iSaturation);
103 col = rotateHue(col, iHueShift);
104 return clamp(col, 0.0, 1.0);
105}
106
107/**
108 * @brief Evaluates applyZoomRotation for this shader effect.
109 * @param uv Input value for the calculation.
110 * @param center Input value for the calculation.
111 * @return Computed vec2 value.
112 */
113vec2 applyZoomRotation(vec2 uv, vec2 center) {
114 vec2 p = uv - center;
115 float c = cos(iRotation);
116 float s = sin(iRotation);
117 p = mat2(c, -s, s, c) * p;
118 float z = max(abs(iZoom), 0.001);
119 p /= z;
120 return p + center;
121}
122
123// Triangle wave wrap function: turns 0..1 into a seamless mirror pattern
124/**
125 * @brief Evaluates wrapUV for this shader effect.
126 * @param tc Input value for the calculation.
127 * @return Computed vec2 value.
128 */
129vec2 wrapUV(vec2 tc) {
130 return 1.0 - abs(1.0 - 2.0 * fract(tc * 0.5));
131}
132
133/**
134 * @brief Evaluates mxTexture for this shader effect.
135 * @param tex Input value for the calculation.
136 * @param tc Input value for the calculation.
137 * @return Computed vec4 value.
138 */
139vec4 mxTexture(sampler2D tex, vec2 tc) {
140 vec2 ts = vec2(textureSize(tex, 0));
141 vec2 eps = 0.5 / ts;
142
143 vec2 uv = wrapUV(tc);
144 vec2 sampleUV = clamp(uv, eps, 1.0 - eps);
145
146 float lod = 0.0;
147 vec2 deriv = fwidth(tc);
148 if (deriv.x > 0.0 || deriv.y > 0.0) {
149 lod = log2(max(max(deriv.x, deriv.y) * max(ts.x, ts.y), 1.0));
150 }
151
152 return textureLod(tex, sampleUV, lod);
153}
154
155// --- Procedural Logic ---
156
157// Hard geometric rotation
158/**
159 * @brief Evaluates rotate2D for this shader effect.
160 * @param p Input value for the calculation.
161 * @param a Input value for the calculation.
162 * @return Computed vec2 value.
163 */
164vec2 rotate2D(vec2 p, float a) {
165 float c = cos(a);
166 float s = sin(a);
167 return mat2(c, -s, s, c) * p;
168}
169
170/**
171 * @brief Evaluates kaleido for this shader effect.
172 * @param p Input value for the calculation.
173 * @param slices Input value for the calculation.
174 * @return Computed vec2 value.
175 */
176vec2 kaleido(vec2 p, float slices) {
177 float pi = 3.14159265359;
178 float r = length(p);
179 float a = atan(p.y, p.x);
180 float sector = pi * 2.0 / slices;
181 a = mod(a, sector);
182 a = abs(a - sector * 0.5);
183 return vec2(cos(a), sin(a)) * r;
184}
185
186// Replaces the liquid warp with a hard iterative fold
187/**
188 * @brief Evaluates sampleFractal for this shader effect.
189 * @param uv Input value for the calculation.
190 * @param t Input value for the calculation.
191 * @param strength Input value for the calculation.
192 * @param center Input value for the calculation.
193 * @param res Input value for the calculation.
194 * @return Computed vec3 value.
195 */
196vec3 sampleFractal(vec2 uv, float t, float strength, vec2 center, vec2 res) {
197 float aspect = res.x / res.y;
198
199 // Scale controls for the folding intensity
200 float ampControl = clamp(iAmplitude, 0.0, 2.0);
201 float freqControl = clamp(iFrequency, 0.0, 2.0);
202
203 // Normalize UV to -1..1 range relative to center
204 vec2 p = (uv - center) * vec2(aspect, 1.0);
205
206 // 1. Initial Kaleidoscope (Outer Shape)
207 float slices = 6.0 + floor(ampControl * 4.0);
208 p = kaleido(p, slices);
209
210 // 2. Iterative Fractal Folding
211 // This loop creates the "repeating on itself" effect
212 // It creates copies of the coordinate space inside itself
213 int iterations = 4 + int(ampControl * 2.0);
214 float scale = 1.2 + (freqControl * 0.5);
215 float shift = 0.1 * strength;
216
217 // Slowly rotate the fractal over time
218 float angle = t * 0.1;
219
220 for(int i = 0; i < iterations; i++) {
221 p = abs(p); // Fold space (mirror)
222 p -= shift; // Shift away from center
223 p *= scale; // Scale up (recursion)
224 p = rotate2D(p, angle + float(i)*0.5); // Rotate each layer
225 }
226
227 // 3. Map back to Texture
228 // Add the center back to return to 0..1 space roughly
229 vec2 finalUV = p * 0.5 + center;
230
231 // Slight chromatic aberration based on the fractal intensity
232 float chroma = 0.005 * strength * sin(t);
233
234 float r = mxTexture(samp, finalUV + vec2(chroma, 0.0)).r;
235 float g = mxTexture(samp, finalUV).g;
236 float b = mxTexture(samp, finalUV - vec2(chroma, 0.0)).b;
237
238 return vec3(r, g, b);
239}
240
241/**
242 * @brief Executes the fragment shader entry point.
243 */
244void main() {
245 // 1. Get UVs from the mesh input
246 vec2 uv = tc;
247
248 // 2. Apply Mirror Wrapping
249 uv = wrapUV(uv);
250
251 // 3. Apply Zoom/Rotation
252 uv = applyZoomRotation(uv, vec2(0.5));
253
254 float ampControl = clamp(iAmplitude, 0.0, 2.0);
255
256 // Slower time for geometric fractals usually looks better
257 float tSpeed = 0.2;
258 float t = time_f * tSpeed;
259 float strength = 0.5 + (ampControl * 0.5);
260
261 vec2 center = vec2(0.5);
262
263 if (iMouse.z > 0.0) {
264 center = iMouse.xy / iResolution;
265 }
266
267 // Generate the fractal pattern
268 vec3 col = sampleFractal(uv, t, strength, center, iResolution);
269
270 color.rgb = applyColorAdjustments(col);
271 color.a = 1.0;
272}
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.
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
vec2 kaleido(vec2 p, float seg)
Evaluates kaleido for this shader effect.
mat2 R(float a)
Evaluates R for this shader effect.
float W(vec2 p)
Evaluates W for this shader effect.
mat2 rotate2D(float a)
Evaluates rotate2D for this shader effect.
vec2 applyZoomRotation(vec2 uv, vec2 center)
Evaluates applyZoomRotation for this shader effect.
vec3 sampleFractal(vec2 uv, float t, float strength, vec2 center, vec2 res)
Evaluates sampleFractal for this shader effect.
#define strength
Definition ol.frag:16