From 76e8c3b11684251417a88a21d3bf780bb355fe60 Mon Sep 17 00:00:00 2001 From: MistyMystery Date: Wed, 4 Nov 2026 22:17:46 +0400 Subject: [PATCH 1/4] Import a folder, files or zip of PBR textures as one fill layer Maps are found by file name (base colour, roughness, glossiness, metallic, normal with OpenGL/DirectX detection, height, occlusion, emissive, opacity, packed ORM). Adds file.importPbrFolder and file.scanPbrFolder. --- crates/engine/src/commands.rs | 1 + crates/engine/src/lib.rs | 1 + crates/engine/src/pbr_cmds.rs | 525 ++++++++++++++++++++++++++++++ crates/engine/tests/pbr_import.rs | 349 ++++++++++++++++++++ crates/formats/src/image_res.rs | 23 ++ crates/formats/src/lib.rs | 4 +- crates/formats/src/pbr_maps.rs | 495 ++++++++++++++++++++++++++++ crates/formats/src/zip_read.rs | 84 +++++ 8 files changed, 1481 insertions(+), 1 deletion(-) create mode 100644 crates/engine/src/pbr_cmds.rs create mode 100644 crates/engine/tests/pbr_import.rs create mode 100644 crates/formats/src/pbr_maps.rs create mode 100644 crates/formats/src/zip_read.rs diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs index 94bb350c..c45f4821 100644 --- a/crates/engine/src/commands.rs +++ b/crates/engine/src/commands.rs @@ -71,6 +71,7 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(crate::bake_cmds::specs()); v.extend(crate::bake_paint_cmds::specs()); v.extend(crate::materialx_cmds::specs()); + v.extend(crate::pbr_cmds::specs()); v.extend(crate::file_cmds::specs()); v.extend(crate::preset_cmds::specs()); v.extend(crate::resource_cmds::specs()); diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index ed07d645..ab954994 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -28,6 +28,7 @@ pub mod mask_cmds; pub mod material_lib; pub mod materials; pub mod materialx_cmds; +pub mod pbr_cmds; pub mod meshmap_cmds; pub mod painting; pub mod path_tool; diff --git a/crates/engine/src/pbr_cmds.rs b/crates/engine/src/pbr_cmds.rs new file mode 100644 index 00000000..19b62986 --- /dev/null +++ b/crates/engine/src/pbr_cmds.rs @@ -0,0 +1,525 @@ +//! `file.importPbrFolder` and `file.scanPbrFolder`: a folder (or selected files, or a `.zip`) of +//! PBR texture maps becomes one fill layer, like `file.importMaterialX` does for a `.mtlx`. +//! +//! The maps are found by file name ([`texturecraft_formats::pbr_maps`]: albedo, roughness, +//! metallic, normal with its OpenGL / DirectX convention, height, occlusion, emissive, opacity, +//! packed ORM). A packed ORM image is split into three single-channel resources, a glossiness +//! map is inverted into roughness, and every other map keeps its original bytes as the resource. +//! The folder is untrusted: symbolic links and other non-regular files are not followed, every +//! file and the whole import are size-capped, and an image that cannot be decoded is skipped with a +//! warning while the rest is imported. The whole import is one undo step. A programmatic call never +//! asks: when several files name one map the best is used and the others are listed in +//! `candidates`, so a caller can scan first (`file.scanPbrFolder`) and pass `choose`. + +use std::collections::BTreeMap; +use std::sync::Arc; + +use serde_json::{Value, json}; +use texturecraft_core::{Channel, Fill, Layer, LayerKind, NormalFormat, Projection, Source}; +use texturecraft_formats::pbr_maps::{self, MapKind, PbrSet}; + +use crate::commands::{CommandSpec, always, bad, strp}; +use crate::{EngineError, Result, Session, cmd}; + +/// Most files looked at in one folder or archive. +pub const MAX_FILES: usize = 4096; +/// Largest single image file read. +pub const MAX_FILE_BYTES: u64 = 256 << 20; +/// Largest decoded image (width × height). +pub const MAX_IMAGE_TEXELS: u64 = 64 << 20; +/// Largest sum of decoded texels in one import. +pub const MAX_TOTAL_TEXELS: u64 = 320 << 20; + +pub fn specs() -> Vec { + vec![ + cmd!( + "file.importPbrFolder", + "Import PBR texture folder...", + ["File"], + None, + "{path: a folder or .zip of PBR maps, or files: [paths], name?: layer name, normalFormat?: auto|openGl|directX (auto: from _gl/_dx in the name, else openGl; a value forces it), projection?: uv|triplanar|planar..., scale?, choose?: {map id: file} for maps with several candidates} → {layer, name, maps, candidates, normalFormat, channels, resources, ignored, warnings}: one fill layer; maps are found by file name (baseColor, roughness, glossiness, metallic, normal, height, ambientOcclusion, emissive, opacity, orm); packed ORM is split, glossiness inverted", + always, + import + ), + cmd!( + "file.scanPbrFolder", + "Scan PBR texture folder", + [], + None, + "{path | files} → {name, maps, candidates, normalFormat, ignored}: what file.importPbrFolder would use, changing nothing", + always, + scan + ), + ] +} + +/// One file offered to the importer: its name (folders joined by `/`) and size. +pub type FileList = Vec<(String, u64)>; +/// Reads a file of the list by name. +pub type Loader<'a> = &'a dyn Fn(&str) -> std::result::Result, String>; + +/// How to import. +#[derive(Clone, Debug)] +pub struct PbrOptions { + pub name: Option, + /// Forces every normal map's convention; `None` reads it from the file name (default OpenGL). + pub normal_format: Option, + pub projection: Projection, + /// Files chosen by the caller where several name one map. + pub choose: BTreeMap, +} + +impl Default for PbrOptions { + fn default() -> Self { + PbrOptions { name: None, normal_format: None, projection: Projection::default(), choose: BTreeMap::new() } + } +} + +/// The detection for a file list with the caller's choices applied. +fn detect_with(files: &FileList, choose: &BTreeMap) -> Result { + let mut set = pbr_maps::detect(files.iter().map(|(n, s)| (n.as_str(), *s))); + for (kind, file) in choose { + let list = set.candidates.get(kind).ok_or_else(|| EngineError::Other(format!("choose: no file names the map {}", kind.id())))?; + let pick = list.iter().find(|c| c.name == *file).ok_or_else(|| EngineError::Other(format!("choose: {file} is not a candidate for {}", kind.id())))?.clone(); + // The previously chosen file becomes an ignored one, the picked one stops being ignored. + if let Some(old) = set.chosen.insert(*kind, pick.clone()) { + if old.name != pick.name { + set.ignored.retain(|(n, _)| *n != pick.name); + set.ignored.push((old.name, format!("another file is used for {}", kind.id()))); + } + } + } + Ok(set) +} + +fn format_name(f: NormalFormat) -> &'static str { + match f { + NormalFormat::OpenGl => "openGl", + NormalFormat::DirectX => "directX", + } +} + +/// The convention used for the chosen normal map. +fn normal_format_for(set: &PbrSet, forced: Option) -> NormalFormat { + forced.or_else(|| set.chosen.get(&MapKind::Normal).and_then(|c| c.detected.normal)).unwrap_or(NormalFormat::OpenGl) +} + +fn report(set: &PbrSet, forced: Option) -> Value { + let maps: BTreeMap<&str, &str> = set.chosen.iter().map(|(k, c)| (k.id(), c.name.as_str())).collect(); + let candidates: BTreeMap<&str, Vec<&str>> = + set.candidates.iter().filter(|(_, v)| v.len() > 1).map(|(k, v)| (k.id(), v.iter().map(|c| c.name.as_str()).collect())).collect(); + json!({ + "maps": maps, + "candidates": candidates, + "normalFormat": format_name(normal_format_for(set, forced)), + "ignored": set.ignored.iter().map(|(n, why)| json!([n, why])).collect::>(), + }) +} + +/// How a channel's image is made from the file chosen for a map. +#[derive(Clone, Copy, PartialEq)] +enum Take { + /// The file as it is. + Whole, + /// One colour channel (0 = red) of a packed image. + Channel(usize), + /// 1 − red: glossiness as roughness. + Inverted, +} + +impl Session { + /// Import the maps found among `files` as one fill layer on the active texture set; `load` + /// reads a file by name. Returns the reply of `file.importPbrFolder`. + pub fn import_pbr(&mut self, files: &FileList, load: Loader, opts: &PbrOptions) -> Result { + let set = detect_with(files, &opts.choose)?; + if set.is_empty() { + return Err(EngineError::Other(format!( + "file.importPbrFolder: no PBR texture maps found among {} files (names such as albedo, roughness, metallic, normal, height, ao)", + files.len() + ))); + } + let ts = self.ts(); + let available = self.doc.project.texture_set(ts).map(|t| t.channel_list()).unwrap_or_default(); + let nf = normal_format_for(&set, opts.normal_format); + let mut warnings: Vec = Vec::new(); + + // Which file feeds which channel, and how. + let mut plan: Vec<(Channel, MapKind, Take)> = Vec::new(); + let has = |k: MapKind| set.chosen.contains_key(&k); + let direct = [ + (MapKind::BaseColor, Channel::BaseColor), + (MapKind::Normal, Channel::Normal), + (MapKind::Height, Channel::Height), + (MapKind::Emissive, Channel::Emissive), + (MapKind::Opacity, Channel::Opacity), + ]; + for (kind, channel) in direct { + if has(kind) { + plan.push((channel, kind, Take::Whole)); + } + } + // Separate files win; a packed image fills what they leave open (R = occlusion, G = roughness, B = metallic). + let orm = has(MapKind::PackedOrm); + for (channel, kind, index) in [(Channel::AmbientOcclusion, MapKind::AmbientOcclusion, 0), (Channel::Roughness, MapKind::Roughness, 1), (Channel::Metallic, MapKind::Metallic, 2)] { + if has(kind) { + plan.push((channel, kind, Take::Whole)); + } else if channel == Channel::Roughness && has(MapKind::Glossiness) { + plan.push((channel, MapKind::Glossiness, Take::Inverted)); + } else if orm { + plan.push((channel, MapKind::PackedOrm, Take::Channel(index))); + } + } + if orm && plan.iter().all(|(_, k, _)| *k != MapKind::PackedOrm) { + warnings.push("the packed ORM image was not used: separate occlusion, roughness and metallic maps exist".into()); + } + if has(MapKind::Glossiness) && has(MapKind::Roughness) { + warnings.push("the glossiness map was not used: a roughness map exists".into()); + } + + let mut fill = Fill { projection: opts.projection.clone(), ..Fill::default() }; + let mut added: Vec<(String, Arc>)> = Vec::new(); + let mut decoded = Vec::new(); + let mut channels: Vec = Vec::new(); + let mut total_texels = 0u64; + // Decoded source files by name (a packed image feeds three channels). + let mut sources: BTreeMap>, texturecraft_formats::DecodedImage)>> = BTreeMap::new(); + // Resource per (file, how) so a file used twice is stored once. + let mut made: BTreeMap<(String, usize), String> = BTreeMap::new(); + + let mut uses: BTreeMap = BTreeMap::new(); + for (_, kind, _) in &plan { + if let Some(c) = set.chosen.get(kind) { + *uses.entry(c.name.clone()).or_default() += 1; + } + } + for (channel, kind, take) in plan { + if !available.contains(&channel) { + warnings.push(format!("{} was skipped: this texture set has no such channel", channel.id())); + continue; + } + let Some(chosen) = set.chosen.get(&kind) else { continue }; + let file = chosen.name.clone(); + let slot = match take { + Take::Whole => 9, + Take::Inverted => 8, + Take::Channel(c) => c, + }; + let resource = if let Some(name) = made.get(&(file.clone(), slot)) { + name.clone() + } else { + if !sources.contains_key(&file) { + let loaded = load(&file).and_then(|bytes| { + if bytes.len() as u64 > MAX_FILE_BYTES { + return Err(format!("the file is over {} MB", MAX_FILE_BYTES >> 20)); + } + let image = texturecraft_formats::decode_image_within(&bytes, MAX_IMAGE_TEXELS).map_err(|e| e.to_string())?; + total_texels = total_texels.saturating_add(u64::from(image.width) * u64::from(image.height)); + if total_texels > MAX_TOTAL_TEXELS { + return Err("the textures together are too large to import".into()); + } + Ok((Arc::new(bytes), image)) + }); + match loaded { + Ok(v) => sources.insert(file.clone(), Some(v)), + Err(e) => { + warnings.push(format!("{} was skipped: {file}: {e}", channel.id())); + sources.insert(file.clone(), None) + } + }; + } + let remaining = uses.entry(file.clone()).or_default(); + *remaining = remaining.saturating_sub(1); + let last_use = *remaining == 0; + let leaf = file.rsplit(['/', '\\']).next().unwrap_or(&file).to_string(); + let stem = leaf.rsplit_once('.').map_or(leaf.as_str(), |(s, _)| s).to_string(); + let (res_name, res_bytes, res_image) = match take { + Take::Whole => { + // The file itself; its decoded image moves into the session on its last use. + let entry = if last_use { sources.remove(&file).flatten() } else { sources.get(&file).cloned().flatten() }; + let Some((bytes, image)) = entry else { continue }; + (leaf, bytes, image) + } + Take::Channel(_) | Take::Inverted => { + let Some(Some((_, image))) = sources.get(&file) else { continue }; + let (c, suffix, invert) = match take { + Take::Channel(c) => (c, ["ao", "roughness", "metallic"].get(c).copied().unwrap_or("channel"), false), + _ => (0, "roughness", true), + }; + let values = image.rgba.iter().map(|p| { + let v = p.get(c).copied().unwrap_or(0.0); + if invert { 1.0 - v } else { v } + }); + let png = texturecraft_formats::gray16_png(image.width, image.height, values).map_err(|e| EngineError::Other(e.to_string()))?; + let again = texturecraft_formats::decode_image(&png).map_err(|e| EngineError::Other(e.to_string()))?; + (format!("{stem}_{suffix}.png"), Arc::new(png), again) + } + }; + let base = texturecraft_formats::sanitize_resource_name(&res_name); + let (mut unique, mut n) = (base.clone(), 2); + while self.doc.resources.contains_key(&unique) || added.iter().any(|(a, _)| *a == unique) { + unique = format!("{n}_{base}"); + n += 1; + } + added.push((unique.clone(), res_bytes)); + decoded.push((unique.clone(), crate::resource_cmds::decoded(res_image))); + made.insert((file.clone(), slot), unique.clone()); + unique + }; + let source = if channel == Channel::Normal { Source::Image { name: resource, normal_format: nf } } else { Source::image(resource) }; + fill.sources.insert(channel, source); + channels.push(channel); + } + if channels.is_empty() { + let why = if warnings.is_empty() { String::new() } else { format!(" ({})", warnings.join("; ")) }; + return Err(EngineError::Other(format!("file.importPbrFolder: nothing could be imported{why}"))); + } + let base_name = opts.name.clone().filter(|n| !n.trim().is_empty()).unwrap_or_else(|| "PBR material".to_string()); + let layer_name = self.doc.project.next_layer_name(ts, &base_name); + let resource_names: Vec = added.iter().map(|(n, _)| n.clone()).collect(); + let id = self.edit("Import PBR textures", |s| { + for (n, bytes) in &added { + s.doc.resources.insert(n.clone(), bytes.clone()); + } + let id = s.doc.project.alloc_id(); + crate::layer_cmds::insert_selected(s, Layer::new(id, layer_name, LayerKind::Fill(fill), &channels)) + })?; + self.images.extend(decoded); + let mut reply = report(&set, opts.normal_format); + if let Some(o) = reply.as_object_mut() { + o.insert("layer".into(), json!(id.0)); + o.insert("name".into(), json!(base_name)); + o.insert("channels".into(), json!(channels.iter().map(|c| c.id()).collect::>())); + o.insert("resources".into(), json!(resource_names)); + o.insert("warnings".into(), json!(warnings)); + } + Ok(reply) + } + + /// [`Session::import_pbr`] for files already in memory (the browser, tests). + pub fn import_pbr_bytes(&mut self, files: Vec<(String, Vec)>, opts: &PbrOptions) -> Result { + let list: FileList = files.iter().map(|(n, b)| (n.clone(), b.len() as u64)).collect(); + let by_name: BTreeMap<&str, &Vec> = files.iter().map(|(n, b)| (n.as_str(), b)).collect(); + let load = |name: &str| by_name.get(name).map(|b| (*b).clone()).ok_or_else(|| "no such file".to_string()); + self.import_pbr(&list, &load, opts) + } +} + +/// A name for the material from the files' shared leading text (`brick_wall_albedo`, `brick_wall_rough` → `brick wall`). +pub fn common_name(files: &[String]) -> Option { + let stems: Vec<&str> = files.iter().map(|f| f.rsplit(['/', '\\']).next().unwrap_or(f)).map(|l| l.rsplit_once('.').map_or(l, |(s, _)| s)).collect(); + let first = stems.first()?; + let mut len = first.len(); + for s in &stems[1..] { + len = first.bytes().zip(s.bytes()).take(len).take_while(|(a, b)| a.eq_ignore_ascii_case(b)).count(); + } + let prefix = first.get(..len)?.trim_matches(|c: char| !c.is_alphanumeric()); + (prefix.chars().filter(|c| c.is_alphanumeric()).count() >= 2).then(|| prefix.replace(['_', '-'], " ")) +} + +// --------------------------------------------------------------------------------------------- +// Parameters and sources (native). + +fn options(cmd: &str, p: &Value) -> Result { + let normal_format = match strp(p, "normalFormat") { + None | Some("auto") => None, + Some("openGl" | "opengl") => Some(NormalFormat::OpenGl), + Some("directX" | "directx") => Some(NormalFormat::DirectX), + Some(other) => return Err(bad(cmd, format!("unknown normal format `{other}` (auto, openGl or directX)"))), + }; + let mut choose = BTreeMap::new(); + if let Some(obj) = p.get("choose").and_then(Value::as_object) { + for (k, v) in obj { + let kind = MapKind::from_id(k).ok_or_else(|| bad(cmd, format!("choose: unknown map `{k}`")))?; + let file = v.as_str().ok_or_else(|| bad(cmd, format!("choose.{k} must be a file name")))?; + choose.insert(kind, file.to_string()); + } + } + Ok(PbrOptions { + name: strp(p, "name").map(str::to_string), + normal_format, + projection: crate::layer_cmds::projection_from(p, cmd, None)?, + choose, + }) +} + +/// Pick the one group of files that holds a material: the files at the root if they hold maps, +/// else the one folder that does. Returns the group's folder prefix (`""` or `"sub/"`). +fn pick_group(cmd: &str, files: &FileList) -> Result { + let mut dirs: BTreeMap = BTreeMap::new(); + for (name, size) in files { + let dir = name.rsplit_once('/').map_or("", |(d, _)| d).to_string(); + dirs.entry(dir).or_default().push((name.clone(), *size)); + } + let with_maps: Vec<&String> = dirs.iter().filter(|(_, f)| !pbr_maps::detect(f.iter().map(|(n, s)| (n.as_str(), *s))).is_empty()).map(|(d, _)| d).collect(); + if with_maps.iter().any(|d| d.is_empty()) { + return Ok(String::new()); + } + match with_maps.as_slice() { + [] => Err(bad(cmd, format!("no PBR texture maps found among {} files (names such as albedo, roughness, metallic, normal, height, ao)", files.len()))), + [one] => Ok(format!("{one}/")), + many => { + let shown: Vec<&str> = many.iter().take(8).map(|d| d.as_str()).collect(); + Err(bad(cmd, format!("{} folders hold PBR maps ({}{}): import one of them", many.len(), shown.join(", "), if many.len() > 8 { ", ..." } else { "" }))) + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +mod native { + use std::cell::RefCell; + use std::path::{Path, PathBuf}; + + use super::*; + + /// Where the files of an import come from. + pub enum Origin { + Folder { root: PathBuf }, + Files(Vec), + Zip(RefCell), + } + + pub struct Found { + pub source: Origin, + pub files: FileList, + /// A name for the material (folder, archive or shared file-name prefix). + pub name: Option, + } + + /// Regular files below `dir` (not following symbolic links), at most `MAX_FILES`, 4 levels deep. + fn walk(root: &Path, dir: &Path, depth: usize, out: &mut FileList) { + if depth > 4 || out.len() >= MAX_FILES { + return; + } + let Ok(rd) = std::fs::read_dir(dir) else { return }; + for entry in rd.flatten() { + if out.len() >= MAX_FILES { + return; + } + let path = entry.path(); + // `symlink_metadata`: a link, pipe or device is neither read nor followed. + let Ok(meta) = std::fs::symlink_metadata(&path) else { continue }; + if meta.is_dir() { + walk(root, &path, depth + 1, out); + } else if meta.is_file() + && let Ok(rel) = path.strip_prefix(root) + { + let name = rel.components().map(|c| c.as_os_str().to_string_lossy().into_owned()).collect::>().join("/"); + out.push((name, meta.len())); + } + } + } + + pub fn find(cmd: &str, p: &Value) -> Result { + if let Some(arr) = p.get("files").and_then(Value::as_array) { + let mut paths = Vec::new(); + let mut list = FileList::new(); + for v in arr.iter().take(MAX_FILES) { + let Some(s) = v.as_str() else { return Err(bad(cmd, "`files` must be a list of paths")) }; + let path = PathBuf::from(s); + let meta = std::fs::symlink_metadata(&path).map_err(|e| bad(cmd, format!("cannot read {s}: {e}")))?; + if !meta.is_file() { + continue; + } + let leaf = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); + // Names are leaf names; the same leaf twice keeps the first. + if list.iter().any(|(n, _)| *n == leaf) { + continue; + } + list.push((leaf, meta.len())); + paths.push(path); + } + let name = common_name(&list.iter().map(|(n, _)| n.clone()).collect::>()); + return Ok(Found { source: Origin::Files(paths), files: list, name }); + } + let path = strp(p, "path").filter(|s| !s.trim().is_empty()).ok_or_else(|| bad(cmd, "`path` (a folder or .zip) or `files` is required"))?; + let meta = std::fs::symlink_metadata(path).map_err(|e| bad(cmd, format!("cannot read {path}: {e}")))?; + let stem = |s: &str| Path::new(s).file_stem().map(|n| n.to_string_lossy().replace(['_', '-'], " ")); + if meta.is_dir() { + let root = Path::new(path).canonicalize().map_err(|e| bad(cmd, format!("cannot read {path}: {e}")))?; + let mut files = FileList::new(); + walk(&root, &root, 0, &mut files); + let group = pick_group(cmd, &files)?; + let files: FileList = files.into_iter().filter(|(n, _)| n.strip_prefix(group.as_str()).is_some_and(|rest| !rest.contains('/'))).collect(); + let name = if group.is_empty() { stem(path) } else { stem(group.trim_end_matches('/')) }.or_else(|| root.file_name().map(|n| n.to_string_lossy().into_owned())); + let root = if group.is_empty() { root } else { root.join(group.trim_end_matches('/')) }; + let files = files.into_iter().map(|(n, s)| (n.rsplit('/').next().unwrap_or(&n).to_string(), s)).collect(); + return Ok(Found { source: Origin::Folder { root }, files, name }); + } + if meta.is_file() && path.to_ascii_lowercase().ends_with(".zip") { + if meta.len() > MAX_FILE_BYTES * 2 { + return Err(bad(cmd, format!("{path} is too large"))); + } + let bytes = std::fs::read(path).map_err(|e| bad(cmd, format!("cannot read {path}: {e}")))?; + let mut zip = texturecraft_formats::zip_read::ZipFiles::open(bytes).map_err(|e| bad(cmd, format!("{path} is not a readable zip: {e}")))?; + let files = zip.list(MAX_FILES); + let group = pick_group(cmd, &files)?; + let files: FileList = files.into_iter().filter(|(n, _)| n.strip_prefix(group.as_str()).is_some_and(|rest| !rest.contains('/'))).collect(); + let name = if group.is_empty() { stem(path) } else { stem(group.trim_end_matches('/')) }; + return Ok(Found { source: Origin::Zip(RefCell::new(zip)), files, name }); + } + Err(bad(cmd, format!("{path} is not a folder or a .zip file"))) + } + + impl Found { + /// Reads one file of the list. Archive entries are read through a limit, since their + /// declared size can lie. + pub fn load(&self, name: &str) -> std::result::Result, String> { + match &self.source { + Origin::Folder { root } => read_regular(&root.join(name)), + Origin::Files(paths) => { + let path = paths.iter().find(|p| p.file_name().is_some_and(|n| n.to_string_lossy() == name)).ok_or("no such file")?; + read_regular(path) + } + Origin::Zip(zip) => zip.borrow_mut().read(name, MAX_FILE_BYTES).map_err(|e| e.to_string()), + } + } + } + + fn read_regular(path: &Path) -> std::result::Result, String> { + let meta = std::fs::symlink_metadata(path).map_err(|e| e.to_string())?; + // A link, pipe or device in the folder would escape it or block the read. + if !meta.is_file() { + return Err("it is not a regular file".into()); + } + if meta.len() > MAX_FILE_BYTES { + return Err(format!("the file is over {} MB", MAX_FILE_BYTES >> 20)); + } + std::fs::read(path).map_err(|e| e.to_string()) + } +} + +#[cfg(not(target_arch = "wasm32"))] +fn import(s: &mut Session, p: &Value) -> Result { + let cmd = "file.importPbrFolder"; + let mut opts = options(cmd, p)?; + let found = native::find(cmd, p)?; + if opts.name.is_none() { + opts.name = found.name.clone(); + } + s.import_pbr(&found.files, &|name| found.load(name), &opts) +} + +#[cfg(not(target_arch = "wasm32"))] +fn scan(s: &mut Session, p: &Value) -> Result { + let cmd = "file.scanPbrFolder"; + let opts = options(cmd, p)?; + let found = native::find(cmd, p)?; + let set = detect_with(&found.files, &opts.choose)?; + let mut reply = report(&set, opts.normal_format); + if let Some(o) = reply.as_object_mut() { + o.insert("name".into(), json!(opts.name.or(found.name))); + o.insert("channelsAvailable".into(), json!(s.doc.project.texture_set(s.ts()).map(|t| t.channel_list()).unwrap_or_default().iter().map(|c| c.id()).collect::>())); + } + Ok(reply) +} + +#[cfg(target_arch = "wasm32")] +fn import(_: &mut Session, p: &Value) -> Result { + let _ = options("file.importPbrFolder", p)?; + Err(EngineError::Other("file paths are not available in the browser".into())) +} + +#[cfg(target_arch = "wasm32")] +fn scan(_: &mut Session, _: &Value) -> Result { + Err(EngineError::Other("file paths are not available in the browser".into())) +} diff --git a/crates/engine/tests/pbr_import.rs b/crates/engine/tests/pbr_import.rs new file mode 100644 index 00000000..007af6c6 --- /dev/null +++ b/crates/engine/tests/pbr_import.rs @@ -0,0 +1,349 @@ +//! `file.importPbrFolder` / `file.scanPbrFolder`: a folder, files or a zip of PBR maps becomes +//! one fill layer. + +use std::io::Write as _; +use std::path::{Path, PathBuf}; + +use serde_json::{Value, json}; +use texturecraft_engine::core::{Channel, Fill, LayerId, LayerKind, NormalFormat, Projection, Source}; +use texturecraft_engine::geom::primitives; +use texturecraft_engine::{Session, execute}; + +fn session() -> Session { + Session::new_project("t", primitives::builtin("plane").unwrap(), "builtin:plane", None, 128).unwrap() +} + +/// A session whose texture set also has an ambient occlusion channel (the default set has none). +fn session_with_ao() -> Session { + let mut s = session(); + execute(&mut s, "textureSet.channel.add", &json!({"channel": "ambientOcclusion"})).unwrap(); + s +} + +fn folder(tag: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("tc_pbr_{tag}_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +fn png(path: &Path, rgb: [u8; 3]) { + image::RgbImage::from_pixel(4, 4, image::Rgb(rgb)).save(path).unwrap(); +} + +fn fill_of(s: &Session, layer: u64) -> Fill { + match &s.doc.project.layer(LayerId(layer)).unwrap().kind { + LayerKind::Fill(f) => f.clone(), + other => panic!("not a fill: {other:?}"), + } +} + +fn import(s: &mut Session, dir: &Path, extra: Value) -> Result { + let mut p = json!({ "path": dir.display().to_string() }); + if let (Some(p), Some(e)) = (p.as_object_mut(), extra.as_object()) { + p.extend(e.clone()); + } + execute(s, "file.importPbrFolder", &p) +} + +fn image_name(f: &Fill, c: Channel) -> String { + match f.sources.get(&c) { + Some(Source::Image { name, .. }) => name.clone(), + other => panic!("{c:?} is {other:?}"), + } +} + +#[test] +fn a_folder_becomes_one_fill_layer_in_one_undo_step() { + let dir = folder("ok"); + for (name, rgb) in [ + ("Brick_Albedo.png", [200, 40, 40]), + ("Brick_Roughness.png", [128, 128, 128]), + ("Brick_Metallic.png", [0, 0, 0]), + ("Brick_Normal_DX.png", [128, 51, 255]), + ("Brick_Height.png", [90, 90, 90]), + ("Brick_AO.png", [255, 255, 255]), + ("Brick_Preview.png", [1, 2, 3]), + ] { + png(&dir.join(name), rgb); + } + std::fs::write(dir.join("readme.txt"), "hi").unwrap(); + let mut s = session_with_ao(); + let res = s.doc.resources.len(); + let r = import(&mut s, &dir, json!({})).unwrap(); + let layer = r["layer"].as_u64().unwrap(); + let fill = fill_of(&s, layer); + for c in [Channel::BaseColor, Channel::Roughness, Channel::Metallic, Channel::Normal, Channel::Height, Channel::AmbientOcclusion] { + assert!(matches!(fill.sources.get(&c), Some(Source::Image { .. })), "{c:?}: {:?}", fill.sources); + } + // The convention comes from `_DX` in the name; the preview and the text file are ignored. + assert!(matches!(fill.sources.get(&Channel::Normal), Some(Source::Image { normal_format: NormalFormat::DirectX, .. }))); + assert_eq!(r["normalFormat"], "directX"); + assert_eq!(r["maps"]["baseColor"], "Brick_Albedo.png"); + let ignored = r["ignored"].to_string(); + assert!(ignored.contains("Brick_Preview.png") && ignored.contains("preview image") && ignored.contains("readme.txt"), "{ignored}"); + assert!(!s.doc.resources.keys().any(|k| k.to_lowercase().contains("preview"))); + assert_eq!(s.doc.resources.len(), res + 6); + assert!(matches!(fill.projection, Projection::Uv { scale, .. } if scale == [1.0, 1.0])); + let layer_name = s.doc.project.layer(LayerId(layer)).unwrap().name.clone(); + assert!(layer_name.starts_with("tc pbr ok"), "named after the folder: {layer_name}"); + assert_eq!(r["name"], "tc pbr ok ".to_string() + &std::process::id().to_string()); + // Every decoded image is ready for the viewport. + for n in s.doc.resources.keys() { + assert!(s.images.contains_key(n), "{n}"); + } + execute(&mut s, "edit.undo", &Value::Null).unwrap(); + assert_eq!(s.doc.resources.len(), res); + assert!(s.doc.project.layer(LayerId(layer)).is_err()); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn a_directx_normal_map_is_flipped_to_opengl_where_it_is_sampled() { + // Green 0.2 in the file: a DirectX map reads 0.8 as OpenGL, an OpenGL map stays 0.2. + let green = |files: &[&str], extra: Value| -> f32 { + let dir = folder("flip"); + for f in files { + png(&dir.join(f), [128, 51, 255]); + } + let mut s = session(); + import(&mut s, &dir, extra).unwrap(); + s.update(); + let v = s.caches[0].composite.value(Channel::Normal, 64, 64); + let _ = std::fs::remove_dir_all(&dir); + v[1] + }; + let (dx, gl) = (green(&["wall_normal_dx.png"], json!({})), green(&["wall_normal_gl.png"], json!({}))); + assert!((dx - 0.8).abs() < 0.02, "DirectX name flips: {dx}"); + assert!((gl - 0.2).abs() < 0.02, "OpenGL name does not: {gl}"); + // No convention in the name: OpenGL by default, and the toggle decides. + assert!((green(&["wall_normal.png"], json!({})) - 0.2).abs() < 0.02); + assert!((green(&["wall_normal.png"], json!({"normalFormat": "directX"})) - 0.8).abs() < 0.02); + // The toggle forces the convention even against the name. + assert!((green(&["wall_normal_dx.png"], json!({"normalFormat": "openGl"})) - 0.2).abs() < 0.02); + assert!((green(&["wall_normal_dx.png"], json!({"normalFormat": "auto"})) - 0.8).abs() < 0.02); +} + +#[test] +fn a_packed_orm_image_is_split_into_three_maps() { + let dir = folder("orm"); + // Occlusion 0.2 (R), roughness 0.5 (G), metallic 0.9 (B). + png(&dir.join("floor_ORM.png"), [51, 128, 230]); + png(&dir.join("floor_BaseColor.png"), [10, 20, 30]); + let mut s = session_with_ao(); + let r = import(&mut s, &dir, json!({})).unwrap(); + let fill = fill_of(&s, r["layer"].as_u64().unwrap()); + let names: Vec = [Channel::AmbientOcclusion, Channel::Roughness, Channel::Metallic].iter().map(|c| image_name(&fill, *c)).collect(); + assert_eq!(names.iter().collect::>().len(), 3, "three separate resources: {names:?}"); + s.update(); + let at = |c| s.caches[0].composite.value(c, 64, 64)[0]; + assert!((at(Channel::AmbientOcclusion) - 0.2).abs() < 0.01, "{}", at(Channel::AmbientOcclusion)); + assert!((at(Channel::Roughness) - 0.5).abs() < 0.01); + assert!((at(Channel::Metallic) - 0.9).abs() < 0.01); + // The saved resources are real images that decode to the same values after a reload. + for n in &names { + let d = texturecraft_engine::formats::decode_image(&s.doc.resources[n]).unwrap(); + assert_eq!((d.width, d.height), (4, 4)); + } + // A set without an occlusion channel skips it with a warning and keeps the rest. + let mut plain = session(); + let r = import(&mut plain, &dir, json!({})).unwrap(); + assert!(r["warnings"].to_string().contains("ambientOcclusion was skipped"), "{r}"); + assert!(r["channels"].as_array().unwrap().contains(&json!("metallic"))); + // Separate maps win over the packed image for the channels they cover. + png(&dir.join("floor_Roughness.png"), [255, 255, 255]); + let mut s = session_with_ao(); + let r = import(&mut s, &dir, json!({})).unwrap(); + s.update(); + assert!((s.caches[0].composite.value(Channel::Roughness, 64, 64)[0] - 1.0).abs() < 0.01); + assert!((s.caches[0].composite.value(Channel::Metallic, 64, 64)[0] - 0.9).abs() < 0.01, "still from the ORM image"); + assert!(r["maps"]["roughness"] == "floor_Roughness.png"); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn glossiness_becomes_roughness() { + let dir = folder("gloss"); + png(&dir.join("tile_diffuse.png"), [1, 1, 1]); + png(&dir.join("tile_gloss.png"), [204, 204, 204]); // 0.8 glossy = 0.2 rough + let mut s = session(); + let r = import(&mut s, &dir, json!({})).unwrap(); + assert!(r["channels"].as_array().unwrap().contains(&json!("roughness")), "{r}"); + s.update(); + assert!((s.caches[0].composite.value(Channel::Roughness, 64, 64)[0] - 0.2).abs() < 0.01); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn missing_maps_stay_at_their_defaults() { + let dir = folder("missing"); + png(&dir.join("only_albedo.png"), [200, 10, 10]); + let mut s = session(); + let r = import(&mut s, &dir, json!({})).unwrap(); + assert_eq!(r["channels"], json!(["baseColor"]), "{r}"); + let fill = fill_of(&s, r["layer"].as_u64().unwrap()); + assert_eq!(fill.sources.len(), 1); + // A folder with nothing recognisable is an error that changes nothing. + let empty = folder("empty"); + png(&empty.join("holiday.png"), [1, 2, 3]); + std::fs::write(empty.join("notes.txt"), "x").unwrap(); + let (layers, res) = (s.doc.project.texture_sets[0].stack.len(), s.doc.resources.len()); + assert!(import(&mut s, &empty, json!({})).is_err()); + assert_eq!((s.doc.project.texture_sets[0].stack.len(), s.doc.resources.len()), (layers, res)); + let _ = std::fs::remove_dir_all(&dir); + let _ = std::fs::remove_dir_all(&empty); +} + +#[test] +fn several_candidates_use_the_best_and_can_be_chosen() { + let dir = folder("choose"); + png(&dir.join("m_albedo_1k.png"), [1, 1, 1]); + png(&dir.join("m_albedo_4k.png"), [2, 2, 2]); + png(&dir.join("m_nor_dx.png"), [128, 128, 255]); + png(&dir.join("m_nor_gl.png"), [128, 128, 255]); + let mut s = session(); + let scan = execute(&mut s, "file.scanPbrFolder", &json!({"path": dir.display().to_string()})).unwrap(); + assert_eq!(scan["maps"]["baseColor"], "m_albedo_4k.png"); + assert_eq!(scan["maps"]["normal"], "m_nor_gl.png", "the OpenGL file needs no flip"); + assert_eq!(scan["candidates"]["baseColor"].as_array().unwrap().len(), 2); + assert!(s.doc.resources.is_empty(), "scanning changes nothing"); + let r = import(&mut s, &dir, json!({"choose": {"baseColor": "m_albedo_1k.png", "normal": "m_nor_dx.png"}})).unwrap(); + assert_eq!(r["maps"]["baseColor"], "m_albedo_1k.png"); + assert_eq!(r["normalFormat"], "directX"); + assert!(import(&mut s, &dir, json!({"choose": {"baseColor": "nope.png"}})).is_err()); + assert!(import(&mut s, &dir, json!({"choose": {"roughness": "m_albedo_1k.png"}})).is_err()); + assert!(import(&mut s, &dir, json!({"choose": {"bogus": "x"}})).is_err()); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn projection_name_and_selected_files() { + let dir = folder("files"); + png(&dir.join("moss_col.png"), [1, 90, 1]); + png(&dir.join("moss_rough.png"), [200, 200, 200]); + png(&dir.join("other_albedo.png"), [9, 9, 9]); + let mut s = session(); + let files: Vec = ["moss_col.png", "moss_rough.png"].iter().map(|f| dir.join(f).display().to_string()).collect(); + let r = execute(&mut s, "file.importPbrFolder", &json!({"files": files, "projection": "triplanar", "scale": [2, 2]})).unwrap(); + assert_eq!(r["name"], "moss"); + let fill = fill_of(&s, r["layer"].as_u64().unwrap()); + assert!(matches!(fill.projection, Projection::Triplanar { scale, .. } if scale == 2.0), "{:?}", fill.projection); + assert_eq!(fill.sources.len(), 2); + let r = execute(&mut s, "file.importPbrFolder", &json!({"files": [dir.join("moss_col.png").display().to_string()], "name": "Mossy"})).unwrap(); + assert!(s.doc.project.layer(LayerId(r["layer"].as_u64().unwrap())).unwrap().name.starts_with("Mossy")); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn a_zip_and_a_pack_of_folders() { + let dir = folder("zip"); + let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let o = zip::write::SimpleFileOptions::default(); + let one = { + let mut b = Vec::new(); + image::RgbImage::from_pixel(4, 4, image::Rgb([5, 6, 7])).write_to(&mut std::io::Cursor::new(&mut b), image::ImageFormat::Png).unwrap(); + b + }; + for name in ["brick/brick_albedo.png", "brick/brick_roughness.png", "__MACOSX/brick/._brick_albedo.png", "brick/preview.png"] { + z.start_file(name, o).unwrap(); + z.write_all(&one).unwrap(); + } + let bytes = z.finish().unwrap().into_inner(); + let path = dir.join("Brick Pack.zip"); + std::fs::write(&path, &bytes).unwrap(); + let mut s = session(); + let r = execute(&mut s, "file.importPbrFolder", &json!({"path": path.display().to_string()})).unwrap(); + assert_eq!(r["channels"], json!(["baseColor", "roughness"]), "{r}"); + assert_eq!(r["name"], "brick"); + + // Two material folders in one pack: ask instead of guessing. + let pack = folder("pack"); + for m in ["a", "b"] { + std::fs::create_dir_all(pack.join(m)).unwrap(); + png(&pack.join(m).join(format!("{m}_albedo.png")), [1, 2, 3]); + } + let err = import(&mut s, &pack, json!({})).unwrap_err().to_string(); + assert!(err.contains("2 folders") && err.contains('a') && err.contains('b'), "{err}"); + // Pointing at one of them works; a folder with one material below it is found. + assert!(import(&mut s, &pack.join("a"), json!({})).is_ok()); + std::fs::remove_dir_all(pack.join("b")).unwrap(); + assert!(import(&mut s, &pack, json!({})).is_ok()); + // Garbage zip. + std::fs::write(dir.join("bad.zip"), b"PK\x03\x04garbage").unwrap(); + assert!(execute(&mut s, "file.importPbrFolder", &json!({"path": dir.join("bad.zip").display().to_string()})).is_err()); + let _ = std::fs::remove_dir_all(&dir); + let _ = std::fs::remove_dir_all(&pack); +} + +#[test] +fn hostile_images_are_skipped_without_a_panic() { + let dir = folder("hostile"); + png(&dir.join("w_albedo.png"), [1, 2, 3]); + // Not an image at all, a truncated PNG, and an image that is too wide. + std::fs::write(dir.join("w_roughness.png"), b"definitely not a png").unwrap(); + let good = std::fs::read(dir.join("w_albedo.png")).unwrap(); + std::fs::write(dir.join("w_metallic.png"), &good[..good.len() / 2]).unwrap(); + image::GrayImage::new(20_000, 1).save(dir.join("w_height.png")).unwrap(); + // More texels than one import accepts (67 108 864) in a file that is small on disk. + image::GrayImage::new(16_384, 4_097).save(dir.join("w_emissive.png")).unwrap(); + // A folder named like an image, and a link out of the folder. + std::fs::create_dir_all(dir.join("w_opacity.png")).unwrap(); + #[cfg(unix)] + { + let outside = dir.parent().unwrap().join(format!("tc_pbr_outside_{}.png", std::process::id())); + png(&outside, [9, 9, 9]); + std::os::unix::fs::symlink(&outside, dir.join("w_ao.png")).unwrap(); + } + let mut s = session(); + let r = import(&mut s, &dir, json!({})).unwrap(); + assert_eq!(r["channels"], json!(["baseColor"]), "{r}"); + let warnings = r["warnings"].to_string(); + for needle in ["roughness", "metallic", "height", "emissive"] { + assert!(warnings.contains(needle), "{needle}: {warnings}"); + } + #[cfg(unix)] + { + assert!(!r["maps"].as_object().unwrap().contains_key("ambientOcclusion"), "a link is not followed: {r}"); + let _ = std::fs::remove_file(dir.parent().unwrap().join(format!("tc_pbr_outside_{}.png", std::process::id()))); + } + // Every map hostile: an error, and the project is as it was. + let only_bad = folder("onlybad"); + std::fs::write(only_bad.join("w_albedo.png"), b"nope").unwrap(); + let before = (s.doc.project.texture_sets[0].stack.len(), s.doc.resources.len()); + assert!(import(&mut s, &only_bad, json!({})).is_err()); + assert_eq!((s.doc.project.texture_sets[0].stack.len(), s.doc.resources.len()), before); + let _ = std::fs::remove_dir_all(&dir); + let _ = std::fs::remove_dir_all(&only_bad); +} + +#[test] +fn bad_params_are_errors() { + let mut s = session(); + let run = |s: &mut Session, p: Value| execute(s, "file.importPbrFolder", &p); + assert!(run(&mut s, json!({})).is_err()); + assert!(run(&mut s, json!({"path": ""})).is_err()); + assert!(run(&mut s, json!({"path": "/definitely/not/here"})).is_err()); + assert!(run(&mut s, json!({"files": [1, 2]})).is_err()); + assert!(run(&mut s, json!({"files": ["/definitely/not/here.png"]})).is_err()); + let dir = folder("params"); + png(&dir.join("a_albedo.png"), [1, 1, 1]); + assert!(import(&mut s, &dir, json!({"normalFormat": "sideways"})).is_err()); + assert!(import(&mut s, &dir, json!({"projection": "nope"})).is_err()); + assert!(import(&mut s, &dir.join("a_albedo.png"), json!({})).is_err(), "a single image is not a folder or zip"); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn byte_api_imports_without_a_file_system() { + // What the browser uses: files already in memory. + let png_bytes = |rgb| { + let mut b = Vec::new(); + image::RgbImage::from_pixel(2, 2, image::Rgb(rgb)).write_to(&mut std::io::Cursor::new(&mut b), image::ImageFormat::Png).unwrap(); + b + }; + let mut s = session(); + let files = vec![("t_albedo.png".to_string(), png_bytes([1, 2, 3])), ("t_nrm_dx.png".to_string(), png_bytes([128, 128, 255]))]; + let r = s.import_pbr_bytes(files, &texturecraft_engine::pbr_cmds::PbrOptions::default()).unwrap(); + assert_eq!(r["normalFormat"], "directX"); + assert_eq!(r["channels"], json!(["baseColor", "normal"])); +} diff --git a/crates/formats/src/image_res.rs b/crates/formats/src/image_res.rs index 49e1e014..c44de477 100644 --- a/crates/formats/src/image_res.rs +++ b/crates/formats/src/image_res.rs @@ -49,6 +49,17 @@ pub fn coverage_png(width: u32, height: u32, coverage: &[u8], rgb: [u8; 3]) -> R Ok(png) } +/// 16-bit single-channel PNG bytes of `values` (0..1, `width × height`, top row first): one map +/// split out of a packed image, or an inverted one. +pub fn gray16_png(width: u32, height: u32, values: impl Iterator) -> Result> { + let data: Vec = values.map(|v| if v.is_finite() { (v.clamp(0.0, 1.0) * 65535.0).round() as u16 } else { 0 }).collect(); + let img = image::ImageBuffer::, Vec>::from_raw(width, height, data) + .ok_or_else(|| FormatError::Invalid(format!("pixel count does not match {width}x{height}")))?; + let mut png = Vec::new(); + img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).map_err(|e| FormatError::Invalid(e.to_string()))?; + Ok(png) +} + #[cfg(test)] mod tests { use super::*; @@ -69,6 +80,18 @@ mod tests { assert!(coverage_png(2, 2, &[0], [255; 3]).is_err()); } + #[test] + fn gray16_round_trips_and_rejects_a_wrong_count() { + let png = gray16_png(2, 1, [0.0, 1.0].into_iter()).unwrap(); + let d = decode_image(&png).unwrap(); + assert_eq!((d.width, d.height), (2, 1)); + assert_eq!((d.rgba[0][0], d.rgba[1][0]), (0.0, 1.0)); + assert!(gray16_png(2, 2, [0.5].into_iter()).is_err()); + // NaN becomes 0, out-of-range values clamp. + let d = decode_image(&gray16_png(3, 1, [f32::NAN, 9.0, -9.0].into_iter()).unwrap()).unwrap(); + assert_eq!((d.rgba[0][0], d.rgba[1][0], d.rgba[2][0]), (0.0, 1.0, 0.0)); + } + #[test] fn decoding_within_a_texel_limit_refuses_big_images_from_the_header() { let png = { diff --git a/crates/formats/src/lib.rs b/crates/formats/src/lib.rs index d504072a..be82a4f3 100644 --- a/crates/formats/src/lib.rs +++ b/crates/formats/src/lib.rs @@ -35,13 +35,15 @@ pub mod materialx_read; pub mod mcmat; pub mod mesh_export; pub mod mesh_import; +pub mod pbr_maps; pub mod presets; pub mod project_file; +pub mod zip_read; pub use export::{ Dest, ExportInput, ExportOptions, ExportPreset, ExportedFile, FileFormat, MapSpec, Padding, PlannedFile, SourceRef, VirtualMap, list_outputs, render_maps, }; -pub use image_res::{DecodedImage, coverage_png, decode_image, decode_image_within}; +pub use image_res::{DecodedImage, coverage_png, decode_image, decode_image_within, gray16_png}; pub use mesh_export::{GlbTextures, NamedFiles, UsdExport, UsdFormat, glb_bytes, glb_export_options, obj_bytes, obj_with_mtl, usd_bytes, usd_export_options}; pub use mesh_import::{HighPolyMesh, ImportedMesh, MeshFile, MeshReport, import_high_poly_bytes, import_mesh_bytes, mesh_report, self_contained_mesh}; #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/formats/src/pbr_maps.rs b/crates/formats/src/pbr_maps.rs new file mode 100644 index 00000000..50062244 --- /dev/null +++ b/crates/formats/src/pbr_maps.rs @@ -0,0 +1,495 @@ +//! Recognising the maps of a PBR texture set from file names. +//! +//! Texture sites, scanners and game engines name the same maps in many ways (`Brick_Albedo.png`, +//! `brick_diff_4k.jpg`, `T_Brick_BC.tga`, `brick-NormalDX.exr`, `Brick_ARM.png`, `_BaseMap`...). +//! This module turns a list of file names into one [`PbrSet`]: for each [`MapKind`] the best +//! file, the other candidates, the normal-map convention and the files it ignored. It is pure +//! (names in, names out), so it works in the browser too and is tested with a table of +//! real-world styles. +//! +//! # Rules +//! +//! 1. Only image files count (`png jpg jpeg tga tif tiff bmp webp exr hdr`), matched +//! case-insensitively. Dot files (`._x.png`, made by archivers on a Mac), files in +//! `__MACOSX` and preview images (a token such as `preview`, `thumb`, `thumbnail`, `sphere`, +//! `render`, `swatch`, `icon`, `screenshot`) are ignored. +//! 2. The file stem is split into lower-case tokens at separators (`_ - . space`), at +//! lower→upper case changes (`BaseColor`) and between letters and digits (`diff4k`). +//! 3. The map is named by the **last** known word (or word pair); resolution (`1k`..`16k`, +//! `2048`), bit depth (`16bit`), convention (`gl`, `dx`) and other tokens are skipped. +//! Pairs (`base color`, `ambient occlusion`, `ao rough metal`) and glued names +//! (`basecolor`, `normaldx`) are understood. +//! 4. Normal maps: `gl` / `ogl` / `opengl` means OpenGL, `dx` / `d3d` / `directx` means DirectX; +//! with neither the convention is unknown and the caller's default applies. +//! 5. When several files name the same map the best wins: for normals the OpenGL one (no +//! flip), then the highest resolution token, then the largest file, then the name. All of +//! them stay in [`PbrSet::candidates`] so a caller can ask the user. +//! +//! Single letters are not accepted, except a trailing `n` (normal): `r`, `m`, `d` and `h` are +//! ambiguous with colour channels. + +use std::collections::BTreeMap; + +use texturecraft_core::NormalFormat; + +/// The kinds of map a PBR folder holds. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum MapKind { + BaseColor, + Roughness, + /// Glossiness is the inverse of roughness; the importer inverts it. + Glossiness, + Metallic, + Normal, + Height, + AmbientOcclusion, + Emissive, + Opacity, + /// One image: occlusion in red, roughness in green, metallic in blue (`ORM`, `ARM`). + PackedOrm, + /// Recognised, but TextureCraft has no channel for it (cavity, specular, ...): reported, + /// not imported. + Unused, +} + +impl MapKind { + const IMPORTED: [MapKind; 10] = [ + MapKind::BaseColor, + MapKind::Roughness, + MapKind::Glossiness, + MapKind::Metallic, + MapKind::Normal, + MapKind::Height, + MapKind::AmbientOcclusion, + MapKind::Emissive, + MapKind::Opacity, + MapKind::PackedOrm, + ]; + + /// Stable id for JSON. + pub fn id(self) -> &'static str { + match self { + MapKind::BaseColor => "baseColor", + MapKind::Roughness => "roughness", + MapKind::Glossiness => "glossiness", + MapKind::Metallic => "metallic", + MapKind::Normal => "normal", + MapKind::Height => "height", + MapKind::AmbientOcclusion => "ambientOcclusion", + MapKind::Emissive => "emissive", + MapKind::Opacity => "opacity", + MapKind::PackedOrm => "orm", + MapKind::Unused => "unused", + } + } + + /// The kind with this JSON id. + pub fn from_id(id: &str) -> Option { + Self::IMPORTED.into_iter().find(|k| k.id() == id) + } +} + +/// File extensions read as images (lower case). +pub const IMAGE_EXTS: &[&str] = &["png", "jpg", "jpeg", "tga", "tif", "tiff", "bmp", "webp", "exr", "hdr"]; + +/// What the name of one file says. +#[derive(Clone, Debug, PartialEq)] +pub struct Detected { + pub kind: MapKind, + /// The convention named for a normal map, if any. + pub normal: Option, + /// Resolution in pixels named by a `2k` / `2048` token, 0 if none. + pub resolution: u32, + /// Higher is a stronger match (a weak word such as `bump` loses to a strong one). + pub strength: u8, +} + +/// One candidate file. +#[derive(Clone, Debug, PartialEq)] +pub struct Candidate { + /// The file's path as given. + pub name: String, + pub detected: Detected, + /// File size in bytes (a tie-breaker; 0 when unknown). + pub bytes: u64, +} + +/// The maps found among a list of files. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct PbrSet { + /// The best file per kind. + pub chosen: BTreeMap, + /// Every file that names each kind, best first (the chosen one is first). + pub candidates: BTreeMap>, + /// Files not used, with why (`not an image`, `preview image`, `unknown map name`, + /// `unused map`, `another file is used for `). + pub ignored: Vec<(String, String)>, +} + +impl PbrSet { + /// True when no map was recognised. + pub fn is_empty(&self) -> bool { + self.chosen.is_empty() + } +} + +// --------------------------------------------------------------------------------------------- +// Tokens. + +/// Lower-case tokens of a file stem: split at separators, camelCase changes and letter/digit +/// changes; a number and its unit stay together (`4k`, `16bit`). +fn tokens(stem: &str) -> Vec { + let chars: Vec = stem.chars().collect(); + let mut out: Vec = Vec::new(); + let mut cur = String::new(); + let mut prev: Option = None; + for (i, &c) in chars.iter().enumerate() { + if !c.is_alphanumeric() { + if !cur.is_empty() { + out.push(std::mem::take(&mut cur)); + } + prev = None; + continue; + } + if let Some(p) = prev { + let next_lower = chars.get(i + 1).is_some_and(|n| n.is_lowercase()); + // aB -> a|B; ABc -> A|Bc; a1 -> a|1; 1a -> 1|a. + let split = (p.is_lowercase() && c.is_uppercase()) || (p.is_uppercase() && c.is_uppercase() && next_lower) || (p.is_alphabetic() != c.is_alphabetic()); + if split && !cur.is_empty() { + out.push(std::mem::take(&mut cur)); + } + } + cur.extend(c.to_lowercase()); + prev = Some(c); + } + if !cur.is_empty() { + out.push(cur); + } + let mut joined: Vec = Vec::new(); + for t in out { + if let Some(last) = joined.last_mut() + && !last.is_empty() + && last.chars().all(|c| c.is_ascii_digit()) + && matches!(t.as_str(), "k" | "bit" | "px") + { + last.push_str(&t); + continue; + } + joined.push(t); + } + joined +} + +/// Pixels named by a resolution token: `1k`..`16k`, `512px`, or a bare power of two ≥ 128. +fn resolution_of(t: &str) -> Option { + if let Some(n) = t.strip_suffix('k').and_then(|n| n.parse::().ok()) { + return (1..=16).contains(&n).then_some(n * 1024); + } + if let Some(n) = t.strip_suffix("px").and_then(|n| n.parse::().ok()) { + return (16..=16384).contains(&n).then_some(n); + } + let n: u32 = t.parse().ok()?; + ((128..=16384).contains(&n) && n.is_power_of_two()).then_some(n) +} + +fn normal_format_of(t: &str) -> Option { + match t { + "gl" | "ogl" | "opengl" | "normalgl" => Some(NormalFormat::OpenGl), + "dx" | "d3d" | "directx" | "normaldx" => Some(NormalFormat::DirectX), + _ => None, + } +} + +/// A single known word → (kind, strength). +fn word(t: &str) -> Option<(MapKind, u8)> { + use MapKind::*; + Some(match t { + "basecolor" | "basecolour" | "albedo" | "diffuse" | "diff" | "dif" | "col" | "color" | "colour" | "bc" | "basemap" | "maintex" | "alb" => (BaseColor, 3), + "roughness" | "rough" | "rgh" | "rou" => (Roughness, 3), + "gloss" | "glossiness" | "glossy" | "smoothness" | "smooth" => (Glossiness, 3), + "metallic" | "metalness" | "metal" | "mtl" | "met" => (Metallic, 3), + "normal" | "nrm" | "nor" | "norm" | "nrml" | "normalmap" | "bumpmap" | "nm" | "n" | "normalgl" | "normaldx" => (Normal, 3), + "height" | "disp" | "displacement" | "displace" | "heightmap" | "hgt" | "dsp" | "parallax" => (Height, 3), + "bump" => (Height, 1), + "ao" | "ambientocclusion" | "occlusion" | "occ" | "ambocc" | "occlusionmap" => (AmbientOcclusion, 3), + "emissive" | "emission" | "emit" | "emis" | "glow" | "emissivecolor" | "emissionmap" | "illum" => (Emissive, 3), + "opacity" | "alpha" | "opac" | "transparency" | "cutout" | "coverage" => (Opacity, 3), + "orm" | "arm" | "aorm" => (PackedOrm, 4), + "cavity" | "curvature" | "specular" | "spec" | "translucency" | "thickness" | "scatter" | "sss" | "fuzz" | "sheen" | "ior" | "mask" | "flow" | "reflection" => { + (Unused, 1) + } + _ => return None, + }) +} + +fn is_preview(t: &str) -> bool { + matches!(t, "preview" | "previews" | "thumb" | "thumbs" | "thumbnail" | "sphere" | "render" | "swatch" | "icon" | "screenshot" | "prev" | "overview") +} + +fn is_junk_path(path: &str) -> bool { + path.split(['/', '\\']).any(|part| part == "__MACOSX" || part.starts_with("._") || part == ".DS_Store") +} + +/// What the file `name` (a path or a bare name) is, or why it is not a map. +pub fn classify(name: &str) -> std::result::Result { + if is_junk_path(name) { + return Err("hidden or archive metadata file"); + } + let leaf = name.rsplit(['/', '\\']).next().unwrap_or(name); + let (stem, ext) = leaf.rsplit_once('.').unwrap_or((leaf, "")); + if !IMAGE_EXTS.contains(&ext.to_ascii_lowercase().as_str()) { + return Err("not an image"); + } + let toks = tokens(stem); + if toks.iter().any(|t| is_preview(t)) { + return Err("preview image"); + } + let resolution = toks.iter().filter_map(|t| resolution_of(t)).max().unwrap_or(0); + let normal = toks.iter().find_map(|t| normal_format_of(t)); + // (token index, kind, strength) of every known word. + let words: Vec<(usize, MapKind, u8)> = toks.iter().enumerate().filter_map(|(i, t)| word(t).map(|(k, s)| (i, k, s))).collect(); + + let done = |kind: MapKind, strength: u8| Ok(Detected { kind, normal: if kind == MapKind::Normal { normal } else { None }, resolution, strength }); + + // `ao rough metal` written as three words. + for w in words.windows(3) { + if let [(a, MapKind::AmbientOcclusion, _), (b, MapKind::Roughness, _), (c, MapKind::Metallic, _)] = w + && *b == a + 1 + && *c == b + 1 + { + return done(MapKind::PackedOrm, 4); + } + } + // Word pairs and the position they end at. + let mut pair: Option<(usize, MapKind)> = None; + for (i, t) in toks.iter().enumerate() { + let next = toks.get(i + 1).map(String::as_str); + let kind = match (t.as_str(), next) { + ("base", Some("color" | "colour" | "map")) | ("main", Some("tex")) => Some(MapKind::BaseColor), + ("ambient", Some("occlusion")) | ("mixed", Some("ao")) => Some(MapKind::AmbientOcclusion), + ("bump", Some("map")) => Some(MapKind::Normal), + _ => None, + }; + if let Some(kind) = kind { + pair = Some((i + 1, kind)); + } + } + // The last known word wins; a pair counts if it ends at or after it. + let single = words.iter().rev().find(|(i, kind, _)| { + // A lone `n` only names a normal map as the final meaningful token. + let lone_n = toks.get(*i).is_some_and(|t| t == "n"); + !lone_n || (*kind == MapKind::Normal && toks.iter().skip(i + 1).all(|t| resolution_of(t).is_some() || normal_format_of(t).is_some() || t.ends_with("bit"))) + }); + match (pair, single) { + (Some((pi, kind)), Some((si, _, _))) if pi >= *si => done(kind, 3), + (Some((_, kind)), None) => done(kind, 3), + (_, Some((_, MapKind::Height, 1))) => { + // `bump` is weak: any stronger word in the name wins over it. + match words.iter().rev().find(|(_, k, s)| *s >= 3 && *k != MapKind::Unused) { + Some((_, k, s)) => done(*k, *s), + None => done(MapKind::Height, 1), + } + } + (_, Some((_, kind, strength))) => done(*kind, *strength), + (None, None) => Err("unknown map name"), + } +} + +/// Sort key for files naming the same map: smaller is better. +type Rank = (u8, std::cmp::Reverse, std::cmp::Reverse, std::cmp::Reverse, String); + +fn rank(c: &Candidate) -> Rank { + // OpenGL first (no flip), then unlabelled, then DirectX. + let fmt = match c.detected.normal { + Some(NormalFormat::OpenGl) => 0, + None => 1, + Some(NormalFormat::DirectX) => 2, + }; + (fmt, std::cmp::Reverse(c.detected.strength), std::cmp::Reverse(c.detected.resolution), std::cmp::Reverse(c.bytes), c.name.to_ascii_lowercase()) +} + +/// Group `files` (`(path, size in bytes)`) into a [`PbrSet`]. +pub fn detect<'a>(files: impl IntoIterator) -> PbrSet { + let mut set = PbrSet::default(); + for (name, bytes) in files { + match classify(name) { + Err(why) => set.ignored.push((name.to_string(), why.to_string())), + Ok(d) if d.kind == MapKind::Unused => set.ignored.push((name.to_string(), "unused map".to_string())), + Ok(detected) => set.candidates.entry(detected.kind).or_default().push(Candidate { name: name.to_string(), detected, bytes }), + } + } + for (kind, list) in &mut set.candidates { + list.sort_by_key(rank); + if let Some(best) = list.first() { + set.chosen.insert(*kind, best.clone()); + } + for other in list.iter().skip(1) { + set.ignored.push((other.name.clone(), format!("another file is used for {}", kind.id()))); + } + } + set +} + +#[cfg(test)] +mod tests { + use super::*; + use MapKind::*; + + fn kind(name: &str) -> Option { + classify(name).ok().map(|d| d.kind) + } + + #[test] + fn real_world_naming_styles() { + let table: &[(&str, MapKind)] = &[ + // Plain suffixes, any case and separator. + ("brick_albedo.png", BaseColor), + ("brick_basecolor.png", BaseColor), + ("brick_base_color.png", BaseColor), + ("Brick_BaseColor.png", BaseColor), + ("Brick-Base-Color.PNG", BaseColor), + ("brick_diffuse.jpg", BaseColor), + ("brick_diff_4k.jpg", BaseColor), + ("brick_col.tga", BaseColor), + ("brick_color.png", BaseColor), + ("Brick_Colour.png", BaseColor), + ("brick_roughness.png", Roughness), + ("brick_rough.png", Roughness), + ("brick_Rough_2K.jpg", Roughness), + ("brick_metallic.png", Metallic), + ("brick_metalness.png", Metallic), + ("brick_metal.png", Metallic), + ("brick_normal.png", Normal), + ("brick_nrm.png", Normal), + ("brick_nor_gl_4k.exr", Normal), + ("brick_nor_dx_4k.exr", Normal), + ("brick_NormalGL.jpg", Normal), + ("brick_NormalDX.jpg", Normal), + ("brick_normal_opengl.png", Normal), + ("brick_n.png", Normal), + ("brick_n_2k.png", Normal), + ("brick_height.png", Height), + ("brick_disp_4k.png", Height), + ("brick_displacement.png", Height), + ("brick_Displacement.jpg", Height), + ("brick_ao.png", AmbientOcclusion), + ("brick_AO.png", AmbientOcclusion), + ("brick_ambientocclusion.png", AmbientOcclusion), + ("brick_AmbientOcclusion.jpg", AmbientOcclusion), + ("brick_ambient_occlusion.png", AmbientOcclusion), + ("brick_occlusion.png", AmbientOcclusion), + ("brick_mixed_ao.png", AmbientOcclusion), + ("brick_emissive.png", Emissive), + ("brick_emission.png", Emissive), + ("brick_opacity.png", Opacity), + ("brick_alpha.png", Opacity), + ("brick_orm.png", PackedOrm), + ("brick_arm_2k.jpg", PackedOrm), + ("brick_ao_rough_metal.png", PackedOrm), + // Prefix styles and engine conventions. + ("Albedo_brick.png", BaseColor), + ("T_Brick_BC.tga", BaseColor), + ("T_Brick_N.tga", Normal), + ("T_Brick_ORM.tga", PackedOrm), + ("brick_BaseMap.png", BaseColor), + ("brick_MainTex.png", BaseColor), + ("brick_BumpMap.png", Normal), + ("brick_OcclusionMap.png", AmbientOcclusion), + ("brick_EmissionMap.png", Emissive), + // Scan sites: ids, resolution and format tokens in the name. + ("Bricks059_4K-JPG_Color.jpg", BaseColor), + ("Bricks059_4K-JPG_NormalGL.jpg", Normal), + ("Bricks059_4K-JPG_Roughness.jpg", Roughness), + ("Bricks059_1K_AmbientOcclusion.png", AmbientOcclusion), + ("wall_1234_8k_Albedo.png", BaseColor), + ("wood16bit_roughness.png", Roughness), + // Weak and inverted maps. + ("brick_bump.png", Height), + ("WoodFloor_Gloss.png", Glossiness), + ("wood_smoothness.png", Glossiness), + ]; + let wrong: Vec = + table.iter().filter(|(name, want)| kind(name) != Some(*want)).map(|(name, want)| format!("{name}: got {:?}, want {want:?}", kind(name))).collect(); + assert!(wrong.is_empty(), "{}", wrong.join("\n")); + } + + #[test] + fn non_maps_are_ignored_with_a_reason() { + for (name, why) in [ + ("readme.txt", "not an image"), + ("brick.mtlx", "not an image"), + ("brick_preview.png", "preview image"), + ("Brick_Thumbnail.jpg", "preview image"), + ("sphere_render.png", "preview image"), + ("._brick_albedo.png", "hidden or archive metadata file"), + ("__MACOSX/brick_albedo.png", "hidden or archive metadata file"), + ("brick.png", "unknown map name"), + ("", "not an image"), + ] { + assert_eq!(classify(name).unwrap_err(), why, "{name}"); + } + // Recognised words with no channel here are reported as unused, not as maps. + assert_eq!(kind("brick_cavity.png"), Some(Unused)); + assert!(detect([("brick_cavity.png", 1)]).ignored.iter().any(|(_, w)| w == "unused map")); + } + + #[test] + fn a_weak_word_loses_to_a_strong_one() { + // `bump` alone is a height map; next to a stronger word that word names the file. + assert_eq!(kind("brick_bump.png"), Some(Height)); + assert_eq!(kind("bump_brick_normal.png"), Some(Normal)); + // A pair at the end beats an earlier single word. + assert_eq!(kind("diffuse_wall_ambient_occlusion.png"), Some(AmbientOcclusion)); + } + + #[test] + fn normal_convention_comes_from_the_name() { + let n = |name: &str| classify(name).unwrap().normal; + assert_eq!(n("a_normal_gl.png"), Some(NormalFormat::OpenGl)); + assert_eq!(n("a_normal_opengl.png"), Some(NormalFormat::OpenGl)); + assert_eq!(n("a_normal_dx.png"), Some(NormalFormat::DirectX)); + assert_eq!(n("a_NormalDX.png"), Some(NormalFormat::DirectX)); + assert_eq!(n("a_nor_directx_2k.png"), Some(NormalFormat::DirectX)); + assert_eq!(n("a_normal.png"), None); + // `dx` on a map that is not a normal map says nothing. + assert_eq!(n("a_roughness_dx.png"), None); + } + + #[test] + fn several_candidates_pick_gl_then_resolution_then_size() { + let set = detect([ + ("m/n_dx.png", 10), + ("m/n_gl.png", 5), + ("m/albedo_1k.png", 900), + ("m/albedo_4k.png", 100), + ("m/albedo_2k.png", 500), + ("m/rough.png", 7), + ("m/rough.jpg", 9), + ("m/notes.txt", 1), + ("m/preview.png", 1), + ("m/mystery.png", 1), + ]); + let pick = |k| set.chosen.get(&k).map(|c| c.name.as_str()); + // The convention that needs no flip wins even when the DirectX file is bigger. + assert_eq!(pick(Normal), Some("m/n_gl.png")); + // The higher resolution token wins over file size. + assert_eq!(pick(BaseColor), Some("m/albedo_4k.png")); + // No resolution token: the larger file. + assert_eq!(pick(Roughness), Some("m/rough.jpg")); + assert_eq!(set.candidates[&BaseColor].len(), 3); + let why = |name: &str| set.ignored.iter().find(|(n, _)| n == name).map(|(_, w)| w.as_str()); + assert_eq!(why("m/notes.txt"), Some("not an image")); + assert_eq!(why("m/preview.png"), Some("preview image")); + assert_eq!(why("m/mystery.png"), Some("unknown map name")); + assert_eq!(why("m/albedo_1k.png"), Some("another file is used for baseColor")); + } + + #[test] + fn empty_and_hostile_names_do_not_panic() { + assert!(detect([]).is_empty()); + let long = "a".repeat(100_000); + let weird = ["\u{0}.png", "....png", "ÄÖÜ_Ålbedo.png", "🙂.png", "_.png", "-.exr", long.as_str(), "x/../../y_albedo.png", "C:\\a\\b_albedo.png", "99999999999999_albedo.png"]; + let set = detect(weird.iter().map(|n| (*n, 0))); + assert!(set.chosen.contains_key(&BaseColor)); + } +} diff --git a/crates/formats/src/zip_read.rs b/crates/formats/src/zip_read.rs new file mode 100644 index 00000000..bb900f43 --- /dev/null +++ b/crates/formats/src/zip_read.rs @@ -0,0 +1,84 @@ +//! Reading files out of a zip archive held in memory (a downloaded texture pack). +//! +//! Entry names are only labels here, never joined to a path. Declared sizes can lie, so every +//! read goes through a limit; the number of entries listed is capped. + +use std::io::Read as _; + +use crate::{FormatError, Result, malformed}; + +/// An opened archive. +pub struct ZipFiles { + archive: zip::ZipArchive>>, +} + +impl ZipFiles { + pub fn open(bytes: Vec) -> Result { + let archive = crate::guard("zip", || zip::ZipArchive::new(std::io::Cursor::new(bytes)).map_err(|e| malformed("zip", e)))?; + Ok(ZipFiles { archive }) + } + + /// The files (not folders) as `(name with / separators, declared size)`, at most `max`. + pub fn list(&mut self, max: usize) -> Vec<(String, u64)> { + let mut out = Vec::new(); + for i in 0..self.archive.len().min(max) { + if let Ok(e) = self.archive.by_index_raw(i) + && e.is_file() + && let Ok(name) = e.name() + { + out.push((name.replace('\\', "/"), e.size())); + } + } + out + } + + /// The bytes of the file `name` (as listed), refusing more than `cap` bytes. + pub fn read(&mut self, name: &str, cap: u64) -> Result> { + let index = (0..self.archive.len()) + .find(|i| self.archive.by_index_raw(*i).is_ok_and(|e| e.name().is_ok_and(|n| n.replace('\\', "/") == name))) + .ok_or_else(|| FormatError::Invalid(format!("{name} is not in the archive")))?; + let mut entry = crate::guard("zip", || self.archive.by_index(index).map_err(|e| malformed("zip", e)))?; + if entry.size() > cap { + return Err(FormatError::TooLarge(format!("{name} is over {} MB", cap >> 20))); + } + let mut out = Vec::new(); + (&mut entry).take(cap.saturating_add(1)).read_to_end(&mut out)?; + if out.len() as u64 > cap { + return Err(FormatError::TooLarge(format!("{name} is over {} MB", cap >> 20))); + } + Ok(out) + } +} + +#[cfg(test)] +mod tests { + use std::io::Write as _; + + use super::*; + + fn make(files: &[(&str, &[u8])]) -> Vec { + let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated); + for (n, b) in files { + z.start_file(*n, o).unwrap(); + z.write_all(b).unwrap(); + } + z.finish().unwrap().into_inner() + } + + #[test] + fn lists_reads_and_caps() { + let mut z = ZipFiles::open(make(&[("a/x.png", b"hello"), ("b.txt", &[7u8; 5000])])).unwrap(); + assert_eq!(z.list(10), vec![("a/x.png".to_string(), 5), ("b.txt".to_string(), 5000)]); + assert_eq!(z.list(1).len(), 1); + assert_eq!(z.read("a/x.png", 100).unwrap(), b"hello"); + assert!(z.read("b.txt", 100).is_err(), "over the cap"); + assert!(z.read("nope", 100).is_err()); + } + + #[test] + fn garbage_is_an_error() { + assert!(ZipFiles::open(b"not a zip".to_vec()).is_err()); + assert!(ZipFiles::open(Vec::new()).is_err()); + } +} From 2df185b2a6a427ea91470cf6e1ab56a104c2b48e Mon Sep 17 00:00:00 2001 From: MistyMystery Date: Wed, 4 Nov 2026 22:17:46 +0400 Subject: [PATCH 2/4] PBR folder import: dialog, menu item, folder drop, library copy, translations, docs File > Import PBR texture folder..., drop a folder, zip or several map files, choose between candidate files, normal convention and projection, optionally keep the material in the library. Documented in docs/control-protocol.md. --- ROADMAP.md | 2 +- crates/engine/src/lib.rs | 2 +- crates/engine/src/pbr_cmds.rs | 50 +++---- crates/formats/src/pbr_maps.rs | 28 +++- crates/ui-egui/src/i18n/ar.rs | 15 +++ crates/ui-egui/src/i18n/de.rs | 15 +++ crates/ui-egui/src/i18n/es.rs | 15 +++ crates/ui-egui/src/i18n/fr.rs | 15 +++ crates/ui-egui/src/i18n/it.rs | 15 +++ crates/ui-egui/src/i18n/ja.rs | 15 +++ crates/ui-egui/src/i18n/ko.rs | 15 +++ crates/ui-egui/src/i18n/pt_br.rs | 15 +++ crates/ui-egui/src/i18n/zh_hans.rs | 15 +++ crates/ui-egui/src/lib.rs | 14 +- crates/ui-egui/src/menus.rs | 1 + crates/ui-egui/src/pbr_import.rs | 202 +++++++++++++++++++++++++++++ crates/ui-egui/src/windows.rs | 7 +- docs/control-protocol.md | 51 ++++++++ docs/parity.md | 3 +- 19 files changed, 459 insertions(+), 36 deletions(-) create mode 100644 crates/ui-egui/src/pbr_import.rs diff --git a/ROADMAP.md b/ROADMAP.md index 654f5c35..d105bb1c 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -11,7 +11,7 @@ end to end today (engine + UI + automation + tests), not what is planned. | Area | Weight | Done | What works | Main gaps | |---|---:|---:|---|---| -| Project and files | 8 | 95% | New project from glTF/GLB, FBX (binary and ASCII, 6.x and 7.x), COLLADA, **USD (usda, usdc, usdz)**, **Alembic (Ogawa)**, OBJ, PLY, STL or built-in meshes (auto-unwrap when UVs are missing; a `.gltf`'s buffers and images and a loose USD layer's textures are read from its folder and kept in the project), **project templates** (built-in and saved from a project), `.tcraft` open/save/save as/save a copy, autosave, **asks to save unsaved changes** before quitting or replacing the project (in the browser too, and before the tab closes), recent files, drag and drop, reimport mesh **with strokes preserved on the surface**, **MaterialX import** (`file.importMaterialX`: a `.mtlx` material with `open_pbr_surface`, `standard_surface` or `gltf_pbr` becomes one fill layer with its textures and values), image resources, remove unused resources, textured OBJ (MTL), GLB and **USD** mesh export, browser open/save/download | HDF5 Alembic; Alembic animation (first sample only) | +| Project and files | 8 | 95% | New project from glTF/GLB, FBX (binary and ASCII, 6.x and 7.x), COLLADA, **USD (usda, usdc, usdz)**, **Alembic (Ogawa)**, OBJ, PLY, STL or built-in meshes (auto-unwrap when UVs are missing; a `.gltf`'s buffers and images and a loose USD layer's textures are read from its folder and kept in the project), **project templates** (built-in and saved from a project), `.tcraft` open/save/save as/save a copy, autosave, **asks to save unsaved changes** before quitting or replacing the project (in the browser too, and before the tab closes), recent files, drag and drop, reimport mesh **with strokes preserved on the surface**, **MaterialX import** (`file.importMaterialX`: a `.mtlx` material with `open_pbr_surface`, `standard_surface` or `gltf_pbr` becomes one fill layer with its textures and values), **PBR folder import** (`file.importPbrFolder`: a folder, zip or dropped files of albedo / roughness / metallic / normal (OpenGL or DirectX) / height / occlusion / emissive / opacity / packed ORM maps become one fill layer, with a dialog and an optional library copy), image resources, remove unused resources, textured OBJ (MTL), GLB and **USD** mesh export, browser open/save/download | HDF5 Alembic; Alembic animation (first sample only) | | Texture sets | 6 | 100% | One set per material, **UDIM / UV tiles** (auto-detected or switched on later with resampling, **a resolution per tile** whose filters and height slopes keep their size in UV space, one texture per tile at its own size, `$udim` export), list with visibility (show or hide all, invert, **focus** on one set), sizes 128–8192 with resampling, add/remove channels (19 channel types incl. coat, sheen, anisotropy and scattering), height strength, normal format | — | | Layer stack | 16 | 98% | Paint, fill, folder and **instance** layers; 32 per-channel blend modes and opacities; white/black/painted masks with **mask with height**; generator, fill, levels (with a histogram of its input), invert/threshold, color selection, compare, blur, sharpen and HSL effects on masks and layer content; anchor points; effects **dragged to reorder**, duplicated, and **copied, cut and pasted** across layers, masks and sets (Ctrl+C, Ctrl+X, Ctrl+V, Ctrl+D and Delete act on a selected effect); smart masks; mask view (Alt+click); Alt+click a material channel to keep it alone; duplicate, group, drag to reorder, rename, color tags, copy/cut/paste (also as instance) across sets, flatten, instantiate across sets, save layers as smart materials; **30 library filters** (adjustments incl. histogram scan, range and shift, blur/sharpen/high pass, bevel, glow, edge detect, warps, mosaic, baked lighting, material finishes); **geometry masks** (a layer on chosen mesh parts) | A few niche filters of the reference app's catalog | | Painting tools | 14 | 91% | Paint, eraser, projection with image stencils, polygon fill, **path** and **lasso fill**, smudge, clone, color picker, quick mask, Shift+click lines with preview, 2D view painting; size, flow, stroke opacity, spacing, hardness, angle, follow path, jitters, alignment, angle fade, backface culling, lazy mouse, **Ctrl+drag in a view for size, hardness, flow and angle**, X/Y/Z mirror and **radial symmetry**, pen pressure in the 3D and 2D views (Windows Ink, and touch pens in the browser), procedural and image alphas, **text** in any installed font (as an alpha or a decal), **particle brush** | Pen tilt, and pen pressure on Linux, macOS and desktop browsers (not exposed by the windowing layer) | diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index ab954994..b10e9f21 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -28,10 +28,10 @@ pub mod mask_cmds; pub mod material_lib; pub mod materials; pub mod materialx_cmds; -pub mod pbr_cmds; pub mod meshmap_cmds; pub mod painting; pub mod path_tool; +pub mod pbr_cmds; // Mesh reimport reads files: native only. #[cfg(not(target_arch = "wasm32"))] mod preserve; diff --git a/crates/engine/src/pbr_cmds.rs b/crates/engine/src/pbr_cmds.rs index 19b62986..22fbf460 100644 --- a/crates/engine/src/pbr_cmds.rs +++ b/crates/engine/src/pbr_cmds.rs @@ -59,7 +59,7 @@ pub type FileList = Vec<(String, u64)>; pub type Loader<'a> = &'a dyn Fn(&str) -> std::result::Result, String>; /// How to import. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct PbrOptions { pub name: Option, /// Forces every normal map's convention; `None` reads it from the file name (default OpenGL). @@ -69,24 +69,19 @@ pub struct PbrOptions { pub choose: BTreeMap, } -impl Default for PbrOptions { - fn default() -> Self { - PbrOptions { name: None, normal_format: None, projection: Projection::default(), choose: BTreeMap::new() } - } -} - /// The detection for a file list with the caller's choices applied. fn detect_with(files: &FileList, choose: &BTreeMap) -> Result { let mut set = pbr_maps::detect(files.iter().map(|(n, s)| (n.as_str(), *s))); for (kind, file) in choose { let list = set.candidates.get(kind).ok_or_else(|| EngineError::Other(format!("choose: no file names the map {}", kind.id())))?; - let pick = list.iter().find(|c| c.name == *file).ok_or_else(|| EngineError::Other(format!("choose: {file} is not a candidate for {}", kind.id())))?.clone(); + let pick = + list.iter().find(|c| c.name == *file).ok_or_else(|| EngineError::Other(format!("choose: {file} is not a candidate for {}", kind.id())))?.clone(); // The previously chosen file becomes an ignored one, the picked one stops being ignored. - if let Some(old) = set.chosen.insert(*kind, pick.clone()) { - if old.name != pick.name { - set.ignored.retain(|(n, _)| *n != pick.name); - set.ignored.push((old.name, format!("another file is used for {}", kind.id()))); - } + if let Some(old) = set.chosen.insert(*kind, pick.clone()) + && old.name != pick.name + { + set.ignored.retain(|(n, _)| *n != pick.name); + set.ignored.push((old.name, format!("another file is used for {}", kind.id()))); } } Ok(set) @@ -116,6 +111,9 @@ fn report(set: &PbrSet, forced: Option) -> Value { }) } +/// A file's bytes and its decoded image. +type Loaded = (Arc>, texturecraft_formats::DecodedImage); + /// How a channel's image is made from the file chosen for a map. #[derive(Clone, Copy, PartialEq)] enum Take { @@ -160,7 +158,9 @@ impl Session { } // Separate files win; a packed image fills what they leave open (R = occlusion, G = roughness, B = metallic). let orm = has(MapKind::PackedOrm); - for (channel, kind, index) in [(Channel::AmbientOcclusion, MapKind::AmbientOcclusion, 0), (Channel::Roughness, MapKind::Roughness, 1), (Channel::Metallic, MapKind::Metallic, 2)] { + for (channel, kind, index) in + [(Channel::AmbientOcclusion, MapKind::AmbientOcclusion, 0), (Channel::Roughness, MapKind::Roughness, 1), (Channel::Metallic, MapKind::Metallic, 2)] + { if has(kind) { plan.push((channel, kind, Take::Whole)); } else if channel == Channel::Roughness && has(MapKind::Glossiness) { @@ -182,7 +182,7 @@ impl Session { let mut channels: Vec = Vec::new(); let mut total_texels = 0u64; // Decoded source files by name (a packed image feeds three channels). - let mut sources: BTreeMap>, texturecraft_formats::DecodedImage)>> = BTreeMap::new(); + let mut sources: BTreeMap> = BTreeMap::new(); // Resource per (file, how) so a file used twice is stored once. let mut made: BTreeMap<(String, usize), String> = BTreeMap::new(); @@ -334,12 +334,7 @@ fn options(cmd: &str, p: &Value) -> Result { choose.insert(kind, file.to_string()); } } - Ok(PbrOptions { - name: strp(p, "name").map(str::to_string), - normal_format, - projection: crate::layer_cmds::projection_from(p, cmd, None)?, - choose, - }) + Ok(PbrOptions { name: strp(p, "name").map(str::to_string), normal_format, projection: crate::layer_cmds::projection_from(p, cmd, None)?, choose }) } /// Pick the one group of files that holds a material: the files at the root if they hold maps, @@ -359,7 +354,10 @@ fn pick_group(cmd: &str, files: &FileList) -> Result { [one] => Ok(format!("{one}/")), many => { let shown: Vec<&str> = many.iter().take(8).map(|d| d.as_str()).collect(); - Err(bad(cmd, format!("{} folders hold PBR maps ({}{}): import one of them", many.len(), shown.join(", "), if many.len() > 8 { ", ..." } else { "" }))) + Err(bad( + cmd, + format!("{} folders hold PBR maps ({}{}): import one of them", many.len(), shown.join(", "), if many.len() > 8 { ", ..." } else { "" }), + )) } } } @@ -440,7 +438,8 @@ mod native { walk(&root, &root, 0, &mut files); let group = pick_group(cmd, &files)?; let files: FileList = files.into_iter().filter(|(n, _)| n.strip_prefix(group.as_str()).is_some_and(|rest| !rest.contains('/'))).collect(); - let name = if group.is_empty() { stem(path) } else { stem(group.trim_end_matches('/')) }.or_else(|| root.file_name().map(|n| n.to_string_lossy().into_owned())); + let name = if group.is_empty() { stem(path) } else { stem(group.trim_end_matches('/')) } + .or_else(|| root.file_name().map(|n| n.to_string_lossy().into_owned())); let root = if group.is_empty() { root } else { root.join(group.trim_end_matches('/')) }; let files = files.into_iter().map(|(n, s)| (n.rsplit('/').next().unwrap_or(&n).to_string(), s)).collect(); return Ok(Found { source: Origin::Folder { root }, files, name }); @@ -508,7 +507,10 @@ fn scan(s: &mut Session, p: &Value) -> Result { let mut reply = report(&set, opts.normal_format); if let Some(o) = reply.as_object_mut() { o.insert("name".into(), json!(opts.name.or(found.name))); - o.insert("channelsAvailable".into(), json!(s.doc.project.texture_set(s.ts()).map(|t| t.channel_list()).unwrap_or_default().iter().map(|c| c.id()).collect::>())); + o.insert( + "channelsAvailable".into(), + json!(s.doc.project.texture_set(s.ts()).map(|t| t.channel_list()).unwrap_or_default().iter().map(|c| c.id()).collect::>()), + ); } Ok(reply) } diff --git a/crates/formats/src/pbr_maps.rs b/crates/formats/src/pbr_maps.rs index 50062244..356530fe 100644 --- a/crates/formats/src/pbr_maps.rs +++ b/crates/formats/src/pbr_maps.rs @@ -154,7 +154,8 @@ fn tokens(stem: &str) -> Vec { if let Some(p) = prev { let next_lower = chars.get(i + 1).is_some_and(|n| n.is_lowercase()); // aB -> a|B; ABc -> A|Bc; a1 -> a|1; 1a -> 1|a. - let split = (p.is_lowercase() && c.is_uppercase()) || (p.is_uppercase() && c.is_uppercase() && next_lower) || (p.is_alphabetic() != c.is_alphabetic()); + let split = + (p.is_lowercase() && c.is_uppercase()) || (p.is_uppercase() && c.is_uppercase() && next_lower) || (p.is_alphabetic() != c.is_alphabetic()); if split && !cur.is_empty() { out.push(std::mem::take(&mut cur)); } @@ -204,7 +205,9 @@ fn normal_format_of(t: &str) -> Option { fn word(t: &str) -> Option<(MapKind, u8)> { use MapKind::*; Some(match t { - "basecolor" | "basecolour" | "albedo" | "diffuse" | "diff" | "dif" | "col" | "color" | "colour" | "bc" | "basemap" | "maintex" | "alb" => (BaseColor, 3), + "basecolor" | "basecolour" | "albedo" | "diffuse" | "diff" | "dif" | "col" | "color" | "colour" | "bc" | "basemap" | "maintex" | "alb" => { + (BaseColor, 3) + } "roughness" | "rough" | "rgh" | "rou" => (Roughness, 3), "gloss" | "glossiness" | "glossy" | "smoothness" | "smooth" => (Glossiness, 3), "metallic" | "metalness" | "metal" | "mtl" | "met" => (Metallic, 3), @@ -215,9 +218,8 @@ fn word(t: &str) -> Option<(MapKind, u8)> { "emissive" | "emission" | "emit" | "emis" | "glow" | "emissivecolor" | "emissionmap" | "illum" => (Emissive, 3), "opacity" | "alpha" | "opac" | "transparency" | "cutout" | "coverage" => (Opacity, 3), "orm" | "arm" | "aorm" => (PackedOrm, 4), - "cavity" | "curvature" | "specular" | "spec" | "translucency" | "thickness" | "scatter" | "sss" | "fuzz" | "sheen" | "ior" | "mask" | "flow" | "reflection" => { - (Unused, 1) - } + "cavity" | "curvature" | "specular" | "spec" | "translucency" | "thickness" | "scatter" | "sss" | "fuzz" | "sheen" | "ior" | "mask" | "flow" + | "reflection" => (Unused, 1), _ => return None, }) } @@ -278,7 +280,8 @@ pub fn classify(name: &str) -> std::result::Result { let single = words.iter().rev().find(|(i, kind, _)| { // A lone `n` only names a normal map as the final meaningful token. let lone_n = toks.get(*i).is_some_and(|t| t == "n"); - !lone_n || (*kind == MapKind::Normal && toks.iter().skip(i + 1).all(|t| resolution_of(t).is_some() || normal_format_of(t).is_some() || t.ends_with("bit"))) + !lone_n + || (*kind == MapKind::Normal && toks.iter().skip(i + 1).all(|t| resolution_of(t).is_some() || normal_format_of(t).is_some() || t.ends_with("bit"))) }); match (pair, single) { (Some((pi, kind)), Some((si, _, _))) if pi >= *si => done(kind, 3), @@ -488,7 +491,18 @@ mod tests { fn empty_and_hostile_names_do_not_panic() { assert!(detect([]).is_empty()); let long = "a".repeat(100_000); - let weird = ["\u{0}.png", "....png", "ÄÖÜ_Ålbedo.png", "🙂.png", "_.png", "-.exr", long.as_str(), "x/../../y_albedo.png", "C:\\a\\b_albedo.png", "99999999999999_albedo.png"]; + let weird = [ + "\u{0}.png", + "....png", + "ÄÖÜ_Ålbedo.png", + "🙂.png", + "_.png", + "-.exr", + long.as_str(), + "x/../../y_albedo.png", + "C:\\a\\b_albedo.png", + "99999999999999_albedo.png", + ]; let set = detect(weird.iter().map(|n| (*n, 0))); assert!(set.chosen.contains_key(&BaseColor)); } diff --git a/crates/ui-egui/src/i18n/ar.rs b/crates/ui-egui/src/i18n/ar.rs index 6ef517e9..aef46c3d 100644 --- a/crates/ui-egui/src/i18n/ar.rs +++ b/crates/ui-egui/src/i18n/ar.rs @@ -1041,4 +1041,19 @@ pub const TABLE: &[(&str, &str)] = &[ ("Value Bounds Max", "الحد الأقصى للقيمة"), ("Value Bounds Min", "الحد الأدنى للقيمة"), ("Vertex Color", "لون الرأس"), + ("Add to the material library", "إضافة إلى مكتبة المواد"), + ("Added {name} to the library", "تمت إضافة {name} إلى المكتبة"), + ("Automatic", "تلقائي"), + ("Detected maps", "الخرائط المكتشفة"), + ("DirectX", "DirectX"), + ("Import", "استيراد"), + ("Import PBR texture folder...", "استيراد مجلد خامات PBR..."), + ("Import PBR textures", "استيراد خامات PBR"), + ("Layer name", "اسم الطبقة"), + ("Nothing recognised in this folder", "لم يتم التعرف على شيء في هذا المجلد"), + ("OpenGL", "OpenGL"), + ("Packed (occlusion, roughness, metallic)", "مدمجة (الإطباق، الخشونة، المعدنية)"), + ("Triplanar", "ثلاثي المستويات"), + ("UV", "UV"), + ("{n} files ignored", "تم تجاهل {n} ملفات"), ]; diff --git a/crates/ui-egui/src/i18n/de.rs b/crates/ui-egui/src/i18n/de.rs index caaabb14..b2e41ca6 100644 --- a/crates/ui-egui/src/i18n/de.rs +++ b/crates/ui-egui/src/i18n/de.rs @@ -1090,4 +1090,19 @@ pub const TABLE: &[(&str, &str)] = &[ ("Value Bounds Max", "Maximaler Grenzwert"), ("Value Bounds Min", "Minimaler Grenzwert"), ("Vertex Color", "Scheitelpunktfarbe"), + ("Add to the material library", "Zur Materialbibliothek hinzufügen"), + ("Added {name} to the library", "{name} zur Bibliothek hinzugefügt"), + ("Automatic", "Automatisch"), + ("Detected maps", "Erkannte Maps"), + ("DirectX", "DirectX"), + ("Import", "Importieren"), + ("Import PBR texture folder...", "PBR-Texturordner importieren..."), + ("Import PBR textures", "PBR-Texturen importieren"), + ("Layer name", "Ebenenname"), + ("Nothing recognised in this folder", "In diesem Ordner wurde nichts erkannt"), + ("OpenGL", "OpenGL"), + ("Packed (occlusion, roughness, metallic)", "Gepackt (Okklusion, Rauheit, Metallisch)"), + ("Triplanar", "Triplanar"), + ("UV", "UV"), + ("{n} files ignored", "{n} Dateien ignoriert"), ]; diff --git a/crates/ui-egui/src/i18n/es.rs b/crates/ui-egui/src/i18n/es.rs index 6c8ccccb..8e7e4f56 100644 --- a/crates/ui-egui/src/i18n/es.rs +++ b/crates/ui-egui/src/i18n/es.rs @@ -1075,4 +1075,19 @@ pub const TABLE: &[(&str, &str)] = &[ ("Value Bounds Max", "Límite máximo"), ("Value Bounds Min", "Límite mínimo"), ("Vertex Color", "Color de vértice"), + ("Add to the material library", "Añadir a la biblioteca de materiales"), + ("Added {name} to the library", "{name} añadido a la biblioteca"), + ("Automatic", "Automático"), + ("Detected maps", "Mapas detectados"), + ("DirectX", "DirectX"), + ("Import", "Importar"), + ("Import PBR texture folder...", "Importar carpeta de texturas PBR..."), + ("Import PBR textures", "Importar texturas PBR"), + ("Layer name", "Nombre de la capa"), + ("Nothing recognised in this folder", "No se reconoció nada en esta carpeta"), + ("OpenGL", "OpenGL"), + ("Packed (occlusion, roughness, metallic)", "Empaquetado (oclusión, rugosidad, metálico)"), + ("Triplanar", "Triplanar"), + ("UV", "UV"), + ("{n} files ignored", "{n} archivos ignorados"), ]; diff --git a/crates/ui-egui/src/i18n/fr.rs b/crates/ui-egui/src/i18n/fr.rs index f10bba8d..5726b061 100644 --- a/crates/ui-egui/src/i18n/fr.rs +++ b/crates/ui-egui/src/i18n/fr.rs @@ -1099,4 +1099,19 @@ pub const TABLE: &[(&str, &str)] = &[ ("Value Bounds Max", "Borne maximale"), ("Value Bounds Min", "Borne minimale"), ("Vertex Color", "Couleur des sommets"), + ("Add to the material library", "Ajouter à la bibliothèque de matériaux"), + ("Added {name} to the library", "{name} ajouté à la bibliothèque"), + ("Automatic", "Automatique"), + ("Detected maps", "Cartes détectées"), + ("DirectX", "DirectX"), + ("Import", "Importer"), + ("Import PBR texture folder...", "Importer un dossier de textures PBR..."), + ("Import PBR textures", "Importer des textures PBR"), + ("Layer name", "Nom du calque"), + ("Nothing recognised in this folder", "Rien de reconnu dans ce dossier"), + ("OpenGL", "OpenGL"), + ("Packed (occlusion, roughness, metallic)", "Packée (occlusion, rugosité, métallique)"), + ("Triplanar", "Triplanaire"), + ("UV", "UV"), + ("{n} files ignored", "{n} fichiers ignorés"), ]; diff --git a/crates/ui-egui/src/i18n/it.rs b/crates/ui-egui/src/i18n/it.rs index 1c19d597..9474bc7c 100644 --- a/crates/ui-egui/src/i18n/it.rs +++ b/crates/ui-egui/src/i18n/it.rs @@ -1084,4 +1084,19 @@ pub const TABLE: &[(&str, &str)] = &[ ("Value Bounds Max", "Limite massimo"), ("Value Bounds Min", "Limite minimo"), ("Vertex Color", "Colore dei vertici"), + ("Add to the material library", "Aggiungi alla libreria dei materiali"), + ("Added {name} to the library", "{name} aggiunto alla libreria"), + ("Automatic", "Automatico"), + ("Detected maps", "Mappe rilevate"), + ("DirectX", "DirectX"), + ("Import", "Importa"), + ("Import PBR texture folder...", "Importa cartella di texture PBR..."), + ("Import PBR textures", "Importa texture PBR"), + ("Layer name", "Nome del livello"), + ("Nothing recognised in this folder", "Nulla di riconosciuto in questa cartella"), + ("OpenGL", "OpenGL"), + ("Packed (occlusion, roughness, metallic)", "Impacchettata (occlusione, ruvidità, metallico)"), + ("Triplanar", "Triplanare"), + ("UV", "UV"), + ("{n} files ignored", "{n} file ignorati"), ]; diff --git a/crates/ui-egui/src/i18n/ja.rs b/crates/ui-egui/src/i18n/ja.rs index 068603b5..b96b7519 100644 --- a/crates/ui-egui/src/i18n/ja.rs +++ b/crates/ui-egui/src/i18n/ja.rs @@ -1069,4 +1069,19 @@ pub const TABLE: &[(&str, &str)] = &[ ("Value Bounds Max", "値の上限"), ("Value Bounds Min", "値の下限"), ("Vertex Color", "頂点カラー"), + ("Add to the material library", "マテリアルライブラリに追加"), + ("Added {name} to the library", "{name} をライブラリに追加しました"), + ("Automatic", "自動"), + ("Detected maps", "検出されたマップ"), + ("DirectX", "DirectX"), + ("Import", "インポート"), + ("Import PBR texture folder...", "PBR テクスチャフォルダをインポート..."), + ("Import PBR textures", "PBR テクスチャをインポート"), + ("Layer name", "レイヤー名"), + ("Nothing recognised in this folder", "このフォルダで認識できるものはありません"), + ("OpenGL", "OpenGL"), + ("Packed (occlusion, roughness, metallic)", "パック (オクルージョン、粗さ、メタリック)"), + ("Triplanar", "トライプラナー"), + ("UV", "UV"), + ("{n} files ignored", "{n} 個のファイルを無視しました"), ]; diff --git a/crates/ui-egui/src/i18n/ko.rs b/crates/ui-egui/src/i18n/ko.rs index 137d6177..cc6c46b0 100644 --- a/crates/ui-egui/src/i18n/ko.rs +++ b/crates/ui-egui/src/i18n/ko.rs @@ -1054,4 +1054,19 @@ pub const TABLE: &[(&str, &str)] = &[ ("Value Bounds Max", "값 상한"), ("Value Bounds Min", "값 하한"), ("Vertex Color", "정점 색상"), + ("Add to the material library", "머티리얼 라이브러리에 추가"), + ("Added {name} to the library", "{name}을(를) 라이브러리에 추가했습니다"), + ("Automatic", "자동"), + ("Detected maps", "감지된 맵"), + ("DirectX", "DirectX"), + ("Import", "가져오기"), + ("Import PBR texture folder...", "PBR 텍스처 폴더 가져오기..."), + ("Import PBR textures", "PBR 텍스처 가져오기"), + ("Layer name", "레이어 이름"), + ("Nothing recognised in this folder", "이 폴더에서 인식된 항목이 없습니다"), + ("OpenGL", "OpenGL"), + ("Packed (occlusion, roughness, metallic)", "패킹됨 (오클루전, 거칠기, 메탈릭)"), + ("Triplanar", "트라이플래너"), + ("UV", "UV"), + ("{n} files ignored", "파일 {n}개 무시됨"), ]; diff --git a/crates/ui-egui/src/i18n/pt_br.rs b/crates/ui-egui/src/i18n/pt_br.rs index 99627b25..c7aa4984 100644 --- a/crates/ui-egui/src/i18n/pt_br.rs +++ b/crates/ui-egui/src/i18n/pt_br.rs @@ -1072,4 +1072,19 @@ pub const TABLE: &[(&str, &str)] = &[ ("Value Bounds Max", "Limite máximo"), ("Value Bounds Min", "Limite mínimo"), ("Vertex Color", "Cor do vértice"), + ("Add to the material library", "Adicionar à biblioteca de materiais"), + ("Added {name} to the library", "{name} adicionado à biblioteca"), + ("Automatic", "Automático"), + ("Detected maps", "Mapas detectados"), + ("DirectX", "DirectX"), + ("Import", "Importar"), + ("Import PBR texture folder...", "Importar pasta de texturas PBR..."), + ("Import PBR textures", "Importar texturas PBR"), + ("Layer name", "Nome da camada"), + ("Nothing recognised in this folder", "Nada reconhecido nesta pasta"), + ("OpenGL", "OpenGL"), + ("Packed (occlusion, roughness, metallic)", "Empacotado (oclusão, rugosidade, metálico)"), + ("Triplanar", "Triplanar"), + ("UV", "UV"), + ("{n} files ignored", "{n} arquivos ignorados"), ]; diff --git a/crates/ui-egui/src/i18n/zh_hans.rs b/crates/ui-egui/src/i18n/zh_hans.rs index 18623ce3..54064669 100644 --- a/crates/ui-egui/src/i18n/zh_hans.rs +++ b/crates/ui-egui/src/i18n/zh_hans.rs @@ -1000,4 +1000,19 @@ pub const TABLE: &[(&str, &str)] = &[ ("Value Bounds Max", "值上限"), ("Value Bounds Min", "值下限"), ("Vertex Color", "顶点颜色"), + ("Add to the material library", "添加到材质库"), + ("Added {name} to the library", "已将 {name} 添加到库"), + ("Automatic", "自动"), + ("Detected maps", "检测到的贴图"), + ("DirectX", "DirectX"), + ("Import", "导入"), + ("Import PBR texture folder...", "导入 PBR 纹理文件夹..."), + ("Import PBR textures", "导入 PBR 纹理"), + ("Layer name", "图层名称"), + ("Nothing recognised in this folder", "此文件夹中没有可识别的内容"), + ("OpenGL", "OpenGL"), + ("Packed (occlusion, roughness, metallic)", "打包 (遮蔽、粗糙度、金属度)"), + ("Triplanar", "三平面"), + ("UV", "UV"), + ("{n} files ignored", "已忽略 {n} 个文件"), ]; diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 8bfc66c9..2c73c7ef 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -18,6 +18,7 @@ pub mod i18n; pub mod keymap; pub mod menus; pub mod parity; +pub mod pbr_import; pub mod profile; pub mod render_mode; pub mod shelf; @@ -82,6 +83,8 @@ pub struct TextureCraftApp { /// Texture sets the GPU could not hold, and the text of the one sticky toast naming them. pub(crate) gpu_memory: (Vec, Option), pub bake: Option, + /// The Import PBR textures dialog while it is open. + pub pbr_import: Option, pub resize: Option, pub link_cooks: Vec, pub thumbs: thumbs::Thumbs, @@ -142,6 +145,7 @@ impl TextureCraftApp { toasts: Vec::new(), gpu_memory: (Vec::new(), None), bake: None, + pbr_import: None, resize: None, link_cooks: Vec::new(), thumbs: thumbs::Thumbs::default(), @@ -445,7 +449,15 @@ impl eframe::App for TextureCraftApp { control::poll(self, ctx); web_files::poll(self); let dropped = ctx.input(|i| i.raw.dropped_files.clone()); - for f in &dropped { + // A folder, a zip or several PBR maps dropped together open the PBR import dialog. + #[cfg(not(target_arch = "wasm32"))] + let taken = !dropped.is_empty() && { + let paths: Vec = dropped.iter().map(|f| f.path().to_path_buf()).collect(); + pbr_import::dropped(self, &paths) + }; + #[cfg(target_arch = "wasm32")] + let taken = false; + for f in dropped.iter().filter(|_| !taken) { web_files::dropped(self, ctx, f); } self.poll_bake(ctx); diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index a2e85bf7..0512bf77 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -46,6 +46,7 @@ pub const CATALOG: &[Item] = &[ sep("File"), it("File", "Import resources...", "resource.import", None), it("File", "Import MaterialX material...", "file.importMaterialX", None), + it("File", "Import PBR texture folder...", "file.importPbrFolder", None), it("File", "Import material...", "material.load", None), it("File", "Export Textures...", "ui.export", Some("Ctrl+Shift+E")), it("File", "Export Mesh...", "file.exportMesh", None), diff --git a/crates/ui-egui/src/pbr_import.rs b/crates/ui-egui/src/pbr_import.rs new file mode 100644 index 00000000..3aefeecb --- /dev/null +++ b/crates/ui-egui/src/pbr_import.rs @@ -0,0 +1,202 @@ +//! File › Import PBR texture folder: pick a folder (or drop a folder, a zip or several image +//! files), check which maps were found, then import them as one fill layer. +//! +//! The dialog is a thin front end over `file.scanPbrFolder` and `file.importPbrFolder`: it +//! holds the choices (layer name, a file where several name one map, the normal-map convention, +//! the projection, whether to keep the material in the library) and passes them as params. + +use std::collections::BTreeMap; + +use egui::RichText; +use serde_json::{Value, json}; + +use crate::TextureCraftApp; +use crate::i18n::{n_, tr, trf}; +use crate::widgets::{combo, primary_button}; + +/// The open import dialog. +pub struct PbrDialog { + /// `{path}` or `{files}`: where the maps come from. + source: Value, + /// What the scan found. + scan: Value, + name: String, + /// The file picked per map where several were found. + choose: BTreeMap, + /// 0: automatic (from the file name), 1: OpenGL, 2: DirectX. + normal: usize, + triplanar: bool, + library: bool, +} + +/// Map ids in the order they are listed, with their English label. +const MAPS: [(&str, &str); 10] = [ + ("baseColor", "Base color"), + ("roughness", "Roughness"), + ("glossiness", "Glossiness"), + ("metallic", "Metallic"), + ("normal", "Normal"), + ("height", "Height"), + ("ambientOcclusion", "Ambient occlusion"), + ("emissive", "Emissive"), + ("opacity", "Opacity"), + ("orm", n_("Packed (occlusion, roughness, metallic)")), +]; + +/// Scan `source` and open the dialog on the result (an error toast when nothing is found). +pub fn begin(app: &mut TextureCraftApp, source: Value) { + let Some(scan) = app.run("file.scanPbrFolder", source.clone()) else { return }; + let name = scan.get("name").and_then(Value::as_str).unwrap_or_default().to_string(); + app.pbr_import = Some(PbrDialog { source, scan, name, choose: BTreeMap::new(), normal: 0, triplanar: false, library: false }); +} + +/// File › Import PBR texture folder: choose the folder. +#[cfg(not(target_arch = "wasm32"))] +pub fn pick(app: &mut TextureCraftApp) { + if let Some(dir) = rfd::FileDialog::new().pick_folder() { + begin(app, json!({ "path": dir.display().to_string() })); + } +} + +/// Dropped paths: a folder, a zip, or two or more image files that name PBR maps open the +/// dialog. Returns true when the drop was taken. +#[cfg(not(target_arch = "wasm32"))] +pub fn dropped(app: &mut TextureCraftApp, paths: &[std::path::PathBuf]) -> bool { + use texturecraft_engine::formats::pbr_maps; + if let Some(dir) = paths.iter().find(|p| p.is_dir()) { + begin(app, json!({ "path": dir.display().to_string() })); + return true; + } + if let Some(zip) = paths.iter().find(|p| p.extension().is_some_and(|e| e.eq_ignore_ascii_case("zip"))) { + begin(app, json!({ "path": zip.display().to_string() })); + return true; + } + let names: Vec = paths.iter().filter_map(|p| p.file_name().map(|n| n.to_string_lossy().into_owned())).collect(); + if names.len() >= 2 && pbr_maps::detect(names.iter().map(|n| (n.as_str(), 0))).chosen.len() >= 2 { + begin(app, json!({ "files": paths.iter().map(|p| p.display().to_string()).collect::>() })); + return true; + } + false +} + +fn label_of(id: &str) -> &'static str { + MAPS.iter().find(|(i, _)| *i == id).map_or("", |(_, l)| *l) +} + +/// The dialog, while open. +pub fn window(app: &mut TextureCraftApp, ctx: &egui::Context) { + let Some(mut d) = app.pbr_import.take() else { return }; + let mut open = true; + let (mut go, mut cancel) = (false, false); + crate::windows::dialog("pbrImport", tr("Import PBR textures"), &mut open).show(ctx, |ui| { + ui.set_min_width(380.0); + ui.horizontal(|ui| { + ui.label(tr("Layer name")); + ui.text_edit_singleline(&mut d.name); + }); + ui.add_space(4.0); + ui.label(RichText::new(tr("Detected maps")).strong()); + let maps = d.scan.get("maps").and_then(Value::as_object).cloned().unwrap_or_default(); + let candidates = d.scan.get("candidates").and_then(Value::as_object).cloned().unwrap_or_default(); + for (id, _) in MAPS { + let Some(file) = maps.get(id).and_then(Value::as_str) else { continue }; + let shown = d.choose.get(id).map(String::as_str).unwrap_or(file).to_string(); + match candidates.get(id).and_then(Value::as_array) { + Some(all) => { + combo(ui, tr(label_of(id)), shown, |ui| { + for c in all.iter().filter_map(Value::as_str) { + let mut cur = d.choose.get(id).cloned().unwrap_or_else(|| file.to_string()); + if ui.selectable_value(&mut cur, c.to_string(), c).clicked() { + d.choose.insert(id.to_string(), cur); + } + } + }); + } + None => { + ui.horizontal(|ui| { + ui.label(tr(label_of(id))); + ui.label(RichText::new(shown).weak()); + }); + } + } + } + if maps.is_empty() { + ui.label(tr("Nothing recognised in this folder")); + } + let ignored = d.scan.get("ignored").and_then(Value::as_array).map_or(0, Vec::len); + if ignored > 0 { + ui.label(RichText::new(trf("{n} files ignored", &[("n", &ignored)])).weak()); + } + ui.add_space(6.0); + if maps.contains_key("normal") { + let found = d.scan.get("normalFormat").and_then(Value::as_str).unwrap_or("openGl") == "directX"; + let auto = if found { format!("{} ({})", tr("Automatic"), tr("DirectX")) } else { format!("{} ({})", tr("Automatic"), tr("OpenGL")) }; + let shown = [auto.clone(), tr("OpenGL").to_string(), tr("DirectX").to_string()][d.normal.min(2)].clone(); + combo(ui, tr("Normal map format"), shown, |ui| { + ui.selectable_value(&mut d.normal, 0, auto); + ui.selectable_value(&mut d.normal, 1, tr("OpenGL")); + ui.selectable_value(&mut d.normal, 2, tr("DirectX")); + }); + } + combo(ui, tr("Projection"), if d.triplanar { tr("Triplanar") } else { tr("UV") }, |ui| { + ui.selectable_value(&mut d.triplanar, false, tr("UV")); + ui.selectable_value(&mut d.triplanar, true, tr("Triplanar")); + }); + #[cfg(not(target_arch = "wasm32"))] + ui.checkbox(&mut d.library, tr("Add to the material library")); + ui.add_space(8.0); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + ui.add_enabled_ui(!maps.is_empty(), |ui| go = ui.add(primary_button(ui, tr("Import"))).clicked()); + cancel = ui.button(tr("Cancel")).clicked(); + }); + }); + if go { + run(app, &d); + } + if open && !go && !cancel { + app.pbr_import = Some(d); + } +} + +/// Import with the dialog's choices. +fn run(app: &mut TextureCraftApp, d: &PbrDialog) { + let mut p = d.source.clone(); + p["name"] = json!(d.name.trim()); + p["normalFormat"] = json!(["auto", "openGl", "directX"][d.normal.min(2)]); + if d.triplanar { + p["projection"] = json!("triplanar"); + } + if !d.choose.is_empty() { + p["choose"] = json!(d.choose); + } + let Some(r) = app.run("file.importPbrFolder", p) else { return }; + let name = r.get("name").and_then(Value::as_str).unwrap_or_default().to_string(); + for w in r.get("warnings").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str) { + app.session.log.push(format!("PBR {name}: {w}")); + } + app.toast(trf("Imported {name}", &[("name", &name)]), false); + app.ui.dock.focus(crate::docking::Panel::Layers); + #[cfg(not(target_arch = "wasm32"))] + if d.library + && let Some(layer) = r.get("layer").and_then(Value::as_u64) + { + save_to_library(app, layer, &name); + } +} + +/// Keep the imported layer as a smart material in the user library (shelf › Materials). +#[cfg(not(target_arch = "wasm32"))] +fn save_to_library(app: &mut TextureCraftApp, layer: u64, name: &str) { + let stem = texturecraft_engine::formats::export::sanitize_file_stem(name); + let dir = crate::library_dir(); + let mut path = dir.join(format!("{stem}.{}", texturecraft_engine::material_lib::SMART_MATERIAL_EXT)); + let mut n = 2; + while path.exists() { + path = dir.join(format!("{stem} {n}.{}", texturecraft_engine::material_lib::SMART_MATERIAL_EXT)); + n += 1; + } + if app.run("material.save", json!({ "layer": layer, "path": path.display().to_string(), "name": name })).is_some() { + app.library = None; + app.toast(trf("Added {name} to the library", &[("name", &name)]), false); + } +} diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs index c8bd72c5..f905c0a5 100644 --- a/crates/ui-egui/src/windows.rs +++ b/crates/ui-egui/src/windows.rs @@ -24,6 +24,7 @@ pub fn show(app: &mut TextureCraftApp, ctx: &egui::Context) { crate::windows::export(app, ctx); #[cfg(target_arch = "wasm32")] export_mesh(app, ctx); + crate::pbr_import::window(app, ctx); unsaved(app, ctx); } @@ -101,7 +102,7 @@ pub(crate) fn window<'a>(id: &str, title: &str, open: &'a mut bool) -> egui::Win } /// A centered dialog; `id` keeps its place across UI languages. -fn dialog<'a>(id: &str, title: &str, open: &'a mut bool) -> egui::Window<'a> { +pub(crate) fn dialog<'a>(id: &str, title: &str, open: &'a mut bool) -> egui::Window<'a> { egui::Window::new(title.to_owned()) .id(egui::Id::unique(id)) .open(open) @@ -793,6 +794,10 @@ pub fn file_action(app: &mut TextureCraftApp, _ctx: &egui::Context, id: &str) -> } true } + "file.importPbrFolder" => { + crate::pbr_import::pick(app); + true + } "file.exportMesh" => { let name = format!("{}.obj", texturecraft_engine::formats::export::sanitize_file_stem(&app.session.doc.project.name)); if let Some(p) = rfd::FileDialog::new() diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 3d7c2206..cb427a28 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -223,6 +223,57 @@ printf '%s\n' '{"id":1,"method":"command.run","params":{"id":"project.new","para '{"id":2,"method":"render","params":{"width":512,"height":512}}' | texturecraft-cli serve ``` +## PBR texture folders + +`file.importPbrFolder` turns a folder, a `.zip` or a list of image files of PBR maps into one fill layer +(one undo step), the way `file.importMaterialX` does for a `.mtlx`. `file.scanPbrFolder` takes the same +`path` / `files` and reports what would be used without changing anything. Both are `file.*` commands, so +they run over the control channel and the CLI, not through MCP `command_run` (which refuses `file.*` +commands and host paths). + +| Command | Params | +|---|---| +| `file.importPbrFolder` | `{path \| files, name?, normalFormat?: auto\|openGl\|directX, projection?: uv\|triplanar\|planar..., scale?, choose?: {map: file}}` → `{layer, name, maps, candidates, normalFormat, channels, resources, ignored, warnings}` | +| `file.scanPbrFolder` | `{path \| files, normalFormat?, choose?}` → `{name, maps, candidates, normalFormat, ignored, channelsAvailable}` | + +Maps are found by file name, case-insensitively, from the last known word in the name; resolution +(`2k`, `4096`), bit depth and other tokens are skipped: + +| Map (`maps` key) | Words | +|---|---| +| `baseColor` | `basecolor`, `base_color`, `albedo`, `diffuse`, `diff`, `col`, `color`, `bc`, `basemap`, `maintex` | +| `roughness` | `roughness`, `rough`, `rgh` | +| `glossiness` (inverted into roughness; a roughness file wins) | `gloss`, `glossiness`, `smoothness` | +| `metallic` | `metallic`, `metalness`, `metal`, `mtl` | +| `normal` | `normal`, `nrm`, `nor`, `norm`, `normalmap`, `bumpmap`, a trailing `n` | +| `height` | `height`, `disp`, `displacement`, `hgt`, `bump` (weakest) | +| `ambientOcclusion` | `ao`, `ambientocclusion`, `ambient_occlusion`, `occlusion`, `occ` | +| `emissive` | `emissive`, `emission`, `emit`, `glow` | +| `opacity` | `opacity`, `alpha`, `transparency` | +| `orm` (split: R occlusion, G roughness, B metallic; separate files win) | `orm`, `arm`, `ao_rough_metal` | + +- **Normal convention.** `gl` / `opengl` in the name is OpenGL, `dx` / `directx` is DirectX (the layer's + normal source is flagged DirectX, so its green is flipped where it is sampled). With neither the map is + OpenGL. `normalFormat: openGl|directX` forces every normal map; `auto` (the default) reads the name. +- **Ignored**: other files, dot files and `__MACOSX`, preview images (`preview`, `thumb`, `thumbnail`, + `sphere`, `render`, `swatch`, `icon`), and maps with no channel here (cavity, curvature, specular...). + Each is listed in `ignored` with the reason. +- **Several files for one map** (resolutions, formats, GL and DX): the OpenGL normal, then the highest + resolution token, then the largest file wins. A programmatic call never asks; `candidates` lists the + alternatives and `choose: {"baseColor": "file.png"}` picks one (the app's dialog shows a drop-down). +- **Where.** A folder's own files if they hold maps; otherwise the one sub-folder that does (several are + an error that names them). The layer is named after the folder, archive or shared file-name prefix. +- **Channels** the texture set doesn't have are skipped with a warning. Projection defaults to UV + (`scale` 1); `projection: triplanar` is the option for models without good UVs. +- **Hostile input.** Links, pipes and devices are not followed, at most 4096 files are listed, a file is + at most 256 MB, an image 64 M texels (320 M per import), a zip entry is read through the same limit. + An image that doesn't decode is skipped with a warning; if none decodes the import is an error that + changes nothing. + +In the app: File › Import PBR texture folder... or drop a folder, a `.zip` or several map files on the +window. The dialog shows the maps found, lets you pick between candidates, set the normal convention and +projection, and keep the material in the library (shelf › Materials). + ## MaterialCraft materials A `.mcmat` portable material (MaterialCraft's format, read here by `formats::mcmat`; the manifest is diff --git a/docs/parity.md b/docs/parity.md index d6543d54..2b61a985 100644 --- a/docs/parity.md +++ b/docs/parity.md @@ -2,7 +2,7 @@ Generated by `TC_UPDATE_PARITY=1 cargo test -p texturecraft-ui-egui parity`. Each row is a menu item of the target menu layout with TextureCraft's command id; **live** means the id is implemented. Feature parity beyond menus is tracked in [`ROADMAP.md`](../ROADMAP.md). -**55 of 55 menu items live (100%).** +**56 of 56 menu items live (100%).** ## File @@ -18,6 +18,7 @@ Generated by `TC_UPDATE_PARITY=1 cargo test -p texturecraft-ui-egui parity`. Eac | Remove unused resources... | `file.removeUnused` | | live | | Import resources... | `resource.import` | | live | | Import MaterialX material... | `file.importMaterialX` | | live | +| Import PBR texture folder... | `file.importPbrFolder` | | live | | Import material... | `material.load` | | live | | Export Textures... | `ui.export` | Ctrl+Shift+E | live | | Export Mesh... | `file.exportMesh` | | live | From b8e7782830e6c14870af102a8a4b59d743d8a391 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Tue, 17 Nov 2026 07:42:09 +0000 Subject: [PATCH 3/4] Auto: Fix PBR import names, map detection, and stale dialogs Use the documented RAM fallback for zero readings. Preserve dotted folder names, recognize split normal aliases, honor later map names, and clear import dialogs after project replacement. Count same-map drop candidates and clean test fixtures on unwind. --- crates/engine/src/pbr_cmds/native.rs | 5 +- crates/engine/tests/it/pbr_import.rs | 77 ++++++++++++------- .../engine/tests/it/pbr_import/test_folder.rs | 28 +++++++ crates/formats/src/pbr_map/mod.rs | 24 +++++- crates/formats/src/project_file/entries.rs | 29 ++++++- crates/ui-egui/src/lib.rs | 4 + crates/ui-egui/src/pbr_import.rs | 3 +- crates/ui-egui/src/web_files.rs | 1 + crates/ui-egui/tests/ui_snapshots.rs | 56 ++++++++++++++ 9 files changed, 189 insertions(+), 38 deletions(-) create mode 100644 crates/engine/tests/it/pbr_import/test_folder.rs diff --git a/crates/engine/src/pbr_cmds/native.rs b/crates/engine/src/pbr_cmds/native.rs index 90aca641..258850d4 100644 --- a/crates/engine/src/pbr_cmds/native.rs +++ b/crates/engine/src/pbr_cmds/native.rs @@ -73,6 +73,7 @@ pub(super) fn find(cmd: &str, p: &Value) -> Result { let path = strp(p, "path").filter(|s| !s.trim().is_empty()).ok_or_else(|| bad(cmd, "`path` (a folder or .zip) or `files` is required"))?; let metadata = std::fs::symlink_metadata(path).map_err(|e| bad(cmd, format!("cannot read {path}: {e}")))?; let stem = |s: &str| Path::new(s).file_stem().map(|n| n.to_string_lossy().replace(['_', '-'], " ")); + let dir_name = |s: &str| Path::new(s).file_name().map(|n| n.to_string_lossy().replace(['_', '-'], " ")); if metadata.is_dir() { let root = Path::new(path).canonicalize().map_err(|e| bad(cmd, format!("cannot read {path}: {e}")))?; let mut files = FileList::new(); @@ -80,7 +81,7 @@ pub(super) fn find(cmd: &str, p: &Value) -> Result { walk(&root, &root, 0, &mut visited_entries, &mut files).map_err(|error| bad(cmd, format!("cannot scan {path}: {error}")))?; let group = pick_group(cmd, &files)?; let files: FileList = files.into_iter().filter(|(n, _)| n.strip_prefix(group.as_str()).is_some_and(|rest| !rest.contains('/'))).collect(); - let name = if group.is_empty() { stem(path) } else { stem(group.trim_end_matches('/')) } + let name = if group.is_empty() { dir_name(path) } else { dir_name(group.trim_end_matches('/')) } .or_else(|| root.file_name().map(|n| n.to_string_lossy().into_owned())); let root = if group.is_empty() { root } else { root.join(group.trim_end_matches('/')) }; let files = files.into_iter().map(|(n, s)| (n.rsplit('/').next().unwrap_or(&n).to_string(), s)).collect(); @@ -97,7 +98,7 @@ pub(super) fn find(cmd: &str, p: &Value) -> Result { let files = zip.list(MAX_FILES); let group = pick_group(cmd, &files)?; let files: FileList = files.into_iter().filter(|(n, _)| n.strip_prefix(group.as_str()).is_some_and(|rest| !rest.contains('/'))).collect(); - let name = if group.is_empty() { stem(path) } else { stem(group.trim_end_matches('/')) }; + let name = if group.is_empty() { stem(path) } else { dir_name(group.trim_end_matches('/')) }; return Ok(Found { source: Origin::Zip(RefCell::new(zip)), files, name }); } diff --git a/crates/engine/tests/it/pbr_import.rs b/crates/engine/tests/it/pbr_import.rs index 58e6e6a6..787c0ed2 100644 --- a/crates/engine/tests/it/pbr_import.rs +++ b/crates/engine/tests/it/pbr_import.rs @@ -2,13 +2,17 @@ //! one fill layer. use std::io::Write as _; -use std::path::{Path, PathBuf}; +use std::path::Path; use serde_json::{Value, json}; use texturecraft_engine::core::{Channel, Fill, LayerId, LayerKind, NormalFormat, Projection, Source}; use texturecraft_engine::geom::primitives; use texturecraft_engine::{Session, execute}; +mod test_folder; + +use test_folder::TestFolder; + fn new_session() -> Session { Session::new_project("t", primitives::builtin("plane").unwrap(), "builtin:plane", None, 128).unwrap() } @@ -20,11 +24,25 @@ fn session_with_ao() -> Session { session } -fn folder(tag: &str) -> PathBuf { - let directory = std::env::temp_dir().join(format!("tc_pbr_{tag}_{}", std::process::id())); - let _ = std::fs::remove_dir_all(&directory); - std::fs::create_dir_all(&directory).unwrap(); - directory +fn folder(tag: &str) -> TestFolder { + TestFolder::new(tag) +} + +#[test] +fn fixture_directory_is_removed_during_unwind() { + let path = std::cell::RefCell::new(None); + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let directory = folder("unwind"); + *path.borrow_mut() = Some(directory.as_path().to_path_buf()); + std::fs::write(directory.join("fixture"), b"fixture").unwrap(); + panic!("exercise fixture cleanup"); + })); + + assert!(result.is_err()); + let path = path.into_inner().unwrap(); + let remained = path.exists(); + let _ = std::fs::remove_dir_all(&path); + assert!(!remained, "fixture directory remained after unwind: {}", path.display()); } fn png(path: &Path, rgb: [u8; 3]) { @@ -88,7 +106,7 @@ fn a_folder_becomes_one_fill_layer_in_one_undo_step() { assert!(matches!(fill.projection, Projection::Uv { scale, .. } if scale == [1.0, 1.0])); let layer_name = session.doc.project.layer(LayerId(layer)).unwrap().name.clone(); assert!(layer_name.starts_with("tc pbr ok"), "named after the folder: {layer_name}"); - assert_eq!(reply["name"], format!("tc pbr ok {} 1", std::process::id())); + assert_eq!(reply["name"], layer_name); // Every decoded image is ready for the viewport for n in session.doc.resources.keys() { @@ -97,7 +115,6 @@ fn a_folder_becomes_one_fill_layer_in_one_undo_step() { execute(&mut session, "edit.undo", &Value::Null).unwrap(); assert_eq!(session.doc.resources.len(), res); assert!(session.doc.project.layer(LayerId(layer)).is_err()); - let _ = std::fs::remove_dir_all(&directory); } #[test] @@ -112,7 +129,6 @@ fn a_directx_normal_map_is_flipped_to_opengl_where_it_is_sampled() { import(&mut session, &directory, extra).unwrap(); session.update(); let v = session.caches[0].composite.value(Channel::Normal, 64, 64); - let _ = std::fs::remove_dir_all(&directory); v[1] }; let (dx, gl) = (green(&["wall_normal_dx.png"], json!({})), green(&["wall_normal_gl.png"], json!({}))); @@ -166,7 +182,6 @@ fn a_packed_orm_image_is_split_into_three_maps() { assert!((session.caches[0].composite.value(Channel::Roughness, 64, 64)[0] - 1.0).abs() < 0.01); assert!((session.caches[0].composite.value(Channel::Metallic, 64, 64)[0] - 0.9).abs() < 0.01, "still from the ORM image"); assert!(reply["maps"]["roughness"] == "floor_Roughness.png"); - let _ = std::fs::remove_dir_all(&directory); } #[test] @@ -179,7 +194,6 @@ fn glossiness_becomes_roughness() { assert!(reply["channels"].as_array().unwrap().contains(&json!("roughness")), "{reply}"); session.update(); assert!((session.caches[0].composite.value(Channel::Roughness, 64, 64)[0] - 0.2).abs() < 0.01); - let _ = std::fs::remove_dir_all(&directory); } #[test] @@ -199,8 +213,6 @@ fn missing_maps_stay_at_their_defaults() { let (layers, res) = (session.doc.project.texture_sets[0].stack.len(), session.doc.resources.len()); assert!(import(&mut session, &empty, json!({})).is_err()); assert_eq!((session.doc.project.texture_sets[0].stack.len(), session.doc.resources.len()), (layers, res)); - let _ = std::fs::remove_dir_all(&directory); - let _ = std::fs::remove_dir_all(&empty); } #[test] @@ -222,7 +234,6 @@ fn several_candidates_use_the_best_and_can_be_chosen() { assert!(import(&mut session, &directory, json!({"choose": {"baseColor": "nope.png"}})).is_err()); assert!(import(&mut session, &directory, json!({"choose": {"roughness": "m_albedo_1k.png"}})).is_err()); assert!(import(&mut session, &directory, json!({"choose": {"bogus": "x"}})).is_err()); - let _ = std::fs::remove_dir_all(&directory); } #[test] @@ -241,7 +252,23 @@ fn projection_name_and_selected_files() { let reply = execute(&mut session, "file.importPbrFolder", &json!({"files": [directory.join("moss_col.png").display().to_string()], "name": "Mossy"})).unwrap(); assert!(session.doc.project.layer(LayerId(reply["layer"].as_u64().unwrap())).unwrap().name.starts_with("Mossy")); - let _ = std::fs::remove_dir_all(&directory); +} + +#[test] +fn folder_and_archive_group_names_keep_dots() { + let directory = folder("wood_floor_1.5k"); + png(&directory.join("wood_albedo.png"), [1, 2, 3]); + let mut session = new_session(); + let reply = import(&mut session, &directory, json!({})).unwrap(); + assert!(reply["name"].as_str().unwrap().contains("1.5k"), "{reply}"); + + let mut archive = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + archive.start_file("wood_1.5k/wood_albedo.png", zip::write::SimpleFileOptions::default()).unwrap(); + archive.write_all(&std::fs::read(directory.join("wood_albedo.png")).unwrap()).unwrap(); + let zip_path = directory.join("wood.zip"); + std::fs::write(&zip_path, archive.finish().unwrap().into_inner()).unwrap(); + let reply = import(&mut session, &zip_path, json!({})).unwrap(); + assert_eq!(reply["name"], "wood 1.5k 1"); } #[test] @@ -283,8 +310,6 @@ fn a_zip_and_a_pack_of_folders() { // Garbage zip std::fs::write(directory.join("bad.zip"), b"PK\x03\x04garbage").unwrap(); assert!(execute(&mut session, "file.importPbrFolder", &json!({"path": directory.join("bad.zip").display().to_string()})).is_err()); - let _ = std::fs::remove_dir_all(&directory); - let _ = std::fs::remove_dir_all(&pack); } #[test] @@ -309,11 +334,13 @@ fn hostile_images_are_skipped_without_a_panic() { // A folder named like an image, and a link out of the folder std::fs::create_dir_all(directory.join("w_opacity.png")).unwrap(); #[cfg(unix)] - { - let outside = directory.parent().unwrap().join(format!("tc_pbr_outside_{}.png", std::process::id())); + let outside_target = { + let outside_dir = folder("outside"); + let outside = outside_dir.join("w_ao.png"); png(&outside, [9, 9, 9]); std::os::unix::fs::symlink(&outside, directory.join("w_ao.png")).unwrap(); - } + outside_dir + }; let mut session = new_session(); execute(&mut session, "textureSet.channel.add", &json!({"channel":"emissive"})).unwrap(); let reply = import(&mut session, &directory, json!({})).unwrap(); @@ -326,7 +353,7 @@ fn hostile_images_are_skipped_without_a_panic() { #[cfg(unix)] { assert!(!reply["maps"].as_object().unwrap().contains_key("ambientOcclusion"), "a link is not followed: {reply}"); - let _ = std::fs::remove_file(directory.parent().unwrap().join(format!("tc_pbr_outside_{}.png", std::process::id()))); + assert!(outside_target.join("w_ao.png").exists()); } // Every map hostile: an error, and the project is as it was @@ -335,8 +362,6 @@ fn hostile_images_are_skipped_without_a_panic() { let before = (session.doc.project.texture_sets[0].stack.len(), session.doc.resources.len()); assert!(import(&mut session, &only_bad, json!({})).is_err()); assert_eq!((session.doc.project.texture_sets[0].stack.len(), session.doc.resources.len()), before); - let _ = std::fs::remove_dir_all(&directory); - let _ = std::fs::remove_dir_all(&only_bad); } #[test] @@ -353,7 +378,6 @@ fn bad_params_are_errors() { assert!(import(&mut session, &directory, json!({"normalFormat": "sideways"})).is_err()); assert!(import(&mut session, &directory, json!({"projection": "nope"})).is_err()); assert!(import(&mut session, &directory.join("a_albedo.png"), json!({})).is_err(), "a single image is not a folder or zip"); - let _ = std::fs::remove_dir_all(&directory); } #[test] @@ -407,7 +431,6 @@ fn repeated_import_reports_the_created_layer_name() { let layer = session.doc.project.layer(LayerId(reply["layer"].as_u64().unwrap())).unwrap(); assert_ne!(layer.name, "Moss"); assert_eq!(reply["name"], layer.name); - std::fs::remove_dir_all(directory).unwrap(); } #[test] @@ -421,7 +444,6 @@ fn scans_reject_excessive_entries_instead_of_silently_truncating() { let selected = execute(&mut session, "file.scanPbrFolder", &json!({"files": files})); assert!(selected.unwrap_err().to_string().contains("limit")); - std::fs::remove_dir_all(directory).unwrap(); } #[test] @@ -433,8 +455,7 @@ fn scans_count_empty_directories_toward_the_limit() { std::fs::create_dir(directory.join(index.to_string())).unwrap(); } - let scanned = execute(&mut session, "file.scanPbrFolder", &json!({"path": directory})); + let scanned = execute(&mut session, "file.scanPbrFolder", &json!({"path": directory.display().to_string()})); assert!(scanned.unwrap_err().to_string().contains("limit")); - std::fs::remove_dir_all(directory).unwrap(); } diff --git a/crates/engine/tests/it/pbr_import/test_folder.rs b/crates/engine/tests/it/pbr_import/test_folder.rs new file mode 100644 index 00000000..72d0584a --- /dev/null +++ b/crates/engine/tests/it/pbr_import/test_folder.rs @@ -0,0 +1,28 @@ +use std::path::PathBuf; +use std::sync::atomic::{AtomicUsize, Ordering}; + +pub(super) struct TestFolder(PathBuf); + +impl TestFolder { + pub(super) fn new(tag: &str) -> Self { + static NEXT_ID: AtomicUsize = AtomicUsize::new(0); + let id = NEXT_ID.fetch_add(1, Ordering::Relaxed); + let directory = std::env::temp_dir().join(format!("tc_pbr_{tag}_{}_{id}", std::process::id())); + std::fs::create_dir(&directory).unwrap(); + Self(directory) + } +} + +impl std::ops::Deref for TestFolder { + type Target = PathBuf; + + fn deref(&self) -> &PathBuf { + &self.0 + } +} + +impl Drop for TestFolder { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} diff --git a/crates/formats/src/pbr_map/mod.rs b/crates/formats/src/pbr_map/mod.rs index 29c84d0b..f189c733 100644 --- a/crates/formats/src/pbr_map/mod.rs +++ b/crates/formats/src/pbr_map/mod.rs @@ -163,20 +163,27 @@ pub fn classify(name: &str) -> std::result::Result { } let resolution = name_tokens.iter().filter_map(|t| resolution_of(t)).max().unwrap_or(0); - let normal = name_tokens.iter().find_map(|t| normal_format_of(t)); + let normal = name_tokens.iter().enumerate().find_map(|(i, t)| { + normal_format_of(t).or_else(|| match (t.as_str(), name_tokens.get(i + 1).map(String::as_str), name_tokens.get(i + 2).map(String::as_str)) { + ("direct", Some("x"), _) | ("d", Some("3"), Some("d")) => Some(NormalFormat::DirectX), + ("open", Some("gl"), _) => Some(NormalFormat::OpenGl), + _ => None, + }) + }); // (token index, kind, strength) of every known word let words: Vec<(usize, MapKind, u8)> = name_tokens.iter().enumerate().filter_map(|(i, t)| word(t).map(|(k, s)| (i, k, s))).collect(); let done = |kind: MapKind, strength: u8| Ok(Detected { kind, normal: if kind == MapKind::Normal { normal } else { None }, resolution, strength }); - // `ao rough metal` written as three words + // `ao rough metal` written as three words competes with later map names + let mut packed_end = None; for w in words.windows(3) { if let [(a, MapKind::AmbientOcclusion, _), (b, MapKind::Roughness, _), (c, MapKind::Metallic, _)] = w && *b == a + 1 && *c == b + 1 { - return done(MapKind::PackedOrm, 4); + packed_end = Some(*c); } } @@ -203,6 +210,9 @@ pub fn classify(name: &str) -> std::result::Result { || (*kind == MapKind::Normal && name_tokens.iter().skip(i + 1).all(|t| resolution_of(t).is_some() || normal_format_of(t).is_some() || t.ends_with("bit"))) }); + if packed_end.is_some_and(|end| pair.is_none_or(|(index, _)| end >= index) && single.is_none_or(|(index, _, _)| end >= *index)) { + return done(MapKind::PackedOrm, 4); + } match (pair, single) { (Some((pair_index, kind)), Some((single_index, _, _))) if pair_index >= *single_index => done(kind, 3), (Some((_, kind)), None) => done(kind, 3), @@ -375,12 +385,20 @@ mod tests { assert_eq!(n("a_normal_dx.png"), Some(NormalFormat::DirectX)); assert_eq!(n("a_NormalDX.png"), Some(NormalFormat::DirectX)); assert_eq!(n("a_nor_directx_2k.png"), Some(NormalFormat::DirectX)); + assert_eq!(n("brick_normal_DirectX.png"), Some(NormalFormat::DirectX)); + assert_eq!(n("brick_normal_d3d.png"), Some(NormalFormat::DirectX)); assert_eq!(n("a_normal.png"), None); // `dx` on a map that is not a normal map says nothing assert_eq!(n("a_roughness_dx.png"), None); } + #[test] + fn packed_sequence_yields_to_a_later_map_word() { + assert_eq!(kind("brick_ao_rough_metal_basecolor.png"), Some(BaseColor)); + assert_eq!(kind("brick_basecolor_ao_rough_metal.png"), Some(PackedOrm)); + } + #[test] fn several_candidates_pick_gl_then_resolution_then_size() { let set = detect([ diff --git a/crates/formats/src/project_file/entries.rs b/crates/formats/src/project_file/entries.rs index 7e0238e0..1ad13a9d 100644 --- a/crates/formats/src/project_file/entries.rs +++ b/crates/formats/src/project_file/entries.rs @@ -36,6 +36,8 @@ pub(super) struct Entry<'a> { mod limits { use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering}; + const FALLBACK_AVAILABLE_RAM_BYTES: u64 = 8 << 30; + /// Cores a save leaves to the UI (an autosave runs while the user keeps painting). static RESERVED_CORES: AtomicUsize = AtomicUsize::new(DEFAULT_RESERVED_CORES); /// Share of the available RAM, in percent, that entries in flight may hold. @@ -56,11 +58,30 @@ mod limits { /// RAM the system can give out now, in bytes (the total when the system does not say it; 8 GiB /// when the system tells nothing). pub fn available_ram() -> u64 { - let Ok(mut sys) = sysinfo::System::new() else { return 8 << 30 }; + let Ok(mut sys) = sysinfo::System::new() else { return FALLBACK_AVAILABLE_RAM_BYTES }; sys.refresh_memory(); - match sys.available_memory() { - 0 => sys.total_memory(), - n => n, + reported_ram(sys.available_memory(), sys.total_memory()) + } + + fn reported_ram(available: u64, total: u64) -> u64 { + if available != 0 { + available + } else if total != 0 { + total + } else { + FALLBACK_AVAILABLE_RAM_BYTES + } + } + + #[cfg(test)] + mod tests { + use super::reported_ram; + + #[test] + fn missing_memory_readings_use_fallback() { + assert_eq!(reported_ram(0, 0), 8 << 30); + assert_eq!(reported_ram(0, 12 << 30), 12 << 30); + assert_eq!(reported_ram(3 << 30, 12 << 30), 3 << 30); } } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index a0cc846e..bdc8f073 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -297,6 +297,10 @@ impl TextureCraftApp { let glide_from = (id.starts_with("camera.select") || id.starts_with("camera.bookmark")).then_some(self.session.camera); match texturecraft_engine::execute(&mut self.session, id, ¶ms) { Ok(v) => { + // A pending dialog must not import its old source into the replacement project + if matches!(id, "file.open" | "file.close" | "project.new" | "project.sample") { + self.pbr_import = None; + } if let Some(from) = glide_from.filter(|f| *f != self.session.camera) { self.camera_glide = Some(CameraGlide { from, to: self.session.camera, start: self.last_time }); self.session.camera = from; diff --git a/crates/ui-egui/src/pbr_import.rs b/crates/ui-egui/src/pbr_import.rs index 5bd924b8..84fa2550 100644 --- a/crates/ui-egui/src/pbr_import.rs +++ b/crates/ui-egui/src/pbr_import.rs @@ -75,7 +75,8 @@ pub fn dropped(app: &mut TextureCraftApp, paths: &[std::path::PathBuf]) -> bool } let names: Vec = paths.iter().filter_map(|p| p.file_name().map(|n| n.to_string_lossy().into_owned())).collect(); - if names.len() >= 2 && pbr_maps::detect(names.iter().map(|n| (n.as_str(), 0))).chosen.len() >= 2 { + let recognized = pbr_maps::detect(names.iter().map(|n| (n.as_str(), 0))).candidates.values().map(Vec::len).sum::(); + if recognized >= 2 { begin(app, json!({ "files": paths.iter().map(|p| p.display().to_string()).collect::>() })); return true; } diff --git a/crates/ui-egui/src/web_files.rs b/crates/ui-egui/src/web_files.rs index 759d5576..a436cd01 100644 --- a/crates/ui-egui/src/web_files.rs +++ b/crates/ui-egui/src/web_files.rs @@ -144,6 +144,7 @@ pub fn load(app: &mut TextureCraftApp, name: &str, bytes: Vec) { match next { Ok(next) => { app.session.switch_to(next); + app.pbr_import = None; app.toast(trf("Opened {name}", &[("name", &name)]), false); } Err(e) => app.toast(trf("Couldn't open {name}: {error}", &[("name", &name), ("error", &e)]), true), diff --git a/crates/ui-egui/tests/ui_snapshots.rs b/crates/ui-egui/tests/ui_snapshots.rs index 46968c26..32a647f8 100644 --- a/crates/ui-egui/tests/ui_snapshots.rs +++ b/crates/ui-egui/tests/ui_snapshots.rs @@ -67,6 +67,62 @@ fn pbr_import_dialog_keeps_choices_after_error() { save(&mut harness, &output_dir, "pbr-import-error"); } +#[test] +#[ignore] +fn pbr_drop_candidates_and_project_replacement() { + let Some(output_dir) = std::env::var_os("TC_UI_SNAPSHOTS").map(PathBuf::from) else { return }; + + std::fs::create_dir_all(&output_dir).unwrap(); + let source_dir = output_dir.join("pbr-drop-candidates"); + std::fs::create_dir_all(&source_dir).unwrap(); + let files: Vec<_> = ["m_albedo_1k.png", "m_albedo_4k.png", "notes.txt"].iter().map(|name| source_dir.join(name)).collect(); + for file in &files { + std::fs::write(file, b"fixture").unwrap(); + } + + let session = texturecraft_engine::sample_session(128, 64).unwrap(); + let mut harness = Harness::builder() + .with_size(egui::vec2(1280.0, 800.0)) + .with_pixels_per_point(1.0) + .wgpu() + .build_eframe(|context| TextureCraftApp::new(context, session)); + assert!(texturecraft_ui_egui::pbr_import::dropped(harness.state_mut(), &files)); + assert!(harness.state().pbr_import.is_some()); + harness.run_steps(3); + assert!(harness.get_all_by_role(egui::Role::ComboBox).any(|node| node.value().as_deref() == Some("m_albedo_4k.png"))); + save(&mut harness, &output_dir, "pbr-drop-candidates"); + + assert!(harness.state_mut().run("project.new", serde_json::json!({"mesh": "not-a-mesh"})).is_none()); + assert!(harness.state().pbr_import.is_some()); + assert!(harness.state_mut().run("project.new", serde_json::json!({"resolution": 128})).is_some()); + assert!(harness.state().pbr_import.is_none()); + assert!(!texturecraft_ui_egui::pbr_import::dropped(harness.state_mut(), &files[1..])); + std::fs::remove_dir_all(source_dir).unwrap(); +} + +#[test] +#[ignore] +fn pbr_dialog_is_cleared_when_project_changes() { + let Some(output_dir) = std::env::var_os("TC_UI_SNAPSHOTS").map(PathBuf::from) else { return }; + + std::fs::create_dir_all(&output_dir).unwrap(); + let source_dir = output_dir.join("pbr-replacement-source"); + std::fs::create_dir_all(&source_dir).unwrap(); + std::fs::write(source_dir.join("m_albedo.png"), b"fixture").unwrap(); + let session = texturecraft_engine::sample_session(128, 64).unwrap(); + let mut harness = Harness::builder() + .with_size(egui::vec2(1280.0, 800.0)) + .with_pixels_per_point(1.0) + .wgpu() + .build_eframe(|context| TextureCraftApp::new(context, session)); + texturecraft_ui_egui::pbr_import::begin(harness.state_mut(), serde_json::json!({"path": source_dir.display().to_string()})); + assert!(harness.state().pbr_import.is_some()); + + assert!(harness.state_mut().run("project.new", serde_json::json!({"resolution": 128})).is_some()); + assert!(harness.state().pbr_import.is_none()); + std::fs::remove_dir_all(source_dir).unwrap(); +} + /// Capture the moving preview and settled render after submitting real GPU callbacks. #[test] #[ignore] From 7a840ea672bc3fc713911092f42c5c24c224f3e5 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Tue, 17 Nov 2026 09:17:19 +0000 Subject: [PATCH 4/4] Auto: Preserve PBR aliases, mixed drops, and displayed choices Recognize split emissive-color names, route unconsumed dropped files through their existing import path, and pin the dialog's scanned map choices when importing. Add focused regressions for all three behaviors. --- crates/formats/src/pbr_map/mod.rs | 7 +++ crates/ui-egui/src/lib.rs | 12 ++-- crates/ui-egui/src/pbr_import.rs | 45 +++++++++----- crates/ui-egui/tests/ui_snapshots.rs | 61 +++++++++++++++---- .../tests/ui_snapshots/dropped_path.rs | 19 ++++++ 5 files changed, 114 insertions(+), 30 deletions(-) create mode 100644 crates/ui-egui/tests/ui_snapshots/dropped_path.rs diff --git a/crates/formats/src/pbr_map/mod.rs b/crates/formats/src/pbr_map/mod.rs index f189c733..255fbd3e 100644 --- a/crates/formats/src/pbr_map/mod.rs +++ b/crates/formats/src/pbr_map/mod.rs @@ -193,6 +193,7 @@ pub fn classify(name: &str) -> std::result::Result { let next = name_tokens.get(i + 1).map(String::as_str); let kind = match (t.as_str(), next) { ("base", Some("color" | "colour" | "map")) | ("main", Some("tex")) => Some(MapKind::BaseColor), + ("emissive", Some("color" | "colour")) => Some(MapKind::Emissive), ("ambient", Some("occlusion")) | ("mixed", Some("ao")) => Some(MapKind::AmbientOcclusion), ("bump", Some("map")) => Some(MapKind::Normal), _ => None, @@ -272,6 +273,12 @@ mod tests { classify(name).ok().map(|d| d.kind) } + #[test] + fn emissive_color_alias_survives_camel_case() { + assert_eq!(kind("brick_emissivecolor.png"), Some(Emissive)); + assert_eq!(kind("brick_EmissiveColor.png"), Some(Emissive)); + } + #[test] fn real_world_naming_styles() { let table: &[(&str, MapKind)] = &[ diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 5ee5f6b0..74d92580 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -701,14 +701,18 @@ impl eframe::App for TextureCraftApp { // A folder, a zip or several PBR maps dropped together open the PBR import dialog #[cfg(not(target_arch = "wasm32"))] - let taken = !dropped.is_empty() && { + let consumed = if !dropped.is_empty() { let paths: Vec = dropped.iter().map(|f| f.path().to_path_buf()).collect(); pbr_import::dropped(self, &paths) + } else { + Vec::new() }; #[cfg(target_arch = "wasm32")] - let taken = false; - for f in dropped.iter().filter(|_| !taken) { - web_files::dropped(self, ctx, f); + let consumed: Vec = Vec::new(); + for (index, file) in dropped.iter().enumerate() { + if consumed.binary_search(&index).is_err() { + web_files::dropped(self, ctx, file); + } } self.poll_bake(ctx); self.poll_resize(ctx); diff --git a/crates/ui-egui/src/pbr_import.rs b/crates/ui-egui/src/pbr_import.rs index 84fa2550..1b6300c1 100644 --- a/crates/ui-egui/src/pbr_import.rs +++ b/crates/ui-egui/src/pbr_import.rs @@ -60,28 +60,34 @@ pub fn pick(app: &mut TextureCraftApp) { } /// Dropped paths: a folder, a zip, or two or more image files that name PBR maps open the -/// dialog. Returns true when the drop was taken. +/// dialog. Returns the indices consumed by the dialog; other files keep their normal drop path. #[cfg(not(target_arch = "wasm32"))] -pub fn dropped(app: &mut TextureCraftApp, paths: &[std::path::PathBuf]) -> bool { +pub fn dropped(app: &mut TextureCraftApp, paths: &[std::path::PathBuf]) -> Vec { use texturecraft_engine::formats::pbr_maps; - if let Some(dir) = paths.iter().find(|p| p.is_dir()) { + if let Some((index, dir)) = paths.iter().enumerate().find(|(_, p)| p.is_dir()) { begin(app, json!({ "path": dir.display().to_string() })); - return true; + return vec![index]; } - if let Some(zip) = paths.iter().find(|p| p.extension().is_some_and(|e| e.eq_ignore_ascii_case("zip"))) { + if let Some((index, zip)) = paths.iter().enumerate().find(|(_, p)| p.extension().is_some_and(|e| e.eq_ignore_ascii_case("zip"))) { begin(app, json!({ "path": zip.display().to_string() })); - return true; + return vec![index]; } - let names: Vec = paths.iter().filter_map(|p| p.file_name().map(|n| n.to_string_lossy().into_owned())).collect(); - let recognized = pbr_maps::detect(names.iter().map(|n| (n.as_str(), 0))).candidates.values().map(Vec::len).sum::(); - if recognized >= 2 { - begin(app, json!({ "files": paths.iter().map(|p| p.display().to_string()).collect::>() })); - return true; + let recognized: Vec = paths + .iter() + .enumerate() + .filter_map(|(index, path)| { + let name = path.file_name()?.to_string_lossy(); + pbr_maps::classify(&name).ok().filter(|detected| detected.kind != pbr_maps::MapKind::Unused).map(|_| index) + }) + .collect(); + if recognized.len() >= 2 { + begin(app, json!({ "files": recognized.iter().filter_map(|&index| paths.get(index)).map(|p| p.display().to_string()).collect::>() })); + return recognized; } - false + Vec::new() } fn label_of(id: &str) -> &'static str { @@ -177,8 +183,19 @@ fn run(app: &mut TextureCraftApp, dialog_state: &PbrDialog) -> bool { if dialog_state.triplanar { params["projection"] = json!("triplanar"); } - if !dialog_state.choose.is_empty() { - params["choose"] = json!(dialog_state.choose); + + // Pin the displayed candidates so a later folder scan cannot silently replace them + let mut choose: BTreeMap = dialog_state + .scan + .get("maps") + .and_then(Value::as_object) + .into_iter() + .flat_map(|maps| maps.iter()) + .filter_map(|(id, file)| file.as_str().map(|file| (id.clone(), file.to_string()))) + .collect(); + choose.extend(dialog_state.choose.clone()); + if !choose.is_empty() { + params["choose"] = json!(choose); } let Some(reply) = app.run("file.importPbrFolder", params) else { return false }; diff --git a/crates/ui-egui/tests/ui_snapshots.rs b/crates/ui-egui/tests/ui_snapshots.rs index f9ada12f..f41eeff5 100644 --- a/crates/ui-egui/tests/ui_snapshots.rs +++ b/crates/ui-egui/tests/ui_snapshots.rs @@ -14,6 +14,9 @@ use texturecraft_ui_egui::state::Mode; use texturecraft_ui_egui::theme::Brightness; use texturecraft_ui_egui::titlebar::TitleBar; +#[path = "ui_snapshots/dropped_path.rs"] +mod dropped_path; + /// Click a map's row in Mesh Map Bakers, which shows its settings fn select_map(harness: &mut Harness<'_, TextureCraftApp>, name: &str) { harness.get_by_role_and_label(egui::Role::Button, name).click(); @@ -69,16 +72,11 @@ fn pbr_import_dialog_keeps_choices_after_error() { #[test] #[ignore] -fn pbr_drop_candidates_and_project_replacement() { - let Some(output_dir) = std::env::var_os("TC_UI_SNAPSHOTS").map(PathBuf::from) else { return }; - - std::fs::create_dir_all(&output_dir).unwrap(); - let source_dir = output_dir.join("pbr-drop-candidates"); +fn pbr_import_uses_displayed_default_after_folder_changes() { + let source_dir = std::env::temp_dir().join(format!("tc_pbr_pin_{}", std::process::id())); std::fs::create_dir_all(&source_dir).unwrap(); - let files: Vec<_> = ["m_albedo_1k.png", "m_albedo_4k.png", "notes.txt"].iter().map(|name| source_dir.join(name)).collect(); - for file in &files { - std::fs::write(file, b"fixture").unwrap(); - } + let bytes = texturecraft_engine::formats::gray16_png(2, 2, [0.5; 4].into_iter()).unwrap(); + std::fs::write(source_dir.join("tile_albedo_4k.png"), &bytes).unwrap(); let session = texturecraft_engine::sample_session(128, 64).unwrap(); let mut harness = Harness::builder() @@ -86,9 +84,48 @@ fn pbr_drop_candidates_and_project_replacement() { .with_pixels_per_point(1.0) .wgpu() .build_eframe(|context| TextureCraftApp::new(context, session)); - assert!(texturecraft_ui_egui::pbr_import::dropped(harness.state_mut(), &files)); - assert!(harness.state().pbr_import.is_some()); + texturecraft_ui_egui::pbr_import::begin(harness.state_mut(), serde_json::json!({"path": source_dir.display().to_string()})); + std::fs::write(source_dir.join("tile_albedo_8k.png"), &bytes).unwrap(); + harness.run_steps(3); + harness.get_by_role_and_label(egui::Role::Button, "Import").click(); + harness.run_steps(2); + + let resources = &harness.state().session.doc.resources; + assert!(resources.keys().any(|name| name.contains("tile_albedo_4k")), "{resources:?}"); + assert!(!resources.keys().any(|name| name.contains("tile_albedo_8k")), "{resources:?}"); + std::fs::remove_dir_all(source_dir).unwrap(); +} + +#[test] +#[ignore] +fn pbr_drop_candidates_and_project_replacement() { + let Some(output_dir) = std::env::var_os("TC_UI_SNAPSHOTS").map(PathBuf::from) else { return }; + + std::fs::create_dir_all(&output_dir).unwrap(); + let source_dir = output_dir.join("pbr-drop-candidates"); + std::fs::create_dir_all(&source_dir).unwrap(); + let files: Vec<_> = ["m_albedo_1k.png", "m_albedo_4k.png", "reference.png"].iter().map(|name| source_dir.join(name)).collect(); + for file in &files { + std::fs::write(file, b"fixture").unwrap(); + } + let reference = texturecraft_engine::formats::gray16_png(2, 2, [0.5; 4].into_iter()).unwrap(); + std::fs::write(&files[2], reference).unwrap(); + + let session = texturecraft_engine::sample_session(128, 64).unwrap(); + let mut harness = Harness::builder() + .with_size(egui::vec2(1280.0, 800.0)) + .with_pixels_per_point(1.0) + .wgpu() + .build_eframe(|context| TextureCraftApp::new(context, session)); + harness.input_mut().dropped_files = files.iter().cloned().map(dropped_path::handle).collect(); + harness.run_steps(1); + assert!(harness.state().pbr_import.is_some()); + assert!(harness.state().session.doc.resources.keys().any(|name| name.contains("reference.png"))); + assert!(!harness.state().session.doc.resources.keys().any(|name| name.contains("m_albedo"))); + harness.run_steps(3); + assert!(harness.query_by_label("2 files ignored").is_none()); + assert!(harness.query_by_label("1 files ignored").is_some()); assert!(harness.get_all_by_role(egui::Role::ComboBox).any(|node| node.value().as_deref() == Some("m_albedo_4k.png"))); save(&mut harness, &output_dir, "pbr-drop-candidates"); @@ -96,7 +133,7 @@ fn pbr_drop_candidates_and_project_replacement() { assert!(harness.state().pbr_import.is_some()); assert!(harness.state_mut().run("project.new", serde_json::json!({"resolution": 128})).is_some()); assert!(harness.state().pbr_import.is_none()); - assert!(!texturecraft_ui_egui::pbr_import::dropped(harness.state_mut(), &files[1..])); + assert!(texturecraft_ui_egui::pbr_import::dropped(harness.state_mut(), &files[1..]).is_empty()); std::fs::remove_dir_all(source_dir).unwrap(); } diff --git a/crates/ui-egui/tests/ui_snapshots/dropped_path.rs b/crates/ui-egui/tests/ui_snapshots/dropped_path.rs new file mode 100644 index 00000000..0dc92881 --- /dev/null +++ b/crates/ui-egui/tests/ui_snapshots/dropped_path.rs @@ -0,0 +1,19 @@ +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +#[derive(Debug)] +pub struct DroppedPath(PathBuf); + +impl egui::DroppedFile for DroppedPath { + fn path(&self) -> &Path { + &self.0 + } + + fn bytes(&self) -> Result, String> { + std::fs::read(&self.0).map_err(|error| error.to_string()) + } +} + +pub fn handle(path: PathBuf) -> egui::DroppedFileHandle { + Arc::new(DroppedPath(path)) +}