Razaekel / noise-rs

Procedural noise generation library for Rust.
Apache License 2.0
854 stars 120 forks source link

Add cone generator #309

Open Cabbache opened 1 year ago

Razaekel commented 1 year ago

I'd like an explanation of the purpose for this noisefn.

Cabbache commented 1 year ago

I have a use case where I needed to generate a binary terrain (1 or -1) in 2d that gradually becomes denser as you move further away from the center. There doesn't seem to be an easy way to do this without the cone NoiseFn. Here's how the cone is useful in this case:

use noise::{Fbm, Perlin, Constant, Cone, Negate, Add, Select, Clamp};
use noise::utils::{NoiseMapBuilder, PlaneMapBuilder};
use noise::MultiFractal;

fn main() {
  let noise = Select::new(
    Constant::new(-1.0),
    Constant::new(1.0),
    Add::new(
      Negate::new(
        Clamp::new(
          Cone::default().set_radius(39000f64)
        ).set_upper_bound(0.0)
      ),
      Fbm::<Perlin>::new(1)
        .set_octaves(5)
        .set_frequency(0.0003)
        .set_lacunarity(1.1)
        .set_persistence(2.0)
    )
  )
  .set_bounds(0.2, 1.0)
  .set_falloff(-1.0);

  let world = PlaneMapBuilder::<_, 2>::new(noise)
    .set_size(500, 500)
    .set_x_bounds(-30000.0, 30000.0)
    .set_y_bounds(-30000.0, 30000.0)
    .build();

  world.write_to_file("arena.png");
}

This code is essentially subtracting a cone from fbm noise.