ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
rainbow_fractal_random.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file rainbow_fractal_random.frag
5 * @brief Vulkan fragment shader: rainbow fractal random.
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 iResolution ext.u0.zw
18#define time_f ext.u2.y
19
20layout(location = 0) in vec2 tc;
21layout(location = 0) out vec4 color;
22layout(set = 0, binding = 0) uniform sampler2D samp;
23
24
25
26//--------------------------------------
27// Utility functions
28//--------------------------------------
29/**
30 * @brief Evaluates hash for this shader effect.
31 * @param n Input value for the calculation.
32 * @return Computed float value.
33 */
34float hash(float n) {
35 return fract(sin(n) * 43758.5453123);
36}
37
38/**
39 * @brief Evaluates random2 for this shader effect.
40 * @param st Input value for the calculation.
41 * @return Computed vec2 value.
42 */
43vec2 random2(vec2 st) {
44 st = vec2(dot(st, vec2(127.1, 311.7)),
45 dot(st, vec2(269.5, 183.3)));
46 return -1.0 + 2.0 * fract(sin(st) * 43758.5453123);
47}
48
49/**
50 * @brief Evaluates smoothRandom2 for this shader effect.
51 * @param t Input value for the calculation.
52 * @return Computed vec2 value.
53 */
54vec2 smoothRandom2(float t) {
55 float t0 = floor(t);
56 float t1 = t0 + 1.0;
57 vec2 rand0 = random2(vec2(t0));
58 vec2 rand1 = random2(vec2(t1));
59 float mix_factor = fract(t);
60 return mix(rand0, rand1, smoothstep(0.0, 1.0, mix_factor));
61}
62
63/**
64 * @brief Evaluates rainbow for this shader effect.
65 * @param t Input value for the calculation.
66 * @return Computed vec3 value.
67 */
68vec3 rainbow(float t) {
69 t = fract(t);
70 float r = abs(t * 6.0 - 3.0) - 1.0;
71 float g = 2.0 - abs(t * 6.0 - 2.0);
72 float b = 2.0 - abs(t * 6.0 - 4.0);
73 return clamp(vec3(r, g, b), 0.0, 1.0);
74}
75
76/**
77 * @brief Evaluates pingPong for this shader effect.
78 * @param x Input value for the calculation.
79 * @param length Input value for the calculation.
80 * @return Computed float value.
81 */
82float pingPong(float x, float length) {
83 float modVal = mod(x, length * 2.0);
84 return (modVal <= length) ? modVal : (length * 2.0 - modVal);
85}
86
87//--------------------------------------
88// Basic blur (kept as-is)
89//--------------------------------------
90/**
91 * @brief Evaluates blur for this shader effect.
92 * @param image Input value for the calculation.
93 * @param uv Input value for the calculation.
94 * @param resolution Input value for the calculation.
95 * @return Computed vec4 value.
96 */
97vec4 blur(sampler2D image, vec2 uv, vec2 resolution) {
98 vec2 texelSize = 1.0 / resolution;
99 vec4 result = vec4(0.0);
100 float kernel[100];
101 float kernelVals[100] = float[](
102 0.5, 1.0, 1.5, 2.0, 2.5, 2.5, 2.0, 1.5, 1.0, 0.5,
103 1.0, 2.0, 2.5, 3.0, 3.5, 3.5, 3.0, 2.5, 2.0, 1.0,
104 1.5, 2.5, 3.0, 3.5, 4.0, 4.0, 3.5, 3.0, 2.5, 1.5,
105 2.0, 3.0, 3.5, 4.0, 4.5, 4.5, 4.0, 3.5, 3.0, 2.0,
106 2.5, 3.5, 4.0, 4.5, 5.0, 5.0, 4.5, 4.0, 3.5, 2.5,
107 2.5, 3.5, 4.0, 4.5, 5.0, 5.0, 4.5, 4.0, 3.5, 2.5,
108 2.0, 3.0, 3.5, 4.0, 4.5, 4.5, 4.0, 3.5, 3.0, 2.0,
109 1.5, 2.5, 3.0, 3.5, 4.0, 4.0, 3.5, 3.0, 2.5, 1.5,
110 1.0, 2.0, 2.5, 3.0, 3.5, 3.5, 3.0, 2.5, 2.0, 1.0,
111 0.5, 1.0, 1.5, 2.0, 2.5, 2.5, 2.0, 1.5, 1.0, 0.5
112 );
113
114 for (int i = 0; i < 100; i++) {
115 kernel[i] = kernelVals[i];
116 }
117
118 float kernelSum = 0.0;
119 for (int i = 0; i < 100; i++) {
120 kernelSum += kernel[i];
121 }
122
123 for (int x = -5; x <= 4; ++x) {
124 for (int y = -5; y <= 4; ++y) {
125 vec2 offset = vec2(float(x), float(y)) * texelSize;
126 result += texture(image, uv + offset) * kernel[(y + 5) * 10 + (x + 5)];
127 }
128 }
129
130 return result / kernelSum;
131}
132
133//--------------------------------------
134// Fractal function – now with infinite zoom logic
135//--------------------------------------
136float fractal(vec2 uv, vec2 c)
137{
138 // We'll do a simple Julia-like iteration
139 vec2 z = uv;
140 const float maxIterations = 50.0;
141 float iteration = 0.0;
142
143 for (float i = 0.0; i < maxIterations; i++)
144 {
145 // z = z^2 + c
146 vec2 z_sq = vec2(z.x*z.x - z.y*z.y, 2.0*z.x*z.y);
147 z = z_sq + c;
148
149 if (length(z) > 2.0) break;
150 iteration += 1.0;
151 }
152
153 return iteration / maxIterations;
154}
155
156//--------------------------------------
157// Get a zoom + offset for “infinite” travel
158//--------------------------------------
159vec2 getFractalUV(vec2 uv, float t)
160{
161 // How long each zoom cycle lasts
162 float cycleDuration = 6.0;
163 // Which "zoom cycle" are we in?
164 float cycleIndex = floor(t / cycleDuration);
165 // Local time in [0..cycleDuration)
166 float cycleTime = fract(t / cycleDuration) * cycleDuration;
167
168 // Exponential zoom factor
169 float zoomSpeed = 0.5; // tweak this for faster or slower zoom
170 float zoom = pow(1.3, cycleTime * zoomSpeed);
171
172 // Optionally pick a “center” each cycle so we jump around
173 // For simplicity, pick some pseudorandom center:
174 // random2( cycleIndex ) returns something in [-1,1]
175 vec2 randOffset = random2(vec2(cycleIndex * 37.1234));
176 // Scale it down so it doesn’t jump too far from origin
177 randOffset *= 0.5;
178
179 // Move uv so that we zoom around randOffset
180 // Step 1: shift uv relative to center
181 uv -= randOffset;
182 // Step 2: apply zoom
183 uv /= zoom;
184 // Step 3: shift back
185 uv += randOffset;
186
187 return uv;
188}
189
190void main(void)
191{
192 //--------------------------------------
193 // Normalize uv to [-1,1], correct aspect
194 //--------------------------------------
195 vec2 uv = tc * 2.0 - 1.0;
196 uv.y *= iResolution.y / iResolution.x;
197
198 //--------------------------------------
199 // We create an infinite zoom
200 //--------------------------------------
201 // Decide how we choose c (the constant in z = z^2 + c).
202 // You can also animate c over time in some interesting way.
203 // Here, let’s revolve it with sine/cosine, but also include
204 // cycle-based shifting so we pick different fractal shapes:
205 float cycleIndex = floor(time_f / 6.0);
206 vec2 cShift = random2(vec2(cycleIndex * 13.97)) * 1.0;
207 vec2 c = vec2(sin(time_f * 0.7), cos(time_f * 0.9)) + cShift;
208
209 // Apply infinite zoom to uv
210 vec2 zoomedUV = getFractalUV(uv, time_f);
211
212 // Evaluate fractal
213 float fVal = fractal(zoomedUV, c);
214
215 // Map fractal value onto a rainbow color
216 vec3 fractalColor = rainbow(fVal);
217
218 //--------------------------------------
219 // Optional: sample blurred background
220 //--------------------------------------
221 vec4 blurred_color = blur(samp, tc, iResolution);
222
223 //--------------------------------------
224 // Blend fractal color with blurred texture
225 //--------------------------------------
226 float blendFactor = 0.5;
227 vec3 blended_color = mix(blurred_color.rgb, fractalColor, blendFactor);
228
229 //--------------------------------------
230 // Just a little extra time-based effect:
231 //--------------------------------------
232 float time_t = pingPong(time_f, 15.0) + 1.0;
233 color = vec4(sin(blended_color * time_t), blurred_color.a);
234}
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
float pingPong(float x, float length)
Evaluates pingPong for this shader effect.
Definition af_scale.frag:32
vec4 blur(sampler2D image, vec2 uv, vec2 resolution)
Evaluates blur for this shader effect.
Definition af_scale.frag:44
float hash(vec2 p)
Evaluates hash for this shader effect.
vec3 rainbow(float t)
Evaluates rainbow for this shader effect.
float n(vec2 p)
Evaluates n for this shader effect.
float rand1(float x)
Evaluates rand1 for this shader effect.
vec4 fractalColor(vec2 uv)
Evaluates fractalColor for this shader effect.
Definition deeps1.frag:66
const int maxIterations
Definition fractal.frag:60
vec2 random2(vec2 st)
Evaluates random2 for this shader effect.
float fractal(vec2 uv, float time)
Evaluates fractal for this shader effect.
vec2 smoothRandom2(float t)
Evaluates smoothRandom2 for this shader effect.
vec2 getFractalUV(vec2 uv, float t)