diff options
| author | s-ol <s+removethis@s-ol.nu> | 2025-12-26 16:19:22 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-12-26 16:19:22 +0000 |
| commit | 07c7bfc188c7dee398f51e1a1ba1e333cd2cef77 (patch) | |
| tree | a152a897d6fba52e67004267ce82516ff2ee224d | |
| parent | working compilation (diff) | |
| download | nodetoy-07c7bfc188c7dee398f51e1a1ba1e333cd2cef77.tar.gz nodetoy-07c7bfc188c7dee398f51e1a1ba1e333cd2cef77.zip | |
compile and view!
| -rw-r--r-- | src/library.rs | 104 | ||||
| -rw-r--r-- | src/main.rs | 10 | ||||
| -rw-r--r-- | src/preview.rs | 2 |
3 files changed, 111 insertions, 5 deletions
diff --git a/src/library.rs b/src/library.rs index 3e83e1b..d20a133 100644 --- a/src/library.rs +++ b/src/library.rs @@ -97,6 +97,51 @@ impl<T: FixedNode> ConcreteNode for T { } } +/* +trait ProvideImpl<T> { + type Impl = T; +} + +impl ConcreteNode for ProvideImpl { + ProvideImpl::Impl::inputs(self) +} + + +/// Helper trait for Nodes with a single fixed-type output +trait OutputOnlyNode: fmt::Debug { + fn output_type(&self) -> Type; + + fn compile(&self, out_type: Type, output: &str, f: &mut dyn fmt::Write) -> fmt::Result; +} + +impl<T: OutputOnlyNode> ConcreteNode for T { + fn inputs(&self) -> usize { + 0 + } + fn outputs(&self) -> usize { + 1 + } + + fn signatures_matching(&self, connected: &[Option<Type>]) -> Vec<TypeSignature> { + vec![self.signature(connected)] + } + + fn signature(&self, connected: &[Option<Type>]) -> TypeSignature { + TypeSignature::new([], [self.output_type()]) + } + + fn compile( + &self, + signature: TypeSignature, + inputs: Vec<String>, + outputs: Vec<String>, + f: &mut dyn fmt::Write, + ) -> fmt::Result { + self.compile(signature.outputs[0], &outputs[0], f) + } +} +*/ + /// Arithmetic Operations #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum BinArithmetic { @@ -390,6 +435,7 @@ impl FixedNode for Output { TypeSignature::new([Vector(Float(Single), D4)], []), TypeSignature::new([Scalar(Float(Single))], []), TypeSignature::new([Scalar(Float(Double))], []), + TypeSignature::new([Vector(Float(Single), D2)], []), ] .into_iter(), ) @@ -403,6 +449,7 @@ impl FixedNode for Output { f: &mut dyn fmt::Write, ) -> fmt::Result { match signature.inputs[0] { + Vector(_, D2) => write!(f, "out_color = vec4({}, 0.0, 1.0);\n", inputs[0]), Vector(_, D3) => write!(f, "out_color = vec4({}, 1.0);\n", inputs[0]), Vector(_, D4) => write!(f, "out_color = {};\n", inputs[0]), Scalar(_) => write!(f, "out_color = vec4(vec3({}), 1.0);\n", inputs[0]), @@ -491,3 +538,60 @@ impl fmt::Display for Constant { write!(f, "Constant {}", self.typ) } } + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum Input { + UV, + // Resolution, +} + +impl ConcreteNode for Input { + fn inputs(&self) -> usize { + 0 + } + fn outputs(&self) -> usize { + 1 + } + + fn signatures_matching(&self, connected: &[Option<Type>]) -> Vec<TypeSignature> { + vec![self.signature(connected)] + } + + fn signature(&self, _connected: &[Option<Type>]) -> TypeSignature { + TypeSignature::new([], [Vector(Float(Single), D2)]) + } + + fn compile( + &self, + signature: TypeSignature, + _inputs: Vec<String>, + outputs: Vec<String>, + f: &mut dyn fmt::Write, + ) -> fmt::Result { + let out_typ = &signature.outputs[0]; + let out_name = &outputs[0]; + write!( + f, + "{out_typ} {out_name} = {};\n", + match self { + Input::UV => "in_uv", + } + ) + } +} +impl Input { + pub fn all() -> impl Iterator<Item = Input> { + [ + Input::UV, + // Input::Resolution, + ] + .into_iter() + } +} +impl fmt::Display for Input { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Input::UV => write!(f, "UV Coordinates"), + } + } +} diff --git a/src/main.rs b/src/main.rs index 55132bd..5e1632b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -158,6 +158,7 @@ const UNTYPED_COLOR: Color32 = Color32::from_rgb(0xb0, 0xb0, 0xb0); #[derive(Clone, PartialEq, serde::Serialize, serde::Deserialize)] enum DemoNode { Constant(library::Constant), + Input(library::Input), Builtin(library::BuiltinFunction), Output(library::Output), } @@ -210,8 +211,8 @@ impl DemoNode { } pub fn all() -> impl Iterator<Item = Self> { - library::Constant::all() - .map(DemoNode::from) + (library::Constant::all().map(DemoNode::from)) + .chain(library::Input::all().map(DemoNode::from)) .chain(library::Output::all().map(DemoNode::from)) .chain(library::BuiltinFunction::all().map(DemoNode::from)) } @@ -220,6 +221,7 @@ impl fmt::Display for DemoNode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { DemoNode::Constant(x) => fmt::Display::fmt(x, f), + DemoNode::Input(x) => fmt::Display::fmt(x, f), DemoNode::Builtin(x) => fmt::Display::fmt(x, f), DemoNode::Output(x) => fmt::Display::fmt(x, f), } @@ -251,11 +253,11 @@ impl DemoViewer { f, " #version 450 -in vec4 in_color; +in vec2 in_uv; out vec4 out_color; void main() {{ - " +" )?; while let Some(id) = order.pop() { snarl[id].compile_node(id, snarl, f)?; diff --git a/src/preview.rs b/src/preview.rs index 151f9c2..e4f7413 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -32,7 +32,7 @@ in vec2 in_uv; out vec4 out_color; void main() { - out_color = vec3(in_uv, 0.0, 0.0); + out_color = vec4(1, 0, 1, 1.0); }", )); resources.insert(format); |
