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

@ -32,6 +32,15 @@ pub fn node_definition(input: TokenStream) -> TokenStream {
TokenStream::from(expanded)
}
fn add_line_numbers(input: String) -> String {
return input
.split('\n')
.enumerate()
.map(|(i, line)| format!("{:2}: {}", i + 1, line))
.collect::<Vec<String>>()
.join("\n");
}
#[proc_macro]
pub fn include_definition_file(input: TokenStream) -> TokenStream {
let file_path = syn::parse_macro_input!(input as syn::LitStr).value();
@ -51,8 +60,9 @@ pub fn include_definition_file(input: TokenStream) -> TokenStream {
// Optionally, validate that the content is valid JSON
let _: NodeDefinition = serde_json::from_str(&json_content).unwrap_or_else(|err| {
panic!(
"JSON file contains invalid JSON: \n {} \n {}",
json_content, err
"JSON file contains invalid JSON: \n{} \n{}",
err,
add_line_numbers(json_content.clone())
)
});

View File

@ -80,10 +80,10 @@ pub struct NodeInputSelect {
pub default_options: DefaultOptions,
#[serde(skip_serializing_if = "Option::is_none")]
pub value: Option<String>,
pub value: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<HashMap<String, Value>>,
pub options: Option<Vec<String>>,
}
#[derive(Serialize, Deserialize)]

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)