fix: random node now works as expected

This commit is contained in:
2024-04-23 20:35:56 +02:00
parent 198a868fc6
commit 070a5b52d0
16 changed files with 141 additions and 84 deletions

View File

@ -13,3 +13,4 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
console_error_panic_hook = { version = "0.1.7", optional = true }
glam = "0.27.0"
noise = "0.9.0"

View File

@ -2,6 +2,7 @@ mod encoding;
mod nodes;
mod tree;
pub use encoding::*;
pub use nodes::reset_call_count;
pub use tree::*;
pub mod geometry;

View File

@ -1,4 +1,5 @@
use crate::{encoding, log};
use noise::{core::open_simplex::open_simplex_2d, permutationtable::PermutationTable, Vector2};
pub fn math_node(args: &[i32]) -> i32 {
let math_type = args[0];
@ -19,16 +20,32 @@ pub fn math_node(args: &[i32]) -> i32 {
static mut CALL_COUNT: i32 = 0;
pub fn reset_call_count() {
unsafe {
CALL_COUNT = 0;
}
}
pub fn random_node(args: &[i32]) -> i32 {
let min = encoding::decode_float(args[0]);
let max = encoding::decode_float(args[1]);
let seed = (args[2] + unsafe { CALL_COUNT } * 2312312) % 100_000;
let v = seed as f32 / 100_000.0;
log!("Random node: min: {}, max: {}, seed: {}", min, max, seed);
let result = min + v * (max - min);
let seed = (args[2] + unsafe { CALL_COUNT }) % 100_000;
let hasher = PermutationTable::new(seed as u32);
let value = open_simplex_2d(Vector2::new(seed as f64, 5.0), &hasher) as f32 + 0.5;
log!(
"Random node: min: {}, max: {}, seed: {}, v: {}",
min,
max,
seed,
value
);
let result = min + value * (max - min);
unsafe {
CALL_COUNT += 1;
CALL_COUNT = (CALL_COUNT + 512) % i32::MAX;
}
encoding::encode_float(result)