diff --git a/Cargo.lock b/Cargo.lock index 53fd35f..1710c1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -302,7 +302,7 @@ dependencies = [ ] [[package]] -name = "triangle" +name = "template" version = "0.1.0" dependencies = [ "console_error_panic_hook", diff --git a/app/src/lib/graph-interface/graph-manager.ts b/app/src/lib/graph-interface/graph-manager.ts index 2f8604f..7df1de9 100644 --- a/app/src/lib/graph-interface/graph-manager.ts +++ b/app/src/lib/graph-interface/graph-manager.ts @@ -8,6 +8,8 @@ import type { NodeInput } from "@nodes/types"; const logger = createLogger("graph-manager"); +logger.mute(); + function areSocketsCompatible(output: string | undefined, inputs: string | string[] | undefined) { if (Array.isArray(inputs) && output) { return inputs.includes(output); @@ -225,6 +227,32 @@ export class GraphManager extends EventEmitter<{ "save": Graph, "result": any, " return this.registry.getNode(id); } + async loadNode(id: string) { + + await this.registry.load([id]); + const nodeType = this.registry.getNode(id); + + if (!nodeType) return; + + const settingTypes = this.settingTypes; + const settingValues = this.settings; + if (nodeType.inputs) { + for (const key in nodeType.inputs) { + let settingId = nodeType.inputs[key].setting; + if (settingId) { + settingTypes[settingId] = nodeType.inputs[key]; + if (settingValues[settingId] === undefined && "value" in type.inputs[key]) { + settingValues[settingId] = nodeType.inputs[key].value; + } + } + } + } + + this.settings = settingValues; + this.settingTypes = settingTypes; + this.emit("settings", { types: settingTypes, values: settingValues }); + } + getChildrenOfNode(node: Node) { const children = []; const stack = node.tmp?.children?.slice(0); @@ -349,6 +377,8 @@ export class GraphManager extends EventEmitter<{ "save": Graph, "result": any, " return; } + + const node: Node = { id: this.createNodeId(), type, position, tmp: { type: nodeType }, props }; this.nodes.update((nodes) => { diff --git a/app/src/lib/graph-interface/graph/Graph.svelte b/app/src/lib/graph-interface/graph/Graph.svelte index 6496788..67b078d 100644 --- a/app/src/lib/graph-interface/graph/Graph.svelte +++ b/app/src/lib/graph-interface/graph/Graph.svelte @@ -773,14 +773,13 @@ } const pos = projectScreenToWorld(mx, my); - graph.registry.load([nodeId]).then(() => { + graph.loadNode(nodeId).then(() => { graph.createNode({ type: nodeId, props: {}, position: pos, }); }); - console.log({ nodeId }); } function handlerDragOver(e: DragEvent) { diff --git a/app/src/lib/node-registry-client.ts b/app/src/lib/node-registry-client.ts index 35af521..48567d1 100644 --- a/app/src/lib/node-registry-client.ts +++ b/app/src/lib/node-registry-client.ts @@ -10,7 +10,7 @@ export class RemoteNodeRegistry implements NodeRegistry { constructor(private url: string) { } - private async loadNode(id: `${string}/${string}/${string}`) { + async loadNode(id: `${string}/${string}/${string}`) { const wasmResponse = await this.fetchNode(id); // Setup Wasm wrapper @@ -81,6 +81,8 @@ export class RemoteNodeRegistry implements NodeRegistry { log.log("loaded nodes in", duration, "ms"); this.status = "ready"; + + return nodes } getNode(id: string) { diff --git a/app/src/lib/node-store/BreadCrumbs.svelte b/app/src/lib/node-store/BreadCrumbs.svelte index 9e2c9fb..87270c3 100644 --- a/app/src/lib/node-store/BreadCrumbs.svelte +++ b/app/src/lib/node-store/BreadCrumbs.svelte @@ -1,19 +1,15 @@ @@ -61,7 +57,8 @@ cursor: pointer; } - .breadcrumbs > button::after { + .breadcrumbs > button::after, + .breadcrumbs :global(select)::after { content: "/"; position: absolute; right: -11px; @@ -78,4 +75,9 @@ font-size: 1em; opacity: 0.5; } + + .breadcrumbs :global(select) { + padding: 3px 5px; + outline: none; + } diff --git a/app/src/lib/node-store/NodeStore.svelte b/app/src/lib/node-store/NodeStore.svelte index 4e52b64..1ed77da 100644 --- a/app/src/lib/node-store/NodeStore.svelte +++ b/app/src/lib/node-store/NodeStore.svelte @@ -26,7 +26,9 @@
{#if !activeUser} -

Users

+
+

Users

+
{#await nodeRegistry.fetchUsers()}
Loading...
{:then users} @@ -44,7 +46,15 @@ {#await nodeRegistry.fetchUser(activeUser)}
Loading...
{:then user} -

Collections

+
+ +

Collections

+
{#each user.collections as collection} +

Nodes

+
{#await nodeRegistry.fetchCollection(`${activeUser}/${activeCollection}`)}
Loading...
{:then collection} {#each collection.nodes as node} - + {#await nodeRegistry.fetchNodeDefinition(node.id)} +
Loading...
+ {:then node} + + {:catch error} +
{error.message}
+ {/await} {/each} {:catch error}
{error.message}
{/await} - {:else} - {#await nodeRegistry.fetchNodeDefinition(`${activeUser}/${activeCollection}/${activeNode}`)} -
Loading...
- {:then node} - - {:catch error} -
{error.message}
- {/await} {/if} diff --git a/app/src/lib/runtime-executor.ts b/app/src/lib/runtime-executor.ts index 284d676..384d41c 100644 --- a/app/src/lib/runtime-executor.ts +++ b/app/src/lib/runtime-executor.ts @@ -111,6 +111,8 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor { // here we store the intermediate results of the nodes const results: Record = {}; + const runSeed = settings["randomSeed"] === true ? Math.floor(Math.random() * 100000000) : 5120983; + for (const node of sortedNodes) { const node_type = this.typeMap.get(node.type)!; @@ -120,7 +122,7 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor { for (const [key, input] of Object.entries(node_type.inputs || {})) { if (input.type === "seed") { - inputs[key] = Math.floor(Math.random() * 100000000); + inputs[key] = runSeed; continue; } diff --git a/app/src/routes/+page.svelte b/app/src/routes/+page.svelte index 2403834..775ce64 100644 --- a/app/src/routes/+page.svelte +++ b/app/src/routes/+page.svelte @@ -93,8 +93,16 @@ icon: "i-tabler-chart-bar", id: "graph", settings: writable(ev.detail.values), - definition: ev.detail.types, + definition: { + randomSeed: { + type: "boolean", + label: "Random Seed", + value: true, + }, + ...ev.detail.types, + }, }; + settings = settings; } diff --git a/nodes/max/plantarium/.template/Cargo.toml b/nodes/max/plantarium/.template/Cargo.toml index 2b724c9..9185f6a 100644 --- a/nodes/max/plantarium/.template/Cargo.toml +++ b/nodes/max/plantarium/.template/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "triangle" +name = "template" version = "0.1.0" authors = ["Max Richter "] edition = "2018" diff --git a/nodes/max/plantarium/.template/package.json b/nodes/max/plantarium/.template/package.json index a601fc9..86916c9 100644 --- a/nodes/max/plantarium/.template/package.json +++ b/nodes/max/plantarium/.template/package.json @@ -1,6 +1,6 @@ { "scripts": { "build": "wasm-pack build --release --out-name index --no-default-features", - "dev": "cargo watch -s 'pnpm build'" + "dev": "cargo watch -s 'wasm-pack build --dev --out-name index --no-default-features'" } } diff --git a/nodes/max/plantarium/random/src/definition.json b/nodes/max/plantarium/random/src/definition.json new file mode 100644 index 0000000..d9db7c0 --- /dev/null +++ b/nodes/max/plantarium/random/src/definition.json @@ -0,0 +1,18 @@ +{ + "outputs": [ + "float" + ], + "inputs": { + "min": { + "type": "float", + "value": 2 + }, + "max": { + "type": "float", + "value": 2 + }, + "seed": { + "type": "seed" + } + } +} diff --git a/nodes/max/plantarium/random/src/lib.rs b/nodes/max/plantarium/random/src/lib.rs index a62d940..a553ec8 100644 --- a/nodes/max/plantarium/random/src/lib.rs +++ b/nodes/max/plantarium/random/src/lib.rs @@ -1,48 +1,22 @@ -use macros::define_node; +use macros::include_definition_file; use wasm_bindgen::prelude::*; -define_node!( - r#"{ - "outputs": ["float"], - "inputs": { - "min": { "type": "float", "value": 2 }, - "max": { "type": "float", "value": 2 }, - "seed": { "type": "seed" } - } - }"# -); +include_definition_file!("src/definition.json"); #[wasm_bindgen] pub fn execute(args: &[i32]) -> Vec { - // let min: String; - // if var_min.is_string() { - // min = unwrap_string(var_min); - // } else { - // min = unwrap_int(var_min).to_string(); - // } - // - // let max: String; - // if var_max.is_string() { - // max = unwrap_string(var_max); - // } else { - // max = unwrap_int(var_max).to_string(); - // } - // - // let seed: String; - // if var_seed.is_string() { - // seed = unwrap_string(var_seed); - // } else { - // seed = unwrap_int(var_seed).to_string(); - // } - // - // log(&format!("min: {}, max: {}, seed: {}", min, max, seed)); - // - // // Interpolate strings into JSON format - // let json_string = format!( - // r#"{{"__type": "random", "min": {}, "max": {}, "seed": {}}}"#, - // min, max, seed - // ); + let mut result = Vec::with_capacity(args.len() + 7); + result.push(0); + result.push(1); + result.push(0); // encoding the [ bracket + result.push(args[1] + 1); - // json_string - vec![1, args[0]] + result.push(1); // adding the node-type, random: 0 + result.extend_from_slice(&args[2..]); + + result.push(1); + result.push(1); // closing bracket + result.push(1); + result.push(1); // closing bracket + result } diff --git a/nodes/max/plantarium/stem/src/lib.rs b/nodes/max/plantarium/stem/src/lib.rs index e608e67..7221388 100644 --- a/nodes/max/plantarium/stem/src/lib.rs +++ b/nodes/max/plantarium/stem/src/lib.rs @@ -33,7 +33,7 @@ pub fn execute(input: &[i32]) -> Vec { let a = i as f32 / (res_curve - 1) as f32; path_p[i * 4] = origin[0] + (a * 8.0).sin() * 0.2; path_p[i * 4 + 1] = origin[1] + a * length; - path_p[i * 4 + 2] = origin[2] + 0.0; + path_p[i * 4 + 2] = origin[2] + ((a + 2.0) * 8.0).sin() * 0.2; path_p[i * 4 + 3] = thickness * (1.0 - a); } diff --git a/nodes/max/plantarium/triangle/package.json b/nodes/max/plantarium/triangle/package.json index a601fc9..86916c9 100644 --- a/nodes/max/plantarium/triangle/package.json +++ b/nodes/max/plantarium/triangle/package.json @@ -1,6 +1,6 @@ { "scripts": { "build": "wasm-pack build --release --out-name index --no-default-features", - "dev": "cargo watch -s 'pnpm build'" + "dev": "cargo watch -s 'wasm-pack build --dev --out-name index --no-default-features'" } } diff --git a/nodes/max/plantarium/triangle/src/lib.rs b/nodes/max/plantarium/triangle/src/lib.rs index 3af6ada..fedc895 100644 --- a/nodes/max/plantarium/triangle/src/lib.rs +++ b/nodes/max/plantarium/triangle/src/lib.rs @@ -1,5 +1,5 @@ use macros::include_definition_file; -use utils::{decode_float, encode_float, wrap_arg}; +use utils::{decode_float, encode_float, evaluate_arg, evaluate_float, get_args, wrap_arg}; use wasm_bindgen::prelude::*; use web_sys::console; @@ -9,11 +9,15 @@ include_definition_file!("src/input.json"); #[wasm_bindgen] pub fn execute(input: &[i32]) -> Vec { - let size = input[2]; - let decoded = decode_float(input[2]); + utils::set_panic_hook(); + + let args = get_args(input); + + let size = evaluate_arg(args[0]); + let decoded = decode_float(size); let negative_size = encode_float(-decoded); - console::log_1(&format!("WASM(triangle): input: {:?} -> {}", input, decoded).into()); + console::log_1(&format!("WASM(triangle): input: {:?} -> {}", args[0],decoded).into()); // [[1,3, x, y, z, x, y,z,x,y,z]]; wrap_arg(&[ @@ -22,8 +26,6 @@ pub fn execute(input: &[i32]) -> Vec { 1, // 1 face // this are the indeces for the face 0, 2, 1, - // this is the normal for the single face 1065353216 == 1.0f encoded is i32 - 0, 1065353216, 0, // negative_size, // x -> point 1 0, // y @@ -36,6 +38,10 @@ pub fn execute(input: &[i32]) -> Vec { 0, // x -> point 3 0, // y size, // z + // this is the normal for the single face 1065353216 == 1.0f encoded is i32 + 0, 1065353216, 0, + 0, 1065353216, 0, + 0, 1065353216, 0, ]) } diff --git a/nodes/max/plantarium/vec3/package.json b/nodes/max/plantarium/vec3/package.json index a601fc9..86916c9 100644 --- a/nodes/max/plantarium/vec3/package.json +++ b/nodes/max/plantarium/vec3/package.json @@ -1,6 +1,6 @@ { "scripts": { "build": "wasm-pack build --release --out-name index --no-default-features", - "dev": "cargo watch -s 'pnpm build'" + "dev": "cargo watch -s 'wasm-pack build --dev --out-name index --no-default-features'" } } diff --git a/packages/utils/src/geometry/extrude_path.rs b/packages/utils/src/geometry/extrude_path.rs index 89184e3..ac1d271 100644 --- a/packages/utils/src/geometry/extrude_path.rs +++ b/packages/utils/src/geometry/extrude_path.rs @@ -53,11 +53,11 @@ pub fn extrude_path(input_path: &[i32], res_x: usize) -> Vec { }; 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 { 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; diff --git a/packages/utils/src/helpers.rs b/packages/utils/src/helpers.rs deleted file mode 100644 index 4a0e6c4..0000000 --- a/packages/utils/src/helpers.rs +++ /dev/null @@ -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 { - *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; - } - } -} diff --git a/packages/utils/src/lib.rs b/packages/utils/src/lib.rs index a01de3b..a12b170 100644 --- a/packages/utils/src/lib.rs +++ b/packages/utils/src/lib.rs @@ -1,9 +1,7 @@ mod encoding; -mod helpers; mod nodes; mod tree; pub use encoding::*; -pub use helpers::*; pub use tree::*; pub mod geometry; diff --git a/packages/utils/src/nodes.rs b/packages/utils/src/nodes.rs index cf3d203..a624960 100644 --- a/packages/utils/src/nodes.rs +++ b/packages/utils/src/nodes.rs @@ -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) +} diff --git a/packages/utils/src/tree.rs b/packages/utils/src/tree.rs index 15184ed..9e161a9 100644 --- a/packages/utils/src/tree.rs +++ b/packages/utils/src/tree.rs @@ -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, } }