Skip to content

Commit

Permalink
Remove useless exponentiation in fractals generators by multiplying t…
Browse files Browse the repository at this point in the history
…he value in the loop (as suggested for fbm in #299). Fixed calc_scale_factor in fbm constructor.
  • Loading branch information
PrinceOfBorgo authored and Razaekel committed Mar 17, 2024
1 parent 90f2a3c commit 95c6bc4
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 23 deletions.
12 changes: 6 additions & 6 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,14 @@ cargo run --example "super_simplex" --features="images"
```
cargo run --example "terrace" --features="images"
```
![terrace_inverted.png]

*terrace_inverted.png*

![terrace.png]

*terrace.png*

![terrace_inverted.png]

*terrace_inverted.png*


## [texturegranite](/examples/texturegranite.rs)

Expand Down Expand Up @@ -765,10 +765,10 @@ cargo run --example "worley" --features="images"

[super_simplex 3d.png]: /images/examples/super_simplex%203d.png

[terrace_inverted.png]: /images/examples/terrace_inverted.png

[terrace.png]: /images/examples/terrace.png

[terrace_inverted.png]: /images/examples/terrace_inverted.png

[texture_granite_planar.png]: /images/examples/texture_granite_planar.png

[texture_granite_seamless.png]: /images/examples/texture_granite_seamless.png
Expand Down
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/fbm.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
9 changes: 4 additions & 5 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
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

0 comments on commit 95c6bc4

Please sign in to comment.