feat: yaaay first stem

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

1
Cargo.lock generated
View File

@ -374,6 +374,7 @@ dependencies = [
"serde",
"serde_json",
"wasm-bindgen",
"web-sys",
]
[[package]]

View File

@ -1,9 +1,5 @@
import type { Graph, NodeRegistry, NodeType, RuntimeExecutor } from "@nodes/types";
import { encodeFloat } from "./helpers/encode";
import { concat_encoded } from "./helpers/flat_tree";
import { fastHash } from "./helpers/fastHash";
import { fastHash, concat_encoded, encodeFloat } from "@nodes/utils"
export class MemoryRuntimeExecutor implements RuntimeExecutor {

View File

@ -16,16 +16,20 @@
}
</script>
<T.GridHelper args={[20, 20]} />
<T.PerspectiveCamera position={[-10, 10, 10]} makeDefault fov={50}>
<OrbitControls />
</T.PerspectiveCamera>
<T.DirectionalLight position={[0, 10, 10]} />
<T.AmbientLight intensity={0.5} />
{#each geometry as geo}
{#each geo.attributes.position.array as attr, i}
{#if false}
{#each geo.attributes.position.array as _, i}
{#if i % 3 === 0}
<Text text={i / 3} fontSize={1} position={getPosition(geo, i)} />
<Text text={i / 3} fontSize={0.25} position={getPosition(geo, i)} />
{/if}
{/each}
@ -33,12 +37,8 @@
<T is={geo} />
<T.PointsMaterial size={0.25} />
</T.Points>
{/if}
<T.Mesh geometry={geo}>
<T.MeshStandardMaterial color="hotpink" />
</T.Mesh>
{:else}
<T.Mesh>
<T.BoxGeometry args={[1, 1, 1]} />
<T.MeshStandardMaterial color="hotpink" />
<T.MeshStandardMaterial color="green" />
</T.Mesh>
{/each}

View File

@ -38,12 +38,13 @@
);
index = index + vertexCount * 3;
console.log({ vertices, normals, indices });
// Add data to geometry
geometry.setIndex([...indices]);
geometry.setAttribute("position", new Float32BufferAttribute(vertices, 3));
geometry.setAttribute("normal", new Float32BufferAttribute(normals, 3));
// geometry.computeVertexNormals();
//geometry.computeVertexNormals();
geometry.computeVertexNormals();
return geometry;
}
@ -94,19 +95,19 @@
$: if (result) {
const inputs = parse_args(result);
for (let input of inputs) {
console.log({ inputs });
geometries = inputs
.map((input) => {
if (input[0] === 1) {
const geo = createGeometryFromEncodedData(input);
geometries = [geo];
console.log(geo);
}
return geo;
}
})
.filter(Boolean) as BufferGeometry[];
}
</script>
<Canvas>
<Scene geometry={geometries} />
</Canvas>
<style>
</style>

View File

@ -5,8 +5,7 @@
import { RemoteNodeRegistry } from "$lib/node-registry";
import * as templates from "$lib/graph-templates";
import type { Graph } from "@nodes/types";
import { decode, encode } from "$lib/helpers/flat_tree";
import { decodeFloat, encodeFloat } from "$lib/helpers/encode";
import { decode, encode, decodeFloat, encodeFloat } from "@nodes/utils";
import Viewer from "$lib/viewer/Viewer.svelte";
globalThis.decode = decode;
@ -14,6 +13,41 @@
globalThis.df = decodeFloat;
globalThis.en = encodeFloat;
globalThis.ci = function createIndeces(resX: number, stemLength = 1) {
const index = new Uint16Array(resX * (Math.max(stemLength, 1) - 1) * 6);
for (let i = 0; i < stemLength; i++) {
const indexOffset = i * resX * 6;
const positionOffset = i * resX;
for (let j = 0; j < resX; j++) {
const _indexOffset = indexOffset + j * 6;
const _positionOffset = positionOffset + j;
console.log(`iio: ${_indexOffset} pio: ${_positionOffset} j: ${j}`);
if (j === resX - 1) {
index[_indexOffset + 0] = _positionOffset + 1;
index[_indexOffset + 1] = _positionOffset - resX + 1;
index[_indexOffset + 2] = _positionOffset;
index[_indexOffset + 3] = _positionOffset;
index[_indexOffset + 4] = _positionOffset + resX;
index[_indexOffset + 5] = _positionOffset + 1;
} else {
index[_indexOffset + 0] = _positionOffset + resX + 1;
index[_indexOffset + 1] = _positionOffset + 1;
index[_indexOffset + 2] = _positionOffset;
index[_indexOffset + 3] = _positionOffset;
index[_indexOffset + 4] = _positionOffset + resX;
index[_indexOffset + 5] = _positionOffset + resX + 1;
}
}
}
return index;
};
const nodeRegistry = new RemoteNodeRegistry("http://localhost:3001");
const runtimeExecutor = new MemoryRuntimeExecutor(nodeRegistry);
@ -28,7 +62,7 @@
let a = performance.now();
res = runtimeExecutor.execute(event.detail);
time = performance.now() - a;
console.log(res);
console.log({ res, time });
}
function handleSave(event: CustomEvent<Graph>) {

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
]])
])
}

View File

@ -8,6 +8,7 @@ default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.92"
web-sys = { version = "0.3.69", features = ["console"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
console_error_panic_hook = { version = "0.1.7", optional = true }

View File

@ -1 +1,115 @@
pub fn extrude_path(path: &[i32]) {}
use crate::log;
use super::create_empty_geometry;
use glam::{Mat4, Quat, Vec3};
fn create_circle(res: usize) -> Vec<f32> {
let angle = (2.0 * std::f32::consts::PI) / res as f32;
let mut circle = Vec::new();
for i in 0..res {
circle.push((angle * i as f32).cos());
circle.push((angle * i as f32).sin());
}
circle
}
#[rustfmt::skip]
pub fn extrude_path(input_path: &[i32], res_x: usize) -> Vec<i32> {
let point_amount = input_path.len() / 4;
let face_amount = (point_amount - 1) * res_x * 2;
let vertices_amount = point_amount * res_x;
let circle = create_circle(res_x);
let mut geometry = create_empty_geometry(vertices_amount, face_amount);
let (_header,rest) = geometry.split_at_mut(5);
let (indices, rest) = rest.split_at_mut(face_amount*3);
let (_positions, _normals) = rest.split_at_mut(vertices_amount*3);
let positions: &mut [f32];
let normals: &mut [f32];
let path: &[f32];
unsafe {
path = std::slice::from_raw_parts(input_path.as_ptr() as *const f32, input_path.len());
positions = std::slice::from_raw_parts_mut(_positions.as_mut_ptr() as *mut f32, _positions.len());
normals = std::slice::from_raw_parts_mut(_normals.as_mut_ptr() as *mut f32, _normals.len());
}
normals[0] = 0.0;
for i in 0..point_amount {
let index_offset = i * res_x * 6;
let position_offset = i * res_x;
let point = Vec3::from_slice(&path[i*4..i*4+3]);
let thickness = path[i*4+3];
let next_point = if i == point_amount - 1 { point } else { Vec3::from_slice(&path[(i+1)*4..(i+1)*4+3]) };
let prev_point = if i == 0 { point } else { Vec3::from_slice(&path[(i-1)*4..(i-1)*4+3]) };
let mut v = if i == 0 {
point - next_point
} else if i == point_amount - 1 {
prev_point - point
} else {
prev_point - next_point
};
v = v.normalize();
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 mat = Mat4::IDENTITY * Mat4::from_quat(quat);
for j in 0..res_x {
if i < point_amount - 1 {
let i_index_offset = index_offset + j * 6;
let i_position_offset = position_offset + j;
if j == res_x - 1 {
indices[i_index_offset ] = (i_position_offset + 1) as i32;
indices[i_index_offset + 1] = (i_position_offset - res_x + 1) as i32;
indices[i_index_offset + 2] = (i_position_offset) as i32;
indices[i_index_offset + 3] = (i_position_offset) as i32;
indices[i_index_offset + 4] = (i_position_offset + res_x) as i32;
indices[i_index_offset + 5] = (i_position_offset + 1) as i32;
} else {
indices[i_index_offset ] = (i_position_offset + res_x + 1) as i32;
indices[i_index_offset + 1] = (i_position_offset + 1) as i32;
indices[i_index_offset + 2] = (i_position_offset) as i32;
indices[i_index_offset + 3] = (i_position_offset) as i32;
indices[i_index_offset + 4] = (i_position_offset + res_x) as i32;
indices[i_index_offset + 5] = (i_position_offset + res_x + 1) as i32;
}
}
// construct the points
let idx = i * res_x * 3 + j * 3;
let circle_x = circle[j * 2 ] * thickness;
let circle_y = circle[j * 2 + 1] * thickness;
let _pt = Vec3::new(
point[0] + circle_x,
point[1],
point[2] + circle_y,
);
let pt = Mat4::transform_point3(&mat, _pt) + point;
normals[idx ] = circle_x;
normals[idx + 1] = 0.0;
normals[idx + 2] = circle_y;
positions[idx ] = pt[0];
positions[idx + 1] = pt[1];
positions[idx + 2] = pt[2];
}
}
geometry
}

View File

@ -1,3 +1,34 @@
pub mod calculate_normals;
pub mod extrude_path;
mod calculate_normals;
mod extrude_path;
pub use calculate_normals::*;
pub use extrude_path::*;
use crate::log;
#[rustfmt::skip]
pub fn create_empty_geometry(vertex_amount: usize, face_amount: usize) -> Vec<i32> {
log!(
"create_empty_geometry: vertex_amount: {}, face_amount: {}",
vertex_amount,
face_amount
);
let amount =
3 // definition (type, vertex_amount, face_amount)
+ 4 // opening and closing brackets
+ vertex_amount * 3 // positions
+ vertex_amount * 3 // normals
+ face_amount * 3; // faces
let mut vec: Vec<i32> = vec![0; amount];
vec[0] = 0; // opening bracket
vec[1] = amount as i32 - 2; // opening bracket
vec[2] = 1; // type: geometry
vec[3] = vertex_amount as i32;
vec[4] = face_amount as i32;
vec[amount - 2] = 1; // closing bracket
vec[amount - 1] = 1; // closing bracket
vec
}

View File

@ -1 +1,4 @@
export * from "./wasm-wrapper";
export * from "./flatTree"
export * from "./encoding"
export * from "./fastHash"

View File

@ -7,6 +7,14 @@ pub use helpers::*;
pub use tree::*;
pub mod geometry;
#[macro_export]
macro_rules! log {
($($arg:tt)*) => {{
use web_sys::console;
console::log_1(&format!($($arg)*).into());
}}
}
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then

View File

@ -57,20 +57,34 @@ pub fn get_args(args: &[i32]) -> Vec<&[i32]> {
out_args
}
pub fn concat_args(args: Vec<&[i32]>) -> Vec<i32> {
let total_length: usize = args.iter().map(|arg| arg.len()).sum();
pub fn concat_args(mut data: Vec<Vec<i32>>) -> Vec<i32> {
let mut total_length = 4; // Start with 4 to account for [0, 1] at the start and [1, 1] at the end
let mut out_args = Vec::with_capacity(total_length + 4);
out_args.extend_from_slice(&[0, 1]);
for arg in args {
out_args.extend_from_slice(arg);
// Calculate the total length first to avoid reallocations
for vec in &data {
total_length += vec.len(); // +4 for [0, 1] and [1, 1] per inner vec
}
out_args.extend_from_slice(&[1, 1]);
let mut result = Vec::with_capacity(total_length);
out_args
// Add [0, 1] initially
// result.push(0);
// result.push(1);
// Process each vector
for vec in data.iter_mut() {
result.push(0);
result.push(1);
result.append(vec);
result.push(1);
result.push(1);
}
// Add [1, 1] at the end
// result.push(1);
// result.push(1);
result
}
pub fn wrap_arg(arg: &[i32]) -> Vec<i32> {