RPG-Alex / project-julia

A Web Assembly Adaptation of a procedurally generated image based on Julia Sets
2 stars 1 forks source link

Smoothing the fractal #3

Closed Silzinc closed 8 months ago

Silzinc commented 8 months ago

If the refactoring proposed in #2 is made, there is yet another improvement that would make the fractal a lot smoother. It basically consists in modifying julia as such:

// Transforms the [0, 1] value to another value using a smoothstep-like function
#[inline]
fn smoother(iter: u8, z: Complex<float>) -> float
{
  (iter as float - z.norm_squared().log2().max(1.).log2()).max(0.).min(u8::MAX as float) / u8::MAX as float
}

// Returns a float between 0 and 1 that represents the color of the pixel
fn julia(c: Complex<float>, x: float, y: float) -> float
{
  let mut z = Complex::new(x, y);

  for i in 0..u8::MAX {
    if z.norm_squared() > 4.0 {
      return smoother(i, z);
    }
    z = z * z + c;
  }
  smoother(u8::MAX, z)
}

Smoothing off: Capture d’écran du 2024-03-04 20-35-25 Smoothing on: Capture d’écran du 2024-03-04 02-38-25