Created
July 26, 2020 16:51
-
-
Save seandewar/2b0e2641bcc17a5a24fd62bce74d4285 to your computer and use it in GitHub Desktop.
Simple mandelbrot shader for shadertoy.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// max number of iterations to use for computing mandelbrot | |
const uint maxIterations = 20u; | |
// sampling level for regular grid multisample anti-aliasing; 1 disables. the | |
// number of samples used is the square of this number (e.g: 4u is MSAA 16x) | |
const uint msaaLevel = 2u; | |
vec3 mandelbrot(const vec2 c) | |
{ | |
vec2 z = vec2(0.); | |
for (uint i = 0u; i < maxIterations; ++i) { | |
z = vec2(z.x * z.x - z.y * z.y + c.x, 2. * z.x * z.y + c.y); | |
if (z.x * z.x + z.y * z.y > 4.) | |
return vec3(1.); // escapes to infinity | |
} | |
return vec3(0.); | |
} | |
void mainImage(out vec4 fragColor, vec2 fragCoord) | |
{ | |
vec3 color = vec3(0.); | |
// offset positions of the msaa samples and average the colours | |
for (uint i = 0u; i < msaaLevel; ++i) { | |
for (uint j = 0u; j < msaaLevel; ++j) { | |
vec2 spoffset = vec2(float(2u * i + 1u) * (0.5 / float(msaaLevel)), | |
float(2u * j + 1u) * (0.5 / float(msaaLevel))); | |
vec2 suv = (fragCoord - vec2(0.5, 0.5) + spoffset) / iResolution.xy; | |
color += vec3(suv.x, suv.y, 0.5) | |
* mandelbrot(2.5 * suv - vec2(2., 1.25)); | |
} | |
} | |
color /= float(msaaLevel * msaaLevel); | |
fragColor = vec4(color, 1.); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment