Mandelbrot and Julia set

This is a shader that can render the mandelbrot set and the julia set (both are fractals that rely on the same equations). You can also play around with the values and see what happens, and note that the julia set just looks like a sphere if you don’t change the c value. I actually made half of it in version 4.5, and the rest after I had upgraded to 4.7. But since the version selector does not have 4.7 at the time of upload, it only says 4.5.

Note that you can’t zoom in forever, because vec2 is only 32-bit and you would need to recompile the engine to get 64-bit.

Also I’m sorry for the low resolution cover image and screenshots, the laptop screen simply isn’t big enoungh.

IMPORTANT: It is currently not working on this website, but I am trying to figure it out. For now you can check out the itch.io page

Shader code
shader_type canvas_item;

uniform bool use_gradient = true;
uniform sampler2D gradient;
uniform int repetitions = 500;
uniform float scale = 5;
uniform bool julia_set = false;
uniform vec2 z = vec2(0, 0);
uniform vec2 c = vec2(0, 0);

float get_mandelbrot_color(vec2 _c) { // This is the mandelbrot set.
	vec2 _z = z;
	float n = 0.0;
	for (; n < float(repetitions) && length(_z) < 2.0; n++) {
		float x = _z.x;
		float y = _z.y;
		_z = vec2(x*x - y*y, 2.0*x*y) + _c;
	}
	return n / float(repetitions);
}

float get_julia_color(vec2 _z) { // This is the julia set. _c and _z is swapped.
	vec2 _c = c;
	float n = 0.0;
	for (; n < float(repetitions) && length(_z) < 2.0; n++) {
		float x = _z.x;
		float y = _z.y;
		_z = vec2(x*x - y*y, 2.0*x*y) + _c;
	}
	return n / float(repetitions);
}

vec3 get_gradient_color(float position) {
    return texture(gradient, vec2(position, 0.5)).rgb;
}

float get_color(vec2 pos) {
	pos -= vec2(0.5);
	pos *= vec2(scale);
	if (julia_set) {
		return get_julia_color(pos);
	}
	return get_mandelbrot_color(pos);
}

void fragment() {
	float value = get_color(UV / TEXTURE_PIXEL_SIZE);
	vec3 color;
	if (use_gradient) {
		color = get_gradient_color(value);
	} else {
		if (abs(value-1.0) < 0.0001) {
			color = vec3(0.0);
		} else {
			color = vec3(value);
		}
	}
	COLOR.rgb = color;

	// Ignore this, this is just me trying to make this work on the website.
	COLOR.rgb = UV;
}
Live Preview
Tags
fractal, julia set, mandelbrot
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

guest

0 Comments
Oldest
Newest Most Voted