feat: add rotate node
All checks were successful
Deploy to GitHub Pages / build_site (push) Successful in 1m54s

This commit is contained in:
2024-05-02 03:37:30 +02:00
parent e2b18370f1
commit 5fe0c8a795
18 changed files with 294 additions and 38 deletions

View File

@@ -73,7 +73,7 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
let px = j as f64 + a * length * scale;
let py = a * scale as f64;
path.points[i * 4] += noise_x.get([px, py]) as f32
path.points[i * 4] = noise_x.get([px, py]) as f32
* directional_strength[0]
* strength
* lerp(1.0, a as f32, fix_bottom);

View File

@@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log

View File

@@ -0,0 +1,29 @@
[package]
name = "rotate"
version = "0.1.0"
authors = ["Max Richter <jim-x@web.de>"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.84"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
utils = { version = "0.1.0", path = "../../../../packages/utils" }
macros = { version = "0.1.0", path = "../../../../packages/macros" }
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"] }
glam = "0.27.0"
[dev-dependencies]
wasm-bindgen-test = "0.3.34"

View File

@@ -0,0 +1,6 @@
{
"scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features",
"dev": "cargo watch -s 'wasm-pack build --dev --out-name index --no-default-features'"
}
}

View File

@@ -0,0 +1,36 @@
{
"id": "max/plantarium/rotate",
"meta": {
"description": "The rotate node rotates a plant around a specified axis by a specified amount."
},
"outputs": [
"path"
],
"inputs": {
"plant": {
"type": "path"
},
"axis": {
"type": "select",
"internal": true,
"label": "",
"options": ["x", "y", "z"],
"description": "Along which axis should we rotate?"
},
"spread": {
"type": "boolean",
"internal": true,
"hidden": true,
"value": true,
"description": "If multiple objects are connected, should we rotate them as one or spread them?"
},
"angle": {
"type": "float",
"min": 0,
"max": 6.28318530718,
"step": 0.05,
"value": 0,
"description": "Rotation angle"
}
}
}

View File

@@ -0,0 +1,72 @@
use glam::{Mat4, Vec3};
use macros::include_definition_file;
use utils::{
concat_args, evaluate_float, evaluate_int, geometry::wrap_path_mut, log, set_panic_hook,
split_args,
};
use wasm_bindgen::prelude::*;
include_definition_file!("src/input.json");
#[wasm_bindgen]
pub fn execute(input: &[i32]) -> Vec<i32> {
set_panic_hook();
log!("DEBUG args: {:?}", input);
let args = split_args(input);
let plants = split_args(args[0]);
let axis = evaluate_int(args[1]); // 0 =x, 1 = y, 2 = z
let spread = evaluate_int(args[2]);
let angle = evaluate_float(args[3]);
let output: Vec<Vec<i32>> = plants
.iter()
.enumerate()
.map(|(j, _path_data)| {
let mut path_data = _path_data.to_vec();
// if this is not a path don't modify it
if path_data[2] != 0 {
return path_data;
}
let path = wrap_path_mut(&mut path_data);
let length = path.get_length() as f64;
let origin = [path.points[0], path.points[1], path.points[2]];
let axis = match axis {
0 => Vec3::X,
1 => Vec3::Y,
2 => Vec3::Z,
_ => panic!("Invalid axis"),
};
let rotation = if spread == 1 {
let angle = angle * (j as f32 / plants.len() as f32);
Mat4::from_axis_angle(axis, angle)
} else {
Mat4::from_axis_angle(axis, angle)
};
for i in 0..path.length {
let mut p = Vec3::new(
path.points[i * 4] - origin[0],
path.points[i * 4 + 1] - origin[1],
path.points[i * 4 + 2] - origin[2],
);
p = rotation.transform_vector3(p);
path.points[i * 4] = p.x + origin[0];
path.points[i * 4 + 1] = p.y + origin[1];
path.points[i * 4 + 2] = p.z + origin[2];
}
path_data
})
.collect();
concat_args(output.iter().map(|x| x.as_slice()).collect())
}

View File

@@ -0,0 +1,13 @@
//! Test suite for the Web and headless browsers.
#![cfg(target_arch = "wasm32")]
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
}