Float point imprecision
Hi, I've rushed up some relatively simple code to generate a Mandelbrot set using an Image effect shader in Unity. It looks nice and all, but I want to have a good looking zoom, and the problem is that very quickly my image gets very pixelated as my shader zooms in. Im almost certain this is due to floating point imprecision (I represent my complex numbers with a float2), since I need to make calculations about numbers that are more and more precise, and at some point I think the computer just can't make a difference between two complex numbers. However, when I look at zooms online, the limit before imprecision kicks in is much farther away in the zoom. It there a way to optimize my script? Here it is (cNorm calculates complex norm, quad is short for quadratic and performs z^2+c):
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = fixed4(0,0,0,0);
float xMin = -32;
float xMax = 32;
float yMin = -18;
float yMax = 18;
float xRange = xMax - xMin;
float yRange = yMax - yMin;
float Re = (xMin + xRange * i.uv.x)/exp(_Zoom-1) +_CenterRe/10000;
float Im = (yMin + yRange * i.uv.y)/exp(_Zoom-1) +_CenterIm/10000;
float2 c = float2(Re, Im);
float2 z = quad(0, c);
int m = 0;
while (cNorm(z) <= 2 && m < _MaxIters)
{
z = quad(z, c);
m = m + 1;
}
if (cNorm(z) > 2 )
{
col.xyz = HUEtoRGB(m / _MaxIters);
}
return col;
}