diff --git a/Cargo.lock b/Cargo.lock
index 79cc47d..bb61929 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2681,6 +2681,7 @@ dependencies = [
"materialcraft-core",
"materialcraft-image",
"materialcraft-nodes",
+ "serde",
"serde_json",
]
diff --git a/assets/sbs/functions.sbs b/assets/sbs/functions.sbs
index c9713d3..39203b9 100644
--- a/assets/sbs/functions.sbs
+++ b/assets/sbs/functions.sbs
@@ -1 +1 @@
-
+
diff --git a/crates/core/src/base_parameter.rs b/crates/core/src/base_parameter.rs
new file mode 100644
index 0000000..cbef563
--- /dev/null
+++ b/crates/core/src/base_parameter.rs
@@ -0,0 +1,140 @@
+use serde::{Deserialize, Serialize};
+
+use crate::{BaseParameters, OutputFormat, ParameterInheritance, SizeMode, Value, ValueType};
+
+/// Native base-parameter identities are distinct from node-specific parameter identifiers
+#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
+#[serde(rename_all = "lowercase")]
+pub enum BaseParameter {
+ OutputSize,
+ Format,
+ PixelSize,
+ PixelRatio,
+ Tiling,
+ RandomSeed,
+}
+
+impl BaseParameter {
+ pub const ALL: [Self; 6] = [Self::OutputSize, Self::Format, Self::PixelSize, Self::PixelRatio, Self::Tiling, Self::RandomSeed];
+
+ pub fn named(name: &str) -> Option {
+ Self::ALL.into_iter().find(|parameter| parameter.identifier() == name)
+ }
+
+ pub fn identifier(self) -> &'static str {
+ match self {
+ Self::OutputSize => "outputsize",
+ Self::Format => "format",
+ Self::PixelSize => "pixelsize",
+ Self::PixelRatio => "pixelratio",
+ Self::Tiling => "tiling",
+ Self::RandomSeed => "randomseed",
+ }
+ }
+
+ pub fn label(self) -> &'static str {
+ match self {
+ Self::OutputSize => "Output size",
+ Self::Format => "Output format",
+ Self::PixelSize => "Pixel size",
+ Self::PixelRatio => "Pixel ratio",
+ Self::Tiling => "Tiling mode",
+ Self::RandomSeed => "Random seed",
+ }
+ }
+
+ pub fn value_type(self) -> ValueType {
+ match self {
+ Self::OutputSize => ValueType::Integer2,
+ Self::PixelSize => ValueType::Float2,
+ _ => ValueType::Integer,
+ }
+ }
+
+ pub fn value(self, base: &BaseParameters) -> Value {
+ match self {
+ Self::OutputSize => Value::Int2(base.size),
+ Self::Format => Value::Int(match base.format {
+ OutputFormat::U8 => 0,
+ OutputFormat::Inherit | OutputFormat::U16 => 1,
+ OutputFormat::F16 => 2,
+ OutputFormat::F32 => 3,
+ }),
+ Self::PixelSize => Value::Float2(base.pixel_size),
+ Self::PixelRatio => Value::Int(base.pixel_ratio),
+ Self::Tiling => Value::Int(i32::from(base.tiling[0]) | (i32::from(base.tiling[1]) << 1)),
+ Self::RandomSeed => Value::Int(base.seed),
+ }
+ }
+
+ pub fn set_value(self, base: &mut BaseParameters, value: &Value) -> Result<(), String> {
+ if ValueType::of(value) != Some(self.value_type()) {
+ return Err(format!("{} requires {}", self.label(), self.value_type().label()));
+ }
+
+ match (self, value) {
+ (Self::OutputSize, Value::Int2(size)) => base.size = size.map(|component| component.clamp(-12, 13)),
+ (Self::Format, Value::Int(format)) => {
+ base.format = match format {
+ 0 => OutputFormat::U8,
+ 1 => OutputFormat::U16,
+ 2 => OutputFormat::F16,
+ 3 => OutputFormat::F32,
+ _ => return Err("Output format must be between 0 and 3".into()),
+ };
+ }
+ (Self::PixelSize, Value::Float2(size)) => {
+ if size.iter().any(|component| !component.is_finite() || *component <= 0.0 || *component > 8192.0) {
+ return Err("Pixel size components must be finite and between 0 and 8192, excluding zero".into());
+ }
+
+ base.pixel_size = *size;
+ }
+ (Self::PixelRatio, Value::Int(ratio)) if (0..=1).contains(ratio) => base.pixel_ratio = *ratio,
+ (Self::Tiling, Value::Int(tiling)) if (0..=3).contains(tiling) => base.tiling = [tiling & 1 != 0, tiling & 2 != 0],
+ (Self::RandomSeed, Value::Int(seed)) => base.seed = *seed,
+ _ => return Err(format!("{} is outside its supported range", self.label())),
+ }
+
+ Ok(())
+ }
+
+ pub fn inheritance(self, base: &BaseParameters) -> ParameterInheritance {
+ match self {
+ Self::OutputSize => match base.size_mode {
+ SizeMode::Absolute => ParameterInheritance::Absolute,
+ SizeMode::RelativeToInput => ParameterInheritance::RelativeToInput,
+ SizeMode::RelativeToParent => ParameterInheritance::RelativeToParent,
+ },
+ Self::Format if base.format == OutputFormat::Inherit => ParameterInheritance::RelativeToInput,
+ Self::Format => base.format_mode,
+ Self::PixelSize => base.pixel_size_mode,
+ Self::PixelRatio => base.pixel_ratio_mode,
+ Self::Tiling => base.tiling_mode,
+ Self::RandomSeed => base.seed_mode,
+ }
+ }
+
+ pub fn set_inheritance(self, base: &mut BaseParameters, inheritance: ParameterInheritance) {
+ match self {
+ Self::OutputSize => {
+ base.size_mode = match inheritance {
+ ParameterInheritance::Absolute => SizeMode::Absolute,
+ ParameterInheritance::RelativeToInput => SizeMode::RelativeToInput,
+ ParameterInheritance::RelativeToParent => SizeMode::RelativeToParent,
+ };
+ }
+ Self::Format => {
+ if base.format == OutputFormat::Inherit {
+ base.format = OutputFormat::U16;
+ }
+
+ base.format_mode = inheritance;
+ }
+ Self::PixelSize => base.pixel_size_mode = inheritance,
+ Self::PixelRatio => base.pixel_ratio_mode = inheritance,
+ Self::Tiling => base.tiling_mode = inheritance,
+ Self::RandomSeed => base.seed_mode = inheritance,
+ }
+ }
+}
diff --git a/crates/core/src/base_parameters.rs b/crates/core/src/base_parameters.rs
index ed0f58d..4db8979 100644
--- a/crates/core/src/base_parameters.rs
+++ b/crates/core/src/base_parameters.rs
@@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};
-use crate::{OutputFormat, ParameterInheritance, SizeMode};
+use crate::{BaseParameter, FunctionGraph, OutputFormat, ParameterInheritance, SizeMode};
+use std::collections::BTreeMap;
/// Parameters every node has ("Base parameters")
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
@@ -10,8 +11,14 @@ pub struct BaseParameters {
/// log2 offset per axis (relative modes) or log2 size (absolute)
pub size: [i32; 2],
pub format: OutputFormat,
- /// Added to the graph's random seed for this node
+ pub format_mode: ParameterInheritance,
+ /// Added to the inherited seed, or used directly in absolute mode
pub seed: i32,
+ pub seed_mode: ParameterInheritance,
+ pub pixel_size: [f32; 2],
+ pub pixel_size_mode: ParameterInheritance,
+ #[serde(skip_serializing_if = "BTreeMap::is_empty")]
+ pub functions: BTreeMap,
/// Whether sampling wraps at the edges (horizontal, vertical)
pub tiling: [bool; 2],
pub tiling_mode: ParameterInheritance,
@@ -26,7 +33,12 @@ impl Default for BaseParameters {
size_mode: SizeMode::RelativeToInput,
size: [0, 0],
format: OutputFormat::Inherit,
+ format_mode: ParameterInheritance::Absolute,
seed: 0,
+ seed_mode: ParameterInheritance::RelativeToInput,
+ pixel_size: [1.0; 2],
+ pixel_size_mode: ParameterInheritance::RelativeToInput,
+ functions: BTreeMap::new(),
tiling: [true, true],
tiling_mode: ParameterInheritance::default(),
pixel_ratio: 0,
diff --git a/crates/core/src/exposed_parameter.rs b/crates/core/src/exposed_parameter.rs
index 43d663a..d8320b4 100644
--- a/crates/core/src/exposed_parameter.rs
+++ b/crates/core/src/exposed_parameter.rs
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
-use crate::Value;
+use crate::{ParameterEditor, Value};
/// The kind of value an exposed graph input carries
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
@@ -12,6 +12,14 @@ pub struct ExposedParameter {
#[serde(default, skip_serializing_if = "String::is_empty")]
pub group: String,
pub default: Value,
+ #[serde(default, skip_serializing_if = "String::is_empty")]
+ pub description: String,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub editor: Option,
+ #[serde(default)]
+ pub clamp: bool,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub step: Option,
#[serde(default)]
pub min: f32,
#[serde(default = "one")]
@@ -24,3 +32,16 @@ pub struct ExposedParameter {
fn one() -> f32 {
1.0
}
+
+impl ExposedParameter {
+ pub fn editor(&self) -> ParameterEditor {
+ self.editor.filter(|editor| ParameterEditor::choices(&self.default).contains(editor)).unwrap_or_else(|| ParameterEditor::for_value(&self.default))
+ }
+
+ pub fn step(&self) -> f32 {
+ self.step.filter(|step| step.is_finite() && *step > 0.0).unwrap_or(match self.default {
+ Value::Int(_) | Value::Int2(_) | Value::Int3(_) | Value::Int4(_) | Value::Enum(_) => 1.0,
+ _ => 0.01,
+ })
+ }
+}
diff --git a/crates/core/src/function_graph.rs b/crates/core/src/function_graph.rs
index 84addc0..02eac7c 100644
--- a/crates/core/src/function_graph.rs
+++ b/crates/core/src/function_graph.rs
@@ -53,6 +53,38 @@ impl FunctionGraph {
graph
}
+ pub fn input(identifier: &str, value_type: crate::ValueType) -> FunctionGraph {
+ use crate::ValueType;
+
+ let operation = match value_type {
+ ValueType::Boolean => "get_bool",
+ ValueType::Integer => "get_integer1",
+ ValueType::Integer2 => "get_integer2",
+ ValueType::Integer3 => "get_integer3",
+ ValueType::Integer4 => "get_integer4",
+ ValueType::Float => "get_float1",
+ ValueType::Float2 => "get_float2",
+ ValueType::Float3 => "get_float3",
+ ValueType::Float4 => "get_float4",
+ ValueType::String => "get_string",
+ };
+ let mut graph = FunctionGraph::default();
+ graph.output = Some(graph.add(operation, &[], Some(Value::Text(identifier.into()))));
+ graph
+ }
+
+ pub fn input_identifier(&self) -> Option<&str> {
+ let output = self.node(self.output?)?;
+ if self.nodes.len() != 1 || !output.op.starts_with("get_") {
+ return None;
+ }
+
+ match &output.value {
+ Some(Value::Text(identifier)) => Some(identifier),
+ _ => None,
+ }
+ }
+
pub fn add_constant(&mut self, value: Value) -> u32 {
let operation = match &value {
Value::Float(_) => "const_float1",
diff --git a/crates/core/src/graph.rs b/crates/core/src/graph.rs
index a277554..61a6a23 100644
--- a/crates/core/src/graph.rs
+++ b/crates/core/src/graph.rs
@@ -105,13 +105,19 @@ impl Graph {
/// All editable Function canvases, including processor and parameter functions
pub fn functions(&self) -> impl Iterator- {
- self.function.iter().chain(self.nodes.iter().flat_map(|node| node.functions.values().chain(node.params.values().flat_map(value_functions))))
+ self.function.iter().chain(
+ self.nodes
+ .iter()
+ .flat_map(|node| node.base.functions.values().chain(node.functions.values()).chain(node.params.values().flat_map(value_functions))),
+ )
}
pub fn functions_mut(&mut self) -> impl Iterator
- {
- self.function
- .iter_mut()
- .chain(self.nodes.iter_mut().flat_map(|node| node.functions.values_mut().chain(node.params.values_mut().flat_map(value_functions_mut))))
+ self.function.iter_mut().chain(
+ self.nodes.iter_mut().flat_map(|node| {
+ node.base.functions.values_mut().chain(node.functions.values_mut()).chain(node.params.values_mut().flat_map(value_functions_mut))
+ }),
+ )
}
pub fn instances(&self) -> impl Iterator
- {
@@ -125,8 +131,10 @@ impl Graph {
self.function.iter_mut().flat_map(|function| function.nodes.iter_mut().filter_map(|node| node.instance.as_mut())).chain(self.nodes.iter_mut().flat_map(
|node| {
node.instance.iter_mut().chain(
- node.functions
+ node.base
+ .functions
.values_mut()
+ .chain(node.functions.values_mut())
.chain(node.params.values_mut().flat_map(value_functions_mut))
.flat_map(|function| function.nodes.iter_mut().filter_map(|node| node.instance.as_mut())),
)
diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs
index b02cfa3..f9ac317 100644
--- a/crates/core/src/lib.rs
+++ b/crates/core/src/lib.rs
@@ -2,6 +2,7 @@
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
mod attributes;
+mod base_parameter;
mod base_parameters;
mod canvas_annotations;
mod color;
@@ -29,6 +30,7 @@ mod node;
mod node_id;
mod output_format;
mod package;
+mod parameter_editor;
mod parameter_inheritance;
mod pattern_graph;
mod pattern_node;
@@ -43,6 +45,7 @@ mod value;
mod value_type;
pub use attributes::Attributes;
+pub use base_parameter::BaseParameter;
pub use base_parameters::BaseParameters;
pub use color::{linear_to_srgb, parse_color, srgb_to_linear};
pub use comment::Comment;
@@ -69,6 +72,7 @@ pub use node::Node;
pub use node_id::NodeId;
pub use output_format::OutputFormat;
pub use package::{PACKAGE_EXT, Package};
+pub use parameter_editor::ParameterEditor;
pub use parameter_inheritance::ParameterInheritance;
pub use pattern_graph::PatternGraph;
pub use pattern_node::PatternNode;
diff --git a/crates/core/src/parameter_editor.rs b/crates/core/src/parameter_editor.rs
new file mode 100644
index 0000000..b6db985
--- /dev/null
+++ b/crates/core/src/parameter_editor.rs
@@ -0,0 +1,43 @@
+use crate::Value;
+use serde::{Deserialize, Serialize};
+
+/// Presentation of an exposed value in Properties
+#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub enum ParameterEditor {
+ Slider,
+ Input,
+ Color,
+ Dropdown,
+ Toggle,
+ Text,
+}
+
+impl ParameterEditor {
+ pub fn choices(value: &Value) -> &'static [Self] {
+ match value {
+ Value::Bool(_) => &[Self::Toggle],
+ Value::Text(_) => &[Self::Text],
+ Value::Enum(_) => &[Self::Dropdown],
+ Value::Color(_) => &[Self::Color, Self::Input],
+ Value::Float3(_) | Value::Float4(_) => &[Self::Slider, Self::Input, Self::Color],
+ Value::Float(_) | Value::Float2(_) | Value::Int(_) | Value::Int2(_) | Value::Int3(_) | Value::Int4(_) => &[Self::Slider, Self::Input],
+ _ => &[],
+ }
+ }
+
+ pub fn for_value(value: &Value) -> Self {
+ Self::choices(value).first().copied().unwrap_or(Self::Input)
+ }
+
+ pub fn label(self) -> &'static str {
+ match self {
+ Self::Slider => "Slider",
+ Self::Input => "Input field",
+ Self::Color => "Color",
+ Self::Dropdown => "Drop down list",
+ Self::Toggle => "Toggle",
+ Self::Text => "Text",
+ }
+ }
+}
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 51ccc34..b5f4582 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -68,6 +68,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
v.extend(crate::document_history_commands::specs());
v.extend(crate::embedded_edit_commands::specs());
v.extend(crate::graph_cmds::specs());
+ v.extend(crate::exposed_input_commands::specs());
v.extend(crate::graph_property_commands::specs());
v.extend(crate::alignment_commands::specs());
v.extend(crate::annotation_commands::specs());
@@ -79,6 +80,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
v.extend(crate::comment_commands::specs());
v.extend(crate::pin_commands::specs());
v.extend(crate::node_cmds::specs());
+ v.extend(crate::parameter_function_commands::specs());
v.extend(crate::node_graph_commands::specs());
v.extend(crate::pattern_graph_commands::specs());
v.extend(crate::connection_commands::specs());
diff --git a/crates/engine/src/exposed_input_commands.rs b/crates/engine/src/exposed_input_commands.rs
new file mode 100644
index 0000000..f9bba42
--- /dev/null
+++ b/crates/engine/src/exposed_input_commands.rs
@@ -0,0 +1,180 @@
+use crate::{
+ Result, Session, cmd,
+ commands::{CommandSpec, bad, has_graph, strp},
+ parameter_target::ParameterTarget,
+};
+use materialcraft_core::{ExposedParameter, FunctionGraph, ParameterEditor, Value, ValueType};
+use serde_json::{Value as J, json};
+
+pub(crate) fn specs() -> Vec {
+ vec![
+ cmd!(
+ "graph.exposed.prepare",
+ "Prepare graph input",
+ [],
+ None,
+ "{bind: {node, param}, default?} returns input settings without changing the graph",
+ has_graph,
+ prepare
+ ),
+ cmd!(
+ "graph.exposed.add",
+ "Expose parameter",
+ [],
+ None,
+ "{id?, label?, group?, description?, default?, min?, max?, editor?, clamp?, step?, bind?: {node, param}}",
+ has_graph,
+ add
+ ),
+ ]
+}
+
+fn prepare(session: &mut Session, parameters: &J) -> Result {
+ let (input, _) = prepare_input(session, parameters)?;
+ Ok(json!(input))
+}
+
+fn add(session: &mut Session, parameters: &J) -> Result {
+ let (input, target) = prepare_input(session, parameters)?;
+ let value_type = ValueType::of(&input.default).ok_or_else(|| bad("graph.exposed.add", "input must have a scalar or vector type"))?;
+ let identifier = input.id.clone();
+ session.edit("Expose parameter", |session| {
+ let graph = session.graph_mut()?;
+ graph.exposed.push(input);
+ if let Some(target) = target {
+ target.set_function(graph, FunctionGraph::input(&identifier, value_type))?;
+ }
+
+ Ok(json!({"id": identifier}))
+ })
+}
+
+fn number(parameters: &J, name: &str) -> Result