feat: yaaay first stem

This commit is contained in:
2024-04-18 00:02:50 +02:00
parent 1da13523ea
commit 2edd22136f
21 changed files with 297 additions and 102 deletions

View File

@@ -1,4 +1,4 @@
use crate::geometry::calculate_normals::calculate_normals;
use crate::geometry::calculate_normals;
use macros::include_definition_file;
use utils::{
decode_float, encode_float, evaluate_args, geometry, get_args, set_panic_hook, wrap_arg,

View File

@@ -1,36 +1,31 @@
use macros::include_definition_file;
use utils::{concat_args, decode_float, encode_float, get_args};
use utils::{concat_args, geometry::extrude_path, get_args};
use wasm_bindgen::prelude::*;
use web_sys::console;
include_definition_file!("src/inputs.json");
#[rustfmt::skip]
#[wasm_bindgen]
pub fn execute(input: &[i32]) -> Vec<i32> {
pub fn execute(input: Vec<i32>) -> Vec<i32> {
utils::set_panic_hook();
let args = get_args(input);
let args = get_args(input.as_slice());
console::log_1(&format!("WASM(output_node): input: {:?}", args).into());
let mut output:Vec<&[i32]> = Vec::new();
let mut output:Vec<Vec<i32>> = Vec::new();
for arg in args {
if arg.len() < 3 {
continue;
if arg.len() < 3 { continue; }
if arg[2] == 0 {
let _arg = &arg[3..];
let geometry = extrude_path(_arg, 8);
output.push(geometry);
}else if arg[2] == 1 {
output.push(arg.to_vec());
}
output.push( match arg[2] {
// stem
0 => &[0,1,1,1],
// geometry
1 => arg,
_ => &[0,1,1,1],
})
}
concat_args(output)
}

View File

@@ -1,42 +1,42 @@
use macros::include_definition_file;
use utils::{evaluate_args, get_args};
use utils::{decode_float, evaluate_args, get_args, log, set_panic_hook, wrap_arg};
use wasm_bindgen::prelude::*;
use web_sys::console;
include_definition_file!("src/input.json");
#[wasm_bindgen]
pub fn execute(input: &[i32]) -> Vec<i32> {
set_panic_hook();
let args = get_args(input);
let length = evaluate_args(args[0]);
let thickness = evaluate_args(args[1]);
let resolution = 32; //evaluate_args(args[2]);
let length = decode_float(evaluate_args(args[0])[0]);
let thickness = decode_float(evaluate_args(args[1])[0]);
let resolution = 64; //evaluate_args(args[2]);
console::log_1(
&format!(
"length: {:?}, thickness: {:?}, resolution: {:?}",
length, thickness, resolution
)
.into(),
);
let mut path: Vec<i32> = vec![0; resolution * 4 + 1];
path.resize(resolution * 4 + 1, 0);
vec![
0, 1, 0, // opening bracket
11, // opening bracket
0, // type: plant
0, // alpha: 0
0, // x
0, // y
0, // z
1, // thickness
0, // x
2, // y
0, // z
1, // thickness
1, // closing bracket
1, //closing bracket
1, // closing bracket
1, //closing bracket
]
path[0] = 0;
let slice = &mut path[1..];
// Unsafe code to transmute the i32 slice to an f32 slice
let path_p: &mut [f32] = unsafe {
// Ensure that the length of the slice is a multiple of 4
assert_eq!(slice.len() % 4, 0);
// Transmute the i32 slice to an f32 slice
std::slice::from_raw_parts_mut(slice.as_ptr() as *mut f32, slice.len())
};
for i in 0..resolution {
let a = i as f32 / resolution as f32;
path_p[i * 4] = (a * 8.0).sin() * 0.2;
path_p[i * 4 + 1] = a * length;
path_p[i * 4 + 2] = 0.0;
path_p[i * 4 + 3] = thickness * (1.0 - a);
}
wrap_arg(&path)
}

View File

@@ -1,5 +1,5 @@
use macros::include_definition_file;
use utils::{concat_args, decode_float, encode_float};
use utils::{concat_args, decode_float, encode_float, wrap_arg};
use wasm_bindgen::prelude::*;
use web_sys::console;
@@ -16,8 +16,7 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
console::log_1(&format!("WASM(triangle): input: {:?} -> {}", input, decoded).into());
// [[1,3, x, y, z, x, y,z,x,y,z]];
concat_args(vec![&[
0, 19, // opening bracket + distance to next bracket
wrap_arg(&[
1, // 1: geometry
3, // 3 vertices
1, // 1 face
@@ -37,8 +36,6 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
0, // x -> point 3
0, // y
size, // z
//
1, 1 // closing brackets
]])
])
}