mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-21 20:08:15 +00:00
Use shared MaterialX parser and writer
This commit is contained in:
Generated
+28
-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=b8f70b46ee0c5fbfd552cbe2a4dae68652519153#b8f70b46ee0c5fbfd552cbe2a4dae68652519153"
|
||||
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=b8f70b46ee0c5fbfd552cbe2a4dae68652519153#b8f70b46ee0c5fbfd552cbe2a4dae68652519153"
|
||||
|
||||
[[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=b8f70b46ee0c5fbfd552cbe2a4dae68652519153#b8f70b46ee0c5fbfd552cbe2a4dae68652519153"
|
||||
dependencies = [
|
||||
"fixed_decimal",
|
||||
"icu_decimal",
|
||||
@@ -799,17 +799,26 @@ 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=b8f70b46ee0c5fbfd552cbe2a4dae68652519153#b8f70b46ee0c5fbfd552cbe2a4dae68652519153"
|
||||
dependencies = [
|
||||
"egui",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-materialx"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=b8f70b46ee0c5fbfd552cbe2a4dae68652519153#b8f70b46ee0c5fbfd552cbe2a4dae68652519153"
|
||||
dependencies = [
|
||||
"craft-xml",
|
||||
"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=b8f70b46ee0c5fbfd552cbe2a4dae68652519153#b8f70b46ee0c5fbfd552cbe2a4dae68652519153"
|
||||
dependencies = [
|
||||
"craft-keymap",
|
||||
"craft-quit",
|
||||
@@ -822,7 +831,7 @@ dependencies = [
|
||||
[[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=b8f70b46ee0c5fbfd552cbe2a4dae68652519153#b8f70b46ee0c5fbfd552cbe2a4dae68652519153"
|
||||
dependencies = [
|
||||
"dispatch2",
|
||||
"egui",
|
||||
@@ -833,7 +842,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=b8f70b46ee0c5fbfd552cbe2a4dae68652519153#b8f70b46ee0c5fbfd552cbe2a4dae68652519153"
|
||||
dependencies = [
|
||||
"craft-fonts-build",
|
||||
"craft-i18n",
|
||||
@@ -854,7 +863,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=b8f70b46ee0c5fbfd552cbe2a4dae68652519153#b8f70b46ee0c5fbfd552cbe2a4dae68652519153"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dunce",
|
||||
@@ -881,7 +890,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=b8f70b46ee0c5fbfd552cbe2a4dae68652519153#b8f70b46ee0c5fbfd552cbe2a4dae68652519153"
|
||||
dependencies = [
|
||||
"egui",
|
||||
"objc2 0.6.5",
|
||||
@@ -889,6 +898,15 @@ dependencies = [
|
||||
"wayland-client",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=b8f70b46ee0c5fbfd552cbe2a4dae68652519153#b8f70b46ee0c5fbfd552cbe2a4dae68652519153"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.5.2"
|
||||
@@ -4369,6 +4387,7 @@ name = "texturecraft-formats"
|
||||
version = "0.6.1"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"craft-materialx",
|
||||
"exr",
|
||||
"flate2",
|
||||
"gltf",
|
||||
|
||||
+9
-8
@@ -66,20 +66,21 @@ 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 = "b8f70b46ee0c5fbfd552cbe2a4dae68652519153" }
|
||||
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "b8f70b46ee0c5fbfd552cbe2a4dae68652519153", features = ["serde"] }
|
||||
craft-materialx = { git = "https://github.com/storytold/craft-libs", rev = "b8f70b46ee0c5fbfd552cbe2a4dae68652519153" }
|
||||
# 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 = "b8f70b46ee0c5fbfd552cbe2a4dae68652519153", 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 = "b8f70b46ee0c5fbfd552cbe2a4dae68652519153", 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 = "b8f70b46ee0c5fbfd552cbe2a4dae68652519153" }
|
||||
# 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 = "b8f70b46ee0c5fbfd552cbe2a4dae68652519153", 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 = "b8f70b46ee0c5fbfd552cbe2a4dae68652519153", 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 = "b8f70b46ee0c5fbfd552cbe2a4dae68652519153" }
|
||||
|
||||
# egui 0.37 and winit: the suite's forks (storytold/egui, storytold/winit), as craft-libs pins them.
|
||||
[patch.crates-io]
|
||||
|
||||
@@ -9,6 +9,7 @@ license.workspace = true
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-materialx = { 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)]
|
||||
@@ -456,6 +165,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" />"#, "");
|
||||
|
||||
Reference in New Issue
Block a user