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

This commit is contained in:
2024-04-25 13:15:24 +02:00
parent 41ca2123ba
commit c1e6d141bf
14 changed files with 390 additions and 26 deletions

View File

@@ -65,6 +65,14 @@
"min": 3,
"max": 64,
"setting": "resolution.curve"
},
"rotation": {
"type": "float",
"hidden": true,
"min": 0,
"max": 360,
"step": 0.01,
"value": 0
}
}
}

View File

@@ -1,10 +1,10 @@
use std::f32::consts::PI;
use glam::Vec3;
use macros::include_definition_file;
use std::f32::consts::PI;
use utils::{
concat_arg_vecs, evaluate_float, evaluate_int,
geometry::{create_path, get_direction_at_path, get_point_at_path, wrap_path, wrap_path_mut},
geometry::{
create_path, interpolate_along_path, rotate_vector_by_angle, wrap_path, wrap_path_mut,
},
log, set_panic_hook, split_args,
};
use wasm_bindgen::prelude::*;
@@ -51,42 +51,35 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
let length = evaluate_float(args[1]);
let thickness = evaluate_float(args[2]);
let offset_single = evaluate_float(args[3]);
let offset_single = if i % 2 == 0 {
evaluate_float(args[3])
} else {
0.0
};
// log!("a: {}, length: {}, thickness: {}, offset_single: {}, lowest_branch: {}, highest_branch: {}", a, length, thickness, offset_single, lowest_branch, highest_branch);
// log!("a: {}, length: {}, thickness: {}, offset_single: {}, lowest_branch: {}, highest_branch: {}", a, length, thickness, offset_single, lowest_branch, highest_branch);
let root_alpha = (a * (highest_branch - lowest_branch) + lowest_branch)
.min(1.0)
.max(0.0);
let is_left = i % 2 == 0;
let (branch_origin, orthogonal, direction) = interpolate_along_path(
path.points,
root_alpha + (offset_single - 0.5) * 6.0 / resolution as f32,
);
let branch_origin = get_point_at_path(path.points, root_alpha);
//const [_vx, , _vz] = interpolateSkeletonVec(stem.skeleton, a);
let direction_slice = get_direction_at_path(path.points, root_alpha);
let direction = Vec3::from_slice(&direction_slice).normalize();
let rotation_angle = if is_left { PI } else { -PI };
let rotation_angle = (evaluate_float(args[9]) * PI / 180.0) * i as f32;
// check if diration contains NaN
if direction[0].is_nan() || direction[1].is_nan() || direction[2].is_nan() {
if orthogonal[0].is_nan() || orthogonal[1].is_nan() || orthogonal[2].is_nan() {
log!(
"BRANCH direction contains NaN: {:?}, slice: {:?} branch_origin: {:?}, branch: {}",
"BRANCH direction contains NaN: {:?}, branch_origin: {:?}, branch: {}",
direction,
direction_slice,
branch_origin,
i
);
continue;
}
let branch_direction = Vec3::from_slice(&[
direction[0] * rotation_angle.cos() - direction[2] * rotation_angle.sin(),
0.0,
direction[0] * rotation_angle.sin() + direction[2] * rotation_angle.cos(),
])
.normalize();
let branch_direction = rotate_vector_by_angle(orthogonal, direction, rotation_angle);
log!(
"BRANCH depth: {}, branch_origin: {:?}, direction_at: {:?}, branch_direction: {:?}",

View File

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

View File

@@ -0,0 +1,30 @@
[package]
name = "gravity"
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"] }
noise = "0.9.0"
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,29 @@
{
"id": "max/plantarium/gravity",
"outputs": [
"path"
],
"inputs": {
"plant": {
"type": "path"
},
"strength": {
"type": "float",
"min": 0.1,
"max": 1
},
"curviness": {
"type": "float",
"hidden": true,
"min": 0.1,
"max": 1
},
"depth": {
"type": "integer",
"min": 1,
"max": 10,
"value": 1,
"hidden": true
}
}
}

View File

@@ -0,0 +1,129 @@
use glam::Vec3;
use macros::include_definition_file;
use utils::{
concat_args, evaluate_float, evaluate_int, geometry::wrap_path_mut, log, reset_call_count,
set_panic_hook, split_args,
};
use wasm_bindgen::prelude::*;
include_definition_file!("src/input.json");
fn lerp_vec3(a: Vec3, b: Vec3, t: f32) -> Vec3 {
a + (b - a) * t
}
#[wasm_bindgen]
pub fn execute(input: &[i32]) -> Vec<i32> {
set_panic_hook();
reset_call_count();
let args = split_args(input);
let plants = split_args(args[0]);
let depth = evaluate_int(args[3]);
let mut max_depth = 0;
for path_data in plants.iter() {
if path_data[2] != 0 {
continue;
}
max_depth = max_depth.max(path_data[3]);
}
let output: Vec<Vec<i32>> = plants
.iter()
.map(|_path_data| {
let mut path_data = _path_data.to_vec();
if path_data[2] != 0 || path_data[3] < (max_depth - depth + 1) {
return path_data;
}
let path = wrap_path_mut(&mut path_data);
let mut offset_vec = Vec3::ZERO;
for i in 1..path.length {
// let alpha = i as f32 / (path.length - 1) as f32;
let start_index = (i - 1) * 4;
let end_index = start_index + 4;
let start_point = Vec3::from_slice(&path.points[start_index..start_index + 3]);
let end_point = Vec3::from_slice(&path.points[end_index..end_index + 3]);
log!("--------------------------------");
log!(
"start_index: {:?} end_index: {:?} length:{}",
start_index,
end_index,
path.points.len()
);
if start_point[0].is_nan() {
log!("start_point is nan {:?}", path.points);
continue;
}
log!("start_point: {:?}", start_point);
log!("end_point: {:?}", end_point);
let length = (end_point - start_point).length();
let normalised = (end_point - start_point).normalize();
if normalised[0].is_nan() {
log!("normalised is nan {:?}", normalised);
continue;
}
let strength = evaluate_float(args[1]);
let down_point = Vec3::new(0.0, -length * strength, 0.0);
if down_point[0].is_nan() {
log!("down_point is nan {:?}", down_point);
continue;
}
let curviness = evaluate_float(args[2]);
let mut mid_point = lerp_vec3(
normalised,
down_point,
curviness * (i as f32 / path.length as f32).sqrt(),
);
if mid_point[0].is_nan() {
log!("mid_point is nan {:?}", mid_point);
log!("normalised: {:?}", normalised);
log!("curviness: {:?}", curviness);
continue;
}
if mid_point[0] == 0.0 && mid_point[2] == 0.0 {
mid_point[0] += 0.0001;
mid_point[2] += 0.0001;
}
mid_point = mid_point.normalize();
mid_point *= length;
let final_end_point = start_point + mid_point;
let offset_end_point = end_point + offset_vec;
if offset_end_point[0].is_nan() {
log!("offset_end_point is nan {:?}", offset_end_point);
continue;
}
path.points[end_index] = offset_end_point[0];
path.points[end_index + 1] = offset_end_point[1];
path.points[end_index + 2] = offset_end_point[2];
let offset = final_end_point - end_point;
offset_vec += offset;
}
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);
}

View File

@@ -21,7 +21,7 @@
"type": "float",
"label": "Fixate bottom of plant",
"hidden": true,
"value": 1,
"value": 1.0,
"min": 0,
"max": 1
},