mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-20 06:20:50 +01:00
Auto: Integrate latest main with reviewed generator changes
This commit is contained in:
commit
23ef7b15ab
17 files changed
+549
-669
No files matched your search
Generated
+58
-9
@@ -758,7 +758,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-dialogs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
|
||||
dependencies = [
|
||||
"block2 0.6.2",
|
||||
"objc2 0.6.5",
|
||||
@@ -771,12 +771,12 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-fonts-build"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
|
||||
|
||||
[[package]]
|
||||
name = "craft-i18n"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
|
||||
dependencies = [
|
||||
"fixed_decimal",
|
||||
"icu_decimal",
|
||||
@@ -799,17 +799,37 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-keymap"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
|
||||
dependencies = [
|
||||
"egui",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-materialx"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
|
||||
dependencies = [
|
||||
"craft-xml",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-mcmat"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=828b07f3c9f3f6a2e935087b859016a5e46b1c31#828b07f3c9f3f6a2e935087b859016a5e46b1c31"
|
||||
dependencies = [
|
||||
"craft-zip",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-menu"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
|
||||
dependencies = [
|
||||
"craft-keymap",
|
||||
"craft-quit",
|
||||
@@ -819,10 +839,15 @@ dependencies = [
|
||||
"objc2-app-kit 0.3.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-mesh"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=d6ed8c20f12188c1f39c89b3a8436bd0d09c3748#d6ed8c20f12188c1f39c89b3a8436bd0d09c3748"
|
||||
|
||||
[[package]]
|
||||
name = "craft-quit"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
|
||||
dependencies = [
|
||||
"dispatch2",
|
||||
"egui",
|
||||
@@ -833,7 +858,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-ui"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
|
||||
dependencies = [
|
||||
"craft-fonts-build",
|
||||
"craft-i18n",
|
||||
@@ -854,7 +879,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-update"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dunce",
|
||||
@@ -881,7 +906,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-window"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
|
||||
dependencies = [
|
||||
"egui",
|
||||
"objc2 0.6.5",
|
||||
@@ -889,6 +914,26 @@ dependencies = [
|
||||
"wayland-client",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=828b07f3c9f3f6a2e935087b859016a5e46b1c31#828b07f3c9f3f6a2e935087b859016a5e46b1c31"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"thiserror 2.0.21",
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.5.2"
|
||||
@@ -4369,6 +4414,10 @@ name = "texturecraft-formats"
|
||||
version = "0.6.1"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"craft-materialx",
|
||||
"craft-mcmat",
|
||||
"craft-mesh",
|
||||
"craft-zip",
|
||||
"exr",
|
||||
"flate2",
|
||||
"gltf",
|
||||
|
||||
+12
-8
@@ -66,20 +66,24 @@ rustix = { version = "1", default-features = false, features = ["std", "fs", "pr
|
||||
# Shared Craft egui components (widget palette, icon buttons, panel docking): storytold/craft-libs,
|
||||
# MIT OR Apache-2.0, pinned to one rev.
|
||||
# Shared catalog lookup, pinned with the other Craft libraries.
|
||||
craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
|
||||
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["serde"] }
|
||||
craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585" }
|
||||
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["serde"] }
|
||||
craft-materialx = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585" }
|
||||
craft-mesh = { git = "https://github.com/storytold/craft-libs", rev = "d6ed8c20f12188c1f39c89b3a8436bd0d09c3748", default-features = false, features = ["stl"] }
|
||||
craft-mcmat = { git = "https://github.com/storytold/craft-libs", rev = "828b07f3c9f3f6a2e935087b859016a5e46b1c31" }
|
||||
craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "828b07f3c9f3f6a2e935087b859016a5e46b1c31" }
|
||||
# Who draws the window frame, and the app-drawn strip as the title bar (crates/ui-egui/src/titlebar.rs).
|
||||
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
|
||||
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["egui"] }
|
||||
# Release checks, the update prompt and the update helper (crates/ui-egui/src/updates_ui.rs).
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["egui"] }
|
||||
# File dialogs that never block the frame (crates/ui-egui/src/file_dialogs.rs).
|
||||
craft-dialogs = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
|
||||
craft-dialogs = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585" }
|
||||
# Shortcut chords, lookup and dispatch (crates/ui-egui/src/keymap.rs).
|
||||
craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
|
||||
craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["egui"] }
|
||||
# Quitting the macOS way instead of closing the window (crates/ui-egui/src/windows.rs).
|
||||
craft-quit = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
|
||||
craft-quit = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["egui"] }
|
||||
# The macOS app menu whose Quit asks about unsaved changes (apps/texturecraft/src/main.rs).
|
||||
craft-menu = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
|
||||
craft-menu = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585" }
|
||||
|
||||
# egui 0.37 and winit: the suite's forks (storytold/egui, storytold/winit), as craft-libs pins them.
|
||||
[patch.crates-io]
|
||||
|
||||
+105
-10
@@ -56,7 +56,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Save MaterialCraft package",
|
||||
[],
|
||||
None,
|
||||
"{layer?, path} — writes the MaterialCraft package (.mcraft) behind a material layer, to open in MaterialCraft",
|
||||
"{layer?, path} — writes the MaterialCraft source (.mcraft or .sbs) behind a material layer",
|
||||
has_layer,
|
||||
save_source
|
||||
),
|
||||
@@ -178,7 +178,9 @@ fn save_source(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const CMD: &str = "material.link.saveSource";
|
||||
let id = layer_param(s, CMD, p)?;
|
||||
let path = strp(p, "path").ok_or_else(|| bad(CMD, "`path` is required"))?;
|
||||
let path = if path.to_ascii_lowercase().ends_with(&format!(".{MCRAFT_EXT}")) { path.to_string() } else { format!("{path}.{MCRAFT_EXT}") };
|
||||
let link = link_of(s, CMD, id)?;
|
||||
let ext = source_extension(&link.package);
|
||||
let path = if path.to_ascii_lowercase().ends_with(&format!(".{ext}")) { path.to_string() } else { format!("{path}.{ext}") };
|
||||
let bytes = s.link_package(id)?;
|
||||
texturecraft_formats::write_atomic(std::path::Path::new(&path), &bytes).map_err(|e| bad(CMD, e.to_string()))?;
|
||||
Ok(json!({"path": path}))
|
||||
@@ -195,6 +197,10 @@ fn save_source(_: &mut Session, _: &Value) -> Result<Value> {
|
||||
pub struct LinkJob {
|
||||
pub layer: LayerId,
|
||||
pub package: Arc<Vec<u8>>,
|
||||
/// The source package extension (`mcraft` or `sbs`).
|
||||
pub source_ext: &'static str,
|
||||
/// The named graph in a linked material; direct package imports use its default.
|
||||
pub graph: Option<String>,
|
||||
/// Pixels across (the texture set's resolution).
|
||||
pub size: u32,
|
||||
/// Every parameter's value, by id, in MaterialCraft's typed form.
|
||||
@@ -226,7 +232,7 @@ fn quiet(program: &std::path::Path) -> std::process::Command {
|
||||
c
|
||||
}
|
||||
|
||||
/// Open `package` (a `.mcraft` file) in the MaterialCraft app (`$MATERIALCRAFT`, next to this
|
||||
/// Open `package` (a `.mcraft` or `.sbs` file) in the MaterialCraft app (`$MATERIALCRAFT`, next to this
|
||||
/// program, or on `PATH`).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn open_in_materialcraft(package: &std::path::Path) -> Result<()> {
|
||||
@@ -236,11 +242,21 @@ pub fn open_in_materialcraft(package: &std::path::Path) -> Result<()> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Source file extensions are chosen from the linked resource name, never an untrusted path.
|
||||
pub fn source_extension(name: &str) -> &'static str {
|
||||
if name.to_ascii_lowercase().ends_with(".sbs") { "sbs" } else { MCRAFT_EXT }
|
||||
}
|
||||
|
||||
impl LinkJob {
|
||||
/// Cook through `materialcraft-cli` (`$MATERIALCRAFT_CLI`, next to this program, or on
|
||||
/// `PATH`); returns `.mcmat` bytes. Blocks for the cook (about a second at 2K).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn run(&self) -> Result<Vec<u8>> {
|
||||
self.run_with_cli(&materialcraft_tool("materialcraft-cli", "MATERIALCRAFT_CLI"))
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn run_with_cli(&self, cli: &std::path::Path) -> Result<Vec<u8>> {
|
||||
let other = |e: String| EngineError::Other(e);
|
||||
let n = NEXT_JOB.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
let dir = std::env::temp_dir().join(format!("texturecraft-link-{}-{n}", std::process::id()));
|
||||
@@ -252,15 +268,27 @@ impl LinkJob {
|
||||
std::fs::write(&p, bytes).map_err(|e| other(format!("{name}: {e}")))?;
|
||||
utf8(&p)
|
||||
};
|
||||
let src = write("source.mcraft", &self.package)?;
|
||||
let ext = if self.source_ext == "sbs" { "sbs" } else { MCRAFT_EXT };
|
||||
let src = write(&format!("source.{ext}"), &self.package)?;
|
||||
let mut inputs = Map::new();
|
||||
for (i, (id, png)) in self.inputs.iter().enumerate() {
|
||||
inputs.insert(id.clone(), json!(write(&format!("input{i}.png"), png)?));
|
||||
}
|
||||
let out = dir.join(format!("out.{MCMAT_EXT}"));
|
||||
let params = json!({"path": utf8(&out)?, "size": self.size, "values": self.values, "inputs": inputs});
|
||||
let cli = materialcraft_tool("materialcraft-cli", "MATERIALCRAFT_CLI");
|
||||
let run = quiet(&cli).arg("run").arg(&src).arg("file.exportMaterial").arg(params.to_string()).output().map_err(|e| {
|
||||
let mut command = quiet(cli);
|
||||
if let Some(graph) = &self.graph {
|
||||
// Opening the source starts on its first graph; select the graph saved in the link.
|
||||
let script = json!({"open": src, "steps": [
|
||||
{"id": "graph.open", "params": {"graph": graph}},
|
||||
{"id": "file.exportMaterial", "params": params},
|
||||
]});
|
||||
let script_path = write("cook.json", script.to_string().as_bytes())?;
|
||||
command.arg("batch").arg(script_path);
|
||||
} else {
|
||||
command.arg("run").arg(&src).arg("file.exportMaterial").arg(params.to_string());
|
||||
}
|
||||
let run = command.output().map_err(|e| {
|
||||
other(format!(
|
||||
"MaterialCraft's command line ({}) did not start: {e}. Install MaterialCraft next to TextureCraft or set MATERIALCRAFT_CLI",
|
||||
cli.display()
|
||||
@@ -301,7 +329,15 @@ impl Session {
|
||||
let bytes = if is_mcraft(bytes) {
|
||||
let ts = self.ts();
|
||||
let size = self.doc.project.texture_set(ts)?.resolution;
|
||||
let job = LinkJob { layer: LayerId(0), package: Arc::new(bytes.to_vec()), size, values: Map::new(), inputs: Vec::new() };
|
||||
let job = LinkJob {
|
||||
layer: LayerId(0),
|
||||
package: Arc::new(bytes.to_vec()),
|
||||
source_ext: MCRAFT_EXT,
|
||||
graph: None,
|
||||
size,
|
||||
values: Map::new(),
|
||||
inputs: Vec::new(),
|
||||
};
|
||||
cooked = job.run()?;
|
||||
cooked.as_slice()
|
||||
} else {
|
||||
@@ -316,7 +352,7 @@ impl Session {
|
||||
let (id, names) = self.edit(&label, |s| {
|
||||
let ts = s.ts();
|
||||
let stem = texturecraft_formats::sanitize_resource_name(&m.manifest.name);
|
||||
let package = unique(s, &format!("{stem}.{MCRAFT_EXT}"));
|
||||
let package = unique(s, &format!("{stem}.{}", source_extension(&m.manifest.source)));
|
||||
s.doc.resources.insert(package.clone(), Arc::new(m.source.clone()));
|
||||
let link = MaterialLink {
|
||||
name: m.manifest.name.clone(),
|
||||
@@ -345,7 +381,7 @@ impl Session {
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
/// The `.mcraft` package behind linked layer `id`.
|
||||
/// The source package behind linked layer `id`.
|
||||
pub fn link_package(&self, id: LayerId) -> Result<Arc<Vec<u8>>> {
|
||||
let link = link_of(self, "material.link", id)?;
|
||||
self.doc.resources.get(&link.package).cloned().ok_or_else(|| EngineError::Other(format!("the package of {} is missing", link.name)))
|
||||
@@ -376,7 +412,15 @@ impl Session {
|
||||
|
||||
// UDIM tiles share one cook at the texture set's base resolution
|
||||
let size = self.doc.project.texture_set(texture_set)?.resolution;
|
||||
Ok(LinkJob { layer: id, package: self.link_package(id)?, size, values, inputs })
|
||||
Ok(LinkJob {
|
||||
layer: id,
|
||||
package: self.link_package(id)?,
|
||||
source_ext: source_extension(&link.package),
|
||||
graph: Some(link.graph.clone()),
|
||||
size,
|
||||
values,
|
||||
inputs,
|
||||
})
|
||||
}
|
||||
|
||||
/// Swap in maps a [`LinkJob`] cooked (`.mcmat` bytes) and the values they were cooked with.
|
||||
@@ -456,3 +500,54 @@ fn put_maps(s: &mut Session, stem: &str, maps: &[(Channel, Vec<u8>, texturecraft
|
||||
}
|
||||
names
|
||||
}
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
mod subprocess_tests {
|
||||
use super::*;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
#[test]
|
||||
fn cook_uses_the_saved_graph_and_source_type_without_global_environment() {
|
||||
let id = NEXT_JOB.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
let dir = std::env::temp_dir().join(format!("texturecraft-cook-test-{}-{id}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let cli = dir.join("materialcraft-cli");
|
||||
let script = r#"#!/bin/sh
|
||||
set -eu
|
||||
case "$1" in
|
||||
batch)
|
||||
grep -F '"graph":"Second"' "$2" >/dev/null
|
||||
grep -F '"id":"file.exportMaterial"' "$2" >/dev/null
|
||||
test -f "$(dirname "$2")/source.sbs"
|
||||
test -f "$(dirname "$2")/input0.png"
|
||||
cp "$(dirname "$0")/result.mcmat" "$(dirname "$2")/out.mcmat"
|
||||
;;
|
||||
run)
|
||||
test "$(basename "$2")" = "source.mcraft"
|
||||
cp "$(dirname "$0")/result.mcmat" "$(dirname "$2")/out.mcmat"
|
||||
;;
|
||||
*) exit 1 ;;
|
||||
esac
|
||||
"#;
|
||||
std::fs::write(&cli, script).unwrap();
|
||||
std::fs::set_permissions(&cli, std::fs::Permissions::from_mode(0o700)).unwrap();
|
||||
std::fs::write(dir.join("result.mcmat"), b"cooked").unwrap();
|
||||
let mut job = LinkJob {
|
||||
layer: LayerId(1),
|
||||
package: Arc::new(b"source".to_vec()),
|
||||
source_ext: "sbs",
|
||||
graph: Some("Second".into()),
|
||||
size: 64,
|
||||
values: Map::new(),
|
||||
inputs: vec![("curvature".into(), b"png".to_vec())],
|
||||
};
|
||||
assert_eq!(job.run_with_cli(&cli).unwrap(), b"cooked");
|
||||
job.graph = None;
|
||||
job.source_ext = "mcraft";
|
||||
assert_eq!(job.run_with_cli(&cli).unwrap(), b"cooked");
|
||||
job.graph = Some("wrong".into());
|
||||
assert!(job.run_with_cli(&cli).is_err());
|
||||
assert!(job.run_with_cli(&dir.join("missing")).is_err());
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,14 @@ fn session() -> Session {
|
||||
|
||||
/// A `.mcmat` as MaterialCraft writes it: uniform maps, one float input `wear` cooked at `wear`.
|
||||
fn mcmat(roughness: f32, wear: f32) -> Vec<u8> {
|
||||
mcmat_version(roughness, wear, 1)
|
||||
}
|
||||
|
||||
fn mcmat_version(roughness: f32, wear: f32, version: u32) -> Vec<u8> {
|
||||
let source = if version == 2 { "source.sbs" } else { "source.mcraft" };
|
||||
let gray = |v: f32| mesh_map_png(4, 1, &[v; 16]).unwrap();
|
||||
let manifest = json!({
|
||||
"format": "craft-material", "version": 1, "name": "Brick wall", "graph": "Brick_wall", "size": [4, 4],
|
||||
"format": "craft-material", "version": version, "name": "Brick wall", "graph": "Brick_wall", "size": [4, 4],
|
||||
"maps": [
|
||||
{"usage": "baseColor", "file": "maps/baseColor.png", "colorSpace": "srgb"},
|
||||
{"usage": "roughness", "file": "maps/roughness.png", "colorSpace": "linear"},
|
||||
@@ -42,7 +47,7 @@ fn mcmat(roughness: f32, wear: f32) -> Vec<u8> {
|
||||
{"identifier": "bentNormal", "kind": "color"}
|
||||
],
|
||||
"presets": [{"name": "Worn", "values": {"wear": {"t": "float", "v": 1.0}, "note": {"t": "text", "v": "x"}}}],
|
||||
"source": "source.mcraft"
|
||||
"source": source
|
||||
});
|
||||
let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
let o = zip::write::SimpleFileOptions::default();
|
||||
@@ -51,7 +56,7 @@ fn mcmat(roughness: f32, wear: f32) -> Vec<u8> {
|
||||
("maps/baseColor.png", mesh_map_png(4, 3, &[0.5; 48]).unwrap()),
|
||||
("maps/roughness.png", gray(roughness)),
|
||||
("maps/ambientOcclusion.png", gray(1.0)),
|
||||
("source.mcraft", b"package bytes".to_vec()),
|
||||
(source, b"package bytes".to_vec()),
|
||||
] {
|
||||
z.start_file(name, o).unwrap();
|
||||
z.write_all(&bytes).unwrap();
|
||||
@@ -81,6 +86,24 @@ fn map_name(s: &Session, id: LayerId, c: Channel) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn version_two_preserves_the_sbs_source_for_future_cooks() {
|
||||
let mut session = session();
|
||||
let id = session.add_linked_material_bytes_at(&mcmat_version(0.25, 0.5, 2), Place::Selection).unwrap();
|
||||
let linked = link(&session, id);
|
||||
assert!(linked.package.ends_with(".sbs"));
|
||||
let job = session.link_job(id, &serde_json::Map::new()).unwrap();
|
||||
assert_eq!(job.source_ext, "sbs");
|
||||
assert_eq!(job.graph.as_deref(), Some("Brick_wall"));
|
||||
assert_eq!(job.package.as_slice(), b"package bytes");
|
||||
let base = std::env::temp_dir().join(format!("texturecraft-sbs-source-{}", std::process::id()));
|
||||
let saved = execute(&mut session, "material.link.saveSource", &json!({"layer": id.0, "path": base.to_str().unwrap()})).unwrap();
|
||||
let path = saved["path"].as_str().unwrap();
|
||||
assert!(path.ends_with(".sbs"));
|
||||
assert_eq!(std::fs::read(path).unwrap(), b"package bytes");
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn imports_as_a_linked_fill_with_its_maps() {
|
||||
let mut s = session();
|
||||
@@ -199,7 +222,7 @@ fn cooks_through_materialcraft_cli_when_available() {
|
||||
let dir = std::env::temp_dir().join(format!("tc-mc-link-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let pkg = dir.join("bricks.mcraft");
|
||||
let ok = std::process::Command::new(&cli).args(["new", "bricks"]).arg(&pkg).args(["--size", "64"]).status().unwrap();
|
||||
let ok = std::process::Command::new(&cli).args(["new", "pbr"]).arg(&pkg).args(["--size", "64"]).status().unwrap();
|
||||
assert!(ok.success());
|
||||
let exposed = dir.join("exposed.mcraft");
|
||||
let ok = std::process::Command::new(&cli)
|
||||
@@ -217,9 +240,24 @@ fn cooks_through_materialcraft_cli_when_available() {
|
||||
assert_eq!(link(&s, id).size, 128, "cooked at the texture set's resolution");
|
||||
assert!(link(&s, id).params.iter().any(|p| p.id == "wear"));
|
||||
execute(&mut s, "material.link.set", &json!({"layer": id.0, "id": "wear", "value": 0.8})).unwrap();
|
||||
assert_eq!(link(&s, id).params.iter().find(|p| p.id == "wear").unwrap().value, json!({"t": "float", "v": 0.8}));
|
||||
let out = dir.join("back.mcraft");
|
||||
let wear = link(&s, id).params.iter().find(|p| p.id == "wear").unwrap().value["v"].as_f64().unwrap();
|
||||
assert!((wear - 0.8).abs() < 1e-6);
|
||||
let out = dir.join("back.sbs");
|
||||
execute(&mut s, "material.link.saveSource", &json!({"layer": id.0, "path": out.to_str().unwrap()})).unwrap();
|
||||
assert!(std::fs::metadata(&out).unwrap().len() > 0);
|
||||
|
||||
// The portable material can name a graph other than the first in the embedded source.
|
||||
let multi = dir.join("multi.mcraft");
|
||||
let ok = std::process::Command::new(&cli).arg("run").arg(&exposed).arg("graph.duplicate").arg("{}").arg("-o").arg(&multi).status().unwrap();
|
||||
assert!(ok.success());
|
||||
let portable = dir.join("second.mcmat");
|
||||
let params = json!({"graph": 1, "size": 64, "path": portable.to_str().unwrap()});
|
||||
let ok = std::process::Command::new(&cli).arg("run").arg(&multi).arg("file.exportMaterial").arg(params.to_string()).status().unwrap();
|
||||
assert!(ok.success());
|
||||
let second = s.add_linked_material_bytes_at(&std::fs::read(&portable).unwrap(), Place::Selection).unwrap();
|
||||
let expected_graph = link(&s, second).graph;
|
||||
let job = s.link_job(second, &serde_json::Map::new()).unwrap();
|
||||
let result = texturecraft_engine::formats::mcmat::read_mcmat(&job.run().unwrap()).unwrap();
|
||||
assert_eq!(result.manifest.graph, expected_graph);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
@@ -9,6 +9,10 @@ license.workspace = true
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-materialx = { workspace = true }
|
||||
craft-mesh = { workspace = true }
|
||||
craft-mcmat = { workspace = true }
|
||||
craft-zip = { workspace = true }
|
||||
texturecraft-core = { workspace = true }
|
||||
texturecraft-texel = { workspace = true }
|
||||
texturecraft-geom = { workspace = true }
|
||||
|
||||
+20
-123
@@ -1,20 +1,7 @@
|
||||
//! MaterialX documents for exported texture sets, written from the ASWF MaterialX specification
|
||||
//! (version 1.39) and the OpenPBR Surface specification. Pure Rust: no MaterialX library is
|
||||
//! linked, and nothing is read from any other application.
|
||||
//! TextureCraft's MaterialX export adapter.
|
||||
//!
|
||||
//! [`write`] takes a material's name and the texture files an export produced, each tagged with
|
||||
//! the OpenPBR input it feeds, and gives the text of a `.mtlx` file: an `open_pbr_surface` shader
|
||||
//! whose inputs read `tiledimage` nodes, wrapped in a `surfacematerial`. The inputs are the ones
|
||||
//! the built-in "OpenPBR (separate maps)" export preset writes ([`INPUTS`]); `height` becomes a
|
||||
//! `displacement` shader on the material and `geometry_normal` goes through a `normalmap` node
|
||||
//! (OpenGL, +Y up, which is what that node expects).
|
||||
//!
|
||||
//! Colour textures (base, fuzz and emission colour) are tagged with the colour space they were
|
||||
//! exported in ([`colorspace_name`]); every data map is left untagged (linear). UDIM sets write the `<UDIM>` filename token MaterialX defines.
|
||||
//! Texture file names are relative to the document.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::fmt::Write as _;
|
||||
//! The shared `craft-materialx` crate owns the MaterialX document writer. This module maps the
|
||||
//! built-in preset's map names and TextureCraft color spaces to that format-level API.
|
||||
|
||||
use texturecraft_texel::ColorSpace;
|
||||
|
||||
@@ -51,7 +38,6 @@ pub const INPUTS: &[(&str, Kind)] = &[
|
||||
("height", Kind::Height),
|
||||
];
|
||||
|
||||
/// The file-name prefix of the built-in OpenPBR preset's maps.
|
||||
const PREFIX: &str = "$mesh_$textureSet_";
|
||||
|
||||
/// One exported texture: the OpenPBR input it feeds and the file (relative to the `.mtlx`).
|
||||
@@ -59,14 +45,11 @@ const PREFIX: &str = "$mesh_$textureSet_";
|
||||
pub struct Texture {
|
||||
pub input: String,
|
||||
pub file: String,
|
||||
/// The MaterialX colour space of the file's values (`srgb_texture`, `lin_rec709`, ...);
|
||||
/// used for colour inputs only, data maps are always linear.
|
||||
/// The MaterialX colour space of the file's values; used for color inputs only.
|
||||
pub colorspace: &'static str,
|
||||
}
|
||||
|
||||
/// The MaterialX colour space of colour maps written in `space`. MaterialX 1.39 defines no
|
||||
/// ACES2065-1 (AP0) space: it gets `lin_ap0_scene`, its name in later versions of the
|
||||
/// specification, which 1.39 readers may not know.
|
||||
/// The MaterialX colour space of colour maps written in `space`.
|
||||
pub fn colorspace_name(space: ColorSpace) -> &'static str {
|
||||
match space {
|
||||
ColorSpace::Srgb => "srgb_texture",
|
||||
@@ -76,116 +59,28 @@ pub fn colorspace_name(space: ColorSpace) -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
/// The OpenPBR input a preset map's file template feeds (`$mesh_$textureSet_base_color` →
|
||||
/// `base_color`), or None for templates this module has no place for.
|
||||
/// The OpenPBR input a preset map's file template feeds, or None for unsupported templates.
|
||||
pub fn input_of_template(template: &str) -> Option<&'static str> {
|
||||
let suffix = template.strip_prefix(PREFIX)?;
|
||||
INPUTS.iter().find(|(n, _)| *n == suffix).map(|(n, _)| *n)
|
||||
}
|
||||
|
||||
fn kind_of(input: &str) -> Option<Kind> {
|
||||
INPUTS.iter().find(|(n, _)| *n == input).map(|(_, k)| *k)
|
||||
INPUTS.iter().find(|(name, _)| *name == suffix).map(|(name, _)| *name)
|
||||
}
|
||||
|
||||
/// A MaterialX name: letters, digits and underscores, not starting with a digit.
|
||||
pub fn node_name(s: &str) -> String {
|
||||
let mut out: String = s.chars().map(|c| if c.is_ascii_alphanumeric() || c == '_' { c } else { '_' }).collect();
|
||||
pub fn node_name(name: &str) -> String {
|
||||
let mut out: String = name.chars().map(|c| if c.is_ascii_alphanumeric() || c == '_' { c } else { '_' }).collect();
|
||||
if out.is_empty() || out.starts_with(|c: char| c.is_ascii_digit()) {
|
||||
out.insert(0, '_');
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn esc(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len());
|
||||
for c in s.chars() {
|
||||
match c {
|
||||
'&' => out.push_str("&"),
|
||||
'<' => out.push_str("<"),
|
||||
'>' => out.push_str(">"),
|
||||
'"' => out.push_str("""),
|
||||
'\'' => out.push_str("'"),
|
||||
c if c.is_control() => out.push(' '),
|
||||
c => out.push(c),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn image_name(input: &str) -> String {
|
||||
format!("image_{}", node_name(input))
|
||||
}
|
||||
|
||||
/// The text of a MaterialX document for `name` with the given textures. `physical_size_cm` goes
|
||||
/// into the material's `doc` attribute. Inputs this module has no place for are skipped, and so
|
||||
/// are repeats of one input (the first wins).
|
||||
/// The text of a MaterialX OpenPBR document for `name` and the given texture files.
|
||||
pub fn write(name: &str, textures: &[Texture], physical_size_cm: Option<[f32; 2]>) -> String {
|
||||
let base = node_name(name);
|
||||
let mut seen = HashSet::new();
|
||||
let tex: Vec<(&Texture, Kind)> = textures.iter().filter_map(|t| kind_of(&t.input).map(|k| (t, k))).filter(|(t, _)| seen.insert(t.input.as_str())).collect();
|
||||
let has = |i: &str| tex.iter().any(|(t, _)| t.input == i);
|
||||
let mut x = String::new();
|
||||
let _ = writeln!(x, "<?xml version=\"1.0\"?>");
|
||||
let _ = writeln!(x, "<materialx version=\"1.39\" colorspace=\"lin_rec709\">");
|
||||
|
||||
let shader = format!("OPBR_{base}");
|
||||
let _ = writeln!(x, " <open_pbr_surface name=\"{}\" type=\"surfaceshader\">", esc(&shader));
|
||||
for (t, kind) in &tex {
|
||||
let (ty, node) = match kind {
|
||||
Kind::Color => ("color3", image_name(&t.input)),
|
||||
Kind::Float => ("float", image_name(&t.input)),
|
||||
Kind::Normal => ("vector3", "normalmap".to_owned()),
|
||||
Kind::Height => continue,
|
||||
};
|
||||
let _ = writeln!(x, " <input name=\"{}\" type=\"{ty}\" nodename=\"{node}\" />", t.input);
|
||||
}
|
||||
if has("emission_color") {
|
||||
let _ = writeln!(x, " <input name=\"emission_luminance\" type=\"float\" value=\"1\" />");
|
||||
}
|
||||
let _ = writeln!(x, " </open_pbr_surface>");
|
||||
|
||||
for (t, kind) in &tex {
|
||||
let (ty, space) = match kind {
|
||||
Kind::Color => ("color3", Some(t.colorspace)),
|
||||
Kind::Normal => ("vector3", None),
|
||||
Kind::Float | Kind::Height => ("float", None),
|
||||
};
|
||||
let node = image_name(&t.input);
|
||||
let _ = writeln!(x, " <tiledimage name=\"{node}\" type=\"{ty}\">");
|
||||
match space {
|
||||
Some(space) => {
|
||||
let _ = writeln!(x, " <input name=\"file\" type=\"filename\" value=\"{}\" colorspace=\"{space}\" />", esc(&t.file));
|
||||
}
|
||||
None => {
|
||||
let _ = writeln!(x, " <input name=\"file\" type=\"filename\" value=\"{}\" />", esc(&t.file));
|
||||
}
|
||||
}
|
||||
let _ = writeln!(x, " </tiledimage>");
|
||||
}
|
||||
if has("geometry_normal") {
|
||||
let _ = writeln!(x, " <normalmap name=\"normalmap\" type=\"vector3\">");
|
||||
let _ = writeln!(x, " <input name=\"in\" type=\"vector3\" nodename=\"{}\" />", image_name("geometry_normal"));
|
||||
let _ = writeln!(x, " </normalmap>");
|
||||
}
|
||||
if has("height") {
|
||||
let _ = writeln!(x, " <displacement name=\"DISP_{base}\" type=\"displacementshader\">");
|
||||
let _ = writeln!(x, " <input name=\"displacement\" type=\"float\" nodename=\"{}\" />", image_name("height"));
|
||||
let _ = writeln!(x, " <input name=\"scale\" type=\"float\" value=\"1\" />");
|
||||
let _ = writeln!(x, " </displacement>");
|
||||
}
|
||||
|
||||
let doc = match physical_size_cm {
|
||||
Some([w, h]) if w.is_finite() && h.is_finite() => format!(" doc=\"Tiles every {w} x {h} cm\""),
|
||||
_ => String::new(),
|
||||
};
|
||||
let _ = writeln!(x, " <surfacematerial name=\"{}\" type=\"material\"{doc}>", esc(&base));
|
||||
let _ = writeln!(x, " <input name=\"surfaceshader\" type=\"surfaceshader\" nodename=\"{}\" />", esc(&shader));
|
||||
if has("height") {
|
||||
let _ = writeln!(x, " <input name=\"displacementshader\" type=\"displacementshader\" nodename=\"DISP_{base}\" />");
|
||||
}
|
||||
let _ = writeln!(x, " </surfacematerial>");
|
||||
let _ = writeln!(x, "</materialx>");
|
||||
x
|
||||
let textures: Vec<craft_materialx::Texture> = textures
|
||||
.iter()
|
||||
.map(|texture| craft_materialx::Texture { input: texture.input.clone(), file: texture.file.clone(), colorspace: texture.colorspace.to_owned() })
|
||||
.collect();
|
||||
craft_materialx::write_open_pbr(name, &textures, physical_size_cm)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -247,7 +142,7 @@ mod tests {
|
||||
assert!(x.contains(want), "missing {want}\n{x}");
|
||||
}
|
||||
// Data textures carry no colour space, and height is not an OpenPBR input.
|
||||
assert!(x.contains("value=\"stone_specular_roughness.png\" />"));
|
||||
assert!(x.contains("value=\"stone_specular_roughness.png\"/>"));
|
||||
assert!(!x.contains("name=\"height\""));
|
||||
}
|
||||
|
||||
@@ -265,7 +160,7 @@ mod tests {
|
||||
fn colour_inputs_carry_their_files_space_and_data_inputs_none() {
|
||||
let lin = |input: &str, file: &str| Texture { colorspace: "lin_rec709", ..tex(input, file) };
|
||||
let x = write("m", &[lin("base_color", "c.exr"), lin("specular_roughness", "r.exr")], None);
|
||||
assert!(x.contains("value=\"c.exr\" colorspace=\"lin_rec709\"") && x.contains("value=\"r.exr\" />"), "{x}");
|
||||
assert!(x.contains("value=\"c.exr\" colorspace=\"lin_rec709\"") && x.contains("value=\"r.exr\"/>"), "{x}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -292,7 +187,9 @@ mod tests {
|
||||
assert_eq!(node_name(""), "_");
|
||||
let x = write("a<b>&\"c", &[tex("base_color", "x\"&<'>.<UDIM>.png")], Some([f32::NAN, 1.0]));
|
||||
assert!(balanced(&x.replace("<UDIM>", "UDIM")), "{x}");
|
||||
assert!(x.contains("value=\"x"&<'>.<UDIM>.png\""));
|
||||
let doc = craft_materialx::Document::parse(x.as_bytes()).unwrap();
|
||||
let material = doc.read_material(None).unwrap();
|
||||
assert!(material.inputs.iter().any(|(input, value)| input == "base_color" && value == &craft_materialx::Value::Image("x\"&<'>.<UDIM>.png".into())));
|
||||
assert!(!x.contains("doc="), "a non-finite size is left out");
|
||||
assert!(x.contains("OPBR_a_b___c"));
|
||||
}
|
||||
|
||||
@@ -1,41 +1,16 @@
|
||||
//! Reading a MaterialX (`.mtlx`) material into TextureCraft's channels, written from the ASWF
|
||||
//! MaterialX specification (1.39) and the OpenPBR Surface and glTF PBR node definitions. Pure
|
||||
//! Rust: no MaterialX library is linked.
|
||||
//! MaterialX material import adapted to TextureCraft's channel model.
|
||||
//!
|
||||
//! [`read_materialx`] finds a `surfacematerial`, follows its `surfaceshader` to an
|
||||
//! `open_pbr_surface`, `standard_surface` or `gltf_pbr` node, and turns each input TextureCraft
|
||||
//! has a channel for into a literal value or an image file:
|
||||
//!
|
||||
//! - a literal `value` becomes a constant (`color3` values tagged `srgb_texture` are converted
|
||||
//! to linear; a colour given for a scalar channel is averaged);
|
||||
//! - an input wired to a `tiledimage` or `image` node (directly or through a `nodegraph`
|
||||
//! output) becomes that node's `file` (with `fileprefix` applied); `normalmap`, `convert` and
|
||||
//! `constant` nodes in between are passed through;
|
||||
//! - the material's `displacementshader` becomes the height channel;
|
||||
//! - `uvtiling` of the first tiled image is returned as the UV tiling.
|
||||
//!
|
||||
//! Anything else (maths nodes, procedural patterns, other shaders) is skipped with a warning
|
||||
//! rather than guessed at. Emission follows the specifications' defaults: OpenPBR emits nothing
|
||||
//! unless `emission_luminance` is above 0, and `standard_surface` unless `emission` is.
|
||||
//!
|
||||
//! The reader takes untrusted bytes: the document size, nesting and element count are capped, a
|
||||
//! wiring loop stops at a depth limit, and every failure is an error, never a panic.
|
||||
//! XML parsing, nodegraph traversal and shader input resolution live in `craft-materialx`.
|
||||
//! This module maps those format-level values to TextureCraft's channels and color handling.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use quick_xml::events::{BytesStart, Event};
|
||||
use texturecraft_core::Channel;
|
||||
use texturecraft_core::{Channel, ChannelKind};
|
||||
use texturecraft_texel::srgb_to_linear;
|
||||
|
||||
use crate::{FormatError, Result, malformed};
|
||||
|
||||
const KIND: &str = "MaterialX document";
|
||||
/// Largest document read, in bytes.
|
||||
pub const MAX_BYTES: usize = 16 << 20;
|
||||
const MAX_DEPTH: usize = 64;
|
||||
const MAX_ELEMENTS: usize = 200_000;
|
||||
/// Longest chain of nodes followed from one shader input.
|
||||
const MAX_HOPS: usize = 16;
|
||||
pub const MAX_BYTES: usize = craft_materialx::MAX_BYTES;
|
||||
|
||||
/// What one channel is set from.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
@@ -58,259 +33,40 @@ pub struct Material {
|
||||
pub warnings: Vec<String>,
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// XML.
|
||||
|
||||
#[derive(Default)]
|
||||
struct El {
|
||||
name: String,
|
||||
attrs: Vec<(String, String)>,
|
||||
children: Vec<El>,
|
||||
fn channel(shader: craft_materialx::Shader, input: &str) -> Option<Channel> {
|
||||
use craft_materialx::Shader::{GltfPbr, OpenPbr, StandardSurface};
|
||||
Some(match (shader, input) {
|
||||
(OpenPbr, "base_color") | (StandardSurface, "base_color") | (GltfPbr, "base_color") => Channel::BaseColor,
|
||||
(OpenPbr, "base_metalness") | (StandardSurface, "metalness") | (GltfPbr, "metallic") => Channel::Metallic,
|
||||
(OpenPbr, "specular_roughness") | (StandardSurface, "specular_roughness") | (GltfPbr, "roughness") => Channel::Roughness,
|
||||
(OpenPbr, "geometry_normal") | (StandardSurface, "normal") | (GltfPbr, "normal") => Channel::Normal,
|
||||
(OpenPbr, "geometry_opacity") | (StandardSurface, "opacity") | (GltfPbr, "alpha") => Channel::Opacity,
|
||||
(OpenPbr, "emission_color") | (StandardSurface, "emission_color") | (GltfPbr, "emission") => Channel::Emissive,
|
||||
(OpenPbr, "coat_weight") | (StandardSurface, "coat") | (GltfPbr, "clearcoat") => Channel::CoatWeight,
|
||||
(OpenPbr, "coat_roughness") | (StandardSurface, "coat_roughness") | (GltfPbr, "clearcoat_roughness") => Channel::CoatRoughness,
|
||||
(OpenPbr, "geometry_coat_normal") | (StandardSurface, "coat_normal") | (GltfPbr, "clearcoat_normal") => Channel::CoatNormal,
|
||||
(OpenPbr, "fuzz_weight") | (StandardSurface, "sheen") => Channel::SheenWeight,
|
||||
(OpenPbr, "fuzz_color") | (StandardSurface, "sheen_color") | (GltfPbr, "sheen_color") => Channel::SheenColor,
|
||||
(OpenPbr, "fuzz_roughness") | (StandardSurface, "sheen_roughness") | (GltfPbr, "sheen_roughness") => Channel::SheenRoughness,
|
||||
(OpenPbr, "specular_roughness_anisotropy") | (StandardSurface, "specular_anisotropy") => Channel::AnisotropyLevel,
|
||||
(OpenPbr, "transmission_weight") | (StandardSurface, "transmission") | (GltfPbr, "transmission") => Channel::Transmission,
|
||||
(OpenPbr, "subsurface_weight") | (StandardSurface, "subsurface") => Channel::ScatterWeight,
|
||||
(GltfPbr, "occlusion") => Channel::AmbientOcclusion,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
impl El {
|
||||
fn attr(&self, k: &str) -> Option<&str> {
|
||||
self.attrs.iter().find(|(n, _)| n == k).map(|(_, v)| v.as_str())
|
||||
fn constant(channel: Channel, values: &[f32], colorspace: Option<&str>) -> [f32; 3] {
|
||||
let mut values = values.to_vec();
|
||||
if let Some(space) = colorspace
|
||||
&& (space.starts_with("srgb") || space.starts_with("g22") || space.starts_with("g18"))
|
||||
&& values.len() >= 3
|
||||
{
|
||||
values.iter_mut().take(3).for_each(|c| *c = srgb_to_linear(*c));
|
||||
}
|
||||
|
||||
/// The child element named `name` (the `name` attribute: inputs and nodes are looked up by it).
|
||||
fn input(&self, name: &str) -> Option<&El> {
|
||||
self.children.iter().find(|c| c.attr("name") == Some(name))
|
||||
}
|
||||
}
|
||||
|
||||
fn element(e: &BytesStart) -> El {
|
||||
let text = |b: &[u8]| String::from_utf8_lossy(b).into_owned();
|
||||
let attrs = e
|
||||
.attributes()
|
||||
.with_checks(false)
|
||||
.flatten()
|
||||
.map(|a| {
|
||||
(text(a.key.local_name().as_ref()), a.normalized_value(quick_xml::XmlVersion::Implicit1_0).map_or_else(|_| text(&a.value), |v| v.into_owned()))
|
||||
})
|
||||
.collect();
|
||||
El { name: text(e.local_name().as_ref()), attrs, children: Vec::new() }
|
||||
}
|
||||
|
||||
fn parse_xml(bytes: &[u8]) -> Result<El> {
|
||||
let mut reader = quick_xml::Reader::from_reader(bytes);
|
||||
let mut stack = vec![El::default()];
|
||||
let mut count = 0usize;
|
||||
loop {
|
||||
let event = reader.read_event().map_err(|e| malformed(KIND, e))?;
|
||||
if matches!(event, Event::Start(_) | Event::Empty(_)) {
|
||||
count += 1;
|
||||
if count > MAX_ELEMENTS || stack.len() > MAX_DEPTH {
|
||||
return Err(FormatError::TooLarge(format!(
|
||||
"the MaterialX document has more than {MAX_ELEMENTS} elements or is nested deeper than {MAX_DEPTH}"
|
||||
)));
|
||||
}
|
||||
}
|
||||
match event {
|
||||
Event::Start(e) => stack.push(element(&e)),
|
||||
Event::Empty(e) => {
|
||||
if let Some(parent) = stack.last_mut() {
|
||||
parent.children.push(element(&e));
|
||||
}
|
||||
}
|
||||
Event::End(_) => {
|
||||
let el = stack.pop().filter(|_| !stack.is_empty()).ok_or_else(|| malformed(KIND, "unbalanced end tag"))?;
|
||||
if let Some(parent) = stack.last_mut() {
|
||||
parent.children.push(el);
|
||||
}
|
||||
}
|
||||
Event::Eof => break,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
match (stack.pop(), stack.is_empty()) {
|
||||
(Some(root), true) => Ok(root),
|
||||
_ => Err(malformed(KIND, "unclosed elements at the end of the document")),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Shader input tables: (shader input, channel). `kind` says how to read the value.
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum Shader {
|
||||
OpenPbr,
|
||||
Standard,
|
||||
Gltf,
|
||||
}
|
||||
|
||||
const OPEN_PBR: &[(&str, Channel)] = &[
|
||||
("base_color", Channel::BaseColor),
|
||||
("base_metalness", Channel::Metallic),
|
||||
("specular_roughness", Channel::Roughness),
|
||||
("geometry_normal", Channel::Normal),
|
||||
("geometry_opacity", Channel::Opacity),
|
||||
("emission_color", Channel::Emissive),
|
||||
("coat_weight", Channel::CoatWeight),
|
||||
("coat_roughness", Channel::CoatRoughness),
|
||||
("geometry_coat_normal", Channel::CoatNormal),
|
||||
("fuzz_weight", Channel::SheenWeight),
|
||||
("fuzz_color", Channel::SheenColor),
|
||||
("fuzz_roughness", Channel::SheenRoughness),
|
||||
("specular_roughness_anisotropy", Channel::AnisotropyLevel),
|
||||
("transmission_weight", Channel::Transmission),
|
||||
("subsurface_weight", Channel::ScatterWeight),
|
||||
];
|
||||
|
||||
const STANDARD: &[(&str, Channel)] = &[
|
||||
("base_color", Channel::BaseColor),
|
||||
("metalness", Channel::Metallic),
|
||||
("specular_roughness", Channel::Roughness),
|
||||
("normal", Channel::Normal),
|
||||
("opacity", Channel::Opacity),
|
||||
("emission_color", Channel::Emissive),
|
||||
("coat", Channel::CoatWeight),
|
||||
("coat_roughness", Channel::CoatRoughness),
|
||||
("coat_normal", Channel::CoatNormal),
|
||||
("sheen", Channel::SheenWeight),
|
||||
("sheen_color", Channel::SheenColor),
|
||||
("sheen_roughness", Channel::SheenRoughness),
|
||||
("specular_anisotropy", Channel::AnisotropyLevel),
|
||||
("transmission", Channel::Transmission),
|
||||
("subsurface", Channel::ScatterWeight),
|
||||
];
|
||||
|
||||
const GLTF: &[(&str, Channel)] = &[
|
||||
("base_color", Channel::BaseColor),
|
||||
("metallic", Channel::Metallic),
|
||||
("roughness", Channel::Roughness),
|
||||
("normal", Channel::Normal),
|
||||
("alpha", Channel::Opacity),
|
||||
("emission", Channel::Emissive),
|
||||
("occlusion", Channel::AmbientOcclusion),
|
||||
("clearcoat", Channel::CoatWeight),
|
||||
("clearcoat_roughness", Channel::CoatRoughness),
|
||||
("clearcoat_normal", Channel::CoatNormal),
|
||||
("sheen_color", Channel::SheenColor),
|
||||
("sheen_roughness", Channel::SheenRoughness),
|
||||
("transmission", Channel::Transmission),
|
||||
];
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Resolving what feeds an input.
|
||||
|
||||
struct Doc<'a> {
|
||||
root: &'a El,
|
||||
/// Every node by name: the document's own, and each nodegraph's (qualified `graph/node`).
|
||||
nodes: HashMap<String, &'a El>,
|
||||
/// Each nodegraph's outputs: `graph/output` to the node name inside (qualified).
|
||||
outputs: HashMap<String, String>,
|
||||
tiling: Option<[f32; 2]>,
|
||||
warnings: Vec<String>,
|
||||
}
|
||||
|
||||
enum Found {
|
||||
Constant(Vec<f32>),
|
||||
Image(String),
|
||||
None,
|
||||
}
|
||||
|
||||
fn numbers(text: &str) -> Option<Vec<f32>> {
|
||||
let v: Option<Vec<f32>> = text.split(',').map(|p| p.trim().parse::<f32>().ok().filter(|f| f.is_finite())).collect();
|
||||
v.filter(|v| !v.is_empty() && v.len() <= 4)
|
||||
}
|
||||
|
||||
impl<'a> Doc<'a> {
|
||||
fn new(root: &'a El) -> Doc<'a> {
|
||||
let mut nodes = HashMap::new();
|
||||
let mut outputs = HashMap::new();
|
||||
for c in &root.children {
|
||||
match (c.name.as_str(), c.attr("name")) {
|
||||
("nodegraph", Some(g)) => {
|
||||
for n in &c.children {
|
||||
let Some(name) = n.attr("name") else { continue };
|
||||
if n.name == "output" {
|
||||
if let Some(from) = n.attr("nodename") {
|
||||
outputs.insert(format!("{g}/{name}"), format!("{g}/{from}"));
|
||||
}
|
||||
} else {
|
||||
nodes.insert(format!("{g}/{name}"), n);
|
||||
}
|
||||
}
|
||||
}
|
||||
(_, Some(name)) => {
|
||||
nodes.insert(name.to_owned(), c);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Doc { root, nodes, outputs, tiling: None, warnings: Vec::new() }
|
||||
}
|
||||
|
||||
/// What `input` (an `<input>` element of a node) resolves to; `scope` is the nodegraph the
|
||||
/// node lives in, if any.
|
||||
fn resolve(&mut self, input: &El, scope: Option<&str>, hops: usize) -> Found {
|
||||
if hops > MAX_HOPS {
|
||||
self.warnings.push("a node chain is longer than 16 nodes (or loops); skipped".into());
|
||||
return Found::None;
|
||||
}
|
||||
if let Some(v) = input.attr("value") {
|
||||
return numbers(v).map_or(Found::None, |n| Found::Constant(self.tag(input, n)));
|
||||
}
|
||||
let target = match (input.attr("nodegraph"), input.attr("output"), input.attr("nodename")) {
|
||||
(Some(g), out, _) => {
|
||||
let out = out.map(str::to_owned).or_else(|| self.outputs.keys().find_map(|k| k.strip_prefix(&format!("{g}/")).map(str::to_owned)));
|
||||
out.and_then(|o| self.outputs.get(&format!("{g}/{o}")).cloned())
|
||||
}
|
||||
(None, _, Some(n)) => {
|
||||
Some(scope.map_or_else(|| n.to_owned(), |g| format!("{g}/{n}"))).filter(|q| self.nodes.contains_key(q)).or_else(|| Some(n.to_owned()))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
let Some(node) = target.as_deref().and_then(|t| self.nodes.get(t).copied()) else { return Found::None };
|
||||
let scope = target.as_deref().and_then(|t| t.split_once('/')).map(|(g, _)| g.to_owned());
|
||||
let scope = scope.as_deref();
|
||||
match node.name.as_str() {
|
||||
"tiledimage" | "image" => {
|
||||
let Some(file) = node.input("file").and_then(|f| f.attr("value")) else {
|
||||
self.warnings.push(format!("image node {:?} has no file", node.attr("name").unwrap_or("")));
|
||||
return Found::None;
|
||||
};
|
||||
let prefix =
|
||||
[node.input("file").and_then(|f| f.attr("fileprefix")), node.attr("fileprefix"), self.root.attr("fileprefix")].into_iter().flatten().next();
|
||||
if self.tiling.is_none()
|
||||
&& let Some(t) =
|
||||
node.input("uvtiling").and_then(|t| t.attr("value")).and_then(numbers).filter(|t| t.len() == 2 && t.iter().all(|v| *v > 0.0))
|
||||
{
|
||||
// The range a fill's UV scale accepts (`layer.fill.projection`).
|
||||
self.tiling = Some([t[0].clamp(0.001, 1000.0), t[1].clamp(0.001, 1000.0)]);
|
||||
}
|
||||
Found::Image(format!("{}{file}", prefix.unwrap_or("")))
|
||||
}
|
||||
"normalmap" | "convert" => match node.input("in") {
|
||||
Some(i) => self.resolve(i, scope, hops + 1),
|
||||
None => Found::None,
|
||||
},
|
||||
"constant" => match node.input("value") {
|
||||
Some(v) => self.resolve(v, scope, hops + 1),
|
||||
None => Found::None,
|
||||
},
|
||||
other => {
|
||||
self.warnings.push(format!("a {other} node ({:?}) is not supported; its input was skipped", node.attr("name").unwrap_or("")));
|
||||
Found::None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Literal values tagged with an sRGB colour space are converted to linear.
|
||||
fn tag(&self, input: &El, mut n: Vec<f32>) -> Vec<f32> {
|
||||
let space = input.attr("colorspace").or_else(|| self.root.attr("colorspace")).unwrap_or("");
|
||||
if input.attr("colorspace").is_some() && (space.starts_with("srgb") || space.starts_with("g22") || space.starts_with("g18")) && n.len() >= 3 {
|
||||
n.iter_mut().take(3).for_each(|c| *c = srgb_to_linear(*c));
|
||||
}
|
||||
n
|
||||
}
|
||||
}
|
||||
|
||||
fn to_constant(channel: Channel, n: &[f32]) -> [f32; 3] {
|
||||
match (channel.kind(), n) {
|
||||
match (channel.kind(), values.as_slice()) {
|
||||
(_, [a]) => [*a; 3],
|
||||
(texturecraft_core::ChannelKind::Color, [r, g, b, ..]) => [*r, *g, *b],
|
||||
(ChannelKind::Color, [r, g, b, ..]) => [*r, *g, *b],
|
||||
(_, [r, g, b, ..]) => {
|
||||
let mean = (r + g + b) / 3.0;
|
||||
[mean; 3]
|
||||
@@ -319,9 +75,11 @@ fn to_constant(channel: Channel, n: &[f32]) -> [f32; 3] {
|
||||
}
|
||||
}
|
||||
|
||||
/// The literal number of `input`, if it is one.
|
||||
fn literal(shader: &El, name: &str) -> Option<f32> {
|
||||
shader.input(name).and_then(|i| i.attr("value")).and_then(numbers).and_then(|n| n.first().copied())
|
||||
fn app_value(channel: Channel, value: craft_materialx::Value) -> Value {
|
||||
match value {
|
||||
craft_materialx::Value::Constant { values, colorspace } => Value::Constant(constant(channel, &values, colorspace.as_deref())),
|
||||
craft_materialx::Value::Image(file) => Value::Image(file),
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the material called `material` (default: the first) from a `.mtlx` document.
|
||||
@@ -330,79 +88,30 @@ pub fn read_materialx(bytes: &[u8], material: Option<&str>) -> Result<Material>
|
||||
}
|
||||
|
||||
fn read_inner(bytes: &[u8], material: Option<&str>) -> Result<Material> {
|
||||
if bytes.len() > MAX_BYTES {
|
||||
return Err(FormatError::TooLarge(format!("the MaterialX document is over {} MiB", MAX_BYTES >> 20)));
|
||||
}
|
||||
let top = parse_xml(bytes)?;
|
||||
let root = top.children.iter().find(|c| c.name == "materialx").ok_or_else(|| malformed(KIND, "no <materialx> element"))?;
|
||||
let mut doc = Doc::new(root);
|
||||
let materials: Vec<&El> = root.children.iter().filter(|c| c.name == "surfacematerial").collect();
|
||||
let mat = match material {
|
||||
Some(name) => materials
|
||||
.iter()
|
||||
.copied()
|
||||
.find(|m| m.attr("name") == Some(name))
|
||||
.ok_or_else(|| FormatError::Invalid(format!("no material named {name:?} (found: {})", names(&materials))))?,
|
||||
None => materials.first().copied().ok_or_else(|| FormatError::Unsupported("the document has no surfacematerial".into()))?,
|
||||
};
|
||||
let mut out = Material { name: mat.attr("name").unwrap_or("Material").to_owned(), channels: Vec::new(), uv_tiling: [1.0, 1.0], warnings: Vec::new() };
|
||||
let shader = mat
|
||||
.input("surfaceshader")
|
||||
.and_then(|i| i.attr("nodename"))
|
||||
.and_then(|n| doc.nodes.get(n).copied())
|
||||
.ok_or_else(|| malformed(KIND, "the material has no surface shader"))?;
|
||||
let (kind, table) = match shader.name.as_str() {
|
||||
"open_pbr_surface" => (Shader::OpenPbr, OPEN_PBR),
|
||||
"standard_surface" => (Shader::Standard, STANDARD),
|
||||
"gltf_pbr" => (Shader::Gltf, GLTF),
|
||||
other => return Err(FormatError::Unsupported(format!("the surface shader is a {other}; open_pbr_surface, standard_surface and gltf_pbr are read"))),
|
||||
};
|
||||
// Emission is off by default in OpenPBR and standard_surface.
|
||||
let emits = match kind {
|
||||
Shader::OpenPbr => {
|
||||
shader.input("emission_luminance").is_some_and(|i| i.attr("value").is_none() || literal(shader, "emission_luminance").is_some_and(|v| v > 0.0))
|
||||
}
|
||||
Shader::Standard => shader.input("emission").is_some_and(|i| i.attr("value").is_none() || literal(shader, "emission").is_some_and(|v| v > 0.0)),
|
||||
Shader::Gltf => true,
|
||||
};
|
||||
let weight = match kind {
|
||||
Shader::OpenPbr => literal(shader, "base_weight"),
|
||||
Shader::Standard => literal(shader, "base"),
|
||||
Shader::Gltf => None,
|
||||
};
|
||||
if weight.is_some_and(|w| (w - 1.0).abs() > 1e-6) {
|
||||
doc.warnings.push("the base weight isn't 1; it was ignored".into());
|
||||
}
|
||||
for &(input, channel) in table {
|
||||
let Some(el) = shader.input(input) else { continue };
|
||||
if channel == Channel::Emissive && !emits && kind != Shader::Gltf {
|
||||
doc.warnings.push(format!("{input} was skipped: the shader's emission is 0"));
|
||||
continue;
|
||||
}
|
||||
match doc.resolve(el, None, 0) {
|
||||
Found::Constant(n) => out.channels.push((channel, Value::Constant(to_constant(channel, &n)))),
|
||||
Found::Image(f) => out.channels.push((channel, Value::Image(f))),
|
||||
Found::None => {}
|
||||
let document = craft_materialx::Document::parse(bytes).map_err(map_error)?;
|
||||
let shared = document.read_material(material).map_err(map_error)?;
|
||||
let mut channels = Vec::new();
|
||||
for (input, value) in shared.inputs {
|
||||
if let Some(channel) = channel(shared.shader, &input) {
|
||||
channels.push((channel, app_value(channel, value)));
|
||||
}
|
||||
}
|
||||
// The displacement shader's `displacement` input is the height.
|
||||
if let Some(d) = mat.input("displacementshader").and_then(|i| i.attr("nodename")).and_then(|n| doc.nodes.get(n).copied())
|
||||
&& let Some(i) = d.input("displacement")
|
||||
{
|
||||
match doc.resolve(i, None, 0) {
|
||||
Found::Constant(n) => out.channels.push((Channel::Height, Value::Constant(to_constant(Channel::Height, &n)))),
|
||||
Found::Image(f) => out.channels.push((Channel::Height, Value::Image(f))),
|
||||
Found::None => {}
|
||||
}
|
||||
if let Some(value) = shared.displacement {
|
||||
channels.push((Channel::Height, app_value(Channel::Height, value)));
|
||||
}
|
||||
out.uv_tiling = doc.tiling.unwrap_or([1.0, 1.0]);
|
||||
out.warnings = doc.warnings;
|
||||
Ok(out)
|
||||
Ok(Material { name: shared.name, channels, uv_tiling: shared.uv_tiling, warnings: shared.warnings })
|
||||
}
|
||||
|
||||
fn names(materials: &[&El]) -> String {
|
||||
let n: Vec<&str> = materials.iter().filter_map(|m| m.attr("name")).collect();
|
||||
if n.is_empty() { "none".into() } else { n.join(", ") }
|
||||
fn map_error(error: craft_materialx::Error) -> FormatError {
|
||||
use craft_materialx::Error;
|
||||
match error {
|
||||
Error::Xml(xml) if xml.is_limit() => FormatError::TooLarge(xml.to_string()),
|
||||
Error::MissingMaterial(name) => FormatError::Invalid(format!("no material named {name:?}")),
|
||||
Error::Unsupported(message) => FormatError::Unsupported(message),
|
||||
Error::Malformed(message) => malformed(KIND, message),
|
||||
Error::Root => malformed(KIND, "no <materialx> element"),
|
||||
Error::Xml(other) => malformed(KIND, other),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -439,6 +148,19 @@ mod tests {
|
||||
m.channels.iter().find(|(ch, _)| *ch == c).map(|(_, v)| v)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn displacement_only_image_supplies_uv_tiling() {
|
||||
let xml = br#"<materialx>
|
||||
<open_pbr_surface name="S"><input name="base_color" value="0.3,0.4,0.5"/></open_pbr_surface>
|
||||
<tiledimage name="H"><input name="file" value="height.png"/><input name="uvtiling" value="4,2"/></tiledimage>
|
||||
<displacement name="D"><input name="displacement" nodename="H"/></displacement>
|
||||
<surfacematerial name="M"><input name="surfaceshader" nodename="S"/><input name="displacementshader" nodename="D"/></surfacematerial>
|
||||
</materialx>"#;
|
||||
let material = read_materialx(xml, None).unwrap();
|
||||
assert_eq!(get(&material, Channel::Height), Some(&Value::Image("height.png".into())));
|
||||
assert_eq!(material.uv_tiling, [4.0, 2.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_pbr_inputs_become_channels() {
|
||||
let m = read_materialx(OPENPBR.as_bytes(), None).unwrap();
|
||||
@@ -456,6 +178,24 @@ mod tests {
|
||||
assert_eq!(read_materialx(extreme.as_bytes(), None).unwrap().uv_tiling, [1000.0, 0.001], "clamped like a fill's UV scale");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nodegraph_input_without_output_uses_the_first_output() {
|
||||
let xml = r#"<materialx version="1.38">
|
||||
<nodegraph name="NG">
|
||||
<tiledimage name="a" type="color3"><input name="file" type="filename" value="first.png" /></tiledimage>
|
||||
<tiledimage name="b" type="color3"><input name="file" type="filename" value="second.png" /></tiledimage>
|
||||
<output name="o1" type="color3" nodename="a" />
|
||||
<output name="o2" type="color3" nodename="b" />
|
||||
</nodegraph>
|
||||
<standard_surface name="S" type="surfaceshader"><input name="base_color" type="color3" nodegraph="NG" /></standard_surface>
|
||||
<surfacematerial name="M" type="material"><input name="surfaceshader" type="surfaceshader" nodename="S" /></surfacematerial>
|
||||
</materialx>"#;
|
||||
for _ in 0..50 {
|
||||
let material = read_materialx(xml.as_bytes(), None).unwrap();
|
||||
assert_eq!(get(&material, Channel::BaseColor), Some(&Value::Image("first.png".into())));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emission_is_off_unless_the_shader_emits() {
|
||||
let x = OPENPBR.replace(r#"<input name="emission_luminance" type="float" value="2" />"#, "");
|
||||
|
||||
+82
-85
@@ -1,11 +1,11 @@
|
||||
//! MaterialCraft portable materials (`.mcmat`, read only): a zip with `material.json` (the
|
||||
//! manifest: maps by usage, exposed parameters, image inputs, presets), `maps/<usage>.png`
|
||||
//! (8-bit sRGB for color usages, 16-bit linear otherwise; OpenGL normals) and the source
|
||||
//! `.mcraft` package. TextureCraft keeps its own reader (the apps share the format, not code).
|
||||
//! (8-bit sRGB for color usages, 16-bit linear otherwise; OpenGL normals) and the
|
||||
//! source package. The shared reader handles the portable container and manifest.
|
||||
//!
|
||||
//! Also the 16-bit PNG encoder that hands baked mesh maps to MaterialCraft as graph inputs.
|
||||
|
||||
use std::io::{Cursor, Read};
|
||||
use std::io::Cursor;
|
||||
|
||||
use serde::Deserialize;
|
||||
use texturecraft_core::{LinkParam, LinkPreset};
|
||||
@@ -16,8 +16,7 @@ use crate::{FormatError, Result};
|
||||
pub const MCMAT_EXT: &str = "mcmat";
|
||||
/// MaterialCraft's package extension.
|
||||
pub const MCRAFT_EXT: &str = "mcraft";
|
||||
const FORMAT: &str = "craft-material";
|
||||
const VERSION: u32 = 1;
|
||||
const VERSION: u32 = 2;
|
||||
/// Largest entry read (maps are at most 8192² × 16 bits, compressed).
|
||||
const MAX_ENTRY: u64 = 512 << 20;
|
||||
/// Most map entries one manifest may name (a material has at most nine usages).
|
||||
@@ -70,68 +69,76 @@ fn malformed(msg: impl Into<String>) -> FormatError {
|
||||
FormatError::Malformed { kind: KIND, msg: msg.into() }
|
||||
}
|
||||
|
||||
fn read_entry(name: &str, entry: impl Read, total_bytes: &mut u64) -> Result<Vec<u8>> {
|
||||
// Earlier maps remain live while the next entry is read
|
||||
let remaining_bytes = MAX_TOTAL.saturating_sub(*total_bytes);
|
||||
let entry_limit = MAX_ENTRY.min(remaining_bytes);
|
||||
let mut bytes = Vec::new();
|
||||
entry.take(entry_limit + 1).read_to_end(&mut bytes).map_err(|error| malformed(format!("{name}: {error}")))?;
|
||||
if bytes.len() as u64 > MAX_ENTRY {
|
||||
return Err(FormatError::TooLarge(name.to_string()));
|
||||
fn shared_error(error: craft_mcmat::Error) -> FormatError {
|
||||
match error {
|
||||
craft_mcmat::Error::Version(version) => FormatError::Unsupported(format!("{KIND} version {version} (this version reads up to {VERSION})")),
|
||||
craft_mcmat::Error::Limit(message) => malformed(message),
|
||||
craft_mcmat::Error::Archive(ref archive) if archive.is_limit() => FormatError::TooLarge(error.to_string()),
|
||||
other => malformed(other.to_string()),
|
||||
}
|
||||
|
||||
let next_total = total_bytes.saturating_add(bytes.len() as u64);
|
||||
if next_total > MAX_TOTAL {
|
||||
return Err(FormatError::TooLarge(format!("{KIND} payload (over {MAX_TOTAL} bytes)")));
|
||||
}
|
||||
|
||||
// Keep spare read capacity from accumulating across retained map entries
|
||||
bytes.shrink_to_fit();
|
||||
*total_bytes = next_total;
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
/// Read `.mcmat` bytes.
|
||||
/// Read `.mcmat` bytes within TextureCraft's existing per-entry and total payload limits.
|
||||
pub fn read_mcmat(bytes: &[u8]) -> Result<PortableMaterial> {
|
||||
let mut archive = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|error| malformed(error.to_string()))?;
|
||||
let mut total_bytes = 0u64;
|
||||
let mut read = |name: &str| -> Result<Vec<u8>> {
|
||||
let entry = archive.by_name(name).map_err(|_| malformed(format!("{name} is missing")))?;
|
||||
read_entry(name, entry, &mut total_bytes)
|
||||
let limits = craft_mcmat::Limits {
|
||||
min_version: 0,
|
||||
max_version: VERSION,
|
||||
max_entries: 100_000,
|
||||
max_maps: MAX_MAPS,
|
||||
max_manifest_bytes: MAX_ENTRY,
|
||||
max_entry_bytes: MAX_ENTRY,
|
||||
max_total_bytes: MAX_TOTAL,
|
||||
};
|
||||
|
||||
let manifest: Manifest = serde_json::from_slice(&read("material.json")?).map_err(|error| malformed(format!("material.json: {error}")))?;
|
||||
if manifest.format != FORMAT {
|
||||
return Err(malformed(format!("format `{}`", manifest.format)));
|
||||
}
|
||||
|
||||
if manifest.version > VERSION {
|
||||
return Err(FormatError::Unsupported(format!("{KIND} version {} (this version reads up to {VERSION})", manifest.version)));
|
||||
}
|
||||
|
||||
if manifest.maps.len() > MAX_MAPS {
|
||||
return Err(malformed(format!("{} maps, over the {MAX_MAPS} map limit", manifest.maps.len())));
|
||||
}
|
||||
|
||||
let maps = manifest.maps.iter().map(|map| Ok((map.usage.clone(), read(&map.file)?))).collect::<Result<Vec<_>>>()?;
|
||||
let source = read(&manifest.source)?;
|
||||
|
||||
let mut archive = craft_mcmat::Material::open(bytes, limits).map_err(shared_error)?;
|
||||
// Consume the JSON values rather than cloning the entire untrusted manifest into a second tree.
|
||||
let parameters = std::mem::take(&mut archive.manifest.parameters)
|
||||
.into_iter()
|
||||
.map(serde_json::from_value)
|
||||
.collect::<std::result::Result<_, _>>()
|
||||
.map_err(|e| malformed(format!("material.json: {e}")))?;
|
||||
let inputs = std::mem::take(&mut archive.manifest.inputs)
|
||||
.into_iter()
|
||||
.map(serde_json::from_value)
|
||||
.collect::<std::result::Result<_, _>>()
|
||||
.map_err(|e| malformed(format!("material.json: {e}")))?;
|
||||
let presets = std::mem::take(&mut archive.manifest.presets)
|
||||
.into_iter()
|
||||
.map(serde_json::from_value)
|
||||
.collect::<std::result::Result<_, _>>()
|
||||
.map_err(|e| malformed(format!("material.json: {e}")))?;
|
||||
let shared = &archive.manifest;
|
||||
let manifest = Manifest {
|
||||
format: shared.format.clone(),
|
||||
version: shared.version,
|
||||
name: shared.name.clone(),
|
||||
graph: shared.graph.clone(),
|
||||
size: shared.size,
|
||||
maps: shared.maps.iter().map(|m| MapEntry { usage: m.usage.clone(), file: m.file.clone() }).collect(),
|
||||
parameters,
|
||||
inputs,
|
||||
presets,
|
||||
source: shared.source.clone(),
|
||||
};
|
||||
let maps = manifest.maps.iter().map(|map| Ok((map.usage.clone(), archive.read(&map.file).map_err(shared_error)?))).collect::<Result<Vec<_>>>()?;
|
||||
let source = archive.read_source().map_err(shared_error)?;
|
||||
Ok(PortableMaterial { manifest, maps, source })
|
||||
}
|
||||
|
||||
/// Whether `bytes` are a zip holding `entry` (`material.json`: a portable material,
|
||||
/// `package.json`: a MaterialCraft package).
|
||||
fn zip_has(bytes: &[u8], entry: &str) -> bool {
|
||||
zip::ZipArchive::new(Cursor::new(bytes)).is_ok_and(|mut z| z.by_name(entry).is_ok())
|
||||
}
|
||||
|
||||
/// Whether `bytes` contain a portable material manifest within the ZIP entry limit.
|
||||
pub fn is_mcmat(bytes: &[u8]) -> bool {
|
||||
zip_has(bytes, "material.json")
|
||||
craft_mcmat::is_material(bytes, 100_000)
|
||||
}
|
||||
|
||||
pub fn is_mcraft(bytes: &[u8]) -> bool {
|
||||
zip_has(bytes, "package.json")
|
||||
let Ok(mut archive) = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 100_000, max_total_bytes: 0 }) else { return false };
|
||||
// A portable material may itself contain source.sbs, so it is not a source archive.
|
||||
if archive.has_duplicate_names() || archive.contains("material.json") {
|
||||
return false;
|
||||
}
|
||||
if archive.contains("package.json") {
|
||||
return true;
|
||||
}
|
||||
(0..archive.len()).any(|i| archive.entry_name(i).ok().flatten().is_some_and(|name| !name.contains('/') && name.to_ascii_lowercase().ends_with(".sbs")))
|
||||
}
|
||||
|
||||
/// A baked mesh map (`components` floats per texel, 0..1, rows top first) as a 16-bit PNG
|
||||
@@ -161,13 +168,14 @@ mod tests {
|
||||
|
||||
/// A `.mcmat` as MaterialCraft writes it (see its `formats/src/material.rs`).
|
||||
pub(crate) fn sample(version: u32) -> Vec<u8> {
|
||||
let source = if version >= 2 { "source.sbs" } else { "source.mcraft" };
|
||||
let manifest = serde_json::json!({
|
||||
"format": "craft-material", "version": version, "name": "Brick wall", "graph": "Brick_wall", "size": [4, 4],
|
||||
"maps": [{"usage": "baseColor", "file": "maps/baseColor.png", "colorSpace": "srgb"}],
|
||||
"parameters": [{"id": "wear", "label": "Wear", "default": {"t": "float", "v": 0.5}, "min": 0.0, "max": 1.0, "value": {"t": "float", "v": 0.25}}],
|
||||
"inputs": [{"identifier": "curvature", "kind": "gray"}],
|
||||
"presets": [{"name": "Worn", "values": {"wear": {"t": "float", "v": 1.0}}}],
|
||||
"source": "source.mcraft", "futureField": true
|
||||
"source": source, "futureField": true
|
||||
});
|
||||
let mut z = zip::ZipWriter::new(Cursor::new(Vec::new()));
|
||||
let o = zip::write::SimpleFileOptions::default();
|
||||
@@ -175,7 +183,7 @@ mod tests {
|
||||
z.write_all(manifest.to_string().as_bytes()).unwrap();
|
||||
z.start_file("maps/baseColor.png", o).unwrap();
|
||||
z.write_all(&mesh_map_png(4, 3, &[0.5; 48]).unwrap()).unwrap();
|
||||
z.start_file("source.mcraft", o).unwrap();
|
||||
z.start_file(source, o).unwrap();
|
||||
z.write_all(b"pkg").unwrap();
|
||||
z.finish().unwrap().into_inner()
|
||||
}
|
||||
@@ -195,15 +203,29 @@ mod tests {
|
||||
assert_eq!(material.maps[0].0, "baseColor");
|
||||
assert_eq!(crate::decode_image(&material.maps[0].1).unwrap().width, 4);
|
||||
assert_eq!(material.source, b"pkg");
|
||||
assert!(matches!(read_mcmat(&sample(2)), Err(FormatError::Unsupported(_))));
|
||||
let current = read_mcmat(&sample(2)).unwrap();
|
||||
assert_eq!(current.manifest.source, "source.sbs");
|
||||
assert_eq!(current.source, b"pkg");
|
||||
assert!(matches!(read_mcmat(&sample(3)), Err(FormatError::Unsupported(_))));
|
||||
assert!(read_mcmat(b"junk").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recognizes_both_generations_of_source_archive_without_confusing_portable_materials() {
|
||||
for entry in ["package.json", "package.sbs"] {
|
||||
let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
|
||||
zip.start_file(entry, zip::write::SimpleFileOptions::default()).unwrap();
|
||||
zip.write_all(b"source").unwrap();
|
||||
let bytes = zip.finish().unwrap().into_inner();
|
||||
assert!(is_mcraft(&bytes), "{entry}");
|
||||
}
|
||||
assert!(!is_mcraft(&sample(2)), "a portable material contains source.sbs too");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unbounded_map_list_is_refused() {
|
||||
let maps: Vec<_> = (0..=MAX_MAPS).map(|_| serde_json::json!({"usage": "baseColor", "file": "maps/baseColor.png"})).collect();
|
||||
let manifest =
|
||||
serde_json::json!({"format": FORMAT, "version": VERSION, "name": "n", "graph": "g", "size": [4, 4], "maps": maps, "source": "source.mcraft"});
|
||||
let manifest = serde_json::json!({"format": craft_mcmat::FORMAT, "version": VERSION, "name": "n", "graph": "g", "size": [4, 4], "maps": maps, "source": "source.mcraft"});
|
||||
let mut archive = zip::ZipWriter::new(Cursor::new(Vec::new()));
|
||||
let options = zip::write::SimpleFileOptions::default();
|
||||
archive.start_file("material.json", options).unwrap();
|
||||
@@ -218,31 +240,6 @@ mod tests {
|
||||
assert!(matches!(result, Err(FormatError::Malformed { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entry_read_stops_at_the_remaining_payload_budget() {
|
||||
let payload = vec![7u8; 1024];
|
||||
let mut input = Cursor::new(&payload);
|
||||
let mut total_bytes = MAX_TOTAL - 8;
|
||||
|
||||
let result = read_entry("maps/extra.png", &mut input, &mut total_bytes);
|
||||
|
||||
assert!(matches!(result, Err(FormatError::TooLarge(_))));
|
||||
assert_eq!(input.position(), 9, "only one byte beyond the remaining budget is needed to reject the entry");
|
||||
assert_eq!(total_bytes, MAX_TOTAL - 8, "a rejected entry does not consume the payload budget");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn payload_budget_accepts_the_exact_limit_and_empty_entries() {
|
||||
let mut total_bytes = MAX_TOTAL - 4;
|
||||
|
||||
let bytes = read_entry("map.png", Cursor::new([1, 2, 3, 4]), &mut total_bytes).unwrap();
|
||||
|
||||
assert_eq!(bytes, [1, 2, 3, 4]);
|
||||
assert_eq!(total_bytes, MAX_TOTAL);
|
||||
assert!(read_entry("empty.mcraft", Cursor::new([]), &mut total_bytes).unwrap().is_empty());
|
||||
assert!(matches!(read_entry("extra.png", Cursor::new([1]), &mut total_bytes), Err(FormatError::TooLarge(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mesh_maps_encode_as_16_bit() {
|
||||
let png = mesh_map_png(2, 1, &[0.0, 0.25, 0.5, 1.0]).unwrap();
|
||||
|
||||
@@ -28,9 +28,9 @@ fn parse(bytes: &[u8]) -> Result<(gltf::Document, Option<Vec<u8>>)> {
|
||||
let (json, blob) = if bytes.starts_with(b"glTF") {
|
||||
let mut glb = gltf::Glb::from_slice(bytes).map_err(|e| malformed(KIND, e))?;
|
||||
let blob = glb.bin.take().map(std::borrow::Cow::into_owned);
|
||||
(glb.json.into_owned(), blob)
|
||||
(glb.json, blob)
|
||||
} else {
|
||||
(bytes.to_vec(), None)
|
||||
(std::borrow::Cow::Borrowed(bytes), None)
|
||||
};
|
||||
let mut value: serde_json::Value = serde_json::from_slice(&json).map_err(|e| malformed(KIND, e))?;
|
||||
if let Some(scenes) = value.get_mut("scenes").and_then(serde_json::Value::as_array_mut) {
|
||||
@@ -40,8 +40,8 @@ fn parse(bytes: &[u8]) -> Result<(gltf::Document, Option<Vec<u8>>)> {
|
||||
}
|
||||
}
|
||||
}
|
||||
let json = serde_json::to_vec(&value).map_err(|e| malformed(KIND, e))?;
|
||||
let root = gltf::json::Root::from_slice(&json).map_err(|e| malformed(KIND, e))?;
|
||||
// Consume the DOM directly rather than serialising it back to a second full-size buffer.
|
||||
let root: gltf::json::Root = serde_json::from_value(value).map_err(|e| malformed(KIND, e))?;
|
||||
let document = gltf::Document::from_json(root).map_err(|e| malformed(KIND, e))?;
|
||||
Ok((document, blob))
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
//! Wavefront OBJ (via `tobj`): polygons fan-triangulated, one vertex per unique v/vt/vn.
|
||||
|
||||
use std::io::{BufReader, Read};
|
||||
|
||||
use texturecraft_geom::glam::{Vec2, Vec3, Vec4};
|
||||
|
||||
use super::{RawMesh, check_triangles, check_vertices};
|
||||
@@ -27,12 +29,15 @@ fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
|
||||
}
|
||||
|
||||
let library: String = names.iter().map(|name| format!("newmtl {name}\n")).collect();
|
||||
let source = format!("mtllib texturecraft-usemtl.mtl\n{text}");
|
||||
let prefix = "mtllib texturecraft-usemtl.mtl\n";
|
||||
|
||||
// Feed the prefix and the borrowed (or lossily converted) body as one reader: neither a
|
||||
// whole-file copy nor an extra string is needed before tobj parses it.
|
||||
let mut source = BufReader::new(prefix.as_bytes().chain(text.as_bytes()));
|
||||
|
||||
// Polygons stay whole so the wireframe can show them; `RawMesh::polygon` triangulates
|
||||
let options = tobj::LoadOptions { single_index: true, triangulate: false, ignore_points: true, ignore_lines: true };
|
||||
let (models, materials) =
|
||||
tobj::load_obj_buf(&mut source.as_bytes(), &options, |_| tobj::load_mtl_buf(&mut library.as_bytes())).map_err(|e| malformed(KIND, e))?;
|
||||
let (models, materials) = tobj::load_obj_buf(&mut source, &options, |_| tobj::load_mtl_buf(&mut library.as_bytes())).map_err(|e| malformed(KIND, e))?;
|
||||
let materials = materials.unwrap_or_default();
|
||||
|
||||
let mut raw = RawMesh::default();
|
||||
|
||||
@@ -1,78 +1,75 @@
|
||||
//! STL (binary and ascii). Facets keep their own corners, so shading is flat; there are no UVs.
|
||||
//! STL (binary and ASCII), read by the shared bounded mesh importer.
|
||||
|
||||
use texturecraft_geom::glam::Vec3;
|
||||
use texturecraft_geom::glam::{Vec3, Vec4};
|
||||
|
||||
use super::{RawMesh, check_triangles};
|
||||
use crate::{Result, malformed};
|
||||
use super::{MAX_VERTICES, RawMesh, check_triangles};
|
||||
use crate::{FormatError, Result};
|
||||
|
||||
const KIND: &str = "STL";
|
||||
|
||||
pub(crate) fn read(bytes: &[u8]) -> Result<RawMesh> {
|
||||
let count = bytes.get(80..84).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize);
|
||||
let binary_len = count.and_then(|c| c.checked_mul(50)).and_then(|n| n.checked_add(84));
|
||||
let looks_ascii = bytes.iter().position(|b| !b.is_ascii_whitespace()).and_then(|i| bytes.get(i..i + 5)) == Some(b"solid".as_slice());
|
||||
let tris = match (count, binary_len) {
|
||||
// Exact size wins: many binary files also start with "solid".
|
||||
(Some(c), Some(len)) if len == bytes.len() => binary(bytes, c)?,
|
||||
_ if looks_ascii => ascii(bytes)?,
|
||||
(Some(c), Some(len)) if len < bytes.len() => binary(bytes, c)?,
|
||||
_ => return Err(malformed(KIND, "neither a complete binary STL nor an ascii STL")),
|
||||
};
|
||||
let scene = craft_mesh::stl::read(bytes, &stl_limits()).map_err(|e| FormatError::Invalid(e.to_string()))?;
|
||||
let mut raw = RawMesh::default();
|
||||
check_triangles(0, tris.len())?;
|
||||
let mut normals = Vec::with_capacity(tris.len() * 3);
|
||||
for t in &tris {
|
||||
let n = (t[1] - t[0]).cross(t[2] - t[0]).try_normalize().unwrap_or(Vec3::Y);
|
||||
raw.positions.extend_from_slice(t);
|
||||
normals.extend_from_slice(&[n; 3]);
|
||||
raw.warnings = scene.warnings;
|
||||
|
||||
let mut tri_count = 0;
|
||||
let mut vertex_colors = Vec::new();
|
||||
let has_colors = !scene.materials.is_empty();
|
||||
for mesh in scene.meshes {
|
||||
// The shared model groups colored facets into parts. Position indices still reflect
|
||||
// source order, so merge the parts back into STL order for TextureCraft's raw mesh.
|
||||
let mut triangles = Vec::new();
|
||||
for part in mesh.parts {
|
||||
let material = part.material.and_then(|i| scene.materials.get(i));
|
||||
let color = material.map(|m| Vec4::from_array(m.base_color));
|
||||
for tri in part.triangles() {
|
||||
let first_position = tri.iter().map(|c| c.position).min().unwrap_or(u32::MAX);
|
||||
triangles.push((first_position, tri, color));
|
||||
}
|
||||
}
|
||||
triangles.sort_by_key(|(first_position, _, _)| *first_position);
|
||||
for (_, tri, color) in triangles {
|
||||
check_triangles(tri_count, 1)?;
|
||||
tri_count += 1;
|
||||
let mut points = [Vec3::ZERO; 3];
|
||||
for (i, corner) in tri.into_iter().enumerate() {
|
||||
let p = mesh.positions.get(corner.position as usize).ok_or_else(|| FormatError::Invalid(format!("{KIND}: position index out of range")))?;
|
||||
points[i] = Vec3::new(p[0] as f32, p[1] as f32, p[2] as f32);
|
||||
}
|
||||
// Recompute per-facet normals as TextureCraft's old importer did; stored STL
|
||||
// normals are often zero or disagree with the vertex winding.
|
||||
let normal = (points[1] - points[0]).cross(points[2] - points[0]).try_normalize().unwrap_or(Vec3::Y);
|
||||
raw.positions.extend_from_slice(&points);
|
||||
raw.normals.get_or_insert_with(Vec::new).extend_from_slice(&[normal; 3]);
|
||||
if has_colors {
|
||||
// The shared reader normalizes STL's color extensions to linear RGBA.
|
||||
// Uncolored facets alongside colored ones receive its neutral gray material.
|
||||
let color = color.unwrap_or(Vec4::new(0.6, 0.6, 0.6, 1.0));
|
||||
vertex_colors.extend_from_slice(&[color; 3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if raw.positions.is_empty() {
|
||||
return Err(crate::malformed(KIND, "no facets"));
|
||||
}
|
||||
if has_colors {
|
||||
raw.colors = Some(vertex_colors);
|
||||
}
|
||||
raw.normals = Some(normals);
|
||||
raw.groups.push(("Default".into(), String::new(), (0..raw.positions.len() as u32).collect()));
|
||||
Ok(raw)
|
||||
}
|
||||
|
||||
fn binary(bytes: &[u8], count: usize) -> Result<Vec<[Vec3; 3]>> {
|
||||
check_triangles(0, count)?;
|
||||
let mut out = Vec::with_capacity(count);
|
||||
for i in 0..count {
|
||||
let f = |k: usize| -> Result<f32> {
|
||||
let o = 84 + i * 50 + 12 + k * 4;
|
||||
let b = bytes.get(o..o + 4).ok_or_else(|| malformed(KIND, "truncated facet"))?;
|
||||
Ok(f32::from_le_bytes([b[0], b[1], b[2], b[3]]))
|
||||
};
|
||||
let v = |c: usize| -> Result<Vec3> { Ok(Vec3::new(f(c * 3)?, f(c * 3 + 1)?, f(c * 3 + 2)?)) };
|
||||
out.push([v(0)?, v(1)?, v(2)?]);
|
||||
}
|
||||
Ok(out)
|
||||
/// STL stores three positions per facet; retain the app's established triangle capacity.
|
||||
fn stl_limits() -> craft_mesh::Limits {
|
||||
craft_mesh::Limits { max_vertices: MAX_VERTICES, ..craft_mesh::Limits::default() }
|
||||
}
|
||||
|
||||
fn ascii(bytes: &[u8]) -> Result<Vec<[Vec3; 3]>> {
|
||||
let text = std::str::from_utf8(bytes).map_err(|_| malformed(KIND, "ascii STL is not text"))?;
|
||||
let mut out = Vec::new();
|
||||
let mut facet: Vec<Vec3> = Vec::new();
|
||||
let mut words = text.split_whitespace();
|
||||
while let Some(w) = words.next() {
|
||||
match w {
|
||||
"vertex" => {
|
||||
let mut c = [0.0f32; 3];
|
||||
for v in &mut c {
|
||||
let tok = words.next().ok_or_else(|| malformed(KIND, "truncated vertex"))?;
|
||||
*v = tok.parse().map_err(|_| malformed(KIND, format!("bad number \"{tok}\"")))?;
|
||||
}
|
||||
facet.push(Vec3::from(c));
|
||||
}
|
||||
"endfacet" => {
|
||||
for i in 1..facet.len().saturating_sub(1) {
|
||||
check_triangles(out.len(), 1)?;
|
||||
out.push([facet[0], facet[i], facet[i + 1]]);
|
||||
}
|
||||
facet.clear();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn shared_reader_accepts_the_app_vertex_limit() {
|
||||
let limits = super::stl_limits();
|
||||
assert_eq!(limits.max_vertices, super::MAX_VERTICES);
|
||||
assert_eq!(limits.max_triangles, texturecraft_geom::MAX_TRIANGLES);
|
||||
}
|
||||
if out.is_empty() {
|
||||
return Err(malformed(KIND, "no facets"));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
@@ -414,6 +414,43 @@ fn stl_ascii_and_binary() {
|
||||
assert_eq!(import_mesh_bytes("c.stl", &b).unwrap().mesh.triangle_count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stl_shared_reader_keeps_flat_normals_and_surfaces_shared_repairs_and_colors() {
|
||||
let mut bytes = stl_binary();
|
||||
// VisCAM/SolidView: bit 15 marks a facet color; red occupies bits 10..14.
|
||||
bytes[132..134].copy_from_slice(&0xfc00u16.to_le_bytes());
|
||||
// A third facet repeats that color after an uncolored facet. The shared reader groups its
|
||||
// parts by color, so the adapter must restore the source facet order.
|
||||
bytes.extend([0.0f32; 3].iter().flat_map(|v| v.to_le_bytes()));
|
||||
for v in [[0.0f32, 0.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 1.0]] {
|
||||
bytes.extend(v.iter().flat_map(|x| x.to_le_bytes()));
|
||||
}
|
||||
bytes.extend(0xfc00u16.to_le_bytes());
|
||||
// Some exporters leave the header count at zero despite having facets.
|
||||
bytes[80..84].copy_from_slice(&0u32.to_le_bytes());
|
||||
let imported = import_mesh_bytes("colored.stl", &bytes).unwrap();
|
||||
assert_eq!(imported.mesh.triangle_count(), 3);
|
||||
assert_eq!(imported.mesh.submeshes.len(), 1);
|
||||
assert_eq!(imported.mesh.submeshes[0].name, "colored");
|
||||
assert_eq!(
|
||||
imported.mesh.positions,
|
||||
vec![
|
||||
Vec3::ZERO,
|
||||
Vec3::X,
|
||||
Vec3::new(1.0, 1.0, 0.0),
|
||||
Vec3::ZERO,
|
||||
Vec3::new(1.0, 1.0, 0.0),
|
||||
Vec3::Y,
|
||||
Vec3::Z,
|
||||
Vec3::new(1.0, 0.0, 1.0),
|
||||
Vec3::new(1.0, 1.0, 1.0)
|
||||
]
|
||||
);
|
||||
assert_eq!(imported.mesh.normals, vec![Vec3::Z; 9]);
|
||||
assert!(imported.mesh.colors.iter().any(|c| c.x > 0.99 && c.y < 0.01 && c.z < 0.01));
|
||||
assert!(imported.report.warnings.iter().any(|w| w.contains("counts no facets")));
|
||||
}
|
||||
|
||||
/// Every prefix and some corruptions of a valid file: errors, never panics (and never an
|
||||
/// error from the panic guard, which would mean a parser panicked).
|
||||
pub(crate) fn hammer(name: &str, good: &[u8]) {
|
||||
|
||||
@@ -2033,7 +2033,8 @@ fn link_properties(app: &mut TextureCraftApp, ui: &mut Ui, layer: LayerId, link:
|
||||
app.cook_link(layer);
|
||||
}
|
||||
if ui.button(tr("Open in MaterialCraft")).clicked() {
|
||||
let path = std::env::temp_dir().join(format!("{}.mcraft", texturecraft_engine::formats::export::sanitize_file_stem(&link.name)));
|
||||
let ext = texturecraft_engine::link_cmds::source_extension(&link.package);
|
||||
let path = std::env::temp_dir().join(format!("{}.{ext}", texturecraft_engine::formats::export::sanitize_file_stem(&link.name)));
|
||||
let opened = app
|
||||
.session
|
||||
.link_package(layer)
|
||||
|
||||
@@ -206,7 +206,13 @@ mod tests {
|
||||
|
||||
let mut answers = HashMap::new();
|
||||
let start = Instant::now();
|
||||
while answers.len() < requests_sent && start.elapsed() < Duration::from_secs(120) {
|
||||
// Instrumented orb baking can take several minutes on coverage runners.
|
||||
let timeout = if std::env::var_os("LLVM_PROFILE_FILE").is_some() || std::env::var("RUSTFLAGS").is_ok_and(|f| f.contains("instrument-coverage")) {
|
||||
Duration::from_secs(600)
|
||||
} else {
|
||||
Duration::from_secs(120)
|
||||
};
|
||||
while answers.len() < requests_sent && start.elapsed() < timeout {
|
||||
answers.extend(worker.poll());
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
|
||||
@@ -155,6 +155,15 @@ fn high_dpi_smart_material_hover_stays_within_the_renderer_limit() {
|
||||
check_high_dpi_orb_hover(1, "Steel Painted Worn");
|
||||
}
|
||||
|
||||
// Coverage instrumentation makes the 512px software-rendered preview substantially slower.
|
||||
fn high_dpi_worker_timeout() -> Duration {
|
||||
if std::env::var_os("LLVM_PROFILE_FILE").is_some() || std::env::var("RUSTFLAGS").is_ok_and(|f| f.contains("instrument-coverage")) {
|
||||
Duration::from_secs(600)
|
||||
} else {
|
||||
WORKER_TIMEOUT
|
||||
}
|
||||
}
|
||||
|
||||
fn check_high_dpi_orb_hover(shelf_tab: usize, label: &str) {
|
||||
use texturecraft_engine::orb_preview::MAX_PREVIEW_SIZE_PX;
|
||||
|
||||
@@ -166,7 +175,7 @@ fn check_high_dpi_orb_hover(shelf_tab: usize, label: &str) {
|
||||
|
||||
let expected = Some([MAX_PREVIEW_SIZE_PX; 2]);
|
||||
let started = Instant::now();
|
||||
while largest_drawn_texture(&harness, BIG_PREVIEW_SIZE) != expected && started.elapsed() < WORKER_TIMEOUT {
|
||||
while largest_drawn_texture(&harness, BIG_PREVIEW_SIZE) != expected && started.elapsed() < high_dpi_worker_timeout() {
|
||||
harness.run_steps(1);
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
|
||||
@@ -121,10 +121,10 @@ on the CPU by `render::displace` from the composited height.
|
||||
|
||||
A fill can carry a `MaterialLink` (`core::layer`): a MaterialCraft graph whose cooked maps are the
|
||||
fill's image sources. `engine::link_cmds` imports `.mcmat` files (`formats::mcmat`), keeps the
|
||||
`.mcraft` package as a resource, and re-cooks by running `materialcraft-cli` with the link's
|
||||
`.mcraft` or `.sbs` source as a resource, and re-cooks its named graph by running `materialcraft-cli` with the link's
|
||||
values, the texture set's resolution and its baked mesh maps (`LinkJob::run`, on a worker thread
|
||||
in the UI). Every cook stores its maps under fresh resource names, so undo, redo and the decoded
|
||||
image cache never disagree. The apps share the file format and the command line, never code.
|
||||
image cache never disagree. The apps share the `craft-mcmat` format library and the command line; neither depends on the other app.
|
||||
|
||||
## Commands and automation
|
||||
|
||||
|
||||
@@ -325,8 +325,9 @@ of a second.
|
||||
A `.mcmat` portable material (MaterialCraft's format, read here by `formats::mcmat`; the manifest is
|
||||
documented in MaterialCraft's `docs/control-protocol.md` § Portable materials) becomes a fill layer
|
||||
whose channel sources are its cooked maps and whose `link` (shown by `project.inspect`) holds the
|
||||
graph's exposed inputs, presets and image inputs. The apps share the format and the command line,
|
||||
not code.
|
||||
graph's exposed inputs, presets and image inputs. Both apps use the shared `craft-mcmat` container.
|
||||
Versions 1 and 2 are accepted; version 1 contains a legacy JSON `.mcraft` source that current
|
||||
MaterialCraft cannot reopen, so its cooked maps remain usable but re-cooking needs a compatible older version.
|
||||
|
||||
| Command | Params |
|
||||
|---|---|
|
||||
@@ -334,9 +335,9 @@ not code.
|
||||
| `material.link.set` | `{layer?, id, value, cook?: true}`: a number, `[..]`, a bool, `"#rrggbb"` or a typed `{"t", "v"}`; re-cooks unless `cook: false` |
|
||||
| `material.link.preset` | `{layer?, name, cook?: true}` |
|
||||
| `material.link.cook` | `{layer?}`: re-cook at the texture set's resolution with its baked mesh maps (after a bake or a resize) |
|
||||
| `material.link.saveSource` | `{layer?, path}`: the `.mcraft` package, to open in MaterialCraft |
|
||||
| `material.link.saveSource` | `{layer?, path}`: the `.mcraft` or `.sbs` source, to open in MaterialCraft |
|
||||
|
||||
Re-cooks run `materialcraft-cli run <package> file.exportMaterial {path, size, values, inputs}`:
|
||||
Re-cooks use `materialcraft-cli batch` to open the saved graph by name and run `file.exportMaterial` with its size, values and inputs:
|
||||
`$MATERIALCRAFT_CLI`, else `materialcraft-cli` next to the TextureCraft executable, else on `PATH`
|
||||
(the app is `$MATERIALCRAFT` / `materialcraft`). Baked maps feed graph input nodes whose identifier
|
||||
matches the map's shared id (`ambientOcclusion`, `curvature`, `thickness`, `position`,
|
||||
|
||||
Reference in new issue
Block a user