feat: some shut

This commit is contained in:
2024-04-20 04:08:30 +02:00
parent 78c88e4d66
commit 4c7c4cac2c
21 changed files with 172 additions and 171 deletions

View File

@ -53,11 +53,11 @@ pub fn extrude_path(input_path: &[i32], res_x: usize) -> Vec<i32> {
};
v = v.normalize();
let n = Vec3::new(0.0, 1.0, 0.0); // Assuming 'n' is the up vector or similar
let n = Vec3::new(0.0, -1.0, 0.0); // Assuming 'n' is the up vector or similar
let axis = n.cross(v);
let angle = n.dot(v).acos();
let quat = Quat::from_axis_angle(axis, angle);
let quat = Quat::from_axis_angle(axis, angle).normalize();
let mat = Mat4::IDENTITY * Mat4::from_quat(quat);
for j in 0..res_x {
@ -98,7 +98,7 @@ pub fn extrude_path(input_path: &[i32], res_x: usize) -> Vec<i32> {
point[2] + circle_y,
);
let pt = Mat4::transform_point3(&mat, _pt) + point;
let pt = Mat4::transform_vector3(&mat, _pt) + point;
normals[idx ] = circle_x;
normals[idx + 1] = 0.0;

View File

@ -1,78 +0,0 @@
use std::cell::RefCell;
use serde_json::Value;
use wasm_bindgen::prelude::*;
pub fn unwrap_int(val: JsValue) -> i32 {
if val.is_undefined() || val.is_null() {
panic!("Value is undefined");
}
return val.as_f64().unwrap() as i32;
}
pub fn unwrap_float(val: JsValue) -> f64 {
if val.is_undefined() || val.is_null() {
panic!("Value is undefined");
}
return val.as_f64().unwrap();
}
pub fn unwrap_string(val: JsValue) -> String {
if val.is_undefined() || val.is_null() {
panic!("Value is undefined");
}
return val.as_string().unwrap();
}
pub fn evaluate_parameters(val: JsValue) -> f64 {
let str = unwrap_string(val);
let v: Value = serde_json::from_str(&str).unwrap();
let index = RefCell::new(0.0);
return walk_json(&v, &index);
}
fn walk_json(value: &Value, depth: &RefCell<f64>) -> f64 {
*depth.borrow_mut() += 1.0;
match value {
// If it's an object, recursively walk through its fields
Value::Object(obj) => {
let obj_type = obj.get("__type").unwrap();
if obj_type == "random" {
let min = walk_json(obj.get("min").unwrap(), depth);
let max = walk_json(obj.get("max").unwrap(), depth);
let seed = (obj.get("seed").unwrap().as_f64().unwrap() + *depth.borrow() * 2000.0)
/ 1000000.0;
let range = max - min;
let seed = seed % range;
return seed - min;
} else if obj_type == "math" {
let a = walk_json(obj.get("a").unwrap(), depth);
let b = walk_json(obj.get("b").unwrap(), depth);
let op_type = obj.get("op_type").unwrap();
if op_type == 0 {
return a + b;
} else if op_type == 1 {
return a - b;
} else if op_type == 2 {
return a * b;
} else if op_type == 3 {
return a / b;
}
}
return 0.0;
}
Value::Array(arr) => {
for val in arr {
walk_json(val, depth);
}
return 0.0;
}
Value::Number(num) => {
return num.as_f64().unwrap();
}
// If it's a primitive value, print it
_ => {
return 0.0;
}
}
}

View File

@ -1,9 +1,7 @@
mod encoding;
mod helpers;
mod nodes;
mod tree;
pub use encoding::*;
pub use helpers::*;
pub use tree::*;
pub mod geometry;

View File

@ -1,4 +1,4 @@
use crate::encoding;
use crate::{encoding, log};
pub fn math_node(args: &[i32]) -> i32 {
let math_type = args[0];
@ -16,3 +16,20 @@ pub fn math_node(args: &[i32]) -> i32 {
encoding::encode_float(result)
}
static mut CALL_COUNT: i32 = 0;
pub fn random_node(args: &[i32]) -> i32 {
let min = encoding::decode_float(args[0]);
let max = encoding::decode_float(args[1]);
let seed = (args[2] + unsafe { CALL_COUNT } * 2312312) % 100_000;
let v = seed as f32 / 100_000.0;
log!("Random node: min: {}, max: {}, seed: {}", min, max, seed);
let result = min + v * (max - min);
unsafe {
CALL_COUNT += 1;
}
encoding::encode_float(result)
}

View File

@ -1,4 +1,4 @@
use crate::decode_float;
use crate::{decode_float, log};
pub fn get_args(args: &[i32]) -> Vec<&[i32]> {
let mut idx: usize = 0;
@ -103,6 +103,7 @@ pub fn evaluate_node(input_args: &[i32]) -> i32 {
match node_type {
0 => crate::nodes::math_node(&input_args[1..]),
1 => crate::nodes::random_node(&input_args[1..]),
_ => 0,
}
}