1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
use crate::{
math::vectors::*,
noise_fns::{MultiFractal, NoiseFn, Seedable},
};
use alloc::vec::Vec;
/// Noise function that outputs ridged-multifractal noise.
///
/// This noise function, heavily based on the fBm-noise function, generates
/// ridged-multifractal noise. Ridged-multifractal noise is generated in much
/// the same way as fBm noise, except the output of each octave is modified by
/// an absolute-value function. Modifying the octave values in this way
/// produces ridge-like formations.
///
/// The values output from this function will usually range from -1.0 to 1.0 with
/// default values for the parameters, but there are no guarantees that all
/// output values will exist within this range. If the parameters are modified
/// from their defaults, then the output will need to be scaled to remain in
/// the [-1, 1] range.
///
/// Ridged-multifractal noise is often used to generate craggy mountainous
/// terrain or marble-like textures.
#[derive(Clone, Debug)]
pub struct RidgedMulti<T> {
/// Total number of frequency octaves to generate the noise with.
///
/// The number of octaves control the _amount of detail_ in the noise
/// function. Adding more octaves increases the detail, with the drawback
/// of increasing the calculation time.
pub octaves: usize,
/// The number of cycles per unit length that the noise function outputs.
pub frequency: f64,
/// A multiplier that determines how quickly the frequency increases for
/// each successive octave in the noise function.
///
/// The frequency of each successive octave is equal to the product of the
/// previous octave's frequency and the lacunarity value.
///
/// A lacunarity of 2.0 results in the frequency doubling every octave. For
/// almost all cases, 2.0 is a good value to use.
pub lacunarity: f64,
/// A multiplier that determines how quickly the amplitudes diminish for
/// each successive octave in the noise function.
///
/// The amplitude of each successive octave is equal to the product of the
/// previous octave's amplitude and the persistence value. Increasing the
/// persistence produces "rougher" noise.
pub persistence: f64,
/// The attenuation to apply to the weight on each octave. This reduces
/// the strength of each successive octave, making their respective
/// ridges smaller. The default attenuation is 2.0, making each octave
/// half the height of the previous.
pub attenuation: f64,
seed: u32,
sources: Vec<T>,
scale_factor: f64,
}
impl<T> RidgedMulti<T>
where
T: Default + Seedable,
{
pub const DEFAULT_SEED: u32 = 0;
pub const DEFAULT_OCTAVE_COUNT: usize = 6;
pub const DEFAULT_FREQUENCY: f64 = 1.0;
pub const DEFAULT_LACUNARITY: f64 = core::f64::consts::PI * 2.0 / 3.0;
pub const DEFAULT_PERSISTENCE: f64 = 1.0;
pub const DEFAULT_ATTENUATION: f64 = 2.0;
pub const MAX_OCTAVES: usize = 32;
pub fn new(seed: u32) -> Self {
Self {
seed,
octaves: Self::DEFAULT_OCTAVE_COUNT,
frequency: Self::DEFAULT_FREQUENCY,
lacunarity: Self::DEFAULT_LACUNARITY,
persistence: Self::DEFAULT_PERSISTENCE,
attenuation: Self::DEFAULT_ATTENUATION,
sources: super::build_sources(seed, Self::DEFAULT_OCTAVE_COUNT),
scale_factor: Self::calc_scale_factor(
Self::DEFAULT_PERSISTENCE,
Self::DEFAULT_ATTENUATION,
Self::DEFAULT_OCTAVE_COUNT,
),
}
}
pub fn set_attenuation(self, attenuation: f64) -> Self {
Self {
attenuation,
scale_factor: Self::calc_scale_factor(self.persistence, attenuation, self.octaves),
..self
}
}
pub fn set_sources(self, sources: Vec<T>) -> Self {
Self { sources, ..self }
}
fn calc_scale_factor(persistence: f64, attenuation: f64, octaves: usize) -> f64 {
let mut denom = 0.0;
// Do octave 0
let mut amplitude = 1.0;
let mut weight = 1.0;
let mut signal = weight * amplitude;
denom += signal;
if octaves >= 1 {
denom += (1..=octaves).fold(0.0, |acc, x| {
amplitude *= persistence;
weight = (signal / attenuation.powi(x as i32)).clamp(0.0, 1.0);
signal = weight * amplitude;
acc + signal
});
}
2.0 / denom
}
}
impl<T> Default for RidgedMulti<T>
where
T: Default + Seedable,
{
fn default() -> Self {
Self::new(Self::DEFAULT_SEED)
}
}
impl<T> MultiFractal for RidgedMulti<T>
where
T: Default + Seedable,
{
fn set_octaves(self, mut octaves: usize) -> Self {
if self.octaves == octaves {
return self;
}
octaves = octaves.clamp(1, Self::MAX_OCTAVES);
Self {
octaves,
sources: super::build_sources(self.seed, octaves),
scale_factor: Self::calc_scale_factor(self.persistence, self.attenuation, octaves),
..self
}
}
fn set_frequency(self, frequency: f64) -> Self {
Self { frequency, ..self }
}
fn set_lacunarity(self, lacunarity: f64) -> Self {
Self { lacunarity, ..self }
}
fn set_persistence(self, persistence: f64) -> Self {
Self {
persistence,
scale_factor: Self::calc_scale_factor(persistence, self.attenuation, self.octaves),
..self
}
}
}
impl<T> Seedable for RidgedMulti<T>
where
T: Default + Seedable,
{
fn set_seed(self, seed: u32) -> Self {
if self.seed == seed {
return self;
}
Self {
seed,
sources: super::build_sources(seed, self.octaves),
..self
}
}
fn seed(&self) -> u32 {
self.seed
}
}
/// 2-dimensional `RidgedMulti` noise
impl<T> NoiseFn<f64, 2> for RidgedMulti<T>
where
T: NoiseFn<f64, 2>,
{
fn get(&self, point: [f64; 2]) -> f64 {
let mut point = Vector2::from(point);
let mut result = 0.0;
let mut weight = 1.0;
let mut attenuation = 1.0;
point *= self.frequency;
for x in 0..self.octaves {
// Get the value.
let mut signal = self.sources[x].get(point.into_array());
// Make the ridges.
signal = signal.abs();
signal = 1.0 - signal;
// Square the signal to increase the sharpness of the ridges.
signal *= signal;
// Apply the weighting from the previous octave to the signal.
// Larger values have higher weights, producing sharp points along
// the ridges.
signal *= weight;
// Weight successive contributions by the previous signal.
weight = signal / self.attenuation;
// Clamp the weight to [0,1] to prevent the result from diverging.
weight = weight.clamp(0.0, 1.0);
// Scale the amplitude appropriately for this frequency.
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;
// Increase the frequency.
point *= self.lacunarity;
}
// The result before scaling will be 0 to something positive, so need to sale it back down
// to -1 to 1. We don't know what the upper limit is, but it can be calculated based on the
// number of octaves, and the persistence and attenuation values. By dividing the result by
// what the upper limit should be / 2, we should get a value between 0 and 2. Then we can
// shift the result to cover the -1 to 1 range.
// Scale the result to [0, 2]
result *= self.scale_factor;
// Shift the result to [-1, 1]
result - 1.0
}
}
/// 3-dimensional `RidgedMulti` noise
impl<T> NoiseFn<f64, 3> for RidgedMulti<T>
where
T: NoiseFn<f64, 3>,
{
fn get(&self, point: [f64; 3]) -> f64 {
let mut point = Vector3::from(point);
let mut result = 0.0;
let mut weight = 1.0;
let mut attenuation = 1.0;
point *= self.frequency;
for x in 0..self.octaves {
// Get the value.
let mut signal = self.sources[x].get(point.into_array());
// Make the ridges.
signal = signal.abs();
signal = 1.0 - signal;
// Square the signal to increase the sharpness of the ridges.
signal *= signal;
// Apply the weighting from the previous octave to the signal.
// Larger values have higher weights, producing sharp points along
// the ridges.
signal *= weight;
// Weight successive contributions by the previous signal.
weight = signal / self.attenuation;
// Clamp the weight to [0,1] to prevent the result from diverging.
weight = weight.clamp(0.0, 1.0);
// Scale the amplitude appropriately for this frequency.
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;
// Increase the frequency.
point *= self.lacunarity;
}
// The result before scaling will be 0 to something positive, so need to sale it back down
// to -1 to 1. We don't know what the upper limit is, but it can be calculated based on the
// number of octaves, and the persistence and attenuation values. By dividing the result by
// what the upper limit should be / 2, we should get a value between 0 and 2. Then we can
// shift the result to cover the -1 to 1 range.
// Scale the result to [0, 2]
result *= self.scale_factor;
// Shift the result to [-1, 1]
result - 1.0
}
}
/// 4-dimensional `RidgedMulti` noise
impl<T> NoiseFn<f64, 4> for RidgedMulti<T>
where
T: NoiseFn<f64, 4>,
{
fn get(&self, point: [f64; 4]) -> f64 {
let mut point = Vector4::from(point);
let mut result = 0.0;
let mut weight = 1.0;
let mut attenuation = 1.0;
point *= self.frequency;
for x in 0..self.octaves {
// Get the value.
let mut signal = self.sources[x].get(point.into_array());
// Make the ridges.
signal = signal.abs();
signal = 1.0 - signal;
// Square the signal to increase the sharpness of the ridges.
signal *= signal;
// Apply the weighting from the previous octave to the signal.
// Larger values have higher weights, producing sharp points along
// the ridges.
signal *= weight;
// Weight successive contributions by the previous signal.
weight = signal / self.attenuation;
// Clamp the weight to [0,1] to prevent the result from diverging.
weight = weight.clamp(0.0, 1.0);
// Scale the amplitude appropriately for this frequency.
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;
// Increase the frequency.
point *= self.lacunarity;
}
// The result before scaling will be 0 to something positive, so need to sale it back down
// to -1 to 1. We don't know what the upper limit is, but it can be calculated based on the
// number of octaves, and the persistence and attenuation values. By dividing the result by
// what the upper limit should be / 2, we should get a value between 0 and 2. Then we can
// shift the result to cover the -1 to 1 range.
// Scale the result to [0, 2]
result *= self.scale_factor;
// Shift the result to [-1, 1]
result - 1.0
}
}