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

implement rng in place of u32 seeds #350

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
270 changes: 135 additions & 135 deletions examples/complexplanet.rs

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions examples/power.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
extern crate noise;

use noise::{utils::*, Perlin, Power};
use noise::{utils::*, Perlin, Power, DEFAULT_SEED};
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

mod utils;

fn main() {
let perlin1 = Perlin::default();
let perlin2 = Perlin::new(1);
let perlin2 = Perlin::new(XorShiftRng::from_seed(DEFAULT_SEED).gen());
let power = Power::new(perlin1, perlin2);

utils::write_example_to_file(&PlaneMapBuilder::new(power).build(), "power.png");
Expand Down
10 changes: 7 additions & 3 deletions examples/ridgedmulti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

extern crate noise;

use noise::{utils::*, Perlin, RidgedMulti, Worley};
use noise::{utils::*, Perlin, RidgedMulti, Worley, DEFAULT_SEED};
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

mod utils;

fn main() {
let ridged_multi = RidgedMulti::<Perlin>::default();
let mut rng = XorShiftRng::from_seed(DEFAULT_SEED);

let ridged_multi = RidgedMulti::<Perlin>::new(rng.gen());

utils::write_example_to_file(
&PlaneMapBuilder::new(ridged_multi).build(),
Expand All @@ -21,7 +25,7 @@ fn main() {
"ridged_multi_worley.png",
);

let ridged_multi = RidgedMulti::<RidgedMulti<Perlin>>::default();
let ridged_multi = RidgedMulti::<RidgedMulti<Perlin>>::new(rng.gen());

utils::write_example_to_file(
&PlaneMapBuilder::new(ridged_multi).build(),
Expand Down
10 changes: 7 additions & 3 deletions examples/texturegranite.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
extern crate noise;

use noise::{core::worley::ReturnType, utils::*, *};
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

mod utils;

fn main() {
let mut rng = XorShiftRng::from_seed(DEFAULT_SEED);

// Primary granite texture. This generates the "roughness" of the texture
// when lit by a light source.
let primary_granite = Billow::<Perlin>::new(0)
let primary_granite = Billow::<Perlin>::new(rng.gen())
.set_frequency(8.0)
.set_persistence(0.625)
.set_lacunarity(2.18359375)
.set_octaves(6);

// Use Worley polygons to produce the small grains for the granite texture.
let base_grains = Worley::new(1)
let base_grains = Worley::new(rng.gen())
.set_frequency(16.0)
.set_return_type(ReturnType::Distance);

Expand All @@ -28,7 +32,7 @@ fn main() {

// Finally, perturb the granite texture to add realism.
let final_granite = Turbulence::<_, Perlin>::new(combined_granite)
.set_seed(2)
.set_seed(rng.gen())
.set_frequency(4.0)
.set_power(1.0 / 8.0)
.set_roughness(6);
Expand Down
10 changes: 7 additions & 3 deletions examples/texturejade.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
extern crate noise;

use noise::{utils::*, *};
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

mod utils;

fn main() {
let mut rng = XorShiftRng::from_seed(DEFAULT_SEED);

// Primary jade texture. The ridges from the ridged-multifractal function
// produces the veins.
let primary_jade = RidgedMulti::<Perlin>::new(0)
let primary_jade = RidgedMulti::<Perlin>::new(rng.gen())
.set_frequency(2.0)
.set_lacunarity(2.20703125)
.set_octaves(6);
Expand All @@ -24,7 +28,7 @@ fn main() {

// Slightly perturb the secondary jade texture for more realism.
let perturbed_base_secondary_jade = Turbulence::<_, Perlin>::new(rotated_base_secondary_jade)
.set_seed(1)
.set_seed(rng.gen())
.set_frequency(4.0)
.set_power(1.0 / 4.0)
.set_roughness(4);
Expand All @@ -43,7 +47,7 @@ fn main() {
// Finally, perturb the combined jade texture to produce the final jade
// texture. A low roughness produces nice veins.
let final_jade = Turbulence::<_, Perlin>::new(combined_jade)
.set_seed(2)
.set_seed(rng.gen())
.set_frequency(4.0)
.set_power(1.0 / 16.0)
.set_roughness(2);
Expand Down
12 changes: 8 additions & 4 deletions examples/textureslime.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
extern crate noise;

use noise::{utils::*, *};
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

mod utils;

fn main() {
let mut rng = XorShiftRng::from_seed(DEFAULT_SEED);

// Large slime bubble texture.
let large_slime = Billow::<Perlin>::new(0)
let large_slime = Billow::<Perlin>::new(rng.gen())
.set_frequency(4.0)
.set_lacunarity(2.12109375)
.set_octaves(1);

// Base of the small slime bubble texture. This texture will eventually
// appear inside cracks in the large slime bubble texture.
let small_slime_base = Billow::<Perlin>::new(1)
let small_slime_base = Billow::<Perlin>::new(rng.gen())
.set_frequency(24.0)
.set_lacunarity(2.14453125)
.set_octaves(1);
Expand All @@ -25,7 +29,7 @@ fn main() {

// Create a map that specifies where the large and small slime bubble
// textures will appear in the final texture map.
let slime_map = RidgedMulti::<Perlin>::new(2)
let slime_map = RidgedMulti::<Perlin>::new(rng.gen())
.set_frequency(2.0)
.set_lacunarity(2.20703125)
.set_octaves(3);
Expand All @@ -42,7 +46,7 @@ fn main() {

// Finally, perturb the slime texture to add realism.
let final_slime = Turbulence::<_, Perlin>::new(slime_chooser)
.set_seed(3)
.set_seed(rng.gen())
.set_frequency(8.0)
.set_power(1.0 / 32.0)
.set_roughness(2);
Expand Down
10 changes: 7 additions & 3 deletions examples/texturewood.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
extern crate noise;

use noise::{utils::*, *};
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

mod utils;

fn main() {
let mut rng = XorShiftRng::from_seed(DEFAULT_SEED);

// Base wood texture. Uses concentric cylinders aligned on the z-axis, like a log.
let base_wood = Cylinders::new().set_frequency(16.0);

// Basic Multifractal noise to use for the wood grain.
let wood_grain_noise = BasicMulti::<Perlin>::new(0)
let wood_grain_noise = BasicMulti::<Perlin>::new(rng.gen())
.set_frequency(48.0)
.set_persistence(0.5)
.set_lacunarity(2.20703125)
Expand All @@ -29,7 +33,7 @@ fn main() {

// Slightly perturb the wood to create a more realistic texture.
let perturbed_wood = Turbulence::<_, Perlin>::new(combined_wood)
.set_seed(1)
.set_seed(rng.gen())
.set_frequency(4.0)
.set_power(1.0 / 256.0)
.set_roughness(4);
Expand All @@ -42,7 +46,7 @@ fn main() {

// Finally, perturb the wood texture again to produce the final texture.
let final_wood = Turbulence::<_, Perlin>::new(rotated_wood)
.set_seed(2)
.set_seed(rng.gen())
.set_frequency(2.0)
.set_power(1.0 / 64.0)
.set_roughness(4);
Expand Down
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] += x as u8;
sources.push(source.set_seed(seed));
}
sources
}
15 changes: 8 additions & 7 deletions src/noise_fns/generators/fractals/basicmulti.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 @@ -44,7 +45,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 +55,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 +93,7 @@ where
T: Default + Seedable,
{
fn default() -> Self {
Self::new(Self::DEFAULT_SEED)
Self::new(DEFAULT_SEED)
}
}

Expand Down Expand Up @@ -135,7 +136,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 +148,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/billow.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 +42,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 +52,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 +89,7 @@ where
T: Default + Seedable,
{
fn default() -> Self {
Self::new(Self::DEFAULT_SEED)
Self::new(DEFAULT_SEED)
}
}

Expand Down Expand Up @@ -131,7 +132,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 +144,7 @@ where
}
}

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