diff --git a/Cargo.lock b/Cargo.lock index a939100a..b3d82592 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4459,6 +4459,7 @@ dependencies = [ "epaint_default_fonts", "image", "rayon", + "rustix 1.1.5", "serde", "serde_json", "texturecraft-bake", diff --git a/ROADMAP.md b/ROADMAP.md index 63419f45..759dfde7 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -108,7 +108,7 @@ menu entries. Feature rows overlap and are not an additional additive score. | Area | % ready | Hours | What works / main gaps | |---|---:|---|---| -| Projects and files | ~80% | TBD | Current `.tcraft` new/save/reopen passed with layers, effects and baked maps retained. Schema/container rework remains possible; versioning/migration policy and renewed persistence/recovery tests are needed if adopted. Large cases, Alembic HDF5 and animation remain open | +| Projects and files | ~80% | TBD | Current `.tcraft` new/save/reopen passed with layers, effects and baked maps retained. Schema/container rework remains possible; versioning/migration policy and renewed persistence/recovery tests are needed if adopted. Large cases, Alembic HDF5 and animation remain open; PBR folder/ZIP/selected-file import landed after this assessment: `file.importPbrFolder` creates one fill layer from named maps, including packed ORM and OpenGL/DirectX normals, with a UI dialog and optional native library copy; it is not credited in this score | | Texture sets and UDIM | ~80% | TBD | Two tiles exported at independent 128/256 px sizes with expected packed-channel values; 21 channel kinds in source. Many-set/many-tile production coverage remains untested | | Layers, masks and effects | ~75% | TBD | UI fill/black-mask creation, generator editing/duplication and native persistence passed at the assessed revision. Mask-thumbnail context actions remain missing; effect renaming and mask-stack anchors landed afterward, with live UI validation still open | | Painting and precise 2D work | ~70% | TBD | Mouse painting in both views, keyboard undo/redo, view shortcuts, brush gesture and UV-island rectangle fill passed. Nine tool kinds exist; brush feel, pressure/tilt, other tool task suites and complete trim sheets need validation | diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 2500d9ed..3d66b9df 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -22,6 +22,9 @@ serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } +[target.'cfg(unix)'.dependencies] +rustix = { workspace = true } + [dev-dependencies] image = { workspace = true } zip = { workspace = true } diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs index adeb0959..06b74adb 100644 --- a/crates/engine/src/commands.rs +++ b/crates/engine/src/commands.rs @@ -74,6 +74,7 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(crate::bake::specs()); v.extend(crate::bake_preview::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 8a5b3df0..34b6bf3a 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -39,6 +39,7 @@ pub mod orb_preview; pub mod painting; mod path_draft; pub mod path_tool; +pub mod pbr_cmds; mod polygon_fill; mod polygon_fill_drag; // Mesh reimport reads files on native targets only diff --git a/crates/engine/src/pbr_cmds.rs b/crates/engine/src/pbr_cmds.rs new file mode 100644 index 00000000..a9635f49 --- /dev/null +++ b/crates/engine/src/pbr_cmds.rs @@ -0,0 +1,477 @@ +//! `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, 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; +/// Upper bound on the import's estimated decoded and temporary image memory. +const MAX_IMPORT_MEMORY_BYTES: u64 = 4 << 30; +/// Leave most available memory to the document, renderer, and operating system. +const IMPORT_MEMORY_SHARE: u64 = 4; +const RGBA_FLOAT_BYTES_PER_TEXEL: u64 = 16; +/// Headroom for the decoder and one generated-map conversion in flight. +const IMPORT_TEMP_BYTES_PER_TEXEL: u64 = 32; + +fn import_memory_budget() -> u64 { + #[cfg(not(target_arch = "wasm32"))] + let available_bytes = texturecraft_formats::available_save_resources().1; + #[cfg(target_arch = "wasm32")] + let available_bytes = MAX_IMPORT_MEMORY_BYTES; + + (available_bytes / IMPORT_MEMORY_SHARE).min(MAX_IMPORT_MEMORY_BYTES) +} + +fn allowed_source_texels(budget_bytes: u64, retained_bytes: u64, output_count: usize) -> u64 { + // A source and each output retain RGBA floats; leave room for decoding and conversion + let retained_per_texel = RGBA_FLOAT_BYTES_PER_TEXEL * (1 + output_count as u64); + let temporary_per_texel = IMPORT_TEMP_BYTES_PER_TEXEL; + budget_bytes.saturating_sub(retained_bytes) / (retained_per_texel + temporary_per_texel) +} + +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?, normalFormat?, choose?, projection?, scale?} → {name, maps, candidates, normalFormat, channelsAvailable, ignored}: native paths only; validates import options without decoding or changing the project", + 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>; + +mod pbr_options; +mod take; + +pub use pbr_options::PbrOptions; +use take::Take; + +/// 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()) + && 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::>(), + }) +} + +/// A file's bytes and its decoded image. +type Loaded = (Arc>, texturecraft_formats::DecodedImage); + +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, options: &PbrOptions) -> Result { + self.import_pbr_with_budget(files, load, options, import_memory_budget()) + } + + fn import_pbr_with_budget(&mut self, files: &FileList, load: Loader, options: &PbrOptions, memory_budget: u64) -> Result { + let set = detect_with(files, &options.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 texture_set_index = self.ts(); + let available = self.doc.project.texture_set(texture_set_index).map(|t| t.channel_list()).unwrap_or_default(); + let normal_format = normal_format_for(&set, options.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: options.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; + let mut retained_bytes = 0u64; + + // Decoded source files by name (a packed image feeds three channels) + let mut sources: BTreeMap> = BTreeMap::new(); + + // Resource per (file, how) so a file used twice is stored once + let mut made: BTreeMap<(String, Take), String> = BTreeMap::new(); + + let mut uses: BTreeMap = BTreeMap::new(); + for (channel, kind, _) in &plan { + if !available.contains(channel) { + continue; + } + + 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 resource = if let Some(name) = made.get(&(file.clone(), take)) { + name.clone() + } else { + if !sources.contains_key(&file) { + let output_count = uses.get(&file).copied().unwrap_or_default(); + let allowed_texels = allowed_source_texels(memory_budget, retained_bytes, output_count).min(MAX_IMAGE_TEXELS); + let loaded = if allowed_texels == 0 { + Err("not enough memory is available to import more textures".to_string()) + } else { + 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, allowed_texels).map_err(|e| e.to_string())?; + let texels = u64::from(image.width) * u64::from(image.height); + total_texels = total_texels.saturating_add(texels); + if total_texels > MAX_TOTAL_TEXELS { + return Err("the textures together are too large to import".into()); + } + + retained_bytes = retained_bytes.saturating_add(texels.saturating_mul(RGBA_FLOAT_BYTES_PER_TEXEL * (1 + output_count as u64))); + 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 (resource_name, resource_bytes, resource_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, generated_image) = + texturecraft_formats::gray16_resource(image.width, image.height, values).map_err(|error| EngineError::Other(error.to_string()))?; + + if last_use { + sources.remove(&file); + } + + (format!("{stem}_{suffix}.png"), Arc::new(png), generated_image) + } + }; + let base = texturecraft_formats::sanitize_resource_name(&resource_name); + let (mut unique, mut suffix_index) = (base.clone(), 2); + while self.doc.resources.contains_key(&unique) || added.iter().any(|(a, _)| *a == unique) { + unique = format!("{suffix_index}_{base}"); + suffix_index += 1; + } + + added.push((unique.clone(), resource_bytes)); + decoded.push((unique.clone(), crate::resource_cmds::decoded(resource_image))); + made.insert((file.clone(), take), unique.clone()); + unique + }; + let source = if channel == Channel::Normal { Source::Image { name: resource, normal_format } } 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 = options.name.clone().filter(|n| !n.trim().is_empty()).unwrap_or_else(|| "PBR material".to_string()); + let layer_name = self.doc.project.next_layer_name(texture_set_index, &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.clone(), LayerKind::Fill(fill), &channels)) + })?; + self.images.extend(decoded); + + let mut reply = report(&set, options.normal_format); + if let Some(o) = reply.as_object_mut() { + o.insert("layer".into(), json!(id.0)); + o.insert("name".into(), json!(layer_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)>, options: &PbrOptions) -> Result { + let list: FileList = files.iter().map(|(n, b)| (n.clone(), b.len() as u64)).collect(); + let by_name = std::cell::RefCell::new(files.into_iter().collect::>()); + + // The importer caches each source, so ownership transfers on its only load + let load = |name: &str| by_name.borrow_mut().remove(name).ok_or_else(|| "no such file".to_string()); + self.import_pbr(&list, &load, options) + } +} + +/// 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").filter(|name| !name.trim().is_empty()).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/"`). +#[cfg(not(target_arch = "wasm32"))] +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; +#[cfg(not(target_arch = "wasm32"))] +fn import(s: &mut Session, p: &Value) -> Result { + let cmd = "file.importPbrFolder"; + let mut options = options(cmd, p)?; + let found = native::find(cmd, p)?; + if options.name.is_none() { + options.name = found.name.clone(); + } + s.import_pbr(&found.files, &|name| found.load(name), &options) +} + +#[cfg(not(target_arch = "wasm32"))] +fn scan(s: &mut Session, p: &Value) -> Result { + let cmd = "file.scanPbrFolder"; + let options = options(cmd, p)?; + let found = native::find(cmd, p)?; + let set = detect_with(&found.files, &options.choose)?; + let mut reply = report(&set, options.normal_format); + if let Some(o) = reply.as_object_mut() { + o.insert("name".into(), json!(options.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())) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn import_rejects_an_image_before_decoding_when_memory_is_insufficient() { + let bytes = texturecraft_formats::gray16_png(2, 2, [0.5; 4].into_iter()).unwrap(); + let files = vec![("tile_albedo.png".to_string(), bytes.len() as u64)]; + let mut session = Session::new_project("test", crate::geom::primitives::builtin("plane").unwrap(), "builtin:plane", None, 128).unwrap(); + let load = |_: &str| Ok(bytes.clone()); + + let result = session.import_pbr_with_budget(&files, &load, &PbrOptions::default(), 64); + + assert!(result.unwrap_err().to_string().contains("more than 1 texels")); + assert!(session.doc.resources.is_empty()); + assert_eq!(allowed_source_texels(64, 0, 1), 1); + assert_eq!(allowed_source_texels(256, 0, 1), 4); + assert_eq!(allowed_source_texels(256, 64, 3), 2); + + let never_load = |_: &str| -> std::result::Result, String> { panic!("no source should load after the budget is spent") }; + let result = session.import_pbr_with_budget(&files, &never_load, &PbrOptions::default(), 0); + + assert!(result.unwrap_err().to_string().contains("not enough memory")); + } +} diff --git a/crates/engine/src/pbr_cmds/native.rs b/crates/engine/src/pbr_cmds/native.rs new file mode 100644 index 00000000..258850d4 --- /dev/null +++ b/crates/engine/src/pbr_cmds/native.rs @@ -0,0 +1,106 @@ +use std::cell::RefCell; +use std::path::{Path, PathBuf}; + +mod found; +mod origin; + +pub(in crate::pbr_cmds) use found::Found; +use origin::Origin; + +use super::*; + +/// Regular files below `dir`, with bounded directory traversal and no silent truncation. +fn walk(root: &Path, dir: &Path, depth: usize, visited: &mut usize, files: &mut FileList) -> std::result::Result<(), String> { + const MAX_DIRECTORY_DEPTH: usize = 4; + if depth > MAX_DIRECTORY_DEPTH { + return Err(format!("directory depth exceeds the limit of {MAX_DIRECTORY_DEPTH}")); + } + + let entries = std::fs::read_dir(dir).map_err(|error| error.to_string())?; + for entry in entries { + *visited += 1; + if *visited > MAX_FILES { + return Err(format!("directory entry count exceeds the limit of {MAX_FILES}")); + } + + let path = entry.map_err(|error| error.to_string())?.path(); + + // Links and special files must not become import candidates + let metadata = std::fs::symlink_metadata(&path).map_err(|error| error.to_string())?; + if metadata.is_dir() { + walk(root, &path, depth + 1, visited, files)?; + } else if metadata.is_file() + && let Ok(relative) = path.strip_prefix(root) + { + let name = relative.components().map(|component| component.as_os_str().to_string_lossy().into_owned()).collect::>().join("/"); + files.push((name, metadata.len())); + } + } + + Ok(()) +} + +pub(super) fn find(cmd: &str, p: &Value) -> Result { + if let Some(selected) = p.get("files").and_then(Value::as_array) { + let mut paths = Vec::new(); + let mut list = FileList::new(); + if selected.len() > MAX_FILES { + return Err(bad(cmd, format!("selected file count exceeds the limit of {MAX_FILES}"))); + } + + for v in selected { + let Some(s) = v.as_str() else { return Err(bad(cmd, "`files` must be a list of paths")) }; + let path = PathBuf::from(s); + let metadata = std::fs::symlink_metadata(&path).map_err(|e| bad(cmd, format!("cannot read {s}: {e}")))?; + if !metadata.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, metadata.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 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(); + let mut visited_entries = 0; + 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() { 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(); + return Ok(Found { source: Origin::Folder { root }, files, name }); + } + + if metadata.is_file() && path.to_ascii_lowercase().ends_with(".zip") { + if metadata.len() > MAX_FILE_BYTES * 2 { + return Err(bad(cmd, format!("{path} is too large"))); + } + + let bytes = found::read_regular(Path::new(path), MAX_FILE_BYTES * 2).map_err(|error| bad(cmd, format!("cannot read {path}: {error}")))?; + 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 { dir_name(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"))) +} diff --git a/crates/engine/src/pbr_cmds/native/found.rs b/crates/engine/src/pbr_cmds/native/found.rs new file mode 100644 index 00000000..454203a6 --- /dev/null +++ b/crates/engine/src/pbr_cmds/native/found.rs @@ -0,0 +1,100 @@ +use std::path::Path; + +use super::Origin; +use crate::pbr_cmds::{FileList, MAX_FILE_BYTES}; + +pub(in crate::pbr_cmds) struct Found { + pub(super) source: Origin, + pub(in crate::pbr_cmds) files: FileList, + /// A name for the material (folder, archive or shared file-name prefix). + pub(in crate::pbr_cmds) name: Option, +} + +impl Found { + /// Reads one file of the list. Archive entries are read through a limit, since their + /// declared size can lie. + pub(in crate::pbr_cmds) fn load(&self, name: &str) -> std::result::Result, String> { + match &self.source { + Origin::Folder { root } => read_regular(&root.join(name), MAX_FILE_BYTES), + 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, MAX_FILE_BYTES) + } + Origin::Zip(zip) => zip.borrow_mut().read(name, MAX_FILE_BYTES).map_err(|e| e.to_string()), + } + } +} + +pub(super) fn read_regular(path: &Path, max_bytes: u64) -> std::result::Result, String> { + use std::io::Read as _; + + // Reject links and special files before opening; check the opened handle again below + let metadata = std::fs::symlink_metadata(path).map_err(|error| error.to_string())?; + if !metadata.is_file() { + return Err("it is not a regular file".into()); + } + + #[cfg(unix)] + let file = { + use rustix::fs::{Mode, OFlags, open}; + + // NONBLOCK avoids waiting if a regular file is replaced by a pipe during open + let descriptor = + open(path, OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC, Mode::empty()).map_err(|error| error.to_string())?; + std::fs::File::from(descriptor) + }; + #[cfg(not(unix))] + let file = { + let mut options = std::fs::OpenOptions::new(); + options.read(true); + #[cfg(windows)] + { + use std::os::windows::fs::OpenOptionsExt as _; + + // FILE_FLAG_OPEN_REPARSE_POINT opens a replaced link itself instead of its target + const OPEN_REPARSE_POINT: u32 = 0x0020_0000; + options.custom_flags(OPEN_REPARSE_POINT); + } + options.open(path).map_err(|error| error.to_string())? + }; + let metadata = file.metadata().map_err(|error| error.to_string())?; + if !metadata.is_file() || metadata.file_type().is_symlink() { + return Err("it is not a regular file".into()); + } + + if metadata.len() > max_bytes { + return Err(format!("the file exceeds the limit of {max_bytes} bytes")); + } + + // The file can grow after metadata was read, so bound actual bytes too + let mut bytes = Vec::new(); + file.take(max_bytes.saturating_add(1)).read_to_end(&mut bytes).map_err(|error| error.to_string())?; + if bytes.len() as u64 > max_bytes { + return Err(format!("the file exceeds the limit of {max_bytes} bytes")); + } + + Ok(bytes) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn regular_reads_enforce_the_limit_and_reject_links() { + let path = std::env::temp_dir().join(format!("tc_pbr_read_{}", std::process::id())); + std::fs::write(&path, [1u8; 16]).unwrap(); + + assert_eq!(read_regular(&path, 16).unwrap(), [1u8; 16]); + assert!(read_regular(&path, 15).unwrap_err().contains("limit")); + #[cfg(unix)] + { + let link = path.with_extension("link"); + std::os::unix::fs::symlink(&path, &link).unwrap(); + + assert!(read_regular(&link, 16).unwrap_err().contains("regular file")); + std::fs::remove_file(link).unwrap(); + } + std::fs::remove_file(path).unwrap(); + } +} diff --git a/crates/engine/src/pbr_cmds/native/origin.rs b/crates/engine/src/pbr_cmds/native/origin.rs new file mode 100644 index 00000000..5c3045a0 --- /dev/null +++ b/crates/engine/src/pbr_cmds/native/origin.rs @@ -0,0 +1,9 @@ +use std::cell::RefCell; +use std::path::PathBuf; + +/// Where the files of an import come from. +pub(super) enum Origin { + Folder { root: PathBuf }, + Files(Vec), + Zip(RefCell), +} diff --git a/crates/engine/src/pbr_cmds/pbr_options.rs b/crates/engine/src/pbr_cmds/pbr_options.rs new file mode 100644 index 00000000..88180c83 --- /dev/null +++ b/crates/engine/src/pbr_cmds/pbr_options.rs @@ -0,0 +1,14 @@ +use std::collections::BTreeMap; +use texturecraft_core::{NormalFormat, Projection}; +use texturecraft_formats::pbr_maps::MapKind; + +/// How to import. +#[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). + pub normal_format: Option, + pub projection: Projection, + /// Files chosen by the caller where several name one map. + pub choose: BTreeMap, +} diff --git a/crates/engine/src/pbr_cmds/take.rs b/crates/engine/src/pbr_cmds/take.rs new file mode 100644 index 00000000..34176521 --- /dev/null +++ b/crates/engine/src/pbr_cmds/take.rs @@ -0,0 +1,10 @@ +/// How a channel's image is made from the file chosen for a map. +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub(super) 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, +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 8973da23..7c61e03b 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -21,6 +21,7 @@ mod meshmap_import; mod painter_gaps; mod path_perf; mod path_tool; +mod pbr_import; mod perf; mod polygon_fill; mod project_recovery; diff --git a/crates/engine/tests/it/pbr_import.rs b/crates/engine/tests/it/pbr_import.rs new file mode 100644 index 00000000..787c0ed2 --- /dev/null +++ b/crates/engine/tests/it/pbr_import.rs @@ -0,0 +1,461 @@ +//! `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; + +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() +} + +/// A session whose texture set also has an ambient occlusion channel (the default set has none). +fn session_with_ao() -> Session { + let mut session = new_session(); + execute(&mut session, "textureSet.channel.add", &json!({"channel": "ambientOcclusion"})).unwrap(); + session +} + +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]) { + image::RgbImage::from_pixel(4, 4, image::Rgb(rgb)).save(path).unwrap(); +} + +fn fill_of(session: &Session, layer: u64) -> Fill { + match &session.doc.project.layer(LayerId(layer)).unwrap().kind { + LayerKind::Fill(f) => f.clone(), + other => panic!("not a fill: {other:?}"), + } +} + +fn import(session: &mut Session, directory: &Path, extra: Value) -> Result { + let mut p = json!({ "path": directory.display().to_string() }); + if let (Some(p), Some(e)) = (p.as_object_mut(), extra.as_object()) { + p.extend(e.clone()); + } + execute(session, "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 directory = 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(&directory.join(name), rgb); + } + std::fs::write(directory.join("readme.txt"), "hi").unwrap(); + let mut session = session_with_ao(); + let res = session.doc.resources.len(); + let reply = import(&mut session, &directory, json!({})).unwrap(); + let layer = reply["layer"].as_u64().unwrap(); + let fill = fill_of(&session, 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!(reply["normalFormat"], "directX"); + assert_eq!(reply["maps"]["baseColor"], "Brick_Albedo.png"); + let ignored = reply["ignored"].to_string(); + assert!(ignored.contains("Brick_Preview.png") && ignored.contains("preview image") && ignored.contains("readme.txt"), "{ignored}"); + assert!(!session.doc.resources.keys().any(|k| k.to_lowercase().contains("preview"))); + assert_eq!(session.doc.resources.len(), res + 6); + 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"], layer_name); + + // Every decoded image is ready for the viewport + for n in session.doc.resources.keys() { + assert!(session.images.contains_key(n), "{n}"); + } + execute(&mut session, "edit.undo", &Value::Null).unwrap(); + assert_eq!(session.doc.resources.len(), res); + assert!(session.doc.project.layer(LayerId(layer)).is_err()); +} + +#[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 directory = folder("flip"); + for f in files { + png(&directory.join(f), [128, 51, 255]); + } + let mut session = new_session(); + import(&mut session, &directory, extra).unwrap(); + session.update(); + let v = session.caches[0].composite.value(Channel::Normal, 64, 64); + 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 directory = folder("orm"); + + // Occlusion 0.2 (R), roughness 0.5 (G), metallic 0.9 (B) + png(&directory.join("floor_ORM.png"), [51, 128, 230]); + png(&directory.join("floor_BaseColor.png"), [10, 20, 30]); + let mut session = session_with_ao(); + let reply = import(&mut session, &directory, json!({})).unwrap(); + let fill = fill_of(&session, reply["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:?}"); + session.update(); + let at = |c| session.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(&session.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 = new_session(); + let reply = import(&mut plain, &directory, json!({})).unwrap(); + assert!(reply["warnings"].to_string().contains("ambientOcclusion was skipped"), "{reply}"); + assert!(reply["channels"].as_array().unwrap().contains(&json!("metallic"))); + + // Separate maps win over the packed image for the channels they cover + png(&directory.join("floor_Roughness.png"), [255, 255, 255]); + let mut session = session_with_ao(); + let reply = import(&mut session, &directory, json!({})).unwrap(); + session.update(); + 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"); +} + +#[test] +fn glossiness_becomes_roughness() { + let directory = folder("gloss"); + png(&directory.join("tile_diffuse.png"), [1, 1, 1]); + png(&directory.join("tile_gloss.png"), [204, 204, 204]); // 0.8 glossy = 0.2 rough + let mut session = new_session(); + let reply = import(&mut session, &directory, json!({})).unwrap(); + 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); +} + +#[test] +fn missing_maps_stay_at_their_defaults() { + let directory = folder("missing"); + png(&directory.join("only_albedo.png"), [200, 10, 10]); + let mut session = new_session(); + let reply = import(&mut session, &directory, json!({})).unwrap(); + assert_eq!(reply["channels"], json!(["baseColor"]), "{reply}"); + let fill = fill_of(&session, reply["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) = (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)); +} + +#[test] +fn several_candidates_use_the_best_and_can_be_chosen() { + let directory = folder("choose"); + png(&directory.join("m_albedo_1k.png"), [1, 1, 1]); + png(&directory.join("m_albedo_4k.png"), [2, 2, 2]); + png(&directory.join("m_nor_dx.png"), [128, 128, 255]); + png(&directory.join("m_nor_gl.png"), [128, 128, 255]); + let mut session = new_session(); + let scan = execute(&mut session, "file.scanPbrFolder", &json!({"path": directory.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!(session.doc.resources.is_empty(), "scanning changes nothing"); + let reply = import(&mut session, &directory, json!({"choose": {"baseColor": "m_albedo_1k.png", "normal": "m_nor_dx.png"}})).unwrap(); + assert_eq!(reply["maps"]["baseColor"], "m_albedo_1k.png"); + assert_eq!(reply["normalFormat"], "directX"); + 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()); +} + +#[test] +fn projection_name_and_selected_files() { + let directory = folder("files"); + png(&directory.join("moss_col.png"), [1, 90, 1]); + png(&directory.join("moss_rough.png"), [200, 200, 200]); + png(&directory.join("other_albedo.png"), [9, 9, 9]); + let mut session = new_session(); + let files: Vec = ["moss_col.png", "moss_rough.png"].iter().map(|f| directory.join(f).display().to_string()).collect(); + let reply = execute(&mut session, "file.importPbrFolder", &json!({"files": files, "projection": "triplanar", "scale": [2, 2]})).unwrap(); + assert_eq!(reply["name"], "moss 1"); + let fill = fill_of(&session, reply["layer"].as_u64().unwrap()); + assert!(matches!(fill.projection, Projection::Triplanar { scale, .. } if scale == 2.0), "{:?}", fill.projection); + assert_eq!(fill.sources.len(), 2); + 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")); +} + +#[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] +fn a_zip_and_a_pack_of_folders() { + let directory = folder("zip"); + let mut archive = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let options = 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"] { + archive.start_file(name, options).unwrap(); + archive.write_all(&one).unwrap(); + } + let bytes = archive.finish().unwrap().into_inner(); + let path = directory.join("Brick Pack.zip"); + std::fs::write(&path, &bytes).unwrap(); + let mut session = new_session(); + let reply = execute(&mut session, "file.importPbrFolder", &json!({"path": path.display().to_string()})).unwrap(); + assert_eq!(reply["channels"], json!(["baseColor", "roughness"]), "{reply}"); + assert_eq!(reply["name"], "brick 1"); + + // 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 session, &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 session, &pack.join("a"), json!({})).is_ok()); + std::fs::remove_dir_all(pack.join("b")).unwrap(); + assert!(import(&mut session, &pack, json!({})).is_ok()); + + // 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()); +} + +#[test] +fn hostile_images_are_skipped_without_a_panic() { + let directory = folder("hostile"); + png(&directory.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(directory.join("w_roughness.png"), b"definitely not a png").unwrap(); + let good = std::fs::read(directory.join("w_albedo.png")).unwrap(); + std::fs::write(directory.join("w_metallic.png"), &good[..good.len() / 2]).unwrap(); + image::GrayImage::new(20_000, 1).save(directory.join("w_height.png")).unwrap(); + + // A header alone must reject a source beyond the per-image budget + let mut header = [0u8; 18]; + header[2] = 2; + header[12..14].copy_from_slice(&16_384u16.to_le_bytes()); + header[14..16].copy_from_slice(&4_097u16.to_le_bytes()); + header[16] = 24; + std::fs::write(directory.join("w_emissive.tga"), header).unwrap(); + + // 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_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(); + assert_eq!(reply["channels"], json!(["baseColor"]), "{reply}"); + let warnings = reply["warnings"].to_string(); + assert!(warnings.contains("more than") && warnings.contains("texels"), "header limit must run: {warnings}"); + for needle in ["roughness", "metallic", "height", "emissive"] { + assert!(warnings.contains(needle), "{needle}: {warnings}"); + } + #[cfg(unix)] + { + assert!(!reply["maps"].as_object().unwrap().contains_key("ambientOcclusion"), "a link is not followed: {reply}"); + assert!(outside_target.join("w_ao.png").exists()); + } + + // 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 = (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); +} + +#[test] +fn bad_params_are_errors() { + let mut session = new_session(); + let run = |session: &mut Session, p: Value| execute(session, "file.importPbrFolder", &p); + assert!(run(&mut session, json!({})).is_err()); + assert!(run(&mut session, json!({"path": ""})).is_err()); + assert!(run(&mut session, json!({"path": "/definitely/not/here"})).is_err()); + assert!(run(&mut session, json!({"files": [1, 2]})).is_err()); + assert!(run(&mut session, json!({"files": ["/definitely/not/here.png"]})).is_err()); + let directory = folder("params"); + png(&directory.join("a_albedo.png"), [1, 1, 1]); + 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"); +} + +#[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 session = new_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 reply = session.import_pbr_bytes(files, &texturecraft_engine::pbr_cmds::PbrOptions::default()).unwrap(); + assert_eq!(reply["normalFormat"], "directX"); + assert_eq!(reply["channels"], json!(["baseColor", "normal"])); + + let first_image = std::sync::Arc::downgrade(session.images.values().next().unwrap()); + let first_bytes = std::sync::Arc::downgrade(session.doc.resources.values().next().unwrap()); + let first_resource_count = session.doc.resources.len(); + let first_image_count = session.images.len(); + session + .import_pbr_bytes(vec![("other_roughness.png".to_string(), png_bytes([64, 64, 64]))], &texturecraft_engine::pbr_cmds::PbrOptions::default()) + .unwrap(); + assert_eq!(session.doc.resources.len(), first_resource_count + 1); + assert_eq!(session.images.len(), first_image_count + 1); + + execute(&mut session, "edit.undo", &Value::Null).unwrap(); + assert_eq!(session.doc.resources.len(), first_resource_count); + assert_eq!(session.images.len(), first_image_count); + + execute(&mut session, "edit.undo", &Value::Null).unwrap(); + assert!(session.doc.resources.is_empty()); + assert!(session.images.is_empty()); + assert!(first_image.upgrade().is_none(), "decoded pixels must be released after undo"); + assert!(first_bytes.upgrade().is_some(), "redo intentionally retains compressed bytes"); + + drop(session); + + assert!(first_bytes.upgrade().is_none(), "dropping the session releases history too"); +} + +#[test] +fn repeated_import_reports_the_created_layer_name() { + let directory = folder("repeated_name"); + png(&directory.join("moss_albedo.png"), [1, 90, 1]); + let mut session = new_session(); + import(&mut session, &directory, json!({"name": "Moss"})).unwrap(); + + let reply = import(&mut session, &directory, json!({"name": "Moss"})).unwrap(); + + let layer = session.doc.project.layer(LayerId(reply["layer"].as_u64().unwrap())).unwrap(); + assert_ne!(layer.name, "Moss"); + assert_eq!(reply["name"], layer.name); +} + +#[test] +fn scans_reject_excessive_entries_instead_of_silently_truncating() { + let directory = folder("scan_limit"); + let path = directory.join("moss_albedo.png"); + png(&path, [1, 90, 1]); + let mut session = new_session(); + let files = vec![path; texturecraft_engine::pbr_cmds::MAX_FILES + 1]; + + let selected = execute(&mut session, "file.scanPbrFolder", &json!({"files": files})); + + assert!(selected.unwrap_err().to_string().contains("limit")); +} + +#[test] +fn scans_count_empty_directories_toward_the_limit() { + let directory = folder("directory_limit"); + png(&directory.join("moss_albedo.png"), [1, 90, 1]); + let mut session = new_session(); + for index in 0..texturecraft_engine::pbr_cmds::MAX_FILES { + std::fs::create_dir(directory.join(index.to_string())).unwrap(); + } + + let scanned = execute(&mut session, "file.scanPbrFolder", &json!({"path": directory.display().to_string()})); + + assert!(scanned.unwrap_err().to_string().contains("limit")); +} 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/image_res.rs b/crates/formats/src/image_res.rs index 324c1d26..d766bb68 100644 --- a/crates/formats/src/image_res.rs +++ b/crates/formats/src/image_res.rs @@ -53,13 +53,62 @@ pub fn decode_image_within(bytes: &[u8], max_texels: u64) -> Result Result> { let rgba: Vec = coverage.iter().flat_map(|a| [rgb[0], rgb[1], rgb[2], *a]).collect(); - let img = image::RgbaImage::from_raw(width, height, rgba) + let image = image::RgbaImage::from_raw(width, height, rgba) .ok_or_else(|| FormatError::Invalid(format!("{} coverage values for {width}x{height}", coverage.len())))?; 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()))?; + image.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).map_err(|e| FormatError::Invalid(e.to_string()))?; 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 image = gray16_image(width, height, values)?; + + encode_gray16(&image) +} + +/// A single-channel PNG and its decoded pixels, quantized identically to a saved/reloaded image. +pub fn gray16_resource(width: u32, height: u32, values: impl Iterator) -> Result<(Vec, DecodedImage)> { + let image = gray16_image(width, height, values)?; + let png = encode_gray16(&image)?; + let rgba = image + .pixels() + .map(|pixel| { + // Match image's integer-to-float expansion so reload is bit-identical + const GRAY16_TO_FLOAT: f32 = 1.0 / u16::MAX as f32; + let value = f32::from(pixel.0[0]) * GRAY16_TO_FLOAT; + [value, value, value, 1.0] + }) + .collect(); + + Ok((png, DecodedImage { width, height, rgba, srgb: true })) +} + +fn encode_gray16(image: &image::ImageBuffer, Vec>) -> Result> { + let mut png = Vec::new(); + image.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).map_err(|error| FormatError::Invalid(error.to_string()))?; + + Ok(png) +} + +fn gray16_image(width: u32, height: u32, values: impl Iterator) -> Result, Vec>> { + let texels = u64::from(width) * u64::from(height); + if width == 0 || height == 0 || width > MAX_IMAGE_SIZE || height > MAX_IMAGE_SIZE || texels > MAX_RESOURCE_TEXELS { + return Err(FormatError::TooLarge(format!("image is {width}x{height} (limit {MAX_RESOURCE_TEXELS} texels)"))); + } + + let expected = usize::try_from(texels).map_err(|_| FormatError::TooLarge(format!("image is {width}x{height}")))?; + let data: Vec = values.take(expected + 1).map(|v| if v.is_finite() { (v.clamp(0.0, 1.0) * f32::from(u16::MAX)).round() as u16 } else { 0 }).collect(); + if data.len() != expected { + return Err(FormatError::Invalid(format!("pixel count does not match {width}x{height}"))); + } + + let image = image::ImageBuffer::, Vec>::from_raw(width, height, data) + .ok_or_else(|| FormatError::Invalid(format!("pixel count does not match {width}x{height}")))?; + Ok(image) +} + #[cfg(test)] mod tests { use super::*; @@ -84,6 +133,53 @@ 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 decoded = decode_image(&png).unwrap(); + assert_eq!((decoded.width, decoded.height), (2, 1)); + assert_eq!((decoded.rgba[0][0], decoded.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 decoded = decode_image(&gray16_png(3, 1, [f32::NAN, 9.0, -9.0].into_iter()).unwrap()).unwrap(); + assert_eq!((decoded.rgba[0][0], decoded.rgba[1][0], decoded.rgba[2][0]), (0.0, 1.0, 0.0)); + } + + #[test] + fn generated_resource_matches_reload_for_every_gray16_value() { + let values = (0..=u16::MAX).map(|value| f32::from(value) / f32::from(u16::MAX)); + + let (png, decoded) = gray16_resource(256, 256, values).unwrap(); + + let reloaded = decode_image(&png).unwrap(); + assert_eq!((decoded.width, decoded.height, decoded.srgb), (reloaded.width, reloaded.height, reloaded.srgb)); + for (index, (cached, saved)) in decoded.rgba.iter().zip(&reloaded.rgba).enumerate() { + assert_eq!(cached, saved, "pixel {index}"); + } + let extremes = [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, -1.0, 2.0, 0.5]; + let (png, decoded) = gray16_resource(6, 1, extremes.into_iter()).unwrap(); + + let reloaded = decode_image(&png).unwrap(); + assert_eq!((decoded.width, decoded.height, decoded.srgb), (reloaded.width, reloaded.height, reloaded.srgb)); + for (index, (cached, saved)) in decoded.rgba.iter().zip(&reloaded.rgba).enumerate() { + assert_eq!(cached, saved, "pixel {index}"); + } + assert!(gray16_resource(2, 1, [0.5].into_iter()).is_err()); + } + + #[test] + fn gray16_stops_after_one_excess_pixel() { + let mut pixels_read = 0; + let values = std::iter::from_fn(|| { + pixels_read += 1; + assert!(pixels_read <= 2, "iterator must stop after the first excess pixel"); + Some(0.5) + }); + + assert!(gray16_png(1, 1, values).is_err()); + } + #[test] fn tga_without_a_magic_signature_decodes_within_the_texel_budget() { let mut bytes = Vec::new(); diff --git a/crates/formats/src/lib.rs b/crates/formats/src/lib.rs index c7309f2e..17820459 100644 --- a/crates/formats/src/lib.rs +++ b/crates/formats/src/lib.rs @@ -35,13 +35,16 @@ pub mod materialx_read; pub mod mcmat; pub mod mesh_export; pub mod mesh_import; +#[path = "pbr_map/mod.rs"] +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, gray16_resource}; 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_map/candidate.rs b/crates/formats/src/pbr_map/candidate.rs new file mode 100644 index 00000000..90d722f0 --- /dev/null +++ b/crates/formats/src/pbr_map/candidate.rs @@ -0,0 +1,11 @@ +use super::Detected; + +/// 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, +} diff --git a/crates/formats/src/pbr_map/detected.rs b/crates/formats/src/pbr_map/detected.rs new file mode 100644 index 00000000..144f8657 --- /dev/null +++ b/crates/formats/src/pbr_map/detected.rs @@ -0,0 +1,14 @@ +use super::MapKind; +use texturecraft_core::NormalFormat; + +/// 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, +} diff --git a/crates/formats/src/pbr_map/map_kind.rs b/crates/formats/src/pbr_map/map_kind.rs new file mode 100644 index 00000000..a18d1ebe --- /dev/null +++ b/crates/formats/src/pbr_map/map_kind.rs @@ -0,0 +1,56 @@ +/// 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) + } +} diff --git a/crates/formats/src/pbr_map/mod.rs b/crates/formats/src/pbr_map/mod.rs new file mode 100644 index 00000000..255fbd3e --- /dev/null +++ b/crates/formats/src/pbr_map/mod.rs @@ -0,0 +1,465 @@ +//! 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 texturecraft_core::NormalFormat; + +mod candidate; +mod detected; +mod map_kind; +mod pbr_set; + +pub use candidate::Candidate; +pub use detected::Detected; +pub use map_kind::MapKind; +pub use pbr_set::PbrSet; + +/// File extensions read as images (lower case). +pub const IMAGE_EXTS: &[&str] = &["png", "jpg", "jpeg", "tga", "tif", "tiff", "bmp", "webp", "exr", "hdr"]; + +// 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 current_token = String::new(); + let mut previous_char: Option = None; + for (i, &c) in chars.iter().enumerate() { + if !c.is_alphanumeric() { + if !current_token.is_empty() { + out.push(std::mem::take(&mut current_token)); + } + previous_char = None; + continue; + } + + if let Some(p) = previous_char { + 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 && !current_token.is_empty() { + out.push(std::mem::take(&mut current_token)); + } + } + current_token.extend(c.to_lowercase()); + previous_char = Some(c); + } + if !current_token.is_empty() { + out.push(current_token); + } + 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(|| 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 name_tokens = tokens(stem); + if name_tokens.iter().any(|t| is_preview(t)) { + return Err("preview image"); + } + + let resolution = name_tokens.iter().filter_map(|t| resolution_of(t)).max().unwrap_or(0); + 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 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 + { + packed_end = Some(*c); + } + } + + // Word pairs and the position they end at + let mut pair: Option<(usize, MapKind)> = None; + for (i, t) in name_tokens.iter().enumerate() { + 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, + }; + 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 = name_tokens.get(*i).is_some_and(|t| t == "n"); + !lone_n + || (*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), + (_, 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 format_priority = match c.detected.normal { + Some(NormalFormat::OpenGl) => 0, + None => 1, + Some(NormalFormat::DirectX) => 2, + }; + (format_priority, 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 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)] = &[ + // 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("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([ + ("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 oversized_resolution_tokens_do_not_overflow() { + assert_eq!(classify("brick_4294967295k_albedo.png").unwrap().resolution, 0); + } + + #[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/pbr_map/pbr_set.rs b/crates/formats/src/pbr_map/pbr_set.rs new file mode 100644 index 00000000..4da67768 --- /dev/null +++ b/crates/formats/src/pbr_map/pbr_set.rs @@ -0,0 +1,21 @@ +use super::{Candidate, MapKind}; +use std::collections::BTreeMap; + +/// 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() + } +} 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/formats/src/zip_read.rs b/crates/formats/src/zip_read.rs new file mode 100644 index 00000000..dacd9610 --- /dev/null +++ b/crates/formats/src/zip_read.rs @@ -0,0 +1,55 @@ +//! 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. + +mod zip_files; + +pub use zip_files::ZipFiles; + +/// Maximum number of archive entries indexed for a texture pack. +pub const MAX_ENTRIES: u64 = 4096; + +#[cfg(test)] +mod tests { + use std::io::Write as _; + + use super::*; + + fn make(files: &[(&str, &[u8])]) -> Vec { + let mut archive = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let options = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated); + for (n, b) in files { + archive.start_file(*n, options).unwrap(); + archive.write_all(b).unwrap(); + } + archive.finish().unwrap().into_inner() + } + + #[test] + fn lists_reads_and_caps() { + let mut archive = ZipFiles::open(make(&[("a/x.png", b"hello"), ("b.txt", &[7u8; 5000])])).unwrap(); + assert_eq!(archive.list(10), vec![("a/x.png".to_string(), 5), ("b.txt".to_string(), 5000)]); + assert_eq!(archive.list(1).len(), 1); + assert_eq!(archive.read("a/x.png", 100).unwrap(), b"hello"); + assert!(archive.read("b.txt", 100).is_err(), "over the cap"); + assert!(archive.read("nope", 100).is_err()); + } + + #[test] + fn excessive_entry_count_is_rejected_before_indexing() { + let names: Vec = (0..4097).map(|index| format!("{index}.png")).collect(); + let entries: Vec<(&str, &[u8])> = names.iter().map(|name| (name.as_str(), b"".as_slice())).collect(); + let bytes = make(&entries); + + let result = ZipFiles::open(bytes); + + assert!(matches!(result, Err(crate::FormatError::TooLarge(_)))); + } + + #[test] + fn garbage_is_an_error() { + assert!(ZipFiles::open(b"not a zip".to_vec()).is_err()); + assert!(ZipFiles::open(Vec::new()).is_err()); + } +} diff --git a/crates/formats/src/zip_read/zip_files.rs b/crates/formats/src/zip_read/zip_files.rs new file mode 100644 index 00000000..c961ec1d --- /dev/null +++ b/crates/formats/src/zip_read/zip_files.rs @@ -0,0 +1,52 @@ +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 { + // Bound central-directory allocation before the ZIP decoder indexes entries + craft_zip::check_entry_count(&bytes, super::MAX_ENTRIES) + .map_err(|error| if error.is_limit() { FormatError::TooLarge(error.to_string()) } else { malformed("zip", error) })?; + + 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) + } +} diff --git a/crates/ui-egui/src/i18n/ar.rs b/crates/ui-egui/src/i18n/ar.rs index 4da3d4eb..39a77aca 100644 --- a/crates/ui-egui/src/i18n/ar.rs +++ b/crates/ui-egui/src/i18n/ar.rs @@ -1045,6 +1045,21 @@ 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} ملفات"), ("Select the layer content", "تحديد محتوى الطبقة"), ("Paint layer", "طبقة رسم"), ("Fill layer", "طبقة تعبئة"), diff --git a/crates/ui-egui/src/i18n/de.rs b/crates/ui-egui/src/i18n/de.rs index 11d7cc41..0ee8bc63 100644 --- a/crates/ui-egui/src/i18n/de.rs +++ b/crates/ui-egui/src/i18n/de.rs @@ -1085,6 +1085,21 @@ 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"), ("Select the layer content", "Ebeneninhalt auswählen"), ("Paint layer", "Malebene"), ("Fill layer", "Füllebene"), diff --git a/crates/ui-egui/src/i18n/es.rs b/crates/ui-egui/src/i18n/es.rs index 06c86363..fbb2d61e 100644 --- a/crates/ui-egui/src/i18n/es.rs +++ b/crates/ui-egui/src/i18n/es.rs @@ -1073,6 +1073,21 @@ 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"), ("Select the layer content", "Seleccionar el contenido de la capa"), ("Paint layer", "Capa de pintura"), ("Fill layer", "Capa de relleno"), diff --git a/crates/ui-egui/src/i18n/fr.rs b/crates/ui-egui/src/i18n/fr.rs index 76df429e..6ed0bd03 100644 --- a/crates/ui-egui/src/i18n/fr.rs +++ b/crates/ui-egui/src/i18n/fr.rs @@ -1097,6 +1097,21 @@ 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"), ("Select the layer content", "Sélectionner le contenu du calque"), ("Paint layer", "Calque de peinture"), ("Fill layer", "Calque de remplissage"), diff --git a/crates/ui-egui/src/i18n/it.rs b/crates/ui-egui/src/i18n/it.rs index cb63927c..aaa43fa1 100644 --- a/crates/ui-egui/src/i18n/it.rs +++ b/crates/ui-egui/src/i18n/it.rs @@ -1079,6 +1079,21 @@ 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, rugosità, metallico)"), + ("Triplanar", "Triplanare"), + ("UV", "UV"), + ("{n} files ignored", "{n} file ignorati"), ("Select the layer content", "Seleziona il contenuto del livello"), ("Paint layer", "Livello di pittura"), ("Fill layer", "Livello di riempimento"), diff --git a/crates/ui-egui/src/i18n/ja.rs b/crates/ui-egui/src/i18n/ja.rs index 0a39f24f..17a5e3b5 100644 --- a/crates/ui-egui/src/i18n/ja.rs +++ b/crates/ui-egui/src/i18n/ja.rs @@ -1067,6 +1067,21 @@ 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} 個のファイルを無視しました"), ("Select the layer content", "レイヤーの内容を選択"), ("Paint layer", "ペイントレイヤー"), ("Fill layer", "塗りつぶしレイヤー"), diff --git a/crates/ui-egui/src/i18n/ko.rs b/crates/ui-egui/src/i18n/ko.rs index 002f0bbd..14f3859a 100644 --- a/crates/ui-egui/src/i18n/ko.rs +++ b/crates/ui-egui/src/i18n/ko.rs @@ -1055,6 +1055,21 @@ 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}개 무시됨"), ("Select the layer content", "레이어 내용 선택"), ("Paint layer", "페인트 레이어"), ("Fill layer", "채우기 레이어"), diff --git a/crates/ui-egui/src/i18n/pt_br.rs b/crates/ui-egui/src/i18n/pt_br.rs index 1b3a2e34..7e376db9 100644 --- a/crates/ui-egui/src/i18n/pt_br.rs +++ b/crates/ui-egui/src/i18n/pt_br.rs @@ -1070,6 +1070,21 @@ 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"), ("Select the layer content", "Selecionar o conteúdo da camada"), ("Paint layer", "Camada de pintura"), ("Fill layer", "Camada de preenchimento"), diff --git a/crates/ui-egui/src/i18n/zh_hans.rs b/crates/ui-egui/src/i18n/zh_hans.rs index 3cefc53a..966df28b 100644 --- a/crates/ui-egui/src/i18n/zh_hans.rs +++ b/crates/ui-egui/src/i18n/zh_hans.rs @@ -1010,6 +1010,21 @@ 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} 个文件"), ("Select the layer content", "选择图层内容"), ("Paint layer", "绘画图层"), ("Fill layer", "填充图层"), diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 2ded5072..74d92580 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -40,6 +40,7 @@ mod orb_shape; pub mod parity; mod path_hit; mod path_surface_cache; +pub mod pbr_import; mod preview_image; mod preview_request; mod preview_worker; @@ -140,6 +141,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, @@ -230,6 +233,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(), @@ -295,6 +299,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; @@ -690,13 +698,27 @@ impl eframe::App for TextureCraftApp { self.run_file_dialog_answers(); web_files::poll(self); let dropped = ctx.input(|i| i.raw.dropped_files.clone()); - for f in &dropped { - web_files::dropped(self, ctx, f); + + // A folder, a zip or several PBR maps dropped together open the PBR import dialog + #[cfg(not(target_arch = "wasm32"))] + 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 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); self.poll_link_cooks(ctx); self.thumbs.sync_images(&self.session.images, self.session.revision); + // The library shelf rescans when the window comes back (MaterialCraft may have sent a material) let focused = ctx.input(|i| i.viewport().focused).unwrap_or(true); if focused && !self.focused { @@ -720,6 +742,7 @@ impl eframe::App for TextureCraftApp { updates_ui::tick(self, ctx); self.bindings.sync(&self.ui.keymap, craft_keymap::egui::platform(ctx)); menus::shortcuts(self, ctx); + // The Path tool's curve repaints (a preview while placed) as its points, brush or material change if let Err(e) = self.session.path_live() { self.session.cancel_path(); @@ -728,7 +751,8 @@ impl eframe::App for TextureCraftApp { profile::drive(self, ctx); self.toasts.retain(|t| t.until > now); self.profiler.logic_ms = (now_ms() - t0) as f32; - // Last, so it sees every close request this frame's work made. + + // Last, so it sees every close request this frame's work made windows::close_request(self, ctx); } diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 9a4d3208..9c66fd45 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -48,6 +48,8 @@ pub const CATALOG: &[Item] = &[ sep("File"), it("File", "Import resources...", "resource.import", None), it("File", "Import MaterialX material...", "file.importMaterialX", None), + #[cfg(not(target_arch = "wasm32"))] + 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..1b6300c1 --- /dev/null +++ b/crates/ui-egui/src/pbr_import.rs @@ -0,0 +1,237 @@ +//! 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}; + +mod pbr_dialog; + +pub use pbr_dialog::PbrDialog; + +/// 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, + #[cfg(not(target_arch = "wasm32"))] + library: false, + }); +} + +/// File › Import PBR texture folder: choose the folder. +#[cfg(not(target_arch = "wasm32"))] +pub fn pick(app: &mut TextureCraftApp) { + use crate::file_dialogs::{Bound, FileDialog}; + app.ask_file(FileDialog::folder(), Bound::Project, |app, paths| { + if let Some(dir) = paths.first() { + 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 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]) -> Vec { + use texturecraft_engine::formats::pbr_maps; + if let Some((index, dir)) = paths.iter().enumerate().find(|(_, p)| p.is_dir()) { + begin(app, json!({ "path": dir.display().to_string() })); + return vec![index]; + } + + 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 vec![index]; + } + + 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; + } + + Vec::new() +} + +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 dialog_state) = app.pbr_import.take() else { return }; + + let mut open = true; + let (mut import_clicked, mut cancel_clicked) = (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 dialog_state.name); + }); + ui.add_space(4.0); + ui.label(RichText::new(tr("Detected maps")).strong()); + let maps = dialog_state.scan.get("maps").and_then(Value::as_object); + let candidates = dialog_state.scan.get("candidates").and_then(Value::as_object); + + for (id, _) in MAPS { + let Some(file) = maps.and_then(|maps| maps.get(id)).and_then(Value::as_str) else { continue }; + let shown = dialog_state.choose.get(id).map(String::as_str).unwrap_or(file).to_string(); + match candidates.and_then(|candidates| candidates.get(id)).and_then(Value::as_array) { + Some(all) => { + combo(ui, tr(label_of(id)), shown, |ui| { + for candidate in all.iter().filter_map(Value::as_str) { + let selected = dialog_state.choose.get(id).map(String::as_str).unwrap_or(file); + if ui.selectable_label(selected == candidate, candidate).clicked() { + dialog_state.choose.insert(id.to_string(), candidate.to_string()); + } + } + }); + } + None => { + ui.horizontal(|ui| { + ui.label(tr(label_of(id))); + ui.label(RichText::new(shown).weak()); + }); + } + } + } + if maps.is_none_or(|maps| maps.is_empty()) { + ui.label(tr("Nothing recognised in this folder")); + } + + let ignored = dialog_state.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.is_some_and(|maps| maps.contains_key("normal")) { + let chosen = dialog_state.choose.get("normal").map(String::as_str).or_else(|| maps.and_then(|maps| maps.get("normal")).and_then(Value::as_str)); + let found = chosen + .and_then(|name| texturecraft_engine::formats::pbr_maps::classify(name).ok()) + .is_some_and(|detected| detected.normal == Some(texturecraft_engine::core::NormalFormat::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()][dialog_state.normal.min(2)].clone(); + combo(ui, tr("Normal map format"), shown, |ui| { + ui.selectable_value(&mut dialog_state.normal, 0, auto); + ui.selectable_value(&mut dialog_state.normal, 1, tr("OpenGL")); + ui.selectable_value(&mut dialog_state.normal, 2, tr("DirectX")); + }); + } + combo(ui, tr("Projection"), if dialog_state.triplanar { tr("Triplanar") } else { tr("UV") }, |ui| { + ui.selectable_value(&mut dialog_state.triplanar, false, tr("UV")); + ui.selectable_value(&mut dialog_state.triplanar, true, tr("Triplanar")); + }); + #[cfg(not(target_arch = "wasm32"))] + ui.checkbox(&mut dialog_state.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_some_and(|maps| !maps.is_empty()), |ui| import_clicked = ui.add(primary_button(ui, tr("Import"))).clicked()); + cancel_clicked = ui.button(tr("Cancel")).clicked(); + }); + }); + + let imported = import_clicked && run(app, &dialog_state); + if open && !imported && !cancel_clicked { + app.pbr_import = Some(dialog_state); + } +} + +/// Import with the dialog's choices. +fn run(app: &mut TextureCraftApp, dialog_state: &PbrDialog) -> bool { + let mut params = dialog_state.source.clone(); + params["name"] = json!(dialog_state.name.trim()); + params["normalFormat"] = json!(["auto", "openGl", "directX"][dialog_state.normal.min(2)]); + if dialog_state.triplanar { + params["projection"] = json!("triplanar"); + } + + // 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 }; + + let name = reply.get("name").and_then(Value::as_str).unwrap_or_default().to_string(); + for warning in reply.get("warnings").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str) { + app.session.log.push(format!("PBR {name}: {warning}")); + } + + app.toast(trf("Imported {name}", &[("name", &name)]), false); + app.ui.dock.focus(crate::docking::Panel::Layers); + #[cfg(not(target_arch = "wasm32"))] + if dialog_state.library + && let Some(layer) = reply.get("layer").and_then(Value::as_u64) + { + save_to_library(app, layer, &name); + } + + true +} + +/// 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 suffix_index = 2; + + while path.exists() { + path = dir.join(format!("{stem} {suffix_index}.{}", texturecraft_engine::material_lib::SMART_MATERIAL_EXT)); + suffix_index += 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/pbr_import/pbr_dialog.rs b/crates/ui-egui/src/pbr_import/pbr_dialog.rs new file mode 100644 index 00000000..d645f54a --- /dev/null +++ b/crates/ui-egui/src/pbr_import/pbr_dialog.rs @@ -0,0 +1,18 @@ +use serde_json::Value; +use std::collections::BTreeMap; + +/// The open import dialog. +pub struct PbrDialog { + /// `{path}` or `{files}`: where the maps come from. + pub(super) source: Value, + /// What the scan found. + pub(super) scan: Value, + pub(super) name: String, + /// The file picked per map where several were found. + pub(super) choose: BTreeMap, + /// 0: automatic (from the file name), 1: OpenGL, 2: DirectX. + pub(super) normal: usize, + pub(super) triplanar: bool, + #[cfg(not(target_arch = "wasm32"))] + pub(super) library: bool, +} 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/src/windows.rs b/crates/ui-egui/src/windows.rs index 0416b022..5650c4de 100644 --- a/crates/ui-egui/src/windows.rs +++ b/crates/ui-egui/src/windows.rs @@ -29,6 +29,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); } @@ -921,6 +922,7 @@ pub fn file_action(app: &mut TextureCraftApp, _ctx: &egui::Context, id: &str) -> if paths.is_empty() { return; } + for p in paths { if let Some(r) = app.run("resource.import", json!({"path": p.display().to_string()})) { let name = r.get("name").and_then(|n| n.as_str()).unwrap_or_default().to_string(); @@ -946,6 +948,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)); let dialog = FileDialog::save() diff --git a/crates/ui-egui/tests/ui_snapshots.rs b/crates/ui-egui/tests/ui_snapshots.rs index 4997332b..d337cad2 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(); @@ -28,6 +31,135 @@ fn save(h: &mut Harness<'_, TextureCraftApp>, dir: &Path, name: &str) { eprintln!("wrote {}", path.display()); } +#[test] +#[ignore] +fn pbr_import_dialog_keeps_choices_after_error() { + 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 = std::env::temp_dir().join(format!("tc_pbr_snapshot_{}", std::process::id())); + std::fs::create_dir_all(&source_dir).unwrap(); + let bytes = texturecraft_engine::formats::gray16_png(2, 2, [0.5; 4].into_iter()).unwrap(); + for name in ["tile_albedo.png", "tile_normal_gl.png", "tile_normal_dx.png"] { + std::fs::write(source_dir.join(name), &bytes).unwrap(); + } + let session = texturecraft_engine::sample_session(256, 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.state_mut().ui.prefs.language = "en".into(); + texturecraft_ui_egui::pbr_import::begin(harness.state_mut(), serde_json::json!({"path": source_dir.display().to_string()})); + + harness.run_steps(3); + harness.get_all_by_role(egui::Role::ComboBox).find(|node| node.value().as_deref() == Some("tile_normal_gl.png")).unwrap().click(); + harness.run_steps(2); + harness.get_by_label("tile_normal_dx.png").click(); + harness.run_steps(2); + + assert!(harness.get_all_by_role(egui::Role::ComboBox).any(|node| node.value().as_deref() == Some("Automatic (DirectX)"))); + save(&mut harness, &output_dir, "pbr-import-dialog"); + std::fs::remove_dir_all(&source_dir).unwrap(); + harness.get_by_role_and_label(egui::Role::Button, "Import").click(); + harness.run_steps(2); + + assert!(harness.state().pbr_import.is_some()); + assert!(harness.get_all_by_role(egui::Role::ComboBox).any(|node| node.value().as_deref() == Some("tile_normal_dx.png"))); + assert!(harness.get_all_by_role(egui::Role::ComboBox).any(|node| node.value().as_deref() == Some("Automatic (DirectX)"))); + save(&mut harness, &output_dir, "pbr-import-error"); +} + +#[test] +#[ignore] +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 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() + .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()})); + 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"); + + 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..]).is_empty()); + 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] 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)) +} diff --git a/docs/control-protocol.md b/docs/control-protocol.md index eda79bd9..98272ddc 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -311,6 +311,64 @@ 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, name?, normalFormat?, choose?, projection?, scale?}` → `{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. + The import reply's `name` is the created layer's unique name, including its numeric suffix. + A blank `name` uses the same source-derived default as an omitted name. +- **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 rejected as sources. A scan admits at most 4096 + directory entries (including subdirectories), selected files, or ZIP entries. Folder traversal is + limited to four levels below the root; exceeding a scan limit returns an error instead of a partial list. + A file or ZIP entry is at most 256 MiB, an archive 512 MiB, an image 64 Mi texels (320 Mi per import). + Actual reads are capped even if a file grows. Decoded sources and outputs also share an estimated + memory budget: one quarter of available memory, capped at 4 GiB (1 GiB for the byte API on the web). + Compressed inputs already held by the caller and existing project/GPU memory are outside that estimate. + An image that exceeds the budget or cannot decode is skipped with a warning; if none decodes the + import is an error that changes nothing. `maps` describes detection; `channels` and `resources` + describe the successful import. Scan validates import options without decoding pixels. + +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). ## Camera bookmarks Saved cameras (`Project.cameras`, stored in the `.tcraft` file) are the camera bookmarks. diff --git a/docs/parity.md b/docs/parity.md index 75f6fd30..8ef5389a 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). -**59 of 59 menu items live (100%).** +**60 of 60 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 |