This repository has been archived by the owner on Oct 17, 2021. It is now read-only.
forked from tgstation/rust-g
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shim for Rust noise library, exposing 2d Perlin noise functions (t…
…gstation#28) Provides an interface to get 2d Perlin noise generated by the Rust [noise] library. rustg_noise_get_at_coordinates(seed, x, y) takes an integer seed and floating-point coordinates, and returns a floating-point value in the range [0,1]. [noise]: https://docs.rs/noise/0.6.0/noise/
- Loading branch information
Showing
7 changed files
with
189 additions
and
4 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use noise::{NoiseFn, Perlin, Seedable}; | ||
use std::cell::RefCell; | ||
use std::collections::HashMap; | ||
use std::collections::hash_map::Entry; | ||
|
||
use crate::error::Result; | ||
|
||
thread_local! { | ||
static GENERATORS: RefCell<HashMap<String, Perlin>> = RefCell::new(HashMap::new()); | ||
} | ||
|
||
byond_fn! { noise_get_at_coordinates(seed, x, y) { | ||
get_at_coordinates(seed, x, y).ok() | ||
} } | ||
|
||
//note that this will be 0 at integer x & y, scaling is left up to the caller | ||
fn get_at_coordinates(seed_as_str: &str, x_as_str: &str, y_as_str: &str) -> Result<String> { | ||
let x = x_as_str.parse::<f64>()?; | ||
let y = y_as_str.parse::<f64>()?; | ||
GENERATORS.with(|cell| { | ||
let mut generators = cell.borrow_mut(); | ||
let mut entry = generators.entry(seed_as_str.to_string()); | ||
let generator = match entry { | ||
Entry::Occupied(ref mut occ) => occ.get_mut(), | ||
Entry::Vacant(vac) => { | ||
let seed = seed_as_str.parse::<u32>()?; | ||
let perlin = Perlin::new().set_seed(seed); | ||
vac.insert(perlin) | ||
} | ||
}; | ||
//perlin noise produces a result in [-sqrt(0.5), sqrt(0.5)] which we scale to [0, 1] for simplicity | ||
let unscaled = generator.get([x, y]); | ||
let scaled_from_0_to_1 = (unscaled * 2.0_f64.sqrt() + 1.0) / 2.0; | ||
Ok(scaled_from_0_to_1.to_string()) | ||
}) | ||
} |