Publish a selected graph as MCMAT v3 and bake it to MaterialX (#270)

* Publish selected graphs as MCMAT v3 and bake parameterized MaterialX exports

* Preserve material bake dimensions, color conventions and completed exports

* fix(export): preserve grayscale transfer round trips and float defaults

* fix(material): preserve disabled nodes and localized typed inputs

* Fix capability FIFO opens and save destination link rebasing

* Keep material export dialogs bound to their source package

* fix(material): bound inline assets and pin merged MCMAT

* fix(sbs): reject graph interfaces that exceed reader bounds

* Bound resize scheduling test raster workload

* fix(material): retain inline precedence on budget errors

* fix(material): preserve bounded fallback and XML identifiers

* fix(material): bound discarded inline inflation work

* Preserve bounded material dependencies and explicit export options

* Auto: Pin workspace saves and preserve material export choices

Hold the resolved save directory across SBS serialization and writes. Only
clean up owned staging files and compare existing assets incrementally.

Bound source assets before archive serialization, limit discarded SBS
inflation, and account for shared graph depth. Decode preview inputs on
demand and release failed browser downloads.

Distinguish automatic and explicit output colors, borrow graphs while drawing
export tabs, and preserve numeric export selectors and MCP option validation.

---------

Co-authored-by: Dylan M. Taylor <dylan@dylanmtaylor.com>
This commit is contained in:
Chris HassebrookandDylan M. Taylor authored and GitHub committed 2026-11-18 18:54:45 -05:00
1 parent 754b88a081
commit 827ba9259a
72 files changed
+3525 -995

No files matched your search

Generated
+27 -4
View File
@@ -937,9 +937,18 @@ dependencies = [
]
[[package]]
name = "craft-mcmat"
name = "craft-materialx"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
dependencies = [
"craft-xml",
"thiserror 2.0.21",
]
[[package]]
name = "craft-mcmat"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=7a7a18cb40ac4cd5fca29c0dc224ac5178d40a89#7a7a18cb40ac4cd5fca29c0dc224ac5178d40a89"
dependencies = [
"craft-zip",
"serde",
@@ -990,6 +999,16 @@ dependencies = [
"encoding_rs",
]
[[package]]
name = "craft-platform"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
dependencies = [
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "craft-quit"
version = "0.1.0"
@@ -1112,7 +1131,7 @@ dependencies = [
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=7a7a18cb40ac4cd5fca29c0dc224ac5178d40a89#7a7a18cb40ac4cd5fca29c0dc224ac5178d40a89"
dependencies = [
"flate2",
"thiserror 2.0.21",
@@ -2823,6 +2842,7 @@ dependencies = [
"schemars",
"serde",
"serde_json",
"tempfile",
"thiserror 2.0.21",
"tokio",
]
@@ -2834,6 +2854,7 @@ dependencies = [
"materialcraft-automation",
"materialcraft-engine",
"serde_json",
"tempfile",
"tokio",
"winresource",
]
@@ -2851,6 +2872,7 @@ dependencies = [
name = "materialcraft-engine"
version = "0.4.1"
dependencies = [
"craft-materialx",
"craft-texture",
"image",
"materialcraft-core",
@@ -2858,9 +2880,11 @@ dependencies = [
"materialcraft-formats",
"materialcraft-geom",
"materialcraft-nodes",
"rustix 1.1.6",
"serde",
"serde_json",
"sha2",
"tempfile",
"thiserror 2.0.21",
]
@@ -2942,6 +2966,7 @@ dependencies = [
"craft-atomic",
"craft-dialogs",
"craft-keymap",
"craft-platform",
"craft-quit",
"craft-ui",
"craft-update",
@@ -2951,7 +2976,6 @@ dependencies = [
"egui-phosphor",
"egui_kittest",
"image",
"js-sys",
"log",
"materialcraft-engine",
"materialcraft-render",
@@ -2961,7 +2985,6 @@ dependencies = [
"sha2",
"sys-locale",
"tempfile",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"zip 2.6.1",
+5 -2
View File
@@ -83,14 +83,17 @@ flate2 = { version = "1", default-features = false, features = ["rust_backend"]
cap-std = "4"
getrandom = "0.3"
# Shared Craft egui components from storytold/craft-libs
# MIT OR Apache-2.0; all shared dependencies use one reviewed revision
# MIT OR Apache-2.0
egui_kittest = "0.37"
craft-mcmat = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" }
craft-materialx = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" }
# MCMAT v3 uses its merged archive revision and the matching ZIP crate
craft-mcmat = { git = "https://github.com/storytold/craft-libs", rev = "7a7a18cb40ac4cd5fca29c0dc224ac5178d40a89" }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", features = ["serde"] }
# Keyboard bindings (chords, dispatch and display text): storytold/craft-libs, MIT OR Apache-2.0
craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", features = ["egui"] }
# Native dialogs that do not block the event loop: storytold/craft-libs, MIT OR Apache-2.0
craft-dialogs = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" }
craft-platform = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" }
# The platform quit and the macOS Quit menu item: storytold/craft-libs, MIT OR Apache-2.0
craft-quit = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", features = ["egui"] }
craft-menu = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" }
+5 -4
View File
@@ -145,11 +145,12 @@ cargo run --release -p materialcraft # desktop app
cargo run --release -p materialcraft -- package.sbs # open an SBS package
cargo run --release -p materialcraft -- --control 7878 # + JSON control channel
cargo run --release -p materialcraft-cli -- mcp # MCP server (stdio)
cargo run --release -p materialcraft-cli -- new wood wood.sbs
cargo run --release -p materialcraft-cli -- render wood.sbs wood.png --scene sphere --full --hq
cargo run --release -p materialcraft-cli -- export wood.sbs textures --format png --depth 16
cargo run --release -p materialcraft-cli -- new pbr surface.sbs --name Surface
cargo run --release -p materialcraft-cli -- render surface.sbs surface.png --scene sphere --full --hq
cargo run --release -p materialcraft-cli -- export surface.sbs textures --format png --depth 16
cargo run --release -p materialcraft-cli -- export my.sbs out --set wear=0.7 # with a graph input "wear" set first
cargo run --release -p materialcraft-cli -- run my.sbs file.exportMaterial '{"path": "my.mcmat", "size": 2048}' # for TextureCraft
cargo run --release -p materialcraft-cli -- publish surface.sbs surface.mcmat --graph Surface
cargo run --release -p materialcraft-cli -- export surface.mcmat build/surface --profile materialx --size 512
cd apps/materialcraft-web && trunk serve # web build
cargo xtask ci # fmt, clippy, tests, layering, wasm
```
+3
View File
@@ -17,3 +17,6 @@ tokio = { workspace = true }
[build-dependencies]
winresource = "0.1"
[dev-dependencies]
tempfile = "3"
+126 -21
View File
@@ -13,8 +13,10 @@ materialcraft-cli: headless MaterialCraft
USAGE:
materialcraft-cli --version
materialcraft-cli info <package.sbs | sample>
Graphs, nodes, outputs and cook results, as JSON.
materialcraft-cli info <project | material.mcmat | sample>
JSON graph inspection, or the published interface of a .mcmat.
materialcraft-cli publish <project.sbs | project.mcraft> <out.mcmat> --graph <SBS-path | index>
Publish the selected material graph with its reachable source dependencies.
materialcraft-cli commands [--filter <text>]
List the engine commands (id, label, params).
materialcraft-cli nodes [--filter <text>] [--category <text>]
@@ -25,11 +27,16 @@ USAGE:
Run [{\"id\", \"params\"}, ...] or {\"open\": <input>, \"steps\": [...], \"save\": <out.sbs>}.
materialcraft-cli new <template> <out.sbs> [--size <px>] [--name <name>]
A new package with one graph from a template (empty, pbr, atomics).
materialcraft-cli cook <input> [--graph <index>] [--set <input>=<value>]... [--full]
Cook a graph and print per-node timings and errors.
materialcraft-cli export <input> <dir> [--graph <index>] [--set <input>=<value>]... [--format <ext>]
[--depth 8|16|32] [--size <px>] [--pattern <p>]
Cook at full size and write every output (png, jpg, tga, tif, bmp, webp, exr, hdr).
materialcraft-cli cook <input> [--graph <graph>] [--set <id>=<value>]... [--input <id>=<image>]... [--preset <name>] [--size <px|WxH>] [--full]
Print the published outputs, sizes and means for a material, or an authoring graph inspection.
materialcraft-cli export <input> <dir> [--graph <SBS path or index>] [--set <input>=<value>]... [--format <ext>]
[--depth 8|16|32] [--size <px|WxH>] [--pattern <p>] [--profile maps|materialx]
[--preset <name>] [--input <id>=<image>]...
Write output images, or material.mtlx and its baked textures with --profile materialx.
--normal-format opengl|directx requires --profile maps|materialx and declares the input convention.
--graph-path <SBS path> selects a literal path, including numeric names, instead of --graph.
Named profiles choose their own format and filenames; omit --format and --pattern.
--displacement-scale <units> scales signed MaterialX displacement (default 1).
materialcraft-cli render <input> <out.png> [--size <W>x<H>] [--view 3d|2d] [--scene <id>] [--node <id>]
[--yaw <deg>] [--pitch <deg>] [--graph <index>] [--set <input>=<value>]... [--full]
[--tiling <n>] [--hq [--displacement <radii>]]
@@ -43,21 +50,44 @@ USAGE:
materialcraft-cli serve [--port <port>] [--read-root <dir>] [--write-root <dir>]
JSON lines ({\"id\",\"method\",\"params\"}) on stdio, or on 127.0.0.1:<port> with a token.
<input> is an .sbs package or `sample` (the built-in atomic sample). Packages save as native SBS. --set gives a graph input a value
<input> is an .sbs, .mcraft, .mcmat or `sample` (the built-in atomic sample). Packages save as native SBS. --set gives a graph input a value
(JSON, e.g. `--set roughness=0.4 --set tint=\"#aa7744\"`) before cooking. See docs/control-protocol.md.
";
/// (name, flags taking a value, bare flags)
const SUBCOMMANDS: &[(&str, &[&str], &[&str])] = &[
("info", &[], &[]),
("publish", &["--graph", "--graph-path"], &[]),
("commands", &["--filter"], &[]),
("nodes", &["--filter", "--category"], &[]),
("run", &["-o"], &[]),
("batch", &[], &[]),
("new", &["--size", "--name"], &[]),
("cook", &["--graph", "--set"], &["--full"]),
("export", &["--graph", "--set", "--format", "--depth", "--size", "--pattern"], &[]),
("render", &["--size", "--view", "--scene", "--node", "--yaw", "--pitch", "--graph", "--set", "--tiling", "--displacement"], &["--full", "--hq"]),
("cook", &["--graph", "--graph-path", "--set", "--size", "--input", "--preset"], &["--full"]),
(
"export",
&[
"--graph",
"--graph-path",
"--set",
"--format",
"--depth",
"--size",
"--pattern",
"--profile",
"--input",
"--preset",
"--out",
"--displacement-scale",
"--normal-format",
],
&[],
),
(
"render",
&["--size", "--view", "--scene", "--node", "--yaw", "--pitch", "--graph", "--graph-path", "--set", "--tiling", "--displacement"],
&["--full", "--hq"],
),
("mcp", &["--bridge", "--token", "--read-root", "--write-root", "--automation-read-root", "--automation-write-root"], &[]),
("serve", &["--port", "--read-root", "--write-root", "--automation-read-root", "--automation-write-root"], &[]),
];
@@ -121,7 +151,7 @@ fn version() -> String {
fn open(input: &str) -> Result<Headless, String> {
let mut h = Headless::new(Headless::default_session(), Filesystem::TrustedLocal);
if input != "sample" {
h.open(input).map_err(|e| e.to_string())?;
h.load(input).map_err(|e| e.to_string())?;
}
Ok(h)
}
@@ -132,7 +162,7 @@ fn print(v: &Value) {
/// Open `--graph` and apply every `--set <input>=<value>` (the value is JSON, else a string)
fn select_graph(h: &mut Headless, a: &Args) -> Result<(), String> {
if let Some(g) = a.num::<u64>("--graph")? {
if let Some(g) = selected_graph(a)? {
h.command_run("graph.open", json!({"graph": g})).map_err(|e| e.to_string())?;
}
for (_, v) in a.flags.iter().filter(|(f, _)| f == "--set") {
@@ -143,6 +173,51 @@ fn select_graph(h: &mut Headless, a: &Args) -> Result<(), String> {
Ok(())
}
fn selected_graph(a: &Args) -> Result<Option<Value>, String> {
match (a.get("--graph"), a.get("--graph-path")) {
(Some(_), Some(_)) => Err("use either --graph or --graph-path, not both".into()),
(_, Some(path)) => Ok(Some(json!(path))),
(Some(selector), None) => Ok(Some(graph_selector(selector))),
(None, None) => Ok(None),
}
}
fn graph_selector(selector: &str) -> Value {
selector.parse::<u64>().map_or_else(|_| json!(selector), |index| json!(index))
}
fn output_size(size: &str) -> Result<Value, String> {
let pixels = |side: &str| side.parse::<u32>().map_err(|_| "--size expects pixels or WIDTHxHEIGHT".to_string());
match size.split_once('x') {
Some((width, height)) => Ok(json!([pixels(width)?, pixels(height)?])),
None => Ok(json!(pixels(size)?)),
}
}
fn cook_parameters(a: &Args) -> Result<Value, String> {
let mut values = serde_json::Map::new();
let mut inputs = serde_json::Map::new();
for (flag, value) in &a.flags {
if flag != "--set" && flag != "--input" {
continue;
}
let (id, value) =
value.as_deref().and_then(|v| v.split_once('=')).filter(|(id, _)| !id.is_empty()).ok_or_else(|| format!("{flag} expects identifier=value"))?;
if flag == "--set" {
values.insert(id.to_string(), serde_json::from_str(value).unwrap_or_else(|_| json!(value)));
} else {
inputs.insert(id.to_string(), json!(value));
}
}
let graph = selected_graph(a)?;
Ok(json!({"graph":graph,"size":a.get("--size").map(output_size).transpose()?,"preset":a.get("--preset"),"values":values,"inputs":inputs}))
}
fn published(path: &str) -> bool {
std::path::Path::new(path).extension().and_then(|e| e.to_str()).is_some_and(|e| e.eq_ignore_ascii_case("mcmat"))
}
fn main() {
let raw: Vec<String> = std::env::args().skip(1).collect();
let code = match raw.first().map(String::as_str) {
@@ -168,8 +243,9 @@ fn main() {
fn run(cmd: &str, a: &Args) -> Result<(), String> {
match cmd {
"info" => {
let mut h = open(a.pos(0, "<input>")?)?;
print(&h.inspect());
let input = a.pos(0, "<input>")?;
let mut h = open(input)?;
print(&if published(input) { h.command_run("material.info", Value::Null).map_err(|e| e.to_string())? } else { h.inspect() });
}
"commands" => {
let h = open("sample")?;
@@ -210,6 +286,7 @@ fn run(cmd: &str, a: &Args) -> Result<(), String> {
if !r["failed"].is_null() {
return Err("the batch stopped at an error".into());
}
if let Some(out) = save {
h.save(Some(out)).map_err(|e| e.to_string())?;
}
@@ -224,16 +301,34 @@ fn run(cmd: &str, a: &Args) -> Result<(), String> {
h.command_run("graph.delete", json!({"graph": 0})).map_err(|e| e.to_string())?;
print(&h.save(Some(out)).map_err(|e| e.to_string())?);
}
"publish" => {
let mut h = open(a.pos(0, "<project>")?)?;
let graph = selected_graph(a)?.ok_or("publish requires --graph <SBS path or index> or --graph-path <SBS path>")?;
print(&h.export_material(&json!({"path":a.pos(1,"<out.mcmat>")?, "graph":graph})).map_err(|e| e.to_string())?);
}
"cook" => {
let mut h = open(a.pos(0, "<input>")?)?;
select_graph(&mut h, a)?;
print(&h.command_run("graph.cook", json!({"full": a.has("--full")})).map_err(|e| e.to_string())?);
let input = a.pos(0, "<input>")?;
let mut h = open(input)?;
if published(input) || ["--size", "--input", "--preset"].iter().any(|flag| a.has(flag)) {
print(&h.command_run("material.cook", cook_parameters(a)?).map_err(|e| e.to_string())?);
} else {
select_graph(&mut h, a)?;
print(&h.command_run("graph.cook", json!({"full":a.has("--full")})).map_err(|e| e.to_string())?);
}
}
"export" => {
let mut h = open(a.pos(0, "<input>")?)?;
select_graph(&mut h, a)?;
let dir = a.pos(1, "<dir>")?;
let p = json!({"dir": dir, "graph": a.num::<u64>("--graph")?, "format": a.get("--format"), "depth": a.num::<u64>("--depth")?, "size": a.num::<u64>("--size")?, "pattern": a.get("--pattern")});
let mut p = cook_parameters(a)?;
p["dir"] = json!(a.get("--out").or_else(|| a.pos.get(1).map(String::as_str)).ok_or("export requires an output directory")?);
p["format"] = json!(a.get("--format"));
p["depth"] = json!(a.num::<u32>("--depth")?);
p["pattern"] = json!(a.get("--pattern"));
p["profile"] = json!(a.get("--profile"));
p["normalFormat"] = json!(a.get("--normal-format"));
if let Some(scale) = a.num::<f64>("--displacement-scale")? {
p["displacementScale"] = serde_json::Number::from_f64(scale).ok_or("--displacement-scale must be finite")?.into();
}
print(&h.export(&p).map_err(|e| e.to_string())?);
}
"render" => {
@@ -319,6 +414,16 @@ mod tests {
v.iter().map(|s| s.to_string()).collect()
}
#[test]
fn graph_paths_remain_literal_even_when_numeric() {
let a = parse("publish", &strings(&["--graph-path", "1"])).unwrap();
assert_eq!(selected_graph(&a).unwrap(), Some(json!("1")));
let a = parse("publish", &strings(&["--graph", "1"])).unwrap();
assert_eq!(selected_graph(&a).unwrap(), Some(json!(1)));
let a = parse("publish", &strings(&["--graph", "1", "--graph-path", "1"])).unwrap();
assert!(selected_graph(&a).is_err());
}
#[test]
fn automation_root_flags_are_aliases_of_the_short_ones() {
for cmd in ["mcp", "serve"] {
+101
View File
@@ -0,0 +1,101 @@
//! Exercise the distribution workflow through the public CLI, including reopening the archive in fresh processes.
use std::path::Path;
use std::process::Command;
use materialcraft_engine::formats;
use serde_json::{Value, json};
fn run(dir: &Path, args: &[&str]) -> Value {
let output = Command::new(env!("CARGO_BIN_EXE_materialcraft-cli")).current_dir(dir).args(args).output().unwrap();
assert!(output.status.success(), "{args:?}: {}", String::from_utf8_lossy(&output.stderr));
serde_json::from_slice(&output.stdout).unwrap()
}
#[test]
fn a_published_graph_bakes_openpbr_variations_after_the_project_is_removed() {
let temp = tempfile::tempdir().unwrap();
let dir = temp.path();
run(dir, &["new", "pbr", "source.sbs", "--name", "Oak", "--size", "32"]);
run(dir, &["run", "source.sbs", "graph.exposed.add", r#"{"id":"tint","bind":{"node":1,"param":"color"}}"#, "-o", "studio.mcraft"]);
run(dir, &["publish", "studio.mcraft", "oak.mcmat", "--graph", "Oak"]);
std::fs::remove_file(dir.join("studio.mcraft")).unwrap();
std::fs::remove_file(dir.join("source.sbs")).unwrap();
let invalid_scale = Command::new(env!("CARGO_BIN_EXE_materialcraft-cli"))
.current_dir(dir)
.args(["export", "oak.mcmat", "invalid", "--profile", "materialx", "--displacement-scale", "NaN"])
.output()
.unwrap();
assert!(!invalid_scale.status.success(), "an invalid displacement scale produced a material");
assert!(String::from_utf8_lossy(&invalid_scale.stderr).contains("displacement-scale"));
assert!(!dir.join("invalid").exists());
let initial = run(dir, &["info", "oak.mcmat"]);
assert_eq!(initial["parameters"][0]["id"], "tint");
for (folder, size, tint) in [("small", 32, "#ff0000"), ("large", 64, "#0000ff")] {
let report =
run(dir, &["export", "oak.mcmat", "--out", folder, "--profile", "materialx", "--size", &size.to_string(), "--set", &format!("tint={tint}")]);
let maps = report["maps"].as_array().unwrap();
assert_eq!(maps.len(), 6, "every output of the material template is exported");
let base = maps.iter().find(|m| m["usage"] == "baseColor").unwrap();
let relative = base["file"].as_str().unwrap();
assert_eq!(base["size"], json!([size, size]));
let root = dir.join(folder);
let xml = std::fs::read_to_string(root.join("material.mtlx")).unwrap();
assert!(xml.contains("<open_pbr_surface") && xml.contains("<normalmap") && xml.contains("<displacement"));
assert!(xml.contains(&format!("value=\"{relative}\"")) && !xml.contains(dir.to_string_lossy().as_ref()));
let map = formats::decode(&std::fs::read(root.join(relative)).unwrap(), false).unwrap();
let pixel = map.get(0, 0, false);
let expected = if size == 32 { [1.0, 0.0, 0.0] } else { [0.0, 0.0, 1.0] };
assert_eq!(&pixel[..3], &expected);
let height = maps.iter().find(|m| m["usage"] == "height").unwrap()["file"].as_str().unwrap();
let displacement = formats::decode(&std::fs::read(root.join(height)).unwrap(), true).unwrap();
assert!((displacement.get(0, 0, false)[0] + 0.5).abs() < 1e-5);
assert!(report["unmapped"].as_array().unwrap().iter().any(|id| id == "ambientocclusion"));
}
let rectangular = run(dir, &["cook", "oak.mcmat", "--size", "64x32"]);
assert_eq!(rectangular["outputs"][0]["size"], json!([64, 32]));
assert_eq!(run(dir, &["info", "oak.mcmat"]), initial);
}
#[test]
fn failed_materialx_reexport_preserves_the_last_complete_material() {
let temp = tempfile::tempdir().unwrap();
let dir = temp.path();
run(dir, &["new", "pbr", "source.sbs", "--name", "Oak", "--size", "32"]);
let export = |folder, size| run(dir, &["export", "source.sbs", folder, "--profile", "materialx", "--size", size]);
let first = export("material", "32");
let added = run(dir, &["run", "source.sbs", "graph.node.add", r#"{"def":"output","params":{"identifier":"extra"}}"#, "-o", "source.sbs"]);
let wire = json!({"from":1,"to":added["id"],"toPort":"input"}).to_string();
run(dir, &["run", "source.sbs", "graph.connect", &wire, "-o", "source.sbs"]);
let next = export("reference", "64");
let material = dir.join("material");
let original_files: Vec<_> = first["maps"]
.as_array()
.unwrap()
.iter()
.map(|map| {
let name = map["file"].as_str().unwrap();
(name, std::fs::read(material.join(name)).unwrap())
})
.collect();
let document = std::fs::read(material.join("material.mtlx")).unwrap();
let extra = next["maps"].as_array().unwrap().iter().find(|map| map["identifier"] == "extra").unwrap();
let blocked = material.join(extra["file"].as_str().unwrap());
std::fs::create_dir(&blocked).unwrap();
let failed = Command::new(env!("CARGO_BIN_EXE_materialcraft-cli"))
.current_dir(dir)
.args(["export", "source.sbs", "material", "--profile", "materialx", "--size", "64"])
.output()
.unwrap();
assert!(!failed.status.success());
let error = String::from_utf8_lossy(&failed.stderr);
assert!(error.contains(blocked.file_name().unwrap().to_str().unwrap()), "{error}");
assert_eq!(std::fs::read(material.join("material.mtlx")).unwrap(), document);
for (name, bytes) in original_files {
assert_eq!(std::fs::read(material.join(name)).unwrap(), bytes, "{name}");
}
}
+1
View File
@@ -26,4 +26,5 @@ getrandom = { workspace = true }
rustix = { workspace = true }
[dev-dependencies]
tempfile = "3"
rmcp = { workspace = true, features = ["client"] }
+55 -36
View File
@@ -110,6 +110,11 @@ impl Headless {
/// Run an engine command. Untrusted callers can't reach host paths through it
pub fn command_run(&mut self, id: &str, params: Value) -> Result<Value, AutomationError> {
if id == "material.cook" {
return Ok(materialcraft_engine::material_bake::cook(&self.session, &params, |path| {
self.read(path).map_err(|e| materialcraft_engine::EngineError::Other(e.to_string()))
})?);
}
if !self.trusted() {
authorize_command(id, &params)?;
}
@@ -131,34 +136,34 @@ impl Headless {
/// Open an `.sbs` or `.mcraft` package (or a `.mcmat` material's source), using the granted file access
pub fn open(&mut self, path: &str) -> Result<Value, AutomationError> {
self.load(path)?;
Ok(self.inspect())
}
/// Open the document without generating preview images when a caller only needs its interface.
pub fn load(&mut self, path: &str) -> Result<(), AutomationError> {
let bytes = self.read(path)?;
// Workspace save paths stay relative to their granted roots
let document = self.session.open_packages().into_iter().find(|entry| entry.2 == Some(path)).map(|entry| entry.0);
if let Some(document) = document {
self.session.activate_document(document)?;
return Ok(self.inspect());
return Ok(());
}
let folder = path.replace('\\', "/").rsplit_once('/').map(|(folder, _)| format!("{folder}/")).unwrap_or_default();
let fs = &self.fs;
let linked = |relative: &str| -> Option<Vec<u8>> {
let relative = relative.trim().replace('\\', "/");
let plain = !relative.is_empty() && !relative.contains(':') && !relative.starts_with('/');
let inside = relative.split('/').all(|part| !part.is_empty() && part != "." && part != "..");
if !plain || !inside {
return None;
match &self.fs {
Filesystem::TrustedLocal => file_cmds::open_bytes_linked(&mut self.session, &bytes, Some(path.into()), &file_cmds::linked_files(path))?,
Filesystem::Workspace(workspace) => {
let document = workspace.document_path(path)?;
file_cmds::open_bytes_scoped(&mut self.session, &bytes, &document.to_string_lossy(), &|file| workspace.read_dependency(file))?;
}
read_file(fs, &format!("{folder}{relative}")).ok()
};
file_cmds::open_bytes_linked(&mut self.session, &bytes, Some(path.to_string()), &linked)?;
Filesystem::Denied => return Err(bad("file access is not granted")),
}
if self.session.path.is_some() {
self.session.path = Some(path.to_string());
}
Ok(self.inspect())
Ok(())
}
/// Save SBS with adjacent resources, or an independent MaterialCraft archive.
@@ -168,24 +173,32 @@ impl Headless {
None => self.session.path.clone().ok_or_else(|| bad("the package has no file yet (new, or opened from another program): give a `path`"))?,
};
let files = file_cmds::save_files_at(&self.session, &path)?;
let (destination, workspace_write) = match &self.fs {
Filesystem::Workspace(workspace) => {
let (destination, writer, save_path) = workspace.save_destination(&path)?;
(destination.to_string_lossy().into_owned(), Some((writer, save_path)))
}
Filesystem::TrustedLocal => (path.clone(), None),
Filesystem::Denied => return Err(bad("file access is not granted")),
};
let files = file_cmds::save_files_at(&self.session, &destination)?;
let bytes = if file_cmds::is_mcraft(&path) {
let bytes = files.zip().map_err(|error| bad(error.to_string()))?;
self.write(&path, &bytes)?;
if let Some((writer, save_path)) = &workspace_write {
writer.write(save_path, &bytes)?;
} else {
self.write(&path, &bytes)?;
}
bytes.len()
} else {
match &self.fs {
Filesystem::Denied => return Err(bad("file access is not granted")),
Filesystem::TrustedLocal => {
materialcraft_engine::formats::project::write_files(Path::new(&path), &files).map_err(|error| bad(error.to_string()))?
}
Filesystem::Workspace(workspace) => {
workspace.check_write(&path)?;
let parent = path.rsplit_once('/').map_or(String::new(), |(parent, _)| format!("{parent}/"));
match &workspace_write {
None => materialcraft_engine::formats::project::write_files(Path::new(&path), &files).map_err(|error| bad(error.to_string()))?,
Some((writer, save_path)) => {
let parent = save_path.rsplit_once('/').map_or(String::new(), |(parent, _)| format!("{parent}/"));
for name in files.files.keys().filter(|name| *name != &files.entry) {
workspace.check_write(&format!("{parent}{name}"))?;
writer.check_write(&format!("{parent}{name}"))?;
}
workspace.create_dir_all(&format!("{parent}resources"))?;
writer.create_dir_all(&format!("{parent}resources"))?;
for (name, bytes) in &files.files {
if name == &files.entry {
continue;
@@ -193,11 +206,11 @@ impl Headless {
let target = format!("{parent}{name}");
if let Some((directory, _)) = target.rsplit_once('/') {
workspace.create_dir_all(directory)?;
writer.create_dir_all(directory)?;
}
workspace.write_resource(&target, bytes.as_slice())?;
writer.write_resource(&target, bytes.as_slice())?;
}
workspace.write(&path, files.xml().map_err(|error| bad(error.to_string()))?)?;
writer.write(save_path, files.xml().map_err(|error| bad(error.to_string()))?)?;
}
}
files.xml().map_err(|error| bad(error.to_string()))?.len()
@@ -218,8 +231,9 @@ impl Headless {
/// Export every output of a graph into folder `dir`
pub fn export(&mut self, p: &Value) -> Result<Value, AutomationError> {
let dir = p.get("dir").and_then(Value::as_str).ok_or_else(|| bad("`dir` is required"))?;
let request = materialcraft_engine::ExportRequest::from_params(&self.session, p)?;
let files = file_cmds::export_outputs(&self.session, &request)?;
let (files, mut report) = materialcraft_engine::material_bake::export(&self.session, p, |path| {
self.read(path).map_err(|e| materialcraft_engine::EngineError::Other(e.to_string()))
})?;
self.create_dir_all(dir)?;
let mut written = Vec::new();
@@ -228,7 +242,8 @@ impl Headless {
self.write(&path, &exported.bytes)?;
written.push(path);
}
Ok(json!({"files": written}))
report["files"] = json!(written);
Ok(report)
}
/// Export a portable material below the granted write root
@@ -241,10 +256,14 @@ impl Headless {
Filesystem::TrustedLocal => {}
}
let (gi, options) = material_cmds::material_request(&self.session, "material.export", p)?;
let bytes = material_cmds::material_bytes(&self.session, gi, &options)?;
let gi = material_cmds::publish_graph(&self.session, p)?;
let bytes = if self.trusted() {
material_cmds::material_bytes(&self.session, gi)?
} else {
material_cmds::material_bytes_with_resource_limit(&self.session, gi, MAX_EXPORTED_MATERIAL_BYTES)?
};
if bytes.len() as u64 > MAX_EXPORTED_MATERIAL_BYTES {
return Err(bad(format!("the material is {} bytes (limit {MAX_EXPORTED_MATERIAL_BYTES}); export a smaller `size`", bytes.len())));
return Err(bad(format!("the material is {} bytes (limit {MAX_EXPORTED_MATERIAL_BYTES}); reduce source asset sizes", bytes.len())));
}
self.write(&path, &bytes)?;
+24 -20
View File
@@ -272,9 +272,7 @@ mod tests {
}
/// A workspace session rooted at a fresh `inside` folder, with an `outside` sibling.
fn workspace_session(name: &str, write: bool) -> (std::path::PathBuf, std::path::PathBuf, Mutex<Headless>) {
let base = std::env::temp_dir().join(format!("mc-rpc-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&base);
fn workspace_session(base: &std::path::Path, write: bool) -> (std::path::PathBuf, std::path::PathBuf, Mutex<Headless>) {
let (inside, outside) = (base.join("inside"), base.join("outside"));
std::fs::create_dir_all(&inside).unwrap();
std::fs::create_dir_all(&outside).unwrap();
@@ -289,26 +287,29 @@ mod tests {
#[test]
fn material_export_writes_a_portable_material_below_the_write_root() {
let (inside, _, h) = workspace_session("mat-ok", true);
let directory = tempfile::tempdir().unwrap();
let (inside, _, h) = workspace_session(directory.path(), true);
std::fs::create_dir(inside.join("out")).unwrap();
let r = call(&h, "material.export", json!({"path": "out/brick", "size": 32})).unwrap();
assert_eq!(r["path"], "out/brick.mcmat");
let published = call(&h, "material.export", json!({"path": "out/brick"})).unwrap();
assert_eq!(published["path"], "out/brick.mcmat");
let bytes = std::fs::read(inside.join("out/brick.mcmat")).unwrap();
assert_eq!(r["bytes"], bytes.len());
let (m, source) = materialcraft_engine::formats::material::read_material(&bytes).unwrap();
assert_eq!(m.size, [32, 32]);
assert!(m.maps.iter().any(|e| e.usage == "height"));
assert!(!source.is_empty());
assert_eq!(published["bytes"], bytes.len());
let (package, entry) = materialcraft_engine::formats::material::read_material(&bytes).unwrap();
assert!(package.graph(entry).unwrap().nodes.iter().any(|node| node.def == "output"));
assert_eq!(std::fs::read_dir(inside.join("out")).unwrap().count(), 1, "no temporary files left behind");
// An explicit extension is kept (case-insensitively) and the file can be opened again.
let r = call(&h, "material.export", json!({"path": "again.MCMAT", "size": 16})).unwrap();
assert_eq!(r["path"], "again.MCMAT");
// Preserve the caller’s extension spelling when reopening the archive
let published = call(&h, "material.export", json!({"path": "again.MCMAT"})).unwrap();
assert_eq!(published["path"], "again.MCMAT");
assert!(call(&h, "package.open", json!({"path": "again.MCMAT"})).is_ok());
}
#[test]
fn material_export_reports_bad_requests() {
let (inside, _, h) = workspace_session("mat-bad", true);
let directory = tempfile::tempdir().unwrap();
let (inside, _, h) = workspace_session(directory.path(), true);
for bad in [json!({}), json!({"path": "m", "graph": 99}), json!({"path": "m", "values": {"no_such_input": 1}})] {
assert!(call(&h, "material.export", bad.clone()).is_err(), "{bad}");
}
@@ -321,7 +322,8 @@ mod tests {
let denied = session();
let e = call(&denied, "material.export", json!({"path": "m.mcmat"})).unwrap_err();
assert!(e.contains("not granted"), "{e}");
let (inside, _, read_only) = workspace_session("mat-readonly", false);
let directory = tempfile::tempdir().unwrap();
let (inside, _, read_only) = workspace_session(directory.path(), false);
let e = call(&read_only, "material.export", json!({"path": "m.mcmat"})).unwrap_err();
assert!(e.contains("no write root"), "{e}");
assert_eq!(std::fs::read_dir(&inside).unwrap().count(), 0);
@@ -329,19 +331,21 @@ mod tests {
#[test]
fn material_export_cannot_escape_the_write_root() {
let (_inside, outside, h) = workspace_session("mat-escape", true);
let directory = tempfile::tempdir().unwrap();
let (_inside, outside, h) = workspace_session(directory.path(), true);
let abs = outside.join("abs.mcmat").display().to_string();
for p in ["../outside/m.mcmat", "a/../../outside/m.mcmat", "/tmp/m.mcmat", "a\\b.mcmat", "C:/m.mcmat", "./m.mcmat", abs.as_str()] {
let e = call(&h, "material.export", json!({"path": p, "size": 16}));
let e = call(&h, "material.export", json!({"path": p}));
assert!(e.is_err(), "accepted {p:?}");
}
#[cfg(unix)]
{
std::os::unix::fs::symlink(&outside, _inside.join("link")).unwrap();
assert!(call(&h, "material.export", json!({"path": "link/m.mcmat", "size": 16})).is_err());
assert!(call(&h, "material.export", json!({"path": "link/m.mcmat"})).is_err());
}
assert_eq!(std::fs::read_dir(&outside).unwrap().count(), 0, "nothing written outside the root");
// The generic command path stays closed too: host paths are still refused.
// The generic command must enforce the same file access boundary
let e = call(&h, "command.run", json!({"id": "file.exportMaterial", "params": {"path": abs}})).unwrap_err();
assert!(e.contains("disabled for automation"), "{e}");
}
@@ -225,9 +225,9 @@ impl MaterialCraftMcp {
to_result(self.call("outputs.export", params(&p)).await)
}
#[tool(description = "Cook a graph at full size and write it as a portable .mcmat material (cooked maps, exposed inputs, \
source package) to a path relative to the write root, for TextureCraft. Optional graph, size and exposed input values. \
Returns {path, bytes}.")]
#[tool(
description = "Publish one root graph and its source dependencies as a procedural v3 .mcmat below the write root. Use outputs_export to bake with parameter values, image inputs and a requested size."
)]
async fn material_export(&self, Parameters(p): Parameters<MaterialExportParams>) -> Result<CallToolResult, McpError> {
to_result(self.call("material.export", params(&p)).await)
}
@@ -299,21 +299,22 @@ mod tests {
#[tokio::test(flavor = "multi_thread")]
async fn material_export_tool_writes_through_the_workspace_only() {
let base = std::env::temp_dir().join(format!("mc-mcp-{}-material", std::process::id()));
let _ = std::fs::remove_dir_all(&base);
std::fs::create_dir_all(&base).unwrap();
let args = || Parameters(MaterialExportParams { path: "m".into(), graph: None, size: Some(16), values: None });
let directory = tempfile::tempdir().unwrap();
let write_root = directory.path().join("workspace");
std::fs::create_dir(&write_root).unwrap();
let args = || Parameters(MaterialExportParams { path: "m".into(), graph: None });
let denied = MaterialCraftMcp::headless(Headless::new(materialcraft_engine::sample_session(), Filesystem::Denied));
assert_eq!(denied.material_export(args()).await.unwrap().is_error, Some(true));
let ws = crate::AuthorizedWorkspace::new(None, Some(&base)).unwrap();
let ws = crate::AuthorizedWorkspace::new(None, Some(&write_root)).unwrap();
let mcp = MaterialCraftMcp::headless(Headless::new(materialcraft_engine::sample_session(), Filesystem::Workspace(ws)));
assert_eq!(mcp.material_export(args()).await.unwrap().is_error, Some(false));
assert!(base.join("m.mcmat").is_file());
let escape = Parameters(MaterialExportParams { path: "../m.mcmat".into(), graph: None, size: Some(16), values: None });
assert!(write_root.join("m.mcmat").is_file());
let escape = Parameters(MaterialExportParams { path: "../m.mcmat".into(), graph: None });
assert_eq!(mcp.material_export(escape).await.unwrap().is_error, Some(true));
assert!(!base.parent().unwrap().join("m.mcmat").exists());
assert!(!directory.path().join("m.mcmat").exists());
}
/// Every sub-schema must be a JSON object. Claude Desktop's proxy rejects a boolean schema
@@ -14,16 +14,58 @@ pub struct ExportParams {
/// 8, 16 or 32 bits (default: the graph's output format)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub depth: Option<u32>,
/// Pixels of the larger side, rounded down to a power of two, 16 to 8192 (default: the graph's size)
/// The larger side or [width, height] in pixels, rounded down to powers of two from 16 to 8192.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub size: Option<u32>,
/// Graph index (default: the open graph)
#[schemars(schema_with = "size_schema")]
pub size: Option<materialcraft_engine::ExportSize>,
/// Graph index or full SBS path. Defaults to the open graph.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub graph: Option<u32>,
pub graph: Option<serde_json::Value>,
/// Output identifiers to write (default: every connected output)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub outputs: Option<Vec<String>>,
/// Color space per output identifier: srgb, linear or raw (default: sRGB for color usages, raw for data)
#[serde(default, rename = "colorSpaces", skip_serializing_if = "Option::is_none")]
pub color_spaces: Option<std::collections::BTreeMap<String, String>>,
/// Export profile: materialx for OpenPBR with baked textures, maps for labeled textures.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub profile: Option<String>,
/// Optional named preset, applied before explicit values.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub preset: Option<String>,
/// Height range in scene units for MaterialX displacement.
#[serde(default, rename = "displacementScale", skip_serializing_if = "Option::is_none")]
pub displacement_scale: Option<f32>,
/// Source tangent-space normal convention for baked maps. Omit to use the graph's convention.
#[serde(default, rename = "normalFormat", skip_serializing_if = "Option::is_none")]
pub normal_format: Option<String>,
/// Typed exposed parameter overrides for this cook.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(schema_with = "values_schema")]
pub values: Option<std::collections::BTreeMap<String, serde_json::Value>>,
/// Image input identifier to path below the read root.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub inputs: Option<std::collections::BTreeMap<String, String>>,
}
fn size_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({"anyOf":[{"type":"integer","minimum":0},{"type":"array","minItems":2,"maxItems":2,"items":{"type":"integer","minimum":0}},{"type":"null"}]})
}
fn values_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({"type":["object","null"],"additionalProperties":{}})
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn export_forwards_both_dimensions_and_the_source_normal_convention() {
let request = json!({"dir":".", "profile":"materialx", "size":[128,64], "normalFormat":"directx"});
let params: ExportParams = serde_json::from_value(request.clone()).unwrap();
assert_eq!(serde_json::to_value(params).unwrap(), request);
}
}
@@ -3,17 +3,35 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
// Some MCP clients reject boolean sub-schemas
#[schemars(extend("additionalProperties" = {"not": {}}))]
pub struct MaterialExportParams {
/// Forward-slash path relative to the write root (`.mcmat` is appended if missing). The
/// parent folder must exist
pub path: String,
/// Graph index (default: the open graph)
/// Graph index or full SBS path; defaults to the open graph.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub graph: Option<u32>,
/// Square size in pixels, rounded to a power of two from 16 to 8192 (default: the graph's size)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub size: Option<u32>,
/// Exposed input values to cook with and embed, by input id (see `session_inspect`)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub values: Option<Value>,
pub graph: Option<Value>,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn material_publication_rejects_bake_options() {
for (option, value) in [("size", json!(256)), ("size", json!(null)), ("values", json!({})), ("inputs", json!({})), ("preset", json!("Worn"))] {
let mut request = json!({"path":"material"});
request[option] = value;
let error = serde_json::from_value::<MaterialExportParams>(request).unwrap_err();
assert!(error.to_string().contains(&format!("unknown field `{option}`")));
}
let params = serde_json::from_value::<MaterialExportParams>(json!({"path":"material", "graph":"Materials/Oak"})).unwrap();
assert_eq!(params.graph, Some(json!("Materials/Oak")));
}
}
+216 -28
View File
@@ -17,19 +17,78 @@ pub const MAX_READ_BYTES: u64 = 2 << 30;
#[derive(Clone, Default)]
pub struct AuthorizedWorkspace {
read: Option<Arc<Dir>>,
read_path: Option<PathBuf>,
write: Option<Arc<Dir>>,
write_path: Option<PathBuf>,
}
impl AuthorizedWorkspace {
pub fn new(read_root: Option<&Path>, write_root: Option<&Path>) -> Result<Self, AutomationError> {
Ok(Self { read: open_root(read_root, "read")?, write: open_root(write_root, "write")? })
let read = open_root(read_root, "read")?;
let read_path = read_root.map(std::fs::canonicalize).transpose().map_err(|e| bad(e.to_string()))?;
let write = open_root(write_root, "write")?;
let write_path = write_root.map(std::fs::canonicalize).transpose().map_err(|e| bad(e.to_string()))?;
Ok(Self { read, read_path, write, write_path })
}
/// Engine identities are absolute. Actual dependency reads still go through the directory capability.
pub(crate) fn document_path(&self, path: &str) -> Result<PathBuf, AutomationError> {
Ok(self.read_path.as_ref().ok_or_else(|| denied("read"))?.join(relative_path(path)?))
}
/// Resolve a save once and keep its destination capability through serialization and writes.
pub(crate) fn save_destination(&self, path: &str) -> Result<(PathBuf, Self, String), AutomationError> {
let relative = relative_path(path)?;
let root = self.write.as_ref().ok_or_else(|| denied("write"))?;
let parent = relative.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new("."));
// Resolve directory symlinks inside the capability so rebased links use the correct depth
let mut ancestor = parent;
let mut suffix = PathBuf::new();
let resolved_ancestor = loop {
match root.canonicalize(ancestor) {
Ok(resolved) => break resolved,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
// Defer directory creation until after serialization
// Reject dangling symlinks
match root.symlink_metadata(ancestor) {
Err(missing) if missing.kind() == std::io::ErrorKind::NotFound => {}
_ => return Err(file_error("resolve destination", path, error)),
}
let leaf = ancestor.file_name().ok_or_else(|| file_error("resolve destination", path, error))?;
suffix = Path::new(leaf).join(suffix);
ancestor = ancestor.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new("."));
}
Err(error) => return Err(file_error("resolve destination", path, error)),
}
};
let leaf = relative.file_name().ok_or_else(|| bad("missing destination filename"))?;
let relative = suffix.join(leaf);
let save_path = relative.to_str().ok_or_else(|| bad("the save path must be UTF-8"))?.replace('\\', "/");
let write_path = self.write_path.as_ref().ok_or_else(|| denied("write"))?.join(&resolved_ancestor);
// Writes through this handle no longer traverse the original directory alias
let dir = root.open_dir(&resolved_ancestor).map_err(|error| file_error("resolve destination", path, error))?;
let destination = write_path.join(relative);
let workspace = Self { write: Some(Arc::new(dir)), write_path: Some(write_path), ..Self::default() };
Ok((destination, workspace, save_path))
}
pub(crate) fn read_dependency(&self, path: &str) -> Option<Vec<u8>> {
if path.starts_with("sbs://") {
return materialcraft_engine::formats::sbs::library::read(path).ok();
}
let relative = Path::new(path).strip_prefix(self.read_path.as_ref()?).ok()?;
self.read(&relative.to_str()?.replace('\\', "/")).ok()
}
/// Read one regular file below the read root
pub fn read(&self, path: &str) -> Result<Vec<u8>, AutomationError> {
let rel = relative_path(path)?;
let root = self.read.as_ref().ok_or_else(|| denied("read"))?;
let file = root.open(&rel).map_err(|e| file_error("read", path, e))?;
let file = root.open_with(&rel, &regular_read_options()).map_err(|e| file_error("read", path, e))?;
let metadata = file.metadata().map_err(|e| file_error("read", path, e))?;
if !metadata.is_file() {
return Err(bad(format!("not a regular file: `{path}`")));
@@ -38,11 +97,13 @@ impl AuthorizedWorkspace {
if metadata.len() > MAX_READ_BYTES {
return Err(bad(format!("file too large: `{path}` is {} bytes (limit {MAX_READ_BYTES})", metadata.len())));
}
let mut bytes = Vec::new();
file.take(MAX_READ_BYTES + 1).read_to_end(&mut bytes).map_err(|e| file_error("read", path, e))?;
if bytes.len() as u64 > MAX_READ_BYTES {
return Err(bad(format!("file too large: `{path}` exceeds the {MAX_READ_BYTES}-byte limit")));
}
Ok(bytes)
}
@@ -54,17 +115,17 @@ impl AuthorizedWorkspace {
let tmp = rel.with_file_name(format!(".{leaf}.{}.tmp", std::process::id()));
let mut options = OpenOptions::new();
options.write(true).create_new(true);
let res = root
.open_with(&tmp, &options)
.and_then(|mut f| {
f.write_all(bytes)?;
f.sync_all()
})
.and_then(|()| root.rename(&tmp, root, &rel));
if let Err(e) = res {
// Only the writer that acquired the staging file may remove it
let mut file = root.open_with(&tmp, &options).map_err(|error| file_error("write", path, error))?;
let written = file.write_all(bytes).and_then(|()| file.sync_all());
drop(file);
if let Err(error) = written.and_then(|()| root.rename(&tmp, root, &rel)) {
let _ = root.remove_file(&tmp);
return Err(file_error("write", path, e));
return Err(file_error("write", path, error));
}
Ok(())
}
@@ -95,15 +156,13 @@ impl AuthorizedWorkspace {
}
}
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
let file = root.open(&rel).map_err(|error| file_error("read", path, error))?;
let file = root.open_with(&rel, &regular_read_options()).map_err(|error| file_error("read", path, error))?;
let metadata = file.metadata().map_err(|error| file_error("read", path, error))?;
if !metadata.is_file() || metadata.len() != bytes.len() as u64 {
return Err(bad(format!("Resource filename collision: `{path}`")));
}
let mut existing = Vec::new();
file.take(bytes.len() as u64 + 1).read_to_end(&mut existing).map_err(|error| file_error("read", path, error))?;
if existing != bytes {
if !resource_matches(file, bytes).map_err(|error| file_error("read", path, error))? {
return Err(bad(format!("Resource filename collision: `{path}`")));
}
}
@@ -113,11 +172,45 @@ impl AuthorizedWorkspace {
}
}
// A repeated save compares immutable assets without buffering a second copy
fn resource_matches(mut file: cap_std::fs::File, mut expected: &[u8]) -> std::io::Result<bool> {
const COMPARISON_BUFFER_BYTES: usize = 8 * 1024;
let mut buffer = [0; COMPARISON_BUFFER_BYTES];
loop {
let count = match file.read(&mut buffer) {
Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue,
result => result?,
};
if count == 0 {
return Ok(expected.is_empty());
}
let Some(rest) = expected.strip_prefix(&buffer[..count]) else {
return Ok(false);
};
expected = rest;
}
}
// Keep capability-based resolution while preventing FIFO opens from waiting for a writer
fn regular_read_options() -> OpenOptions {
let mut options = OpenOptions::new();
options.read(true);
#[cfg(unix)]
{
use cap_std::fs::OpenOptionsExt;
options.custom_flags(rustix::fs::OFlags::NONBLOCK.bits() as i32);
}
options
}
fn open_root(path: Option<&Path>, authority: &str) -> Result<Option<Arc<Dir>>, AutomationError> {
let Some(path) = path else { return Ok(None) };
if path.as_os_str().is_empty() {
return Err(bad(format!("the automation {authority} root is empty")));
}
let dir =
Dir::open_ambient_dir(path, ambient_authority()).map_err(|e| AutomationError::Io(format!("cannot open {authority} root `{}`: {e}", path.display())))?;
Ok(Some(Arc::new(dir)))
@@ -129,29 +222,36 @@ pub fn relative_path(raw: &str) -> Result<PathBuf, AutomationError> {
if raw.is_empty() {
return Err(reject("empty"));
}
if raw.contains('\\') {
return Err(reject("backslash"));
}
if raw.starts_with('/') {
return Err(reject("absolute"));
}
if raw.contains(':') {
return Err(reject("drive, scheme or stream prefix"));
}
for c in raw.split('/') {
if c.is_empty() || c == "." || c == ".." {
for component in raw.split('/') {
if component.is_empty() || component == "." || component == ".." {
return Err(reject("empty, `.` or `..` component"));
}
if c.ends_with(['.', ' ']) || c.chars().any(char::is_control) {
if component.ends_with(['.', ' ']) || component.chars().any(char::is_control) {
return Err(reject("component ends in a dot or space, or has control characters"));
}
let stem = c.split('.').next().unwrap_or(c).to_ascii_uppercase();
let stem = component.split('.').next().unwrap_or(component).to_ascii_uppercase();
let device = matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL" | "CONIN$" | "CONOUT$")
|| (stem.len() == 4 && (stem.starts_with("COM") || stem.starts_with("LPT")) && stem.ends_with(|ch: char| ch.is_ascii_digit()));
if device {
return Err(reject("reserved device name"));
}
}
Ok(PathBuf::from(raw))
}
@@ -167,6 +267,7 @@ fn file_error(op: &str, path: &str, e: std::io::Error) -> AutomationError {
pub fn authorize_command(id: &str, params: &Value) -> Result<(), AutomationError> {
let has_path = ["path", "dir"].iter().any(|k| params.get(k).is_some_and(|v| !v.is_null()));
let ok = match id {
"material.cook" => !has_path && params.get("inputs").is_none_or(|v| v.is_null() || v.as_object().is_some_and(|o| o.is_empty())),
"file.new" | "file.newPackage" | "file.newAtomicSample" => !has_path,
_ => !id.starts_with("file.") && !has_path,
};
@@ -183,9 +284,7 @@ mod tests {
use super::*;
use serde_json::json;
fn roots(name: &str) -> (PathBuf, PathBuf, AuthorizedWorkspace) {
let base = std::env::temp_dir().join(format!("mc-ws-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&base);
fn roots(base: &Path) -> (PathBuf, PathBuf, AuthorizedWorkspace) {
let (inside, outside) = (base.join("inside"), base.join("outside"));
std::fs::create_dir_all(&inside).unwrap();
std::fs::create_dir_all(&outside).unwrap();
@@ -193,9 +292,37 @@ mod tests {
(inside, outside, ws)
}
#[cfg(unix)]
#[test]
fn capability_reads_and_resource_collisions_reject_fifos_without_a_writer() {
use std::os::unix::fs::OpenOptionsExt;
use std::time::Duration;
for collision in [false, true] {
let directory = tempfile::tempdir().unwrap();
let (inside, _, ws) = roots(directory.path());
let path = inside.join("pipe");
assert!(std::process::Command::new("mkfifo").arg(&path).status().unwrap().success());
let (sender, receiver) = std::sync::mpsc::channel();
let worker = std::thread::spawn(move || {
let result = if collision { ws.write_resource("pipe", b"x") } else { ws.read("pipe").map(|_| ()) };
sender.send(result).unwrap();
});
let result = receiver.recv_timeout(Duration::from_secs(2));
if result.is_err() {
// Release the regression's blocked read before failing the test
let writer = std::fs::OpenOptions::new().write(true).custom_flags(rustix::fs::OFlags::NONBLOCK.bits() as i32).open(&path).unwrap();
drop(writer);
}
worker.join().unwrap();
assert!(result.expect("FIFO open must not wait for a writer").is_err());
}
}
#[test]
fn reads_and_writes_stay_in_the_root() {
let (inside, _, ws) = roots("valid");
let directory = tempfile::tempdir().unwrap();
let (inside, _, ws) = roots(directory.path());
std::fs::write(inside.join("a.txt"), b"canary").unwrap();
assert_eq!(ws.read("a.txt").unwrap(), b"canary");
ws.create_dir_all("out/deep").unwrap();
@@ -207,9 +334,39 @@ mod tests {
assert!(ws.write("missing/c.txt", b"x").is_err(), "parent must exist");
}
#[test]
fn immutable_resources_compare_the_last_byte_of_a_partial_buffer() {
let directory = tempfile::tempdir().unwrap();
let (inside, _, workspace) = roots(directory.path());
let mut bytes = vec![17; 32 * 1024 + 1];
workspace.write_resource("asset.bin", &bytes).unwrap();
workspace.write_resource("asset.bin", &bytes).unwrap();
*bytes.last_mut().unwrap() = 42;
let collision = workspace.write_resource("asset.bin", &bytes).unwrap_err();
assert!(collision.to_string().contains("collision"));
assert_eq!(std::fs::read(inside.join("asset.bin")).unwrap().last(), Some(&17));
}
#[test]
fn a_busy_save_preserves_the_existing_staging_file() {
let directory = tempfile::tempdir().unwrap();
let (inside, _, workspace) = roots(directory.path());
let staging = inside.join(format!(".scene.sbs.{}.tmp", std::process::id()));
std::fs::write(&staging, b"pending save").unwrap();
let result = workspace.write("scene.sbs", b"second save");
assert!(result.is_err());
assert_eq!(std::fs::read(staging).unwrap(), b"pending save");
assert!(!inside.join("scene.sbs").exists());
}
#[test]
fn escapes_and_malformed_paths_are_rejected() {
let (_, outside, ws) = roots("reject");
let directory = tempfile::tempdir().unwrap();
let (_, outside, ws) = roots(directory.path());
let abs = outside.join("x.txt").display().to_string();
for p in ["", "/abs", "../x", "a/../../x", "a\\b", "C:/x", "a//b", "./a", "a:b", "NUL", "con.txt", "x/COM1.log", "dot.", abs.as_str()] {
assert!(ws.read(p).is_err(), "read accepted {p:?}");
@@ -220,7 +377,8 @@ mod tests {
#[test]
fn authorities_are_separate_and_fail_closed() {
let (inside, _, _) = roots("separate");
let directory = tempfile::tempdir().unwrap();
let (inside, _, _) = roots(directory.path());
std::fs::write(inside.join("r.txt"), b"r").unwrap();
let ro = AuthorizedWorkspace::new(Some(&inside), None).unwrap();
assert!(ro.read("r.txt").is_ok());
@@ -232,7 +390,8 @@ mod tests {
#[cfg(unix)]
#[test]
fn oversized_reads_are_rejected_without_allocating() {
let (inside, _, ws) = roots("oversized");
let directory = tempfile::tempdir().unwrap();
let (inside, _, ws) = roots(directory.path());
let file = std::fs::File::create(inside.join("huge.bin")).unwrap();
file.set_len(MAX_READ_BYTES + 1).unwrap();
let err = ws.read("huge.bin").unwrap_err().to_string();
@@ -242,7 +401,8 @@ mod tests {
#[cfg(unix)]
#[test]
fn symlink_escape_is_rejected() {
let (inside, outside, ws) = roots("symlink");
let directory = tempfile::tempdir().unwrap();
let (inside, outside, ws) = roots(directory.path());
std::fs::write(outside.join("secret.txt"), b"s").unwrap();
std::os::unix::fs::symlink(&outside, inside.join("link")).unwrap();
assert!(ws.read("link/secret.txt").is_err());
@@ -250,10 +410,37 @@ mod tests {
assert!(!outside.join("new.txt").exists());
}
#[cfg(unix)]
#[test]
fn save_keeps_the_resolved_destination_when_an_alias_is_retargeted() {
use std::os::unix::fs::symlink;
let directory = tempfile::tempdir().unwrap();
let (inside, _, workspace) = roots(directory.path());
std::fs::create_dir(inside.join("original")).unwrap();
std::fs::create_dir_all(inside.join("alternate/deep")).unwrap();
let alias = inside.join("alias");
symlink("original", &alias).unwrap();
let (destination, writer, save_path) = workspace.save_destination("alias/scene.sbs").unwrap();
std::fs::remove_file(&alias).unwrap();
symlink("alternate/deep", &alias).unwrap();
let parent = save_path.rsplit_once('/').map_or(String::new(), |(parent, _)| format!("{parent}/"));
writer.create_dir_all(&format!("{parent}resources")).unwrap();
writer.write_resource(&format!("{parent}resources/asset.bin"), b"asset").unwrap();
writer.write(&save_path, b"source").unwrap();
assert_eq!(destination, inside.join("original/scene.sbs"));
assert_eq!(std::fs::read(destination).unwrap(), b"source");
assert_eq!(std::fs::read(inside.join("original/resources/asset.bin")).unwrap(), b"asset");
assert_eq!(std::fs::read_dir(inside.join("alternate/deep")).unwrap().count(), 0);
}
#[cfg(windows)]
#[test]
fn junction_escape_is_rejected() {
let (inside, outside, ws) = roots("junction");
let directory = tempfile::tempdir().unwrap();
let (inside, outside, ws) = roots(directory.path());
std::fs::write(outside.join("secret.txt"), b"s").unwrap();
let link = inside.join("junction");
let made = std::process::Command::new("cmd.exe").args(["/D", "/C", "mklink", "/J"]).arg(&link).arg(&outside).output();
@@ -261,6 +448,7 @@ mod tests {
eprintln!("skipping: cannot create a junction");
return;
}
assert!(ws.read("junction/secret.txt").is_err());
assert!(ws.write("junction/new.txt", b"x").is_err());
assert!(!outside.join("new.txt").exists());
+102 -5
View File
@@ -4,7 +4,8 @@ use serde_json::json;
#[test]
fn reopening_relative_paths_activates_the_existing_document_and_preserves_edits() {
let root = std::env::temp_dir().join(format!("materialcraft-workspace-reopen-{}", std::process::id()));
let temporary = tempfile::tempdir().unwrap();
let root = temporary.path().to_path_buf();
std::fs::create_dir_all(&root).unwrap();
for extension in ["sbs", "mcraft"] {
@@ -42,13 +43,12 @@ fn reopening_relative_paths_activates_the_existing_document_and_preserves_edits(
assert!(root.join(&filename).is_file());
assert_eq!(reopened.session.path.as_deref(), Some(filename.as_str()));
}
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn both_formats_save_and_reopen_using_separate_workspace_capabilities() {
let root = std::env::temp_dir().join(format!("materialcraft-workspace-packages-{}", std::process::id()));
let temporary = tempfile::tempdir().unwrap();
let root = temporary.path().to_path_buf();
let input = root.join("input");
let output = root.join("output");
std::fs::create_dir_all(&input).unwrap();
@@ -83,5 +83,102 @@ fn both_formats_save_and_reopen_using_separate_workspace_capabilities() {
// Release workspace directory handles before Windows removes the test tree
drop(app);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn scoped_publication_resolves_linked_graphs_and_images_using_the_granted_read_root() {
use materialcraft_engine::core::{Graph, GraphInstance, GraphPort, Resource, ResourceBytes, Value};
use materialcraft_engine::{formats, image};
let directory = tempfile::tempdir().unwrap();
let dir = directory.path();
let input = dir.join("input");
let output = dir.join("published");
std::fs::create_dir_all(input.join("parts")).unwrap();
std::fs::create_dir_all(&output).unwrap();
let image = image::Image::filled(2, 2, image::Kind::Color, [0.25, 0.5, 0.75, 1.0]);
let pixels = formats::encode(&image, "png", formats::Depth::U8, false).unwrap();
let mut helpers = Package::new("Helpers");
helpers.resources.push(Resource::new("finish", "finish.png", ResourceBytes::new(pixels.clone())));
let mut finish = Graph::new("Finish");
let bitmap = finish.add_node("bitmap", [0.0; 2]);
finish.node_mut(bitmap).unwrap().params.insert("resource".into(), Value::Text("finish".into()));
let finish_output = finish.add_node("output", [0.0; 2]);
finish.node_mut(finish_output).unwrap().params.insert("identifier".into(), Value::Text("basecolor".into()));
finish.connect(bitmap, "output", finish_output, "input").unwrap();
helpers.graphs.push(finish);
let helper = formats::project::ProjectFiles::build(&helpers, "finish.sbs", false).unwrap();
formats::project::write_files(&input.join("parts/finish.sbs"), &helper).unwrap();
std::fs::write(dir.join("outside.sbs"), helper.xml().unwrap()).unwrap();
for (project, dependency) in [("surface.sbs", "parts/finish.sbs"), ("missing.sbs", "../outside.sbs")] {
let mut graph = Graph::new("Oak");
let call = graph.add_node("graph_instance", [0.; 2]);
graph.node_mut(call).unwrap().instance = Some(GraphInstance {
package: dependency.into(),
graph: "Finish".into(),
inputs: vec![],
outputs: vec![GraphPort { identifier: "basecolor".into(), type_code: 2 }],
defaults: Default::default(),
label: String::new(),
});
let node = graph.add_node("output", [200., 0.]);
graph.node_mut(node).unwrap().params.extend([("identifier".into(), Value::Text("color".into())), ("usage".into(), Value::Enum(0))]);
graph.connect(call, "basecolor", node, "input").unwrap();
let mut package = Package::new("Surface");
package.graphs.push(graph);
std::fs::write(input.join(project), formats::sbs::write_sbs(&package).unwrap()).unwrap();
let fs = AuthorizedWorkspace::new(Some(&input), Some(&output)).unwrap();
let mut app = Headless::new(Session::new(Package::new("Empty")), Filesystem::Workspace(fs));
app.load(project).unwrap();
let result = app.export_material(&json!({"path":format!("{project}.mcmat")}));
if project == "surface.sbs" {
assert!(result.is_ok(), "{result:?}");
let (pkg, entry) = formats::material::read_material(&std::fs::read(output.join("surface.sbs.mcmat")).unwrap()).unwrap();
let entry = pkg.graph(entry).unwrap();
assert_eq!(entry.name, "Oak");
assert_eq!(pkg.graphs.len(), 2);
let instance = entry.instances().next().unwrap();
assert!(instance.package.is_empty());
assert_eq!(entry.instances().count(), 1);
assert_eq!(pkg.graph_by_name(&instance.graph).unwrap().1.name, "Finish");
assert_eq!(pkg.resources.len(), 1);
assert_eq!(pkg.resources[0].bytes.as_slice(), pixels);
// Rebase links from the actual destination under the granted write root
app.save(Some("saved.sbs")).unwrap();
let xml = std::fs::read_to_string(output.join("saved.sbs")).unwrap();
assert!(xml.contains("../input/parts/finish.sbs"), "{xml}");
assert_eq!(app.session.path.as_deref(), Some("saved.sbs"));
let workspace = AuthorizedWorkspace::new(Some(dir), Some(&output)).unwrap();
let mut reopened = Headless::new(Session::new(Package::new("Empty")), Filesystem::Workspace(workspace));
reopened.load("published/saved.sbs").unwrap();
let helper_path = input.join("parts/finish.sbs").canonicalize().unwrap().to_string_lossy().into_owned();
assert!(reopened.session.pkg.dependencies.contains_key(&helper_path));
assert!(reopened.session.pkg.dependencies.values().any(|dependency| dependency.name == "Helpers"));
app.save(Some("new/sub/saved.sbs")).unwrap();
let xml = std::fs::read_to_string(output.join("new/sub/saved.sbs")).unwrap();
assert!(xml.contains("../../../input/parts/finish.sbs"), "{xml}");
reopened.load("published/new/sub/saved.sbs").unwrap();
assert!(reopened.session.pkg.dependencies.contains_key(&helper_path));
#[cfg(unix)]
{
std::fs::create_dir_all(output.join("nested/deep")).unwrap();
std::os::unix::fs::symlink("nested/deep", output.join("alias")).unwrap();
app.save(Some("alias/new/linked.sbs")).unwrap();
let xml = std::fs::read_to_string(output.join("nested/deep/new/linked.sbs")).unwrap();
assert!(xml.contains("../../../../input/parts/finish.sbs"), "{xml}");
reopened.load("published/nested/deep/new/linked.sbs").unwrap();
assert!(reopened.session.pkg.dependencies.values().any(|dependency| dependency.name == "Helpers"));
}
} else {
let error = result.unwrap_err().to_string();
assert!(error.contains("the instanced graph does not exist"), "{error}");
let outside = dir.join("outside.sbs").canonicalize().unwrap().to_string_lossy().into_owned();
assert!(!app.session.pkg.dependencies.contains_key(&outside));
assert!(!app.session.pkg.dependencies.values().any(|dependency| dependency.name == "Helpers"));
assert!(!output.join("missing.sbs.mcmat").exists());
}
}
}
+7
View File
@@ -9,6 +9,7 @@ license.workspace = true
workspace = true
[dependencies]
craft-materialx = { workspace = true }
materialcraft-core = { workspace = true }
craft-texture = { workspace = true }
materialcraft-nodes = { workspace = true }
@@ -20,3 +21,9 @@ serde = { workspace = true }
serde_json = { workspace = true }
sha2 = { workspace = true }
thiserror = { workspace = true }
[target.'cfg(unix)'.dependencies]
rustix.workspace = true
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tempfile = "3"
+1
View File
@@ -64,6 +64,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
v.extend(crate::node_info_commands::specs());
v.extend(crate::atomic_sample_commands::specs());
v.extend(crate::material_cmds::specs());
v.extend(crate::material_bake::specs());
v.extend(crate::edit_cmds::specs());
v.extend(crate::document_history_commands::specs());
v.extend(crate::library_cache_commands::specs());
+40 -12
View File
@@ -36,7 +36,12 @@ impl ExportRequest {
/// Read `file.export` params, checking them against the session's package
pub fn from_params(s: &Session, p: &J) -> Result<Self> {
let graph_index = u64p(p, "graph").map_or(s.graph, |index| index as usize);
// Preserve historical numeric selectors alongside exact SBS paths
let graph_index = if p.get("graph").is_some_and(J::is_number) {
u64p(p, "graph").and_then(|i| usize::try_from(i).ok()).ok_or_else(|| bad(CMD, "`graph` must be a nonnegative index"))?
} else {
crate::graph_cmds::graph_index(s, p)?
};
let graph = s.pkg.graph(graph_index)?;
let format = strp(p, "format").unwrap_or("png").trim_start_matches('.').to_ascii_lowercase();
@@ -44,12 +49,14 @@ impl ExportRequest {
return Err(bad(CMD, format!("unknown format `{format}`")));
}
let depth = match u64p(p, "depth") {
None => None,
Some(8) => Some(Depth::U8),
Some(16) => Some(Depth::U16),
Some(32) => Some(Depth::F32),
Some(depth) => return Err(bad(CMD, format!("depth must be 8, 16 or 32, not {depth}"))),
let depth = match p.get("depth") {
None | Some(J::Null) => None,
Some(value) => Some(match value.as_f64() {
Some(8.0) => Depth::U8,
Some(16.0) => Depth::U16,
Some(32.0) => Depth::F32,
_ => return Err(bad(CMD, format!("depth must be 8, 16 or 32, not {value}"))),
}),
};
let identifiers = output_identifiers(graph);
@@ -64,7 +71,7 @@ impl ExportRequest {
format,
pattern: strp(p, "pattern").unwrap_or("$(graph)_$(identifier)").to_string(),
depth,
size: export_size(p.get("size"))?,
size: export_size(CMD, p.get("size"))?,
outputs,
color_spaces: color_spaces(p.get("colorSpaces"), &identifiers, &graph.name)?,
})
@@ -121,7 +128,7 @@ fn known_output<'a>(identifier: &'a str, identifiers: &[String], graph: &str) ->
}
/// `size`: the larger side in pixels (0 = the graph's size) or `[width, height]`
fn export_size(value: Option<&J>) -> Result<Option<ExportSize>> {
pub(crate) fn export_size(command: &str, value: Option<&J>) -> Result<Option<ExportSize>> {
let pixels = |value: &J| value.as_u64().or_else(|| value.as_f64().filter(|f| f.is_finite() && *f >= 0.0).map(|f| f.round() as u64));
let clamp = |pixels: u64| pixels.min(u64::from(u32::MAX)) as u32;
@@ -130,14 +137,35 @@ fn export_size(value: Option<&J>) -> Result<Option<ExportSize>> {
Some(J::Array(sides)) => match sides.as_slice() {
[width, height] => match (pixels(width), pixels(height)) {
(Some(width), Some(height)) => Ok(Some(ExportSize::Exact([clamp(width), clamp(height)]))),
_ => Err(bad(CMD, "`size` [width, height] takes two pixel counts")),
_ => Err(bad(command, "`size` [width, height] takes two pixel counts")),
},
_ => Err(bad(CMD, "`size` [width, height] takes two pixel counts")),
_ => Err(bad(command, "`size` [width, height] takes two pixel counts")),
},
Some(value) => match pixels(value) {
Some(0) => Ok(None),
Some(side) => Ok(Some(ExportSize::LargerSide(clamp(side)))),
None => Err(bad(CMD, "`size` is a pixel count or [width, height]")),
None => Err(bad(command, "`size` is a pixel count or [width, height]")),
},
}
}
#[cfg(test)]
mod selector_tests {
use super::*;
use serde_json::json;
#[test]
fn file_export_keeps_numeric_coercion_and_exact_graph_paths() {
let mut package = materialcraft_core::Package::new("selectors");
package.graphs.push(Graph::new("1"));
package.graphs.push(Graph::new("Second"));
let session = Session::new(package);
for selector in [json!(1), json!(1.0), json!(0.6)] {
assert_eq!(ExportRequest::from_params(&session, &json!({"graph":selector})).unwrap().graph, 1);
}
assert_eq!(ExportRequest::from_params(&session, &json!({"graph":"1"})).unwrap().graph, 0);
for selector in [json!(-1), json!(2.0), json!(1e100)] {
assert!(ExportRequest::from_params(&session, &json!({"graph":selector})).is_err());
}
}
}
+5 -4
View File
@@ -1,5 +1,6 @@
/// The pixel size `file.export` cooks a graph at
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum ExportSize {
/// The larger side in pixels; the other keeps the graph's proportions
LargerSide(u32),
@@ -13,9 +14,9 @@ impl ExportSize {
match self {
ExportSize::LargerSide(pixels) => {
let target = log2_pixels(pixels);
let bounded = graph_size.map(|side| side.clamp(4, 13));
let bounded = graph_size.map(|side| side.clamp(materialcraft_core::MIN_SIZE_LOG2, materialcraft_core::MAX_SIZE_LOG2));
let larger = bounded[0].max(bounded[1]);
bounded.map(|side| (side + target).saturating_sub(larger).max(4))
bounded.map(|side| (side + target).saturating_sub(larger).max(materialcraft_core::MIN_SIZE_LOG2))
}
ExportSize::Exact(pixels) => pixels.map(log2_pixels),
}
@@ -23,5 +24,5 @@ impl ExportSize {
}
fn log2_pixels(pixels: u32) -> u32 {
31 - pixels.clamp(16, 8192).leading_zeros()
pixels.clamp(1 << materialcraft_core::MIN_SIZE_LOG2, 1 << materialcraft_core::MAX_SIZE_LOG2).ilog2()
}
+49 -27
View File
@@ -38,7 +38,7 @@ pub(crate) fn specs() -> Vec<CommandSpec> {
"Export outputs...",
["File"],
Some("Ctrl+Shift+E"),
"{dir, format? (png|jpg|tga|bmp|webp|exr|hdr), pattern? ($(graph)_$(identifier)), depth? (8|16|32), size? (px of the larger side, or [width, height]; rounded down to powers of two, 16..8192; default the graph's size, keeping its proportions), outputs? ([identifier]: default every connected output), colorSpaces? ({identifier: srgb|linear|raw}: default sRGB for color usages, raw for data; linear and raw write values unchanged), graph?} cooks the graph at full size and writes the outputs",
"{dir, format? (png|jpg|tga|bmp|webp|exr|hdr), pattern? ($(graph)_$(identifier)), depth? (8|16|32), size? (px of the larger side, or [width, height]; rounded down to powers of two, 16..8192; default the graph's size, keeping its proportions), outputs? ([identifier]: default every connected output), colorSpaces? ({identifier: srgb|linear|raw}: default sRGB for color usages, raw for data; linear and raw write values unchanged), graph?, profile? (maps|materialx), preset?, values? {parameter: value}, inputs? {identifier: image path}, normalFormat? (opengl|directx), displacementScale?} cooks the selected outputs and writes the requested export",
has_graph,
export
),
@@ -178,6 +178,22 @@ pub fn open_bytes(session: &mut Session, bytes: &[u8], name: Option<String>) ->
}
pub fn open_bytes_linked(session: &mut Session, bytes: &[u8], name: Option<String>, linked: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<()> {
open_resolved(session, bytes, name, linked, crate::linked_files::load)
}
/// Resolve every external graph and its assets through the caller's read capability.
pub fn open_bytes_scoped(session: &mut Session, bytes: &[u8], path: &str, read: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<()> {
let assets = |reference: &str| read(&crate::document_path::resolve(path, reference));
open_resolved(session, bytes, Some(path.into()), &assets, |pkg| crate::linked_files::load_scoped(pkg, read))
}
fn open_resolved(
session: &mut Session,
bytes: &[u8],
name: Option<String>,
linked: &dyn Fn(&str) -> Option<Vec<u8>>,
load: impl FnOnce(&mut Package) -> Vec<String>,
) -> Result<()> {
let name = name.map(|path| crate::document_path::normalize(&path));
if let Some(name) = &name
&& let Some((id, ..)) = session.open_packages().into_iter().find(|entry| entry.2 == Some(name.as_str()))
@@ -185,10 +201,10 @@ pub fn open_bytes_linked(session: &mut Session, bytes: &[u8], name: Option<Strin
return session.activate_document(id);
}
if materialcraft_formats::material::is_material(bytes) {
let (manifest, source) = materialcraft_formats::material::read_material(bytes)?;
session.open_package(materialcraft_formats::sbs::read_sbs(&source, "source.sbs", &|_| None)?.pkg, None)?;
session.graph = session.pkg.graph_by_name(&manifest.graph).map_or(0, |(index, _)| index);
if name.as_deref().is_some_and(|path| has_extension(path, "mcmat")) || materialcraft_formats::material::is_material(bytes) {
let (package, entry) = materialcraft_formats::material::read_material(bytes)?;
session.open_package(package, None)?;
session.graph = entry;
session.open_material_preview();
return Ok(());
}
@@ -197,7 +213,7 @@ pub fn open_bytes_linked(session: &mut Session, bytes: &[u8], name: Option<Strin
for (path, source) in &session.pkg.dependencies {
read.pkg.dependencies.entry(path.clone()).or_insert_with(|| source.clone());
}
let link_warnings = crate::linked_files::load(&mut read.pkg);
let link_warnings = load(&mut read.pkg);
let path = if read.warnings.is_empty() { name } else { None };
read.warnings.extend(link_warnings);
session.open_package(read.pkg, path)?;
@@ -254,16 +270,19 @@ fn package_for_save(session: &Session, path: Option<&str>) -> Package {
}
pub fn save_files_at(session: &Session, path: &str) -> Result<materialcraft_formats::project::ProjectFiles> {
session.package(session.document_id)?;
let archive = is_mcraft(path);
let package = if archive { session.pkg.clone() } else { package_for_save(session, Some(path)) };
let active_package = session.package(session.document_id)?;
let filename = std::path::Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or("package.sbs");
if is_mcraft(path) {
return Ok(materialcraft_formats::project::ProjectFiles::build(active_package, filename, true)?);
}
let package = package_for_save(session, Some(path));
#[cfg(not(target_arch = "wasm32"))]
if !archive && materialcraft_formats::sbs::library::contains_file(std::path::Path::new(path)) {
if materialcraft_formats::sbs::library::contains_file(std::path::Path::new(path)) {
return Ok(materialcraft_formats::project::ProjectFiles::build_linked(&package, filename)?);
}
Ok(materialcraft_formats::project::ProjectFiles::build(&package, filename, archive)?)
Ok(materialcraft_formats::project::ProjectFiles::build(&package, filename, false)?)
}
#[cfg(not(target_arch = "wasm32"))]
@@ -345,21 +364,15 @@ fn save(_: &mut Session, _: &J) -> Result<J> {
/// Cook the requested graph at full size and encode its requested outputs (`pattern` names files)
pub fn export_outputs(s: &Session, request: &ExportRequest) -> Result<Vec<Exported>> {
let mut package = s.pkg.clone();
if let Some(size) = request.size {
let graph = package.graph_mut(request.graph)?;
graph.size = size.log2_size(graph.size);
}
let graph = package.graph(request.graph)?;
let decoder = s.resource_decoder();
let mut options = materialcraft_eval::Options::new(&decoder);
options.environment = s.environment;
let cooked = materialcraft_eval::cook(&package, request.graph, &options, &s.cache);
let options = crate::MaterialOptions { size: request.size, ..Default::default() };
let cooked = options.cook(s, request.graph, request.outputs.as_deref(), None)?;
encode_outputs(s.pkg.graph(request.graph)?, &cooked, request)
}
pub(crate) fn encode_outputs(graph: &materialcraft_core::Graph, cooked: &materialcraft_eval::Cooked, request: &ExportRequest) -> Result<Vec<Exported>> {
let mut files = Vec::new();
let mut names = std::collections::HashSet::new();
for (id, usage, identifier, image) in materialcraft_eval::outputs(graph, &cooked) {
for (id, usage, identifier, image) in materialcraft_eval::outputs(graph, cooked) {
let Some(image) = image.filter(|_| request.includes(&identifier)) else { continue };
let file = format!("{}.{}", output_name(&request.pattern, &graph.name, &identifier, usage), request.format);
@@ -370,7 +383,13 @@ pub fn export_outputs(s: &Session, request: &ExportRequest) -> Result<Vec<Export
let node = graph.node(id)?;
let output_format = if node.base.format == materialcraft_core::OutputFormat::Inherit { graph.format } else { node.base.format };
let depth = request.depth.unwrap_or_else(|| Depth::from_format(output_format));
let bytes = encode(image, &request.format, depth, request.color_space(&identifier, usage).encodes_srgb())?;
// Keep grayscale float exports scene-linear unless the caller picks a color space
let srgb = request.color_space(&identifier, usage).encodes_srgb()
&& !(image.kind() == craft_texture::Kind::Gray
&& matches!(request.format.as_str(), "exr" | "hdr")
&& !request.color_spaces.contains_key(&identifier));
let bytes = encode(image, &request.format, depth, srgb)?;
files.push(Exported { file, bytes });
}
@@ -385,12 +404,14 @@ pub fn export_outputs(s: &Session, request: &ExportRequest) -> Result<Vec<Export
#[cfg(not(target_arch = "wasm32"))]
fn export(s: &mut Session, p: &J) -> Result<J> {
let dir = strp(p, "dir").ok_or_else(|| bad("file.export", "`dir` is required"))?;
let files = export_outputs(s, &ExportRequest::from_params(s, p)?)?;
let (files, mut report) = crate::material_bake::export(s, p, crate::material_bake::read_input)?;
let folder = std::path::Path::new(dir);
std::fs::create_dir_all(folder).map_err(|error| bad("file.export", format!("{dir}: {error}")))?;
// Check every name first so a rejected export leaves the folder untouched
if let Some(existing) = files.iter().find(|exported| folder.join(&exported.file).exists()) {
if p.get("profile").and_then(J::as_str) != Some("materialx")
&& let Some(existing) = files.iter().find(|exported| folder.join(&exported.file).exists())
{
return Err(bad(
"file.export",
format!("duplicate export filename `{}`: use unique output identifiers or a different filename pattern", existing.file),
@@ -404,7 +425,8 @@ fn export(s: &mut Session, p: &J) -> Result<J> {
written.push(path.display().to_string());
}
s.log(format!("Exported {} files to {dir}", written.len()));
Ok(json!({"files": written}))
report["files"] = json!(written);
Ok(report)
}
#[cfg(target_arch = "wasm32")]
+13 -8
View File
@@ -2,7 +2,7 @@
use serde_json::{Value as J, json};
use materialcraft_core::{MAX_SIZE_LOG2, MIN_SIZE_LOG2, NodeId, OutputFormat, Value};
use materialcraft_core::{Graph, MAX_SIZE_LOG2, MIN_SIZE_LOG2, NodeId, OutputFormat, Value};
use crate::commands::{CommandSpec, always, bad, boolp, has_graph, has_selection, i64p, ids_param, strp, u64p};
use crate::{Result, Session, cmd};
@@ -59,12 +59,14 @@ pub(crate) fn specs() -> Vec<CommandSpec> {
]
}
fn graph_index(s: &Session, p: &J) -> Result<usize> {
pub(crate) fn graph_index(s: &Session, p: &J) -> Result<usize> {
match p.get("graph") {
None | Some(J::Null) => Ok(s.graph),
Some(J::String(name)) => s.pkg.graph_by_name(name).map(|(i, _)| i).ok_or_else(|| bad("graph", format!("no graph `{name}`"))),
Some(J::String(name)) => {
s.pkg.graph_by_name(name).map(|(i, _)| i).ok_or_else(|| bad("graph", format!("unknown or ambiguous graph `{name}`; use its full SBS path")))
}
Some(v) => {
let i = v.as_u64().ok_or_else(|| bad("graph", "`graph` is an index or a name"))? as usize;
let i = v.as_u64().and_then(|i| usize::try_from(i).ok()).ok_or_else(|| bad("graph", "`graph` is an index or an SBS path"))?;
s.pkg.graph(i)?;
Ok(i)
}
@@ -233,16 +235,19 @@ fn preset_apply(s: &mut Session, p: &J) -> Result<J> {
s.edit("Apply preset", |s| {
let g = s.graph_mut()?;
let preset = g.presets.iter().find(|x| x.name == name).cloned().ok_or_else(|| bad("graph.preset.apply", format!("no preset `{name}`")))?;
for (k, v) in preset.values {
// Only inputs that still exist, with their current type
if g.exposed.iter().any(|e| e.id == k && e.default.type_name() == v.type_name()) {
g.inputs.insert(k, v);
for (identifier, value) in preset.values {
if preset_value_matches(g, &identifier, &value) {
g.inputs.insert(identifier, value);
}
}
Ok(J::Null)
})
}
pub(crate) fn preset_value_matches(graph: &Graph, identifier: &str, value: &Value) -> bool {
graph.exposed.iter().any(|input| input.id == identifier && input.default.type_name() == value.type_name())
}
fn preset_delete(s: &mut Session, p: &J) -> Result<J> {
let name = preset_name(p, "graph.preset.delete")?;
s.edit("Delete preset", |s| {
+1
View File
@@ -70,6 +70,7 @@ pub use library_cache_prefs::LibraryCachePrefs;
pub mod graph_edit;
pub mod graph_grid;
pub mod inspect;
pub mod material_bake;
pub mod material_cmds;
pub mod node_cmds;
pub mod samples;
+88 -20
View File
@@ -51,10 +51,42 @@ fn references(package: &Package) -> Vec<String> {
/// Seed the registry with already parsed library packages before traversing dependencies. The
/// registry stays flat so shared dependencies are parsed once and cannot form Arc ownership cycles.
pub(crate) fn load_references(package: &mut Package, mut pending: Vec<String>) -> Vec<String> {
pub(crate) fn load_references(package: &mut Package, pending: Vec<String>) -> Vec<String> {
resolve(package, pending, &load_file)
}
pub(crate) fn load_scoped(package: &mut Package, read: &dyn Fn(&str) -> Option<Vec<u8>>) -> Vec<String> {
resolve(package, references(package), &|path| {
if path.starts_with("sbs://") {
return load_file(path);
}
let bytes = read(path).ok_or_else(|| format!("missing or inaccessible dependency `{path}`"))?;
let assets = |reference: &str| read(&document_path::resolve(path, reference));
crate::file_cmds::read_package_bytes(&bytes, path, &assets).map_err(|error| error.to_string())
})
}
fn load_file(path: &str) -> Result<materialcraft_formats::sbs::SbsRead, String> {
let bytes = if path.starts_with("sbs://") {
materialcraft_formats::sbs::library::read(path)?
} else {
#[cfg(target_arch = "wasm32")]
return Err("external file is unavailable in the browser".into());
#[cfg(not(target_arch = "wasm32"))]
crate::linked_resource::read_regular(std::path::Path::new(path)).map_err(|error| error.to_string())?
};
crate::file_cmds::read_package_bytes(&bytes, path, &crate::file_cmds::linked_files(path)).map_err(|error| error.to_string())
}
const MAX_DEPENDENCY_READS: usize = 4096;
fn resolve(package: &mut Package, mut pending: Vec<String>, read: &dyn Fn(&str) -> Result<materialcraft_formats::sbs::SbsRead, String>) -> Vec<String> {
use std::{collections::BTreeSet, sync::Arc};
let mut visited = BTreeSet::new();
let mut warnings = Vec::new();
let mut read_attempts = 0;
while let Some(path) = pending.pop() {
if !visited.insert(path.clone()) {
continue;
@@ -65,27 +97,20 @@ pub(crate) fn load_references(package: &mut Package, mut pending: Vec<String>) -
continue;
}
let mut read = || -> std::result::Result<Package, String> {
if path.starts_with("sbs://") {
let bytes = materialcraft_formats::sbs::library::read(&path)?;
let read = materialcraft_formats::sbs::read_sbs(&bytes, &path, &crate::file_cmds::linked_files(&path)).map_err(|error| error.to_string())?;
warnings.extend(read.warnings);
return Ok(read.pkg);
}
// Cached snapshots only need traversal, while failed reads still spend the allowance
if read_attempts == MAX_DEPENDENCY_READS {
warnings.push("linked material exceeds the package dependency read limit".into());
break;
}
#[cfg(target_arch = "wasm32")]
return Err("external file is unavailable in the browser".into());
#[cfg(not(target_arch = "wasm32"))]
{
let bytes = std::fs::read(&path).map_err(|error| error.to_string())?;
let read = materialcraft_formats::sbs::read_sbs(&bytes, &path, &crate::file_cmds::linked_files(&path)).map_err(|error| error.to_string())?;
read_attempts += 1;
match read(&path) {
Ok(read) => {
warnings.extend(read.warnings);
Ok(read.pkg)
}
};
match read() {
Ok(mut source) => {
normalize_references(&mut source, &path);
let source = read.pkg;
for (name, dependency) in &source.dependencies {
package.dependencies.entry(name.clone()).or_insert_with(|| dependency.clone());
}
pending.extend(references(&source));
package.dependencies.insert(path, Arc::new(source));
}
@@ -95,3 +120,46 @@ pub(crate) fn load_references(package: &mut Package, mut pending: Vec<String>) -
warnings
}
#[cfg(test)]
mod tests {
use super::*;
use std::{cell::Cell, sync::Arc};
#[test]
fn cached_paths_do_not_spend_the_new_dependency_read_budget() {
for cached_first in [true, false] {
let mut package = Package::new("root");
let mut pending = vec!["new".to_string()];
for i in 0..MAX_DEPENDENCY_READS {
let name = format!("cached{i}");
package.dependencies.insert(name.clone(), Arc::new(Package::new(&name)));
pending.push(name);
}
if !cached_first {
pending.reverse();
}
let calls = Cell::new(0);
let warnings = resolve(&mut package, pending, &|path| {
calls.set(calls.get() + 1);
assert_eq!(path, "new");
Ok(materialcraft_formats::sbs::SbsRead { pkg: Package::new(path), warnings: vec![] })
});
assert!(warnings.is_empty(), "{warnings:?}");
assert_eq!(calls.get(), 1);
assert!(package.dependencies.contains_key("new"));
}
}
#[test]
fn failed_new_reads_still_spend_the_budget_and_duplicates_do_not() {
let calls = Cell::new(0);
let pending = (0..=MAX_DEPENDENCY_READS).flat_map(|i| [format!("missing{i}"), format!("missing{i}")]).collect();
let warnings = resolve(&mut Package::new("root"), pending, &|_| {
calls.set(calls.get() + 1);
Err("missing".into())
});
assert_eq!(calls.get(), MAX_DEPENDENCY_READS);
assert!(warnings.last().unwrap().contains("dependency read limit"));
}
}
+30 -5
View File
@@ -5,6 +5,34 @@ use std::io::Read as _;
#[cfg(not(target_arch = "wasm32"))]
const MAX_LINKED_RESOURCE_BYTES: u64 = 256 << 20;
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn read_regular(path: &std::path::Path) -> std::io::Result<Vec<u8>> {
use std::io::{Error, ErrorKind};
let mut open = std::fs::OpenOptions::new();
open.read(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
// A named pipe must reach the regular-file check without waiting for a writer
open.custom_flags(rustix::fs::OFlags::NONBLOCK.bits() as i32);
}
let file = open.open(path)?;
let metadata = file.metadata()?;
let invalid = || Error::new(ErrorKind::InvalidData, "expected a regular file of at most 256 MiB");
if !metadata.is_file() || metadata.len() > MAX_LINKED_RESOURCE_BYTES {
return Err(invalid());
}
let mut data = Vec::new();
file.take(MAX_LINKED_RESOURCE_BYTES + 1).read_to_end(&mut data)?;
if data.len() as u64 > MAX_LINKED_RESOURCE_BYTES {
return Err(invalid());
}
Ok(data)
}
/// Read resource paths relative to an SBS package, including portable library aliases.
pub fn reader(path: &str) -> impl Fn(&str) -> Option<Vec<u8>> + use<> {
let owner = path.to_string();
@@ -44,16 +72,13 @@ pub fn reader(path: &str) -> impl Fn(&str) -> Option<Vec<u8>> + use<> {
let folder = folder.as_ref()?;
let file = std::fs::canonicalize(folder.join(&relative)).ok()?;
let metadata = std::fs::metadata(&file).ok()?;
// Parent references are legal inside the configured library, never beyond its root
if !file.starts_with(allowed_root.as_ref()?) || !metadata.is_file() || metadata.len() > MAX_LINKED_RESOURCE_BYTES {
if !file.starts_with(allowed_root.as_ref()?) {
return None;
}
let mut bytes = Vec::new();
std::fs::File::open(&file).ok()?.take(MAX_LINKED_RESOURCE_BYTES + 1).read_to_end(&mut bytes).ok()?;
(bytes.len() as u64 <= MAX_LINKED_RESOURCE_BYTES).then_some(bytes)
read_regular(&file).ok()
}
#[cfg(target_arch = "wasm32")]
None
+283
View File
@@ -0,0 +1,283 @@
//! Inspect and evaluate the published graph through the same CPU cook as an authoring document.
use std::collections::BTreeSet;
use craft_texture::Kind;
use materialcraft_core::{Graph, GraphIndex, NodeId};
use materialcraft_eval::{Cooked, outputs};
use materialcraft_formats::{Depth, encode};
use serde_json::{Value as J, json};
use sha2::{Digest, Sha256};
use crate::commands::{CommandSpec, bad, has_graph};
use crate::{ExportRequest, Exported, MaterialOptions, Result, Session, cmd, graph_cmds::graph_index};
pub(crate) fn specs() -> Vec<CommandSpec> {
vec![
cmd!(
"material.info",
"Inspect material",
[],
None,
"{graph?} returns the material interface, published values, image inputs, outputs, presets and supported sizes",
has_graph,
|s, p| info(s, p)
),
cmd!(
"material.cook",
"Cook material",
[],
None,
"{graph?, size?, preset?, values?, inputs?} evaluates a variation and returns its output dimensions and means",
has_graph,
|s, p| cook(s, p, read_input)
),
]
}
#[cfg(not(target_arch = "wasm32"))]
pub fn read_input(path: &str) -> Result<Vec<u8>> {
crate::linked_resource::read_regular(std::path::Path::new(path)).map_err(|error| bad("material.cook", format!("{path}: {error}")))
}
#[cfg(target_arch = "wasm32")]
pub fn read_input(_: &str) -> Result<Vec<u8>> {
Err(bad("material.cook", "bind an image resource in the browser"))
}
pub fn info(s: &Session, p: &J) -> Result<J> {
let index = graph_index(s, p)?;
let graph = s.pkg.graph(index)?;
let parameters: Vec<_> = graph
.exposed
.iter()
.map(|param| {
let mut json = json!(param);
json["value"] = json!(graph.input_value(&param.id));
json
})
.collect();
Ok(json!({"version": materialcraft_formats::material::VERSION, "graph":graph.sbs_path(), "name":graph.display_name(),
"size":graph.size_px(), "supportedSizes":(materialcraft_core::MIN_SIZE_LOG2..=materialcraft_core::MAX_SIZE_LOG2).map(|v| 1u32 << v).collect::<Vec<_>>(),
"parameters":parameters,"presets":graph.presets,"inputs":crate::material_cmds::image_inputs(graph),
"outputs":graph.nodes.iter().filter(|n|n.def=="output").map(|n|json!({"identifier":materialcraft_eval::identifier(n),"usage":materialcraft_eval::output_usage(n)})).collect::<Vec<_>>()
}))
}
pub(crate) fn check_publish(s: &Session, index: usize) -> Result<()> {
let graph = s.pkg.graph(index)?;
if graph.function.is_some() {
return Err(bad("file.exportMaterial", "select a material graph"));
}
let cooked = MaterialOptions::default().cook(s, index, None, Some(1 << materialcraft_core::MIN_SIZE_LOG2))?;
if !outputs(graph, &cooked).into_iter().any(|(id, _, _, image)| image.is_some() && graph.connections.iter().any(|wire| wire.to == id)) {
return Err(bad("file.exportMaterial", format!("graph `{}` has no connected, cookable material outputs", graph.sbs_path())));
}
Ok(())
}
pub fn cook(s: &Session, p: &J, read: impl Fn(&str) -> Result<Vec<u8>>) -> Result<J> {
let index = graph_index(s, p)?;
let graph = s.pkg.graph(index)?;
let cooked = MaterialOptions::from_params(graph, p, read)?.cook(s, index, None, None)?;
Ok(json!({"graph":graph.sbs_path(),"ms":cooked.ms,
"outputs":outputs(graph,&cooked).into_iter().filter_map(|(_,usage,id,image)| {
let image = image?;
Some(json!({"identifier":id,"usage":usage,"size":[image.width(),image.height()],"mean":image.mean()}))
}).collect::<Vec<_>>() }))
}
/// Encode every file before the caller writes to its destination. Paths in the report are relative.
pub fn export(s: &Session, p: &J, read: impl Fn(&str) -> Result<Vec<u8>>) -> Result<(Vec<Exported>, J)> {
let option = |key| p.get(key).filter(|value| !value.is_null());
let request = ExportRequest::from_params(s, p)?;
let graph = s.pkg.graph(request.graph)?;
let selected = request.outputs.as_deref();
let materialx = match option("profile") {
Some(value) if value == "materialx" => true,
Some(value) if value == "maps" => false,
Some(value) => return Err(bad("file.export", format!("unknown export profile {value}"))),
None => {
if option("normalFormat").is_some() || option("displacementScale").is_some() {
return Err(bad("file.export", "normalFormat and displacementScale require an explicit export profile"));
}
let options = MaterialOptions::from_params(graph, p, read)?;
let cooked = options.cook(s, request.graph, selected, None)?;
return Ok((crate::file_cmds::encode_outputs(graph, &cooked, &request)?, json!({})));
}
};
if option("format").is_some() || option("pattern").is_some() {
return Err(bad("file.export", "named profiles choose their own format and filenames; omit format and pattern"));
}
let scale = match option("displacementScale") {
Some(_) if !materialx => return Err(bad("file.export", "`displacementScale` requires the materialx profile")),
Some(value) => {
value.as_f64().map(|x| x as f32).filter(|x| x.is_finite() && *x >= 0.0).ok_or_else(|| bad("file.export", "invalid displacementScale"))?
}
None => 1.0,
};
let normal_y_down = match option("normalFormat") {
None => None,
Some(value) if value == "opengl" => Some(false),
Some(value) if value == "directx" => Some(true),
Some(_) => return Err(bad("file.export", "normalFormat must be opengl or directx")),
};
let options = MaterialOptions::from_params(graph, p, read)?;
let cooked = options.cook(s, request.graph, selected, None)?;
let (files, mut report) = encode_maps(graph, &cooked, &request, materialx, scale, normal_y_down)?;
// Report the same graph selected by the legacy numeric coercion
let mut interface = info(s, &json!({"graph": request.graph}))?;
if let Some(params) = interface["parameters"].as_array_mut() {
for parameter in params {
if let Some(value) = parameter["id"].as_str().and_then(|id| options.values.get(id)) {
parameter["value"] = json!(value);
}
}
}
report["material"] = interface;
Ok((files, report))
}
fn shader_input(usage: &str) -> Option<&'static str> {
Some(match usage {
"baseColor" | "diffuse" => "base_color",
"roughness" => "specular_roughness",
"metallic" => "base_metalness",
"normal" => "geometry_normal",
"emissive" => "emission_color",
"opacity" => "geometry_opacity",
"height" => "height",
"coatOpacity" => "coat_weight",
"coatRoughness" => "coat_roughness",
"transmissive" => "transmission_weight",
"scattering" => "subsurface_weight",
"sheenOpacity" => "fuzz_weight",
"sheenColor" => "fuzz_color",
"sheenRoughness" => "fuzz_roughness",
"anisotropyLevel" => "specular_roughness_anisotropy",
_ => return None,
})
}
/// Direct normal generators expose their evaluated convention. Raw images default to OpenGL.
fn normal_y_down(index: &GraphIndex<'_>, cooked: &Cooked, output: NodeId) -> bool {
let mut source = output;
for _ in 0..cooked.nodes.len() {
let Some(node) = index.input_of(source, "input").and_then(|wire| index.node(wire.from)) else { break };
if node.def == "normal" && !node.disabled {
return cooked.nodes.get(&node.id).and_then(|result| result.parameters.get("opengl")).and_then(materialcraft_core::Value::as_bool) == Some(false);
}
if node.def != "dot" {
break;
}
source = node.id;
}
false
}
fn encode_maps(
graph: &Graph,
cooked: &Cooked,
request: &ExportRequest,
materialx: bool,
scale: f32,
source_y_down: Option<bool>,
) -> Result<(Vec<Exported>, J)> {
let index = GraphIndex::new(graph);
let mut files = Vec::new();
let mut maps = Vec::new();
let mut textures = Vec::new();
let mut unmapped = Vec::new();
let mut names = BTreeSet::new();
let mut shader_inputs = BTreeSet::new();
for (id, usage, identifier, image) in outputs(graph, cooked) {
let Some(image) = image.filter(|_| request.includes(&identifier) && !index.incoming(id).is_empty()) else { continue };
let shader = shader_input(usage);
let color = materialcraft_formats::material::srgb_usage(usage);
let space = request.color_space(&identifier, usage);
if materialx && shader.is_some() && !color && space.encodes_srgb() {
return Err(bad("file.export", format!("OpenPBR requires linear data for `{identifier}`; select raw or linear")));
}
let height = materialx && usage == "height";
// MaterialX takes signed scene-unit displacement and tangent-space Y-up normals
let converted;
let image = if height {
converted = image.map(Kind::Gray, |pixel| [(pixel[0] - 0.5) * scale, 0.0, 0.0, 1.0]);
&converted
} else if matches!(usage, "normal" | "coatNormal") && source_y_down.unwrap_or_else(|| normal_y_down(&index, cooked, id)) {
converted = image.map(Kind::Color, |[r, g, b, a]| [r, 1.0 - g, b, a]);
&converted
} else {
image
};
let float = height || request.depth == Some(Depth::F32) || image.data().iter().any(|value| !(0.0..=1.0).contains(value));
if float && request.color_spaces.get(&identifier).is_some_and(|space| space.encodes_srgb()) {
return Err(bad("file.export", format!("EXR output `{identifier}` requires linear values; omit the sRGB override or select raw or linear")));
}
let (ext, depth) = if float {
("exr", Depth::F32)
} else {
let depth = request.depth.unwrap_or(if color { Depth::U8 } else { Depth::U16 });
("png", depth)
};
let stem = materialcraft_formats::safe_file_name(&identifier);
if !names.insert(format!("{stem}.{ext}").to_lowercase()) {
return Err(bad("file.export", format!("duplicate output filename `{stem}.{ext}`; give each output a unique identifier")));
}
let srgb = !float && space.encodes_srgb();
let bytes = encode(image, ext, depth, srgb)?;
let file = if materialx {
// Keep every texture referenced by the last complete document until the new write succeeds
const MAX_STEM_CHARS: usize = 32;
let digest = Sha256::new().chain_update(stem.as_bytes()).chain_update([0]).chain_update(&bytes).finalize();
let prefix: String = stem.chars().take(MAX_STEM_CHARS).collect();
format!("{prefix}-{digest:x}.{ext}")
} else {
format!("{stem}.{ext}")
};
maps.push(json!({"identifier":identifier,"usage":usage,"file":file,"size":[image.width(),image.height()],"colorSpace":if srgb {"srgb"}else{"linear"}}));
if materialx {
if let Some(input) = shader.filter(|i| shader_inputs.insert(*i)) {
textures.push(craft_materialx::Texture {
input: input.into(),
file: file.clone(),
colorspace: if srgb { "srgb_texture" } else { "lin_rec709" }.into(),
});
} else {
unmapped.push(identifier);
}
}
files.push(Exported { file, bytes });
}
if files.is_empty() {
return Err(bad("file.export", "no connected image outputs were selected"));
}
if materialx {
if textures.is_empty() {
return Err(bad("file.export", format!("no selected output maps to OpenPBR: {}", unmapped.join(", "))));
}
let doc = craft_materialx::write_open_pbr(&graph.name, &textures, None);
files.push(Exported { file: "material.mtlx".into(), bytes: doc.into_bytes() });
}
Ok((files, json!({"maps":maps,"unmapped":unmapped,"normalOrientation":"Y+","displacementScale":scale})))
}
+25 -224
View File
@@ -1,23 +1,10 @@
#[cfg(any(test, not(target_arch = "wasm32")))]
use std::collections::BTreeMap;
use serde_json::{Value as J, json};
#[cfg(test)]
use craft_texture::Image;
use materialcraft_core::ComponentUsage;
use materialcraft_core::Graph;
#[cfg(any(test, not(target_arch = "wasm32")))]
use materialcraft_core::Value;
use materialcraft_formats::material::{FORMAT, ImageInput, Manifest, MapEntry, Parameter, PresetEntry, VERSION, portable, srgb_usage};
use materialcraft_formats::{Depth, encode};
#[cfg(not(target_arch = "wasm32"))]
use crate::commands::u64p;
use crate::commands::{CommandSpec, bad, has_graph, strp};
use crate::{Result, Session, cmd};
use materialcraft_core::Graph;
use materialcraft_formats::material::ImageInput;
#[cfg(not(target_arch = "wasm32"))]
use materialcraft_formats::material::MATERIAL_EXT;
use serde_json::{Value as J, json};
pub(crate) fn specs() -> Vec<CommandSpec> {
vec![
@@ -26,7 +13,7 @@ pub(crate) fn specs() -> Vec<CommandSpec> {
"Export material...",
["File"],
None,
"{path, graph?, size? (px, power of two), values? {input: value}, inputs? {identifier: image path}} cooks the graph and writes a portable .mcmat material (maps, exposed inputs, source package) for TextureCraft",
"{path, graph? (index or SBS path)} publishes one root graph with its source dependencies in a v3 .mcmat",
has_graph,
export_material
),
@@ -52,69 +39,24 @@ pub(crate) fn specs() -> Vec<CommandSpec> {
]
}
pub use crate::MaterialOptions;
/// Publish a self-contained source snapshot after checking the selected outputs can cook.
pub fn material_bytes(session: &Session, graph: usize) -> Result<Vec<u8>> {
crate::material_bake::check_publish(session, graph)?;
Ok(materialcraft_formats::material::write_material(&session.pkg, graph)?)
}
/// Cook `graph_index` with `options` and pack it as a portable material (`.mcmat` bytes)
pub fn material_bytes(session: &Session, graph_index: usize, options: &MaterialOptions) -> Result<Vec<u8>> {
let mut package = session.pkg.clone();
let graph = package.graph_mut(graph_index)?;
if let Some(side_pixels) = options.size {
let log2 = 31 - side_pixels.clamp(16, 8192).leading_zeros();
graph.size = [log2, log2];
}
graph.inputs.extend(options.values.clone());
let graph = package.graph(graph_index)?;
let decode = session.resource_decoder();
let mut cook_options = materialcraft_eval::Options::new(&decode);
cook_options.environment = session.environment;
cook_options.inputs = options.inputs.clone();
let cooked = materialcraft_eval::cook(&package, graph_index, &cook_options, &session.cache);
let mut maps: Vec<(String, Vec<u8>)> = Vec::new();
let mut entries = Vec::new();
let mut size = [0, 0];
for (_, usage, _, image) in materialcraft_eval::outputs(graph, &cooked) {
let Some(image) = image else { continue };
if usage == "any" || entries.iter().any(|e: &MapEntry| e.usage == usage) {
continue;
}
/// Publish with an additional budget for the combined uncompressed source assets.
pub fn material_bytes_with_resource_limit(session: &Session, graph: usize, maximum_resource_bytes: u64) -> Result<Vec<u8>> {
crate::material_bake::check_publish(session, graph)?;
Ok(materialcraft_formats::material::write_material_with_resource_limit(&session.pkg, graph, maximum_resource_bytes)?)
}
let srgb = srgb_usage(usage);
let file = format!("maps/{usage}.png");
maps.push((file.clone(), encode(image, "png", if srgb { Depth::U8 } else { Depth::U16 }, srgb)?));
entries.push(MapEntry { usage: usage.into(), file, color_space: if srgb { "srgb" } else { "linear" }.into() });
size = [image.width(), image.height()];
}
if entries.is_empty() {
return Err(bad(
"file.exportMaterial",
format!(
"graph `{}` has no outputs with a material usage ({})",
graph.name,
ComponentUsage::ALL.iter().filter(|usage| **usage != ComponentUsage::ANY).map(|usage| usage.id).collect::<Vec<_>>().join(", ")
),
));
pub fn publish_graph(s: &Session, p: &J) -> Result<usize> {
if ["size", "values", "inputs", "preset"].iter().any(|key| p.get(key).is_some_and(|v| !v.is_null())) {
return Err(bad("file.exportMaterial", "pass size, values, preset and image inputs when cooking or exporting outputs"));
}
let manifest = Manifest {
format: FORMAT.into(),
version: VERSION,
name: graph.name.replace('_', " "),
graph: graph.name.clone(),
size,
maps: entries,
parameters: graph
.exposed
.iter()
.filter(|e| portable(&e.default))
.map(|e| Parameter { param: e.clone(), value: graph.input_value(&e.id).cloned().unwrap_or_else(|| e.default.clone()) })
.collect(),
inputs: image_inputs(graph),
presets: graph.presets.iter().map(|p| PresetEntry { name: p.name.clone(), values: p.values.clone() }).collect(),
source: String::new(),
};
let source = materialcraft_formats::sbs::write_sbs(&package)?;
Ok(materialcraft_formats::material::write_material(&manifest, &maps, &source)?)
crate::graph_cmds::graph_index(s, p)
}
/// The graph's input nodes (one entry per identifier)
@@ -123,51 +65,19 @@ pub fn image_inputs(g: &Graph) -> Vec<ImageInput> {
for n in g.nodes.iter().filter(|n| matches!(n.def.as_str(), "input_grayscale" | "input_color")) {
let identifier = materialcraft_eval::identifier(n);
if !out.iter().any(|i| i.identifier == identifier) {
out.push(ImageInput { identifier, kind: if n.def == "input_grayscale" { "gray" } else { "color" }.into() });
let usage = if n.params.contains_key("usage") { materialcraft_eval::output_usage(n) } else { "any" };
out.push(ImageInput { identifier, kind: if n.def == "input_grayscale" { "gray" } else { "color" }.into(), usage: usage.into() });
}
}
out
}
/// `values` as typed exposed input values of graph `g`
#[cfg(any(test, not(target_arch = "wasm32")))]
fn values_param(cmd: &str, g: &Graph, p: &J) -> Result<BTreeMap<String, Value>> {
let Some(obj) = p.get("values").and_then(J::as_object) else { return Ok(BTreeMap::new()) };
obj.iter()
.map(|(id, v)| {
let e = g.exposed.iter().find(|e| e.id == *id).ok_or_else(|| bad(cmd, format!("no input `{id}`")))?;
let val = e.default.coerce_json(v).ok_or_else(|| bad(cmd, format!("`{id}` takes a {}", e.default.type_name())))?;
Ok((id.clone(), val))
})
.collect()
}
#[cfg(not(target_arch = "wasm32"))]
fn graph_param(s: &Session, p: &J) -> Result<usize> {
let gi = u64p(p, "graph").map_or(s.graph, |g| g as usize);
s.pkg.graph(gi)?;
Ok(gi)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn material_request(s: &Session, cmd: &str, p: &J) -> Result<(usize, MaterialOptions)> {
let gi = graph_param(s, p)?;
let o = MaterialOptions { size: u64p(p, "size").map(|v| v.min(8192) as u32), values: values_param(cmd, s.pkg.graph(gi)?, p)?, ..Default::default() };
Ok((gi, o))
}
#[cfg(not(target_arch = "wasm32"))]
fn export_material(s: &mut Session, p: &J) -> Result<J> {
const CMD: &str = "file.exportMaterial";
let path = strp(p, "path").ok_or_else(|| bad(CMD, "`path` is required"))?;
let path = if path.to_ascii_lowercase().ends_with(&format!(".{MATERIAL_EXT}")) { path.to_string() } else { format!("{path}.{MATERIAL_EXT}") };
let (gi, mut o) = material_request(s, CMD, p)?;
for (id, file) in p.get("inputs").and_then(J::as_object).into_iter().flatten() {
let file = file.as_str().ok_or_else(|| bad(CMD, format!("inputs.{id} must be an image path")))?;
let bytes = std::fs::read(file).map_err(|e| bad(CMD, format!("{file}: {e}")))?;
o.inputs.insert(id.clone(), materialcraft_formats::decode(&bytes, !srgb_usage(id))?);
}
let bytes = material_bytes(s, gi, &o)?;
let bytes = material_bytes(s, publish_graph(s, p)?)?;
materialcraft_formats::write_atomic(std::path::Path::new(&path), &bytes)?;
s.log(format!("Exported material {path}"));
Ok(json!({"path": path, "bytes": bytes.len()}))
@@ -188,8 +98,8 @@ pub fn texturecraft_library() -> std::path::PathBuf {
#[cfg(not(target_arch = "wasm32"))]
fn send_to_texturecraft(s: &mut Session, p: &J) -> Result<J> {
const CMD: &str = "file.sendToTextureCraft";
let gi = graph_param(s, p)?;
let bytes = material_bytes(s, gi, &MaterialOptions::default())?;
let gi = publish_graph(s, p)?;
let bytes = material_bytes(s, gi)?;
let dir = texturecraft_library();
std::fs::create_dir_all(&dir).map_err(|e| bad(CMD, format!("{}: {e}", dir.display())))?;
let path = dir.join(format!("{}.{MATERIAL_EXT}", materialcraft_formats::safe_file_name(&s.pkg.graph(gi)?.name)));
@@ -260,114 +170,5 @@ fn unbind(s: &mut Session, p: &J) -> Result<J> {
}
#[cfg(test)]
mod tests {
use super::*;
use materialcraft_formats::material::read_material;
#[test]
fn exports_maps_inputs_and_values() {
let mut s = crate::sample_session();
crate::execute(&mut s, "graph.exposed.add", &json!({"id": "wear", "default": 0.5})).unwrap();
crate::execute(&mut s, "graph.exposed.add", &json!({"id": "label", "default": {"t": "text", "v": "x"}})).unwrap();
let values = values_param("t", s.graph().unwrap(), &json!({"values": {"wear": 0.9}})).unwrap();
let bytes = material_bytes(&s, s.graph, &MaterialOptions { size: Some(32), values, ..Default::default() }).unwrap();
let (m, source) = read_material(&bytes).unwrap();
assert_eq!(m.size, [32, 32]);
assert!(m.maps.iter().any(|entry| entry.usage == "height" && entry.color_space == "linear"));
let ids: Vec<&str> = m.parameters.iter().map(|p| p.param.id.as_str()).collect();
assert_eq!(ids, ["wear"], "text inputs are not portable");
assert_eq!(m.parameters[0].value, Value::Float(0.9), "the manifest carries the cooked value");
assert_eq!(
materialcraft_formats::sbs::read_sbs(&source, "source.sbs", &|_| None).unwrap().pkg.graphs[s.graph].input_value("wear"),
Some(&Value::Float(0.9))
);
// The source package opens again, on the material's graph, with no file to save over
crate::file_cmds::open_bytes(&mut s, &bytes, Some("x.mcmat".into())).unwrap();
assert_eq!(s.graph().unwrap().name, m.graph);
assert!(s.path.is_none());
}
#[test]
fn binds_preview_images_to_input_nodes() {
let mut s = crate::sample_session();
crate::execute(&mut s, "graph.node.add", &json!({"def": "input_grayscale", "params": {"identifier": "curvature"}})).unwrap();
let png = encode(&Image::filled(4, 4, craft_texture::Kind::Gray, [0.7; 4]), "png", Depth::U16, false).unwrap();
let res = crate::file_cmds::add_resource(&mut s, "curv.png", png, None).unwrap();
assert!(crate::execute(&mut s, "graph.input.bind", &json!({"identifier": "nope", "resource": res})).is_err());
crate::execute(&mut s, "graph.input.bind", &json!({"identifier": "curvature", "resource": res, "linear": true})).unwrap();
assert_eq!(s.graph().unwrap().input_images.get("curvature"), Some(&res));
let bytes = materialcraft_formats::sbs::write_sbs(&s.pkg).unwrap();
let reopened = materialcraft_formats::sbs::read_sbs(&bytes, "input.sbs", &|_| None).unwrap().pkg;
assert_eq!(reopened.graphs[s.graph].input_images.get("curvature"), Some(&res));
assert!(reopened.graphs[s.graph].input_images_linear.contains("curvature"));
let bytes = material_bytes(&s, s.graph, &MaterialOptions { size: Some(16), ..Default::default() }).unwrap();
let (m, _) = read_material(&bytes).unwrap();
assert!(m.inputs.iter().any(|i| i.identifier == "curvature" && i.kind == "gray"));
assert_eq!(crate::execute(&mut s, "graph.input.unbind", &json!({"identifier": "curvature"})).unwrap(), J::Bool(true));
assert!(s.graph().unwrap().input_images.is_empty());
assert!(s.graph().unwrap().input_images_linear.is_empty());
}
#[test]
fn rejects_unknown_values() {
let s = crate::sample_session();
let g = s.graph().unwrap();
assert!(values_param("t", g, &json!({"values": {"no_such_input": 1}})).is_err());
assert!(values_param("t", g, &json!({})).unwrap().is_empty());
}
#[test]
fn export_material_decodes_data_inputs_as_linear() {
#[cfg(not(target_arch = "wasm32"))]
{
use materialcraft_formats::decode;
let mut s = crate::sample_session();
let input =
crate::execute(&mut s, "graph.node.add", &json!({"def": "input_color", "params": {"identifier": "normal"}})).unwrap()["id"].as_u64().unwrap();
let output =
crate::execute(&mut s, "graph.node.add", &json!({"def": "output", "params": {"usage": ComponentUsage::index("normal")}})).unwrap()["id"]
.as_u64()
.unwrap();
crate::execute(&mut s, "graph.connect", &json!({"from": input, "to": output})).unwrap();
let png = encode(&Image::filled(16, 16, craft_texture::Kind::Color, [128.0 / 255.0; 4]), "png", Depth::U16, false).unwrap();
let dir = std::env::temp_dir().join(format!("materialcraft-linear-input-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("normal.png");
std::fs::write(&file, &png).unwrap();
let input_path = file.to_str().unwrap().to_string();
let output_path = dir.join("out.mcmat").to_str().unwrap().to_string();
let result = crate::execute(&mut s, "file.exportMaterial", &json!({"path": output_path, "size": 16, "inputs": {"normal": input_path}}));
assert!(result.is_ok());
let bytes = std::fs::read(&output_path).unwrap();
let _ = std::fs::remove_dir_all(&dir);
// The uncompressed archive exposes the normal map after its local file header
let name = b"maps/normal.png";
let pos = bytes.windows(name.len()).position(|w| w == name).unwrap();
let header = pos - 30;
assert_eq!(&bytes[header..header + 4], b"PK\x03\x04");
let name_len = u16::from_le_bytes([bytes[header + 26], bytes[header + 27]]) as usize;
let extra_len = u16::from_le_bytes([bytes[header + 28], bytes[header + 29]]) as usize;
let compressed = u32::from_le_bytes([bytes[header + 18], bytes[header + 19], bytes[header + 20], bytes[header + 21]]) as usize;
let start = header + 30 + name_len + extra_len;
let map = &bytes[start..start + compressed];
let decoded = decode(map, true).unwrap();
assert!((decoded.get(0, 0, false)[0] - 128.0 / 255.0).abs() < 1e-3, "data inputs must be decoded as linear");
}
}
}
#[path = "material_cmds.test.rs"]
mod tests;
+503
View File
@@ -0,0 +1,503 @@
use super::*;
use crate::{MaterialOptions, execute, file_cmds, material_bake};
use craft_texture::{Image, Kind};
use materialcraft_core::{ComponentUsage, GraphInstance, GraphPort, Package, Preset, Resource, ResourceBytes, Value};
use materialcraft_formats::{Depth, encode};
fn node(graph: &mut Graph, def: &str, params: &[(&str, Value)]) -> materialcraft_core::NodeId {
let id = graph.add_node(def, [0.0; 2]);
graph.node_mut(id).unwrap().params.extend(params.iter().map(|(k, v)| (k.to_string(), v.clone())));
id
}
fn output(graph: &mut Graph, source: materialcraft_core::NodeId, port: &str, id: &str, usage: &str) {
let out = node(graph, "output", &[("identifier", Value::Text(id.into())), ("usage", Value::Enum(ComponentUsage::index(usage)))]);
graph.connect(source, port, out, "input").unwrap();
}
fn fixture() -> Session {
let mut project = Package::new("Studio");
project.graphs.push(Graph::new("Unused"));
let mut graph = Graph::new("Oak");
graph.folder = "Materials".into();
graph.size = [5, 4];
for (owner, usage, gray) in [("a.sbs", "height", 0.22), ("b.sbs", "metallic", 0.75)] {
let mut helper = Graph::new("Grain");
let image = node(&mut helper, "bitmap", &[("resource", Value::Text("grain".into()))]);
output(&mut helper, image, "output", "grain", "height");
let mut pkg = Package::new("Helpers");
pkg.graphs.push(helper);
let data = encode(&Image::filled(16, 16, Kind::Gray, [gray; 4]), "png", Depth::U16, false).unwrap();
pkg.resources.push(Resource::new("grain", "image.png", ResourceBytes::new(data)));
project.dependencies.insert(owner.into(), std::sync::Arc::new(pkg));
let inst = node(&mut graph, "graph_instance", &[]);
graph.node_mut(inst).unwrap().instance = Some(GraphInstance {
package: owner.into(),
graph: "Grain".into(),
inputs: vec![],
outputs: vec![GraphPort { identifier: "grain".into(), type_code: 1 }],
defaults: Default::default(),
label: String::new(),
});
output(&mut graph, inst, "grain", usage, usage);
}
let source = node(&mut graph, "uniform", &[]);
output(&mut graph, source, "output", "albedo", "baseColor");
let mesh = node(&mut graph, "input_grayscale", &[("identifier", Value::Text("curvature".into()))]);
output(&mut graph, mesh, "output", "mesh", "roughness");
project.graphs.push(graph);
let mut session = Session::new(project);
session.graph = 1;
execute(&mut session, "graph.exposed.add", &json!({"id":"tint","bind":{"node":source,"param":"color"}})).unwrap();
execute(&mut session, "graph.exposed.set", &json!({"id":"tint","value":[0.4,0.2,0.1,1.0]})).unwrap();
session.graph_mut().unwrap().presets.push(Preset { name: "Dark".into(), values: [("tint".into(), Value::Color([0.15, 0.1, 0.05, 1.0]))].into() });
session
}
#[test]
fn published_entry_cooks_independently_with_values_sizes_and_image_inputs() {
let source = fixture();
let bytes = material_bytes(&source, source.graph).unwrap();
let (pkg, entry) = materialcraft_formats::material::read_material(&bytes).unwrap();
assert_eq!(pkg.graphs.len(), 3);
assert_eq!(pkg.resources.len(), 2);
assert!(pkg.dependencies.is_empty());
assert_eq!(pkg.graph(entry).unwrap().sbs_path(), "Oak");
assert!(pkg.graphs.iter().all(|g| g.name != "Unused"));
let mut opened = Session::new(Package::new("other"));
file_cmds::open_bytes(&mut opened, &bytes, Some("Oak.mcmat".into())).unwrap();
let info = material_bake::info(&opened, &J::Null).unwrap();
assert_eq!(info["graph"], "Oak");
assert_eq!(info["parameters"][0]["default"], json!(source.graph().unwrap().exposed[0].default));
assert_eq!(info["parameters"][0]["value"], json!(source.graph().unwrap().input_value("tint")));
assert_eq!(info["presets"][0]["name"], "Dark");
let mut variations = Vec::new();
for size in [32, 64] {
for wear in [0.1, 0.7] {
for gray in [0.2, 0.8] {
let mesh = encode(&Image::filled(16, 16, Kind::Gray, [gray; 4]), "png", Depth::U16, false).unwrap();
let p = json!({"profile":"maps","size":size,"preset":"Dark","values":{"tint":[wear,0.1,0.05,1.0]},"inputs":{"curvature":"mesh.png"}});
let bake = |s: &Session| {
material_bake::export(s, &p, |path| {
assert_eq!(path, "mesh.png");
Ok(mesh.clone())
})
.unwrap()
};
let (original, _) = bake(&source);
let (packed, report) = bake(&opened);
assert_eq!(original.len(), 4);
assert_eq!(packed.len(), original.len());
assert_eq!(report["maps"][0]["size"], json!([size, size / 2]));
for (a, b) in original.iter().zip(&packed) {
assert_eq!(a.file, b.file);
assert_eq!(a.bytes, b.bytes, "{}", a.file);
}
let sample = |name: &str, linear: bool| {
let map = packed.iter().find(|f| f.file == name).unwrap();
materialcraft_formats::decode(&map.bytes, linear).unwrap().get(0, 0, false)[0]
};
assert!((f64::from(sample("albedo.png", false)) - wear).abs() < 0.01);
assert!((sample("mesh.png", true) - gray).abs() < 0.01);
variations.push(report["material"]["parameters"][0]["value"].clone());
}
}
}
assert_ne!(variations[0], variations[2]);
assert_eq!(info, material_bake::info(&opened, &J::Null).unwrap(), "cook overrides leaked");
assert!(
material_bake::cook(&opened, &json!({"values":{"missing":0}}), |path| Err(crate::EngineError::Other(format!("unexpected file read: {path}")))).is_err()
);
let error = material_bake::cook(&opened, &json!({"inputs":{"missing":"x"}}), |path| panic!("unexpected file read: {path}")).unwrap_err();
assert!(error.to_string().contains("unknown image input `missing`"));
}
#[test]
fn rejects_missing_ambiguous_and_uncookable_materials() {
let mut session = fixture();
assert!(material_bytes(&session, 0).is_err());
session.pkg.dependencies.remove("b.sbs");
assert!(material_bytes(&session, 1).is_err());
let empty = Package::new("Empty");
assert!(materialcraft_formats::material::entry_graph(&empty).is_err());
let mut ambiguous = empty;
ambiguous.graphs.extend([Graph::new("A"), Graph::new("B")]);
assert!(materialcraft_formats::material::entry_graph(&ambiguous).is_err());
}
#[test]
fn preset_values_apply_before_explicit_overrides() {
let s = fixture();
let options = MaterialOptions::from_params(s.graph().unwrap(), &json!({"preset":"Dark"}), |path| {
Err(crate::EngineError::Other(format!("unexpected file read: {path}")))
})
.unwrap();
assert_eq!(options.values["tint"], Value::Color([0.15, 0.1, 0.05, 1.0]));
}
#[test]
fn publishing_sbs_and_project_archives_survives_removing_the_entire_source_tree() {
let directory = tempfile::tempdir().unwrap();
let dir = directory.path();
let authored = dir.join("authored");
std::fs::create_dir_all(&authored).unwrap();
let source = fixture();
for filename in ["a.sbs", "b.sbs"] {
let dependency = &source.pkg.dependencies[filename];
let files = materialcraft_formats::project::ProjectFiles::build(dependency, filename, false).unwrap();
materialcraft_formats::project::write_files(&authored.join(filename), &files).unwrap();
}
for ext in ["sbs", "mcraft"] {
let path = authored.join(format!("studio.{ext}"));
// Save the independent project beside its library packages and their original bitmaps
let files = materialcraft_formats::project::ProjectFiles::build(&source.pkg, &format!("studio.{ext}"), ext == "mcraft").unwrap();
if ext == "mcraft" {
std::fs::write(&path, files.zip().unwrap()).unwrap();
} else {
materialcraft_formats::project::write_files(&path, &files).unwrap();
}
let mut editor = Session::new(Package::new("Empty"));
execute(&mut editor, "file.open", &json!({"path":path})).unwrap();
execute(&mut editor, "file.exportMaterial", &json!({"graph":"Materials/Oak","path":dir.join(format!("{ext}.mcmat"))})).unwrap();
}
std::fs::remove_dir_all(&authored).unwrap();
for ext in ["sbs", "mcraft"] {
let mut viewer = Session::new(Package::new("Empty"));
execute(&mut viewer, "file.open", &json!({"path":dir.join(format!("{ext}.mcmat"))})).unwrap();
let settings = json!({"profile":"maps","preset":"Dark","size":32});
let bake = |s: &Session| {
material_bake::export(s, &settings, |path| Err(crate::EngineError::Other(format!("unexpected file read: {path}"))))
.unwrap()
.0
.into_iter()
.map(|f| (f.file, f.bytes))
.collect::<std::collections::BTreeMap<_, _>>()
};
assert_eq!(bake(&source), bake(&viewer));
}
}
#[test]
fn snapshots_transitive_function_calls_and_rejects_recursion_or_missing_assets() {
use materialcraft_core::FunctionGraph;
let mut s = fixture();
let call = |owner: &str, name: &str| {
let mut f = FunctionGraph::default();
f.output = Some(f.add("instance", &[], None));
f.nodes[0].type_code = 256;
f.nodes[0].instance = Some(GraphInstance {
package: owner.into(),
graph: name.into(),
inputs: vec![],
outputs: vec![],
defaults: Default::default(),
label: String::new(),
});
f
};
let mut utils = Package::new("Functions");
let mut constant = Graph::new("Amount");
constant.function_type = 256;
constant.function = Some(FunctionGraph::constant(Value::Float(0.4)));
utils.graphs.push(constant);
let mut wrapper = Graph::new("Wrapper");
wrapper.function_type = 256;
wrapper.function = Some(call("", "Amount"));
utils.graphs.push(wrapper);
s.pkg.dependencies.insert("math.sbs".into(), std::sync::Arc::new(utils));
let graph = s.graph_mut().unwrap();
let fx = graph.nodes.iter_mut().find(|n| n.def == "uniform").unwrap();
fx.functions.insert("color_mode".into(), call("math.sbs", "Wrapper"));
let bytes = materialcraft_formats::material::write_material(&s.pkg, s.graph).unwrap();
let (p, root) = materialcraft_formats::material::read_material(&bytes).unwrap();
assert_eq!(p.graphs.iter().filter(|g| g.function.is_some()).count(), 2);
assert_eq!(p.graph(root).unwrap().name, "Oak");
for reference in p.graphs.iter().flat_map(|g| g.instances()) {
assert!(reference.package.is_empty() && p.graph_by_name(&reference.graph).is_some());
}
let lib = std::sync::Arc::make_mut(s.pkg.dependencies.get_mut("math.sbs").unwrap());
lib.graphs[0].function = Some(call("", "Wrapper"));
assert!(materialcraft_formats::material::write_material(&s.pkg, s.graph).unwrap_err().to_string().contains("cyclic"));
std::sync::Arc::make_mut(s.pkg.dependencies.get_mut("a.sbs").unwrap()).resources.clear();
assert!(materialcraft_formats::material::write_material(&s.pkg, s.graph).unwrap_err().to_string().contains("missing resource"));
}
fn export_fixture(source: &str, params: &[(&str, Value)], usage: &str) -> Session {
let mut graph = Graph::new("Surface");
graph.size = [5, 5];
let generator = node(&mut graph, source, params);
output(&mut graph, generator, "output", "result", usage);
let mut package = Package::new("Studio");
package.graphs.push(graph);
Session::new(package)
}
#[test]
fn grayscale_base_color_uses_the_transfer_declared_to_materialx() {
let session = export_fixture("uniform", &[("color", Value::Color([0.5; 4])), ("color_mode", Value::Bool(false))], "baseColor");
let (files, report) = material_bake::export(&session, &json!({"profile":"materialx"}), material_bake::read_input).unwrap();
let map = &report["maps"][0];
let image = image::load_from_memory(&files.iter().find(|file| file.file == map["file"]).unwrap().bytes).unwrap();
assert_eq!(map["colorSpace"], "srgb");
assert_eq!(image.into_rgb8().get_pixel(0, 0).0, [188; 3]);
}
#[test]
fn an_overridden_color_input_keeps_the_published_transfer() {
let session =
export_fixture("input_color", &[("identifier", Value::Text("swatch".into())), ("usage", Value::Enum(ComponentUsage::index("baseColor")))], "baseColor");
let png = encode(&Image::filled(32, 32, Kind::Color, [0.21586; 4]), "png", Depth::U8, true).unwrap();
let cooked = material_bake::cook(&session, &json!({"inputs":{"swatch":"color.png"}}), |_| Ok(png.clone())).unwrap();
assert!((cooked["outputs"][0]["mean"][0].as_f64().unwrap() - 0.21586).abs() < 0.001);
}
#[test]
fn normal_node_conventions_produce_the_same_published_y_up_map() {
let mut session = export_fixture("normal", &[("intensity", Value::Float(8.0))], "normal");
let graph = session.graph_mut().unwrap();
let normal = graph.nodes[0].id;
let input = node(graph, "input_grayscale", &[("identifier", Value::Text("slope".into()))]);
graph.connect(input, "output", normal, "input").unwrap();
let slope = Image::generate(32, 32, Kind::Gray, |_, y| [if y < 16 { 0.0 } else { 1.0 }; 4]);
let png = encode(&slope, "png", Depth::U8, false).unwrap();
let mut greens = Vec::new();
for open_gl in [None, Some(false), Some(true)] {
if let Some(open_gl) = open_gl {
session.graph_mut().unwrap().node_mut(normal).unwrap().params.insert("opengl".into(), Value::Bool(open_gl));
}
let (files, report) = material_bake::export(&session, &json!({"profile":"materialx","inputs":{"slope":"input.png"}}), |_| Ok(png.clone())).unwrap();
let bytes = &files.iter().find(|file| file.file == report["maps"][0]["file"]).unwrap().bytes;
let pixel = materialcraft_formats::decode(bytes, true).unwrap().get(16, 16, false);
assert_eq!(report["normalOrientation"], "Y+");
greens.push(pixel[1]);
}
assert!(greens[2] > 0.8);
assert!(greens.iter().all(|green| (green - greens[2]).abs() < 0.0001), "{greens:?}");
}
#[test]
fn raw_normal_inputs_accept_an_explicit_source_convention() {
let session = export_fixture("input_color", &[("identifier", Value::Text("normal".into()))], "normal");
let png = encode(&Image::filled(32, 32, Kind::Color, [0.5, 0.25, 1.0, 1.0]), "png", Depth::U16, false).unwrap();
for (format, expected) in [(J::Null, 0.25), (json!("opengl"), 0.25), (json!("directx"), 0.75)] {
let params = json!({"profile":"maps","inputs":{"normal":"normal.png"},"normalFormat":format});
let (files, _) = material_bake::export(&session, &params, |_| Ok(png.clone())).unwrap();
let green = materialcraft_formats::decode(&files[0].bytes, true).unwrap().get(0, 0, false)[1];
assert!((green - expected).abs() < 0.0001);
}
}
#[test]
fn selecting_a_healthy_output_excludes_other_cook_failures() {
let mut session = export_fixture("uniform", &[], "baseColor");
let graph = session.graph_mut().unwrap();
let missing = node(graph, "bitmap", &[("resource", Value::Text("missing".into()))]);
output(graph, missing, "output", "broken", "roughness");
for profile in [J::Null, json!("maps"), json!("materialx")] {
let (files, _) = material_bake::export(&session, &json!({"profile":profile,"outputs":["result"]}), material_bake::read_input).unwrap();
assert!(!files.is_empty());
assert!(material_bake::export(&session, &json!({"profile":profile,"outputs":["broken"]}), material_bake::read_input).is_err());
}
}
#[test]
fn requested_bake_dimensions_apply_to_fixed_size_source_nodes() {
let mut session = export_fixture("uniform", &[], "baseColor");
let id = session.graph().unwrap().nodes[0].id;
execute(&mut session, "graph.node.base", &json!({"id":id,"sizeMode":"absolute","size":[4,4]})).unwrap();
let settings = json!({"profile":"materialx","size":[128,64]});
let (files, report) = material_bake::export(&session, &settings, material_bake::read_input).unwrap();
let pixels = materialcraft_formats::decode(&files[0].bytes, false).unwrap();
assert_eq!([pixels.width(), pixels.height()], [128, 64]);
assert_eq!(report["maps"][0]["size"], json!([128, 64]));
assert_eq!(material_bake::cook(&session, &settings, material_bake::read_input).unwrap()["outputs"][0]["size"], json!([128, 64]));
}
#[test]
fn an_openpbr_scalar_rejects_an_unrepresentable_srgb_override() {
let session = export_fixture("uniform", &[], "roughness");
let settings = json!({"profile":"materialx","colorSpaces":{"result":"srgb"}});
let result = material_bake::export(&session, &settings, material_bake::read_input);
assert!(result.err().is_some_and(|error| error.to_string().contains("linear")));
}
#[test]
fn explicit_srgb_is_rejected_when_the_profile_requires_float_textures() {
let session = export_fixture("uniform", &[], "baseColor");
let automatic = json!({"profile":"materialx","depth":32});
let explicit_srgb = json!({"profile":"materialx","depth":32,"colorSpaces":{"result":"srgb"}});
let (_, report) = material_bake::export(&session, &automatic, material_bake::read_input).unwrap();
let error = material_bake::export(&session, &explicit_srgb, material_bake::read_input).err().expect("an explicit sRGB override cannot produce linear EXR");
assert_eq!(report["maps"][0]["colorSpace"], "linear");
assert!(report["maps"][0]["file"].as_str().unwrap().ends_with(".exr"));
assert!(error.to_string().contains("requires linear values"));
}
#[test]
fn profile_controls_fail_when_the_requested_export_cannot_apply_them() {
let session = export_fixture("uniform", &[], "baseColor");
for (params, option) in [
(json!({"profile":"maps","displacementScale":2}), "displacementScale"),
(json!({"profile":"maps","depth":8.4}), "depth"),
(json!({"profile":"maps","depth":"mistyped"}), "depth"),
] {
let error = material_bake::export(&session, &params, material_bake::read_input).err().expect("an unused option must be rejected");
assert!(error.to_string().contains(option), "{error}");
}
}
#[test]
fn legacy_grayscale_float_exports_keep_linear_defaults_and_explicit_overrides() {
let session = export_fixture("uniform", &[("color", Value::Color([0.25; 4])), ("color_mode", Value::Bool(false))], "baseColor");
for format in ["exr", "hdr"] {
for explicit in [None, Some("raw"), Some("linear"), Some("srgb")] {
let mut params = json!({"format": format});
if let Some(space) = explicit {
params["colorSpaces"] = json!({"result": space});
}
let (files, _) = material_bake::export(&session, &params, material_bake::read_input).unwrap();
let decoded = materialcraft_formats::decode(&files[0].bytes, false).unwrap();
let expected = if explicit == Some("srgb") { craft_texture::linear_to_srgb(0.25) } else { 0.25 };
assert!((decoded.get(0, 0, false)[0] - expected).abs() < 0.005, "{format} {explicit:?}");
}
}
}
#[test]
fn published_disabled_and_typed_instance_cooks_match_source() {
use materialcraft_core::ValueType;
let mut helper = Graph::new("ValueHelper");
helper.size = [4, 4];
let value = node(&mut helper, "input_value", &[("identifier", Value::Text("amount".into())), ("default", Value::Float(0.25))]);
output(&mut helper, value, "output", "number", "height");
let mut dependency = Package::new("Dependency");
dependency.graphs.push(helper);
let mut graph = Graph::new("Root");
graph.size = [4, 4];
let value = node(&mut graph, "input_value", &[("identifier", Value::Text("source".into())), ("default", Value::Float(0.75))]);
let instance = node(&mut graph, "graph_instance", &[]);
graph.node_mut(instance).unwrap().instance = Some(GraphInstance {
package: "helper.sbs".into(),
graph: "ValueHelper".into(),
inputs: vec![GraphPort { identifier: "amount".into(), type_code: ValueType::Float as u32 }],
outputs: vec![GraphPort { identifier: "number".into(), type_code: ValueType::Float as u32 }],
defaults: Default::default(),
label: String::new(),
});
graph.connect(value, "output", instance, "amount").unwrap();
output(&mut graph, instance, "number", "number", "height");
let color = node(&mut graph, "uniform", &[("color", Value::Color([0.2, 0.3, 0.4, 1.0]))]);
let levels = node(&mut graph, "levels", &[("out_low", Value::Float4([1.0; 4])), ("out_high", Value::Float4([0.0; 4]))]);
graph.node_mut(levels).unwrap().disabled = true;
graph.connect(color, "output", levels, "input").unwrap();
output(&mut graph, levels, "output", "image", "baseColor");
let mut package = Package::new("Package");
package.graphs.push(graph);
package.dependencies.insert("helper.sbs".into(), std::sync::Arc::new(dependency));
let bytes = materialcraft_formats::material::write_material(&package, 0).unwrap();
let (opened, entry) = materialcraft_formats::material::read_material(&bytes).unwrap();
let cook = |pkg: Package, entry| {
// Cook without Session::new because it can reconstruct a missing interface
let options = materialcraft_eval::Options::new(&|_| None);
let cache = std::sync::Mutex::new(materialcraft_eval::Cache::default());
let cooked = materialcraft_eval::cook(&pkg, entry, &options, &cache);
let graph = &pkg.graphs[entry];
let instance = graph.nodes.iter().find(|node| node.def == "graph_instance").unwrap().id;
let levels = graph.nodes.iter().find(|node| node.def == "levels").unwrap().id;
assert!(cooked.nodes[&instance].error.is_none(), "{:?}", cooked.nodes[&instance].error);
assert_eq!(cooked.nodes[&instance].outputs[0].as_value(), Some(&Value::Float(0.75)));
assert!(cooked.nodes[&levels].error.is_none());
cooked.image(levels, 0).unwrap().get(0, 0, false)
};
let original = cook(package, 0);
let restored = cook(opened, entry);
assert_eq!(restored, original);
assert!((restored[0] - 0.2).abs() < 0.01, "disabled levels must preserve its input: {restored:?}");
}
#[test]
fn material_options_ignore_stale_presets_but_validate_explicit_values() {
let mut session = fixture();
let graph = session.graph_mut().unwrap();
graph.presets[0].values.insert("removed".into(), Value::Float(0.5));
graph.presets[0].values.insert("tint".into(), Value::Text("old type".into()));
let before = graph.clone();
let read = |_: &str| -> crate::Result<Vec<u8>> { Err(crate::EngineError::Other("unexpected image read".into())) };
let options = MaterialOptions::from_params(graph, &json!({"preset":"Dark"}), read).unwrap();
assert!(options.values.is_empty());
let options = MaterialOptions::from_params(graph, &json!({"preset":"Dark", "values":{"tint":[0.7,0.6,0.5,1.0]}}), read).unwrap();
assert_eq!(options.values.len(), 1);
assert_eq!(options.values["tint"], Value::Color([0.7, 0.6, 0.5, 1.0]));
assert!(MaterialOptions::from_params(graph, &json!({"values":{"removed":0.5}}), read).is_err());
assert!(MaterialOptions::from_params(graph, &json!({"values":{"tint":"wrong"}}), read).is_err());
assert_eq!(*graph, before);
}
#[test]
fn material_options_size_errors_name_the_material_command() {
let session = fixture();
let parameters = json!({"size":"bad"});
let error =
MaterialOptions::from_params(session.graph().unwrap(), &parameters, |_| Err(crate::EngineError::Other("unexpected image read".into()))).unwrap_err();
assert!(matches!(error, crate::EngineError::BadParams { cmd, msg } if cmd == "material.cook" && msg.contains("size")));
let error = crate::export_request::export_size("file.export", parameters.get("size")).unwrap_err();
assert!(matches!(error, crate::EngineError::BadParams { cmd, msg } if cmd == "file.export" && msg.contains("size")));
}
#[test]
fn named_profiles_reject_ignored_format_and_pattern() {
let session = fixture();
for extra in [json!({"format":"jpg"}), json!({"pattern":"oak_$(identifier)"})] {
let mut params = json!({"graph":"Materials/Oak","profile":"maps"});
params.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
let err = material_bake::export(&session, &params, |_| panic!("invalid options must precede input reads")).err().expect("invalid export must fail");
assert!(err.to_string().contains("omit format and pattern"));
}
let err =
material_bake::export(&session, &json!({"graph":"Materials/Oak","normalFormat":"directx"}), |_| panic!("invalid options must precede input reads"))
.err()
.expect("invalid export must fail");
assert!(err.to_string().contains("require an explicit export profile"));
}
#[test]
fn float_maps_reject_explicit_srgb_instead_of_ignoring_it() {
let session = fixture();
let params = json!({"graph":"Materials/Oak","profile":"maps","outputs":["albedo"],"depth":32,"colorSpaces":{"albedo":"srgb"}});
let err = material_bake::export(&session, &params, material_bake::read_input).err().expect("invalid export must fail");
assert!(err.to_string().contains("requires linear values"));
}
#[test]
fn named_profiles_report_the_validated_numeric_graph_selector() {
let session = fixture();
for profile in ["maps", "materialx"] {
for selector in [json!(1.0), json!(0.6)] {
let (files, report) =
material_bake::export(&session, &json!({"graph":selector,"profile":profile,"outputs":["albedo"],"size":16}), material_bake::read_input)
.unwrap();
assert!(!files.is_empty());
assert_eq!(report["material"]["graph"], "Materials/Oak");
}
}
}
+95 -7
View File
@@ -1,14 +1,102 @@
use craft_texture::Image;
use materialcraft_core::Value;
use std::collections::BTreeMap;
use materialcraft_core::{BaseParameter, Graph, SizeMode, Value};
use materialcraft_eval::{Cooked, Options};
use serde_json::Value as J;
use std::{borrow::Cow, collections::BTreeMap};
/// What to cook a portable material with, over the graph's own values
use crate::commands::bad;
use crate::{ExportSize, Result, Session};
/// Per-invocation inputs. The authoring graph and the source archive retain their own values.
#[derive(Clone, Debug, Default)]
pub struct MaterialOptions {
/// Square size in pixels (rounded to a power of two, 16..8192); `None` = the graph's size
pub size: Option<u32>,
/// Exposed input values (they also become the embedded package's values)
pub size: Option<ExportSize>,
pub values: BTreeMap<String, Value>,
/// Images for the graph's input nodes, by identifier (mesh maps)
pub inputs: BTreeMap<String, Image>,
}
impl MaterialOptions {
pub fn from_params(graph: &Graph, p: &J, read: impl Fn(&str) -> Result<Vec<u8>>) -> Result<Self> {
let invalid = |msg| bad("material.cook", msg);
let mut options = Self { size: crate::export_request::export_size("material.cook", p.get("size"))?, ..Default::default() };
if let Some(value) = p.get("preset").filter(|v| !v.is_null()) {
let name = value.as_str().ok_or_else(|| invalid("preset must be a name".into()))?;
let preset = graph.presets.iter().find(|preset| preset.name == name).ok_or_else(|| invalid(format!("unknown preset `{name}`")))?;
options.values.extend(
preset
.values
.iter()
.filter(|(id, value)| crate::graph_cmds::preset_value_matches(graph, id, value))
.map(|(id, value)| (id.clone(), value.clone())),
);
}
if let Some(value) = p.get("values").filter(|v| !v.is_null()) {
for (id, v) in value.as_object().ok_or_else(|| invalid("values must map parameter identifiers to values".into()))? {
let param = graph.exposed.iter().find(|e| e.id == *id).ok_or_else(|| invalid(format!("unknown exposed parameter `{id}`")))?;
let value = param.default.coerce_json(v).ok_or_else(|| invalid(format!("`{id}` takes a {}", param.default.type_name())))?;
options.values.insert(id.clone(), value);
}
}
if let Some(value) = p.get("inputs").filter(|v| !v.is_null()) {
let declared = crate::material_cmds::image_inputs(graph);
const MAX_DECODED_INPUT_BYTES: usize = 1 << 30;
let mut remaining = MAX_DECODED_INPUT_BYTES;
for (id, path) in value.as_object().ok_or_else(|| invalid("inputs must map image identifiers to file paths".into()))? {
let input = declared.iter().find(|input| input.identifier == *id).ok_or_else(|| invalid(format!("unknown image input `{id}`")))?;
let linear = if graph.input_images.contains_key(id) {
graph.input_images_linear.contains(id)
} else {
let usage = if input.usage == "any" { id } else { &input.usage };
!materialcraft_formats::material::srgb_usage(usage)
};
let path = path.as_str().ok_or_else(|| invalid(format!("inputs.{id} must be an image path")))?;
let image = materialcraft_formats::decode(&read(path)?, linear)?;
remaining = remaining.checked_sub(std::mem::size_of_val(image.data())).ok_or_else(|| invalid("decoded image inputs exceed 1 GiB".into()))?;
options.inputs.insert(id.clone(), image);
}
}
Ok(options)
}
pub(crate) fn cook(&self, session: &Session, index: usize, selected: Option<&[String]>, max_size: Option<u32>) -> Result<Cooked> {
let graph = session.pkg.graph(index)?;
let roots = graph
.nodes
.iter()
.filter(|node| node.def == "output")
.filter(|node| selected.is_none_or(|ids| ids.contains(&materialcraft_eval::identifier(node))))
.map(|node| node.id)
.collect();
let mut package = Cow::Borrowed(&session.pkg);
if let Some(size) = self.size {
let graph = package.to_mut().graph_mut(index)?;
graph.size = size.log2_size(graph.size);
// Fixed-size generators keep their native detail while final output bridges resample to the requested size
for output in graph.nodes.iter_mut().filter(|node| node.def == "output") {
output.base.size_mode = SizeMode::RelativeToParent;
output.base.size = [0, 0];
output.base.functions.remove(&BaseParameter::OutputSize);
}
}
let decoder = session.resource_decoder();
let mut options = Options::new(&decoder);
options.environment = session.environment;
options.max_size = max_size;
options.only = Some(roots);
options.inputs.clone_from(&self.inputs);
options.overrides.clone_from(&self.values);
let cooked = materialcraft_eval::cook(&package, index, &options, &session.cache);
if let Some((id, error)) = cooked.nodes.iter().find_map(|(id, r)| Some((id, r.error.as_ref()?))) {
return Err(bad("material.cook", format!("graph `{}`, node {}: {error}", graph.sbs_path(), id.0)));
}
Ok(cooked)
}
}
+22 -19
View File
@@ -1,7 +1,7 @@
use crate::{Cache, Cooked, Options, now_ms};
use crate::{node_execution::cook_node, prepared_graph::PreparedGraph};
use craft_texture::Image;
use materialcraft_core::{Package, Value};
use materialcraft_core::{Graph, Node, Package, Value};
use std::{
collections::BTreeMap,
sync::{Mutex, atomic::Ordering},
@@ -12,23 +12,7 @@ pub fn cook(package: &Package, graph_index: usize, options: &Options, cache: &Mu
let mut cooked = Cooked::default();
let Some(graph) = package.graphs.get(graph_index) else { return cooked };
// Input nodes show their bound preview images; the caller's images win
let bound;
let options = if !graph.input_images.is_empty() {
let mut inputs: BTreeMap<String, Image> = graph
.input_images
.iter()
.filter_map(|(id, resource)| {
Some((id.clone(), (options.resource)(&craft_texture::resource_key(resource, graph.input_images_linear.contains(id)))?))
})
.collect();
inputs.extend(options.inputs.iter().map(|(name, value)| (name.clone(), value.clone())));
bound = Options { only: options.only.clone(), inputs, overrides: options.overrides.clone(), graph_stack: options.graph_stack.clone(), ..*options };
&bound
} else {
options
};
let mut previews = BTreeMap::new();
let prepared = PreparedGraph::new(package, graph, options);
// Exposed parameter values: the graph's own, then the caller's overrides
@@ -50,9 +34,28 @@ pub fn cook(package: &Package, graph_index: usize, options: &Options, cache: &Mu
let Some(node) = prepared.index.node(id) else { continue };
let result = cook_node(package, graph_index, &prepared, node, &cooked, &variables, options, cache);
let input = input_image(graph, node, options, &mut previews);
let result = cook_node(package, graph_index, &prepared, node, &cooked, &variables, options, input, cache);
cooked.nodes.insert(id, result);
}
cooked.ms = (now_ms() - started_ms) as f32;
cooked
}
fn input_image<'a>(graph: &Graph, node: &Node, options: &'a Options<'_>, previews: &'a mut BTreeMap<String, Option<Image>>) -> Option<&'a Image> {
if !node.def.starts_with("input_") {
return None;
}
let identifier = crate::identifier(node);
if let Some(image) = options.inputs.get(&identifier) {
return Some(image);
}
let resource = graph.input_images.get(&identifier)?;
let linear = graph.input_images_linear.contains(&identifier);
// Nodes sharing an input also share its first lookup, including a missing preview
previews.entry(identifier).or_insert_with(|| (options.resource)(&craft_texture::resource_key(resource, linear))).as_ref()
}
+1 -2
View File
@@ -32,6 +32,7 @@ pub(crate) fn cook_node(
cooked: &Cooked,
graph_variables: &BTreeMap<String, Value>,
options: &Options,
input_image: Option<&Image>,
cache: &Mutex<Cache>,
) -> NodeResult {
let (input_ports, output_ports) = prepared.ports(node.id);
@@ -129,8 +130,6 @@ pub(crate) fn cook_node(
let tile = base.tiling[0] && base.tiling[1];
// Input nodes read the caller's image for their identifier
let input_image = node.def.starts_with("input_").then(|| options.inputs.get(&identifier(node))).flatten();
let resource_hash = match parameters.get("resource") {
Some(Value::Text(name)) => package.resource(name).map_or(0, |r| r.bytes.content_hash()),
_ => 0,
+105
View File
@@ -47,6 +47,111 @@ fn cooks_and_caches() {
assert_eq!(outputs(&p.graphs[0], &c).len(), 1);
}
#[test]
fn material_overrides_skip_bound_preview_decodes_and_release_buffers() {
let mut graph = Graph::new("Bound inputs");
graph.size = [4; 2];
let identifiers = ["height", "curvature", "thickness"];
let mut outputs = Vec::new();
for identifier in identifiers {
let input = graph.add_node("input_grayscale", [0.0; 2]);
graph.node_mut(input).unwrap().params.insert("identifier".into(), Value::Text(identifier.into()));
let output = graph.add_node("output", [0.0; 2]);
graph.connect(input, "output", output, "input").unwrap();
graph.input_images.insert(identifier.into(), format!("preview_{identifier}"));
outputs.push(output);
}
let mut package = Package::new("Bound inputs");
package.graphs.push(graph);
let preview_buffers = Mutex::new(Vec::new());
let read_preview = |_: &str| {
let image = Image::filled(1024, 1024, Kind::Gray, [0.25; 4]);
preview_buffers.lock().unwrap().push(std::sync::Arc::downgrade(image.shared_data()));
Some(image)
};
let cache = Mutex::new(Cache::new(0));
for value in [0.125, 0.5, 0.875] {
let mut options = Options::new(&read_preview);
let mut supplied_buffers = Vec::new();
for identifier in &identifiers[..2] {
let image = Image::filled(16, 16, Kind::Gray, [value; 4]);
supplied_buffers.push(std::sync::Arc::downgrade(image.shared_data()));
options.inputs.insert((*identifier).into(), image);
}
let result = cook(&package, 0, &options, &cache);
for (output, expected) in outputs.iter().zip([value, value, 0.25]) {
let image = result.image(*output, 0).unwrap();
assert!((image.mean()[0] - expected).abs() < 0.005);
}
drop(result);
drop(options);
assert!(supplied_buffers.iter().all(|buffer| buffer.upgrade().is_none()));
}
let previews = preview_buffers.into_inner().unwrap();
assert!(previews.iter().all(|buffer| buffer.upgrade().is_none()));
eprintln!("Decoded preview planes: {} ({} bytes of grayscale payload)", previews.len(), previews.len() * 1024 * 1024 * size_of::<f32>());
assert_eq!(previews.len(), 3, "one fallback preview decode per cook");
}
#[test]
fn selected_outputs_decode_only_reachable_input_previews() {
let mut graph = Graph::new("Preview selection");
graph.size = [4; 2];
let mut outputs = Vec::new();
for identifier in ["height", "roughness", "normal", "height"] {
let input = graph.add_node("input_grayscale", [0.0; 2]);
graph.node_mut(input).unwrap().params.insert("identifier".into(), Value::Text(identifier.into()));
graph.input_images.insert(identifier.into(), identifier.into());
let output = graph.add_node("output", [0.0; 2]);
graph.connect(input, "output", output, "input").unwrap();
outputs.push(output);
}
let mut package = Package::new("Preview selection");
package.graphs.push(graph);
let mut counts = Vec::new();
for (selected, expected) in [(vec![outputs[0], outputs[3]], 0.25), (vec![outputs[1]], 0.5), (vec![outputs[2]], 0.75)] {
let buffers = Mutex::new(Vec::new());
let peak_planes = std::sync::atomic::AtomicUsize::new(0);
let read_preview = |name: &str| {
let value = match craft_texture::split_resource_key(name).0 {
"height" => 0.25,
"roughness" => 0.5,
"normal" => 0.75,
other => panic!("unexpected preview {other}"),
};
let image = Image::filled(1024, 1024, Kind::Gray, [value; 4]);
let mut buffers = buffers.lock().unwrap();
buffers.push(std::sync::Arc::downgrade(image.shared_data()));
peak_planes.fetch_max(buffers.iter().filter(|plane| plane.strong_count() > 0).count(), std::sync::atomic::Ordering::Relaxed);
Some(image)
};
let mut options = Options::new(&read_preview);
options.only = Some(selected.clone());
let cache = Mutex::new(Cache::new(0));
let result = cook(&package, 0, &options, &cache);
for output in &outputs {
if selected.contains(output) {
assert!((result.image(*output, 0).unwrap().mean()[0] - expected).abs() < 0.005);
} else {
assert!(result.image(*output, 0).is_none());
}
}
let buffers = buffers.into_inner().unwrap();
assert!(buffers.iter().all(|buffer| buffer.upgrade().is_none()));
counts.push((buffers.len(), peak_planes.into_inner()));
}
eprintln!("Decoded and peak live 1024-square grayscale previews per selection: {counts:?}");
assert_eq!(counts, [(1, 1); 3]);
}
#[test]
fn processor_tiling_variable_tracks_both_axes_and_invalidates_cached_values() {
let mut function = materialcraft_core::FunctionGraph::default();
+33 -4
View File
@@ -62,15 +62,19 @@ pub fn decode(bytes: &[u8], linear: bool) -> Result<Image> {
/// Encode `image` as `ext` (see [`EXPORT_FORMATS`]). `srgb`: color values are written through the sRGB curve (base color); data maps (normal, roughness…) are written linear
pub fn encode(image: &Image, ext: &str, depth: Depth, srgb: bool) -> Result<Vec<u8>> {
let ext = ext.trim_start_matches('.').to_ascii_lowercase();
let encode_channel = |v: f32| if srgb && image.kind() == Kind::Color { linear_to_srgb(v) } else { v };
let encode_channel = |v: f32| if srgb { linear_to_srgb(v) } else { v };
let (w, h) = (image.width(), image.height());
let color = image.kind() == Kind::Color;
// Store sRGB grayscale colors as RGB so readers apply the transfer
// Preserve single-channel storage for raw grayscale data
let source_color = image.kind() == Kind::Color;
let color = source_color || srgb;
let pixel = |i: usize| -> [f32; 4] {
if color {
if source_color {
let s = image.data().get(i * 4..i * 4 + 4).unwrap_or(&[0.0, 0.0, 0.0, 1.0]);
[encode_channel(s[0]), encode_channel(s[1]), encode_channel(s[2]), s[3]]
} else {
let v = image.data().get(i).copied().unwrap_or(0.0);
let v = encode_channel(image.data().get(i).copied().unwrap_or(0.0));
[v, v, v, 1.0]
}
};
@@ -128,3 +132,28 @@ pub fn encode(image: &Image, ext: &str, depth: Depth, srgb: bool) -> Result<Vec<
dynamic_image.write_to(&mut output, format).map_err(|e| FormatError::Image(e.to_string()))?;
Ok(output.into_inner())
}
#[cfg(test)]
mod transfer_tests {
use super::*;
#[test]
fn grayscale_png_round_trips_selected_transfer() {
let image = Image::filled(2, 2, Kind::Gray, [0.25; 4]);
for depth in [Depth::U8, Depth::U16] {
for srgb in [false, true] {
let bytes = encode(&image, "png", depth, srgb).unwrap();
let decoded = decode(&bytes, !srgb).unwrap();
assert_eq!(decoded.kind(), if srgb { Kind::Color } else { Kind::Gray });
assert!((decoded.data()[0] - 0.25).abs() < 0.003);
if !srgb {
// Existing grayscale data imports stay linear even at the default setting
assert!((decode(&bytes, false).unwrap().data()[0] - 0.25).abs() < 0.003);
}
let stored = decode(&bytes, true).unwrap();
let expected = if srgb { linear_to_srgb(0.25) } else { 0.25 };
assert!((stored.data()[0] - expected).abs() < 0.003);
}
}
}
}
-1
View File
@@ -23,7 +23,6 @@ pub(crate) use guard::guard;
pub use image::{decode, encode};
pub type Result<T> = std::result::Result<T, FormatError>;
pub(crate) const MAX_JSON: u64 = 64 << 20;
pub(crate) const MAX_RESOURCE: u64 = 256 << 20;
pub const EXPORT_FORMATS: &[(&str, &str)] =
&[("png", "PNG"), ("jpg", "JPEG"), ("tga", "TGA"), ("bmp", "BMP"), ("webp", "WEBP"), ("exr", "OpenEXR"), ("hdr", "Radiance HDR")];
+211 -141
View File
@@ -1,15 +1,80 @@
use super::Manifest;
use materialcraft_core::Value;
use crate::{FormatError, Result, project::ProjectFiles, sbs};
use materialcraft_core::Package;
use std::cell::Cell;
use crate::{FormatError, MAX_JSON, MAX_RESOURCE, Result};
/// The portable material extension
pub use craft_mcmat::VERSION;
pub const MATERIAL_EXT: &str = "mcmat";
/// `format` of every manifest
pub const FORMAT: &str = "craft-material";
/// Newest manifest version this build reads and writes
pub const VERSION: u32 = 2;
const SOURCE: &str = "source.sbs";
pub fn is_material(bytes: &[u8]) -> bool {
craft_mcmat::is_material(bytes, craft_mcmat::Limits::default().max_entries)
}
pub fn write_material(package: &Package, entry: usize) -> Result<Vec<u8>> {
write_material_with_resource_limit(package, entry, craft_mcmat::Limits::default().max_total_bytes)
}
/// Limit the aggregate source assets before generating the output archive.
pub fn write_material_with_resource_limit(package: &Package, entry: usize, maximum_resource_bytes: u64) -> Result<Vec<u8>> {
let snapshot = super::snapshot(package, entry)?;
let mut remaining = maximum_resource_bytes;
for resource in &snapshot.resources {
remaining = remaining.checked_sub(resource.bytes.len() as u64).ok_or_else(|| {
FormatError::BadPackage(format!("material source assets exceed the {maximum_resource_bytes}-byte limit; reduce source asset sizes"))
})?;
}
let files = ProjectFiles::build(&snapshot, craft_mcmat::SOURCE_ENTRY, false)?;
let resources = files.files.iter().filter(|(name, _)| **name != files.entry).map(|(name, bytes)| (name.as_str(), bytes.as_slice()));
craft_mcmat::write(files.xml()?, resources).map_err(|err| FormatError::BadPackage(err.to_string()))
}
pub fn read_material(bytes: &[u8]) -> Result<(Package, usize)> {
let limits = craft_mcmat::Limits::default();
let material = craft_mcmat::Material::open(bytes, limits).map_err(|err| FormatError::BadPackage(err.to_string()))?;
let read = read_source(&material, limits.max_total_bytes)?;
drop(material);
if let Some(warning) = read.warnings.first() {
return Err(FormatError::BadPackage(format!("source.sbs: {warning}")));
}
let pkg = read.pkg;
let entry = super::entry_graph(&pkg)?;
if pkg.graphs.iter().flat_map(|g| g.instances()).any(|instance| !instance.package.is_empty()) {
return Err(FormatError::BadPackage("source.sbs has an external graph reference; republish with its dependencies".into()));
}
// Reuse the publisher’s dependency checks for third-party archives
super::snapshot(&pkg, entry)?;
Ok((pkg, entry))
}
// Linked aliases and inline SBS assets share one decoded-byte budget
fn read_source(material: &craft_mcmat::Material, budget: u64) -> Result<sbs::SbsRead> {
let remaining = Cell::new(budget);
let over_budget = Cell::new(false);
let mut read = sbs::SbsDocument::parse(&material.source)?.read_with_resource_budget(
craft_mcmat::SOURCE_ENTRY,
&|name| {
let data = material.resources.get(name)?;
let Some(bytes_left) = remaining.get().checked_sub(data.len() as u64) else {
over_budget.set(true);
return None;
};
remaining.set(bytes_left);
Some(data.clone())
},
&remaining,
)?;
if over_budget.get() {
read.warnings.insert(0, "archive member exceeds the remaining material resource budget".into());
}
Ok(read)
}
/// Whether maps of `usage` hold colors (stored sRGB-encoded) rather than data (linear)
pub fn srgb_usage(usage: &str) -> bool {
@@ -29,145 +94,150 @@ pub fn srgb_usage(usage: &str) -> bool {
)
}
/// Whether other apps can set a value of this type from the manifest
pub fn portable(v: &Value) -> bool {
matches!(
v,
Value::Float(_)
| Value::Float2(_)
| Value::Float3(_)
| Value::Float4(_)
| Value::Int(_)
| Value::Int2(_)
| Value::Bool(_)
| Value::Color(_)
| Value::Enum(_)
)
}
/// Write a portable material: `manifest` (its `source` is set here), the encoded maps named by `manifest.maps[].file`, and the source package bytes
pub fn write_material(manifest: &Manifest, maps: &[(String, Vec<u8>)], source: &[u8]) -> Result<Vec<u8>> {
let manifest = Manifest { source: SOURCE.into(), ..manifest.clone() };
let json = serde_json::to_value(manifest).map_err(|e| FormatError::Io(e.to_string()))?;
let shared: craft_mcmat::Manifest = serde_json::from_value(json).map_err(|e| FormatError::Io(e.to_string()))?;
craft_mcmat::write(&shared, maps, source).map_err(|e| FormatError::Io(e.to_string()))
}
/// Read a portable material's manifest and source package bytes
pub fn read_material(bytes: &[u8]) -> Result<(Manifest, Vec<u8>)> {
let bad = |e: craft_mcmat::Error| FormatError::BadPackage(format!("portable material: {e}"));
let limits = craft_mcmat::Limits {
min_version: VERSION,
max_version: VERSION,
max_entries: 100_000,
max_maps: 100_000,
max_manifest_bytes: MAX_JSON,
max_entry_bytes: MAX_RESOURCE,
max_total_bytes: MAX_JSON + MAX_RESOURCE,
};
let mut material = craft_mcmat::Material::open(bytes, limits).map_err(bad)?;
// Consume nested values individually to avoid copying the whole untrusted JSON manifest.
let parameters = std::mem::take(&mut material.manifest.parameters)
.into_iter()
.map(serde_json::from_value)
.collect::<std::result::Result<_, _>>()
.map_err(|e| FormatError::BadPackage(format!("portable material: {e}")))?;
let inputs = std::mem::take(&mut material.manifest.inputs)
.into_iter()
.map(serde_json::from_value)
.collect::<std::result::Result<_, _>>()
.map_err(|e| FormatError::BadPackage(format!("portable material: {e}")))?;
let presets = std::mem::take(&mut material.manifest.presets)
.into_iter()
.map(serde_json::from_value)
.collect::<std::result::Result<_, _>>()
.map_err(|e| FormatError::BadPackage(format!("portable material: {e}")))?;
let maps = std::mem::take(&mut material.manifest.maps)
.into_iter()
.map(|map| {
let mut fields = map.extra;
fields.insert("usage".into(), map.usage.into());
fields.insert("file".into(), map.file.into());
serde_json::from_value(serde_json::Value::Object(fields.into_iter().collect()))
})
.collect::<std::result::Result<_, _>>()
.map_err(|e| FormatError::BadPackage(format!("portable material: {e}")))?;
let shared = &material.manifest;
let manifest = Manifest {
format: shared.format.clone(),
version: shared.version,
name: shared.name.clone(),
graph: shared.graph.clone(),
size: shared.size,
maps,
parameters,
inputs,
presets,
source: shared.source.clone(),
};
let source = material.read_source().map_err(bad)?;
Ok((manifest, source))
}
/// Whether `bytes` look like a portable material (a zip with `material.json`)
pub fn is_material(bytes: &[u8]) -> bool {
craft_mcmat::is_material(bytes, 100_000)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::material::{ImageInput, MapEntry, Parameter};
use materialcraft_core::ExposedParameter;
use base64::Engine as _;
use materialcraft_core::{Graph, Resource, ResourceBytes, Value};
use std::io::Write;
fn manifest() -> Manifest {
let param = ExposedParameter {
description: String::new(),
editor: None,
clamp: false,
step: None,
id: "rough".into(),
label: "Roughness".into(),
group: String::new(),
default: Value::Float(0.5),
min: 0.0,
max: 1.0,
options: Vec::new(),
};
Manifest {
format: FORMAT.into(),
version: VERSION,
name: "Bricks".into(),
graph: "Bricks".into(),
size: [64, 64],
maps: vec![MapEntry { usage: "baseColor".into(), file: "maps/baseColor.png".into(), color_space: "srgb".into() }],
parameters: vec![Parameter { param, value: Value::Float(0.25) }],
inputs: vec![ImageInput { identifier: "curvature".into(), kind: "gray".into() }],
presets: Vec::new(),
source: String::new(),
fn inline(bytes: &[u8]) -> String {
let mut encoder = flate2::write::ZlibEncoder::new((bytes.len() as u32).to_be_bytes().to_vec(), flate2::Compression::default());
encoder.write_all(bytes).unwrap();
let encoded = base64::engine::general_purpose::STANDARD.encode(encoder.finish().unwrap());
format!("<resource><identifier v=\"inline\"/><source><binembedded><strdata v=\"{encoded}\"/></binembedded></source></resource>")
}
fn linked(name: &str) -> String {
format!("<resource><identifier v=\"{name}\"/><filepath v=\"resources/member.png\"/></resource>")
}
fn make_material(content: &str) -> craft_mcmat::Material {
let source = format!("<package><content>{content}</content></package>");
let bytes = craft_mcmat::write(source.as_bytes(), [("resources/member.png", b"abc".as_slice())]).unwrap();
craft_mcmat::Material::open(&bytes, craft_mcmat::Limits::default()).unwrap()
}
#[test]
fn publication_checks_the_combined_bytes_of_distinct_source_assets() {
const ASSET_BYTES: usize = 4096;
let mut package = Package::new("Assets");
let mut graph = Graph::new("Root");
for (index, name) in ["a", "b"].into_iter().enumerate() {
package.resources.push(Resource::new(name, format!("{name}.bin"), ResourceBytes::new(vec![index as u8; ASSET_BYTES])));
let bitmap = graph.add_node("bitmap", [0.0; 2]);
graph.node_mut(bitmap).unwrap().params.insert("resource".into(), Value::Text(name.into()));
let output = graph.add_node("output", [0.0; 2]);
graph.connect(bitmap, "output", output, "input").unwrap();
}
package.graphs.push(graph);
let total = (2 * ASSET_BYTES) as u64;
let error = write_material_with_resource_limit(&package, 0, total - 1).unwrap_err();
let archive = write_material_with_resource_limit(&package, 0, total).unwrap();
let (reopened, _) = read_material(&archive).unwrap();
assert!(error.to_string().contains("material source assets exceed"));
assert!(archive.len() < ASSET_BYTES);
assert_eq!(reopened.resources.iter().map(|resource| resource.bytes.len()).sum::<usize>(), total as usize);
}
#[test]
fn inline_budget_failure_does_not_substitute_a_different_linked_asset() {
let content = inline(b"four").replace("</resource>", "<filepath v=\"resources/member.png\"/></resource>");
let material = make_material(&content);
let read = read_source(&material, 3).unwrap();
assert!(read.warnings.iter().any(|warning| warning.contains("resource budget")));
assert!(read.pkg.resources.is_empty());
let read = read_source(&material, 4).unwrap();
assert!(read.warnings.is_empty());
assert_eq!(read.pkg.resources[0].bytes.as_slice(), b"four");
let damaged = content.replace("<strdata v=", "<unused v=");
let read = read_source(&make_material(&damaged), 3).unwrap();
assert!(read.warnings.is_empty());
assert_eq!(read.pkg.resources[0].bytes.as_slice(), b"abc");
}
#[test]
fn corrupt_inline_headers_on_either_side_of_the_budget_keep_linked_fallback() {
for length in [3_u32, 4] {
let mut raw = length.to_be_bytes().to_vec();
raw.push(255);
let encoded = base64::engine::general_purpose::STANDARD.encode(raw);
let content = format!(
"<resource><identifier v=\"inline\"/><filepath v=\"resources/member.png\"/><source><binembedded><strdata v=\"{encoded}\"/></binembedded></source></resource>"
);
let read = read_source(&make_material(&content), 3).unwrap();
assert!(read.warnings.is_empty());
assert_eq!(read.pkg.resources[0].bytes.as_slice(), b"abc");
}
}
#[test]
fn round_trips_and_rejects_newer_versions() {
let m = manifest();
let bytes = write_material(&m, &[("maps/baseColor.png".into(), vec![1, 2, 3])], b"pkg").unwrap();
assert!(is_material(&bytes));
let (back, source) = read_material(&bytes).unwrap();
assert_eq!(back, Manifest { source: SOURCE.into(), ..m.clone() }, "the writer names the source entry");
assert_eq!(source, b"pkg");
// Parameters are flat objects with the value in the API's typed form
let json = serde_json::to_value(&m).unwrap();
assert_eq!(json["parameters"][0]["id"], "rough");
assert_eq!(json["parameters"][0]["value"], serde_json::json!({"t": "float", "v": 0.25}));
fn proven_inline_length_mismatch_preserves_linked_fallback() {
let mut encoder = flate2::write::ZlibEncoder::new(3_u32.to_be_bytes().to_vec(), flate2::Compression::default());
encoder.write_all(b"four").unwrap();
let encoded = base64::engine::general_purpose::STANDARD.encode(encoder.finish().unwrap());
let content = format!(
"<resource><identifier v=\"inline\"/><filepath v=\"resources/member.png\"/><source><binembedded><strdata v=\"{encoded}\"/></binembedded></source></resource>"
);
let read = read_source(&make_material(&content), 3).unwrap();
// Version 1 embeds the old JSON-based .mcraft source, which this app's current
// SBS package reader cannot open; do not claim that accepting its manifest is enough.
let older = Manifest { version: 1, ..m.clone() };
assert!(read_material(&write_material(&older, &[], b"").unwrap()).is_err());
let newer = Manifest { version: VERSION + 1, ..m };
assert!(read_material(&write_material(&newer, &[], b"").unwrap()).is_err());
assert!(!is_material(b"not a zip"));
assert!(read_material(b"garbage").is_err());
assert!(read.warnings.is_empty(), "{:?}", read.warnings);
assert_eq!(read.pkg.resources[0].bytes.as_slice(), b"abc");
}
#[test]
fn repeated_oversized_inline_probes_exhaust_document_work() {
let content = format!("{}{}{}", inline(b"four"), inline(b"four"), inline(b""));
let read = read_source(&make_material(&content), 3).unwrap();
assert!(read.pkg.resources.is_empty());
assert!(read.warnings.iter().any(|warning| warning.contains("inflation work budget")));
}
#[test]
fn inline_and_member_assets_share_the_exact_budget_in_either_order() {
for content in [format!("{}{}", inline(b"four"), linked("member")), format!("{}{}", linked("member"), inline(b"four"))] {
let material = make_material(&content);
let read = read_source(&material, 7).unwrap();
assert!(read.warnings.is_empty(), "{:?}", read.warnings);
assert_eq!(read.pkg.resources.iter().map(|resource| resource.bytes.len()).sum::<usize>(), 7);
let read = read_source(&material, 6).unwrap();
assert!(!read.warnings.is_empty(), "budget failure must reject the material");
assert!(read.pkg.resources.iter().map(|resource| resource.bytes.len()).sum::<usize>() <= 6);
}
}
#[test]
fn multiple_inline_assets_and_member_aliases_cannot_bypass_the_budget() {
for content in [format!("{}{}", inline(b"abc"), inline(b"def")), format!("{}{}", linked("a"), linked("b"))] {
let material = make_material(&content);
assert!(read_source(&material, 6).unwrap().warnings.is_empty());
let read = read_source(&material, 5).unwrap();
assert!(!read.warnings.is_empty());
assert_eq!(read.pkg.resources.iter().map(|resource| resource.bytes.len()).sum::<usize>(), 3);
}
}
#[test]
fn archive_budget_errors_name_the_budget_rather_than_a_missing_member() {
let material = make_material(&format!("{}{}", linked("a"), linked("b")));
let read = read_source(&material, 5).unwrap();
assert!(read.warnings.first().unwrap().contains("resource budget"));
assert_eq!(read.pkg.resources.iter().map(|resource| resource.bytes.len()).sum::<usize>(), 3);
}
#[test]
fn standalone_sbs_keeps_inline_compatibility_and_zero_length_is_free() {
let material = make_material(&format!("{}{}", inline(b""), linked("member")));
assert!(read_source(&material, 3).unwrap().warnings.is_empty());
let material = make_material(&format!("{}{}", inline(b"abc"), inline(b"def")));
let read = sbs::read_sbs(&material.source, "source.sbs", &|_| None).unwrap();
assert!(read.warnings.is_empty());
assert_eq!(read.pkg.resources.iter().map(|resource| resource.bytes.len()).sum::<usize>(), 6);
}
}
@@ -5,4 +5,6 @@ pub struct ImageInput {
pub identifier: String,
/// `gray` or `color`
pub kind: String,
/// The graph-authored usage, independent of the input identifier.
pub usage: String,
}
-22
View File
@@ -1,22 +0,0 @@
use super::{ImageInput, MapEntry, Parameter, PresetEntry};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Manifest {
pub format: String,
pub version: u32,
pub name: String,
/// The graph of the source package the maps were cooked from
pub graph: String,
/// Size of the maps in pixels
pub size: [u32; 2],
pub maps: Vec<MapEntry>,
/// Parameters other apps may change (scalar, vector, color, toggle and option inputs)
pub parameters: Vec<Parameter>,
/// Image inputs of the graph, fed from mesh maps by identifier
pub inputs: Vec<ImageInput>,
#[serde(default)]
pub presets: Vec<PresetEntry>,
pub source: String,
}
-11
View File
@@ -1,11 +0,0 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MapEntry {
/// Usage id: `baseColor`, `normal`, `roughness`, `metallic`, `height`, `ambientOcclusion`, `emissive`, `opacity`, `specular`
pub usage: String,
pub file: String,
/// `srgb` or `linear`
pub color_space: String,
}
+3 -9
View File
@@ -1,13 +1,7 @@
mod archive;
mod image_input;
mod manifest;
mod map_entry;
mod parameter;
mod preset_entry;
mod snapshot_builder;
pub use archive::{FORMAT, MATERIAL_EXT, VERSION, is_material, portable, read_material, srgb_usage, write_material};
pub use archive::{MATERIAL_EXT, VERSION, is_material, read_material, srgb_usage, write_material, write_material_with_resource_limit};
pub use image_input::ImageInput;
pub use manifest::Manifest;
pub use map_entry::MapEntry;
pub use parameter::Parameter;
pub use preset_entry::PresetEntry;
pub use snapshot_builder::{entry_graph, snapshot};
-10
View File
@@ -1,10 +0,0 @@
use materialcraft_core::{ExposedParameter, Value};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Parameter {
#[serde(flatten)]
pub param: ExposedParameter,
/// The value the maps were cooked with
pub value: Value,
}
@@ -1,9 +0,0 @@
use materialcraft_core::Value;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct PresetEntry {
pub name: String,
pub values: BTreeMap<String, Value>,
}
@@ -0,0 +1,227 @@
//! Copy reachable SBS definitions with local, collision-free identities.
use std::collections::{BTreeMap, BTreeSet};
use materialcraft_core::{Graph, GraphIndex, Package, Value};
use crate::{FormatError, Result};
// Bound the combined graph and function traversal independently of cooking
const MAX_DEPENDENCY_DEPTH: usize = 32;
fn bad(message: impl Into<String>) -> FormatError {
FormatError::BadPackage(message.into())
}
/// The location of the entry in the source project is independent of its location in the publication.
pub fn snapshot(package: &Package, entry: usize) -> Result<Package> {
let graph = package.graph(entry).map_err(|e| bad(e.to_string()))?;
if graph.function.is_some() {
return Err(bad(format!("select a material graph with connected outputs: `{}`", graph.sbs_path())));
}
let mut result = Package::new(&graph.name);
result.format_version.clone_from(&package.format_version);
let mut builder = SnapshotBuilder { root: package, result, completed: BTreeMap::new(), active: BTreeSet::new(), resources: BTreeMap::new(), nodes: 0 };
builder.copy_graph("", entry)?;
Ok(builder.result)
}
type GraphKey = (String, usize);
struct SnapshotBuilder<'a> {
root: &'a Package,
result: Package,
completed: BTreeMap<GraphKey, (String, usize)>,
active: BTreeSet<GraphKey>,
resources: BTreeMap<(String, String), String>,
nodes: usize,
}
impl<'a> SnapshotBuilder<'a> {
fn package(&self, owner: &str) -> Result<&'a Package> {
if owner.is_empty() {
return Ok(self.root);
}
self.root.dependencies.get(owner).map(AsRef::as_ref).ok_or_else(|| bad(format!("missing source package `{owner}`")))
}
fn reference(&mut self, owner: &str, name: &str) -> Result<(String, usize)> {
let (index, _) = self.package(owner)?.graph_by_name(name).ok_or_else(|| bad(format!("missing or ambiguous graph `{name}` in `{owner}`")))?;
self.copy_graph(owner, index)
}
fn copy_graph(&mut self, owner: &str, index: usize) -> Result<(String, usize)> {
let key = (owner.to_owned(), index);
let source = self.package(owner)?.graph(index).map_err(|e| bad(e.to_string()))?;
if self.active.contains(&key) {
return Err(bad(format!("cyclic graph reference at `{}`", source.sbs_path())));
}
if let Some((path, depth)) = self.completed.get(&key) {
// A shared subtree can be reached from a longer path than its first visit
if self.active.len() + depth > MAX_DEPENDENCY_DEPTH {
return Err(bad("published graph dependencies exceed the depth limit"));
}
return Ok((path.clone(), *depth));
}
let graph_count = self.completed.len() + self.active.len();
if self.active.len() >= MAX_DEPENDENCY_DEPTH || graph_count >= crate::sbs::MAX_GRAPHS {
return Err(bad("published graph dependencies exceed the depth or graph count limit"));
}
let mut graph = source.clone();
if graph.function.is_none() {
let outputs: Vec<_> = graph.nodes.iter().filter(|node| node.def == "output").map(|node| node.id).collect();
let index = GraphIndex::new(&graph);
if graph_count == 0 && !outputs.iter().any(|id| !index.incoming(*id).is_empty()) {
return Err(bad(format!("select a material graph with connected outputs: `{}`", graph.sbs_path())));
}
let needed = index.required_nodes(&outputs);
graph.nodes.retain(|node| needed.contains(&node.id) || matches!(node.def.as_str(), "input_color" | "input_grayscale" | "input_value"));
graph.connections.retain(|c| needed.contains(&c.from) && needed.contains(&c.to));
if graph.topo_order().len() != graph.nodes.len() {
return Err(bad(format!("cycle in graph `{}`", graph.sbs_path())));
}
}
self.nodes = self.nodes.saturating_add(graph.nodes.len());
if self.nodes > crate::sbs::MAX_NODES {
return Err(bad("published graph exceeds the node count limit"));
}
graph.folder = if graph_count == 0 { String::new() } else { format!("Dependencies/g{graph_count}") };
let path = graph.sbs_path();
self.active.insert(key.clone());
let mut max_child_depth = 0;
for instance in graph.instances_mut() {
let package = if instance.package.is_empty() { owner } else { &instance.package };
let (local_path, depth) = self.reference(package, &instance.graph)?;
instance.graph = local_path;
max_child_depth = max_child_depth.max(depth);
instance.package.clear();
}
for node in &mut graph.nodes {
if node.def == materialcraft_core::MISSING_NODE {
return Err(bad(format!("unsupported node `{}` in `{path}`", node.label)));
}
if node.def == "graph_instance" {
if let Some(instance) = &node.instance {
node.params.insert("graph".into(), Value::Text(instance.graph.clone()));
} else if let Some(Value::Text(target)) = node.params.get_mut("graph") {
let (local_path, depth) = self.reference(owner, target)?;
*target = local_path;
max_child_depth = max_child_depth.max(depth);
} else {
return Err(bad(format!("missing graph reference in `{path}`")));
}
}
match node.def.as_str() {
"bitmap" | "svg" => {
let Some(Value::Text(name)) = node.params.get_mut("resource").filter(|v| !matches!(v, Value::Text(s) if s.is_empty())) else {
return Err(bad(format!("missing resource for {} node {} in `{path}`", node.def, node.id.0)));
};
*name = self.resource(owner, name)?;
}
"text" => {
if let Some(Value::Text(name)) = node.params.get_mut("fontdata")
&& !name.is_empty()
{
*name = self.resource(owner, name)?;
}
}
_ => {}
}
}
for name in graph.input_images.values_mut() {
*name = self.resource(owner, name)?;
}
let depth = max_child_depth + 1;
self.active.remove(&key);
self.completed.insert(key, (path.clone(), depth));
self.result.graphs.push(graph);
Ok((path, depth))
}
fn resource(&mut self, owner: &str, name: &str) -> Result<String> {
let key = (owner.to_string(), name.to_string());
if let Some(local) = self.resources.get(&key) {
return Ok(local.clone());
}
let mut resource = self.package(owner)?.resource(name).cloned().ok_or_else(|| bad(format!("missing resource `{name}` in `{owner}`")))?;
if resource.bytes.is_empty() {
return Err(bad(format!("missing resource bytes for `{name}` ({})", resource.source)));
}
let local = format!("asset_{}", self.resources.len() + 1);
resource.name.clone_from(&local);
resource.folder = "Resources".into();
resource.source.clear();
self.resources.insert(key, local.clone());
self.result.resources.push(resource);
Ok(local)
}
}
pub fn entry_graph(pkg: &Package) -> Result<usize> {
let mut roots = pkg.graphs.iter().enumerate().filter(|(_, g)| g.folder.is_empty());
match (roots.next(), roots.next()) {
(Some((index, Graph { function: None, .. })), None) => Ok(index),
_ => Err(bad("source.sbs must have exactly one root material graph; put helper graphs and functions in a dependency folder")),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn connect_reference(graph: &mut Graph, target: &str) {
let instance = graph.add_node("graph_instance", [0.0; 2]);
graph.node_mut(instance).unwrap().params.insert("graph".into(), Value::Text(target.into()));
let output = graph.add_node("output", [0.0; 2]);
graph.connect(instance, "output", output, "input").unwrap();
}
#[test]
fn depth_limit_applies_to_both_paths_through_a_shared_subtree() {
for depth in [MAX_DEPENDENCY_DEPTH, MAX_DEPENDENCY_DEPTH + 1] {
for cached_first in [true, false] {
let mut package = Package::new("Shared subtree");
let mut root = Graph::new("Root");
let targets = if cached_first { ["Shared", "Link0"] } else { ["Link0", "Shared"] };
for target in targets {
connect_reference(&mut root, target);
}
package.graphs.push(root);
let link_count = depth - 3;
for index in 0..link_count {
let mut link = Graph::new(&format!("Link{index}"));
let target = if index + 1 == link_count { "Shared".into() } else { format!("Link{}", index + 1) };
connect_reference(&mut link, &target);
package.graphs.push(link);
}
let mut shared = Graph::new("Shared");
connect_reference(&mut shared, "Leaf");
package.graphs.extend([shared, Graph::new("Leaf")]);
let published = snapshot(&package, 0);
if depth == MAX_DEPENDENCY_DEPTH {
assert_eq!(published.unwrap().graphs.len(), depth);
} else {
assert!(published.unwrap_err().to_string().contains("depth"), "cached first: {cached_first}");
}
}
}
}
}
+2 -2
View File
@@ -1,8 +1,8 @@
pub(super) const MAX_XML_NODES: u32 = 8_000_000;
pub(super) const MAX_GRAPHS: usize = 4096;
pub(crate) const MAX_GRAPHS: usize = 4096;
pub(super) const MAX_NODES: usize = 100_000;
pub(crate) const MAX_NODES: usize = 100_000;
pub(super) const UPDATER_VERSION: &str = env!("CARGO_PKG_VERSION");
+115 -12
View File
@@ -1,29 +1,70 @@
use super::embedded_error::EmbeddedError;
use crate::{FormatError, Result};
use base64::Engine as _;
use std::io::{Read, Write};
use std::{
cell::Cell,
io::{self, Read, Write},
};
pub(super) fn base64_engine() -> base64::engine::GeneralPurpose {
base64::engine::general_purpose::STANDARD
}
pub(super) fn decode_embedded(strdata: &str) -> std::result::Result<Vec<u8>, String> {
let raw = base64_engine().decode(strdata.split_whitespace().collect::<String>()).map_err(|e| format!("bad base64: {e}"))?;
decode_embedded_bounded(strdata, crate::MAX_RESOURCE, &Cell::new(crate::MAX_RESOURCE + 1)).map_err(|error| error.to_string())
}
pub(super) fn decode_embedded_bounded(strdata: &str, remaining_bytes: u64, inflation_work: &Cell<u64>) -> std::result::Result<Vec<u8>, EmbeddedError> {
let work_before = inflation_work.get();
if work_before == 0 {
return Err(EmbeddedError::WorkBudget);
}
let raw = match strdata.split_once(char::is_whitespace) {
Some((prefix, rest)) => base64_engine().decode(std::iter::once(prefix).chain(rest.split_whitespace()).collect::<String>()),
None => base64_engine().decode(strdata),
}
.map_err(|error| format!("bad base64: {error}"))?;
let (length, compressed) = raw.split_at_checked(4).ok_or("too short")?;
let length = u32::from_be_bytes([length[0], length[1], length[2], length[3]]) as usize;
if length as u64 > crate::MAX_RESOURCE {
let declared_bytes = u64::from(u32::from_be_bytes([length[0], length[1], length[2], length[3]]));
if declared_bytes > crate::MAX_RESOURCE {
return Err("embedded resource exceeds the 256 MiB limit".into());
}
let mut out = Vec::new();
// Read one probe byte beyond the tighter bound to detect overruns
let probe_limit = declared_bytes.min(remaining_bytes) + 1;
let mut decoder = flate2::read::ZlibDecoder::new(compressed);
let mut limited = decoder.by_ref().take(probe_limit.min(work_before));
let mut output = Vec::new();
let result = if declared_bytes > remaining_bytes || declared_bytes >= work_before {
// Discard probe output that cannot become a retained asset
io::copy(&mut limited, &mut io::sink())
} else {
limited.read_to_end(&mut output).map(|bytes| bytes as u64)
};
// Reading one extra byte detects data that contradicts the declared format length
let mut decoder = flate2::read::ZlibDecoder::new(compressed).take(length as u64 + 1);
decoder.read_to_end(&mut out).map_err(|e| format!("bad compressed data: {e}"))?;
if out.len() != length {
return Err(format!("{} bytes where {length} were declared", out.len()));
// Charge every emitted byte, including output produced before a checksum error
inflation_work.set(work_before.saturating_sub(decoder.total_out()));
let actual_bytes = result.map_err(|error| format!("bad compressed data: {error}"))?;
if actual_bytes > declared_bytes {
return Err(format!("{actual_bytes} bytes where {declared_bytes} were declared").into());
}
Ok(out)
if work_before < probe_limit && actual_bytes == work_before {
return Err(EmbeddedError::WorkBudget);
}
if actual_bytes > remaining_bytes {
return Err(EmbeddedError::Budget);
}
if actual_bytes != declared_bytes {
return Err(format!("{actual_bytes} bytes where {declared_bytes} were declared").into());
}
// ResourceBytes retains the vector and its capacity
output.shrink_to_fit();
Ok(output)
}
pub(super) fn encode_embedded(bytes: &[u8]) -> Result<String> {
@@ -58,11 +99,73 @@ mod tests {
assert!(decode_embedded("AA").unwrap_err().starts_with("bad base64:"));
let encoded = encode_embedded(b"payload").unwrap();
let spaced = encoded.chars().map(|c| format!("{c} \n")).collect::<String>();
let spaced = encoded.chars().map(|c| format!("{c} \n\u{2003}")).collect::<String>();
assert_eq!(decode_embedded(&spaced).unwrap(), b"payload");
}
#[test]
fn aggregate_budget_bounds_inflation_without_masking_early_corruption() {
for length in [3_u32, 4] {
let mut raw = length.to_be_bytes().to_vec();
raw.push(255);
let encoded = base64_engine().encode(raw);
assert!(matches!(decode_embedded_bounded(&encoded, 3, &Cell::new(16)), Err(EmbeddedError::Damaged(_))));
}
let encoded = encode_embedded(b"four").unwrap();
assert!(matches!(decode_embedded_bounded(&encoded, 3, &Cell::new(16)), Err(EmbeddedError::Budget)));
assert_eq!(decode_embedded_bounded(&encoded, 4, &Cell::new(16)).unwrap(), b"four");
}
#[test]
fn inflation_work_counts_discarded_prefixes_and_stops_before_next_decode() {
let work = Cell::new(8);
let encoded = encode_embedded(b"four").unwrap();
for expected in [4, 0] {
assert!(matches!(decode_embedded_bounded(&encoded, 3, &work), Err(EmbeddedError::Budget)));
assert_eq!(work.get(), expected);
}
// Exhaustion stops the next resource before Base64 decoding
assert!(matches!(decode_embedded_bounded("!!!!", 3, &work), Err(EmbeddedError::WorkBudget)));
let work = Cell::new(2);
assert!(matches!(decode_embedded_bounded(&encoded, 4, &work), Err(EmbeddedError::WorkBudget)));
assert_eq!(work.get(), 0);
}
#[test]
fn checksum_errors_charge_output_without_spending_retained_budget() {
let mut raw = base64_engine().decode(encode_embedded(b"four").unwrap()).unwrap();
*raw.last_mut().unwrap() ^= 1;
let work = Cell::new(8);
assert!(matches!(decode_embedded_bounded(&base64_engine().encode(raw), 4, &work), Err(EmbeddedError::Damaged(_))));
assert_eq!(work.get(), 4);
}
#[test]
fn proven_length_mismatch_precedes_retained_budget() {
let mut raw = base64_engine().decode(encode_embedded(b"four").unwrap()).unwrap();
raw[..4].copy_from_slice(&3_u32.to_be_bytes());
let work = Cell::new(4);
assert!(matches!(decode_embedded_bounded(&base64_engine().encode(raw), 3, &work), Err(EmbeddedError::Damaged(_))));
assert_eq!(work.get(), 0);
}
#[test]
fn retained_assets_release_spare_decompression_capacity() {
let bytes = vec![17; 2 << 20];
let encoded = encode_embedded(&bytes).unwrap();
let decoded = decode_embedded(&encoded).unwrap();
assert_eq!(decoded, bytes);
assert!(decoded.capacity() < 2 * decoded.len(), "retained {} bytes for a {}-byte asset", decoded.capacity(), decoded.len());
}
#[test]
fn decompression_requires_the_declared_length() {
let encoded = encode_embedded(&[1, 2, 3, 4]).unwrap();
+28
View File
@@ -0,0 +1,28 @@
#[derive(Debug)]
pub(super) enum EmbeddedError {
Damaged(String),
Budget,
WorkBudget,
}
impl From<String> for EmbeddedError {
fn from(message: String) -> Self {
Self::Damaged(message)
}
}
impl From<&str> for EmbeddedError {
fn from(message: &str) -> Self {
Self::Damaged(message.into())
}
}
impl std::fmt::Display for EmbeddedError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Damaged(message) => formatter.write_str(message),
Self::Budget => formatter.write_str("embedded resource exceeds the remaining material resource budget"),
Self::WorkBudget => formatter.write_str("embedded resource exceeds the material inflation work budget"),
}
}
}
@@ -0,0 +1,138 @@
//! Preserve typed graph inputs, including unconnected inputs, when localizing graph references.
use super::xml::{child_element, child_value, children_named, xml_escape};
use materialcraft_core::GraphPort;
use roxmltree::Node;
use std::{collections::BTreeSet, fmt::Write};
pub(super) const MAX_INPUTS: usize = 4096;
pub(super) fn read(element: Node<'_, '_>) -> Option<Result<Vec<GraphPort>, String>> {
let interface = child_element(element, "materialcraftInterface")?;
Some((|| {
let mut inputs = Vec::new();
let mut names = BTreeSet::new();
for port in children_named(interface, "input") {
if inputs.len() == MAX_INPUTS {
return Err(format!("Graph instance exceeds the {MAX_INPUTS}-input limit"));
}
let identifier = child_value(port, "identifier").unwrap_or("");
if identifier.is_empty() || !names.insert(identifier) {
return Err("Invalid or duplicate graph input identifier".into());
}
let type_code = child_value(port, "type").and_then(|value| value.parse().ok()).ok_or("Invalid graph input type")?;
inputs.push(GraphPort { identifier: identifier.into(), type_code });
}
Ok(inputs)
})())
}
pub(super) fn write(out: &mut String, inputs: &[GraphPort]) -> Result<(), String> {
if inputs.len() > MAX_INPUTS {
return Err(format!("Graph instance exceeds the {MAX_INPUTS}-input limit"));
}
// Validate before writing because the XML escaper drops unsupported characters
let mut names = BTreeSet::new();
for port in inputs {
if port.identifier.is_empty() || !port.identifier.chars().all(craft_xml::is_xml_char) || !names.insert(&port.identifier) {
return Err("Invalid or duplicate graph input identifier".into());
}
}
out.push_str("<materialcraftInterface>");
for port in inputs {
let _ = write!(out, "<input><identifier v=\"{}\"/><type v=\"{}\"/></input>", xml_escape(&port.identifier), port.type_code);
}
out.push_str("</materialcraftInterface>");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn interface_rejects_duplicate_malformed_and_excess_inputs() {
let parse = |body: &str| {
let xml = format!("<compInstance><materialcraftInterface>{body}</materialcraftInterface></compInstance>");
let document = roxmltree::Document::parse(&xml).unwrap();
read(document.root_element()).unwrap()
};
assert!(parse("<input><identifier v=\"a\"/><type v=\"bad\"/></input>").is_err());
assert!(parse("<input><identifier v=\"a\"/><type v=\"1\"/></input>".repeat(2).as_str()).is_err());
let mut body = String::new();
for index in 0..MAX_INPUTS {
write!(body, "<input><identifier v=\"p{index}\"/><type v=\"2\"/></input>").unwrap();
}
assert_eq!(parse(&body).unwrap().len(), MAX_INPUTS);
body.push_str("<input><identifier v=\"extra\"/><type v=\"2\"/></input>");
assert!(parse(&body).is_err());
}
}
#[cfg(test)]
mod writer_tests {
use super::*;
#[test]
fn writer_preserves_identifiers_or_refuses_before_appending() {
for invalid in ['\0', '\u{1f}', '\u{fffe}', '\u{ffff}'] {
let inputs =
vec![GraphPort { identifier: "roughness".into(), type_code: 2 }, GraphPort { identifier: format!("roughness{invalid}"), type_code: 2 }];
let mut out = String::from("unchanged");
assert!(write(&mut out, &inputs).is_err());
assert_eq!(out, "unchanged");
assert!(write(&mut out, &inputs[1..]).is_err());
assert_eq!(out, "unchanged");
}
let inputs = vec![GraphPort { identifier: "é😀<&\"'\t\n\r".into(), type_code: 2 }];
let mut xml = String::from("<compInstance>");
write(&mut xml, &inputs).unwrap();
xml.push_str("</compInstance>");
let document = roxmltree::Document::parse(&xml).unwrap();
assert_eq!(read(document.root_element()).unwrap().unwrap(), inputs);
}
#[test]
fn writer_matches_reader_limits_and_refuses_before_appending() {
let mut inputs: Vec<_> = (0..MAX_INPUTS).map(|index| GraphPort { identifier: format!("p{index}"), type_code: 2 }).collect();
let mut xml = String::from("<compInstance>");
write(&mut xml, &inputs).unwrap();
xml.push_str("</compInstance>");
let document = roxmltree::Document::parse(&xml).unwrap();
assert_eq!(read(document.root_element()).unwrap().unwrap(), inputs);
inputs.push(GraphPort { identifier: "extra".into(), type_code: 2 });
let mut out = String::from("unchanged");
assert!(write(&mut out, &inputs).unwrap_err().contains("4096"));
assert_eq!(out, "unchanged");
inputs.truncate(2);
inputs[1].identifier = inputs[0].identifier.clone();
assert!(write(&mut out, &inputs).is_err());
inputs[1].identifier.clear();
assert!(write(&mut out, &inputs).is_err());
assert_eq!(out, "unchanged");
}
#[test]
fn writer_rejects_input_identifiers_that_xml_would_change() {
let mut xml = String::new();
let inputs = [GraphPort { identifier: "roughness".into(), type_code: 2 }, GraphPort { identifier: "roughness\u{1f}".into(), type_code: 2 }];
assert!(write(&mut xml, &inputs).is_err());
assert!(xml.is_empty());
}
}
+4
View File
@@ -4,6 +4,7 @@ mod component_usage;
mod connection;
mod constant;
mod embedded;
mod embedded_error;
mod filter_map;
mod function_graph;
mod function_instance;
@@ -11,6 +12,7 @@ mod graph_attributes;
mod group_layout;
mod image_input;
mod input_bridge;
mod instance_interface;
mod layout_frame;
mod layout_item;
pub mod library;
@@ -35,6 +37,7 @@ mod warnings;
mod xml;
pub use constant::SBS_EXT;
pub(crate) use constant::{MAX_GRAPHS, MAX_NODES};
pub use layout_frame::LayoutFrame;
pub use layout_item::LayoutItem;
pub use library_layout::read_layout;
@@ -49,6 +52,7 @@ mod tests;
mod read;
mod write;
mod presets;
mod preview_binding;
mod resource;
+56
View File
@@ -0,0 +1,56 @@
//! Named SBS input presets refer to the same identifiers and UIDs as the graph interface.
use std::collections::HashMap;
use std::fmt::Write;
use materialcraft_core::{Graph, Preset};
use roxmltree::Node;
use super::sbs_value::{constant_xml, exposed_type_code, sbs_constant, to_param_value};
use super::xml::{child_element, child_value, children_named, xml_escape};
pub(super) fn write(out: &mut String, graph: &Graph, uids: &HashMap<String, u64>) {
if graph.presets.is_empty() {
return;
}
out.push_str("<sbspresets>");
for preset in &graph.presets {
let _ = write!(out, "<sbspreset><label v=\"{}\"/><presetinputs>", xml_escape(&preset.name));
for (id, value) in &preset.values {
if let (Some(uid), Some(constant)) = (uids.get(id), constant_xml(value)) {
let _ = write!(
out,
"<presetinput><uid v=\"{uid}\"/><identifier v=\"{}\"/><paramValue>{constant}</paramValue><type v=\"{}\"/></presetinput>",
xml_escape(id),
exposed_type_code(value)
);
}
}
out.push_str("</presetinputs></sbspreset>");
}
out.push_str("</sbspresets>");
}
pub(super) fn read(element: Node<'_, '_>, graph: &mut Graph) {
let Some(presets) = child_element(element, "sbspresets") else {
return;
};
let exposed: HashMap<_, _> = graph.exposed.iter().map(|p| (p.id.as_str(), p)).collect();
for preset in children_named(presets, "sbspreset") {
let Some(name) = child_value(preset, "label") else {
continue;
};
let values = child_element(preset, "presetinputs")
.into_iter()
.flat_map(|inputs| children_named(inputs, "presetinput"))
.filter_map(|input| {
let param = exposed.get(child_value(input, "identifier")?)?;
let raw = sbs_constant(child_element(input, "paramValue")?)?;
Some((param.id.clone(), to_param_value(&param.default, &raw, param.options.len())?))
})
.collect();
graph.presets.push(Preset { name: name.into(), values });
}
}
+10 -1
View File
@@ -15,7 +15,12 @@ pub fn read_sbs(bytes: &[u8], name: &str, linked: &dyn Fn(&str) -> Option<Vec<u8
super::SbsDocument::parse(bytes)?.read(name, linked)
}
pub(super) fn read_document(doc: &roxmltree::Document<'_>, name: &str, linked: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<SbsRead> {
pub(super) fn read_document(
doc: &roxmltree::Document<'_>,
name: &str,
linked: &dyn Fn(&str) -> Option<Vec<u8>>,
resource_budget: Option<&std::cell::Cell<u64>>,
) -> Result<SbsRead> {
let text = doc.input_text();
let root = doc.root_element();
@@ -28,7 +33,11 @@ pub(super) fn read_document(doc: &roxmltree::Document<'_>, name: &str, linked: &
_ => "Untitled package".to_string(),
};
// Include one probe byte so a valid inline asset can use the exact retained limit
let inflation_work = std::cell::Cell::new(resource_budget.map_or(0, |budget| budget.get().saturating_add(1)));
let mut reader = SbsReader {
inflation_work,
resource_budget,
text,
warnings: &mut warnings,
dependencies: HashMap::new(),
+6 -1
View File
@@ -34,9 +34,14 @@ impl<'input> SbsDocument<'input> {
super::library_layout::read_document(&self.document, uri)
}
/// Apply one aggregate resource budget to inline data and the caller's linked reads.
pub(crate) fn read_with_resource_budget(&self, name: &str, linked: &dyn Fn(&str) -> Option<Vec<u8>>, budget: &std::cell::Cell<u64>) -> Result<SbsRead> {
super::read::read_document(&self.document, name, linked, Some(budget))
}
/// Read owned package data; the result does not retain the XML document or input bytes.
pub fn read(&self, name: &str, linked: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<SbsRead> {
super::read::read_document(&self.document, name, linked)
super::read::read_document(&self.document, name, linked, None)
}
}
+54 -8
View File
@@ -3,10 +3,12 @@ use super::component_usage;
use super::connection::connect_all;
use super::constant::{MAX_NODES, MAX_WIRES};
use super::constant::{NODE_HALF_SIZE, SELF_DEPENDENCY};
use super::embedded::decode_embedded;
use super::embedded::{decode_embedded, decode_embedded_bounded};
use super::embedded_error::EmbeddedError;
use super::filter_map::{BASE_PARAMS, FilterMap, HANDLED_PARAMS, filter_by_sbs_name};
use super::function_graph;
use super::input_bridge::InputBridge;
use super::instance_interface::MAX_INPUTS;
use super::placeholder::{add_placeholder_input, fill_placeholder, placeholder_outputs};
use super::read_node::ReadNode;
use super::sbs_value::{SbsValue, exposed_default, sbs_constant, to_natural_value, to_param_value};
@@ -22,6 +24,8 @@ use roxmltree::Node as XmlNode;
use std::collections::{HashMap, HashSet};
pub(super) struct SbsReader<'text, 'warnings> {
pub(super) inflation_work: std::cell::Cell<u64>,
pub(super) resource_budget: Option<&'warnings std::cell::Cell<u64>>,
pub(super) text: &'text str,
pub(super) warnings: &'warnings mut Warnings,
pub(super) dependencies: HashMap<String, String>,
@@ -108,9 +112,25 @@ impl<'text> SbsReader<'text, '_> {
let mut why_missing = String::new();
if let Some(embedded) = source.and_then(|s| child_element(s, "binembedded")) {
match decode_embedded(child_value(embedded, "strdata").unwrap_or("")) {
Ok(data) if !font || !data.is_empty() => bytes = Some(data),
let encoded = child_value(embedded, "strdata").unwrap_or("");
let decoded = match self.resource_budget {
Some(budget) => decode_embedded_bounded(encoded, budget.get(), &self.inflation_work),
None => decode_embedded(encoded).map_err(EmbeddedError::Damaged),
};
match decoded {
Ok(data) if !font || !data.is_empty() => {
if let Some(budget) = self.resource_budget {
// The decoder checked the retained length
budget.set(budget.get().saturating_sub(data.len() as u64));
}
bytes = Some(data);
}
Ok(_) => why_missing = "its embedded font data is empty".into(),
Err(error @ (EmbeddedError::Budget | EmbeddedError::WorkBudget)) => {
// Reject a budget error to preserve inline asset precedence
self.warnings.add(format!("resource `{path}`: {error}"));
return unique_name(&mut self.used_resource_names, if identifier.is_empty() { "resource" } else { identifier });
}
Err(e) => why_missing = format!("its embedded data is damaged ({e})"),
}
}
@@ -135,7 +155,7 @@ impl<'text> SbsReader<'text, '_> {
}
}
// A supplied font, including cour.ttf or embedded data, always wins. Only absence uses the shared fallback
// Prefer supplied font bytes before trying the shared fallback
if font && bytes.is_none() {
if let Some(name) = super::resource_replacement::load_font(self, pkg, identifier, path, linked) {
return name;
@@ -262,6 +282,9 @@ impl<'text> SbsReader<'text, '_> {
self.node_count += 1;
let mut node = self.read_node(&mut graph, *comp_node, &image_inputs, &outputs);
if let Some(document_node) = graph.nodes.get_mut(node.index) {
document_node.disabled = child_value(*comp_node, "materialcraftDisabled") == Some("1");
}
for input in child_element(*comp_node, "inputValues").into_iter().flat_map(|inputs| children_named(inputs, "compInputValue")).take(64) {
let identifier = child_value(input, "identifier").unwrap_or("");
let value_type = child_value(input, "type").and_then(|code| code.parse().ok()).and_then(materialcraft_core::ValueType::from_code);
@@ -339,9 +362,15 @@ impl<'text> SbsReader<'text, '_> {
step: None,
};
super::parameter_widget::read(input, &mut exposed);
if let Some(current) =
child_element(input, "materialcraftValue").and_then(sbs_constant).and_then(|raw| to_param_value(&exposed.default, &raw, exposed.options.len()))
{
graph.inputs.insert(exposed.id.clone(), current);
}
graph.exposed.push(exposed);
}
super::presets::read(element, graph);
image_inputs
}
@@ -374,6 +403,13 @@ impl<'text> SbsReader<'text, '_> {
if let Some(node) = graph.nodes.get_mut(to.index) {
if let Some(instance) = &mut node.instance {
if !instance.inputs.iter().any(|port| port.identifier == sbs_input) {
if instance.inputs.len() >= MAX_INPUTS || sbs_input.is_empty() {
self.warnings.add(format!(
"graph `{name}`: invalid input `{sbs_input}` or instance at its {MAX_INPUTS}-input limit; wire was dropped"
));
continue;
}
instance.inputs.push(materialcraft_core::GraphPort { identifier: sbs_input.into(), type_code: 1 });
}
} else {
@@ -556,9 +592,19 @@ impl<'text> SbsReader<'text, '_> {
if own_graph.is_some() || external.is_some() {
let (id, index, node) = push_node(graph, "graph_instance", pos);
if let Some(node) = node {
if let Some(sub_graph) = own_graph {
node.params.insert("graph".into(), Value::Text(sub_graph));
} else {
let interface = super::instance_interface::read(implementation);
let has_interface = interface.is_some();
let inputs = match interface.transpose() {
Ok(inputs) => inputs.unwrap_or_default(),
Err(error) => {
self.warnings.add(error);
Vec::new()
}
};
if let Some(sub_graph) = &own_graph {
node.params.insert("graph".into(), Value::Text(sub_graph.clone()));
}
if own_graph.is_none() || has_interface {
let comp_node = implementation.parent().and_then(|parent| parent.parent());
let types: HashMap<_, _> = comp_node
.and_then(|node| child_element(node, "compOutputs"))
@@ -573,7 +619,7 @@ impl<'text> SbsReader<'text, '_> {
defaults: Default::default(),
package: external.unwrap_or("").into(),
graph: path.clone(),
inputs: Vec::new(),
inputs,
outputs: bridgings
.iter()
.map(|(uid, identifier)| materialcraft_core::GraphPort {
+16 -2
View File
@@ -91,6 +91,10 @@ impl SbsWriter {
let (x, y) = (node.pos[0] + NODE_HALF_SIZE[0], node.pos[1] + NODE_HALF_SIZE[1]);
let _ = write!(out, "<compNode><uid v=\"{uid}\"/><GUILayout><gpos v=\"{} {} 0\"/></GUILayout>", format_float(x), format_float(y));
if node.disabled {
out.push_str("<materialcraftDisabled v=\"1\"/>");
}
if !node.input_values.is_empty() {
out.push_str("<inputValues>");
for (identifier, value) in &node.input_values {
@@ -207,24 +211,31 @@ impl SbsWriter {
out.push_str("</paraminput>");
}
let mut exposed_uids = HashMap::new();
for exposed in &graph.exposed {
let default = constant_xml(graph.inputs.get(&exposed.id).unwrap_or(&exposed.default)).unwrap_or_else(|| "<constantValueFloat1 v=\"0\"/>".into());
let uid = self.next_uid();
exposed_uids.insert(exposed.id.clone(), uid);
let default = constant_xml(&exposed.default).unwrap_or_else(|| "<constantValueFloat1 v=\"0\"/>".into());
let group = if exposed.group.is_empty() { String::new() } else { format!("<group v=\"{}\"/>", xml_escape(&exposed.group)) };
let _ = write!(
out,
"<paraminput><identifier v=\"{}\"/><uid v=\"{}\"/><attributes><label v=\"{}\"/><description v=\"{}\"/></attributes><type v=\"{}\"/><defaultValue>{default}</defaultValue>",
xml_escape(&exposed.id),
self.next_uid(),
uid,
xml_escape(&exposed.label),
xml_escape(&exposed.description),
exposed_type_code(&exposed.default)
);
if let Some(current) = graph.inputs.get(&exposed.id).and_then(constant_xml) {
let _ = write!(out, "<materialcraftValue>{current}</materialcraftValue>");
}
super::parameter_widget::write(out, exposed);
let _ = write!(out, "{group}</paraminput>");
}
out.push_str("</paraminputs>");
super::presets::write(out, graph, &exposed_uids);
entries
}
@@ -296,6 +307,9 @@ impl SbsWriter {
xml_escape(&sub_graph),
dependency
);
if let Some(instance) = &node.instance {
super::instance_interface::write(out, &instance.inputs).map_err(FormatError::Io)?;
}
write_bridgings(out, outputs);
out.push_str("</compInstance>");
}
+80
View File
@@ -458,3 +458,83 @@ fn input_usage_defaults_and_unknown_metadata_stay_unclassified() {
}
}
}
#[test]
fn published_material_preserves_disabled_nodes_and_localized_typed_interfaces() {
use crate::material::{read_material, write_material};
use materialcraft_core::{GraphInstance, GraphPort, ValueType};
let mut helper = Graph::new("Helper");
let input = helper.add_node("input_grayscale", [0.0; 2]);
helper.node_mut(input).unwrap().params.insert("identifier".into(), Value::Text("gray".into()));
let out = helper.add_node("output", [0.0; 2]);
helper.node_mut(out).unwrap().params.insert("identifier".into(), Value::Text("result".into()));
helper.connect(input, "output", out, "input").unwrap();
let mut dependency = Package::new("Dependency");
dependency.graphs.push(helper);
let mut graph = Graph::new("Root");
let uniform = graph.add_node("uniform", [0.0; 2]);
let levels = graph.add_node("levels", [0.0; 2]);
graph.node_mut(levels).unwrap().disabled = true;
graph.connect(uniform, "output", levels, "input").unwrap();
let instance = graph.add_node("graph_instance", [0.0; 2]);
let inputs = vec![GraphPort { identifier: "gray".into(), type_code: 2 }, GraphPort { identifier: "unused<&".into(), type_code: ValueType::Float as u32 }];
graph.node_mut(instance).unwrap().instance = Some(GraphInstance {
package: "dependency.sbs".into(),
graph: "Helper".into(),
inputs: inputs.clone(),
outputs: vec![GraphPort { identifier: "result".into(), type_code: 2 }],
defaults: Default::default(),
label: String::new(),
});
graph.connect(levels, "output", instance, "gray").unwrap();
let out = graph.add_node("output", [0.0; 2]);
graph.connect(instance, "result", out, "input").unwrap();
let mut package = Package::new("Package");
package.graphs.push(graph);
package.dependencies.insert("dependency.sbs".into(), std::sync::Arc::new(dependency));
let bytes = write_material(&package, 0).unwrap();
let (opened, entry) = read_material(&bytes).unwrap();
let graph = &opened.graphs[entry];
assert!(graph.nodes.iter().find(|node| node.def == "levels").unwrap().disabled);
assert!(!graph.nodes.iter().find(|node| node.def == "uniform").unwrap().disabled);
let node = graph.nodes.iter().find(|node| node.def == "graph_instance").unwrap();
let restored = node.instance.as_ref().expect("localization must retain the typed interface");
assert!(restored.package.is_empty());
assert_eq!(restored.inputs, inputs);
assert_eq!(restored.outputs, vec![GraphPort { identifier: "result".into(), type_code: 2 }]);
assert!(opened.graph_by_name(&restored.graph).is_some());
assert!(graph.connections.iter().any(|wire| wire.to == node.id && wire.to_port == "gray"));
// Republish the already-localized graph to exercise local interfaces
let bytes = write_material(&opened, entry).unwrap();
let (reopened, entry) = read_material(&bytes).unwrap();
assert_eq!(reopened.graphs[entry].nodes.iter().find_map(|node| node.instance.as_ref()).unwrap().inputs, inputs);
assert!(reopened.graphs[entry].nodes.iter().find(|node| node.def == "levels").unwrap().disabled);
}
#[test]
fn inferred_instance_inputs_respect_the_typed_interface_limit() {
use materialcraft_core::{GraphInstance, GraphPort};
let mut package = Package::new("Package");
let mut root = Graph::new("Root");
let source = root.add_node("uniform", [0.0; 2]);
let instance = root.add_node("graph_instance", [0.0; 2]);
let inputs = (0..4096).map(|index| GraphPort { identifier: format!("input{index}"), type_code: 1 }).collect();
root.node_mut(instance).unwrap().instance =
Some(GraphInstance { label: String::new(), package: String::new(), graph: "Helper".into(), inputs, outputs: Vec::new(), defaults: Default::default() });
root.connect(source, "output", instance, "input0").unwrap();
package.graphs.extend([root, Graph::new("Helper")]);
let xml = String::from_utf8(write_sbs(&package).unwrap()).unwrap();
let xml = xml.replace("<connection><identifier v=\"input0\"", "<connection><identifier v=\"inferred\"");
let restored = read_sbs(xml.as_bytes(), "instance.sbs", &no_linked_files).unwrap();
let root = &restored.pkg.graphs[0];
assert_eq!(root.instances().next().unwrap().inputs.len(), 4096);
assert!(root.connections.is_empty());
assert!(restored.warnings.iter().any(|warning| warning.contains("4096-input")));
}
+2 -3
View File
@@ -36,11 +36,10 @@ craft-ui = { workspace = true, features = ["native-windows"] }
# Browser file I/O (web_files.rs): async pickers and Blob downloads.
[target.'cfg(target_arch = "wasm32")'.dependencies]
craft-platform = { workspace = true, features = ["browser"] }
rfd = { workspace = true }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
js-sys = "0.3"
web-sys = { version = "0.3", features = ["Window", "Document", "HtmlElement", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "Url", "CssStyleDeclaration", "Navigator"] }
web-sys = { version = "0.3", features = ["Window", "Navigator"] }
# winit needs a window system on Linux, so the crate's own tests build with X11.
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
+2 -2
View File
@@ -14,9 +14,9 @@ pub enum ExportTab {
FromGraph,
/// The outputs of several graphs as image files
Batch,
/// A MaterialX document with its textures in a folder (#190)
/// A MaterialX document with its textures in a folder
MaterialX,
/// The same files as MaterialX in one .mcmat archive (#190)
/// A published procedural graph with its source dependencies
Mcmat,
}
+84 -10
View File
@@ -35,7 +35,7 @@ fn selection_names_the_checked_outputs_and_their_color_spaces() {
let (outputs, color_spaces) = settings.selection(graph).unwrap();
assert_eq!(outputs, Value::Null, "with every output checked, file.export writes them all");
assert_eq!(color_spaces, json!({"base_color": "srgb", "height": "raw"}), "unpicked outputs follow their usage");
assert_eq!(color_spaces, json!({}), "unpicked outputs follow the encoder's defaults");
settings.skipped_outputs.insert(output_key("New_graph", "height"));
settings.color_spaces.insert(output_key("New_graph", "base_color"), materialcraft_engine::ColorSpace::Linear);
@@ -91,15 +91,9 @@ fn unwired_options_say_they_are_not_available_yet() {
let directory = temp("unwired");
let planned = ExportSettings { destination: directory.display().to_string(), format: "dds".into(), ..ExportSettings::default() };
for (tab, settings) in [(ExportTab::MaterialX, ExportSettings::default()), (ExportTab::Mcmat, ExportSettings::default()), (ExportTab::FromGraph, planned)] {
app.ui.export_tab = tab;
let toasts = app.toasts.len();
assert!(!export(&mut app, &settings), "{tab:?} keeps the window open");
let toast = app.toasts.get(toasts).map(|toast| toast.text.clone()).unwrap_or_default();
assert!(toast.contains("isn't available yet"), "{tab:?}: {toast}");
}
app.ui.export_tab = ExportTab::FromGraph;
assert!(!export(&mut app, &planned));
assert!(app.toasts.last().unwrap().text.contains("isn't available yet"));
assert!(!directory.exists(), "nothing is written");
}
@@ -111,3 +105,83 @@ fn the_format_list_drops_tiff_and_lists_dds_as_planned() {
assert_eq!(ExportFormat::find("dds").map(|format| format.available), Some(false));
assert_eq!(ExportFormat::find("png").map(ExportFormat::title).as_deref(), Some("PNG (*.png)"));
}
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn material_dialog_rejects_another_package_with_the_same_graph_path() {
for tab in [ExportTab::Mcmat, ExportTab::MaterialX] {
let mut app = app();
open(&mut app, tab);
let draft = app.ui.export_draft.clone().unwrap();
let original = app.session.document_id;
app.run("file.newPackage", json!({})).unwrap();
app.run("file.new", json!({"name": draft.material.root, "template": "empty"})).unwrap();
assert!(app.session.pkg.graph_by_name(&draft.material.root).is_some());
assert!(!publish(&mut app, &draft));
assert!(app.toasts.last().unwrap().text.contains("export package changed"));
assert!(app.material_export_job.is_none());
// Switching back preserves the original dialog and reaches destination validation
app.run("package.activate", json!({"document": original})).unwrap();
assert!(!publish(&mut app, &draft));
assert!(app.toasts.last().unwrap().text.contains("export destination"));
}
}
#[test]
fn material_dialog_does_not_restore_a_closed_package_identity() {
let mut app = app();
open(&mut app, ExportTab::Mcmat);
let draft = app.ui.export_draft.clone().unwrap();
app.run("package.closeAll", json!({"discard": true})).unwrap();
assert!(!publish(&mut app, &draft));
assert!(app.toasts.last().unwrap().text.contains("export package changed"));
app.run("file.new", json!({"name": draft.material.root, "template": "empty"})).unwrap();
app.toasts.clear();
assert!(!publish(&mut app, &draft));
assert!(app.toasts.last().unwrap().text.contains("export package changed"));
open(&mut app, ExportTab::Mcmat);
let reopened = app.ui.export_draft.as_ref().unwrap();
assert_eq!(reopened.material.document, Some(app.session.document_id));
assert_ne!(reopened.material.document, draft.material.document);
}
#[test]
fn material_dialog_identity_is_not_saved_in_preferences() {
let mut app = app();
open(&mut app, ExportTab::Mcmat);
let draft = app.ui.export_draft.as_ref().unwrap();
let stored = serde_json::to_value(draft).unwrap();
assert!(stored["material"].get("document").is_none());
let restored: ExportSettings = serde_json::from_value(stored).unwrap();
assert_eq!(restored.material.document, None);
}
#[test]
fn folder_graphs_keep_independent_output_selection_and_color_spaces() {
let app = app();
let mut indoor = app.session.graph().unwrap().clone();
indoor.name = "Oak".into();
indoor.folder = "Indoor".into();
let mut outdoor = indoor.clone();
outdoor.folder = "Outdoor".into();
let mut settings = ExportSettings::default();
settings.include_all(&indoor, false);
assert!(settings.selection(&indoor).is_none());
assert_eq!(settings.selection(&outdoor).unwrap(), (Value::Null, json!({})));
settings.include_all(&indoor, true);
settings.color_spaces.insert(output_key(&indoor.sbs_path(), "base_color"), materialcraft_engine::ColorSpace::Linear);
assert_eq!(settings.selection(&indoor).unwrap(), (Value::Null, json!({"base_color":"linear"})));
assert_eq!(settings.selection(&outdoor).unwrap(), (Value::Null, json!({})));
}
#[test]
fn material_export_reports_missing_shader_connections_on_every_platform() {
assert_eq!(material_export_message(&Value::Null), tr("Material exported"));
let message = material_export_message(&json!({"unmapped":["custom_output"]}));
assert!(message.contains(tr("Exported without shader connections")));
assert!(message.contains("custom_output"));
}
@@ -1,6 +1,7 @@
use super::TextureDepth;
use serde::{Deserialize, Serialize};
/// MaterialX and .mcmat publishing options from #190, kept for the exporter that will read them
/// Destinations and bake settings for the selected source graph.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct MaterialExportSettings {
@@ -10,13 +11,16 @@ pub struct MaterialExportSettings {
pub archive: String,
/// Pixels of the larger side; 0 keeps the graph's size
pub size: u32,
/// Source tangent convention when the output graph does not identify it
pub normal_y_down: Option<bool>,
pub texture_depth: TextureDepth,
pub normal_orientation: NormalOrientation,
#[serde(skip)]
pub root: String,
/// The open package this dialog was created for; never restore an identity from preferences.
#[serde(skip)]
pub document: Option<u64>,
/// Height in scene units of a white displacement texel
pub displacement_scale: f32,
/// Add preview.png next to material.mtlx
pub include_preview: bool,
pub controls: ExposedControls,
}
impl Default for MaterialExportSettings {
@@ -25,76 +29,11 @@ impl Default for MaterialExportSettings {
folder: String::new(),
archive: String::new(),
size: 0,
normal_y_down: None,
texture_depth: TextureDepth::default(),
normal_orientation: NormalOrientation::default(),
root: String::new(),
document: None,
displacement_scale: 1.0,
include_preview: false,
controls: ExposedControls::default(),
}
}
}
/// PNG precision of the published textures; HDR and signed data always use lossless EXR
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum TextureDepth {
/// 16 bits for normal and height, 8 bits for the rest
#[default]
Automatic,
Eight,
Sixteen,
}
impl TextureDepth {
pub const ALL: [TextureDepth; 3] = [TextureDepth::Automatic, TextureDepth::Eight, TextureDepth::Sixteen];
pub fn label(self) -> &'static str {
match self {
TextureDepth::Automatic => "Automatic (16 bits for normal and height)",
TextureDepth::Eight => "8 bits",
TextureDepth::Sixteen => "16 bits",
}
}
}
/// Which way the green channel of published normal maps points
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum NormalOrientation {
#[default]
OpenGl,
DirectX,
}
impl NormalOrientation {
pub const ALL: [NormalOrientation; 2] = [NormalOrientation::OpenGl, NormalOrientation::DirectX];
pub fn label(self) -> &'static str {
match self {
NormalOrientation::OpenGl => "OpenGL (Y+)",
NormalOrientation::DirectX => "DirectX (Y-)",
}
}
}
/// What happens to the graph's exposed parameters in the published material
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ExposedControls {
/// Keep the ones the published material can still evaluate
#[default]
KeepSupported,
/// Bake every control into the textures
BakeAll,
}
impl ExposedControls {
pub const ALL: [ExposedControls; 2] = [ExposedControls::KeepSupported, ExposedControls::BakeAll];
pub fn label(self) -> &'static str {
match self {
ExposedControls::KeepSupported => "Keep supported controls",
ExposedControls::BakeAll => "Bake every control",
}
}
}
+47 -68
View File
@@ -1,89 +1,68 @@
//! The MaterialX and MCMAT tabs: #190 publishing options, kept until the exporter that reads them lands
use egui::{RichText, Ui};
use super::{ExportSettings, ExportTab, ExposedControls, NormalOrientation, TextureDepth, outputs};
use crate::MaterialCraftApp;
use crate::i18n::tr;
use crate::theme::Tokens;
//! Publish a procedural archive or bake its outputs for MaterialX.
use super::{ExportSettings, ExportTab, TextureDepth, outputs};
use crate::{MaterialCraftApp, i18n::tr};
use egui::Ui;
pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui, settings: &mut ExportSettings, tab: ExportTab) {
let tokens = Tokens::get(ui.ctx());
let contents = if tab == ExportTab::Mcmat {
tr("One .mcmat archive: material.mtlx, material.json, resources/ and the optional preview.png")
} else {
tr("A folder: material.mtlx, material.json, resources/ and the optional preview.png")
};
ui.label(RichText::new(contents).size(11.0).color(tokens.text_dim));
ui.add_space(4.0);
let material = &mut settings.material;
super::form(ui, "export-material-options", |ui, width| {
super::form_label(ui, tr("Root graph"));
let selected = material.root.clone();
super::dropdown(ui, "Root graph", width, &selected, |ui| {
for graph in app.session.pkg.graphs.iter().filter(|g| g.function.is_none()) {
let path = graph.sbs_path();
ui.selectable_value(&mut material.root, path.clone(), path);
}
});
ui.end_row();
#[cfg(not(target_arch = "wasm32"))]
{
super::form_label(ui, tr("Destination"));
let path = if tab == ExportTab::Mcmat { &mut settings.material.archive } else { &mut settings.material.folder };
let path = if tab == ExportTab::Mcmat { &mut material.archive } else { &mut material.folder };
if super::path_field(ui, path, width) {
ask_destination(app, tab);
ask_destination(app, tab, &material.root);
}
ui.end_row();
}
let material = &mut settings.material;
super::form_label(ui, tr("Resolution"));
super::size_menu(ui, "Resolution", width, &mut material.size);
ui.end_row();
super::form_label(ui, tr("Textures"));
super::dropdown(ui, "Textures", width, tr(material.texture_depth.label()), |ui| {
for depth in TextureDepth::ALL {
ui.selectable_value(&mut material.texture_depth, depth, tr(depth.label()));
}
});
ui.end_row();
super::form_label(ui, tr("Normal orientation"));
super::dropdown(ui, "Normal orientation", width, tr(material.normal_orientation.label()), |ui| {
for orientation in NormalOrientation::ALL {
ui.selectable_value(&mut material.normal_orientation, orientation, tr(orientation.label()));
}
});
ui.end_row();
super::form_label(ui, tr("Displacement scale"));
let field = ui.add(egui::DragValue::new(&mut material.displacement_scale).speed(0.01).range(0.0..=100.0).max_decimals(3));
crate::widgets::name(&field, egui::Role::SpinButton, "Displacement scale");
ui.end_row();
super::form_label(ui, tr("Exposed parameters"));
super::dropdown(ui, "Exposed parameters", width, tr(material.controls.label()), |ui| {
for controls in ExposedControls::ALL {
ui.selectable_value(&mut material.controls, controls, tr(controls.label()));
}
});
ui.end_row();
ui.label("");
let preview = ui.checkbox(&mut material.include_preview, tr("Include preview.png"));
crate::widgets::name(&preview, egui::Role::CheckBox, "Include preview.png");
ui.end_row();
if tab == ExportTab::MaterialX {
super::form_label(ui, tr("Resolution"));
super::size_menu(ui, "Resolution", width, &mut material.size);
ui.end_row();
super::form_label(ui, tr("Textures"));
super::dropdown(ui, "Textures", width, tr(material.texture_depth.label()), |ui| {
for depth in TextureDepth::ALL {
ui.selectable_value(&mut material.texture_depth, depth, tr(depth.label()));
}
});
ui.end_row();
super::form_label(ui, tr("Source normals"));
let conventions = [(None, "Automatic"), (Some(false), "OpenGL (Y+)"), (Some(true), "DirectX (Y-)")];
let label = conventions.iter().find(|(value, _)| *value == material.normal_y_down).map_or("Automatic", |(_, label)| *label);
super::dropdown(ui, "Source normals", width, tr(label), |ui| {
for (value, label) in conventions {
ui.selectable_value(&mut material.normal_y_down, value, tr(label));
}
});
ui.end_row();
super::form_label(ui, tr("Displacement scale"));
ui.add(egui::DragValue::new(&mut material.displacement_scale).speed(0.01).range(0.0..=100.0));
ui.end_row();
}
});
ui.add_space(6.0);
ui.label(RichText::new(tr("Outputs")).strong());
let graph = app.session.graph().ok().filter(|graph| graph.function.is_none());
outputs::tree(ui, settings, graph.as_slice());
ui.add_space(4.0);
ui.label(RichText::new(tr("Publishing arrives with the new .mcmat format (#190); these options are kept for it")).size(11.0).color(tokens.text_dim));
if tab == ExportTab::MaterialX {
let graph = app.session.pkg.graph_by_name(&material.root).map(|(_, g)| g);
outputs::tree(ui, settings, graph.as_slice());
}
}
/// A folder for MaterialX, an archive path for MCMAT
#[cfg(not(target_arch = "wasm32"))]
fn ask_destination(app: &mut MaterialCraftApp, tab: ExportTab) {
fn ask_destination(app: &mut MaterialCraftApp, tab: ExportTab, root: &str) {
use materialcraft_engine::formats::material::MATERIAL_EXT;
let dialog = if tab == ExportTab::Mcmat {
let graph = app.session.graph().map(|graph| graph.name.clone()).unwrap_or_default();
let file = format!("{}.{MATERIAL_EXT}", materialcraft_engine::formats::safe_file_name(&graph));
let graph = root.rsplit('/').next().unwrap_or(root);
let file = format!("{}.{MATERIAL_EXT}", materialcraft_engine::formats::safe_file_name(graph));
craft_dialogs::FileDialog::save().file_name(file).filter("MaterialCraft material", &[MATERIAL_EXT])
} else {
craft_dialogs::FileDialog::folder()
+154 -27
View File
@@ -1,4 +1,4 @@
//! The Export window: image files from the open graph or a batch of graphs, and the #190 MaterialX and MCMAT options
//! Export graph outputs, bake MaterialX textures, or publish a procedural MCMAT.
mod export_format;
mod export_settings;
@@ -7,10 +7,12 @@ mod export_tests;
mod material_export_settings;
mod material_tab;
mod outputs;
mod texture_depth;
pub use export_format::ExportFormat;
pub use export_settings::{BatchExportSettings, ExportSettings, ExportTab, output_key};
pub use material_export_settings::{ExposedControls, MaterialExportSettings, NormalOrientation, TextureDepth};
pub use material_export_settings::MaterialExportSettings;
pub use texture_depth::TextureDepth;
use egui::{RichText, Ui, vec2};
use egui_phosphor::regular as icon;
@@ -37,6 +39,10 @@ enum Action {
/// Open the Export window on `tab`
pub(crate) fn open(app: &mut MaterialCraftApp, tab: ExportTab) {
let mut draft = app.ui.export.clone();
draft.material.root = app.session.graph().ok().filter(|g| g.function.is_none()).map(Graph::sbs_path).unwrap_or_default();
draft.material.document = app.session.has_package().then_some(app.session.document_id);
app.ui.export_draft = Some(draft);
app.ui.export_tab = tab;
app.ui.windows.export = true;
}
@@ -95,11 +101,12 @@ fn tabs(ui: &mut Ui, current: &mut ExportTab) {
}
fn from_graph(app: &mut MaterialCraftApp, ui: &mut Ui, settings: &mut ExportSettings) {
let graph = app.session.graph().ok().filter(|graph| graph.function.is_none()).cloned();
image_options(app, ui, settings, graph.as_ref());
image_options(app, ui, settings);
let graph = app.session.graph().ok().filter(|graph| graph.function.is_none());
ui.add_space(6.0);
ui.label(RichText::new(tr("Outputs")).strong());
outputs::tree(ui, settings, graph.as_slice().iter().collect::<Vec<_>>().as_slice());
outputs::tree(ui, settings, graph.as_slice());
all_or_none(ui, settings, graph.as_slice());
ui.add_space(4.0);
@@ -113,11 +120,12 @@ fn from_graph(app: &mut MaterialCraftApp, ui: &mut Ui, settings: &mut ExportSett
}
fn batch(app: &mut MaterialCraftApp, ui: &mut Ui, settings: &mut ExportSettings) {
let graphs = material_graphs(app);
image_options(app, ui, settings, graphs.first());
image_options(app, ui, settings);
let graphs: Vec<_> = app.session.pkg.graphs.iter().filter(|graph| graph.function.is_none()).collect();
ui.add_space(6.0);
ui.label(RichText::new(tr("Outputs")).strong());
outputs::tree(ui, settings, graphs.iter().collect::<Vec<_>>().as_slice());
outputs::tree(ui, settings, &graphs);
all_or_none(ui, settings, &graphs);
ui.add_space(4.0);
@@ -143,8 +151,14 @@ fn batch(app: &mut MaterialCraftApp, ui: &mut Ui, settings: &mut ExportSettings)
}
/// Destination, format, bit depth, file name pattern and its preview
fn image_options(app: &mut MaterialCraftApp, ui: &mut Ui, settings: &mut ExportSettings, preview_graph: Option<&Graph>) {
fn image_options(app: &mut MaterialCraftApp, ui: &mut Ui, settings: &mut ExportSettings) {
let preview_graph = if app.ui.export_tab == ExportTab::Batch {
app.session.pkg.graphs.iter().find(|graph| graph.function.is_none())
} else {
app.session.graph().ok().filter(|graph| graph.function.is_none())
};
let preview = preview_graph.and_then(|graph| preview_file(settings, graph)).unwrap_or_default();
form(ui, "export-image-options", |ui, width| {
#[cfg(not(target_arch = "wasm32"))]
{
@@ -155,9 +169,6 @@ fn image_options(app: &mut MaterialCraftApp, ui: &mut Ui, settings: &mut ExportS
}
ui.end_row();
}
#[cfg(target_arch = "wasm32")]
let _ = &app;
form_label(ui, tr("Format"));
let selected = ExportFormat::find(&settings.format).map_or_else(|| settings.format.clone(), ExportFormat::title);
dropdown(ui, "Format", width, selected, |ui| {
@@ -235,12 +246,13 @@ pub(super) fn path_field(ui: &mut Ui, path: &mut String, width: f32) -> bool {
/// The file name the first checked output of `graph` is written to
fn preview_file(settings: &ExportSettings, graph: &Graph) -> Option<String> {
let output = outputs::listed(graph).into_iter().find(|output| settings.includes(&graph.name, &output.identifier))?;
let graph_path = graph.sbs_path();
let output = outputs::listed(graph).find(|output| settings.includes(&graph_path, &output.identifier))?;
let name = materialcraft_engine::formats::output_name(&settings.pattern, &graph.name, &output.identifier, output.usage);
Some(format!("{name}.{}", settings.format))
}
fn all_or_none(ui: &mut Ui, settings: &mut ExportSettings, graphs: &[Graph]) {
fn all_or_none(ui: &mut Ui, settings: &mut ExportSettings, graphs: &[&Graph]) {
ui.horizontal(|ui| {
for (label, include) in [("All", true), ("None", false)] {
if ui.add(egui::Button::new(tr(label)).min_size(vec2(100.0, 0.0))).clicked() {
@@ -284,22 +296,10 @@ fn footer(ui: &mut Ui, tab: ExportTab) -> Option<Action> {
action
}
/// The package's material graphs, in package order
fn material_graphs(app: &MaterialCraftApp) -> Vec<Graph> {
app.session.pkg.graphs.iter().filter(|graph| graph.function.is_none()).cloned().collect()
}
/// Export what the current tab describes; true when the window can close
fn export(app: &mut MaterialCraftApp, settings: &ExportSettings) -> bool {
match app.ui.export_tab {
ExportTab::MaterialX => {
app.toast(tr("MaterialX export isn't available yet (#190)"), true);
false
}
ExportTab::Mcmat => {
app.toast(tr(".mcmat export isn't available yet (#190)"), true);
false
}
ExportTab::MaterialX | ExportTab::Mcmat => publish(app, settings),
ExportTab::FromGraph => {
let graph = app.session.graph;
export_images(app, settings, &[graph], Value::Null)
@@ -320,6 +320,7 @@ fn export_images(app: &mut MaterialCraftApp, settings: &ExportSettings, graphs:
app.toast(crate::i18n::trf("{format} export isn't available yet", &[("format", &format.title())]), true);
return false;
}
if settings.destination.trim().is_empty() && !cfg!(target_arch = "wasm32") {
app.toast(tr("Choose a folder to export to"), true);
return false;
@@ -356,3 +357,129 @@ fn export_images(app: &mut MaterialCraftApp, settings: &ExportSettings, graphs:
true
}
}
fn publish(app: &mut MaterialCraftApp, settings: &ExportSettings) -> bool {
let material = &settings.material;
// Keep the draft tied to its package because graph paths can repeat across documents
if !app.session.has_package() || material.document != Some(app.session.document_id) {
app.toast(tr("The export package changed. Reopen the Export window"), true);
return false;
}
let Some((index, graph)) = app.session.pkg.graph_by_name(&material.root) else {
app.toast(tr("Choose a root graph"), true);
return false;
};
let is_archive = app.ui.export_tab == ExportTab::Mcmat;
let (command, params) = if is_archive {
("file.exportMaterial", json!({"graph":index,"path":material.archive}))
} else {
let Some((outputs, color_spaces)) = settings.selection(graph) else {
app.toast(tr("Check at least one output to export"), true);
return false;
};
let depth = match material.texture_depth {
TextureDepth::Automatic => None,
TextureDepth::Eight => Some(8),
TextureDepth::Sixteen => Some(16),
};
(
"file.export",
json!({"graph":index,"dir":material.folder,"profile":"materialx","size":material.size,"depth":depth,"outputs":outputs,
"colorSpaces":color_spaces,"displacementScale":material.displacement_scale,"normalFormat":material.normal_y_down.map(|down| if down { "directx" } else { "opengl" })}),
)
};
#[cfg(not(target_arch = "wasm32"))]
{
if app.material_export_job.is_some() {
app.toast(tr("A material export is running"), true);
return false;
}
if (if is_archive { &material.archive } else { &material.folder }).trim().is_empty() {
app.toast(tr("Choose an export destination"), true);
return false;
}
let package = app.session.pkg.clone();
let environment = app.session.environment;
let (sender, receiver) = std::sync::mpsc::channel();
let worker = std::thread::Builder::new().name("materialcraft-publish".into()).spawn(move || {
let mut session = materialcraft_engine::Session::new(package);
session.environment = environment;
let result = materialcraft_engine::execute(&mut session, command, &params).map_err(|e| e.to_string());
let _ = sender.send(result);
});
match worker {
Ok(_) => {
app.material_export_job = Some(receiver);
app.toast(tr("Exporting material"), false);
true
}
Err(error) => {
app.toast(error.to_string(), true);
false
}
}
}
#[cfg(target_arch = "wasm32")]
{
let result = if command == "file.exportMaterial" {
materialcraft_engine::material_cmds::material_bytes(&app.session, index).map_err(|e| e.to_string()).and_then(|bytes| {
let name = format!("{}.mcmat", materialcraft_engine::formats::safe_file_name(&graph.name));
crate::web_files::download(&name, &bytes).map(|()| Value::Null)
})
} else {
materialcraft_engine::material_bake::export(&app.session, &params, materialcraft_engine::material_bake::read_input)
.map_err(|e| e.to_string())
.and_then(|(files, report)| {
let files = files.into_iter().map(|f| (f.file, f.bytes)).collect::<Vec<_>>();
crate::web_files::download(
&format!("{}_materialx.zip", materialcraft_engine::formats::safe_file_name(&graph.name)),
&crate::web_files::zip_files(&files)?,
)
.map(|()| report)
})
};
match result {
Ok(report) => {
app.toast(material_export_message(&report), false);
true
}
Err(error) => {
app.toast(error, true);
false
}
}
}
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn poll(app: &mut MaterialCraftApp, ctx: &egui::Context) {
let Some(job) = &app.material_export_job else { return };
let result = match job.try_recv() {
Ok(result) => result,
Err(std::sync::mpsc::TryRecvError::Empty) => {
ctx.request_repaint_after(std::time::Duration::from_millis(100));
return;
}
Err(error) => Err(error.to_string()),
};
app.material_export_job = None;
match result {
Ok(report) => {
app.toast(material_export_message(&report), false);
}
Err(error) => app.toast(error, true),
}
}
/// Keep native and browser exports explicit about omitted shader connections.
fn material_export_message(report: &Value) -> String {
let unmapped = report["unmapped"].as_array().into_iter().flatten().filter_map(Value::as_str).collect::<Vec<_>>();
if unmapped.is_empty() { tr("Material exported").to_string() } else { format!("{}: {}", tr("Exported without shader connections"), unmapped.join(", ")) }
}
+35 -20
View File
@@ -20,13 +20,12 @@ pub struct ListedOutput {
}
/// `graph`'s output nodes in node order
pub fn listed(graph: &Graph) -> Vec<ListedOutput> {
pub fn listed(graph: &Graph) -> impl Iterator<Item = ListedOutput> {
graph
.nodes
.iter()
.filter(|node| node.def == "output")
.map(|node| ListedOutput { identifier: materialcraft_engine::eval::identifier(node), usage: materialcraft_engine::eval::output_usage(node) })
.collect()
}
impl ExportSettings {
@@ -34,14 +33,16 @@ impl ExportSettings {
!self.skipped_outputs.contains(&output_key(graph, identifier))
}
/// The chosen color space or the graph usage's default before format-specific encoding.
pub fn color_space(&self, graph: &str, output: &ListedOutput) -> ColorSpace {
self.color_spaces.get(&output_key(graph, &output.identifier)).copied().unwrap_or_else(|| ColorSpace::for_usage(output.usage))
}
/// Check or uncheck every output of `graph`
pub fn include_all(&mut self, graph: &Graph, include: bool) {
let graph_path = graph.sbs_path();
for output in listed(graph) {
let key = output_key(&graph.name, &output.identifier);
let key = output_key(&graph_path, &output.identifier);
if include {
self.skipped_outputs.remove(&key);
} else {
@@ -52,16 +53,22 @@ impl ExportSettings {
/// The `file.export` `outputs` and `colorSpaces` for `graph`; `None` when none of its outputs is checked
pub fn selection(&self, graph: &Graph) -> Option<(Value, Value)> {
let outputs = listed(graph);
let included: Vec<&ListedOutput> = outputs.iter().filter(|output| self.includes(&graph.name, &output.identifier)).collect();
let graph_path = graph.sbs_path();
let outputs: Vec<_> = listed(graph).collect();
let included: Vec<&ListedOutput> = outputs.iter().filter(|output| self.includes(&graph_path, &output.identifier)).collect();
if included.is_empty() {
return None;
}
let identifiers =
if included.len() == outputs.len() { Value::Null } else { json!(included.iter().map(|output| &output.identifier).collect::<Vec<_>>()) };
let color_spaces: serde_json::Map<String, Value> =
included.iter().map(|output| (output.identifier.clone(), json!(self.color_space(&graph.name, output).id()))).collect();
let color_spaces: serde_json::Map<String, Value> = included
.iter()
.filter_map(|output| {
let space = self.color_spaces.get(&output_key(&graph_path, &output.identifier))?;
Some((output.identifier.clone(), json!(space.id())))
})
.collect();
Some((identifiers, Value::Object(color_spaces)))
}
}
@@ -76,9 +83,10 @@ pub fn tree(ui: &mut Ui, settings: &mut ExportSettings, graphs: &[&Graph]) {
egui::ScrollArea::vertical().id_salt("export-outputs").max_height(240.0).auto_shrink([false, true]).show(ui, |ui| {
for graph in graphs {
let outputs = listed(graph);
let included = outputs.iter().filter(|output| settings.includes(&graph.name, &output.identifier)).count();
let id = ui.make_persistent_id(("export-graph", &graph.name));
let graph_path = graph.sbs_path();
let outputs: Vec<_> = listed(graph).collect();
let included = outputs.iter().filter(|output| settings.includes(&graph_path, &output.identifier)).count();
let id = ui.make_persistent_id(("export-graph", &graph_path));
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
.show_header(ui, |ui| {
let mut all = !outputs.is_empty() && included == outputs.len();
@@ -91,21 +99,21 @@ pub fn tree(ui: &mut Ui, settings: &mut ExportSettings, graphs: &[&Graph]) {
ui.add_space((OUTPUT_COLUMN_WIDTH + indent + 16.0 - ui.min_rect().width()).max(8.0));
ui.label(RichText::new(tr("Exported color space")).color(tokens.text_dim));
})
.body(|ui| outputs_grid(ui, settings, graph, &outputs));
.body(|ui| outputs_grid(ui, settings, &graph_path, &outputs));
}
});
}
fn outputs_grid(ui: &mut Ui, settings: &mut ExportSettings, graph: &Graph, outputs: &[ListedOutput]) {
fn outputs_grid(ui: &mut Ui, settings: &mut ExportSettings, graph_path: &str, outputs: &[ListedOutput]) {
if outputs.is_empty() {
ui.label(RichText::new(tr("This graph has no outputs")).color(Tokens::get(ui.ctx()).text_dim));
return;
}
egui::Grid::new(("export-outputs-grid", &graph.name)).num_columns(2).min_col_width(OUTPUT_COLUMN_WIDTH).spacing([8.0, 4.0]).show(ui, |ui| {
egui::Grid::new(("export-outputs-grid", graph_path)).num_columns(2).min_col_width(OUTPUT_COLUMN_WIDTH).spacing([8.0, 4.0]).show(ui, |ui| {
for output in outputs {
let key = output_key(&graph.name, &output.identifier);
let mut included = settings.includes(&graph.name, &output.identifier);
let key = output_key(graph_path, &output.identifier);
let mut included = !settings.skipped_outputs.contains(&key);
let checkbox = ui.checkbox(&mut included, &output.identifier);
crate::widgets::name(&checkbox, egui::Role::CheckBox, &output.identifier);
if checkbox.changed() {
@@ -116,15 +124,22 @@ fn outputs_grid(ui: &mut Ui, settings: &mut ExportSettings, graph: &Graph, outpu
}
}
let mut space = settings.color_space(&graph.name, output);
let combo = egui::ComboBox::from_id_salt(("export-color-space", &key)).width(110.0).selected_text(space.label()).show_ui(ui, |ui| {
let original_space = settings.color_spaces.get(&key).copied();
let mut space = original_space;
let label = space.map_or_else(|| tr("Automatic"), ColorSpace::label);
let combo = egui::ComboBox::from_id_salt(("export-color-space", &key)).width(110.0).selected_text(label).show_ui(ui, |ui| {
ui.selectable_value(&mut space, None, tr("Automatic"));
for choice in ColorSpace::ALL {
ui.selectable_value(&mut space, choice, choice.label());
ui.selectable_value(&mut space, Some(choice), choice.label());
}
});
crate::widgets::name(&combo.response, egui::Role::ComboBox, &format!("{} {}", output.identifier, tr("color space")));
if space != settings.color_space(&graph.name, output) {
settings.color_spaces.insert(key, space);
if space != original_space {
if let Some(space) = space {
settings.color_spaces.insert(key, space);
} else {
settings.color_spaces.remove(&key);
}
}
ui.end_row();
}
@@ -0,0 +1,24 @@
use serde::{Deserialize, Serialize};
/// PNG precision of the published textures; HDR and signed data always use lossless EXR
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum TextureDepth {
/// 16 bits for data textures and 8 bits for color
#[default]
Automatic,
Eight,
Sixteen,
}
impl TextureDepth {
pub const ALL: [TextureDepth; 3] = [TextureDepth::Automatic, TextureDepth::Eight, TextureDepth::Sixteen];
pub fn label(self) -> &'static str {
match self {
TextureDepth::Automatic => "Automatic (16-bit data, 8-bit color)",
TextureDepth::Eight => "8 bits",
TextureDepth::Sixteen => "16 bits",
}
}
}
+10 -4
View File
@@ -4,7 +4,6 @@ pub const TABLE: &[(&str, &str)] = &[
("$pos", "$pos"),
("$size", "$size"),
("(checker)", "(رقعة شطرنج)"),
(".mcmat export isn't available yet (#190)", "تصدير .mcmat غير متاح بعد (#190)"),
("1024 px", "1024 بكسل"),
("16 bits", "16 بت"),
("16 bits per channel", "16 بت لكل قناة"),
@@ -74,7 +73,8 @@ pub const TABLE: &[(&str, &str)] = &[
("Atomic nodes", "عقد أولية"),
("Attributes", "السمات"),
("Auto backup", "النسخ الاحتياطي التلقائي"),
("Automatic (16 bits for normal and height)", "تلقائي (16 بت للناظم والارتفاع)"),
("Automatic", "تلقائي"),
("Automatic (16-bit data, 8-bit color)", "تلقائي (16 بت للبيانات، 8 بت للألوان)"),
("Automatic Clip Planes", "مستويات قطع تلقائية"),
("Automatic export when outputs change", "تصدير تلقائي عند تغيّر المخرجات"),
("Automation documentation...", "توثيق الأتمتة..."),
@@ -124,6 +124,8 @@ pub const TABLE: &[(&str, &str)] = &[
("Check at least one output to export", "حدّد مخرجًا واحدًا على الأقل للتصدير"),
("Check for updates...", "التحقق من وجود تحديثات..."),
("Choose a folder to export to", "اختر مجلداً للتصدير إليه"),
("Choose a root graph", "اختر الرسم البياني الجذري"),
("Choose an export destination", "اختر وجهة التصدير"),
("Choose...", "اختيار..."),
("Clamp", "تقييد النطاق (Clamp)"),
("Clear cache", "مسح الذاكرة المؤقتة"),
@@ -242,6 +244,9 @@ pub const TABLE: &[(&str, &str)] = &[
("Export outputs... (Ctrl+Shift+E)", "تصدير المخرجات... (Ctrl+Shift+E)"),
("Export settings saved", "حُفظت إعدادات التصدير"),
("Export Tessellated Geometry...", "تصدير الهندسة المقسّمة..."),
("A material export is running", "جارٍ تصدير مادة"),
("Exported without shader connections", "تم التصدير دون توصيلات للمظلّل"),
("Exporting material", "جارٍ تصدير المادة"),
("Exported", "تم التصدير"),
("Exported color space", "فضاء الألوان المُصدَّر"),
("Expose as graph input", "كشف كمدخل للمخطط"),
@@ -371,11 +376,11 @@ pub const TABLE: &[(&str, &str)] = &[
("Luminance", "النصوع"),
("Main toolbar", "شريط الأدوات الرئيسي"),
("Mat ball", "كرة المادة"),
("Material exported", "تم تصدير المادة"),
("Material", "المادة"),
("Material filters", "مرشحات المواد"),
("Material graph", "مخطط مادة"),
("Materials", "المواد"),
("MaterialX export isn't available yet (#190)", "تصدير MaterialX غير متاح بعد (#190)"),
("Maximum", "الحد الأقصى"),
("Maximum stored previews", "أقصى عدد للمعاينات المخزنة"),
("Medium (4 spp)", "متوسط (4 عينات/بكسل)"),
@@ -501,7 +506,6 @@ pub const TABLE: &[(&str, &str)] = &[
("Projection", "الإسقاط"),
("Projects", "المشاريع"),
("Properties", "الخصائص"),
("Publishing arrives with the new .mcmat format (#190); these options are kept for it", "يصل النشر مع صيغة .mcmat الجديدة (#190)؛ وتُحفظ هذه الخيارات لها"),
("Radius", "نصف القطر"),
("Radius scale", "مقياس نصف القطر"),
("Raise the mesh by the height output", "رفع المجسم بمخرج الارتفاع"),
@@ -548,6 +552,7 @@ pub const TABLE: &[(&str, &str)] = &[
("Reset scene", "إعادة ضبط المشهد"),
("Reset Selected", "إعادة ضبط المحدد"),
("Reset to default", "إعادة ضبط للافتراضي"),
("Root graph", "الرسم البياني الجذري"),
("Resolution", "الدقة"),
("Resources", "الموارد"),
("Revert", "استرجاع"),
@@ -623,6 +628,7 @@ pub const TABLE: &[(&str, &str)] = &[
("Sine", "جيب الزاوية (Sine)"),
("Size", "الحجم"),
("Snap to grid", "المحاذاة إلى الشبكة"),
("Source normals", "اتجاه نواظم المصدر"),
("Specific parameters", "المعلمات المخصصة"),
("Specular", "الانعكاس"),
("Specular Glossiness", "انعكاس ولمعان"),
+7
View File
@@ -54,6 +54,8 @@ pub struct MaterialCraftApp {
/// Native file dialogs (desktop only)
#[cfg(not(target_arch = "wasm32"))]
pub(crate) dialogs: crate::native_dialogs::Dialogs,
#[cfg(not(target_arch = "wasm32"))]
pub(crate) material_export_job: Option<std::sync::mpsc::Receiver<Result<Value, String>>>,
/// The held action waiting for the Save dialog the unsaved-changes prompt opened
pub(crate) awaiting_save: Option<crate::unsaved::Guarded>,
/// The window buttons to draw when the app draws the window frame; `None` while the desktop draws it (desktop only)
@@ -130,10 +132,13 @@ impl MaterialCraftApp {
updates_rt: crate::updates_ui::Runtime::default(),
#[cfg(not(target_arch = "wasm32"))]
dialogs: crate::native_dialogs::unattached(),
#[cfg(not(target_arch = "wasm32"))]
material_export_job: None,
awaiting_save: None,
#[cfg(not(target_arch = "wasm32"))]
window_layout: None,
keymap: menus::keymap(craft_keymap::Platform::current()),
// Accept clipboard events without duplicating web key commands
dispatcher: craft_keymap::egui::Dispatcher::new().with_repeat(true).with_clipboard_events(true),
system_clipboard: None,
@@ -509,6 +514,8 @@ impl MaterialCraftApp {
web_files::poll(self);
#[cfg(not(target_arch = "wasm32"))]
crate::native_dialogs::poll(self);
#[cfg(not(target_arch = "wasm32"))]
crate::export::poll(self, ctx);
if !crate::shell::modality::Modality::capture(self, ctx).is_blocked() {
for f in ctx.input(|i| i.raw.dropped_files.clone()) {
web_files::dropped(self, ctx, &f);
+5 -3
View File
@@ -485,12 +485,14 @@ fn a_saved_package_goes_to_the_document_that_asked() {
let second = app.session.document_id;
assert_ne!(first, second, "a second package is open");
let path = temp("first.sbs");
// Keep adjacent SBS resources in an exclusive directory
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("first.sbs");
let saved = save_to(&mut app, first, false, &path.display().to_string());
assert!(saved);
assert!(saved, "{}", app.toasts.last().map_or("no error toast", |toast| toast.text.as_str()));
assert_eq!(app.session.document_id, first, "the asking package is active again");
assert_eq!(app.session.path.as_deref().map(std::path::PathBuf::from), std::fs::canonicalize(&path).ok());
std::fs::remove_file(path).ok();
}
#[test]
+6 -23
View File
@@ -206,8 +206,6 @@ pub use web::*;
mod web {
use std::sync::PoisonError;
use wasm_bindgen::JsCast as _;
use super::{INBOX, Pick, zip_files};
use crate::MaterialCraftApp;
use crate::i18n::tr;
@@ -320,27 +318,12 @@ mod web {
/// Start a browser download of `bytes` named `name` (Blob + a temporary `<a download>`)
pub fn download(name: &str, bytes: &[u8]) -> Result<(), String> {
let js = |e: wasm_bindgen::JsValue| format!("{e:?}");
let window = web_sys::window().ok_or("no window")?;
let document = window.document().ok_or("no document")?;
let parts = js_sys::Array::of1(&js_sys::Uint8Array::from(bytes));
let opts = web_sys::BlobPropertyBag::new();
opts.set_type(if name.ends_with(".zip") { "application/zip" } else { "application/octet-stream" });
let blob = web_sys::Blob::new_with_u8_array_sequence_and_options(&parts, &opts).map_err(js)?;
let url = web_sys::Url::create_object_url_with_blob(&blob).map_err(js)?;
let a: web_sys::HtmlAnchorElement = document.create_element("a").map_err(js)?.dyn_into().map_err(|_| "not an anchor")?;
a.set_href(&url);
a.set_download(name);
a.style().set_property("display", "none").map_err(js)?;
document.body().ok_or("no body")?.append_child(&a).map_err(js)?;
a.click();
a.remove();
// Revoke once the click has been dispatched; the download keeps its own reference
let revoke = wasm_bindgen::closure::Closure::once_into_js(move || {
web_sys::Url::revoke_object_url(&url).ok();
});
window.set_timeout_with_callback_and_timeout_and_arguments_0(revoke.unchecked_ref(), 10_000).map_err(js)?;
Ok(())
use craft_platform::browser::{DownloadOptions, download_bytes};
// Allow the browser to acquire the Blob after the click
const DOWNLOAD_URL_LIFETIME_MS: i32 = 10_000;
let mime = if name.ends_with(".zip") { "application/zip" } else { "application/octet-stream" };
download_bytes(name, mime, bytes, DownloadOptions { revoke_after_ms: DOWNLOAD_URL_LIFETIME_MS }).map_err(|error| format!("{error:?}"))
}
}
+4 -3
View File
@@ -260,8 +260,8 @@ pub fn function_summary(app: &mut MaterialCraftApp, ui: &mut Ui, node: NodeId, p
});
}
/// Project formats offered by File ▸ Open.
pub const OPEN_EXTS: &[&str] = &["sbs", "mcraft"];
/// Authoring and published material formats offered by File ▸ Open.
pub const OPEN_EXTS: &[&str] = &["sbs", "mcraft", "mcmat"];
pub(crate) fn package_filters(open: bool, archive: bool) -> Vec<(&'static str, &'static [&'static str])> {
let mut filters: Vec<(&str, &[&str])> = vec![("SBS XML Package (*.sbs)", &["sbs"]), ("MaterialCraft Project (*.mcraft)", &["mcraft"])];
@@ -269,7 +269,8 @@ pub(crate) fn package_filters(open: bool, archive: bool) -> Vec<(&'static str, &
filters.reverse();
}
if open {
filters.insert(0, ("All formats (*.sbs *.mcraft)", OPEN_EXTS));
filters.push(("MaterialCraft Material (*.mcmat)", &["mcmat"]));
filters.insert(0, ("All formats (*.sbs *.mcraft *.mcmat)", OPEN_EXTS));
}
filters
}
+4 -2
View File
@@ -130,6 +130,7 @@ mod import_tests {
assert_eq!(app.session.history().len(), base);
}
}
#[test]
fn project_dialogs_offer_separate_formats_and_import_filters_show_full_names() {
use super::{package_filters, resource_filters};
@@ -140,8 +141,9 @@ fn project_dialogs_offer_separate_formats_and_import_filters_show_full_names() {
let opened = package_filters(true, false);
assert_eq!(opened[0], ("All formats (*.sbs *.mcraft)", &["sbs", "mcraft"][..]));
assert_eq!(&opened[1..], &saved);
assert_eq!(opened[0], ("All formats (*.sbs *.mcraft *.mcmat)", &["sbs", "mcraft", "mcmat"][..]));
assert_eq!(&opened[1..3], &saved);
assert_eq!(opened[3].1, &["mcmat"]);
let bitmap = resource_filters(false);
+55 -2
View File
@@ -25,7 +25,8 @@ fn open_from_the_graph_menu(harness: &mut Harness<'static, MaterialCraftApp>, it
let graph = harness.state().session.graph().unwrap().name.clone();
harness.get(By::new().role(Role::TreeItem).label(graph.as_str())).click_secondary();
harness.run_steps(3);
// The Explorer toolbar has an Export outputs button too; the menu opens after it
// The menu item follows the identically named toolbar button
harness.get_all(By::new().role(Role::Button).label(item)).last().unwrap().click();
harness.run_steps(3);
}
@@ -41,7 +42,7 @@ fn the_graph_menu_opens_the_window_on_the_matching_tab() {
open_from_the_graph_menu(&mut harness, "Export material...");
assert!(harness.state().ui.windows.export);
assert_eq!(harness.state().ui.export_tab, ExportTab::Mcmat);
assert!(harness.query(By::new().role(Role::ComboBox).label("Normal orientation")).is_some(), "the #190 options are listed");
assert!(harness.query(By::new().role(Role::ComboBox).label("Root graph")).is_some(), "the publishing dialog identifies its root graph");
harness.get(By::new().label("Close")).click();
harness.run_steps(3);
@@ -95,3 +96,55 @@ fn checking_outputs_in_the_tree_updates_the_window_copy() {
harness.run_steps(2);
assert!(harness.state().ui.export_draft.as_ref().unwrap().skipped_outputs.is_empty(), "the graph's checkbox checks all its outputs");
}
#[test]
fn output_color_can_be_explicit_or_automatic() {
let mut harness = harness();
open_from_the_graph_menu(&mut harness, "Export outputs...");
let graph = harness.state().session.graph().unwrap().sbs_path();
let key = materialcraft_ui_egui::export::output_key(&graph, "height");
harness.get(By::new().role(Role::ComboBox).label("height color space")).click();
harness.run_steps(2);
harness.get_by_label("Raw").click();
harness.run_steps(2);
let draft = harness.state().ui.export_draft.as_ref().unwrap();
assert_eq!(draft.color_spaces.get(&key), Some(&materialcraft_engine::ColorSpace::Raw));
harness.get(By::new().role(Role::ComboBox).label("height color space")).click();
harness.run_steps(2);
harness.get_by_label("Automatic").click();
harness.run_steps(2);
let draft = harness.state().ui.export_draft.as_ref().unwrap();
assert!(!draft.color_spaces.contains_key(&key));
}
#[test]
fn publishes_the_graph_chosen_in_the_dialog() {
let folder = tempfile::tempdir().unwrap();
let archive = folder.path().join("picked.mcmat");
let mut harness = harness();
let mut other = harness.state().session.graph().unwrap().clone();
other.name = "Chosen".into();
other.folder = "Materials".into();
harness.state_mut().session.pkg.graphs.push(other);
harness.run_steps(5);
open_from_the_graph_menu(&mut harness, "Export material...");
harness.get(By::new().role(Role::ComboBox).label("Root graph")).click();
harness.run_steps(2);
harness.get_by_label("Materials/Chosen").click();
harness.run_steps(2);
harness.state_mut().ui.export_draft.as_mut().unwrap().material.archive = archive.to_string_lossy().into_owned();
harness.get(By::new().role(Role::Button).label("Export")).click();
harness.run_steps(3);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
while !archive.exists() && std::time::Instant::now() < deadline {
harness.run_steps(1);
std::thread::sleep(std::time::Duration::from_millis(10));
}
let (package, entry) = materialcraft_engine::formats::material::read_material(&std::fs::read(archive).unwrap()).unwrap();
assert_eq!(package.graph(entry).unwrap().sbs_path(), "Chosen");
assert_ne!(harness.state().session.graph().unwrap().name, "Chosen");
}
+19 -5
View File
@@ -8,7 +8,12 @@ use egui_kittest::Harness;
use materialcraft_ui_egui::{MaterialCraftApp, UiState};
fn harness(w: f32, h: f32) -> Harness<'static, MaterialCraftApp> {
let app = MaterialCraftApp::headless(materialcraft_engine::sample_session(), UiState::default());
let mut app = MaterialCraftApp::headless(materialcraft_engine::sample_session(), UiState::default());
// Retain real background rendering with fewer pixels to test resize scheduling under coverage
app.session.view3d.override_resolution = true;
app.session.view3d.viewport_resolution = true;
app.session.view3d.render_scale = 25;
Harness::builder().with_size(vec2(w, h)).with_pixels_per_point(1.0).build_ui_state(
|ui, app: &mut MaterialCraftApp| {
app.step(&ui.ctx().clone());
@@ -24,7 +29,7 @@ fn settle(h: &mut Harness<'static, MaterialCraftApp>) {
h.step();
let app = h.state();
if !app.cooker.busy() && !app.session.dirty && !app.view3d.busy() && app.view3d.renders > 0 {
// A few more frames: the settle delay may still be running after the last change.
// Allow the resize settle delay to elapse after the last change
for _ in 0..20 {
h.step();
}
@@ -34,7 +39,14 @@ fn settle(h: &mut Harness<'static, MaterialCraftApp>) {
}
std::thread::sleep(Duration::from_millis(20));
}
panic!("the 3D view never settled");
let app = h.state();
panic!(
"the 3D view never settled: cooker={}, dirty={}, view_busy={}, renders={}",
app.cooker.busy(),
app.session.dirty,
app.view3d.busy(),
app.view3d.renders
);
}
#[test]
@@ -43,14 +55,16 @@ fn a_resize_does_not_render_the_3d_view_on_every_frame() {
settle(&mut h);
let before = h.state().view3d.renders;
let shown = h.state().view3d.shown.as_ref().map(|(w, ht, _)| (*w, *ht));
// A drag: the window (and with it every panel) gets narrower on each of 40 frames.
// Simulate a continuous window resize
for i in 0..40 {
h.set_size(vec2(1400.0 - 4.0 * i as f32, 860.0));
h.step();
}
assert_eq!(h.state().view3d.renders, before, "the 3D view rendered while it was being resized");
assert!(!h.state().view3d.busy(), "a render started while the view was being resized");
// Released: one full-quality frame at the new size, in the background.
// Expect one background render once the size settles
settle(&mut h);
assert_eq!(h.state().view3d.renders, before + 1);
let now = h.state().view3d.shown.as_ref().map(|(w, ht, _)| (*w, *ht));
+20 -5
View File
@@ -180,14 +180,25 @@ fn explorer_drags_a_linked_graph_between_open_packages() {
harness.state_mut().run("graph.node.param", json!({"id": color.0, "param": "color", "value": [0.9, 0.3, 0.1, 1.0]})).unwrap();
let position = harness.get(By::new().role(egui::accesskit::Role::TreeItem).label("Target canvas")).rect().center();
double_click(&mut harness, position);
settle(&mut harness, 8);
// Wait for the linked graph to be recooked after switching back to its document
let expected_pixel = [0.9, 0.3, 0.1, 1.0];
let linked_color_updated = |harness: &Harness<'static, MaterialCraftApp>| {
harness
.state()
.session
.cooked
.image(instance, 0)
.is_some_and(|image| image.get(0, 0, false).into_iter().zip(expected_pixel).all(|(actual, expected)| (actual - expected).abs() <= 1.0 / 255.0))
};
let deadline = std::time::Instant::now() + Duration::from_secs(6);
while !linked_color_updated(&harness) && std::time::Instant::now() < deadline {
settle(&mut harness, 1);
}
assert_eq!(harness.state().session.document_id, target_document);
assert_eq!(harness.state().session.history(), ["Add graph instance"]);
let value = harness.state().session.cooked.image(instance, 0).unwrap().get(0, 0, false);
assert!(value.into_iter().zip([0.9, 0.3, 0.1, 1.0]).all(|(actual, expected)| (actual - expected).abs() <= 1.0 / 255.0));
assert!(linked_color_updated(&harness), "the linked instance uses the updated source pixel");
assert!(harness.state().session.modified);
save(&mut harness, &directory, "linked-instance-source-edit");
@@ -1672,6 +1683,10 @@ fn explorer_inputs_and_graph_drop_share_labels_and_material_assignment() {
save(&mut harness, &directory, "graph-drop-target");
harness.event(egui::Event::PointerButton { pos: end, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE });
// Process the queued release before testing a frame that may already show the hover preview
harness.step();
let has_expected_material = |harness: &Harness<'static, MaterialCraftApp>| {
harness
.state()
+1 -6
View File
@@ -390,12 +390,7 @@ See [atomic-workflow.md](atomic-workflow.md) for the tested scope and remaining
## Other apps
A `.mcmat` portable material (`formats/src/material/`, `engine/src/material_cmds.rs`) holds a
graph's cooked maps, its portable exposed inputs, presets and input nodes, and the source package.
TextureCraft paints with it and re-cooks it by running `materialcraft-cli run <package>
file.exportMaterial {path, size, values, inputs}`: the apps share the file format and the command
line, never code. Graphs keep preview images for their input nodes (`Graph::input_images`, mesh
maps from TextureCraft); `eval::cook` feeds them to the top-level graph, and a caller's images win.
The MCMAT v3 publisher (`formats/src/material/`) copies a selected graph, its reachable graph and function definitions, and their resource bytes into a single SBS document with local references. The shared `craft-mcmat` crate writes the ZIP. The reader selects the sole root graph by its location. `engine/src/material_bake.rs` evaluates variations through the CPU cook engine. The CLI exposes the same entry for inspection, TextureCraft's labeled map export and the MaterialX OpenPBR export. Supplied images override the graph's saved input previews for that cook.
Preview cooks publish the editor graph and assigned material graphs together. A synchronous cook
invalidates older worker results. Displacement uses the assigned Height source even while another
+30 -32
View File
@@ -38,8 +38,8 @@ to the saving application's release version. Save As and archive packaging retai
| `session.inspect` | all | package, graphs, the open graph's nodes (def, params, bindings, base, ports, cook error/size/mean/ms), wires, frames, comments, pins, exposed inputs, views, history |
| `package.open {path}` / `package.save {path?}` | headless | workspace-scoped package I/O |
| `bitmap.import {path, name?}` | headless | embed an image as a resource |
| `outputs.export {dir, format?, pattern?, depth?, size?, graph?, outputs?, colorSpaces?}` | headless | cook at full size and write the outputs: `size` is the larger side or `[width, height]`, `outputs` lists identifiers (default all), `colorSpaces` maps identifiers to `srgb`, `linear` or `raw` (default by usage) |
| `material.export {path, graph?, size?, values?}` | headless | write a portable `.mcmat` below the write root |
| `outputs.export {dir, format?, pattern?, depth?, size?, graph?, outputs?, colorSpaces?, profile?, preset?, values?, inputs?, normalFormat?, displacementScale?}` | headless | cook at full size and write the outputs: `size` is the larger side or `[width, height]`, `outputs` lists identifiers (default all), `colorSpaces` maps identifiers to `srgb`, `linear` or `raw` (default by usage) |
| `material.export {path, graph?}` | headless | write a portable `.mcmat` below the write root |
| `render {view?, width?, height?, yaw?, pitch?, scene?, node?, port?, full?, tiling?, hq?, displacement?}` | headless | PNG (base64) of the 3D view or of one node; `hq` is the beauty render (displaced mesh, ray-traced AO, shadowed key light, 3×3 supersampling), `displacement` its depth in scene radii |
| `ui.inspect` | app | UI state, the graph view rectangle, every node's screen rectangle, every port's identifier, display label and screen position, fps, toasts |
| `ui.menu.list` / `ui.menu.invoke {id}` | app | the menu catalog; behave like a click. While a dialog or prompt is open (an app dialog, the unsaved-changes prompt, a file dialog, or the update prompt or result window; not Home, the Shortcuts list or the notice that an update installed), an item lists as disabled and `ui.menu.invoke` returns an error. When a file dialog is the only one open, the items that show a file dialog stay enabled and bring it forward |
@@ -350,38 +350,36 @@ includes frame, comment and pin rectangles for synthetic input.
## Portable materials (TextureCraft)
A `.mcmat` portable material is a zip: `material.json` (manifest), `maps/<usage>.png` and
`source.sbs` (the package). TextureCraft applies it as a fill layer and keeps the exposed inputs
editable, re-cooking through `materialcraft-cli`. The format is a convention the two apps share;
each has its own reader and writer (`crates/formats/src/material.rs` here), so keep them in step.
A v3 `.mcmat` publishes a single material graph for procedural cooking. Choose **Export material… → MCMAT → Root graph** or give the graph's full SBS path to the CLI.
```json
{ "format": "craft-material", "version": 2, "name": "Brick wall", "graph": "Brick_wall", "size": [2048, 2048],
"maps": [{"usage": "baseColor", "file": "maps/baseColor.png", "colorSpace": "srgb"}],
"parameters": [{"id": "wear", "label": "Wear", "default": {"t": "float", "v": 0.5}, "min": 0, "max": 1,
"value": {"t": "float", "v": 0.8}}],
"inputs": [{"identifier": "curvature", "kind": "gray"}],
"presets": [{"name": "Worn", "values": {"wear": {"t": "float", "v": 1.0}}}],
"source": "source.sbs" }
The commands below assume `surfaces.sbs` has a connected material graph at `Materials/Oak`, a scalar control named `wear`, a preset named `Worn`, and an image input named `curvature`. Supply a mesh map at `mesh/curvature.exr`.
```sh
materialcraft-cli publish surfaces.sbs Oak.mcmat --graph Materials/Oak
materialcraft-cli info Oak.mcmat
materialcraft-cli cook Oak.mcmat --size 1024 --set wear=0.2
materialcraft-cli export Oak.mcmat build/oak --profile materialx --size 2048 \
--preset Worn --set wear=0.7 --input curvature=mesh/curvature.exr
```
- Usages come from the shared `ComponentUsage` catalog (the first output of each, excluding
`any`). The catalog includes PBR, diffuse/specular, coat, sheen, scattering and user channels.
Color maps are 8-bit sRGB PNGs; data maps are 16-bit linear. Each map records its color space.
Additional usage identifiers do not change the manifest version; consumers may ignore usages
they do not support. Normals are tangent space, OpenGL (Y+); height is 0..1 with 0.5 at the surface.
The 3D preview supports base color (including `diffuse`), normal, roughness, metallic, height,
ambient occlusion, emissive and opacity; other usages remain available for SBS and exports.
- Parameters are the exposed inputs other apps can set (float, float2-4, int, int2, bool, color,
enum), values in the typed form `{"t", "v"}`; colors are linear RGBA.
- Inputs are the graph's input nodes. Mesh maps feed them by identifier: `ambientOcclusion`,
`curvature`, `thickness`, `position`, `worldNormal`, `normal`, `height`, `id`, `bentNormal`,
`opacity`, `colorMap`, `worldDirection`.
- Readers ignore unknown fields and require manifest `version` 2.
The ZIP contains a version marker (`material.json` with `{"version":3}`), a DEFLATE-compressed `source.sbs`, and the source assets its graphs reference under `resources/`. The sole graph at the root of the SBS document is the entry. Reachable helper graphs and functions have local paths under `Dependencies/`. Republish v1 and v2 archives from the original authoring project.
`info` reads the entry's path, dimensions, supported resolutions, typed defaults, saved values, presets, image inputs and output usages. A cook starts from the saved values, applies the named preset, then applies each explicit override. `--set` accepts numbers, booleans, JSON vectors, colors and typed values such as `{"t":"float","v":0.7}`. The size is the longer side in pixels. The graph retains its aspect ratio within the supported powers of two from 16 to 8192. Use `--size 1024x512` to choose both sides. An omitted size uses the authored dimensions. An explicit size also sets the final dimensions of outputs fed by fixed-size nodes.
TextureCraft requests `--profile maps` at its texture set's resolution and supplies mesh maps through repeated `--input identifier=path`. Common identifiers include `ambientOcclusion`, `curvature`, `thickness`, `position`, `worldNormal`, `normal`, `height`, `id`, `bentNormal`, `opacity`, `colorMap` and `worldDirection`. An image override uses the published preview binding’s color space when present. Otherwise the input’s declared usage determines the transfer, with the identifier as a fallback for untagged inputs. The JSON report lists each map's identifier, usage, filename, color space and actual dimensions.
The CLI's `--graph-path` selects a literal SBS path, including a numeric name, while `--graph` retains numeric-index shorthand.
The `materialx` profile writes `material.mtlx` with relative texture paths and an OpenPBR surface. Color textures default to sRGB PNG, data textures to linear 16-bit PNG, and out-of-range values to floating-point EXR. Both bake profiles write tangent-space Y+ normals. A directly connected Normal node supplies its evaluated convention, including through routing dots. Other normal sources default to Y+. Use `--normal-format directx` for a Y− source or `--normal-format opengl` for Y+. OpenPBR scalar and normal inputs require linear data. Height becomes signed EXR displacement in scene units using `(height - 0.5) × displacementScale`. The CLI accepts `--displacement-scale`. MaterialX texture filenames include a hash of the output name and bytes. Re-export writes the document last and keeps earlier texture versions, so a failed export leaves the previous material usable. Outputs without an OpenPBR connection are written alongside the document and listed in `unmapped`.
The named profiles choose their own file names and formats. Use `--format` and `--pattern` with plain image export. `--normal-format` applies to both named profiles, and `--displacement-scale` applies to MaterialX. Profile EXR files are linear. Leave the color space automatic, or choose linear or raw explicitly.
| Command | Params |
|---|---|
| `file.exportMaterial` | `{path, graph?, size? (px), values? {input: value}, inputs? {identifier: image path}}`: cook and write a `.mcmat`. TextureCraft runs `materialcraft-cli run source.sbs file.exportMaterial {...}` to re-cook at its texture set's resolution with its own values and baked mesh maps. In the app, Export material... (and `ui.menu.invoke file.exportMaterial`) opens the Export window on its MCMAT tab, whose #190 options wait for the new exporter |
| `file.exportMaterial` | `{path, graph?}` publishes the chosen source graph by SBS path or index. The default is the open graph. |
| `material.info` | `{graph?}` reads the material interface. |
| `material.cook` | `{graph?, size?, preset?, values?, inputs?}` returns output dimensions and mean values. |
| `file.export` | `{dir, graph?, profile?, size?, preset?, values?, inputs?, normalFormat?, displacementScale?}` bakes images using the selected profile. |
| `file.sendToTextureCraft` | `{graph?}`: export into TextureCraft's library folder (`Documents/TextureCraft/library`), where its shelf lists it |
| `graph.input.bind` / `graph.input.unbind` | `{identifier, resource? \| path?, linear?}` / `{identifier}`: a preview image for an input node (a mesh map exported from TextureCraft), saved in the package |
| `file.open` | `{path}` opens `.sbs` XML or an independent `.mcraft` ZIP. Also opens a `.mcmat`'s source without a save path. Returns `warnings` for unsupported content; also in `session.inspect` as `package.warnings` |
@@ -401,10 +399,10 @@ each has its own reader and writer (`crates/formats/src/material.rs` here), so k
`ui_screenshot`, `ui_inspect`, `ui_menu_invoke`, `ui_input`. Bridged to the app, `package_save`
without a path runs File > Save through `ui.menu.invoke`, so it returns that call's error when a
dialog or prompt blocks it. `package_open` opens `.mcmat` files
too, and `material_export` `{path, graph?, size?, values?}` writes one below `--write-root` (`path` is
relative, the parent folder must exist, `values` are exposed input values). The workspace sandbox
still refuses `file.*` commands, so a sandboxed agent exports through the tool, not `file.exportMaterial`;
mesh-map `inputs` stay a CLI feature.
too. `material_export` accepts `{path, graph?}` and publishes below the write root. Use
`outputs_export` to bake variations with `profile`, `size`, `preset`, `values` and `inputs`.
Image paths are relative to the read root and the destination is relative to the write root.
Workspace publication limits both the combined uncompressed assets and the final archive to 256 MiB.
While an FX-Map subgraph is open, `ui.inspect.subgraph` identifies its owner and selection. Canvas
node IDs and ports refer to that embedded graph. `ui.menu.invoke` with `ui.graph.parent` returns
+1 -2
View File
@@ -36,7 +36,7 @@ generators, Spline & Path tools, MCF functions, 3D view, PBR materials and Value
library packages for them are added to `assets/sbs`; the CC0 environments in `assets/hdr` are not
listed yet.
> **Last reviewed:** 2026-11-02 · **Last updated:** 2026-11-02 · **Change:** minor (portable `.mcmat` materials closed the published-archive gap) · **Target:** the professional node-based material workflow
> **Last reviewed:** 2026-11-02 · **Last updated:** 2026-11-18 · **Change:** procedural MCMAT publishing and baking are available over MCP · **Target:** the professional node-based material workflow
Every known shortfall, most important first. Pick from the top.
@@ -45,7 +45,6 @@ Every known shortfall, most important first. Pick from the top.
| 1 | Node library breadth | 227 nodes vs. ~400 in the reference library | Recipes that need scan processing, more grunges, splines or SDF shapes | 30 h | target-app-parity (Node library) |
| 2 | GPU 3D view and a path tracer | The CPU renderer has tessellation, lights and shadows, but a large or finely tessellated view takes tens of milliseconds a frame; no ground plane, one material per mesh | Smooth navigation at high resolution, reference-quality renders | 12 h | target-app-parity (3D view) |
| 3 | Mesh-based generators and bakers | None | Workflows that start from a mesh | 15 h | target-app-parity (Node library) |
| 4 | Portable materials over MCP | `.mcmat` export runs from the app and the CLI; the MCP workspace sandbox refuses `file.*` commands and has no `material_export` tool | Sandboxed agents can't hand a material to TextureCraft | 1 h | target-app-parity (Files) |
| 5 | Function loops and reusable functions | Functions live on one parameter or processor | Iterative effects; sharing logic between nodes | 5 h | target-app-parity (Function graphs) |
| 6 | Input visibility conditions | Visibility expressions are not implemented; instance output sizes can use parameter functions | Organizing large graphs' inputs | 4 h | target-app-parity (Graph parameters) |
| 7 | `ui.screenshot` times out on this test machine | The window-capture callback never fires here; `view.screenshot` and the offscreen snapshot test work | Agents bridged to the app see views but not the whole window | 1 h | — |
+1 -1
View File
@@ -38,7 +38,7 @@ automation + tests), weighted by how much of the day each area carries. **Remain
| Graph parameters and instances | 8 | 70% | Exposed inputs (float, int, bool, enum, color) bound to parameters of any compatible type, shown in groups, edited with one shared editor on the graph and on its instances; presets (save, apply, delete); sub-graph instances with their own inputs and outputs; graph attributes; `--set` on the CLI | Visibility conditions, embedded vs. linked packages |
| 2D view | 5 | 90% | The reference layout: a toolbar above the image (background image, save, copy to the clipboard, output choice, UV overlay, Information and Histogram panels) and one below (channel menu with C/R/G/B/A keys, checkerboard, premultiplied alpha, sRGB or linear display, tiling across the whole view, physical size, exact fit, 1:1, stepped zoom with a zoom field, zoom lock); an Information panel with the pixel under the pointer and a color sampler (values at the image's bit depth and as floats, HSV, position); a Histogram panel with channel choice, bit depth, square-root scale, used range and a selectable range; size/format line | Color picker to parameters, floating the two panels, UV sets beyond the first |
| 3D view | 10 | 75% | The reference layout: menus (Scene, Materials, Lights, Camera, Environment, Display, Renderer), a tool strip (scene browser, Select, Camera and Light tools, environment background, shadows, a displacement pop-up, the display helpers) and a bar with the channels, the color space and the scene; the material, lights, camera, environment and renderer in the Properties panel. Thirteen built-in meshes and OBJ, glTF and GLB meshes; nine shaders (the OpenPBR Surface model with coat, fuzz, thin film, transmission and subsurface, metallic/roughness with coated and subsurface variants, specular/glossiness, Blinn, Lambert, unlit and mesh information) with every parameter editable, switchable and replaced by a graph output; height by tessellation and displacement or parallax occlusion; an ambient light and two point lights with shadows; 4 procedural environments with rotation, exposure and an on/off switch; focal length, perspective or orthographic projection, viewpoints, framing, clip planes, a studio backdrop, a background color or the environment behind the mesh, and a render resolution of its own; wireframe, backface culling, grid, axes with a scale, bounding box, light helpers, vertex tangent space, scene statistics, resolution and frame rate; tiling, UV scale and physical size; alpha transparency and an alpha channel view; save and copy the render, export the tessellated mesh as OBJ, state files and a saved default state | A GPU renderer and a path tracer, a ground plane that catches shadows, several materials on one mesh, loading shader definitions, anisotropy, dispersion and thin-walled transmission in the renderer |
| Files | 8 | 70% | `.mcraft` packages with embedded bitmaps; `.mcmat` portable materials (cooked maps, exposed inputs, presets, input nodes, the source package) that TextureCraft paints with and re-cooks through the CLI at its own resolution, values and mesh maps; preview images for input nodes; an Export window (From graph, Batch at a chosen output size, and MaterialX and MCMAT tabs holding the #190 publishing options) writing the checked outputs (PNG, JPEG, TGA, BMP, WEBP, EXR, HDR; DDS listed for later; 8/16/32 bits; name pattern; sRGB, linear or raw per output); bitmap import (PNG, JPEG, TGA, TIFF, BMP, WEBP, EXR, HDR); mesh import (OBJ, glTF, GLB); open, save, import and export in the browser; atomic writes; autosave; recent files | An MCP tool for portable materials, package dependencies, SVG and font files as resources |
| Files | 8 | 70% | `.mcraft` packages with embedded bitmaps; `.mcmat` portable materials (selected root graph, reachable helper graphs and source assets) that TextureCraft paints with and re-cooks through the CLI at its own resolution, values and mesh maps; preview images for input nodes; an Export window (From graph, Batch at a chosen output size, and OpenPBR MaterialX baking and procedural MCMAT publishing) writing the checked outputs (PNG, JPEG, TGA, BMP, WEBP, EXR, HDR; DDS listed for later; 8/16/32 bits; name pattern; sRGB, linear or raw per output); bitmap import (PNG, JPEG, TGA, TIFF, BMP, WEBP, EXR, HDR); mesh import (OBJ, glTF, GLB); open, save, import and export in the browser; atomic writes; autosave; recent files | Package dependencies, SVG and font files as resources |
| UI and UX | 8 | 70% | Dockable/floatable panels (explorer, library, graph, 3D, 2D, properties, console, history), menus, main toolbar, graph toolbar with atomic quick-insert, library with live previews, properties with curve/gradient/color editors and a Levels editor over the input histogram, the reference File, Tools, Windows and Help menus (Save all, Cancel rendering, Suspend engine, main toolbar toggle, help and community links), New graph dialog with template cards, a category filter, width and height and a target package, export dialog, a paged Preferences window (General, Projects, Graph, 3D View, Library and Shortcuts pages with OK, Cancel and Apply; recent list length, invert zoom, numbered autosave copies, node search on Tab and on wire drop, outputs in the 3D view on open, node creation shortcuts), a Plugin Manager window (no plugin loading yet), shortcuts list, a home screen at startup (templates, recent packages, learn and community links), visual function editor, presets bar, accessible names on every widget, status bar | A plugin system, bakers, keymap editor for application commands, minimap, multi-graph tabs |
| Platforms | 4 | 50% | Windows, macOS, Linux, FreeBSD and web builds compile; browser file I/O; packaging scripts and release workflows | First release run |
| **Total** | **100** | **≈ 69%** | | |