Merge main into geometric pattern additions

This commit is contained in:
Morion committed 2026-11-16 12:37:57 -05:00
commit 101206b9cd
178 files changed
+22641 -4447

No files matched your search

-1
View File
@@ -24,7 +24,6 @@ jobs:
# Same craft-fonts commit as release.yml.
craft-fonts-ref: aced4137c022f1f587624d48e81ec77e1914a9cc
os: '["ubuntu-24.04","windows-latest"]'
toolchain: "1.96.0"
# xtask ci runs cargo test (including doctests), not cargo nextest.
tools: ""
packages: >-
+4
View File
@@ -229,3 +229,7 @@ after every arc of work that builds and passes.
- `docs/releasing.md`: release builds and signing.
- `plan/` is an optional gitignored notebook (execution notes). Do not ask for it and do not stop when it is absent.
- `log/` (local, gitignored): the dev log.
## Shared Craft libraries
Before adding or changing reusable file-format, codec, protocol or UI code, check [craft-libs](https://github.com/storytold/craft-libs) and existing contributor work; reuse its public APIs or extend the appropriate shared library instead of adding an app-local duplicate, keeping app-specific policy and model mapping local and preserving compatibility, security limits and regression coverage.
+77
View File
@@ -0,0 +1,77 @@
# Code style
Style conventions for TextureCraft's Rust code.
Read alongside [AGENTS.md](AGENTS.md) for repository requirements.
## Naming and Rust conventions
- Use descriptive names that explain purpose; include units where useful. Avoid misleading names
and abbreviations whose meaning is unclear in context.
- Follow Rust naming conventions: `snake_case` for functions, variables, fields, and modules;
`UpperCamelCase` for types and traits; `SCREAMING_SNAKE_CASE` for constants and statics.
- Conventional Rust names are not cryptic abbreviations. Keep names such as `fmt`, `iter`, `len`,
`args`, `err`, `id`, and `ui` when their meaning is clear. Do not expand them just to make them
longer.
- Keep standard file and directory names such as `lib.rs`, `main.rs`, `mod.rs`, `src`, `tests`,
`examples`, and `benches`. Rust keywords such as `mut`, `ref`, `impl`, and `mod` are language
syntax, not naming problems.
- Short names are appropriate in a clear local context: `i` for an index, `x` and `y` for
coordinates, `w` and `h` for image dimensions, and conventional generic parameters such as `T`.
Use more descriptive names when scope or ambiguity makes those hard to follow.
## Magic numbers
- Give non-obvious limits, thresholds, sizes, and tuning values descriptive named constants
instead of leaving unexplained numeric literals in the code.
- Name constants for their purpose, with units where useful, rather than for their literal value.
Keep them in the narrowest useful scope.
- Reuse an existing constant when it represents the same concept. Equal numeric values alone do
not mean two concepts should share a constant.
- Explain the origin or rationale of non-obvious coefficients and tolerances in a short comment.
Use standard library constants for standard mathematical values.
- Keep literals inline when their role is clear, such as zero initialization, incrementing by one,
or dividing by two to find a midpoint. Do not introduce names such as `ZERO` or `TWO` that add
no meaning.
- Apply this guidance to tests too; clear fixture values and expected results can remain literal.
## Formatting and spacing
- Let `cargo fmt` and the repository's [rustfmt.toml](rustfmt.toml) handle mechanical formatting.
Do not introduce a competing indentation or line-wrapping convention.
- Use one blank line between distinct logical steps: input preparation, validation, computation,
mutation, and result construction.
- Keep closely related statements together. Do not add a blank line after every declaration or
fragment a single operation with extra spacing.
- Put a blank line after a multiline early-exit guard, including `if` and `let ... else`, before
the next statement.
- Put a blank line before a comment introducing a new step, with no blank line between the
comment and the code it explains.
- Keep documentation and attributes attached to their declarations.
- Separate function definitions with one blank line. Avoid extra empty lines just inside braces
or between every field, argument, or statement.
- In tests, separate preparation, action, and verification with blank lines when those phases
are distinct; keep related assertions together.
## Source organization
- Give each struct or enum its own source file, with its implementation beside it. Use the full
type name in `snake_case`, such as `mesh_curvature_settings.rs` for `MeshCurvatureSettings`.
- Preserve conventional crate roots and module entry points such as `lib.rs`, `main.rs`, and
`mod.rs`. Follow the command and node module placement required by `AGENTS.md`.
- Prefer singular folder names for new domain modules, such as `map`, `setting`, or `tracing`.
Keep standard Rust directories and repository-mandated paths unchanged.
- Group related types into focused modules rather than collecting unrelated types in one large file.
- Apply these preferences to new types and source organization changes within scope. Do not
reorganize unrelated existing code to enforce them.
## Comments
- Explain intent or a non-obvious constraint rather than narrating the code.
- Use short `//` comments. Avoid block comments and wrapped prose in ordinary implementation
comments; adjacent comment lines should each express a concise point.
- Do not add trailing punctuation to ordinary code comments.
- Preserve explanations of guards and invariants. Remove stale or redundant comments without
losing necessary rationale.
- API documentation, required license headers, and examples may use conventional multiline
formatting and punctuation.
- Apply the same comment guidance to production code and tests.
Generated
+115 -16
View File
@@ -755,10 +755,19 @@ dependencies = [
"libc",
]
[[package]]
name = "craft-cms"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=272a095b61257917693da3d09f2f8ecccd7f0295#272a095b61257917693da3d09f2f8ecccd7f0295"
dependencies = [
"md-5",
"rayon",
]
[[package]]
name = "craft-dialogs"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
dependencies = [
"block2 0.6.2",
"objc2 0.6.5",
@@ -771,12 +780,12 @@ dependencies = [
[[package]]
name = "craft-fonts-build"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
[[package]]
name = "craft-i18n"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
dependencies = [
"fixed_decimal",
"icu_decimal",
@@ -799,17 +808,37 @@ dependencies = [
[[package]]
name = "craft-keymap"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
dependencies = [
"egui",
"serde",
"serde_json",
]
[[package]]
name = "craft-materialx"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
dependencies = [
"craft-xml",
"thiserror 2.0.21",
]
[[package]]
name = "craft-mcmat"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83#c376774f15ae7cd3b06a7ac664f01a8645af4b83"
dependencies = [
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83)",
"serde",
"serde_json",
"thiserror 2.0.21",
]
[[package]]
name = "craft-menu"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
dependencies = [
"craft-keymap",
"craft-quit",
@@ -819,10 +848,19 @@ dependencies = [
"objc2-app-kit 0.3.2",
]
[[package]]
name = "craft-mesh"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=b952ca0231867c8ad6749ac44cab5fc903c3ad52#b952ca0231867c8ad6749ac44cab5fc903c3ad52"
dependencies = [
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b952ca0231867c8ad6749ac44cab5fc903c3ad52)",
"miniz_oxide 0.9.1",
]
[[package]]
name = "craft-quit"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
dependencies = [
"dispatch2",
"egui",
@@ -830,10 +868,21 @@ dependencies = [
"objc2-app-kit 0.3.2",
]
[[package]]
name = "craft-tcraft"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83#c376774f15ae7cd3b06a7ac664f01a8645af4b83"
dependencies = [
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83)",
"serde",
"serde_json",
"thiserror 2.0.21",
]
[[package]]
name = "craft-ui"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
dependencies = [
"craft-fonts-build",
"craft-i18n",
@@ -854,7 +903,7 @@ dependencies = [
[[package]]
name = "craft-update"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
dependencies = [
"chrono",
"dunce",
@@ -878,10 +927,19 @@ dependencies = [
"zip",
]
[[package]]
name = "craft-usd"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=f7655fb3ea59d6b21681700c0ff880823f5bb709#f7655fb3ea59d6b21681700c0ff880823f5bb709"
dependencies = [
"lz4_flex",
"thiserror 2.0.21",
]
[[package]]
name = "craft-window"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
dependencies = [
"egui",
"objc2 0.6.5",
@@ -889,6 +947,36 @@ dependencies = [
"wayland-client",
]
[[package]]
name = "craft-xml"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=390fe98b0dd3fb84db7c3af7cbd958db4e8cc585#390fe98b0dd3fb84db7c3af7cbd958db4e8cc585"
dependencies = [
"quick-xml 0.41.0",
"serde",
"thiserror 2.0.21",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=b952ca0231867c8ad6749ac44cab5fc903c3ad52#b952ca0231867c8ad6749ac44cab5fc903c3ad52"
dependencies = [
"flate2",
"thiserror 2.0.21",
"zip",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83#c376774f15ae7cd3b06a7ac664f01a8645af4b83"
dependencies = [
"flate2",
"thiserror 2.0.21",
"zip",
]
[[package]]
name = "crc32fast"
version = "1.5.2"
@@ -2454,6 +2542,16 @@ version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4facc753ae494aeb6e3c22f839b158aebd4f9270f55cd3c79906c45476c47ab4"
[[package]]
name = "md-5"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
dependencies = [
"cfg-if",
"digest",
]
[[package]]
name = "memchr"
version = "2.8.3"
@@ -4369,6 +4467,13 @@ name = "texturecraft-formats"
version = "0.6.1"
dependencies = [
"base64 0.22.1",
"craft-cms",
"craft-materialx",
"craft-mcmat",
"craft-mesh",
"craft-tcraft",
"craft-usd",
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83)",
"exr",
"flate2",
"gltf",
@@ -4385,7 +4490,6 @@ dependencies = [
"texturecraft-geom",
"texturecraft-texel",
"thiserror 2.0.21",
"tobj",
"zip",
]
@@ -4425,6 +4529,7 @@ dependencies = [
"log",
"pollster 0.4.0",
"rayon",
"serde",
"texturecraft-bake",
"texturecraft-compose",
"texturecraft-core",
@@ -4582,12 +4687,6 @@ dependencies = [
"zerovec",
]
[[package]]
name = "tobj"
version = "4.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6eb8e04167c1c0c76b5de63226fd733485ad63ef71e40de31e16272f47b099e2"
[[package]]
name = "tokio"
version = "1.53.3"
+21 -9
View File
@@ -12,6 +12,7 @@ rust-version = "1.95"
repository = "https://github.com/storytold/texturecraft"
[workspace.dependencies]
craft-usd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "f7655fb3ea59d6b21681700c0ff880823f5bb709" }
# internal
texturecraft-core = { path = "crates/core" }
texturecraft-texel = { path = "crates/texel" }
@@ -49,7 +50,6 @@ wgpu = "30"
rfd = "0.17"
zip = { version = "9", default-features = false, features = ["deflate"] }
gltf = { version = "1.4", default-features = false, features = ["import", "utils", "names", "KHR_materials_emissive_strength"] }
tobj = { version = "4", default-features = false }
flate2 = "1"
lz4_flex = { version = "0.11", default-features = false, features = ["safe-decode", "safe-encode"] }
quick-xml = "0.41"
@@ -66,20 +66,25 @@ rustix = { version = "1", default-features = false, features = ["std", "fs", "pr
# Shared Craft egui components (widget palette, icon buttons, panel docking): storytold/craft-libs,
# MIT OR Apache-2.0, pinned to one rev.
# Shared catalog lookup, pinned with the other Craft libraries.
craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["serde"] }
craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585" }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["serde"] }
craft-materialx = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585" }
craft-tcraft = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" }
craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" }
craft-mesh = { git = "https://github.com/storytold/craft-libs", rev = "b952ca0231867c8ad6749ac44cab5fc903c3ad52", default-features = false, features = ["stl", "ply", "fbx", "obj"] }
craft-mcmat = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" }
# Who draws the window frame, and the app-drawn strip as the title bar (crates/ui-egui/src/titlebar.rs).
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["egui"] }
# Release checks, the update prompt and the update helper (crates/ui-egui/src/updates_ui.rs).
craft-update = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
craft-update = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["egui"] }
# File dialogs that never block the frame (crates/ui-egui/src/file_dialogs.rs).
craft-dialogs = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
craft-dialogs = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585" }
# Shortcut chords, lookup and dispatch (crates/ui-egui/src/keymap.rs).
craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["egui"] }
# Quitting the macOS way instead of closing the window (crates/ui-egui/src/windows.rs).
craft-quit = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
craft-quit = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["egui"] }
# The macOS app menu whose Quit asks about unsaved changes (apps/texturecraft/src/main.rs).
craft-menu = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
craft-menu = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585" }
# egui 0.37 and winit: the suite's forks (storytold/egui, storytold/winit), as craft-libs pins them.
[patch.crates-io]
@@ -117,6 +122,13 @@ opt-level = 1
lto = "thin"
codegen-units = 1
# Fast optimized verification: each test executable otherwise repeats cross-crate LTO.
# `false` keeps thin LTO within each crate; release packaging keeps the profile above.
[profile.verify]
inherits = "release"
lto = false
codegen-units = 16
[profile.wasm-release]
inherits = "release"
opt-level = "s"
+1 -1
View File
@@ -14,7 +14,7 @@ end to end today (engine + UI + automation + tests), not what is planned.
| Project and files | 8 | 95% | New project from glTF/GLB, FBX (binary and ASCII, 6.x and 7.x), COLLADA, **USD (usda, usdc, usdz)**, **Alembic (Ogawa)**, OBJ, PLY, STL or built-in meshes (auto-unwrap when UVs are missing; a `.gltf`'s buffers and images and a loose USD layer's textures are read from its folder and kept in the project), **project templates** (built-in and saved from a project), `.tcraft` open/save/save as/save a copy, autosave, **asks to save unsaved changes** before quitting or replacing the project (in the browser too, and before the tab closes), recent files, drag and drop, reimport mesh **with strokes preserved on the surface**, **MaterialX import** (`file.importMaterialX`: a `.mtlx` material with `open_pbr_surface`, `standard_surface` or `gltf_pbr` becomes one fill layer with its textures and values), image resources, remove unused resources, textured OBJ (MTL), GLB and **USD** mesh export, browser open/save/download | HDF5 Alembic; Alembic animation (first sample only) |
| Texture sets | 6 | 100% | One set per material, **UDIM / UV tiles** (auto-detected or switched on later with resampling, **a resolution per tile** whose filters and height slopes keep their size in UV space, one texture per tile at its own size, `$udim` export), list with visibility (show or hide all, invert, **focus** on one set), sizes 128–8192 with resampling, add/remove channels (19 channel types incl. coat, sheen, anisotropy and scattering), height strength, normal format | — |
| Layer stack | 16 | 98% | Paint, fill, folder and **instance** layers; 32 per-channel blend modes and opacities; white/black/painted masks with **mask with height**; generator, fill, levels (with a histogram of its input), invert/threshold, color selection, compare, blur, sharpen and HSL effects on masks and layer content; anchor points; effects **dragged to reorder**, duplicated, and **copied, cut and pasted** across layers, masks and sets (Ctrl+C, Ctrl+X, Ctrl+V, Ctrl+D and Delete act on a selected effect); smart masks; mask view (Alt+click); Alt+click a material channel to keep it alone; duplicate, group, drag to reorder, rename, color tags, copy/cut/paste (also as instance) across sets, flatten, instantiate across sets, save layers as smart materials; **30 library filters** (adjustments incl. histogram scan, range and shift, blur/sharpen/high pass, bevel, glow, edge detect, warps, mosaic, baked lighting, material finishes); **geometry masks** (a layer on chosen mesh parts) | A few niche filters of the reference app's catalog |
| Painting tools | 14 | 91% | Paint, eraser, projection with image stencils, polygon fill, **path** and **lasso fill**, smudge, clone, color picker, quick mask, Shift+click lines with preview, 2D view painting; size, flow, stroke opacity, spacing, hardness, angle, follow path, jitters, alignment, angle fade, backface culling, lazy mouse, **Ctrl+drag in a view for size, hardness, flow and angle**, X/Y/Z mirror and **radial symmetry**, pen pressure in the 3D and 2D views (Windows Ink, and touch pens in the browser), procedural and image alphas, **text** in any installed font (as an alpha or a decal), **particle brush** | Pen tilt, and pen pressure on Linux, macOS and desktop browsers (not exposed by the windowing layer) |
| Painting tools | 14 | 91% | Paint, eraser, projection with image stencils, polygon fill, **path** and **lasso fill**, smudge, clone, material picker, quick mask, Shift+click lines with preview, 2D view painting; size, flow, stroke opacity, spacing, hardness, angle, follow path, jitters, alignment, angle fade, backface culling, lazy mouse, **Ctrl+drag in a view for size, hardness, flow and angle**, X/Y/Z mirror and **radial symmetry**, pen pressure in the 3D and 2D views (Windows Ink, and touch pens in the browser), procedural and image alphas, **text** in any installed font (as an alpha or a decal), **particle brush** | Pen tilt, and pen pressure on Linux, macOS and desktop browsers (not exposed by the windowing layer) |
| 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, 24 smart masks, **67 procedural brush alphas**, **18 brush presets** plus user presets, 29 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 | 90% | All ten mesh-map bakers; low-poly and high-to-low baking with CPU/GPU tracing; mesh-name matching, backface filtering, custom and distance-based cages, frontal/rear distances and average normals; undoable 2D cage/skew painting saved per texture set; rectangular output, antialiasing, dilation/diffusion, ID sources, ray filters, AO attenuation/ground plane, curvature controls and map normalization; per-texture-set selection, progress/cancel and mesh-map import | Residual Painter map differences; reference validation for Height, Bent Normal and Opacity and across additional assets; 3D cage-editing overlay |
| 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 |
+24 -2
View File
@@ -1,12 +1,34 @@
//! Drives the `texturecraft-cli` binary.
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use serde_json::Value;
// Nextest 0.9.130+ remaps this variable when an archive is extracted on another
// runner. Cargo's compile-time path still supports ordinary `cargo test`.
fn cli_binary() -> OsString {
cli_binary_path(std::env::var_os("CARGO_BIN_EXE_texturecraft-cli"))
}
fn cli_binary_path(runner_path: Option<OsString>) -> OsString {
runner_path.unwrap_or_else(|| env!("CARGO_BIN_EXE_texturecraft-cli").into())
}
#[test]
fn cli_binary_prefers_relocated_runner_path() {
let relocated = OsString::from("relocated archive/texturecraft-cli");
assert_eq!(cli_binary_path(Some(relocated.clone())), relocated);
}
#[test]
fn cli_binary_defaults_to_cargo_path() {
assert_eq!(cli_binary_path(None), OsString::from(env!("CARGO_BIN_EXE_texturecraft-cli")));
}
fn cli(args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_texturecraft-cli")).args(args).output().unwrap()
Command::new(cli_binary()).args(args).output().unwrap()
}
fn ok(args: &[&str]) -> String {
@@ -101,7 +123,7 @@ fn info_on_a_mesh_reports_uvs() {
fn serve_takes_the_automation_root_spelling_too() {
use std::io::Write as _;
let d = tmp("automation-root");
let mut child = Command::new(env!("CARGO_BIN_EXE_texturecraft-cli"))
let mut child = Command::new(cli_binary())
.args(["serve", "--automation-write-root", p(&d)])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
+40 -7
View File
@@ -16,6 +16,8 @@ pub struct CageShell {
pub starts: Vec<RayStart>,
/// Corner pairs along the low poly's polygon edges, for the wireframe
pub edges: Vec<[u32; 2]>,
/// Corner pairs along the low poly's hard edges without a UV seam
pub missing_seams: Vec<[u32; 2]>,
/// World units per unit of the max frontal distance setting
pub cage_units: f32,
}
@@ -24,6 +26,30 @@ impl CageShell {
/// The cage of `request`'s texture set, from the same ray starts the bake uses
pub fn new(request: &BakeRequest) -> Result<CageShell, BakeError> {
let ray_frame = RayFrame::new(request, MeshExtent::of(request.mesh)?)?;
Self::build(request, ray_frame, || {
// Each scan drops other materials' edges before the next scan starts
let wire_edges = request.mesh.wire_edges().into_iter().nth(request.material).unwrap_or_default().into_iter().collect();
let missing_seam_edges = request.mesh.missing_seam_edges().into_iter().nth(request.material).unwrap_or_default().into_iter().collect();
(wire_edges, missing_seam_edges)
})
}
/// Build a cage using edges already scanned for every material of the low mesh
pub fn new_with_edges(request: &BakeRequest, wire_edges: &[Vec<(u32, u32)>], missing_seam_edges: &[Vec<(u32, u32)>]) -> Result<CageShell, BakeError> {
let ray_frame = RayFrame::new(request, MeshExtent::of(request.mesh)?)?;
Self::build(request, ray_frame, || {
(
wire_edges.get(request.material).into_iter().flatten().copied().collect(),
missing_seam_edges.get(request.material).into_iter().flatten().copied().collect(),
)
})
}
fn build(
request: &BakeRequest,
ray_frame: RayFrame,
edge_sets: impl FnOnce() -> (HashSet<(u32, u32)>, HashSet<(u32, u32)>),
) -> Result<CageShell, BakeError> {
let (frame, mesh) = (&ray_frame, request.mesh);
let triangles: Vec<usize> = mesh.triangles_of(request.material).collect();
let (positions, starts): (Vec<Vec3>, Vec<RayStart>) = triangles
@@ -36,8 +62,8 @@ impl CageShell {
})
.unzip();
let wire_edges: HashSet<(u32, u32)> = mesh.wire_edges().into_iter().nth(request.material).unwrap_or_default().into_iter().collect();
let mut edge_corners = HashMap::new();
let (wire_edges, missing_seam_edges) = edge_sets();
let (mut edge_corners, mut seam_corners) = (HashMap::new(), HashMap::new());
for (triangle_index, &triangle) in triangles.iter().enumerate() {
let Some(vertices) = mesh.tri(triangle) else { continue };
@@ -45,16 +71,23 @@ impl CageShell {
let next = (corner + 1) % 3;
let (from, to) = (vertices[corner] as u32, vertices[next] as u32);
let key = (from.min(to), from.max(to));
let first_corner = (triangle_index * 3) as u32;
let corners = [first_corner + corner as u32, first_corner + next as u32];
if wire_edges.contains(&key) {
let first_corner = (triangle_index * 3) as u32;
edge_corners.entry(key).or_insert([first_corner + corner as u32, first_corner + next as u32]);
edge_corners.entry(key).or_insert(corners);
}
if missing_seam_edges.contains(&key) {
seam_corners.entry(key).or_insert(corners);
}
}
}
let mut edges: Vec<[u32; 2]> = edge_corners.into_values().collect();
edges.sort_unstable();
let sorted = |corners: HashMap<(u32, u32), [u32; 2]>| {
let mut pairs: Vec<[u32; 2]> = corners.into_values().collect();
pairs.sort_unstable();
pairs
};
Ok(CageShell { positions, starts, edges, cage_units: ray_frame.cage_units })
Ok(CageShell { positions, starts, edges: sorted(edge_corners), missing_seams: sorted(seam_corners), cage_units: ray_frame.cage_units })
}
/// Each corner's cage position at `max_frontal`, the setting before relative-to-bounding-box scaling
+2
View File
@@ -9,6 +9,7 @@ mod bake_request;
mod cage;
mod high_poly;
mod mesh_extent;
mod name_pair;
mod progress;
pub use bake_error::BakeError;
@@ -17,6 +18,7 @@ pub use bake_request::BakeRequest;
pub use cage::{CageShell, HighPolyReach, RayStart, VertexReach};
pub use high_poly::HighPoly;
pub(crate) use mesh_extent::MeshExtent;
pub use name_pair::NamePair;
pub use progress::Progress;
mod map;
+2
View File
@@ -4,8 +4,10 @@ mod curvature_tuning;
mod diffusion;
mod normal_curvature;
mod normalization;
mod part_key;
pub(crate) use diffusion::diffuse;
pub(crate) use normalization::normalize;
pub(crate) use part_key::part_key;
mod sample;
mod seam;
pub(crate) use seam::fix_seams;
+18
View File
@@ -0,0 +1,18 @@
use std::collections::HashMap;
/// A mesh part's key in `keys` (shared by names that agree once `suffix` is dropped), whether it ignores backfaces, and its lowercase name without that suffix
pub(crate) fn part_key(name: &str, suffix: &str, backface_suffix: &str, keys: &mut HashMap<String, u32>) -> (u32, bool, String) {
let name = name.trim().to_lowercase();
let ignore_backface = !backface_suffix.is_empty() && name.ends_with(backface_suffix);
let name = if ignore_backface { name.strip_suffix(backface_suffix).unwrap_or(&name).to_owned() } else { name };
(match_key(&name, suffix, keys), ignore_backface, name)
}
/// The key in `keys` of the lowercase part name `name` without `suffix`
fn match_key(name: &str, suffix: &str, keys: &mut HashMap<String, u32>) -> u32 {
let suffix = suffix.trim().to_lowercase();
let base = name.strip_suffix(suffix.as_str()).filter(|_| !suffix.is_empty()).unwrap_or(name).to_string();
let next = keys.len() as u32;
*keys.entry(base).or_insert(next)
}
+9 -13
View File
@@ -1,3 +1,4 @@
use super::part_key::part_key;
use super::sampling::{generated_hue, interpolate, splitmix64};
use crate::BakeRequest;
use crate::tracing::{ANY, TraceMesh};
@@ -50,21 +51,24 @@ impl<'a> Source<'a> {
let mut mesh_ids = Vec::with_capacity(mesh.submeshes.len());
let mut part_keys = Vec::with_capacity(mesh.submeshes.len());
let backface_suffix = settings.ignore_backface_suffix.trim().to_lowercase();
for (part_index, submesh) in mesh.submeshes.iter().enumerate() {
let first_triangle = (submesh.start / 3) as usize;
let last_triangle = (submesh.start.saturating_add(submesh.count) / 3) as usize;
let range = first_triangle.min(last_triangle)..last_triangle.min(triangle_count);
part.get_mut(range.clone()).unwrap_or_default().fill(part_index as u32);
let name = submesh.name.trim().to_lowercase();
let ignore_backface = !backface_suffix.is_empty() && name.ends_with(&backface_suffix);
let (key, ignore_backface, name) = part_key(&submesh.name, suffix, &backface_suffix, keys);
backface_culling.get_mut(range).unwrap_or_default().fill(ignore_backface);
let name = if ignore_backface { name.strip_suffix(&backface_suffix).unwrap_or(&name) } else { &name };
part_keys.push(match_key(name, suffix, keys));
part_keys.push(key);
let next = mesh_keys.len() as u32;
let file_id = file_ids.get(part_index).copied().unwrap_or(0);
mesh_ids.push(*mesh_keys.entry((file_id, name.to_owned())).or_insert(next));
mesh_ids.push(*mesh_keys.entry((file_id, name)).or_insert(next));
}
let key = part.iter().map(|part_index| part_keys.get(*part_index as usize).copied().unwrap_or(ANY)).collect();
Source {
mesh,
bvh,
@@ -138,11 +142,3 @@ fn base_color(material: &BaseColor, uv: Vec2) -> Vec4 {
let color = sampled_color * material.factor;
Vec4::new(linear_to_srgb(color.x), linear_to_srgb(color.y), linear_to_srgb(color.z), color.w)
}
fn match_key(name: &str, suffix: &str, keys: &mut HashMap<String, u32>) -> u32 {
let name = name.trim().to_lowercase();
let suffix = suffix.trim().to_lowercase();
let base = name.strip_suffix(suffix.as_str()).filter(|_| !suffix.is_empty()).unwrap_or(&name).to_string();
let next = keys.len() as u32;
*keys.entry(base).or_insert(next)
}
+103
View File
@@ -0,0 +1,103 @@
//! How "By Mesh Name" pairs low-poly parts with high-poly parts, listed before a bake runs
use std::collections::{HashMap, HashSet};
use texturecraft_core::HighPolySettings;
use texturecraft_geom::Mesh;
use crate::map::part_key;
/// A low-poly part and the high-poly part its rays may hit under "By Mesh Name"; one side is missing when that part pairs with nothing
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NamePair {
/// The low-poly part's name as in its mesh
pub low: Option<String>,
/// The high-poly part's name as in its mesh
pub high: Option<String>,
}
impl NamePair {
/// Each part of `low` in `material` with every `high` part it pairs with (a row per pair), then the `high` parts pairing with none; keyed like the bake, low poly first
pub fn list(low: &Mesh, material: usize, high: &Mesh, settings: &HighPolySettings) -> Vec<NamePair> {
let backface_suffix = settings.ignore_backface_suffix.trim().to_lowercase();
let mut keys = HashMap::new();
let mut keyed = |mesh: &Mesh, suffix: &str, only_material: Option<usize>| {
let mut seen = HashSet::new();
let mut parts = Vec::new();
for part in mesh.submeshes.iter().filter(|part| only_material.is_none_or(|material| part.material == material)) {
let (key, ..) = part_key(&part.name, suffix, &backface_suffix, &mut keys);
if seen.insert(part.name.as_str()) {
parts.push((key, part.name.clone()));
}
}
parts
};
let low_parts = keyed(low, &settings.low_suffix, Some(material));
let high_parts = keyed(high, &settings.high_suffix, None);
let mut pairs = Vec::new();
for (key, low_name) in &low_parts {
let matches: Vec<&String> = high_parts.iter().filter(|(high_key, _)| high_key == key).map(|(_, name)| name).collect();
if matches.is_empty() {
pairs.push(NamePair { low: Some(low_name.clone()), high: None });
}
pairs.extend(matches.into_iter().map(|high_name| NamePair { low: Some(low_name.clone()), high: Some(high_name.clone()) }));
}
let low_keys: HashSet<u32> = low_parts.iter().map(|(key, _)| *key).collect();
pairs.extend(high_parts.into_iter().filter(|(key, _)| !low_keys.contains(key)).map(|(_, name)| NamePair { low: None, high: Some(name) }));
pairs
}
/// Whether one side is missing: a part the bake will leave unpaired
pub fn unpaired(&self) -> bool {
self.low.is_none() || self.high.is_none()
}
}
#[cfg(test)]
mod tests {
use super::*;
use texturecraft_geom::SubMesh;
/// A mesh with one empty part per (name, material)
fn parts(names: &[(&str, usize)]) -> Mesh {
Mesh {
submeshes: names.iter().map(|(name, material)| SubMesh { name: (*name).into(), material: *material, start: 0, count: 0 }).collect(),
materials: vec!["first".into(), "second".into()],
..Default::default()
}
}
fn pair(low: Option<&str>, high: Option<&str>) -> NamePair {
NamePair { low: low.map(Into::into), high: high.map(Into::into) }
}
#[test]
fn parts_pair_by_name_without_their_suffixes() {
let low = parts(&[("Barrel_low", 0), ("Base_low", 0), ("Slide_low", 1)]);
let high = parts(&[("Barrel_high", 0), ("Slide_high", 0), ("Grip_high", 0), ("BARREL_high_ignorebf", 0)]);
let settings = HighPolySettings::default();
assert_eq!(
NamePair::list(&low, 0, &high, &settings),
vec![
pair(Some("Barrel_low"), Some("Barrel_high")),
pair(Some("Barrel_low"), Some("BARREL_high_ignorebf")),
pair(Some("Base_low"), None),
pair(None, Some("Slide_high")),
pair(None, Some("Grip_high")),
],
"another material's parts are left out, so its high-poly part counts as unpaired here"
);
assert_eq!(NamePair::list(&low, 1, &high, &settings).iter().filter(|pair| !pair.unpaired()).count(), 1);
}
#[test]
fn a_part_split_over_triangle_ranges_is_listed_once() {
let low = parts(&[("Body_low", 0), ("Body_low", 0)]);
let high = parts(&[("Body_high", 0), ("Body_high", 0)]);
assert_eq!(NamePair::list(&low, 0, &high, &HighPolySettings::default()), vec![pair(Some("Body_low"), Some("Body_high"))]);
}
}
+89
View File
@@ -101,3 +101,92 @@ fn the_cage_follows_the_frontal_distance_and_a_cage_mesh() {
assert!(corners.iter().all(|corner| (corner.abs().max_element() - 0.75).abs() < 1e-6), "the cage mesh is drawn as it is");
assert!(corners.iter().all(|corner| corner.is_finite() && *corner != Vec3::ZERO));
}
#[test]
fn the_cage_lists_its_hard_edges_without_a_uv_seam() {
// A quad whose diagonal is split into two vertices with the same UV, smooth or hard by the copy's normal
let quad_with_diagonal_normal = |normal: Vec3| {
let mut mesh = Mesh::from_parts(
vec![Vec3::ZERO, Vec3::X, Vec3::new(1.0, 1.0, 0.0), Vec3::Y],
None,
vec![Vec2::new(0.0, 1.0), Vec2::new(1.0, 1.0), Vec2::new(1.0, 0.0), Vec2::new(0.0, 0.0)],
vec![0, 1, 2, 0, 2, 3],
"Default",
)
.unwrap();
mesh.positions.push(mesh.positions[0]);
mesh.normals.push(normal);
mesh.tangents.push(mesh.tangents[0]);
mesh.uvs.push(mesh.uvs[0]);
mesh.indices[3] = 4;
mesh
};
let shell_of = |low: &Mesh| {
let bvh = Bvh::new(low);
let bake_settings = settings(16, &[BakedMap::Normal]);
let request = BakeRequest {
low_base_color: &[],
mesh: low,
bvh: &bvh,
material: 0,
settings: &bake_settings,
high: Some(HighPoly { file_ids: &[], mesh: low, bvh: &bvh, base_color: &[] }),
cage: None,
skew: None,
cage_paint: None,
tiles: (1, 1),
};
let shell = CageShell::new(&request).unwrap();
let shared = CageShell::new_with_edges(&request, &low.wire_edges(), &low.missing_seam_edges()).unwrap();
assert_eq!(shared.positions, shell.positions);
assert_eq!(shared.corners(0.1), shell.corners(0.1));
assert_eq!(shared.edges, shell.edges);
assert_eq!(shared.missing_seams, shell.missing_seams);
shell
};
let smooth = quad_with_diagonal_normal(Vec3::Z);
assert!(shell_of(&smooth).missing_seams.is_empty(), "the same normal on both sides is a smooth edge");
let hard = quad_with_diagonal_normal(Vec3::new(1.0, 0.0, 1.0).normalize());
let shell = shell_of(&hard);
let diagonal = [Vec3::ZERO, Vec3::new(1.0, 1.0, 0.0)];
assert_eq!(shell.missing_seams.len(), 2, "the diagonal, once per triangle");
for [from, to] in &shell.missing_seams {
let mut ends = [shell.positions[*from as usize], shell.positions[*to as usize]];
ends.sort_by(|a, b| a.x.total_cmp(&b.x));
assert_eq!(ends, diagonal, "the line runs along the hard diagonal");
}
}
#[test]
fn shared_edges_select_the_requested_material() {
let mut mesh = primitives::cube(1.0);
mesh.append(&primitives::cube(1.0));
let bvh = Bvh::new(&mesh);
let bake_settings = settings(16, &[BakedMap::Normal]);
let wire_edges = mesh.wire_edges();
let missing_seam_edges = mesh.missing_seam_edges();
for material in 0..mesh.materials.len() {
let request = BakeRequest {
low_base_color: &[],
mesh: &mesh,
bvh: &bvh,
material,
settings: &bake_settings,
high: None,
cage: None,
skew: None,
cage_paint: None,
tiles: (1, 1),
};
let direct = CageShell::new(&request).unwrap();
let shared = CageShell::new_with_edges(&request, &wire_edges, &missing_seam_edges).unwrap();
assert_eq!(shared.positions, direct.positions);
assert_eq!(shared.starts, direct.starts);
assert_eq!(shared.edges, direct.edges);
assert_eq!(shared.missing_seams, direct.missing_seams);
}
}
+115 -44
View File
@@ -10,10 +10,12 @@ use std::collections::{BTreeSet, HashMap, VecDeque};
use std::sync::Arc;
use texturecraft_core::project::MAX_DEPTH;
use texturecraft_core::{BakedMap, Channel, Layer, LayerId, LayerKind, Project, TextureSet};
use texturecraft_core::{BakedMap, Channel, EffectKind, Layer, LayerId, LayerKind, Project, Source, TextureSet};
use texturecraft_texel::TileImage;
use crate::eval::{Inputs, PaintTarget};
use crate::held_fields::HeldFields;
use crate::image_identity::ImageIdentity;
use crate::store::{BakedMaps, ImageStore, PixelStore};
/// Default byte budget of a [`BelowCache`], per nesting level it holds (see
@@ -40,11 +42,28 @@ pub struct BelowCache {
/// Tiles of the whole pass a caller splits into several composites (0: each composite is a
/// pass). A pass larger than the cache records only what fits.
pub pass: usize,
/// Tiles the caller composites next in this pass, after those of the coming composite (empty: none, or a pass that is one
/// composite). A stack with neighbourhood filters then filters the coming tiles and these at once and keeps the result for
/// the composites that follow, instead of filtering each small chunk's surroundings again.
pub ahead: Vec<(u32, u32)>,
pub(crate) held: Option<HeldFields>,
}
impl Default for BelowCache {
fn default() -> Self {
BelowCache { hot: None, key: None, tiles: HashMap::new(), order: VecDeque::new(), clock: 0, bytes: 0, budget: BELOW_BUDGET, levels: 1, pass: 0 }
BelowCache {
hot: None,
key: None,
tiles: HashMap::new(),
order: VecDeque::new(),
clock: 0,
bytes: 0,
budget: BELOW_BUDGET,
levels: 1,
pass: 0,
ahead: Vec::new(),
held: None,
}
}
}
@@ -78,48 +97,83 @@ pub struct StackKey {
layers: Vec<Layer>,
pixels: Vec<(LayerId, Option<Channel>, Shared)>,
baked: (u32, Vec<(BakedMap, usize)>),
images: Vec<(String, usize)>,
images: Vec<(String, ImageIdentity)>,
}
impl StackKey {
/// The key of `roots` (layers of texture set `ts`, bottom → top) and their effects.
pub fn new(project: &Project, pixels: &PixelStore, baked: &BakedMaps, images: &ImageStore, ts: usize, roots: &[LayerId]) -> Option<StackKey> {
StackKey::of_tree(project, pixels, baked, images, ts, roots.to_vec(), &project.render_tree(roots))
/// The key of `roots` (layers of a texture set, bottom → top) and their effects.
pub fn new(
project: &Project,
pixels: &PixelStore,
baked: &BakedMaps,
images: &ImageStore,
texture_set_index: usize,
roots: &[LayerId],
) -> Option<StackKey> {
StackKey::of_tree(project, pixels, baked, images, texture_set_index, roots.to_vec(), &project.render_tree(roots))
}
/// The key of the layers in `tree` (and their effects), drawn as `roots` arrange them.
/// The key of the layers in `render_tree` (and their effects), drawn as `roots` arrange them.
fn of_tree(
project: &Project,
pixels: &PixelStore,
baked: &BakedMaps,
images: &ImageStore,
ts: usize,
texture_set_index: usize,
roots: Vec<LayerId>,
tree: &BTreeSet<LayerId>,
render_tree: &BTreeSet<LayerId>,
) -> Option<StackKey> {
let mut set = project.texture_set(ts).ok()?.clone();
let mut set = project.texture_set(texture_set_index).ok()?.clone();
set.name.clear();
set.description.clear();
set.stack.clear();
set.bake = Default::default();
let effects: BTreeSet<LayerId> = tree.iter().filter_map(|id| project.layers.get(id)).flat_map(effect_ids).collect();
let pixels = tree
let effect_ids: BTreeSet<LayerId> = render_tree.iter().filter_map(|id| project.layers.get(id)).flat_map(effect_ids).collect();
let pixel_handles = render_tree
.iter()
.chain(&effects)
.chain(&effect_ids)
.flat_map(|id| {
let layer = Channel::ALL.into_iter().filter_map(move |c| pixels.layer(*id, c).map(|img| (*id, Some(c), Shared(img.clone()))));
layer.chain(pixels.masks.get(id).map(|img| (*id, None, Shared(img.clone()))))
let layer_channels =
Channel::ALL.into_iter().filter_map(move |channel| pixels.layer(*id, channel).map(|image| (*id, Some(channel), Shared(image.clone()))));
layer_channels.chain(pixels.masks.get(id).map(|image| (*id, None, Shared(image.clone()))))
})
.collect();
let mut images: Vec<(String, usize)> = images.iter().map(|(n, i)| (n.clone(), Arc::as_ptr(i) as usize)).collect();
images.sort();
let mut referenced_images = BTreeSet::new();
if !images.is_empty() {
let mut record_image_reference = |source: &Source| {
if let Source::Image { name, .. } = source
&& let Some((name, _)) = images.get_key_value(name)
{
referenced_images.insert(name);
}
};
for layer in render_tree.iter().filter_map(|id| project.layers.get(id)) {
if let LayerKind::Fill(fill) = &layer.kind {
fill.sources.values().for_each(&mut record_image_reference);
}
for effect in layer.mask.iter().flat_map(|mask| &mask.effects).chain(&layer.content_effects) {
match &effect.kind {
EffectKind::Fill { source, .. } => record_image_reference(source),
EffectKind::Material(fill) => fill.sources.values().for_each(&mut record_image_reference),
EffectKind::Generator(generator) => generator.inputs.values().for_each(&mut record_image_reference),
_ => {}
}
}
}
}
let image_identities =
referenced_images.into_iter().filter_map(|name| images.get(name).map(|image| (name.clone(), ImageIdentity::new(image)))).collect();
Some(StackKey {
set,
roots,
layers: tree.iter().filter_map(|id| project.layers.get(id)).map(render_view).collect(),
pixels,
baked: (baked.resolution, baked.maps.iter().map(|(m, d)| (*m, Arc::as_ptr(d) as usize)).collect()),
images,
layers: render_tree.iter().filter_map(|id| project.layers.get(id)).map(render_view).collect(),
pixels: pixel_handles,
baked: (baked.resolution, baked.maps.iter().map(|(map, data)| (*map, Arc::as_ptr(data) as usize)).collect()),
images: image_identities,
})
}
}
@@ -173,23 +227,30 @@ impl BelowCache {
pub fn clear(&mut self) {
let tiles = std::mem::take(&mut self.tiles);
// Freeing a full cache (up to the byte budget in small bands) can take most of a
// second: a thread does it so the frame that switched the hot layer doesn't stall.
// Native builds free cached bands off-thread to avoid a frame stall
#[cfg(not(target_arch = "wasm32"))]
if !tiles.is_empty() {
let _ = std::thread::Builder::new().name("texturecraft-free".into()).spawn(move || drop(tiles));
}
#[cfg(target_arch = "wasm32")]
drop(tiles);
self.order.clear();
self.bytes = 0;
self.key = None;
self.held = None;
}
pub fn bytes(&self) -> usize {
self.bytes
}
/// Whether neighbourhood-filter fields are kept for the composites that follow (see [`BelowCache::ahead`]).
pub fn holds_fields(&self) -> bool {
self.held.is_some()
}
/// The byte limit: the budget for the top level and as much again for a hot layer nested in
/// folders (each tile then holds the stack at two or more levels).
pub fn limit(&self) -> usize {
@@ -219,7 +280,7 @@ impl BelowCache {
self.clock += 1;
*stamp = self.clock;
self.order.push_back((t, self.clock));
// Drop superseded uses once they outnumber the tiles.
// Drop superseded uses once they outnumber the tiles
if self.order.len() > 2 * self.tiles.len() + 64 {
let tiles = &self.tiles;
self.order.retain(|(t, s)| tiles.get(t).is_some_and(|(stamp, _)| stamp == s));
@@ -256,43 +317,53 @@ impl BelowCache {
/// live stroke). Empties the cache when what the cached part reads changed.
pub(crate) fn prepare(&mut self, inp: &Inputs) -> Option<Vec<Level>> {
let hot = self.hot?;
let p = inp.project;
let set = p.texture_set(inp.ts).ok()?;
// Anchor readers depend on the stack below them.
if p.render_tree(&set.stack).iter().any(|id| p.layers.get(id).is_some_and(Layer::reads_anchor)) {
let project = inp.project;
let set = project.texture_set(inp.ts).ok()?;
// Anchor readers depend on the stack below them
if project.render_tree(&set.stack).iter().any(|id| project.layers.get(id).is_some_and(Layer::reads_anchor)) {
return None;
}
let levels = path(p, &set.stack, hot)?;
let rest: Vec<LayerId> = levels.iter().flat_map(|l| l.below.iter().chain(&l.above)).copied().collect();
if rest.is_empty() && levels.len() == 1 {
let levels = path(project, &set.stack, hot)?;
let off_path_roots: Vec<LayerId> = levels.iter().flat_map(|level| level.below.iter().chain(&level.above)).copied().collect();
if off_path_roots.is_empty() && levels.len() == 1 {
return None;
}
let mut tree = p.render_tree(&rest);
// Folders holding the hot layer (and the folders their instances draw) count with their
// own state only, not their children.
for l in levels.iter().take(levels.len() - 1) {
let mut id = Some(l.at);
let mut cached_tree = project.render_tree(&off_path_roots);
// Cache containing folders by state, without their children
for level in levels.iter().take(levels.len() - 1) {
let mut id = Some(level.at);
for _ in 0..=MAX_DEPTH {
let Some(i) = id.filter(|i| tree.insert(*i)) else { break };
id = p.layers.get(&i).and_then(Layer::instance_source);
let Some(instance_id) = id.filter(|instance_id| cached_tree.insert(*instance_id)) else { break };
id = project.layers.get(&instance_id).and_then(Layer::instance_source);
}
}
if tree.contains(&hot) {
// Repeated hot instances cannot share the off-path cache
if cached_tree.contains(&hot) {
return None;
}
if let Some(pv) = inp.preview {
let (PaintTarget::Layer(t) | PaintTarget::Mask(t)) = pv.target;
if tree.contains(&t) || tree.iter().filter_map(|id| p.layers.get(id)).flat_map(effect_ids).any(|e| e == t) {
if let Some(preview) = inp.preview {
let (PaintTarget::Layer(target) | PaintTarget::Mask(target)) = preview.target;
if cached_tree.contains(&target)
|| cached_tree.iter().filter_map(|id| project.layers.get(id)).flat_map(effect_ids).any(|effect_id| effect_id == target)
{
return None;
}
}
let roots = set.stack.iter().chain(levels.iter().map(|l| &l.at)).copied().collect();
let key = StackKey::of_tree(p, inp.pixels, inp.baked, inp.images, inp.ts, roots, &tree)?;
let roots = set.stack.iter().chain(levels.iter().map(|level| &level.at)).copied().collect();
let key = StackKey::of_tree(project, inp.pixels, inp.baked, inp.images, inp.ts, roots, &cached_tree)?;
self.levels = levels.len();
if self.key.as_ref() != Some(&key) {
self.clear();
self.key = Some(key);
}
Some(levels)
}
}
+39 -10
View File
@@ -125,8 +125,8 @@ fn store(curvature: &Arc<Vec<f32>>, key: CacheKey, scales: &BlockScales, sizes:
sizes
};
// One layout per curvature map, with the most recently used map at the end
cache.retain(|(source, ..)| source.strong_count() > 0 && !std::ptr::eq(source.as_ptr(), Arc::as_ptr(curvature)));
// Keep layout variants so thumbnails and the viewport reuse their own levels
cache.retain(|entry| entry.0.strong_count() > 0 && !is_entry_for(entry, curvature, key, scales));
let sizes = Arc::new(sizes);
cache.push((Arc::downgrade(curvature), key, scales.clone(), sizes.clone()));
trim_cache(&mut cache, CACHE_BYTES);
@@ -338,6 +338,7 @@ mod tests {
#[test]
fn unused_maps_are_evicted_even_while_undo_keeps_the_sources_alive() {
let _guard = CACHE_TESTS.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
let sources: Vec<_> = (0..=CACHE_MAPS).map(|_| Arc::new(vec![0.5; 16])).collect();
let scales = BlockScales::default();
let key = ((4, 4), (4, 4), (4, 4));
@@ -408,18 +409,46 @@ mod tests {
}
}
// Cache lifecycle tests must not evict each other's entries in parallel
static CACHE_TESTS: Mutex<()> = Mutex::new(());
#[test]
fn a_new_layout_replaces_the_sizes_of_the_old_one() {
fn alternating_layouts_reuse_their_cached_sizes() {
let _guard = CACHE_TESTS.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
let curvature = Arc::new(vec![0.5; 16]);
let scales = BlockScales::default();
let entries_for_curvature = || {
let cache = CACHE.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
cache.iter().filter(|(source, ..)| std::ptr::eq(source.as_ptr(), Arc::as_ptr(&curvature))).count()
};
let viewport_key = ((4, 4), (4, 4), (4, 4));
let thumbnail_key = ((4, 4), (4, 4), (2, 2));
let viewport = store(&curvature, viewport_key, &scales, CurvatureSizes { levels: Vec::new() });
let thumbnail = store(&curvature, thumbnail_key, &scales, CurvatureSizes { levels: Vec::new() });
store(&curvature, ((4, 4), (4, 4), (4, 4)), &scales, CurvatureSizes { levels: Vec::new() });
store(&curvature, ((4, 4), (2, 2), (8, 8)), &scales, CurvatureSizes { levels: Vec::new() });
assert_eq!(entries_for_curvature(), 1, "only the latest layout of a curvature map stays cached");
assert!(Arc::ptr_eq(&cached(&curvature, viewport_key, &scales).unwrap(), &viewport), "a thumbnail must not evict the viewport layout");
assert!(Arc::ptr_eq(&cached(&curvature, thumbnail_key, &scales).unwrap(), &thumbnail), "both layouts stay reusable");
let mut set = texturecraft_core::TextureSet::new("Tiles", 0, 4);
set.uv_tiles = vec![1001, 1002];
set.tile_resolutions.insert(1002, 2);
let scaled = set.block_scales();
assert_ne!(scaled, scales);
assert!(cached(&curvature, viewport_key, &scaled).is_none(), "different tile scales cannot read the unscaled result");
let scaled_sizes = store(&curvature, viewport_key, &scaled, CurvatureSizes { levels: Vec::new() });
assert!(Arc::ptr_eq(&cached(&curvature, viewport_key, &scaled).unwrap(), &scaled_sizes));
assert!(Arc::ptr_eq(&cached(&curvature, viewport_key, &scales).unwrap(), &viewport), "scale variants also coexist");
let replacement = store(&curvature, viewport_key, &scales, CurvatureSizes { levels: Vec::new() });
assert!(Arc::ptr_eq(&cached(&curvature, viewport_key, &scales).unwrap(), &replacement));
assert!(Arc::ptr_eq(&cached(&curvature, thumbnail_key, &scales).unwrap(), &thumbnail), "updating one layout keeps the other");
}
#[test]
fn layout_variants_obey_the_entry_limit() {
let _guard = CACHE_TESTS.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
let curvature = Arc::new(vec![0.5; 16]);
let scales = BlockScales::default();
let key = |size| ((4, 4), (4, 4), (size, size));
for size in 1..=CACHE_MAPS as u32 + 1 {
store(&curvature, key(size), &scales, CurvatureSizes { levels: Vec::new() });
}
assert!(cached(&curvature, key(1), &scales).is_none(), "layout variants cannot grow without bound");
assert!(cached(&curvature, key(CACHE_MAPS as u32 + 1), &scales).is_some(), "the newest layout stays cached");
}
#[test]
+595 -155
View File
File diff suppressed because it is too large. Load diff
+24
View File
@@ -0,0 +1,24 @@
use std::collections::BTreeSet;
use std::sync::Arc;
use crate::below::StackKey;
use crate::eval::Fields;
/// Neighbourhood-filter fields kept between the composites of one pass.
pub(crate) struct HeldFields {
/// Everything the fields read.
pub key: Arc<StackKey>,
/// The working size they were built at.
pub size: (u32, u32),
/// The tiles every layer's fields hold.
pub stack_tiles: BTreeSet<(u32, u32)>,
/// The tiles the hot layer's fields hold (the same tiles when the stack was not split).
pub hot_tiles: BTreeSet<(u32, u32)>,
pub fields: Arc<Fields>,
}
impl std::fmt::Debug for HeldFields {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HeldFields").field("stack_tiles", &self.stack_tiles.len()).field("hot_tiles", &self.hot_tiles.len()).finish_non_exhaustive()
}
}
+19
View File
@@ -0,0 +1,19 @@
use std::sync::{Arc, Weak};
use crate::store::ImageRes;
/// Keeps an image's allocation identity stable without retaining pixels after resource removal.
#[derive(Debug)]
pub(crate) struct ImageIdentity(Weak<ImageRes>);
impl ImageIdentity {
pub(crate) fn new(image: &Arc<ImageRes>) -> Self {
Self(Arc::downgrade(image))
}
}
impl PartialEq for ImageIdentity {
fn eq(&self, other: &Self) -> bool {
self.0.ptr_eq(&other.0)
}
}
+4 -2
View File
@@ -13,6 +13,8 @@ mod curvature_size_level;
pub mod curvature_sizes;
pub mod display;
pub mod eval;
mod held_fields;
mod image_identity;
pub mod pattern;
pub mod store;
pub mod thumb;
@@ -20,8 +22,8 @@ pub mod thumb;
pub use below::{BelowCache, StackKey};
pub use display::{DisplayKind, tile_bytes};
pub use eval::{
Composite, Inputs, PaintTarget, Stencil, StrokePreview, all_tiles, composite_tiles, composite_tiles_cached, covered_tiles, effect_histogram, flatten_layer,
mask_at, mask_image, pad_tiles,
BLOCK, Composite, Inputs, PaintTarget, Stencil, StrokePreview, all_tiles, composite_tiles, composite_tiles_cached, covered_tiles, effect_histogram,
flatten_layer, mask_at, mask_image, pad_tiles,
};
pub use store::{BakedMaps, ImageRes, ImageStore, PixelStore};
pub use thumb::{THUMB_TEXELS, ThumbImage, ThumbTarget};
+261 -2
View File
@@ -6,8 +6,8 @@ use std::collections::BTreeMap;
use texturecraft_compose::{BelowCache, Composite, Inputs, PaintTarget, PixelStore, StrokePreview, all_tiles, composite_tiles, composite_tiles_cached};
use texturecraft_core::{
BlendMode, Channel, Effect, EffectKind, Fill, Filter, Generator, GeneratorKind, HeightMask, Layer, LayerId, LayerKind, Mask, MaskBackground, Procedural,
ProceduralKind, Project, Projection, Source, TextureSet,
BakedMap, BlendMode, Channel, Effect, EffectKind, Fill, Filter, Generator, GeneratorKind, HeightMask, Layer, LayerId, LayerKind, Mask, MaskBackground,
Procedural, ProceduralKind, Project, Projection, Source, TextureSet,
};
use texturecraft_geom::{Mesh, UvGBuffer, primitives};
use texturecraft_texel::{TILE, TileImage, to_u16};
@@ -573,3 +573,262 @@ fn gathering_filters_composite_identically_in_any_grouping_and_cached() {
assert!(!check_cached(&f, 0, hot).is_empty());
}
}
/// Composite every tile in chunks of `chunk`, telling the cache the `ahead` tiles that follow each chunk, as a pass spread over frames does.
fn run_sliced(fixture: &Fixture, cache: &mut BelowCache, chunk: usize, ahead: usize) -> Composite {
let all = all_tiles(RES, RES);
let (images, baked) = (Default::default(), Default::default());
let mut out = Composite::new(&fixture.project.texture_sets[0].channel_list(), RES);
for (index, tiles) in all.chunks(chunk).enumerate() {
let done = (index * chunk + tiles.len()).min(all.len());
cache.ahead = all.iter().skip(done).take(ahead).copied().collect();
composite_tiles_cached(&inputs(fixture, 0, &images, &baked), &mut out, tiles, Some(&mut *cache));
assert_eq!(cache.holds_fields(), !cache.ahead.is_empty(), "fields are kept exactly while tiles follow (chunk {chunk}, ahead {ahead})");
}
out
}
#[test]
fn filter_fields_kept_across_the_chunks_of_a_pass_composite_exactly() {
let mut fixture = fixture();
let top = heavy_stack(&mut fixture);
let blurred_fill = fixture.project.texture_sets[0].stack[1];
let expected = reference(&fixture, 0);
for hot in [None, Some(top), Some(blurred_fill)] {
// Exercise reuse within and recomputation beyond retained runs
for (chunk, ahead) in [(1, 1), (1, 5), (3, 4), (5, 16), (2, 0)] {
let mut cache = BelowCache::default();
cache.set_hot(hot);
assert!(run_sliced(&fixture, &mut cache, chunk, ahead) == expected, "a cold sliced pass differs (hot {hot:?}, chunk {chunk}, ahead {ahead})");
// A warm hot-layer cache needs fewer fields
assert!(run_sliced(&fixture, &mut cache, chunk, ahead) == expected, "a warm sliced pass differs (hot {hot:?}, chunk {chunk}, ahead {ahead})");
}
}
// Evicted tiles need fields from every layer again
let mut full = BelowCache::default();
full.set_hot(Some(top));
run(&fixture, 0, &all_tiles(RES, RES), Some(&mut full));
let tile_bytes = full.tile_bytes().unwrap();
for tiles_held in [2, 5, 9] {
for (chunk, ahead) in [(1, 5), (3, 4), (2, 16)] {
let mut cache = BelowCache::default();
cache.budget = tiles_held * tile_bytes;
cache.set_hot(Some(top));
for pass in 0..3 {
assert!(
run_sliced(&fixture, &mut cache, chunk, ahead) == expected,
"pass {pass} differs with {tiles_held} cached tiles (chunk {chunk}, ahead {ahead})"
);
}
}
}
}
#[test]
fn kept_filter_fields_are_not_used_after_an_edit_or_under_a_live_stroke() {
let mut fixture = fixture();
let top = heavy_stack(&mut fixture);
let all = all_tiles(RES, RES);
let (first, rest) = all.split_at(3);
let mut cache = BelowCache::default();
cache.set_hot(Some(top));
// Keep fields for the remaining tiles
cache.ahead = rest.to_vec();
let mut out = run(&fixture, 0, first, Some(&mut cache));
assert!(cache.holds_fields());
// Changing the hot mask must invalidate the retained fields
let mask = fixture.project.layer_mut(top).unwrap().mask.as_mut().unwrap();
mask.effects[1].kind = EffectKind::Filter(Filter::Blur { radius: 6.0 });
let (images, baked) = (Default::default(), Default::default());
cache.ahead = Vec::new();
composite_tiles_cached(&inputs(&fixture, 0, &images, &baked), &mut out, &all, Some(&mut cache));
assert!(out == reference(&fixture, 0), "fields kept from before an edit were used after it");
assert!(!cache.holds_fields());
// A live stroke must bypass retained fields
let preview = stroke(top);
cache.ahead = rest.to_vec();
let mut live = run_with(&fixture, 0, first, Some(&mut cache), Some(&preview));
assert!(!cache.holds_fields());
let mut inp = inputs(&fixture, 0, &images, &baked);
inp.preview = Some(&preview);
composite_tiles_cached(&inp, &mut live, rest, Some(&mut cache));
assert!(live == run_with(&fixture, 0, &all, None, Some(&preview)), "a live stroke over a sliced pass differs");
}
/// Kept fields hold a layer off the hot path only over the tiles the cache lacked when they were built. A tile the cache drops
/// while they are in use needs that layer's fields again.
#[test]
fn tiles_the_cache_drops_during_a_kept_run_are_filtered_again() {
const SIZE: u32 = 512;
let mesh = primitives::sphere(1.0, 32, 16);
let gbuf = UvGBuffer::new(&mesh, 0, SIZE, SIZE);
let mut project = Project::new("t", "builtin:sphere");
project.texture_sets.push(TextureSet::new("A", 0, SIZE));
let mut fixture = Fixture { project, mesh, gbuf, pixels: PixelStore::default() };
add(&mut fixture, 0, fill(&[(Channel::BaseColor, procedural(ProceduralKind::Clouds, 0.1, 0.9))]), None);
// Pattern detail exposes any missing blur field
let mut blurred = Mask::new(MaskBackground::Black);
blurred.effects.push(effect(&mut fixture, EffectKind::Fill { source: procedural(ProceduralKind::Checker, 0.0, 1.0), projection: Projection::default() }));
blurred.effects.push(effect(&mut fixture, EffectKind::Filter(Filter::Blur { radius: 40.0 })));
add(&mut fixture, 0, fill(&[(Channel::Roughness, procedural(ProceduralKind::Grunge, 0.0, 1.0))]), Some(blurred));
// Keep the blurred layer visible under the hot layer
let hot = add(&mut fixture, 0, LayerKind::Paint, None);
paint_rect_at(&mut fixture, hot, SIZE, (10, 10), (40, 40), 0.2);
let mut all = all_tiles(SIZE, SIZE);
all.sort_by_key(|&(x, y)| (y, x));
let channels = fixture.project.texture_sets[0].channel_list();
let (images, baked) = (Default::default(), Default::default());
let mut expected = Composite::new(&channels, SIZE);
composite_tiles(&inputs(&fixture, 0, &images, &baked), &mut expected, &all);
// Prime the cache with only the two bottom rows
let rows = (SIZE / TILE) as usize;
let (above, bottom) = all.split_at(all.len() - 2 * rows);
let mut cache = BelowCache::default();
cache.set_hot(Some(hot));
let mut out = Composite::new(&channels, SIZE);
composite_tiles_cached(&inputs(&fixture, 0, &images, &baked), &mut out, bottom, Some(&mut cache));
assert_eq!(cache.len(), bottom.len());
cache.budget = cache.bytes();
// Cache eviction during the run must rebuild missing lower-layer fields
let mut out = Composite::new(&channels, SIZE);
for (index, tile) in all.iter().enumerate() {
cache.ahead = all.iter().skip(index + 1).copied().collect();
composite_tiles_cached(&inputs(&fixture, 0, &images, &baked), &mut out, &[*tile], Some(&mut cache));
if index == 0 {
assert!(cache.holds_fields());
}
}
assert!(above.len() > bottom.len());
assert!(out == expected, "a tile dropped from the cache was composited with fields that lack the layers off the hot path");
}
/// A stroke that starts while fields are kept must not leave them allocated for as long as it lasts, even though tiles still follow.
#[test]
fn a_live_stroke_releases_the_fields_kept_before_it_started() {
let mut fixture = fixture();
let top = heavy_stack(&mut fixture);
let all = all_tiles(RES, RES);
let (first, rest) = all.split_at(3);
let mut cache = BelowCache::default();
cache.set_hot(Some(top));
cache.ahead = rest.to_vec();
run(&fixture, 0, first, Some(&mut cache));
assert!(cache.holds_fields());
// A stroke must release fields even while tiles remain
let preview = stroke(top);
let mut live = run_with(&fixture, 0, first, Some(&mut cache), Some(&preview));
assert!(!cache.holds_fields(), "fields kept before the stroke stay allocated while it lasts");
let (images, baked) = (Default::default(), Default::default());
let mut inp = inputs(&fixture, 0, &images, &baked);
inp.preview = Some(&preview);
composite_tiles_cached(&inp, &mut live, rest, Some(&mut cache));
assert!(live == run_with(&fixture, 0, &all, None, Some(&preview)), "a stroke that starts during a sliced pass differs");
}
#[test]
fn cached_stack_tracks_referenced_images_only() {
use std::sync::Arc;
use texturecraft_compose::{ImageRes, ImageStore};
let mut fixture = fixture();
let hot_layer = heavy_stack(&mut fixture);
let bottom_layer = fixture.project.texture_sets[0].stack[0];
let LayerKind::Fill(fill) = &mut fixture.project.layer_mut(bottom_layer).unwrap().kind else { panic!("expected fill") };
fill.sources.insert(Channel::Roughness, Source::Image { name: "used".into(), normal_format: Default::default() });
let image_with_value = |value| Arc::new(ImageRes { width: RES, height: RES, rgba: vec![[value; 4]; (RES * RES) as usize], srgb: false });
let mut images = ImageStore::default();
let baked = Default::default();
let tiles = all_tiles(RES, RES);
let mut cache = BelowCache::default();
cache.set_hot(Some(hot_layer));
let mut output = Composite::new(&fixture.project.texture_sets[0].channel_list(), RES);
let mut previous_image = None;
for stage in ["cold", "resolve", "unused1", "unused2", "unused3", "replace", "remove"] {
match stage {
"resolve" => {
let image = image_with_value(1.0);
previous_image = Some(Arc::downgrade(&image));
images.insert("used".into(), image);
}
"replace" => {
let replacement = image_with_value(0.4);
let replacement_weak = Arc::downgrade(&replacement);
images.insert("used".into(), replacement);
assert!(previous_image.as_ref().is_some_and(|old| old.upgrade().is_none()), "cached key must not retain replaced image pixels");
previous_image = Some(replacement_weak);
}
"remove" => {
images.remove("used");
assert!(previous_image.as_ref().is_some_and(|old| old.upgrade().is_none()), "cached key must not retain removed image pixels");
}
"cold" => {}
_ => {
images.insert(stage.into(), image_with_value(0.5));
}
}
fixture.pixels.layer_mut(hot_layer, Channel::BaseColor, (RES, RES)).set(5, 5, &[stage.len() as u16, 0, 0, u16::MAX]);
let input = inputs(&fixture, 0, &images, &baked);
composite_tiles_cached(&input, &mut output, &tiles[..1], Some(&mut cache));
assert!(!cache.is_empty(), "{stage}: first tile should be cached");
let retained = cache.len() - 1;
composite_tiles_cached(&input, &mut output, &tiles[1..], Some(&mut cache));
assert_eq!(retained, if stage.starts_with("unused") { tiles.len() - 1 } else { 0 }, "{stage}");
let mut expected = Composite::new(&fixture.project.texture_sets[0].channel_list(), RES);
composite_tiles(&input, &mut expected, &tiles);
assert!(output == expected, "{stage}");
}
}
#[test]
fn generator_input_image_changes_invalidate_stack_key() {
use std::sync::Arc;
use texturecraft_compose::{ImageRes, ImageStore, StackKey};
let mut fixture = fixture();
heavy_stack(&mut fixture);
let generated_layer = fixture.project.texture_sets[0].stack[1];
let mask = fixture.project.layer_mut(generated_layer).unwrap().mask.as_mut().unwrap();
let EffectKind::Generator(generator) = &mut mask.effects[0].kind else { panic!("expected generator") };
*generator = Generator::new(GeneratorKind::Dirt);
generator.inputs.insert(BakedMap::AmbientOcclusion, Source::image("used"));
let image = |value| Arc::new(ImageRes { width: RES, height: RES, rgba: vec![[value; 4]; (RES * RES) as usize], srgb: false });
let mut images = ImageStore::default();
images.insert("used".into(), image(0.1));
let baked = Default::default();
let roots = &fixture.project.texture_sets[0].stack;
let key = |images: &ImageStore| StackKey::new(&fixture.project, &fixture.pixels, &baked, images, 0, roots).unwrap();
let original = key(&images);
images.insert("unrelated".into(), image(0.5));
assert_eq!(key(&images), original, "an unrelated image leaves the key unchanged");
let old_image = Arc::downgrade(&images["used"]);
images.insert("used".into(), image(0.9));
assert!(old_image.upgrade().is_none(), "the cache must not retain the old image pixels");
assert!(key(&images) != original, "replacing a generator input must invalidate the cache");
images.remove("used");
assert!(key(&images) != original, "removing a generator input must invalidate the cache");
}
+8 -1
View File
@@ -1,4 +1,4 @@
use super::{CageSettings, CommonBakeSettings, HighPolySettings, MeshMapSettings, SkewSettings};
use super::{CageSettings, CageType, CommonBakeSettings, HighPolySettings, MeshMapSettings, SkewSettings};
use serde::{Deserialize, Serialize};
/// Persisted clean-room bake settings, grouped by the controls they belong to
@@ -11,3 +11,10 @@ pub struct BakeSettings {
pub skew: SkewSettings,
pub maps: MeshMapSettings,
}
impl BakeSettings {
/// Whether a painted skew map steers the rays: it needs a separate high poly and a distance-based cage with averaged normals
pub fn skew_applies(&self) -> bool {
!self.high_poly.use_low_as_high && self.cage.kind == CageType::DistanceBased && self.cage.average_normals
}
}
+108 -4
View File
@@ -4,13 +4,19 @@ use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::{BlendMode, Channel, ChannelKind, lum};
use crate::{BakedMap, BlendMode, Channel, ChannelKind, lum};
mod curvature_detail;
mod curvature_mode;
mod generator_global;
mod output_resolution;
mod resolution_mode;
pub use curvature_detail::CurvatureDetail;
pub use curvature_mode::CurvatureMode;
pub use generator_global::GeneratorGlobal;
pub use output_resolution::OutputResolution;
pub use resolution_mode::ResolutionMode;
/// Id of a layer or effect (one id space per project).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
@@ -550,6 +556,17 @@ impl GeneratorKind {
GeneratorKind::Direction => "Light / direction",
}
}
/// The mesh maps this kind reads, each an input that an image, a uniform value or an anchor can replace.
pub fn inputs(self) -> &'static [BakedMap] {
match self {
GeneratorKind::EdgeWear | GeneratorKind::Curvature => &[BakedMap::Curvature],
GeneratorKind::Dirt | GeneratorKind::AmbientOcclusion => &[BakedMap::AmbientOcclusion],
GeneratorKind::ThinAreas => &[BakedMap::Thickness],
GeneratorKind::Position | GeneratorKind::Direction => &[],
}
}
pub fn parse(s: &str) -> Option<GeneratorKind> {
let norm = |x: &str| x.to_ascii_lowercase().replace([' ', '_', '-', ':'], "");
GeneratorKind::ALL.into_iter().find(|k| norm(&format!("{k:?}")) == norm(s) || norm(k.label()) == norm(s))
@@ -576,9 +593,25 @@ pub struct Generator {
/// Mode and detail sizes of `Curvature` / `EdgeWear` generators (None: the curvature map as baked, as before detail sizes existed).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub detail: Option<CurvatureDetail>,
/// Blur, balance and contrast of the finished value (neutral for generators saved before they existed).
#[serde(default, skip_serializing_if = "GeneratorGlobal::is_neutral")]
pub global: GeneratorGlobal,
/// The resolution the generator is computed at (the texture set's for generators saved before it existed).
#[serde(default, skip_serializing_if = "OutputResolution::is_default")]
pub resolution: OutputResolution,
/// What an input reads instead of its mesh map (an image, a uniform value or an anchor); inputs not listed read their mesh map.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub inputs: BTreeMap<BakedMap, Source>,
}
impl Generator {
/// The softest a ramp gets, so its threshold never divides by zero.
pub const MIN_SOFTNESS: f32 = 1e-3;
/// The most noise a generator mixes in, and the range of its noise scale.
pub const MAX_NOISE_AMOUNT: f32 = 2.0;
pub const MIN_NOISE_SCALE: f32 = 0.01;
pub const MAX_NOISE_SCALE: f32 = 1000.0;
pub fn new(kind: GeneratorKind) -> Generator {
let (level, softness, noise_amount) = match kind {
GeneratorKind::EdgeWear => (0.5, 0.25, 0.3),
@@ -592,7 +625,21 @@ impl Generator {
GeneratorKind::EdgeWear => Some(CurvatureDetail { mode: CurvatureMode::Edges, weights: CurvatureDetail::EDGE_WEIGHTS }),
_ => None,
};
Generator { kind, level, softness, noise_amount, noise_scale: 8.0, seed: 7, offset: 0.0, direction: [0.0, 1.0, 0.0], invert: false, detail }
Generator {
kind,
level,
softness,
noise_amount,
noise_scale: 8.0,
seed: 7,
offset: 0.0,
direction: [0.0, 1.0, 0.0],
invert: false,
detail,
global: GeneratorGlobal::default(),
resolution: OutputResolution::default(),
inputs: BTreeMap::new(),
}
}
/// The mask value for a (noise-broken) signal `s`: the smooth threshold at `level` over
@@ -1062,14 +1109,46 @@ impl EffectKind {
matches!(self, EffectKind::Paint | EffectKind::Path(_))
}
/// Anchor points read by this effect's sources.
/// Whether a source of this effect (a fill, a material or a generator input) reads an anchor point.
fn reads_anchor(&self) -> bool {
match self {
EffectKind::Fill { source, .. } => source.anchor().is_some(),
EffectKind::Material(f) => f.sources.values().any(|s| s.anchor().is_some()),
EffectKind::Material(fill) => fill.sources.values().any(|source| source.anchor().is_some()),
EffectKind::Generator(generator) => generator.inputs.values().any(|source| source.anchor().is_some()),
_ => false,
}
}
/// The anchor points this effect's sources read, with the channel each reads.
pub fn anchors_read(&self) -> Vec<(LayerId, Channel)> {
let sources: Vec<&Source> = match self {
EffectKind::Fill { source, .. } => vec![source],
EffectKind::Material(fill) => fill.sources.values().collect(),
EffectKind::Generator(generator) => generator.inputs.values().collect(),
_ => Vec::new(),
};
sources
.into_iter()
.filter_map(|source| match source {
Source::Anchor { anchor, channel } => Some((*anchor, *channel)),
_ => None,
})
.collect()
}
/// The image resources this effect's sources read (a fill, material or generator input).
pub fn images_read(&self) -> impl Iterator<Item = &str> {
let sources: Vec<&Source> = match self {
EffectKind::Fill { source, .. } => vec![source],
EffectKind::Material(fill) => fill.sources.values().collect(),
EffectKind::Generator(generator) => generator.inputs.values().collect(),
_ => Vec::new(),
};
sources.into_iter().filter_map(|source| match source {
Source::Image { name, .. } => Some(name.as_str()),
_ => None,
})
}
}
impl Layer {
@@ -1104,6 +1183,15 @@ impl Effect {
mod tests {
use super::*;
#[test]
fn material_effect_images_are_reported_as_used() {
let mut fill = Fill::default();
fill.sources.insert(Channel::BaseColor, Source::image("material.png"));
let effect = EffectKind::Material(fill);
assert_eq!(effect.images_read().collect::<Vec<_>>(), vec!["material.png"]);
}
#[test]
fn generator_offset_shifts_the_mask_value() {
let mut g = Generator::new(GeneratorKind::Position);
@@ -1139,6 +1227,22 @@ mod tests {
assert_eq!(Generator::new(GeneratorKind::Dirt).detail, None, "only curvature and edge wear generators have detail sizes");
}
#[test]
fn generators_saved_before_the_new_settings_load_and_save_unchanged() {
let saved = serde_json::to_value(Generator::new(GeneratorKind::Dirt)).unwrap();
for field in ["global", "resolution", "inputs"] {
assert!(saved.get(field).is_none(), "a default `{field}` is not written, so saved files stay as they were");
}
let loaded: Generator = serde_json::from_value(saved).unwrap();
assert!(loaded.global.is_neutral());
let mut blurred = Generator::new(GeneratorKind::Dirt);
blurred.global.blur = 0.5;
let round_trip: Generator = serde_json::from_value(serde_json::to_value(&blurred).unwrap()).unwrap();
assert_eq!(round_trip, blurred);
}
#[test]
fn levels_map_range_and_gamma() {
let l = Levels { in_low: 0.2, in_high: 0.6, gamma: 1.0, out_low: 0.0, out_high: 1.0 };
+96
View File
@@ -0,0 +1,96 @@
use serde::{Deserialize, Serialize};
/// Adjustments every generator applies to its finished value: a blur, then balance and contrast.
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct GeneratorGlobal {
/// Blur of the generated value (0..1 of [`GeneratorGlobal::MAX_BLUR`]).
pub blur: f32,
/// Shifts the value darker (below 0.5) or brighter (above 0.5); 0.5 leaves it unchanged.
pub balance: f32,
/// Narrows the ramp around its middle (0 unchanged, 1 a hard step).
pub contrast: f32,
}
impl GeneratorGlobal {
/// The widest blur, as a radius in texels at [`GeneratorGlobal::BLUR_RESOLUTION`].
pub const MAX_BLUR: f32 = 64.0;
/// Narrower than this, the balanced range is a hard step.
pub const MIN_RANGE: f32 = 1e-6;
/// The texels per side the blur radius is measured at (2K).
pub const BLUR_RESOLUTION: f32 = 2048.0;
pub fn is_neutral(&self) -> bool {
*self == GeneratorGlobal::default()
}
/// The blur as a radius in texels at [`GeneratorGlobal::BLUR_RESOLUTION`].
pub fn blur_radius(&self) -> f32 {
self.blur.clamp(0.0, 1.0) * GeneratorGlobal::MAX_BLUR
}
/// The blur as a radius in texels at `resolution` texels per side.
pub fn blur_radius_at(&self, resolution: u32) -> f32 {
self.blur_radius() * resolution as f32 / GeneratorGlobal::BLUR_RESOLUTION
}
/// `value` after balance and contrast: balance picks the input range stretched to 0..1, contrast narrows it around its middle.
pub fn apply(&self, value: f32) -> f32 {
if self.balance == 0.5 && self.contrast == 0.0 {
return value;
}
let balance = self.balance.clamp(0.0, 1.0);
let (low, high) = ((1.0 - 2.0 * balance).max(0.0), (2.0 - 2.0 * balance).min(1.0));
let middle = (low + high) * 0.5;
let keep = 1.0 - self.contrast.clamp(0.0, 1.0);
let (low, high) = (middle - (middle - low) * keep, middle + (high - middle) * keep);
if high - low < GeneratorGlobal::MIN_RANGE {
return if value >= middle { 1.0 } else { 0.0 };
}
((value - low) / (high - low)).clamp(0.0, 1.0)
}
}
impl Default for GeneratorGlobal {
fn default() -> Self {
GeneratorGlobal { blur: 0.0, balance: 0.5, contrast: 0.0 }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn neutral_settings_leave_the_value_unchanged() {
let global = GeneratorGlobal::default();
for value in [0.0, 0.1, 0.5, 0.93, 1.0] {
assert_eq!(global.apply(value), value);
}
}
#[test]
fn balance_darkens_below_the_middle_and_brightens_above() {
let darker = GeneratorGlobal { balance: 0.25, ..GeneratorGlobal::default() };
assert_eq!(darker.apply(0.4), 0.0, "the lower half goes black");
assert!((darker.apply(0.75) - 0.5).abs() < 1e-6, "the upper half stretches over the whole range");
let brighter = GeneratorGlobal { balance: 0.75, ..GeneratorGlobal::default() };
assert!((brighter.apply(0.25) - 0.5).abs() < 1e-6);
assert_eq!(brighter.apply(0.6), 1.0, "the upper half goes white");
assert_eq!(GeneratorGlobal { balance: 0.0, ..GeneratorGlobal::default() }.apply(0.99), 0.0);
assert_eq!(GeneratorGlobal { balance: 1.0, ..GeneratorGlobal::default() }.apply(0.01), 1.0);
}
#[test]
fn full_contrast_is_a_step_at_the_middle_of_the_balanced_range() {
let step = GeneratorGlobal { contrast: 1.0, ..GeneratorGlobal::default() };
assert_eq!(step.apply(0.49), 0.0);
assert_eq!(step.apply(0.51), 1.0);
let half = GeneratorGlobal { contrast: 0.5, ..GeneratorGlobal::default() };
assert_eq!(half.apply(0.25), 0.0);
assert!((half.apply(0.5) - 0.5).abs() < 1e-6);
assert_eq!(half.apply(0.75), 1.0);
}
}
+120
View File
@@ -0,0 +1,120 @@
use serde::{Deserialize, Serialize};
use super::resolution_mode::ResolutionMode;
/// The resolution a generator computes its output at, before it is sampled onto the texture set.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct OutputResolution {
pub mode: ResolutionMode,
/// Texels per side for [`ResolutionMode::Custom`].
pub custom: u32,
/// Power of two applied to the base size (−3 = 1/8 … 3 = ×8; 0 = the same as the context).
pub factor: i8,
}
impl OutputResolution {
/// The factors the reference app offers: 1/8 to ×8.
pub const FACTORS: std::ops::RangeInclusive<i8> = -3..=3;
/// The sizes offered for [`ResolutionMode::Custom`].
pub const CUSTOM_SIZES: [u32; 6] = [128, 256, 512, 1024, 2048, 4096];
/// The largest output, the largest texture set resolution.
pub const MAX_SIZE: u32 = 8192;
/// The coarsest output: one value per this many texels of the context per side (a compositing tile).
pub const MAX_CELL: u32 = 64;
pub fn is_default(&self) -> bool {
*self == OutputResolution::default()
}
/// Whether some UV tile can get a coarser output than its own resolution (a custom size, the set's size or a factor below 1).
pub fn can_reduce(&self) -> bool {
matches!(self.mode, ResolutionMode::Custom | ResolutionMode::TextureSet) || self.factor < 0
}
/// The largest cell a UV tile of up to `uv_tile` texels per side can get, whatever its texture set's resolution.
pub fn largest_cell(&self, uv_tile: u32) -> u32 {
if self.mode == ResolutionMode::TextureSet { OutputResolution::MAX_CELL } else { self.cell(uv_tile, uv_tile) }
}
/// Texels per side of the output for a UV tile of `uv_tile` texels per side in a texture set of `texture_set`.
pub fn output_size(&self, uv_tile: u32, texture_set: u32) -> u32 {
let base = match self.mode {
ResolutionMode::Auto | ResolutionMode::UvTile => uv_tile,
ResolutionMode::TextureSet => texture_set,
ResolutionMode::Custom => self.custom,
};
let factor = self.factor.clamp(*OutputResolution::FACTORS.start(), *OutputResolution::FACTORS.end());
let scaled = if factor >= 0 { base.saturating_mul(1 << factor) } else { base >> -factor };
// Hostile tile sizes can make the minimum exceed the supported maximum
let minimum = (uv_tile / OutputResolution::MAX_CELL).clamp(1, OutputResolution::MAX_SIZE);
scaled.clamp(minimum, OutputResolution::MAX_SIZE)
}
/// How many texels of the UV tile one output texel covers per side (1 when the output is as fine or finer).
pub fn cell(&self, uv_tile: u32, texture_set: u32) -> u32 {
(uv_tile / self.output_size(uv_tile, texture_set)).max(1)
}
}
impl Default for OutputResolution {
fn default() -> Self {
OutputResolution { mode: ResolutionMode::Auto, custom: 2048, factor: 0 }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_default_follows_the_uv_tile() {
let resolution = OutputResolution::default();
assert_eq!(resolution.output_size(1024, 2048), 1024);
assert_eq!(resolution.cell(1024, 2048), 1);
assert!(!resolution.can_reduce());
}
#[test]
fn the_texture_set_mode_reads_the_set_not_the_uv_tile() {
let resolution = OutputResolution { mode: ResolutionMode::TextureSet, factor: -1, ..OutputResolution::default() };
assert_eq!(resolution.output_size(512, 2048), 1024, "half the set's 2048");
assert_eq!(resolution.cell(512, 2048), 1, "finer than the 512 tile: one texel per texel");
}
#[test]
fn factors_and_custom_sizes_scale_the_output() {
let eighth = OutputResolution { factor: -3, ..OutputResolution::default() };
assert_eq!((eighth.output_size(2048, 2048), eighth.cell(2048, 2048)), (256, 8));
let custom = OutputResolution { mode: ResolutionMode::Custom, custom: 512, factor: 1 };
assert_eq!((custom.output_size(2048, 2048), custom.cell(2048, 2048)), (1024, 2));
let finer = OutputResolution { factor: 3, ..OutputResolution::default() };
assert_eq!((finer.output_size(2048, 2048), finer.cell(2048, 2048)), (8192, 1), "a finer output than the tile keeps one texel per texel");
}
#[test]
fn the_largest_cell_covers_any_texture_set() {
let set = OutputResolution { mode: ResolutionMode::TextureSet, ..OutputResolution::default() };
assert!(set.can_reduce(), "a UV tile larger than its set gets the set's coarser size");
assert_eq!(set.largest_cell(2048), OutputResolution::MAX_CELL);
assert_eq!(OutputResolution { factor: -2, ..OutputResolution::default() }.largest_cell(2048), 4);
}
#[test]
fn hostile_sizes_stay_within_the_largest_output_and_the_coarsest_cell() {
let huge = OutputResolution { mode: ResolutionMode::Custom, custom: u32::MAX, factor: i8::MAX };
assert_eq!(huge.output_size(2048, 2048), OutputResolution::MAX_SIZE);
let tiny = OutputResolution { mode: ResolutionMode::Custom, custom: 1, factor: -3 };
assert_eq!(tiny.cell(4096, 4096), OutputResolution::MAX_CELL);
}
#[test]
fn hostile_uv_tile_size_does_not_panic() {
let resolution = OutputResolution::default();
assert_eq!(resolution.output_size(u32::MAX, 2048), OutputResolution::MAX_SIZE);
}
}
+32
View File
@@ -0,0 +1,32 @@
use serde::{Deserialize, Serialize};
/// What a generator's output resolution is based on.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ResolutionMode {
/// The context: the resolution of the UV tile it is computed for.
Auto,
/// The texture set's resolution.
TextureSet,
/// The UV tile's resolution.
UvTile,
/// A size of its own.
Custom,
}
impl ResolutionMode {
pub const ALL: [ResolutionMode; 4] = [ResolutionMode::Auto, ResolutionMode::TextureSet, ResolutionMode::UvTile, ResolutionMode::Custom];
pub fn label(self) -> &'static str {
match self {
ResolutionMode::Auto => "Auto",
ResolutionMode::TextureSet => "Texture set",
ResolutionMode::UvTile => "UV tile",
ResolutionMode::Custom => "Custom",
}
}
pub fn parse(name: &str) -> Option<ResolutionMode> {
let norm = |text: &str| text.to_ascii_lowercase().replace([' ', '_', '-'], "");
ResolutionMode::ALL.into_iter().find(|mode| norm(&format!("{mode:?}")) == norm(name) || norm(mode.label()) == norm(name))
}
}
+3 -3
View File
@@ -24,9 +24,9 @@ pub use bake::{
pub use blend::{BlendMode, blend1, blend3, lum, mix3};
pub use channel::{Channel, ChannelFormat, ChannelKind};
pub use layer::{
ChannelBlend, CompareOp, CurvatureDetail, CurvatureMode, Decal, Effect, EffectKind, Fill, Filter, Generator, GeneratorKind, HeightMask, Layer, LayerId,
LayerKind, Levels, LinkParam, LinkPreset, Mask, MaskBackground, MaterialLink, PathShape, Procedural, ProceduralKind, Projection, Source, gradient,
hsl_adjust, per_unit,
ChannelBlend, CompareOp, CurvatureDetail, CurvatureMode, Decal, Effect, EffectKind, Fill, Filter, Generator, GeneratorGlobal, GeneratorKind, HeightMask,
Layer, LayerId, LayerKind, Levels, LinkParam, LinkPreset, Mask, MaskBackground, MaterialLink, OutputResolution, PathShape, Procedural, ProceduralKind,
Projection, ResolutionMode, Source, gradient, hsl_adjust, per_unit,
};
pub use project::{
AoMixing, BlockScales, Camera, DepthOfField, DisplaySettings, FORMAT_VERSION, MAX_RESOLUTION, MIN_RESOLUTION, NormalFormat, NormalMixing, Project,
@@ -23,6 +23,16 @@ impl BakeCageInputs {
CageShell::new(&self.request())
}
/// Whether this set uses the session's shared low mesh
pub fn uses_mesh(&self, mesh: &Arc<Mesh>) -> bool {
Arc::ptr_eq(&self.mesh, mesh)
}
/// Build a cage from edges shared across sets using the same low mesh
pub fn cage_shell_with_edges(&self, wire_edges: &[Vec<(u32, u32)>], missing_seam_edges: &[Vec<(u32, u32)>]) -> Result<CageShell, BakeError> {
CageShell::new_with_edges(&self.request(), wire_edges, missing_seam_edges)
}
/// Where each high-poly vertex sits relative to the bake's transfer rays
pub fn high_poly_reach(&self) -> Result<HighPolyReach, BakeError> {
HighPolyReach::new(&self.request())
+109
View File
@@ -0,0 +1,109 @@
//! What can spoil a texture set's bake, checked before it runs: overlapping UVs and parts "By Mesh Name" leaves unpaired
use std::collections::{BTreeMap, BTreeSet};
use texturecraft_bake::NamePair;
use texturecraft_core::MeshMatchMode;
use texturecraft_geom::{Mesh, SubMesh, UvGBuffer, glam::Vec2};
use crate::{Result, Session};
/// Side of the UV raster the overlap check counts texels on, per UV tile (as mesh import does)
const OVERLAP_CHECK_RESOLUTION: u32 = 512;
/// The only material of the UV-only mesh the overlap check builds per UV tile
const BLOCK_MATERIAL: usize = 0;
impl Session {
/// About the share (0..1) of texture set `texture_set`'s UV layout the bake sees covered more than once, over the UV tiles it bakes: a texel under three triangles counts twice
pub fn bake_uv_overlap(&self, texture_set: usize) -> Result<f32> {
let set = self.doc.project.texture_set(texture_set)?;
let mesh = self.bake_mesh(texture_set)?;
let (overlaps, covered) = match set.tile_grid() {
Some(grid) => {
let baked_blocks: BTreeSet<(u32, u32)> =
set.uv_tiles.iter().filter(|tile| !set.bake_excluded_tiles.contains(tile)).filter_map(|tile| grid.block(*tile)).collect();
let blocks = triangles_by_block(&mesh, set.material, (grid.cols, grid.rows), &baked_blocks);
blocks
.values()
.map(|block| overlap_counts(block, BLOCK_MATERIAL))
.fold((0, 0), |(overlaps, covered), (block_overlaps, block_covered)| (overlaps + block_overlaps, covered + block_covered))
}
None => overlap_counts(&mesh, set.material),
};
Ok(if covered == 0 { 0.0 } else { (overlaps as f32 / covered as f32).min(1.0) })
}
/// How texture set `texture_set`'s low-poly parts pair with the high-poly parts ([`NamePair::list`]); `None` unless it bakes from a high poly "By Mesh Name"
pub fn bake_name_pairs(&self, texture_set: usize) -> Result<Option<Vec<NamePair>>> {
let set = self.doc.project.texture_set(texture_set)?;
let settings = &set.bake.high_poly;
let high = self.bake_meshes.merged_high().filter(|_| settings.match_mode == MeshMatchMode::ByMeshName && !settings.use_low_as_high);
Ok(high.map(|high| NamePair::list(&self.mesh, set.material, &high.mesh, settings)))
}
/// The high-poly parts none of `texture_sets` bakes from, by name: a set matching "Always" bakes from every part, one matching "By Mesh Name" only from the parts its names pair with
pub fn bake_unpaired_high_parts(&self, texture_sets: &[usize]) -> Result<Vec<String>> {
let (mut paired, mut unpaired) = (BTreeSet::new(), BTreeSet::new());
for &texture_set in texture_sets {
let settings = &self.doc.project.texture_set(texture_set)?.bake.high_poly;
if !settings.use_low_as_high && settings.match_mode == MeshMatchMode::Always {
return Ok(Vec::new());
}
for pair in self.bake_name_pairs(texture_set)?.unwrap_or_default() {
match pair {
NamePair { low: Some(_), high: Some(high) } => paired.insert(high),
NamePair { low: None, high: Some(high) } => unpaired.insert(high),
_ => false,
};
}
}
Ok(unpaired.difference(&paired).cloned().collect())
}
}
/// Texels covered more than once and texels covered, rasterizing material `material`'s UVs over 0..1
fn overlap_counts(mesh: &Mesh, material: usize) -> (usize, usize) {
let gbuffer = UvGBuffer::new(mesh, material, OVERLAP_CHECK_RESOLUTION, OVERLAP_CHECK_RESOLUTION);
(gbuffer.overlaps as usize, gbuffer.covered_count())
}
/// Material `material`'s triangles sorted into the `baked_blocks` of a `grid` (columns, rows) atlas their UVs reach, one UV-only mesh per block with the block's UVs over 0..1: a single pass, however many blocks
fn triangles_by_block(mesh: &Mesh, material: usize, grid: (u32, u32), baked_blocks: &BTreeSet<(u32, u32)>) -> BTreeMap<(u32, u32), Mesh> {
let scale = Vec2::new(grid.0 as f32, grid.1 as f32);
let mut blocks: BTreeMap<(u32, u32), Mesh> = BTreeMap::new();
for triangle in mesh.triangles_of(material) {
let Some([first, second, third]) = mesh.tri(triangle) else { continue };
let (Some(first_uv), Some(second_uv), Some(third_uv)) = (mesh.uvs.get(first), mesh.uvs.get(second), mesh.uvs.get(third)) else { continue };
let uvs = [*first_uv * scale, *second_uv * scale, *third_uv * scale];
// Every block the triangle's UV bounds touch, so a triangle across a border counts in both
let (uv_min, uv_max) = uvs.iter().fold((Vec2::INFINITY, Vec2::NEG_INFINITY), |(uv_min, uv_max), uv| (uv_min.min(*uv), uv_max.max(*uv)));
// Capped at the grid: a stray huge UV must not walk billions of blocks
let columns = uv_min.x.floor().max(0.0) as u32..=((uv_max.x.ceil() as u32).saturating_sub(1)).min(grid.0 - 1);
let rows = uv_min.y.floor().max(0.0) as u32..=((uv_max.y.ceil() as u32).saturating_sub(1)).min(grid.1 - 1);
for column in columns {
for row in rows.clone() {
if !baked_blocks.contains(&(column, row)) {
continue;
}
let block = blocks.entry((column, row)).or_insert_with(|| Mesh { materials: vec![String::new()], ..Mesh::default() });
let offset = Vec2::new(column as f32, row as f32);
block.indices.extend(block.uvs.len() as u32..block.uvs.len() as u32 + 3);
block.uvs.extend(uvs.iter().map(|uv| *uv - offset));
}
}
}
for block in blocks.values_mut() {
block.submeshes = vec![SubMesh { name: String::new(), material: BLOCK_MATERIAL, start: 0, count: block.indices.len() as u32 }];
}
blocks
}
+14 -3
View File
@@ -1,9 +1,10 @@
use rayon::prelude::*;
use texturecraft_bake::HighPolyReach;
/// Per high-poly vertex: 1.0 if its owning set's bake misses it, otherwise 0.0
/// Distances are each set's (max frontal, max rear); filtered or unavailable sets leave their vertices unmarked
/// `vertex_count` is the validated high-poly mesh's vertex count
/// The value [`high_poly_missed`] gives a vertex that `only_set` doesn't own, which the 3D view leaves out
pub const HIDDEN_HIGH_POLY_VERTEX: f32 = -1.0;
/// Per vertex of the validated high poly: 1.0 if its owning set's bake misses it at that set's (max frontal, max rear), 0.0 if reached or unknown, [`HIDDEN_HIGH_POLY_VERTEX`] if `only_set` doesn't own it
pub fn high_poly_missed(reach: &[Option<HighPolyReach>], distances: &[(f32, f32)], only_set: Option<usize>, vertex_count: usize) -> Vec<f32> {
// Ownership is already assigned by HighPolyReach; other sets must not color the vertex
let sets: Vec<(&HighPolyReach, f32, f32)> = reach
@@ -13,11 +14,21 @@ pub fn high_poly_missed(reach: &[Option<HighPolyReach>], distances: &[(f32, f32)
.filter(|(texture_set, _)| only_set.is_none_or(|only| only == *texture_set))
.filter_map(|(_, (reach, &(max_frontal, max_rear)))| Some((reach.as_ref()?, max_frontal, max_rear)))
.collect();
(0..vertex_count)
.into_par_iter()
.map(|vertex| {
if only_set.is_some_and(|only| !owns(reach, only, vertex)) {
return HIDDEN_HIGH_POLY_VERTEX;
}
let missed = sets.iter().find_map(|(reach, max_frontal, max_rear)| reach.missed(vertex, *max_frontal, *max_rear));
f32::from(u8::from(missed.unwrap_or(false)))
})
.collect()
}
/// Whether texture set `texture_set`'s reach claims high-poly vertex `vertex`
fn owns(reach: &[Option<HighPolyReach>], texture_set: usize, vertex: usize) -> bool {
reach.get(texture_set).and_then(Option::as_ref).and_then(|set| set.vertices.get(vertex)).is_some_and(Option::is_some)
}
+3 -2
View File
@@ -182,12 +182,13 @@ impl Session {
/// Texture set `texture_set`'s painted skew or cage map, used only when it bakes from a high poly
fn bake_paint(&self, texture_set: usize, has_high: bool, target: BakePaintTarget) -> Result<Option<Arc<BakePaintMap>>> {
let set = self.doc.project.texture_set(texture_set)?;
let paint = self.doc.bake_paint.get(&(texture_set, target)).filter(|_| has_high);
let steers = has_high && (target != BakePaintTarget::Skew || set.bake.skew_applies());
let paint = self.doc.bake_paint.get(&(texture_set, target)).filter(|_| steers);
paint.map(|map| super::layout::paint_for_bake(map, set)).transpose()
}
/// The low poly texture set `texture_set` bakes: UV tiles of mixed resolutions move into its atlas
fn bake_mesh(&self, texture_set: usize) -> Result<Arc<Mesh>> {
pub(super) fn bake_mesh(&self, texture_set: usize) -> Result<Arc<Mesh>> {
let set = self.doc.project.texture_set(texture_set)?;
Ok(match set.tile_grid().filter(|_| !set.tile_resolutions.is_empty()) {
Some(grid) => {
+2 -1
View File
@@ -1,6 +1,7 @@
//! Baking tasks, commands, settings helpers, and the mesh-map baking API
mod bake_cage_inputs;
mod bake_checks;
mod bake_mesh;
mod bake_meshes;
mod bake_task;
@@ -19,7 +20,7 @@ pub use bake_cage_inputs::BakeCageInputs;
pub use bake_mesh::BakeMesh;
pub use bake_meshes::BakeMeshes;
pub use bake_task::BakeTask;
pub use high_poly_missed::high_poly_missed;
pub use high_poly_missed::{HIDDEN_HIGH_POLY_VERTEX, high_poly_missed};
pub use merged_high::MergedHigh;
pub use mesh_map_command::{install_gpu_tracer, meshes_json, settings_params};
pub use model_patch::settings_patch;
+62 -4
View File
@@ -195,16 +195,26 @@ pub(crate) fn blend_param(cmd: &str, p: &Value, key: &str) -> Result<Option<Blen
}
/// A color: `[r, g, b]` (linear 0..1) or `"#rrggbb"` (sRGB hex, converted to linear).
pub(crate) fn color_param(p: &Value, key: &str) -> Option<[f32; 3]> {
color_of(p.get(key)?)
pub(crate) fn color_param(cmd: &str, p: &Value, key: &str) -> Result<Option<[f32; 3]>> {
match p.get(key) {
None | Some(Value::Null) => Ok(None),
Some(v) => color_of(v).map(Some).ok_or_else(|| bad(cmd, format!("`{key}` must be a color ([r, g, b] linear or \"#rrggbb\")"))),
}
}
/// A color value (see [`color_param`]).
pub(crate) fn color_of(v: &Value) -> Option<[f32; 3]> {
match v {
Value::Array(a) if a.len() >= 3 => {
let v: Vec<f32> = a.iter().take(3).map(|x| x.as_f64().unwrap_or(0.0) as f32).collect();
if v.iter().all(|x| x.is_finite()) { Some([v[0], v[1], v[2]]) } else { None }
let mut out = [0.0f32; 3];
for (i, x) in a.iter().take(3).enumerate() {
let f = x.as_f64()? as f32;
if !f.is_finite() {
return None;
}
out[i] = f;
}
Some(out)
}
Value::Number(n) => n.as_f64().map(|v| [v as f32; 3]).filter(|v| v[0].is_finite()),
Value::String(s) => {
@@ -218,3 +228,51 @@ pub(crate) fn color_of(v: &Value) -> Option<[f32; 3]> {
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn color_param_accepts_valid_forms() {
let p = json!({
"hex": "#336699",
"hex_plain": "336699",
"rgb": [0.2, 0.4, 0.6],
"grey": 0.5,
});
assert!(color_param("cmd", &p, "hex").unwrap().is_some());
assert!(color_param("cmd", &p, "hex_plain").unwrap().is_some());
assert_eq!(color_param("cmd", &p, "rgb").unwrap(), Some([0.2, 0.4, 0.6]));
assert_eq!(color_param("cmd", &p, "grey").unwrap(), Some([0.5, 0.5, 0.5]));
assert_eq!(color_param("cmd", &p, "missing").unwrap(), None);
assert_eq!(color_param("cmd", &json!({"c": null}), "c").unwrap(), None);
}
#[test]
fn color_param_rejects_invalid_forms() {
let bad_cases = [
("#fff", "`color` must be a color ([r, g, b] linear or \"#rrggbb\")"),
("red", "`color` must be a color ([r, g, b] linear or \"#rrggbb\")"),
("#12345", "`color` must be a color ([r, g, b] linear or \"#rrggbb\")"),
("#gggggg", "`color` must be a color ([r, g, b] linear or \"#rrggbb\")"),
];
for (input, expected_err) in bad_cases {
let p = json!({ "color": input });
let err = color_param("test.cmd", &p, "color").unwrap_err();
assert_eq!(err.to_string(), format!("`test.cmd`: {expected_err}"));
}
let bad_array = json!({ "color": ["a", "b", "c"] });
assert!(color_param("test.cmd", &bad_array, "color").is_err());
let short_array = json!({ "color": [0.5, 0.5] });
assert!(color_param("test.cmd", &short_array, "color").is_err());
let overflow_array = json!({ "color": [1e40, 0.0, 0.0] });
assert!(color_param("test.cmd", &overflow_array, "color").is_err());
let overflow_scalar = json!({ "color": 1e40 });
assert!(color_param("test.cmd", &overflow_scalar, "color").is_err());
}
}
+56 -45
View File
@@ -2,7 +2,7 @@
//! clipboard: it holds a layer or an effect.
use serde_json::{Value, json};
use texturecraft_core::{EffectKind, LayerId, Mask, MaskBackground, Source};
use texturecraft_core::{EffectKind, LayerId, Mask, MaskBackground};
use crate::commands::{CommandSpec, bad, has_layer, layer_param, strp};
use crate::layer_ops::{empty_texel, fit};
@@ -55,26 +55,29 @@ fn cut(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({ "cut": eid }))
}
fn paste(s: &mut Session, p: &Value) -> Result<Value> {
fn paste(session: &mut Session, params: &Value) -> Result<Value> {
const CMD: &str = "effect.paste";
let (src, eid) = s.clipboard.clone().ok_or_else(|| bad(CMD, "the clipboard holds no effect"))?;
let effect = src.project.effect(eid).map(|(_, e)| e.clone()).ok_or_else(|| bad(CMD, "the clipboard holds no effect"))?;
let layer = layer_param(s, CMD, p)?;
// The selected effect's stack, or the content or mask the selection targets.
let selected = match s.target {
StackTarget::Effect(x) if s.selected_layer() == Some(layer) => Some(x),
let (copied, copied_id) = session.clipboard.clone().ok_or_else(|| bad(CMD, "the clipboard holds no effect"))?;
let effect = copied.project.effect(copied_id).map(|(_, effect)| effect.clone()).ok_or_else(|| bad(CMD, "the clipboard holds no effect"))?;
let layer = layer_param(session, CMD, params)?;
// The selected effect's stack, or the content or mask the selection targets
let selected = match session.target {
StackTarget::Effect(selected) if session.selected_layer() == Some(layer) => Some(selected),
_ => None,
};
let in_content = |s: &Session, x: LayerId| {
s.doc.project.effect(x).and_then(|(o, _)| s.doc.project.layers.get(&o)).is_some_and(|l| l.content_effects.iter().any(|e| e.id == x))
let in_content = |session: &Session, effect_id: LayerId| {
let owner = session.doc.project.effect(effect_id).and_then(|(owner, _)| session.doc.project.layers.get(&owner));
owner.is_some_and(|owner| owner.content_effects.iter().any(|effect| effect.id == effect_id))
};
let asked = strp(p, "target").is_some();
let asked = strp(params, "target").is_some();
let content = match selected {
_ if asked => content_target(p, CMD)?,
Some(x) => in_content(s, x),
None => s.target == StackTarget::Content && s.selected_layer() == Some(layer),
_ if asked => content_target(params, CMD)?,
Some(selected) => in_content(session, selected),
None => session.target == StackTarget::Content && session.selected_layer() == Some(layer),
};
// Effects that fit one stack only go there, unless another was asked for.
// Effects that fit one stack only go there, unless another was asked for
let content = match effect.kind.content_only() {
Some(only) if only != content && asked => {
return Err(bad(CMD, format!("{} effects go on the {}", effect.kind.name(), if only { "layer content" } else { "mask" })));
@@ -82,46 +85,54 @@ fn paste(s: &mut Session, p: &Value) -> Result<Value> {
Some(only) => only,
None => content,
};
// An instance's content and (unless it has its own) mask belong to its source.
let owner = if content { s.doc.project.content_layer(layer).map_or(layer, |l| l.id) } else { s.doc.project.mask_owner(layer) };
let owner_layer = s.doc.project.layer(owner)?;
// An instance's content and (unless it has its own) mask belong to its source
let owner = if content { session.doc.project.content_layer(layer).map_or(layer, |source| source.id) } else { session.doc.project.mask_owner(layer) };
let owner_layer = session.doc.project.layer(owner)?;
if content && owner_layer.is_group() {
return Err(EngineError::Other(format!("\"{}\" is a folder: content effects go on paint and fill layers", owner_layer.name)));
}
// A fill reading an anchor point needs one below its new layer.
if let EffectKind::Fill { source: Source::Anchor { anchor, channel }, .. } = &effect.kind {
anchor_source(s, CMD, owner, *anchor, *channel)?;
// Each anchor point the effect reads must be below its new layer
for (anchor, channel) in effect.kind.anchors_read() {
anchor_source(session, CMD, owner, anchor, channel)?;
}
let (ts, _, _) = s.doc.project.locate(owner).ok_or_else(|| bad(CMD, "the layer is not in a stack"))?;
let (size, channels) = s.doc.project.texture_set(ts).map(|t| (t.size(), t.channel_list()))?;
let new = s.edit("Paste effect", |s| {
let new = s.doc.project.alloc_id();
let l = s.doc.project.layer_mut(owner)?;
let list = if content {
&mut l.content_effects
let (set, _, _) = session.doc.project.locate(owner).ok_or_else(|| bad(CMD, "the layer is not in a stack"))?;
let (size, channels) = session.doc.project.texture_set(set).map(|texture_set| (texture_set.size(), texture_set.channel_list()))?;
let pasted = session.edit("Paste effect", |session| {
let pasted = session.doc.project.alloc_id();
let owner_layer = session.doc.project.layer_mut(owner)?;
let stack = if content {
&mut owner_layer.content_effects
} else {
let bg = if matches!(effect.kind, EffectKind::Generator(_) | EffectKind::Fill { .. }) { MaskBackground::Black } else { MaskBackground::White };
&mut l.mask.get_or_insert_with(|| Mask::new(bg)).effects
let background =
if matches!(effect.kind, EffectKind::Generator(_) | EffectKind::Fill { .. }) { MaskBackground::Black } else { MaskBackground::White };
&mut owner_layer.mask.get_or_insert_with(|| Mask::new(background)).effects
};
let at = selected.and_then(|x| list.iter().position(|e| e.id == x)).map_or(list.len(), |i| i + 1);
list.insert(at, texturecraft_core::Effect { id: new, ..effect.clone() });
// Painted pixels come along, resampled to the set's size (a mask's paint stays a mask's).
if let Some(img) = src.pixels.masks.get(&eid).filter(|_| !content) {
s.doc.pixels.masks.insert(new, fit(img, size, &[0, 0]));
let index = selected.and_then(|selected| stack.iter().position(|effect| effect.id == selected)).map_or(stack.len(), |position| position + 1);
stack.insert(index, texturecraft_core::Effect { id: pasted, ..effect.clone() });
// Painted pixels come along, resampled to the set's size (a mask's paint stays a mask's)
if let Some(image) = copied.pixels.masks.get(&copied_id).filter(|_| !content) {
session.doc.pixels.masks.insert(pasted, fit(image, size, &[0, 0]));
}
for c in channels.iter().copied().filter(|_| content) {
if let Some(img) = src.pixels.layer(eid, c) {
s.doc.pixels.layers.insert((new, c), fit(img, size, &empty_texel(c)));
for channel in channels.iter().copied().filter(|_| content) {
if let Some(image) = copied.pixels.layer(copied_id, channel) {
session.doc.pixels.layers.insert((pasted, channel), fit(image, size, &empty_texel(channel)));
}
}
// The target belongs to the active set's selection: a paste into another set leaves it.
if let Some((lts, _, _)) = s.doc.project.locate(layer) {
s.selected.insert(lts, layer);
if lts == s.ts() {
s.target = StackTarget::Effect(new);
// The target belongs to the active set's selection: a paste into another set leaves it
if let Some((layer_set, _, _)) = session.doc.project.locate(layer) {
session.selected.insert(layer_set, layer);
if layer_set == session.ts() {
session.target = StackTarget::Effect(pasted);
}
}
Ok(new)
Ok(pasted)
})?;
Ok(json!({ "effect": new.0 }))
Ok(json!({ "effect": pasted.0 }))
}
+110
View File
@@ -37,6 +37,15 @@ pub fn specs() -> Vec<CommandSpec> {
always,
reimport
),
cmd!(
"file.changeMesh",
"Change mesh",
[],
None,
"{path: the new mesh file, preserveStrokes?: bool (painted pixels follow the surface where the UVs differ; default true)} → {preserved?: texels re-projected}. Keeps layers, texture sets and settings; texture sets map to the new mesh by material index",
always,
change_mesh
),
cmd!(
"file.exportTextures",
"Export textures",
@@ -438,6 +447,16 @@ fn reimport(s: &mut Session, p: &Value) -> Result<Value> {
Ok(reply)
}
#[cfg(not(target_arch = "wasm32"))]
fn change_mesh(s: &mut Session, p: &Value) -> Result<Value> {
let path = native::path_param(p, "path", "file.changeMesh")?;
let file = native::read_mesh("file.changeMesh", path)?;
let reply = s.reimport_mesh(Some(file), crate::commands::boolp(p, "preserveStrokes").unwrap_or(true))?;
// Only after the swap succeeded: Reimport mesh and Save keep following the new file.
s.doc.project.mesh_path = native::absolute(path);
Ok(reply)
}
#[cfg(not(target_arch = "wasm32"))]
impl Session {
/// The file the mesh was imported from, when it is still there: what Reimport mesh reads again.
@@ -471,6 +490,19 @@ impl Session {
let mut next = Session::new(doc, mesh, source)?;
next.log.extend(warnings);
next.path = self.path.clone();
next.sync_images();
// The high polys and the skew map stay. The cage is sampled by triangle index and
// barycentrics, so it stays only for an identical mesh; the low poly's base colours are
// read again from the new mesh.
next.bake_meshes = self.bake_meshes.clone();
next.bake_meshes.low_base_color = Default::default();
if next.bake_meshes.cage.is_some() && *next.mesh != *self.mesh {
next.bake_meshes.cage = None;
next.log.push("the cage mesh was dropped: the low-poly mesh changed".into());
}
if self.doc.bake_paint.keys().any(|(_, target)| *target == texturecraft_core::BakePaintTarget::Skew) && self.source.uvs != next.source.uvs {
next.log.push("the skew map is laid over the old UV layout; check it in Bake mode".into());
}
next.modified = true;
next.project_id = self.project_id;
let preserved = preserve_strokes.then(|| crate::preserve::preserve_strokes(self, &mut next));
@@ -621,6 +653,8 @@ use unsupported as save_copy;
#[cfg(target_arch = "wasm32")]
use unsupported as reimport;
#[cfg(target_arch = "wasm32")]
use unsupported as change_mesh;
#[cfg(target_arch = "wasm32")]
use unsupported as export_textures;
#[cfg(target_arch = "wasm32")]
use unsupported as export_mesh;
@@ -764,6 +798,82 @@ f 1/1 2/2 3/3
let _ = std::fs::remove_dir_all(&dir);
}
/// A project built on one mesh moves to another mesh file with its layers intact, even when
/// it carries its own copy of the mesh (a plain reimport would only re-read that copy).
#[test]
fn change_mesh_swaps_the_mesh_and_keeps_the_layers() {
let dir = std::env::temp_dir().join(format!("tc_change_mesh_{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let quad = dir.join("quad.obj");
std::fs::write(&quad, "v 0 0 0\nv 1 0 0\nv 1 1 0\nv 0 1 0\nvt 0 0\nvt 1 0\nvt 1 1\nvt 0 1\nf 1/1 2/2 3/3 4/4\n").unwrap();
let tri = dir.join("tri.obj");
std::fs::write(&tri, "v 0 0 0\nv 1 0 0\nv 0 1 0\nvt 0 0\nvt 1 0\nvt 0 1\nf 1/1 2/2 3/3\n").unwrap();
let mut s = Session::from_mesh_bytes("quad.obj", &std::fs::read(&quad).unwrap(), 128, "quad").unwrap();
crate::execute(&mut s, "layer.add.fill", &json!({"color": [1.0, 0.0, 0.0]})).unwrap();
let layers = s.doc.project.layers.len();
assert!(crate::execute(&mut s, "file.changeMesh", &json!({})).is_err(), "a path is required");
crate::execute(&mut s, "file.changeMesh", &json!({"path": tri.display().to_string()})).unwrap();
assert_eq!(s.mesh.triangle_count(), 1, "the new mesh is in use");
assert_eq!(s.doc.project.mesh_file, "tri.obj");
assert_eq!(s.doc.project.layers.len(), layers, "the layers stay");
std::fs::remove_dir_all(&dir).ok();
}
/// Open A, change to B, edit B on disk: Reimport mesh (also after save and reopen) must read B,
/// not A. A failed change leaves the remembered path alone.
#[test]
fn change_mesh_remembers_the_new_file_for_reimport() {
let dir = std::env::temp_dir().join(format!("tc_change_mesh_path_{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let obj = |x: f32| format!("v 0 0 0\nv {x} 0 0\nv 0 1 0\nvt 0 0\nvt 1 0\nvt 0 1\nf 1/1 2/2 3/3\n");
let (a, b) = (dir.join("a.obj"), dir.join("b.obj"));
std::fs::write(&a, obj(1.0)).unwrap();
std::fs::write(&b, obj(2.0)).unwrap();
let width = |s: &Session| s.mesh.positions.iter().map(|p| p.x).fold(0.0_f32, f32::max);
let mut s = Session::new_project("t", primitives::material_ball(), "builtin:material_ball", None, 128).unwrap();
crate::execute(&mut s, "file.new", &json!({"mesh": a.display().to_string(), "resolution": 128})).unwrap();
let a_abs = s.remembered_mesh_path().map(str::to_string).unwrap();
// A failed change (unreadable file) keeps A.
let missing = dir.join("missing.obj");
assert!(crate::execute(&mut s, "file.changeMesh", &json!({"path": missing.display().to_string()})).is_err());
assert_eq!(s.remembered_mesh_path(), Some(a_abs.as_str()));
crate::execute(&mut s, "file.changeMesh", &json!({"path": b.display().to_string()})).unwrap();
assert_eq!(width(&s), 2.0);
let b_abs = s.remembered_mesh_path().map(str::to_string).expect("B is remembered");
assert_ne!(a_abs, b_abs);
// The remembered path survives save and reopen.
let mut reopened = Session::from_project_bytes(&s.to_project_bytes().unwrap()).unwrap();
assert_eq!(reopened.remembered_mesh_path(), Some(b_abs.as_str()));
// Edit B, then Reimport from the remembered path: B's edits arrive, not A.
std::fs::write(&b, obj(3.0)).unwrap();
let remembered = reopened.remembered_mesh_path().map(str::to_string).unwrap();
crate::execute(&mut reopened, "file.reimportMesh", &json!({"path": remembered})).unwrap();
assert_eq!(width(&reopened), 3.0);
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn change_mesh_resets_base_colours_and_drops_a_cage_of_another_mesh() {
let dir = std::env::temp_dir().join(format!("tc_change_mesh_cage_{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let a = dir.join("a.obj");
std::fs::write(&a, "v 0 0 0\nv 1 0 0\nv 0 1 0\nvt 0 0\nvt 1 0\nvt 0 1\nf 1/1 2/2 3/3\n").unwrap();
// Same triangle count, other vertices.
let b = dir.join("b.obj");
std::fs::write(&b, "v 0 0 0\nv 2 0 0\nv 0 2 0\nvt 0 0\nvt 1 0\nvt 0 1\nf 1/1 2/2 3/3\n").unwrap();
let mut s = Session::from_mesh_bytes("a.obj", &std::fs::read(&a).unwrap(), 128, "a").unwrap();
let cage = crate::bake::BakeMesh { name: "cage".into(), bytes: Default::default(), mesh: s.mesh.clone(), base_color: Vec::new() };
s.bake_meshes.cage = Some(cage);
let _ = s.bake_meshes.low_base_color.set(Err("old".into()));
// The same mesh again keeps the cage but not the cached colours.
crate::execute(&mut s, "file.changeMesh", &json!({"path": a.display().to_string()})).unwrap();
assert!(s.bake_meshes.cage.is_some());
assert!(s.bake_meshes.low_base_color.get().is_none(), "the colours are read again");
crate::execute(&mut s, "file.changeMesh", &json!({"path": b.display().to_string()})).unwrap();
assert!(s.bake_meshes.cage.is_none(), "the cage was made for another mesh");
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn unnamed_parts_keep_their_names_and_masks_through_save_and_reopen() {
// A GLB quad whose mesh and node have no name: its one part is named after the file.
+3 -4
View File
@@ -253,8 +253,8 @@ pub(crate) fn apply(p: &Value, f: &mut Filter, cmd: &str) -> Result<()> {
}
Filter::ColorBalance { shadows, midtones, highlights } => {
for (key, slot) in [("shadows", shadows), ("midtones", midtones), ("highlights", highlights)] {
if p.get(key).is_some() {
let v = color_param(p, key).ok_or_else(|| bad(cmd, format!("`{key}` must be [r, g, b] offsets in -1..1")))?;
if let Some(val) = p.get(key).filter(|v| !v.is_null()) {
let v = color_of(val).ok_or_else(|| bad(cmd, format!("`{key}` must be [r, g, b] offsets in -1..1")))?;
*slot = v.map(|x| x.clamp(-1.0, 1.0));
}
}
@@ -305,8 +305,7 @@ pub(crate) fn apply(p: &Value, f: &mut Filter, cmd: &str) -> Result<()> {
}
Filter::ReplaceColor { from, to, tolerance, softness } => {
for (key, slot) in [("from", from), ("to", to)] {
if p.get(key).is_some() {
let v = color_param(p, key).ok_or_else(|| bad(cmd, format!("`{key}` must be a color ([r, g, b] linear or \"#rrggbb\")")))?;
if let Some(v) = color_param(cmd, p, key)? {
*slot = v.map(|x| x.clamp(0.0, 1.0));
}
}
+10 -3
View File
@@ -69,9 +69,16 @@ fn effect_json(session: &Session, effect: &Effect, content: bool) -> Value {
out
}
/// A generator's settings, with its curvature mode and detail size weights when it has them.
/// A generator's settings: global adjustments, then its resolution, replaced inputs and curvature detail when it has them.
fn generator_json(generator: &Generator) -> Value {
let mut out = json!({ "type": format!("{:?}", generator.kind), "level": generator.level, "softness": generator.softness, "noise": generator.noise_amount, "offset": generator.offset, "invert": generator.invert });
let mut out = json!({ "type": format!("{:?}", generator.kind), "level": generator.level, "softness": generator.softness, "noise": generator.noise_amount, "offset": generator.offset, "invert": generator.invert, "blur": generator.global.blur, "balance": generator.global.balance, "contrast": generator.global.contrast });
if !generator.resolution.is_default() {
out["resolution"] =
json!({ "mode": format!("{:?}", generator.resolution.mode), "custom": generator.resolution.custom, "factor": generator.resolution.factor });
}
if !generator.inputs.is_empty() {
out["inputs"] = generator.inputs.iter().map(|(map, source)| (map.id().to_string(), source_json(source))).collect::<serde_json::Map<_, _>>().into();
}
if let Some(detail) = &generator.detail {
out["mode"] = json!(format!("{:?}", detail.mode));
out["sizes"] = json!(detail.weights);
@@ -105,7 +112,7 @@ fn layer_json(s: &Session, l: &Layer, depth: usize) -> Value {
v["geometryMask"] = json!(parts);
}
if matches!(l.kind, LayerKind::Paint) {
// Sorted: the store is a hash map, and inspect output must be stable.
// Sort the hash map entries for stable inspection output
let mut painted: Vec<(&str, usize)> =
s.doc.pixels.layers.iter().filter(|((id, _), _)| *id == l.id).map(|((_, c), img)| (c.id(), img.allocated_count())).collect();
painted.sort_unstable();
+10 -6
View File
@@ -71,9 +71,9 @@ fn add_paint(s: &mut Session, p: &Value) -> Result<Value> {
}
/// A uniform fill from the `color`, `roughness`, `metallic`, `height` and `opacity` params.
pub(crate) fn fill_from(p: &Value) -> Fill {
pub(crate) fn fill_from(cmd: &str, p: &Value) -> Result<Fill> {
let mut fill = Fill::default();
if let Some(c) = color_param(p, "color") {
if let Some(c) = color_param(cmd, p, "color")? {
fill.sources.insert(Channel::BaseColor, Source::Uniform(c));
}
for (key, ch) in [("roughness", Channel::Roughness), ("metallic", Channel::Metallic), ("height", Channel::Height), ("opacity", Channel::Opacity)] {
@@ -81,13 +81,13 @@ pub(crate) fn fill_from(p: &Value) -> Fill {
fill.sources.insert(ch, Source::Uniform([v; 3]));
}
}
fill
Ok(fill)
}
fn add_fill(s: &mut Session, p: &Value) -> Result<Value> {
let ts = s.ts();
let name = strp(p, "name").map(str::to_string).unwrap_or_else(|| s.doc.project.next_layer_name(ts, "Fill layer"));
let fill = fill_from(p);
let fill = fill_from("layer.add.fill", p)?;
let id = s.edit("Add fill layer", |s| {
let id = s.doc.project.alloc_id();
insert_selected(s, Layer::new(id, name, LayerKind::Fill(fill), &channels_of(s)))
@@ -316,6 +316,10 @@ fn fill_set(s: &mut Session, p: &Value) -> Result<Value> {
let solo = boolp(p, "solo") == Some(true);
let enabled = if solo { Some(true) } else { boolp(p, "enabled") };
let normal_format = normal_format_param("layer.fill.set", p)?;
let low = color_param("layer.fill.set", p, "low")?.unwrap_or([0.0; 3]);
let high = color_param("layer.fill.set", p, "high")?.unwrap_or([1.0; 3]);
let color = color_param("layer.fill.set", p, "color")?;
let value = color_param("layer.fill.set", p, "value")?;
let source = if let Some(kind) = strp(p, "procedural") {
let kind = ProceduralKind::parse(kind).ok_or_else(|| bad("layer.fill.set", format!("unknown procedural `{kind}`")))?;
let mut pr = Procedural::new(kind);
@@ -332,8 +336,8 @@ fn fill_set(s: &mut Session, p: &Value) -> Result<Value> {
pr.balance = v.clamp(-1.0, 1.0);
}
pr.invert = boolp(p, "invert").unwrap_or(false);
Some(Source::Procedural { pattern: pr, low: color_param(p, "low").unwrap_or([0.0; 3]), high: color_param(p, "high").unwrap_or([1.0; 3]) })
} else if let Some(c) = color_param(p, "color").or_else(|| color_param(p, "value")) {
Some(Source::Procedural { pattern: pr, low, high })
} else if let Some(c) = color.or(value) {
Some(Source::Uniform(c))
} else if let Some(name) = strp(p, "image") {
Some(Source::Image { name: name.to_string(), normal_format: normal_format.unwrap_or_default() })
+2 -1
View File
@@ -41,7 +41,7 @@ mod path_draft;
pub mod path_tool;
mod polygon_fill;
mod polygon_fill_drag;
// Mesh reimport reads files: native only.
// Mesh reimport reads files on native targets only
#[cfg(not(target_arch = "wasm32"))]
mod preserve;
pub mod preset_cmds;
@@ -55,6 +55,7 @@ pub mod smart_masks;
pub mod template_cmds;
pub mod text_cmds;
pub mod thumbs;
mod tool_availability;
pub mod tool_cmds;
mod triangle_partition;
mod uv_stroke_state;
+132 -30
View File
@@ -56,7 +56,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Save MaterialCraft package",
[],
None,
"{layer?, path} — writes the MaterialCraft package (.mcraft) behind a material layer, to open in MaterialCraft",
"{layer?, path} — writes the MaterialCraft source (.mcraft or .sbs) behind a material layer",
has_layer,
save_source
),
@@ -178,7 +178,9 @@ 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 link = link_of(s, CMD, id)?;
let ext = source_extension(&link.package);
let path = if path.to_ascii_lowercase().ends_with(&format!(".{ext}")) { path.to_string() } else { format!("{path}.{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}))
@@ -195,6 +197,10 @@ fn save_source(_: &mut Session, _: &Value) -> Result<Value> {
pub struct LinkJob {
pub layer: LayerId,
pub package: Arc<Vec<u8>>,
/// The source package extension (`mcraft` or `sbs`).
pub source_ext: &'static str,
/// The named graph in a linked material; direct package imports use its default.
pub graph: Option<String>,
/// Pixels across (the texture set's resolution).
pub size: u32,
/// Every parameter's value, by id, in MaterialCraft's typed form.
@@ -226,7 +232,7 @@ fn quiet(program: &std::path::Path) -> std::process::Command {
c
}
/// Open `package` (a `.mcraft` file) in the MaterialCraft app (`$MATERIALCRAFT`, next to this
/// Open `package` (a `.mcraft` or `.sbs` 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<()> {
@@ -236,11 +242,21 @@ pub fn open_in_materialcraft(package: &std::path::Path) -> Result<()> {
})
}
/// Source file extensions are chosen from the linked resource name, never an untrusted path.
pub fn source_extension(name: &str) -> &'static str {
if name.to_ascii_lowercase().ends_with(".sbs") { "sbs" } else { MCRAFT_EXT }
}
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>> {
self.run_with_cli(&materialcraft_tool("materialcraft-cli", "MATERIALCRAFT_CLI"))
}
#[cfg(not(target_arch = "wasm32"))]
fn run_with_cli(&self, cli: &std::path::Path) -> 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()));
@@ -252,15 +268,27 @@ impl LinkJob {
std::fs::write(&p, bytes).map_err(|e| other(format!("{name}: {e}")))?;
utf8(&p)
};
let src = write("source.mcraft", &self.package)?;
let ext = if self.source_ext == "sbs" { "sbs" } else { MCRAFT_EXT };
let src = write(&format!("source.{ext}"), &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| {
let mut command = quiet(cli);
if let Some(graph) = &self.graph {
// Opening the source starts on its first graph; select the graph saved in the link.
let script = json!({"open": src, "steps": [
{"id": "graph.open", "params": {"graph": graph}},
{"id": "file.exportMaterial", "params": params},
]});
let script_path = write("cook.json", script.to_string().as_bytes())?;
command.arg("batch").arg(script_path);
} else {
command.arg("run").arg(&src).arg("file.exportMaterial").arg(params.to_string());
}
let run = command.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()
@@ -301,7 +329,15 @@ impl Session {
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() };
let job = LinkJob {
layer: LayerId(0),
package: Arc::new(bytes.to_vec()),
source_ext: MCRAFT_EXT,
graph: None,
size,
values: Map::new(),
inputs: Vec::new(),
};
cooked = job.run()?;
cooked.as_slice()
} else {
@@ -316,7 +352,7 @@ impl Session {
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}"));
let package = unique(s, &format!("{stem}.{}", source_extension(&m.manifest.source)));
s.doc.resources.insert(package.clone(), Arc::new(m.source.clone()));
let link = MaterialLink {
name: m.manifest.name.clone(),
@@ -345,7 +381,7 @@ impl Session {
Ok(id)
}
/// The `.mcraft` package behind linked layer `id`.
/// The source 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)))
@@ -376,45 +412,60 @@ impl Session {
// UDIM tiles share one cook at the texture set's base resolution
let size = self.doc.project.texture_set(texture_set)?.resolution;
Ok(LinkJob { layer: id, package: self.link_package(id)?, size, values, inputs })
Ok(LinkJob {
layer: id,
package: self.link_package(id)?,
source_ext: source_extension(&link.package),
graph: Some(link.graph.clone()),
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 {
let material = read_mcmat(bytes).map_err(|error| EngineError::Other(error.to_string()))?;
let maps = decoded_maps(&material)?;
let names = self.edit_merging(EDIT, merge, |session| {
let LayerKind::Fill(mut fill) = session.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();
for parameter in &mut link.params {
if let Some(cooked) = material.manifest.parameters.iter().find(|candidate| candidate.id == parameter.id) {
parameter.value = cooked.value.clone();
}
}
link.size = m.manifest.size[0];
link.size = material.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);
let old_names: Vec<String> =
fill.sources.values().filter_map(|source| if let Source::Image { name, .. } = source { Some(name.clone()) } else { None }).collect();
let names = put_maps(session, &stem, &maps, &mut fill);
session.doc.project.layer_mut(id)?.kind = LayerKind::Fill(fill);
// Duplicated layers may still use the earlier maps
let mut used_names = Vec::new();
for set in 0..session.doc.project.texture_sets.len() {
for root in session.doc.project.texture_sets.get(set).map(|texture_set| texture_set.stack.clone()).unwrap_or_default() {
crate::material_lib::images_used(&session.doc.project, root, &mut used_names, 0);
}
}
for name in old.iter().filter(|n| !used.contains(n)) {
s.doc.resources.remove(name);
for name in old_names.iter().filter(|name| !used_names.contains(name)) {
session.doc.resources.remove(name);
}
Ok(names)
})?;
self.sync_images();
// Reuse the validated maps before synchronization considers decoding missing images
self.cache_decoded(names, maps);
self.sync_images();
Ok(())
}
@@ -449,3 +500,54 @@ fn put_maps(s: &mut Session, stem: &str, maps: &[(Channel, Vec<u8>, texturecraft
}
names
}
#[cfg(all(test, unix))]
mod subprocess_tests {
use super::*;
use std::os::unix::fs::PermissionsExt;
#[test]
fn cook_uses_the_saved_graph_and_source_type_without_global_environment() {
let id = NEXT_JOB.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!("texturecraft-cook-test-{}-{id}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let cli = dir.join("materialcraft-cli");
let script = r#"#!/bin/sh
set -eu
case "$1" in
batch)
grep -F '"graph":"Second"' "$2" >/dev/null
grep -F '"id":"file.exportMaterial"' "$2" >/dev/null
test -f "$(dirname "$2")/source.sbs"
test -f "$(dirname "$2")/input0.png"
cp "$(dirname "$0")/result.mcmat" "$(dirname "$2")/out.mcmat"
;;
run)
test "$(basename "$2")" = "source.mcraft"
cp "$(dirname "$0")/result.mcmat" "$(dirname "$2")/out.mcmat"
;;
*) exit 1 ;;
esac
"#;
std::fs::write(&cli, script).unwrap();
std::fs::set_permissions(&cli, std::fs::Permissions::from_mode(0o700)).unwrap();
std::fs::write(dir.join("result.mcmat"), b"cooked").unwrap();
let mut job = LinkJob {
layer: LayerId(1),
package: Arc::new(b"source".to_vec()),
source_ext: "sbs",
graph: Some("Second".into()),
size: 64,
values: Map::new(),
inputs: vec![("curvature".into(), b"png".to_vec())],
};
assert_eq!(job.run_with_cli(&cli).unwrap(), b"cooked");
job.graph = None;
job.source_ext = "mcraft";
assert_eq!(job.run_with_cli(&cli).unwrap(), b"cooked");
job.graph = Some("wrong".into());
assert!(job.run_with_cli(&cli).is_err());
assert!(job.run_with_cli(&dir.join("missing")).is_err());
let _ = std::fs::remove_dir_all(dir);
}
}
+92 -11
View File
@@ -2,8 +2,8 @@
use serde_json::{Value, json};
use texturecraft_core::{
BlendMode, Channel, CompareOp, CurvatureDetail, CurvatureMode, Effect, EffectKind, Filter, Generator, GeneratorKind, LayerId, Levels, Mask, MaskBackground,
Procedural, ProceduralKind, Project, Source,
BakedMap, BlendMode, Channel, CompareOp, CurvatureDetail, CurvatureMode, Effect, EffectKind, Filter, Generator, GeneratorKind, LayerId, Levels, Mask,
MaskBackground, OutputResolution, Procedural, ProceduralKind, Project, ResolutionMode, Source,
};
use crate::commands::{CommandSpec, always, bad, blend_param, boolp, channel_param, color_param, f32p, has_layer, layer_param, strp, u64p};
@@ -49,7 +49,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Add generator",
["Layers", "Effects"],
None,
"{layer?, name?, generator?: edgeWear|dirt|thinAreas|curvature|ambientOcclusion|position|direction, level?, softness?, noise?, offset?: -1..1 added to the result, invert?, mode?: edges|cavities|both|raw, sizes?: [7 weights 0..1, finest first] (curvature and edgeWear)}",
"{layer?, name?, generator?: edgeWear|dirt|thinAreas|curvature|ambientOcclusion|position|direction, level?, softness?, noise?, offset?: -1..1 added to the result, invert?, blur?: 0..1, balance?: 0..1 (0.5 unchanged), contrast?: 0..1, resolution?: auto|textureSet|uvTile|custom, customResolution?, resolutionFactor?: -3..3 (1/8..x8), mode?: edges|cavities|both|raw, sizes?: [7 weights 0..1, finest first] (curvature and edgeWear)}",
has_layer,
add_generator
),
@@ -94,7 +94,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Edit effect",
[],
None,
"{effect, visible?, blend?, opacity?, name?, filter?: id from filter.list (a filter effect only: swaps its type, keeping position, blend and opacity; its current id edits it in place), level?, softness?, noise?, noiseScale?, offset?, invert?, mode?, sizes?, value?, anchor?, anchorChannel?, inLow?, inHigh?, gamma?, outLow?, outHigh?, tolerance?, colors?: [[r,g,b], ...] (a color selection's ID colors), filter parameters (see effect.add.filter)}",
"{effect, visible?, blend?, opacity?, name?, filter?: id from filter.list (a filter effect only: swaps its type, keeping position, blend and opacity; its current id edits it in place), generator?: kind (a generator effect only: changes kind in place), level?, softness?, noise?, noiseScale?, offset?, invert?, blur?, balance?, contrast?, resolution?, customResolution?, resolutionFactor?, input?: mesh map id + one of inputImage?, inputValue?, inputAnchor?, inputBaked?: true (a generator), mode?, sizes?, value?, anchor?, anchorChannel?, inLow?, inHigh?, gamma?, outLow?, outHigh?, tolerance?, colors?: [[r,g,b], ...] (a color selection's ID colors), filter parameters (see effect.add.filter)}",
has_layer,
set
),
@@ -141,7 +141,7 @@ fn add_mask(s: &mut Session, p: &Value, bg: MaskBackground) -> Result<Value> {
}
fn add_color_mask(s: &mut Session, p: &Value) -> Result<Value> {
let color = color_param(p, "color").unwrap_or([1.0, 0.0, 0.0]);
let color = color_param("mask.add.colorSelection", p, "color")?.unwrap_or([1.0, 0.0, 0.0]);
let tolerance = f32p(p, "tolerance").unwrap_or(0.1).clamp(0.0, 1.0);
with_mask(s, p, "mask.add.colorSelection", "Add mask with color selection", |s, id| {
let eid = s.doc.project.alloc_id();
@@ -242,7 +242,7 @@ fn push_effect(s: &mut Session, p: &Value, kind: EffectKind, blend: BlendMode) -
let label = format!("Add {}", kind.name().to_ascii_lowercase());
let eid = with_mask(s, p, "effect.add", &label, |s, id| {
let eid = s.doc.project.alloc_id();
// An instance's content and (unless it has its own) mask belong to its source.
// An instance's content and inherited mask belong to its source
let id = if content { s.doc.project.content_layer(id).map_or(id, |l| l.id) } else { s.doc.project.mask_owner(id) };
let l = s.doc.project.layer_mut(id)?;
let bg = if matches!(kind, EffectKind::Generator(_) | EffectKind::Fill { .. }) { MaskBackground::Black } else { MaskBackground::White };
@@ -285,7 +285,7 @@ fn add_fill(s: &mut Session, p: &Value) -> Result<Value> {
let blend = blend_param("effect.add.fill", p, "blend")?.unwrap_or(BlendMode::Normal);
let id = layer_param(s, "effect.add.fill", p)?;
if content_target(p, "effect.add.fill")? {
let mut fill = crate::layer_cmds::fill_from(p);
let mut fill = crate::layer_cmds::fill_from("effect.add.fill", p)?;
if fill.sources.is_empty() {
fill.sources = s.doc.project.layer(id)?.channels.keys().map(|c| (*c, Source::uniform_of(*c))).collect();
}
@@ -323,13 +323,13 @@ fn generator_from(command: &str, params: &Value, generator: &mut Generator) -> R
generator.softness = softness.clamp(0.0, 1.0);
}
if let Some(noise) = f32p(params, "noise") {
generator.noise_amount = noise.clamp(0.0, 2.0);
generator.noise_amount = noise.clamp(0.0, Generator::MAX_NOISE_AMOUNT);
}
if let Some(offset) = f32p(params, "offset") {
generator.offset = offset.clamp(-1.0, 1.0);
}
if let Some(noise_scale) = f32p(params, "noiseScale") {
generator.noise_scale = noise_scale.clamp(0.01, 1000.0);
generator.noise_scale = noise_scale.clamp(Generator::MIN_NOISE_SCALE, Generator::MAX_NOISE_SCALE);
}
if let Some(seed) = u64p(params, "seed") {
generator.seed = seed as u32;
@@ -337,9 +337,29 @@ fn generator_from(command: &str, params: &Value, generator: &mut Generator) -> R
if let Some(invert) = boolp(params, "invert") {
generator.invert = invert;
}
if let Some(direction) = color_param(params, "direction") {
if let Some(direction) = color_param(command, params, "direction")? {
generator.direction = direction;
}
if let Some(blur) = f32p(params, "blur") {
generator.global.blur = blur.clamp(0.0, 1.0);
}
if let Some(balance) = f32p(params, "balance") {
generator.global.balance = balance.clamp(0.0, 1.0);
}
if let Some(contrast) = f32p(params, "contrast") {
generator.global.contrast = contrast.clamp(0.0, 1.0);
}
if let Some(value) = params.get("resolution") {
let name = value.as_str().ok_or_else(|| bad(command, "`resolution` must be a string"))?;
generator.resolution.mode = ResolutionMode::parse(name).ok_or_else(|| bad(command, format!("unknown resolution `{name}`")))?;
}
if let Some(custom) = u64p(params, "customResolution") {
generator.resolution.custom = custom.clamp(1, u64::from(OutputResolution::MAX_SIZE)) as u32;
}
if let Some(factor) = params.get("resolutionFactor") {
let factor = factor.as_i64().ok_or_else(|| bad(command, "`resolutionFactor` must be a whole number"))?;
generator.resolution.factor = factor.clamp(i64::from(*OutputResolution::FACTORS.start()), i64::from(*OutputResolution::FACTORS.end())) as i8;
}
// A mode or sizes also give a curvature generator saved before detail sizes its detail
let mode = params
@@ -378,6 +398,35 @@ fn size_weights(command: &str, value: &Value) -> Result<[f32; CurvatureDetail::S
Ok(weights)
}
/// The generator input named by `input` and what replaces it (None: its mesh map again), for a generator on layer `owner`.
fn generator_input_param(session: &Session, owner: LayerId, params: &Value) -> Result<Option<(BakedMap, Option<Source>)>> {
const CMD: &str = "effect.set";
let Some(name) = params.get("input") else { return Ok(None) };
let name = name.as_str().ok_or_else(|| bad(CMD, "`input` must be a mesh map id"))?;
let map = BakedMap::parse(name).ok_or_else(|| bad(CMD, format!("unknown input `{name}`")))?;
let image = strp(params, "inputImage");
let value = f32p(params, "inputValue");
let anchor = u64p(params, "inputAnchor");
let baked = boolp(params, "inputBaked") == Some(true);
let source = match (image, value, anchor, baked) {
(Some(image), None, None, false) => {
if !session.images.contains_key(image) {
return Err(bad(CMD, format!("no image resource `{image}`")));
}
Some(Source::Image { name: image.to_string(), normal_format: Default::default() })
}
(None, Some(value), None, false) => Some(Source::Uniform([value.clamp(0.0, 1.0); 3])),
(None, None, Some(anchor), false) => {
let channel = channel_param(CMD, params, "anchorChannel")?.unwrap_or(Channel::Height);
Some(anchor_source(session, CMD, owner, LayerId(anchor), channel)?)
}
(None, None, None, true) => None,
_ => return Err(bad(CMD, "`input` takes exactly one of inputImage, inputValue, inputAnchor or inputBaked")),
};
Ok(Some((map, source)))
}
fn add_generator(session: &mut Session, params: &Value) -> Result<Value> {
let kind = match strp(params, "generator") {
Some(name) => GeneratorKind::parse(name).ok_or_else(|| bad("effect.add.generator", format!("unknown generator `{name}`")))?,
@@ -458,6 +507,13 @@ fn set(session: &mut Session, params: &Value) -> Result<Value> {
const CMD: &str = "effect.set";
let effect = effect_id(session, params, CMD)?;
let blend = blend_param(CMD, params, "blend")?;
let generator_kind = params
.get("generator")
.map(|value| {
let name = value.as_str().ok_or_else(|| bad(CMD, "`generator` must be a string"))?;
GeneratorKind::parse(name).ok_or_else(|| bad(CMD, format!("unknown generator `{name}`")))
})
.transpose()?;
// `anchor` picks a fill's anchor point, `anchorChannel` alone re-targets the current one
let anchor = match session.doc.project.effect(effect) {
@@ -475,6 +531,12 @@ fn set(session: &mut Session, params: &Value) -> Result<Value> {
_ => None,
};
// `input` names a generator input that `inputImage`, `inputValue` or `inputAnchor` replaces, or `inputBaked` puts back
let input = match session.doc.project.effect(effect) {
Some((owner, Effect { kind: EffectKind::Generator(_), .. })) => generator_input_param(session, owner, params)?,
_ => None,
};
// Naming the filter the effect already is edits its parameters like a plain effect.set
let swapped = match strp(params, "filter") {
Some(filter) if crate::filter_lib::find(filter).is_some_and(|def| current_filter_id(session, effect) == Some(def.id)) => None,
@@ -508,7 +570,26 @@ fn set(session: &mut Session, params: &Value) -> Result<Value> {
}
match &mut edited.kind {
EffectKind::Generator(generator) => generator_from(CMD, params, generator)?,
EffectKind::Generator(generator) => {
if let Some(kind) = generator_kind
&& kind != generator.kind
{
generator.kind = kind;
generator.detail = Generator::new(kind).detail;
generator.inputs.retain(|map, _| kind.inputs().contains(map));
}
generator_from(CMD, params, generator)?;
if let Some((map, source)) = input {
if !generator.kind.inputs().contains(&map) {
return Err(bad(CMD, format!("this generator has no `{}` input", map.id())));
}
match source {
Some(source) => generator.inputs.insert(map, source),
None => generator.inputs.remove(&map),
};
}
}
_ if generator_kind.is_some() => return Err(bad(CMD, "`generator` requires a generator effect")),
EffectKind::Levels(levels) => levels_from(params, levels),
// The swapped filter was built with the parameters already
EffectKind::Filter(_) if swaps_filter => {}
+13 -14
View File
@@ -7,7 +7,7 @@ use std::sync::Arc;
use serde_json::Value;
use texturecraft_compose::PixelStore;
use texturecraft_core::{EffectKind, LayerId, LayerKind, Project, Source, TextureSet};
use texturecraft_core::{LayerId, LayerKind, Project, Source, TextureSet};
use crate::commands::{CommandSpec, always, has_layer};
use crate::materials::Place;
@@ -36,27 +36,26 @@ fn fe(e: texturecraft_formats::FormatError) -> EngineError {
EngineError::Other(e.to_string())
}
/// Image resources a layer subtree uses (fill sources and fill effects).
pub(crate) fn images_used(p: &Project, id: LayerId, out: &mut Vec<String>, depth: usize) {
/// Image resources a layer subtree uses (fill sources and mask or content effects).
pub(crate) fn images_used(project: &Project, id: LayerId, out: &mut Vec<String>, depth: usize) {
if depth > texturecraft_core::project::MAX_DEPTH {
return;
}
let Some(l) = p.layers.get(&id) else { return };
if let LayerKind::Fill(f) = &l.kind {
for s in f.sources.values() {
if let Source::Image { name, .. } = s {
let Some(layer) = project.layers.get(&id) else { return };
if let LayerKind::Fill(fill) = &layer.kind {
for source in fill.sources.values() {
if let Source::Image { name, .. } = source {
out.push(name.clone());
}
}
out.extend(f.link.as_ref().map(|link| link.package.clone()));
out.extend(fill.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 {
out.push(name.clone());
}
for effect in layer.mask.iter().flat_map(|mask| mask.effects.iter()).chain(&layer.content_effects) {
out.extend(effect.kind.images_read().map(str::to_string));
}
for c in l.children() {
images_used(p, *c, out, depth + 1);
for child in layer.children() {
images_used(project, *child, out, depth + 1);
}
}
+15 -15
View File
@@ -1,5 +1,5 @@
//! Interactive painting on the session: strokes (begin / continue / end / cancel), polygon fill
//! and the color picker. The UI, CLI and agents all go through these.
//! and the material picker. The UI, CLI and agents all go through these.
use std::collections::{BTreeMap, BTreeSet};
@@ -343,9 +343,7 @@ impl Session {
let Some(cache) = self.caches.get_mut(active.ts) else { return };
let settings = &active.stroke.settings;
let pressure = pressure.clamp(0.0, 1.0);
let size_scale = if pressure < 1.0 { settings.min_size + (1.0 - settings.min_size) * pressure } else { 1.0 };
let radius = settings.size * 2.0 * size_scale;
let radius = settings.uv_stamp_radius(pressure);
let step = (settings.spacing * 2.0 * radius).max(MIN_UV_STAMP_STEP);
// Follow path: turn the stamp with the stroke direction (y up on screen, v grows down)
@@ -555,20 +553,22 @@ impl Session {
Ok(())
}
/// Color picker: copy the material under the cursor into the tool's paint values.
/// Material picker: copy the material under the cursor into the tool's paint values
pub fn color_pick(&mut self, screen: Vec2, viewport: Vec2) -> Result<BTreeMap<Channel, [f32; 3]>> {
let hit = self.pick(screen, viewport).ok_or_else(|| EngineError::Other("nothing under the cursor".into()))?;
let ts = self.texture_set_of_material(hit.material).unwrap_or(self.ts());
let cache = self.caches.get(ts).ok_or_else(|| EngineError::Other("texture set not ready".into()))?;
let (w, h) = (cache.size.0 as f32, cache.size.1 as f32);
let (x, y) = ((hit.uv.x * w).clamp(0.0, w - 1.0) as u32, (hit.uv.y * h).clamp(0.0, h - 1.0) as u32);
let mut out = BTreeMap::new();
for c in cache.composite.channels.keys() {
let v = cache.composite.value(*c, x, y);
self.tool.values.insert(*c, v);
out.insert(*c, v);
let texture_set = self.texture_set_of_material(hit.material).unwrap_or(self.ts());
let cache = self.caches.get(texture_set).ok_or_else(|| EngineError::Other("texture set not ready".into()))?;
let (width, height) = (cache.size.0 as f32, cache.size.1 as f32);
let (x, y) = ((hit.uv.x * width).clamp(0.0, width - 1.0) as u32, (hit.uv.y * height).clamp(0.0, height - 1.0) as u32);
let mut picked = BTreeMap::new();
for channel in cache.composite.channels.keys() {
let value = cache.composite.value(*channel, x, y);
self.tool.values.insert(*channel, value);
picked.insert(*channel, value);
}
Ok(out)
Ok(picked)
}
/// The stroke canvas alpha of the active stroke (for overlays and tests).
+70 -27
View File
@@ -12,7 +12,7 @@ use std::sync::Arc;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use texturecraft_compose::{PaintTarget, PixelStore};
use texturecraft_core::{Channel, Effect, EffectKind, LayerId, Mask, MaskBackground, PathShape};
use texturecraft_core::{Channel, Effect, EffectKind, LayerId, LayerKind, Mask, MaskBackground, PathShape};
use texturecraft_geom::glam::{Mat3, Vec2, Vec3};
use texturecraft_geom::{OrbitCamera, surface_at};
use texturecraft_paint::{BrushSettings, Lasso, PickCtx, fill_coverage, pick, spline, spline_segments};
@@ -368,6 +368,10 @@ impl Session {
/// Store the Path tool's curve (new, or the path being edited) and repaint its sublayer as one undo step, then clear the tool
pub fn commit_path(&mut self, viewport: Vec2) -> Result<Value> {
if self.quick_mask.editing {
return Err(EngineError::Other("paths can't edit the quick mask: stop editing it first".into()));
}
if let Some((effect, index)) = self.tool.path.editing
&& self.paths(effect).and_then(|paths| paths.get(index)).is_some_and(|stored| self.same_as_stored(stored))
{
@@ -396,20 +400,20 @@ impl Session {
let shape = self.path_shape(self.camera, viewport);
let editing = self.tool.path.editing;
let label = editing.map_or(NEW_PATH_LABEL.to_string(), |(_, i)| format!("Edit path {}", i + 1));
let out = self.edit_merging(&label, editing.is_some() && self.tool.path.edited, |s| {
let (eid, index) = match editing {
let report = self.edit_merging(&label, editing.is_some() && self.tool.path.edited, |session| {
let (effect_id, index) = match editing {
Some(at) => at,
None => (s.path_effect()?, usize::MAX),
None => (session.path_effect()?, usize::MAX),
};
s.put_path(eid, index, shape)
session.put_path(effect_id, index, shape)
})?;
if let Some(eid) = out.get("effect").and_then(Value::as_u64) {
self.target = StackTarget::Effect(LayerId(eid));
if let Some(effect_id) = report.get("effect").and_then(Value::as_u64) {
self.target = StackTarget::Effect(LayerId(effect_id));
}
self.tool.path.clear();
Ok(out)
Ok(report)
}
/// Repaint the edited path (one merged undo step) or a new path's preview (no step) when its points, brush or material changed; called every frame
@@ -429,9 +433,13 @@ impl Session {
/// Preview a command's points in its supplied viewport, or the UI's current viewport
fn path_live_in_view(&mut self, viewport: Vec2) -> Result<bool> {
if self.quick_mask.editing {
return Ok(false);
}
let stored = match self.tool.path.editing {
Some((eid, index)) => match self.paths(eid).and_then(|v| v.get(index)).cloned() {
Some(shape) => Some((eid, index, shape)),
Some((effect_id, index)) => match self.paths(effect_id).and_then(|paths| paths.get(index)).cloned() {
Some(shape) => Some((effect_id, index, shape)),
None => {
self.stop_path_edit();
return Ok(false);
@@ -661,6 +669,11 @@ impl Session {
let from = if back { &self.path_steps.0 } else { &self.path_steps.1 };
let Some((index, state)) = from.iter().enumerate().rfind(|(_, state)| !state.same_curve(&self.tool.path)) else { return Ok(false) };
// Returning false here would let undo/redo consume document history instead.
if self.quick_mask.editing {
return Err(EngineError::Other("paths can't edit the quick mask: stop editing it first".into()));
}
let path = &mut self.tool.path;
let previous = std::mem::replace(path, PathState { editing: path.editing, edited: path.edited, ..state.clone() });
if let Err(error) = self.path_live() {
@@ -706,33 +719,40 @@ impl Session {
self.path_steps = Default::default();
}
/// The selected path sublayer, or a new one on the selected layer's content or mask
/// (whichever is selected).
/// The selected path sublayer, or a new one on the selected paint layer's content or mask
fn path_effect(&mut self) -> Result<LayerId> {
let id = self.selected_layer().ok_or_else(|| EngineError::Other("select a layer to add the path to".into()))?;
let p = &self.doc.project;
if self.quick_mask.editing {
return Err(EngineError::Other("paths can't edit the quick mask: stop editing it first".into()));
}
let selected = self.selected_layer().ok_or_else(|| EngineError::Other("select a layer to add the path to".into()))?;
let project = &self.doc.project;
let mask = match self.target {
StackTarget::Effect(e) => match p.effect(e) {
Some((_, x)) if matches!(x.kind, EffectKind::Path(_)) => return Ok(e),
Some((owner, _)) => !p.layer(owner)?.content_effects.iter().any(|x| x.id == e),
StackTarget::Effect(effect) => match project.effect(effect) {
// Paths saved in a fill layer before paths there were removed stay editable
Some((_, found)) if matches!(found.kind, EffectKind::Path(_)) => return Ok(effect),
Some((owner, _)) => !project.layer(owner)?.content_effects.iter().any(|candidate| candidate.id == effect),
None => false,
},
StackTarget::Mask => true,
StackTarget::Content => false,
};
// An instance's content and (unless it has its own) mask belong to its source.
let owner = if mask { p.mask_owner(id) } else { p.content_layer(id).map_or(id, |l| l.id) };
let eid = self.doc.project.alloc_id();
let l = self.doc.project.layer_mut(owner)?;
let e = Effect::new(eid, EffectKind::Path(Vec::new()));
// An instance's content and (unless it has its own) mask belong to its source
let owner = if mask { project.mask_owner(selected) } else { project.content_layer(selected).map_or(selected, |layer| layer.id) };
let path_id = self.doc.project.alloc_id();
let layer = self.doc.project.layer_mut(owner)?;
let path = Effect::new(path_id, EffectKind::Path(Vec::new()));
if mask {
l.mask.get_or_insert_with(|| Mask::new(MaskBackground::Black)).effects.push(e);
} else if l.is_group() {
return Err(EngineError::Other(format!("\"{}\" is a folder: add the path to a layer or a mask", l.name)));
layer.mask.get_or_insert_with(|| Mask::new(MaskBackground::Black)).effects.push(path);
} else if layer.is_group() {
return Err(EngineError::Other(format!("\"{}\" is a folder: add the path to a layer or a mask", layer.name)));
} else if matches!(layer.kind, LayerKind::Paint) {
layer.content_effects.push(path);
} else {
l.content_effects.push(e);
return Err(EngineError::Other(format!("\"{}\" is not a paint layer: add the path to a paint layer or a mask", layer.name)));
}
Ok(eid)
Ok(path_id)
}
/// [`Session::put_path`] recompositing only the tiles it changed, with no undo step: returns the paint report and the document before
@@ -1071,6 +1091,29 @@ mod tests {
assert_eq!(changed_tiles(&before, &after, LayerId(8)), BTreeSet::new());
}
#[test]
fn quick_mask_preserves_pending_path_undo_and_redo() {
let mut session = Session::new_project("t", texturecraft_geom::primitives::plane(1.0, 1), "builtin:test", None, 128).unwrap();
execute(&mut session, "tool.path", &json!({"uvPoints": [[0.2, 0.5], [0.8, 0.5]]})).unwrap();
session.path_checkpoint();
session.tool.path.points[0].y = 0.3;
session.path_live().unwrap();
for back in [true, false] {
let curve = session.tool.path.clone();
let steps = (session.path_steps.0.len(), session.path_steps.1.len());
let history = (session.history_labels(), session.redo_labels());
let pixels = session.doc.pixels.clone();
session.quick_mask.editing = true;
assert!(if back { session.undo() } else { session.redo() }.is_err());
assert!(curve.same_curve(&session.tool.path));
assert_eq!(steps, (session.path_steps.0.len(), session.path_steps.1.len()));
assert_eq!(history, (session.history_labels(), session.redo_labels()));
assert_eq!(format!("{:?}", pixels), format!("{:?}", session.doc.pixels));
session.quick_mask.editing = false;
if back { session.undo() } else { session.redo() }.unwrap();
}
}
#[test]
fn point_checkpoints_skip_noops_and_bound_retained_points() {
let mut session = Session::new_project("t", texturecraft_geom::primitives::plane(1.0, 1), "builtin:test", None, 128).unwrap();
+65 -2
View File
@@ -16,8 +16,14 @@ use crate::Session;
const REACH: f32 = 0.05;
/// Re-project the painted pixels of `next` (a session on the reimported mesh holding `old`'s
/// document) from `old`'s UV layout. Returns how many texels found the old surface.
/// document) from `old`'s UV layout. Returns how many texels found the old surface;
/// a texture set where none does keeps its pixels as they are. Nothing is re-projected when the
/// UV layout and topology are unchanged (same triangles, materials and UVs): the pixels already
/// sit where they belong, however far the mesh moved.
pub(crate) fn preserve_strokes(old: &Session, next: &mut Session) -> usize {
if same_layout(&old.mesh, &next.mesh) {
return 0;
}
let reach = REACH * 2.0 * old.mesh.radius().max(next.mesh.radius());
let mut found = 0;
for ts in 0..next.set_count() {
@@ -52,7 +58,12 @@ pub(crate) fn preserve_strokes(old: &Session, next: &mut Session) -> usize {
((tx, ty), src)
})
.collect();
found += table.values().flatten().filter(|p| p[0].is_finite()).count();
let matched = table.values().flatten().filter(|p| p[0].is_finite()).count();
found += matched;
// No old surface within reach anywhere (a moved or unrelated mesh): the paint stays where it is.
if matched == 0 {
continue;
}
let map = |x: f32, y: f32| {
let (xi, yi) = (x as u32, y as u32);
table.get(&(xi / TILE, yi / TILE)).and_then(|t| t.get(((yi % TILE) * TILE + xi % TILE) as usize)).filter(|p| p[0].is_finite()).map(|p| (p[0], p[1]))
@@ -63,6 +74,12 @@ pub(crate) fn preserve_strokes(old: &Session, next: &mut Session) -> usize {
found
}
/// Same index buffer, same UVs per vertex and the same material ranges.
fn same_layout(a: &texturecraft_geom::Mesh, b: &texturecraft_geom::Mesh) -> bool {
let ranges = |m: &texturecraft_geom::Mesh| m.submeshes.iter().map(|s| (s.material, s.start, s.count)).collect::<Vec<_>>();
a.indices == b.indices && a.uvs == b.uvs && ranges(a) == ranges(b)
}
#[cfg(test)]
mod tests {
use super::*;
@@ -104,4 +121,50 @@ mod tests {
}
assert!(right > 10_000 && wrong == 0, "{right} right, {wrong} wrong");
}
#[test]
fn a_mesh_with_no_surface_in_reach_keeps_the_paint_where_it_is() {
let mut s = Session::new_project("p", primitives::builtin("plane").unwrap(), "builtin:plane", None, 128).unwrap();
let id = s.doc.project.texture_sets[0].stack[0];
let img = s.doc.pixels.layer_mut(id, Channel::BaseColor, (128, 128));
for x in 0..64 {
img.set(x, 40, &[65535, 0, 0, 65535]);
}
// The same plane far away and with mirrored UVs (so the layout differs and the unchanged-layout
// shortcut is not taken): nothing of the old surface is within reach.
let mut mesh = (*s.mesh).clone();
for p in &mut mesh.positions {
p.x += 100.0;
}
for uv in &mut mesh.uvs {
*uv = Vec2::new(1.0 - uv.x, uv.y);
}
let mut next = Session::new(s.doc.clone(), mesh, None).unwrap();
assert_eq!(preserve_strokes(&s, &mut next), 0);
let img = next.doc.pixels.layer(id, Channel::BaseColor).unwrap();
assert_eq!(img.get(10, 40)[3], 65535, "the painted texel is still there");
}
#[test]
fn a_translated_mesh_with_the_same_uvs_keeps_every_pixel() {
let mut s = Session::new_project("p", primitives::builtin("plane").unwrap(), "builtin:plane", None, 128).unwrap();
let id = s.doc.project.texture_sets[0].stack[0];
let img = s.doc.pixels.layer_mut(id, Channel::BaseColor, (128, 128));
for x in 0..128 {
img.set(x, 40, &[65535, 0, 0, 65535]);
}
// Slid along its own surface by a fraction of the plane: half of it still overlaps the old one.
let mut mesh = (*s.mesh).clone();
let shift = mesh.radius() * 0.5;
for p in &mut mesh.positions {
p.x += shift;
}
let mut next = Session::new(s.doc.clone(), mesh, None).unwrap();
assert_eq!(preserve_strokes(&s, &mut next), 0);
let img = next.doc.pixels.layer(id, Channel::BaseColor).unwrap();
for x in 0..128 {
assert_eq!(img.get(x, 40)[3], 65535, "texel {x} of the painted row moved");
assert_eq!(img.get(x, 41)[3], 0, "texel {x} of the next row got paint");
}
}
}
+32 -23
View File
@@ -35,44 +35,53 @@ pub fn specs() -> Vec<CommandSpec> {
"Edit quick mask",
["Viewport"],
Some("U"),
"{editing?: bool} — toggles when omitted; editing enables the mask",
always,
"{editing?: bool} — toggles when omitted; needs the quick mask enabled",
mask_enabled,
edit
),
cmd!("viewport.quickMask.invert", "Invert quick mask", ["Viewport"], Some("Ctrl+Shift+I"), "{}", always, invert),
cmd!("viewport.quickMask.invert", "Invert quick mask", ["Viewport"], Some("Ctrl+Shift+I"), "{}", mask_enabled, invert),
]
}
fn state(s: &Session) -> Value {
let q = &s.quick_mask;
fn mask_enabled(session: &Session) -> std::result::Result<(), String> {
if session.quick_mask.enabled { Ok(()) } else { Err("enable the quick mask first".into()) }
}
fn state(session: &Session) -> Value {
let mask = &session.quick_mask;
json!({
"enabled": q.enabled, "editing": q.editing, "inverted": q.inverted,
"paintedTiles": q.masks.get(&s.ts()).map(TileImage::allocated_count).unwrap_or(0),
"enabled": mask.enabled, "editing": mask.editing, "inverted": mask.inverted,
"paintedTiles": mask.masks.get(&session.ts()).map(TileImage::allocated_count).unwrap_or(0),
})
}
fn changed(s: &mut Session) -> Result<Value> {
s.quick_mask.revision = s.quick_mask.revision.wrapping_add(1);
s.revision = s.revision.wrapping_add(1);
Ok(state(s))
fn changed(session: &mut Session) -> Result<Value> {
session.quick_mask.revision = session.quick_mask.revision.wrapping_add(1);
session.revision = session.revision.wrapping_add(1);
Ok(state(session))
}
fn enable(s: &mut Session, p: &Value) -> Result<Value> {
s.quick_mask.enabled = boolp(p, "enabled").unwrap_or(!s.quick_mask.enabled);
changed(s)
fn enable(session: &mut Session, params: &Value) -> Result<Value> {
let enabled = boolp(params, "enabled").unwrap_or(!session.quick_mask.enabled);
if !enabled && session.quick_mask.editing {
session.cancel_stroke();
session.quick_mask.editing = false;
}
session.quick_mask.enabled = enabled;
changed(session)
}
fn edit(s: &mut Session, p: &Value) -> Result<Value> {
s.cancel_stroke();
let q = &mut s.quick_mask;
q.editing = boolp(p, "editing").unwrap_or(!q.editing);
q.enabled |= q.editing;
changed(s)
fn edit(session: &mut Session, params: &Value) -> Result<Value> {
session.cancel_stroke();
let quick_mask = &mut session.quick_mask;
quick_mask.editing = boolp(params, "editing").unwrap_or(!quick_mask.editing);
changed(session)
}
fn invert(s: &mut Session, _: &Value) -> Result<Value> {
s.quick_mask.inverted = !s.quick_mask.inverted;
changed(s)
fn invert(session: &mut Session, _: &Value) -> Result<Value> {
session.quick_mask.inverted = !session.quick_mask.inverted;
changed(session)
}
impl Session {
+49 -26
View File
@@ -21,7 +21,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Add fill layer from image",
[],
None,
"{name, channel?: baseColor, projection?: uv|triplanar|planar, scale?}",
"{name (exact resource name with extension or unique stem), channel?: baseColor, projection?: uv|triplanar|planar, scale?}",
always,
add_image_fill
),
@@ -38,7 +38,7 @@ pub(crate) fn decoded(d: texturecraft_formats::DecodedImage) -> Arc<ImageRes> {
impl Session {
/// Add an image resource from bytes (one undo step); returns its unique, sanitized name
pub fn import_resource_bytes(&mut self, file_name: &str, bytes: Vec<u8>) -> Result<String> {
self.edit("Import resource", |s| s.add_image_resource(file_name, bytes, u64::MAX))
self.edit("Import resource", |session| session.add_image_resource(file_name, bytes, texturecraft_formats::MAX_RESOURCE_TEXELS))
}
/// Add an image within a decoded texel budget, inside the caller's edit
@@ -47,39 +47,38 @@ impl Session {
return Err(EngineError::Other(format!("{file_name} is larger than {} MB", MAX_BYTES >> 20)));
}
let image = texturecraft_formats::decode_image_within(&bytes, max_texels).map_err(|e| EngineError::Other(format!("{file_name}: {e}")))?;
let leaf = std::path::Path::new(file_name).file_name().and_then(|n| n.to_str()).unwrap_or("image");
let image = texturecraft_formats::decode_image_within(&bytes, max_texels).map_err(|error| EngineError::Other(format!("{file_name}: {error}")))?;
let leaf = std::path::Path::new(file_name).file_name().and_then(|name| name.to_str()).unwrap_or("image");
let base = texturecraft_formats::sanitize_resource_name(leaf);
let mut name = base.clone();
let mut n = 2;
let mut duplicate_index = 2;
while self.doc.resources.contains_key(&name) {
name = format!("{n}_{base}");
n += 1;
name = format!("{duplicate_index}_{base}");
duplicate_index += 1;
}
self.doc.resources.insert(name.clone(), Arc::new(bytes));
self.images.insert(name.clone(), decoded(image));
Ok(name)
}
/// Decode resources that are not decoded yet and forget removed ones (after undo, redo, load).
pub fn sync_images(&mut self) {
let keep: Vec<String> = self.doc.resources.keys().cloned().collect();
self.images.retain(|k, _| keep.contains(k));
// 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) => {
self.images.insert(k, decoded(d));
self.images.retain(|name, _| self.doc.resources.contains_key(name));
// Linked material packages are resources but cannot be decoded as images
let package_suffix = format!(".{}", texturecraft_formats::mcmat::MCRAFT_EXT);
for (name, bytes) in &self.doc.resources {
if self.images.contains_key(name) || name.ends_with(&package_suffix) {
continue;
}
match texturecraft_formats::decode_image(bytes) {
Ok(image) => {
self.images.insert(name.clone(), decoded(image));
}
Err(e) => self.log.push(format!("resource {k}: {e}")),
Err(error) => self.log.push(format!("resource {name}: {error}")),
}
}
}
@@ -179,11 +178,35 @@ fn remove_unused(s: &mut Session, _: &Value) -> Result<Value> {
Ok(json!({ "removed": unused }))
}
fn add_image_fill(s: &mut Session, p: &Value) -> Result<Value> {
let name = strp(p, "name").ok_or_else(|| bad("layer.add.image", "`name` is required"))?.to_string();
if !s.doc.resources.contains_key(&name) {
return Err(bad("layer.add.image", format!("no resource `{name}`")));
#[cfg(not(target_arch = "wasm32"))]
const IMPORT_HINT: &str = "resource.import";
#[cfg(target_arch = "wasm32")]
const IMPORT_HINT: &str = "File > Import resources";
/// Resolve an image resource by exact name or unique file stem.
fn resolve_resource_name(s: &Session, name: &str) -> Result<String> {
if s.images.contains_key(name) {
return Ok(name.to_string());
}
let matches: Vec<&String> = s.images.keys().filter(|k| std::path::Path::new(k).file_stem().and_then(|stem| stem.to_str()) == Some(name)).collect();
match matches.as_slice() {
[single] => Ok((*single).clone()),
[] => {
let available: Vec<&str> = s.images.keys().map(String::as_str).collect();
let hint =
if available.is_empty() { format!(" (no resources imported: use {IMPORT_HINT})") } else { format!(" (available: {})", available.join(", ")) };
Err(bad("layer.add.image", format!("no resource `{name}`{hint}")))
}
multiple => {
let matches_str = multiple.iter().map(|m| format!("`{m}`")).collect::<Vec<_>>().join(", ");
Err(bad("layer.add.image", format!("ambiguous resource `{name}` matches {matches_str}: give the full name with extension")))
}
}
}
fn add_image_fill(s: &mut Session, p: &Value) -> Result<Value> {
let raw_name = strp(p, "name").ok_or_else(|| bad("layer.add.image", "`name` is required"))?;
let name = resolve_resource_name(s, raw_name)?;
let ch = channel_param("layer.add.image", p, "channel")?.unwrap_or(Channel::BaseColor);
let projection = crate::layer_cmds::projection_from(p, "layer.add.image", None)?;
let ts = s.ts();
+128 -53
View File
@@ -109,7 +109,7 @@ impl Tool {
Tool::PolygonFill => "Polygon fill",
Tool::Smudge => "Smudge",
Tool::Clone => "Clone",
Tool::ColorPicker => "Color picker",
Tool::ColorPicker => "Material picker",
Tool::Path => "Path",
Tool::GeometryMask => "Geometry mask",
}
@@ -571,23 +571,56 @@ impl Session {
self.invalidate_changed(other);
}
/// Mark dirty every texture set whose composite differs between `other` and the current
/// Mark dirty every texture set whose composite differs between the previous and current
/// document (after an edit, undo or redo).
pub(crate) fn invalidate_changed(&mut self, other: &Document) {
self.follow_uv_layouts(&other.project);
pub(crate) fn invalidate_changed(&mut self, previous_document: &Document) {
self.follow_uv_layouts(&previous_document.project);
self.sync_caches();
let resources = |d: &Document| d.resources.iter().map(|(n, b)| (n.clone(), Arc::as_ptr(b))).collect::<Vec<_>>();
let all = resources(other) != resources(&self.doc);
let (empty, images) = (BakedMaps::default(), Default::default());
let key = |d: &Document, ts: usize| {
let stack = d.project.texture_sets.get(ts).map(|s| s.stack.as_slice()).unwrap_or_default();
StackKey::new(&d.project, &d.pixels, d.baked.get(&ts).unwrap_or(&empty), &images, ts, stack)
// Removed or replaced resources may change any set's rendered content
let removed_or_replaced_resource =
previous_document.resources.iter().any(|(name, previous)| !self.doc.resources.get(name).is_some_and(|current| Arc::ptr_eq(previous, current)));
let added_resource_names: BTreeSet<_> = self.doc.resources.keys().filter(|name| !previous_document.resources.contains_key(*name)).collect();
let uses_added_resource = |texture_set_index: usize| {
use texturecraft_core::{EffectKind, Fill, Source};
let uses_source = |source: &Source| matches!(source, Source::Image { name, .. } if added_resource_names.contains(name));
let uses_fill =
|fill: &Fill| fill.sources.values().any(uses_source) || fill.link.as_ref().is_some_and(|link| added_resource_names.contains(&link.package));
let project = &self.doc.project;
!added_resource_names.is_empty()
&& project.texture_sets.get(texture_set_index).is_some_and(|set| {
project.render_tree(&set.stack).iter().filter_map(|id| project.layers.get(id)).any(|layer| {
matches!(&layer.kind, LayerKind::Fill(fill) if uses_fill(fill))
|| layer.mask.iter().flat_map(|mask| &mask.effects).chain(&layer.content_effects).any(|effect| match &effect.kind {
EffectKind::Fill { source, .. } => uses_source(source),
EffectKind::Material(fill) => uses_fill(fill),
EffectKind::Generator(generator) => generator.inputs.values().any(uses_source),
_ => false,
})
})
})
};
for ts in 0..self.caches.len() {
if (all || key(other, ts) != key(&self.doc, ts))
&& let Some(c) = self.caches.get_mut(ts)
let (empty_baked_maps, empty_images) = (BakedMaps::default(), Default::default());
let stack_key = |document: &Document, texture_set_index: usize| {
let stack = document.project.texture_sets.get(texture_set_index).map(|set| set.stack.as_slice()).unwrap_or_default();
StackKey::new(
&document.project,
&document.pixels,
document.baked.get(&texture_set_index).unwrap_or(&empty_baked_maps),
&empty_images,
texture_set_index,
stack,
)
};
for texture_set_index in 0..self.caches.len() {
if (removed_or_replaced_resource
|| stack_key(previous_document, texture_set_index) != stack_key(&self.doc, texture_set_index)
|| uses_added_resource(texture_set_index))
&& let Some(cache) = self.caches.get_mut(texture_set_index)
{
c.invalidate_all();
cache.invalidate_all();
}
}
}
@@ -732,7 +765,8 @@ impl Session {
self.modified = true;
self.revision = self.revision.wrapping_add(1);
self.invalidate_changed(other);
// An undone add or duplicate takes the selected effect away: select the stack it was in.
// An undone add or duplicate takes the selected effect away
if let StackTarget::Effect(e) = self.target
&& self.doc.project.effect(e).is_none()
{
@@ -740,6 +774,10 @@ impl Session {
let content = p.effect(e).and_then(|(owner, _)| p.layers.get(&owner)).is_some_and(|l| l.content_effects.iter().any(|x| x.id == e));
self.target = if content { StackTarget::Content } else { StackTarget::Mask };
}
let has_mask = self.selected_layer().and_then(|lid| self.doc.project.layer(self.doc.project.mask_owner(lid)).ok()).is_some_and(|l| l.mask.is_some());
if self.target == StackTarget::Mask && !has_mask {
self.target = StackTarget::Content;
}
}
// -----------------------------------------------------------------------------------------
@@ -759,22 +797,24 @@ impl Session {
/// of a finite budget, after the dirty ones.
pub fn update_for(&mut self, budget_ms: f64, clock: impl Fn() -> f64) -> bool {
let start = clock();
let first = if budget_ms.is_finite() { rayon::current_num_threads().clamp(1, CHUNK) } else { CHUNK };
// The layer being edited: everything else is cached per tile.
let hot = self.stroke.as_ref().and_then(|s| s.target).map(|t| self.paint_owner(t)).or_else(|| self.selected_layer());
let stroke_size = self.stroke.as_ref().and_then(|s| self.caches.get(s.ts)).map(|c| c.size);
let preview = self.stroke.as_ref().and_then(|s| {
let initial_chunk_size = if budget_ms.is_finite() { rayon::current_num_threads().clamp(1, CHUNK) } else { CHUNK };
// The layer being edited: everything else is cached per tile
let hot = self.stroke.as_ref().and_then(|stroke| stroke.target).map(|target| self.paint_owner(target)).or_else(|| self.selected_layer());
let stroke_size = self.stroke.as_ref().and_then(|stroke| self.caches.get(stroke.ts)).map(|cache| cache.size);
let preview = self.stroke.as_ref().and_then(|stroke| {
Some(StrokePreview {
target: s.target?,
alpha: s.canvas.alpha.clone(),
opacity: s.stroke.settings.opacity,
erase: s.erase,
values: s.values.clone(),
target: stroke.target?,
alpha: stroke.canvas.alpha.clone(),
opacity: stroke.stroke.settings.opacity,
erase: stroke.erase,
values: stroke.values.clone(),
mask_value: self.tool.mask_value,
stencil: s.stencil.clone(),
carried: s.canvas.pickup.as_ref().map(|p| p.values.clone()).unwrap_or_default(),
stencil: stroke.stencil.clone(),
carried: stroke.canvas.pickup.as_ref().map(|pickup| pickup.values.clone()).unwrap_or_default(),
})
});
let active = self.ts();
let order = std::iter::once(active).chain((0..self.caches.len()).filter(|ts| *ts != active));
let mut changed = false;
@@ -782,24 +822,27 @@ impl Session {
let Some(cache) = self.caches.get_mut(ts) else { continue };
cache.below.set_hot(hot);
cache.below.pass = cache.dirty.len() + cache.settle.len();
// Instances in sets of the stroke's size show the live stroke too.
// Instances in sets of the stroke's size show the live stroke too
let preview = preview.as_ref().filter(|_| Some(cache.size) == stroke_size);
// Start at the size that fit the budget last time: with a heavy stack (smart
// materials at 8K) one tile per thread alone would take several frames' budget.
let mut chunk = if budget_ms.is_finite() && cache.chunk > 0 { cache.chunk } else { first };
// Reuse the last affordable chunk size for heavy stacks
let mut chunk = if budget_ms.is_finite() && cache.chunk > 0 { cache.chunk } else { initial_chunk_size };
loop {
if cache.dirty.is_empty() {
if cache.settle.is_empty() || clock() - start >= budget_ms.min(SETTLE_BUDGET_MS) {
break;
}
// Small chunks: the settle slice of the budget is short.
cache.dirty.extend(next_chunk(&mut cache.settle, chunk.min(first)));
// Small chunks: the settle slice of the budget is short
cache.dirty.extend(next_chunk(&mut cache.settle, chunk.min(initial_chunk_size)));
}
let t0 = clock();
let chunk_started = clock();
let tiles = next_chunk(&mut cache.dirty, chunk);
for t in &tiles {
cache.settle.remove(t);
for tile in &tiles {
cache.settle.remove(tile);
}
let empty = BakedMaps::default();
let inputs = texturecraft_compose::Inputs {
project: &self.doc.project,
@@ -813,30 +856,39 @@ impl Session {
images: &self.images,
bounds: self.mesh.bounds(),
};
// Look ahead so sliced updates filter a run of tiles once
cache.below.ahead = if budget_ms.is_finite() { tiles_ahead(&cache.dirty, &tiles) } else { Vec::new() };
texturecraft_compose::composite_tiles_cached(&inputs, &mut cache.composite, &tiles, Some(&mut cache.below));
// The composited tiles and the neighbours their padding reached change.
// The composited tiles and the neighbours their padding reached change
let padded = pad_tiles(&mut cache.composite, &cache.dilation, &tiles);
cache.upload.extend(tiles.iter().copied().chain(padded));
cache.revision = cache.revision.wrapping_add(1);
changed = true;
let now = clock();
let left = budget_ms - (now - start);
// As many tiles as fit in what is left, at the pace of this chunk.
let per_tile = (now - t0) / tiles.len().max(1) as f64;
if budget_ms.is_finite() && per_tile > 0.0 {
// Never above one tile per thread: work can turn heavy from one frame to the next.
cache.chunk = ((budget_ms / per_tile) as usize).clamp(1, first);
let remaining_ms = budget_ms - (now - start);
// Estimate how many tiles fit in the remaining frame budget
let ms_per_tile = (now - chunk_started) / tiles.len().max(1) as f64;
if budget_ms.is_finite() && ms_per_tile > 0.0 {
// Never above one tile per thread: work can turn heavy from one frame to the next
cache.chunk = ((budget_ms / ms_per_tile) as usize).clamp(1, initial_chunk_size);
}
if left <= 0.0 {
if remaining_ms <= 0.0 {
return changed;
}
let fit = if per_tile > 0.0 { (left / per_tile) as usize } else { CHUNK };
if fit == 0 {
let fitting_tiles = if ms_per_tile > 0.0 { (remaining_ms / ms_per_tile) as usize } else { CHUNK };
if fitting_tiles == 0 {
return changed;
}
chunk = fit.min(CHUNK);
chunk = fitting_tiles.min(CHUNK);
}
}
changed
}
@@ -959,19 +1011,42 @@ pub const SETTLE_BUDGET_MS: f64 = 6.0;
/// Take up to `n` dirty tiles, block by block (16 × 16 tiles) so neighbourhood filters
/// evaluate compact regions.
fn next_chunk(dirty: &mut BTreeSet<(u32, u32)>, n: usize) -> Vec<(u32, u32)> {
if dirty.len() <= n {
fn next_chunk(dirty: &mut BTreeSet<(u32, u32)>, max_tiles: usize) -> Vec<(u32, u32)> {
// A zero-sized request cannot select a pivot
if max_tiles == 0 {
return Vec::new();
}
if dirty.len() <= max_tiles {
return std::mem::take(dirty).into_iter().collect();
}
let mut tiles: Vec<(u32, u32)> = dirty.iter().copied().collect();
tiles.sort_by_key(|&(x, y)| (y / 16, x / 16, y, x));
tiles.truncate(n);
for t in &tiles {
dirty.remove(t);
// Partition by block order so only the requested chunk needs sorting
tiles.select_nth_unstable_by_key(max_tiles - 1, |&(x, y)| (y / texturecraft_compose::BLOCK, x / texturecraft_compose::BLOCK, y, x));
tiles.truncate(max_tiles);
tiles.sort_by_key(|&(x, y)| (y / texturecraft_compose::BLOCK, x / texturecraft_compose::BLOCK, y, x));
for tile in &tiles {
dirty.remove(tile);
}
tiles
}
/// Dirty tiles a neighbourhood filter runs over ahead of the chunk being composited.
const FILTER_AHEAD: usize = 64;
/// The dirty tiles composited after `chunk` in its 16 × 16 tile blocks, in the order [`next_chunk`] takes them, at most [`FILTER_AHEAD`].
fn tiles_ahead(dirty: &BTreeSet<(u32, u32)>, chunk: &[(u32, u32)]) -> Vec<(u32, u32)> {
let block = |&(x, y): &(u32, u32)| (y / texturecraft_compose::BLOCK, x / texturecraft_compose::BLOCK);
let blocks: BTreeSet<(u32, u32)> = chunk.iter().map(block).collect();
let mut ahead: Vec<(u32, u32)> = dirty.iter().copied().filter(|tile| blocks.contains(&block(tile))).collect();
ahead.sort_by_key(|tile| (block(tile), tile.1, tile.0));
ahead.truncate(FILTER_AHEAD);
ahead
}
/// Valid texture set resolutions: powers of two from 128 to 8192.
pub fn check_resolution(r: u32) -> Result<u32> {
if (128..=8192).contains(&r) && r.is_power_of_two() {
+1 -1
View File
@@ -100,7 +100,7 @@ fn add_text(s: &mut Session, p: &Value) -> Result<Value> {
let (font, index) = font_bytes(strp(p, "font"), strp(p, "style"))?;
let size = f32p(p, "size").unwrap_or(256.0).clamp(8.0, 1024.0);
let img = render_text(&font, index, text, size).map_err(|e| bad(cmd, e))?;
let rgb = color_param(p, "color").unwrap_or([1.0; 3]).map(|c| (texturecraft_texel::linear_to_srgb(c.clamp(0.0, 1.0)) * 255.0).round() as u8);
let rgb = color_param(cmd, p, "color")?.unwrap_or([1.0; 3]).map(|c| (texturecraft_texel::linear_to_srgb(c.clamp(0.0, 1.0)) * 255.0).round() as u8);
let png = texturecraft_formats::coverage_png(img.width, img.height, &img.coverage, rgb).map_err(|e| bad(cmd, e.to_string()))?;
// The resource is named after the text (its first line, shortened) unless a name is given.
let label: String = strp(p, "name").unwrap_or_else(|| text.lines().next().unwrap_or_default()).chars().take(32).collect();
+46
View File
@@ -0,0 +1,46 @@
//! Which tools can act on the current selection: the tool shelf and its shortcuts grey the rest
use texturecraft_core::{EffectKind, LayerKind};
use crate::{Session, StackTarget, Tool};
impl Session {
/// Whether `tool` can act on the selection (or on the quick mask while it is being edited)
pub fn tool_usable(&self, tool: Tool) -> bool {
match tool {
// The picker reads the composite under the cursor, whatever is selected
Tool::ColorPicker => return true,
Tool::GeometryMask => return self.selected_layer().is_some(),
_ => {}
}
// Brushes paint the quick mask whatever is selected; paths can't edit it
if self.quick_mask.editing {
return tool != Tool::Path;
}
let project = &self.doc.project;
let Some(layer) = self.selected_layer().and_then(|id| project.layers.get(&id)) else { return false };
// Smudge and Clone read layer content, so they can't yet paint mask values
let reads_content = matches!(tool, Tool::Smudge | Tool::Clone);
match self.target {
StackTarget::Content => matches!(layer.kind, LayerKind::Paint),
StackTarget::Mask => !reads_content,
StackTarget::Effect(effect) => {
let Some((owner, found)) = project.effect(effect) else { return false };
let Some(owner_layer) = project.layers.get(&owner) else { return false };
let in_content = owner_layer.content_effects.iter().any(|candidate| candidate.id == effect);
let in_fill_content = in_content && matches!(owner_layer.kind, LayerKind::Fill(_));
match found.kind {
// Paths saved in a fill layer before paths there were removed stay editable
EffectKind::Path(_) => tool == Tool::Path,
EffectKind::Paint if tool == Tool::Path => !in_fill_content,
EffectKind::Paint => in_content || !reads_content,
_ => false,
}
}
}
}
}
+40 -33
View File
@@ -6,7 +6,7 @@ use texturecraft_core::Channel;
use texturecraft_geom::glam::Vec2;
use texturecraft_paint::{Alignment, BrushAlpha, MAX_RADIAL_COPIES, alphas};
use crate::commands::{CommandSpec, always, bad, boolp, channel_param, color_param, f32p, strp, vec3};
use crate::commands::{CommandSpec, always, bad, boolp, channel_param, color_of, color_param, f32p, strp, vec3};
use crate::fill_hit_test::FillHitTest;
use crate::fill_mode::FillMode;
use crate::session::{CloneFrom, CloneSource, StackTarget, Tool};
@@ -20,7 +20,7 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!("tool.polygonFill", "Polygon fill", ["Tools"], Some("4"), "{}", always, |s, _| set_tool(s, Tool::PolygonFill)),
cmd!("tool.smudge", "Smudge", ["Tools"], Some("5"), "{}", always, |s, _| set_tool(s, Tool::Smudge)),
cmd!("tool.clone", "Clone", ["Tools"], Some("6"), "{}", always, |s, _| set_tool(s, Tool::Clone)),
cmd!("tool.colorPicker", "Color picker", ["Tools"], Some("P"), "{}", always, |s, _| set_tool(s, Tool::ColorPicker)),
cmd!("tool.colorPicker", "Material picker", ["Tools"], Some("P"), "{}", always, |s, _| set_tool(s, Tool::ColorPicker)),
cmd!(
"tool.geometryMask",
"Geometry mask",
@@ -105,7 +105,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Tool click",
[],
None,
"{x, y, viewport?: [w,h]} — polygon fill / color picker / single dab; polygon fill also takes a box {x0, y0, x1, y1, viewport?}, a 2D view point {uv: [u,v]} or a 2D view box {uv0: [u,v], uv1: [u,v]}",
"{x, y, viewport?: [w,h]} — polygon fill / material picker / single dab; polygon fill also takes a box {x0, y0, x1, y1, viewport?}, a 2D view point {uv: [u,v]} or a 2D view box {uv0: [u,v], uv1: [u,v]}",
always,
click
),
@@ -115,6 +115,7 @@ pub fn specs() -> Vec<CommandSpec> {
pub(crate) fn set_tool(session: &mut Session, tool: Tool) -> Result<Value> {
session.cancel_stroke();
// A box fill under way keeps what it filled as one undo step
session.end_polygon_fill_box();
session.tool.tool = tool;
@@ -285,11 +286,14 @@ pub(crate) fn brush_json(session: &Session) -> Value {
}
fn material(s: &mut Session, p: &Value) -> Result<Value> {
let channel = channel_param("tool.material", p, "channel")?;
let color = color_param("tool.material", p, "color")?;
let value = color_param("tool.material", p, "value")?;
if let Some(v) = f32p(p, "maskValue") {
s.tool.mask_value = v.clamp(0.0, 1.0);
}
if let Some(c) = channel_param("tool.material", p, "channel")? {
if let Some(v) = color_param(p, "color").or_else(|| color_param(p, "value")) {
if let Some(c) = channel {
if let Some(v) = color.or(value) {
let v = if c == Channel::Height { v.map(|x| x.clamp(-1.0, 1.0)) } else { v.map(|x| x.clamp(0.0, 1.0)) };
s.tool.values.insert(c, v);
s.tool.enabled.insert(c);
@@ -339,50 +343,53 @@ fn swap_mask_value(session: &mut Session, _: &Value) -> Result<Value> {
Ok(json!({ "maskValue": session.tool.mask_value }))
}
fn stencil(s: &mut Session, p: &Value) -> Result<Value> {
if boolp(p, "clear") == Some(true) {
s.tool.stencil = None;
fn stencil(session: &mut Session, params: &Value) -> Result<Value> {
if boolp(params, "clear") == Some(true) {
session.tool.stencil = None;
return Ok(Value::Null);
}
if let Some(name) = strp(p, "image") {
if !s.doc.resources.contains_key(name) {
if let Some(name) = strp(params, "image") {
if !session.doc.resources.contains_key(name) {
return Err(bad("tool.stencil", format!("no resource `{name}` (import it first)")));
}
let keep = s.tool.stencil.take();
let mut st = crate::session::StencilState::new(name);
if let Some(k) = keep {
st = crate::session::StencilState { image: name.to_string(), ..k };
let previous_stencil = session.tool.stencil.take();
let mut stencil_state = crate::session::StencilState::new(name);
if let Some(previous_stencil) = previous_stencil {
stencil_state = crate::session::StencilState { image: name.to_string(), ..previous_stencil };
}
s.tool.stencil = Some(st);
session.tool.stencil = Some(stencil_state);
}
let st = s.tool.stencil.as_mut().ok_or_else(|| bad("tool.stencil", "no stencil: give `image`"))?;
if let Some(c) = color_param(p, "center").map(|c| [c[0], c[1]]).or_else(|| pair(p, "center")) {
st.center = c.map(|v| v.clamp(-4.0, 4.0));
let stencil_state = session.tool.stencil.as_mut().ok_or_else(|| bad("tool.stencil", "no stencil: give `image`"))?;
if let Some(center) = pair(params, "center").or_else(|| params.get("center").and_then(color_of).map(|color| [color[0], color[1]])) {
stencil_state.center = center.map(|value| value.clamp(-4.0, 4.0));
}
if let Some(v) = f32p(p, "scale") {
st.scale = v.clamp(0.01, 10.0);
if let Some(scale) = f32p(params, "scale") {
stencil_state.scale = scale.clamp(0.01, 10.0);
}
if let Some(v) = f32p(p, "rotation") {
st.rotation = v.rem_euclid(360.0);
if let Some(rotation) = f32p(params, "rotation") {
stencil_state.rotation = rotation.rem_euclid(360.0);
}
if let Some(v) = boolp(p, "tile") {
st.tile = v;
if let Some(tile) = boolp(params, "tile") {
stencil_state.tile = tile;
}
if let Some(v) = f32p(p, "opacity") {
st.opacity = v.clamp(0.0, 1.0);
if let Some(opacity) = f32p(params, "opacity") {
stencil_state.opacity = opacity.clamp(0.0, 1.0);
}
s.revision = s.revision.wrapping_add(1);
Ok(serde_json::to_value(&s.tool.stencil).unwrap_or(Value::Null))
session.revision = session.revision.wrapping_add(1);
Ok(serde_json::to_value(&session.tool.stencil).unwrap_or(Value::Null))
}
/// A `[a, b]` param of two finite numbers.
fn pair(p: &Value, key: &str) -> Option<[f32; 2]> {
let a = p.get(key)?.as_array()?;
let v: Vec<f32> = a.iter().filter_map(Value::as_f64).map(|x| x as f32).filter(|x| x.is_finite()).collect();
match v.as_slice() {
[x, y] => Some([*x, *y]),
_ => None,
}
let [x, y] = a.as_slice() else { return None };
let x = x.as_f64()? as f32;
let y = y.as_f64()? as f32;
(x.is_finite() && y.is_finite()).then_some([x, y])
}
fn clone_source(s: &mut Session, p: &Value) -> Result<Value> {
+2 -1
View File
@@ -211,6 +211,7 @@ fn display(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(e) = env.as_deref().filter(|e| !e.starts_with(texturecraft_core::project::ENV_RESOURCE_PREFIX) && !ENVIRONMENTS.contains(e)) {
return Err(bad("display.set", format!("unknown environment `{e}`: one of {}, or resource:<image>", ENVIRONMENTS.join(", "))));
}
let scatter_color = color_param("display.set", p, "scatterColor")?;
let d = &mut s.doc.project.display;
if let Some(e) = env {
d.environment = e;
@@ -236,7 +237,7 @@ fn display(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(t) = tm {
d.tone_mapping = t;
}
if let Some(c) = color_param(p, "scatterColor") {
if let Some(c) = scatter_color {
d.scatter_color = c.map(|v| v.clamp(0.0, 1.0));
}
if let Some(b) = boolp(p, "shadows") {
+72 -9
View File
@@ -464,7 +464,8 @@ fn leaning_session(lean: bool) -> Session {
}
let high_text = obj(&mesh, 0.1, f32::MAX, [1.0; 3]);
let mut s = Session::new_project("t", mesh, "builtin:plane", None, 256).unwrap();
// A folder of its own per call: tests run in parallel, and another one removes the shared folder.
// Tests run in parallel, so each call needs its own folder
static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
let call = CALLS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!("texturecraft-lean-test-{}-{call}", std::process::id()));
@@ -472,7 +473,8 @@ fn leaning_session(lean: bool) -> Session {
let high = dir.join("high.obj");
std::fs::write(&high, high_text).unwrap();
execute(&mut s, "bake.highPoly.add", &json!({ "path": high.display().to_string() })).unwrap();
// The mesh is parsed and kept on add, so the file can go.
// The imported mesh remains after its source file is removed
let _ = std::fs::remove_dir_all(&dir);
execute(
&mut s,
@@ -489,10 +491,12 @@ fn the_skew_map_is_saved_with_the_project() {
execute(&mut s, "bake.skew.set", &json!({ "value": 0.25 })).unwrap();
let back = Session::from_project_bytes(&s.to_project_bytes().unwrap()).unwrap();
assert_eq!(back.doc.bake_paint, s.doc.bake_paint);
// No skew: none comes back.
// Clearing skew persists
execute(&mut s, "bake.skew.clear", &Value::Null).unwrap();
assert!(Session::from_project_bytes(&s.to_project_bytes().unwrap()).unwrap().doc.bake_paint.is_empty());
// A map with a non-finite value is skipped with a log line, and the project still opens.
// Invalid saved skew is skipped without preventing project load
let mut pf = s.project_file();
pf.bake_paint.insert((0, texturecraft_core::BakePaintTarget::Skew), texturecraft_formats::BakePaintData { width: 1, height: 1, values: vec![f32::NAN] });
let opened = Session::from_project_file(pf).unwrap();
@@ -504,27 +508,46 @@ fn the_skew_map_is_saved_with_the_project() {
);
}
#[test]
fn the_skew_map_steers_only_a_distance_cage_with_averaged_normals() {
let mut s = leaning_session(false);
execute(&mut s, "bake.skew.set", &json!({ "value": 0.25 })).unwrap();
assert!(s.bake_task(0).unwrap().skew.is_some());
s.doc.project.texture_sets[0].bake.cage.average_normals = false;
assert!(s.bake_task(0).unwrap().skew.is_none(), "without averaged normals the skew map is left out");
s.doc.project.texture_sets[0].bake.cage.average_normals = true;
let cage = obj(&s.mesh.clone(), 0.05, f32::MAX, [1.0; 3]);
s.set_cage_bytes("cage.obj", cage.into_bytes()).unwrap();
s.doc.project.texture_sets[0].bake.cage.kind = texturecraft_core::CageType::CustomFile;
assert!(s.bake_task(0).unwrap().skew.is_none(), "a cage file sets the rays itself");
}
#[test]
fn painting_skew_widens_a_map_and_bad_strokes_change_nothing() {
let mut s = leaning_session(true);
// Painting with no map makes a black one at `size` and puts the dab in it.
// The first stroke creates a black map at the requested size
let r = execute(&mut s, "bake.skew.paint", &json!({ "points": [[0.5, 0.5]], "radius": 0.1, "size": 64 })).unwrap();
assert_eq!(r["skew"]["width"], 64);
let map = s.doc.bake_paint.get(&(0, texturecraft_core::BakePaintTarget::Skew)).cloned().unwrap();
assert!(map.values.iter().any(|v| *v > 0.99) && map.values.contains(&0.0));
let rev = s.revision;
// Erasing with value 0 brings it back down; the size stays.
// Erasing retains the map size
execute(&mut s, "bake.skew.paint", &json!({ "points": [[0.5, 0.5]], "radius": 0.2, "value": 0.0, "hardness": 1.0 })).unwrap();
let after = s.doc.bake_paint.get(&(0, texturecraft_core::BakePaintTarget::Skew)).cloned().unwrap();
assert_eq!(after.width, 64);
assert!(after.values.iter().all(|v| *v < 0.01) && s.revision > rev);
// A constant map is widened, keeping its value where nothing is painted.
// Resizing preserves untouched constant values
execute(&mut s, "bake.skew.set", &json!({ "value": 0.5 })).unwrap();
execute(&mut s, "bake.skew.paint", &json!({ "points": [[0.1, 0.1]], "radius": 0.05, "size": 32 })).unwrap();
let wide = s.doc.bake_paint.get(&(0, texturecraft_core::BakePaintTarget::Skew)).cloned().unwrap();
assert_eq!(wide.width, 32);
assert!((wide.values[wide.values.len() / 2] - 0.5).abs() < 1e-5, "{}", wide.values[wide.values.len() / 2]);
// Hostile input is an error and leaves the map alone.
// Invalid strokes leave the map unchanged
let before = s.doc.bake_paint.get(&(0, texturecraft_core::BakePaintTarget::Skew)).cloned();
for bad in [
json!({}),
@@ -541,7 +564,8 @@ fn painting_skew_widens_a_map_and_bad_strokes_change_nothing() {
assert!(execute(&mut s, "bake.skew.paint", &bad).is_err(), "{bad}");
}
assert_eq!(s.doc.bake_paint.get(&(0, texturecraft_core::BakePaintTarget::Skew)).cloned(), before);
// And the painted map is saved with the project.
// Painted skew survives a project round trip
let back = Session::from_project_bytes(&s.to_project_bytes().unwrap()).unwrap();
assert_eq!(back.doc.bake_paint, s.doc.bake_paint);
}
@@ -612,3 +636,42 @@ fn a_failed_multi_set_bake_restores_settings_pixels_and_history() {
assert_eq!(session.doc.baked, original.baked);
assert_eq!(session.history_labels(), history);
}
/// `file.changeMesh` swaps the mesh under a baked project: the high poly and the skew map stay,
/// the project counts as changed, and image resources are decoded in the new session.
#[test]
fn changing_the_mesh_keeps_the_bake_setup_and_marks_the_project_changed() {
let mut s = plane_session();
let plane = s.mesh.as_ref().clone();
let high = temp_file("change_high.obj", &obj(&plane, 0.002, f32::MAX, [1.0; 3]));
execute(&mut s, "bake.highPoly.add", &json!({ "path": high })).unwrap();
execute(&mut s, "bake.skew.set", &json!({ "value": 1.0 })).unwrap();
// 1x1 PNG as an image resource.
let png = [
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08,
0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xf8, 0xcf, 0xc0, 0xf0, 0x1f, 0x00,
0x05, 0x00, 0x01, 0xff, 0x89, 0x99, 0x3d, 0x1d, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
];
s.import_resource_bytes("dot.png", png.to_vec()).unwrap();
s.modified = false;
let mesh_id = s.mesh_id;
let mut low = String::new();
for p in &plane.positions {
low += &format!("v {} {} {}\n", p.x, p.y, p.z);
}
for uv in &plane.uvs {
low += &format!("vt {} {}\n", uv.x, 1.0 - uv.y);
}
for t in plane.indices.as_chunks::<3>().0 {
low += &format!("f {a}/{a} {b}/{b} {c}/{c}\n", a = t[0] + 1, b = t[1] + 1, c = t[2] + 1);
}
let low = temp_file("change_low.obj", &low);
execute(&mut s, "file.changeMesh", &json!({ "path": low })).unwrap();
assert_ne!(s.mesh_id, mesh_id, "the session is on a new mesh");
assert_eq!(s.bake_meshes.high.len(), 1, "the high poly stays");
assert!(s.doc.bake_paint.keys().any(|(_, target)| *target == texturecraft_engine::core::BakePaintTarget::Skew), "the skew map stays");
assert!(s.modified, "the project differs from the saved one");
assert_eq!(s.images.len(), 1, "image resources are decoded in the new session");
}
+112
View File
@@ -0,0 +1,112 @@
//! What bake mode lists before a bake: overlapping UVs and parts "By Mesh Name" leaves unpaired
use texturecraft_engine::Session;
use texturecraft_engine::bake::NamePair;
use texturecraft_engine::core::MeshMatchMode;
/// Two quads side by side; `shared_uvs` lays both on the same UV square instead of side by side in UV space
fn two_quads(shared_uvs: bool) -> Session {
let second_u = if shared_uvs { 0.0 } else { 0.5 };
let obj = format!(
"o Box_low\nv 0 0 0\nv 1 0 0\nv 1 1 0\nv 0 1 0\nvt 0 0\nvt 0.5 0\nvt 0.5 1\nvt 0 1\nf 1/1 2/2 3/3 4/4\n\
o Lone_low\nv 2 0 0\nv 3 0 0\nv 3 1 0\nv 2 1 0\nvt {second_u} 0\nvt {} 0\nvt {} 1\nvt {second_u} 1\nf 5/5 6/6 7/7 8/8\n",
second_u + 0.5,
second_u + 0.5
);
Session::from_mesh_bytes("quads.obj", obj.as_bytes(), 128, "quads").unwrap()
}
const HIGH_POLY: &[u8] = b"o Box_high\nv 0 0 0.01\nv 1 0 0.01\nv 0 1 0.01\nf 1 2 3\no Extra_high\nv 2 0 0.01\nv 3 0 0.01\nv 2 1 0.01\nf 4 5 6\n";
#[test]
fn overlapping_uvs_are_measured_before_the_bake() {
assert!(two_quads(true).bake_uv_overlap(0).unwrap() > 0.4, "the second quad covers the first one's UVs");
assert_eq!(two_quads(false).bake_uv_overlap(0).unwrap(), 0.0, "side by side in UV space nothing overlaps");
const MISSING_TEXTURE_SET: usize = 9;
assert!(two_quads(false).bake_uv_overlap(MISSING_TEXTURE_SET).is_err());
}
#[test]
fn overlap_in_a_uv_tile_left_out_of_the_bake_is_not_counted() {
// One quad in tile 1001, two on the same UVs in tile 1002
let obj = "o First\nv 0 0 0\nv 1 0 0\nv 1 1 0\nv 0 1 0\nvt 0 0\nvt 0.5 0\nvt 0.5 1\nvt 0 1\nf 1/1 2/2 3/3 4/4\n\
o Second\nv 2 0 0\nv 3 0 0\nv 3 1 0\nv 2 1 0\nvt 1 0\nvt 1.5 0\nvt 1.5 1\nvt 1 1\nf 5/5 6/6 7/7 8/8\n\
o Third\nv 4 0 0\nv 5 0 0\nv 5 1 0\nv 4 1 0\nvt 1 0\nvt 1.5 0\nvt 1.5 1\nvt 1 1\nf 9/9 10/10 11/11 12/12\n";
let mut session = Session::from_mesh_bytes("tiles.obj", obj.as_bytes(), 128, "tiles").unwrap();
assert_eq!(session.doc.project.texture_sets[0].uv_tiles, [1001, 1002]);
assert!(session.bake_uv_overlap(0).unwrap() > 0.4, "the last two quads share tile 1002's UVs");
session.doc.project.texture_sets[0].bake_excluded_tiles.insert(1002);
assert_eq!(session.bake_uv_overlap(0).unwrap(), 0.0, "tile 1001 alone has no overlap");
}
#[test]
fn overlap_is_measured_at_the_same_detail_in_every_uv_tile() {
// Tiles 1001 and 1010 make a grid ten tiles wide; 512 texels across all ten would give each tile about 51
const TEXELS_PER_TILE: f32 = 512.0;
const SLIVER_TEXELS: f32 = 4.0;
// Inside tile 1010 (column 9), clear of the wide raster's texel centres
const SLIVER_LEFT_U: f32 = 9.1;
let (left, right) = (SLIVER_LEFT_U, SLIVER_LEFT_U + SLIVER_TEXELS / TEXELS_PER_TILE);
let sliver = |name: &str, first: u32, x: f32| {
let (second, third, fourth) = (first + 1, first + 2, first + 3);
let corners = format!("v {x} 0 0\nv {} 0 0\nv {} 1 0\nv {x} 1 0\n", x + 1.0, x + 1.0);
format!(
"o {name}\n{corners}vt {left} 0\nvt {right} 0\nvt {right} 1\nvt {left} 1\nf {first}/{first} {second}/{second} {third}/{third} {fourth}/{fourth}\n"
)
};
let corner = "o Corner\nv 0 0 0\nv 1 0 0\nv 1 1 0\nv 0 1 0\nvt 0 0\nvt 0.5 0\nvt 0.5 1\nvt 0 1\nf 1/1 2/2 3/3 4/4\n";
let obj = [corner.to_owned(), sliver("First", 5, 2.0), sliver("Second", 9, 4.0)].concat();
let session = Session::from_mesh_bytes("wide.obj", obj.as_bytes(), 128, "wide").unwrap();
assert_eq!(session.doc.project.texture_sets[0].uv_tiles, [1001, 1010]);
assert!(session.bake_uv_overlap(0).unwrap() > 0.0, "two slivers a few texels wide overlap in tile 1010");
}
#[test]
fn a_triangle_across_a_uv_tile_border_counts_in_both_tiles() {
// Two quads on the same UVs, from u 0.9 in tile 1001 to u 1.1 in tile 1002
let obj = "o First\nv 0 0 0\nv 1 0 0\nv 1 1 0\nv 0 1 0\nvt 0.9 0\nvt 1.1 0\nvt 1.1 1\nvt 0.9 1\nf 1/1 2/2 3/3 4/4\n\
o Second\nv 2 0 0\nv 3 0 0\nv 3 1 0\nv 2 1 0\nvt 0.9 0\nvt 1.1 0\nvt 1.1 1\nvt 0.9 1\nf 5/5 6/6 7/7 8/8\n";
let mut session = Session::from_mesh_bytes("border.obj", obj.as_bytes(), 128, "border").unwrap();
assert_eq!(session.doc.project.texture_sets[0].uv_tiles, [1001, 1002]);
for (excluded, kept) in [(1002, 1001), (1001, 1002)] {
let excluded_tiles = &mut session.doc.project.texture_sets[0].bake_excluded_tiles;
excluded_tiles.clear();
excluded_tiles.insert(excluded);
assert!(session.bake_uv_overlap(0).unwrap() > 0.4, "tile {kept} holds part of the overlap");
}
}
#[test]
fn unpaired_parts_are_listed_only_when_matching_by_name() {
let mut session = two_quads(false);
assert_eq!(session.bake_name_pairs(0).unwrap(), None, "no high poly");
session.add_high_poly_bytes("high.obj", HIGH_POLY.to_vec()).unwrap();
assert_eq!(session.bake_name_pairs(0).unwrap(), None, "\"Always\" pairs every part");
session.doc.project.texture_sets[0].bake.high_poly.match_mode = MeshMatchMode::ByMeshName;
let pairs = session.bake_name_pairs(0).unwrap().unwrap();
let unpaired: Vec<&NamePair> = pairs.iter().filter(|pair| pair.unpaired()).collect();
assert_eq!(unpaired, [&NamePair { low: Some("Lone_low".into()), high: None }, &NamePair { low: None, high: Some("Extra_high".into()) }], "{pairs:?}");
assert!(pairs.contains(&NamePair { low: Some("Box_low".into()), high: Some("Box_high".into()) }));
session.doc.project.texture_sets[0].bake.high_poly.use_low_as_high = true;
assert_eq!(session.bake_name_pairs(0).unwrap(), None, "the low poly as its own high poly pairs every part");
}
#[test]
fn a_set_matching_always_bakes_from_every_high_poly_part() {
let obj = "o Box_low\nusemtl Body\nv 0 0 0\nv 1 0 0\nv 0 1 0\nvt 0 0\nvt 1 0\nvt 0 1\nf 1/1 2/2 3/3\n\
o Lid_low\nusemtl Lid\nv 2 0 0\nv 3 0 0\nv 2 1 0\nvt 0 0\nvt 1 0\nvt 0 1\nf 4/4 5/5 6/6\n";
let mut session = Session::from_mesh_bytes("sets.obj", obj.as_bytes(), 128, "sets").unwrap();
session.add_high_poly_bytes("high.obj", HIGH_POLY.to_vec()).unwrap();
let both = [0, 1];
session.doc.project.texture_sets[0].bake.high_poly.match_mode = MeshMatchMode::ByMeshName;
assert_eq!(session.bake_unpaired_high_parts(&both).unwrap(), Vec::<String>::new(), "the other set matches \"Always\"");
session.doc.project.texture_sets[1].bake.high_poly.match_mode = MeshMatchMode::ByMeshName;
assert_eq!(session.bake_unpaired_high_parts(&both).unwrap(), ["Extra_high"]);
}
+132
View File
@@ -0,0 +1,132 @@
//! Generator inputs that read an anchor point or an image, through engine commands.
use serde_json::{Value, json};
use std::sync::Arc;
use texturecraft_engine::core::{BakedMap, Channel, EffectKind, Source};
use texturecraft_engine::geom::primitives;
use texturecraft_engine::{Session, execute};
/// The texture set's texels per side.
const RESOLUTION: u32 = 256;
fn session() -> Session {
let mut session = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:sphere", None, RESOLUTION).unwrap();
session.viewport = texturecraft_engine::geom::glam::Vec2::new(400.0, 400.0);
session.camera.yaw = 0.0;
session.camera.pitch = 0.0;
session.update();
session
}
/// Composited value of `channel` under the viewport center.
fn center(session: &mut Session, channel: Channel) -> [f32; 3] {
session.update();
let hit = session.pick(session.viewport * 0.5, session.viewport).unwrap();
let texels = RESOLUTION as f32;
session.caches[0].composite.value(channel, (hit.uv.x * texels) as u32, (hit.uv.y * texels) as u32)
}
fn id(value: Value, key: &str) -> u64 {
value[key].as_u64().unwrap()
}
/// A low fill (height 0.8) with an anchor, then a white fill whose black mask holds an AO generator reading it: (anchor, low layer, generator).
fn anchored_generator(session: &mut Session) -> (u64, u64, u64) {
let low = id(execute(session, "layer.add.fill", &json!({"height": 0.8})).unwrap(), "layer");
let anchor = id(execute(session, "effect.add.anchor", &json!({"name": "Height"})).unwrap(), "effect");
execute(session, "layer.add.fill", &json!({"color": [1.0, 1.0, 1.0]})).unwrap();
execute(session, "mask.add.black", &Value::Null).unwrap();
let generator = id(execute(session, "effect.add.generator", &json!({"generator": "ambientOcclusion", "level": 0.5, "softness": 0.5})).unwrap(), "effect");
execute(session, "effect.set", &json!({"effect": generator, "input": "ambientOcclusion", "inputAnchor": anchor, "anchorChannel": "height"})).unwrap();
(anchor, low, generator)
}
#[test]
fn a_generator_input_reads_the_anchor_points_value() {
let mut session = session();
anchored_generator(&mut session);
// The low fill's gray, then white through the mask: an occlusion of 0.8 masks most of it, the channel default (0) none
let base = center(&mut session, Channel::BaseColor)[0];
assert!(base > 0.9, "the generator reads the anchor's height, not the default: {base}");
}
#[test]
fn a_generator_reading_an_anchor_cannot_be_pasted_below_it() {
let mut session = session();
let (_, low, generator) = anchored_generator(&mut session);
execute(&mut session, "effect.copy", &json!({"effect": generator})).unwrap();
let pasted = execute(&mut session, "effect.paste", &json!({"layer": low, "target": "mask"}));
assert!(pasted.is_err(), "the anchor point is on that layer, not below it");
}
#[test]
fn removing_unused_resources_keeps_an_image_read_by_a_generator_input() {
let mut session = session();
let image = image::RgbaImage::from_pixel(4, 4, image::Rgba([128, 128, 128, 255]));
let mut bytes = Vec::new();
image.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
let name = session.import_resource_bytes("occlusion.png", bytes).unwrap();
execute(&mut session, "layer.add.fill", &Value::Null).unwrap();
execute(&mut session, "mask.add.black", &Value::Null).unwrap();
let generator = id(execute(&mut session, "effect.add.generator", &json!({"generator": "dirt"})).unwrap(), "effect");
execute(&mut session, "effect.set", &json!({"effect": generator, "input": "ambientOcclusion", "inputImage": name})).unwrap();
execute(&mut session, "file.removeUnused", &Value::Null).unwrap();
assert!(session.doc.resources.contains_key(&name), "the image is still read by the generator input");
}
#[test]
fn removing_unused_resources_keeps_an_image_read_by_a_content_effect() {
let mut session = session();
let image = image::RgbaImage::from_pixel(4, 4, image::Rgba([128, 128, 128, 255]));
let mut bytes = Vec::new();
image.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
let name = session.import_resource_bytes("content.png", bytes).unwrap();
execute(&mut session, "layer.add.fill", &Value::Null).unwrap();
let effect = id(execute(&mut session, "effect.add.fill", &json!({"target": "content"})).unwrap(), "effect");
execute(&mut session, "layer.fill.set", &json!({"effect": effect, "channel": "baseColor", "image": name})).unwrap();
execute(&mut session, "file.removeUnused", &Value::Null).unwrap();
assert!(session.doc.resources.contains_key(&name), "the content effect still reads the image");
}
#[test]
fn generator_input_rejects_a_resource_that_is_not_an_image() {
let mut session = session();
session.doc.resources.insert("linked.mcmat".into(), Arc::new(vec![1, 2, 3]));
execute(&mut session, "layer.add.fill", &Value::Null).unwrap();
execute(&mut session, "mask.add.black", &Value::Null).unwrap();
let generator = id(execute(&mut session, "effect.add.generator", &json!({"generator": "dirt"})).unwrap(), "effect");
let result = execute(&mut session, "effect.set", &json!({"effect": generator, "input": "ambientOcclusion", "inputImage": "linked.mcmat"}));
assert!(result.is_err(), "a package resource has no image pixels for the generator to sample");
}
#[test]
fn importing_a_missing_generator_input_invalidates_the_composite() {
let mut session = session();
execute(&mut session, "layer.add.fill", &json!({"color": [1.0, 1.0, 1.0]})).unwrap();
execute(&mut session, "mask.add.black", &Value::Null).unwrap();
let effect = id(execute(&mut session, "effect.add.generator", &json!({"generator": "ambientOcclusion"})).unwrap(), "effect");
session
.edit("Reference an absent image", |session| {
let effect = session.doc.project.effect_mut(texturecraft_engine::core::LayerId(effect)).unwrap();
let EffectKind::Generator(generator) = &mut effect.kind else { panic!("expected generator") };
generator.inputs.insert(BakedMap::AmbientOcclusion, Source::image("later.png"));
Ok(())
})
.unwrap();
let before = center(&mut session, Channel::BaseColor)[0];
let image = image::RgbaImage::from_pixel(4, 4, image::Rgba([0, 0, 0, 255]));
let mut bytes = Vec::new();
image.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
session.import_resource_bytes("later.png", bytes).unwrap();
let after = center(&mut session, Channel::BaseColor)[0];
assert!((after - before).abs() > 0.2, "the new image must update the generator's composited mask: {before} -> {after}");
}
+6 -5
View File
@@ -1,4 +1,4 @@
use texturecraft_engine::bake::{HighPolyReach, RayStart, VertexReach, high_poly_missed};
use texturecraft_engine::bake::{HIDDEN_HIGH_POLY_VERTEX, HighPolyReach, RayStart, VertexReach, high_poly_missed};
use texturecraft_engine::geom::glam::Vec3;
fn vertex(height: f32) -> Option<VertexReach> {
@@ -30,12 +30,13 @@ fn each_owning_set_uses_its_own_frontal_and_rear_distances() {
}
#[test]
fn selected_set_filter_leaves_other_sets_unmarked() {
fn selected_set_filter_hides_other_sets_vertices() {
const H: f32 = HIDDEN_HIGH_POLY_VERTEX;
let reaches = sets();
assert_eq!(high_poly_missed(&reaches, &DISTANCES, Some(1), 6), vec![1.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
assert_eq!(high_poly_missed(&reaches, &DISTANCES, Some(2), 6), vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0]);
assert_eq!(high_poly_missed(&reaches, &DISTANCES, Some(1), 6), vec![1.0, 0.0, H, H, H, H]);
assert_eq!(high_poly_missed(&reaches, &DISTANCES, Some(2), 6), vec![H, H, 0.0, 1.0, H, H]);
for only_set in [0, reaches.len(), usize::MAX] {
assert_eq!(high_poly_missed(&reaches, &DISTANCES, Some(only_set), 6), vec![0.0; 6]);
assert_eq!(high_poly_missed(&reaches, &DISTANCES, Some(only_set), 6), vec![H; 6], "a set without a reach shows none of the high poly");
}
}
+47 -1
View File
@@ -2,7 +2,7 @@
//! frame-budgeted compositor, through engine commands.
use serde_json::{Value, json};
use texturecraft_engine::core::{Channel, LayerId, LayerKind};
use texturecraft_engine::core::{Channel, Fill, LayerId, LayerKind, Source};
use texturecraft_engine::geom::glam::Vec2;
use texturecraft_engine::geom::primitives;
use texturecraft_engine::{Session, execute};
@@ -206,6 +206,52 @@ fn edits_invalidate_only_the_sets_they_change() {
assert!((rough(&mut s, 0) - 0.4).abs() < 1e-3);
}
#[test]
fn importing_images_invalidates_only_sets_with_newly_resolved_references() {
use std::sync::Arc;
let mut session = session();
let referenced_layer = session.doc.project.texture_sets[1].stack[0];
session
.edit("Reference missing image", |session| {
let mut fill = Fill::default();
fill.sources.insert(Channel::BaseColor, Source::image("later.png"));
session.doc.project.layer_mut(referenced_layer).unwrap().kind = LayerKind::Fill(fill);
Ok(())
})
.unwrap();
session.update();
let mut image_bytes = Vec::new();
let image = image::RgbaImage::from_pixel(1, 1, image::Rgba([255, 0, 0, 255]));
image.write_to(&mut std::io::Cursor::new(&mut image_bytes), image::ImageFormat::Png).unwrap();
session.import_resource_bytes("later.png", image_bytes.clone()).unwrap();
assert!(session.caches[0].dirty.is_empty(), "unrelated set stays clean");
assert!(!session.caches[1].dirty.is_empty(), "newly resolved image invalidates its set");
session.update();
let (x, y) = covered(&session, 1);
assert!(session.caches[1].composite.value(Channel::BaseColor, x, y)[0] > 0.9);
let imported_image = Arc::downgrade(&session.images["later.png"]);
session.undo().unwrap();
assert!(!session.caches[0].dirty.is_empty(), "resource removal retains broad invalidation");
assert!(!session.caches[1].dirty.is_empty());
assert!(imported_image.upgrade().is_none(), "undo releases the decoded image even while encoded bytes remain in history");
session.update();
session.redo().unwrap();
assert!(session.caches[0].dirty.is_empty());
assert!(!session.caches[1].dirty.is_empty());
assert!(!imported_image.ptr_eq(&Arc::downgrade(&session.images["later.png"])), "redo decodes a new image allocation");
session.update();
session.import_resource_bytes("unused.png", image_bytes).unwrap();
assert!(session.caches.iter().all(|cache| cache.dirty.is_empty()), "unused image leaves every set clean");
let resolved_image = Arc::downgrade(&session.images["later.png"]);
session.sync_images();
assert!(resolved_image.ptr_eq(&Arc::downgrade(&session.images["later.png"])), "sync reuses already decoded images");
}
#[test]
fn budgeted_updates_spread_work_and_end_identical() {
let mut s = session();
+3
View File
@@ -2,6 +2,7 @@
//! Keep feature tests in their own modules; the standalone session.rs target remains available.
mod bake;
mod bake_checks;
mod bake_resolution;
mod bake_settings;
mod bake_tiles;
@@ -10,6 +11,7 @@ mod content_effects;
mod export_colour_space;
mod filter_swap;
mod filters;
mod generator_inputs;
mod high_poly_missed;
mod instances;
mod materialcraft_link;
@@ -24,6 +26,7 @@ mod polygon_fill;
mod project_recovery;
mod sample;
mod thumbs;
mod tool_availability;
mod tools;
mod udim;
mod uv_strokes;
+83 -37
View File
@@ -21,9 +21,14 @@ fn session() -> Session {
/// A `.mcmat` as MaterialCraft writes it: uniform maps, one float input `wear` cooked at `wear`.
fn mcmat(roughness: f32, wear: f32) -> Vec<u8> {
mcmat_version(roughness, wear, 1)
}
fn mcmat_version(roughness: f32, wear: f32, version: u32) -> Vec<u8> {
let source = if version == 2 { "source.sbs" } else { "source.mcraft" };
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],
"format": "craft-material", "version": version, "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"},
@@ -42,7 +47,7 @@ fn mcmat(roughness: f32, wear: f32) -> Vec<u8> {
{"identifier": "bentNormal", "kind": "color"}
],
"presets": [{"name": "Worn", "values": {"wear": {"t": "float", "v": 1.0}, "note": {"t": "text", "v": "x"}}}],
"source": "source.mcraft"
"source": source
});
let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
let o = zip::write::SimpleFileOptions::default();
@@ -51,7 +56,7 @@ fn mcmat(roughness: f32, wear: f32) -> Vec<u8> {
("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()),
(source, b"package bytes".to_vec()),
] {
z.start_file(name, o).unwrap();
z.write_all(&bytes).unwrap();
@@ -81,6 +86,24 @@ fn map_name(s: &Session, id: LayerId, c: Channel) -> String {
}
}
#[test]
fn version_two_preserves_the_sbs_source_for_future_cooks() {
let mut session = session();
let id = session.add_linked_material_bytes_at(&mcmat_version(0.25, 0.5, 2), Place::Selection).unwrap();
let linked = link(&session, id);
assert!(linked.package.ends_with(".sbs"));
let job = session.link_job(id, &serde_json::Map::new()).unwrap();
assert_eq!(job.source_ext, "sbs");
assert_eq!(job.graph.as_deref(), Some("Brick_wall"));
assert_eq!(job.package.as_slice(), b"package bytes");
let base = std::env::temp_dir().join(format!("texturecraft-sbs-source-{}", std::process::id()));
let saved = execute(&mut session, "material.link.saveSource", &json!({"layer": id.0, "path": base.to_str().unwrap()})).unwrap();
let path = saved["path"].as_str().unwrap();
assert!(path.ends_with(".sbs"));
assert_eq!(std::fs::read(path).unwrap(), b"package bytes");
let _ = std::fs::remove_file(path);
}
#[test]
fn imports_as_a_linked_fill_with_its_maps() {
let mut s = session();
@@ -108,47 +131,55 @@ fn imports_as_a_linked_fill_with_its_maps() {
/// An image marked DirectX keeps the mark when a re-cook replaces it.
#[test]
fn recooks_keep_the_normal_format_of_a_map() {
let mut s = session();
let id = s.add_linked_material_bytes_at(&mcmat(0.25, 0.5), Place::Selection).unwrap();
execute(&mut s, "layer.fill.set", &json!({"layer": id.0, "channel": "roughness", "normalFormat": "directX"})).unwrap();
s.apply_link_cook(id, &mcmat(0.75, 0.5), false).unwrap();
let LayerKind::Fill(fill) = &s.doc.project.layer(id).unwrap().kind else { panic!("a fill") };
let mut session = session();
let id = session.add_linked_material_bytes_at(&mcmat(0.25, 0.5), Place::Selection).unwrap();
execute(&mut session, "layer.fill.set", &json!({"layer": id.0, "channel": "roughness", "normalFormat": "directX"})).unwrap();
session.apply_link_cook(id, &mcmat(0.75, 0.5), false).unwrap();
let LayerKind::Fill(fill) = &session.doc.project.layer(id).unwrap().kind else { panic!("a fill") };
assert!(matches!(fill.sources.get(&Channel::Roughness), Some(Source::Image { normal_format: NormalFormat::DirectX, .. })));
}
#[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}));
let mut session = session();
let id = session.add_linked_material_bytes_at(&mcmat(0.25, 0.5), Place::Selection).unwrap();
let set = |session: &mut Session, params: Value| execute(session, "material.link.set", &params);
assert!(set(&mut session, json!({"id": "nope", "value": 1, "cook": false})).is_err());
assert!(set(&mut session, json!({"id": "wear", "value": "soft", "cook": false})).is_err());
assert!(set(&mut session, json!({"id": "wear", "value": f64::NAN, "cook": false})).is_err());
set(&mut session, json!({"id": "tint", "value": "#ff0000", "cook": false})).unwrap();
assert_eq!(link(&session, id).params[1].value, json!({"t": "color", "v": [1.0, 0.0, 0.0, 1.0]}));
set(&mut session, json!({"id": "wear", "value": 0.9, "cook": false})).unwrap();
assert_eq!(link(&session, id).params[0].value, json!({"t": "float", "v": 0.9}));
// Cooks follow the texture set resolution
let job = session.link_job(id, &serde_json::Map::new()).unwrap();
// 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);
// Merge the cooked maps into the parameter edit
let old = map_name(&session, id, Channel::Roughness);
session.apply_link_cook(id, &mcmat(0.75, 0.9), true).unwrap();
// 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}));
assert!((roughness(&mut session) - 0.75).abs() < 2e-3);
assert!(!session.doc.resources.contains_key(&old), "old maps are dropped");
execute(&mut session, "edit.undo", &Value::Null).unwrap();
assert!((roughness(&mut session) - 0.25).abs() < 2e-3, "undo restores the earlier maps");
assert_eq!(link(&session, id).params[0].value, json!({"t": "float", "v": 0.5}));
execute(&mut session, "edit.redo", &Value::Null).unwrap();
assert!((roughness(&mut session) - 0.75).abs() < 2e-3);
// Presets skip unsupported inputs and reject unknown names
assert!(execute(&mut session, "material.link.preset", &json!({"name": "Nope", "cook": false})).is_err());
execute(&mut session, "material.link.preset", &json!({"name": "Worn", "cook": false})).unwrap();
assert_eq!(link(&session, id).params[0].value, json!({"t": "float", "v": 1.0}));
}
#[test]
@@ -191,7 +222,7 @@ fn cooks_through_materialcraft_cli_when_available() {
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();
let ok = std::process::Command::new(&cli).args(["new", "pbr"]).arg(&pkg).args(["--size", "64"]).status().unwrap();
assert!(ok.success());
let exposed = dir.join("exposed.mcraft");
let ok = std::process::Command::new(&cli)
@@ -209,9 +240,24 @@ fn cooks_through_materialcraft_cli_when_available() {
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");
let wear = link(&s, id).params.iter().find(|p| p.id == "wear").unwrap().value["v"].as_f64().unwrap();
assert!((wear - 0.8).abs() < 1e-6);
let out = dir.join("back.sbs");
execute(&mut s, "material.link.saveSource", &json!({"layer": id.0, "path": out.to_str().unwrap()})).unwrap();
assert!(std::fs::metadata(&out).unwrap().len() > 0);
// The portable material can name a graph other than the first in the embedded source.
let multi = dir.join("multi.mcraft");
let ok = std::process::Command::new(&cli).arg("run").arg(&exposed).arg("graph.duplicate").arg("{}").arg("-o").arg(&multi).status().unwrap();
assert!(ok.success());
let portable = dir.join("second.mcmat");
let params = json!({"graph": 1, "size": 64, "path": portable.to_str().unwrap()});
let ok = std::process::Command::new(&cli).arg("run").arg(&multi).arg("file.exportMaterial").arg(params.to_string()).status().unwrap();
assert!(ok.success());
let second = s.add_linked_material_bytes_at(&std::fs::read(&portable).unwrap(), Place::Selection).unwrap();
let expected_graph = link(&s, second).graph;
let job = s.link_job(second, &serde_json::Map::new()).unwrap();
let result = texturecraft_engine::formats::mcmat::read_mcmat(&job.run().unwrap()).unwrap();
assert_eq!(result.manifest.graph, expected_graph);
let _ = std::fs::remove_dir_all(&dir);
}
+40
View File
@@ -397,3 +397,43 @@ fn bench_layer_thumbs_4k() {
}
eprintln!(" every target once: {:.1} ms ({} pictures)", total.as_secs_f64() * 1e3, targets.len());
}
/// A full recomposite in one call against the same one spread over frames, as the UI runs it, with and without a blurred mask
/// in the stack: neighbourhood filters are what a frame slice pays for apart from its own tiles.
#[test]
#[ignore]
fn bench_sliced_filters_4k() {
for blur in [false, true] {
let mut session = Session::new_project("sliced", primitives::sphere(1.0, 64, 32), "builtin:sphere", None, 4096).unwrap();
session.viewport = Vec2::new(800.0, 800.0);
let run = |session: &mut Session, id: &str, params: Value| execute(session, id, &params).unwrap();
run(&mut session, "layer.add.fill", json!({"color": [0.6, 0.5, 0.4], "roughness": 0.6}));
run(&mut session, "layer.add.fill", json!({"color": [0.1, 0.3, 0.6]}));
run(&mut session, "mask.add.black", Value::Null);
run(&mut session, "effect.add.generator", json!({"generator": "position", "level": 0.5}));
if blur {
run(&mut session, "effect.add.filter", json!({"filter": "blur", "radius": 8}));
}
session.update();
session.caches[0].invalidate_all();
let started = Instant::now();
session.update();
let direct = started.elapsed();
session.caches[0].invalidate_all();
let clock = Instant::now();
let elapsed_ms = || clock.elapsed().as_secs_f64() * 1000.0;
let (mut frames, mut longest_frame) = (0, Duration::ZERO);
while session.pending_tiles() > 0 {
let started = Instant::now();
session.update_for(30.0, elapsed_ms);
longest_frame = longest_frame.max(started.elapsed());
frames += 1;
}
let label = if blur { "blurred mask" } else { "no filter" };
println!("[sliced 4096², {label}] one call {direct:.2?}; in 30 ms slices {frames} frames, worst {longest_frame:.2?}, total {:.2?}", clock.elapsed());
}
}
+336
View File
@@ -0,0 +1,336 @@
//! Which tools can act on each selection, and that every usable tool acts there in both views
mod selection;
use selection::Selection;
use serde_json::{Value, json};
use texturecraft_engine::core::{Effect, EffectKind, LayerId};
use texturecraft_engine::geom::glam::{Vec2, Vec3};
use texturecraft_engine::{Session, StackTarget, Tool, execute};
/// Two triangles side by side, each a mesh part with its own corner of the UV square
const PARTS_OBJ: &str = "o Lid\nv 0 0 0\nv 2 0 0\nv 0 2 0\nvt 0 0\nvt 0.5 0\nvt 0 0.5\nf 1/1 2/2 3/3\n\
o Body\nv 2 2 0\nv 4 2 0\nv 2 4 0\nvt 0.5 0.5\nvt 1 0.5\nvt 0.5 1\nf 4/4 5/5 6/6\n";
const VIEWPORT_PIXELS: f32 = 400.0;
fn stencil_png() -> Vec<u8> {
let mut out = Vec::new();
let green = image::RgbaImage::from_fn(8, 8, |_, _| image::Rgba([0, 255, 0, 255]));
green.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
out
}
fn base_session() -> Session {
let mut session = Session::from_mesh_bytes("parts.obj", PARTS_OBJ.as_bytes(), 256, "parts").unwrap();
session.viewport = Vec2::splat(VIEWPORT_PIXELS);
(session.camera.yaw, session.camera.pitch) = (0.0, 0.0);
let (low, high) = session.mesh.bounds();
session.camera.frame(low, high);
session.update();
execute(&mut session, "tool.material", &json!({"channel": "baseColor", "color": [1.0, 0.0, 0.0]})).unwrap();
execute(&mut session, "tool.brush", &json!({"size": 6})).unwrap();
let stencil = session.import_resource_bytes("stencil.png", stencil_png()).unwrap();
execute(&mut session, "tool.stencil", &json!({"image": stencil})).unwrap();
session
}
fn screen(session: &Session, point: [f32; 3]) -> Vec2 {
let ndc = session.camera.project(Vec3::from(point), 1.0).unwrap();
Vec2::new((ndc.x + 1.0) * VIEWPORT_PIXELS / 2.0, (1.0 - ndc.y) * VIEWPORT_PIXELS / 2.0)
}
fn add_layer(session: &mut Session, command: &str) -> u64 {
execute(session, command, &json!({})).unwrap()["layer"].as_u64().unwrap()
}
fn select(session: &mut Session, layer: u64, target: Value) {
execute(session, "layer.select", &json!({"layer": layer, "target": target})).unwrap();
}
fn select_nothing(session: &mut Session) {
let set = session.ts();
session.selected.remove(&set);
session.target = StackTarget::Content;
}
/// Adds an effect with `command` to the layer's content or mask and selects it
fn select_new_effect(session: &mut Session, layer: u64, mask: bool, command: &str, effect_params: Value) {
if mask {
execute(session, "mask.add.black", &json!({"layer": layer})).unwrap();
}
let target = if mask { "mask" } else { "content" };
let mut params = effect_params;
params["layer"] = json!(layer);
params["target"] = json!(target);
execute(session, command, &params).unwrap();
let owner = session.doc.project.layer(LayerId(layer)).unwrap();
let effects = if mask { &owner.mask.as_ref().unwrap().effects } else { &owner.content_effects };
let effect = effects.last().unwrap().id.0;
select(session, layer, json!(effect));
}
fn select_new_mask(session: &mut Session, layer: u64) {
execute(session, "mask.add.black", &json!({"layer": layer})).unwrap();
select(session, layer, json!("mask"));
}
fn session_with(selection: Selection) -> Session {
let mut session = base_session();
match selection {
Selection::PaintContent => {
let layer = add_layer(&mut session, "layer.add.paint");
select(&mut session, layer, json!("content"));
}
Selection::FillContent => {
let layer = add_layer(&mut session, "layer.add.fill");
select(&mut session, layer, json!("content"));
}
Selection::PaintMask => {
let layer = add_layer(&mut session, "layer.add.paint");
select_new_mask(&mut session, layer);
}
Selection::FillMask => {
let layer = add_layer(&mut session, "layer.add.fill");
select_new_mask(&mut session, layer);
}
Selection::FolderMask => {
let folder = add_layer(&mut session, "layer.add.group");
select_new_mask(&mut session, folder);
}
Selection::InstanceOwnMask => {
let source = add_layer(&mut session, "layer.add.paint");
let instance = execute(&mut session, "layer.instance", &json!({"layer": source})).unwrap()["layer"].as_u64().unwrap();
select_new_mask(&mut session, instance);
}
Selection::PaintSublayer => {
let layer = add_layer(&mut session, "layer.add.paint");
select_new_effect(&mut session, layer, false, "effect.add.paint", json!({}));
}
Selection::PaintSublayerInMask => {
let layer = add_layer(&mut session, "layer.add.paint");
select_new_effect(&mut session, layer, true, "effect.add.paint", json!({}));
}
Selection::PaintSublayerInFill => {
let layer = add_layer(&mut session, "layer.add.fill");
select_new_effect(&mut session, layer, false, "effect.add.paint", json!({}));
}
Selection::PathSublayer => {
let layer = add_layer(&mut session, "layer.add.paint");
select(&mut session, layer, json!("content"));
let (start, end) = (screen(&session, [0.2, 0.2, 0.0]), screen(&session, [1.0, 0.4, 0.0]));
execute(&mut session, "tool.path", &json!({"points": [[start.x, start.y], [end.x, end.y]]})).unwrap();
execute(&mut session, "tool.path", &json!({"commit": true})).unwrap();
let path = session.doc.project.layer(LayerId(layer)).unwrap().content_effects.last().unwrap().id.0;
select(&mut session, layer, json!(path));
}
Selection::FilterInPaint | Selection::FilterInFill | Selection::FilterInMask => {
let command = if selection == Selection::FilterInFill { "layer.add.fill" } else { "layer.add.paint" };
let layer = add_layer(&mut session, command);
select_new_effect(&mut session, layer, selection == Selection::FilterInMask, "effect.add.filter", json!({"filter": "invert"}));
}
Selection::FillEffectInPaint => {
let layer = add_layer(&mut session, "layer.add.paint");
select_new_effect(&mut session, layer, false, "effect.add.fill", json!({}));
}
Selection::GeneratorInFillMask => {
let layer = add_layer(&mut session, "layer.add.fill");
select_new_effect(&mut session, layer, true, "effect.add.generator", json!({}));
}
Selection::FolderContent => {
let folder = add_layer(&mut session, "layer.add.group");
select(&mut session, folder, json!("content"));
}
Selection::InstanceOfPaint | Selection::InstanceOfFill => {
let command = if selection == Selection::InstanceOfFill { "layer.add.fill" } else { "layer.add.paint" };
let source = add_layer(&mut session, command);
let instance = execute(&mut session, "layer.instance", &json!({"layer": source})).unwrap()["layer"].as_u64().unwrap();
select(&mut session, instance, json!("content"));
}
Selection::Nothing => select_nothing(&mut session),
Selection::QuickMaskOverPaint | Selection::QuickMaskOverFill => {
let command = if selection == Selection::QuickMaskOverFill { "layer.add.fill" } else { "layer.add.paint" };
let layer = add_layer(&mut session, command);
select(&mut session, layer, json!("content"));
execute(&mut session, "viewport.quickMask", &json!({"enabled": true})).unwrap();
execute(&mut session, "viewport.quickMask.edit", &json!({"editing": true})).unwrap();
}
Selection::QuickMaskOverNothing => {
select_nothing(&mut session);
execute(&mut session, "viewport.quickMask", &json!({"enabled": true})).unwrap();
execute(&mut session, "viewport.quickMask.edit", &json!({"editing": true})).unwrap();
}
}
session
}
/// Uses `tool` once the way the viewport does: a stroke, a click or a committed path
fn use_tool(session: &mut Session, tool: Tool, in_2d_view: bool) -> texturecraft_engine::Result<()> {
session.tool.tool = tool;
if tool == Tool::Clone {
let source = screen(session, [1.2, 0.3, 0.0]);
execute(session, "tool.cloneSource", &json!({"x": source.x, "y": source.y}))?;
}
let points = [screen(session, [0.3, 0.3, 0.0]), screen(session, [0.6, 0.4, 0.0]), screen(session, [0.9, 0.5, 0.0])];
let uv_points = [Vec2::new(0.05, 0.95), Vec2::new(0.1, 0.92), Vec2::new(0.15, 0.9)];
if tool == Tool::Path {
let params = if in_2d_view {
json!({"uvPoints": uv_points.iter().map(|uv| [uv.x, uv.y]).collect::<Vec<_>>()})
} else {
json!({"points": points.iter().map(|point| [point.x, point.y]).collect::<Vec<_>>()})
};
execute(session, "tool.path", &params)?;
execute(session, "tool.path", &json!({"commit": true}))?;
return Ok(());
}
let single_click = matches!(tool, Tool::PolygonFill | Tool::ColorPicker | Tool::GeometryMask);
let viewport = session.viewport;
if in_2d_view {
session.begin_stroke_uv(uv_points[0], 1.0)?;
} else {
session.begin_stroke(points[0], viewport, 1.0)?;
}
if single_click {
return Ok(());
}
for (point, uv) in points.iter().zip(&uv_points).skip(1) {
if in_2d_view {
session.continue_stroke_uv(*uv, 1.0);
} else {
session.continue_stroke(*point, 1.0);
}
}
session.end_stroke()
}
#[test]
fn every_selection_offers_the_tools_that_can_act_on_it() {
let mut wrong = Vec::new();
for selection in Selection::ALL {
let session = session_with(selection);
for tool in Tool::ALL {
let usable = session.tool_usable(tool);
if usable != selection.offers(tool) {
wrong.push(format!("{selection:?} {tool:?}: usable {usable}"));
}
}
}
assert!(wrong.is_empty(), "{wrong:#?}");
}
#[test]
fn every_usable_tool_acts_in_both_views() {
let mut wrong = Vec::new();
for in_2d_view in [false, true] {
for selection in Selection::ALL {
for tool in Tool::ALL {
// The picker and the geometry mask read the 3D view only, whatever is selected
if in_2d_view && matches!(tool, Tool::ColorPicker | Tool::GeometryMask) {
continue;
}
let mut session = session_with(selection);
let usable = session.tool_usable(tool);
let result = use_tool(&mut session, tool, in_2d_view);
let view = if in_2d_view { "2D" } else { "3D" };
match result {
Err(error) if usable => wrong.push(format!("{view} {selection:?} {tool:?}: usable but {error}")),
Ok(()) if !usable && !selection.scripts_still_paint() => wrong.push(format!("{view} {selection:?} {tool:?}: greyed but acts")),
_ => {}
}
}
}
}
assert!(wrong.is_empty(), "{wrong:#?}");
}
fn commit_path(session: &mut Session) -> texturecraft_engine::Result<Value> {
execute(session, "tool.path", &json!({"uvPoints": [[0.05, 0.95], [0.15, 0.9]], "commit": true}))
}
fn path_count(session: &Session, path_sublayer: LayerId) -> usize {
match session.doc.project.effect(path_sublayer).map(|(_, effect)| &effect.kind) {
Some(EffectKind::Path(paths)) => paths.len(),
_ => 0,
}
}
#[test]
fn paths_are_refused_on_fill_layers_and_the_quick_mask_but_saved_ones_stay_editable() {
let mut session = session_with(Selection::FillContent);
let fill = session.selected_layer().unwrap();
assert!(commit_path(&mut session).is_err(), "no new paths in a fill layer");
assert!(session.doc.project.layer(fill).unwrap().content_effects.is_empty());
let mut session = session_with(Selection::QuickMaskOverPaint);
assert!(commit_path(&mut session).is_err(), "paths can't edit the quick mask");
// A project saved while paths could go into fill layers
let mut session = session_with(Selection::FillContent);
let fill = session.selected_layer().unwrap();
let saved_path = session.doc.project.alloc_id();
session.doc.project.layer_mut(fill).unwrap().content_effects.push(Effect::new(saved_path, EffectKind::Path(Vec::new())));
select(&mut session, fill.0, json!(saved_path.0));
assert!(session.tool_usable(Tool::Path));
commit_path(&mut session).unwrap();
assert_eq!(path_count(&session, saved_path), 1, "the saved path sublayer takes the new path");
}
#[test]
fn quick_mask_edit_and_invert_need_the_mask_on() {
let mut session = base_session();
assert!(execute(&mut session, "viewport.quickMask.edit", &json!({"editing": true})).is_err());
assert!(execute(&mut session, "viewport.quickMask.invert", &json!({})).is_err());
execute(&mut session, "viewport.quickMask", &json!({"enabled": true})).unwrap();
execute(&mut session, "viewport.quickMask.edit", &json!({"editing": true})).unwrap();
execute(&mut session, "viewport.quickMask", &json!({"enabled": false})).unwrap();
assert!(!session.quick_mask.editing, "turning the mask off stops editing it");
}
#[test]
fn a_saved_path_cannot_be_committed_while_editing_the_quick_mask() {
let mut session = session_with(Selection::PaintContent);
commit_path(&mut session).unwrap();
let StackTarget::Effect(path_sublayer) = session.target else { panic!("the committed path is not selected") };
let points_before = match session.doc.project.effect(path_sublayer).map(|(_, effect)| &effect.kind) {
Some(EffectKind::Path(paths)) => paths[0].points.clone(),
_ => panic!("the committed path is missing"),
};
execute(&mut session, "viewport.quickMask", &json!({"enabled": true})).unwrap();
execute(&mut session, "viewport.quickMask.edit", &json!({"editing": true})).unwrap();
execute(&mut session, "path.edit", &json!({"effect": path_sublayer.0, "index": 0})).unwrap();
assert!(!session.tool_usable(Tool::Path));
execute(&mut session, "tool.path", &json!({"uvPoints": [[0.2, 0.8], [0.3, 0.7]]})).unwrap();
assert!(!session.path_live().unwrap(), "live preview must leave the stored path alone");
let result = execute(&mut session, "tool.path", &json!({"commit": true}));
assert!(result.is_err(), "a greyed Path tool must not commit a stored path into the quick mask edit state");
let points_after = match session.doc.project.effect(path_sublayer).map(|(_, effect)| &effect.kind) {
Some(EffectKind::Path(paths)) => paths[0].points.clone(),
_ => panic!("the stored path is missing"),
};
assert_eq!(points_after, points_before);
}
@@ -0,0 +1,83 @@
//! Selection cases for the tool availability tests
use texturecraft_engine::Tool;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Selection {
PaintContent,
FillContent,
PaintMask,
FillMask,
FolderMask,
InstanceOwnMask,
PaintSublayer,
PaintSublayerInMask,
PaintSublayerInFill,
PathSublayer,
FilterInPaint,
FilterInFill,
FilterInMask,
FillEffectInPaint,
GeneratorInFillMask,
FolderContent,
InstanceOfPaint,
InstanceOfFill,
Nothing,
QuickMaskOverPaint,
QuickMaskOverFill,
QuickMaskOverNothing,
}
impl Selection {
pub(super) const ALL: [Selection; 22] = [
Selection::PaintContent,
Selection::FillContent,
Selection::PaintMask,
Selection::FillMask,
Selection::FolderMask,
Selection::InstanceOwnMask,
Selection::PaintSublayer,
Selection::PaintSublayerInMask,
Selection::PaintSublayerInFill,
Selection::PathSublayer,
Selection::FilterInPaint,
Selection::FilterInFill,
Selection::FilterInMask,
Selection::FillEffectInPaint,
Selection::GeneratorInFillMask,
Selection::FolderContent,
Selection::InstanceOfPaint,
Selection::InstanceOfFill,
Selection::Nothing,
Selection::QuickMaskOverPaint,
Selection::QuickMaskOverFill,
Selection::QuickMaskOverNothing,
];
/// Whether the shelf offers `tool` for this selection
pub(super) fn offers(self, tool: Tool) -> bool {
let reads_content = matches!(tool, Tool::Smudge | Tool::Clone);
match tool {
Tool::ColorPicker => true,
Tool::GeometryMask => !matches!(self, Selection::Nothing | Selection::QuickMaskOverNothing),
_ => match self {
Selection::PaintContent | Selection::PaintSublayer => true,
Selection::PaintSublayerInFill => tool != Tool::Path,
Selection::PaintMask | Selection::FillMask | Selection::FolderMask | Selection::InstanceOwnMask | Selection::PaintSublayerInMask => {
!reads_content
}
Selection::PathSublayer => tool == Tool::Path,
Selection::QuickMaskOverPaint | Selection::QuickMaskOverFill | Selection::QuickMaskOverNothing => tool != Tool::Path,
_ => false,
},
}
}
/// Greyed in the shelf, but scripts still paint here: below the effect, or the instance's source
pub(super) fn scripts_still_paint(self) -> bool {
matches!(
self,
Selection::FilterInPaint | Selection::FillEffectInPaint | Selection::FilterInMask | Selection::GeneratorInFillMask | Selection::InstanceOfPaint
)
}
}
+81 -23
View File
@@ -153,32 +153,39 @@ fn library_alphas_shape_the_stamp() {
#[test]
fn quick_mask_limits_painting() {
let mut s = session();
paint_red(&mut s, 20.0);
let r = execute(&mut s, "viewport.quickMask.edit", &Value::Null).unwrap();
assert_eq!(r["editing"], json!(true));
assert_eq!(r["enabled"], json!(true), "editing enables the mask");
let undo = s.history_labels().len();
dab(&mut s, 150.0, 200.0);
assert!(!red(color_at(&s, 150.0, 200.0)), "mask strokes do not paint the layer");
assert_eq!(s.history_labels().len(), undo, "mask strokes stay out of the history");
assert!(s.quick_mask_rgba(0, 64).is_some_and(|v| v.len() == 64 * 64 * 4));
execute(&mut s, "viewport.quickMask.edit", &Value::Null).unwrap();
let mut session = session();
paint_red(&mut session, 20.0);
execute(&mut session, "viewport.quickMask", &json!({"enabled": true})).unwrap();
execute(&mut s, "tool.brush", &json!({"size": 6})).unwrap();
dab(&mut s, 150.0, 200.0);
dab(&mut s, 250.0, 200.0);
assert!(red(color_at(&s, 150.0, 200.0)), "inside the mask");
assert!(!red(color_at(&s, 250.0, 200.0)), "outside the mask");
let state = execute(&mut session, "viewport.quickMask.edit", &Value::Null).unwrap();
let undo = session.history_labels().len();
dab(&mut session, 150.0, 200.0);
execute(&mut s, "viewport.quickMask.invert", &Value::Null).unwrap();
dab(&mut s, 250.0, 200.0);
assert!(red(color_at(&s, 250.0, 200.0)), "inverted: outside is open");
assert_eq!(state["editing"], json!(true));
assert!(!red(color_at(&session, 150.0, 200.0)), "mask strokes do not paint the layer");
assert_eq!(session.history_labels().len(), undo, "mask strokes stay out of the history");
let overlay_size: u32 = 64;
let rgba_bytes = (overlay_size * overlay_size * 4) as usize;
assert!(session.quick_mask_rgba(0, overlay_size).is_some_and(|pixels| pixels.len() == rgba_bytes), "an RGBA8 overlay of the requested size");
execute(&mut s, "viewport.quickMask", &json!({"enabled": false})).unwrap();
execute(&mut s, "tool.material", &json!({"channel": "baseColor", "color": [0.0, 1.0, 0.0]})).unwrap();
dab(&mut s, 150.0, 200.0);
assert!(color_at(&s, 150.0, 200.0)[1] > 0.85, "disabled: everything is open");
execute(&mut session, "viewport.quickMask.edit", &Value::Null).unwrap();
execute(&mut session, "tool.brush", &json!({"size": 6})).unwrap();
dab(&mut session, 150.0, 200.0);
dab(&mut session, 250.0, 200.0);
assert!(red(color_at(&session, 150.0, 200.0)), "inside the mask");
assert!(!red(color_at(&session, 250.0, 200.0)), "outside the mask");
execute(&mut session, "viewport.quickMask.invert", &Value::Null).unwrap();
dab(&mut session, 250.0, 200.0);
assert!(red(color_at(&session, 250.0, 200.0)), "inverted: outside is open");
execute(&mut session, "viewport.quickMask", &json!({"enabled": false})).unwrap();
execute(&mut session, "tool.material", &json!({"channel": "baseColor", "color": [0.0, 1.0, 0.0]})).unwrap();
dab(&mut session, 150.0, 200.0);
assert!(color_at(&session, 150.0, 200.0)[1] > 0.85, "disabled: everything is open");
}
/// How much redder than green a texel must be to count as painted (faded edges included)
@@ -244,3 +251,54 @@ fn swap_mask_value_inverts_only_while_a_mask_is_the_target() {
assert_eq!(session.tool.mask_value, 0.0, "a mask effect paints the mask too");
}
#[test]
fn stroke_after_undoing_mask_is_paint_stroke() {
let mut s = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:random", None, 256).unwrap();
s.viewport = Vec2::new(400.0, 400.0);
execute(&mut s, "layer.add.fill", &json!({"color": [0.2, 0.5, 0.3], "roughness": 0.7})).unwrap();
execute(&mut s, "layer.add.paint", &Value::Null).unwrap();
s.update();
execute(&mut s, "tool.brush", &json!({"size": 20, "hardness": 50, "opacity": 0.8})).unwrap();
execute(&mut s, "tool.paint", &Value::Null).unwrap();
execute(&mut s, "tool.material", &json!({"channel": "baseColor", "color": [0.9, 0.3, 0.9]})).unwrap();
execute(&mut s, "tool.stroke", &json!({"points": [[130, 130], [170, 150]]})).unwrap();
s.update();
execute(&mut s, "mask.add.white", &Value::Null).unwrap();
s.update();
execute(&mut s, "edit.undo", &Value::Null).unwrap();
s.update();
execute(&mut s, "tool.stroke", &json!({"points": [[167, 183], [207, 203]]})).unwrap();
s.update();
let undone = execute(&mut s, "edit.undo", &Value::Null).unwrap();
assert_eq!(undone["undone"], "Paint stroke", "the stroke after undoing the mask was recorded as {}", undone["undone"]);
execute(&mut s, "edit.redo", &Value::Null).unwrap();
s.update();
let settled = s.caches[0].composite.clone();
s.caches[0].below.clear();
s.caches[0].invalidate_all();
s.update();
assert_eq!(settled, s.caches[0].composite, "the composite after the stroke matches a full recomposite");
}
#[test]
fn undoing_after_painting_on_instance_source_mask_keeps_mask_target() {
let mut s = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:random", None, 256).unwrap();
s.viewport = Vec2::new(400.0, 400.0);
execute(&mut s, "layer.add.paint", &Value::Null).unwrap();
execute(&mut s, "mask.add.white", &Value::Null).unwrap();
execute(&mut s, "layer.instance", &Value::Null).unwrap();
s.update();
s.target = StackTarget::Mask;
execute(&mut s, "tool.brush", &json!({"size": 20, "hardness": 50, "opacity": 0.8})).unwrap();
execute(&mut s, "tool.stroke", &json!({"points": [[170, 180], [210, 200]]})).unwrap();
execute(&mut s, "edit.undo", &Value::Null).unwrap();
assert_eq!(s.target, StackTarget::Mask);
execute(&mut s, "tool.stroke", &json!({"points": [[180, 180], [210, 200]]})).unwrap();
let undone = execute(&mut s, "edit.undo", &Value::Null).unwrap();
assert_eq!(undone["undone"], "Mask stroke");
}
+176
View File
@@ -308,6 +308,67 @@ fn image_resources_fill_layers_and_round_trip() {
assert!(s.import_resource_bytes("bad.png", vec![1, 2, 3]).is_err());
}
#[test]
fn image_resource_lookup_accepts_extension_and_stem() {
let mut s = session();
// No resources imported yet
let err = execute(&mut s, "layer.add.image", &json!({"name": "missing"})).unwrap_err();
assert!(err.to_string().contains("no resource `missing` (no resources imported: use resource.import)"));
let mut png = Vec::new();
let img = image::RgbaImage::from_fn(4, 4, |_, _| image::Rgba([0, 255, 0, 255]));
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
s.import_resource_bytes("tile.png", png.clone()).unwrap();
// Lookup by stem
let r = execute(&mut s, "layer.add.image", &json!({"name": "tile"})).unwrap();
assert!(r["layer"].as_u64().is_some());
// Lookup by full name
let r2 = execute(&mut s, "layer.add.image", &json!({"name": "tile.png"})).unwrap();
assert!(r2["layer"].as_u64().is_some());
// Missing resource lists available
let err = execute(&mut s, "layer.add.image", &json!({"name": "wood"})).unwrap_err();
assert!(err.to_string().contains("no resource `wood` (available: tile.png)"));
// Ambiguous stem resolution
s.import_resource_bytes("tile.jpg", png).unwrap();
let err = execute(&mut s, "layer.add.image", &json!({"name": "tile"})).unwrap_err();
assert!(err.to_string().contains("ambiguous resource `tile`"));
}
#[test]
fn image_resource_lookup_ignores_linked_packages() {
use std::sync::Arc;
use texturecraft_engine::core::{LayerId, LayerKind, Source};
let mut s = session();
s.doc.resources.insert("wood.mcraft".into(), Arc::new(b"package bytes".to_vec()));
s.sync_images();
assert!(!s.images.contains_key("wood.mcraft"));
let err = execute(&mut s, "layer.add.image", &json!({"name": "wood"})).unwrap_err();
assert!(err.to_string().contains("no resources imported"), "{err}");
let err = execute(&mut s, "layer.add.image", &json!({"name": "wood.mcraft"})).unwrap_err();
assert!(err.to_string().contains("no resource `wood.mcraft` (no resources imported"), "{err}");
let mut png = Vec::new();
let img = image::RgbaImage::from_fn(4, 4, |_, _| image::Rgba([0, 255, 0, 255]));
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
s.import_resource_bytes("wood.png", png).unwrap();
let err = execute(&mut s, "layer.add.image", &json!({"name": "missing"})).unwrap_err();
assert!(err.to_string().contains("no resource `missing` (available: wood.png)"), "{err}");
let res = execute(&mut s, "layer.add.image", &json!({"name": "wood"})).unwrap();
let id = LayerId(res["layer"].as_u64().unwrap());
let LayerKind::Fill(fill) = &s.doc.project.layer(id).unwrap().kind else { panic!("expected fill layer") };
assert_eq!(fill.sources.get(&Channel::BaseColor), Some(&Source::image("wood.png")));
}
#[test]
fn brush_presets_apply_save_and_load() {
let mut s = session();
@@ -524,3 +585,118 @@ fn curvature_generators_take_a_mode_and_detail_sizes() {
assert!(dirt.is_err(), "only curvature generators have a mode");
assert_eq!(execute(&mut state, "project.inspect", &Value::Null).unwrap().to_string(), inspect, "failed commands leave the project unchanged");
}
#[test]
fn generators_take_blur_balance_and_contrast() {
let mut state = session();
execute(&mut state, "layer.add.fill", &json!({})).unwrap();
execute(&mut state, "mask.add.black", &Value::Null).unwrap();
let added = execute(&mut state, "effect.add.generator", &json!({"generator": "dirt", "blur": 0.25})).unwrap();
let effect = added["effect"].clone();
execute(&mut state, "effect.set", &json!({"effect": effect, "balance": 0.75, "contrast": 2.0})).unwrap();
let inspect = execute(&mut state, "project.inspect", &Value::Null).unwrap().to_string();
assert!(inspect.contains(r#""blur":0.25"#) && inspect.contains(r#""balance":0.75"#), "{inspect}");
assert!(inspect.contains(r#""contrast":1.0"#), "contrast is clamped to 0..1: {inspect}");
execute(&mut state, "effect.set", &json!({"effect": effect, "resolution": "custom", "customResolution": 512, "resolutionFactor": -9})).unwrap();
let inspect = execute(&mut state, "project.inspect", &Value::Null).unwrap().to_string();
assert!(inspect.contains(r#""resolution":{"custom":512,"factor":-3,"mode":"Custom"}"#), "the factor is clamped to 1/8: {inspect}");
assert!(execute(&mut state, "effect.set", &json!({"effect": effect, "resolution": "huge"})).is_err(), "unknown resolutions are rejected");
}
#[test]
fn a_generator_input_reads_a_uniform_value_or_its_mesh_map_again() {
let mut state = session();
execute(&mut state, "layer.add.fill", &json!({})).unwrap();
execute(&mut state, "mask.add.black", &Value::Null).unwrap();
let effect = execute(&mut state, "effect.add.generator", &json!({"generator": "dirt"})).unwrap()["effect"].clone();
execute(&mut state, "effect.set", &json!({"effect": effect, "input": "ambientOcclusion", "inputValue": 0.25})).unwrap();
let inspect = execute(&mut state, "project.inspect", &Value::Null).unwrap().to_string();
assert!(inspect.contains(r#""inputs":{"ambientOcclusion":{"uniform":[0.25,0.25,0.25]}}"#), "{inspect}");
let wrong_input = execute(&mut state, "effect.set", &json!({"effect": effect, "input": "curvature", "inputValue": 0.5}));
assert!(wrong_input.is_err(), "dirt reads ambient occlusion, not curvature");
let missing_image = execute(&mut state, "effect.set", &json!({"effect": effect, "input": "ambientOcclusion", "inputImage": "nothing.png"}));
assert!(missing_image.is_err(), "the image must be a resource of the project");
let two_sources = execute(&mut state, "effect.set", &json!({"effect": effect, "input": "ambientOcclusion", "inputValue": 0.5, "inputBaked": true}));
assert!(two_sources.is_err(), "an input takes one source");
execute(&mut state, "effect.set", &json!({"effect": effect, "input": "ambientOcclusion", "inputBaked": true})).unwrap();
let inspect = execute(&mut state, "project.inspect", &Value::Null).unwrap().to_string();
assert!(!inspect.contains(r#""inputs""#), "the mesh map is read again: {inspect}");
}
#[test]
fn changing_generator_kind_keeps_the_effect_identity_and_settings() {
let mut state = session();
execute(&mut state, "layer.add.fill", &json!({})).unwrap();
execute(&mut state, "mask.add.black", &Value::Null).unwrap();
let effect = execute(&mut state, "effect.add.generator", &json!({"generator": "dirt"})).unwrap()["effect"].as_u64().unwrap();
execute(&mut state, "effect.set", &json!({"effect": effect, "name": "Keep me", "opacity": 0.4, "level": 0.2})).unwrap();
execute(&mut state, "effect.set", &json!({"effect": effect, "input": "ambientOcclusion", "inputValue": 0.25})).unwrap();
let later = execute(&mut state, "effect.add.generator", &json!({"generator": "position"})).unwrap()["effect"].as_u64().unwrap();
execute(&mut state, "effect.set", &json!({"effect": effect, "generator": "ambientOcclusion"})).unwrap();
let (owner, edited) = state.doc.project.effect(texturecraft_core::LayerId(effect)).unwrap();
assert!(matches!(&edited.kind, texturecraft_core::EffectKind::Generator(generator)
if generator.kind == texturecraft_core::GeneratorKind::AmbientOcclusion
&& generator.level == 0.2
&& matches!(generator.inputs.get(&texturecraft_core::BakedMap::AmbientOcclusion), Some(texturecraft_core::Source::Uniform([0.25, 0.25, 0.25])))));
assert_eq!(edited.name, "Keep me");
assert_eq!(edited.opacity, 0.4);
let layer = state.doc.project.layer(owner).unwrap();
let ids: Vec<u64> = layer.mask.as_ref().unwrap().effects.iter().map(|item| item.id.0).collect();
assert_eq!(ids, vec![effect, later]);
}
#[test]
fn unparseable_color_param_returns_error() {
let mut s = session();
let err = execute(&mut s, "layer.add.fill", &json!({"color": "#fff"})).unwrap_err();
assert_eq!(err.to_string(), "`layer.add.fill`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
let err = execute(&mut s, "layer.fill.set", &json!({"channel": "baseColor", "color": "#123"})).unwrap_err();
assert_eq!(err.to_string(), "`layer.fill.set`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
let err = execute(&mut s, "tool.material", &json!({"channel": "baseColor", "color": "invalid"})).unwrap_err();
assert_eq!(err.to_string(), "`tool.material`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
let err = execute(&mut s, "mask.add.colorSelection", &json!({"color": "#xyz"})).unwrap_err();
assert_eq!(err.to_string(), "`mask.add.colorSelection`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
let err = execute(&mut s, "display.set", &json!({"scatterColor": "#12345"})).unwrap_err();
assert_eq!(err.to_string(), "`display.set`: `scatterColor` must be a color ([r, g, b] linear or \"#rrggbb\")");
let rev_before = s.revision;
let exposure_before = s.doc.project.display.env_exposure;
let err = execute(&mut s, "display.set", &json!({"exposure": 2.0, "scatterColor": "invalid"})).unwrap_err();
assert_eq!(err.to_string(), "`display.set`: `scatterColor` must be a color ([r, g, b] linear or \"#rrggbb\")");
assert_eq!(s.doc.project.display.env_exposure, exposure_before);
assert_eq!(s.revision, rev_before);
let mask_before = s.tool.mask_value;
let err = execute(&mut s, "tool.material", &json!({"maskValue": 0.25, "channel": "baseColor", "color": "invalid"})).unwrap_err();
assert_eq!(err.to_string(), "`tool.material`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
assert_eq!(s.tool.mask_value, mask_before);
assert_eq!(s.revision, rev_before);
let err = execute(&mut s, "tool.material", &json!({"color": "invalid"})).unwrap_err();
assert_eq!(err.to_string(), "`tool.material`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
assert_eq!(s.revision, rev_before);
let err = execute(&mut s, "layer.add.fill", &json!({"color": [1e40, 0.0, 0.0]})).unwrap_err();
assert_eq!(err.to_string(), "`layer.add.fill`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
assert_eq!(s.revision, rev_before);
let err = execute(&mut s, "layer.fill.set", &json!({"channel": "baseColor", "procedural": "noise", "color": "red"})).unwrap_err();
assert_eq!(err.to_string(), "`layer.fill.set`: `color` must be a color ([r, g, b] linear or \"#rrggbb\")");
assert_eq!(s.revision, rev_before);
assert!(execute(&mut s, "tool.cloneSource", &json!({"uv": [0.2, 0.8, 0.4]})).is_err());
execute(&mut s, "tool.polygonFill", &Value::Null).unwrap();
let err = execute(&mut s, "tool.click", &json!({"uv": [0.2, 0.8, 0.4]})).unwrap_err();
assert_eq!(err.to_string(), "`tool.click`: a 2D view point needs `uv` as [u, v]");
}
+8 -1
View File
@@ -9,13 +9,20 @@ license.workspace = true
workspace = true
[dependencies]
craft-usd.workspace = true
# ICC v2 serialization prerequisite: craft-libs#1735; app colour policy stays local.
craft-cms-icc-v2 = { package = "craft-cms", version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "272a095b61257917693da3d09f2f8ecccd7f0295" }
craft-materialx = { workspace = true }
craft-mesh = { workspace = true }
craft-mcmat = { workspace = true }
craft-tcraft = { workspace = true }
craft-zip = { workspace = true }
texturecraft-core = { workspace = true }
texturecraft-texel = { workspace = true }
texturecraft-geom = { workspace = true }
texturecraft-compose = { workspace = true }
image = { workspace = true }
gltf = { workspace = true }
tobj = { workspace = true }
flate2 = { workspace = true }
lz4_flex = { workspace = true }
half = { workspace = true }
+31 -110
View File
@@ -1,63 +1,7 @@
//! ICC profiles for the export colour spaces: a version 2.1 matrix/curve display profile, small
//! (about 1.2 KiB) and readable by every colour-managed viewer and editor.
//!
//! The colorants come from the space's primaries adapted to the D50 profile connection space
//! (see `ColorSpace::to_xyz_d50`). sRGB carries its transfer curve as a sampled table; the linear
//! spaces carry a gamma of 1. Written from the ICC.1:2001-04 specification; nothing is copied
//! from a colour-management library or from another profile.
//! ICC v2.1 export policy; byte serialization is shared with craft-cms.
use craft_cms_icc_v2::write_v2::{MatrixProfile, ToneCurve, matrix_profile};
use texturecraft_texel::{ColorSpace, srgb_to_linear};
/// Samples of the sRGB curve table.
const CURVE_POINTS: usize = 1024;
/// The D50 white of the profile connection space.
const D50_XYZ: [f64; 3] = [0.9642, 1.0, 0.8249];
fn s15f16(v: f64) -> [u8; 4] {
((v * 65536.0).round().clamp(f64::from(i32::MIN), f64::from(i32::MAX)) as i32).to_be_bytes()
}
fn xyz_tag(v: [f64; 3]) -> Vec<u8> {
let mut b = b"XYZ \0\0\0\0".to_vec();
for c in v {
b.extend_from_slice(&s15f16(c));
}
b
}
fn text_tag(text: &str) -> Vec<u8> {
let mut b = b"text\0\0\0\0".to_vec();
b.extend_from_slice(text.as_bytes());
b.push(0);
b
}
/// `textDescriptionType` with the ASCII description only.
fn desc_tag(text: &str) -> Vec<u8> {
let mut b = b"desc\0\0\0\0".to_vec();
b.extend_from_slice(&(text.len() as u32 + 1).to_be_bytes());
b.extend_from_slice(text.as_bytes());
b.push(0);
// No Unicode text and no Macintosh ScriptCode text: empty fields and the 67-byte filler.
b.extend_from_slice(&[0; 4 + 4 + 2 + 1 + 67]);
b
}
fn curve_tag(space: ColorSpace) -> Vec<u8> {
let mut b = b"curv\0\0\0\0".to_vec();
if space == ColorSpace::Srgb {
b.extend_from_slice(&(CURVE_POINTS as u32).to_be_bytes());
for i in 0..CURVE_POINTS {
let v = f64::from(srgb_to_linear(i as f32 / (CURVE_POINTS - 1) as f32));
b.extend_from_slice(&((v * 65535.0).round().clamp(0.0, 65535.0) as u16).to_be_bytes());
}
} else {
// One entry is a gamma, u8Fixed8: 1.0 is linear.
b.extend_from_slice(&1u32.to_be_bytes());
b.extend_from_slice(&0x0100u16.to_be_bytes());
}
b
}
fn description(space: ColorSpace) -> &'static str {
match space {
@@ -68,64 +12,41 @@ fn description(space: ColorSpace) -> &'static str {
}
}
/// The ICC profile of `space`, or None if its primaries are degenerate (never for the built-in ones).
/// The ICC profile of `space`, or None if its primaries are degenerate.
pub fn profile(space: ColorSpace) -> Option<Vec<u8>> {
let m = space.to_xyz_d50()?;
let column = |c: usize| [m[0][c], m[1][c], m[2][c]];
let curve = curve_tag(space);
// The green and blue curves are the red one's entry.
let tags: [(&[u8; 4], Vec<u8>); 9] = [
(b"desc", desc_tag(description(space))),
(b"cprt", text_tag("No copyright, generated by TextureCraft")),
(b"wtpt", xyz_tag(D50_XYZ)),
(b"rXYZ", xyz_tag(column(0))),
(b"gXYZ", xyz_tag(column(1))),
(b"bXYZ", xyz_tag(column(2))),
(b"rTRC", curve.clone()),
(b"gTRC", curve.clone()),
(b"bTRC", curve),
];
// Header (128) + tag count (4) + 12 per tag, then the tag data, each tag padded to 4 bytes.
let table_end = 128 + 4 + 12 * tags.len();
let mut offsets = Vec::new();
let mut data: Vec<u8> = Vec::new();
for (i, (_, bytes)) in tags.iter().enumerate() {
let shared = i >= 7;
let at = if shared { offsets.get(6).copied().unwrap_or(0) } else { table_end + data.len() };
offsets.push(at);
if !shared {
data.extend_from_slice(bytes);
while !data.len().is_multiple_of(4) {
data.push(0);
}
}
}
let total = table_end + data.len();
let mut out = vec![0u8; 128];
out.get_mut(0..4)?.copy_from_slice(&(total as u32).to_be_bytes());
out.get_mut(8..12)?.copy_from_slice(&[2, 0x10, 0, 0]); // version 2.1.0
out.get_mut(12..16)?.copy_from_slice(b"mntr");
out.get_mut(16..20)?.copy_from_slice(b"RGB ");
out.get_mut(20..24)?.copy_from_slice(b"XYZ ");
// A fixed date (2026-01-01 00:00:00), so a space always gives the same bytes.
out.get_mut(24..36)?.copy_from_slice(&[0x07, 0xEA, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0]);
out.get_mut(36..40)?.copy_from_slice(b"acsp");
for (k, v) in D50_XYZ.iter().enumerate() {
out.get_mut(68 + 4 * k..72 + 4 * k)?.copy_from_slice(&s15f16(*v));
}
out.extend_from_slice(&(tags.len() as u32).to_be_bytes());
for ((sig, bytes), at) in tags.iter().zip(&offsets) {
out.extend_from_slice(*sig);
out.extend_from_slice(&(*at as u32).to_be_bytes());
out.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
}
out.extend_from_slice(&data);
Some(out)
let matrix = space.to_xyz_d50()?;
let samples: Vec<u16> = if space == ColorSpace::Srgb {
(0..CURVE_POINTS)
.map(|i| {
let v = f64::from(srgb_to_linear(i as f32 / (CURVE_POINTS - 1) as f32));
(v * 65535.0).round().clamp(0.0, 65535.0) as u16
})
.collect()
} else {
Vec::new()
};
matrix_profile(&MatrixProfile {
matrix,
curve: if samples.is_empty() { ToneCurve::Linear } else { ToneCurve::Samples(&samples) },
description: description(space),
copyright: "No copyright, generated by TextureCraft",
date: [2026, 1, 1, 0, 0, 0],
})
}
#[cfg(test)]
#[path = "icc_legacy_test.rs"]
mod legacy;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn all_spaces_match_legacy_wire_bytes() {
for space in ColorSpace::ALL {
assert_eq!(profile(space), legacy::profile(space), "{space:?}");
}
}
fn u32_at(b: &[u8], i: usize) -> u32 {
u32::from_be_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]])
+124
View File
@@ -0,0 +1,124 @@
//! ICC profiles for the export colour spaces: a version 2.1 matrix/curve display profile, small
//! (about 1.2 KiB) and readable by every colour-managed viewer and editor.
//!
//! The colorants come from the space's primaries adapted to the D50 profile connection space
//! (see `ColorSpace::to_xyz_d50`). sRGB carries its transfer curve as a sampled table; the linear
//! spaces carry a gamma of 1. Written from the ICC.1:2001-04 specification; nothing is copied
//! from a colour-management library or from another profile.
use texturecraft_texel::{ColorSpace, srgb_to_linear};
/// Samples of the sRGB curve table.
const CURVE_POINTS: usize = 1024;
/// The D50 white of the profile connection space.
const D50_XYZ: [f64; 3] = [0.9642, 1.0, 0.8249];
fn s15f16(v: f64) -> [u8; 4] {
((v * 65536.0).round().clamp(f64::from(i32::MIN), f64::from(i32::MAX)) as i32).to_be_bytes()
}
fn xyz_tag(v: [f64; 3]) -> Vec<u8> {
let mut b = b"XYZ \0\0\0\0".to_vec();
for c in v {
b.extend_from_slice(&s15f16(c));
}
b
}
fn text_tag(text: &str) -> Vec<u8> {
let mut b = b"text\0\0\0\0".to_vec();
b.extend_from_slice(text.as_bytes());
b.push(0);
b
}
/// `textDescriptionType` with the ASCII description only.
fn desc_tag(text: &str) -> Vec<u8> {
let mut b = b"desc\0\0\0\0".to_vec();
b.extend_from_slice(&(text.len() as u32 + 1).to_be_bytes());
b.extend_from_slice(text.as_bytes());
b.push(0);
// No Unicode text and no Macintosh ScriptCode text: empty fields and the 67-byte filler.
b.extend_from_slice(&[0; 4 + 4 + 2 + 1 + 67]);
b
}
fn curve_tag(space: ColorSpace) -> Vec<u8> {
let mut b = b"curv\0\0\0\0".to_vec();
if space == ColorSpace::Srgb {
b.extend_from_slice(&(CURVE_POINTS as u32).to_be_bytes());
for i in 0..CURVE_POINTS {
let v = f64::from(srgb_to_linear(i as f32 / (CURVE_POINTS - 1) as f32));
b.extend_from_slice(&((v * 65535.0).round().clamp(0.0, 65535.0) as u16).to_be_bytes());
}
} else {
// One entry is a gamma, u8Fixed8: 1.0 is linear.
b.extend_from_slice(&1u32.to_be_bytes());
b.extend_from_slice(&0x0100u16.to_be_bytes());
}
b
}
fn description(space: ColorSpace) -> &'static str {
match space {
ColorSpace::Srgb => "sRGB (Rec.709 primaries, sRGB curve)",
ColorSpace::LinearRec709 => "Linear Rec.709 (sRGB primaries, D65)",
ColorSpace::AcesCg => "ACEScg (AP1 primaries, linear)",
ColorSpace::Aces2065 => "ACES2065-1 (AP0 primaries, linear)",
}
}
/// The ICC profile of `space`, or None if its primaries are degenerate (never for the built-in ones).
pub fn profile(space: ColorSpace) -> Option<Vec<u8>> {
let m = space.to_xyz_d50()?;
let column = |c: usize| [m[0][c], m[1][c], m[2][c]];
let curve = curve_tag(space);
// The green and blue curves are the red one's entry.
let tags: [(&[u8; 4], Vec<u8>); 9] = [
(b"desc", desc_tag(description(space))),
(b"cprt", text_tag("No copyright, generated by TextureCraft")),
(b"wtpt", xyz_tag(D50_XYZ)),
(b"rXYZ", xyz_tag(column(0))),
(b"gXYZ", xyz_tag(column(1))),
(b"bXYZ", xyz_tag(column(2))),
(b"rTRC", curve.clone()),
(b"gTRC", curve.clone()),
(b"bTRC", curve),
];
// Header (128) + tag count (4) + 12 per tag, then the tag data, each tag padded to 4 bytes.
let table_end = 128 + 4 + 12 * tags.len();
let mut offsets = Vec::new();
let mut data: Vec<u8> = Vec::new();
for (i, (_, bytes)) in tags.iter().enumerate() {
let shared = i >= 7;
let at = if shared { offsets.get(6).copied().unwrap_or(0) } else { table_end + data.len() };
offsets.push(at);
if !shared {
data.extend_from_slice(bytes);
while !data.len().is_multiple_of(4) {
data.push(0);
}
}
}
let total = table_end + data.len();
let mut out = vec![0u8; 128];
out.get_mut(0..4)?.copy_from_slice(&(total as u32).to_be_bytes());
out.get_mut(8..12)?.copy_from_slice(&[2, 0x10, 0, 0]); // version 2.1.0
out.get_mut(12..16)?.copy_from_slice(b"mntr");
out.get_mut(16..20)?.copy_from_slice(b"RGB ");
out.get_mut(20..24)?.copy_from_slice(b"XYZ ");
// A fixed date (2026-01-01 00:00:00), so a space always gives the same bytes.
out.get_mut(24..36)?.copy_from_slice(&[0x07, 0xEA, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0]);
out.get_mut(36..40)?.copy_from_slice(b"acsp");
for (k, v) in D50_XYZ.iter().enumerate() {
out.get_mut(68 + 4 * k..72 + 4 * k)?.copy_from_slice(&s15f16(*v));
}
out.extend_from_slice(&(tags.len() as u32).to_be_bytes());
for ((sig, bytes), at) in tags.iter().zip(&offsets) {
out.extend_from_slice(*sig);
out.extend_from_slice(&(*at as u32).to_be_bytes());
out.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
}
out.extend_from_slice(&data);
Some(out)
}
+41 -16
View File
@@ -1,6 +1,6 @@
//! Decoding imported image resources (fills, alphas, stencils).
use crate::{FormatError, MAX_IMAGE_SIZE, Result};
use crate::{FormatError, MAX_IMAGE_SIZE, MAX_RESOURCE_TEXELS, Result};
/// A decoded image: RGBA as 0..1 floats in the file's own encoding (8/16-bit files stay
/// sRGB-encoded; `srgb` says so), row-major, top row first.
@@ -15,33 +15,37 @@ pub struct DecodedImage {
/// Decode PNG, JPEG, TGA, TIFF, BMP, WEBP, EXR or HDR bytes.
pub fn decode_image(bytes: &[u8]) -> Result<DecodedImage> {
decode_image_within(bytes, u64::MAX)
decode_image_within(bytes, MAX_RESOURCE_TEXELS)
}
/// [`decode_image`] for an image of at most `max_texels` (width × height), checked from the
/// header, before any pixel is decoded or allocated.
pub fn decode_image_within(bytes: &[u8], max_texels: u64) -> Result<DecodedImage> {
let mut reader =
image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() })?;
let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes))
.with_guessed_format()
.map_err(|error| FormatError::Malformed { kind: "image", msg: error.to_string() })?;
// TGA has no magic signature; its decoder validates the header when no format was recognized
let format = reader.format().unwrap_or(image::ImageFormat::Tga);
reader.set_format(format);
let (width, height) = reader.into_dimensions().map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() })?;
let (width, height) = reader.into_dimensions().map_err(|error| FormatError::Malformed { kind: "image", msg: error.to_string() })?;
if width == 0 || height == 0 || width > MAX_IMAGE_SIZE || height > MAX_IMAGE_SIZE {
return Err(FormatError::TooLarge(format!("image is {width}x{height} (limit {MAX_IMAGE_SIZE})")));
}
if u64::from(width) * u64::from(height) > max_texels {
return Err(FormatError::TooLarge(format!("image is {width}x{height}, more than {max_texels} texels")));
}
let image =
crate::guard("image", || image::load_from_memory_with_format(bytes, format).map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() }))?;
let image = crate::guard("image", || {
image::load_from_memory_with_format(bytes, format).map_err(|error| FormatError::Malformed { kind: "image", msg: error.to_string() })
})?;
let srgb = !matches!(image, image::DynamicImage::ImageRgb32F(_) | image::DynamicImage::ImageRgba32F(_));
let float_image = image.into_rgba32f();
let (width, height) = float_image.dimensions();
let rgba = float_image.pixels().map(|pixel| pixel.0).collect();
Ok(DecodedImage { width, height, rgba, srgb })
}
@@ -63,15 +67,19 @@ mod tests {
#[test]
fn decodes_png_and_rejects_garbage() {
let mut png = Vec::new();
let img = image::RgbaImage::from_fn(4, 2, |x, _| image::Rgba([x as u8 * 60, 0, 0, 255]));
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let d = decode_image(&png).unwrap();
assert_eq!((d.width, d.height), (4, 2));
assert!(d.srgb);
assert!((d.rgba[1][0] - 60.0 / 255.0).abs() < 1e-3);
let image = image::RgbaImage::from_fn(4, 2, |x, _| image::Rgba([x as u8 * 60, 0, 0, 255]));
image.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let decoded = decode_image(&png).unwrap();
assert_eq!((decoded.width, decoded.height), (4, 2));
assert!(decoded.srgb);
assert!((decoded.rgba[1][0] - 60.0 / 255.0).abs() < 1e-3);
assert!(decode_image(b"not an image").is_err());
assert!(decode_image(&png[..png.len() / 2]).is_err());
let text = decode_image(&coverage_png(2, 1, &[0, 255], [255; 3]).unwrap()).unwrap();
assert_eq!(text.rgba, vec![[1.0, 1.0, 1.0, 0.0], [1.0; 4]]);
assert!(coverage_png(2, 2, &[0], [255; 3]).is_err());
}
@@ -80,11 +88,15 @@ mod tests {
fn tga_without_a_magic_signature_decodes_within_the_texel_budget() {
let mut bytes = Vec::new();
image::RgbaImage::from_pixel(4, 4, image::Rgba([128, 51, 255, 255])).write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Tga).unwrap();
let image = decode_image_within(&bytes, 16).unwrap();
assert_eq!((image.width, image.height), (4, 4));
assert!((image.rgba[0][1] - 0.2).abs() < 1e-6);
assert!(image.srgb);
let header = &bytes[..18];
assert!(matches!(decode_image_within(header, 15), Err(FormatError::TooLarge(_))));
assert!(decode_image(header).is_err());
}
@@ -92,12 +104,25 @@ mod tests {
#[test]
fn decoding_within_a_texel_limit_refuses_big_images_from_the_header() {
let png = {
let mut b = Vec::new();
image::RgbImage::from_pixel(8, 8, image::Rgb([1, 2, 3])).write_to(&mut std::io::Cursor::new(&mut b), image::ImageFormat::Png).unwrap();
b
let mut bytes = Vec::new();
image::RgbImage::from_pixel(8, 8, image::Rgb([1, 2, 3])).write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
bytes
};
assert_eq!(decode_image_within(&png, 64).unwrap().width, 8);
assert!(matches!(decode_image_within(&png, 63), Err(FormatError::TooLarge(_))));
assert!(decode_image(&png).is_ok());
}
#[test]
fn the_default_decoder_refuses_more_than_the_resource_texel_budget_from_the_header() {
// The edge limit permits 16384² but the default resource budget does not
let mut header = [0u8; 18];
header[2] = 2;
header[12..14].copy_from_slice(&16_384u16.to_le_bytes());
header[14..16].copy_from_slice(&16_384u16.to_le_bytes());
header[16] = 24;
assert!(matches!(decode_image(&header), Err(FormatError::TooLarge(_))));
}
}
+61 -8
View File
@@ -95,6 +95,10 @@ pub(crate) fn guard<T>(kind: &'static str, f: impl FnOnce() -> Result<T>) -> Res
/// Largest image edge accepted or produced anywhere in this crate.
pub const MAX_IMAGE_SIZE: u32 = 16_384;
/// Most texels an imported image resource may decode to by default (8192 × 8192).
/// Larger images are refused from their header before any pixels are decoded.
pub const MAX_RESOURCE_TEXELS: u64 = 1 << 26;
/// Read a whole file, refusing files over `cap` bytes.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn read_capped(path: &std::path::Path, cap: u64) -> Result<Vec<u8>> {
@@ -108,15 +112,64 @@ pub(crate) fn read_capped(path: &std::path::Path, cap: u64) -> Result<Vec<u8>> {
Ok(std::fs::read(path)?)
}
/// Write `bytes` to `path` atomically: `<path>.tmp` first, then rename over the target.
/// Create a new temporary file beside `path`, refusing to reuse an existing (possibly symlinked) name.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn create_temp(path: &std::path::Path) -> Result<(std::path::PathBuf, std::fs::File)> {
let parent = path.parent().unwrap_or_else(|| std::path::Path::new("."));
let name = path.file_name().and_then(|name| name.to_str()).unwrap_or("out");
// Bound retries when stale files or concurrent writers occupy candidate names
const MAX_TEMP_ATTEMPTS: u32 = 100;
for attempt in 0..MAX_TEMP_ATTEMPTS {
let temporary_path = parent.join(format!(".{name}.{}.{attempt}.tmp", std::process::id()));
match std::fs::OpenOptions::new().write(true).create_new(true).open(&temporary_path) {
Ok(file) => return Ok((temporary_path, file)),
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(error) => return Err(error.into()),
}
}
Err(FormatError::Io(std::io::Error::new(std::io::ErrorKind::AlreadyExists, "could not create a temporary file")))
}
/// Write `bytes` to `path` atomically: a new temporary file first, then rename over the target.
#[cfg(not(target_arch = "wasm32"))]
pub fn write_atomic(path: &std::path::Path, bytes: &[u8]) -> Result<()> {
let mut tmp = path.as_os_str().to_owned();
tmp.push(".tmp");
let tmp = std::path::PathBuf::from(tmp);
let res = std::fs::write(&tmp, bytes).and_then(|_| std::fs::rename(&tmp, path));
if res.is_err() {
let _ = std::fs::remove_file(&tmp);
use std::io::Write;
let (temporary_path, mut file) = create_temp(path)?;
let result = file.write_all(bytes).and_then(|_| file.sync_all());
drop(file);
let result = result.and_then(|_| std::fs::rename(&temporary_path, path));
if result.is_err() {
let _ = std::fs::remove_file(&temporary_path);
}
Ok(result?)
}
#[cfg(all(test, unix))]
mod atomic_tests {
use super::write_atomic;
#[test]
fn write_atomic_does_not_follow_a_planted_temp_symlink() -> std::io::Result<()> {
let directory = std::env::temp_dir().join(format!("tc-atomic-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&directory);
std::fs::create_dir_all(&directory)?;
let target = directory.join("out.png");
let victim = directory.join("victim.txt");
std::fs::write(&victim, b"safe")?;
std::os::unix::fs::symlink(&victim, directory.join("out.png.tmp"))?;
write_atomic(&target, b"new").map_err(std::io::Error::other)?;
assert_eq!(std::fs::read(&victim)?, b"safe");
assert_eq!(std::fs::read(&target)?, b"new");
let _ = std::fs::remove_dir_all(&directory);
Ok(())
}
Ok(res?)
}
+20 -123
View File
@@ -1,20 +1,7 @@
//! MaterialX documents for exported texture sets, written from the ASWF MaterialX specification
//! (version 1.39) and the OpenPBR Surface specification. Pure Rust: no MaterialX library is
//! linked, and nothing is read from any other application.
//! TextureCraft's MaterialX export adapter.
//!
//! [`write`] takes a material's name and the texture files an export produced, each tagged with
//! the OpenPBR input it feeds, and gives the text of a `.mtlx` file: an `open_pbr_surface` shader
//! whose inputs read `tiledimage` nodes, wrapped in a `surfacematerial`. The inputs are the ones
//! the built-in "OpenPBR (separate maps)" export preset writes ([`INPUTS`]); `height` becomes a
//! `displacement` shader on the material and `geometry_normal` goes through a `normalmap` node
//! (OpenGL, +Y up, which is what that node expects).
//!
//! Colour textures (base, fuzz and emission colour) are tagged with the colour space they were
//! exported in ([`colorspace_name`]); every data map is left untagged (linear). UDIM sets write the `<UDIM>` filename token MaterialX defines.
//! Texture file names are relative to the document.
use std::collections::HashSet;
use std::fmt::Write as _;
//! The shared `craft-materialx` crate owns the MaterialX document writer. This module maps the
//! built-in preset's map names and TextureCraft color spaces to that format-level API.
use texturecraft_texel::ColorSpace;
@@ -51,7 +38,6 @@ pub const INPUTS: &[(&str, Kind)] = &[
("height", Kind::Height),
];
/// The file-name prefix of the built-in OpenPBR preset's maps.
const PREFIX: &str = "$mesh_$textureSet_";
/// One exported texture: the OpenPBR input it feeds and the file (relative to the `.mtlx`).
@@ -59,14 +45,11 @@ const PREFIX: &str = "$mesh_$textureSet_";
pub struct Texture {
pub input: String,
pub file: String,
/// The MaterialX colour space of the file's values (`srgb_texture`, `lin_rec709`, ...);
/// used for colour inputs only, data maps are always linear.
/// The MaterialX colour space of the file's values; used for color inputs only.
pub colorspace: &'static str,
}
/// The MaterialX colour space of colour maps written in `space`. MaterialX 1.39 defines no
/// ACES2065-1 (AP0) space: it gets `lin_ap0_scene`, its name in later versions of the
/// specification, which 1.39 readers may not know.
/// The MaterialX colour space of colour maps written in `space`.
pub fn colorspace_name(space: ColorSpace) -> &'static str {
match space {
ColorSpace::Srgb => "srgb_texture",
@@ -76,116 +59,28 @@ pub fn colorspace_name(space: ColorSpace) -> &'static str {
}
}
/// The OpenPBR input a preset map's file template feeds (`$mesh_$textureSet_base_color` →
/// `base_color`), or None for templates this module has no place for.
/// The OpenPBR input a preset map's file template feeds, or None for unsupported templates.
pub fn input_of_template(template: &str) -> Option<&'static str> {
let suffix = template.strip_prefix(PREFIX)?;
INPUTS.iter().find(|(n, _)| *n == suffix).map(|(n, _)| *n)
}
fn kind_of(input: &str) -> Option<Kind> {
INPUTS.iter().find(|(n, _)| *n == input).map(|(_, k)| *k)
INPUTS.iter().find(|(name, _)| *name == suffix).map(|(name, _)| *name)
}
/// A MaterialX name: letters, digits and underscores, not starting with a digit.
pub fn node_name(s: &str) -> String {
let mut out: String = s.chars().map(|c| if c.is_ascii_alphanumeric() || c == '_' { c } else { '_' }).collect();
pub fn node_name(name: &str) -> String {
let mut out: String = name.chars().map(|c| if c.is_ascii_alphanumeric() || c == '_' { c } else { '_' }).collect();
if out.is_empty() || out.starts_with(|c: char| c.is_ascii_digit()) {
out.insert(0, '_');
}
out
}
fn esc(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
match c {
'&' => out.push_str("&amp;"),
'<' => out.push_str("&lt;"),
'>' => out.push_str("&gt;"),
'"' => out.push_str("&quot;"),
'\'' => out.push_str("&apos;"),
c if c.is_control() => out.push(' '),
c => out.push(c),
}
}
out
}
fn image_name(input: &str) -> String {
format!("image_{}", node_name(input))
}
/// The text of a MaterialX document for `name` with the given textures. `physical_size_cm` goes
/// into the material's `doc` attribute. Inputs this module has no place for are skipped, and so
/// are repeats of one input (the first wins).
/// The text of a MaterialX OpenPBR document for `name` and the given texture files.
pub fn write(name: &str, textures: &[Texture], physical_size_cm: Option<[f32; 2]>) -> String {
let base = node_name(name);
let mut seen = HashSet::new();
let tex: Vec<(&Texture, Kind)> = textures.iter().filter_map(|t| kind_of(&t.input).map(|k| (t, k))).filter(|(t, _)| seen.insert(t.input.as_str())).collect();
let has = |i: &str| tex.iter().any(|(t, _)| t.input == i);
let mut x = String::new();
let _ = writeln!(x, "<?xml version=\"1.0\"?>");
let _ = writeln!(x, "<materialx version=\"1.39\" colorspace=\"lin_rec709\">");
let shader = format!("OPBR_{base}");
let _ = writeln!(x, " <open_pbr_surface name=\"{}\" type=\"surfaceshader\">", esc(&shader));
for (t, kind) in &tex {
let (ty, node) = match kind {
Kind::Color => ("color3", image_name(&t.input)),
Kind::Float => ("float", image_name(&t.input)),
Kind::Normal => ("vector3", "normalmap".to_owned()),
Kind::Height => continue,
};
let _ = writeln!(x, " <input name=\"{}\" type=\"{ty}\" nodename=\"{node}\" />", t.input);
}
if has("emission_color") {
let _ = writeln!(x, " <input name=\"emission_luminance\" type=\"float\" value=\"1\" />");
}
let _ = writeln!(x, " </open_pbr_surface>");
for (t, kind) in &tex {
let (ty, space) = match kind {
Kind::Color => ("color3", Some(t.colorspace)),
Kind::Normal => ("vector3", None),
Kind::Float | Kind::Height => ("float", None),
};
let node = image_name(&t.input);
let _ = writeln!(x, " <tiledimage name=\"{node}\" type=\"{ty}\">");
match space {
Some(space) => {
let _ = writeln!(x, " <input name=\"file\" type=\"filename\" value=\"{}\" colorspace=\"{space}\" />", esc(&t.file));
}
None => {
let _ = writeln!(x, " <input name=\"file\" type=\"filename\" value=\"{}\" />", esc(&t.file));
}
}
let _ = writeln!(x, " </tiledimage>");
}
if has("geometry_normal") {
let _ = writeln!(x, " <normalmap name=\"normalmap\" type=\"vector3\">");
let _ = writeln!(x, " <input name=\"in\" type=\"vector3\" nodename=\"{}\" />", image_name("geometry_normal"));
let _ = writeln!(x, " </normalmap>");
}
if has("height") {
let _ = writeln!(x, " <displacement name=\"DISP_{base}\" type=\"displacementshader\">");
let _ = writeln!(x, " <input name=\"displacement\" type=\"float\" nodename=\"{}\" />", image_name("height"));
let _ = writeln!(x, " <input name=\"scale\" type=\"float\" value=\"1\" />");
let _ = writeln!(x, " </displacement>");
}
let doc = match physical_size_cm {
Some([w, h]) if w.is_finite() && h.is_finite() => format!(" doc=\"Tiles every {w} x {h} cm\""),
_ => String::new(),
};
let _ = writeln!(x, " <surfacematerial name=\"{}\" type=\"material\"{doc}>", esc(&base));
let _ = writeln!(x, " <input name=\"surfaceshader\" type=\"surfaceshader\" nodename=\"{}\" />", esc(&shader));
if has("height") {
let _ = writeln!(x, " <input name=\"displacementshader\" type=\"displacementshader\" nodename=\"DISP_{base}\" />");
}
let _ = writeln!(x, " </surfacematerial>");
let _ = writeln!(x, "</materialx>");
x
let textures: Vec<craft_materialx::Texture> = textures
.iter()
.map(|texture| craft_materialx::Texture { input: texture.input.clone(), file: texture.file.clone(), colorspace: texture.colorspace.to_owned() })
.collect();
craft_materialx::write_open_pbr(name, &textures, physical_size_cm)
}
#[cfg(test)]
@@ -247,7 +142,7 @@ mod tests {
assert!(x.contains(want), "missing {want}\n{x}");
}
// Data textures carry no colour space, and height is not an OpenPBR input.
assert!(x.contains("value=\"stone_specular_roughness.png\" />"));
assert!(x.contains("value=\"stone_specular_roughness.png\"/>"));
assert!(!x.contains("name=\"height\""));
}
@@ -265,7 +160,7 @@ mod tests {
fn colour_inputs_carry_their_files_space_and_data_inputs_none() {
let lin = |input: &str, file: &str| Texture { colorspace: "lin_rec709", ..tex(input, file) };
let x = write("m", &[lin("base_color", "c.exr"), lin("specular_roughness", "r.exr")], None);
assert!(x.contains("value=\"c.exr\" colorspace=\"lin_rec709\"") && x.contains("value=\"r.exr\" />"), "{x}");
assert!(x.contains("value=\"c.exr\" colorspace=\"lin_rec709\"") && x.contains("value=\"r.exr\"/>"), "{x}");
}
#[test]
@@ -292,7 +187,9 @@ mod tests {
assert_eq!(node_name(""), "_");
let x = write("a<b>&\"c", &[tex("base_color", "x\"&<'>.<UDIM>.png")], Some([f32::NAN, 1.0]));
assert!(balanced(&x.replace("&lt;UDIM&gt;", "UDIM")), "{x}");
assert!(x.contains("value=\"x&quot;&amp;&lt;&apos;&gt;.&lt;UDIM&gt;.png\""));
let doc = craft_materialx::Document::parse(x.as_bytes()).unwrap();
let material = doc.read_material(None).unwrap();
assert!(material.inputs.iter().any(|(input, value)| input == "base_color" && value == &craft_materialx::Value::Image("x\"&<'>.<UDIM>.png".into())));
assert!(!x.contains("doc="), "a non-finite size is left out");
assert!(x.contains("OPBR_a_b___c"));
}
+90 -350
View File
@@ -1,41 +1,16 @@
//! Reading a MaterialX (`.mtlx`) material into TextureCraft's channels, written from the ASWF
//! MaterialX specification (1.39) and the OpenPBR Surface and glTF PBR node definitions. Pure
//! Rust: no MaterialX library is linked.
//! MaterialX material import adapted to TextureCraft's channel model.
//!
//! [`read_materialx`] finds a `surfacematerial`, follows its `surfaceshader` to an
//! `open_pbr_surface`, `standard_surface` or `gltf_pbr` node, and turns each input TextureCraft
//! has a channel for into a literal value or an image file:
//!
//! - a literal `value` becomes a constant (`color3` values tagged `srgb_texture` are converted
//! to linear; a colour given for a scalar channel is averaged);
//! - an input wired to a `tiledimage` or `image` node (directly or through a `nodegraph`
//! output) becomes that node's `file` (with `fileprefix` applied); `normalmap`, `convert` and
//! `constant` nodes in between are passed through;
//! - the material's `displacementshader` becomes the height channel;
//! - `uvtiling` of the first tiled image is returned as the UV tiling.
//!
//! Anything else (maths nodes, procedural patterns, other shaders) is skipped with a warning
//! rather than guessed at. Emission follows the specifications' defaults: OpenPBR emits nothing
//! unless `emission_luminance` is above 0, and `standard_surface` unless `emission` is.
//!
//! The reader takes untrusted bytes: the document size, nesting and element count are capped, a
//! wiring loop stops at a depth limit, and every failure is an error, never a panic.
//! XML parsing, nodegraph traversal and shader input resolution live in `craft-materialx`.
//! This module maps those format-level values to TextureCraft's channels and color handling.
use std::collections::HashMap;
use quick_xml::events::{BytesStart, Event};
use texturecraft_core::Channel;
use texturecraft_core::{Channel, ChannelKind};
use texturecraft_texel::srgb_to_linear;
use crate::{FormatError, Result, malformed};
const KIND: &str = "MaterialX document";
/// Largest document read, in bytes.
pub const MAX_BYTES: usize = 16 << 20;
const MAX_DEPTH: usize = 64;
const MAX_ELEMENTS: usize = 200_000;
/// Longest chain of nodes followed from one shader input.
const MAX_HOPS: usize = 16;
pub const MAX_BYTES: usize = craft_materialx::MAX_BYTES;
/// What one channel is set from.
#[derive(Clone, Debug, PartialEq)]
@@ -58,259 +33,40 @@ pub struct Material {
pub warnings: Vec<String>,
}
// ---------------------------------------------------------------------------------------------
// XML.
#[derive(Default)]
struct El {
name: String,
attrs: Vec<(String, String)>,
children: Vec<El>,
fn channel(shader: craft_materialx::Shader, input: &str) -> Option<Channel> {
use craft_materialx::Shader::{GltfPbr, OpenPbr, StandardSurface};
Some(match (shader, input) {
(OpenPbr, "base_color") | (StandardSurface, "base_color") | (GltfPbr, "base_color") => Channel::BaseColor,
(OpenPbr, "base_metalness") | (StandardSurface, "metalness") | (GltfPbr, "metallic") => Channel::Metallic,
(OpenPbr, "specular_roughness") | (StandardSurface, "specular_roughness") | (GltfPbr, "roughness") => Channel::Roughness,
(OpenPbr, "geometry_normal") | (StandardSurface, "normal") | (GltfPbr, "normal") => Channel::Normal,
(OpenPbr, "geometry_opacity") | (StandardSurface, "opacity") | (GltfPbr, "alpha") => Channel::Opacity,
(OpenPbr, "emission_color") | (StandardSurface, "emission_color") | (GltfPbr, "emission") => Channel::Emissive,
(OpenPbr, "coat_weight") | (StandardSurface, "coat") | (GltfPbr, "clearcoat") => Channel::CoatWeight,
(OpenPbr, "coat_roughness") | (StandardSurface, "coat_roughness") | (GltfPbr, "clearcoat_roughness") => Channel::CoatRoughness,
(OpenPbr, "geometry_coat_normal") | (StandardSurface, "coat_normal") | (GltfPbr, "clearcoat_normal") => Channel::CoatNormal,
(OpenPbr, "fuzz_weight") | (StandardSurface, "sheen") => Channel::SheenWeight,
(OpenPbr, "fuzz_color") | (StandardSurface, "sheen_color") | (GltfPbr, "sheen_color") => Channel::SheenColor,
(OpenPbr, "fuzz_roughness") | (StandardSurface, "sheen_roughness") | (GltfPbr, "sheen_roughness") => Channel::SheenRoughness,
(OpenPbr, "specular_roughness_anisotropy") | (StandardSurface, "specular_anisotropy") => Channel::AnisotropyLevel,
(OpenPbr, "transmission_weight") | (StandardSurface, "transmission") | (GltfPbr, "transmission") => Channel::Transmission,
(OpenPbr, "subsurface_weight") | (StandardSurface, "subsurface") => Channel::ScatterWeight,
(GltfPbr, "occlusion") => Channel::AmbientOcclusion,
_ => return None,
})
}
impl El {
fn attr(&self, k: &str) -> Option<&str> {
self.attrs.iter().find(|(n, _)| n == k).map(|(_, v)| v.as_str())
fn constant(channel: Channel, values: &[f32], colorspace: Option<&str>) -> [f32; 3] {
let mut values = values.to_vec();
if let Some(space) = colorspace
&& (space.starts_with("srgb") || space.starts_with("g22") || space.starts_with("g18"))
&& values.len() >= 3
{
values.iter_mut().take(3).for_each(|c| *c = srgb_to_linear(*c));
}
/// The child element named `name` (the `name` attribute: inputs and nodes are looked up by it).
fn input(&self, name: &str) -> Option<&El> {
self.children.iter().find(|c| c.attr("name") == Some(name))
}
}
fn element(e: &BytesStart) -> El {
let text = |b: &[u8]| String::from_utf8_lossy(b).into_owned();
let attrs = e
.attributes()
.with_checks(false)
.flatten()
.map(|a| {
(text(a.key.local_name().as_ref()), a.normalized_value(quick_xml::XmlVersion::Implicit1_0).map_or_else(|_| text(&a.value), |v| v.into_owned()))
})
.collect();
El { name: text(e.local_name().as_ref()), attrs, children: Vec::new() }
}
fn parse_xml(bytes: &[u8]) -> Result<El> {
let mut reader = quick_xml::Reader::from_reader(bytes);
let mut stack = vec![El::default()];
let mut count = 0usize;
loop {
let event = reader.read_event().map_err(|e| malformed(KIND, e))?;
if matches!(event, Event::Start(_) | Event::Empty(_)) {
count += 1;
if count > MAX_ELEMENTS || stack.len() > MAX_DEPTH {
return Err(FormatError::TooLarge(format!(
"the MaterialX document has more than {MAX_ELEMENTS} elements or is nested deeper than {MAX_DEPTH}"
)));
}
}
match event {
Event::Start(e) => stack.push(element(&e)),
Event::Empty(e) => {
if let Some(parent) = stack.last_mut() {
parent.children.push(element(&e));
}
}
Event::End(_) => {
let el = stack.pop().filter(|_| !stack.is_empty()).ok_or_else(|| malformed(KIND, "unbalanced end tag"))?;
if let Some(parent) = stack.last_mut() {
parent.children.push(el);
}
}
Event::Eof => break,
_ => {}
}
}
match (stack.pop(), stack.is_empty()) {
(Some(root), true) => Ok(root),
_ => Err(malformed(KIND, "unclosed elements at the end of the document")),
}
}
// ---------------------------------------------------------------------------------------------
// Shader input tables: (shader input, channel). `kind` says how to read the value.
#[derive(Clone, Copy, PartialEq)]
enum Shader {
OpenPbr,
Standard,
Gltf,
}
const OPEN_PBR: &[(&str, Channel)] = &[
("base_color", Channel::BaseColor),
("base_metalness", Channel::Metallic),
("specular_roughness", Channel::Roughness),
("geometry_normal", Channel::Normal),
("geometry_opacity", Channel::Opacity),
("emission_color", Channel::Emissive),
("coat_weight", Channel::CoatWeight),
("coat_roughness", Channel::CoatRoughness),
("geometry_coat_normal", Channel::CoatNormal),
("fuzz_weight", Channel::SheenWeight),
("fuzz_color", Channel::SheenColor),
("fuzz_roughness", Channel::SheenRoughness),
("specular_roughness_anisotropy", Channel::AnisotropyLevel),
("transmission_weight", Channel::Transmission),
("subsurface_weight", Channel::ScatterWeight),
];
const STANDARD: &[(&str, Channel)] = &[
("base_color", Channel::BaseColor),
("metalness", Channel::Metallic),
("specular_roughness", Channel::Roughness),
("normal", Channel::Normal),
("opacity", Channel::Opacity),
("emission_color", Channel::Emissive),
("coat", Channel::CoatWeight),
("coat_roughness", Channel::CoatRoughness),
("coat_normal", Channel::CoatNormal),
("sheen", Channel::SheenWeight),
("sheen_color", Channel::SheenColor),
("sheen_roughness", Channel::SheenRoughness),
("specular_anisotropy", Channel::AnisotropyLevel),
("transmission", Channel::Transmission),
("subsurface", Channel::ScatterWeight),
];
const GLTF: &[(&str, Channel)] = &[
("base_color", Channel::BaseColor),
("metallic", Channel::Metallic),
("roughness", Channel::Roughness),
("normal", Channel::Normal),
("alpha", Channel::Opacity),
("emission", Channel::Emissive),
("occlusion", Channel::AmbientOcclusion),
("clearcoat", Channel::CoatWeight),
("clearcoat_roughness", Channel::CoatRoughness),
("clearcoat_normal", Channel::CoatNormal),
("sheen_color", Channel::SheenColor),
("sheen_roughness", Channel::SheenRoughness),
("transmission", Channel::Transmission),
];
// ---------------------------------------------------------------------------------------------
// Resolving what feeds an input.
struct Doc<'a> {
root: &'a El,
/// Every node by name: the document's own, and each nodegraph's (qualified `graph/node`).
nodes: HashMap<String, &'a El>,
/// Each nodegraph's outputs: `graph/output` to the node name inside (qualified).
outputs: HashMap<String, String>,
tiling: Option<[f32; 2]>,
warnings: Vec<String>,
}
enum Found {
Constant(Vec<f32>),
Image(String),
None,
}
fn numbers(text: &str) -> Option<Vec<f32>> {
let v: Option<Vec<f32>> = text.split(',').map(|p| p.trim().parse::<f32>().ok().filter(|f| f.is_finite())).collect();
v.filter(|v| !v.is_empty() && v.len() <= 4)
}
impl<'a> Doc<'a> {
fn new(root: &'a El) -> Doc<'a> {
let mut nodes = HashMap::new();
let mut outputs = HashMap::new();
for c in &root.children {
match (c.name.as_str(), c.attr("name")) {
("nodegraph", Some(g)) => {
for n in &c.children {
let Some(name) = n.attr("name") else { continue };
if n.name == "output" {
if let Some(from) = n.attr("nodename") {
outputs.insert(format!("{g}/{name}"), format!("{g}/{from}"));
}
} else {
nodes.insert(format!("{g}/{name}"), n);
}
}
}
(_, Some(name)) => {
nodes.insert(name.to_owned(), c);
}
_ => {}
}
}
Doc { root, nodes, outputs, tiling: None, warnings: Vec::new() }
}
/// What `input` (an `<input>` element of a node) resolves to; `scope` is the nodegraph the
/// node lives in, if any.
fn resolve(&mut self, input: &El, scope: Option<&str>, hops: usize) -> Found {
if hops > MAX_HOPS {
self.warnings.push("a node chain is longer than 16 nodes (or loops); skipped".into());
return Found::None;
}
if let Some(v) = input.attr("value") {
return numbers(v).map_or(Found::None, |n| Found::Constant(self.tag(input, n)));
}
let target = match (input.attr("nodegraph"), input.attr("output"), input.attr("nodename")) {
(Some(g), out, _) => {
let out = out.map(str::to_owned).or_else(|| self.outputs.keys().find_map(|k| k.strip_prefix(&format!("{g}/")).map(str::to_owned)));
out.and_then(|o| self.outputs.get(&format!("{g}/{o}")).cloned())
}
(None, _, Some(n)) => {
Some(scope.map_or_else(|| n.to_owned(), |g| format!("{g}/{n}"))).filter(|q| self.nodes.contains_key(q)).or_else(|| Some(n.to_owned()))
}
_ => None,
};
let Some(node) = target.as_deref().and_then(|t| self.nodes.get(t).copied()) else { return Found::None };
let scope = target.as_deref().and_then(|t| t.split_once('/')).map(|(g, _)| g.to_owned());
let scope = scope.as_deref();
match node.name.as_str() {
"tiledimage" | "image" => {
let Some(file) = node.input("file").and_then(|f| f.attr("value")) else {
self.warnings.push(format!("image node {:?} has no file", node.attr("name").unwrap_or("")));
return Found::None;
};
let prefix =
[node.input("file").and_then(|f| f.attr("fileprefix")), node.attr("fileprefix"), self.root.attr("fileprefix")].into_iter().flatten().next();
if self.tiling.is_none()
&& let Some(t) =
node.input("uvtiling").and_then(|t| t.attr("value")).and_then(numbers).filter(|t| t.len() == 2 && t.iter().all(|v| *v > 0.0))
{
// The range a fill's UV scale accepts (`layer.fill.projection`).
self.tiling = Some([t[0].clamp(0.001, 1000.0), t[1].clamp(0.001, 1000.0)]);
}
Found::Image(format!("{}{file}", prefix.unwrap_or("")))
}
"normalmap" | "convert" => match node.input("in") {
Some(i) => self.resolve(i, scope, hops + 1),
None => Found::None,
},
"constant" => match node.input("value") {
Some(v) => self.resolve(v, scope, hops + 1),
None => Found::None,
},
other => {
self.warnings.push(format!("a {other} node ({:?}) is not supported; its input was skipped", node.attr("name").unwrap_or("")));
Found::None
}
}
}
/// Literal values tagged with an sRGB colour space are converted to linear.
fn tag(&self, input: &El, mut n: Vec<f32>) -> Vec<f32> {
let space = input.attr("colorspace").or_else(|| self.root.attr("colorspace")).unwrap_or("");
if input.attr("colorspace").is_some() && (space.starts_with("srgb") || space.starts_with("g22") || space.starts_with("g18")) && n.len() >= 3 {
n.iter_mut().take(3).for_each(|c| *c = srgb_to_linear(*c));
}
n
}
}
fn to_constant(channel: Channel, n: &[f32]) -> [f32; 3] {
match (channel.kind(), n) {
match (channel.kind(), values.as_slice()) {
(_, [a]) => [*a; 3],
(texturecraft_core::ChannelKind::Color, [r, g, b, ..]) => [*r, *g, *b],
(ChannelKind::Color, [r, g, b, ..]) => [*r, *g, *b],
(_, [r, g, b, ..]) => {
let mean = (r + g + b) / 3.0;
[mean; 3]
@@ -319,9 +75,11 @@ fn to_constant(channel: Channel, n: &[f32]) -> [f32; 3] {
}
}
/// The literal number of `input`, if it is one.
fn literal(shader: &El, name: &str) -> Option<f32> {
shader.input(name).and_then(|i| i.attr("value")).and_then(numbers).and_then(|n| n.first().copied())
fn app_value(channel: Channel, value: craft_materialx::Value) -> Value {
match value {
craft_materialx::Value::Constant { values, colorspace } => Value::Constant(constant(channel, &values, colorspace.as_deref())),
craft_materialx::Value::Image(file) => Value::Image(file),
}
}
/// Read the material called `material` (default: the first) from a `.mtlx` document.
@@ -330,79 +88,30 @@ pub fn read_materialx(bytes: &[u8], material: Option<&str>) -> Result<Material>
}
fn read_inner(bytes: &[u8], material: Option<&str>) -> Result<Material> {
if bytes.len() > MAX_BYTES {
return Err(FormatError::TooLarge(format!("the MaterialX document is over {} MiB", MAX_BYTES >> 20)));
}
let top = parse_xml(bytes)?;
let root = top.children.iter().find(|c| c.name == "materialx").ok_or_else(|| malformed(KIND, "no <materialx> element"))?;
let mut doc = Doc::new(root);
let materials: Vec<&El> = root.children.iter().filter(|c| c.name == "surfacematerial").collect();
let mat = match material {
Some(name) => materials
.iter()
.copied()
.find(|m| m.attr("name") == Some(name))
.ok_or_else(|| FormatError::Invalid(format!("no material named {name:?} (found: {})", names(&materials))))?,
None => materials.first().copied().ok_or_else(|| FormatError::Unsupported("the document has no surfacematerial".into()))?,
};
let mut out = Material { name: mat.attr("name").unwrap_or("Material").to_owned(), channels: Vec::new(), uv_tiling: [1.0, 1.0], warnings: Vec::new() };
let shader = mat
.input("surfaceshader")
.and_then(|i| i.attr("nodename"))
.and_then(|n| doc.nodes.get(n).copied())
.ok_or_else(|| malformed(KIND, "the material has no surface shader"))?;
let (kind, table) = match shader.name.as_str() {
"open_pbr_surface" => (Shader::OpenPbr, OPEN_PBR),
"standard_surface" => (Shader::Standard, STANDARD),
"gltf_pbr" => (Shader::Gltf, GLTF),
other => return Err(FormatError::Unsupported(format!("the surface shader is a {other}; open_pbr_surface, standard_surface and gltf_pbr are read"))),
};
// Emission is off by default in OpenPBR and standard_surface.
let emits = match kind {
Shader::OpenPbr => {
shader.input("emission_luminance").is_some_and(|i| i.attr("value").is_none() || literal(shader, "emission_luminance").is_some_and(|v| v > 0.0))
}
Shader::Standard => shader.input("emission").is_some_and(|i| i.attr("value").is_none() || literal(shader, "emission").is_some_and(|v| v > 0.0)),
Shader::Gltf => true,
};
let weight = match kind {
Shader::OpenPbr => literal(shader, "base_weight"),
Shader::Standard => literal(shader, "base"),
Shader::Gltf => None,
};
if weight.is_some_and(|w| (w - 1.0).abs() > 1e-6) {
doc.warnings.push("the base weight isn't 1; it was ignored".into());
}
for &(input, channel) in table {
let Some(el) = shader.input(input) else { continue };
if channel == Channel::Emissive && !emits && kind != Shader::Gltf {
doc.warnings.push(format!("{input} was skipped: the shader's emission is 0"));
continue;
}
match doc.resolve(el, None, 0) {
Found::Constant(n) => out.channels.push((channel, Value::Constant(to_constant(channel, &n)))),
Found::Image(f) => out.channels.push((channel, Value::Image(f))),
Found::None => {}
let document = craft_materialx::Document::parse(bytes).map_err(map_error)?;
let shared = document.read_material(material).map_err(map_error)?;
let mut channels = Vec::new();
for (input, value) in shared.inputs {
if let Some(channel) = channel(shared.shader, &input) {
channels.push((channel, app_value(channel, value)));
}
}
// The displacement shader's `displacement` input is the height.
if let Some(d) = mat.input("displacementshader").and_then(|i| i.attr("nodename")).and_then(|n| doc.nodes.get(n).copied())
&& let Some(i) = d.input("displacement")
{
match doc.resolve(i, None, 0) {
Found::Constant(n) => out.channels.push((Channel::Height, Value::Constant(to_constant(Channel::Height, &n)))),
Found::Image(f) => out.channels.push((Channel::Height, Value::Image(f))),
Found::None => {}
}
if let Some(value) = shared.displacement {
channels.push((Channel::Height, app_value(Channel::Height, value)));
}
out.uv_tiling = doc.tiling.unwrap_or([1.0, 1.0]);
out.warnings = doc.warnings;
Ok(out)
Ok(Material { name: shared.name, channels, uv_tiling: shared.uv_tiling, warnings: shared.warnings })
}
fn names(materials: &[&El]) -> String {
let n: Vec<&str> = materials.iter().filter_map(|m| m.attr("name")).collect();
if n.is_empty() { "none".into() } else { n.join(", ") }
fn map_error(error: craft_materialx::Error) -> FormatError {
use craft_materialx::Error;
match error {
Error::Xml(xml) if xml.is_limit() => FormatError::TooLarge(xml.to_string()),
Error::MissingMaterial(name) => FormatError::Invalid(format!("no material named {name:?}")),
Error::Unsupported(message) => FormatError::Unsupported(message),
Error::Malformed(message) => malformed(KIND, message),
Error::Root => malformed(KIND, "no <materialx> element"),
Error::Xml(other) => malformed(KIND, other),
}
}
#[cfg(test)]
@@ -439,6 +148,19 @@ mod tests {
m.channels.iter().find(|(ch, _)| *ch == c).map(|(_, v)| v)
}
#[test]
fn displacement_only_image_supplies_uv_tiling() {
let xml = br#"<materialx>
<open_pbr_surface name="S"><input name="base_color" value="0.3,0.4,0.5"/></open_pbr_surface>
<tiledimage name="H"><input name="file" value="height.png"/><input name="uvtiling" value="4,2"/></tiledimage>
<displacement name="D"><input name="displacement" nodename="H"/></displacement>
<surfacematerial name="M"><input name="surfaceshader" nodename="S"/><input name="displacementshader" nodename="D"/></surfacematerial>
</materialx>"#;
let material = read_materialx(xml, None).unwrap();
assert_eq!(get(&material, Channel::Height), Some(&Value::Image("height.png".into())));
assert_eq!(material.uv_tiling, [4.0, 2.0]);
}
#[test]
fn open_pbr_inputs_become_channels() {
let m = read_materialx(OPENPBR.as_bytes(), None).unwrap();
@@ -456,6 +178,24 @@ mod tests {
assert_eq!(read_materialx(extreme.as_bytes(), None).unwrap().uv_tiling, [1000.0, 0.001], "clamped like a fill's UV scale");
}
#[test]
fn nodegraph_input_without_output_uses_the_first_output() {
let xml = r#"<materialx version="1.38">
<nodegraph name="NG">
<tiledimage name="a" type="color3"><input name="file" type="filename" value="first.png" /></tiledimage>
<tiledimage name="b" type="color3"><input name="file" type="filename" value="second.png" /></tiledimage>
<output name="o1" type="color3" nodename="a" />
<output name="o2" type="color3" nodename="b" />
</nodegraph>
<standard_surface name="S" type="surfaceshader"><input name="base_color" type="color3" nodegraph="NG" /></standard_surface>
<surfacematerial name="M" type="material"><input name="surfaceshader" type="surfaceshader" nodename="S" /></surfacematerial>
</materialx>"#;
for _ in 0..50 {
let material = read_materialx(xml.as_bytes(), None).unwrap();
assert_eq!(get(&material, Channel::BaseColor), Some(&Value::Image("first.png".into())));
}
}
#[test]
fn emission_is_off_unless_the_shader_emits() {
let x = OPENPBR.replace(r#"<input name="emission_luminance" type="float" value="2" />"#, "");
+117 -44
View File
@@ -1,11 +1,11 @@
//! 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).
//! (8-bit sRGB for color usages, 16-bit linear otherwise; OpenGL normals) and the
//! source package. The shared reader handles the portable container and manifest.
//!
//! Also the 16-bit PNG encoder that hands baked mesh maps to MaterialCraft as graph inputs.
use std::io::{Cursor, Read};
use std::io::Cursor;
use serde::Deserialize;
use texturecraft_core::{LinkParam, LinkPreset};
@@ -16,10 +16,13 @@ use crate::{FormatError, Result};
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;
const VERSION: u32 = 2;
/// Largest entry read (maps are at most 8192² × 16 bits, compressed).
const MAX_ENTRY: u64 = 512 << 20;
/// Most map entries one manifest may name (a material has at most nine usages).
const MAX_MAPS: usize = 64;
/// Largest total payload read from one `.mcmat`: maps and source together (audit: TextureCraft #4).
const MAX_TOTAL: u64 = 1 << 30;
const KIND: &str = "MaterialCraft material";
#[derive(Clone, Debug, PartialEq, Deserialize)]
@@ -66,42 +69,76 @@ fn malformed(msg: impl Into<String>) -> FormatError {
FormatError::Malformed { kind: KIND, msg: msg.into() }
}
/// Read `.mcmat` bytes.
fn shared_error(error: craft_mcmat::Error) -> FormatError {
match error {
craft_mcmat::Error::Version(version) => FormatError::Unsupported(format!("{KIND} version {version} (this version reads up to {VERSION})")),
craft_mcmat::Error::Limit(message) => malformed(message),
craft_mcmat::Error::Archive(ref archive) if archive.is_limit() => FormatError::TooLarge(error.to_string()),
other => malformed(other.to_string()),
}
}
/// Read `.mcmat` bytes within TextureCraft's existing per-entry and total payload limits.
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 limits = craft_mcmat::Limits {
min_version: 0,
max_version: VERSION,
max_entries: 100_000,
max_maps: MAX_MAPS,
max_manifest_bytes: MAX_ENTRY,
max_entry_bytes: MAX_ENTRY,
max_total_bytes: MAX_TOTAL,
};
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)?;
let mut archive = craft_mcmat::Material::open(bytes, limits).map_err(shared_error)?;
// Consume the JSON values rather than cloning the entire untrusted manifest into a second tree.
let parameters = std::mem::take(&mut archive.manifest.parameters)
.into_iter()
.map(serde_json::from_value)
.collect::<std::result::Result<_, _>>()
.map_err(|e| malformed(format!("material.json: {e}")))?;
let inputs = std::mem::take(&mut archive.manifest.inputs)
.into_iter()
.map(serde_json::from_value)
.collect::<std::result::Result<_, _>>()
.map_err(|e| malformed(format!("material.json: {e}")))?;
let presets = std::mem::take(&mut archive.manifest.presets)
.into_iter()
.map(serde_json::from_value)
.collect::<std::result::Result<_, _>>()
.map_err(|e| malformed(format!("material.json: {e}")))?;
let shared = &archive.manifest;
let manifest = Manifest {
format: shared.format.clone(),
version: shared.version,
name: shared.name.clone(),
graph: shared.graph.clone(),
size: shared.size,
maps: shared.maps.iter().map(|m| MapEntry { usage: m.usage.clone(), file: m.file.clone() }).collect(),
parameters,
inputs,
presets,
source: shared.source.clone(),
};
let maps = manifest.maps.iter().map(|map| Ok((map.usage.clone(), archive.read(&map.file).map_err(shared_error)?))).collect::<Result<Vec<_>>>()?;
let source = archive.read_source().map_err(shared_error)?;
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())
}
/// Whether `bytes` contain a portable material manifest within the ZIP entry limit.
pub fn is_mcmat(bytes: &[u8]) -> bool {
zip_has(bytes, "material.json")
craft_mcmat::is_material(bytes, 100_000)
}
pub fn is_mcraft(bytes: &[u8]) -> bool {
zip_has(bytes, "package.json")
let Ok(mut archive) = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 100_000, max_total_bytes: 0 }) else { return false };
// A portable material may itself contain source.sbs, so it is not a source archive.
if archive.has_duplicate_names() || archive.contains("material.json") {
return false;
}
if archive.contains("package.json") {
return true;
}
(0..archive.len()).any(|i| archive.entry_name(i).ok().flatten().is_some_and(|name| !name.contains('/') && name.to_ascii_lowercase().ends_with(".sbs")))
}
/// A baked mesh map (`components` floats per texel, 0..1, rows top first) as a 16-bit PNG
@@ -131,13 +168,14 @@ mod tests {
/// A `.mcmat` as MaterialCraft writes it (see its `formats/src/material.rs`).
pub(crate) fn sample(version: u32) -> Vec<u8> {
let source = if version >= 2 { "source.sbs" } else { "source.mcraft" };
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
"source": source, "futureField": true
});
let mut z = zip::ZipWriter::new(Cursor::new(Vec::new()));
let o = zip::write::SimpleFileOptions::default();
@@ -145,7 +183,7 @@ mod tests {
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.start_file(source, o).unwrap();
z.write_all(b"pkg").unwrap();
z.finish().unwrap().into_inner()
}
@@ -153,20 +191,55 @@ mod tests {
#[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(_))));
let material = read_mcmat(&bytes).unwrap();
assert_eq!(material.manifest.graph, "Brick_wall");
assert_eq!(material.manifest.parameters[0].id, "wear");
assert_eq!(material.manifest.parameters[0].value, serde_json::json!({"t": "float", "v": 0.25}));
assert_eq!(material.manifest.presets[0].name, "Worn");
assert_eq!(material.manifest.inputs[0].identifier, "curvature");
assert_eq!(material.maps[0].0, "baseColor");
assert_eq!(crate::decode_image(&material.maps[0].1).unwrap().width, 4);
assert_eq!(material.source, b"pkg");
let current = read_mcmat(&sample(2)).unwrap();
assert_eq!(current.manifest.source, "source.sbs");
assert_eq!(current.source, b"pkg");
assert!(matches!(read_mcmat(&sample(3)), Err(FormatError::Unsupported(_))));
assert!(read_mcmat(b"junk").is_err());
}
#[test]
fn recognizes_both_generations_of_source_archive_without_confusing_portable_materials() {
for entry in ["package.json", "package.sbs"] {
let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
zip.start_file(entry, zip::write::SimpleFileOptions::default()).unwrap();
zip.write_all(b"source").unwrap();
let bytes = zip.finish().unwrap().into_inner();
assert!(is_mcraft(&bytes), "{entry}");
}
assert!(!is_mcraft(&sample(2)), "a portable material contains source.sbs too");
}
#[test]
fn an_unbounded_map_list_is_refused() {
let maps: Vec<_> = (0..=MAX_MAPS).map(|_| serde_json::json!({"usage": "baseColor", "file": "maps/baseColor.png"})).collect();
let manifest = serde_json::json!({"format": craft_mcmat::FORMAT, "version": VERSION, "name": "n", "graph": "g", "size": [4, 4], "maps": maps, "source": "source.mcraft"});
let mut archive = zip::ZipWriter::new(Cursor::new(Vec::new()));
let options = zip::write::SimpleFileOptions::default();
archive.start_file("material.json", options).unwrap();
archive.write_all(manifest.to_string().as_bytes()).unwrap();
archive.start_file("maps/baseColor.png", options).unwrap();
archive.write_all(&mesh_map_png(4, 3, &[0.5; 48]).unwrap()).unwrap();
archive.start_file("source.mcraft", options).unwrap();
archive.write_all(b"pkg").unwrap();
let result = read_mcmat(&archive.finish().unwrap().into_inner());
assert!(matches!(result, Err(FormatError::Malformed { .. })));
}
#[test]
fn mesh_maps_encode_as_16_bit() {
let png = mesh_map_png(2, 1, &[0.0, 0.25, 0.5, 1.0]).unwrap();
+17 -15
View File
@@ -154,7 +154,7 @@ impl Writer {
UsdValue::Ints(v) if v.len() >= codec::MIN_COMPRESSED_ARRAY => {
let at = self.offset();
self.out.extend_from_slice(&(v.len() as u64).to_le_bytes());
self.ints(&v.iter().map(|&i| i64::from(i)).collect::<Vec<_>>());
self.ints(&v.iter().map(|&i| i64::from(i)).collect::<Vec<_>>())?;
rep(ty::INT, codec::IS_ARRAY | codec::IS_COMPRESSED, at)
}
UsdValue::Ints(v) => self.raw_array(ty::INT, v.len(), v.iter().flat_map(|i| i.to_le_bytes())),
@@ -174,10 +174,11 @@ impl Writer {
}
/// Compressed integers: their compressed size, then the LZ4-framed coding.
fn ints(&mut self, v: &[i64]) {
let comp = codec::compress(&codec::encode_ints(v, 4));
fn ints(&mut self, v: &[i64]) -> Result<()> {
let comp = codec::compress(&codec::encode_ints(v, 4)?)?;
self.out.extend_from_slice(&(comp.len() as u64).to_le_bytes());
self.out.extend_from_slice(&comp);
Ok(())
}
fn spec(&mut self, path: usize, kind: i64, fields: Vec<(&str, u64)>) {
@@ -303,7 +304,7 @@ impl Writer {
self.out.extend_from_slice(&(v as u64).to_le_bytes());
}
fn sections(&mut self) {
fn sections(&mut self) -> Result<()> {
let mut toc = Vec::new();
let start = self.offset();
let mut raw = Vec::new();
@@ -311,7 +312,7 @@ impl Writer {
raw.extend_from_slice(t.as_bytes());
raw.push(0);
}
let comp = codec::compress(&raw);
let comp = codec::compress(&raw)?;
self.u64(self.tokens.len());
self.u64(raw.len());
self.u64(comp.len());
@@ -327,9 +328,9 @@ impl Writer {
let start = self.offset();
let fields = std::mem::take(&mut self.fields);
self.u64(fields.len());
self.ints(&fields.iter().map(|f| i64::from(f.0)).collect::<Vec<_>>());
self.ints(&fields.iter().map(|f| i64::from(f.0)).collect::<Vec<_>>())?;
let reps: Vec<u8> = fields.iter().flat_map(|f| f.1.to_le_bytes()).collect();
let comp = codec::compress(&reps);
let comp = codec::compress(&reps)?;
self.u64(comp.len());
self.out.extend_from_slice(&comp);
self.section(&mut toc, "FIELDS", start);
@@ -337,7 +338,7 @@ impl Writer {
let start = self.offset();
let sets = std::mem::take(&mut self.fieldsets);
self.u64(sets.len());
self.ints(&sets);
self.ints(&sets)?;
self.section(&mut toc, "FIELDSETS", start);
let start = self.offset();
@@ -345,7 +346,7 @@ impl Writer {
self.u64(self.paths.len());
self.u64(self.paths.len());
for column in &table {
self.ints(column);
self.ints(column)?;
}
self.section(&mut toc, "PATHS", start);
@@ -353,7 +354,7 @@ impl Writer {
let specs = std::mem::take(&mut self.specs);
self.u64(specs.len());
for k in 0..3 {
self.ints(&specs.iter().map(|s| s.get(k).copied().unwrap_or_default()).collect::<Vec<_>>());
self.ints(&specs.iter().map(|s| s.get(k).copied().unwrap_or_default()).collect::<Vec<_>>())?;
}
self.section(&mut toc, "SPECS", start);
@@ -371,6 +372,7 @@ impl Writer {
if let Some(slot) = self.out.get_mut(16..24) {
slot.copy_from_slice(&toc_at.to_le_bytes());
}
Ok(())
}
}
@@ -400,9 +402,9 @@ impl Writer {
Ok(())
}
fn finish(mut self) -> Vec<u8> {
self.sections();
self.out
fn finish(mut self) -> Result<Vec<u8>> {
self.sections()?;
Ok(self.out)
}
}
@@ -413,7 +415,7 @@ pub(crate) fn write(layer: &UsdLayer) -> Result<Vec<u8>> {
w.add_prim_paths(0, "/", root);
w.root_spec(&root.name)?;
w.prim_specs(&format!("/{}", root.name), root)?;
Ok(w.finish())
w.finish()
}
#[cfg(test)]
@@ -504,7 +506,7 @@ mod tests {
for (path, value) in attrs.into_iter().zip([points, counts, indices]) {
w.spec(path, SPEC_ATTRIBUTE, vec![("default", value)]);
}
let bytes = w.finish();
let bytes = w.finish().unwrap();
let m = crate::import_mesh_bytes("crafted.usdc", &bytes).unwrap().mesh;
assert_eq!(m.triangle_count(), 1, "the class prim itself is not drawn");
+20 -8
View File
@@ -217,7 +217,7 @@ impl<'a> Ogawa<'a> {
let value_bytes = match property.pod {
0..=2 => 1,
3 | 4 => 2,
3 | 4 | 9 => 2,
5 | 6 | 10 => 4,
7 | 8 | 11 => 8,
other => return Err(FormatError::Unsupported(format!("Alembic property \"{}\" of data type {other}", property.name))),
@@ -233,6 +233,7 @@ impl<'a> Ogawa<'a> {
6 => raw.as_chunks::<4>().0.iter().map(|c| i32::from_le_bytes(*c) as f64).collect(),
7 => raw.as_chunks::<8>().0.iter().map(|c| u64::from_le_bytes(*c) as f64).collect(),
8 => raw.as_chunks::<8>().0.iter().map(|c| i64::from_le_bytes(*c) as f64).collect(),
9 => raw.as_chunks::<2>().0.iter().map(|c| half::f16::from_bits(u16::from_le_bytes(*c)).to_f64()).collect(),
10 => raw.as_chunks::<4>().0.iter().map(|c| f32::from_le_bytes(*c) as f64).collect(),
_ => raw.as_chunks::<8>().0.iter().map(|c| f64::from_le_bytes(*c)).collect(),
})
@@ -280,19 +281,30 @@ fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
if root.len() < 6 || !(0..6).all(|i| is_data(i) != (i == 2)) {
return Err(malformed(KIND, "the archive group does not have the Alembic layout"));
}
// Indexed metadata: a u8 length before each string.
let block = o.data(root[5])?;
let mut c = Cursor { b: block, pos: 0 };
while c.pos < block.len() {
let n = c.u8()? as usize;
o.indexed_meta.push(c.string(n)?);
}
read_indexed_meta(block, &mut o.indexed_meta)?;
let mut sink = PolygonSink::default();
let mut walk = Walk { o, sink: &mut sink, visits: 0 };
walk.object(root[2], Mat4::IDENTITY, "", 0)?;
Ok(sink.finish())
}
/// The most indexed metadata entries that can ever be used: objects and properties refer to one
/// with a single byte, and 0xff already means "stored inline", so only 0 to 254 are reachable.
const MAX_INDEXED_META: usize = 255;
/// Indexed metadata: a u8 length before each string. `out` starts with the empty entry 0. Reading
/// stops once `out` holds [`MAX_INDEXED_META`] entries, because no later entry can be referred to
/// and an empty one costs a whole `String` in memory for one byte of file.
fn read_indexed_meta(block: &[u8], out: &mut Vec<String>) -> Result<()> {
let mut c = Cursor { b: block, pos: 0 };
while c.pos < block.len() && out.len() < MAX_INDEXED_META {
let n = c.u8()? as usize;
out.push(c.string(n)?);
}
Ok(())
}
struct Walk<'a, 'b> {
o: Ogawa<'a>,
sink: &'b mut PolygonSink,
@@ -542,7 +554,7 @@ impl Walk<'_, '_> {
Vec2::ZERO
}
});
corners.push(Corner { p: *p, n, uv });
corners.push(Corner { p: *p, n, uv, color: None });
}
k = end;
if !keep_order {
@@ -243,6 +243,38 @@ fn strip_alone_keeps_uvs_flipped_to_v_down() {
}
}
#[test]
fn half_float_uvs_import_without_losing_values() {
let mut a = Abc { w: Writer::new() };
let pp = a.array("P", "", F32, 3, &f32s(&[0.0, 0.0, 0.0, 0.5, 1.0, 0.0, 1.0, 0.0, 0.0]));
let fi = a.array(".faceIndices", "", I32, 1, &i32s(&[0, 1, 2]));
let fc = a.array(".faceCounts", "", I32, 1, &i32s(&[3]));
let uv_bytes: Vec<u8> = [0u16, 0, 0x3800, 0x3c00, 0x3c00, 0].iter().flat_map(|x| x.to_le_bytes()).collect();
let uv = a.array("uv", "", 9, 2, &uv_bytes);
let geom = a.compound(".geom", "schema=AbcGeom_PolyMesh_v1", vec![pp, fi, fc, uv]);
let mesh = a.object(vec![geom], &[]);
let top = a.object(vec![], &[("m", "", mesh)]);
let imported = crate::import_mesh_bytes("m.abc", &a.archive(top, &["schema=AbcGeom_PolyMesh_v1"])).unwrap();
assert!(!imported.report.uvs_generated);
for (p, uv) in imported.mesh.positions.iter().zip(&imported.mesh.uvs) {
assert_eq!(*uv, texturecraft_geom::glam::Vec2::new(p.x, 1.0 - p.y));
}
}
#[test]
fn half_float_values_preserve_special_values() {
let mut a = Abc { w: Writer::new() };
let raw: Vec<u8> = [0xc000u16, 0x0001, 0x7c00, 0x7e00].iter().flat_map(|x| x.to_le_bytes()).collect();
let p = a.array("values", "", 9, 1, &raw);
let property = Prop { name: p.name.into(), meta: p.meta, kind: PropKind::Array, pod: 9, extent: 1, group: p.group };
let mut ogawa = Ogawa { b: &a.w.b, budget: MAX_DECODED_BYTES, indexed_meta: Vec::new() };
let values = ogawa.values(&property).unwrap();
assert_eq!(values[0], -2.0);
assert_eq!(values[1], 2f64.powi(-24));
assert_eq!(values[2], f64::INFINITY);
assert!(values[3].is_nan());
}
#[test]
fn hdf5_and_garbage_are_clear_errors() {
let e = read(b"\x89HDF\r\n\x1a\n rest of an hdf5 file").err().unwrap().to_string();
@@ -291,3 +323,29 @@ fn self_referencing_groups_are_bounded() {
let bytes = a.archive(at, &[]);
assert!(read(&bytes).is_err());
}
#[test]
fn indexed_metadata_stops_at_the_entries_a_byte_can_index() {
// One real entry followed by a million empty ones: a byte each in the file, a `String` each
// in memory, and no index past 254 can be used anyway.
let mut block = vec![3, b'a', b'b', b'c'];
block.extend(std::iter::repeat_n(0u8, 1_000_000));
let mut kept = vec![String::new()];
read_indexed_meta(&block, &mut kept).unwrap();
assert_eq!(kept.len(), MAX_INDEXED_META);
assert_eq!(kept.get(1).map(String::as_str), Some("abc"));
// The mesh schema comes from metadata index 1 even when the table has unreachable entries.
let mut a = Abc { w: Writer::new() };
let pp = a.array("P", "", F32, 3, &f32s(&[0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0]));
let fi = a.array(".faceIndices", "", I32, 1, &i32s(&[0, 1, 2]));
let fc = a.array(".faceCounts", "", I32, 1, &i32s(&[3]));
let geom = a.compound(".geom", "schema=AbcGeom_PolyMesh_v1", vec![pp, fi, fc]);
let mesh = a.object(vec![geom], &[]);
let top = a.object(vec![], &[("tri", "", mesh)]);
let mut metas = vec!["schema=AbcGeom_PolyMesh_v1"];
metas.extend(std::iter::repeat_n("", 5_000));
let raw = read(&a.archive(top, &metas)).unwrap();
assert_eq!(raw.positions, [Vec3::X, Vec3::Y, Vec3::ZERO]);
assert_eq!(raw.groups, vec![(DEFAULT_MATERIAL.into(), "tri".into(), vec![0, 1, 2])]);
}
+1
View File
@@ -356,6 +356,7 @@ fn add_geometry(d: &mut Doc, sink: &mut PolygonSink, geom: &El, m: Mat4, bind: &
p: m.transform_point3(Vec3::from(pos)),
n: n.map(|n| nm * Vec3::from(n)),
uv: t.map(|[u, v]| Vec2::new(u, 1.0 - v)),
color: None,
});
}
next = next.saturating_add(size);
File diff suppressed because it is too large. Load diff
+27 -1
View File
@@ -20,8 +20,34 @@ pub(crate) fn read(bytes: &[u8]) -> Result<RawMesh> {
crate::guard(KIND, || read_inner(bytes))
}
/// Loads and validates the document like `gltf::Gltf::from_slice`, except that a scene without a
/// `nodes` array is an empty scene. The `gltf` crate declares the field as required, but the
/// glTF 2.0 schema lists no required properties for a scene, and exporters write such scenes
/// for scenes that contain no objects.
fn parse(bytes: &[u8]) -> Result<(gltf::Document, Option<Vec<u8>>)> {
let (json, blob) = if bytes.starts_with(b"glTF") {
let mut glb = gltf::Glb::from_slice(bytes).map_err(|e| malformed(KIND, e))?;
let blob = glb.bin.take().map(std::borrow::Cow::into_owned);
(glb.json, blob)
} else {
(std::borrow::Cow::Borrowed(bytes), None)
};
let mut value: serde_json::Value = serde_json::from_slice(&json).map_err(|e| malformed(KIND, e))?;
if let Some(scenes) = value.get_mut("scenes").and_then(serde_json::Value::as_array_mut) {
for scene in scenes {
if let Some(scene) = scene.as_object_mut() {
scene.entry("nodes").or_insert_with(|| serde_json::Value::Array(Vec::new()));
}
}
}
// Consume the DOM directly rather than serialising it back to a second full-size buffer.
let root: gltf::json::Root = serde_json::from_value(value).map_err(|e| malformed(KIND, e))?;
let document = gltf::Document::from_json(root).map_err(|e| malformed(KIND, e))?;
Ok((document, blob))
}
fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
let gltf::Gltf { document, blob } = gltf::Gltf::from_slice(bytes).map_err(|e| malformed(KIND, e))?;
let (document, blob) = parse(bytes)?;
check_buffer_uris(&document)?;
let buffers = gltf::import_buffers(&document, None, blob).map_err(|e| match e {
gltf::Error::ExternalReferenceInSliceImport => FormatError::Unsupported(
+22 -3
View File
@@ -2,7 +2,7 @@
//! world-space [`Mesh`] with one submesh per material and part (glTF node, OBJ object, FBX
//! model, COLLADA node, USD Mesh prim, Alembic PolyMesh; unnamed parts take the file stem), materials contiguous,
//! glTF UV convention (origin top-left, v down),
//! normals (computed when missing), vertex colors (glTF, OBJ, PLY), UVs (auto-unwrapped when
//! normals (computed when missing), vertex colors (glTF, FBX, OBJ, PLY), UVs (auto-unwrapped when
//! missing) and MikkTSpace tangents. glTF, FBX, COLLADA and USD cameras come along as the
//! project's camera list.
@@ -182,10 +182,11 @@ pub(crate) struct Corner {
pub p: Vec3,
pub n: Option<Vec3>,
pub uv: Option<Vec2>,
pub color: Option<Vec4>,
}
/// Collects polygons into a [`RawMesh`]: fan triangulation, one vertex per unique corner, and
/// normals / UVs kept only when every corner has them (else computed / unwrapped).
/// normals / UVs / colors kept only when every corner has them (else computed / unwrapped / white).
#[derive(Default)]
pub(crate) struct PolygonSink {
/// Part name of the polygons that follow.
@@ -193,10 +194,13 @@ pub(crate) struct PolygonSink {
raw: RawMesh,
normals: Vec<Vec3>,
uvs: Vec<Vec2>,
/// Allocated by the first coloured corner, so a scene without colors pays nothing for them.
colors: Option<Vec<Vec4>>,
missing_normals: bool,
missing_uvs: bool,
some_uvs: bool,
ids: HashMap<[u32; 8], u32>,
missing_colors: bool,
ids: HashMap<[u32; 12], u32>,
polygon: Vec<u32>,
triangles: usize,
}
@@ -213,13 +217,24 @@ impl PolygonSink {
self.missing_normals |= c.n.is_none();
self.missing_uvs |= c.uv.is_none();
self.some_uvs |= c.uv.is_some();
self.missing_colors |= c.color.is_none();
let (n, uv) = (c.n.unwrap_or_default(), c.uv.unwrap_or_default());
// A corner without a color must not merge with one that has it.
let color_key = c.color.map_or([u32::MAX; 4], |v| v.to_array().map(f32::to_bits));
let key = [c.p.x, c.p.y, c.p.z, n.x, n.y, n.z, uv.x, uv.y].map(f32::to_bits);
let key = [key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7], color_key[0], color_key[1], color_key[2], color_key[3]];
let id = match self.ids.entry(key) {
Entry::Occupied(e) => *e.get(),
Entry::Vacant(e) => {
let id = self.raw.positions.len();
check_vertices(id + 1)?;
// White stands in for the corners before the first coloured one.
if c.color.is_some() && self.colors.is_none() {
self.colors = Some(vec![Vec4::ONE; id]);
}
if let Some(colors) = self.colors.as_mut() {
colors.push(c.color.unwrap_or(Vec4::ONE));
}
self.raw.positions.push(c.p);
self.normals.push(n);
self.uvs.push(uv);
@@ -242,8 +257,12 @@ impl PolygonSink {
if self.missing_uvs && self.some_uvs {
self.warn("some polygons have no UVs; the whole mesh was unwrapped automatically".into());
}
if self.missing_colors && self.colors.is_some() {
self.warn("some polygon corners have no color in the color layer; vertex colors were dropped".into());
}
self.raw.normals = (!self.missing_normals).then_some(self.normals);
self.raw.uvs = (!self.missing_uvs).then_some(self.uvs);
self.raw.colors = if self.missing_colors { None } else { self.colors.take() };
self.raw
}
}
+70 -93
View File
@@ -1,10 +1,9 @@
//! Wavefront OBJ (via `tobj`): polygons fan-triangulated, one vertex per unique v/vt/vn.
//! Wavefront OBJ through the shared bounded mesh reader.
use super::{MAX_VERTICES, RawMesh, check_vertices};
use crate::{FormatError, Result, malformed};
use std::collections::HashMap;
use texturecraft_geom::glam::{Vec2, Vec3, Vec4};
use super::{RawMesh, check_triangles, check_vertices};
use crate::{Result, malformed};
const KIND: &str = "OBJ";
pub(crate) fn read(bytes: &[u8]) -> Result<RawMesh> {
@@ -12,99 +11,77 @@ pub(crate) fn read(bytes: &[u8]) -> Result<RawMesh> {
}
fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
let text = String::from_utf8_lossy(bytes);
// A synthetic library lets tobj assign usemtl names without reading external MTL files
let mut names: Vec<&str> = Vec::new();
for line in text.lines().map(str::trim) {
if line.split_whitespace().next() == Some("usemtl")
&& let Some((_, name)) = line.split_once(' ')
&& !name.trim().is_empty()
&& !names.contains(&name.trim())
{
names.push(name.trim());
}
}
let library: String = names.iter().map(|name| format!("newmtl {name}\n")).collect();
let source = format!("mtllib texturecraft-usemtl.mtl\n{text}");
// Polygons stay whole so the wireframe can show them; `RawMesh::polygon` triangulates
let options = tobj::LoadOptions { single_index: true, triangulate: false, ignore_points: true, ignore_lines: true };
let (models, materials) =
tobj::load_obj_buf(&mut source.as_bytes(), &options, |_| tobj::load_mtl_buf(&mut library.as_bytes())).map_err(|e| malformed(KIND, e))?;
let materials = materials.unwrap_or_default();
let limits = craft_mesh::Limits {
max_vertices: MAX_VERTICES,
max_triangles: texturecraft_geom::MAX_TRIANGLES,
max_face_corners: MAX_VERTICES,
max_meshes: MAX_VERTICES,
max_parts: MAX_VERTICES,
max_materials: MAX_VERTICES,
max_data_bytes: super::MAX_MESH_FILE_BYTES as usize,
..Default::default()
};
let options = craft_mesh::obj::ReadOptions { strict: true, preserve_runs: true, ignore_material_libraries: true, groups_as_objects: true };
let scene = craft_mesh::obj::read_with_options(bytes, &limits, &mut craft_mesh::no_external, &options).map_err(|e| match e {
craft_mesh::Error::TooLarge(s) => FormatError::TooLarge(s),
e => FormatError::Invalid(e.to_string()),
})?;
let any_uvs = scene.meshes.iter().flat_map(|mesh| mesh.parts.iter()).flat_map(|part| part.corners.iter()).any(|c| c.uv != craft_mesh::NONE);
let any_colors = scene.meshes.iter().any(|mesh| !mesh.colors.is_empty());
let mut raw = RawMesh::default();
let any_uvs = models.iter().any(|m| !m.mesh.indices.is_empty() && m.mesh.texcoords.len() == (m.mesh.positions.len() / 3) * 2);
let (mut normals, mut uvs, mut colors) = (Some(Vec::new()), any_uvs.then(Vec::new), Vec::new());
let mut partial_uvs = false;
for model in &models {
let mesh = &model.mesh;
if mesh.indices.is_empty() {
continue;
}
let vertex_count = mesh.positions.len() / 3;
check_vertices(raw.positions.len().saturating_add(vertex_count))?;
// Corners per face; without arities every face is a triangle
let arities: Vec<usize> =
if mesh.face_arities.is_empty() { vec![3; mesh.indices.len() / 3] } else { mesh.face_arities.iter().map(|a| *a as usize).collect() };
check_triangles(raw.triangle_count(), arities.iter().map(|a| a.saturating_sub(2)).sum())?;
let base = raw.positions.len() as u32;
raw.positions.extend(mesh.positions.as_chunks::<3>().0.iter().map(|p| Vec3::new(p[0], p[1], p[2])));
if mesh.normals.len() == vertex_count * 3 {
if let Some(v) = &mut normals {
v.extend(mesh.normals.as_chunks::<3>().0.iter().map(|p| Vec3::new(p[0], p[1], p[2])));
}
} else {
normals = None;
}
if let Some(v) = &mut uvs {
if mesh.texcoords.len() == vertex_count * 2 {
// OBJ UVs have their origin bottom-left
v.extend(mesh.texcoords.as_chunks::<2>().0.iter().map(|t| Vec2::new(t[0], 1.0 - t[1])));
} else {
v.extend(std::iter::repeat_n(Vec2::ZERO, vertex_count));
partial_uvs = true;
}
}
// `v x y z r g b`: objects without colors are white
match mesh.vertex_color.len() == vertex_count * 3 {
true => colors.extend(mesh.vertex_color.as_chunks::<3>().0.iter().map(|c| Vec4::new(c[0], c[1], c[2], 1.0))),
false => colors.extend(std::iter::repeat_n(Vec4::ONE, vertex_count)),
}
let name = match mesh.material_id {
Some(id) => materials.get(id).map(|mat| mat.name.clone()).filter(|s| !s.is_empty()).unwrap_or_else(|| format!("Material_{id}")),
None => "Default".to_string(),
};
if let Some(i) = mesh.indices.iter().find(|i| **i as usize >= vertex_count) {
return Err(malformed(KIND, format!("face index {i} out of range")));
}
// tobj names models without `o`/`g` "unnamed_object"; an empty name takes the file stem
let part = if model.name == "unnamed_object" { "" } else { model.name.as_str() };
let (mut rest, mut corners) = (mesh.indices.as_slice(), Vec::new());
for arity in arities {
let Some((face, tail)) = rest.split_at_checked(arity) else { break };
rest = tail;
corners.clear();
corners.extend(face.iter().map(|i| base + i));
if arity >= 3 {
raw.polygon(&name, part, &corners);
let (mut normals, mut uvs, mut colors) = (Vec::new(), Vec::new(), Vec::new());
let (mut missing_normals, mut missing_uvs) = (false, false);
for mesh in &scene.meshes {
for part in &mesh.parts {
let material = part.material.and_then(|i| scene.materials.get(i)).map_or("Default", |m| m.name.as_str());
let name = if part.name.is_empty() { mesh.name.as_str() } else { part.name.as_str() };
// OBJ model/material runs have independent v/vt/vn deduplication.
let mut ids = HashMap::new();
for face in part.faces() {
let mut polygon = Vec::with_capacity(face.len());
for corner in face {
let id = if let Some(id) = ids.get(corner) {
*id
} else {
let position = mesh.positions.get(corner.position as usize).ok_or_else(|| malformed(KIND, "position index out of range"))?;
check_vertices(raw.positions.len().saturating_add(1))?;
// Bound both the output pools and the per-run corner map before growth.
let staging = raw
.positions
.len()
.checked_add(1)
.and_then(|n| n.checked_mul(64))
.and_then(|n| ids.len().checked_add(1).and_then(|m| m.checked_mul(48)).and_then(|m| n.checked_add(m)));
if staging.is_none_or(|n| n > limits.max_data_bytes) {
return Err(FormatError::TooLarge("OBJ mesh exceeds the memory limit".into()));
}
let id = u32::try_from(raw.positions.len()).map_err(|_| FormatError::TooLarge("OBJ vertex count".into()))?;
raw.positions.push(Vec3::new(position[0] as f32, position[1] as f32, position[2] as f32));
let normal = mesh.normals.get(corner.normal as usize).map(|n| Vec3::new(n[0] as f32, n[1] as f32, n[2] as f32));
missing_normals |= normal.is_none();
normals.push(normal.unwrap_or_default());
if any_uvs {
let uv = mesh.uv_sets.first().and_then(|set| set.get(corner.uv as usize)).map(|v| Vec2::new(v[0], v[1]));
missing_uvs |= uv.is_none();
uvs.push(uv.unwrap_or_default());
}
if any_colors {
colors.push(mesh.colors.get(corner.position as usize).copied().map(Vec4::from_array).unwrap_or(Vec4::ONE));
}
ids.insert(*corner, id);
id
};
polygon.push(id);
}
raw.polygon(material, name, &polygon);
}
}
}
if partial_uvs {
if missing_uvs {
raw.warnings.push("some objects have no UVs; default UVs were assigned".into());
}
raw.normals = normals;
raw.uvs = uvs;
raw.colors = models.iter().any(|model| !model.mesh.vertex_color.is_empty()).then_some(colors);
raw.normals = (!missing_normals).then_some(normals);
raw.uvs = any_uvs.then_some(uvs);
raw.colors = any_colors.then_some(colors);
Ok(raw)
}
+90 -85
View File
@@ -10,40 +10,20 @@ const KIND: &str = "PLY";
/// Longest polygon accepted (a list count above this is treated as corruption).
const MAX_FACE_VERTICES: usize = 4096;
#[derive(Clone, Copy, PartialEq)]
enum Ty {
I8,
U8,
I16,
U16,
I32,
U32,
F32,
F64,
}
use craft_mesh::ply::{Encoding as SharedEncoding, Header, Rows, Scalar as Ty};
impl Ty {
fn parse(s: &str) -> Option<Ty> {
Some(match s {
"char" | "int8" => Ty::I8,
"uchar" | "uint8" => Ty::U8,
"short" | "int16" => Ty::I16,
"ushort" | "uint16" => Ty::U16,
"int" | "int32" => Ty::I32,
"uint" | "uint32" => Ty::U32,
"float" | "float32" => Ty::F32,
"double" | "float64" => Ty::F64,
_ => return None,
})
}
fn size(self) -> usize {
match self {
Ty::I8 | Ty::U8 => 1,
Ty::I16 | Ty::U16 => 2,
Ty::I32 | Ty::U32 | Ty::F32 => 4,
Ty::F64 => 8,
}
}
fn parse_type(s: &str) -> Option<Ty> {
Some(match s {
"char" | "int8" => Ty::I8,
"uchar" | "uint8" => Ty::U8,
"short" | "int16" => Ty::I16,
"ushort" | "uint16" => Ty::U16,
"int" | "int32" => Ty::I32,
"uint" | "uint32" => Ty::U32,
"float" | "float32" => Ty::F32,
"double" | "float64" => Ty::F64,
_ => return None,
})
}
struct Property {
@@ -66,60 +46,36 @@ enum Encoding {
Be,
}
/// Reads scalars from the body in either encoding.
struct Body<'a> {
data: &'a [u8],
pos: usize,
enc: Encoding,
}
impl Body<'_> {
fn next_token(&mut self) -> Option<&str> {
let rest = self.data.get(self.pos..)?;
let start = rest.iter().position(|b| !b.is_ascii_whitespace())?;
let len = rest.get(start..)?.iter().position(|b| b.is_ascii_whitespace()).unwrap_or(rest.len() - start);
self.pos += start + len;
std::str::from_utf8(rest.get(start..start + len)?).ok()
}
fn read(&mut self, ty: Ty) -> Result<f64> {
if self.enc == Encoding::Ascii {
let tok = self.next_token().ok_or_else(|| malformed(KIND, "unexpected end of data"))?;
return tok.parse::<f64>().map_err(|_| malformed(KIND, format!("bad number \"{tok}\"")));
}
let n = ty.size();
let bytes = self.data.get(self.pos..self.pos + n).ok_or_else(|| malformed(KIND, "unexpected end of data"))?;
self.pos += n;
let mut b = [0u8; 8];
b.get_mut(..n).unwrap_or_default().copy_from_slice(bytes);
if self.enc == Encoding::Be {
b.get_mut(..n).unwrap_or_default().reverse();
}
Ok(match ty {
Ty::I8 => b[0] as i8 as f64,
Ty::U8 => b[0] as f64,
Ty::I16 => i16::from_le_bytes([b[0], b[1]]) as f64,
Ty::U16 => u16::from_le_bytes([b[0], b[1]]) as f64,
Ty::I32 => i32::from_le_bytes([b[0], b[1], b[2], b[3]]) as f64,
Ty::U32 => u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as f64,
Ty::F32 => f32::from_le_bytes([b[0], b[1], b[2], b[3]]) as f64,
Ty::F64 => f64::from_le_bytes(b),
})
}
fn remaining(&self) -> usize {
self.data.len().saturating_sub(self.pos)
}
}
pub(crate) fn read(bytes: &[u8]) -> Result<RawMesh> {
let (elements, enc, body_start) = header(bytes)?;
let mut body = Body { data: bytes.get(body_start..).unwrap_or_default(), pos: 0, enc };
// Keep the strict app header grammar and mesh policy; delegate only wire scalars.
let encoding = match enc {
Encoding::Ascii => SharedEncoding::Ascii,
Encoding::Le => SharedEncoding::Little,
Encoding::Be => SharedEncoding::Big,
};
let shared_header = Header { encoding, elements: Vec::new(), body: body_start, comments: Vec::new() };
let mut body = Rows::new(&shared_header, bytes);
let mut raw = RawMesh::default();
let (mut normals, mut uvs, mut colors) = (Vec::new(), Vec::new(), Vec::new());
// Elements may appear in any order, so take the vertex count from the header.
let nverts: usize = elements.iter().filter(|e| e.name == "vertex").map(|e| e.count).fold(0, usize::saturating_add);
// The shared ASCII reader counts the delimiter after a value as read; the previous reader
// left it in the remaining bytes. Only an element without properties can notice the difference.
let ascii_slack = usize::from(enc == Encoding::Ascii);
for el in &elements {
// An element without properties reads nothing, so its rows are skipped. Its count stays
// bounded by the bytes left, as the previous reader counted them.
if el.props.is_empty() {
if el.count > body.remaining().saturating_add(ascii_slack) {
return Err(malformed(KIND, format!("element \"{}\" claims {} items but the file is too short", el.name, el.count)));
}
// A vertex element still needs positions, as below.
if el.name == "vertex" {
return Err(malformed(KIND, "vertices have no x, y, z"));
}
continue;
}
// Every item takes at least one byte, so the count can never exceed what is left.
if el.count > body.remaining() {
return Err(malformed(KIND, format!("element \"{}\" claims {} items but the file is too short", el.name, el.count)));
@@ -149,17 +105,17 @@ pub(crate) fn read(bytes: &[u8]) -> Result<RawMesh> {
match p.list {
None => {
if let Some(v) = vals.get_mut(k) {
*v = body.read(p.ty)?;
*v = body.value(p.ty).map_err(|e| malformed(KIND, e.to_string()))?;
}
}
Some(ct) => {
let len = body.read(ct)?;
let len = body.value(ct).map_err(|e| malformed(KIND, e.to_string()))?;
if !(0.0..=MAX_FACE_VERTICES as f64).contains(&len) {
return Err(malformed(KIND, format!("bad list length {len}")));
}
poly.clear();
for _ in 0..len as usize {
let v = body.read(p.ty)?;
let v = body.value(p.ty).map_err(|e| malformed(KIND, e.to_string()))?;
if face_list == Some(k) {
if !(0.0..nverts as f64).contains(&v) {
return Err(malformed(KIND, format!("face index {v} out of range")));
@@ -240,13 +196,13 @@ fn header(bytes: &[u8]) -> Result<(Vec<Element>, Encoding, usize)> {
}
["property", "list", ct, ty, name] => {
let el = elements.last_mut().ok_or_else(|| malformed(KIND, "property before element"))?;
let (ct, ty) = (Ty::parse(ct), Ty::parse(ty));
let (ct, ty) = (parse_type(ct), parse_type(ty));
let (Some(ct), Some(ty)) = (ct, ty) else { return Err(malformed(KIND, format!("bad list types in \"{line}\""))) };
el.props.push(Property { name: name.to_string(), ty, list: Some(ct) });
}
["property", ty, name] => {
let el = elements.last_mut().ok_or_else(|| malformed(KIND, "property before element"))?;
let ty = Ty::parse(ty).ok_or_else(|| malformed(KIND, format!("bad property type {ty}")))?;
let ty = parse_type(ty).ok_or_else(|| malformed(KIND, format!("bad property type {ty}")))?;
el.props.push(Property { name: name.to_string(), ty, list: None });
}
["ply"] | [] | ["comment", ..] | ["obj_info", ..] => {}
@@ -256,3 +212,52 @@ fn header(bytes: &[u8]) -> Result<(Vec<Element>, Encoding, usize)> {
let enc = enc.ok_or_else(|| malformed(KIND, "header has no format line"))?;
Ok((elements, enc, pos))
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod shared_rows_tests {
use super::*;
#[test]
fn trailing_propertyless_element_after_a_newline_still_opens() {
let bytes = b"ply\nformat ascii 1.0\nelement vertex 1\nproperty float x\nproperty float y\nproperty float z\nelement edge 1\nend_header\n0 0 0\n";
assert_eq!(read(bytes).unwrap().positions, vec![Vec3::new(0.0, 0.0, 0.0)]);
let huge = b"ply\nformat ascii 1.0\nelement vertex 1\nproperty float x\nproperty float y\nproperty float z\nelement edge 3\nend_header\n0 0 0\n";
assert!(read(huge).is_err());
}
#[test]
fn propertyless_vertex_element_is_still_rejected() {
let bytes = b"ply\nformat ascii 1.0\nelement vertex 1\nelement face 1\nproperty list uchar int vertex_indices\nend_header\n3 0 0 0\n";
let Err(e) = read(bytes) else { panic!("a vertex element without x, y, z must not open") };
let err = e.to_string();
assert!(err.contains("vertices have no x, y, z"), "{err}");
}
#[test]
fn binary_scalar_endianness_matches_ascii() {
for (format, big) in [("binary_little_endian", false), ("binary_big_endian", true)] {
let mut bytes =
format!("ply\nformat {format} 1.0\nelement vertex 1\nproperty short x\nproperty float y\nproperty double z\nend_header\n").into_bytes();
bytes.extend(if big { (-123i16).to_be_bytes() } else { (-123i16).to_le_bytes() });
bytes.extend(if big { 1.25f32.to_be_bytes() } else { 1.25f32.to_le_bytes() });
bytes.extend(if big { (-2.5f64).to_be_bytes() } else { (-2.5f64).to_le_bytes() });
assert_eq!(read(&bytes).unwrap().positions, vec![Vec3::new(-123.0, 1.25, -2.5)]);
bytes.pop();
assert!(read(&bytes).is_err());
}
let ascii = b"ply\nformat ascii 1.0\nelement vertex 1\nproperty short x\nproperty float y\nproperty double z\nend_header\n -123 \t1.25\r\n -2.5 ";
assert_eq!(read(ascii).unwrap().positions, vec![Vec3::new(-123.0, 1.25, -2.5)]);
}
#[test]
fn strict_header_and_invalid_face_policy_remain_local() {
for extra in ["unknown directive\n", "property nonsense x\n"] {
let bytes = format!("ply\nformat ascii 1.0\nelement vertex 1\n{extra}end_header\n0");
assert!(read(bytes.as_bytes()).is_err());
}
assert!(read(b"ply\nformat ascii 1.0\ncomment \xff\nend_header\n").is_err());
let bytes = b"ply\nformat ascii 1.0\nelement face 1\nproperty list uchar int vertex_indices\nelement vertex 1\nproperty float x\nproperty float y\nproperty float z\nend_header\n3 0 0 1\n0 0 0\n";
assert!(read(bytes).is_err()); // Do not turn a bad face into a warning/skip.
}
}
+99 -3
View File
@@ -3,7 +3,7 @@
use std::io::Write as _;
use texturecraft_geom::glam::{Vec2, Vec3};
use texturecraft_geom::glam::{Vec2, Vec3, Vec4};
use super::tests::hammer;
use super::*;
@@ -298,7 +298,46 @@ fn fbx_binary_32_and_64_bit_compressed_or_not() {
}
}
/// [`scene`] with a per-polygon-vertex color layer: corner `i` gets `(i/8, 1 - i/8, 0.25, 1)`.
fn colored_scene() -> Vec<N> {
let mut nodes = scene(false);
let objects = nodes.iter_mut().find(|node| node.name == "Objects").unwrap();
let geometry = objects.children.iter_mut().find(|node| node.name == "Geometry").unwrap();
let colors: Vec<f64> = (0..7).flat_map(|i| [i as f64 / 8.0, 1.0 - i as f64 / 8.0, 0.25, 1.0]).collect();
geometry.children.push(layer(
"LayerElementColor",
"ByPolygonVertex",
"IndexToDirect",
vec![n("Colors", vec![P::Doubles(colors)], vec![]), n("ColorIndex", vec![P::Ints((0..7).collect())], vec![])],
));
nodes
}
#[test]
fn fbx_vertex_colors_reach_the_mesh_and_high_polys() {
let nodes = colored_scene();
let expected: Vec<Vec4> = (0..7).map(|i| Vec4::new(i as f32 / 8.0, 1.0 - i as f32 / 8.0, 0.25, 1.0)).collect();
for (label, bytes) in [("binary", binary_fbx(&nodes, 7400, false)), ("ascii", ascii_fbx(&nodes).into_bytes())] {
let imported = import_mesh_bytes("colors.fbx", &bytes).unwrap_or_else(|e| panic!("{label}: {e}"));
assert_eq!(imported.mesh.colors, expected, "{label}: the color layer reaches the mesh");
let high = import_high_poly_bytes("colors.fbx", &bytes).unwrap_or_else(|e| panic!("{label}: {e}"));
assert_eq!(high.mesh.colors, expected, "{label}: and the high poly used for ID bakes");
}
}
/// [`scene`] with a diffuse color on Red and an embedded diffuse texture on Blue.
#[test]
fn fbx_camera_models_are_capped_during_import() {
let mut nodes = scene(false);
let objects = nodes.iter_mut().find(|node| node.name == "Objects").unwrap();
for id in 100..199 {
objects.children.push(n("Model", vec![P::L(id), P::S("Camera\\u{0}\\u{1}Model"), P::S("Camera")], vec![]));
}
let raw = fbx::read(&binary_fbx(&nodes, 7500, true)).unwrap();
assert_eq!(raw.cameras.len(), MAX_CAMERAS);
}
fn textured_scene(png: &[u8]) -> Vec<N> {
let mut nodes = scene(false);
let objects = nodes.iter_mut().find(|n| n.name == "Objects").unwrap();
@@ -415,6 +454,24 @@ fn fbx_ascii_and_z_up() {
check_fbx_scene(&import_mesh_bytes("z.fbx", &binary_fbx(&scene(true), 7700, true)).unwrap().mesh, true);
}
#[test]
fn fbx_duplicate_geometry_connections_do_not_duplicate_instances() {
let mut nodes = scene(false);
let connections = nodes.iter_mut().find(|node| node.name == "Connections").unwrap();
for _ in 0..50 {
connections.children.push(n("C", vec![P::S("OO"), P::L(10), P::L(21)], vec![]));
}
let imported = import_mesh_bytes("scene.fbx", ascii_fbx(&nodes).as_bytes()).unwrap();
assert_eq!(imported.report.triangles, 3);
let objects = nodes.iter_mut().find(|node| node.name == "Objects").unwrap();
objects.children.push(n("Model", vec![P::L(22), P::S("Other\u{0}\u{1}Model"), P::S("Mesh")], vec![]));
let connections = nodes.iter_mut().find(|node| node.name == "Connections").unwrap();
connections.children.push(n("C", vec![P::S("OO"), P::L(10), P::L(22)], vec![]));
let imported = import_mesh_bytes("scene.fbx", ascii_fbx(&nodes).as_bytes()).unwrap();
assert_eq!(imported.report.triangles, 6, "a different model must remain a distinct instance");
}
/// The scene of [`scene`] (Y up) as FBX 6.1 writes it: geometry inside the Model, objects named
/// `Class::Name`, `Properties60`, `Connect` records, GlobalSettings inside Objects.
const FBX61: &str = r#"; FBX 6.1.0 project file
@@ -589,19 +646,55 @@ fn fbx_old_versions_and_garbage_are_errors() {
assert!(import_mesh_bytes("h.fbx", &huge).is_err());
}
// Keep these fixtures independently schedulable: one serial test took 24 minutes in
// instrumented coverage. Splitting cases keeps every mutation and assertion intact.
#[test]
fn malformed_fbx_and_dae_never_panic() {
fn malformed_fbx_and_dae_never_panic_binary_7400() {
hammer("x.fbx", &binary_fbx(&scene(false), 7400, false));
}
#[test]
fn malformed_fbx_and_dae_never_panic_binary_7500() {
hammer("x.fbx", &binary_fbx(&scene(true), 7500, true));
}
#[test]
fn malformed_fbx_and_dae_never_panic_ascii() {
hammer("x.fbx", ascii_fbx(&scene(false)).as_bytes());
}
#[test]
fn malformed_fbx_and_dae_never_panic_collada() {
hammer("x.dae", DAE.as_bytes());
}
#[test]
fn malformed_fbx_and_dae_never_panic_random_binary() {
random_scene_corruptions(0);
}
#[test]
fn malformed_fbx_and_dae_never_panic_random_ascii() {
random_scene_corruptions(1);
}
#[test]
fn malformed_fbx_and_dae_never_panic_random_collada() {
random_scene_corruptions(2);
}
fn random_scene_corruptions(selected_fixture: usize) {
// Random multi-byte corruptions (a fixed LCG keeps failures reproducible).
let mut seed = 0x2545_f491_4f6c_dd1du64;
let mut next = move || {
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
(seed >> 33) as usize
};
for good in [binary_fbx(&scene(false), 7500, true), ascii_fbx(&scene(true)).into_bytes(), DAE.as_bytes().to_vec()] {
// Replay earlier mutations without parsing them so each fixture receives exactly
// its original LCG stream, not a fresh seed. Fixture sizes and all 1,500 cases
// per fixture stay unchanged; generating these bytes is cheap next to parsing.
let fixtures = [binary_fbx(&scene(false), 7500, true), ascii_fbx(&scene(true)).into_bytes(), DAE.as_bytes().to_vec()];
for (fixture, good) in fixtures.into_iter().enumerate().take(selected_fixture + 1) {
let name = if good.starts_with(b"<?xml") { "r.dae" } else { "r.fbx" };
for _ in 0..1500 {
let mut b = good.clone();
@@ -609,6 +702,9 @@ fn malformed_fbx_and_dae_never_panic() {
let i = next() % b.len();
b[i] = next() as u8;
}
if fixture != selected_fixture {
continue;
}
if let Err(e) = import_mesh_bytes(name, &b) {
assert!(!e.to_string().contains("parser failed"), "{name}: a parser panicked");
}
+59 -62
View File
@@ -1,78 +1,75 @@
//! STL (binary and ascii). Facets keep their own corners, so shading is flat; there are no UVs.
//! STL (binary and ASCII), read by the shared bounded mesh importer.
use texturecraft_geom::glam::Vec3;
use texturecraft_geom::glam::{Vec3, Vec4};
use super::{RawMesh, check_triangles};
use crate::{Result, malformed};
use super::{MAX_VERTICES, RawMesh, check_triangles};
use crate::{FormatError, Result};
const KIND: &str = "STL";
pub(crate) fn read(bytes: &[u8]) -> Result<RawMesh> {
let count = bytes.get(80..84).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize);
let binary_len = count.and_then(|c| c.checked_mul(50)).and_then(|n| n.checked_add(84));
let looks_ascii = bytes.iter().position(|b| !b.is_ascii_whitespace()).and_then(|i| bytes.get(i..i + 5)) == Some(b"solid".as_slice());
let tris = match (count, binary_len) {
// Exact size wins: many binary files also start with "solid".
(Some(c), Some(len)) if len == bytes.len() => binary(bytes, c)?,
_ if looks_ascii => ascii(bytes)?,
(Some(c), Some(len)) if len < bytes.len() => binary(bytes, c)?,
_ => return Err(malformed(KIND, "neither a complete binary STL nor an ascii STL")),
};
let scene = craft_mesh::stl::read(bytes, &stl_limits()).map_err(|e| FormatError::Invalid(e.to_string()))?;
let mut raw = RawMesh::default();
check_triangles(0, tris.len())?;
let mut normals = Vec::with_capacity(tris.len() * 3);
for t in &tris {
let n = (t[1] - t[0]).cross(t[2] - t[0]).try_normalize().unwrap_or(Vec3::Y);
raw.positions.extend_from_slice(t);
normals.extend_from_slice(&[n; 3]);
raw.warnings = scene.warnings;
let mut tri_count = 0;
let mut vertex_colors = Vec::new();
let has_colors = !scene.materials.is_empty();
for mesh in scene.meshes {
// The shared model groups colored facets into parts. Position indices still reflect
// source order, so merge the parts back into STL order for TextureCraft's raw mesh.
let mut triangles = Vec::new();
for part in mesh.parts {
let material = part.material.and_then(|i| scene.materials.get(i));
let color = material.map(|m| Vec4::from_array(m.base_color));
for tri in part.triangles() {
let first_position = tri.iter().map(|c| c.position).min().unwrap_or(u32::MAX);
triangles.push((first_position, tri, color));
}
}
triangles.sort_by_key(|(first_position, _, _)| *first_position);
for (_, tri, color) in triangles {
check_triangles(tri_count, 1)?;
tri_count += 1;
let mut points = [Vec3::ZERO; 3];
for (i, corner) in tri.into_iter().enumerate() {
let p = mesh.positions.get(corner.position as usize).ok_or_else(|| FormatError::Invalid(format!("{KIND}: position index out of range")))?;
points[i] = Vec3::new(p[0] as f32, p[1] as f32, p[2] as f32);
}
// Recompute per-facet normals as TextureCraft's old importer did; stored STL
// normals are often zero or disagree with the vertex winding.
let normal = (points[1] - points[0]).cross(points[2] - points[0]).try_normalize().unwrap_or(Vec3::Y);
raw.positions.extend_from_slice(&points);
raw.normals.get_or_insert_with(Vec::new).extend_from_slice(&[normal; 3]);
if has_colors {
// The shared reader normalizes STL's color extensions to linear RGBA.
// Uncolored facets alongside colored ones receive its neutral gray material.
let color = color.unwrap_or(Vec4::new(0.6, 0.6, 0.6, 1.0));
vertex_colors.extend_from_slice(&[color; 3]);
}
}
}
if raw.positions.is_empty() {
return Err(crate::malformed(KIND, "no facets"));
}
if has_colors {
raw.colors = Some(vertex_colors);
}
raw.normals = Some(normals);
raw.groups.push(("Default".into(), String::new(), (0..raw.positions.len() as u32).collect()));
Ok(raw)
}
fn binary(bytes: &[u8], count: usize) -> Result<Vec<[Vec3; 3]>> {
check_triangles(0, count)?;
let mut out = Vec::with_capacity(count);
for i in 0..count {
let f = |k: usize| -> Result<f32> {
let o = 84 + i * 50 + 12 + k * 4;
let b = bytes.get(o..o + 4).ok_or_else(|| malformed(KIND, "truncated facet"))?;
Ok(f32::from_le_bytes([b[0], b[1], b[2], b[3]]))
};
let v = |c: usize| -> Result<Vec3> { Ok(Vec3::new(f(c * 3)?, f(c * 3 + 1)?, f(c * 3 + 2)?)) };
out.push([v(0)?, v(1)?, v(2)?]);
}
Ok(out)
/// STL stores three positions per facet; retain the app's established triangle capacity.
fn stl_limits() -> craft_mesh::Limits {
craft_mesh::Limits { max_vertices: MAX_VERTICES, ..craft_mesh::Limits::default() }
}
fn ascii(bytes: &[u8]) -> Result<Vec<[Vec3; 3]>> {
let text = std::str::from_utf8(bytes).map_err(|_| malformed(KIND, "ascii STL is not text"))?;
let mut out = Vec::new();
let mut facet: Vec<Vec3> = Vec::new();
let mut words = text.split_whitespace();
while let Some(w) = words.next() {
match w {
"vertex" => {
let mut c = [0.0f32; 3];
for v in &mut c {
let tok = words.next().ok_or_else(|| malformed(KIND, "truncated vertex"))?;
*v = tok.parse().map_err(|_| malformed(KIND, format!("bad number \"{tok}\"")))?;
}
facet.push(Vec3::from(c));
}
"endfacet" => {
for i in 1..facet.len().saturating_sub(1) {
check_triangles(out.len(), 1)?;
out.push([facet[0], facet[i], facet[i + 1]]);
}
facet.clear();
}
_ => {}
}
#[cfg(test)]
mod tests {
#[test]
fn shared_reader_accepts_the_app_vertex_limit() {
let limits = super::stl_limits();
assert_eq!(limits.max_vertices, super::MAX_VERTICES);
assert_eq!(limits.max_triangles, texturecraft_geom::MAX_TRIANGLES);
}
if out.is_empty() {
return Err(malformed(KIND, "no facets"));
}
Ok(out)
}
+79
View File
@@ -118,6 +118,48 @@ fn glb_two_materials_mirrored_node() {
m.validate().unwrap();
}
/// A scene without a `nodes` array is legal glTF (Blender writes one for a scene with no exported
/// objects); it must not stop the default scene from importing.
#[test]
fn gltf_scene_without_nodes_imports_the_default_scene() {
let mut json: serde_json::Value = serde_json::from_slice(&embedded_gltf()).unwrap();
json["scenes"] = serde_json::json!([{ "name": "EmptyReviewScene" }, { "nodes": [0] }]);
json["scene"] = 1.into();
let im = import_mesh_bytes("scene.gltf", &serde_json::to_vec(&json).unwrap()).unwrap();
assert_eq!(im.report.triangles, 2);
// Only an empty scene: nothing to import, but a clear error rather than a parse failure.
json["scenes"] = serde_json::json!([{ "name": "EmptyReviewScene" }]);
json["scene"] = 0.into();
let err = import_mesh_bytes("scene.gltf", &serde_json::to_vec(&json).unwrap()).unwrap_err();
assert!(!err.to_string().contains("missing field"), "{err}");
}
#[test]
fn glb_scene_without_nodes_keeps_binary_mesh_in_default_scene() {
let original = glb(true, true);
let jlen = u32::from_le_bytes(original[12..16].try_into().unwrap()) as usize;
let mut json: serde_json::Value = serde_json::from_slice(&original[20..20 + jlen]).unwrap();
json["scenes"] = serde_json::json!([{ "name": "EmptyReviewScene" }, { "nodes": [0] }]);
json["scene"] = 1.into();
let mut js = serde_json::to_vec(&json).unwrap();
while !js.len().is_multiple_of(4) {
js.push(b' ');
}
let bin_chunk = &original[20 + jlen..];
let mut glb = Vec::new();
glb.extend(b"glTF");
glb.extend(2u32.to_le_bytes());
glb.extend(((20 + js.len() + bin_chunk.len()) as u32).to_le_bytes());
glb.extend((js.len() as u32).to_le_bytes());
glb.extend(b"JSON");
glb.extend(js);
glb.extend(bin_chunk);
let im = import_mesh_bytes("scene.glb", &glb).unwrap();
assert_eq!(im.report.triangles, 2);
assert_eq!(im.mesh.materials, ["Red", "Material_1"]);
assert_outward(&im.mesh);
}
#[test]
fn gltf_embedded_buffers_and_missing_attributes() {
let im = import_mesh_bytes("scene.gltf", &embedded_gltf()).unwrap();
@@ -372,6 +414,43 @@ fn stl_ascii_and_binary() {
assert_eq!(import_mesh_bytes("c.stl", &b).unwrap().mesh.triangle_count(), 2);
}
#[test]
fn stl_shared_reader_keeps_flat_normals_and_surfaces_shared_repairs_and_colors() {
let mut bytes = stl_binary();
// VisCAM/SolidView: bit 15 marks a facet color; red occupies bits 10..14.
bytes[132..134].copy_from_slice(&0xfc00u16.to_le_bytes());
// A third facet repeats that color after an uncolored facet. The shared reader groups its
// parts by color, so the adapter must restore the source facet order.
bytes.extend([0.0f32; 3].iter().flat_map(|v| v.to_le_bytes()));
for v in [[0.0f32, 0.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 1.0]] {
bytes.extend(v.iter().flat_map(|x| x.to_le_bytes()));
}
bytes.extend(0xfc00u16.to_le_bytes());
// Some exporters leave the header count at zero despite having facets.
bytes[80..84].copy_from_slice(&0u32.to_le_bytes());
let imported = import_mesh_bytes("colored.stl", &bytes).unwrap();
assert_eq!(imported.mesh.triangle_count(), 3);
assert_eq!(imported.mesh.submeshes.len(), 1);
assert_eq!(imported.mesh.submeshes[0].name, "colored");
assert_eq!(
imported.mesh.positions,
vec![
Vec3::ZERO,
Vec3::X,
Vec3::new(1.0, 1.0, 0.0),
Vec3::ZERO,
Vec3::new(1.0, 1.0, 0.0),
Vec3::Y,
Vec3::Z,
Vec3::new(1.0, 0.0, 1.0),
Vec3::new(1.0, 1.0, 1.0)
]
);
assert_eq!(imported.mesh.normals, vec![Vec3::Z; 9]);
assert!(imported.mesh.colors.iter().any(|c| c.x > 0.99 && c.y < 0.01 && c.z < 0.01));
assert!(imported.report.warnings.iter().any(|w| w.contains("counts no facets")));
}
/// Every prefix and some corruptions of a valid file: errors, never panics (and never an
/// error from the panic guard, which would mean a parser panicked).
pub(crate) fn hammer(name: &str, good: &[u8]) {
+19 -106
View File
@@ -104,35 +104,13 @@ pub(crate) fn numeric(t: u8) -> Option<(Scalar, usize)> {
/// Decompress Crate's LZ4 framing into at most `max_out` bytes.
pub(crate) fn decompress(input: &[u8], max_out: usize) -> Result<Vec<u8>> {
let bad = |e: lz4_flex::block::DecompressError| malformed(KIND, format!("LZ4 data: {e}"));
let (&chunks, mut rest) = input.split_first().ok_or_else(|| malformed(KIND, "empty compressed block"))?;
// LZ4 expands at most ~255×: never allocate more than the input can produce.
let mut out = vec![0u8; max_out.min(input.len().saturating_mul(256).saturating_add(64))];
let written = if chunks == 0 {
lz4_flex::block::decompress_into(rest, &mut out).map_err(bad)?
} else {
if chunks > 127 {
return Err(malformed(KIND, "bad LZ4 chunk count"));
}
let mut total = 0usize;
for _ in 0..chunks {
let size = rest.get(..4).and_then(|b| b.try_into().ok()).map(i32::from_le_bytes).ok_or_else(|| malformed(KIND, "truncated LZ4 chunk"))?;
let size = usize::try_from(size).map_err(|_| malformed(KIND, "negative LZ4 chunk size"))?;
let chunk = rest.get(4..4usize.saturating_add(size)).ok_or_else(|| malformed(KIND, "truncated LZ4 chunk"))?;
rest = rest.get(4 + size..).unwrap_or_default();
total += lz4_flex::block::decompress_into(chunk, out.get_mut(total..).unwrap_or_default()).map_err(bad)?;
}
total
};
out.truncate(written);
Ok(out)
craft_usd::decompress(input, max_out).map_err(|e| malformed(KIND, e.to_string()))
}
/// Crate's LZ4 framing for one chunk (inputs are far below LZ4's ~2 GB chunk limit).
pub(crate) fn compress(input: &[u8]) -> Vec<u8> {
let mut out = vec![0u8];
out.extend_from_slice(&lz4_flex::block::compress(input));
out
pub(crate) fn compress(input: &[u8]) -> Result<Vec<u8>> {
// Keep one compressed output reservation within the app's decoded-data ceiling.
craft_usd::compress::compress(input, super::MAX_DECODED_BYTES).map_err(|e| malformed(KIND, e.to_string()))
}
/// Upper bound of the encoded size of `n` integers of `width` bytes.
@@ -140,89 +118,24 @@ pub(crate) fn encoded_ints_len(n: usize, width: usize) -> usize {
if n == 0 { 0 } else { width.saturating_add(n.saturating_mul(2).saturating_add(7) / 8).saturating_add(n.saturating_mul(width)) }
}
fn read_signed(b: &[u8], at: usize, width: usize) -> Option<i64> {
let s = b.get(at..at.checked_add(width)?)?;
Some(match width {
1 => i64::from(i8::from_le_bytes(s.try_into().ok()?)),
2 => i64::from(i16::from_le_bytes(s.try_into().ok()?)),
4 => i64::from(i32::from_le_bytes(s.try_into().ok()?)),
_ => i64::from_le_bytes(s.try_into().ok()?),
})
}
/// Decode `n` integers of `width` (4 or 8) bytes; 32-bit values are sign-extended (reinterpret
/// as u32 for unsigned data).
/// Decode packed integers after the caller charges its aggregate decoded-byte budget.
pub(crate) fn decode_ints(data: &[u8], n: usize, width: usize) -> Result<Vec<i64>> {
let err = || malformed(KIND, "truncated integer array");
if n == 0 {
return Ok(Vec::new());
}
let common = read_signed(data, 0, width).ok_or_else(err)?;
let codes_at = width;
let mut at = codes_at + n.div_ceil(4);
let mut out = Vec::with_capacity(n);
let mut prev = 0i64;
for i in 0..n {
let code = (data.get(codes_at + i / 4).ok_or_else(err)? >> (2 * (i % 4))) & 3;
let delta = match code {
0 => common,
c => {
let w = (width >> (3 - c as usize)).max(1);
let v = read_signed(data, at, w).ok_or_else(err)?;
at += w;
v
}
};
prev = if width == 4 { i64::from((prev as i32).wrapping_add(delta as i32)) } else { prev.wrapping_add(delta) };
out.push(prev);
}
Ok(out)
let width = match width {
4 => craft_usd::integers::Width::I32,
8 => craft_usd::integers::Width::I64,
_ => return Err(malformed(KIND, "unsupported integer width")),
};
craft_usd::integers::decode(data, n, width, super::MAX_DECODED_BYTES).map_err(|e| malformed(KIND, e.to_string()))
}
/// Encode integers of `width` (4 or 8) bytes (values must fit the width).
pub(crate) fn encode_ints(values: &[i64], width: usize) -> Vec<u8> {
if values.is_empty() {
return Vec::new();
}
let wrap = |v: i64| if width == 4 { i64::from(v as i32) } else { v };
let mut prev = 0i64;
let deltas: Vec<i64> = values
.iter()
.map(|&v| {
let d = wrap(v.wrapping_sub(prev));
prev = v;
d
})
.collect();
let mut counts = std::collections::HashMap::new();
for d in &deltas {
*counts.entry(*d).or_insert(0usize) += 1;
}
let common = counts.into_iter().max_by_key(|&(v, c)| (c, v)).map_or(0, |(v, _)| v);
let mut out = Vec::with_capacity(encoded_ints_len(values.len(), width));
out.extend_from_slice(&common.to_le_bytes()[..width]);
let codes_at = out.len();
out.resize(codes_at + values.len().div_ceil(4), 0);
for (i, &d) in deltas.iter().enumerate() {
let small = |bits: u32| d >= -(1i64 << (bits - 1)) && d < (1i64 << (bits - 1));
let code: u8 = if d == common {
0
} else if small(width as u32 * 2) {
1
} else if small(width as u32 * 4) {
2
} else {
3
};
if code > 0 {
let w = (width >> (3 - code as usize)).max(1);
out.extend_from_slice(&d.to_le_bytes()[..w]);
}
if let Some(b) = out.get_mut(codes_at + i / 4) {
*b |= code << (2 * (i % 4));
}
}
out
/// Encode packed integers with a combined scratch/output reservation cap.
pub(crate) fn encode_ints(values: &[i64], width: usize) -> Result<Vec<u8>> {
let width = match width {
4 => craft_usd::integers::Width::I32,
8 => craft_usd::integers::Width::I64,
_ => return Err(malformed(KIND, "unsupported integer width")),
};
craft_usd::integers::encode(values, width, super::MAX_DECODED_BYTES).map_err(|e| malformed(KIND, e.to_string()))
}
/// Error for counts that cannot be real (more items than bytes left could describe).
@@ -576,6 +576,7 @@ impl<'a> Stage<'a> {
p: m.transform_point3(Vec3::from(p)),
n: normals.as_ref().and_then(|pv| pv.get::<3>(at)).map(|v| nm * Vec3::from(v)),
uv: uvs.as_ref().and_then(|pv| pv.get::<2>(at)).map(|[u, v]| Vec2::new(u, 1.0 - v)),
color: None,
});
}
if flip {
+42 -3
View File
@@ -408,18 +408,50 @@ fn usd_extension_detects_the_encoding() {
fn integer_coding_round_trips() {
let values: Vec<i64> = vec![0, 1, 2, 3, 3, 3, -5, 1000, -70000, i64::from(i32::MAX), i64::from(i32::MIN), 7, 7, 7, 8];
for width in [4, 8] {
let enc = codec::encode_ints(&values, width);
let enc = codec::encode_ints(&values, width).unwrap();
assert!(enc.len() <= codec::encoded_ints_len(values.len(), width));
assert_eq!(codec::decode_ints(&enc, values.len(), width).unwrap(), values);
assert!(codec::decode_ints(&enc[..enc.len() - 1], values.len(), width).is_err());
}
let wide = vec![i64::MAX, i64::MIN, 0, 1 << 40];
assert_eq!(codec::decode_ints(&codec::encode_ints(&wide, 8), 4, 8).unwrap(), wide);
assert_eq!(codec::decode_ints(&codec::encode_ints(&wide, 8).unwrap(), 4, 8).unwrap(), wide);
let big: Vec<u8> = (0..5000u32).flat_map(|i| (i % 7).to_le_bytes()).collect();
assert_eq!(codec::decompress(&codec::compress(&big), big.len()).unwrap(), big);
assert_eq!(codec::decompress(&codec::compress(&big).unwrap(), big.len()).unwrap(), big);
assert!(codec::decompress(&[0, 0xF0], 10).is_err());
}
#[test]
fn shared_crate_lz4_writer_preserves_bytes() {
for len in [0, 1, 15, 16, 255, 4096] {
let data: Vec<u8> = (0..len).map(|i| (i % 251) as u8).collect();
let mut previous = vec![0];
previous.extend(lz4_flex::block::compress(&data));
assert_eq!(codec::compress(&data).unwrap(), previous);
}
}
#[test]
fn shared_integer_decoder_rejects_unsupported_width_and_oversized_output() {
assert!(codec::decode_ints(&[], 1, 3).is_err());
assert!(codec::decode_ints(&[], super::MAX_DECODED_BYTES / 8 + 1, 8).is_err());
assert!(codec::decode_ints(&[], 0, 4).unwrap().is_empty());
}
#[test]
fn shared_crate_lz4_retains_chunk_and_budget_policy() {
let mut frame = vec![2];
for raw in [b"abc".as_slice(), b"def".as_slice()] {
let block = lz4_flex::block::compress(raw);
frame.extend((block.len() as i32).to_le_bytes());
frame.extend(block);
}
frame.extend(b"old reader ignores this tail");
assert_eq!(codec::decompress(&frame, 6).unwrap(), b"abcdef");
assert!(codec::decompress(&frame, 5).is_err());
assert!(codec::decompress(&[1, 255, 255, 255, 255], 64).is_err());
assert!(codec::decompress(&[128], 64).is_err());
}
#[test]
fn malformed_usd_never_panics() {
// A small two-material mesh keeps the byte loops fast.
@@ -477,3 +509,10 @@ fn loose_layer_textures_are_packaged() {
assert_eq!((f.name, f.bytes == out.bytes, f.warnings.len()), (format!("t.{ext}"), true, 1), "{ext}");
}
}
#[test]
fn shared_integer_writer_preserves_tie_wire_and_rejects_invalid_width() {
assert_eq!(codec::encode_ints(&[1, 3, 4, 6], 4).unwrap(), vec![2, 0, 0, 0, 17, 1, 1]);
assert!(codec::encode_ints(&[1], 3).is_err());
assert!(codec::encode_ints(&[], 8).unwrap().is_empty());
}
+90 -36
View File
@@ -15,6 +15,9 @@ const MIN_VERSION: (u8, u8, u8) = (0, 4, 0);
const MAX_SECTIONS: usize = 64;
/// Items in one table or array.
const MAX_ITEMS: usize = 1 << 28;
/// Entries in the PATHS table. The declared count is tiny in the file but each entry is a
/// materialised path, so it needs its own ceiling as well as the decoded-byte budget.
const MAX_PATHS: usize = 4_000_000;
/// Spec types.
const SPEC_ATTRIBUTE: i64 = 1;
@@ -242,58 +245,73 @@ impl<'a> Crate<'a> {
/// Paths are stored as a pre-order tree: element token (negative for properties) and a
/// jump per node (-2 leaf, -1 child only, 0 sibling only, >0 child and sibling at +jump).
fn read_paths(&mut self, mut s: Cur, budget: &mut Budget) -> Result<()> {
let total = s.count(0)?;
let n = s.count(0)?;
let index = s.ints(n, 4, budget)?;
let elements = s.ints(n, 4, budget)?;
let jumps = s.ints(n, 4, budget)?;
budget.take(total.saturating_mul(64))?;
self.paths = vec![None; total];
let mut work: Vec<(usize, Option<usize>)> = vec![(0, None)];
let mut steps = 0usize;
let bad = || malformed(KIND, "corrupt path table");
while let Some((mut i, mut parent)) = work.pop() {
fn read_paths(&mut self, mut section: Cur, budget: &mut Budget) -> Result<()> {
let path_count = section.count(0)?;
let encoded_count = section.count(0)?;
let path_indices = section.ints(encoded_count, 4, budget)?;
let elements = section.ints(encoded_count, 4, budget)?;
let jumps = section.ints(encoded_count, 4, budget)?;
if path_count > MAX_PATHS {
return Err(FormatError::TooLarge(format!("the USD path table has {path_count} entries (limit {MAX_PATHS})")));
}
// Charge slot storage before allocating the table
budget.take(path_count.saturating_mul(std::mem::size_of::<Option<PathEl>>()))?;
self.paths = vec![None; path_count];
let mut pending_branches: Vec<(usize, Option<usize>)> = vec![(0, None)];
let mut visited_count = 0usize;
let path_error = || malformed(KIND, "corrupt path table");
while let Some((mut cursor_index, mut parent)) = pending_branches.pop() {
loop {
steps += 1;
if steps > n {
return Err(bad());
visited_count += 1;
if visited_count > encoded_count {
return Err(path_error());
}
let this = i;
i += 1;
let at = index.get(this).map(|v| *v as u32 as usize).filter(|p| *p < total).ok_or_else(bad)?;
let el = match parent {
let node_index = cursor_index;
cursor_index += 1;
let path_index =
path_indices.get(node_index).map(|v| *v as u32 as usize).filter(|parent_index| *parent_index < path_count).ok_or_else(path_error)?;
let path_element = match parent {
None => PathEl { text: "/".into(), parent: None, name: String::new(), prop: false },
Some(p) => {
let e = *elements.get(this).ok_or_else(bad)?;
let name = self.token(e.unsigned_abs())?.to_string();
let base = self.paths.get(p).and_then(Option::as_ref).map(|b| b.text.as_str()).ok_or_else(bad)?;
let text = match (e < 0, base) {
(true, _) => format!("{base}.{name}"),
(false, _) if name.starts_with('{') => format!("{base}{name}"),
Some(parent_index) => {
let element_token = *elements.get(node_index).ok_or_else(path_error)?;
let name = self.token(element_token.unsigned_abs())?;
let parent_path = self.paths.get(parent_index).and_then(Option::as_ref).map(|b| b.text.as_str()).ok_or_else(path_error)?;
let separator_bytes = usize::from(element_token < 0 || (!name.starts_with('{') && parent_path != "/"));
let text_bytes = parent_path.len().saturating_add(name.len()).saturating_add(separator_bytes);
// Charge both retained strings before allocating either one
budget.take(text_bytes.saturating_add(name.len()))?;
let name = name.to_string();
let text = match (element_token < 0, parent_path) {
(true, _) => format!("{parent_path}.{name}"),
(false, _) if name.starts_with('{') => format!("{parent_path}{name}"),
(false, "/") => format!("/{name}"),
(false, _) => format!("{base}/{name}"),
(false, _) => format!("{parent_path}/{name}"),
};
budget.take(text.len())?;
PathEl { text, parent: Some(p), name, prop: e < 0 }
PathEl { text, parent: Some(parent_index), name, prop: element_token < 0 }
}
};
if let Some(slot) = self.paths.get_mut(at) {
*slot = Some(el);
if let Some(slot) = self.paths.get_mut(path_index) {
*slot = Some(path_element);
}
let jump = *jumps.get(this).ok_or_else(bad)?;
let jump = *jumps.get(node_index).ok_or_else(path_error)?;
let (child, sibling) = (jump > 0 || jump == -1, jump >= 0);
if child {
if sibling {
work.push((this.checked_add(usize::try_from(jump).map_err(|_| bad())?).ok_or_else(bad)?, parent));
pending_branches.push((node_index.checked_add(usize::try_from(jump).map_err(|_| path_error())?).ok_or_else(path_error)?, parent));
}
parent = Some(at);
parent = Some(path_index);
}
if !child && !sibling {
break;
}
}
}
Ok(())
}
@@ -642,11 +660,47 @@ mod tests {
}
fn compressed_ints(out: &mut Vec<u8>, v: &[i64]) {
let comp = codec::compress(&codec::encode_ints(v, 4));
let comp = codec::compress(&codec::encode_ints(v, 4).unwrap()).unwrap();
out.extend_from_slice(&(comp.len() as u64).to_le_bytes());
out.extend_from_slice(&comp);
}
#[test]
fn repeated_path_names_count_toward_the_decode_budget() {
let name = "long_name_reused";
let mut section = Vec::new();
section.extend_from_slice(&3u64.to_le_bytes());
section.extend_from_slice(&3u64.to_le_bytes());
compressed_ints(&mut section, &[0, 1, 2]);
compressed_ints(&mut section, &[0, 0, 0]);
compressed_ints(&mut section, &[-1, 0, -2]);
let mut crate_data = crate_over(&section, (0, 8, 0));
crate_data.tokens.push(name.into());
let decoded_arrays_bytes = 3 * 3 * std::mem::size_of::<i64>();
let path_slots_bytes = 3 * std::mem::size_of::<Option<PathEl>>();
let path_text_bytes = 2 * (1 + name.len());
let one_name_budget = decoded_arrays_bytes + path_slots_bytes + path_text_bytes + name.len();
let mut insufficient_budget = Budget(one_name_budget);
let rejected = crate_data.read_paths(Cur { b: &section, pos: 0 }, &mut insufficient_budget);
assert!(matches!(rejected, Err(FormatError::TooLarge(_))), "two path names cannot fit a one-name budget");
drop(crate_data);
let mut complete_budget = Budget(one_name_budget + name.len());
let mut crate_data = crate_over(&section, (0, 8, 0));
crate_data.tokens.push(name.into());
crate_data.read_paths(Cur { b: &section, pos: 0 }, &mut complete_budget).unwrap();
assert_eq!(complete_budget.0, 0);
assert_eq!(crate_data.path(1).unwrap().text, format!("/{name}"));
assert_eq!(crate_data.path(2).unwrap().text, format!("/{name}"));
let retained_name_bytes: usize = crate_data.paths.iter().filter_map(Option::as_ref).map(|path| path.name.capacity()).sum();
assert!(retained_name_bytes >= 2 * name.len());
println!("retained names: 2, buffer capacity: {retained_name_bytes} bytes");
}
#[test]
fn compressed_float_and_int_arrays() {
let n = 20usize;
@@ -667,7 +721,7 @@ mod tests {
compressed_ints(&mut b, &idx);
let i64_at = b.len() as u64;
b.extend_from_slice(&(n as u64).to_le_bytes());
let comp = codec::compress(&codec::encode_ints(&(0..n as i64).map(|i| i << 33).collect::<Vec<_>>(), 8));
let comp = codec::compress(&codec::encode_ints(&(0..n as i64).map(|i| i << 33).collect::<Vec<_>>(), 8).unwrap()).unwrap();
b.extend_from_slice(&(comp.len() as u64).to_le_bytes());
b.extend_from_slice(&comp);
let c = crate_over(&b, (0, 8, 0));
@@ -13,14 +13,18 @@ impl LoadBudget {
self.limit_bytes - self.used_bytes
}
pub(super) fn exceeded_message(&self) -> String {
format!(
"project file decompresses to more than {} bytes ({:.2} GiB); increase Max project size on open in Settings > Project to open larger files",
self.limit_bytes,
self.limit_bytes as f64 / (1u64 << 30) as f64,
)
}
/// Reserve bytes before retaining decoded data so entries share one allocation budget.
pub(super) fn take(&mut self, bytes: u64) -> Result<(), String> {
if bytes > self.remaining() {
return Err(format!(
"project file decompresses to more than {} bytes ({:.2} GiB); increase Max project size on open in Settings > Project to open larger files",
self.limit_bytes,
self.limit_bytes as f64 / (1u64 << 30) as f64,
));
return Err(self.exceeded_message());
}
self.used_bytes += bytes;
@@ -1,81 +0,0 @@
use std::cell::Cell;
use std::io::{self, Read, Seek, SeekFrom};
/// Restrict ZIP indexing to a bounded tail and read allowance before enabling payload access.
pub(super) struct MetadataReader<'a, R> {
reader: R,
metadata_only: &'a Cell<bool>,
metadata_start: u64,
remaining_bytes: u64,
position: u64,
}
impl<'a, R: Read + Seek> MetadataReader<'a, R> {
pub(super) fn new(mut reader: R, metadata_only: &'a Cell<bool>, metadata_bytes: u64) -> io::Result<Self> {
let end = reader.seek(SeekFrom::End(0))?;
Ok(Self { reader, metadata_only, metadata_start: end.saturating_sub(metadata_bytes), remaining_bytes: metadata_bytes.saturating_mul(2), position: end })
}
}
impl<R: Read> Read for MetadataReader<'_, R> {
fn read(&mut self, bytes: &mut [u8]) -> io::Result<usize> {
if bytes.is_empty() {
return Ok(0);
}
let metadata_only = self.metadata_only.get();
if metadata_only && (self.position < self.metadata_start || self.remaining_bytes == 0) {
return Err(io::Error::new(io::ErrorKind::InvalidData, "project ZIP metadata exceeds the indexing limit"));
}
let length = if metadata_only { bytes.len().min(usize::try_from(self.remaining_bytes).unwrap_or(usize::MAX)) } else { bytes.len() };
let read = self.reader.read(bytes.get_mut(..length).unwrap_or_default())?;
self.position = self.position.saturating_add(read as u64);
if metadata_only {
self.remaining_bytes -= read as u64;
}
Ok(read)
}
}
impl<R: Seek> Seek for MetadataReader<'_, R> {
fn seek(&mut self, position: SeekFrom) -> io::Result<u64> {
self.position = self.reader.seek(position)?;
Ok(self.position)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn indexing_cannot_read_payloads_outside_the_metadata_window() {
let metadata_only = Cell::new(true);
let mut reader = MetadataReader::new(Cursor::new(vec![7; 100]), &metadata_only, 8).unwrap();
reader.seek(SeekFrom::Start(91)).unwrap();
assert!(reader.read(&mut [0; 1]).is_err());
reader.seek(SeekFrom::Start(92)).unwrap();
reader.read_exact(&mut [0; 8]).unwrap();
metadata_only.set(false);
reader.seek(SeekFrom::Start(0)).unwrap();
reader.read_exact(&mut [0; 100]).unwrap();
}
#[test]
fn repeated_metadata_scans_share_one_read_allowance() {
let metadata_only = Cell::new(true);
let mut reader = MetadataReader::new(Cursor::new(vec![7; 8]), &metadata_only, 8).unwrap();
for _ in 0..2 {
reader.seek(SeekFrom::Start(0)).unwrap();
reader.read_exact(&mut [0; 8]).unwrap();
}
reader.seek(SeekFrom::Start(0)).unwrap();
assert!(reader.read(&mut [0; 1]).is_err());
}
}
+66 -165
View File
@@ -18,7 +18,6 @@
//! count of 0, followed by `width × height × components` f32 LE. Pixels of a UV-tiled texture
//! set cover its whole atlas (`resolution` per tile, see `TileGrid`).
use std::cell::Cell;
use std::collections::{BTreeMap, HashSet};
use std::io::{Cursor, Read, Seek, Write};
use std::sync::Arc;
@@ -31,21 +30,21 @@ use zip::{CompressionMethod, ZipArchive, ZipWriter};
use self::entries::Entry;
use self::load_budget::LoadBudget;
use self::metadata_reader::MetadataReader;
use crate::{FormatError, MAX_IMAGE_SIZE, Result, malformed};
mod entries;
mod load_budget;
mod metadata_reader;
mod project_open_limit;
#[cfg(not(target_arch = "wasm32"))]
pub use entries::{DEFAULT_RAM_PERCENT, DEFAULT_RESERVED_CORES, available_save_resources, save_ram_budget, set_save_resources};
pub use project_open_limit::{DEFAULT_PROJECT_OPEN_LIMIT_GIB, project_open_limit_gib, set_project_open_limit_gib};
const KIND: &str = "project file";
#[cfg(test)]
const MAGIC: &[u8; 4] = b"TCTX";
/// Version of the `.bin` layout.
#[cfg(test)]
const BIN_VERSION: u32 = 1;
#[cfg(test)]
const BIN_HEADER_BYTES: usize = 24;
const MAX_JSON: u64 = 256 << 20;
const MAX_THUMBNAIL: u64 = 64 << 20;
@@ -98,23 +97,23 @@ pub fn save_project_bytes(pf: &ProjectFile) -> Result<Vec<u8>> {
Ok(out.into_inner())
}
/// Save atomically: `<path>.tmp` first, then rename over `path`.
/// Save atomically through a newly created temporary file beside `path`.
#[cfg(not(target_arch = "wasm32"))]
pub fn save_project(path: &std::path::Path, pf: &ProjectFile) -> Result<()> {
let mut tmp = path.as_os_str().to_owned();
tmp.push(".tmp");
let tmp = std::path::PathBuf::from(tmp);
let res = (|| -> Result<()> {
let mut file = std::io::BufWriter::new(std::fs::File::create(&tmp)?);
write_project(&mut file, pf)?;
file.into_inner().map_err(|e| e.into_error())?.sync_all()?;
std::fs::rename(&tmp, path)?;
pub fn save_project(path: &std::path::Path, project_file: &ProjectFile) -> Result<()> {
let (temporary_path, file) = crate::create_temp(path)?;
let result = (|| -> Result<()> {
let mut file = std::io::BufWriter::new(file);
write_project(&mut file, project_file)?;
file.into_inner().map_err(|error| error.into_error())?.sync_all()?;
std::fs::rename(&temporary_path, path)?;
Ok(())
})();
if res.is_err() {
let _ = std::fs::remove_file(&tmp);
if result.is_err() {
let _ = std::fs::remove_file(&temporary_path);
}
res
result
}
fn zip_err(e: zip::result::ZipError) -> FormatError {
@@ -124,6 +123,7 @@ fn zip_err(e: zip::result::ZipError) -> FormatError {
}
}
#[cfg(test)]
fn header(w: u32, h: u32, comps: usize, count: usize) -> Vec<u8> {
let mut b = MAGIC.to_vec();
for v in [BIN_VERSION, w, h, comps as u32, count as u32] {
@@ -132,19 +132,18 @@ fn header(w: u32, h: u32, comps: usize, count: usize) -> Vec<u8> {
b
}
fn write_tiles(w: &mut impl Write, img: &TileImage) -> std::io::Result<()> {
w.write_all(&header(img.width(), img.height(), img.comps(), img.allocated_count()))?;
let mut buf = Vec::with_capacity(TILE_AREA * img.comps() * 2 + 8);
for (tx, ty) in img.allocated() {
buf.clear();
buf.extend_from_slice(&tx.to_le_bytes());
buf.extend_from_slice(&ty.to_le_bytes());
for v in img.tile(tx, ty).unwrap_or_default() {
buf.extend_from_slice(&v.to_le_bytes());
}
w.write_all(&buf)?;
fn codec_write_error(error: craft_tcraft::Error) -> std::io::Error {
match error {
craft_tcraft::Error::Io(error) => error,
other => std::io::Error::other(other),
}
Ok(())
}
fn write_tiles(w: &mut impl Write, img: &TileImage) -> std::io::Result<()> {
let layout = craft_tcraft::Layout { width: img.width(), height: img.height(), components: img.comps() };
let count = u32::try_from(img.allocated_count()).map_err(std::io::Error::other)?;
let tiles = img.allocated().map(|(x, y)| (x, y, img.tile(x, y).unwrap_or_default()));
craft_tcraft::write_tiles(w, layout, count, tiles).map_err(codec_write_error)
}
/// Keep `[a-z0-9]`, at most 8 characters (the mesh entry's extension).
@@ -169,18 +168,12 @@ fn unique_entry_name(dir: &str, name: &str, used: &mut HashSet<String>) -> Strin
entry
}
/// Floats converted to bytes at a time when a baked map streams into the compressor.
#[cfg(test)]
const BAKED_CHUNK: usize = 1 << 16;
fn write_baked(w: &mut dyn Write, res: u32, h: u32, comps: usize, data: &[f32]) -> std::io::Result<()> {
w.write_all(&header(res, h, comps, 0))?;
let mut buf = Vec::with_capacity(BAKED_CHUNK * 4);
for chunk in data.chunks(BAKED_CHUNK) {
buf.clear();
buf.extend(chunk.iter().flat_map(|v| v.to_le_bytes()));
w.write_all(&buf)?;
}
Ok(())
let layout = craft_tcraft::Layout { width: res, height: h, components: comps };
craft_tcraft::write_baked(w, layout, data).map_err(codec_write_error)
}
/// The zip entries of a project file, in file order. Bodies run later, possibly on other threads.
@@ -304,110 +297,34 @@ fn read_all(reader: &mut impl Read, cap: u64, budget: &mut LoadBudget, what: &st
Ok(out)
}
/// Decode bounded chunks so a false header cannot allocate an entire missing map.
fn read_f32s(reader: &mut impl Read, count: usize, what: &str) -> Result<Vec<f32>> {
let mut values = Vec::new();
let mut bytes = vec![0u8; BAKED_CHUNK.min(count) * 4];
let mut remaining = count;
while remaining > 0 {
let chunk_values = BAKED_CHUNK.min(remaining);
let chunk = bytes.get_mut(..chunk_values * 4).ok_or_else(|| malformed(KIND, format!("{what}: invalid float chunk")))?;
read_exact(reader, chunk, what)?;
values.try_reserve(chunk_values).map_err(|_| FormatError::TooLarge(format!("{what}: not enough memory for {count} values")))?;
values.extend(chunk.as_chunks::<4>().0.iter().map(|bytes| f32::from_le_bytes(*bytes)));
remaining -= chunk_values;
}
Ok(values)
}
fn read_exact(r: &mut impl Read, buf: &mut [u8], what: &str) -> Result<()> {
r.read_exact(buf).map_err(|e| malformed(KIND, format!("{what}: {e}")))
}
fn u32_at(b: &[u8], i: usize) -> u32 {
b.get(i * 4..i * 4 + 4).map(|s| u32::from_le_bytes([s[0], s[1], s[2], s[3]])).unwrap_or(0)
}
/// Read and check a `.bin` header: returns (width, height, comps, count).
fn read_header(r: &mut impl Read, what: &str) -> Result<(u32, u32, usize, usize)> {
let mut h = [0u8; BIN_HEADER_BYTES];
read_exact(r, &mut h, what)?;
if h.get(..4) != Some(MAGIC.as_slice()) {
return Err(malformed(KIND, format!("{what}: bad magic")));
}
let v = u32_at(&h, 1);
if v > BIN_VERSION {
return Err(FormatError::NewerVersion { found: v, supported: BIN_VERSION });
}
Ok((u32_at(&h, 2), u32_at(&h, 3), u32_at(&h, 4) as usize, u32_at(&h, 5) as usize))
}
/// Error unless the entry has no bytes left (also makes the zip reader verify the CRC).
fn expect_end(r: &mut impl Read, what: &str) -> Result<()> {
let mut b = [0u8; 1];
match r.read(&mut b) {
Ok(0) => Ok(()),
Ok(_) => Err(malformed(KIND, format!("{what}: trailing data"))),
Err(e) => Err(malformed(KIND, format!("{what}: {e}"))),
}
/// Transfer the bytes charged by the shared codec to this project's one load budget.
fn codec_result<T>(result: std::result::Result<T, craft_tcraft::Error>, shared: &craft_zip::Budget, budget: &mut LoadBudget, what: &str) -> Result<T> {
budget.take(shared.used()).map_err(FormatError::TooLarge)?;
result.map_err(|error| match error {
craft_tcraft::Error::Budget(_) => FormatError::TooLarge(budget.exceeded_message()),
craft_tcraft::Error::Limit(message) => FormatError::TooLarge(format!("{what}: {message}")),
craft_tcraft::Error::NewerVersion(found) => FormatError::NewerVersion { found, supported: craft_tcraft::VERSION },
other => malformed(KIND, format!("{what}: {other}")),
})
}
fn read_tiles(reader: &mut impl Read, size: (u32, u32), components: usize, budget: &mut LoadBudget, what: &str) -> Result<TileImage> {
budget.take(BIN_HEADER_BYTES as u64).map_err(FormatError::TooLarge)?;
let (width, height, stored_components, count) = read_header(reader, what)?;
if (width, height) != size || stored_components != components {
return Err(malformed(
KIND,
format!("{what}: {width}×{height}×{stored_components} does not match the texture set ({}×{}×{components})", size.0, size.1),
));
}
let mut image = TileImage::new(width, height, components);
if count > (image.tiles_x() as usize) * (image.tiles_y() as usize) {
return Err(malformed(KIND, format!("{what}: {count} tiles is more than the image holds")));
}
let mut bytes = vec![0u8; TILE_AREA * components * 2];
let mut position_bytes = [0u8; 8];
for _ in 0..count {
budget.take((position_bytes.len() + bytes.len()) as u64).map_err(FormatError::TooLarge)?;
read_exact(reader, &mut position_bytes, what)?;
let (tx, ty) = (u32_at(&position_bytes, 0), u32_at(&position_bytes, 1));
if tx >= image.tiles_x() || ty >= image.tiles_y() || image.is_allocated(tx, ty) {
return Err(malformed(KIND, format!("{what}: bad or repeated tile ({tx}, {ty})")));
}
read_exact(reader, &mut bytes, what)?;
let data: Vec<u16> = bytes.as_chunks::<2>().0.iter().map(|b| u16::from_le_bytes(*b)).collect();
image.set_tile(tx, ty, Some(Arc::new(data)));
}
expect_end(reader, what)?;
Ok(image)
let layout = craft_tcraft::Layout { width: size.0, height: size.1, components };
let mut shared = craft_zip::Budget::new("project", budget.remaining());
let mut image = None;
let result = craft_tcraft::read_tiles(reader, layout, MAX_TEXELS, &mut shared, |x, y, values| {
image.get_or_insert_with(|| TileImage::new(size.0, size.1, components)).set_tile(x, y, Some(Arc::new(values)));
Ok(())
});
codec_result(result, &shared, budget, what)?;
Ok(image.unwrap_or_else(|| TileImage::new(size.0, size.1, components)))
}
/// A baked map: (width, height, data).
fn read_baked(reader: &mut impl Read, declared_bytes: u64, components: usize, budget: &mut LoadBudget, what: &str) -> Result<(u32, u32, Vec<f32>)> {
budget.take(BIN_HEADER_BYTES as u64).map_err(FormatError::TooLarge)?;
let (width, height, stored_components, _) = read_header(reader, what)?;
if width == 0 || height == 0 || u64::from(width) * u64::from(height) > MAX_TEXELS || stored_components != components {
return Err(malformed(KIND, format!("{what}: bad size {width}×{height}×{stored_components}")));
}
let value_count = u64::from(width) * u64::from(height) * components as u64;
let payload_bytes = value_count * 4;
if declared_bytes != BIN_HEADER_BYTES as u64 + payload_bytes {
return Err(malformed(KIND, format!("{what}: {declared_bytes} bytes for a {width}×{height}×{components} map")));
}
budget.take(payload_bytes).map_err(FormatError::TooLarge)?;
let value_count = usize::try_from(value_count).map_err(|_| FormatError::TooLarge(format!("{what}: too many values for this platform")))?;
let data = read_f32s(reader, value_count, what)?;
expect_end(reader, what)?;
Ok((width, height, data))
let mut shared = craft_zip::Budget::new("project", budget.remaining());
let result = craft_tcraft::read_baked(reader, declared_bytes, components, MAX_TEXELS, &mut shared);
let (layout, values) = codec_result(result, &shared, budget, what)?;
Ok((layout.width, layout.height, values))
}
/// Texture set holding a paint layer, a content effect or a mask effect id.
@@ -422,47 +339,31 @@ fn load<R: Read + Seek>(reader: R) -> Result<ProjectFile> {
}
fn check_archive<R: Read + Seek>(zip: &mut ZipArchive<R>, limit_bytes: u64) -> Result<()> {
if zip.len() > MAX_ENTRIES {
return Err(FormatError::TooLarge(format!("project file has more than {MAX_ENTRIES} entries")));
}
// Count every declared payload, including ignored entries, before any decompression
let mut declared_budget = LoadBudget::new(limit_bytes);
for index in 0..zip.len() {
let entry = zip.by_index_raw(index).map_err(zip_err)?;
declared_budget.take(entry.size()).map_err(FormatError::TooLarge)?;
}
// Shared compressed ranges can amplify one payload into many apparent files
if zip.has_overlapping_entries().map_err(zip_err)? {
return Err(malformed(KIND, "overlapping ZIP entries are not supported"));
}
Ok(())
craft_zip::inspect_seekable(zip, MAX_ENTRIES, limit_bytes).map_err(|error| match error {
craft_zip::InspectError::TooManyEntries { .. } => FormatError::TooLarge(format!("project file has more than {MAX_ENTRIES} entries")),
craft_zip::InspectError::DeclaredBytesExceeded { .. } => FormatError::TooLarge(LoadBudget::new(limit_bytes).exceeded_message()),
craft_zip::InspectError::OverlappingEntries => malformed(KIND, "overlapping ZIP entries are not supported"),
craft_zip::InspectError::Zip(error) => zip_err(error),
})
}
fn open_archive<'a, R: Read + Seek>(reader: R, metadata_only: &'a Cell<bool>, metadata_bytes: u64) -> Result<ZipArchive<MetadataReader<'a, R>>> {
let reader = MetadataReader::new(reader, metadata_only, metadata_bytes)?;
fn open_archive<R: Read + Seek>(reader: R, metadata_bytes: u64) -> Result<ZipArchive<craft_zip::MetadataReader<R>>> {
let (reader, indexing) = craft_zip::MetadataReader::new(reader, metadata_bytes)?;
// Project archives start at byte zero; avoid scanning payloads to guess an embedded ZIP offset
let config = zip::read::Config { archive_offset: zip::read::ArchiveOffset::Known(0) };
let archive = ZipArchive::with_config(config, reader).map_err(zip_err)?;
metadata_only.set(false);
indexing.finish();
Ok(archive)
}
fn load_with_budget<R: Read + Seek>(reader: R, mut budget: LoadBudget) -> Result<ProjectFile> {
let metadata_only = Cell::new(true);
let mut zip = open_archive(reader, &metadata_only, MAX_ZIP_METADATA_BYTES)?;
let mut zip = open_archive(reader, MAX_ZIP_METADATA_BYTES)?;
check_archive(&mut zip, budget.remaining())?;
let json = read_all(&mut zip.by_name("project.json").map_err(zip_err)?, MAX_JSON, &mut budget, "project.json")?;
#[derive(serde::Deserialize)]
struct Version {
format_version: u32,
}
let v: Version = serde_json::from_slice(&json).map_err(|e| malformed(KIND, format!("project.json: {e}")))?;
if v.format_version > FORMAT_VERSION {
return Err(FormatError::NewerVersion { found: v.format_version, supported: FORMAT_VERSION });
let version = craft_tcraft::project_version(&json).map_err(|e| malformed(KIND, format!("project.json: {e}")))?;
if version > FORMAT_VERSION {
return Err(FormatError::NewerVersion { found: version, supported: FORMAT_VERSION });
}
let (project, mut recovered) = recovery::read_project(&json).map_err(|e| malformed(KIND, format!("project.json: {e}")))?;
+19 -11
View File
@@ -82,6 +82,15 @@ fn round_trip_is_identical_and_deterministic() {
assert_same(&builtin, &load_project_bytes(&save_project_bytes(&builtin).unwrap()).unwrap());
}
#[test]
fn shared_project_schema_round_trips_the_app_document() {
let project = sample().pf.project;
let original = serde_json::to_value(&project).unwrap();
let document: craft_tcraft::ProjectDocument = serde_json::from_value(original.clone()).unwrap();
assert_eq!(document.format_version, project.format_version);
assert_eq!(serde_json::to_value(document).unwrap(), original);
}
#[test]
fn legacy_bakes_load_as_is_without_layout_conversion() {
let mut sample = sample();
@@ -803,13 +812,17 @@ fn baked_dimensions_must_match_the_entry_before_reserving_payload_bytes() {
#[test]
fn streamed_float_chunks_preserve_bits_across_the_chunk_boundary() {
let values: Vec<u32> = (0..BAKED_CHUNK + 3).map(|index| (index as u32).wrapping_mul(0x9e3779b9)).collect();
let bytes: Vec<u8> = values.iter().flat_map(|bits| bits.to_le_bytes()).collect();
let mut bytes = header(values.len() as u32, 1, 1, 0);
bytes.extend(values.iter().flat_map(|bits| bits.to_le_bytes()));
let declared = bytes.len() as u64;
let mut reader = Cursor::new(bytes);
let mut budget = LoadBudget::new(declared);
let decoded = read_f32s(&mut reader, values.len(), "baked map").unwrap();
let (_, _, decoded) = read_baked(&mut reader, declared, 1, &mut budget, "baked map").unwrap();
assert_eq!(decoded.into_iter().map(f32::to_bits).collect::<Vec<_>>(), values);
assert_eq!(reader.position(), (values.len() * 4) as u64);
assert_eq!(reader.position(), declared);
assert_eq!(budget.remaining(), 0);
}
#[test]
@@ -818,23 +831,19 @@ fn archive_indexing_does_not_scan_large_payloads_and_rejects_large_metadata() {
writer.start_file("resource", SimpleFileOptions::default().compression_method(CompressionMethod::Stored)).unwrap();
writer.write_all(&vec![7; 1 << 20]).unwrap();
let bytes = writer.finish().unwrap().into_inner();
let metadata_only = Cell::new(true);
let mut archive = open_archive(Cursor::new(bytes), &metadata_only, 4096).unwrap();
let mut archive = open_archive(Cursor::new(bytes), 4096).unwrap();
let mut first_byte = [0; 1];
archive.by_index(0).unwrap().read_exact(&mut first_byte).unwrap();
assert_eq!(first_byte, [7]);
assert!(!metadata_only.get());
let mut writer = ZipWriter::new(Cursor::new(Vec::new()));
writer.start_file("a".repeat(5000), SimpleFileOptions::default()).unwrap();
writer.write_all(&[7]).unwrap();
let bytes = writer.finish().unwrap().into_inner();
let metadata_only = Cell::new(true);
assert!(open_archive(Cursor::new(bytes), &metadata_only, 4096).is_err());
assert!(metadata_only.get(), "failed indexing must not enable payload access");
assert!(open_archive(Cursor::new(bytes), 4096).is_err());
}
#[test]
@@ -845,9 +854,8 @@ fn zip64_entry_counts_remain_supported_and_bounded() {
writer.start_file(format!("e{index}"), SimpleFileOptions::default().compression_method(CompressionMethod::Stored)).unwrap();
}
let bytes = writer.finish().unwrap().into_inner();
let metadata_only = Cell::new(true);
let mut archive = open_archive(Cursor::new(bytes), &metadata_only, MAX_ZIP_METADATA_BYTES).unwrap();
let mut archive = open_archive(Cursor::new(bytes), MAX_ZIP_METADATA_BYTES).unwrap();
let result = check_archive(&mut archive, 0);
assert_eq!(archive.len(), count);
+96 -53
View File
@@ -20,7 +20,7 @@ pub struct UvGBuffer {
pub tri: Vec<u32>,
/// Barycentric weights of the triangle's second and third vertices.
pub bary: Vec<[f32; 2]>,
/// Texels written by more than one triangle (overlapping UVs).
/// Covers past the first on texels where any cover has the texel center strictly inside (overlapping UVs; an edge two neighbours share is no overlap)
pub overlaps: u32,
}
@@ -41,74 +41,95 @@ fn edge(a: Vec2, b: Vec2, p: Vec2) -> f32 {
(b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x)
}
/// Twice-areas (in texels²) below this are degenerate triangles, which cover nothing
const MIN_DOUBLE_AREA: f32 = 1e-12;
/// How far outside a triangle (as a share of its double area) a texel center may sit and still count as covered, so shared edges leave no gaps
const EDGE_TOLERANCE: f32 = 1e-6;
impl UvGBuffer {
/// Rasterize the triangles of material `material` at `width × height`.
/// Rasterize the triangles of material `material` at `width × height`
pub fn new(mesh: &Mesh, material: usize, width: u32, height: u32) -> UvGBuffer {
let (w, h) = (width.max(1), height.max(1));
let size = Vec2::new(w as f32, h as f32);
let bands = h.div_ceil(BAND) as usize;
let mut bins: Vec<Vec<u32>> = vec![Vec::new(); bands];
for t in mesh.triangles_of(material) {
let Some([a, b, c]) = mesh.tri(t) else { continue };
let (Some(ua), Some(ub), Some(uc)) = (mesh.uvs.get(a), mesh.uvs.get(b), mesh.uvs.get(c)) else { continue };
let y0 = (ua.y.min(ub.y).min(uc.y) * size.y).floor().max(0.0) as i64;
let y1 = (ua.y.max(ub.y).max(uc.y) * size.y).ceil().min(size.y) as i64;
if y1 <= y0 || !ua.is_finite() || !ub.is_finite() || !uc.is_finite() {
let (width, height) = (width.max(1), height.max(1));
let size = Vec2::new(width as f32, height as f32);
let corner_uvs = |triangle: usize| {
let [first, second, third] = mesh.tri(triangle)?;
Some([*mesh.uvs.get(first)?, *mesh.uvs.get(second)?, *mesh.uvs.get(third)?])
};
// Sort triangles into bands of rows so each band rasterizes on its own thread
let mut bands: Vec<Vec<u32>> = vec![Vec::new(); height.div_ceil(BAND) as usize];
for triangle in mesh.triangles_of(material) {
let Some(uvs) = corner_uvs(triangle) else { continue };
let top = (uvs.iter().map(|uv| uv.y).fold(f32::INFINITY, f32::min) * size.y).floor().max(0.0) as i64;
let bottom = (uvs.iter().map(|uv| uv.y).fold(f32::NEG_INFINITY, f32::max) * size.y).ceil().min(size.y) as i64;
if bottom <= top || !uvs.iter().all(|uv| uv.is_finite()) {
continue;
}
let (b0, b1) = ((y0 / BAND as i64) as usize, ((y1 - 1).max(0) / BAND as i64) as usize);
for bin in bins.iter_mut().take(b1 + 1).skip(b0) {
bin.push(t as u32);
let (first_band, last_band) = ((top / BAND as i64) as usize, ((bottom - 1).max(0) / BAND as i64) as usize);
for band in bands.iter_mut().take(last_band + 1).skip(first_band) {
band.push(triangle as u32);
}
}
let n = w as usize * h as usize;
let mut tri = vec![NO_TRI; n];
let mut bary = vec![[0.0f32; 2]; n];
let band_len = (BAND * w) as usize;
let texel_count = width as usize * height as usize;
let mut tri = vec![NO_TRI; texel_count];
let mut bary = vec![[0.0f32; 2]; texel_count];
let band_texels = (BAND * width) as usize;
let overlaps: u32 = tri
.par_chunks_mut(band_len)
.zip(bary.par_chunks_mut(band_len))
.par_chunks_mut(band_texels)
.zip(bary.par_chunks_mut(band_texels))
.enumerate()
.map(|(bi, (tri, bary))| {
let mut overlaps = 0u32;
let row0 = bi as u32 * BAND;
let rows = (tri.len() as u32) / w;
for &t in bins.get(bi).map(Vec::as_slice).unwrap_or(&[]) {
let Some([a, b, c]) = mesh.tri(t as usize) else { continue };
let (Some(&ua), Some(&ub), Some(&uc)) = (mesh.uvs.get(a), mesh.uvs.get(b), mesh.uvs.get(c)) else { continue };
let (pa, pb, pc) = (ua * size, ub * size, uc * size);
let area = edge(pa, pb, pc);
if area.abs() < 1e-12 {
.map(|(band_index, (band_tri, band_bary))| {
// Keep cover counts and strict interiors independent of triangle order
let mut covers = vec![0u32; band_tri.len()];
let mut strict_cover = vec![false; band_tri.len()];
let first_row = band_index as u32 * BAND;
let band_rows = (band_tri.len() as u32) / width;
for &triangle in bands.get(band_index).map(Vec::as_slice).unwrap_or(&[]) {
let Some(uvs) = corner_uvs(triangle as usize) else { continue };
let [a, b, c] = uvs.map(|uv| uv * size);
let double_area = edge(a, b, c);
if double_area.abs() < MIN_DOUBLE_AREA {
continue;
}
let eps = -1e-6 * area.abs();
let x0 = pa.x.min(pb.x).min(pc.x).floor().max(0.0) as u32;
let x1 = (pa.x.max(pb.x).max(pc.x).ceil().min(w as f32) as u32).min(w);
let y0 = (pa.y.min(pb.y).min(pc.y).floor().max(row0 as f32) as u32).max(row0);
let y1 = (pa.y.max(pb.y).max(pc.y).ceil() as u32).min(row0 + rows);
for y in y0..y1 {
for x in x0..x1 {
let p = Vec2::new(x as f32 + 0.5, y as f32 + 0.5);
let (w0, w1, w2) = (edge(pb, pc, p) * area.signum(), edge(pc, pa, p) * area.signum(), edge(pa, pb, p) * area.signum());
if w0 < eps || w1 < eps || w2 < eps {
let tolerance = EDGE_TOLERANCE * double_area.abs();
let (sign, inverse_area) = (double_area.signum(), 1.0 / double_area.abs());
let left = a.x.min(b.x).min(c.x).floor().max(0.0) as u32;
let right = (a.x.max(b.x).max(c.x).ceil().min(width as f32) as u32).min(width);
let top = (a.y.min(b.y).min(c.y).floor().max(first_row as f32) as u32).max(first_row);
let bottom = (a.y.max(b.y).max(c.y).ceil() as u32).min(first_row + band_rows);
for y in top..bottom {
for x in left..right {
let center = Vec2::new(x as f32 + 0.5, y as f32 + 0.5);
let weights = [edge(b, c, center) * sign, edge(c, a, center) * sign, edge(a, b, center) * sign];
if weights.iter().any(|weight| *weight < -tolerance) {
continue;
}
let i = ((y - row0) * w + x) as usize;
if let (Some(slot), Some(bw)) = (tri.get_mut(i), bary.get_mut(i)) {
if *slot != NO_TRI {
overlaps += 1;
}
*slot = t;
let inv = 1.0 / area.abs();
*bw = [w1 * inv, w2 * inv];
}
let index = ((y - first_row) * width + x) as usize;
let (Some(slot), Some(slot_bary), Some(cover_count), Some(strict)) =
(band_tri.get_mut(index), band_bary.get_mut(index), covers.get_mut(index), strict_cover.get_mut(index))
else {
continue;
};
*cover_count += 1;
*strict |= weights.iter().all(|weight| *weight > tolerance);
*slot = triangle;
*slot_bary = [weights[1] * inverse_area, weights[2] * inverse_area];
}
}
}
overlaps
// Every cover past the first overlaps, unless all of them only touch the texel center with an edge (neighbours sharing it)
covers.iter().zip(&strict_cover).filter(|(_, strict)| **strict).map(|(count, _)| count.saturating_sub(1)).sum::<u32>()
})
.sum();
UvGBuffer { width: w, height: h, tri, bary, overlaps }
UvGBuffer { width, height, tri, bary, overlaps }
}
pub fn covered(&self, i: usize) -> bool {
@@ -157,7 +178,7 @@ mod tests {
#[test]
fn half_triangle_covers_texels_with_centers_inside() {
// One triangle covering the lower-left half of UV space (v down: y = v·H).
// UV v increases down, so this triangle covers the lower-left half
let m =
Mesh::from_parts(vec![Vec3::ZERO, Vec3::X, Vec3::Y], None, vec![Vec2::new(0.0, 0.0), Vec2::new(0.0, 1.0), Vec2::new(1.0, 1.0)], vec![0, 1, 2], "m")
.unwrap();
@@ -171,6 +192,28 @@ mod tests {
assert_eq!(g.overlaps, 0);
}
#[test]
fn neighbours_sharing_an_edge_through_texel_centers_do_not_overlap() {
// An 8×8 grid of quads at 512 texels: each quad's diagonal runs through 64 texel centers
let grid = primitives::plane(1.0, 8);
let gbuffer = UvGBuffer::new(&grid, 0, 512, 512);
assert_eq!(gbuffer.covered_count(), 512 * 512);
assert_eq!(gbuffer.overlaps, 0);
}
#[test]
fn an_edge_through_a_covered_texel_center_counts_both_edge_covers() {
// The texel center (0.5, 0.5) lies inside the first triangle and on the diagonal the other two share
let uv = vec![Vec2::new(0.0, 0.0), Vec2::new(1.0, 0.2), Vec2::new(0.2, 1.0), Vec2::new(1.0, 1.0), Vec2::new(1.0, 0.0), Vec2::new(0.0, 1.0)];
let positions = vec![Vec3::ZERO; uv.len()];
// In either order: the inside cover first, or the two edge covers first
for indices in [vec![0, 1, 2, 0, 3, 4, 0, 5, 3], vec![0, 3, 4, 0, 5, 3, 0, 1, 2]] {
let m = Mesh::from_parts(positions.clone(), None, uv.clone(), indices.clone(), "m").unwrap();
assert_eq!(UvGBuffer::new(&m, 0, 1, 1).overlaps, 2, "{indices:?}");
}
}
#[test]
fn overlapping_and_degenerate_triangles() {
let uv = vec![Vec2::new(0.0, 0.0), Vec2::new(1.0, 0.0), Vec2::new(0.0, 1.0), Vec2::new(0.5, 0.5)];
+82 -59
View File
@@ -7,6 +7,9 @@ use crate::{GeomError, Result};
/// An edge by the bits of its two end positions, the lower one first
type EdgePositions = ([u32; 3], [u32; 3]);
/// Normals closer than about one degree are the same normal: the edge between them is smooth
const SAME_NORMAL_MIN_COSINE: f32 = 0.9998;
/// A contiguous index range drawn with one material.
#[derive(Clone, Debug, PartialEq)]
pub struct SubMesh {
@@ -85,36 +88,37 @@ impl Mesh {
/// split it from its twin), per material in `0..materials.len().max(1)`.
pub fn wire_edges(&self) -> Vec<Vec<(u32, u32)>> {
(0..self.materials.len().max(1))
.map(|m| {
// Edge → (real in some triangle, triangles using it), in first-seen order.
.map(|material| {
// Keep first-seen edge order while counting its triangle uses
let mut edges: Vec<((u32, u32), bool, u8)> = Vec::new();
let mut at: std::collections::HashMap<(u32, u32), usize> = std::collections::HashMap::new();
for t in self.triangles_of(m) {
let Some(v) = self.indices.get(t * 3..t * 3 + 3) else { continue };
let hidden = self.diagonals.get(t).copied().unwrap_or(0);
for (i, (a, b)) in [(v[0], v[1]), (v[1], v[2]), (v[2], v[0])].into_iter().enumerate() {
let key = (a.min(b), a.max(b));
let real = hidden >> i & 1 == 0;
let e = *at.entry(key).or_insert_with(|| {
let mut edge_indices: std::collections::HashMap<(u32, u32), usize> = std::collections::HashMap::new();
for triangle in self.triangles_of(material) {
let Some(vertices) = self.indices.get(triangle * 3..triangle * 3 + 3) else { continue };
let hidden = self.diagonals.get(triangle).copied().unwrap_or(0);
for (side, (from, to)) in [(vertices[0], vertices[1]), (vertices[1], vertices[2]), (vertices[2], vertices[0])].into_iter().enumerate() {
let key = (from.min(to), from.max(to));
let is_polygon_edge = hidden >> side & 1 == 0;
let edge_index = *edge_indices.entry(key).or_insert_with(|| {
edges.push((key, false, 0));
edges.len() - 1
});
if let Some((_, r, n)) = edges.get_mut(e) {
*r |= real;
*n = n.saturating_add(1);
if let Some((_, edge_is_polygon_side, triangle_count)) = edges.get_mut(edge_index) {
*edge_is_polygon_side |= is_polygon_edge;
*triangle_count = triangle_count.saturating_add(1);
}
}
}
edges.into_iter().filter(|(_, real, n)| *real || *n == 1).map(|(k, ..)| k).collect()
edges.into_iter().filter(|(_, is_polygon_edge, triangle_count)| *is_polygon_edge || *triangle_count == 1).map(|(edge, ..)| edge).collect()
})
.collect()
}
/// Edges where the UV layout splits: two triangles meet there in 3D but not in UV space, per material in `0..materials.len().max(1)`
pub fn uv_seam_edges(&self) -> Vec<Vec<(u32, u32)>> {
/// Hard edges the UV layout does not split, which bake visible lines into the normal map: two triangles meet there with different normals but the same UVs, per material in `0..materials.len().max(1)`
pub fn missing_seam_edges(&self) -> Vec<Vec<(u32, u32)>> {
// Signed zero denotes the same position on either side of an imported seam
let bits = |position: Vec3| position.to_array().map(|coordinate| if coordinate == 0.0 { 0 } else { coordinate.to_bits() });
let mut seams = vec![Vec::new(); self.materials.len().max(1)];
// Match across materials before grouping the lines for texture-set visibility
let mut sides: std::collections::HashMap<EdgePositions, Vec<(u32, u32, usize)>> = std::collections::HashMap::new();
for material in 0..seams.len() {
@@ -132,10 +136,19 @@ impl Mesh {
}
let uv = |index: u32| self.uvs.get(index as usize).copied();
// Imported normals need not be unit length; two missing normals agree
let direction = |index: u32| self.normals.get(index as usize).map_or(Vec3::ZERO, |normal| normal.normalize_or_zero());
let same_normal = |a: u32, b: u32| {
let (a, b) = (direction(a), direction(b));
a == b || a.dot(b) >= SAME_NORMAL_MIN_COSINE
};
for pairs in sides.into_values() {
let Some(&(first_from, first_to, _)) = pairs.first() else { continue };
if pairs.iter().any(|&(from, to, _)| (uv(from), uv(to)) != (uv(first_from), uv(first_to))) {
let uv_split = pairs.iter().any(|&(from, to, _)| (uv(from), uv(to)) != (uv(first_from), uv(first_to)));
let hard = pairs.iter().any(|&(from, to, _)| !same_normal(from, first_from) || !same_normal(to, first_to));
if hard && !uv_split {
for (from, to, material) in pairs {
if let Some(edges) = seams.get_mut(material) {
edges.push((from.min(to), from.max(to)));
@@ -471,67 +484,77 @@ mod tests {
fn wire_edges_leave_out_polygon_diagonals() {
let mut m = quad();
assert_eq!(m.wire_edges(), vec![vec![(0, 1), (1, 2), (0, 2), (2, 3), (0, 3)]], "triangles show every edge");
// Edge 2 of (0, 1, 2) and edge 0 of (0, 2, 3) split the quad.
// Hide the quad's shared diagonal
m.diagonals = vec![0b100, 0b001];
assert_eq!(m.wire_edges(), vec![vec![(0, 1), (1, 2), (2, 3), (0, 3)]]);
// A diagonal whose twin a seam split off (vertex 4 copies vertex 0) stays.
// Keep the diagonal when a seam splits its twin
let mut seam = m.clone();
seam.indices[3] = 4;
assert_eq!(seam.wire_edges()[0].len(), 6);
// Appending keeps each side's diagonals on its own triangles.
// Appending preserves each side's own diagonals
let mut a = Mesh::from_parts(m.positions.clone(), None, m.uvs.clone(), vec![0, 1, 2], "A").unwrap();
a.append(&m);
assert_eq!(a.diagonals, vec![0, 0b100, 0b001]);
assert_eq!(crate::primitives::plane(1.0, 2).wire_edges()[0].len(), 12, "a 2×2 grid of quads has 12 edges");
}
#[test]
fn uv_seams_are_where_the_uv_layout_splits() {
let mesh = quad();
assert_eq!(mesh.uv_seam_edges(), vec![Vec::<(u32, u32)>::new()], "one UV island has no seam");
// Vertex 4 copies vertex 0's position with another UV: the diagonal (0, 2) / (4, 2) is cut
let mut seam = mesh.clone();
seam.positions.push(seam.positions[0]);
seam.normals.push(seam.normals[0]);
seam.tangents.push(seam.tangents[0]);
seam.uvs.push(Vec2::new(0.9, 0.9));
seam.indices[3] = 4;
assert_eq!(seam.uv_seam_edges(), vec![vec![(0, 2), (2, 4)]]);
// The same split with the same UV is a hard edge, not a seam
let mut hard = seam.clone();
hard.uvs[4] = hard.uvs[0];
assert_eq!(hard.uv_seam_edges(), vec![Vec::<(u32, u32)>::new()]);
}
#[test]
fn uv_seams_join_positions_with_opposite_signed_zero() {
let mut mesh = quad();
mesh.positions.push(Vec3::new(-0.0, 0.0, 0.0));
mesh.normals.push(mesh.normals[0]);
mesh.tangents.push(mesh.tangents[0]);
mesh.uvs.push(Vec2::new(0.9, 0.9));
mesh.indices[3] = 4;
assert_eq!(mesh.positions[0], mesh.positions[4]);
assert_eq!(mesh.uv_seam_edges(), vec![vec![(0, 2), (2, 4)]]);
}
#[test]
fn uv_seams_between_materials_belong_to_both_materials() {
/// `quad` with vertex 4 a copy of vertex 0 at `uv` with `normal`, used by the second triangle: the diagonal (0, 2) / (4, 2) has two sides
fn split_quad(uv: Vec2, normal: Vec3) -> Mesh {
let mut mesh = quad();
mesh.positions.push(mesh.positions[0]);
mesh.normals.push(mesh.normals[0]);
mesh.normals.push(normal);
mesh.tangents.push(mesh.tangents[0]);
mesh.uvs.push(Vec2::new(0.9, 0.9));
mesh.uvs.push(uv);
mesh.indices[3] = 4;
mesh
}
#[test]
fn missing_seams_are_hard_edges_the_uv_layout_does_not_split() {
let none = vec![Vec::<(u32, u32)>::new()];
assert_eq!(quad().missing_seam_edges(), none, "a smooth quad has no hard edge");
let shared_uv = quad().uvs[0];
let smooth_split = split_quad(shared_uv, Vec3::Z);
assert_eq!(smooth_split.missing_seam_edges(), none, "the same normal on both sides is a smooth edge");
let hard = split_quad(shared_uv, Vec3::new(1.0, 0.0, 1.0).normalize());
assert_eq!(hard.missing_seam_edges(), vec![vec![(0, 2), (2, 4)]], "a hard edge inside one UV island");
let seamed = split_quad(Vec2::new(0.9, 0.9), hard.normals[4]);
assert_eq!(seamed.missing_seam_edges(), none, "a hard edge on a UV seam is fine");
let mut short_normals = quad();
short_normals.normals.iter_mut().for_each(|normal| *normal *= 0.5);
assert_eq!(short_normals.missing_seam_edges(), none, "normals shorter than unit length point the same way");
assert_eq!(split_quad(shared_uv, Vec3::Z * 0.5).missing_seam_edges(), none, "normals of different lengths point the same way");
let mut no_normals = quad();
no_normals.normals.fill(Vec3::ZERO);
assert_eq!(no_normals.missing_seam_edges(), none, "two missing normals agree");
}
#[test]
fn missing_seams_join_positions_with_opposite_signed_zero() {
let mut mesh = split_quad(quad().uvs[0], Vec3::X);
mesh.positions[4] = Vec3::new(-0.0, 0.0, 0.0);
assert_eq!(mesh.positions[0], mesh.positions[4]);
assert_eq!(mesh.missing_seam_edges(), vec![vec![(0, 2), (2, 4)]]);
}
#[test]
fn missing_seams_between_materials_belong_to_both_materials() {
let mut mesh = split_quad(quad().uvs[0], Vec3::X);
mesh.materials.push("second".into());
mesh.submeshes[0].count = 3;
mesh.submeshes.push(SubMesh { name: "second".into(), material: 1, start: 3, count: 3 });
assert_eq!(mesh.uv_seam_edges(), vec![vec![(0, 2)], vec![(2, 4)]]);
mesh.uvs[4] = mesh.uvs[0];
assert_eq!(mesh.uv_seam_edges(), vec![Vec::<(u32, u32)>::new(); 2], "a material boundary alone is not a UV seam");
assert_eq!(mesh.missing_seam_edges(), vec![vec![(0, 2)], vec![(2, 4)]]);
mesh.normals[4] = mesh.normals[0];
assert_eq!(mesh.missing_seam_edges(), vec![Vec::<(u32, u32)>::new(); 2], "a material boundary alone is not a hard edge");
}
#[test]
+16
View File
@@ -353,6 +353,13 @@ impl Default for BrushSettings {
}
impl BrushSettings {
/// Radius of a 2D (UV) stamp as a fraction of the UV square, for pen `pressure` 0..1: twice the
/// brush size, shrunk towards `min_size` by a light pen. Painting and the 2D cursor share it.
pub fn uv_stamp_radius(&self, pressure: f32) -> f32 {
let p = pressure.clamp(0.0, 1.0);
self.size * 2.0 * if p < 1.0 { self.min_size + (1.0 - self.min_size) * p } else { 1.0 }
}
/// Clamp every value into its valid range (input from UI, CLI or agents)
pub fn sanitized(&self) -> BrushSettings {
let clamp_finite = |value: f32, min: f32, max: f32, default: f32| if value.is_finite() { value.clamp(min, max) } else { default };
@@ -392,6 +399,15 @@ impl BrushSettings {
#[cfg(test)]
mod tests {
#[test]
fn uv_stamp_radius_follows_pen_pressure() {
let b = BrushSettings { size: 0.1, min_size: 0.5, ..BrushSettings::default() };
assert!((b.uv_stamp_radius(1.0) - 0.2).abs() < 1e-6);
assert!((b.uv_stamp_radius(0.5) - 0.15).abs() < 1e-6);
assert!((b.uv_stamp_radius(0.0) - 0.1).abs() < 1e-6);
assert!((b.uv_stamp_radius(7.0) - 0.2).abs() < 1e-6, "pressure clamps to 1");
}
use super::*;
#[test]
+1
View File
@@ -19,6 +19,7 @@ bytemuck = { workspace = true }
half = { workspace = true }
log = { workspace = true }
rayon = { workspace = true }
serde = { workspace = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
pollster = "0.4"
+24
View File
@@ -0,0 +1,24 @@
//! One overlay of bake mode's 3D view: whether it shows, its color and its opacity
/// Whether a part of the bake setup shows in the 3D view, and in which color and opacity
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct BakeOverlay {
pub shown: bool,
/// Linear RGB
pub color: [f32; 3],
/// 0 transparent, 1 opaque; the matching error mixes into the high poly by it
pub opacity: f32,
}
impl BakeOverlay {
/// A hidden overlay in `color`, fully opaque
pub const fn hidden(color: [f32; 3]) -> Self {
Self { shown: false, color, opacity: 1.0 }
}
/// The overlay's shader globals: rgb its color, a its opacity (0 while hidden)
pub(crate) fn globals(&self) -> [f32; 4] {
let [red, green, blue] = self.color;
[red, green, blue, if self.shown { self.opacity.clamp(0.0, 1.0) } else { 0.0 }]
}
}
+53 -7
View File
@@ -1,12 +1,58 @@
//! What bake mode's 3D view draws while a bake is set up: the high poly, the cage, the UV seams and the low poly
//! What bake mode's 3D view draws while a bake is set up: the high poly, the cage, the missing seams and the low poly
/// Which parts of the bake setup the 3D view draws
use crate::bake_overlay::BakeOverlay;
use crate::neutral_material::NeutralMaterial;
/// Default overlay colors, linear RGB
const HIGH_POLY_COLOR: [f32; 3] = [0.0, 0.216, 0.521];
const MATCHING_ERROR_COLOR: [f32; 3] = [1.0, 0.0, 0.0];
const CAGE_SURFACE_COLOR: [f32; 3] = [0.604, 0.451, 0.216];
const CAGE_WIRE_COLOR: [f32; 3] = [0.604, 0.604, 0.604];
const MISSING_SEAM_COLOR: [f32; 3] = [0.451, 0.0, 0.451];
const LOW_POLY_COLOR: [f32; 3] = [0.216, 0.216, 0.216];
/// Which parts of the bake setup the 3D view draws, and how
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BakeVisualization {
pub high_poly: bool,
pub cage: bool,
pub uv_seams: bool,
pub low_poly: bool,
/// Draw the cage, seams and low poly of only this texture set
pub high_poly: BakeOverlay,
/// Tints the high poly where the bake's rays miss it
pub matching_error: BakeOverlay,
pub cage_surface: BakeOverlay,
pub cage_wire: BakeOverlay,
/// The low poly's hard edges that are not UV seams
pub missing_seams: BakeOverlay,
/// The low poly; its color is the neutral material's color
pub low_poly: BakeOverlay,
/// Shade the low poly with this plain material instead of its painted one
pub neutral: Option<NeutralMaterial>,
/// Draw the cage and high poly of only this texture set (the high poly's vertices of other sets come marked hidden)
pub only_set: Option<usize>,
}
impl Default for BakeVisualization {
/// Every part hidden, in its default color, the low poly with its painted material
fn default() -> Self {
Self {
high_poly: BakeOverlay::hidden(HIGH_POLY_COLOR),
matching_error: BakeOverlay::hidden(MATCHING_ERROR_COLOR),
cage_surface: BakeOverlay::hidden(CAGE_SURFACE_COLOR),
cage_wire: BakeOverlay::hidden(CAGE_WIRE_COLOR),
missing_seams: BakeOverlay::hidden(MISSING_SEAM_COLOR),
low_poly: BakeOverlay::hidden(LOW_POLY_COLOR),
neutral: None,
only_set: None,
}
}
}
impl BakeVisualization {
/// Whether any part of the cage shows
pub fn cage_shown(&self) -> bool {
self.cage_surface.shown || self.cage_wire.shown
}
/// The neutral material while the low poly shows with it
pub fn shown_neutral(&self) -> Option<NeutralMaterial> {
self.neutral.filter(|_| self.low_poly.shown)
}
}
+13
View File
@@ -0,0 +1,13 @@
//! A texture set's cage as the renderer takes it
use texturecraft_geom::glam::Vec3;
/// A cage for [`crate::Renderer::set_bake_cage`]: three corners per low-poly triangle, and lines as corner pairs
#[derive(Clone, Copy, Debug)]
pub struct CageGeometry<'a> {
pub corners: &'a [Vec3],
/// The low poly's polygon edges, for the wireframe
pub edges: &'a [[u32; 2]],
/// The low poly's hard edges without a UV seam
pub missing_seams: &'a [[u32; 2]],
}
+3
View File
@@ -6,4 +6,7 @@ pub(crate) struct CageGpu {
pub(crate) corner_count: u32,
pub(crate) edges: wgpu::Buffer,
pub(crate) edge_index_count: u32,
/// Corner pairs along the low poly's hard edges without a UV seam
pub(crate) missing_seams: wgpu::Buffer,
pub(crate) missing_seam_index_count: u32,
}
Loaded 100 of 178 files, more files were not shown because too many files have changed in this diff. Show more