Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cone generator #309

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open

Conversation

Cabbache
Copy link

@Cabbache Cabbache commented Feb 3, 2023

No description provided.

// Calculate the distance of the point from the origin.
let dist_from_center = (x.powi(2) + y.powi(2)).sqrt();

match dist_from_center > self.radius{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should square the radius (you could even store the squared version) so you can avoid the sqrt() call above.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I've moved the sqrt call inside the match expression so that it is only computed for case false.

@Razaekel
Copy link
Owner

Razaekel commented Feb 4, 2023

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

@Cabbache
Copy link
Author

Cabbache commented Feb 4, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants