Skip to content

Commit

Permalink
implement rng in place of u32 seeds
Browse files Browse the repository at this point in the history
Closes #197
  • Loading branch information
Razaekel committed May 19, 2024
1 parent aab9e56 commit 3710f0f
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 138 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//! # Example
//!
//! ```rust
//! use noise::{NoiseFn, Perlin, Seedable};
//! use noise::{NoiseFn, Perlin, Seedable, DEFAULT_SEED};
//!
//! let perlin = Perlin::new(1);
//! let perlin = Perlin::new(DEFAULT_SEED);
//! let val = perlin.get([42.4, 37.7, 2.8]);
//! ```

Expand Down
10 changes: 8 additions & 2 deletions src/noise_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ where
/// Trait for functions that require a seed before generating their values
pub trait Seedable {
/// Set the seed for the function implementing the `Seedable` trait
fn set_seed(self, seed: u32) -> Self;
fn set_seed(self, seed: Seed) -> Self;

/// Getter to retrieve the seed from the function
fn seed(&self) -> u32;
fn seed(&self) -> Seed;
}

pub type Seed = [u8; 16];

pub const DEFAULT_SEED: Seed = [
0x4f, 0x09, 0xd6, 0x9f, 0x62, 0x9b, 0x09, 0x0c, 0x0c, 0x49, 0x09, 0xfe, 0x6f, 0x1d, 0x4a, 0x38,
];
8 changes: 5 additions & 3 deletions src/noise_fns/generators/fractals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod fbm;
mod hybridmulti;
mod ridgedmulti;

use crate::Seedable;
use crate::{Seed, Seedable};

/// Trait for `MultiFractal` functions
pub trait MultiFractal {
Expand All @@ -20,14 +20,16 @@ pub trait MultiFractal {
fn set_persistence(self, persistence: f64) -> Self;
}

fn build_sources<Source>(seed: u32, octaves: usize) -> Vec<Source>
fn build_sources<Source>(seed: Seed, octaves: usize) -> Vec<Source>
where
Source: Default + Seedable,
{
let mut sources = Vec::with_capacity(octaves);
for x in 0..octaves {
let source = Source::default();
sources.push(source.set_seed(seed + x as u32));
let mut seed = seed;
seed[0] = seed[0] + x as u8;

Check failure on line 31 in src/noise_fns/generators/fractals.rs

View workflow job for this annotation

GitHub Actions / Clippy

manual implementation of an assign operation
sources.push(source.set_seed(seed));
}
sources
}
14 changes: 7 additions & 7 deletions src/noise_fns/generators/fractals/basicmulti.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
math::vectors::*,
noise_fns::{MultiFractal, NoiseFn, Seedable},
noise_fns::{MultiFractal, NoiseFn, Seedable, DEFAULT_SEED}, Seed,
};
use alloc::vec::Vec;

Expand Down Expand Up @@ -44,7 +44,8 @@ pub struct BasicMulti<T> {
/// persistence produces "rougher" noise.
pub persistence: f64,

seed: u32,
seed: Seed,

sources: Vec<T>,
scale_factor: f64,
}
Expand All @@ -53,14 +54,13 @@ impl<T> BasicMulti<T>
where
T: Default + Seedable,
{
pub const DEFAULT_SEED: u32 = 0;
pub const DEFAULT_OCTAVES: usize = 6;
pub const DEFAULT_FREQUENCY: f64 = 2.0;
pub const DEFAULT_LACUNARITY: f64 = core::f64::consts::PI * 2.0 / 3.0;
pub const DEFAULT_PERSISTENCE: f64 = 0.5;
pub const MAX_OCTAVES: usize = 32;

pub fn new(seed: u32) -> Self {
pub fn new(seed: Seed) -> Self {
Self {
seed,
octaves: Self::DEFAULT_OCTAVES,
Expand Down Expand Up @@ -92,7 +92,7 @@ where
T: Default + Seedable,
{
fn default() -> Self {
Self::new(Self::DEFAULT_SEED)
Self::new(DEFAULT_SEED)
}
}

Expand Down Expand Up @@ -135,7 +135,7 @@ impl<T> Seedable for BasicMulti<T>
where
T: Default + Seedable,
{
fn set_seed(self, seed: u32) -> Self {
fn set_seed(self, seed: Seed) -> Self {
if self.seed == seed {
return self;
}
Expand All @@ -147,7 +147,7 @@ where
}
}

fn seed(&self) -> u32 {
fn seed(&self) -> Seed {
self.seed
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/noise_fns/generators/fractals/billow.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
math::{scale_shift, vectors::*},
noise_fns::{MultiFractal, NoiseFn, Seedable},
noise_fns::{MultiFractal, NoiseFn, Seedable, DEFAULT_SEED}, Seed,
};
use alloc::vec::Vec;

Expand Down Expand Up @@ -41,7 +41,8 @@ pub struct Billow<T> {
/// persistence produces "rougher" noise.
pub persistence: f64,

seed: u32,
seed: Seed,

sources: Vec<T>,
scale_factor: f64,
}
Expand All @@ -50,14 +51,13 @@ impl<T> Billow<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 = 0.5;
pub const MAX_OCTAVES: usize = 32;

pub fn new(seed: u32) -> Self {
pub fn new(seed: Seed) -> Self {
Self {
seed,
octaves: Self::DEFAULT_OCTAVE_COUNT,
Expand Down Expand Up @@ -88,7 +88,7 @@ where
T: Default + Seedable,
{
fn default() -> Self {
Self::new(Self::DEFAULT_SEED)
Self::new(DEFAULT_SEED)
}
}

Expand Down Expand Up @@ -131,7 +131,7 @@ impl<T> Seedable for Billow<T>
where
T: Default + Seedable,
{
fn set_seed(self, seed: u32) -> Self {
fn set_seed(self, seed: Seed) -> Self {
if self.seed == seed {
return self;
}
Expand All @@ -143,7 +143,7 @@ where
}
}

fn seed(&self) -> u32 {
fn seed(&self) -> Seed {
self.seed
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/noise_fns/generators/fractals/fbm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
math::vectors::*,
noise_fns::{MultiFractal, NoiseFn, Seedable},
noise_fns::{MultiFractal, NoiseFn, Seedable, DEFAULT_SEED}, Seed,
};
use alloc::vec::Vec;

Expand Down Expand Up @@ -50,7 +50,7 @@ pub struct Fbm<T> {
/// persistence produces "rougher" noise.
pub persistence: f64,

seed: u32,
seed: Seed,
sources: Vec<T>,
scale_factor: f64,
}
Expand All @@ -59,14 +59,13 @@ impl<T> Fbm<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 = 0.5;
pub const MAX_OCTAVES: usize = 32;

pub fn new(seed: u32) -> Self {
pub fn new(seed: Seed) -> Self {
Self {
seed,
octaves: Self::DEFAULT_OCTAVE_COUNT,
Expand Down Expand Up @@ -97,7 +96,7 @@ where
T: Default + Seedable,
{
fn default() -> Self {
Self::new(Self::DEFAULT_SEED)
Self::new(DEFAULT_SEED)
}
}

Expand Down Expand Up @@ -140,7 +139,7 @@ impl<T> Seedable for Fbm<T>
where
T: Default + Seedable,
{
fn set_seed(self, seed: u32) -> Self {
fn set_seed(self, seed: Seed) -> Self {
if self.seed == seed {
return self;
}
Expand All @@ -152,7 +151,7 @@ where
}
}

fn seed(&self) -> u32 {
fn seed(&self) -> Seed {
self.seed
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/noise_fns/generators/fractals/hybridmulti.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
math::vectors::*,
noise_fns::{MultiFractal, NoiseFn, Seedable},
noise_fns::{MultiFractal, NoiseFn, Seedable, DEFAULT_SEED}, Seed,
};
use alloc::vec::Vec;

Expand Down Expand Up @@ -38,7 +38,7 @@ pub struct HybridMulti<T> {
/// persistence produces "rougher" noise.
pub persistence: f64,

seed: u32,
seed: Seed,
sources: Vec<T>,
scale_factor: f64,
}
Expand All @@ -47,14 +47,13 @@ impl<T> HybridMulti<T>
where
T: Default + Seedable,
{
pub const DEFAULT_SEED: u32 = 0;
pub const DEFAULT_OCTAVES: usize = 6;
pub const DEFAULT_FREQUENCY: f64 = 2.0;
pub const DEFAULT_LACUNARITY: f64 = core::f64::consts::PI * 2.0 / 3.0;
pub const DEFAULT_PERSISTENCE: f64 = 0.25;
pub const MAX_OCTAVES: usize = 32;

pub fn new(seed: u32) -> Self {
pub fn new(seed: Seed) -> Self {
Self {
seed,
octaves: Self::DEFAULT_OCTAVES,
Expand Down Expand Up @@ -100,7 +99,7 @@ where
T: Default + Seedable,
{
fn default() -> Self {
Self::new(Self::DEFAULT_SEED)
Self::new(DEFAULT_SEED)
}
}

Expand Down Expand Up @@ -143,7 +142,7 @@ impl<T> Seedable for HybridMulti<T>
where
T: Default + Seedable,
{
fn set_seed(self, seed: u32) -> Self {
fn set_seed(self, seed: Seed) -> Self {
if self.seed == seed {
return self;
}
Expand All @@ -155,7 +154,7 @@ where
}
}

fn seed(&self) -> u32 {
fn seed(&self) -> Seed {
self.seed
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/noise_fns/generators/fractals/ridgedmulti.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
math::vectors::*,
noise_fns::{MultiFractal, NoiseFn, Seedable},
noise_fns::{MultiFractal, NoiseFn, Seedable, DEFAULT_SEED},
Seed,
};
use alloc::vec::Vec;

Expand Down Expand Up @@ -56,7 +57,8 @@ pub struct RidgedMulti<T> {
/// half the height of the previous.
pub attenuation: f64,

seed: u32,
seed: Seed,

sources: Vec<T>,
scale_factor: f64,
}
Expand All @@ -65,15 +67,14 @@ 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 {
pub fn new(seed: Seed) -> Self {
Self {
seed,
octaves: Self::DEFAULT_OCTAVE_COUNT,
Expand Down Expand Up @@ -130,7 +131,7 @@ where
T: Default + Seedable,
{
fn default() -> Self {
Self::new(Self::DEFAULT_SEED)
Self::new(DEFAULT_SEED)
}
}

Expand Down Expand Up @@ -173,7 +174,7 @@ impl<T> Seedable for RidgedMulti<T>
where
T: Default + Seedable,
{
fn set_seed(self, seed: u32) -> Self {
fn set_seed(self, seed: Seed) -> Self {
if self.seed == seed {
return self;
}
Expand All @@ -185,7 +186,7 @@ where
}
}

fn seed(&self) -> u32 {
fn seed(&self) -> Seed {
self.seed
}
}
Expand Down
Loading

0 comments on commit 3710f0f

Please sign in to comment.