Merge remote-tracking branch 'origin/main' into fix/review-followups

This commit is contained in:
bflatastic committed 2026-11-02 15:05:10 +07:00
commit d577a7eae8
37 files changed
+1311 -16

No files matched your search

+6 -1
View File
@@ -124,7 +124,12 @@ panic. The shared standard is `../craftrules/standards/never-crash.md`.
alternatives.
7. **Performance is a feature.** Never scan a full texture set per frame; work on dirty tiles.
Benchmark heavy paths (bake, composite, export) in release and record numbers in the dev log.
8. **Verify UI changes visually.** Launch with `--control <port>` and call `ui.screenshot` (whole
8. **Cross-app formats are shared conventions.** MaterialCraft's `.mcmat` portable materials are
read here with our own code (`formats::mcmat`): keep the manifest fields, usage ids and
mesh-map identifiers (`BakedMap::interop_id`) in step with MaterialCraft's
`docs/control-protocol.md`, and never depend on MaterialCraft's crates; talk to it through
`materialcraft-cli`.
9. **Verify UI changes visually.** Launch with `--control <port>` and call `ui.screenshot` (whole
window) or `viewport.screenshot`; look at the PNG. Without a visible screen (a locked machine,
CI), render the whole window offscreen:
`TC_UI_SNAPSHOTS=<dir> cargo test -p texturecraft-ui-egui --release --test ui_snapshots -- --ignored`
Generated
+1
View File
@@ -3910,6 +3910,7 @@ dependencies = [
"texturecraft-paint",
"texturecraft-texel",
"thiserror 2.0.21",
"zip",
]
[[package]]
+5
View File
@@ -127,6 +127,11 @@ traced on the GPU by `texturecraft-cli render`.
- **Open.** glTF/GLB, FBX, COLLADA, USD, Alembic, OBJ, PLY and STL in; PNG, JPEG, TGA, TIFF, BMP,
WEBP, EXR and HDR textures out at 8, 16 or 32 bits, with UDIM tiles; OBJ, textured GLB and USD
mesh export. Every material, environment, brush alpha and built-in mesh is procedural.
- **Works with MaterialCraft.** Node-based materials from
[MaterialCraft](https://github.com/storytold/materialcraft) (`.mcmat`, sent straight to the
shelf) paint and fill like any material, and their graph inputs stay sliders, colors and
presets here: each change re-cooks the material at the texture set's resolution, with this
mesh's baked maps feeding the graph.
- **Agent-native.** Every menu item and tool is a command you can drive from the CLI, a JSON
control channel or an **MCP server**, so Claude and other agents can paint, bake and export the
way a person does.
+1 -1
View File
@@ -15,7 +15,7 @@ end to end today (engine + UI + automation + tests), not what is planned.
| 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 |
| 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 | 90% | 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 |
| 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 |
| Baking | 10 | 100% | AO, curvature (also from the normal map), thickness, world normal, position, ID, normal, height, **bent normals, opacity, color map and world-space direction**; **high-poly meshes**, cage, frontal/rear distances, average normals, **match by mesh name**; per-texture-set selection, common and per-map parameters, progress and cancel; **Auto-rebake** (`bake.autoRebake`: one map bakes itself again in the background when its bake settings, the low poly or the high polys change); **mesh maps from images** (baked elsewhere or edited) dropped on a slot; high-poly base color textures embedded or in files beside a `.gltf`, FBX or USD layer | — |
| Viewport and display | 12 | 95% | PBR with image-based lighting, 6 procedural environments or imported HDR/EXR panoramas with exposure, rotation, blur, opacity, ACES/linear tone mapping, HDR pipeline with bloom, vignette, sharpen and grain, key-light shadows, 4× MSAA plus idle temporal accumulation, the baked normal map under the painted normal, channel / mesh-map / mask / UV checker views, 3D and 2D wireframes of the mesh's own polygons, brush cursor, axis gizmos, subsurface scattering blur, **depth of field**, saved cameras and cameras from glTF, FBX, COLLADA and USD files, displacement (also in shadows and the path tracers), **path-traced Rendering mode** (F10, on the GPU or the CPU) with environment importance sampling and an edge-aware **denoiser**; picks a discrete GPU on multi-GPU machines (`--gpu`, `WGPU_ADAPTER_NAME`, `WGPU_POWER_PREF`), and display textures are made on demand so a mesh with many materials imports on a small GPU | Hardware ray tracing |
| Shader | 6 | 100% | Metallic/roughness and specular/glossiness PBR, **clear coat, sheen, anisotropy and subsurface scattering** (viewport and path tracer), height-to-normal, painted normals, parallax occlusion, emissive, opacity (dithered), AO intensity | — |
+53
View File
@@ -362,6 +362,59 @@ impl Source {
pub struct Fill {
pub sources: BTreeMap<Channel, Source>,
pub projection: Projection,
/// Set when the sources are maps cooked from a MaterialCraft material.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub link: Option<Box<MaterialLink>>,
}
/// A MaterialCraft material behind a fill (from a `.mcmat` portable material): its channel
/// sources are image resources cooked from the graph, and changing a parameter re-cooks them
/// through `materialcraft-cli`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MaterialLink {
pub name: String,
/// The image resource holding the material's package (`.mcraft`).
pub package: String,
/// The package's graph the maps come from.
pub graph: String,
pub params: Vec<LinkParam>,
#[serde(default)]
pub presets: Vec<LinkPreset>,
/// Image inputs of the graph (identifiers such as `curvature`), fed from baked mesh maps.
#[serde(default)]
pub inputs: Vec<String>,
/// Size the maps were cooked at (pixels across).
pub size: u32,
}
/// An exposed input of a linked material. Values keep MaterialCraft's typed form,
/// `{"t": "float" | "float2" | "float3" | "float4" | "int" | "int2" | "bool" | "color" | "enum", "v": …}`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct LinkParam {
pub id: String,
pub label: String,
#[serde(default)]
pub group: String,
pub value: serde_json::Value,
pub default: serde_json::Value,
#[serde(default)]
pub min: f32,
#[serde(default = "one")]
pub max: f32,
/// Labels of an `enum` value.
#[serde(default)]
pub options: Vec<String>,
}
fn one() -> f32 {
1.0
}
/// A named set of linked-material values.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct LinkPreset {
pub name: String,
pub values: BTreeMap<String, serde_json::Value>,
}
impl Fill {
+2 -2
View File
@@ -15,8 +15,8 @@ pub mod project;
pub use blend::{BlendMode, blend1, blend3, lum, mix3};
pub use channel::{Channel, ChannelFormat, ChannelKind};
pub use layer::{
ChannelBlend, CompareOp, Decal, Effect, EffectKind, Fill, Filter, Generator, GeneratorKind, HeightMask, Layer, LayerId, LayerKind, Levels, Mask,
MaskBackground, Procedural, ProceduralKind, Projection, Source, gradient, hsl_adjust, per_unit,
ChannelBlend, CompareOp, Decal, Effect, EffectKind, Fill, Filter, Generator, GeneratorKind, HeightMask, Layer, LayerId, LayerKind, Levels, LinkParam,
LinkPreset, Mask, MaskBackground, MaterialLink, Procedural, ProceduralKind, Projection, Source, gradient, hsl_adjust, per_unit,
};
pub use project::{
AoMixing, BakeMatch, BakeSettings, BakedMap, Camera, DepthOfField, DirectionMode, DisplaySettings, FORMAT_VERSION, MAX_RESOLUTION, MIN_RESOLUTION,
+24
View File
@@ -85,6 +85,30 @@ impl BakedMap {
BakedMap::WorldDirection => "world_space_direction",
}
}
/// The identifier shared with MaterialCraft (graph inputs fed from this map).
pub fn interop_id(self) -> &'static str {
match self {
BakedMap::Normal => "normal",
BakedMap::WorldNormal => "worldNormal",
BakedMap::Id => "id",
BakedMap::AmbientOcclusion => "ambientOcclusion",
BakedMap::Curvature => "curvature",
BakedMap::Position => "position",
BakedMap::Thickness => "thickness",
BakedMap::Height => "height",
BakedMap::BentNormal => "bentNormal",
BakedMap::Opacity => "opacity",
BakedMap::ColorMap => "colorMap",
BakedMap::WorldDirection => "worldDirection",
}
}
/// The map a MaterialCraft graph input `identifier` reads: its shared or own id, ignoring
/// case and underscores (`ambient_occlusion` = `ambientOcclusion`).
pub fn for_input(identifier: &str) -> Option<BakedMap> {
let norm = |x: &str| x.to_ascii_lowercase().replace(['_', '-', ' '], "");
let want = norm(identifier);
BakedMap::ALL.into_iter().find(|m| norm(m.interop_id()) == want || norm(m.id()) == want)
}
/// Components stored (3 for vector maps, 1 for scalar maps).
pub fn components(self) -> usize {
match self {
+1
View File
@@ -24,3 +24,4 @@ thiserror = { workspace = true }
[dev-dependencies]
image = { workspace = true }
zip = { workspace = true }
+1
View File
@@ -75,6 +75,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
v.extend(crate::preset_cmds::specs());
v.extend(crate::resource_cmds::specs());
v.extend(crate::material_lib::specs());
v.extend(crate::link_cmds::specs());
v.extend(crate::smart_masks::specs());
v.extend(crate::filter_lib::specs());
v.extend(crate::decal_cmds::specs());
+3
View File
@@ -26,6 +26,9 @@ fn fill_json(f: &Fill) -> Value {
if let Projection::Decal(d) = &f.projection {
v["decal"] = json!(d);
}
if let Some(link) = &f.link {
v["link"] = json!(link);
}
v
}
+1
View File
@@ -21,6 +21,7 @@ pub mod inspect;
pub mod instance_cmds;
pub mod layer_cmds;
pub mod layer_ops;
pub mod link_cmds;
pub mod mask_cmds;
pub mod material_lib;
pub mod materials;
+440
View File
@@ -0,0 +1,440 @@
//! MaterialCraft materials as fill layers. A `.mcmat` portable material becomes a fill whose
//! channel sources are its cooked maps (image resources) and whose [`MaterialLink`] keeps the
//! graph's exposed parameters. Changing one re-cooks the maps through `materialcraft-cli` at the
//! texture set's resolution, with the set's baked mesh maps feeding graph inputs of the same name.
//! The apps share the file format and the command line, not code.
use std::sync::Arc;
use serde_json::{Map, Value, json};
use texturecraft_core::{BakedMap, Channel, Fill, Layer, LayerId, LayerKind, LinkParam, MaterialLink, Source};
use texturecraft_formats::mcmat::{MCMAT_EXT, MCRAFT_EXT, PortableMaterial, is_mcraft, read_mcmat};
use crate::commands::{CommandSpec, bad, boolp, has_layer, layer_param, strp};
use crate::materials::Place;
use crate::{EngineError, Result, Session, cmd};
/// The undo label of parameter edits and re-cooks (a re-cook merges into the edit before it).
const EDIT: &str = "Edit MaterialCraft material";
pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!(
"material.link.set",
"Set material parameter",
[],
None,
"{layer?, id, value (number, [..], bool, \"#rrggbb\" or {t, v}), cook?: true} — sets an exposed input of a MaterialCraft material layer and re-cooks its maps (cook: false only stores the value)",
has_layer,
set
),
cmd!(
"material.link.preset",
"Apply material preset",
[],
None,
"{layer?, name, cook?: true} — applies a preset of a MaterialCraft material layer",
has_layer,
preset
),
cmd!(
"material.link.cook",
"Re-cook MaterialCraft material",
[],
None,
"{layer?} — re-cooks a MaterialCraft material layer at its texture set's resolution with the set's baked mesh maps",
has_layer,
|s, p| {
let id = layer_param(s, "material.link.cook", p)?;
cook(s, id, Map::new())
}
),
cmd!(
"material.link.saveSource",
"Save MaterialCraft package",
[],
None,
"{layer?, path} — writes the MaterialCraft package (.mcraft) behind a material layer, to open in MaterialCraft",
has_layer,
save_source
),
]
}
/// The link of fill layer `id`.
fn link_of(s: &Session, cmd: &str, id: LayerId) -> Result<MaterialLink> {
match &s.doc.project.layer(id)?.kind {
LayerKind::Fill(Fill { link: Some(link), .. }) => Ok(link.as_ref().clone()),
_ => Err(bad(cmd, format!("layer {} is not a MaterialCraft material", id.0))),
}
}
/// Value types other apps may set (MaterialCraft's portable inputs), with their component count.
fn components(t: &str) -> Option<usize> {
Some(match t {
"float" | "int" | "enum" | "bool" => 1,
"float2" | "int2" => 2,
"float3" => 3,
"float4" | "color" => 4,
_ => return None,
})
}
fn type_of(v: &Value) -> &str {
v.get("t").and_then(Value::as_str).unwrap_or("")
}
/// `v` (plain or `{t, v}`) as a typed value for `param`, with finite numbers.
pub fn typed(param: &LinkParam, v: &Value) -> Option<Value> {
let t = type_of(&param.default);
let n = components(t)?;
let payload = if v.get("t").is_some() { v.get("v")? } else { v };
let num = |x: &Value| x.as_f64().filter(|f| f.is_finite());
let payload = match (t, payload) {
("bool", Value::Bool(b)) => json!(b),
("bool", x) => json!(num(x)? != 0.0),
("color", Value::String(hex)) => {
let h = hex.trim_start_matches('#');
let c = |i: usize| Some(f64::from(u8::from_str_radix(h.get(i..i + 2)?, 16).ok()?) / 255.0);
// Hex colors are sRGB; values are linear.
let lin = |x: f64| if x <= 0.04045 { x / 12.92 } else { ((x + 0.055) / 1.055).powf(2.4) };
json!([lin(c(0)?), lin(c(2)?), lin(c(4)?), 1.0])
}
(_, x) if n == 1 => {
let f = num(x)?;
if matches!(t, "int" | "enum") { json!(f.round() as i64) } else { json!(f) }
}
(_, Value::Array(a)) if a.len() >= n.min(3) => {
let mut out: Vec<f64> = a.iter().take(n).map(num).collect::<Option<_>>()?;
out.resize(n, 1.0);
if t == "int2" { json!(out.iter().map(|f| f.round() as i64).collect::<Vec<_>>()) } else { json!(out) }
}
_ => return None,
};
Some(json!({"t": t, "v": payload}))
}
/// `overrides` checked against the link's parameters.
fn checked(cmd: &str, link: &MaterialLink, overrides: &Map<String, Value>) -> Result<Map<String, Value>> {
overrides
.iter()
.map(|(id, v)| {
let p = link.params.iter().find(|p| p.id == *id).ok_or_else(|| bad(cmd, format!("`{}` has no parameter `{id}`", link.name)))?;
Ok((id.clone(), typed(p, v).ok_or_else(|| bad(cmd, format!("`{id}` takes a {}", type_of(&p.default))))?))
})
.collect()
}
fn set(s: &mut Session, p: &Value) -> Result<Value> {
const CMD: &str = "material.link.set";
let id = layer_param(s, CMD, p)?;
let pid = strp(p, "id").ok_or_else(|| bad(CMD, "`id` is required"))?;
let value = p.get("value").ok_or_else(|| bad(CMD, "`value` is required"))?;
update(s, CMD, id, Map::from_iter([(pid.to_string(), value.clone())]), boolp(p, "cook").unwrap_or(true))
}
fn preset(s: &mut Session, p: &Value) -> Result<Value> {
const CMD: &str = "material.link.preset";
let id = layer_param(s, CMD, p)?;
let name = strp(p, "name").ok_or_else(|| bad(CMD, "`name` is required"))?;
let link = link_of(s, CMD, id)?;
let values = link.presets.iter().find(|pr| pr.name == name).ok_or_else(|| bad(CMD, format!("`{}` has no preset `{name}`", link.name)))?.values.clone();
// Presets may name inputs the painter can't set; those stay as cooked.
let values = values.into_iter().filter(|(k, _)| link.params.iter().any(|p| p.id == *k));
update(s, CMD, id, values, boolp(p, "cook").unwrap_or(true))
}
/// Set parameter values of linked layer `id`; with `cook`, re-cook now (one undo step).
fn update(s: &mut Session, cmd: &str, id: LayerId, overrides: impl IntoIterator<Item = (String, Value)>, cook_now: bool) -> Result<Value> {
let link = link_of(s, cmd, id)?;
let values = checked(cmd, &link, &overrides.into_iter().collect::<Map<_, _>>())?;
if cook_now {
return cook(s, id, values);
}
s.edit(EDIT, |s| {
if let LayerKind::Fill(Fill { link: Some(link), .. }) = &mut s.doc.project.layer_mut(id)?.kind {
for p in &mut link.params {
if let Some(v) = values.get(&p.id) {
p.value = v.clone();
}
}
}
Ok(())
})?;
Ok(json!({"layer": id.0, "cooked": false}))
}
fn cook(s: &mut Session, id: LayerId, overrides: Map<String, Value>) -> Result<Value> {
let job = s.link_job(id, &overrides)?;
let bytes = job.run()?;
s.apply_link_cook(id, &bytes, false)?;
Ok(json!({"layer": id.0, "cooked": true, "size": job.size}))
}
#[cfg(not(target_arch = "wasm32"))]
fn save_source(s: &mut Session, p: &Value) -> Result<Value> {
const CMD: &str = "material.link.saveSource";
let id = layer_param(s, CMD, p)?;
let path = strp(p, "path").ok_or_else(|| bad(CMD, "`path` is required"))?;
let path = if path.to_ascii_lowercase().ends_with(&format!(".{MCRAFT_EXT}")) { path.to_string() } else { format!("{path}.{MCRAFT_EXT}") };
let bytes = s.link_package(id)?;
texturecraft_formats::write_atomic(std::path::Path::new(&path), &bytes).map_err(|e| bad(CMD, e.to_string()))?;
Ok(json!({"path": path}))
}
#[cfg(target_arch = "wasm32")]
fn save_source(_: &mut Session, _: &Value) -> Result<Value> {
Err(EngineError::Other("file paths are not available in the browser".into()))
}
/// A re-cook of a linked layer, to run anywhere (a worker thread) and hand back to
/// [`Session::apply_link_cook`].
#[derive(Clone, Debug)]
pub struct LinkJob {
pub layer: LayerId,
pub package: Arc<Vec<u8>>,
/// Pixels across (the texture set's resolution).
pub size: u32,
/// Every parameter's value, by id, in MaterialCraft's typed form.
pub values: Map<String, Value>,
/// Graph inputs fed from baked mesh maps: identifier → 16-bit PNG.
pub inputs: Vec<(String, Vec<u8>)>,
}
#[cfg(not(target_arch = "wasm32"))]
static NEXT_JOB: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// A MaterialCraft executable: `$<env>`, else next to this program, else `name` on `PATH`.
#[cfg(not(target_arch = "wasm32"))]
pub fn materialcraft_tool(name: &str, env: &str) -> std::path::PathBuf {
if let Some(p) = std::env::var_os(env).filter(|p| !p.is_empty()) {
return p.into();
}
let exe = format!("{name}{}", std::env::consts::EXE_SUFFIX);
std::env::current_exe().ok().and_then(|e| Some(e.parent()?.join(&exe))).filter(|p| p.is_file()).unwrap_or_else(|| exe.into())
}
/// A command for a MaterialCraft executable that opens no console window.
#[cfg(not(target_arch = "wasm32"))]
fn quiet(program: &std::path::Path) -> std::process::Command {
#[allow(unused_mut)]
let mut c = std::process::Command::new(program);
#[cfg(windows)]
std::os::windows::process::CommandExt::creation_flags(&mut c, 0x0800_0000); // CREATE_NO_WINDOW
c
}
/// Open `package` (a `.mcraft` file) in the MaterialCraft app (`$MATERIALCRAFT`, next to this
/// program, or on `PATH`).
#[cfg(not(target_arch = "wasm32"))]
pub fn open_in_materialcraft(package: &std::path::Path) -> Result<()> {
let app = materialcraft_tool("materialcraft", "MATERIALCRAFT");
quiet(&app).arg(package).spawn().map(drop).map_err(|e| {
EngineError::Other(format!("MaterialCraft ({}) did not start: {e}. Install MaterialCraft next to TextureCraft or set MATERIALCRAFT", app.display()))
})
}
impl LinkJob {
/// Cook through `materialcraft-cli` (`$MATERIALCRAFT_CLI`, next to this program, or on
/// `PATH`); returns `.mcmat` bytes. Blocks for the cook (about a second at 2K).
#[cfg(not(target_arch = "wasm32"))]
pub fn run(&self) -> Result<Vec<u8>> {
let other = |e: String| EngineError::Other(e);
let n = NEXT_JOB.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!("texturecraft-link-{}-{n}", std::process::id()));
std::fs::create_dir_all(&dir).map_err(|e| other(format!("{}: {e}", dir.display())))?;
let result = (|| {
let utf8 = |p: &std::path::Path| p.to_str().map(str::to_string).ok_or_else(|| other(format!("{} is not a UTF-8 path", p.display())));
let write = |name: &str, bytes: &[u8]| {
let p = dir.join(name);
std::fs::write(&p, bytes).map_err(|e| other(format!("{name}: {e}")))?;
utf8(&p)
};
let src = write("source.mcraft", &self.package)?;
let mut inputs = Map::new();
for (i, (id, png)) in self.inputs.iter().enumerate() {
inputs.insert(id.clone(), json!(write(&format!("input{i}.png"), png)?));
}
let out = dir.join(format!("out.{MCMAT_EXT}"));
let params = json!({"path": utf8(&out)?, "size": self.size, "values": self.values, "inputs": inputs});
let cli = materialcraft_tool("materialcraft-cli", "MATERIALCRAFT_CLI");
let run = quiet(&cli).arg("run").arg(&src).arg("file.exportMaterial").arg(params.to_string()).output().map_err(|e| {
other(format!(
"MaterialCraft's command line ({}) did not start: {e}. Install MaterialCraft next to TextureCraft or set MATERIALCRAFT_CLI",
cli.display()
))
})?;
if !run.status.success() {
return Err(other(format!("MaterialCraft could not cook the material: {}", String::from_utf8_lossy(&run.stderr).trim())));
}
std::fs::read(&out).map_err(|e| other(format!("{}: {e}", out.display())))
})();
let _ = std::fs::remove_dir_all(&dir);
result
}
#[cfg(target_arch = "wasm32")]
pub fn run(&self) -> Result<Vec<u8>> {
Err(EngineError::Other("re-cooking a MaterialCraft material needs its command line, which the browser can't run".into()))
}
}
/// Decode every map of `m` (validating them) with the channel it feeds.
fn decoded_maps(m: &PortableMaterial) -> Result<Vec<(Channel, Vec<u8>, texturecraft_formats::DecodedImage)>> {
m.maps
.iter()
.filter_map(|(usage, bytes)| Some((Channel::ALL.into_iter().find(|c| c.id() == usage)?, bytes)))
.map(|(c, bytes)| {
let img = texturecraft_formats::decode_image(bytes).map_err(|e| EngineError::Other(format!("{} map: {e}", c.id())))?;
Ok((c, bytes.clone(), img))
})
.collect()
}
impl Session {
/// Add a MaterialCraft material (`.mcmat` bytes, or a `.mcraft` package cooked first) as a
/// fill layer at `place` (one undo step). Returns the new layer.
pub fn add_linked_material_bytes_at(&mut self, bytes: &[u8], place: Place) -> Result<LayerId> {
let cooked;
let bytes = if is_mcraft(bytes) {
let ts = self.ts();
let size = self.doc.project.texture_set(ts)?.resolution;
let job = LinkJob { layer: LayerId(0), package: Arc::new(bytes.to_vec()), size, values: Map::new(), inputs: Vec::new() };
cooked = job.run()?;
cooked.as_slice()
} else {
bytes
};
let m = read_mcmat(bytes).map_err(|e| EngineError::Other(e.to_string()))?;
let maps = decoded_maps(&m)?;
if maps.is_empty() {
return Err(EngineError::Other(format!("{} has no maps TextureCraft can paint with", m.manifest.name)));
}
let label = format!("Add {}", m.manifest.name);
let (id, names) = self.edit(&label, |s| {
let ts = s.ts();
let stem = texturecraft_formats::sanitize_resource_name(&m.manifest.name);
let package = unique(s, &format!("{stem}.{MCRAFT_EXT}"));
s.doc.resources.insert(package.clone(), Arc::new(m.source.clone()));
let link = MaterialLink {
name: m.manifest.name.clone(),
package,
graph: m.manifest.graph.clone(),
params: m.manifest.parameters.iter().filter(|p| components(type_of(&p.default)).is_some()).cloned().collect(),
presets: m.manifest.presets.clone(),
inputs: m.manifest.inputs.iter().map(|i| i.identifier.clone()).collect(),
size: m.manifest.size[0],
};
let mut fill = Fill { link: Some(Box::new(link)), ..Fill::default() };
let names = put_maps(s, &stem, &maps, &mut fill);
for c in fill.sources.keys() {
crate::set_cmds::add_channel(s, ts, *c)?;
}
let channels: Vec<Channel> = fill.sources.keys().copied().collect();
let lid = s.doc.project.alloc_id();
let name = s.doc.project.next_layer_name(ts, &m.manifest.name);
let lid = crate::layer_cmds::insert_selected(s, Layer::new(lid, name, LayerKind::Fill(fill), &channels))?;
place.apply(s, lid)?;
s.selected.insert(ts, lid);
s.target = crate::session::StackTarget::Content;
Ok((lid, names))
})?;
self.cache_decoded(names, maps);
Ok(id)
}
/// The `.mcraft` package behind linked layer `id`.
pub fn link_package(&self, id: LayerId) -> Result<Arc<Vec<u8>>> {
let link = link_of(self, "material.link", id)?;
self.doc.resources.get(&link.package).cloned().ok_or_else(|| EngineError::Other(format!("the package of {} is missing", link.name)))
}
/// The parameter values of linked layer `id`, by id.
pub fn link_values(&self, id: LayerId) -> Result<Map<String, Value>> {
Ok(link_of(self, "material.link", id)?.params.iter().map(|p| (p.id.clone(), p.value.clone())).collect())
}
/// The re-cook of linked layer `id` with `overrides` over its values, at its texture set's
/// resolution, with the set's baked mesh maps for matching graph inputs.
pub fn link_job(&self, id: LayerId, overrides: &Map<String, Value>) -> Result<LinkJob> {
let link = link_of(self, "material.link", id)?;
let (ts, _, _) = self.doc.project.locate(id).ok_or_else(|| EngineError::Other(format!("layer {} is not in a stack", id.0)))?;
let mut values = self.link_values(id)?;
values.extend(overrides.clone());
let mut inputs = Vec::new();
if let Some(baked) = self.doc.baked.get(&ts) {
for ident in &link.inputs {
let Some((m, data)) = BakedMap::for_input(ident).and_then(|m| Some((m, baked.maps.get(&m)?))) else { continue };
let png = texturecraft_formats::mcmat::mesh_map_png(baked.resolution, m.components(), data).map_err(|e| EngineError::Other(e.to_string()))?;
inputs.push((ident.clone(), png));
}
}
// shortcut: one cook per texture set at its base resolution; UDIM tiles share it.
let size = self.doc.project.texture_set(ts)?.resolution;
Ok(LinkJob { layer: id, package: self.link_package(id)?, size, values, inputs })
}
/// Swap in maps a [`LinkJob`] cooked (`.mcmat` bytes) and the values they were cooked with.
/// With `merge`, this folds into the previous undo step when that was an edit of the same kind
/// (a parameter set while the cook ran).
pub fn apply_link_cook(&mut self, id: LayerId, bytes: &[u8], merge: bool) -> Result<()> {
let m = read_mcmat(bytes).map_err(|e| EngineError::Other(e.to_string()))?;
let maps = decoded_maps(&m)?;
let names = self.edit_merging(EDIT, merge, |s| {
let LayerKind::Fill(mut fill) = s.doc.project.layer(id)?.kind.clone() else {
return Err(EngineError::Other(format!("layer {} is not a fill", id.0)));
};
let link = fill.link.as_mut().ok_or_else(|| EngineError::Other(format!("layer {} is not a MaterialCraft material", id.0)))?;
for p in &mut link.params {
if let Some(cooked) = m.manifest.parameters.iter().find(|c| c.id == p.id) {
p.value = cooked.value.clone();
}
}
link.size = m.manifest.size[0];
let stem = texturecraft_formats::sanitize_resource_name(&link.name);
let old: Vec<String> = fill.sources.values().filter_map(|src| if let Source::Image { name } = src { Some(name.clone()) } else { None }).collect();
let names = put_maps(s, &stem, &maps, &mut fill);
s.doc.project.layer_mut(id)?.kind = LayerKind::Fill(fill);
// Old maps go unless another layer (a duplicate) still shows them.
let mut used = Vec::new();
for set in 0..s.doc.project.texture_sets.len() {
for root in s.doc.project.texture_sets.get(set).map(|t| t.stack.clone()).unwrap_or_default() {
crate::material_lib::images_used(&s.doc.project, root, &mut used, 0);
}
}
for name in old.iter().filter(|n| !used.contains(n)) {
s.doc.resources.remove(name);
}
Ok(names)
})?;
self.sync_images();
self.cache_decoded(names, maps);
Ok(())
}
/// Keep the images decoded while validating maps instead of decoding them again.
fn cache_decoded(&mut self, names: Vec<String>, maps: Vec<(Channel, Vec<u8>, texturecraft_formats::DecodedImage)>) {
for (name, (_, _, img)) in names.into_iter().zip(maps) {
self.images.insert(name, crate::resource_cmds::decoded(img));
}
}
}
/// A resource name not in use: `name`, else `2_name`, `3_name`…
fn unique(s: &Session, name: &str) -> String {
(1..).map(|n| if n == 1 { name.to_string() } else { format!("{n}_{name}") }).find(|n| !s.doc.resources.contains_key(n)).unwrap_or_else(|| name.to_string())
}
/// Store `maps` as new resources (names carry a fresh id, so the decoded-image cache never
/// confuses two cooks) and point `fill`'s channels at them. Returns the names, in map order.
fn put_maps(s: &mut Session, stem: &str, maps: &[(Channel, Vec<u8>, texturecraft_formats::DecodedImage)], fill: &mut Fill) -> Vec<String> {
let cook = s.doc.project.alloc_id().0;
let mut names = Vec::new();
for (c, bytes, _) in maps {
let name = unique(s, &format!("{stem}_{}_{cook}.png", c.id()));
s.doc.resources.insert(name.clone(), Arc::new(bytes.clone()));
fill.sources.insert(*c, Source::Image { name: name.clone() });
names.push(name);
}
names
}
+7 -2
View File
@@ -25,7 +25,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Add smart material from file",
[],
None,
"{path: .tcmat file, above?: layer id, inside?: bool, index?: n} — above the selection by default, or placed like material.apply",
"{path: .tcmat smart material, .mcmat MaterialCraft material or .mcraft package, above?: layer id, inside?: bool, index?: n} — above the selection by default, or placed like material.apply",
always,
load
),
@@ -48,6 +48,7 @@ pub(crate) fn images_used(p: &Project, id: LayerId, out: &mut Vec<String>, depth
out.push(name.clone());
}
}
out.extend(f.link.as_ref().map(|link| link.package.clone()));
}
for e in l.mask.iter().flat_map(|m| m.effects.iter()) {
if let EffectKind::Fill { source: Source::Image { name }, .. } = &e.kind {
@@ -96,8 +97,12 @@ impl Session {
self.add_smart_material_bytes_at(bytes, Place::Selection)
}
/// [`Session::add_smart_material_bytes`] at `place` (shelf drag and drop).
/// [`Session::add_smart_material_bytes`] at `place` (shelf drag and drop). MaterialCraft
/// portable materials and packages become linked fill layers.
pub fn add_smart_material_bytes_at(&mut self, bytes: &[u8], place: Place) -> Result<LayerId> {
if texturecraft_formats::mcmat::is_mcmat(bytes) || texturecraft_formats::mcmat::is_mcraft(bytes) {
return self.add_linked_material_bytes_at(bytes, place);
}
let pf = texturecraft_formats::load_project_bytes(bytes).map_err(fe)?;
let root = pf
.project
+1 -1
View File
@@ -201,7 +201,7 @@ impl Builder<'_> {
let id = self.p.alloc_id();
let enabled: Vec<Channel> = sources.iter().map(|(c, _)| *c).filter(|c| self.channels.contains(c)).collect();
let projection = Projection::Triplanar { scale: 1.0, sharpness: 8.0, offset: [0.0; 3] };
Layer::new(id, name, LayerKind::Fill(Fill { sources: sources.into_iter().collect(), projection }), &enabled)
Layer::new(id, name, LayerKind::Fill(Fill { sources: sources.into_iter().collect(), projection, link: None }), &enabled)
}
/// A fill masked by smart mask preset `mask` (inverted: present where the preset is not).
+8 -2
View File
@@ -63,8 +63,14 @@ impl Session {
pub fn sync_images(&mut self) {
let keep: Vec<String> = self.doc.resources.keys().cloned().collect();
self.images.retain(|k, _| keep.contains(k));
let missing: Vec<(String, Arc<Vec<u8>>)> =
self.doc.resources.iter().filter(|(k, _)| !self.images.contains_key(*k)).map(|(k, v)| (k.clone(), v.clone())).collect();
// Linked materials' packages are resources too, not images.
let missing: Vec<(String, Arc<Vec<u8>>)> = self
.doc
.resources
.iter()
.filter(|(k, _)| !self.images.contains_key(*k) && !k.ends_with(&format!(".{}", texturecraft_formats::mcmat::MCRAFT_EXT)))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
for (k, bytes) in missing {
match texturecraft_formats::decode_image(&bytes) {
Ok(d) => {
+201
View File
@@ -0,0 +1,201 @@
//! MaterialCraft materials (`.mcmat`) as linked fill layers: import, parameters, re-cooks,
//! undo, mesh-map inputs and saving. Cooking through a real `materialcraft-cli` runs only when
//! `MATERIALCRAFT_CLI` points at one.
use std::io::Write;
use std::sync::Arc;
use serde_json::{Value, json};
use texturecraft_engine::compose::BakedMaps;
use texturecraft_engine::core::{BakedMap, Channel, LayerId, LayerKind, Source};
use texturecraft_engine::formats::mcmat::mesh_map_png;
use texturecraft_engine::geom::primitives;
use texturecraft_engine::materials::Place;
use texturecraft_engine::{Session, execute};
fn session() -> Session {
let mut s = Session::new_project("t", primitives::sphere(1.0, 32, 16), "builtin:sphere", None, 128).unwrap();
s.update();
s
}
/// A `.mcmat` as MaterialCraft writes it: uniform maps, one float input `wear` cooked at `wear`.
fn mcmat(roughness: f32, wear: f32) -> Vec<u8> {
let gray = |v: f32| mesh_map_png(4, 1, &[v; 16]).unwrap();
let manifest = json!({
"format": "craft-material", "version": 1, "name": "Brick wall", "graph": "Brick_wall", "size": [4, 4],
"maps": [
{"usage": "baseColor", "file": "maps/baseColor.png", "colorSpace": "srgb"},
{"usage": "roughness", "file": "maps/roughness.png", "colorSpace": "linear"},
{"usage": "ambientOcclusion", "file": "maps/ambientOcclusion.png", "colorSpace": "linear"}
],
"parameters": [
{"id": "wear", "label": "Wear", "default": {"t": "float", "v": 0.5}, "min": 0.0, "max": 1.0, "value": {"t": "float", "v": wear}},
{"id": "tint", "label": "Tint", "default": {"t": "color", "v": [1.0, 1.0, 1.0, 1.0]}, "value": {"t": "color", "v": [1.0, 1.0, 1.0, 1.0]}},
{"id": "note", "label": "Note", "default": {"t": "text", "v": ""}, "value": {"t": "text", "v": ""}}
],
"inputs": [{"identifier": "curvature", "kind": "gray"}],
"presets": [{"name": "Worn", "values": {"wear": {"t": "float", "v": 1.0}, "note": {"t": "text", "v": "x"}}}],
"source": "source.mcraft"
});
let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
let o = zip::write::SimpleFileOptions::default();
for (name, bytes) in [
("material.json", manifest.to_string().into_bytes()),
("maps/baseColor.png", mesh_map_png(4, 3, &[0.5; 48]).unwrap()),
("maps/roughness.png", gray(roughness)),
("maps/ambientOcclusion.png", gray(1.0)),
("source.mcraft", b"package bytes".to_vec()),
] {
z.start_file(name, o).unwrap();
z.write_all(&bytes).unwrap();
}
z.finish().unwrap().into_inner()
}
fn roughness(s: &mut Session) -> f32 {
s.update();
s.caches[0].composite.value(Channel::Roughness, 64, 64)[0]
}
fn link(s: &Session, id: LayerId) -> texturecraft_engine::core::MaterialLink {
match &s.doc.project.layer(id).unwrap().kind {
LayerKind::Fill(f) => f.link.as_deref().unwrap().clone(),
_ => panic!("not a fill"),
}
}
fn map_name(s: &Session, id: LayerId, c: Channel) -> String {
match &s.doc.project.layer(id).unwrap().kind {
LayerKind::Fill(f) => match f.sources.get(&c) {
Some(Source::Image { name }) => name.clone(),
other => panic!("{other:?}"),
},
_ => panic!("not a fill"),
}
}
#[test]
fn imports_as_a_linked_fill_with_its_maps() {
let mut s = session();
let id = s.add_linked_material_bytes_at(&mcmat(0.25, 0.5), Place::Selection).unwrap();
assert_eq!(s.selected_layer(), Some(id));
assert!((roughness(&mut s) - 0.25).abs() < 2e-3);
// The material's channels join the texture set; text inputs are not editable here.
assert!(s.doc.project.texture_sets[0].has_channel(Channel::AmbientOcclusion));
let l = link(&s, id);
assert_eq!(l.graph, "Brick_wall");
assert_eq!(l.params.iter().map(|p| p.id.as_str()).collect::<Vec<_>>(), ["wear", "tint"]);
assert_eq!(l.inputs, ["curvature"]);
assert_eq!(s.doc.resources.get(&l.package).map(|b| b.as_slice()), Some(&b"package bytes"[..]));
assert!(!s.images.contains_key(&l.package), "the package is not decoded as an image");
// Inspect shows the link; one undo removes everything.
let inspect = execute(&mut s, "project.inspect", &Value::Null).unwrap().to_string();
assert!(inspect.contains("Brick_wall"), "{inspect}");
let n = s.doc.resources.len();
execute(&mut s, "edit.undo", &Value::Null).unwrap();
assert!(s.doc.project.layer(id).is_err() && s.doc.resources.len() < n);
// Garbage is an error, not a layer.
assert!(s.add_linked_material_bytes_at(b"PK not really", Place::Selection).is_err());
}
#[test]
fn parameters_are_checked_stored_and_recooked_in_one_undo_step() {
let mut s = session();
let id = s.add_linked_material_bytes_at(&mcmat(0.25, 0.5), Place::Selection).unwrap();
let set = |s: &mut Session, p: Value| execute(s, "material.link.set", &p);
assert!(set(&mut s, json!({"id": "nope", "value": 1, "cook": false})).is_err());
assert!(set(&mut s, json!({"id": "wear", "value": "soft", "cook": false})).is_err());
assert!(set(&mut s, json!({"id": "wear", "value": f64::NAN, "cook": false})).is_err());
set(&mut s, json!({"id": "tint", "value": "#ff0000", "cook": false})).unwrap();
assert_eq!(link(&s, id).params[1].value, json!({"t": "color", "v": [1.0, 0.0, 0.0, 1.0]}));
set(&mut s, json!({"id": "wear", "value": 0.9, "cook": false})).unwrap();
assert_eq!(link(&s, id).params[0].value, json!({"t": "float", "v": 0.9}));
// The job carries every value and the texture set's resolution.
let job = s.link_job(id, &serde_json::Map::new()).unwrap();
assert_eq!(job.size, 128);
assert_eq!(job.values["wear"], json!({"t": "float", "v": 0.9}));
// What the job cooked (here: built by hand) replaces the maps, merged into the edit.
let old = map_name(&s, id, Channel::Roughness);
s.apply_link_cook(id, &mcmat(0.75, 0.9), true).unwrap();
assert!((roughness(&mut s) - 0.75).abs() < 2e-3);
assert!(!s.doc.resources.contains_key(&old), "old maps are dropped");
execute(&mut s, "edit.undo", &Value::Null).unwrap();
assert!((roughness(&mut s) - 0.25).abs() < 2e-3, "undo restores the earlier maps");
assert_eq!(link(&s, id).params[0].value, json!({"t": "float", "v": 0.5}));
execute(&mut s, "edit.redo", &Value::Null).unwrap();
assert!((roughness(&mut s) - 0.75).abs() < 2e-3);
// Presets skip inputs the painter can't set; unknown presets are errors.
assert!(execute(&mut s, "material.link.preset", &json!({"name": "Nope", "cook": false})).is_err());
execute(&mut s, "material.link.preset", &json!({"name": "Worn", "cook": false})).unwrap();
assert_eq!(link(&s, id).params[0].value, json!({"t": "float", "v": 1.0}));
}
#[test]
fn duplicates_keep_their_maps_and_mesh_maps_feed_inputs() {
let mut s = session();
let id = s.add_linked_material_bytes_at(&mcmat(0.25, 0.5), Place::Selection).unwrap();
let dup = LayerId(execute(&mut s, "layer.duplicate", &json!({"layer": id.0})).unwrap()["layer"].as_u64().unwrap());
let shared = map_name(&s, dup, Channel::Roughness);
s.apply_link_cook(id, &mcmat(0.75, 0.5), false).unwrap();
assert!(s.doc.resources.contains_key(&shared), "the duplicate still shows the first cook");
s.doc.baked.insert(0, BakedMaps { resolution: 4, maps: [(BakedMap::Curvature, Arc::new(vec![0.5f32; 16]))].into_iter().collect() });
let job = s.link_job(id, &serde_json::Map::new()).unwrap();
assert_eq!(job.inputs.len(), 1);
assert_eq!(job.inputs[0].0, "curvature");
assert_eq!(BakedMap::for_input("ambient_occlusion"), Some(BakedMap::AmbientOcclusion));
assert_eq!(BakedMap::for_input("worldNormal"), Some(BakedMap::WorldNormal));
}
#[test]
fn projects_save_the_link_and_its_package() {
let mut s = session();
let id = s.add_linked_material_bytes_at(&mcmat(0.25, 0.5), Place::Selection).unwrap();
let back = Session::from_project_bytes(&s.to_project_bytes().unwrap()).unwrap();
assert_eq!(link(&back, id), link(&s, id));
assert!(back.doc.resources.contains_key(&link(&s, id).package));
// Smart materials made from the layer carry the package too.
let tcmat = s.smart_material_bytes(id, "Bricks").unwrap();
let mut other = session();
let copy = other.add_smart_material_bytes(&tcmat).unwrap();
assert!(other.doc.resources.contains_key(&link(&other, copy).package));
}
/// The whole path through a real MaterialCraft: a package cooks on import, then re-cooks.
#[test]
fn cooks_through_materialcraft_cli_when_available() {
let Some(cli) = std::env::var_os("MATERIALCRAFT_CLI") else {
eprintln!("MATERIALCRAFT_CLI is not set: skipping the materialcraft-cli round trip");
return;
};
let dir = std::env::temp_dir().join(format!("tc-mc-link-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let pkg = dir.join("bricks.mcraft");
let ok = std::process::Command::new(&cli).args(["new", "bricks"]).arg(&pkg).args(["--size", "64"]).status().unwrap();
assert!(ok.success());
let exposed = dir.join("exposed.mcraft");
let ok = std::process::Command::new(&cli)
.arg("run")
.arg(&pkg)
.arg("graph.exposed.add")
.arg(r#"{"id": "wear", "default": 0.5}"#)
.arg("-o")
.arg(&exposed)
.status()
.unwrap();
assert!(ok.success());
let mut s = session();
let id = s.add_linked_material_bytes_at(&std::fs::read(&exposed).unwrap(), Place::Selection).unwrap();
assert_eq!(link(&s, id).size, 128, "cooked at the texture set's resolution");
assert!(link(&s, id).params.iter().any(|p| p.id == "wear"));
execute(&mut s, "material.link.set", &json!({"layer": id.0, "id": "wear", "value": 0.8})).unwrap();
assert_eq!(link(&s, id).params.iter().find(|p| p.id == "wear").unwrap().value, json!({"t": "float", "v": 0.8}));
let out = dir.join("back.mcraft");
execute(&mut s, "material.link.saveSource", &json!({"layer": id.0, "path": out.to_str().unwrap()})).unwrap();
assert!(std::fs::metadata(&out).unwrap().len() > 0);
let _ = std::fs::remove_dir_all(&dir);
}
+3
View File
@@ -13,6 +13,8 @@
//! name templates, channel packing, virtual maps such as the final normal, padding, resizing)
//! to PNG, JPEG, TGA, TIFF, BMP, WebP, EXR and HDR bytes.
//! - [`materialx`]: a MaterialX (`.mtlx`) OpenPBR document for an exported texture set.
//! - [`mcmat`]: MaterialCraft portable materials (`.mcmat`) and mesh maps as 16-bit PNGs for
//! MaterialCraft graph inputs.
//! - [`presets`]: the built-in export presets and JSON (de)serialization of user presets.
//! - [`project_file`]: the `.tcraft` project zip (project JSON, sparse lossless pixel tiles,
//! baked maps, the original mesh and a thumbnail).
@@ -29,6 +31,7 @@ pub mod fonts;
pub mod image_res;
pub mod materialx;
pub mod materialx_read;
pub mod mcmat;
pub mod mesh_export;
pub mod mesh_import;
pub mod presets;
+179
View File
@@ -0,0 +1,179 @@
//! MaterialCraft portable materials (`.mcmat`, read only): a zip with `material.json` (the
//! manifest: maps by usage, exposed parameters, image inputs, presets), `maps/<usage>.png`
//! (8-bit sRGB for color usages, 16-bit linear otherwise; OpenGL normals) and the source
//! `.mcraft` package. TextureCraft keeps its own reader (the apps share the format, not code).
//!
//! Also the 16-bit PNG encoder that hands baked mesh maps to MaterialCraft as graph inputs.
use std::io::{Cursor, Read};
use serde::Deserialize;
use texturecraft_core::{LinkParam, LinkPreset};
use crate::{FormatError, Result};
/// The portable material extension.
pub const MCMAT_EXT: &str = "mcmat";
/// MaterialCraft's package extension.
pub const MCRAFT_EXT: &str = "mcraft";
const FORMAT: &str = "craft-material";
const VERSION: u32 = 1;
/// Largest entry read (maps are at most 8192² × 16 bits, compressed).
const MAX_ENTRY: u64 = 512 << 20;
const KIND: &str = "MaterialCraft material";
#[derive(Clone, Debug, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Manifest {
pub format: String,
pub version: u32,
pub name: String,
pub graph: String,
pub size: [u32; 2],
pub maps: Vec<MapEntry>,
#[serde(default)]
pub parameters: Vec<LinkParam>,
#[serde(default)]
pub inputs: Vec<ImageInput>,
#[serde(default)]
pub presets: Vec<LinkPreset>,
pub source: String,
}
#[derive(Clone, Debug, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MapEntry {
/// `baseColor`, `normal`, `roughness`, `metallic`, `height`, `ambientOcclusion`, `emissive`,
/// `opacity` or `specular`.
pub usage: String,
pub file: String,
}
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct ImageInput {
pub identifier: String,
}
/// A read `.mcmat`: the manifest, each map's PNG bytes by usage, the source package.
#[derive(Clone, Debug)]
pub struct PortableMaterial {
pub manifest: Manifest,
pub maps: Vec<(String, Vec<u8>)>,
pub source: Vec<u8>,
}
fn malformed(msg: impl Into<String>) -> FormatError {
FormatError::Malformed { kind: KIND, msg: msg.into() }
}
/// Read `.mcmat` bytes.
pub fn read_mcmat(bytes: &[u8]) -> Result<PortableMaterial> {
let mut z = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| malformed(e.to_string()))?;
let mut read = |name: &str| -> Result<Vec<u8>> {
let mut out = Vec::new();
let f = z.by_name(name).map_err(|_| malformed(format!("{name} is missing")))?;
f.take(MAX_ENTRY + 1).read_to_end(&mut out).map_err(|e| malformed(format!("{name}: {e}")))?;
if out.len() as u64 > MAX_ENTRY {
return Err(FormatError::TooLarge(name.to_string()));
}
Ok(out)
};
let manifest: Manifest = serde_json::from_slice(&read("material.json")?).map_err(|e| malformed(format!("material.json: {e}")))?;
if manifest.format != FORMAT {
return Err(malformed(format!("format `{}`", manifest.format)));
}
if manifest.version > VERSION {
return Err(FormatError::Unsupported(format!("{KIND} version {} (this version reads up to {VERSION})", manifest.version)));
}
let maps = manifest.maps.iter().map(|m| Ok((m.usage.clone(), read(&m.file)?))).collect::<Result<Vec<_>>>()?;
let source = read(&manifest.source)?;
Ok(PortableMaterial { manifest, maps, source })
}
/// Whether `bytes` are a zip holding `entry` (`material.json`: a portable material,
/// `package.json`: a MaterialCraft package).
fn zip_has(bytes: &[u8], entry: &str) -> bool {
zip::ZipArchive::new(Cursor::new(bytes)).is_ok_and(|mut z| z.by_name(entry).is_ok())
}
pub fn is_mcmat(bytes: &[u8]) -> bool {
zip_has(bytes, "material.json")
}
pub fn is_mcraft(bytes: &[u8]) -> bool {
zip_has(bytes, "package.json")
}
/// A baked mesh map (`components` floats per texel, 0..1, rows top first) as a 16-bit PNG
/// (grayscale for one component, RGB for three).
pub fn mesh_map_png(width: u32, components: usize, data: &[f32]) -> Result<Vec<u8>> {
let per_row = (width as usize).saturating_mul(components);
let height = data.len().checked_div(per_row).unwrap_or(0) as u32;
if height == 0 || !matches!(components, 1 | 3) {
return Err(FormatError::Invalid(format!("mesh map of {} values, {width} wide, {components} components", data.len())));
}
let px: Vec<u16> = data.iter().take(per_row * height as usize).map(|v| (v.clamp(0.0, 1.0) * 65535.0).round() as u16).collect();
let img = if components == 1 {
image::ImageBuffer::<image::Luma<u16>, _>::from_raw(width, height, px).map(image::DynamicImage::ImageLuma16)
} else {
image::ImageBuffer::<image::Rgb<u16>, _>::from_raw(width, height, px).map(image::DynamicImage::ImageRgb16)
}
.ok_or_else(|| FormatError::Invalid("mesh map size".into()))?;
let mut out = Vec::new();
img.write_to(&mut Cursor::new(&mut out), image::ImageFormat::Png).map_err(|e| FormatError::Invalid(e.to_string()))?;
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
/// A `.mcmat` as MaterialCraft writes it (see its `formats/src/material.rs`).
pub(crate) fn sample(version: u32) -> Vec<u8> {
let manifest = serde_json::json!({
"format": "craft-material", "version": version, "name": "Brick wall", "graph": "Brick_wall", "size": [4, 4],
"maps": [{"usage": "baseColor", "file": "maps/baseColor.png", "colorSpace": "srgb"}],
"parameters": [{"id": "wear", "label": "Wear", "default": {"t": "float", "v": 0.5}, "min": 0.0, "max": 1.0, "value": {"t": "float", "v": 0.25}}],
"inputs": [{"identifier": "curvature", "kind": "gray"}],
"presets": [{"name": "Worn", "values": {"wear": {"t": "float", "v": 1.0}}}],
"source": "source.mcraft", "futureField": true
});
let mut z = zip::ZipWriter::new(Cursor::new(Vec::new()));
let o = zip::write::SimpleFileOptions::default();
z.start_file("material.json", o).unwrap();
z.write_all(manifest.to_string().as_bytes()).unwrap();
z.start_file("maps/baseColor.png", o).unwrap();
z.write_all(&mesh_map_png(4, 3, &[0.5; 48]).unwrap()).unwrap();
z.start_file("source.mcraft", o).unwrap();
z.write_all(b"pkg").unwrap();
z.finish().unwrap().into_inner()
}
#[test]
fn reads_manifest_maps_and_source() {
let bytes = sample(1);
assert!(is_mcmat(&bytes) && !is_mcraft(&bytes));
let m = read_mcmat(&bytes).unwrap();
assert_eq!(m.manifest.graph, "Brick_wall");
assert_eq!(m.manifest.parameters[0].id, "wear");
assert_eq!(m.manifest.parameters[0].value, serde_json::json!({"t": "float", "v": 0.25}));
assert_eq!(m.manifest.presets[0].name, "Worn");
assert_eq!(m.manifest.inputs[0].identifier, "curvature");
assert_eq!(m.maps[0].0, "baseColor");
assert_eq!(crate::decode_image(&m.maps[0].1).unwrap().width, 4);
assert_eq!(m.source, b"pkg");
assert!(matches!(read_mcmat(&sample(2)), Err(FormatError::Unsupported(_))));
assert!(read_mcmat(b"junk").is_err());
}
#[test]
fn mesh_maps_encode_as_16_bit() {
let png = mesh_map_png(2, 1, &[0.0, 0.25, 0.5, 1.0]).unwrap();
let d = crate::decode_image(&png).unwrap();
assert_eq!((d.width, d.height), (2, 2));
assert!((d.rgba[1][0] - 0.25).abs() < 1e-4);
assert!(mesh_map_png(0, 1, &[]).is_err());
assert!(mesh_map_png(2, 2, &[0.0; 8]).is_err());
}
}
+124 -1
View File
@@ -5,7 +5,8 @@ use egui::{Color32, Rect, RichText, Sense, Stroke, Ui, pos2, vec2};
use egui_phosphor::regular as ic;
use serde_json::{Value, json};
use texturecraft_engine::core::{
BakedMap, BlendMode, Channel, Decal, Effect, EffectKind, Fill, Filter, GeneratorKind, Layer, LayerId, LayerKind, ProceduralKind, Source,
BakedMap, BlendMode, Channel, Decal, Effect, EffectKind, Fill, Filter, GeneratorKind, Layer, LayerId, LayerKind, LinkParam, MaterialLink, ProceduralKind,
Source,
};
use texturecraft_engine::filter_lib::{FILTERS, scalar_params};
use texturecraft_engine::paint::{BrushAlpha, alphas};
@@ -1402,11 +1403,133 @@ fn material_section(app: &mut TextureCraftApp, ui: &mut Ui) {
fn fill_properties(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer) {
let LayerKind::Fill(fill) = &layer.kind else { return };
if let Some(link) = &fill.link {
link_properties(app, ui, layer.id, link);
ui.add_space(6.0);
}
let on: Vec<Channel> = layer.channels.keys().copied().collect();
ui.label(caption(tr("Fill")));
fill_editor(app, ui, fill, &json!({"layer": layer.id.0}), &on, layer.id);
}
/// A MaterialCraft material's presets and exposed inputs; edits store the value and re-cook the
/// maps on a worker thread (desktop only: cooking runs MaterialCraft's command line).
fn link_properties(app: &mut TextureCraftApp, ui: &mut Ui, layer: LayerId, link: &MaterialLink) {
let tk = Tokens::get(ui.ctx());
let base = json!({"layer": layer.0, "cook": false});
ui.label(caption(tr("MaterialCraft material")));
ui.horizontal(|ui| {
ui.label(RichText::new(&link.name).strong());
ui.label(RichText::new(trf("Cooked at {size} px", &[("size", &link.size)])).size(10.0).color(tk.text_dim));
if app.link_cooks.iter().any(|c| c.layer == layer) {
ui.spinner();
}
});
let editable = cfg!(not(target_arch = "wasm32"));
ui.add_enabled_ui(editable, |ui| {
if !link.presets.is_empty() {
combo(ui, tr("Preset"), "", |ui| {
for p in &link.presets {
if ui.selectable_label(false, &p.name).clicked() && app.run("material.link.preset", with(&base, json!({"name": p.name}))).is_some() {
app.cook_link(layer);
}
}
});
}
let mut group = "";
for p in &link.params {
if p.group != group && !p.group.is_empty() {
ui.label(RichText::new(p.group.to_uppercase()).size(10.0).strong().color(tk.text_dim));
}
group = &p.group;
if let Some(v) = link_param_editor(ui, layer, p)
&& app.run("material.link.set", with(&base, json!({"id": p.id, "value": v}))).is_some()
{
app.cook_link(layer);
}
}
});
if !editable {
ui.label(RichText::new(tr("Parameters change in the desktop app, with MaterialCraft installed")).size(10.0).color(tk.text_dim));
}
#[cfg(not(target_arch = "wasm32"))]
ui.horizontal(|ui| {
if ui.button(tr("Re-cook")).clicked() {
app.cook_link(layer);
}
if ui.button(tr("Open in MaterialCraft")).clicked() {
let path = std::env::temp_dir().join(format!("{}.mcraft", texturecraft_engine::formats::export::sanitize_file_stem(&link.name)));
let opened = app
.session
.link_package(layer)
.and_then(|b| std::fs::write(&path, b.as_slice()).map_err(|e| texturecraft_engine::EngineError::Other(e.to_string())))
.and_then(|()| texturecraft_engine::link_cmds::open_in_materialcraft(&path));
match opened {
Ok(()) => app.toast(tr("Opened in MaterialCraft: use File > Send to TextureCraft there to bring your changes back"), false),
Err(e) => app.toast(e.to_string(), true),
}
}
});
}
/// The editor of one linked-material input; returns its new value when an edit completes.
fn link_param_editor(ui: &mut Ui, layer: LayerId, p: &LinkParam) -> Option<Value> {
let t = p.default.get("t").and_then(Value::as_str).unwrap_or("");
let v = p.value.get("v").unwrap_or(&Value::Null);
let num = |x: &Value| x.as_f64().unwrap_or(0.0) as f32;
let (lo, hi) = if p.max > p.min { (p.min, p.max) } else { (0.0, 1.0) };
// A slider edit completes when the drag ends, on a click or a typed value.
let edit = |ui: &mut Ui, label: &str, x: f32, decimals: usize| {
let mut y = x;
let r = slider(ui, label, &mut y, lo.min(x)..=hi.max(x), decimals);
(r.drag_stopped() || (!r.dragged() && y != x)).then_some(y)
};
match t {
"bool" => {
let mut b = v.as_bool().unwrap_or(num(v) != 0.0);
ui.checkbox(&mut b, &p.label).changed().then(|| json!(b))
}
"enum" if !p.options.is_empty() => {
let cur = num(v).max(0.0) as usize;
let mut pick = None;
combo(ui, &p.label, p.options.get(cur).map_or("", String::as_str), |ui| {
for (i, o) in p.options.iter().enumerate() {
if ui.selectable_label(i == cur, o).clicked() {
pick = Some(json!(i));
}
}
});
pick
}
"float" | "int" | "enum" => edit(ui, &p.label, num(v), if t == "float" { 2 } else { 0 }).map(|y| json!(y)),
"color" => {
let a = v.as_array().map(|a| a.iter().map(num).collect::<Vec<f32>>()).unwrap_or_default();
let mut rgb = [0, 1, 2].map(|i| a.get(i).copied().unwrap_or(1.0));
let mut out = None;
ui.horizontal(|ui| {
ui.label(&p.label);
if color_swatch(ui, &format!("link_{}_{}", layer.0, p.id), &mut rgb, ui.available_width() - 4.0) {
out = Some(json!([rgb[0], rgb[1], rgb[2], a.get(3).copied().unwrap_or(1.0)]));
}
});
out
}
_ => {
// Vectors: one slider per component.
let mut a: Vec<f32> = v.as_array().map(|a| a.iter().map(num).collect()).unwrap_or_default();
let decimals = if t == "int2" { 0 } else { 2 };
let mut changed = false;
for (x, axis) in a.iter_mut().zip(["x", "y", "z", "w"]) {
if let Some(y) = edit(ui, &format!("{} {axis}", p.label), *x, decimals) {
*x = y;
changed = true;
}
}
changed.then(|| json!(a))
}
}
}
/// `base` with the fields of `extra` added.
fn with(base: &Value, extra: Value) -> Value {
let mut v = base.clone();
+11
View File
@@ -990,4 +990,15 @@ pub const TABLE: &[(&str, &str)] = &[
("Save the changes to {name} before closing?", "هل تريد حفظ التغييرات في {name} قبل الإغلاق؟"),
("Twill weave", "نسيج مائل"),
("Save the changes to {name} first?", "هل تريد حفظ التغييرات في {name} أولًا؟"),
("Import material...", "استيراد خامة..."),
("MaterialCraft material", "خامة MaterialCraft"),
("Cooked at {size} px", "حُسبت بدقة {size} بكسل"),
("Preset", "إعداد مسبق"),
("Re-cook", "إعادة الحساب"),
("Open in MaterialCraft", "فتح في MaterialCraft"),
(
"Opened in MaterialCraft: use File > Send to TextureCraft there to bring your changes back",
"فُتحت في MaterialCraft: استخدم هناك ملف > إرسال إلى TextureCraft لإعادة تغييراتك",
),
("Parameters change in the desktop app, with MaterialCraft installed", "تتغير المعلمات في تطبيق سطح المكتب مع تثبيت MaterialCraft"),
];
+11
View File
@@ -1048,4 +1048,15 @@ pub const TABLE: &[(&str, &str)] = &[
("Save the changes to {name} before closing?", "Änderungen an {name} vor dem Schließen speichern?"),
("Twill weave", "Köperbindung"),
("Save the changes to {name} first?", "Änderungen an {name} zuerst speichern?"),
("Import material...", "Material importieren..."),
("MaterialCraft material", "MaterialCraft-Material"),
("Cooked at {size} px", "Berechnet mit {size} px"),
("Preset", "Vorgabe"),
("Re-cook", "Neu berechnen"),
("Open in MaterialCraft", "In MaterialCraft öffnen"),
(
"Opened in MaterialCraft: use File > Send to TextureCraft there to bring your changes back",
"In MaterialCraft geöffnet: Dort mit Datei > An TextureCraft senden die Änderungen zurückholen",
),
("Parameters change in the desktop app, with MaterialCraft installed", "Parameter lassen sich in der Desktop-App mit installiertem MaterialCraft ändern"),
];
+11
View File
@@ -1030,4 +1030,15 @@ pub const TABLE: &[(&str, &str)] = &[
("Save the changes to {name} before closing?", "¿Guardar los cambios de {name} antes de cerrar?"),
("Twill weave", "Tejido de sarga"),
("Save the changes to {name} first?", "¿Guardar primero los cambios de {name}?"),
("Import material...", "Importar material..."),
("MaterialCraft material", "Material de MaterialCraft"),
("Cooked at {size} px", "Calculado a {size} px"),
("Preset", "Preajuste"),
("Re-cook", "Recalcular"),
("Open in MaterialCraft", "Abrir en MaterialCraft"),
(
"Opened in MaterialCraft: use File > Send to TextureCraft there to bring your changes back",
"Abierto en MaterialCraft: usa allí Archivo > Enviar a TextureCraft para traer tus cambios",
),
("Parameters change in the desktop app, with MaterialCraft installed", "Los parámetros se cambian en la app de escritorio, con MaterialCraft instalado"),
];
+14
View File
@@ -1051,4 +1051,18 @@ pub const TABLE: &[(&str, &str)] = &[
("Save the changes to {name} before closing?", "Enregistrer les modifications de {name} avant de fermer ?"),
("Twill weave", "Armure sergé"),
("Save the changes to {name} first?", "Enregistrer d'abord les modifications de {name} ?"),
("Import material...", "Importer un matériau..."),
("MaterialCraft material", "Matériau MaterialCraft"),
("Cooked at {size} px", "Calculé en {size} px"),
("Preset", "Préréglage"),
("Re-cook", "Recalculer"),
("Open in MaterialCraft", "Ouvrir dans MaterialCraft"),
(
"Opened in MaterialCraft: use File > Send to TextureCraft there to bring your changes back",
"Ouvert dans MaterialCraft : utilisez-y Fichier > Envoyer vers TextureCraft pour récupérer vos modifications",
),
(
"Parameters change in the desktop app, with MaterialCraft installed",
"Les paramètres se modifient dans l'application de bureau, avec MaterialCraft installé",
),
];
+11
View File
@@ -1039,4 +1039,15 @@ pub const TABLE: &[(&str, &str)] = &[
("Save the changes to {name} before closing?", "Salvare le modifiche a {name} prima di chiudere?"),
("Twill weave", "Tessuto a saia"),
("Save the changes to {name} first?", "Salvare prima le modifiche a {name}?"),
("Import material...", "Importa materiale..."),
("MaterialCraft material", "Materiale MaterialCraft"),
("Cooked at {size} px", "Calcolato a {size} px"),
("Preset", "Preset"),
("Re-cook", "Ricalcola"),
("Open in MaterialCraft", "Apri in MaterialCraft"),
(
"Opened in MaterialCraft: use File > Send to TextureCraft there to bring your changes back",
"Aperto in MaterialCraft: usa lì File > Invia a TextureCraft per riportare le modifiche",
),
("Parameters change in the desktop app, with MaterialCraft installed", "I parametri si modificano nell'app desktop, con MaterialCraft installato"),
];
+11
View File
@@ -1015,4 +1015,15 @@ pub const TABLE: &[(&str, &str)] = &[
("Save the changes to {name} before closing?", "閉じる前に {name} の変更を保存しますか?"),
("Twill weave", "綾織り"),
("Save the changes to {name} first?", "先に {name} の変更を保存しますか?"),
("Import material...", "マテリアルを読み込む..."),
("MaterialCraft material", "MaterialCraft マテリアル"),
("Cooked at {size} px", "{size} px で計算済み"),
("Preset", "プリセット"),
("Re-cook", "再計算"),
("Open in MaterialCraft", "MaterialCraft で開く"),
(
"Opened in MaterialCraft: use File > Send to TextureCraft there to bring your changes back",
"MaterialCraft で開きました。変更を戻すには、そちらで「ファイル > TextureCraft に送る」を使います",
),
("Parameters change in the desktop app, with MaterialCraft installed", "パラメーターは MaterialCraft をインストールしたデスクトップ版で変更できます"),
];
+11
View File
@@ -1000,4 +1000,15 @@ pub const TABLE: &[(&str, &str)] = &[
("Save the changes to {name} before closing?", "닫기 전에 {name}의 변경 사항을 저장할까요?"),
("Twill weave", "능직"),
("Save the changes to {name} first?", "먼저 {name}의 변경 사항을 저장할까요?"),
("Import material...", "머티리얼 가져오기..."),
("MaterialCraft material", "MaterialCraft 머티리얼"),
("Cooked at {size} px", "{size} px로 계산됨"),
("Preset", "프리셋"),
("Re-cook", "다시 계산"),
("Open in MaterialCraft", "MaterialCraft에서 열기"),
(
"Opened in MaterialCraft: use File > Send to TextureCraft there to bring your changes back",
"MaterialCraft에서 열었습니다. 변경 내용을 가져오려면 그곳에서 파일 > TextureCraft로 보내기를 사용하세요",
),
("Parameters change in the desktop app, with MaterialCraft installed", "매개변수는 MaterialCraft가 설치된 데스크톱 앱에서 바꿀 수 있습니다"),
];
+11
View File
@@ -1027,4 +1027,15 @@ pub const TABLE: &[(&str, &str)] = &[
("Save the changes to {name} before closing?", "Salvar as alterações em {name} antes de fechar?"),
("Twill weave", "Tecido sarja"),
("Save the changes to {name} first?", "Salvar primeiro as alterações em {name}?"),
("Import material...", "Importar material..."),
("MaterialCraft material", "Material do MaterialCraft"),
("Cooked at {size} px", "Calculado em {size} px"),
("Preset", "Predefinição"),
("Re-cook", "Recalcular"),
("Open in MaterialCraft", "Abrir no MaterialCraft"),
(
"Opened in MaterialCraft: use File > Send to TextureCraft there to bring your changes back",
"Aberto no MaterialCraft: use lá Arquivo > Enviar para o TextureCraft para trazer suas alterações",
),
("Parameters change in the desktop app, with MaterialCraft installed", "Os parâmetros mudam no app para desktop, com o MaterialCraft instalado"),
];
+11
View File
@@ -940,4 +940,15 @@ pub const TABLE: &[(&str, &str)] = &[
("Save the changes to {name} before closing?", "关闭前是否保存对 {name} 的更改?"),
("Twill weave", "斜纹织物"),
("Save the changes to {name} first?", "是否先保存对 {name} 的更改?"),
("Import material...", "导入材质..."),
("MaterialCraft material", "MaterialCraft 材质"),
("Cooked at {size} px", "已按 {size} px 计算"),
("Preset", "预设"),
("Re-cook", "重新计算"),
("Open in MaterialCraft", "在 MaterialCraft 中打开"),
(
"Opened in MaterialCraft: use File > Send to TextureCraft there to bring your changes back",
"已在 MaterialCraft 中打开:在其中使用“文件 > 发送到 TextureCraft”即可带回更改",
),
("Parameters change in the desktop app, with MaterialCraft installed", "参数可在安装了 MaterialCraft 的桌面版中修改"),
];
+69
View File
@@ -55,6 +55,14 @@ pub struct BakeJob {
pub only: Option<texturecraft_engine::core::BakedMap>,
}
/// A MaterialCraft material re-cooking on a worker thread (one per layer at a time).
pub struct LinkCook {
pub layer: texturecraft_engine::core::LayerId,
/// The parameter values it cooks: a result whose values are no longer the layer's is redone.
pub values: serde_json::Map<String, Value>,
pub rx: std::sync::mpsc::Receiver<texturecraft_engine::Result<Vec<u8>>>,
}
/// The browser build's notice when WebGPU is unavailable and wgpu fell back to WebGL2.
pub const WEBGL_NOTICE: &str = i18n::n_(
"WebGPU isn't available in this browser, so TextureCraft runs on WebGL2: Rendering mode path traces on the CPU (slower) and large texture sets show at reduced resolution. A current Chrome, Edge or Safari has WebGPU.",
@@ -76,6 +84,7 @@ pub struct TextureCraftApp {
pub(crate) gpu_memory: (Vec<String>, Option<String>),
pub bake: Option<BakeJob>,
pub resize: Option<ResizeJob>,
pub link_cooks: Vec<LinkCook>,
pub thumbs: thumbs::Thumbs,
/// Last frame's viewport rect (logical points), for scripted input.
pub viewport_rect: egui::Rect,
@@ -108,6 +117,8 @@ pub struct TextureCraftApp {
next_autosave: f64,
/// The brightness and accent the style was last built for.
styled: Option<(theme::Brightness, theme::Accent)>,
/// Whether the window had focus last frame.
focused: bool,
}
impl TextureCraftApp {
@@ -133,6 +144,7 @@ impl TextureCraftApp {
gpu_memory: (Vec::new(), None),
bake: None,
resize: None,
link_cooks: Vec::new(),
thumbs: thumbs::Thumbs::default(),
viewport_rect: egui::Rect::NOTHING,
fps: 0.0,
@@ -150,6 +162,7 @@ impl TextureCraftApp {
last_time: 0.0,
next_autosave,
styled: None,
focused: true,
};
app.ui.rename = None;
app.ui.windows.new_project = false;
@@ -199,6 +212,55 @@ impl TextureCraftApp {
self.bake = Some(BakeJob { queue: sets, current: None, total, only: None });
}
/// Re-cook MaterialCraft material layer `layer` in the background. While one cook of the layer
/// runs no other starts: when it ends with stale values, it is redone with the current ones.
pub fn cook_link(&mut self, layer: texturecraft_engine::core::LayerId) {
if self.link_cooks.iter().any(|c| c.layer == layer) {
return;
}
match self.session.link_job(layer, &serde_json::Map::new()) {
Ok(job) => {
let values = job.values.clone();
let rx = spawn_worker("texturecraft-link", move || job.run());
self.link_cooks.push(LinkCook { layer, values, rx });
}
Err(e) => self.toast(e.to_string(), true),
}
}
fn poll_link_cooks(&mut self, ctx: &egui::Context) {
if self.link_cooks.is_empty() {
return;
}
ctx.request_repaint_after(std::time::Duration::from_millis(50));
let mut done = Vec::new();
self.link_cooks.retain(|c| match c.rx.try_recv() {
Err(std::sync::mpsc::TryRecvError::Empty) => true,
Ok(r) => {
done.push((c.layer, c.values.clone(), r));
false
}
Err(std::sync::mpsc::TryRecvError::Disconnected) => {
done.push((c.layer, c.values.clone(), Err(texturecraft_engine::EngineError::Other("the MaterialCraft cook stopped".into()))));
false
}
});
for (layer, values, result) in done {
let current = self.session.link_values(layer);
match (result, current) {
// The layer is gone (undone, deleted): nothing to show.
(_, Err(_)) => {}
(Ok(_), Ok(now)) if now != values => self.cook_link(layer),
(Ok(bytes), Ok(_)) => {
if let Err(e) = self.session.apply_link_cook(layer, &bytes, true) {
self.toast(e.to_string(), true);
}
}
(Err(e), Ok(_)) => self.toast(e.to_string(), true),
}
}
}
fn poll_bake(&mut self, ctx: &egui::Context) {
let Some(job) = self.bake.as_mut() else { return };
ctx.request_repaint_after(std::time::Duration::from_millis(50));
@@ -415,6 +477,13 @@ impl eframe::App for TextureCraftApp {
self.poll_bake(ctx);
self.drive_auto_rebake();
self.poll_resize(ctx);
self.poll_link_cooks(ctx);
// 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 {
self.library = None;
}
self.focused = focused;
let scale = self.ui.prefs.ui_scale.clamp(0.5, 3.0);
if (ctx.zoom_factor() - scale).abs() > 1e-3 {
ctx.set_zoom_factor(scale);
+1
View File
@@ -46,6 +46,7 @@ pub const CATALOG: &[Item] = &[
sep("File"),
it("File", "Import resources...", "resource.import", None),
it("File", "Import MaterialX material...", "file.importMaterialX", None),
it("File", "Import material...", "material.load", None),
it("File", "Export Textures...", "ui.export", Some("Ctrl+Shift+E")),
it("File", "Export Mesh...", "file.exportMesh", None),
sep("File"),
+6 -2
View File
@@ -9,6 +9,8 @@ use egui_phosphor::regular as ic;
use serde_json::json;
use texturecraft_engine::core::ProceduralKind;
use texturecraft_engine::filter_lib::FILTERS;
#[cfg(not(target_arch = "wasm32"))]
use texturecraft_engine::formats::mcmat::MCMAT_EXT;
use texturecraft_engine::materials::MATERIALS;
use texturecraft_engine::paint::alphas;
use texturecraft_engine::paint::presets::{BrushPreset, PRESETS};
@@ -274,7 +276,7 @@ fn library(app: &mut TextureCraftApp, ui: &mut Ui, filter: &str) {
let tk = Tokens::get(ui.ctx());
if app.library.is_none() {
let mut files: Vec<std::path::PathBuf> = std::fs::read_dir(crate::library_dir())
.map(|d| d.flatten().map(|e| e.path()).filter(|p| p.extension().is_some_and(|x| x == "tcmat")).collect())
.map(|d| d.flatten().map(|e| e.path()).filter(|p| p.extension().is_some_and(|x| x == "tcmat" || x == MCMAT_EXT)).collect())
.unwrap_or_default();
files.sort();
app.library = Some(files);
@@ -296,7 +298,9 @@ fn library(app: &mut TextureCraftApp, ui: &mut Ui, filter: &str) {
}
for f in shown {
let (label, asset) = (name(f), Asset::Library(f.display().to_string()));
let r = ui.add(egui::Button::selectable(false, format!("{} {label}", ic::SPHERE)).sense(Sense::click_and_drag())).on_hover_ui(|ui| {
// MaterialCraft materials (sent from MaterialCraft) show a graph.
let glyph = if f.extension().is_some_and(|x| x == MCMAT_EXT) { ic::GRAPH } else { ic::SPHERE };
let r = ui.add(egui::Button::selectable(false, format!("{glyph} {label}")).sense(Sense::click_and_drag())).on_hover_ui(|ui| {
ui.label(crate::i18n::trf(
"{file}: add above the selected layer (or drag it into the layer stack or onto the viewport)",
&[("file", &f.display())],
+20 -2
View File
@@ -25,6 +25,10 @@ pub const IMAGE_EXTS: &[&str] = &["png", "jpg", "jpeg", "tga", "tif", "tiff", "b
/// Equirect HDR images Display settings › Import HDR accepts.
pub const ENV_EXTS: &[&str] = &["hdr", "exr"];
/// Materials File › Import material adds as layers: smart materials, MaterialCraft portable
/// materials and MaterialCraft packages.
pub const MATERIAL_EXTS: &[&str] = &["tcmat", "mcmat", "mcraft"];
/// Texture size of a project made from an opened mesh (smaller on the single-threaded web).
const MESH_RESOLUTION: u32 = if cfg!(target_arch = "wasm32") { 1024 } else { 2048 };
@@ -120,6 +124,12 @@ pub fn load(app: &mut TextureCraftApp, name: &str, bytes: Vec<u8>) {
}
return;
}
if MATERIAL_EXTS.contains(&ext.as_str()) {
if let Err(e) = app.session.add_smart_material_bytes(&bytes) {
app.toast(trf("Couldn't import {name}: {error}", &[("name", &name), ("error", &e)]), true);
}
return;
}
let bytes = std::sync::Arc::new(bytes);
if crate::windows::ask_to_save(app, crate::state::Unsaved::Load(name.into(), bytes.clone())) {
return;
@@ -143,7 +153,9 @@ pub fn dropped(app: &mut TextureCraftApp, ctx: &egui::Context, file: &egui::Drop
let path = file.path();
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase();
let path = path.display().to_string();
if IMAGE_EXTS.contains(&ext.as_str()) {
if MATERIAL_EXTS.contains(&ext.as_str()) {
app.run("material.load", serde_json::json!({ "path": path }));
} else if IMAGE_EXTS.contains(&ext.as_str()) {
if let Some(r) = app.run("resource.import", serde_json::json!({ "path": path })) {
let name = r.get("name").and_then(|n| n.as_str()).unwrap_or_default().to_string();
app.ui.dock.focus(crate::docking::Panel::Assets);
@@ -188,7 +200,8 @@ fn zip_files(files: &[ExportedFile]) -> Result<Vec<u8>, String> {
#[cfg(target_arch = "wasm32")]
pub use web::{
download, export_mesh, export_textures, import_bake_mesh, import_environment, import_images, import_preset, open, save_project, sync_unsaved, unsaved,
download, export_mesh, export_textures, import_bake_mesh, import_environment, import_images, import_material, import_preset, open, save_project,
sync_unsaved, unsaved,
};
#[cfg(target_arch = "wasm32")]
@@ -215,6 +228,11 @@ mod web {
pick(ctx, tr("Projects and meshes"), OPEN_EXTS, false, Pick::Open);
}
/// File › Import material: pick materials; they add as layers on a later frame.
pub fn import_material(ctx: &egui::Context) {
pick(ctx, tr("Materials"), MATERIAL_EXTS, true, Pick::Open);
}
/// File › Import resources: pick images; they import on a later frame.
pub fn import_images(ctx: &egui::Context) {
pick(ctx, tr("Images"), IMAGE_EXTS, true, Pick::Open);
+7
View File
@@ -1055,6 +1055,12 @@ pub fn file_action(app: &mut TextureCraftApp, _ctx: &egui::Context, id: &str) ->
}
true
}
"material.load" => {
for p in rfd::FileDialog::new().add_filter(tr("Materials"), crate::web_files::MATERIAL_EXTS).pick_files().unwrap_or_default() {
app.run("material.load", json!({"path": p.display().to_string()}));
}
true
}
"file.importResources" | "resource.import" => {
let picks = rfd::FileDialog::new().add_filter(tr("Images"), crate::web_files::IMAGE_EXTS).pick_files();
for p in picks.unwrap_or_default() {
@@ -1131,6 +1137,7 @@ pub fn file_action(app: &mut TextureCraftApp, ctx: &egui::Context, id: &str) ->
match id {
"file.open" => crate::web_files::open(ctx),
"file.importResources" | "resource.import" => crate::web_files::import_images(ctx),
"material.load" => crate::web_files::import_material(ctx),
"file.save" | "file.saveAs" | "file.saveCopy" => crate::web_files::save_project(app),
"file.exportMesh" => app.ui.windows.export_mesh = true,
_ => return false,
+9
View File
@@ -93,6 +93,15 @@ key-light shadow pass (`shaders/displace.wgsl`, shared by both). Rendering mode'
(`render::trace` on the CPU, `render::gpu_trace` on the GPU) trace the same displaced copy, built
on the CPU by `render::displace` from the composited height.
## MaterialCraft materials
A fill can carry a `MaterialLink` (`core::layer`): a MaterialCraft graph whose cooked maps are the
fill's image sources. `engine::link_cmds` imports `.mcmat` files (`formats::mcmat`), keeps the
`.mcraft` package as a resource, and re-cooks by running `materialcraft-cli` with the link's
values, the texture set's resolution and its baked mesh maps (`LinkJob::run`, on a worker thread
in the UI). Every cook stores its maps under fresh resource names, so undo, redo and the decoded
image cache never disagree. The apps share the file format and the command line, never code.
## Commands and automation
Every action is a `CommandSpec` in `engine::commands` (id, label, menu path, shortcut, params doc,
+26 -1
View File
@@ -137,7 +137,7 @@ takes a font family, not a path: it reads only the system's font folders.
| `bake_mesh_add` | `{path, cage?}` | Add a high-definition mesh for baking, or set the cage → `{name, meshes}` |
| `resource_import` | `{path}` | Image resource (png, jpg, tga, tif, bmp, webp, exr, hdr) → its name, for `layer.add.image` / `tool.stencil`, or as a mesh map instead of baking it: `textureSet.meshMap.set {ts?, map, image}` (`textureSet.meshMap.clear {ts?, map}` empties the slot) |
| `material_save` | `{path, layer?, name?}` | A layer or folder as a `.tcmat` smart material below the write root |
| `material_load` | `{path}` | Add a `.tcmat` smart material above the selection |
| `material_load` | `{path}` | Add a `.tcmat` smart material, a MaterialCraft `.mcmat` material or a `.mcraft` package above the selection |
| `textures_export` | `{dir, preset?, ts?, format?, padding?, size?}` | Writes files below the write root |
| `bake` | `{ts?, resolution?, maps?, cpu?}` | Mesh maps for generators and smart materials; rays on the GPU when there is one (`cpu: true`: on the CPU). Per-map ray settings (`ao`, `thickness`, `bentNormal`: rays, maxDistance, spread, distribution; AO ignoreBackface, selfOcclusion) are `bake.settings` parameters |
| `paint_stroke` | `{points, viewport?, color?, roughness?, metallic?, height?, size?, hardness?}` | Paint tool + brush material + `tool.stroke`; points in viewport pixels |
@@ -221,6 +221,31 @@ 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
```
## MaterialCraft materials
A `.mcmat` portable material (MaterialCraft's format, read here by `formats::mcmat`; the manifest is
documented in MaterialCraft's `docs/control-protocol.md` § Portable materials) becomes a fill layer
whose channel sources are its cooked maps and whose `link` (shown by `project.inspect`) holds the
graph's exposed inputs, presets and image inputs. The apps share the format and the command line,
not code.
| Command | Params |
|---|---|
| `material.load` | `{path, above?, inside?, index?}`: `.tcmat`, `.mcmat` or `.mcraft` (cooked first) |
| `material.link.set` | `{layer?, id, value, cook?: true}`: a number, `[..]`, a bool, `"#rrggbb"` or a typed `{"t", "v"}`; re-cooks unless `cook: false` |
| `material.link.preset` | `{layer?, name, cook?: true}` |
| `material.link.cook` | `{layer?}`: re-cook at the texture set's resolution with its baked mesh maps (after a bake or a resize) |
| `material.link.saveSource` | `{layer?, path}`: the `.mcraft` package, to open in MaterialCraft |
Re-cooks run `materialcraft-cli run <package> file.exportMaterial {path, size, values, inputs}`:
`$MATERIALCRAFT_CLI`, else `materialcraft-cli` next to the TextureCraft executable, else on `PATH`
(the app is `$MATERIALCRAFT` / `materialcraft`). Baked maps feed graph input nodes whose identifier
matches the map's shared id (`ambientOcclusion`, `curvature`, `thickness`, `position`,
`worldNormal`, `normal`, `height`, `id`, `bentNormal`, `opacity`, `colorMap`, `worldDirection`;
case and underscores ignored). Usages map to channels of the same id (`baseColor`, `normal` in
OpenGL convention, `roughness`, `metallic`, `height`, `ambientOcclusion`, `emissive`, `opacity`,
`specular`); missing channels join the texture set.
## CLI
```sh
+8
View File
@@ -62,6 +62,14 @@ Everything TextureCraft does today, by area. What's still missing is in [`ROADMA
the model.
- **Your own library:** right-click a layer or folder, choose *Create smart material*, and it
appears under *My smart materials*, pixels and images included.
- **MaterialCraft materials:** *File › Import material*, a drop on the window, or MaterialCraft's
*File › Send to TextureCraft* (it lands under *My smart materials*) adds a `.mcmat` portable
material, or a `.mcraft` package, as a fill layer. Its exposed graph inputs (numbers, vectors,
toggles, options, colors) and presets show in the layer's properties; every change re-cooks
the maps through MaterialCraft's command line at the texture set's resolution, on a worker
thread, with the set's baked mesh maps (curvature, AO, thickness…) feeding graph inputs of the
same name. *Open in MaterialCraft* edits the graph itself. In the browser the cooked maps paint,
but the inputs are read-only.
## Files and texture sets
+2 -1
View File
@@ -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).
**54 of 54 menu items live (100%).**
**55 of 55 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 material... | `material.load` | | live |
| Export Textures... | `ui.export` | Ctrl+Shift+E | live |
| Export Mesh... | `file.exportMesh` | | live |
| Quit | `ui.quit` | Ctrl+Q | live |