Coding

Mandelbrot Set in UNITY

//m3d.space/wp-content/uploads/2017/01/Mandelbrot-GIF.gif

 

Magnification

//m3d.space/wp-content/uploads/2017/01/Mandelbrot-GIF2.gif

 

Changing maximum iterations

Project Description

My attempt to visualize mandelbrot set in unity with standard fragment shader. Unfortunately max zooming is limited due float precision (it breaks on about 30000x zooming). On this scale iterations is fully enough.

Another problem was, that I wanted to build this interactive on WebGL, but shader support restricts For and While loops in code. So I downloaded old version of unity 5.2 and I've built WebPlayer version, but you need one of these browsers to open it: Firefox, Opera, IE

Enjoy


Shader code:

Shader "Custom/Mandelbrot" {
Properties {
_MaxIter ("Max Iterations", Float) = 30
_ZoomIn ("Zoom In", Float) = 0
_PosX("PosX", Float) = 0
_PosY("PosY", Float) = 0
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma target 3.0

#include "UnityCG.cginc"

uniform float _MaxIter;
uniform float _ZoomIn;
uniform float _PosX;
uniform float _PosY;

float4 frag(v2f_img i) : COLOR {
float2 mcoord;
float2 coord = float2(0.0,0.0);
mcoord.x = ((i.uv.x) * (1/_ZoomIn))-2.5 * _PosX;
mcoord.y = ((i.uv.y) * (1/_ZoomIn))-1.0 * _PosY;
float iteration = 0.0;
const float PI = 3.14159265359;
float xtemp;
for ( iteration = 0.0; iteration < _MaxIter; iteration += 1.0) {
if ( coord.x*coord.x + coord.y*coord.y > 10 )
break;
xtemp = coord.x*coord.x - coord.y*coord.y + mcoord.x;
coord.y = 2.0*coord.x*coord.y + mcoord.y;
coord.x = xtemp;
}

float4 color;
float tempPar = 1 - 2*abs(iteration/_MaxIter -0.5);

color.r = tempPar;
color.g = pow(tempPar, 2);
color.b = sqrt(tempPar)/4;
color.a = 1.0;

return color;
}
ENDCG
}
}
}