From b5a659e6b892a9c26c36bbf9ca6e173fa18ce5d5 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 1 Nov 2026 11:40:02 +0700 Subject: [PATCH] Clear coat in USD export The UsdPreviewSurface of each exported material gets clearcoat and clearcoatRoughness from two more UsdUVTexture maps (Clearcoat, ClearcoatRoughness; raw), rendered from the Coat weight and Coat roughness channels, as the GLB export already carries them. - a set without coat channels produces no coat maps; a black coat drops both maps, like a black emissive map - usda, usdc and usdz; File > Export mesh, file.exportMesh and the MCP mesh_export tool - tests: the USD round trip exports a coated and an uncoated (black) material in all three encodings --- README.md | 2 +- crates/formats/README.md | 3 ++- crates/formats/src/mesh_export/usd.rs | 21 +++++++++++++++++---- crates/formats/src/mesh_import/usd/tests.rs | 19 ++++++++++++++++--- docs/features.md | 2 +- 5 files changed, 37 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index bbfb1249..7a5850ba 100644 --- a/README.md +++ b/README.md @@ -190,7 +190,7 @@ from our code against public documentation. **What's missing:** - hardware ray tracing in Rendering mode; - pen tilt (the windowing layer doesn't report it yet); -- cameras from FBX, COLLADA and USD files (glTF cameras are imported), and clear coat in USD export; +- cameras from FBX, COLLADA and USD files (glTF cameras are imported); - Alembic files in the older HDF5 container, and Alembic animation (the first sample is read); - displacement in shadows and the path tracer (the viewport displaces); - a native-speaker review of the translations; diff --git a/crates/formats/README.md b/crates/formats/README.md index 6c09990a..62f9dc5b 100644 --- a/crates/formats/README.md +++ b/crates/formats/README.md @@ -56,7 +56,8 @@ pub struct UsdExport { pub bytes: Vec, pub textures: Vec<(String, Vec)> USD: one `Mesh` prim (vertex normals and `primvars:st`, `subdivisionScheme = "none"`), one `materialBind` GeomSubset per material when there are several, and a UsdPreviewSurface material per material with a UsdUVTexture per map (base color and emissive sRGB; roughness, metallic, -OpenGL normal with scale 2 / bias -1, opacity raw; black emissive maps dropped). Meters, Y up. +OpenGL normal with scale 2 / bias -1, opacity, clear coat and clear coat roughness raw; black +emissive maps dropped, and both clear coat maps when the coat is black). Meters, Y up. `.usdc` is a Crate 0.8.0 file; `.usdz` is that layer first plus the PNGs, stored uncompressed at 64-byte aligned offsets; `.usda` / `.usdc` reference `__.png` next to the layer. diff --git a/crates/formats/src/mesh_export/usd.rs b/crates/formats/src/mesh_export/usd.rs index 75f0ae46..1232373f 100644 --- a/crates/formats/src/mesh_export/usd.rs +++ b/crates/formats/src/mesh_export/usd.rs @@ -2,7 +2,8 @@ //! package of a `.usdc` layer and its PNG textures). One `Mesh` prim (points, normals and //! `primvars:st` per vertex, triangles), one `materialBind` GeomSubset per material when there //! are several, and a UsdPreviewSurface material per material whose maps (base color, -//! roughness, metallic, normal, emissive, opacity) are UsdUVTexture shaders. Meters, Y up. +//! roughness, metallic, normal, emissive, opacity, clear coat and its roughness) are +//! UsdUVTexture shaders. Meters, Y up. use std::fmt::Write as _; use std::io::Write as _; @@ -45,13 +46,15 @@ pub struct UsdExport { /// A UsdPreviewSurface input fed by a texture: (file stem, input, input type, texture output, /// sRGB). -const SLOTS: [(&str, &str, &str, &str, bool); 6] = [ +const SLOTS: [(&str, &str, &str, &str, bool); 8] = [ ("BaseColor", "diffuseColor", "color3f", "rgb", true), ("Roughness", "roughness", "float", "r", false), ("Metallic", "metallic", "float", "r", false), ("Normal", "normal", "normal3f", "rgb", false), ("Emissive", "emissiveColor", "color3f", "rgb", true), ("Opacity", "opacity", "float", "r", false), + ("Clearcoat", "clearcoat", "float", "r", false), + ("ClearcoatRoughness", "clearcoatRoughness", "float", "r", false), ]; /// Texture export options for [`usd_bytes`]: one 8-bit PNG per UsdPreviewSurface input, named @@ -66,6 +69,8 @@ pub fn usd_export_options() -> ExportOptions { "Metallic" => gray(ch(Channel::Metallic, 0)), "Normal" => rgb(|k| virt(VirtualMap::NormalOpenGl, k)), "Emissive" => rgb(|k| ch(Channel::Emissive, k)), + "Clearcoat" => gray(ch(Channel::CoatWeight, 0)), + "ClearcoatRoughness" => gray(ch(Channel::CoatRoughness, 0)), _ => gray(ch(Channel::Opacity, 0)), }; MapSpec { file: stem.into(), format: FileFormat::Png, bit_depth: 8, channels } @@ -290,10 +295,18 @@ pub fn usd_bytes(mesh: &Mesh, name: &str, textures: &[Vec], format let mut names = Vec::with_capacity(mesh.materials.len()); for (m, material) in mesh.materials.iter().enumerate() { let mut slots = Vec::new(); - for f in textures.get(m).map_or(&[][..], Vec::as_slice) { + let maps = textures.get(m).map_or(&[][..], Vec::as_slice); + // Without a coat its roughness means nothing. + let no_coat = maps.iter().find(|f| f.name == "Clearcoat.png").is_none_or(|f| is_black(&f.bytes)); + for f in maps { let slot_stem = f.name.strip_suffix(".png").unwrap_or_default(); let Some(slot) = SLOTS.iter().position(|s| s.0 == slot_stem) else { continue }; - if slot_stem == "Emissive" && is_black(&f.bytes) { + let skip = match slot_stem { + "Emissive" => is_black(&f.bytes), + "Clearcoat" | "ClearcoatRoughness" => no_coat, + _ => false, + }; + if skip { continue; } let file = format!("{stem}_{}_{slot_stem}.png", sanitize_file_stem(material)); diff --git a/crates/formats/src/mesh_import/usd/tests.rs b/crates/formats/src/mesh_import/usd/tests.rs index 3f8f9663..664b3576 100644 --- a/crates/formats/src/mesh_import/usd/tests.rs +++ b/crates/formats/src/mesh_import/usd/tests.rs @@ -296,8 +296,15 @@ fn usda_usdc_and_usdz_round_trip_with_textures() { file("Roughness.png", png([128; 3])), file("Normal.png", png([128, 128, 255])), file("Emissive.png", png([0; 3])), + file("Clearcoat.png", png([0; 3])), + file("ClearcoatRoughness.png", png([30; 3])), + ], + vec![ + file("Metallic.png", png([255; 3])), + file("Opacity.png", png([255; 3])), + file("Clearcoat.png", png([255; 3])), + file("ClearcoatRoughness.png", png([30; 3])), ], - vec![file("Metallic.png", png([255; 3])), file("Opacity.png", png([255; 3]))], vec![], ]; let usda = round_trip(&m, UsdFormat::Usda, "usda", &textures); @@ -306,12 +313,18 @@ fn usda_usdc_and_usdz_round_trip_with_textures() { text.contains("UsdPreviewSurface") && text.contains("@./Test_scene_Red_BaseColor.png@") && !text.contains("Emissive.png"), "black emissive dropped" ); + assert!( + text.contains("Materials/Blue/Clearcoat.outputs:r>") + && text.contains("Materials/Blue/ClearcoatRoughness.outputs:r>") + && !text.contains("Red_Clearcoat"), + "a coat exports with its roughness; no coat (black) drops both" + ); round_trip(&m, UsdFormat::Usdc, "usdc", &textures); let usdz = round_trip(&m, UsdFormat::Usdz, "usdz", &textures); let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&usdz)).unwrap(); let names: Vec = zip.file_names().map(str::to_string).collect(); assert_eq!(names.first().map(String::as_str), Some("Test_scene.usdc")); - assert_eq!(names.len(), 1 + 5, "the layer and every non-black texture"); + assert_eq!(names.len(), 1 + 7, "the layer and every non-black texture"); for i in 0..zip.len() { let mut f = zip.by_index(i).unwrap(); assert_eq!(f.compression(), zip::CompressionMethod::Stored); @@ -324,7 +337,7 @@ fn usda_usdc_and_usdz_round_trip_with_textures() { } // usda and usdc keep their textures for writing next to the layer. let out = usd_bytes(&m, "Test scene", &textures, UsdFormat::Usda).unwrap(); - assert_eq!(out.textures.len(), 5); + assert_eq!(out.textures.len(), 7); assert!(usd_bytes(&Mesh::default(), "x", &[], UsdFormat::Usdc).is_err()); } diff --git a/docs/features.md b/docs/features.md index 5df0090e..86a2c72e 100644 --- a/docs/features.md +++ b/docs/features.md @@ -69,7 +69,7 @@ Everything TextureCraft does today, by area. What's still missing is in [`ROADMA override and OpenGL or DirectX normals. - **Output template editor:** build your own presets with per-channel packing and converted maps, and share them as JSON. -- **Mesh export** to OBJ, textured GLB (with clear coat and sheen) and USD. +- **Mesh export** to OBJ, textured GLB (with clear coat and sheen) and USD (with clear coat). ## The viewport