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

Remove exponentiation from fractal generators (#341) + fix scale factor for fbm (#273) #342

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified examples/README.md
Binary file not shown.
Binary file modified images/examples/blend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/fbm_fbm_perlin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/fbm_perlin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/fbm_worley.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/texture_granite_planar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/texture_granite_seamless.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/texture_granite_sphere.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/texture_jade_planar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/texture_jade_seamless.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/texture_wood_planar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/turbulence.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/unscaledFinalPlanet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/unscaledFinalPlanet_16x_zoom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/unscaledFinalPlanet_4x_zoom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/examples/unscaledFinalPlanet_sphere.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/fbm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/unscaledFinalPlanet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/unscaledFinalPlanet_16x_zoom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/unscaledFinalPlanet_4x_zoom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 15 additions & 3 deletions src/noise_fns/generators/fractals/basicmulti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ where

// if only 1 octave of noise, then return result unchanged, otherwise process another octave
if self.octaves > 1 {
let mut attenuation = self.persistence;
// Spectral construction inner loop, where the fractal is built.
for x in 1..self.octaves {
// Raise the spatial frequency.
Expand All @@ -175,7 +176,10 @@ where
let mut signal = self.sources[x].get(point.into_array());

// Scale the amplitude appropriately for this frequency.
signal *= self.persistence.powi(x as i32);
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ (x + 1)
attenuation *= self.persistence;

// Scale the signal by the current 'altitude' of the function.
signal *= result;
Expand Down Expand Up @@ -204,6 +208,7 @@ where

// if only 1 octave of noise, then return result unchanged, otherwise process another octave
if self.octaves > 1 {
let mut attenuation = self.persistence;
// Spectral construction inner loop, where the fractal is built.
for x in 1..self.octaves {
// Raise the spatial frequency.
Expand All @@ -213,7 +218,10 @@ where
let mut signal = self.sources[x].get(point.into_array());

// Scale the amplitude appropriately for this frequency.
signal *= self.persistence.powi(x as i32);
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ (x + 1)
attenuation *= self.persistence;

// Scale the signal by the current 'altitude' of the function.
signal *= result;
Expand Down Expand Up @@ -242,6 +250,7 @@ where

// if only 1 octave of noise, then return result unchanged, otherwise process another octave
if self.octaves > 1 {
let mut attenuation = self.persistence;
// Spectral construction inner loop, where the fractal is built.
for x in 1..self.octaves {
// Raise the spatial frequency.
Expand All @@ -251,7 +260,10 @@ where
let mut signal = self.sources[x].get(point.into_array());

// Scale the amplitude appropriately for this frequency.
signal *= self.persistence.powi(x as i32);
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ (x + 1)
attenuation *= self.persistence;

// Scale the signal by the current 'altitude' of the function.
signal *= result;
Expand Down
21 changes: 18 additions & 3 deletions src/noise_fns/generators/fractals/billow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ where

let mut result = 0.0;

let mut attenuation = self.persistence;

point *= self.frequency;

for x in 0..self.octaves {
Expand All @@ -169,7 +171,10 @@ where
signal = scale_shift(signal, 2.0);

// Scale the amplitude appropriately for this frequency.
signal *= self.persistence.powi((x as i32) + 1);
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ (x + 1)
attenuation *= self.persistence;

// Add the signal to the result.
result += signal;
Expand All @@ -193,6 +198,8 @@ where

let mut result = 0.0;

let mut attenuation = self.persistence;

point *= self.frequency;

for x in 0..self.octaves {
Expand All @@ -204,7 +211,10 @@ where
signal = scale_shift(signal, 2.0);

// Scale the amplitude appropriately for this frequency.
signal *= self.persistence.powi((x as i32) + 1);
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ (x + 1)
attenuation *= self.persistence;

// Add the signal to the result.
result += signal;
Expand All @@ -228,6 +238,8 @@ where

let mut result = 0.0;

let mut attenuation = self.persistence;

point *= self.frequency;

for x in 0..self.octaves {
Expand All @@ -239,7 +251,10 @@ where
signal = scale_shift(signal, 2.0);

// Scale the amplitude appropriately for this frequency.
signal *= self.persistence.powi((x as i32) + 1);
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ (x + 1)
attenuation *= self.persistence;

// Add the signal to the result.
result += signal;
Expand Down
15 changes: 7 additions & 8 deletions src/noise_fns/generators/fractals/fbm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ pub struct Fbm<T> {
scale_factor: f64,
}

fn calc_scale_factor(persistence: f64, octaves: usize) -> f64 {
1.0 - persistence.powi(octaves as i32)
}

impl<T> Fbm<T>
where
T: Default + Seedable,
Expand All @@ -78,7 +74,10 @@ where
lacunarity: Self::DEFAULT_LACUNARITY,
persistence: Self::DEFAULT_PERSISTENCE,
sources: super::build_sources(seed, Self::DEFAULT_OCTAVE_COUNT),
scale_factor: calc_scale_factor(Self::DEFAULT_PERSISTENCE, Self::DEFAULT_OCTAVE_COUNT),
scale_factor: Self::calc_scale_factor(
Self::DEFAULT_PERSISTENCE,
Self::DEFAULT_OCTAVE_COUNT,
),
}
}

Expand Down Expand Up @@ -180,7 +179,7 @@ where
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ (x + 1)
attenuation *= attenuation;
attenuation *= self.persistence;

// Add the signal to the result.
result += signal;
Expand Down Expand Up @@ -216,7 +215,7 @@ where
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ (x + 1)
attenuation *= attenuation;
attenuation *= self.persistence;

// Add the signal to the result.
result += signal;
Expand Down Expand Up @@ -252,7 +251,7 @@ where
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ (x + 1)
attenuation *= attenuation;
attenuation *= self.persistence;

// Add the signal to the result.
result += signal;
Expand Down
21 changes: 18 additions & 3 deletions src/noise_fns/generators/fractals/hybridmulti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ where
fn get(&self, point: [f64; 2]) -> f64 {
let mut point = Vector2::from(point);

let mut attenuation = self.persistence;

// First unscaled octave of function; later octaves are scaled.
point *= self.frequency;
let mut result = self.sources[0].get(point.into_array()) * self.persistence;
Expand All @@ -185,7 +187,10 @@ where
let mut signal = self.sources[x].get(point.into_array());

// Scale the amplitude appropriately for this frequency.
signal *= self.persistence.powi(x as i32);
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ (x + 1)
attenuation *= self.persistence;

// Add it in, weighted by previous octave's noise value.
result += weight * signal;
Expand All @@ -207,6 +212,8 @@ where
fn get(&self, point: [f64; 3]) -> f64 {
let mut point = Vector3::from(point);

let mut attenuation = self.persistence;

// First unscaled octave of function; later octaves are scaled.
point *= self.frequency;
let mut result = self.sources[0].get(point.into_array()) * self.persistence;
Expand All @@ -224,7 +231,10 @@ where
let mut signal = self.sources[x].get(point.into_array());

// Scale the amplitude appropriately for this frequency.
signal *= self.persistence.powi(x as i32);
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ (x + 1)
attenuation *= self.persistence;

// Add it in, weighted by previous octave's noise value.
result += weight * signal;
Expand All @@ -246,6 +256,8 @@ where
fn get(&self, point: [f64; 4]) -> f64 {
let mut point = Vector4::from(point);

let mut attenuation = self.persistence;

// First unscaled octave of function; later octaves are scaled.
point *= self.frequency;
let mut result = self.sources[0].get(point.into_array()) * self.persistence;
Expand All @@ -263,7 +275,10 @@ where
let mut signal = self.sources[x].get(point.into_array());

// Scale the amplitude appropriately for this frequency.
signal *= self.persistence.powi(x as i32);
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ (x + 1)
attenuation *= self.persistence;

// Add it in, weighted by previous octave's noise value.
result += weight * signal;
Expand Down
21 changes: 18 additions & 3 deletions src/noise_fns/generators/fractals/ridgedmulti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ where
let mut result = 0.0;
let mut weight = 1.0;

let mut attenuation = 1.0;

point *= self.frequency;

for x in 0..self.octaves {
Expand All @@ -226,7 +228,10 @@ where
weight = weight.clamp(0.0, 1.0);

// Scale the amplitude appropriately for this frequency.
signal *= self.persistence.powi(x as i32);
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ x
attenuation *= self.persistence;

// Add the signal to the result.
result += signal;
Expand Down Expand Up @@ -260,6 +265,8 @@ where
let mut result = 0.0;
let mut weight = 1.0;

let mut attenuation = 1.0;

point *= self.frequency;

for x in 0..self.octaves {
Expand All @@ -285,7 +292,10 @@ where
weight = weight.clamp(0.0, 1.0);

// Scale the amplitude appropriately for this frequency.
signal *= self.persistence.powi(x as i32);
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ x
attenuation *= self.persistence;

// Add the signal to the result.
result += signal;
Expand Down Expand Up @@ -319,6 +329,8 @@ where
let mut result = 0.0;
let mut weight = 1.0;

let mut attenuation = 1.0;

point *= self.frequency;

for x in 0..self.octaves {
Expand All @@ -344,7 +356,10 @@ where
weight = weight.clamp(0.0, 1.0);

// Scale the amplitude appropriately for this frequency.
signal *= self.persistence.powi(x as i32);
signal *= attenuation;

// Increase the attenuation for the next octave, to be equal to persistence ^ x
attenuation *= self.persistence;

// Add the signal to the result.
result += signal;
Expand Down
Loading