feat: instance node
All checks were successful
Deploy to GitHub Pages / build_site (push) Successful in 2m44s

This commit is contained in:
2024-05-06 01:10:23 +02:00
parent a01a409b97
commit 10a12ad41c
20 changed files with 695 additions and 147 deletions

View File

@@ -1,7 +1,7 @@
use nodarium_macros::include_definition_file;
use nodarium_utils::{
encode_float, evaluate_float, geometry::calculate_normals, log, set_panic_hook, split_args,
wrap_arg,
concat_args, encode_float, evaluate_float, geometry::calculate_normals, log, set_panic_hook,
split_args, wrap_arg,
};
use wasm_bindgen::prelude::*;
@@ -81,7 +81,7 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
let res = wrap_arg(&cube_geometry);
log!("WASM(cube): output: {:?}", res);
log!("WASM(box): output: {:?}", res);
res

View File

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

View File

@@ -0,0 +1,29 @@
[package]
name = "nodarium_instance"
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.
nodarium_utils = { version = "0.1.0", path = "../../../../packages/utils" }
nodarium_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,32 @@
{
"id": "max/plantarium/instance",
"outputs": [
"path"
],
"inputs": {
"plant": {
"type": "path"
},
"geometry": {
"type": "geometry",
"value": [0,0,0]
},
"amount": {
"type": "integer"
},
"lowestInstance": {
"type": "float",
"min":0,
"max":1,
"value": 0.5,
"hidden": true
},
"highestInstance": {
"type": "float",
"min":0,
"max":1,
"value":1,
"hidden": true
}
}
}

View File

@@ -0,0 +1,119 @@
use glam::{Mat4, Quat, Vec3};
use nodarium_macros::include_definition_file;
use nodarium_utils::{
concat_args, encode_float, evaluate_float, evaluate_int,
geometry::{
calculate_normals, create_instance_data, wrap_geometry_data, wrap_instance_data, wrap_path,
},
log, set_panic_hook, split_args, wrap_arg,
};
use wasm_bindgen::prelude::*;
include_definition_file!("src/input.json");
#[rustfmt::skip]
fn create_geo() -> Vec<i32> {
let size = 5.0;
let p = encode_float(size);
let n = encode_float(-size);
// [[1,3, x, y, z, x, y,z,x,y,z]];
wrap_arg(calculate_normals(&mut [
1, // 1: geometry
8, // 8 vertices
12, // 12 faces
/*
Indeces:
5----6
| 4--+-7
0-|--1 |
3----2
*/
// this are the indeces for the face
0, 1, 2,
0, 2, 3,
0, 3, 4,
4, 5, 0,
6, 1, 0,
5, 6, 0,
7, 2, 1,
6, 7, 1,
2, 7, 3,
3, 7, 4,
7, 6, 4,
4, 6, 5, // Bottom plate
p, n, n,
p, n, p,
n, n, p,
n, n, n, // Top Plate
n, p, n,
p, p, n,
p, p, p,
n, p, p, // this is the normal for every single vert 1065353216 == 1.0f encoded is i32
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
]))
}
#[wasm_bindgen]
pub fn execute(input: &[i32]) -> Vec<i32> {
set_panic_hook();
let args = split_args(input);
let mut inputs = split_args(args[0]);
log!("WASM(instance): inputs: {:?}", inputs);
let mut geo_data = args[1].to_vec();
let geo = wrap_geometry_data(&mut geo_data);
let mut transforms: Vec<Mat4> = Vec::new();
for path_data in inputs.iter() {
let amount = evaluate_int(args[2]);
let lowest_instance = evaluate_float(args[3]);
let highest_instance = evaluate_float(args[4]);
let path = wrap_path(path_data);
for i in 0..amount {
let alpha =
lowest_instance + (i as f32 / amount as f32) * (highest_instance - lowest_instance);
let point = path.get_point_at(alpha);
let direction = path.get_direction_at(alpha);
let transform = Mat4::from_scale_rotation_translation(
Vec3::new(0.1, 0.1, 0.1),
Quat::from_xyzw(direction[0], direction[1], direction[2], 1.0).normalize(),
Vec3::from_slice(&point),
);
transforms.push(transform);
}
}
let mut instance_data = create_instance_data(
geo.positions.len() / 3,
geo.faces.len() / 3,
transforms.len(),
);
let mut instances = wrap_instance_data(&mut instance_data);
instances.set_geometry(geo);
(0..transforms.len()).for_each(|i| {
instances.set_transformation_matrix(i, &transforms[i].to_cols_array());
});
log!("WASM(instance): geo: {:?}", instance_data);
inputs.push(&instance_data);
concat_args(inputs)
}

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

@@ -12,6 +12,8 @@ include_definition_file!("src/inputs.json");
pub fn execute(input: &[i32]) -> Vec<i32> {
set_panic_hook();
log!("WASM(output): input: {:?}", input);
let args = split_args(input);
log!("WASM(output) args: {:?}", args);
@@ -29,16 +31,15 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
log!("arg_type: {}, \n {:?}", arg_type, arg,);
if arg_type == 0 {
// this is path
let vec = arg.to_vec();
output.push(vec.clone());
// if this is path we need to extrude it
output.push(arg.to_vec());
let path_data = wrap_path(arg);
let geometry = extrude_path(path_data, resolution);
output.push(geometry);
} else if arg_type == 1 {
// this is geometry
output.push(arg.to_vec());
continue;
}
output.push(arg.to_vec());
}
concat_args(output.iter().map(|v| v.as_slice()).collect())