From 00776b92d3b6c4e00773e69833b6c664df02a4fb Mon Sep 17 00:00:00 2001 From: Max Richter Date: Tue, 23 Apr 2024 04:59:20 +0200 Subject: [PATCH] feat: fucking hell yeah (it works) dont look at commit time pls --- Cargo.lock | 53 ++++++++++++++++++++++- app/src/lib/result-viewer/Scene.svelte | 25 ++++++----- app/src/lib/settings/app-settings.ts | 5 +++ nodes/max/plantarium/noise/Cargo.toml | 3 +- nodes/max/plantarium/noise/src/input.json | 3 ++ nodes/max/plantarium/noise/src/lib.rs | 40 +++++++++++++++-- nodes/max/plantarium/output/src/lib.rs | 4 +- 7 files changed, 114 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8c2f21b..08521da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,6 +16,12 @@ dependencies = [ "web-sys", ] +[[package]] +name = "autocfg" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" + [[package]] name = "box" version = "0.1.0" @@ -160,11 +166,12 @@ dependencies = [ ] [[package]] -name = "noise" +name = "nodes-noise" version = "0.1.0" dependencies = [ "console_error_panic_hook", "macros", + "noise", "serde", "serde-wasm-bindgen", "utils", @@ -173,6 +180,26 @@ dependencies = [ "web-sys", ] +[[package]] +name = "noise" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6da45c8333f2e152fc665d78a380be060eb84fad8ca4c9f7ac8ca29216cff0cc" +dependencies = [ + "num-traits", + "rand", + "rand_xorshift", +] + +[[package]] +name = "num-traits" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.19.0" @@ -212,6 +239,30 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + +[[package]] +name = "rand_xorshift" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f" +dependencies = [ + "rand_core", +] + [[package]] name = "random" version = "0.1.0" diff --git a/app/src/lib/result-viewer/Scene.svelte b/app/src/lib/result-viewer/Scene.svelte index 59bd45f..070fa3b 100644 --- a/app/src/lib/result-viewer/Scene.svelte +++ b/app/src/lib/result-viewer/Scene.svelte @@ -23,16 +23,6 @@ {/if} -{#if lines} - {#each lines as line} - - - - - - {/each} -{/if} - @@ -54,6 +44,19 @@ {/if} - + {/each} + +{#if $AppSettings.showStemLines && lines} + {#each lines as line} + + + + + {/each} +{/if} diff --git a/app/src/lib/settings/app-settings.ts b/app/src/lib/settings/app-settings.ts index d49fadb..7075a38 100644 --- a/app/src/lib/settings/app-settings.ts +++ b/app/src/lib/settings/app-settings.ts @@ -58,6 +58,11 @@ export const AppSettingTypes = { label: "Show Indices", value: false, }, + showStemLines: { + type: "boolean", + label: "Show Stem Lines", + value: false, + }, stressTest: { __title: "Stress Test", amount: { diff --git a/nodes/max/plantarium/noise/Cargo.toml b/nodes/max/plantarium/noise/Cargo.toml index ba5ed1c..f1ad8ef 100644 --- a/nodes/max/plantarium/noise/Cargo.toml +++ b/nodes/max/plantarium/noise/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "noise" +name = "nodes-noise" version = "0.1.0" authors = ["Max Richter "] edition = "2018" @@ -23,6 +23,7 @@ serde = { version = "1.0", features = ["derive"] } serde-wasm-bindgen = "0.4" console_error_panic_hook = { version = "0.1.7", optional = true } web-sys = { version = "0.3.69", features = ["console"] } +noise = "0.9.0" [dev-dependencies] wasm-bindgen-test = "0.3.34" diff --git a/nodes/max/plantarium/noise/src/input.json b/nodes/max/plantarium/noise/src/input.json index f6ddf14..298f25d 100644 --- a/nodes/max/plantarium/noise/src/input.json +++ b/nodes/max/plantarium/noise/src/input.json @@ -16,6 +16,9 @@ "type": "float", "min": 0.1, "max": 100 + }, + "seed": { + "type": "seed" } } } diff --git a/nodes/max/plantarium/noise/src/lib.rs b/nodes/max/plantarium/noise/src/lib.rs index ef41096..984efc7 100644 --- a/nodes/max/plantarium/noise/src/lib.rs +++ b/nodes/max/plantarium/noise/src/lib.rs @@ -1,14 +1,46 @@ use macros::include_definition_file; -use utils::{concat_args, get_args, log}; +use noise::{core::open_simplex::open_simplex_2d, permutationtable::PermutationTable, Vector2}; +use utils::{ + concat_args, decode_float, encode_float, evaluate_float, get_args, log, set_panic_hook, +}; use wasm_bindgen::prelude::*; include_definition_file!("src/input.json"); -#[rustfmt::skip] #[wasm_bindgen] pub fn execute(input: &[i32]) -> Vec { + set_panic_hook(); + let args = get_args(input); let plants = get_args(args[0]); - log!("noise plants: {:?}", plants); - concat_args(vec![plants[0].to_vec()]) + let scale = evaluate_float(args[1]); + let strength = evaluate_float(args[2]); + let seed = args[3][0]; + + let hasher = PermutationTable::new(seed as u32); + log!("scale: {}, strength: {}, seed: {}", scale, strength, seed); + + let output: Vec> = plants + .iter() + .map(|p| { + let mut plant = p.to_vec(); + + log!("plant: {:?}", plant); + + let points = (plant.len() - 5) / 4; + for i in 0..points { + let a = i as f64 / (points - 1) as f64; + let px = Vector2::new(0.0, a * scale as f64); + let pz = Vector2::new(a * scale as f64, 0.0); + let nx = open_simplex_2d(px, &hasher) as f32 * strength * 0.1; + let nz = open_simplex_2d(pz, &hasher) as f32 * strength * 0.1; + plant[3 + i * 4] = encode_float(decode_float(plant[3 + i * 4]) + nx); + plant[5 + i * 4] = encode_float(decode_float(plant[5 + i * 4]) + nz); + } + + plant + }) + .collect(); + + concat_args(output.iter().map(|v| v.as_slice()).collect()) } diff --git a/nodes/max/plantarium/output/src/lib.rs b/nodes/max/plantarium/output/src/lib.rs index e6625fd..e9d103b 100644 --- a/nodes/max/plantarium/output/src/lib.rs +++ b/nodes/max/plantarium/output/src/lib.rs @@ -35,10 +35,10 @@ pub fn execute(input: &[i32]) -> Vec { if arg_type == 0 { // this is stem - let stem = &arg[2..arg.len() - 2]; + let stem = &arg[3..arg.len() - 2]; output.push(arg.to_vec()); + log!("stem: {:?}", stem); let geometry = extrude_path(stem, resolution); - log!("geometry: {:?}", geometry); output.push(geometry); } else if arg_type == 1 { // this is geometry