diff --git a/ROADMAP.md b/ROADMAP.md index 4e9bc96e..00e73724 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -12,7 +12,7 @@ end to end today (engine + UI + automation + tests), not what is planned. | Area | Weight | Done | What works | Main gaps | |---|---:|---:|---|---| | Project and files | 8 | 95% | New project from glTF/GLB, FBX (binary and ASCII, 6.x and 7.x), COLLADA, **USD (usda, usdc, usdz)**, **Alembic (Ogawa)**, OBJ, PLY, STL or built-in meshes (auto-unwrap when UVs are missing; a `.gltf`'s buffers and images and a loose USD layer's textures are read from its folder and kept in the project), **project templates** (built-in and saved from a project), `.tcraft` open/save/save as/save a copy, autosave, **asks to save unsaved changes** before quitting or replacing the project (in the browser too, and before the tab closes), recent files, drag and drop, reimport mesh **with strokes preserved on the surface**, **MaterialX import** (`file.importMaterialX`: a `.mtlx` material with `open_pbr_surface`, `standard_surface` or `gltf_pbr` becomes one fill layer with its textures and values), image resources, remove unused resources, OBJ, textured GLB and **USD** mesh export, browser open/save/download | HDF5 Alembic; Alembic animation (first sample only) | -| Texture sets | 6 | 95% | One set per material, **UDIM / UV tiles** (auto-detected or switched on later with resampling, **a resolution per tile**, one texture per tile at its own size, `$udim` export), list with visibility (show or hide all, invert, **focus** on one set), sizes 128–8192 with resampling, add/remove channels (19 channel types incl. coat, sheen, anisotropy and scattering), height strength, normal format | Filter radii and height slopes of a tile smaller than its atlas block follow the block; mesh maps from images need a set without UV tiles | +| Texture sets | 6 | 95% | One set per material, **UDIM / UV tiles** (auto-detected or switched on later with resampling, **a resolution per tile**, one texture per tile at its own size, `$udim` export), list with visibility (show or hide all, invert, **focus** on one set), sizes 128–8192 with resampling, add/remove channels (19 channel types incl. coat, sheen, anisotropy and scattering), height strength, normal format | Filter radii and height slopes of a tile smaller than its atlas block follow the block | | Layer stack | 16 | 98% | Paint, fill, folder and **instance** layers; 32 per-channel blend modes and opacities; white/black/painted masks with **mask with height**; generator, fill, levels (with a histogram of its input), invert/threshold, color selection, compare, blur, sharpen and HSL effects on masks and layer content; anchor points; effects **dragged to reorder**, duplicated, and **copied, cut and pasted** across layers, masks and sets (Ctrl+C, Ctrl+X, Ctrl+V, Ctrl+D and Delete act on a selected effect); smart masks; mask view (Alt+click); Alt+click a material channel to keep it alone; duplicate, group, drag to reorder, rename, color tags, copy/cut/paste (also as instance) across sets, flatten, instantiate across sets, save layers as smart materials; **30 library filters** (adjustments incl. histogram scan, range and shift, blur/sharpen/high pass, bevel, glow, edge detect, warps, mosaic, baked lighting, material finishes); **geometry masks** (a layer on chosen mesh parts) | A few niche filters of the reference app's catalog | | Painting tools | 14 | 91% | Paint, eraser, projection with image stencils, polygon fill, **path** and **lasso fill**, smudge, clone, color picker, quick mask, Shift+click lines with preview, 2D view painting; size, flow, stroke opacity, spacing, hardness, angle, follow path, jitters, alignment, angle fade, backface culling, lazy mouse, **Ctrl+drag in a view for size, hardness, flow and angle**, X/Y/Z mirror and **radial symmetry**, pen pressure, procedural and image alphas, **text** in any installed font (as an alpha or a decal), **particle brush** | Pen tilt (not exposed by the windowing layer) | | Materials and assets | 8 | 92% | **MaterialCraft materials** (`.mcmat`, sent to the shelf from MaterialCraft) as fill layers whose graph inputs and presets stay editable, re-cooked at the set's resolution with its baked mesh maps; 63 procedural materials (incl. car paint, velvet, brushed steel, skin, wax, ice, snow, bark), 21 smart materials, 18 smart masks, **67 procedural brush alphas**, **18 brush presets** plus user presets, 23 procedurals, 6 environments plus imported HDR/EXR panoramas, image resources, a user smart material library, a tabbed shelf with search and **drag and drop** onto layers and the viewport | A smaller bundled library than the reference app's | diff --git a/crates/engine/src/meshmap_cmds.rs b/crates/engine/src/meshmap_cmds.rs index 1a42a362..3f3a414e 100644 --- a/crates/engine/src/meshmap_cmds.rs +++ b/crates/engine/src/meshmap_cmds.rs @@ -1,12 +1,13 @@ //! Mesh map slots of the Texture Set Settings: use an image resource as a mesh map (baked in //! another app, or edited by hand) instead of baking it, or clear a slot. +use std::collections::BTreeMap; use std::sync::Arc; use rayon::prelude::*; use serde_json::{Value, json}; -use texturecraft_compose::ImageRes; -use texturecraft_core::{BakedMap, MAX_RESOLUTION, NormalFormat}; +use texturecraft_compose::{ImageRes, ImageStore}; +use texturecraft_core::{BakedMap, MAX_RESOLUTION, NormalFormat, UDIM_RANGE, udim_at}; use crate::commands::{CommandSpec, always, bad, strp}; use crate::set_cmds::ts_param; @@ -19,7 +20,7 @@ pub fn specs() -> Vec { "Use image as mesh map", [], None, - "{ts?, map: mesh map id (normal_base, ambient_occlusion, curvature, id, height…), image: resource name} → {map, resolution} — resampled to the size of the set's other mesh maps (or its bake size); a normal map is read in the project's normal format", + "{ts?, map: mesh map id (normal_base, ambient_occlusion, curvature, id, height…), image: resource name} → {map, resolution} — resampled to the size of the set's other mesh maps (or its bake size); a normal map is read in the project's normal format. A UV-tiled set takes one image per tile, named alike with the tile number (`image: \"ao.1001.png\"` finds ao.1002.png…)", always, set ), @@ -38,18 +39,27 @@ fn set(s: &mut Session, p: &Value) -> Result { let map = map_param(p, CMD)?; let name = strp(p, "image").ok_or_else(|| bad(CMD, "`image` (an image resource name) is required"))?; let img = s.images.get(name).cloned().ok_or_else(|| bad(CMD, format!("no image resource `{name}`")))?; - let set = s.doc.project.texture_set(ts)?; - // shortcut: one image spans the whole set; UV-tiled sets would need one image per tile. - if !set.uv_tiles.is_empty() { - return Err(bad(CMD, format!("\"{}\" has UV tiles: mesh maps from images need a set without them", set.name))); - } - // Every mesh map of a set shares one size: the other maps', or what a bake would make. - let res = s.doc.baked.get(&ts).filter(|b| !b.maps.is_empty()).map_or(set.bake.resolution, |b| b.resolution); - if !(1..=MAX_RESOLUTION).contains(&res) { - return Err(bad(CMD, format!("mesh map size {res} is not in 1..={MAX_RESOLUTION}"))); + let set = s.doc.project.texture_set(ts)?.clone(); + let tiles = set.tile_grid().map(|grid| tile_images(&s.images, name, &set.uv_tiles).map(|imgs| (grid, imgs))).transpose()?; + // Every mesh map of a set shares one size: the other maps', or what a bake would make (a + // block of the bake resolution per tile of the atlas). + let (cols, rows) = tiles.as_ref().map_or((1, 1), |(g, _)| (g.cols, g.rows)); + let (res, height) = match s.doc.baked.get(&ts).filter(|b| !b.maps.is_empty()) { + Some(b) => (b.resolution, b.maps.keys().next().map_or(b.resolution, |m| b.height(*m))), + None => (set.bake.resolution.saturating_mul(cols), set.bake.resolution.saturating_mul(rows)), + }; + if res == 0 || height == 0 || u64::from(res) * u64::from(height) > texturecraft_bake::MAX_TEXELS || (tiles.is_none() && res > MAX_RESOLUTION) { + return Err(bad(CMD, format!("mesh map size {res} × {height} is too large or empty"))); } let directx = map == BakedMap::Normal && s.doc.project.normal_format == NormalFormat::DirectX; - let data = resample(&img, map.components(), res, directx); + let data = match &tiles { + None => resample(map.components(), (res, height), directx, |u, v| img.sample(u, v)), + // An atlas texel reads the image of the tile its mesh UV falls in. + Some((grid, imgs)) => resample(map.components(), (res, height), directx, |u, v| { + let uv = set.from_atlas(grid, [u, v]); + udim_at(uv).and_then(|t| imgs.get(&t)).map_or([0.0; 4], |img| img.sample(uv[0] - uv[0].floor(), uv[1] - uv[1].floor())) + }), + }; s.edit("Use image as mesh map", |s| { let entry = s.doc.baked.entry(ts).or_default(); entry.resolution = res; @@ -60,15 +70,38 @@ fn set(s: &mut Session, p: &Value) -> Result { Ok(json!({ "map": map.id(), "resolution": res })) } -/// `img` as `res` × `res` texels of `comps` values each, as stored in the file (mesh maps hold -/// data, not colors, so nothing is linearized); `flip_green` reads a DirectX normal map. -fn resample(img: &ImageRes, comps: usize, res: u32, flip_green: bool) -> Vec { - let n = res as usize; - let mut out = vec![0.0; n * n * comps]; - out.par_chunks_mut(n * comps).enumerate().for_each(|(y, row)| { - let v = (y as f32 + 0.5) / res as f32; +/// The image of every UV tile of a set: `name` with its tile number (`ao.1001.png`) swapped for +/// each tile's. +fn tile_images(images: &ImageStore, name: &str, tiles: &[u32]) -> Result>> { + const CMD: &str = "textureSet.meshMap.set"; + let at = udim_in(name).ok_or_else(|| bad(CMD, format!("`{name}` names no UV tile: a UV-tiled set takes one image per tile, named like ao.1001.png")))?; + tiles + .iter() + .map(|&t| { + let tile_name = format!("{}{t}{}", name.get(..at).unwrap_or_default(), name.get(at + 4..).unwrap_or_default()); + images.get(&tile_name).cloned().map(|img| (t, img)).ok_or_else(|| bad(CMD, format!("no image `{tile_name}` for UV tile {t}"))) + }) + .collect() +} + +/// Where the last UDIM tile number (four digits alone, in [`UDIM_RANGE`]) in `name` starts. +fn udim_in(name: &str) -> Option { + let b = name.as_bytes(); + let digit = |i: Option| i.and_then(|i| b.get(i)).is_some_and(u8::is_ascii_digit); + (0..b.len().saturating_sub(3)).rev().find(|&i| { + !digit(i.checked_sub(1)) && !digit(Some(i + 4)) && name.get(i..i + 4).and_then(|d| d.parse::().ok()).is_some_and(|t| UDIM_RANGE.contains(&t)) + }) +} + +/// `w` × `h` texels of `comps` values each from `sample` (atlas UV → RGBA), as stored in the +/// file (mesh maps hold data, not colors, so nothing is linearized); `flip_green` reads a +/// DirectX normal map. +fn resample(comps: usize, (w, h): (u32, u32), flip_green: bool, sample: impl Fn(f32, f32) -> [f32; 4] + Sync) -> Vec { + let mut out = vec![0.0; w as usize * h as usize * comps]; + out.par_chunks_mut(w as usize * comps).enumerate().for_each(|(y, row)| { + let v = (y as f32 + 0.5) / h as f32; for (x, texel) in row.chunks_exact_mut(comps).enumerate() { - let mut c = img.sample((x as f32 + 0.5) / res as f32, v); + let mut c = sample((x as f32 + 0.5) / w as f32, v); if flip_green { c[1] = 1.0 - c[1]; } diff --git a/crates/engine/tests/udim.rs b/crates/engine/tests/udim.rs index 54faa9c7..86864fa7 100644 --- a/crates/engine/tests/udim.rs +++ b/crates/engine/tests/udim.rs @@ -127,3 +127,27 @@ fn baking_covers_every_tile() { assert!(x_at(10) < 0.3 && x_at(250) > 0.7, "{} {}", x_at(10), x_at(250)); s.update(); } + +/// A UV-tiled set takes a mesh map from one image per tile, found by the tile number in the +/// name; each tile's block of the atlas reads its own image. +#[test] +fn mesh_maps_from_images_take_one_image_per_tile() { + let mut s = session(); + let png = |v: u8| { + let mut out = Vec::new(); + image::RgbaImage::from_pixel(4, 4, image::Rgba([v, v, v, 255])).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap(); + out + }; + let set = |s: &mut Session, image: &str| execute(s, "textureSet.meshMap.set", &json!({"map": "ambient_occlusion", "image": image})); + let left = s.import_resource_bytes("ao.1001.png", png(255)).unwrap(); + assert!(set(&mut s, &left).unwrap_err().to_string().contains("ao.1002.png"), "tile 1002 has no image yet"); + let plain = s.import_resource_bytes("ao.png", png(255)).unwrap(); + assert!(set(&mut s, &plain).is_err(), "no tile number in the name"); + s.import_resource_bytes("ao.1002.png", png(0)).unwrap(); + let r = set(&mut s, &left).unwrap()["resolution"].as_u64().unwrap() as usize; + let ao = &s.doc.baked[&0].maps[&BakedMap::AmbientOcclusion]; + let h = ao.len() / r; + assert_eq!(r, 2 * h, "a block of the bake size per tile, side by side"); + let at = |x: usize| ao[(h / 2) * r + x]; + assert!(at(r / 4) > 0.95 && at(3 * r / 4) < 0.05, "1001 on the left, 1002 on the right: {} {}", at(r / 4), at(3 * r / 4)); +} diff --git a/docs/features.md b/docs/features.md index aa7a6b98..872c268c 100644 --- a/docs/features.md +++ b/docs/features.md @@ -98,7 +98,8 @@ Everything TextureCraft does today, by area. What's still missing is in [`ROADMA and direction maps, and normal and height maps from high-poly meshes, with a cage, ray distances and matching by mesh name. Or **use your own images** as mesh maps (baked in another app, or edited): drop one from the Assets panel on a slot of the Texture Set Settings, or pick it from - the slot's menu. + the slot's menu. A UV-tiled set takes one image per tile, named with its tile number + (`ao.1001.png`, `ao.1002.png`…): drop or pick any one of them. - **Export presets** for glTF PBR Metal Roughness, Unreal Engine (packed), Unity HDRP (mask map), Unity Standard, Blender (Principled BSDF), Godot 4 (ORM), OpenPBR (with the coat, fuzz, anisotropy, transmission and subsurface maps), separate maps, mesh maps and document channels, in