ACMX Vulkan Shader Library
GLSL 4.50 shader effects for ACMX's Vulkan renderer
Loading...
Searching...
No Matches
julia.frag
Go to the documentation of this file.
1#version 450
2
3/**
4 * @file julia.frag
5 * @brief Vulkan fragment shader: julia.
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 ext.u1.y
18#define iMouse ext.mouse
19#define iResolution ext.u0.zw
20#define time_f ext.u2.y
21#define uamp ext.u1.y
22
23layout(location = 0) in vec2 tc;
24layout(location = 0) out vec4 color;
25
26// Uniforms
27
28layout(set = 0, binding = 0) uniform sampler2D samp;
29
30
31
32
33
34// --------------------------------------------------------
35// Pseudo-random helper: returns a float in [0..1]
36// (Classic 2D hash based on UV coords)
37/**
38 * @brief Evaluates hash12 for this shader effect.
39 * @param p Input value for the calculation.
40 * @return Computed float value.
41 */
42float hash12(vec2 p) {
43 // A simple hashing function
44 vec3 p3 = fract(vec3(p.xyx) * 0.1031);
45 p3 += dot(p3, p3.yzx + 19.19);
46 return fract((p3.x + p3.y) * p3.z);
47}
48
49// --------------------------------------------------------
50// Generate a random seed for location (x,y) or fractal c
51// based on some “key” (cycle index, etc.)
52// --------------------------------------------------------
53/**
54 * @brief Evaluates genSeed for this shader effect.
55 * @param key Input value for the calculation.
56 * @return Computed vec2 value.
57 */
58vec2 genSeed(float key) {
59 // We’ll just return a pair of pseudo-random numbers:
60 float r1 = hash12(vec2(key, 13.789));
61 float r2 = hash12(vec2(key, 37.239));
62 return vec2(r1, r2);
63}
64
65// --------------------------------------------------------
66// Simple helper to rotate 2D coordinates
67// --------------------------------------------------------
68/**
69 * @brief Evaluates rotate2D for this shader effect.
70 * @param angle Input value for the calculation.
71 * @return Computed mat2 value.
72 */
73mat2 rotate2D(float angle){
74 return mat2(cos(angle), -sin(angle),
75 sin(angle), cos(angle));
76}
77
78// --------------------------------------------------------
79// Draw a Julia fractal around `center` using a “seed” c
80// Returns a color with alpha determining intensity
81// --------------------------------------------------------
82/**
83 * @brief Evaluates drawJulia for this shader effect.
84 * @param uv Input value for the calculation.
85 * @param center Input value for the calculation.
86 * @param c Input value for the calculation.
87 * @return Computed vec4 value.
88 */
89vec4 drawJulia(vec2 uv, vec2 center, vec2 c) {
90 // Let’s define how “large” the fractal area is
91 // We'll zoom in around center
92 float zoom = 2.0;
93 uv = (uv - center) * zoom;
94
95 // Iteration
96 const int MAX_ITER = 30;
97 vec2 z = uv;
98 float m = 0.0;
99 for (int i = 0; i < MAX_ITER; i++) {
100 z = vec2(z.x*z.x - z.y*z.y, 2.0*z.x*z.y) + c;
101 if (dot(z,z) > 4.0) {
102 m = float(i) / float(MAX_ITER);
103 break;
104 }
105 }
106
107 // Convert iteration ratio into color
108 float colVal = m;
109 vec3 col = vec3(sin(colVal * 6.2831), // some fun banding
110 colVal,
111 1.0 - colVal);
112 // Return with alpha = min(1.0, some factor)
113 return vec4(col, smoothstep(0.0, 1.0, m));
114}
115
116// --------------------------------------------------------
117// Draw another shape: e.g. a pulsating circle
118// center, radius, etc. For variety.
119// --------------------------------------------------------
120/**
121 * @brief Evaluates drawPulsingCircle for this shader effect.
122 * @param uv Input value for the calculation.
123 * @param center Input value for the calculation.
124 * @param radius Input value for the calculation.
125 * @return Computed vec4 value.
126 */
127vec4 drawPulsingCircle(vec2 uv, vec2 center, float radius) {
128 // Distance from center
129 float dist = length(uv - center);
130 // Pulsate the radius with time
131 float pulse = 0.5 + 0.4 * sin(time_f * 2.0);
132 float finalRadius = radius * pulse;
133
134 // Hard edge circle with smooth fade
135 float edge = smoothstep(finalRadius, finalRadius - 0.01, dist);
136
137 // We can color it however we like
138 // Let’s do a gradient from center to edge
139 float t = dist / finalRadius;
140 vec3 baseColor = mix(vec3(1.0, 0.5, 0.0), // center color
141 vec3(0.0, 0.5, 1.0), // edge color
142 t);
143 // edge is basically alpha for “outside vs inside”
144 // invert because smoothstep finalRadius -> finalRadius-0.01
145 float alpha = 1.0 - edge;
146
147 return vec4(baseColor, alpha);
148}
149
150// --------------------------------------------------------
151// main()
152// We orchestrate time-based cycle here:
153// 1) Draw fractal, fade in/out
154// 2) Once done, draw circle in another random location
155// 3) Repeat
156// --------------------------------------------------------
157void main(void)
158{
159 //-----------------------------------------
160 // 1) Basic pass-through from the original
161 // texture, so we have a background
162 //-----------------------------------------
163 color = texture(samp, tc);
164
165 //-----------------------------------------
166 // 2) Define cycle timing
167 // For example, each cycle is 10 seconds:
168 // 0..4 : fractal fade in (some fraction)
169 // 4..7 : fractal at full alpha
170 // 7..8 : fractal fade out
171 // 8..9.5 : circle fade in
172 // 9.5..12: circle at full alpha
173 // 12..13 : circle fade out
174 // Then repeat
175 //-----------------------------------------
176 float cycleDuration = 13.0;
177 float cycleTime = mod(time_f, cycleDuration);
178 float cycleIndex = floor(time_f / cycleDuration);
179 // We’ll use cycleIndex as a “seed” for random picks
180
181 // Times for fractal
182 float fractalFadeInStart = 0.0;
183 float fractalFadeInEnd = 1.0; // 0..1
184 float fractalHoldEnd = 3.0; // 1..3
185 float fractalFadeOutEnd = 4.0; // 3..4
186
187 // Times for circle
188 float circleFadeInStart = 5.0;
189 float circleFadeInEnd = 6.0; // 5..6
190 float circleHoldEnd = 8.0; // 6..8
191 float circleFadeOutEnd = 9.0; // 8..9
192
193 //-----------------------------------------
194 // 3) Random seeds and positions
195 // We’ll pick:
196 // - fractal location, fractal “c”
197 // - circle location, circle radius
198 //-----------------------------------------
199 // fractal center (in clip space [-1..1])
200 vec2 fractalRand = genSeed(cycleIndex * 3.17);
201 // Map from [0..1] to [-0.8..0.8], so it's not off-screen
202 vec2 fractalCenter = fractalRand * 1.6 - 0.8;
203 // fractal c
204 vec2 fractalSeed = genSeed(cycleIndex * 1.93) * 2.0 - 1.0;
205
206 // circle center
207 vec2 circleRand = genSeed(cycleIndex * 7.87 + 1.234);
208 vec2 circleCenter = circleRand * 1.6 - 0.8;
209 // circle radius
210 float circleRadius = 0.2 + 0.2 * hash12(circleRand + 0.123);
211
212 //-----------------------------------------
213 // 4) Evaluate alpha for fractal in current
214 // time sub-interval
215 //-----------------------------------------
216 float fractalAlpha = 0.0;
217 if (cycleTime >= fractalFadeInStart && cycleTime < fractalFadeInEnd) {
218 // fade in from 0..1
219 float t = (cycleTime - fractalFadeInStart) / (fractalFadeInEnd - fractalFadeInStart);
220 fractalAlpha = smoothstep(0.0, 1.0, t);
221 } else if (cycleTime >= fractalFadeInEnd && cycleTime < fractalHoldEnd) {
222 // hold at 1
223 fractalAlpha = 1.0;
224 } else if (cycleTime >= fractalHoldEnd && cycleTime < fractalFadeOutEnd) {
225 // fade out from 1..0
226 float t = (cycleTime - fractalHoldEnd) / (fractalFadeOutEnd - fractalHoldEnd);
227 fractalAlpha = 1.0 - smoothstep(0.0, 1.0, t);
228 }
229 // else fractalAlpha remains 0 outside that interval
230
231 //-----------------------------------------
232 // 5) Evaluate alpha for circle
233 //-----------------------------------------
234 float circleAlpha = 0.0;
235 if (cycleTime >= circleFadeInStart && cycleTime < circleFadeInEnd) {
236 float t = (cycleTime - circleFadeInStart) / (circleFadeInEnd - circleFadeInStart);
237 circleAlpha = smoothstep(0.0, 1.0, t);
238 } else if (cycleTime >= circleFadeInEnd && cycleTime < circleHoldEnd) {
239 circleAlpha = 1.0;
240 } else if (cycleTime >= circleHoldEnd && cycleTime < circleFadeOutEnd) {
241 float t = (cycleTime - circleHoldEnd) / (circleFadeOutEnd - circleHoldEnd);
242 circleAlpha = 1.0 - smoothstep(0.0, 1.0, t);
243 }
244 // otherwise circleAlpha remains 0
245
246 //-----------------------------------------
247 // 6) Convert the fragment coordinate tc
248 // to a [-1..1] range for easy shaping
249 //-----------------------------------------
250 // (tc in [0..1], so shift+scale to [-1..1])
251 vec2 uv = tc * 2.0 - 1.0;
252 // Account for aspect ratio if needed
253 float aspect = iResolution.x / iResolution.y;
254 uv.x *= aspect;
255
256 //-----------------------------------------
257 // 7) Draw fractal and circle
258 //-----------------------------------------
259 // Fractal
260 vec4 fractalCol = drawJulia(uv, fractalCenter, fractalSeed);
261 fractalCol.a *= fractalAlpha; // fade in/out
262
263 // Circle
264 vec4 circleCol = drawPulsingCircle(uv, circleCenter, circleRadius);
265 circleCol.a *= circleAlpha;
266
267 //-----------------------------------------
268 // 8) Combine with the background texture
269 //-----------------------------------------
270 // Over-blend fractal
271 color = mix(color, fractalCol, fractalCol.a);
272 // Over-blend circle
273 color = mix(color, circleCol, circleCol.a);
274}
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 hash12(vec2 p)
Evaluates hash12 for this shader effect.
#define alpha
Definition color_g.frag:17
mat2 rotate2D(float a)
Evaluates rotate2D for this shader effect.
vec4 drawPulsingCircle(vec2 uv, vec2 center, float radius)
Evaluates drawPulsingCircle for this shader effect.
Definition julia.frag:127
vec4 drawJulia(vec2 uv, vec2 center, vec2 c)
Evaluates drawJulia for this shader effect.
Definition julia.frag:89
vec2 genSeed(float key)
Evaluates genSeed for this shader effect.
Definition julia.frag:58