commit ec859420d5b11e57e25c5c30eef2465d5fdb6cec Author: bflatastic Date: Thu Oct 15 01:33:13 2026 +0700 TextureCraft: clean-room 3D texture painting in pure Rust An open-source, clean-room implementation of a Substance 3D Painter-style texturing workflow, written in pure Rust with egui and wgpu. - Layer stack with 32 blend modes, masks, generators, content effects and anchor points - Painting, projection, smudge, clone, path, lasso fill, particle brush and quick mask - 43 procedural materials, 17 smart materials and 10 smart masks - Mesh map baking with high-poly, cage and match-by-name support - PBR viewport with HDR environments, TAA and a path-traced Rendering mode - glTF, FBX, COLLADA, OBJ, PLY and STL import; texture export presets; OBJ and GLB export - Dockable Painter-style UI, shortcut editor, web build, MCP server and CLI Co-Authored-By: Claude Opus 5.5 diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 00000000..5592118f --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[alias] +xtask = "run -p xtask --" diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..12adf75e --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +* text=auto eol=lf +*.png binary +*.ico binary +*.icns binary +*.ps1 text eol=crlf diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..9a9a3648 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,65 @@ +# Format, lint, test, dependency layering and the wasm32 check on Linux and Windows. +# No macOS here: the org's macOS runner slots are reserved for release.yml +# (craftrules release/cut-a-release.md, Gotchas). +name: CI + +on: + push: + branches: [main] + pull_request: + workflow_dispatch: + +permissions: + contents: read + +env: + CARGO_TERM_COLOR: always + CARGO_INCREMENTAL: 0 + RUST_BACKTRACE: 1 + +# A newer push to a PR branch cancels its older run. Runs on main are never cancelled, so every +# main state that gets a run is checked to the end. +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + check: + name: check (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Install Linux system dependencies (egui/winit/wgpu/rfd) + if: runner.os == 'Linux' + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends \ + libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev \ + libgl1-mesa-dev libgtk-3-dev + + - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable + with: + components: clippy, rustfmt + targets: wasm32-unknown-unknown + + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + + - name: Format + run: cargo fmt --all -- --check + + - name: Clippy + run: cargo clippy --workspace --all-targets --locked -- -D warnings + + - name: Test + run: cargo test --workspace --locked + + - name: Dependency layering + run: cargo xtask layers + + - name: wasm32 check + run: cargo xtask wasm diff --git a/.github/workflows/freebsd.yml b/.github/workflows/freebsd.yml new file mode 100644 index 00000000..0063fed4 --- /dev/null +++ b/.github/workflows/freebsd.yml @@ -0,0 +1,59 @@ +# Builds and tests the workspace on FreeBSD, inside a FreeBSD VM on a Linux runner +# (GitHub has no FreeBSD runners). It then runs packaging/freebsd/package.sh, the script the +# release job runs, and checks the tarball, so a broken FreeBSD package fails here before it can +# fail a release. Nothing is uploaded and no secrets are used. +name: FreeBSD + +# The VM build takes one to several hours, so it runs nightly on main rather than on every push. +# PRs that change the FreeBSD packaging itself still run it. +on: + schedule: + - cron: "17 2 * * *" + pull_request: + paths: + - .github/workflows/freebsd.yml + - packaging/freebsd/** + - packaging/env.sh + workflow_dispatch: + +concurrency: + group: freebsd-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + freebsd: + name: FreeBSD 14 (build + test + package) + runs-on: ubuntu-latest + timeout-minutes: 300 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Build, test and package in a FreeBSD VM + # vmactions/freebsd-vm v1.5.9, pinned by commit (it runs our build). + uses: vmactions/freebsd-vm@c46abacb49f09938ca4e1702d15d836285d694cc # v1.5.9 + with: + release: "14.3" + usesh: true + cpu: 4 + mem: 12288 + # Nothing to bring back; copying the target dir out of the VM fails on large builds. + copyback: false + prepare: | + pkg install -y bash git curl pkgconf cmake python3 libxkbcommon wayland libX11 libXcursor libXrandr libXi libxcb mesa-libs vulkan-loader gtk3 fontconfig freetype2 + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain stable + run: | + set -e + . "$HOME/.cargo/env" + # The checkout is synced in under another owner; let tests that call git read it. + git config --global --add safe.directory "*" + rustc --version + # One test thread: the VM's software Vulkan driver crashes when several tests set up a + # GPU device at once (PhotoCraft). GPU tests must skip themselves when there is no adapter. + CARGO_PROFILE_DEV_DEBUG=0 cargo test --workspace --locked -- --test-threads=1 + # The release package, as release.yml builds it. Free the debug build's disk first. + rm -rf target/debug + CARGO_BUILD_JOBS=4 bash packaging/freebsd/package.sh + tar -tzf dist/release/texturecraft-*-freebsd-x86_64.tar.gz \ + | grep -E '/bin/texturecraft$|/bin/texturecraft-cli$|/share/applications/ai\.storyteller\.texturecraft\.desktop$|/share/doc/texturecraft/LICENSE-MIT$' diff --git a/.github/workflows/packaging-lint.yml b/.github/workflows/packaging-lint.yml new file mode 100644 index 00000000..176a9538 --- /dev/null +++ b/.github/workflows/packaging-lint.yml @@ -0,0 +1,70 @@ +# Quick static checks for the release pipeline (seconds, no Rust build): workflow syntax, +# packaging shell/PowerShell scripts, and the Linux desktop/AppStream/Flatpak metadata. +name: Packaging lint + +on: + push: + branches: [main, release] + paths: [".github/workflows/**", "packaging/**", "assets/app-icon/**"] + pull_request: + paths: [".github/workflows/**", "packaging/**", "assets/app-icon/**"] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: packaging-lint-${{ github.ref }} + cancel-in-progress: true + +jobs: + lint: + runs-on: ubuntu-latest + env: + ACTIONLINT_VERSION: 1.7.12 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Install tools + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends shellcheck desktop-file-utils appstream libxml2-utils python3-yaml + curl -fsSL "https://github.com/rhysd/actionlint/releases/download/v${ACTIONLINT_VERSION}/actionlint_${ACTIONLINT_VERSION}_linux_amd64.tar.gz" \ + | tar -xz -C "$RUNNER_TEMP" actionlint + - name: actionlint + run: | + "$RUNNER_TEMP/actionlint" -color + - name: shellcheck + run: shellcheck packaging/*.sh packaging/*/*.sh packaging/linux/AppRun + - name: AppImage AppRun desktop integration + run: bash packaging/linux/apprun-test.sh + - name: PowerShell + shell: pwsh + run: | + foreach ($f in Get-ChildItem packaging/windows/*.ps1) { + $errors = $null + [System.Management.Automation.Language.Parser]::ParseFile($f.FullName, [ref]$null, [ref]$errors) | Out-Null + if ($errors) { $errors | ForEach-Object { Write-Output "::error file=$($f.Name)::$_" }; exit 1 } + Write-Output "ok $($f.Name)" + } + - name: XML, desktop entry, AppStream, Flatpak manifest + run: | + xmllint --noout packaging/windows/texturecraft.wxs packaging/macos/*.plist* packaging/linux/*.xml* + # Windows Installer needs Icon ids ending in .ico or .exe, or shortcuts show a blank icon (ICE50). + if grep -oE '"$RUNNER_TEMP/ai.storyteller.texturecraft.metainfo.xml" + appstreamcli validate --no-net --explain "$RUNNER_TEMP/ai.storyteller.texturecraft.metainfo.xml" + python3 - <<'EOF' + import yaml + src = yaml.safe_load(open("packaging/linux/flatpak/ai.storyteller.TextureCraft.yml")) + bundle = yaml.safe_load(open("packaging/linux/flatpak/ai.storyteller.TextureCraft.bundle.yml")) + for m in (src, bundle): + assert m["id"] == "ai.storyteller.texturecraft" and m["command"] == "texturecraft", m["id"] + for key in ("runtime", "runtime-version", "sdk", "finish-args"): + assert src[key] == bundle[key], f"Flatpak manifests disagree on {key}: {src[key]} vs {bundle[key]}" + print("Flatpak manifests ok") + EOF + - name: FreeBSD tarball layout (stub binaries) + run: bash packaging/freebsd/package.sh --dry-run diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..870f0d99 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,381 @@ +# Release pipeline: every push to `release` builds platform artifacts (signed where credentials +# are configured) and creates or updates the draft GitHub Release "TextureCraft v". +# See docs/releasing.md and craftrules release/playbook.md. +# +# The signing jobs (macOS, Windows) and the draft-release job run in the `release` environment +# (restricted to the `release` branch), which holds the signing secrets. Every secret is optional: +# missing ones produce unsigned artifacts and a warning, never a failed build. The jobs that sign +# nothing (version, Linux, Flatpak, FreeBSD, web) run without it, so a dispatch on any branch is a +# dry run of them; the release job needs the signed jobs, so a branch run never drafts a release. +name: Release + +on: + push: + branches: [release] + workflow_dispatch: + inputs: + version: + description: "Version override for a test run, e.g. 0.2.0-rc.1 (empty: Cargo.toml)" + type: string + default: "" + +permissions: + contents: read + +concurrency: + group: release-${{ github.ref }} + # Never cancel a run mid-notarization or mid-upload; a newer push waits its turn. + cancel-in-progress: false + +env: + CARGO_TERM_COLOR: always + CARGO_INCREMENTAL: 0 + RUST_BACKTRACE: 1 + TEXTURECRAFT_BUILD_SHA: ${{ github.sha }} + INPUT_VERSION: ${{ inputs.version }} + +jobs: + version: + name: version + runs-on: ubuntu-latest + # No `environment: release` here or on the other jobs that sign nothing: no release secrets. + outputs: + version: ${{ steps.v.outputs.version }} + date: ${{ steps.v.outputs.date }} + prerelease: ${{ steps.v.outputs.prerelease }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - id: v + name: Resolve version + run: | + . packaging/env.sh + v="${INPUT_VERSION#v}" + v="${v:-$VERSION}" + if ! printf '%s' "$v" | grep -Eq '^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$'; then + echo "::error::'$v' is not a version like 1.2.3 or 1.2.3-rc.1" + exit 1 + fi + case "$v" in *-*) pre=true ;; *) pre=false ;; esac + { + echo "version=$v" + echo "date=$(date -u +%Y-%m-%d)" + echo "prerelease=$pre" + } >>"$GITHUB_OUTPUT" + echo "### TextureCraft v$v (\`${GITHUB_SHA::9}\`)" >>"$GITHUB_STEP_SUMMARY" + + macos: + name: macOS universal + needs: version + runs-on: macos-15-xlarge # M2 Pro larger runner; use only in release.yml (actions_macos_xl budget caps spend) + environment: release + env: + TEXTURECRAFT_VERSION: ${{ needs.version.outputs.version }} + TEXTURECRAFT_BUILD_DATE: ${{ needs.version.outputs.date }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable + with: + targets: aarch64-apple-darwin, x86_64-apple-darwin + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + with: + key: release-macos-universal + - name: Apply version override + if: inputs.version != '' + run: cargo xtask version set "$TEXTURECRAFT_VERSION" + - name: Import signing certificate + env: + APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }} + APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }} + KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }} + run: bash packaging/macos/import-cert.sh + - name: Build, sign, notarize + env: + APPLE_ID: ${{ secrets.APPLE_ID }} + APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }} + APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }} + run: bash packaging/macos/package.sh --arch universal + # Unpacks the CLI zip and checks the binary users get: Developer ID, hardened runtime, + # timestamp, and Gatekeeper's notarization lookup; also the DMG's stapled ticket. Strict when + # the signing secrets are present, warnings otherwise. + - name: Verify signatures and notarization + env: + APPLE_ID: ${{ secrets.APPLE_ID }} + APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }} + APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }} + run: bash packaging/macos/verify.sh --arch universal + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: release-macos + path: dist/release/* + if-no-files-found: error + - name: Remove temporary keychain + if: always() + run: | + if [ -n "${MACOS_KEYCHAIN:-}" ]; then security delete-keychain "$MACOS_KEYCHAIN" || true; fi + + windows: + name: Windows ${{ matrix.arch }} + needs: version + runs-on: windows-latest + environment: release + strategy: + fail-fast: false + matrix: + include: + - { arch: x64, target: x86_64-pc-windows-msvc } + - { arch: x86, target: i686-pc-windows-msvc } + # Windows on ARM: cross-compiled on the x64 runner, so signing and WiX run as above. + # .github/workflows/windows-arm64.yml installs and runs it on ARM64 hardware. + - { arch: arm64, target: aarch64-pc-windows-msvc } + env: + TEXTURECRAFT_VERSION: ${{ needs.version.outputs.version }} + TEXTURECRAFT_BUILD_DATE: ${{ needs.version.outputs.date }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable + with: + targets: ${{ matrix.target }} + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + with: + key: release-windows-${{ matrix.arch }} + - name: Apply version override + if: inputs.version != '' + shell: bash + run: cargo xtask version set "$TEXTURECRAFT_VERSION" + - name: Install WiX + shell: pwsh + run: dotnet tool install --global wix --version 5.0.2 + - name: Build, sign, package + shell: pwsh + env: + WINDOWS_CERTIFICATE: ${{ secrets.WINDOWS_CERTIFICATE }} + WINDOWS_CERTIFICATE_PASSWORD: ${{ secrets.WINDOWS_CERTIFICATE_PASSWORD }} + AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }} + AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }} + AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }} + AZURE_SIGNING_ENDPOINT: ${{ secrets.AZURE_SIGNING_ENDPOINT }} + AZURE_SIGNING_ACCOUNT: ${{ secrets.AZURE_SIGNING_ACCOUNT }} + AZURE_CERT_PROFILE: ${{ secrets.AZURE_CERT_PROFILE }} + run: ./packaging/windows/package.ps1 -Arch ${{ matrix.arch }} + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: release-windows-${{ matrix.arch }} + path: dist/release/* + if-no-files-found: error + + linux: + name: Linux ${{ matrix.arch }} + needs: version + # The oldest GitHub-hosted image, so the binaries only need glibc >= 2.35 (Ubuntu 22.04+, + # Debian 12+, Fedora 36+, RHEL 10). See docs/releasing.md. + runs-on: ${{ matrix.runner }} + strategy: + fail-fast: false + matrix: + include: + - { arch: x86_64, runner: ubuntu-22.04, deb: amd64 } + - { arch: aarch64, runner: ubuntu-22.04-arm, deb: arm64 } + env: + TEXTURECRAFT_VERSION: ${{ needs.version.outputs.version }} + TEXTURECRAFT_BUILD_DATE: ${{ needs.version.outputs.date }} + NFPM_VERSION: 2.47.0 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Install build and packaging tools + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends \ + libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev libgtk-3-dev \ + desktop-file-utils appstream python3-yaml zsync + curl -fsSL -o "$RUNNER_TEMP/nfpm.deb" \ + "https://github.com/goreleaser/nfpm/releases/download/v${NFPM_VERSION}/nfpm_${NFPM_VERSION}_${{ matrix.deb }}.deb" + sudo dpkg -i "$RUNNER_TEMP/nfpm.deb" + - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + with: + key: release-linux-${{ matrix.arch }} + - name: Apply version override + if: inputs.version != '' + run: cargo xtask version set "$TEXTURECRAFT_VERSION" + - name: Build and package (AppImage, deb, rpm, tar.gz) + run: bash packaging/linux/package.sh + - name: Check the packages + run: | + dpkg-deb --info dist/release/*.deb + dpkg-deb --contents dist/release/*.deb | grep -E 'usr/bin/texturecraft$|metainfo|applications' + ldd target/release/texturecraft + python3 -c "import yaml; m = yaml.safe_load(open('packaging/linux/flatpak/ai.storyteller.TextureCraft.yml')); assert m['id'] == 'ai.storyteller.texturecraft'" + for appimage in dist/release/*.AppImage; do + chmod +x "$appimage" + APPIMAGE_EXTRACT_AND_RUN=1 "$appimage" --version + # Update information is embedded, and the .zsync for delta updates sits beside it. + "$appimage" --appimage-updateinformation | grep -F 'gh-releases-zsync|' + test -s "$appimage.zsync" + done + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: release-linux-${{ matrix.arch }} + path: dist/release/* + if-no-files-found: error + + flatpak: + name: Flatpak ${{ matrix.arch }} + needs: [version, linux] + # Repackages the Linux job's tarball (the same binaries as the AppImage/deb/rpm); no Rust + # build here. 24.04 for a newer flatpak-builder (1.4) than 22.04 ships. + runs-on: ${{ matrix.runner }} + strategy: + fail-fast: false + matrix: + include: + - { arch: x86_64, runner: ubuntu-24.04 } + - { arch: aarch64, runner: ubuntu-24.04-arm } + env: + TEXTURECRAFT_VERSION: ${{ needs.version.outputs.version }} + TEXTURECRAFT_BUILD_DATE: ${{ needs.version.outputs.date }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: release-linux-${{ matrix.arch }} + path: dist/linux + - name: Install flatpak-builder + run: | + sudo apt-get update + # librsvg2-common: the SVG pixbuf loader. flatpak-builder runs the host's + # `appstreamcli compose`, which renders the scalable icon and fails without it. + sudo apt-get install -y --no-install-recommends flatpak flatpak-builder librsvg2-common + # Ubuntu 24.04's AppArmor blocks the unprivileged user namespaces bubblewrap needs. + sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 + - name: Build the Flatpak bundle + run: bash packaging/linux/flatpak-bundle.sh "dist/linux/texturecraft-$TEXTURECRAFT_VERSION-linux-${{ matrix.arch }}.tar.gz" + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: release-flatpak-${{ matrix.arch }} + path: dist/release/*.flatpak + if-no-files-found: error + + freebsd: + name: FreeBSD x86_64 + needs: version + # GitHub has no FreeBSD runners: build in a FreeBSD 14.3 VM, the same image and packages as + # .github/workflows/freebsd.yml. + runs-on: ubuntu-latest + timeout-minutes: 180 + env: + TEXTURECRAFT_VERSION: ${{ needs.version.outputs.version }} + TEXTURECRAFT_BUILD_DATE: ${{ needs.version.outputs.date }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Build and package in a FreeBSD VM (tar.gz) + # vmactions/freebsd-vm v1.5.9, pinned by commit (it runs our build). + uses: vmactions/freebsd-vm@c46abacb49f09938ca4e1702d15d836285d694cc # v1.5.9 + with: + release: "14.3" + usesh: true + cpu: 4 + mem: 12288 + # Passed into the VM by name; never templated into the script below. + envs: "TEXTURECRAFT_VERSION TEXTURECRAFT_BUILD_DATE TEXTURECRAFT_BUILD_SHA INPUT_VERSION" + # The FreeBSD CI packages, plus bash for the packaging script. + prepare: | + pkg install -y bash git curl pkgconf cmake python3 libxkbcommon wayland libX11 libXcursor libXrandr libXi libxcb mesa-libs vulkan-loader gtk3 fontconfig freetype2 + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain stable + run: | + set -e + . "$HOME/.cargo/env" + git config --global --add safe.directory "$PWD" + # Build outside the checkout so only dist/ is copied back out of the VM (copying the + # target dir back fails on large builds). 4 jobs: more runs the VM out of memory. + export CARGO_TARGET_DIR=/var/tmp/texturecraft-target CARGO_BUILD_JOBS=4 + if [ -n "$INPUT_VERSION" ]; then cargo xtask version set "$TEXTURECRAFT_VERSION"; fi + bash packaging/freebsd/package.sh + tar -tzf dist/release/texturecraft-*-freebsd-x86_64.tar.gz \ + | grep -E '/bin/texturecraft$|/bin/texturecraft-cli$|/share/applications/ai\.storyteller\.texturecraft\.desktop$|/share/doc/texturecraft/LICENSE-MIT$' + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: release-freebsd-x86_64 + path: dist/release/* + if-no-files-found: error + + web: + name: Web (wasm) + needs: version + runs-on: ubuntu-latest + env: + TEXTURECRAFT_VERSION: ${{ needs.version.outputs.version }} + TEXTURECRAFT_BUILD_DATE: ${{ needs.version.outputs.date }} + TRUNK_VERSION: 0.21.14 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable + with: + targets: wasm32-unknown-unknown + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + with: + key: release-web + - name: Install trunk + run: | + curl -fsSL "https://github.com/trunk-rs/trunk/releases/download/v${TRUNK_VERSION}/trunk-x86_64-unknown-linux-gnu.tar.gz" \ + | tar -xz -C "$HOME/.cargo/bin" trunk + trunk --version + - name: Apply version override + if: inputs.version != '' + run: cargo xtask version set "$TEXTURECRAFT_VERSION" + - name: Build and zip + run: bash packaging/web/package.sh + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: release-web + path: dist/release/* + if-no-files-found: error + + release: + name: Draft GitHub Release + needs: [version, macos, windows, linux, flatpak, freebsd, web] + runs-on: ubuntu-latest + environment: release + permissions: + contents: write + env: + VERSION: ${{ needs.version.outputs.version }} + PRERELEASE: ${{ needs.version.outputs.prerelease }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_REPO: ${{ github.repository }} + steps: + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + pattern: release-* + path: artifacts + merge-multiple: true + - name: Checksums + working-directory: artifacts + run: | + sha256sum -- * | tee SHA256SUMS.txt + - name: Create or update the draft release + working-directory: artifacts + run: | + tag="v$VERSION" + title="TextureCraft v$VERSION" + pre=() + if [ "$PRERELEASE" = true ]; then pre=(--prerelease); fi + if draft="$(gh release view "$tag" --json isDraft --jq .isDraft 2>/dev/null)"; then + if [ "$draft" != "true" ]; then + echo "::error::Release $tag is already published. Bump the version (cargo xtask version set) before pushing to release." + exit 1 + fi + echo "Updating draft $tag: replacing its assets" + gh release view "$tag" --json assets --jq '.assets[].name' | while read -r asset; do + gh release delete-asset "$tag" "$asset" --yes + done + gh release edit "$tag" --draft --title "$title" --target "$GITHUB_SHA" --prerelease="$PRERELEASE" + gh release upload "$tag" --clobber -- * + else + echo "Creating draft $tag" + gh release create "$tag" --draft --title "$title" --target "$GITHUB_SHA" --generate-notes "${pre[@]}" -- * + fi + { + echo "Draft release **$title** at \`${GITHUB_SHA::9}\`:" + echo + sed 's/^/ /' SHA256SUMS.txt + } >>"$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/windows-arm64.yml b/.github/workflows/windows-arm64.yml new file mode 100644 index 00000000..d4476cd7 --- /dev/null +++ b/.github/workflows/windows-arm64.yml @@ -0,0 +1,91 @@ +# Windows on ARM: package the ARM64 release the way release.yml does (cross-compiled on an x64 +# runner, unsigned here), then install that MSI on a Windows 11 ARM64 runner, check the installed +# binaries are ARM64, run the CLI natively, and uninstall. (A native ARM64 `cargo test` job is left +# out for now: PdfCraft saw an intermittent access violation in egui UI tests there.) +name: Windows ARM64 + +# Nightly on main (dependency and runner-image drift shows up within a day), and on PRs that +# change the Windows packaging itself. +on: + pull_request: + paths: + - .github/workflows/windows-arm64.yml + - packaging/windows/** + schedule: + - cron: "41 4 * * *" + workflow_dispatch: + +concurrency: + group: windows-arm64-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +env: + CARGO_TERM_COLOR: always + CARGO_INCREMENTAL: 0 + +jobs: + package: + name: package (cross-compiled on x64) + runs-on: windows-latest + timeout-minutes: 90 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable + with: + targets: aarch64-pc-windows-msvc + - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + with: + key: windows-arm64-package + - name: Install WiX + shell: pwsh + run: dotnet tool install --global wix --version 5.0.2 + - name: Build and package (unsigned) + shell: pwsh + env: + DIST: ${{ github.workspace }}\dist\arm64 + run: ./packaging/windows/package.ps1 -Arch arm64 + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: texturecraft-windows-arm64 + path: dist/arm64/* + if-no-files-found: error + retention-days: 7 + + install: + name: install and run on ARM64 + needs: package + runs-on: windows-11-arm + timeout-minutes: 30 + steps: + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: texturecraft-windows-arm64 + path: dist + - name: Install the MSI, check it, run it, uninstall it + shell: pwsh + run: | + $ErrorActionPreference = 'Stop' + $msi = (Get-ChildItem dist/*.msi | Select-Object -First 1).FullName + $p = Start-Process msiexec.exe -ArgumentList '/i', "`"$msi`"", '/qn', '/l*v', 'install.log' -Wait -PassThru + if ($p.ExitCode -ne 0) { Get-Content install.log -Tail 60; throw "msiexec /i exited $($p.ExitCode)" } + $dir = Join-Path $env:ProgramFiles 'TextureCraft' + foreach ($exe in 'texturecraft.exe', 'texturecraft-cli.exe') { + $path = Join-Path $dir $exe + if (-not (Test-Path $path)) { throw "$exe not installed in $dir" } + $bytes = [System.IO.File]::ReadAllBytes($path) + $machine = [BitConverter]::ToUInt16($bytes, [BitConverter]::ToInt32($bytes, 0x3C) + 4) + if ($machine -ne 0xAA64) { throw "$exe is for machine 0x$('{0:X}' -f $machine), not ARM64" } + Write-Output "ok $path (ARM64)" + } + # The installed CLI runs natively on ARM64. + $cli = Join-Path $dir 'texturecraft-cli.exe' + $out = & $cli --version 2>&1 | Out-String + if ($LASTEXITCODE -ne 0) { Write-Output $out; throw "texturecraft-cli --version exited $LASTEXITCODE" } + Write-Output "ok texturecraft-cli --version: $($out.Trim())" + $p = Start-Process msiexec.exe -ArgumentList '/x', "`"$msi`"", '/qn' -Wait -PassThru + if ($p.ExitCode -ne 0) { throw "msiexec /x exited $($p.ExitCode)" } + if (Test-Path (Join-Path $dir 'texturecraft.exe')) { throw 'uninstall left texturecraft.exe behind' } + Write-Output 'ok uninstalled' diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..0996cb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +*~ +plan/ +/target +target/ +/dist +/corpus +/log +/secrets +/temp +/craft-fonts +*.tcraft.tmp diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 00000000..44c16bc4 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,166 @@ +# AGENTS.md: guide for AI agents and contributors + +TextureCraft is an open-source, native 3D texture painter: a clean-room implementation of the +workflow known from Adobe Substance 3D Painter, written in **Rust only** (no JavaScript or TypeScript). **No Tauri, Electron or webview +shells:** the desktop app is native egui/eframe on wgpu, and the web build is the same Rust compiled +to WebAssembly (trunk + wasm-bindgen). Never add Tauri (or any webview/JS UI framework) as a +dependency, build step or packaging target. The product name is always written **TextureCraft** +(`{Function}Craft` in PascalCase, like its siblings PhotoCraft, VectorCraft, FilmCraft, LightCraft, +PdfCraft, EffectCraft, DesignCraft) in user-facing text: UI, window titles, About, installers, +release names, docs prose. Machine names stay lowercase: crates (`texturecraft-*`), binaries, file +names, ids (`ai.storyteller.texturecraft`), the native project extension `.tcraft`. + +Standards and learnings shared across the crafting apps live in `../craftrules` (checked out next +to this repo, or `storytold/craftrules`). Read its `AGENTS.md` at the start of a session, follow its +standards, and contribute reusable learnings back there. Shared code goes only in craft-libs +(`standards/shared-libraries.md`); otherwise copy, don't depend. + +The goal is 1:1 parity with Substance 3D Painter's workflow (same panels, menus, shortcuts, layer +stack semantics and export conventions) with better performance, every feature drivable by agents, +and builds for Windows, macOS, Linux, FreeBSD and the web. Read this file first, then `docs/`. + +## 1. Orientation (5 minutes) + +| Read | Why | +|---|---| +| `docs/architecture.md` | Crate map, layers, the CPU texel engine + GPU viewport split, data flow | +| `docs/control-protocol.md` | The JSON control channel and MCP: how agents drive the running app | +| `ROADMAP.md` | Honest parity estimate, milestones, **current focus** | +| `docs/parity.md` | Generated list of Painter menu items: live or missing | +| `docs/releasing.md` | Versioning (`cargo xtask version`), the `release` branch, packaging | +| `ATTRIBUTION.md` | Every bundled asset with author, source, license | +| `crates//README.md` (where present) | Public API of that crate | + +## 2. Workspace map + +```text +crates/ + core texel geom L0 document model + blend math; sparse COW tiles, padding, noise; meshes, BVH, UV G-buffer, camera + compose bake L1 CPU layer-stack compositor (the oracle) + display conversion; mesh map bakers + paint formats L2 brush engine (strokes, stamps, visibility, commit); mesh import, texture export, .tcraft + engine render L3 Session + command registry (every action is a command); wgpu viewport renderer + automation L4 MCP server + headless JSON-lines RPC + bridge to the running app + ui-egui L5 egui shell (thin: every action goes through engine commands) +apps/ + texturecraft desktop app (eframe/wgpu), `--control ` loopback control server + texturecraft-cli headless CLI (info, run, commands, bake, export, render, mcp, serve) + texturecraft-web the same app in the browser (trunk + wasm-bindgen) +xtask/ cargo xtask layers | wasm | ci | version | ico | icns | parity +``` + +**Layering is enforced** by `cargo xtask layers`: a crate may depend only on lower layers. Nothing +below `ui-egui` may use egui, eframe, winit or rfd; only `render`, `ui-egui` and the apps may use +wgpu. A new crate must be registered in `xtask/src/layers.rs`. + +**The UI crate is swappable.** `texturecraft-ui-egui` holds no document logic: it calls engine +commands (`texturecraft_engine::execute`) and the session's stroke API. Another front end could +replace it without touching the engine. + +## 3. Golden rules + +### Assets, icons and images (absolutely essential) + +- **Never use any icon, image, logo, texture, material, brush, environment, font or other asset + from Adobe, Avid, Image-Line or Autodesk products**, and never copy files from any installed + commercial application (proprietary formats such as `.sbsar`, `.spexp`, `.sppr` or `.spsm`, HDR + environments, UI icons). Not as reference files in the repo either. +- Every image or icon must be **open source, public domain, Creative Commons (CC0/CC-BY), or + original** (made by a contributor who has the right to license it). Prefer generating content in + code (TextureCraft's environments, materials, alphas, meshes and thumbnails are all procedural). +- **Every bundled asset gets a row in [`ATTRIBUTION.md`](ATTRIBUTION.md)** (path, title, author, + source, license) **in the same change that adds it**, plus its license file next to it when the + license requires one. Assets compiled in through dependencies (icon fonts, default fonts) get a + row too. No row, no merge. +- Demo images and README screenshots show TextureCraft rendering original or public-domain content + only. Never personal photos. +- The ArtCraft logos in `docs/brand/` are not open source (`docs/brand/LICENSE-brand.txt`). +- Fonts live in [storytold/craft-fonts](https://github.com/storytold/craft-fonts); never commit font + files here. + +### Clean room + +TextureCraft is an independent, clean-room implementation of a professional texture-painting +workflow. Work from public documentation, general domain knowledge, public algorithms +(GGX/Smith/Schlick, split-sum IBL, MikkTSpace, jump-flood/BFS dilation, Möller–Trumbore, SAH BVH, +spherical harmonics, reoriented normal mapping, W3C blend modes) and public engine conventions +(glTF, Unreal, Unity packing). Never copy proprietary code, shaders, presets or file formats; +Painter file compatibility (`.spp`, `.spexp`, `.sbsar`) is a non-goal. Always describe TextureCraft +as a clean-room implementation, in code, docs and commits. + +### Never crash (outranks feature work) + +People trust TextureCraft with hours of painting. A malformed mesh or project, a bad command or MCP +param, an odd keystroke or a full disk must produce an error the user or agent can act on, never a +panic. The shared standard is `../craftrules/standards/never-crash.md`. + +- Non-test code never panics: no `unwrap()`, `expect()`, `panic!`, `unreachable!`, `todo!` or + `unimplemented!`. Every production crate root carries + `#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]`. +- No `unsafe` (`unsafe_code = "forbid"` for the workspace). +- Input-derived numbers are hostile: `get()` instead of `[i]`, checked/saturating arithmetic, caps + on allocations (mesh triangles, image sizes, zip entries), NaN guards. +- Every crash fix comes with a regression test. The engine's `panic_hunt` test runs every command + with hostile params; keep it green when adding commands. + +### Architecture rules + +1. **Everything is a command.** New user-visible behaviour = a command in the engine + (`crates/engine/src/*_cmds.rs`, registered in `commands.rs`) with id, label, menu path, + shortcut, params doc, `enabled` and `run`, plus tests. The menus, shortcuts, CLI, control channel + and MCP all dispatch commands by id. Menu items come from the catalog in + `crates/ui-egui/src/menus.rs`; an item goes live when its id is implemented. +2. **Programmatic calls never open dialogs.** A command run through the control channel or MCP runs + with defaults; only `ui.menu.invoke` behaves like a menu click. +3. **Inspect shows the agent's work.** `project.inspect` / `session.inspect` must reveal every + layer, mask, effect, fill source and painted tile count an agent changed. +4. **CPU oracle, GPU display.** Layers live as sparse copy-on-write 64×64 tiles; compositing, + painting, baking and export run on the CPU per dirty tile (rayon) and are fully testable; dirty + tiles are uploaded to wgpu textures for the viewport. Any GPU acceleration must match the CPU + compositor in parity tests. +5. **Undo is snapshots.** `Session::edit` clones the document (copy-on-write tiles make this cheap) + and restores it on error, so every command is atomic. +6. **Never break wasm.** L0–L5 must `cargo check --target wasm32-unknown-unknown` + (`cargo xtask wasm`). File-system code is `cfg(not(target_arch = "wasm32"))` with byte-based + alternatives. +7. **Performance is a feature.** Never scan a full texture set per frame; work on dirty tiles. + Benchmark heavy paths (bake, composite, export) in release and record numbers in the dev log. +8. **Verify UI changes visually.** Launch with `--control ` and call `ui.screenshot` (whole + window) or `viewport.screenshot`; look at the PNG. + +## 4. Picking work + +1. `ROADMAP.md` → **Current focus** and the parity table. +2. `docs/parity.md` → missing menu items, grouped by menu. +3. Run the app, take screenshots of TextureCraft and fix the biggest visible gaps in the workflow. +4. `log/devlog.md` (local) → the "Still open" bullets of recent entries. + +## 5. Before you finish a task + +```sh +cargo fmt --all +cargo test -p --release +cargo clippy --workspace --all-targets -- -D warnings +cargo xtask layers +cargo xtask wasm # if you touched L0–L5 +cargo test -p texturecraft-engine --test session panic_hunt # if you added/changed commands +``` + +Then append a terse entry to `log/devlog.md` (what landed, numbers, what's still open). Commit +after every arc of work that builds and passes. + +## 6. Parallel agents + +- Use your own target dir (`CARGO_TARGET_DIR=target/agent-`) and edit only the files you own. + Shared files (`engine/src/commands.rs`, `engine/src/lib.rs`, `ui-egui/src/menus.rs`, + `ui-egui/src/lib.rs`, the root `Cargo.toml`) get small, surgical edits; re-read before editing. +- Put new commands in a **new module** with its own `specs()` registered by one line. +- Write manifests atomically; one broken `Cargo.toml` breaks every agent's build. +- Delete your `target/agent-*` dir when done. + +## 7. Where things are tracked + +- `ROADMAP.md`: milestones, parity estimate and remaining effort. +- `docs/parity.md`: generated Painter menu coverage. +- `docs/releasing.md` and `../craftrules/release/playbook.md`: release builds and signing. +- `plan/` (local, gitignored): execution plan and notes. +- `log/` (local, gitignored): the dev log. diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md new file mode 100644 index 00000000..c5af3f0e --- /dev/null +++ b/ATTRIBUTION.md @@ -0,0 +1,35 @@ +# Attribution + +Every bundled non-code asset in this repository, with its author, source and license. The rule: +**each asset gets a row here in the same change that adds it.** TextureCraft's own code and +original assets are MIT OR Apache-2.0 (see [`LICENSE-MIT`](LICENSE-MIT), +[`LICENSE-APACHE`](LICENSE-APACHE) and [`NOTICE`](NOTICE)); third-party assets must be permissively +licensed and keep their license file next to them. + +| Path | Title | Author | Source | License | +|---|---|---|---|---| +| `assets/app-icon/icon.py` | App icon generator (octopus) | TextureCraft contributors | original | MIT OR Apache-2.0 | +| `assets/app-icon/icon.svg` | App icon, master vector (generated by `icon.py`) | TextureCraft contributors | original | MIT OR Apache-2.0 | +| `assets/app-icon/icon-1024.png` | App icon, 1024 px | TextureCraft contributors | original | MIT OR Apache-2.0 | +| `assets/app-icon/texturecraft.ico` | Windows app icon (16-256 px) | TextureCraft contributors | original | MIT OR Apache-2.0 | +| `assets/app-icon/texturecraft.icns` | macOS app icon (16-1024 px) | TextureCraft contributors | original | MIT OR Apache-2.0 | +| `assets/app-icon/hicolor/{16x16,24x24,32x32,48x48,64x64,128x128,256x256,512x512}/apps/ai.storyteller.texturecraft.png` | App icon, hicolor theme PNGs | TextureCraft contributors | original | MIT OR Apache-2.0 | +| `assets/app-icon/hicolor/scalable/apps/ai.storyteller.texturecraft.svg` | App icon, hicolor scalable (copy of `icon.svg`) | TextureCraft contributors | original | MIT OR Apache-2.0 | +| `docs/brand/artcraft-*.svg`, `docs/brand/artcraft-*.png` | ArtCraft logo and mark | ArtCraft Team | storytold/craftrules `assets/brand/` | ArtCraft brand terms, not open source ([`docs/brand/LICENSE-brand.txt`](docs/brand/LICENSE-brand.txt)) | + +## Third-party assets compiled into the binary + +These come from Rust crate dependencies (not files in this repository) and keep their licenses. + +| Asset | Used for | Author | Source | License | +|---|---|---|---|---| +| Phosphor Icons (regular), via the `egui-phosphor` crate | Every UI icon (tools, layers, panels) | Phosphor Icons (Helena Zhang, Tobias Fried) | https://phosphoricons.com, https://github.com/amPerl/egui-phosphor | MIT (icons); crate MIT OR Apache-2.0 | +| egui default fonts (Ubuntu-Light, Hack, Noto Emoji, emoji-icon-font), via `epaint_default_fonts` | UI text | Canonical / Source Foundry / Google / Jessie Jung | https://github.com/emilk/egui | Ubuntu Font Licence 1.0, MIT, OFL 1.1, MIT | + +## Procedural content (no asset files) + +Built-in environments (`crates/render/src/ibl.rs`), smart materials and procedurals +(`crates/engine/src/materials.rs`, `crates/compose/src/pattern.rs`), brush alphas +(`crates/paint/src/brush.rs`), meshes (`crates/geom/src/primitives.rs`) and shelf thumbnails +(`crates/ui-egui/src/thumbs.rs`) are generated in code by TextureCraft contributors (MIT OR +Apache-2.0). None of them is derived from another product's presets, shaders, materials or images. diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 00000000..3e690427 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,5 @@ +# TextureCraft: instructions for Claude + +Read [`AGENTS.md`](AGENTS.md) first: it is the guide for every agent working in this repository +(workspace map, golden rules including the asset and attribution rules, the definition of done). +Then read `../craftrules/AGENTS.md` for the standards shared by all Crafting Apps. diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 00000000..9b7de418 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,5080 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "accesskit" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3b7f7f85a7e5f68090000ed7622545829afd484d210358702ae4cb97dd0c320" +dependencies = [ + "enumn", + "serde", + "uuid", +] + +[[package]] +name = "accesskit_atspi_common" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e8c61bee90b42a772d39d06a740207dc71a4e780004ace1db8d99fb1baaa954" +dependencies = [ + "accesskit", + "accesskit_consumer 0.36.0", + "atspi-common", + "phf", + "serde", + "zvariant", +] + +[[package]] +name = "accesskit_consumer" +version = "0.35.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53cf47daed85312e763fbf85ceca136e0d7abc68e0a7e12abe11f48172bc3b10" +dependencies = [ + "accesskit", + "hashbrown 0.16.1", +] + +[[package]] +name = "accesskit_consumer" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25e0d7e25d06f4dc21d1774d67146e9e80d6789216cbd4d1e88185b0095dba60" +dependencies = [ + "accesskit", + "hashbrown 0.16.1", +] + +[[package]] +name = "accesskit_consumer" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d10a236f96f87d70732e44520046785431ef01d5bcd6b041317bfadd2f88245" +dependencies = [ + "accesskit", + "hashbrown 0.16.1", +] + +[[package]] +name = "accesskit_macos" +version = "0.26.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce02dc63b43f0c9296af9ac946312a2dc8814427d7a64d2d600971dac55b6076" +dependencies = [ + "accesskit", + "accesskit_consumer 0.38.0", + "hashbrown 0.16.1", + "objc2 0.5.3", + "objc2-app-kit 0.2.2", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "accesskit_unix" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b016ca8db0ea0ea2ceff29a9d6240391492d960716aa471967c00e8cc8cb197c" +dependencies = [ + "accesskit", + "accesskit_atspi_common", + "async-channel", + "async-executor", + "async-task", + "atspi", + "futures-lite", + "futures-util", + "serde", + "zbus", +] + +[[package]] +name = "accesskit_windows" +version = "0.32.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eff7009f1a532e917d66970a1e80c965140c6cfbbabbdde3d64e5431e6c78e21" +dependencies = [ + "accesskit", + "accesskit_consumer 0.35.0", + "hashbrown 0.16.1", + "static_assertions", + "windows", + "windows-core", +] + +[[package]] +name = "accesskit_winit" +version = "0.32.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fe9a94394896352cc4660ca2288bd4ef883d83238853c038b44070c8f134313" +dependencies = [ + "accesskit", + "accesskit_macos", + "accesskit_unix", + "accesskit_windows", + "raw-window-handle", + "winit", +] + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "ahash" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" +dependencies = [ + "cfg-if", + "getrandom 0.3.4", + "once_cell", + "serde", + "version_check", + "zerocopy", +] + +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + +[[package]] +name = "ambient-authority" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9d4ee0d472d1cd2e28c97dfa124b3d8d992e10eb0a035f33f5d12e3a177ba3b" + +[[package]] +name = "android-activity" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f2a1bb052857d5dd49572219344a7332b31b76405648eabac5bc68978251bcd" +dependencies = [ + "android-properties", + "bitflags 2.13.2", + "cc", + "jni", + "libc", + "log", + "ndk", + "ndk-context", + "ndk-sys", + "num_enum", + "thiserror 2.0.21", +] + +[[package]] +name = "android-properties" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc7eb209b1518d6bb87b283c20095f5228ecda460da70b44f0802523dea6da04" + +[[package]] +name = "android_system_properties" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc" +dependencies = [ + "libc", +] + +[[package]] +name = "arbitrary" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" +dependencies = [ + "derive_arbitrary", +] + +[[package]] +name = "arboard" +version = "3.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0348a1c054491f4bfe6ab86a7b6ab1e44e45d899005de92f58b3df180b36ddaf" +dependencies = [ + "clipboard-win", + "image", + "log", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-foundation 0.3.2", + "parking_lot", + "percent-encoding", + "windows-sys 0.60.2", + "x11rb", +] + +[[package]] +name = "arrayvec" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" + +[[package]] +name = "as-raw-xcb-connection" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b" + +[[package]] +name = "ash" +version = "0.38.0+1.3.281" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bb44936d800fea8f016d7f2311c6a4f97aebd5dc86f09906139ec848cf3a46f" +dependencies = [ + "libloading", +] + +[[package]] +name = "async-broadcast" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435a87a52755b8f27fcf321ac4f04b2802e337c8c4872923137471ec39c37532" +dependencies = [ + "event-listener", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-channel" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "924ed96dd52d1b75e9c1a3e6275715fd320f5f9439fb5a4a11fa51f4221158d2" +dependencies = [ + "concurrent-queue", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-executor" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c96bf972d85afc50bf5ab8fe2d54d1586b4e0b46c97c50a0c9e71e2f7bcd812a" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", + "futures-lite", + "pin-project-lite", + "slab", +] + +[[package]] +name = "async-io" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456b8a8feb6f42d237746d4b3e9a178494627745c3c56c6ea55d92ba50d026fc" +dependencies = [ + "autocfg", + "cfg-if", + "concurrent-queue", + "futures-io", + "futures-lite", + "parking", + "polling", + "rustix 1.1.5", + "slab", + "windows-sys 0.61.2", +] + +[[package]] +name = "async-lock" +version = "3.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290f7f2596bd5b78a9fec8088ccd89180d7f9f55b94b0576823bbbdc72ee8311" +dependencies = [ + "event-listener", + "event-listener-strategy", + "pin-project-lite", +] + +[[package]] +name = "async-process" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc50921ec0055cdd8a16de48773bfeec5c972598674347252c0399676be7da75" +dependencies = [ + "async-channel", + "async-io", + "async-lock", + "async-signal", + "async-task", + "blocking", + "cfg-if", + "event-listener", + "futures-lite", + "rustix 1.1.5", +] + +[[package]] +name = "async-recursion" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f8abc12baad266b1c8cec146854c195b5864b4221d4b2ca7296a7ae82d9e451" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "async-signal" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52b5aaafa020cf5053a01f2a60e8ff5dccf550f0f77ec54a4e47285ac2bab485" +dependencies = [ + "async-io", + "async-lock", + "atomic-waker", + "cfg-if", + "futures-core", + "futures-io", + "rustix 1.1.5", + "signal-hook-registry", + "slab", + "windows-sys 0.61.2", +] + +[[package]] +name = "async-task" +version = "4.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" + +[[package]] +name = "async-trait" +version = "0.1.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "atspi" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c77886257be21c9cd89a4ae7e64860c6f0eefca799bb79127913052bd0eefb3d" +dependencies = [ + "atspi-common", + "atspi-proxies", +] + +[[package]] +name = "atspi-common" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20c5617155740c98003016429ad13fe43ce7a77b007479350a9f8bf95a29f63d" +dependencies = [ + "enumflags2", + "serde", + "static_assertions", + "zbus", + "zbus-lockstep", + "zbus-lockstep-macros", + "zbus_names", + "zvariant", +] + +[[package]] +name = "atspi-proxies" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2230e48787ed3eb4088996eab66a32ca20c0b67bbd4fd6cdfe79f04f1f04c9fc" +dependencies = [ + "atspi-common", + "serde", + "zbus", +] + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bit-set" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2f926cc3060f09db9ebc5b52823d85268d24bb917e472c0c4bea35780a7d" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b71798fca2c1fe1086445a7258a4bc81e6e49dcd24c8d0dd9a1e57395b603f51" + +[[package]] +name = "bit_field" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e4b40c7323adcfc0a41c4b88143ed58346ff65a288fc144329c5c45e05d70c6" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" +dependencies = [ + "serde_core", +] + +[[package]] +name = "block2" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c132eebf10f5cad5289222520a4a058514204aed6d791f1cf4fe8088b82d15f" +dependencies = [ + "objc2 0.5.3", +] + +[[package]] +name = "block2" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5" +dependencies = [ + "objc2 0.6.5", +] + +[[package]] +name = "blocking" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a70e4329df6cb94385eed412ec92375c3cdd8a6e502493d1229b6414e4036dfa" +dependencies = [ + "async-channel", + "async-task", + "futures-io", + "futures-lite", + "piper", +] + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "bytemuck" +version = "1.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a1f896587b6f2c069c73d2f0913e2d590c3990285cd2f0b6aa02b786b4c679c" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "byteorder-lite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495" + +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + +[[package]] +name = "calloop" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b99da2f8558ca23c71f4fd15dc57c906239752dd27ff3c00a1d56b685b7cbfec" +dependencies = [ + "bitflags 2.13.2", + "log", + "polling", + "rustix 0.38.44", + "slab", + "thiserror 1.0.69", +] + +[[package]] +name = "calloop" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b32313e937c8721c9d17ee9bf60699a6402b4b7c2cb6ac1bee0ffcaa4b4e5e" +dependencies = [ + "bitflags 2.13.2", + "polling", + "rustix 1.1.5", + "tracing", +] + +[[package]] +name = "calloop-wayland-source" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95a66a987056935f7efce4ab5668920b5d0dac4a7c99991a67395f13702ddd20" +dependencies = [ + "calloop 0.13.0", + "rustix 0.38.44", + "wayland-backend", + "wayland-client", +] + +[[package]] +name = "calloop-wayland-source" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138efcf0940a02ebf0cc8d1eff41a1682a46b431630f4c52450d6265876021fa" +dependencies = [ + "calloop 0.14.5", + "rustix 1.1.5", + "wayland-backend", + "wayland-client", +] + +[[package]] +name = "cap-primitives" +version = "4.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b5f74729fd2f44701d1a8eb47e906cdb3ccd9ec0f02baad85a744b791940b18" +dependencies = [ + "ambient-authority", + "fs-set-times", + "io-extras", + "io-lifetimes 3.0.1", + "ipnet", + "maybe-owned", + "rustix 1.1.5", + "rustix-linux-procfs", + "windows-sys 0.61.2", + "winx", +] + +[[package]] +name = "cap-std" +version = "4.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1ec78e242cfa2cfe276807ac2ecc00315a6c97786977414bcd1c3963b6c91b8" +dependencies = [ + "cap-primitives", + "io-extras", + "io-lifetimes 3.0.1", + "rustix 1.1.5", +] + +[[package]] +name = "cc" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6651c9ed80effdc7db0ff72512157f901af5e3549e341e24b1dd4887d836d838" +dependencies = [ + "find-msvc-tools", + "jobserver", + "libc", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "cfg_aliases" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" + +[[package]] +name = "cgl" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ced0551234e87afee12411d535648dd89d2e7f34c78b753395567aff3d447ff" +dependencies = [ + "libc", +] + +[[package]] +name = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "iana-time-zone", + "num-traits", + "serde", + "windows-link", +] + +[[package]] +name = "clipboard-win" +version = "5.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bde03770d3df201d4fb868f2c9c59e66a3e4e2bd06692a0fe701e7103c7e84d4" +dependencies = [ + "error-code", +] + +[[package]] +name = "codespan-reporting" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af491d569909a7e4dee0ad7db7f5341fef5c614d5b8ec8cf765732aba3cff681" +dependencies = [ + "serde", + "termcolor", + "unicode-width", +] + +[[package]] +name = "color" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ec7c5eb7a16992b1904d76c517d170ab353b0e0b3d5a0c81a8a0cd1037893cf" +dependencies = [ + "bytemuck", +] + +[[package]] +name = "combine" +version = "4.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfc320937d09e6de266b31b9afb480f197d7a861be86be7cb2ea7e5d1bfffc5e" +dependencies = [ + "bytes", + "memchr", +] + +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "core-graphics" +version = "0.23.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "core-graphics-types", + "foreign-types", + "libc", +] + +[[package]] +name = "core-graphics-types" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01a7799fd6b852db0e61728dde9a204c423b44d689dbd432522543614b490e78" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" + +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + +[[package]] +name = "cursor-icon" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f27ae1dd37df86211c42e150270f82743308803d90a6f6e6651cd730d5e1732f" + +[[package]] +name = "darling" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed17f5901b6630b993ca003def43f2f8ef4014fc13b047b57aad617ff32bc2ec" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6837e2cf7485aaae18f86181d2f0e9a7ed297a025e220aeabf63fdebd3a2ddff" +dependencies = [ + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 3.0.9", +] + +[[package]] +name = "darling_macro" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ac7135c3ef02b2f7833bbeb1be5ba7f966dcde8a87c6b87f65a778d71a02785" +dependencies = [ + "darling_core", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "derive_arbitrary" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b034bd7d5f032402a2479444dcc6f74e36a03f31854d41680fb240ef682a1ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "dispatch" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd0c93bb4b0c6d9b77f4435b0ae98c24d17f1c45b2ff844c6151a07256ca923b" + +[[package]] +name = "dispatch2" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0e367e4e7da84520dedcac1901e4da967309406d1e51017ae1abfb97adbd38" +dependencies = [ + "bitflags 2.13.2", + "block2 0.6.2", + "libc", + "objc2 0.6.5", +] + +[[package]] +name = "displaydoc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "dlib" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab8ecd87370524b461f8557c119c405552c396ed91fc0a8eec68679eab26f94a" +dependencies = [ + "libloading", +] + +[[package]] +name = "document-features" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4b8a88685455ed29a21542a33abd9cb6510b6b129abadabdcef0f4c55bc8f61" +dependencies = [ + "litrs", +] + +[[package]] +name = "downcast-rs" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" + +[[package]] +name = "dpi" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8b14ccef22fc6f5a8f4d7d768562a182c04ce9a3b3157b91390b52ddfdf1a76" + +[[package]] +name = "duplicate" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e92f10a49176cbffacaedabfaa11d51db1ea0f80a83c26e1873b43cd1742c24" +dependencies = [ + "heck", + "proc-macro2", + "proc-macro2-diagnostics", +] + +[[package]] +name = "dyn-clone" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" + +[[package]] +name = "ecolor" +version = "0.36.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0656a7530b9e2841ee7a5f983965bdd9840c32f4012f759cfd44b912cced6b34" +dependencies = [ + "bytemuck", + "emath", + "serde", +] + +[[package]] +name = "eframe" +version = "0.36.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55ad41fac6e3149abb4866e93df37dbd33baa95db525c3368c944584e95a5dc2" +dependencies = [ + "ahash", + "bytemuck", + "document-features", + "egui", + "egui-wgpu", + "egui-winit", + "egui_glow", + "glutin", + "glutin-winit", + "image", + "js-sys", + "log", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-foundation 0.3.2", + "parking_lot", + "percent-encoding", + "pollster 1.0.1", + "profiling", + "raw-window-handle", + "ron", + "serde", + "static_assertions", + "wasm-bindgen", + "web-sys", + "web-time", + "wgpu", + "windows-sys 0.61.2", + "winit", +] + +[[package]] +name = "egui" +version = "0.36.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc938cc27cd911415e1e4d151bc56ff9df063d8db081374e87aeb805ea74b2ba" +dependencies = [ + "accesskit", + "ahash", + "bitflags 2.13.2", + "emath", + "epaint", + "itertools", + "log", + "nohash-hasher", + "profiling", + "ron", + "serde", + "smallvec", + "unicode-segmentation", + "web-sys", +] + +[[package]] +name = "egui-phosphor" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f4299551488dddaa8f757727e1eb4ffdaf277ffe6e30b413403468f5ecf4634" +dependencies = [ + "egui", +] + +[[package]] +name = "egui-wgpu" +version = "0.36.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cef1f6d1bba40b09a4d0d62cb822b81030922cfbf65c5373398c3a3caafc5bd" +dependencies = [ + "ahash", + "bytemuck", + "document-features", + "egui", + "epaint", + "log", + "profiling", + "thiserror 2.0.21", + "type-map", + "web-time", + "wgpu", + "winit", +] + +[[package]] +name = "egui-winit" +version = "0.36.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98466000559d66ba4db786a30448b01483946add4188e7f10f522a27b4fb89cc" +dependencies = [ + "accesskit_winit", + "arboard", + "bytemuck", + "egui", + "log", + "objc2 0.6.5", + "objc2-foundation 0.3.2", + "objc2-ui-kit 0.3.2", + "profiling", + "raw-window-handle", + "serde", + "smithay-clipboard", + "web-time", + "winit", +] + +[[package]] +name = "egui_dock" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d9f668eb59ff0ab130c8bce71a513135e1d3017fb3103fb8758e9eea99d759f" +dependencies = [ + "duplicate", + "egui", + "paste", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "egui_glow" +version = "0.36.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "606ed4167daf341e5753d703285c4c1d562324b2c07aa8f405cd7d066fe58f3b" +dependencies = [ + "bytemuck", + "egui", + "glow", + "log", + "profiling", + "winit", +] + +[[package]] +name = "either" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" + +[[package]] +name = "emath" +version = "0.36.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52c8d4141cf0f60fb2f7aefaf352d3ffa5171ef0ab09731c64391801e95e48f1" +dependencies = [ + "bytemuck", + "serde", +] + +[[package]] +name = "endi" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099" + +[[package]] +name = "enumflags2" +version = "0.7.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1027f7680c853e056ebcec683615fb6fbbc07dbaa13b4d5d9442b146ded4ecef" +dependencies = [ + "enumflags2_derive", + "serde", +] + +[[package]] +name = "enumflags2_derive" +version = "0.7.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67c78a4d8fdf9953a5c9d458f9efe940fd97a0cab0941c075a813ac594733827" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "enumn" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f9ed6b3789237c8a0c1c505af1c7eb2c560df6186f01b098c3a1064ea532f38" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "epaint" +version = "0.36.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0ec0308dc23130fc1a7d4623a8534c2ed069c6dc84e258ee4f3de99615aa1a3" +dependencies = [ + "ahash", + "bytemuck", + "ecolor", + "emath", + "epaint_default_fonts", + "font-types", + "harfrust", + "log", + "nohash-hasher", + "parking_lot", + "profiling", + "self_cell", + "serde", + "skrifa", + "smallvec", + "unicode-general-category", + "unicode-segmentation", + "vello_cpu", +] + +[[package]] +name = "epaint_default_fonts" +version = "0.36.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773fa9c96dd0dbef887e39d0ed6177f141cce4d68e6041df77570aa3702dfa13" + +[[package]] +name = "equivalent" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "error-code" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b5343afd4a8365a643ac588dab4cf234a190c7f6c88c9f6dd6ffe00837661b7" + +[[package]] +name = "euclid" +version = "0.22.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1a05365e3b1c6d1650318537c7460c6923f1abdd272ad6842baa2b509957a06" +dependencies = [ + "num-traits", +] + +[[package]] +name = "event-listener" +version = "5.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a23add41df1562121a9393cb065eab5146a1242410f23a644851e90cfd669d2" +dependencies = [ + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be9f3dfaaffdae2972880079a491a1a8bb7cbed0b8dd7a347f668b4150a3b93" +dependencies = [ + "event-listener", + "pin-project-lite", +] + +[[package]] +name = "exr" +version = "1.74.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711fe42c9964295e01ee3fba3f9fe0e1d24b98886950d68efe81b1c76e21adf3" +dependencies = [ + "bit_field", + "half", + "lebe", + "miniz_oxide 0.8.9", + "num-complex", + "pulp", + "smallvec", + "zune-inflate", +] + +[[package]] +name = "fastrand" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" + +[[package]] +name = "fax" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caf1079563223d5d59d83c85886a56e586cfd5c1a26292e971a0fa266531ac5a" + +[[package]] +name = "fdeflate" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c" +dependencies = [ + "simd-adler32", +] + +[[package]] +name = "fearless_simd" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97b65636e5b9ef369943878ac74335ba1c55c1cb6adbf1e2c293c624248d693" + +[[package]] +name = "find-msvc-tools" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484" + +[[package]] +name = "flate2" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb" +dependencies = [ + "crc32fast", + "miniz_oxide 0.9.1", + "zlib-rs", +] + +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + +[[package]] +name = "font-types" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c92d9081c7ab3bd83ef1eecdfad090f3d966e3ce74106779664114cfe1a6274" +dependencies = [ + "bytemuck", + "serde", +] + +[[package]] +name = "foreign-types" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" +dependencies = [ + "foreign-types-macros", + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-macros" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea5190182e6915eb873ddbc16e23b711b6eb1f9c00a0d0a3a91b5f6228475225" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "foreign-types-shared" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" + +[[package]] +name = "fs-set-times" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94e7099f6313ecacbe1256e8ff9d617b75d1bcb16a6fddef94866d225a01a14a" +dependencies = [ + "io-lifetimes 2.0.4", + "rustix 1.1.5", + "windows-sys 0.59.0", +] + +[[package]] +name = "futures" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a31d2a3fbaaeb2af2368bbdd904aa8e812d3c04a1ee10d3171f52d556e5d0a3" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-executor" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "031b47cf1a3c6cc8bc2fc76cd437f521619387907d469316e7c0bc278f1f5432" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53c0fa8157de1303bfffdaa1cc2a673bfffb60102f76b0ef4441659124373fed" + +[[package]] +name = "futures-lite" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f78e10609fe0e0b3f4157ffab1876319b5b0db102a2c60dc4626306dc46b44ad" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "parking", + "pin-project-lite", +] + +[[package]] +name = "futures-macro" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb9654ba8355388abeb8dcb4fc62f511300867002afc858860463bdd9fe0c44" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "futures-sink" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "gethostname" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bd49230192a3797a9a4d6abe9b3eed6f7fa4c8a8a4947977c6f80025f92cbd8" +dependencies = [ + "rustix 1.1.5", + "windows-link", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi 5.3.0", + "wasip2", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", +] + +[[package]] +name = "gl_generator" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a95dfc23a2b4a9a2f5ab41d194f8bfda3cabec42af4e39f08c339eb2a0c124d" +dependencies = [ + "khronos_api", + "log", + "xml-rs", +] + +[[package]] +name = "glam" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3abb554f8ee44336b72d522e0a7fe86a29e09f839a36022fa869a7dfe941a54b" + +[[package]] +name = "glam" +version = "0.30.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19fc433e8437a212d1b6f1e68c7824af3aed907da60afa994e7f542d18d12aa9" +dependencies = [ + "bytemuck", + "serde_core", +] + +[[package]] +name = "glifo" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed4a1bb24121291d27230c1b1b44e07d6a9b28cefdb32fe1581dfb84e14f940a" +dependencies = [ + "bytemuck", + "foldhash", + "hashbrown 0.17.1", + "log", + "peniko", + "skrifa", + "smallvec", + "vello_common", +] + +[[package]] +name = "glow" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29038e1c483364cc6bb3cf78feee1816002e127c331a1eec55a4d202b9e1adb5" +dependencies = [ + "js-sys", + "slotmap", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "gltf" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3ce1918195723ce6ac74e80542c5a96a40c2b26162c1957a5cd70799b8cacf7" +dependencies = [ + "base64 0.13.1", + "byteorder", + "gltf-json", + "image", + "lazy_static", + "serde_json", + "urlencoding", +] + +[[package]] +name = "gltf-derive" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14070e711538afba5d6c807edb74bcb84e5dbb9211a3bf5dea0dfab5b24f4c51" +dependencies = [ + "inflections", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "gltf-json" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6176f9d60a7eab0a877e8e96548605dedbde9190a7ae1e80bbcc1c9af03ab14" +dependencies = [ + "gltf-derive", + "serde", + "serde_derive", + "serde_json", +] + +[[package]] +name = "glutin" +version = "0.32.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12124de845cacfebedff80e877bb37b5b75c34c5a4c89e47e1cdd67fb6041325" +dependencies = [ + "bitflags 2.13.2", + "cfg_aliases", + "cgl", + "dispatch2", + "glutin_egl_sys", + "glutin_glx_sys", + "glutin_wgl_sys", + "libloading", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-core-foundation", + "objc2-foundation 0.3.2", + "once_cell", + "raw-window-handle", + "wayland-sys", + "windows-sys 0.52.0", + "x11-dl", +] + +[[package]] +name = "glutin-winit" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85edca7075f8fc728f28cb8fbb111a96c3b89e930574369e3e9c27eb75d3788f" +dependencies = [ + "cfg_aliases", + "glutin", + "raw-window-handle", + "winit", +] + +[[package]] +name = "glutin_egl_sys" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c4680ba6195f424febdc3ba46e7a42a0e58743f2edb115297b86d7f8ecc02d2" +dependencies = [ + "gl_generator", + "windows-sys 0.52.0", +] + +[[package]] +name = "glutin_glx_sys" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7bb2938045a88b612499fbcba375a77198e01306f52272e692f8c1f3751185" +dependencies = [ + "gl_generator", + "x11-dl", +] + +[[package]] +name = "glutin_wgl_sys" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c4ee00b289aba7a9e5306d57c2d05499b2e5dc427f84ac708bd2c090212cf3e" +dependencies = [ + "gl_generator", +] + +[[package]] +name = "gpu-allocator" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51255ea7cfaadb6c5f1528d43e92a82acb2b96c43365989a28b2d44ee38f8795" +dependencies = [ + "ash", + "hashbrown 0.16.1", + "log", + "presser", + "thiserror 2.0.21", + "windows", +] + +[[package]] +name = "guillotiere" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b17e70c989c36bad147b27a58d148c0741c51448aa5653436547323e524d0ab" +dependencies = [ + "euclid", +] + +[[package]] +name = "half" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" +dependencies = [ + "cfg-if", + "crunchy", + "num-traits", + "zerocopy", +] + +[[package]] +name = "harfrust" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03d949a14aa089bbb282f7dd76a498a7f684428e4257202efc119ec010376f9" +dependencies = [ + "bitflags 2.13.2", + "bytemuck", + "read-fonts", + "smallvec", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +dependencies = [ + "foldhash", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "image" +version = "0.25.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85ab80394333c02fe689eaf900ab500fbd0c2213da414687ebf995a65d5a6104" +dependencies = [ + "bytemuck", + "byteorder-lite", + "exr", + "image-webp", + "moxcms", + "num-traits", + "png", + "tiff", + "zune-core", + "zune-jpeg", +] + +[[package]] +name = "image-webp" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525e9ff3e1a4be2fbea1fdf0e98686a6d98b4d8f937e1bf7402245af1909e8c3" +dependencies = [ + "byteorder-lite", + "quick-error", +] + +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", + "serde", + "serde_core", +] + +[[package]] +name = "inflections" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a257582fdcde896fd96463bf2d40eefea0580021c0712a0e2b028b60b47a837a" + +[[package]] +name = "io-extras" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20fd6de4ccfcc187e38bc21cfa543cb5a302cb86a8b114eb7f0bf0dc9f8ac00f" +dependencies = [ + "io-lifetimes 3.0.1", + "windows-sys 0.60.2", +] + +[[package]] +name = "io-lifetimes" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06432fb54d3be7964ecd3649233cddf80db2832f47fec34c01f65b3d9d774983" + +[[package]] +name = "io-lifetimes" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0fb0570afe1fed943c5c3d4102d5358592d8625fda6a0007fdbe65a92fba96" + +[[package]] +name = "ipnet" +version = "2.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" + +[[package]] +name = "itertools" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b4baf93f58d4425749ca49a51c50ebab072c5df6994d08fed93541c331481dc" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "jni" +version = "0.22.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5efd9a482cf3a427f00d6b35f14332adc7902ce91efb778580e180ff90fa3498" +dependencies = [ + "cfg-if", + "combine", + "jni-macros", + "jni-sys 0.4.1", + "log", + "simd_cesu8", + "thiserror 2.0.21", + "walkdir", + "windows-link", +] + +[[package]] +name = "jni-macros" +version = "0.22.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a00109accc170f0bdb141fed3e393c565b6f5e072365c3bd58f5b062591560a3" +dependencies = [ + "proc-macro2", + "quote", + "rustc_version", + "simd_cesu8", + "syn 2.0.119", +] + +[[package]] +name = "jni-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41a652e1f9b6e0275df1f15b32661cf0d4b78d4d87ddec5e0c3c20f097433258" +dependencies = [ + "jni-sys 0.4.1", +] + +[[package]] +name = "jni-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6377a88cb3910bee9b0fa88d4f42e1d2da8e79915598f65fb0c7ee14c878af2" +dependencies = [ + "jni-sys-macros", +] + +[[package]] +name = "jni-sys-macros" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38c0b942f458fe50cdac086d2f946512305e5631e720728f2a61aabcd47a6264" +dependencies = [ + "quote", + "syn 2.0.119", +] + +[[package]] +name = "jobserver" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3" +dependencies = [ + "getrandom 0.4.3", + "libc", +] + +[[package]] +name = "js-sys" +version = "0.3.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7883d941dae510fb2d978fc3fe018c71c9e2892fd38854de3e8b92c2e5ad9cc5" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "khronos-egl" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6aae1df220ece3c0ada96b8153459b67eebe9ae9212258bb0134ae60416fdf76" +dependencies = [ + "libc", + "libloading", + "pkg-config", +] + +[[package]] +name = "khronos_api" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" + +[[package]] +name = "kurbo" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b60dfc32f652b926df6192e55525b16d186c69d47876c3ead4da5cc9f8450e2" +dependencies = [ + "arrayvec", + "euclid", + "polycool", + "smallvec", +] + +[[package]] +name = "lazy_static" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" + +[[package]] +name = "lebe" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8" + +[[package]] +name = "libc" +version = "0.2.190" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78" + +[[package]] +name = "libloading" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" +dependencies = [ + "cfg-if", + "windows-link", +] + +[[package]] +name = "libm" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" + +[[package]] +name = "libredox" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ff90caf6077a803a240f62fdbe88645a890bbca49ef8174c3cb0404362171d" +dependencies = [ + "bitflags 2.13.2", + "libc", + "plain", + "redox_syscall 0.9.4", +] + +[[package]] +name = "linebender_resource_handle" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4a5ff6bcca6c4867b1c4fd4ef63e4db7436ef363e0ad7531d1558856bae64f4" + +[[package]] +name = "linux-raw-sys" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "litrs" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4744e383959f0db86ede514b809b1c53251889093803c05267acc7d4e7030d70" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" + +[[package]] +name = "maybe-owned" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4facc753ae494aeb6e3c22f839b158aebd4f9270f55cd3c79906c45476c47ab4" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "memmap2" +version = "0.9.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1219ed1b7f229ee7104d281dd01d6802fe28bb6e95d292942c4daacdeb798c0" +dependencies = [ + "libc", +] + +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + +[[package]] +name = "mikktspace" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d0b56b403871a8f992ca626d52cc0a690d4841baea8955dc4af6304ac62f8b0" +dependencies = [ + "glam 0.15.2", +] + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "miniz_oxide" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "mio" +version = "1.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "moxcms" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb85c154ba489f01b25c0d36ae69a87e4a1c73a72631fc6c0eb6dde34a73e44b" +dependencies = [ + "num-traits", + "pxfm", +] + +[[package]] +name = "naga" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a616d2fb8c89516ac2723a581f69d6c18576046bed761bd6b305e5618e6ae130" +dependencies = [ + "arrayvec", + "bit-set", + "bitflags 2.13.2", + "cfg-if", + "cfg_aliases", + "codespan-reporting", + "half", + "hashbrown 0.17.1", + "indexmap", + "libm", + "log", + "naga-types", + "num-traits", + "once_cell", + "rustc-hash 1.1.0", + "spirv", + "thiserror 2.0.21", + "unicode-ident", +] + +[[package]] +name = "naga-types" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "590afbf58a6f4f62873cd5cff4468061844bafa1cdf399cc954537c22d768d49" +dependencies = [ + "hashbrown 0.17.1", + "indexmap", + "rustc-hash 1.1.0", + "thiserror 2.0.21", +] + +[[package]] +name = "ndk" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3f42e7bbe13d351b6bead8286a43aac9534b82bd3cc43e47037f012ebfd62d4" +dependencies = [ + "bitflags 2.13.2", + "jni-sys 0.3.1", + "log", + "ndk-sys", + "num_enum", + "raw-window-handle", + "thiserror 1.0.69", +] + +[[package]] +name = "ndk-context" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27b02d87554356db9e9a873add8782d4ea6e3e58ea071a9adb9a2e8ddb884a8b" + +[[package]] +name = "ndk-sys" +version = "0.6.0+11769913" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee6cda3051665f1fb8d9e08fc35c96d5a244fb1be711a03b71118828afc9a873" +dependencies = [ + "jni-sys 0.3.1", +] + +[[package]] +name = "nohash-hasher" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "bytemuck", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", + "libm", +] + +[[package]] +name = "num_enum" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d0bca838442ec211fa11de3a8b0e0e8f3a4522575b5c4c06ed722e005036f26" +dependencies = [ + "num_enum_derive", + "rustversion", +] + +[[package]] +name = "num_enum_derive" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "680998035259dcfcafe653688bf2aa6d3e2dc05e98be6ab46afb089dc84f1df8" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "objc-sys" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb91bdd390c7ce1a8607f35f3ca7151b65afc0ff5ff3b34fa350f7d7c7e4310" + +[[package]] +name = "objc2" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19d5490aaf8f1d7cf7688dfa9b0ce07900e168852c45cd2c03f534dfd27cfd0b" +dependencies = [ + "objc-sys", + "objc2-encode", +] + +[[package]] +name = "objc2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08849bbd4767dfae9457696856ae1c84fe4e0281bbe4a7abff2d0e06fb7981f8" +dependencies = [ + "objc2-encode", +] + +[[package]] +name = "objc2-app-kit" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4e89ad9e3d7d297152b17d39ed92cd50ca8063a89a9fa569046d41568891eff" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "libc", + "objc2 0.5.3", + "objc2-core-data", + "objc2-core-image", + "objc2-foundation 0.2.2", + "objc2-quartz-core 0.2.2", +] + +[[package]] +name = "objc2-app-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d49e936b501e5c5bf01fda3a9452ff86dc3ea98ad5f283e1455153142d97518c" +dependencies = [ + "bitflags 2.13.2", + "block2 0.6.2", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-cloud-kit" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74dd3b56391c7a0596a295029734d3c1c5e7e510a4cb30245f8221ccea96b009" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "objc2 0.5.3", + "objc2-core-location", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "objc2-contacts" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5ff520e9c33812fd374d8deecef01d4a840e7b41862d849513de77e44aa4889" +dependencies = [ + "block2 0.5.1", + "objc2 0.5.3", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "objc2-core-data" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "617fbf49e071c178c0b24c080767db52958f716d9eabdf0890523aeae54773ef" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "objc2 0.5.3", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "objc2-core-foundation" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536" +dependencies = [ + "bitflags 2.13.2", + "dispatch2", + "objc2 0.6.5", +] + +[[package]] +name = "objc2-core-graphics" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e022c9d066895efa1345f8e33e584b9f958da2fd4cd116792e15e07e4720a807" +dependencies = [ + "bitflags 2.13.2", + "dispatch2", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-io-surface", +] + +[[package]] +name = "objc2-core-image" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55260963a527c99f1819c4f8e3b47fe04f9650694ef348ffd2227e8196d34c80" +dependencies = [ + "block2 0.5.1", + "objc2 0.5.3", + "objc2-foundation 0.2.2", + "objc2-metal 0.2.2", +] + +[[package]] +name = "objc2-core-location" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "000cfee34e683244f284252ee206a27953279d370e309649dc3ee317b37e5781" +dependencies = [ + "block2 0.5.1", + "objc2 0.5.3", + "objc2-contacts", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "objc2-encode" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33" + +[[package]] +name = "objc2-foundation" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee638a5da3799329310ad4cfa62fbf045d5f56e3ef5ba4149e7452dcf89d5a8" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "dispatch", + "libc", + "objc2 0.5.3", +] + +[[package]] +name = "objc2-foundation" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3e0adef53c21f888deb4fa59fc59f7eb17404926ee8a6f59f5df0fd7f9f3272" +dependencies = [ + "bitflags 2.13.2", + "block2 0.6.2", + "objc2 0.6.5", + "objc2-core-foundation", +] + +[[package]] +name = "objc2-io-surface" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180788110936d59bab6bd83b6060ffdfffb3b922ba1396b312ae795e1de9d81d" +dependencies = [ + "bitflags 2.13.2", + "objc2 0.6.5", + "objc2-core-foundation", +] + +[[package]] +name = "objc2-link-presentation" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1a1ae721c5e35be65f01a03b6d2ac13a54cb4fa70d8a5da293d7b0020261398" +dependencies = [ + "block2 0.5.1", + "objc2 0.5.3", + "objc2-app-kit 0.2.2", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "objc2-metal" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0cba1276f6023976a406a14ffa85e1fdd19df6b0f737b063b95f6c8c7aadd6" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "objc2 0.5.3", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "objc2-metal" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0125f776a10d00af4152d74616409f0d4a2053a6f57fa5b7d6aa2854ac04794" +dependencies = [ + "bitflags 2.13.2", + "block2 0.6.2", + "objc2 0.6.5", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-quartz-core" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e42bee7bff906b14b167da2bac5efe6b6a07e6f7c0a21a7308d40c960242dc7a" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "objc2 0.5.3", + "objc2-foundation 0.2.2", + "objc2-metal 0.2.2", +] + +[[package]] +name = "objc2-quartz-core" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96c1358452b371bf9f104e21ec536d37a650eb10f7ee379fff67d2e08d537f1f" +dependencies = [ + "bitflags 2.13.2", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-foundation 0.3.2", + "objc2-metal 0.3.2", +] + +[[package]] +name = "objc2-symbols" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a684efe3dec1b305badae1a28f6555f6ddd3bb2c2267896782858d5a78404dc" +dependencies = [ + "objc2 0.5.3", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "objc2-ui-kit" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8bb46798b20cd6b91cbd113524c490f1686f4c4e8f49502431415f3512e2b6f" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "objc2 0.5.3", + "objc2-cloud-kit", + "objc2-core-data", + "objc2-core-image", + "objc2-core-location", + "objc2-foundation 0.2.2", + "objc2-link-presentation", + "objc2-quartz-core 0.2.2", + "objc2-symbols", + "objc2-uniform-type-identifiers", + "objc2-user-notifications", +] + +[[package]] +name = "objc2-ui-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d87d638e33c06f577498cbcc50491496a3ed4246998a7fbba7ccb98b1e7eab22" +dependencies = [ + "bitflags 2.13.2", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-uniform-type-identifiers" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44fa5f9748dbfe1ca6c0b79ad20725a11eca7c2218bceb4b005cb1be26273bfe" +dependencies = [ + "block2 0.5.1", + "objc2 0.5.3", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "objc2-user-notifications" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76cfcbf642358e8689af64cee815d139339f3ed8ad05103ed5eaf73db8d84cb3" +dependencies = [ + "bitflags 2.13.2", + "block2 0.5.1", + "objc2 0.5.3", + "objc2-core-location", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "orbclient" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5df339f526ea9a60e371768d50efc2f2508c7203290731565d1f7a6f71d21747" +dependencies = [ + "libc", + "libredox", +] + +[[package]] +name = "ordered-float" +version = "5.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c7c9e0d9b23589f26070720bac724174bfec1083e82f7854cdd0267518343c0" +dependencies = [ + "num-traits", +] + +[[package]] +name = "ordered-stream" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aa2b01e1d916879f73a53d01d1d6cee68adbb31d6d9177a8cfce093cced1d50" +dependencies = [ + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall 0.5.18", + "smallvec", + "windows-link", +] + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pastey" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4" + +[[package]] +name = "peniko" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "839c8299360d2e998bdb106dc0a6cd71dcc5f4df51df1b620361bf50e283cca6" +dependencies = [ + "bytemuck", + "color", + "kurbo", + "linebender_resource_handle", + "smallvec", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "phf" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf" +dependencies = [ + "phf_macros", + "phf_shared", + "serde", +] + +[[package]] +name = "phf_generator" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "135ace3a761e564ec88c03a77317a7c6b80bb7f7135ef2544dbe054243b89737" +dependencies = [ + "fastrand", + "phf_shared", +] + +[[package]] +name = "phf_macros" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812f032b54b1e759ccd5f8b6677695d5268c588701effba24601f6932f8269ef" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "phf_shared" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266" +dependencies = [ + "siphasher", +] + +[[package]] +name = "pin-project" +version = "1.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2466b2336ed02bcdca6b294417127b90ec92038d1d5c4fbeac971a922e0e0924" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c96395f0a926bc13b1c17622aaddda1ecb55d49c8f1bf9777e4d877800a43f8b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "piper" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c835479a4443ded371d6c535cbfd8d31ad92c5d23ae9770a61bc155e4992a3c1" +dependencies = [ + "atomic-waker", + "fastrand", + "futures-io", +] + +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "plain" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" + +[[package]] +name = "png" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60769b8b31b2a9f263dae2776c37b1b28ae246943cf719eb6946a1db05128a61" +dependencies = [ + "bitflags 2.13.2", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide 0.8.9", +] + +[[package]] +name = "polling" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d0e4f59085d47d8241c88ead0f274e8a0cb551f3625263c05eb8dd897c34218" +dependencies = [ + "cfg-if", + "concurrent-queue", + "hermit-abi", + "pin-project-lite", + "rustix 1.1.5", + "windows-sys 0.61.2", +] + +[[package]] +name = "pollster" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f3a9f18d041e6d0e102a0a46750538147e5e8992d3b4873aaafee2520b00ce3" + +[[package]] +name = "pollster" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc6355899e1c9462875b6757c79f3caa011a1fdae12bbb1a2e72dd1f234f8336" + +[[package]] +name = "polycool" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50596ddc09eb5ad5f75cacd40209568e66df71baf86e1499a0e99c4cff12a5a6" +dependencies = [ + "arrayvec", +] + +[[package]] +name = "portable-atomic" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85" + +[[package]] +name = "portable-atomic-util" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10ab3eb7f3becc3a1cbc4f2c6f20267996cfc1a6467a873763411b136a122715" +dependencies = [ + "portable-atomic", +] + +[[package]] +name = "presser" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8cf8e6a8aa66ce33f63993ffc4ea4271eb5b0530a9002db8455ea6050c77bfa" + +[[package]] +name = "proc-macro-crate" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e67ba7e9b2b56446f1d419b1d807906278ffa1a658a8a5d8a39dcb1f5a78614f" +dependencies = [ + "toml_edit", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "proc-macro2-diagnostics" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "version_check", +] + +[[package]] +name = "profiling" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5" + +[[package]] +name = "pulp" +version = "0.22.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "046aa45b989642ec2e4717c8e72d677b13edd831a4d3b6cf37d9a3e54912496a" +dependencies = [ + "bytemuck", + "cfg-if", + "libm", + "num-complex", + "paste", + "pulp-wasm-simd-flag", + "raw-cpuid", + "reborrow", + "version_check", +] + +[[package]] +name = "pulp-wasm-simd-flag" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d8f70e07b9c3962945a74e59ca1c511bba65b6419468acc217c457d93f3c740" + +[[package]] +name = "pxfm" +version = "0.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea" + +[[package]] +name = "quick-error" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" + +[[package]] +name = "quick-xml" +version = "0.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e660451e55124f798a69a5af3f49ccfbefbd41910eefd25caf2393e1f3473ec1" +dependencies = [ + "memchr", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "range-alloc" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca45419789ae5a7899559e9512e58ca889e41f04f1f2445e9f4b290ceccd1d08" + +[[package]] +name = "raw-cpuid" +version = "11.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "498cd0dc59d73224351ee52a95fee0f1a617a2eae0e7d9d720cc622c73a54186" +dependencies = [ + "bitflags 2.13.2", +] + +[[package]] +name = "raw-window-handle" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539" + +[[package]] +name = "raw-window-metal" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40d213455a5f1dc59214213c7330e074ddf8114c9a42411eb890c767357ce135" +dependencies = [ + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-foundation 0.3.2", + "objc2-quartz-core 0.3.2", +] + +[[package]] +name = "rayon" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "read-fonts" +version = "0.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "046a7d674daf459825b32f5062056d6882db0d2f5a479fbd76ccfc870ac18709" +dependencies = [ + "bytemuck", + "font-types", + "once_cell", +] + +[[package]] +name = "reborrow" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03251193000f4bd3b042892be858ee50e8b3719f2b08e5833ac4353724632430" + +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags 1.3.2", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags 2.13.2", +] + +[[package]] +name = "redox_syscall" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "737970939a87c6fa31e7acad13307bccbb017a073b695b6089a2c484f929e20e" +dependencies = [ + "bitflags 2.13.2", +] + +[[package]] +name = "ref-cast" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e440fb4e4b4147295338efb76001ab9e4efc0e5839df2c47fc5ac2381d365c3" +dependencies = [ + "ref-cast-impl", +] + +[[package]] +name = "ref-cast-impl" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ecd8964f8453721699a1ed72037b0db49ce2f5a5138486ee89bed6f67cdf3a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "renderdoc-sys" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832" + +[[package]] +name = "rfd" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20dafead71c16a34e1ff357ddefc8afc11e7d51d6d2b9fbd07eaa48e3e540220" +dependencies = [ + "block2 0.6.2", + "dispatch2", + "js-sys", + "libc", + "log", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-core-foundation", + "objc2-foundation 0.3.2", + "percent-encoding", + "pollster 0.4.0", + "raw-window-handle", + "wasm-bindgen", + "wasm-bindgen-futures", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "web-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rmcp" +version = "3.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5023804cbe0304cbe06197ac16e645a0a0d0ffa2953bf735c15c2fa0213350" +dependencies = [ + "chrono", + "futures", + "indexmap", + "pastey", + "pin-project-lite", + "rmcp-macros", + "schemars", + "serde", + "serde_json", + "thiserror 2.0.21", + "tokio", + "tokio-stream", + "tokio-util", + "tracing", + "uuid", +] + +[[package]] +name = "rmcp-macros" +version = "3.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1da498700030856ba63114bceefa30ba21f5e278cb975e5b9de3529fe842b40" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "serde_json", + "syn 3.0.9", +] + +[[package]] +name = "ron" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81116b9531d61eabc41aeb228e4b6b2435bcca3233b98cf3b3077d4e6e9debb3" +dependencies = [ + "bitflags 2.13.2", + "once_cell", + "serde", + "serde_derive", + "typeid", + "unicode-ident", +] + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "0.38.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" +dependencies = [ + "bitflags 2.13.2", + "errno", + "libc", + "linux-raw-sys 0.4.15", + "windows-sys 0.59.0", +] + +[[package]] +name = "rustix" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d" +dependencies = [ + "bitflags 2.13.2", + "errno", + "libc", + "linux-raw-sys 0.12.1", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustix-linux-procfs" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fc84bf7e9aa16c4f2c758f27412dc9841341e16aa682d9c7ac308fe3ee12056" +dependencies = [ + "once_cell", + "rustix 1.1.5", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "schemars" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab508826f74a77ca9d5aba6ff19b522583ee3eaf28a19384ff3d0e5835fadf6e" +dependencies = [ + "chrono", + "dyn-clone", + "ref-cast", + "schemars_derive", + "serde", + "serde_json", +] + +[[package]] +name = "schemars_derive" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1c3a92094fa7d61aa124645844facb6b554dfc797136d0f5fd1f890e2bffc69" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals", + "syn 3.0.9", +] + +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "self_cell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab42ca02749e120097e328d91d415325bdf43b1c72c4c8badf37375fe40a813" + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "serde_derive_internals" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f852137cce035d6a4df67ccce505ff6b3e9fd3a10e3e52b24dc71e650bb1a9bd" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_repr" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d3b1629de253c70a0508c3899572da79ca359fdab27c7920ff00406df418906" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "serde_spanned" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7523beb55eece201a2356bee0bbca0d1ab466c14c07703b2e0ee6d42cb0c2c" +dependencies = [ + "serde_core", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "simd-adler32" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" + +[[package]] +name = "simd_cesu8" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11031e251abf8611c80f460e19dbdeb54a66db918e49c65a7065b46ac7aec520" +dependencies = [ + "rustc_version", + "simdutf8", +] + +[[package]] +name = "simdutf8" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" + +[[package]] +name = "siphasher" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" + +[[package]] +name = "skrifa" +version = "0.44.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819ab7d62b1d3e72d9d9dea5650bac30424f9111364bb94928dbf5ecad1baa68" +dependencies = [ + "bytemuck", + "read-fonts", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "slotmap" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdd58c3c93c3d278ca835519292445cb4b0d4dc59ccfdf7ceadaab3f8aeb4038" +dependencies = [ + "version_check", +] + +[[package]] +name = "smallvec" +version = "1.16.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b3dc8af474f516a851ff4bd12db780f948b9250ad37211e4eec0bccea54e01b" +dependencies = [ + "serde", +] + +[[package]] +name = "smithay-client-toolkit" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3457dea1f0eb631b4034d61d4d8c32074caa6cd1ab2d59f2327bd8461e2c0016" +dependencies = [ + "bitflags 2.13.2", + "calloop 0.13.0", + "calloop-wayland-source 0.3.0", + "cursor-icon", + "libc", + "log", + "memmap2", + "rustix 0.38.44", + "thiserror 1.0.69", + "wayland-backend", + "wayland-client", + "wayland-csd-frame", + "wayland-cursor", + "wayland-protocols", + "wayland-protocols-wlr", + "wayland-scanner", + "xkeysym", +] + +[[package]] +name = "smithay-client-toolkit" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0512da38f5e2b31201a93524adb8d3136276fa4fe4aafab4e1f727a82b534cc0" +dependencies = [ + "bitflags 2.13.2", + "calloop 0.14.5", + "calloop-wayland-source 0.4.1", + "cursor-icon", + "libc", + "log", + "memmap2", + "rustix 1.1.5", + "thiserror 2.0.21", + "wayland-backend", + "wayland-client", + "wayland-csd-frame", + "wayland-cursor", + "wayland-protocols", + "wayland-protocols-experimental", + "wayland-protocols-misc", + "wayland-protocols-wlr", + "wayland-scanner", + "xkeysym", +] + +[[package]] +name = "smithay-clipboard" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71704c03f739f7745053bde45fa203a46c58d25bc5c4efba1d9a60e9dba81226" +dependencies = [ + "libc", + "smithay-client-toolkit 0.20.0", + "wayland-backend", +] + +[[package]] +name = "smol_str" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd538fb6910ac1099850255cf94a94df6551fbdd602454387d0adb2d1ca6dead" +dependencies = [ + "serde", +] + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "spirv" +version = "0.4.0+sdk-1.4.341.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9571ea910ebd84c86af4b3ed27f9dbdc6ad06f17c5f96146b2b671e2976744f" +dependencies = [ + "bitflags 2.13.2", +] + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.3", + "once_cell", + "rustix 1.1.5", + "windows-sys 0.61.2", +] + +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "texturecraft" +version = "0.1.0" +dependencies = [ + "eframe", + "egui", + "image", + "log", + "serde_json", + "texturecraft-engine", + "texturecraft-ui-egui", + "winresource", +] + +[[package]] +name = "texturecraft-automation" +version = "0.1.0" +dependencies = [ + "base64 0.22.1", + "cap-std", + "getrandom 0.3.4", + "image", + "rayon", + "rmcp", + "schemars", + "serde", + "serde_json", + "texturecraft-engine", + "texturecraft-formats", + "texturecraft-render", + "thiserror 2.0.21", + "tokio", +] + +[[package]] +name = "texturecraft-bake" +version = "0.1.0" +dependencies = [ + "rayon", + "texturecraft-core", + "texturecraft-geom", + "texturecraft-texel", + "thiserror 2.0.21", +] + +[[package]] +name = "texturecraft-cli" +version = "0.1.0" +dependencies = [ + "image", + "serde_json", + "texturecraft-automation", + "texturecraft-engine", + "tokio", + "winresource", +] + +[[package]] +name = "texturecraft-compose" +version = "0.1.0" +dependencies = [ + "half", + "rayon", + "texturecraft-core", + "texturecraft-geom", + "texturecraft-texel", +] + +[[package]] +name = "texturecraft-core" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", +] + +[[package]] +name = "texturecraft-engine" +version = "0.1.0" +dependencies = [ + "image", + "rayon", + "serde", + "serde_json", + "texturecraft-bake", + "texturecraft-compose", + "texturecraft-core", + "texturecraft-formats", + "texturecraft-geom", + "texturecraft-paint", + "texturecraft-texel", + "thiserror 2.0.21", +] + +[[package]] +name = "texturecraft-formats" +version = "0.1.0" +dependencies = [ + "exr", + "flate2", + "gltf", + "image", + "quick-xml", + "rayon", + "serde", + "serde_json", + "texturecraft-compose", + "texturecraft-core", + "texturecraft-geom", + "texturecraft-texel", + "thiserror 2.0.21", + "tobj", + "zip", +] + +[[package]] +name = "texturecraft-geom" +version = "0.1.0" +dependencies = [ + "glam 0.30.10", + "mikktspace", + "rayon", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "texturecraft-paint" +version = "0.1.0" +dependencies = [ + "rayon", + "serde", + "texturecraft-compose", + "texturecraft-core", + "texturecraft-geom", + "texturecraft-texel", +] + +[[package]] +name = "texturecraft-render" +version = "0.1.0" +dependencies = [ + "bytemuck", + "half", + "image", + "pollster 0.4.0", + "rayon", + "texturecraft-compose", + "texturecraft-core", + "texturecraft-geom", + "texturecraft-texel", + "wgpu", +] + +[[package]] +name = "texturecraft-texel" +version = "0.1.0" + +[[package]] +name = "texturecraft-ui-egui" +version = "0.1.0" +dependencies = [ + "base64 0.22.1", + "eframe", + "egui", + "egui-phosphor", + "egui_dock", + "image", + "js-sys", + "log", + "rayon", + "rfd", + "serde", + "serde_json", + "texturecraft-engine", + "texturecraft-render", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "zip", +] + +[[package]] +name = "texturecraft-web" +version = "0.1.0" +dependencies = [ + "eframe", + "log", + "texturecraft-engine", + "texturecraft-ui-egui", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl 2.0.21", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "tiff" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63feaf3343d35b6ca4d50483f94843803b0f51634937cc2ec519fc32232bc52" +dependencies = [ + "fax", + "flate2", + "half", + "quick-error", + "weezl", + "zune-jpeg", +] + +[[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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce3335fa71841cda333a58d7615b03901380ecf09d59b3296d21f8bbac0dde4e" +dependencies = [ + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "tokio-stream" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3d06f0b082ba57c26b79407372e57cf2a1e28124f78e9479fe80322cf53420b" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e464cf451ba96ebfc6f9b6542f17ee8b8956e33f1e40d9690624e59d7a7f8a4b" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "libc", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "toml" +version = "1.1.8+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20489e00e4d8741d6be680764cc12e270655e375a20d1011e844a9c3379e678d" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_datetime" +version = "1.1.2+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b86d767906c6c42421dcba507eb9d203e779497710a47782a224bb871653053" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_edit" +version = "0.25.17+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3641d5bbb5349a79e1020a242d251efbc546ad8048d133958323ce9c40a9c9c" +dependencies = [ + "indexmap", + "toml_datetime", + "toml_parser", + "winnow", +] + +[[package]] +name = "toml_parser" +version = "1.1.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c" +dependencies = [ + "winnow", +] + +[[package]] +name = "toml_writer" +version = "1.1.3+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06bdbd8cfc056b8d2e2e85f29b56a3bdbecb527cef81eb39e3e7b98af4652770" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "type-map" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb30dbbd9036155e74adad6812e9898d03ec374946234fbcebd5dfc7b9187b90" +dependencies = [ + "rustc-hash 2.1.3", +] + +[[package]] +name = "typeid" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c" + +[[package]] +name = "uds_windows" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" +dependencies = [ + "memoffset", + "tempfile", + "windows-sys 0.61.2", +] + +[[package]] +name = "unicode-general-category" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b993bddc193ae5bd0d623b49ec06ac3e9312875fdae725a975c51db1cc1677f" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "unicode-segmentation" +version = "1.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" + +[[package]] +name = "unicode-width" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" + +[[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + +[[package]] +name = "uuid" +version = "1.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cc1186384beb7dd8eedea376413fd654937285ea6c9cfbb928dc3043ea4b606" +dependencies = [ + "getrandom 0.4.3", + "js-sys", + "serde_core", + "wasm-bindgen", +] + +[[package]] +name = "vello_common" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2e9aed918117e8152c9eddfd8362d73c465c23f26c44786aa331707b8a64fa2" +dependencies = [ + "bytemuck", + "fearless_simd", + "guillotiere", + "log", + "peniko", + "smallvec", + "thiserror 2.0.21", +] + +[[package]] +name = "vello_cpu" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac7349e1f55f6b801c7c277958df4ea53e7f20f21e8014910ad888b2ecda93ea" +dependencies = [ + "bytemuck", + "glifo", + "hashbrown 0.17.1", + "vello_common", +] + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bb54f33acc68fd454578d9820b0bde1a1a3d17aa17bb7b6595806d02886d409" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cbab34de2d982e9b48e18d216d04c4a6f641066ff19ffb699980f591ee3610e" +dependencies = [ + "js-sys", + "tokio", + "wasm-bindgen", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e29d0c35b16e224a7eeb5cd2d25e3e1968fbd65604117b44d3b789d00ee8535" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f501a8bc3719dba86ef8ae4728879c08001bea749eb1333ac5b91e040e2a6b7" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 3.0.9", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23f0c9c52aa7cd7d77769a4cfe2a9adb1b331f489a41d912ce14513d5ab995c6" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wayland-backend" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38a91b4eaddff87b1cd1074985e3713da4af2c49742d1b356b2c01670a67a078" +dependencies = [ + "cc", + "downcast-rs", + "rustix 1.1.5", + "scoped-tls", + "smallvec", + "wayland-sys", +] + +[[package]] +name = "wayland-client" +version = "0.31.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3c36a0f861ad76d0901f2800b46321410d9f73f2ea88aac0650d86c32688073" +dependencies = [ + "bitflags 2.13.2", + "rustix 1.1.5", + "wayland-backend", + "wayland-scanner", +] + +[[package]] +name = "wayland-csd-frame" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "625c5029dbd43d25e6aa9615e88b829a5cad13b2819c4ae129fdbb7c31ab4c7e" +dependencies = [ + "bitflags 2.13.2", + "cursor-icon", + "wayland-backend", +] + +[[package]] +name = "wayland-cursor" +version = "0.31.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a52d18780be9b1314328a3de5f930b73d2200112e3849ca6cb11822793fb34d" +dependencies = [ + "rustix 1.1.5", + "wayland-client", + "xcursor", +] + +[[package]] +name = "wayland-protocols" +version = "0.32.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d0c813de3daa2ed6520af85a3bd49b0e722a3078506899aa9686fea58dc4b6" +dependencies = [ + "bitflags 2.13.2", + "wayland-backend", + "wayland-client", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-experimental" +version = "20250721.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40a1f863128dcaaec790d7b4b396cc9b9a7a079e878e18c47e6c2d2c5a8dcbb1" +dependencies = [ + "bitflags 2.13.2", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-misc" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e9567599ef23e09b8dad6e429e5738d4509dfc46b3b21f32841a304d16b29c8" +dependencies = [ + "bitflags 2.13.2", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-plasma" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b6d8cf1eb2c1c31ed1f5643c88a6e53538129d4af80030c8cabd1f9fa884d91" +dependencies = [ + "bitflags 2.13.2", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-wlr" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb04e52f7836d7c7976c78ca0250d61e33873c34156a2a1fc9474828ec268234" +dependencies = [ + "bitflags 2.13.2", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-scanner" +version = "0.31.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "338e30461b3a2b67d70eb30a6d89f8e0c93a833e07d2ae89085cd070c4a00ac0" +dependencies = [ + "proc-macro2", + "quick-xml", + "quote", +] + +[[package]] +name = "wayland-sys" +version = "0.31.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8eab23fefc9e41f8e841df4a9c707e8a8c4ed26e944ef69297184de2785e3be" +dependencies = [ + "dlib", + "log", + "once_cell", + "pkg-config", +] + +[[package]] +name = "web-sys" +version = "0.3.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88261b9deccee56594c11a3460c462c41f58d148598fe70ad77070126a68aba4" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "weezl" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28ac98ddc8b9274cb41bb4d9d4d5c425b6020c50c46f25559911905610b4a88" + +[[package]] +name = "wgpu" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "527ccdf43dd5b2e8676eed9984ce00e2bbb0a1b85b70c1969dcb6cd2eb55ab9e" +dependencies = [ + "arrayvec", + "bitflags 2.13.2", + "bytemuck", + "cfg-if", + "cfg_aliases", + "document-features", + "hashbrown 0.17.1", + "js-sys", + "log", + "naga", + "parking_lot", + "portable-atomic", + "profiling", + "raw-window-handle", + "smallvec", + "static_assertions", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "wgpu-core", + "wgpu-hal", + "wgpu-types", +] + +[[package]] +name = "wgpu-core" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c018fce9b6270aa203c2fdd56f3cce996713534bd757e4ea58c8560b121f14" +dependencies = [ + "arrayvec", + "bit-set", + "bit-vec", + "bitflags 2.13.2", + "bytemuck", + "cfg_aliases", + "document-features", + "hashbrown 0.17.1", + "indexmap", + "log", + "naga", + "naga-types", + "once_cell", + "parking_lot", + "portable-atomic", + "profiling", + "raw-window-handle", + "rustc-hash 1.1.0", + "smallvec", + "thiserror 2.0.21", + "wgpu-core-deps-apple", + "wgpu-core-deps-emscripten", + "wgpu-core-deps-wasm", + "wgpu-core-deps-windows-linux-android", + "wgpu-hal", + "wgpu-naga-bridge", + "wgpu-types", +] + +[[package]] +name = "wgpu-core-deps-apple" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "061f3d319a40d39d00b1ecc2c33b89fe21d4e6fe01859df3500a3a8ecccd6b68" +dependencies = [ + "wgpu-hal", +] + +[[package]] +name = "wgpu-core-deps-emscripten" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d98b86cf4abf524a902dd35f18ca6a3f08fc2ae9847c8f10b48e30491b1f0b86" +dependencies = [ + "wgpu-hal", +] + +[[package]] +name = "wgpu-core-deps-wasm" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d260ca2ca8adc654f513a869bf3cac0b47f5b8d3f87803d6942015f8de597aae" +dependencies = [ + "wgpu-hal", +] + +[[package]] +name = "wgpu-core-deps-windows-linux-android" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7586165fd5f6d881cb9ce4bb71f40d6caab2c0f1837e3fc1d9788a197fb6004f" +dependencies = [ + "wgpu-hal", +] + +[[package]] +name = "wgpu-hal" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b7fb58561a792bc237628ba0792e332de418fefe145f13b5ed8201e6d52f58" +dependencies = [ + "android_system_properties", + "arrayvec", + "ash", + "bit-set", + "bitflags 2.13.2", + "block2 0.6.2", + "bytemuck", + "cfg-if", + "cfg_aliases", + "glow", + "glutin_wgl_sys", + "gpu-allocator", + "hashbrown 0.17.1", + "js-sys", + "khronos-egl", + "libc", + "libloading", + "log", + "naga", + "naga-types", + "ndk-sys", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-foundation 0.3.2", + "objc2-metal 0.3.2", + "objc2-quartz-core 0.3.2", + "once_cell", + "ordered-float", + "parking_lot", + "portable-atomic", + "portable-atomic-util", + "profiling", + "range-alloc", + "raw-window-handle", + "raw-window-metal", + "renderdoc-sys", + "smallvec", + "static_assertions", + "thiserror 2.0.21", + "wasm-bindgen", + "wayland-sys", + "web-sys", + "wgpu-naga-bridge", + "wgpu-types", + "windows", + "windows-core", + "windows-result", +] + +[[package]] +name = "wgpu-naga-bridge" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f62e73117bb7a62bfd9c5a5841438a823f6566c6442a808ee269d2d055c081" +dependencies = [ + "naga", + "wgpu-types", +] + +[[package]] +name = "wgpu-types" +version = "30.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99dad6f1fbdbbdb4c278a6508b059d44688f5cebddf78d005a46a31340269286" +dependencies = [ + "bitflags 2.13.2", + "bytemuck", + "js-sys", + "log", + "naga-types", + "raw-window-handle", + "static_assertions", + "web-sys", +] + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "windows" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580" +dependencies = [ + "windows-collections", + "windows-core", + "windows-future", + "windows-numerics", +] + +[[package]] +name = "windows-collections" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610" +dependencies = [ + "windows-core", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-future" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb" +dependencies = [ + "windows-core", + "windows-link", + "windows-threading", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-numerics" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26" +dependencies = [ + "windows-core", + "windows-link", +] + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets 0.53.5", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm 0.52.6", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link", + "windows_aarch64_gnullvm 0.53.1", + "windows_aarch64_msvc 0.53.1", + "windows_i686_gnu 0.53.1", + "windows_i686_gnullvm 0.53.1", + "windows_i686_msvc 0.53.1", + "windows_x86_64_gnu 0.53.1", + "windows_x86_64_gnullvm 0.53.1", + "windows_x86_64_msvc 0.53.1", +] + +[[package]] +name = "windows-threading" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_i686_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + +[[package]] +name = "winit" +version = "0.30.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6755fa58a9f8350bd1e472d4c3fcc25f824ec358933bba33306d0b63df5978d" +dependencies = [ + "ahash", + "android-activity", + "atomic-waker", + "bitflags 2.13.2", + "block2 0.5.1", + "bytemuck", + "calloop 0.13.0", + "cfg_aliases", + "concurrent-queue", + "core-foundation", + "core-graphics", + "cursor-icon", + "dpi", + "js-sys", + "libc", + "memmap2", + "ndk", + "objc2 0.5.3", + "objc2-app-kit 0.2.2", + "objc2-foundation 0.2.2", + "objc2-ui-kit 0.2.2", + "orbclient", + "percent-encoding", + "pin-project", + "raw-window-handle", + "redox_syscall 0.4.1", + "rustix 0.38.44", + "smithay-client-toolkit 0.19.2", + "smol_str", + "tracing", + "unicode-segmentation", + "wasm-bindgen", + "wasm-bindgen-futures", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-protocols-plasma", + "web-sys", + "web-time", + "windows-sys 0.52.0", + "x11-dl", + "x11rb", + "xkbcommon-dl", +] + +[[package]] +name = "winnow" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81" +dependencies = [ + "memchr", +] + +[[package]] +name = "winresource" +version = "0.1.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2bc6d36480b829272a5bdabf08e6dca8004761c1da7e35efef800540c66221b" +dependencies = [ + "toml", + "version_check", +] + +[[package]] +name = "winx" +version = "0.36.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f3fd376f71958b862e7afb20cfe5a22830e1963462f3a17f49d82a6c1d1f42d" +dependencies = [ + "bitflags 2.13.2", + "windows-sys 0.59.0", +] + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "x11-dl" +version = "2.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38735924fedd5314a6e548792904ed8c6de6636285cb9fec04d5b1db85c1516f" +dependencies = [ + "libc", + "once_cell", + "pkg-config", +] + +[[package]] +name = "x11rb" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9993aa5be5a26815fe2c3eacfc1fde061fc1a1f094bf1ad2a18bf9c495dd7414" +dependencies = [ + "as-raw-xcb-connection", + "gethostname", + "libc", + "libloading", + "once_cell", + "rustix 1.1.5", + "x11rb-protocol", +] + +[[package]] +name = "x11rb-protocol" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea6fc2961e4ef194dcbfe56bb845534d0dc8098940c7e5c012a258bfec6701bd" + +[[package]] +name = "xcursor" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "163b33ed8786455e2fa5d72f554057ce3f3182425434f756cd39c99839d88e23" + +[[package]] +name = "xkbcommon-dl" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d039de8032a9a8856a6be89cea3e5d12fdd82306ab7c94d74e6deab2460651c5" +dependencies = [ + "bitflags 2.13.2", + "dlib", + "log", + "once_cell", + "xkeysym", +] + +[[package]] +name = "xkeysym" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56" + +[[package]] +name = "xml-rs" +version = "0.8.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7" + +[[package]] +name = "xtask" +version = "0.1.0" +dependencies = [ + "serde_json", +] + +[[package]] +name = "zbus" +version = "5.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5db4be7c075cb421e4b7ee645541604239bd243ba7c357511f4ff3a74b555907" +dependencies = [ + "async-broadcast", + "async-executor", + "async-io", + "async-lock", + "async-process", + "async-recursion", + "async-task", + "async-trait", + "blocking", + "enumflags2", + "event-listener", + "futures-core", + "futures-lite", + "hex", + "libc", + "ordered-stream", + "rustix 1.1.5", + "serde", + "serde_repr", + "tracing", + "uds_windows", + "uuid", + "windows-sys 0.61.2", + "winnow", + "zbus_macros", + "zbus_names", + "zvariant", +] + +[[package]] +name = "zbus-lockstep" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6998de05217a084b7578728a9443d04ea4cd80f2a0839b8d78770b76ccd45863" +dependencies = [ + "zbus_xml", + "zvariant", +] + +[[package]] +name = "zbus-lockstep-macros" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10da05367f3a7b7553c8cdf8fa91aee6b64afebe32b51c95177957efc47ca3a0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "zbus-lockstep", + "zbus_xml", + "zvariant", +] + +[[package]] +name = "zbus_macros" +version = "5.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2990635d09ade6df1868f72f8cac69a876a90981e8bd3c40b1be413f8dc88f40" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 3.0.9", + "zbus_names", + "zvariant", + "zvariant_utils", +] + +[[package]] +name = "zbus_names" +version = "4.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8bf88b4a3ff53e883001e0e0115b297a9d53c31b9c1edd2bfdd853e3428624e" +dependencies = [ + "serde", + "winnow", + "zvariant", +] + +[[package]] +name = "zbus_xml" +version = "5.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1586c021a01ca0a9216dcd874e546382e156a5cbab5fab6cb5f10087e22682a" +dependencies = [ + "serde", + "winnow", + "zbus_names", + "zvariant", +] + +[[package]] +name = "zcheapstr" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1afec51604565183aeb5c54c20aeab286120d4e4460f7f76e3e8bb8c0d99473" +dependencies = [ + "serde", +] + +[[package]] +name = "zerocopy" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "zip" +version = "2.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fabe6324e908f85a1c52063ce7aa26b68dcb7eb6dbc83a2d148403c9bc3eba50" +dependencies = [ + "arbitrary", + "crc32fast", + "crossbeam-utils", + "displaydoc", + "flate2", + "indexmap", + "memchr", + "thiserror 2.0.21", + "zopfli", +] + +[[package]] +name = "zlib-rs" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" + +[[package]] +name = "zopfli" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaf7fc5d30c28483d93805c4a5e12b05bbb52407fa67c5f8bd552374cd01fb11" +dependencies = [ + "bumpalo", + "crc32fast", + "log", + "simd-adler32", +] + +[[package]] +name = "zune-core" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b" + +[[package]] +name = "zune-inflate" +version = "0.2.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02" +dependencies = [ + "simd-adler32", +] + +[[package]] +name = "zune-jpeg" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27bc9d5b815bc103f142aa054f561d9187d191692ec7c2d1e2b4737f8dbd7296" +dependencies = [ + "zune-core", +] + +[[package]] +name = "zvariant" +version = "5.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1d34c27cc6cdd1f458427519dd6b8612f7b7e3f7b9a0b2355d041dda9869147" +dependencies = [ + "endi", + "enumflags2", + "serde", + "winnow", + "zcheapstr", + "zvariant_derive", + "zvariant_utils", +] + +[[package]] +name = "zvariant_derive" +version = "5.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "864155e69b4352db0c7f374917bf45d1e0c8d17659c8b3dbf9795f3673f8c497" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 3.0.9", + "zvariant_utils", +] + +[[package]] +name = "zvariant_utils" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bad0294361a320b694a328460dc73add56c306150f5cb6bfafc44446120008a3" +dependencies = [ + "proc-macro2", + "quote", + "serde", + "syn 3.0.9", + "winnow", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..b671bc82 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,100 @@ +[workspace] +resolver = "3" +members = ["crates/core", "crates/texel", "crates/geom", "crates/compose", "crates/bake", "crates/formats", "crates/paint", "crates/engine", "crates/render", "crates/automation", "crates/ui-egui", "apps/texturecraft", "apps/texturecraft-cli", "apps/texturecraft-web", "xtask"] +default-members = ["crates/core", "crates/texel", "crates/geom", "crates/compose", "crates/bake", "crates/formats", "crates/paint", "crates/engine", "crates/render", "crates/automation", "crates/ui-egui", "apps/texturecraft", "apps/texturecraft-cli", "xtask"] + +[workspace.package] +version = "0.1.0" +edition = "2024" +license = "MIT OR Apache-2.0" +rust-version = "1.95" +repository = "https://github.com/storytold/texturecraft" + +[workspace.dependencies] +# internal +texturecraft-core = { path = "crates/core" } +texturecraft-texel = { path = "crates/texel" } +texturecraft-geom = { path = "crates/geom" } +texturecraft-bake = { path = "crates/bake" } +texturecraft-paint = { path = "crates/paint" } +texturecraft-formats = { path = "crates/formats" } +texturecraft-compose = { path = "crates/compose" } +texturecraft-engine = { path = "crates/engine" } +texturecraft-render = { path = "crates/render" } +texturecraft-automation = { path = "crates/automation" } +texturecraft-ui-egui = { path = "crates/ui-egui" } + +# external +thiserror = "2" +serde = { version = "1", features = ["derive"] } +serde_json = "1" +glam = { version = "0.30", features = ["serde", "bytemuck"] } +bytemuck = { version = "1", features = ["derive"] } +rayon = "1" +log = "0.4" +image = { version = "0.25", default-features = false, features = ["png", "jpeg", "tga", "tiff", "exr", "hdr", "bmp", "webp"] } +half = "2" +exr = { version = "1.73", default-features = false } +eframe = { version = "0.36", default-features = false } +egui = "0.36" +wgpu = "30" +rfd = "0.17" +zip = { version = "2", 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" +quick-xml = "0.41" +mikktspace = { version = "0.3", default-features = false, features = ["glam"] } +proptest = "1" +rmcp = { version = "3.5", default-features = false, features = ["server", "macros", "transport-io"] } +tokio = { version = "1", features = ["rt-multi-thread", "macros", "io-std", "sync", "net", "io-util", "time"] } +schemars = "1" +base64 = "0.22" +cap-std = "4" +getrandom = "0.3" + +[workspace.lints.rust] +unsafe_code = "forbid" + +[workspace.lints.clippy] +dbg_macro = "warn" +todo = "warn" +# Index loops read better than zipped iterators in per-texel code. +needless_range_loop = "allow" +field_reassign_with_default = "allow" + +[profile.dev.package."*"] +opt-level = 2 + +[profile.dev] +opt-level = 1 + +[profile.release] +lto = "thin" +codegen-units = 1 + +[profile.wasm-release] +inherits = "release" +opt-level = "s" +lto = "fat" +codegen-units = 1 +strip = "debuginfo" + +[profile.wasm-release.package."*"] +opt-level = "s" + +[profile.wasm-release.package.texturecraft-texel] +opt-level = 3 +[profile.wasm-release.package.texturecraft-compose] +opt-level = 3 +[profile.wasm-release.package.texturecraft-paint] +opt-level = 3 +[profile.wasm-release.package.texturecraft-geom] +opt-level = 3 +[profile.wasm-release.package.texturecraft-bake] +opt-level = 3 +# Path tracing and the vector math under it run per pixel. +[profile.wasm-release.package.texturecraft-render] +opt-level = 3 +[profile.wasm-release.package.glam] +opt-level = 3 diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 00000000..f433b1a5 --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 00000000..efc19869 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 ArtCraft Team and the TextureCraft contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/NOTICE b/NOTICE new file mode 100644 index 00000000..f8db46ba --- /dev/null +++ b/NOTICE @@ -0,0 +1,16 @@ +TextureCraft +Copyright (c) 2026 ArtCraft Team and the TextureCraft contributors. +Licensed under the MIT License or the Apache License, Version 2.0, at your option +(LICENSE-MIT, LICENSE-APACHE). + +The ArtCraft name, wordmark and logos in docs/brand/ are trademarks of the ArtCraft Team. They are +not open source and are not covered by the license above; their terms are in +docs/brand/LICENSE-brand.txt. + +Third-party material + +This product bundles third-party material under its own licenses, listed below. The per-file +list (author, source, license) is in ATTRIBUTION.md. + +None: every bundled asset is original to TextureCraft (generated in code or drawn for this +project). See ATTRIBUTION.md. diff --git a/README.md b/README.md new file mode 100644 index 00000000..9beb2041 --- /dev/null +++ b/README.md @@ -0,0 +1,278 @@ +

+ + + + ArtCraft + + +

+ +

TextureCraft

+ +

+ 3D texture painting; an open-source, clean-room reimplementation of Adobe Substance 3D Painter, rebuilt in pure Rust.
+ Paint PBR materials straight onto your models in layers with masks, bake mesh maps,
+ drive smart materials from them, and export textures for any engine. Open source, offline, and yours. +

+ +

+ 100% Rust + macOS · Windows · Linux · FreeBSD · Web + License: MIT OR Apache-2.0 + Status: early alpha +

+ +

+ Join the ArtCraft community on Discord +

+ +

+ TextureCraft on getartcraft.com · + ArtCraft · + All Crafting Apps +

+ +
+ +

+ The TextureCraft workspace: the asset shelf on the left, the 3D viewport with a material ball wearing a painted-steel smart material on a rusted iron base, the 2D UV view, and the texture set list, layer stack and properties on the right +
+ The workspace: asset shelf, 3D viewport, 2D UV view, texture sets, layer stack and properties. +

+ +> [!NOTE] +> **ArtCraft is a community of artists from all walks of life.** Painters, sculptors, game +> artists, photographers, filmmakers, illustrators, designers, hobbyists, and people who picked up +> a stylus last week. If you make things, you belong here. **[Come say hi on Discord](https://discord.gg/artcraft).** + +

+ Features · + Painting · + Layers · + Bake and export · + Agents · + Get started · + Downloads · + Crafting Apps · + Discord +

+ +
+ + + + + + + + +
+

🎨 Familiar by design

+ Texture sets, a layer stack with per-channel blending, masks and generators, the tool shelf, the 3D and 2D views: everything sits where a Painter user expects it. +
+

⚡ Native and fast

+ A tiled, copy-on-write texel engine on every CPU core and a wgpu viewport (Vulkan, Metal, DX12, WebGPU). No Electron, no web view, no waiting. +
+

🧵 Seamless by construction

+ Brush stamps live on the 3D surface, so strokes flow across UV seams, and the camera's visibility keeps paint off the back of your model. +
+

🤖 Agent-ready

+ Every action is a command, so you can drive the same engine from the UI, the CLI, a JSON control channel or an MCP server. +
+ +## Features + +### Paint in 3D + +- **Brushes** with size, flow, stroke opacity, spacing, hardness, angle and follow path; size, + angle, flow and position jitter; tangent or camera alignment, angle fade, backface culling, lazy + mouse, X/Y/Z mirror symmetry and pen pressure. Seven procedural brush alphas (round, square, + spray, grunge, cloud, star, dry streaks), or any imported image as the alpha. +- **Tools:** paint, eraser, **projection** with image stencils (hold S to move, turn and scale the + stencil), polygon fill (triangle, UV island or the whole mesh), **smudge**, **clone** (V + click + sets the source), color picker, Shift+click straight lines, and painting directly in the + **2D UV view**. +- **Quick mask** (Y): paint a temporary mask (U to edit, Ctrl+Shift+I to invert) that protects + everything outside it while you paint. +- **Every channel at once:** base color, metallic, roughness, height, normal, emissive, opacity, + ambient occlusion and specular level, each toggled per stroke, the way Painter's material + properties work. + +### Layers, masks and smart materials + +- **Paint, fill and folder layers** with **32 blend modes per channel** (Normal through Normal map + detail), per-channel opacity, visibility, color tags, drag-and-drop reordering, duplicate, group, + flatten, cut, copy and paste across texture sets. +- **Content effects** on any layer: fill, paint, levels, blur, sharpen, invert, threshold and + **HSL**, applied to the layer's channels before it blends. **Anchor points** record the stack at a + layer so masks and fills higher up can react to it (for example, edge detail from painted height). +- **Masks** with a white or black background and a stack of effects: painted strokes, fills, + **mesh-map generators** (edge wear, dirt, thin areas, curvature, AO, position and direction + gradients, all with world-space noise), levels, blur, sharpen, invert, threshold, ID color + selection and compare. Alt+click a mask to view it alone. +- **Fill layers** take uniform values, **procedurals** (clouds, Perlin, noise, cells, grunge, + scratches, checker, stripes, gradient, wood grain, bricks, tiles, fabric weave, leather grain, + hammered, brushed lines, marble veins, Voronoi, streaks, fingerprints, pores, paint chips) or your + own **images**, through UV, tri-planar, planar, spherical or cylindrical projection. +- **43 materials** (metals from measured reflectance, woods, stone and masonry, plastics, fabric, + leather, ceramic, glass), **17 smart materials** (rusted iron, worn and rust-streaked paint, + military paint, oxidized copper, bronze statue, weathered wood, chipped paint, dirty concrete, + mossy brick…) and **10 smart masks** (worn and chipped edges, dirt, dust, ground dirt, rust + leaks, scratches, grunge, paint chips, fingerprints), all procedural, so they work at any + resolution. The shelf groups them in tabs, with search across every tab. +- **Your own library:** right-click a layer or folder, choose *Create smart material*, and it + appears under *My smart materials*, pixels and images included. + +### Bake and export + +- **Bake mesh maps** on every CPU core: ambient occlusion, curvature, thickness, world-space + normal, position, ID, and normal and height maps from a high poly. A 1024² bake of seven maps on + the material ball takes about 1.5 seconds. +- **Export presets** for glTF PBR Metal Roughness, Unreal Engine (packed), Unity HDRP (mask map), + Unity Standard, Blender (Principled BSDF), separate maps, mesh maps and document channels, in + PNG, JPEG, TGA, TIFF, BMP, WEBP, EXR or HDR at 8, 16 or 32 bits, with UV padding, size + override and OpenGL or DirectX normals. +- **Projects** are a single `.tcraft` file: the mesh, every layer (sparse and lossless), baked maps + and imported images. + +### The viewport + +- Physically based shading (metallic/roughness or specular/glossiness) with image-based lighting + from six procedural studio and outdoor environments or **your own HDR/EXR panoramas**, exposure, + rotation, blur, ACES or linear tone mapping, key-light shadows, bloom, vignette, sharpen and + grain, parallax occlusion, and temporal anti-aliasing that refines the image while you look. +- **Rendering mode** (F10): a progressive, path-traced view of your textures with multi-bounce + light, environment importance sampling and one-click PNG save. +- View any channel, any baked mesh map, or a UV checker; Alt+drag to orbit, middle-drag to pan, + wheel to zoom, F to frame, M / C / B to cycle views. + +## Built for agents + +Everything you can do in the window, an agent can do too: + +- **CLI:** `texturecraft-cli new builtin:material_ball out.tcraft`, `bake`, `export`, `render`, + `run `, `commands`. +- **MCP server:** `texturecraft-cli mcp` gives Claude and other agents tools to create projects, + add layers and masks, paint strokes, bake, export, render previews and inspect everything they + changed. +- **Control channel:** launch the app with `--control ` to drive the running UI: run any + command, invoke menu items, paint, and take screenshots. + +See [`docs/control-protocol.md`](docs/control-protocol.md) for the full protocol and examples. + +## Get started + +You need a recent stable Rust toolchain (1.95 or newer). + +```sh +git clone https://github.com/storytold/texturecraft +cd texturecraft +cargo run --release -p texturecraft +``` + +The app opens with a baked sample project. **File ▸ New project** starts from one of the built-in +meshes or your own glTF, GLB, OBJ, PLY or STL file (UVs are generated if the mesh has none). + +Run the tests with `cargo test --workspace --release`. The architecture is described in +[`docs/architecture.md`](docs/architecture.md), the plan and parity estimate in +[`ROADMAP.md`](ROADMAP.md), and contributor rules in [`AGENTS.md`](AGENTS.md). + +## Downloads + +The easiest way to install TextureCraft is from its page on the ArtCraft website: +**[getartcraft.com/apps/texturecraft](https://getartcraft.com/apps/texturecraft)**. + +Technical users can grab builds from GitHub: the +[latest release](https://github.com/storytold/texturecraft/releases/latest) or +[all releases](https://github.com/storytold/texturecraft/releases). + +| Windows | File | +|---|---| +| Installer (x64, x86, arm64) | `texturecraft--windows-.msi` | +| Portable (x64, x86, arm64) | `texturecraft--windows--portable.zip` | + +| macOS | File | +|---|---| +| Universal app (Apple silicon and Intel), signed and notarized | `texturecraft--macos-universal.dmg` | +| Command-line tools | `texturecraft-cli--macos-universal.zip` | + +| Linux (x86_64, aarch64) | File | +|---|---| +| AppImage (any distribution) | `texturecraft--linux-.AppImage` | +| Debian, Ubuntu, Mint, Pop!_OS | `texturecraft--linux-.deb` | +| Fedora, openSUSE, RHEL | `texturecraft--linux-.rpm` | +| Flatpak bundle | `texturecraft--linux-.flatpak` | +| Plain archive | `texturecraft--linux-.tar.gz` | + +| FreeBSD | File | +|---|---| +| x86_64 | `texturecraft--freebsd-x86_64.tar.gz` | + +| Web (WebAssembly) | File | +|---|---| +| Static site to host or embed | `texturecraft-web-.zip` | + +## The Crafting Apps + +TextureCraft is one of the **Crafting Apps**: free, open-source creative tools from the +[ArtCraft](https://getartcraft.com/) team, each written from scratch in Rust and each able to +stand on its own. + +| | App | What it's for | Code | Learn more | +|:-:|---|---|---|---| +| | **TextureCraft** | **3D texture painting · you are here** | [GitHub](https://github.com/storytold/texturecraft) | [Website](https://getartcraft.com/apps/texturecraft) | +| | **PhotoCraft** | Image editing: layers, masks, type and real PSD files | [GitHub](https://github.com/storytold/photocraft) | [Website](https://getartcraft.com/apps/photocraft) | +| | **VectorCraft** | Vector illustration | [GitHub](https://github.com/storytold/vectorcraft) | [Website](https://getartcraft.com/apps/vectorcraft) | +| | **FilmCraft** | Video editing, color and sound | [GitHub](https://github.com/storytold/filmcraft) | [Website](https://getartcraft.com/apps/filmcraft) | +| | **LightCraft** | Photo library and raw development | [GitHub](https://github.com/storytold/lightcraft) | [Website](https://getartcraft.com/apps/lightcraft) | +| | **PdfCraft** | Reading, organizing and protecting PDFs | [GitHub](https://github.com/storytold/pdfcraft) | [Website](https://getartcraft.com/apps/pdfcraft) | +| | **EffectCraft** | Motion graphics and visual effects | [GitHub](https://github.com/storytold/effectcraft) | [Website](https://getartcraft.com/apps/effectcraft) | +| | **DesignCraft** | Page layout and publishing | [GitHub](https://github.com/storytold/designcraft) | [Website](https://getartcraft.com/apps/designcraft) | +| | **GridCraft** | Spreadsheets and data | [GitHub](https://github.com/storytold/gridcraft) | [Website](https://getartcraft.com/apps/gridcraft) | + +And [**ArtCraft**](https://getartcraft.com/) itself, our AI image and video studio for artists who want real control. + +
+ +

+ Join the ArtCraft community on Discord +

+ +

Come make things with us

+ +

+ Our Discord is where artists of every kind hang out: people who paint, sculpt, model, shoot, draw, + cut film, set type, and people still figuring out what they like to make. Share what you're + working on, ask for help, tell us what's broken, or tell us what you wish these tools could do. + Whatever your medium and however long you've been at it, you're welcome here. +

+ +

+ discord.gg/artcraft · + getartcraft.com · + The Crafting Apps · + TextureCraft +

+ +## License and credits + +TextureCraft is dual-licensed under [MIT](LICENSE-MIT) or [Apache-2.0](LICENSE-APACHE), at your option. +Copyright (c) 2026 ArtCraft Team and the TextureCraft contributors. Required notices are in [NOTICE](NOTICE). + +Bundled fonts, icons, images and other assets keep their own open licenses; each one is listed +with its author, source and license in [ATTRIBUTION.md](ATTRIBUTION.md). + +Every material, environment, brush alpha and mesh shown is generated procedurally by TextureCraft +itself; no third-party textures or models appear in the screenshots. + +The ArtCraft name, wordmark and logos in [`docs/brand/`](docs/brand/) are trademarks of the +ArtCraft Team and are not covered by this license. They may be used only unmodified, and only as +part of this repository and TextureCraft, under [`docs/brand/LICENSE-brand.txt`](docs/brand/LICENSE-brand.txt). +Forks and modified versions must remove them. + +Adobe, Substance 3D Painter, Photoshop, Illustrator, Premiere Pro, Lightroom, Acrobat, After Effects and InDesign are trademarks or registered trademarks of Adobe Inc. in the United States and/or other countries. TextureCraft is an independent, open-source project and is not affiliated with, sponsored by or endorsed by Adobe Inc.; these names are used only to describe the workflows it is compatible with. + +

+ ArtCraft
+ Made by the ArtCraft team and community. +

diff --git a/ROADMAP.md b/ROADMAP.md new file mode 100644 index 00000000..1b9f7156 --- /dev/null +++ b/ROADMAP.md @@ -0,0 +1,52 @@ +# TextureCraft roadmap + +TextureCraft aims for 1:1 parity with Adobe Substance 3D Painter's texturing workflow, with better +performance, a smaller footprint, every feature drivable by agents (MCP, CLI, control channel), and +builds for Windows, macOS, Linux, FreeBSD and the web. + +## Honest parity estimate (2026-10-14) + +Weighted by how much of a professional's daily work each area carries. "Done" counts what works +end to end today (engine + UI + automation + tests), not what is planned. + +| Area | Weight | Done | What works | Main gaps | +|---|---:|---:|---|---| +| Project and files | 8 | 75% | New project from glTF/GLB, FBX (binary and ASCII 7.x), COLLADA, OBJ, PLY, STL or built-in meshes (auto-unwrap when UVs are missing), `.tcraft` open/save/save as/save a copy, autosave, recent files, drag and drop (projects, meshes, images), reimport mesh, image resources, remove unused resources, OBJ and textured GLB mesh export, browser open/save/download | USD, Alembic import; FBX 6; project templates; preserving strokes on reimport | +| Texture sets | 6 | 62% | One set per material, list with visibility, sizes 128–8192 with resampling, add/remove channels (11 channel types incl. specular and glossiness), height strength, normal format | UDIM / UV tiles, per-channel bit depth, normal and AO mixing options, UV padding modes | +| Layer stack | 16 | 75% | Paint, fill and folder layers; 32 per-channel blend modes and opacities; white/black/painted masks; generator, fill, levels, invert/threshold, color selection, compare, blur, sharpen and HSL effects on masks **and layer content**; anchor points read by masks and fills above; smart masks; mask view (Alt+click); duplicate, group, drag to reorder, rename, color tags, copy/cut/paste across sets, flatten, instantiate across sets, save layers as smart materials and add them back | Instance layers, mask with height combination, anchor position inside a content stack | +| Painting tools | 14 | 68% | Paint, eraser, projection with image stencils, polygon fill (triangle, UV island, mesh), smudge, clone, color picker, quick mask, Shift+click lines, 2D view painting; size, flow, stroke opacity, spacing, hardness, angle, follow path, jitters, alignment, angle fade, backface culling, lazy mouse, X/Y/Z symmetry, pen pressure, 7 procedural alphas and image alphas, seam-free 3D stamps with camera visibility | Pen tilt, path / filled path / ribbon tools, particle brushes, geometry decals, clone from the current layer only | +| Materials and assets | 8 | 55% | 43 built-in procedural materials, 17 smart materials, 10 smart masks (`mask.smart.apply`), 22 procedurals, 6 environments plus imported HDR/EXR panoramas, image resources, a user smart material library, a tabbed shelf with search across tabs | Filter and alpha libraries; drag from shelf onto layers (Substance `.sbsar` support is a non-goal: the engine is proprietary) | +| Baking | 10 | 60% | AO, curvature, thickness, world normal, position, ID, normal and height from high poly (engine), resolution, antialiasing, dilation, rays, progress and cancel, bake mode UI | High-poly / cage UI, match by name, bent normals, opacity and color transfer, curvature from normal map, GPU ray-traced baking | +| Viewport and display | 12 | 72% | 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, channel / mesh-map / mask / UV checker views, 3D and 2D wireframes, brush cursor, axis gizmos, **path-traced Rendering mode** (F10) with environment importance sampling | Subsurface scattering, depth of field, multiple cameras, GPU path tracing | +| Shader | 6 | 45% | Metallic/roughness and specular/glossiness PBR, height-to-normal, painted normals, parallax occlusion, emissive, opacity (dithered), AO intensity | OpenPBR features (coat, sheen, subsurface, anisotropy, thin film), tessellated displacement | +| Export | 8 | 70% | 8 presets (glTF, Unreal, Unity HDRP/Standard, Blender, separate maps, mesh maps, document channels), PNG/JPEG/TGA/TIFF/BMP/WEBP/EXR/HDR, 8/16/32-bit, padding modes, size override, OpenGL/DirectX normals, template file names, OBJ and textured GLB mesh export, zip download in the browser | Output template editor, USD export | +| UI and UX | 8 | 65% | Painter-style layout: menus, tool shelf, tool bar, asset shelf, texture set list, layer stack with context menu, properties, display/shader/history windows, export dialog, new project dialog, bake mode, rendering mode, settings (interface scale, autosave), saved UI layouts; all 51 menu items live ([docs/parity.md](docs/parity.md)) | Docking, shortcut editor, localization, tooltips everywhere | +| Platforms | 4 | 60% | Windows, macOS (universal, signed and notarized), Linux (AppImage, deb, rpm, tar.gz, Flatpak), FreeBSD and web release workflows | First real release run; web build polish | +| **Total** | **100** | **≈ 65%** | | | + +Beyond Painter: every command is available to agents through MCP, the CLI and the app's +control channel, and the whole engine runs headless. + +## Remaining effort + +Estimated in wall-clock hours of Claude Opus 5.5 agent work, with three or four agents working in +parallel on separate crates: + +| Milestone | Scope | Estimate | +|---|---|---:| +| M1 Layer stack depth | Instance layers, height-combined masks | 3 h | +| M2 Tools | Path, filled-path and ribbon tools, particle brushes, decals, pen tilt | 6 h | +| M3 Assets | Filter and alpha libraries, shelf drag and drop | 5 h | +| M4 Baking | High-poly and cage UI, match by name, bent normals, transfers, GPU ray-traced baking | 8 h | +| M5 Viewport | SSS, depth of field, cameras, GPU path tracing | 6 h | +| M6 Shader | OpenPBR lobes (coat, sheen, subsurface, anisotropy), displacement | 4 h | +| M7 Formats | USD, Alembic import; USD export; output template editor; UDIM | 8 h | +| M8 UX | Docking, shortcut editor, localization, performance passes at 4K/8K | 6 h | +| M9 Release | First signed release on every platform, web deployment, README screenshots refresh | 4 h | +| **Total** | | **≈ 50 h** | + +## Current focus + +1. Baking (M4): high-poly and cage UI, match by name, bent normals, transfers. +2. Tools (M2): path, filled-path and ribbon tools; particle brushes. +3. UX (M8): docking and a shortcut editor; release (M9) once the repository exists. diff --git a/apps/texturecraft-cli/Cargo.toml b/apps/texturecraft-cli/Cargo.toml new file mode 100644 index 00000000..d458a8d0 --- /dev/null +++ b/apps/texturecraft-cli/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "texturecraft-cli" +description = "Headless TextureCraft: info, run, batch, bake, export, render, MCP server, JSON-lines server" +version.workspace = true +edition.workspace = true +license.workspace = true +build = "build.rs" + +[lints] +workspace = true + +[dependencies] +texturecraft-automation = { workspace = true } +texturecraft-engine = { workspace = true } +serde_json = { workspace = true } +tokio = { workspace = true } + +[dev-dependencies] +image = { workspace = true } + +[build-dependencies] +winresource = "0.1" diff --git a/apps/texturecraft-cli/build.rs b/apps/texturecraft-cli/build.rs new file mode 100644 index 00000000..37e0bd12 --- /dev/null +++ b/apps/texturecraft-cli/build.rs @@ -0,0 +1,2 @@ +// The same build provenance and Windows icon/VERSIONINFO as the app; see that file. +include!("../texturecraft/build.rs"); diff --git a/apps/texturecraft-cli/src/main.rs b/apps/texturecraft-cli/src/main.rs new file mode 100644 index 00000000..5cc9a6a3 --- /dev/null +++ b/apps/texturecraft-cli/src/main.rs @@ -0,0 +1,403 @@ +//! Headless TextureCraft command line: inspect, script, bake, export and render projects, and +//! serve agents over MCP or JSON lines. Never opens a window. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +use std::path::Path; +use std::sync::{Arc, Mutex}; + +use serde_json::{Value, json}; +use texturecraft_automation::security::{client_token, server_token}; +use texturecraft_automation::{AuthorizedWorkspace, BridgeClient, Filesystem, Headless, RenderOptions, TexturecraftMcp, rpc}; +use texturecraft_engine::core::PROJECT_EXT; +use texturecraft_engine::formats::{MeshReport, import_mesh_bytes}; + +const USAGE: &str = "\ +texturecraft-cli: headless TextureCraft + +USAGE: + texturecraft-cli --version + texturecraft-cli info + Texture sets, layers, triangles, materials and the UV report, as JSON. + texturecraft-cli commands [--filter ] + List the engine commands (id, label, params). + texturecraft-cli run [params-json] [-o ] + Run one command and print its JSON result; -o saves the project. + texturecraft-cli batch + Run [{\"id\", \"params\"}, ...] or {\"open\": , \"steps\": [...], \"save\": }. + texturecraft-cli bake [-o ] [--resolution ] [--maps ] + Bake mesh maps for every texture set (a project is saved in place without -o). + texturecraft-cli export [--preset ] [--format ] [--padding ] [--size ] + Export textures (default preset: Document channels + Normal + AO). + texturecraft-cli render [--size x] [--view 3d|2d] [--yaw ] [--pitch ] + [--samples ] [--bounces ] + Render the viewport to PNG (needs a GPU or software adapter); --samples path traces it on + the CPU instead (Rendering mode). + texturecraft-cli new > [--resolution ] [--sample] + New project; --sample bakes and applies a smart material to every texture set. + texturecraft-cli mcp [--bridge ] [--token ] [--read-root ] [--write-root ] + MCP server on stdio: headless (file access only below the roots), or bridged to + `texturecraft --control `. + texturecraft-cli serve [--port ] [--read-root ] [--write-root ] + JSON lines ({\"id\",\"method\",\"params\"}) on stdio, or on 127.0.0.1: with a token. + + is a .tcraft project, a mesh file (.glb .gltf .fbx .dae .obj .ply .stl) or builtin: +(material_ball, sphere, cube, rounded_cube, cylinder, torus, plane, crate). +See docs/control-protocol.md. +"; + +/// (name, flags taking a value, bare flags) +const SUBCOMMANDS: &[(&str, &[&str], &[&str])] = &[ + ("info", &[], &[]), + ("commands", &["--filter"], &[]), + ("run", &["-o"], &[]), + ("batch", &[], &[]), + ("bake", &["-o", "--resolution", "--maps"], &[]), + ("export", &["--preset", "--format", "--padding", "--size"], &[]), + ("render", &["--size", "--view", "--yaw", "--pitch", "--samples", "--bounces"], &[]), + ("new", &["--resolution"], &["--sample"]), + ("mcp", &["--bridge", "--token", "--read-root", "--write-root"], &[]), + ("serve", &["--port", "--read-root", "--write-root"], &[]), +]; + +struct Args { + pos: Vec, + flags: Vec<(String, Option)>, +} + +impl Args { + fn get(&self, k: &str) -> Option<&str> { + self.flags.iter().rev().find(|(f, _)| f == k).and_then(|(_, v)| v.as_deref()) + } + fn has(&self, k: &str) -> bool { + self.flags.iter().any(|(f, _)| f == k) + } + fn num(&self, k: &str) -> Result, String> { + self.get(k).map(|v| v.parse().map_err(|_| format!("{k}: `{v}` is not a valid number"))).transpose() + } + fn pos(&self, n: usize, what: &str) -> Result<&str, String> { + self.pos.get(n).map(String::as_str).ok_or_else(|| format!("missing {what}")) + } +} + +fn parse(values: &[&str], bare: &[&str], args: &[String]) -> Result { + let mut a = Args { pos: Vec::new(), flags: Vec::new() }; + let mut it = args.iter(); + while let Some(s) = it.next() { + let (k, inline) = match s.split_once('=') { + Some((k, v)) if k.starts_with("--") => (k, Some(v.to_string())), + _ => (s.as_str(), None), + }; + if values.contains(&k) { + let v = match inline { + Some(v) => v, + None => it.next().ok_or_else(|| format!("{k} needs a value"))?.clone(), + }; + a.flags.push((k.to_string(), Some(v))); + } else if bare.contains(&k) && inline.is_none() { + a.flags.push((k.to_string(), None)); + } else if k.starts_with('-') && k.len() > 1 && k.parse::().is_err() { + return Err(format!("unknown option `{s}`")); + } else { + a.pos.push(s.clone()); + } + } + Ok(a) +} + +fn version() -> String { + let sha = env!("TEXTURECRAFT_BUILD_SHA"); + let short = sha.get(..7).unwrap_or(sha); + let date = env!("TEXTURECRAFT_BUILD_DATE"); + match (short.is_empty(), date.is_empty()) { + (true, true) => format!("TextureCraft {}", texturecraft_engine::VERSION), + (true, false) => format!("TextureCraft {} ({date})", texturecraft_engine::VERSION), + _ => format!("TextureCraft {} ({short} {date})", texturecraft_engine::VERSION), + } +} + +fn main() { + let args: Vec = std::env::args().skip(1).collect(); + let code = match args.first().map(String::as_str) { + None => { + eprint!("{USAGE}"); + 2 + } + Some("--version" | "-V" | "version") => { + println!("{}", version()); + 0 + } + Some("--help" | "-h" | "help") => { + print!("{USAGE}"); + 0 + } + Some(cmd) => match SUBCOMMANDS.iter().find(|(n, _, _)| *n == cmd) { + None => { + eprintln!("texturecraft-cli: unknown command `{cmd}`\n\n{USAGE}"); + 2 + } + Some(_) if args.iter().any(|a| a == "--help" || a == "-h") => { + print!("{USAGE}"); + 0 + } + Some((name, values, bare)) => match parse(values, bare, args.get(1..).unwrap_or_default()) { + Err(e) => { + eprintln!("texturecraft-cli {name}: {e}\n\n{USAGE}"); + 2 + } + Ok(a) => match dispatch(name, &a) { + Ok(()) => 0, + Err(e) => { + eprintln!("texturecraft-cli {name}: error: {e}"); + 1 + } + }, + }, + }, + }; + std::process::exit(code); +} + +type R = Result<(), String>; + +fn dispatch(name: &str, a: &Args) -> R { + match name { + "info" => info(a), + "commands" => commands(a), + "run" => run(a), + "batch" => batch(a), + "bake" => bake(a), + "export" => export(a), + "render" => render(a), + "new" => new(a), + "mcp" => mcp(a), + "serve" => serve(a), + other => Err(format!("unknown command `{other}`")), + } +} + +fn s(e: impl std::fmt::Display) -> String { + e.to_string() +} + +fn print_json(v: &Value) -> R { + println!("{}", serde_json::to_string_pretty(v).map_err(s)?); + Ok(()) +} + +fn is_project(path: &str) -> bool { + Path::new(path).extension().is_some_and(|e| e.eq_ignore_ascii_case(PROJECT_EXT)) +} + +/// A trusted local session on `input`: a project, a mesh file or `builtin:`. +fn open(input: &str, resolution: Option) -> Result { + let mut h = Headless::new(Headless::default_session().map_err(s)?, Filesystem::TrustedLocal); + match input.strip_prefix("builtin:") { + Some(id) => h.command_run("project.new", json!({"mesh": id, "resolution": resolution.unwrap_or(1024), "name": id})).map(|_| ()), + None if is_project(input) => h.open(input).map(|_| ()), + None => h.import_mesh(input, resolution, None).map(|_| ()), + } + .map_err(s)?; + Ok(h) +} + +fn save(h: &mut Headless, out: &str) -> R { + let r = h.save(Some(out)).map_err(s)?; + eprintln!("saved {}", r["path"].as_str().unwrap_or_default()); + Ok(()) +} + +fn report_json(r: &MeshReport) -> Value { + json!({ + "triangles": r.triangles, "vertices": r.vertices, "materials": r.materials, "uvsGenerated": r.uvs_generated, + "uvOutOfRange": r.uv_out_of_range, "overlapRatio": r.overlap_ratio, "zeroAreaUvTriangles": r.zero_area_uv_triangles, "warnings": r.warnings, + }) +} + +fn info(a: &Args) -> R { + let file = a.pos(0, "")?; + if !is_project(file) { + let bytes = std::fs::read(file).map_err(|e| format!("cannot read {file}: {e}"))?; + let m = import_mesh_bytes(file, &bytes).map_err(s)?; + return print_json(&json!({ + "file": file, "kind": "mesh", "triangles": m.mesh.triangle_count(), "vertices": m.mesh.vertex_count(), + "materials": m.mesh.materials, "textureSets": m.mesh.materials, "uv": report_json(&m.report), + })); + } + let h = open(file, None)?; + let mut v = h.inspect(); + v["file"] = json!(file); + v["kind"] = json!("project"); + // The embedded mesh is re-imported for its UV report (built-in meshes have none). + v["uv"] = match &h.session.mesh_source { + Some((ext, bytes)) => import_mesh_bytes(&format!("mesh.{ext}"), bytes).map(|m| report_json(&m.report)).map_err(s)?, + None => Value::Null, + }; + print_json(&v) +} + +fn commands(a: &Args) -> R { + let h = open("builtin:cube", Some(128))?; + for c in h.command_list(a.get("--filter")).as_array().into_iter().flatten() { + let f = |k: &str| c.get(k).and_then(Value::as_str).unwrap_or_default().to_string(); + println!("{:<28} {:<36} {}", f("id"), f("label"), f("params")); + } + Ok(()) +} + +fn run(a: &Args) -> R { + let (input, id) = (a.pos(0, "")?, a.pos(1, "")?); + let params: Value = match a.pos.get(2) { + Some(j) => serde_json::from_str(j).map_err(|e| format!("params are not JSON: {e}"))?, + None => json!({}), + }; + let mut h = open(input, None)?; + print_json(&h.command_run(id, params).map_err(s)?)?; + match a.get("-o") { + Some(out) => save(&mut h, out), + None => Ok(()), + } +} + +fn batch(a: &Args) -> R { + let path = a.pos(0, "")?; + let text = std::fs::read_to_string(path).map_err(|e| format!("cannot read {path}: {e}"))?; + let script: Value = serde_json::from_str(&text).map_err(|e| format!("{path} is not JSON: {e}"))?; + let steps = + script.as_array().or_else(|| script.get("steps").and_then(Value::as_array)).ok_or("the script must be a list of steps or {\"steps\": [...]}")?; + let field = |k: &str| script.get(k).and_then(Value::as_str); + let mut h = open(field("open").unwrap_or("builtin:material_ball"), None)?; + let reply = h.batch(steps); + print_json(&reply)?; + if let Some(f) = reply.get("failed").filter(|f| !f.is_null()) { + return Err(format!("step {} (`{}`) failed: {}", f["index"], f["id"].as_str().unwrap_or_default(), f["error"].as_str().unwrap_or_default())); + } + match field("save") { + Some(out) => save(&mut h, out), + None => Ok(()), + } +} + +fn bake(a: &Args) -> R { + let input = a.pos(0, "")?; + let out = a.get("-o").or(is_project(input).then_some(input)).ok_or("give -o to save the baked project")?; + let mut p = json!({"ts": "all"}); + if let Some(r) = a.num::("--resolution")? { + p["resolution"] = json!(r); + } + if let Some(m) = a.get("--maps") { + p["maps"] = json!(m.split(',').map(str::trim).filter(|m| !m.is_empty()).collect::>()); + } + let mut h = open(input, None)?; + h.command_run("bake.run", p).map_err(s)?; + save(&mut h, out)?; + print_json(&h.inspect()["textureSets"]) +} + +fn export(a: &Args) -> R { + let (input, dir) = (a.pos(0, "")?, a.pos(1, "")?); + let mut p = json!({"dir": dir}); + for k in ["preset", "format", "padding"] { + if let Some(v) = a.get(&format!("--{k}")) { + p[k] = json!(v); + } + } + if let Some(v) = a.num::("--size")? { + p["size"] = json!(v); + } + let mut h = open(input, None)?; + let r = h.export(&p).map_err(s)?; + for w in r["warnings"].as_array().into_iter().flatten() { + eprintln!("warning: {}", w.as_str().unwrap_or_default()); + } + for f in r["files"].as_array().into_iter().flatten() { + println!("{}", f.as_str().unwrap_or_default()); + } + Ok(()) +} + +fn render(a: &Args) -> R { + let (input, out) = (a.pos(0, "")?, a.pos(1, "")?); + let (width, height) = match a.get("--size") { + Some(sz) => { + let (w, h) = sz.split_once(['x', 'X']).ok_or("--size must be x, e.g. 1280x800")?; + (Some(w.parse().map_err(|_| "bad --size width")?), Some(h.parse().map_err(|_| "bad --size height")?)) + } + None => (None, None), + }; + let o = RenderOptions { + view: a.get("--view").map(str::to_string), + width, + height, + yaw: a.num("--yaw")?, + pitch: a.num("--pitch")?, + samples: a.num("--samples")?, + bounces: a.num("--bounces")?, + }; + let mut h = open(input, None)?; + let (w, ht, png) = h.render_png(&o).map_err(s)?; + std::fs::write(out, png).map_err(|e| format!("cannot write {out}: {e}"))?; + let by = if o.samples.is_some() { "path traced" } else { h.adapter().unwrap_or("GPU") }; + eprintln!("wrote {out} ({w}x{ht}, {by})"); + Ok(()) +} + +fn new(a: &Args) -> R { + let (input, out) = (a.pos(0, "")?, a.pos(1, "")?); + if is_project(input) { + return Err(format!("{input} is already a project")); + } + let res = a.num::("--resolution")?; + let mut h = open(input, res)?; + if a.has("--sample") { + let bake_res = h.session.doc.project.texture_sets.first().map_or(1024, |t| t.resolution.min(1024)); + h.command_run("bake.run", json!({"ts": "all", "resolution": bake_res})).map_err(s)?; + for ts in 0..h.session.set_count() { + h.command_run("textureSet.select", json!({"ts": ts})).map_err(s)?; + h.command_run("material.apply", json!({"material": "steel_painted_worn"})).map_err(s)?; + } + h.command_run("textureSet.select", json!({"ts": 0})).map_err(s)?; + } + save(&mut h, out) +} + +fn workspace(a: &Args) -> Result { + let (r, w) = (a.get("--read-root").map(Path::new), a.get("--write-root").map(Path::new)); + if r.is_none() && w.is_none() { + return Ok(Filesystem::Denied); + } + Ok(Filesystem::Workspace(AuthorizedWorkspace::new(r, w).map_err(s)?)) +} + +fn port_of(v: &str) -> Result { + let p = v.strip_prefix("127.0.0.1:").or_else(|| v.strip_prefix("localhost:")).unwrap_or(v); + p.parse::().ok().filter(|p| *p != 0).ok_or_else(|| format!("`{v}` is not a loopback port (1-65535)")) +} + +fn mcp(a: &Args) -> R { + let server = match a.get("--bridge") { + Some(b) => { + let port = port_of(b)?; + TexturecraftMcp::bridge(BridgeClient::new(port, client_token(port, a.get("--token").map(str::to_string)).map_err(s)?)) + } + None => TexturecraftMcp::headless(Headless::new(Headless::default_session().map_err(s)?, workspace(a)?)), + }; + let rt = tokio::runtime::Builder::new_multi_thread().enable_all().build().map_err(s)?; + rt.block_on(server.serve_stdio()).map_err(s) +} + +fn serve(a: &Args) -> R { + let h = Arc::new(Mutex::new(Headless::new(Headless::default_session().map_err(s)?, workspace(a)?))); + match a.get("--port") { + None => rpc::serve_lines(&h, std::io::stdin().lock(), std::io::stdout().lock()).map_err(s), + Some(p) => { + let port = port_of(p)?; + let (token, file) = server_token(port).map_err(s)?; + rpc::serve_tcp(port, h, token, |addr| match file { + Some(f) => eprintln!("texturecraft-cli: serving on {addr}; token in {}", f.display()), + None => eprintln!("texturecraft-cli: serving on {addr}; token from TEXTURECRAFT_CONTROL_TOKEN"), + }) + .map_err(s) + } + } +} diff --git a/apps/texturecraft-cli/tests/cli.rs b/apps/texturecraft-cli/tests/cli.rs new file mode 100644 index 00000000..31d4b8c7 --- /dev/null +++ b/apps/texturecraft-cli/tests/cli.rs @@ -0,0 +1,129 @@ +//! Drives the `texturecraft-cli` binary. + +use std::path::{Path, PathBuf}; +use std::process::{Command, Output}; + +use serde_json::Value; + +fn cli(args: &[&str]) -> Output { + Command::new(env!("CARGO_BIN_EXE_texturecraft-cli")).args(args).output().unwrap() +} + +fn ok(args: &[&str]) -> String { + let o = cli(args); + assert!(o.status.success(), "{args:?} failed: {}", String::from_utf8_lossy(&o.stderr)); + String::from_utf8(o.stdout).unwrap() +} + +fn json(args: &[&str]) -> Value { + serde_json::from_str(&ok(args)).unwrap() +} + +fn tmp(name: &str) -> PathBuf { + let d = std::env::temp_dir().join(format!("tc-cli-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d +} + +fn p(path: &Path) -> &str { + path.to_str().unwrap() +} + +#[test] +fn version_and_usage_errors() { + let v = ok(&["--version"]); + assert!(v.starts_with("TextureCraft "), "{v}"); + assert_eq!(v.lines().count(), 1); + assert!(ok(&["--help"]).contains("texturecraft-cli mcp")); + assert_eq!(cli(&[]).status.code(), Some(2)); + assert_eq!(cli(&["frobnicate"]).status.code(), Some(2)); + assert_eq!(cli(&["info", "--bogus"]).status.code(), Some(2)); + let o = cli(&["info", "does/not/exist.tcraft"]); + assert_eq!(o.status.code(), Some(1)); + assert!(String::from_utf8_lossy(&o.stderr).contains("error")); + assert_eq!(cli(&["run", "builtin:nope", "layer.add.fill"]).status.code(), Some(1)); + assert_eq!(cli(&["run", "builtin:cube", "layer.add.fill", "{not json"]).status.code(), Some(1)); + assert_eq!(cli(&["render", "builtin:cube", "x.png", "--size", "big"]).status.code(), Some(1)); +} + +#[test] +fn new_info_run_bake_export_batch() { + let d = tmp("flow"); + let a = d.join("a.tcraft"); + ok(&["new", "builtin:cube", p(&a), "--resolution", "128"]); + let info = json(&["info", p(&a)]); + assert_eq!(info["kind"], "project"); + assert_eq!(info["textureSets"][0]["resolution"], 128); + assert!(info["mesh"]["triangles"].as_u64().unwrap() >= 12); + + let b = d.join("b.tcraft"); + let r = json(&["run", p(&a), "layer.add.fill", r#"{"color":[1,0,0],"roughness":0.3}"#, "-o", p(&b)]); + assert!(r["layer"].is_u64(), "{r}"); + assert_eq!(json(&["info", p(&b)])["layers"][0]["kind"], "fill"); + + ok(&["bake", p(&b), "--resolution", "128", "--maps", "ambientOcclusion,curvature"]); + let maps = json(&["info", p(&b)])["textureSets"][0]["baked"]["maps"].clone(); + assert_eq!(maps.as_array().unwrap().len(), 2, "{maps}"); + + let tex = d.join("tex"); + let files = ok(&["export", p(&b), p(&tex), "--preset", "glTF PBR Metal Roughness", "--size", "128"]); + assert!(files.lines().count() >= 3, "{files}"); + for f in files.lines() { + assert!(Path::new(f).is_file(), "{f}"); + } + + let script = d.join("script.json"); + let c = d.join("c.tcraft"); + let body = serde_json::json!({"open": p(&b), "steps": [{"id": "layer.add.paint", "params": {"name": "Scripted"}}], "save": p(&c)}); + std::fs::write(&script, body.to_string()).unwrap(); + assert_eq!(json(&["batch", p(&script)])["completed"], 1); + assert_eq!(json(&["info", p(&c)])["layers"][0]["name"], "Scripted"); + std::fs::write(&script, r#"[{"id":"layer.add.paint"},{"id":"no.such"}]"#).unwrap(); + assert_eq!(cli(&["batch", p(&script)]).status.code(), Some(1)); + let _ = std::fs::remove_dir_all(&d); +} + +#[test] +fn info_on_a_mesh_reports_uvs() { + let d = tmp("mesh"); + let obj = d.join("quad.obj"); + std::fs::write(&obj, "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\nusemtl Paint\nf 1/1 2/2 3/3 4/4\n").unwrap(); + let info = json(&["info", p(&obj)]); + assert_eq!(info["kind"], "mesh"); + assert_eq!(info["triangles"], 2); + assert_eq!(info["materials"][0], "Paint"); + assert_eq!(info["uv"]["uvsGenerated"], false); + let _ = std::fs::remove_dir_all(&d); +} + +#[test] +fn render_writes_a_png() { + let d = tmp("render"); + let out = d.join("ball.png"); + let o = cli(&["render", "builtin:material_ball", p(&out), "--size", "160x120", "--yaw", "30"]); + if !o.status.success() { + let err = String::from_utf8_lossy(&o.stderr); + assert!(err.contains("cannot render"), "{err}"); + eprintln!("skipping: {err}"); + return; + } + let img = image::open(&out).unwrap(); + assert_eq!((img.width(), img.height()), (160, 120)); + let _ = std::fs::remove_dir_all(&d); +} + +#[test] +fn path_traced_render_needs_no_gpu() { + let d = tmp("trace"); + let out = d.join("ball.png"); + ok(&["render", "builtin:material_ball", p(&out), "--size", "96x64", "--samples", "4"]); + let img = image::open(&out).unwrap().to_rgb8(); + assert_eq!(img.dimensions(), (96, 64)); + // The lit ball in the middle is brighter than the dark backdrop corner. + let lum = |x, y| img.get_pixel(x, y).0.iter().map(|v| *v as u32).sum::(); + assert!(lum(48, 28) > lum(1, 1) + 60); + assert_eq!(cli(&["render", "builtin:cube", p(&out), "--samples", "0"]).status.code(), Some(1)); + assert_eq!(cli(&["render", "builtin:cube", p(&out), "--samples", "4", "--view", "2d"]).status.code(), Some(1)); + let _ = std::fs::remove_dir_all(&d); +} diff --git a/apps/texturecraft-web/Cargo.toml b/apps/texturecraft-web/Cargo.toml new file mode 100644 index 00000000..92238e43 --- /dev/null +++ b/apps/texturecraft-web/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "texturecraft-web" +description = "TextureCraft in the browser (eframe web runner, WebGPU with WebGL2 fallback)" +version.workspace = true +edition.workspace = true +license.workspace = true +publish = false + +[lints] +workspace = true + +[[bin]] +name = "texturecraft-web" +path = "src/main.rs" + +[dependencies] +texturecraft-engine = { workspace = true } +texturecraft-ui-egui = { workspace = true } +log = { workspace = true } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +# `wgpu` pulls egui-wgpu's defaults: WebGPU plus the WebGL2 fallback. +eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit", "persistence"] } +wasm-bindgen = "0.2" +wasm-bindgen-futures = "0.4" +web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlCanvasElement", "Location"] } diff --git a/apps/texturecraft-web/Trunk.toml b/apps/texturecraft-web/Trunk.toml new file mode 100644 index 00000000..3befdb40 --- /dev/null +++ b/apps/texturecraft-web/Trunk.toml @@ -0,0 +1,10 @@ +# Build with: trunk build --release (from apps/texturecraft-web; packaging/web/package.sh) +[build] +target = "index.html" +dist = "../../dist/web" +public_url = "./" +filehash = true + +[serve] +addresses = ["127.0.0.1"] +port = 8765 diff --git a/apps/texturecraft-web/index.html b/apps/texturecraft-web/index.html new file mode 100644 index 00000000..738f8699 --- /dev/null +++ b/apps/texturecraft-web/index.html @@ -0,0 +1,30 @@ + + + + + + TextureCraft + + + + + +
+

Loading TextureCraft…

+

This is taking longer than usual. If TextureCraft doesn't start, its files may have failed to + download or come from a different version of this page. Reload to try again; the browser + console lists what failed.

+
+ + diff --git a/apps/texturecraft-web/src/main.rs b/apps/texturecraft-web/src/main.rs new file mode 100644 index 00000000..cd4045c4 --- /dev/null +++ b/apps/texturecraft-web/src/main.rs @@ -0,0 +1,41 @@ +//! TextureCraft in the browser. +//! +//! Runs the same [`texturecraft_ui_egui::TextureCraftApp`] as the desktop app through eframe's web +//! runner (wgpu: WebGPU where available, WebGL2 otherwise). Build with `trunk build --release` +//! from this directory (`packaging/web/package.sh` zips the result). +//! +//! Differences from the desktop app: no control server (browsers can't listen on sockets), and +//! file I/O goes through `texturecraft_ui_egui::web_files` (file picker in, downloads out). +//! +//! URL query flag: `?webgl` forces the WebGL2 backend instead of WebGPU. + +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +#[cfg(target_arch = "wasm32")] +mod web; + +#[cfg(target_arch = "wasm32")] +fn main() { + web::start(); +} + +#[cfg(not(target_arch = "wasm32"))] +fn main() { + eprintln!("texturecraft-web only runs in the browser: build it with `trunk build --release` in apps/texturecraft-web"); +} + +#[cfg(test)] +mod tests { + /// A WebAssembly module that never downloads runs no Rust, so the page itself replaces the + /// endless "Loading" with a way to retry, without a hand-written script. + #[test] + fn loading_page_offers_a_reload_when_startup_stalls() { + let html = include_str!("../index.html"); + let (_, hint) = html.split_once("

").expect("stalled-startup hint in the loading element"); + assert!(hint.contains("failed to") && hint.contains("Reload"), "{hint}"); + assert!(html.contains("animation: texturecraft-stalled"), "the hint appears after a delay"); + assert!(html.contains("id=\"texturecraft_canvas\"") && html.contains("data-cargo-profile=\"wasm-release\"")); + assert!(!html.contains("().ok()) { + Some(canvas) => run(canvas).await, + None => Err(format!("the page has no ")), + }; + if let Some(el) = document.get_element_by_id(LOADING_ID) { + match result { + Ok(()) => el.remove(), + Err(e) => { + log::error!("TextureCraft failed to start: {e}"); + el.set_text_content(Some(&format!("TextureCraft failed to start: {e}. It needs a browser with WebGPU or WebGL2."))); + } + } + } + }); +} + +async fn run(canvas: web_sys::HtmlCanvasElement) -> Result<(), String> { + let mut options = eframe::WebOptions::default(); + if query().contains("webgl") + && let eframe::egui_wgpu::WgpuSetup::CreateNew(create) = &mut options.wgpu_options.wgpu_setup + { + create.instance_descriptor.backends = eframe::wgpu::Backends::GL; + } + eframe::WebRunner::new() + .start( + canvas, + options, + Box::new(|cc| { + if let Some(rs) = &cc.wgpu_render_state { + log::info!("TextureCraft: wgpu backend {:?}", rs.adapter.get_info().backend); + } + Ok(Box::new(TextureCraftApp::new(cc, initial_session()?))) + }), + ) + .await + .map_err(|e| e.as_string().unwrap_or_else(|| format!("{e:?}"))) +} + +/// The baked sample project, or an empty material ball if that fails. Texture and bake sizes are +/// kept small: the web build composites and bakes on one thread (bake 256 took ~12 s to start on a +/// fast desktop CPU, 128 ~7.5 s). +fn initial_session() -> Result { + texturecraft_engine::sample_session(1024, 128) + .or_else(|e| { + log::warn!("sample project failed: {e}"); + Session::new_project("Untitled", texturecraft_engine::geom::primitives::material_ball(), "builtin:material_ball", None, 1024) + }) + .map_err(|e| e.to_string()) +} + +fn query() -> String { + web_sys::window().and_then(|w| w.location().search().ok()).unwrap_or_default() +} diff --git a/apps/texturecraft/Cargo.toml b/apps/texturecraft/Cargo.toml new file mode 100644 index 00000000..66964286 --- /dev/null +++ b/apps/texturecraft/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "texturecraft" +description = "TextureCraft: an open-source 3D texture painter (desktop app)" +version.workspace = true +edition.workspace = true +license.workspace = true +default-run = "texturecraft" +build = "build.rs" + +[lints] +workspace = true + +[dependencies] +texturecraft-engine = { workspace = true } +texturecraft-ui-egui = { workspace = true } +eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit", "wayland", "x11", "persistence"] } +egui = { workspace = true } +image = { workspace = true } +serde_json = { workspace = true } +log = { workspace = true } + +[build-dependencies] +winresource = "0.1" diff --git a/apps/texturecraft/build.rs b/apps/texturecraft/build.rs new file mode 100644 index 00000000..c77d91bd --- /dev/null +++ b/apps/texturecraft/build.rs @@ -0,0 +1,80 @@ +// Build provenance for every target, plus the Windows icon and VERSIONINFO. +// +// Shared by `texturecraft` and `texturecraft-cli` (whose build.rs `include!`s this file). +// +// - Always sets `TEXTURECRAFT_BUILD_SHA` (full git commit) and `TEXTURECRAFT_BUILD_DATE` +// (`YYYY-MM-DD`, UTC) for `env!`. They come from the same-named environment variables (CI and +// packaging/env.sh set them), else from `git` and the clock (`SOURCE_DATE_EPOCH` wins for +// reproducible builds); either is an empty string when unknown, so source tarballs, wasm and +// offline builds still compile. Without the env vars the commit is read when this script runs, +// so a local build can show a stale commit; release builds always set the variable. +// - Windows targets only: embeds assets/app-icon/texturecraft.ico and VERSIONINFO into the exe. +// A missing resource compiler is a warning, so a cross-compile still links, unless +// `TEXTURECRAFT_REQUIRE_WINRES=1` (set by packaging/windows/package.ps1) makes it an error. + +use std::process::Command; + +const ICON: &str = "../../assets/app-icon/texturecraft.ico"; + +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed={ICON}"); + for var in ["TEXTURECRAFT_BUILD_SHA", "TEXTURECRAFT_BUILD_DATE", "TEXTURECRAFT_REQUIRE_WINRES", "SOURCE_DATE_EPOCH"] { + println!("cargo:rerun-if-env-changed={var}"); + } + println!("cargo:rustc-env=TEXTURECRAFT_BUILD_SHA={}", env_or("TEXTURECRAFT_BUILD_SHA", git_sha)); + println!("cargo:rustc-env=TEXTURECRAFT_BUILD_DATE={}", env_or("TEXTURECRAFT_BUILD_DATE", today)); + + if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") { + windows_resources(); + } +} + +fn env_or(var: &str, fallback: fn() -> Option) -> String { + std::env::var(var).ok().map(|v| v.trim().to_owned()).filter(|v| !v.is_empty()).or_else(fallback).unwrap_or_default() +} + +fn git_sha() -> Option { + let out = Command::new("git").args(["rev-parse", "HEAD"]).output().ok().filter(|o| o.status.success())?; + let sha = String::from_utf8(out.stdout).ok()?.trim().to_owned(); + (sha.len() >= 7 && sha.bytes().all(|b| b.is_ascii_hexdigit())).then_some(sha) +} + +/// UTC date of `SOURCE_DATE_EPOCH`, or of now. +fn today() -> Option { + let secs = match std::env::var("SOURCE_DATE_EPOCH") { + Ok(v) => v.trim().parse::().ok()?, + Err(_) => std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).ok()?.as_secs(), + }; + // Days since 1970-01-01 to a civil date (Howard Hinnant's algorithm, proleptic Gregorian). + let z = i64::try_from(secs / 86_400).ok()? + 719_468; + let era = z.div_euclid(146_097); + let doe = z.rem_euclid(146_097); + let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; + let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); + let mp = (5 * doy + 2) / 153; + let day = doy - (153 * mp + 2) / 5 + 1; + let month = if mp < 10 { mp + 3 } else { mp - 9 }; + let year = yoe + era * 400 + i64::from(month <= 2); + Some(format!("{year:04}-{month:02}-{day:02}")) +} + +fn windows_resources() { + let name = std::env::var("CARGO_PKG_NAME").unwrap_or_else(|_| "texturecraft".into()); + let description = if name.ends_with("-cli") { "TextureCraft command-line tool" } else { "TextureCraft 3D texture painter" }; + let mut res = winresource::WindowsResource::new(); + res.set_icon(ICON) + .set("ProductName", "TextureCraft") + .set("FileDescription", description) + .set("CompanyName", "Learning Machines LLC") + .set("LegalCopyright", "Copyright (c) the TextureCraft contributors. MIT OR Apache-2.0.") + .set("OriginalFilename", &format!("{name}.exe")) + .set("InternalName", &name); + if let Err(e) = res.compile() { + if std::env::var_os("TEXTURECRAFT_REQUIRE_WINRES").is_some() { + println!("cargo::error=embedding Windows resources into {name}.exe failed: {e}"); + } else { + println!("cargo:warning={name}.exe built without icon/version resources: {e}"); + } + } +} diff --git a/apps/texturecraft/src/control_server.rs b/apps/texturecraft/src/control_server.rs new file mode 100644 index 00000000..f7ee3b2a --- /dev/null +++ b/apps/texturecraft/src/control_server.rs @@ -0,0 +1,114 @@ +//! Loopback JSON-lines control server (`--control `): one request per line, +//! `{"id", "method", "params"}`, one reply per line. The first request must be +//! `{"method":"auth","params":{"token":"…"}}` with the token from `TEXTURECRAFT_CONTROL_TOKEN` +//! or, when that is unset, the token written to `/texturecraft-control-.token`. + +use std::io::{BufRead, BufReader, Read, Write}; +use std::net::{TcpListener, TcpStream}; +use std::sync::mpsc::{Receiver, Sender, channel}; +use std::time::Duration; + +use serde_json::{Value, json}; +use texturecraft_ui_egui::ControlRequest; + +/// Requests larger than this are rejected (screenshots go the other way). +const MAX_REQUEST: u64 = 8 << 20; + +fn token(port: u16) -> String { + if let Ok(t) = std::env::var("TEXTURECRAFT_CONTROL_TOKEN") + && t.len() >= 16 + { + return t; + } + use std::hash::{BuildHasher, Hasher}; + let mut out = String::new(); + for i in 0..4u64 { + let mut h = std::collections::hash_map::RandomState::new().build_hasher(); + h.write_u64(i ^ u64::from(std::process::id())); + h.write_u128(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0)); + out.push_str(&format!("{:016x}", h.finish())); + } + let path = std::env::temp_dir().join(format!("texturecraft-control-{port}.token")); + if let Err(e) = std::fs::write(&path, &out) { + eprintln!("texturecraft: could not write the control token to {}: {e}", path.display()); + } else { + eprintln!("texturecraft: control token written to {}", path.display()); + } + out +} + +pub fn start(port: u16, ctx: egui::Context) -> Receiver { + let (tx, rx) = channel::(); + let listener = match TcpListener::bind(("127.0.0.1", port)) { + Ok(l) => l, + Err(e) => { + eprintln!("texturecraft: control server failed to bind 127.0.0.1:{port}: {e}"); + return rx; + } + }; + let token = token(port); + eprintln!("texturecraft: control server listening on 127.0.0.1:{port}"); + std::thread::spawn(move || { + for stream in listener.incoming().flatten() { + let (tx, ctx, token) = (tx.clone(), ctx.clone(), token.clone()); + std::thread::spawn(move || serve(stream, &token, tx, ctx)); + } + }); + rx +} + +fn serve(stream: TcpStream, token: &str, tx: Sender, ctx: egui::Context) { + let _ = stream.set_read_timeout(Some(Duration::from_secs(600))); + let Ok(read) = stream.try_clone() else { return }; + let mut reader = BufReader::new(read.take(u64::MAX)); + let mut out = stream; + let mut authed = false; + loop { + let mut line = String::new(); + let n = match reader.by_ref().take(MAX_REQUEST).read_line(&mut line) { + Ok(n) => n, + Err(_) => break, + }; + if n == 0 { + break; + } + if !line.ends_with('\n') && n as u64 >= MAX_REQUEST { + let _ = writeln!(out, "{}", json!({"id": null, "ok": false, "error": "request too large"})); + break; + } + if line.trim().is_empty() { + continue; + } + let msg: Value = match serde_json::from_str(&line) { + Ok(v) => v, + Err(e) => { + let _ = writeln!(out, "{}", json!({"id": null, "ok": false, "error": format!("bad JSON: {e}")})); + continue; + } + }; + let id = msg.get("id").cloned().unwrap_or(Value::Null); + let method = msg.get("method").and_then(Value::as_str).unwrap_or("").to_string(); + let params = msg.get("params").cloned().unwrap_or(json!({})); + let mut reply = if !authed { + let ok = method == "auth" && params.get("token").and_then(Value::as_str) == Some(token); + authed = ok; + if ok { json!({"ok": true}) } else { json!({"ok": false, "error": "authenticate first: {\"method\":\"auth\",\"params\":{\"token\":…}}"}) } + } else { + let (req, rrx) = ControlRequest::new(method, params); + if tx.send(req).is_err() { + break; + } + ctx.request_repaint(); + rrx.recv_timeout(Duration::from_secs(120)).unwrap_or_else(|_| json!({"ok": false, "error": "timeout"})) + }; + if let Some(o) = reply.as_object_mut() { + o.insert("id".into(), id); + } + if writeln!(out, "{reply}").is_err() || out.flush().is_err() || !authed { + if !authed { + continue; + } + break; + } + } +} diff --git a/apps/texturecraft/src/main.rs b/apps/texturecraft/src/main.rs new file mode 100644 index 00000000..92634e36 --- /dev/null +++ b/apps/texturecraft/src/main.rs @@ -0,0 +1,120 @@ +//! TextureCraft desktop app. +//! +//! Usage: `texturecraft [project.tcraft | mesh.glb] [--control ] [--empty] [--version]` +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +mod control_server; + +use texturecraft_engine::Session; +use texturecraft_ui_egui::TextureCraftApp; + +const ICON_PNG: &[u8] = include_bytes!("../../../assets/app-icon/hicolor/256x256/apps/ai.storyteller.texturecraft.png"); + +fn version() -> String { + let sha = env!("TEXTURECRAFT_BUILD_SHA"); + let short = sha.get(..7).unwrap_or(sha); + let date = env!("TEXTURECRAFT_BUILD_DATE"); + match (short.is_empty(), date.is_empty()) { + (true, true) => format!("TextureCraft {}", texturecraft_engine::VERSION), + (true, false) => format!("TextureCraft {} ({date})", texturecraft_engine::VERSION), + _ => format!("TextureCraft {} ({short} {date})", texturecraft_engine::VERSION), + } +} + +struct Args { + file: Option, + control: Option, + empty: bool, +} + +fn parse_args() -> Result, String> { + let mut args = Args { file: None, control: None, empty: false }; + let mut it = std::env::args().skip(1); + while let Some(a) = it.next() { + match a.as_str() { + "--version" | "-V" => { + println!("{}", version()); + return Ok(None); + } + "--help" | "-h" => { + println!("{}\n\nUsage: texturecraft [project.tcraft | mesh file] [--control ] [--empty]", version()); + return Ok(None); + } + "--control" => { + let p = it.next().ok_or("--control needs a port")?; + args.control = Some(p.parse().map_err(|_| format!("invalid port `{p}`"))?); + } + "--empty" => args.empty = true, + f if !f.starts_with('-') => args.file = Some(f.to_string()), + other => return Err(format!("unknown option `{other}`")), + } + } + Ok(Some(args)) +} + +fn initial_session(args: &Args) -> Session { + if let Some(f) = &args.file { + match texturecraft_engine::file_cmds::open_path(f) { + Ok(s) => return s, + Err(e) => eprintln!("texturecraft: could not open {f}: {e}"), + } + } + if !args.empty + && let Ok(s) = texturecraft_engine::sample_session(2048, 512) + { + return s; + } + match Session::new_project("Untitled", texturecraft_engine::geom::primitives::material_ball(), "builtin:material_ball", None, 2048) { + Ok(s) => s, + Err(e) => { + eprintln!("texturecraft: {e}"); + std::process::exit(1); + } + } +} + +fn icon() -> Option { + let img = image::load_from_memory(ICON_PNG).ok()?.into_rgba8(); + let (w, h) = img.dimensions(); + Some(egui::IconData { rgba: img.into_raw(), width: w, height: h }) +} + +fn main() { + let args = match parse_args() { + Ok(Some(a)) => a, + Ok(None) => return, + Err(e) => { + eprintln!("texturecraft: {e}"); + std::process::exit(2); + } + }; + let prev = std::panic::take_hook(); + std::panic::set_hook(Box::new(move |info| { + eprintln!("texturecraft: internal error: {info}"); + prev(info); + })); + let session = initial_session(&args); + let mut viewport = egui::ViewportBuilder::default().with_title("TextureCraft").with_inner_size([1600.0, 960.0]).with_min_inner_size([960.0, 600.0]); + if let Some(i) = icon() { + viewport = viewport.with_icon(i); + } + let options = eframe::NativeOptions { viewport, renderer: eframe::Renderer::Wgpu, ..Default::default() }; + let control = args.control; + let r = eframe::run_native( + "TextureCraft", + options, + Box::new(move |cc| { + let mut app = TextureCraftApp::new(cc, session); + if let Some(port) = control { + app.control = Some(control_server::start(port, cc.egui_ctx.clone())); + } + Ok(Box::new(app)) + }), + ); + if let Err(e) = r { + eprintln!("texturecraft: {e}"); + std::process::exit(1); + } +} diff --git a/assets/app-icon/hicolor/128x128/apps/ai.storyteller.texturecraft.png b/assets/app-icon/hicolor/128x128/apps/ai.storyteller.texturecraft.png new file mode 100644 index 00000000..03736467 Binary files /dev/null and b/assets/app-icon/hicolor/128x128/apps/ai.storyteller.texturecraft.png differ diff --git a/assets/app-icon/hicolor/16x16/apps/ai.storyteller.texturecraft.png b/assets/app-icon/hicolor/16x16/apps/ai.storyteller.texturecraft.png new file mode 100644 index 00000000..e1fef320 Binary files /dev/null and b/assets/app-icon/hicolor/16x16/apps/ai.storyteller.texturecraft.png differ diff --git a/assets/app-icon/hicolor/24x24/apps/ai.storyteller.texturecraft.png b/assets/app-icon/hicolor/24x24/apps/ai.storyteller.texturecraft.png new file mode 100644 index 00000000..cb6b3d3d Binary files /dev/null and b/assets/app-icon/hicolor/24x24/apps/ai.storyteller.texturecraft.png differ diff --git a/assets/app-icon/hicolor/256x256/apps/ai.storyteller.texturecraft.png b/assets/app-icon/hicolor/256x256/apps/ai.storyteller.texturecraft.png new file mode 100644 index 00000000..c15bfa91 Binary files /dev/null and b/assets/app-icon/hicolor/256x256/apps/ai.storyteller.texturecraft.png differ diff --git a/assets/app-icon/hicolor/32x32/apps/ai.storyteller.texturecraft.png b/assets/app-icon/hicolor/32x32/apps/ai.storyteller.texturecraft.png new file mode 100644 index 00000000..a633d495 Binary files /dev/null and b/assets/app-icon/hicolor/32x32/apps/ai.storyteller.texturecraft.png differ diff --git a/assets/app-icon/hicolor/48x48/apps/ai.storyteller.texturecraft.png b/assets/app-icon/hicolor/48x48/apps/ai.storyteller.texturecraft.png new file mode 100644 index 00000000..f6dec508 Binary files /dev/null and b/assets/app-icon/hicolor/48x48/apps/ai.storyteller.texturecraft.png differ diff --git a/assets/app-icon/hicolor/512x512/apps/ai.storyteller.texturecraft.png b/assets/app-icon/hicolor/512x512/apps/ai.storyteller.texturecraft.png new file mode 100644 index 00000000..9ab1231a Binary files /dev/null and b/assets/app-icon/hicolor/512x512/apps/ai.storyteller.texturecraft.png differ diff --git a/assets/app-icon/hicolor/64x64/apps/ai.storyteller.texturecraft.png b/assets/app-icon/hicolor/64x64/apps/ai.storyteller.texturecraft.png new file mode 100644 index 00000000..21b04424 Binary files /dev/null and b/assets/app-icon/hicolor/64x64/apps/ai.storyteller.texturecraft.png differ diff --git a/assets/app-icon/hicolor/scalable/apps/ai.storyteller.texturecraft.svg b/assets/app-icon/hicolor/scalable/apps/ai.storyteller.texturecraft.svg new file mode 100644 index 00000000..1bb8fbd2 --- /dev/null +++ b/assets/app-icon/hicolor/scalable/apps/ai.storyteller.texturecraft.svg @@ -0,0 +1,169 @@ + + TextureCraft + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/assets/app-icon/icon-1024.png b/assets/app-icon/icon-1024.png new file mode 100644 index 00000000..58eceab1 Binary files /dev/null and b/assets/app-icon/icon-1024.png differ diff --git a/assets/app-icon/icon.py b/assets/app-icon/icon.py new file mode 100644 index 00000000..a85a64a3 --- /dev/null +++ b/assets/app-icon/icon.py @@ -0,0 +1,164 @@ +"""TextureCraft app icon: an octopus drawn in code, written to icon.svg beside this file. + +Original artwork (no traced or third-party source), MIT OR Apache-2.0 like the rest of the repo. +Follows craftrules standards/icon-design.md: 512 tile with rx=112, three colours only (ink, paper, +TextureCraft copper), a tight head-and-shoulders portrait with big eyes, carved lines that swell +and taper. Edit the shapes here, run `python3 assets/app-icon/icon.py`, then `packaging/icons.sh` +to render every PNG, the .ico and the .icns. Plain Python 3, no dependencies. +""" +import math +from pathlib import Path + +INK, PAPER, APP = "#0b0b0c", "#efe9dc", "#c2672b" + + +def catmull(pts, n=24, closed=False): + out = [] + m = len(pts) + segs = m if closed else m - 1 + for i in range(segs): + g = (lambda k: pts[k % m]) if closed else (lambda k: pts[min(max(k, 0), m - 1)]) + p0, p1, p2, p3 = g(i - 1), g(i), g(i + 1), g(i + 2) + for s in range(n): + t = s / n + t2, t3 = t * t, t * t * t + out.append(tuple(0.5 * ((2 * p1[j]) + (-p0[j] + p2[j]) * t + (2 * p0[j] - 5 * p1[j] + 4 * p2[j] - p3[j]) * t2 + + (-p0[j] + 3 * p1[j] - 3 * p2[j] + p3[j]) * t3) for j in range(2))) + if not closed: + out.append(pts[-1]) + return out + + +def arclen(pts): + acc = [0.0] + for a, b in zip(pts, pts[1:]): + acc.append(acc[-1] + math.dist(a, b)) + total = acc[-1] or 1.0 + return [a / total for a in acc] + + +def normals(pts): + out = [] + for i in range(len(pts)): + a = pts[max(i - 1, 0)] + b = pts[min(i + 1, len(pts) - 1)] + dx, dy = b[0] - a[0], b[1] - a[1] + d = math.hypot(dx, dy) or 1.0 + out.append((-dy / d, dx / d)) + return out + + +def path(pts, close=True): + s = "M" + " L".join(f"{x:.1f} {y:.1f}" for x, y in pts) + return s + (" Z" if close else "") + + +def ribbon(center, width, offset=lambda u: 0.0, u0=0.0, u1=1.0): + """Closed outline of a stroke along `center` whose width (and sideways offset) vary with u.""" + us = arclen(center) + ns = normals(center) + left, right = [], [] + for p, n, u in zip(center, ns, us): + if u < u0 or u > u1: + continue + v = (u - u0) / ((u1 - u0) or 1) + w, o = width(v) / 2, offset(u) + left.append((p[0] + n[0] * (o + w), p[1] + n[1] * (o + w))) + right.append((p[0] + n[0] * (o - w), p[1] + n[1] * (o - w))) + return left + right[::-1] + + +def swell(peak, lo=0.0): + """Carved line: thin at both ends, `peak` wide in the middle.""" + return lambda v: lo + (peak - lo) * math.sin(math.pi * min(max(v, 0), 1)) ** 0.8 + + +def at(center, u): + us = arclen(center) + ns = normals(center) + for i in range(1, len(us)): + if us[i] >= u: + return center[i], ns[i] + return center[-1], ns[-1] + + +out = [] +el = out.append + + +def arm(points, base, tip, side, suckers=(0.45, 0.97), shade=(0.05, 0.75)): + """An arm: paper body with ink contour, a carved shading line, suckers along `side`.""" + c = catmull(points) + w = lambda v: tip + (base - tip) * (1 - v) ** 1.15 + el(f'') + # Shading line on the side away from the suckers. + el(f'') + # Suckers: paper discs with an ink ring and a dark centre, shrinking toward the tip. + u = suckers[0] + while u < suckers[1]: + p, n = at(c, u) + r = max(w(u) * 0.2, 3.2) + cx, cy = p[0] + n[0] * side * w(u) * 0.36, p[1] + n[1] * side * w(u) * 0.36 + el(f'') + if r > 5: + el(f'') + u += max(r * 2.9, 8) / 420 + + +# Arms, back to front. +arm([(206, 262), (140, 292), (78, 300), (38, 278), (32, 242), (58, 226), (80, 242), (70, 262)], 96, 7, 1, suckers=(0.42, 0.97)) # left curl +arm([(306, 262), (372, 292), (434, 300), (474, 278), (480, 242), (454, 226), (432, 242), (442, 262)], 96, 7, -1, suckers=(0.42, 0.97)) # right curl +arm([(222, 262), (196, 300), (150, 352), (104, 424), (76, 540)], 98, 34, -1, suckers=(0.3, 1.0)) # left, off the bottom +arm([(290, 262), (316, 300), (360, 352), (420, 402), (462, 452), (452, 494), (416, 492), (410, 464), (428, 452)], 98, 7, 1, suckers=(0.4, 0.97)) # right, curl +arm([(240, 250), (236, 302), (222, 382), (196, 446), (184, 540)], 104, 40, 1, suckers=(0.34, 1.0)) # front left +arm([(274, 250), (280, 302), (302, 372), (302, 432), (278, 476), (244, 472), (236, 446), (254, 436)], 104, 7, 1, suckers=(0.4, 0.97)) # front right curl + +# Head: mantle + eye region, one silhouette. +head = catmull([(262, 62), (338, 74), (390, 116), (406, 176), (378, 226), (364, 254), (332, 286), (256, 302), (180, 286), + (148, 254), (134, 226), (108, 176), (128, 112), (186, 72)], closed=True) +el(f'') +el(f'') +# Engraved shading: contour-following lines that thicken toward the shadow side (right and below). +cx0, cy0 = 236, 156 +for k, f in enumerate([0.95, 0.88, 0.81, 0.74, 0.67]): + ring = [(cx0 + (x - cx0) * f, cy0 + (y - cy0) * f) for x, y in head] + n = len(ring) + # Right flank of the mantle, in shadow. + arc = ring[int(n * (0.03 + k * 0.012)):int(n * (0.30 - k * 0.012))] + el(f'') +for k, f in enumerate([0.95, 0.9]): + ring = [(cx0 + (x - cx0) * f, cy0 + (y - cy0) * f) for x, y in head] + n = len(ring) + # Under the head, where the arms join. + arc = ring[int(n * (0.40 + k * 0.02)):int(n * (0.60 - k * 0.02))] + el(f'') +# A few carved strokes over the dome (texture), left side lighter. +for pts, wd in [([(196, 104), (176, 124), (166, 150)], 6), ([(222, 96), (212, 108)], 4.5), ([(288, 98), (312, 112), (330, 134)], 6)]: + el(f'') + +# Eyes. +for ex, ey, look in [(172, 214, 4), (340, 214, -4)]: + el(f'') + el(f'') + el(f'') + el(f'') + el(f'') + s = 1 if ex < 256 else -1 + # Upper lid: a heavy carved arc. + lid = catmull([(ex - 50 * s, ey - 18), (ex - 20 * s, ey - 50), (ex + 22 * s, ey - 52), (ex + 48 * s, ey - 28)]) + el(f'') + # Lower lid crease. + low = catmull([(ex - 34 * s, ey + 44), (ex, ey + 58), (ex + 36 * s, ey + 46)]) + el(f'') + +body = "\n ".join(out) +svg = f''' + TextureCraft + + + + {body} + + +''' +Path(__file__).with_name("icon.svg").write_text(svg, encoding="utf-8", newline="\n") diff --git a/assets/app-icon/icon.svg b/assets/app-icon/icon.svg new file mode 100644 index 00000000..1bb8fbd2 --- /dev/null +++ b/assets/app-icon/icon.svg @@ -0,0 +1,169 @@ + + TextureCraft + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/assets/app-icon/texturecraft.icns b/assets/app-icon/texturecraft.icns new file mode 100644 index 00000000..b7c64eca Binary files /dev/null and b/assets/app-icon/texturecraft.icns differ diff --git a/assets/app-icon/texturecraft.ico b/assets/app-icon/texturecraft.ico new file mode 100644 index 00000000..7592b795 Binary files /dev/null and b/assets/app-icon/texturecraft.ico differ diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 00000000..4b6c79f5 --- /dev/null +++ b/clippy.toml @@ -0,0 +1,4 @@ +allow-unwrap-in-tests = true +allow-expect-in-tests = true +allow-panic-in-tests = true +allow-indexing-slicing-in-tests = true diff --git a/crates/automation/Cargo.toml b/crates/automation/Cargo.toml new file mode 100644 index 00000000..5b1d59d5 --- /dev/null +++ b/crates/automation/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "texturecraft-automation" +description = "TextureCraft automation: MCP server (rmcp), headless JSON-lines RPC and a bridge to the running app" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lints] +workspace = true + +[dependencies] +texturecraft-engine = { workspace = true } +texturecraft-render = { workspace = true } +texturecraft-formats = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } +image = { workspace = true } +rayon = { workspace = true } +rmcp = { workspace = true } +tokio = { workspace = true } +schemars = { workspace = true } +base64 = { workspace = true } +cap-std = { workspace = true } +getrandom = { workspace = true } + +[dev-dependencies] +rmcp = { workspace = true, features = ["client"] } diff --git a/crates/automation/README.md b/crates/automation/README.md new file mode 100644 index 00000000..c77bdb6f --- /dev/null +++ b/crates/automation/README.md @@ -0,0 +1,45 @@ +# texturecraft-automation + +Agent-facing automation for TextureCraft (layer L4: engine, render and formats; no UI toolkit). +The protocol and tool reference is [`docs/control-protocol.md`](../../docs/control-protocol.md). + +## Public API + +```rust +// An in-process session with file access and windowless renders. +pub struct Headless { pub session: Session, .. } +impl Headless { + pub fn new(session: Session, fs: Filesystem) -> Headless; + pub fn default_session() -> Result; // material ball, 1024 + pub fn command_run(&mut self, id: &str, params: Value) -> Result; + pub fn batch(&mut self, steps: &[Value]) -> Value; // stops at the first error + pub fn open(&mut self, path: &str) -> Result; // .tcraft or mesh + pub fn import_mesh(&mut self, path: &str, resolution: Option, name: Option<&str>) -> Result; + pub fn save(&mut self, path: Option<&str>) -> Result; + pub fn export(&mut self, params: &Value) -> Result; + pub fn render_png(&mut self, o: &RenderOptions) -> Result<(u32, u32, Vec), AutomationError>; + pub fn handle(&mut self, method: &str, params: &Value) -> Result; // JSON-lines methods +} +pub enum Filesystem { Denied, TrustedLocal, Workspace(AuthorizedWorkspace) } + +// Read and write roots as cap-std directory capabilities: `..`, absolute paths, drive +// prefixes, device names and symlink/junction escapes fail. +pub struct AuthorizedWorkspace; +pub fn workspace::authorize_command(id: &str, params: &Value) -> Result<(), AutomationError>; + +pub struct TexturecraftMcp; // rmcp server: ::headless(Headless) | ::bridge(BridgeClient) +pub struct BridgeClient; // client of `texturecraft --control ` +pub mod rpc; // serve_lines (stdio), serve_tcp (loopback + token) +pub mod security; // tokens, bounded line reads, connection limit +pub mod budgets; // reply ceilings: 8 MiB JSON, 5 MiB / 2048 px PNG +``` + +## Rules + +- Untrusted callers (MCP, `serve`) reach files only through `Filesystem::Workspace`; path-taking + `file.*` commands are refused by `authorize_command`. The CLI's own subcommands use + `Filesystem::TrustedLocal` (the user typed the paths). +- Renders create the GPU device on first use and return an error when there is no adapter; a + wgpu panic is caught and reported, and the device is recreated on the next render. +- Tests: `tests/agent_tasks.rs` drives the MCP server in process (tokio duplex) through ten + agent tasks, the workspace boundary and junk params for every tool. diff --git a/crates/automation/src/bridge.rs b/crates/automation/src/bridge.rs new file mode 100644 index 00000000..fbd8e809 --- /dev/null +++ b/crates/automation/src/bridge.rs @@ -0,0 +1,107 @@ +//! Client for the running app's control channel (`texturecraft --control `): one JSON +//! request per line, replies matched by `id`. Keeps one connection, reconnects once on failure. + +use std::sync::atomic::{AtomicU64, Ordering}; +use std::time::Duration; + +use serde_json::{Value, json}; +use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader}; +use tokio::net::TcpStream; +use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; +use tokio::sync::Mutex; + +use crate::AutomationError; +use crate::budgets::MAX_BRIDGE_REPLY_BYTES; + +type Conn = (BufReader, OwnedWriteHalf); + +pub struct BridgeClient { + port: u16, + token: String, + conn: Mutex>, + next_id: AtomicU64, +} + +impl BridgeClient { + /// A client for the app on `127.0.0.1:port` (loopback only, like the server). + pub fn new(port: u16, token: String) -> BridgeClient { + BridgeClient { port, token, conn: Mutex::new(None), next_id: AtomicU64::new(1) } + } + + pub fn port(&self) -> u16 { + self.port + } + + async fn connect(&self) -> Result { + let addr = ("127.0.0.1", self.port); + let s = tokio::time::timeout(Duration::from_secs(5), TcpStream::connect(addr)) + .await + .map_err(|_| AutomationError::Bridge(format!("timed out connecting to 127.0.0.1:{}", self.port)))? + .map_err(|e| { + AutomationError::Bridge(format!("cannot connect to 127.0.0.1:{} ({e}); start the app with `texturecraft --control {}`", self.port, self.port)) + })?; + let (r, w) = s.into_split(); + let mut conn = (BufReader::new(r), w); + let id = self.next_id.fetch_add(1, Ordering::Relaxed); + tokio::time::timeout(Duration::from_secs(5), exchange(&mut conn, id, "auth", &json!({"token": self.token}))) + .await + .map_err(|_| AutomationError::Bridge("authentication timed out".into()))?? + .map_err(|e| AutomationError::Bridge(format!("authentication failed: {e}")))?; + Ok(conn) + } + + /// Call a control method; returns its `result` or the app's error. + pub async fn call(&self, method: &str, params: Value) -> Result { + let mut guard = self.conn.lock().await; + for attempt in 0..2 { + let mut conn = match guard.take() { + Some(c) => c, + None => self.connect().await?, + }; + let id = self.next_id.fetch_add(1, Ordering::Relaxed); + match tokio::time::timeout(Duration::from_secs(150), exchange(&mut conn, id, method, ¶ms)).await { + Ok(Ok(reply)) => { + // An oversized reply (`Bridge`) leaves unread bytes: drop that connection. + if !matches!(reply, Err(AutomationError::Bridge(_))) { + *guard = Some(conn); + } + return reply; + } + // A dropped connection (the app restarted) is retried once on a fresh one. + Ok(Err(e)) if attempt == 1 => return Err(e), + Ok(Err(_)) => {} + Err(_) => return Err(AutomationError::Bridge(format!("`{method}` timed out"))), + } + } + Err(AutomationError::Bridge(format!("`{method}` failed"))) + } +} + +/// Outer `Err`: transport failure (retryable). Inner: the app's answer. +async fn exchange(conn: &mut Conn, id: u64, method: &str, params: &Value) -> Result, AutomationError> { + let io = |e: std::io::Error| AutomationError::Bridge(e.to_string()); + let line = format!("{}\n", json!({"id": id, "method": method, "params": params})); + conn.1.write_all(line.as_bytes()).await.map_err(io)?; + conn.1.flush().await.map_err(io)?; + let mut buf = Vec::new(); + loop { + buf.clear(); + let n = (&mut conn.0).take(MAX_BRIDGE_REPLY_BYTES as u64 + 1).read_until(b'\n', &mut buf).await.map_err(io)?; + if n == 0 { + return Err(AutomationError::Bridge("the app closed the connection".into())); + } + if n > MAX_BRIDGE_REPLY_BYTES { + // Not retried: the request may have completed. + return Ok(Err(AutomationError::Bridge(format!("reply over {MAX_BRIDGE_REPLY_BYTES} bytes; the operation may have completed")))); + } + let Ok(v) = serde_json::from_slice::(&buf) else { continue }; + if v.get("id").and_then(Value::as_u64) != Some(id) { + continue; // a stale reply to an earlier, timed-out request + } + return Ok(if v.get("ok").and_then(Value::as_bool) == Some(true) { + Ok(v.get("result").cloned().unwrap_or(Value::Null)) + } else { + Err(AutomationError::App(v.get("error").and_then(Value::as_str).unwrap_or("unknown error").to_string())) + }); + } +} diff --git a/crates/automation/src/budgets.rs b/crates/automation/src/budgets.rs new file mode 100644 index 00000000..8c0cb0c4 --- /dev/null +++ b/crates/automation/src/budgets.rs @@ -0,0 +1,75 @@ +//! Ceilings for automation replies: rendered and screenshot PNGs and JSON payloads. + +use serde::Serialize; + +use crate::{AutomationError, bad}; + +/// Largest JSON reply (MCP tool text, one JSON line). +pub const MAX_RESPONSE_BYTES: usize = 8 << 20; +/// Largest PNG returned to a client (before base64). +pub const MAX_PNG_BYTES: usize = 5 << 20; +/// Largest render or screenshot edge returned to a client; larger screenshots are downscaled. +pub const MAX_PNG_SIDE: u32 = 2048; +/// Largest render edge for local CLI renders. +pub const MAX_LOCAL_RENDER_SIDE: u32 = 4096; +/// Largest reply line read from the app (full-window screenshots are big). +pub const MAX_BRIDGE_REPLY_BYTES: usize = 64 << 20; + +/// Serialize compactly, refusing replies over [`MAX_RESPONSE_BYTES`]. +pub fn json_text(v: &impl Serialize) -> Result { + let s = serde_json::to_string(v).map_err(|e| AutomationError::Other(format!("cannot encode the reply: {e}")))?; + if s.len() > MAX_RESPONSE_BYTES { + return Err(bad(format!("reply is {} bytes, over the {MAX_RESPONSE_BYTES}-byte limit; the operation may have completed", s.len()))); + } + Ok(s) +} + +pub fn check_png(bytes: usize) -> Result<(), AutomationError> { + if bytes > MAX_PNG_BYTES { + return Err(bad(format!("PNG is {bytes} bytes, over the {MAX_PNG_BYTES}-byte limit; render smaller"))); + } + Ok(()) +} + +pub fn encode_png(w: u32, h: u32, rgba: Vec) -> Result, AutomationError> { + let img = image::RgbaImage::from_raw(w, h, rgba).ok_or_else(|| AutomationError::Other("render buffer has the wrong size".into()))?; + let mut png = Vec::new(); + img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).map_err(|e| AutomationError::Other(format!("PNG encoding failed: {e}")))?; + Ok(png) +} + +/// Downscale a PNG (an app screenshot) so its longest edge is at most `max_side`, then check its +/// byte size. Decoding is capped: a small compressed reply can still claim a huge image. +pub fn fit_png(png: Vec, max_side: u32) -> Result, AutomationError> { + let mut reader = image::ImageReader::with_format(std::io::Cursor::new(&png), image::ImageFormat::Png); + let mut limits = image::Limits::default(); + limits.max_image_width = Some(16_384); + limits.max_image_height = Some(16_384); + limits.max_alloc = Some(1 << 30); + reader.limits(limits); + let img = reader.decode().map_err(|e| AutomationError::Other(format!("screenshot is not a valid PNG: {e}")))?; + let out = if img.width().max(img.height()) > max_side { + let small = img.thumbnail(max_side, max_side).into_rgba8(); + encode_png(small.width(), small.height(), small.into_raw())? + } else { + png + }; + check_png(out.len())?; + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn fit_png_downscales_and_rejects_garbage() { + let big = encode_png(3000, 10, vec![200; 3000 * 10 * 4]).unwrap(); + let small = fit_png(big, MAX_PNG_SIDE).unwrap(); + let img = image::load_from_memory(&small).unwrap(); + assert_eq!(img.width(), MAX_PNG_SIDE); + assert!(fit_png(b"not a png".to_vec(), 64).is_err()); + assert!(json_text(&"x".repeat(MAX_RESPONSE_BYTES)).is_err()); + assert!(check_png(MAX_PNG_BYTES + 1).is_err()); + } +} diff --git a/crates/automation/src/headless.rs b/crates/automation/src/headless.rs new file mode 100644 index 00000000..ef73e813 --- /dev/null +++ b/crates/automation/src/headless.rs @@ -0,0 +1,306 @@ +//! The headless backend: a [`Session`] plus file I/O and windowless renders. Synchronous and +//! UI-free; the MCP server, the RPC server and the CLI all drive it. + +use std::path::Path; + +use serde_json::{Value, json}; +use texturecraft_engine::core::PROJECT_EXT; +use texturecraft_engine::geom::primitives; +use texturecraft_engine::{Session, commands, file_cmds, inspect}; +use texturecraft_render::ViewKind; + +use crate::budgets::{MAX_LOCAL_RENDER_SIDE, MAX_PNG_SIDE, check_png, encode_png}; +use crate::render::Gpu; +use crate::workspace::{MAX_READ_BYTES, authorize_command}; +use crate::{AuthorizedWorkspace, AutomationError, bad}; + +/// Where file paths given to [`Headless`] may point. +pub enum Filesystem { + /// No file access (an MCP server started without roots). + Denied, + /// Host paths as given: the local CLI user typed them. + TrustedLocal, + /// Relative paths under the authorized roots (remote or agent callers). + Workspace(AuthorizedWorkspace), +} + +/// What [`Headless::render_png`] draws. +#[derive(Clone, Debug, Default)] +pub struct RenderOptions { + /// `3d` (default) or `2d`/`uv` (the UV view of the active texture set). + pub view: Option, + /// Pixels; default the session viewport (the space `tool.stroke` points live in). + pub width: Option, + pub height: Option, + /// Camera overrides in degrees (the session camera is not changed). + pub yaw: Option, + pub pitch: Option, + /// Path trace with this many samples per pixel (Rendering mode) instead of the GPU view. + pub samples: Option, + /// Light bounces for path tracing (default 6). + pub bounces: Option, +} + +/// Most samples per pixel a path-traced render may take. +pub const MAX_TRACE_SAMPLES: u32 = 4096; + +pub struct Headless { + pub session: Session, + fs: Filesystem, + /// Created on the first render; `Err` once no adapter was found. + gpu: Option>, +} + +fn ext_of(path: &str) -> String { + Path::new(path).extension().and_then(|e| e.to_str()).unwrap_or_default().to_ascii_lowercase() +} + +fn join(dir: &str, name: &str) -> String { + if dir == "." { name.to_string() } else { format!("{}/{name}", dir.trim_end_matches('/')) } +} + +impl Headless { + pub fn new(session: Session, fs: Filesystem) -> Headless { + Headless { session, fs, gpu: None } + } + + /// The session a fresh server starts with: the material ball at 1024. + pub fn default_session() -> Result { + Ok(Session::new_project("Untitled", primitives::material_ball(), "builtin:material_ball", None, 1024)?) + } + + fn trusted(&self) -> bool { + matches!(self.fs, Filesystem::TrustedLocal) + } + + fn read(&self, path: &str) -> Result, AutomationError> { + match &self.fs { + Filesystem::Denied => Err(bad("file access is not granted: start the server with --read-root

")), + Filesystem::TrustedLocal => { + let len = std::fs::metadata(path).map_err(|e| AutomationError::Io(format!("cannot read {path}: {e}")))?.len(); + if len > MAX_READ_BYTES { + return Err(bad(format!("{path} is {len} bytes (limit {MAX_READ_BYTES})"))); + } + std::fs::read(path).map_err(|e| AutomationError::Io(format!("cannot read {path}: {e}"))) + } + Filesystem::Workspace(w) => w.read(path), + } + } + + fn write(&self, path: &str, bytes: &[u8]) -> Result<(), AutomationError> { + match &self.fs { + Filesystem::Denied => Err(bad("file access is not granted: start the server with --write-root ")), + Filesystem::TrustedLocal => { + // Atomic: a crash or full disk never leaves a half-written project. + let tmp = format!("{path}.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); + } + res.map_err(|e| AutomationError::Io(format!("cannot write {path}: {e}"))) + } + Filesystem::Workspace(w) => w.write(path, bytes), + } + } + + fn create_dir_all(&self, dir: &str) -> Result<(), AutomationError> { + match &self.fs { + _ if dir == "." => Ok(()), + Filesystem::Denied => Err(bad("file access is not granted: start the server with --write-root ")), + Filesystem::TrustedLocal => std::fs::create_dir_all(dir).map_err(|e| AutomationError::Io(format!("cannot create {dir}: {e}"))), + Filesystem::Workspace(w) => w.create_dir_all(dir), + } + } + + /// Replace the session, keeping the viewport size scripted strokes use. + fn replace(&mut self, mut next: Session) -> Value { + next.viewport = self.session.viewport; + self.session = next; + self.inspect() + } + + pub fn inspect(&self) -> Value { + inspect::session(&self.session) + } + + pub fn command_list(&self, filter: Option<&str>) -> Value { + commands::list(&self.session, filter) + } + + /// Run an engine command. Untrusted callers can't reach host paths through it. + pub fn command_run(&mut self, id: &str, params: Value) -> Result { + if !self.trusted() { + authorize_command(id, ¶ms)?; + } + Ok(texturecraft_engine::execute(&mut self.session, id, ¶ms)?) + } + + /// Run `[{id, params?}]` in order, stopping at the first error (reported with its index). + pub fn batch(&mut self, steps: &[Value]) -> Value { + let mut results = Vec::new(); + for (i, step) in steps.iter().enumerate() { + let id = step.get("id").and_then(Value::as_str).unwrap_or_default(); + match self.command_run(id, step.get("params").cloned().unwrap_or(Value::Null)) { + Ok(v) => results.push(v), + Err(e) => return batch_reply(results, Some((i, id, e.to_string()))), + } + } + batch_reply(results, None) + } + + /// Open a `.tcraft` project or a mesh file (a new project at 2048). + pub fn open(&mut self, path: &str) -> Result { + if ext_of(path) != PROJECT_EXT { + return self.import_mesh(path, None, None); + } + let mut next = Session::from_project_bytes(&self.read(path)?)?; + next.path = Some(path.to_string()); + next.modified = false; + Ok(self.replace(next)) + } + + /// A new project from a mesh file: one texture set per material. + pub fn import_mesh(&mut self, path: &str, resolution: Option, name: Option<&str>) -> Result { + let bytes = self.read(path)?; + let stem = Path::new(path).file_stem().and_then(|s| s.to_str()).unwrap_or("Untitled"); + let next = Session::from_mesh_bytes(path, &bytes, resolution.unwrap_or(2048), name.unwrap_or(stem))?; + Ok(self.replace(next)) + } + + /// Import an image file as a project resource (for `layer.add.image`, `tool.stencil`). + pub fn import_resource(&mut self, path: &str) -> Result { + let bytes = self.read(path)?; + Ok(json!({ "name": self.session.import_resource_bytes(path, bytes)? })) + } + + /// Save a layer (default: the selected one) as a smart material file (`.tcmat`). + pub fn material_save(&mut self, p: &Value) -> Result { + let path = p.get("path").and_then(Value::as_str).ok_or_else(|| bad("`path` is required"))?; + let s = &self.session; + let id = match p.get("layer").and_then(Value::as_u64) { + Some(i) => texturecraft_engine::core::LayerId(i), + None => s.selected_layer().ok_or_else(|| bad("no layer selected and no `layer` given"))?, + }; + let name = match p.get("name").and_then(Value::as_str) { + Some(n) => n.to_string(), + None => s.doc.project.layer(id).map(|l| l.name.clone()).map_err(|_| bad(format!("no layer {}", id.0)))?, + }; + let bytes = s.smart_material_bytes(id, &name)?; + let ext = texturecraft_engine::material_lib::SMART_MATERIAL_EXT; + let path = if ext_of(path) == ext { path.to_string() } else { format!("{path}.{ext}") }; + self.write(&path, &bytes)?; + Ok(json!({ "path": path })) + } + + /// Add a smart material from a `.tcmat` file above the selection. + pub fn material_load(&mut self, path: &str) -> Result { + let bytes = self.read(path)?; + Ok(json!({ "layer": self.session.add_smart_material_bytes(&bytes)?.0 })) + } + + /// Save as `.tcraft` to `path`, or to the project's own file. + pub fn save(&mut self, path: Option<&str>) -> Result { + let target = match path.or(self.session.path.as_deref()) { + Some(p) if ext_of(p) == PROJECT_EXT => p.to_string(), + Some(p) => format!("{p}.{PROJECT_EXT}"), + None => return Err(bad("the project has no file yet: give `path`")), + }; + self.write(&target, &self.session.to_project_bytes()?)?; + self.session.path = Some(target.clone()); + self.session.modified = false; + Ok(json!({ "path": target })) + } + + /// Export textures to `dir` (`file.exportTextures` params: preset, ts, format, padding, size…). + pub fn export(&mut self, p: &Value) -> Result { + let dir = p.get("dir").and_then(Value::as_str).filter(|d| !d.is_empty()).ok_or_else(|| bad("`dir` is required"))?; + let opts = file_cmds::export_options(p, "textures.export")?; + let n = self.session.set_count(); + let sets: Vec = match p.get("ts") { + None | Some(Value::Null) => (0..n).collect(), + Some(Value::String(a)) if a == "all" => (0..n).collect(), + Some(v) => vec![v.as_u64().and_then(|t| usize::try_from(t).ok()).filter(|t| *t < n).ok_or_else(|| bad(format!("invalid `ts` {v}")))?], + }; + let files = self.session.export_files(&sets, &opts)?; + self.create_dir_all(dir)?; + let mut written = Vec::new(); + for f in &files { + let path = join(dir, &f.name); + self.write(&path, &f.bytes)?; + written.push(path); + } + let warnings: Vec<&String> = files.iter().flat_map(|f| &f.warnings).collect(); + Ok(json!({ "files": written, "warnings": warnings })) + } + + /// Render the session to PNG: `(width, height, png)`. Errors without a GPU adapter. + pub fn render_png(&mut self, o: &RenderOptions) -> Result<(u32, u32, Vec), AutomationError> { + let s = &self.session; + let kind = match o.view.as_deref().unwrap_or("3d") { + "3d" => ViewKind::Scene, + "2d" | "uv" => ViewKind::Uv { ts: s.ts(), zoom: 1.0, pan: [0.0, 0.0] }, + other => return Err(bad(format!("unknown view `{other}` (3d, 2d or uv)"))), + }; + let max = if self.trusted() { MAX_LOCAL_RENDER_SIDE } else { MAX_PNG_SIDE }; + let side = |v: Option, d: f32| { + let v = v.unwrap_or(d.round() as u32); + if (16..=max).contains(&v) { Ok(v) } else { Err(bad(format!("render size {v} must be 16 to {max} pixels"))) } + }; + let size = [side(o.width, s.viewport.x)?, side(o.height, s.viewport.y)?]; + let mut camera = s.camera; + for (v, slot, lim) in [(o.yaw, &mut camera.yaw, 1e6), (o.pitch, &mut camera.pitch, 89.0)] { + if let Some(v) = v { + if !v.is_finite() { + return Err(bad("camera angles must be finite")); + } + *slot = v.clamp(-lim, lim).to_radians(); + } + } + if let Some(samples) = o.samples { + if !(1..=MAX_TRACE_SAMPLES).contains(&samples) || o.view.as_deref().is_some_and(|v| v != "3d") { + return Err(bad(format!("path tracing renders the 3D view with 1 to {MAX_TRACE_SAMPLES} samples"))); + } + let settings = texturecraft_render::trace::TraceSettings { bounces: o.bounces.unwrap_or(6).clamp(1, 32) }; + let (w, h, rgba) = crate::render::path_trace(&mut self.session, size, camera, samples, &settings); + let png = encode_png(w, h, rgba)?; + if !self.trusted() { + check_png(png.len())?; + } + return Ok((w, h, png)); + } + let gpu = match self.gpu.get_or_insert_with(Gpu::new) { + Ok(g) => g, + Err(e) => return Err(AutomationError::Other(format!("cannot render: {e}"))), + }; + // Last-resort guard: wgpu panics on device errors; drop the device and report instead. + let session = &mut self.session; + let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| gpu.render(session, kind, size, camera))); + let (w, h, rgba) = match out { + Ok(Some(px)) => px, + Ok(None) => return Err(AutomationError::Other("the GPU returned no image".into())), + Err(_) => { + self.gpu = None; + return Err(AutomationError::Other("the GPU renderer failed".into())); + } + }; + let png = encode_png(w, h, rgba)?; + if !self.trusted() { + check_png(png.len())?; + } + Ok((w, h, png)) + } + + /// The GPU adapter used for renders, if one was created. + pub fn adapter(&self) -> Option<&str> { + self.gpu.as_ref().and_then(|g| g.as_ref().ok()).map(Gpu::adapter) + } +} + +/// `{completed, results, failed: {index, id, error} | null}` for `command_batch`. +pub fn batch_reply(results: Vec, failed: Option<(usize, &str, String)>) -> Value { + json!({ + "completed": results.len(), + "results": results, + "failed": failed.map(|(index, id, error)| json!({ "index": index, "id": id, "error": error })), + }) +} diff --git a/crates/automation/src/lib.rs b/crates/automation/src/lib.rs new file mode 100644 index 00000000..e6fdb71d --- /dev/null +++ b/crates/automation/src/lib.rs @@ -0,0 +1,49 @@ +//! # texturecraft-automation +//! +//! Agent-facing automation, L4 (no UI toolkit): +//! +//! * [`TexturecraftMcp`]: an MCP server (official `rmcp` SDK). Headless mode drives an in-process +//! [`Headless`] session; bridge mode forwards to a running desktop app over the JSON-lines +//! control channel (`docs/control-protocol.md`). +//! * [`Headless`]: a [`texturecraft_engine::Session`] plus file I/O through an +//! [`AuthorizedWorkspace`] and windowless GPU renders. Shared by the MCP server, the RPC server +//! and `texturecraft-cli`. +//! * [`rpc`]: the control channel's methods (plus `render`) over stdio or loopback TCP, used by +//! `texturecraft-cli serve`. +//! * [`BridgeClient`]: the client side of the app's control channel. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod bridge; +pub mod budgets; +pub mod headless; +pub mod render; +pub mod rpc; +pub mod security; +pub mod server; +pub mod workspace; + +pub use bridge::BridgeClient; +pub use headless::{Filesystem, Headless, RenderOptions}; +pub use server::{Backend, TexturecraftMcp}; +pub use workspace::AuthorizedWorkspace; + +#[derive(Debug, thiserror::Error)] +pub enum AutomationError { + #[error("{0}")] + BadRequest(String), + #[error("I/O: {0}")] + Io(String), + #[error(transparent)] + Engine(#[from] texturecraft_engine::EngineError), + #[error("bridge: {0}")] + Bridge(String), + #[error("app: {0}")] + App(String), + #[error("{0}")] + Other(String), +} + +pub(crate) fn bad(msg: impl Into) -> AutomationError { + AutomationError::BadRequest(msg.into()) +} diff --git a/crates/automation/src/render.rs b/crates/automation/src/render.rs new file mode 100644 index 00000000..7e9d2c32 --- /dev/null +++ b/crates/automation/src/render.rs @@ -0,0 +1,168 @@ +//! Windowless renders of a session: the desktop app's per-frame GPU sync (`ui-egui/src/gpu.rs`) +//! and viewport shading (`ui-egui/src/viewport.rs`), done in one shot. + +use rayon::prelude::*; +use texturecraft_engine::compose::{DisplayKind, all_tiles, tile_bytes}; +use texturecraft_engine::{Session, ViewMode}; +use texturecraft_render::trace::{TraceDisplay, TraceScene, TraceSet, TraceSettings, Tracer}; +use texturecraft_render::{Frame, Shade, ViewKind}; + +/// A headless GPU device that remembers what it already holds of the session. +pub struct Gpu { + h: texturecraft_render::headless::Headless, + mesh_id: u64, + sets: Vec<(u32, usize)>, + env: String, +} + +impl Gpu { + /// Errors when there is no GPU (or software) adapter. + pub fn new() -> Result { + Ok(Gpu { h: texturecraft_render::headless::Headless::new()?, mesh_id: 0, sets: Vec::new(), env: String::new() }) + } + + pub fn adapter(&self) -> &str { + &self.h.adapter_name + } + + /// Bring the GPU up to date with `s` (mesh, texture sets, environment, dirty tiles). + fn sync(&mut self, s: &mut Session) { + s.update(); + let h = &mut self.h; + let mut reupload = false; + if self.mesh_id != s.mesh_id { + h.renderer.set_mesh(&h.device, &h.queue, &s.mesh, s.mesh_id); + self.mesh_id = s.mesh_id; + self.sets.clear(); + reupload = true; + } + let sets: Vec<(u32, usize)> = s.doc.project.texture_sets.iter().map(|t| (t.resolution, t.material)).collect(); + if sets != self.sets { + h.renderer.set_texture_sets(&h.device, &h.queue, &sets, s.mesh.materials.len().max(1)); + self.sets = sets; + reupload = true; + } + let d = &s.doc.project.display; + if d.environment != self.env { + let image = d.environment_resource().and_then(|n| s.images.get(n)); + h.renderer.set_environment(&h.device, &h.queue, &d.environment, image.map(|i| i.as_ref())); + self.env.clone_from(&d.environment); + } + for ts in 0..s.caches.len() { + let mut tiles = s.take_uploads(ts); + let Some(cache) = s.caches.get(ts) else { continue }; + if reupload { + tiles = all_tiles(cache.resolution); + } + let empty = Default::default(); + let baked = s.doc.baked.get(&ts).unwrap_or(&empty); + let comp = &cache.composite; + let bytes: Vec<_> = tiles + .par_iter() + .flat_map_iter(|&(tx, ty)| DisplayKind::ALL.into_iter().map(move |k| (k, tx, ty))) + .map(|(k, tx, ty)| (k, tx, ty, tile_bytes(comp, baked, k, tx, ty))) + .collect(); + for (k, tx, ty, b) in bytes { + h.renderer.upload_tile(&h.queue, ts, k, tx, ty, &b); + } + // The aux texture behind the mesh-map and mask views (rebuilt every render). + let res = cache.resolution.min(2048); + let aux = match s.view { + ViewMode::MeshMap(m) => Some(texturecraft_render::mesh_maps::mesh_map_rgba(s.doc.baked.get(&ts), m, res)), + ViewMode::Mask(id) if s.doc.project.locate(id).is_some_and(|(t, _, _)| t == ts) => s.mask_view_rgba(id, res), + _ => None, + }; + if let Some(rgba) = aux { + h.renderer.upload_aux(&h.queue, ts, res, &rgba); + } + } + } + + /// Render `s` with `kind` at `size` through `camera`; RGBA8 (sRGB) pixels. + pub fn render(&mut self, s: &mut Session, kind: ViewKind, size: [u32; 2], camera: texturecraft_engine::geom::OrbitCamera) -> Option<(u32, u32, Vec)> { + self.sync(s); + let frame = Frame { view_id: 1, size, kind, camera, shade: shade(s), visible: s.doc.project.texture_sets.iter().map(|t| t.visible).collect() }; + self.h.render(&frame) + } +} + +/// A progressive CPU path trace of the session's 3D view (the app's Rendering mode), finished +/// after `samples` passes and denoised; RGBA8 (sRGB) pixels. +pub fn path_trace(s: &mut Session, size: [u32; 2], camera: texturecraft_engine::geom::OrbitCamera, samples: u32, settings: &TraceSettings) -> (u32, u32, Vec) { + s.update(); + let sets = s + .doc + .project + .texture_sets + .iter() + .zip(&s.caches) + .map(|(ts, c)| TraceSet { material: ts.material, composite: c.composite.clone(), height_strength: ts.height_strength }) + .collect(); + let d = &s.doc.project.display; + let env_image = d.environment_resource().and_then(|n| s.images.get(n)); + let scene = TraceScene::for_display(s.mesh.clone(), s.bvh.clone(), sets, d, env_image.map(|i| i.as_ref())); + let mut tracer = Tracer::new(size[0], size[1]); + for _ in 0..samples { + tracer.pass(&scene, &camera, settings); + } + (tracer.width, tracer.height, tracer.rgba8(&TraceDisplay { denoise: true, ..TraceDisplay::from_display(d) })) +} + +/// How the viewport shades the session (the app's `viewport::shade`). +pub fn shade(s: &Session) -> Shade { + let (view_mode, channel) = match s.view { + ViewMode::Material => (0, 0), + ViewMode::Channel(c) => (1, c.index() as u32), + ViewMode::MeshMap(_) | ViewMode::Mask(_) => (2, 0), + ViewMode::UvChecker => (3, 0), + }; + Shade { + height_strength: s.doc.project.texture_sets.get(s.ts()).map(|t| t.height_strength).unwrap_or(1.0), + view_mode, + channel, + // The app's default (only the 2D view draws it). + wireframe: true, + ..Shade::from_display(&s.doc.project.display) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + use texturecraft_engine::execute; + + /// A 64×32 Radiance `.hdr` (flat RGBE scanlines): a hot orange sky over a dim blue ground. + fn sky_hdr() -> Vec { + let rgbe = |c: [f32; 3]| { + let e = c.iter().copied().fold(0.0f32, f32::max).log2().floor() as i32 + 1; + let k = 256.0 / 2f32.powi(e); + [(c[0] * k) as u8, (c[1] * k) as u8, (c[2] * k) as u8, (e + 128) as u8] + }; + let mut out = b"#?RADIANCE\nFORMAT=32-bit_rle_rgbe\n\n-Y 32 +X 64\n".to_vec(); + for y in 0..32 { + let px = rgbe(if y < 16 { [12.0, 4.0, 1.0] } else { [0.02, 0.05, 0.2] }); + out.extend((0..64).flat_map(|_| px)); + } + out + } + + #[test] + fn renders_with_an_imported_hdr_environment() { + let mesh = texturecraft_engine::geom::primitives::material_ball(); + let mut s = Session::new_project("t", mesh, "builtin:material_ball", None, 256).unwrap(); + let name = s.import_resource_bytes("sky.hdr", sky_hdr()).unwrap(); + assert!(s.images[&name].rgba[0][0] > 10.0, "the decoder keeps HDR values"); + let Ok(mut gpu) = Gpu::new() else { + eprintln!("skipping: no GPU adapter"); + return; + }; + let cam = s.camera; + let (_, _, base) = gpu.render(&mut s, ViewKind::Scene, [160, 120], cam).unwrap(); + execute(&mut s, "display.set", &json!({"environment": format!("resource:{name}"), "specGloss": true, "parallax": true})).unwrap(); + let (_, _, lit) = gpu.render(&mut s, ViewKind::Scene, [160, 120], cam).unwrap(); + assert_ne!(base, lit, "the HDR environment changes the render"); + let warm = |px: &[u8]| px.chunks(4).map(|p| p[0] as f32 - p[2] as f32).sum::(); + assert!(warm(&lit) > warm(&base), "the orange sky warms the image"); + } +} diff --git a/crates/automation/src/rpc.rs b/crates/automation/src/rpc.rs new file mode 100644 index 00000000..d8e50d38 --- /dev/null +++ b/crates/automation/src/rpc.rs @@ -0,0 +1,273 @@ +//! Headless JSON-lines server with the app's control-channel envelope: +//! `{"id","method","params"}` → `{"id","ok":true,"result"}` | `{"id","ok":false,"error"}`. +//! `texturecraft-cli serve` runs it on stdio or on a loopback TCP port (first request `auth`). +//! +//! Methods: the app's `ping`, `command.run {id, params?}`, `command.list {filter?}`, +//! `session.inspect`, plus `command.batch {steps}`, `project.open {path}`, +//! `project.save {path?}`, `mesh.import {path, resolution?, name?}`, `resource.import {path}`, +//! `material.save {path, layer?, name?}`, `material.load {path}` (workspace-scoped versions of +//! the engine commands), +//! `textures.export {dir, …}`, `render {view?, width?, height?, yaw?, pitch?}` (PNG base64, the +//! shape of the app's `viewport.screenshot`) and `methods`. + +use std::io::{BufRead, Write}; +use std::net::TcpListener; +use std::sync::{Arc, Mutex, PoisonError}; + +use base64::Engine as _; +use serde_json::{Value, json}; + +use crate::budgets::json_text; +use crate::headless::RenderOptions; +use crate::security::{ConnectionLimiter, LineRead, MAX_BATCH_STEPS, MAX_CONNECTIONS, MAX_REQUEST_BYTES, read_bounded_line, token_matches}; +use crate::{AutomationError, Headless, bad}; + +pub const METHODS: &[&str] = &[ + "ping", + "methods", + "session.inspect", + "command.list", + "command.run", + "command.batch", + "project.open", + "project.save", + "mesh.import", + "resource.import", + "material.save", + "material.load", + "textures.export", + "render", +]; + +fn str_of<'a>(p: &'a Value, k: &str) -> Option<&'a str> { + p.get(k).and_then(Value::as_str) +} + +fn u32_of(p: &Value, k: &str) -> Result, AutomationError> { + match p.get(k) { + None | Some(Value::Null) => Ok(None), + Some(v) => v.as_u64().and_then(|v| u32::try_from(v).ok()).map(Some).ok_or_else(|| bad(format!("`{k}` must be a whole number"))), + } +} + +fn f32_of(p: &Value, k: &str) -> Result, AutomationError> { + match p.get(k) { + None | Some(Value::Null) => Ok(None), + Some(v) => v.as_f64().map(|v| Some(v as f32)).ok_or_else(|| bad(format!("`{k}` must be a number"))), + } +} + +fn required<'a>(p: &'a Value, k: &str) -> Result<&'a str, AutomationError> { + str_of(p, k).ok_or_else(|| bad(format!("`{k}` is required"))) +} + +impl Headless { + /// Dispatch one request. Unknown methods and bad params are errors, never panics. + pub fn handle(&mut self, method: &str, p: &Value) -> Result { + match method { + "ping" => Ok(json!("pong")), + "methods" => Ok(json!(METHODS)), + "session.inspect" => Ok(self.inspect()), + "command.list" => Ok(self.command_list(str_of(p, "filter"))), + "command.run" => self.command_run(required(p, "id")?, p.get("params").cloned().unwrap_or(Value::Null)), + "command.batch" => { + let steps = p.get("steps").and_then(Value::as_array).ok_or_else(|| bad("`steps` must be an array of {id, params}"))?; + if steps.len() > MAX_BATCH_STEPS { + return Err(bad(format!("at most {MAX_BATCH_STEPS} steps per batch"))); + } + Ok(self.batch(steps)) + } + "project.open" => self.open(required(p, "path")?), + "project.save" => self.save(str_of(p, "path")), + "mesh.import" => self.import_mesh(required(p, "path")?, u32_of(p, "resolution")?, str_of(p, "name")), + "resource.import" => self.import_resource(required(p, "path")?), + "material.save" => self.material_save(p), + "material.load" => self.material_load(required(p, "path")?), + "textures.export" => self.export(p), + "render" => { + let o = RenderOptions { + view: str_of(p, "view").map(str::to_string), + width: u32_of(p, "width")?, + height: u32_of(p, "height")?, + yaw: f32_of(p, "yaw")?, + pitch: f32_of(p, "pitch")?, + samples: u32_of(p, "samples")?, + bounces: u32_of(p, "bounces")?, + }; + let (w, h, png) = self.render_png(&o)?; + Ok(json!({ "width": w, "height": h, "png": base64::engine::general_purpose::STANDARD.encode(png) })) + } + m if m.starts_with("ui.") || m == "viewport.screenshot" => { + Err(bad(format!("`{m}` needs the running app (texturecraft --control ); headless renders use `render`"))) + } + other => Err(bad(format!("unknown method `{other}` (see `methods`)"))), + } + } +} + +/// Answer one request line. A panic inside a command becomes an error reply (edits run on a +/// snapshot, so the session stays consistent). +pub fn respond(h: &Mutex, line: &str) -> Value { + let req: Value = match serde_json::from_str(line) { + Ok(v) => v, + Err(e) => return json!({"id": null, "ok": false, "error": format!("bad JSON: {e}")}), + }; + let id = req.get("id").cloned().unwrap_or(Value::Null); + let Some(method) = req.get("method").and_then(Value::as_str) else { + return json!({"id": id, "ok": false, "error": "`method` is required"}); + }; + let params = req.get("params").cloned().unwrap_or(Value::Null); + let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| h.lock().unwrap_or_else(PoisonError::into_inner).handle(method, ¶ms))) + .unwrap_or_else(|_| Err(AutomationError::Other(format!("internal error in `{method}`")))); + match r { + Ok(v) => json!({"id": id, "ok": true, "result": v}), + Err(e) => json!({"id": id, "ok": false, "error": e.to_string()}), + } +} + +/// Write one reply line; an oversized reply becomes an error with the same id. +fn write_reply(w: &mut impl Write, reply: &Value) -> std::io::Result<()> { + let text = json_text(reply).unwrap_or_else(|e| json!({"id": reply.get("id"), "ok": false, "error": e.to_string()}).to_string()); + writeln!(w, "{text}")?; + w.flush() +} + +/// Serve JSON lines from `r` to `w` until EOF (stdio mode: no authentication). +pub fn serve_lines(h: &Mutex, mut r: impl BufRead, mut w: impl Write) -> std::io::Result<()> { + let mut line = String::new(); + loop { + let reply = match read_bounded_line(&mut r, &mut line) { + Ok(LineRead::Eof) => return Ok(()), + Ok(LineRead::TooLong) => json!({"id": null, "ok": false, "error": format!("request exceeds {MAX_REQUEST_BYTES} bytes")}), + Err(e) if e.kind() == std::io::ErrorKind::InvalidData => json!({"id": null, "ok": false, "error": "request is not UTF-8"}), + Err(e) => return Err(e), + Ok(LineRead::Line) if line.trim().is_empty() => continue, + Ok(LineRead::Line) => respond(h, &line), + }; + write_reply(&mut w, &reply)?; + } +} + +/// One TCP connection: the first request must be `auth {token}`, as on the app's channel. +fn serve_connection(h: &Mutex, stream: std::net::TcpStream, token: &str) -> std::io::Result<()> { + stream.set_read_timeout(Some(std::time::Duration::from_secs(600)))?; + let mut r = std::io::BufReader::new(stream.try_clone()?); + let mut w = stream; + let mut line = String::new(); + let mut authed = false; + loop { + match read_bounded_line(&mut r, &mut line)? { + LineRead::Eof => return Ok(()), + LineRead::TooLong => return write_reply(&mut w, &json!({"id": null, "ok": false, "error": "request too large"})), + LineRead::Line if line.trim().is_empty() => continue, + LineRead::Line => {} + } + if authed { + write_reply(&mut w, &respond(h, &line))?; + continue; + } + let req: Value = serde_json::from_str(&line).unwrap_or(Value::Null); + let supplied = req.get("params").and_then(|p| p.get("token")).and_then(Value::as_str).unwrap_or_default(); + authed = req.get("method").and_then(Value::as_str) == Some("auth") && token_matches(token, supplied); + let id = req.get("id").cloned().unwrap_or(Value::Null); + if !authed { + return write_reply(&mut w, &json!({"id": id, "ok": false, "error": "authenticate first: {\"method\":\"auth\",\"params\":{\"token\":…}}"})); + } + write_reply(&mut w, &json!({"id": id, "ok": true}))?; + } +} + +/// Serve authenticated JSON lines on `127.0.0.1:port` (0 picks a port) with one shared +/// session. `ready` gets the bound address. +pub fn serve_tcp(port: u16, h: Arc>, token: String, ready: impl FnOnce(std::net::SocketAddr)) -> Result<(), AutomationError> { + let listener = TcpListener::bind(("127.0.0.1", port)).map_err(|e| AutomationError::Io(format!("cannot listen on 127.0.0.1:{port}: {e}")))?; + ready(listener.local_addr().map_err(|e| AutomationError::Io(e.to_string()))?); + let limiter = ConnectionLimiter::default(); + let token = Arc::new(token); + for stream in listener.incoming() { + let Ok(mut stream) = stream else { continue }; + let Some(permit) = limiter.try_acquire(MAX_CONNECTIONS) else { + let _ = writeln!(stream, "{}", json!({"id": null, "ok": false, "error": "too many connections"})); + continue; + }; + let (h, token) = (h.clone(), token.clone()); + std::thread::spawn(move || { + let _permit = permit; + let _ = serve_connection(&h, stream, &token); + }); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::headless::Filesystem; + + fn session() -> Mutex { + let s = texturecraft_engine::Session::new_project("t", texturecraft_engine::geom::primitives::cube(2.0), "builtin:cube", None, 128).unwrap(); + Mutex::new(Headless::new(s, Filesystem::Denied)) + } + + #[test] + fn lines_round_trip_and_errors() { + let h = session(); + let input = concat!( + r#"{"id":1,"method":"command.run","params":{"id":"layer.add.fill","params":{"color":[1,0,0]}}}"#, + "\n\n", + r#"{"id":2,"method":"session.inspect"}"#, + "\n", + r#"{"id":3,"method":"nope"}"#, + "\n", + "not json\n", + r#"{"id":4,"method":"command.run","params":{"id":"file.open","params":{"path":"/etc/passwd"}}}"#, + "\n", + r#"{"id":5,"method":"project.open","params":{"path":"x.tcraft"}}"#, + "\n", + r#"{"id":6,"method":"command.batch","params":{"steps":[{"id":"layer.add.paint"},{"id":"nope"},{"id":"layer.add.paint"}]}}"#, + "\n", + ); + let mut out = Vec::new(); + serve_lines(&h, input.as_bytes(), &mut out).unwrap(); + let r: Vec = String::from_utf8(out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect(); + assert_eq!(r.len(), 7); + assert_eq!(r[0]["ok"], true); + assert_eq!(r[1]["result"]["layers"][0]["kind"], "fill"); + assert_eq!(r[2]["ok"], false); + assert_eq!(r[3]["id"], Value::Null); + assert!(r[4]["error"].as_str().unwrap().contains("disabled for automation"), "{}", r[4]); + assert!(r[5]["error"].as_str().unwrap().contains("not granted"), "{}", r[5]); + assert_eq!(r[6]["result"]["completed"], 1); + assert_eq!(r[6]["result"]["failed"]["index"], 1); + } + + #[test] + fn tcp_requires_the_token() { + use std::io::{BufRead, BufReader}; + let h = Arc::new(session()); + let (tx, rx) = std::sync::mpsc::channel(); + let token = "0123456789abcdef0123".to_string(); + std::thread::spawn(move || serve_tcp(0, h, token, |a| tx.send(a).unwrap())); + let addr = rx.recv().unwrap(); + let talk = |lines: &[&str]| { + let s = std::net::TcpStream::connect(addr).unwrap(); + let mut w = s.try_clone().unwrap(); + let mut r = BufReader::new(s); + lines + .iter() + .map(|l| { + writeln!(w, "{l}").unwrap(); + let mut reply = String::new(); + r.read_line(&mut reply).unwrap(); + serde_json::from_str::(&reply).unwrap() + }) + .collect::>() + }; + let denied = talk(&[r#"{"id":1,"method":"session.inspect"}"#]); + assert_eq!(denied[0]["ok"], false); + let ok = talk(&[r#"{"id":1,"method":"auth","params":{"token":"0123456789abcdef0123"}}"#, r#"{"id":2,"method":"ping"}"#]); + assert_eq!(ok[0]["ok"], true); + assert_eq!(ok[1]["result"], "pong"); + assert_eq!(ok[1]["id"], 2); + } +} diff --git a/crates/automation/src/security.rs b/crates/automation/src/security.rs new file mode 100644 index 00000000..c781dfb3 --- /dev/null +++ b/crates/automation/src/security.rs @@ -0,0 +1,171 @@ +//! Tokens and bounded framing for the loopback JSON-lines transports (the app's control channel +//! and `texturecraft-cli serve` share the token conventions). + +use std::io::BufRead; +use std::path::PathBuf; +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; + +use crate::{AutomationError, bad}; + +/// Environment variable holding the control token (both sides). +pub const TOKEN_ENV: &str = "TEXTURECRAFT_CONTROL_TOKEN"; +/// Largest JSON request line, newline included (the app's limit too). +pub const MAX_REQUEST_BYTES: usize = 8 << 20; +/// Simultaneous TCP connections served by `serve --port`. +pub const MAX_CONNECTIONS: usize = 16; +/// Steps in one `command_batch`. +pub const MAX_BATCH_STEPS: usize = 256; + +/// The token file the app (and `serve --port`) writes when the environment gives no token. +pub fn token_file(port: u16) -> PathBuf { + std::env::temp_dir().join(format!("texturecraft-control-{port}.token")) +} + +/// The app accepts any environment token of at least 16 characters; generated ones are 64 hex. +fn validate_token(t: &str) -> Result<(), AutomationError> { + if (16..=1024).contains(&t.len()) && t.bytes().all(|b| b.is_ascii_graphic()) { + Ok(()) + } else { + Err(bad("control token must be 16 to 1024 printable ASCII characters")) + } +} + +/// A fresh 256-bit token as 64 hex digits, from the OS random generator. +pub fn generate_token() -> Result { + let mut bytes = [0u8; 32]; + getrandom::fill(&mut bytes).map_err(|e| AutomationError::Other(format!("cannot generate a control token: {e}")))?; + Ok(bytes.iter().map(|b| format!("{b:02x}")).collect()) +} + +/// Token for a client of `port`: explicit, else `TEXTURECRAFT_CONTROL_TOKEN`, else the token file. +pub fn client_token(port: u16, supplied: Option) -> Result { + let token = match supplied.or_else(|| std::env::var(TOKEN_ENV).ok()) { + Some(t) => t, + None => { + let path = token_file(port); + std::fs::read_to_string(&path) + .map_err(|e| bad(format!("no control token: set {TOKEN_ENV} or start the app with `--control {port}` so it writes {} ({e})", path.display())))? + } + }; + let token = token.trim().to_string(); + validate_token(&token)?; + Ok(token) +} + +/// Token for a server on `port`: `TEXTURECRAFT_CONTROL_TOKEN`, else a fresh one written to +/// [`token_file`] (owner-only on Unix). Returns the token and where it was written. +pub fn server_token(port: u16) -> Result<(String, Option), AutomationError> { + if let Ok(t) = std::env::var(TOKEN_ENV) { + validate_token(t.trim())?; + return Ok((t.trim().to_string(), None)); + } + let token = generate_token()?; + let path = token_file(port); + let mut o = std::fs::OpenOptions::new(); + o.write(true).create(true).truncate(true); + #[cfg(unix)] + std::os::unix::fs::OpenOptionsExt::mode(&mut o, 0o600); + let res = o.open(&path).and_then(|mut f| std::io::Write::write_all(&mut f, token.as_bytes())); + res.map_err(|e| AutomationError::Io(format!("cannot write the control token to {}: {e}", path.display())))?; + Ok((token, Some(path))) +} + +/// Compare equal-length tokens without an early exit. +pub fn token_matches(expected: &str, supplied: &str) -> bool { + expected.len() == supplied.len() && expected.bytes().zip(supplied.bytes()).fold(0u8, |d, (a, b)| d | (a ^ b)) == 0 +} + +pub enum LineRead { + Eof, + Line, + /// Over [`MAX_REQUEST_BYTES`]; the rest of the line is still unread. + TooLong, +} + +/// Read one line, never buffering more than [`MAX_REQUEST_BYTES`] + 1 bytes. +pub fn read_bounded_line(r: &mut impl BufRead, line: &mut String) -> std::io::Result { + line.clear(); + let mut bytes = Vec::new(); + let n = std::io::Read::take(&mut *r, (MAX_REQUEST_BYTES + 1) as u64).read_until(b'\n', &mut bytes)?; + if n == 0 { + return Ok(LineRead::Eof); + } + if n > MAX_REQUEST_BYTES { + if bytes.last() != Some(&b'\n') { + discard_rest_of_line(r)?; + } + return Ok(LineRead::TooLong); + } + *line = String::from_utf8(bytes).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; + Ok(LineRead::Line) +} + +fn discard_rest_of_line(r: &mut impl BufRead) -> std::io::Result<()> { + loop { + let buf = r.fill_buf()?; + if buf.is_empty() { + return Ok(()); + } + if let Some(end) = buf.iter().position(|&b| b == b'\n') { + r.consume(end + 1); + return Ok(()); + } + let n = buf.len(); + r.consume(n); + } +} + +/// Counts live connections; a permit is released when dropped. +#[derive(Clone, Default)] +pub struct ConnectionLimiter(Arc); + +pub struct Permit(Arc); + +impl ConnectionLimiter { + pub fn try_acquire(&self, max: usize) -> Option { + self.0.fetch_update(Ordering::AcqRel, Ordering::Acquire, |n| (n < max).then_some(n + 1)).ok().map(|_| Permit(self.0.clone())) + } +} + +impl Drop for Permit { + fn drop(&mut self) { + self.0.fetch_sub(1, Ordering::AcqRel); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn tokens() { + let a = generate_token().unwrap(); + assert_eq!(a.len(), 64); + assert_ne!(a, generate_token().unwrap()); + assert!(token_matches(&a, &a.clone())); + assert!(!token_matches(&a, "short")); + assert!(client_token(1, Some("too short".into())).is_err()); + assert!(client_token(1, Some(format!(" {a}\n"))).is_ok()); + } + + #[test] + fn oversized_lines_are_skipped() { + let input = format!("{}\n{{\"ok\":1}}\n", "x".repeat(MAX_REQUEST_BYTES + 10)); + let mut r = std::io::Cursor::new(input); + let mut line = String::new(); + assert!(matches!(read_bounded_line(&mut r, &mut line).unwrap(), LineRead::TooLong)); + assert!(matches!(read_bounded_line(&mut r, &mut line).unwrap(), LineRead::Line)); + assert_eq!(line, "{\"ok\":1}\n"); + assert!(matches!(read_bounded_line(&mut r, &mut line).unwrap(), LineRead::Eof)); + } + + #[test] + fn limiter_releases() { + let l = ConnectionLimiter::default(); + let p = l.try_acquire(1).unwrap(); + assert!(l.try_acquire(1).is_none()); + drop(p); + assert!(l.try_acquire(1).is_some()); + } +} diff --git a/crates/automation/src/server.rs b/crates/automation/src/server.rs new file mode 100644 index 00000000..fc3ef49f --- /dev/null +++ b/crates/automation/src/server.rs @@ -0,0 +1,499 @@ +//! The MCP server (rmcp). Tool names are `snake_case`; each tool maps to one control-channel +//! method (see [`crate::rpc`]), answered in process (headless) or by the running app (bridge). + +use std::sync::{Arc, Mutex, PoisonError}; + +use base64::Engine as _; +use rmcp::handler::server::router::tool::ToolRouter; +use rmcp::handler::server::wrapper::Parameters; +use rmcp::model::{CallToolResult, ContentBlock as Content}; +use rmcp::{ErrorData as McpError, ServerHandler, ServiceExt, tool, tool_handler, tool_router}; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; +use serde_json::{Value, json}; + +use crate::budgets::{MAX_PNG_SIDE, check_png, fit_png, json_text}; +use crate::headless::batch_reply; +use crate::security::MAX_BATCH_STEPS; +use crate::workspace::authorize_command; +use crate::{AutomationError, BridgeClient, Headless, bad}; + +/// Where tools run. +pub enum Backend { + /// An in-process session. + Headless(Arc>), + /// The running desktop app, over its control channel. + Bridge(Arc), +} + +#[derive(Clone)] +pub struct TexturecraftMcp { + backend: Arc, + tool_router: ToolRouter, +} + +// --------------------------------------------------------------------------------------------- +// Parameters (serialized back to JSON as the control method's params) + +#[derive(Debug, Default, Deserialize, Serialize, JsonSchema)] +pub struct FilterParams { + /// Only commands whose id or label contains this text (case-insensitive). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub filter: Option, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +pub struct RunParams { + /// Command id from `command_list`, e.g. `layer.add.fill`, `mask.add.black`, `edit.undo`. + pub id: String, + /// Command parameters (see the command's `params` doc). Omitted = defaults; never a dialog. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub params: Option, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +pub struct BatchParams { + /// Commands run in order; the batch stops at the first error and reports its index. + pub steps: Vec, +} + +#[derive(Debug, Default, Deserialize, Serialize, JsonSchema)] +pub struct NewParams { + /// Built-in mesh: material_ball (default), sphere, cube, rounded_cube, cylinder, torus, plane, crate. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub mesh: Option, + /// Texture set resolution, a power of two from 128 to 8192 (default 1024). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub resolution: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub name: Option, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +pub struct OpenParams { + /// Forward-slash path relative to the read root. + pub path: String, +} + +#[derive(Debug, Default, Deserialize, Serialize, JsonSchema)] +pub struct SaveParams { + /// Forward-slash path relative to the write root (`.tcraft` is appended if missing). Omit to + /// save to the project's own file. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub path: Option, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +pub struct MaterialSaveParams { + /// `.tcmat` file relative to the write root (the extension is appended if missing). + pub path: String, + /// Layer or folder id (default: the selected layer). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub layer: Option, + /// Material name (default: the layer name). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub name: Option, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +pub struct ImportParams { + /// Mesh file relative to the read root: .glb, .gltf (self-contained), .fbx, .dae, .obj, .ply, .stl. + pub path: String, + /// Texture set resolution (default 2048). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub resolution: Option, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +pub struct ExportParams { + /// Output folder relative to the write root (created if missing; `.` = the root). + pub dir: String, + /// Preset name from `command_run export.presets`, e.g. "glTF PBR Metal Roughness". + #[serde(default, skip_serializing_if = "Option::is_none")] + pub preset: Option, + /// Texture set index or "all" (default all). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ts: Option, + /// File format override: png, jpg, tga, tif, exr, … + #[serde(default, skip_serializing_if = "Option::is_none")] + pub format: Option, + /// none | dilation | dilationDefault | infinite + #[serde(default, skip_serializing_if = "Option::is_none")] + pub padding: Option, + /// Output size in pixels (power of two). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub size: Option, +} + +#[derive(Debug, Default, Deserialize, Serialize, JsonSchema)] +pub struct BakeParams { + /// Texture set index or "all" (default: the active one). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub ts: Option, + /// Bake resolution (power of two). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub resolution: Option, + /// Mesh map ids, e.g. ["ambientOcclusion", "curvature"] (default: the set's bake settings). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub maps: Option>, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +pub struct StrokeParams { + /// Stroke points `[[x, y], …]` in viewport pixels (origin top-left), as in `viewport_render`. + pub points: Vec<[f64; 2]>, + /// Viewport size `[w, h]` the points refer to (default: the session viewport, 1280×800). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub viewport: Option<[f64; 2]>, + /// Base color: `[r, g, b]` linear 0..1 or `"#rrggbb"`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub color: Option, + /// 0..1 + #[serde(default, skip_serializing_if = "Option::is_none")] + pub roughness: Option, + /// 0..1 + #[serde(default, skip_serializing_if = "Option::is_none")] + pub metallic: Option, + /// -1..1 + #[serde(default, skip_serializing_if = "Option::is_none")] + pub height: Option, + /// Brush size 0..100 (Painter's scale). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub size: Option, + /// Brush hardness 0..100. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub hardness: Option, +} + +#[derive(Debug, Default, Deserialize, Serialize, JsonSchema)] +pub struct RenderParams { + /// `3d` (default) or `2d` (the UV view of the active texture set). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub view: Option, + /// Pixels, 16..2048 (default: the session viewport, the space `paint_stroke` points use). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub width: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub height: Option, + /// Camera yaw in degrees for this render only. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub yaw: Option, + /// Camera pitch in degrees (-89..89) for this render only. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub pitch: Option, + /// Path trace the 3D view with this many samples per pixel (1..4096) instead of the + /// real-time viewport: slower, physically based light transport, no GPU needed. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub samples: Option, + /// Light bounces when path tracing (default 6). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub bounces: Option, +} + +#[derive(Debug, Default, Deserialize, Serialize, JsonSchema)] +pub struct ScreenshotParams { + /// `3d` (default) or `2d`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub view: Option, +} + +#[derive(Debug, Deserialize, Serialize, JsonSchema)] +pub struct MenuParams { + /// Menu item id from the app's `ui.menu.list`, e.g. `file.save`, `viewport.frame`. + pub id: String, +} + +// --------------------------------------------------------------------------------------------- +// Results + +fn ok_json(v: &Value) -> CallToolResult { + match json_text(v) { + Ok(text) => CallToolResult::success(vec![Content::text(text)]), + Err(e) => fail(e), + } +} + +fn fail(e: impl std::fmt::Display) -> CallToolResult { + CallToolResult::error(vec![Content::text(e.to_string())]) +} + +fn to_result(r: Result) -> Result { + Ok(match r { + Ok(v) => ok_json(&v), + Err(e) => fail(e), + }) +} + +/// A `{width, height, png: base64}` reply (render or app screenshot) as image content. +fn image_result(r: Result, note: &str, refit: bool) -> Result { + let png = r.and_then(|v| { + let b64 = v.get("png").and_then(Value::as_str).ok_or_else(|| AutomationError::Other("the reply has no PNG".into()))?; + if !refit { + check_png(b64.len() / 4 * 3)?; + return Ok(b64.to_string()); + } + let bytes = base64::engine::general_purpose::STANDARD.decode(b64).map_err(|e| AutomationError::Other(format!("bad PNG data: {e}")))?; + Ok(base64::engine::general_purpose::STANDARD.encode(fit_png(bytes, MAX_PNG_SIDE)?)) + }); + Ok(match png { + Ok(b64) => CallToolResult::success(vec![Content::image(b64, "image/png"), Content::text(note.to_string())]), + Err(e) => fail(e), + }) +} + +fn params(p: &T) -> Value { + serde_json::to_value(p).unwrap_or(Value::Null) +} + +impl TexturecraftMcp { + pub fn headless(h: Headless) -> Self { + Self::with_backend(Backend::Headless(Arc::new(Mutex::new(h)))) + } + + pub fn bridge(client: BridgeClient) -> Self { + Self::with_backend(Backend::Bridge(Arc::new(client))) + } + + pub fn with_backend(backend: Backend) -> Self { + TexturecraftMcp { backend: Arc::new(backend), tool_router: Self::tool_router() } + } + + /// Serve MCP on stdin/stdout until the client disconnects. + pub async fn serve_stdio(self) -> Result<(), AutomationError> { + let running = self.serve(rmcp::transport::stdio()).await.map_err(|e| AutomationError::Other(format!("MCP start: {e}")))?; + running.waiting().await.map_err(|e| AutomationError::Other(e.to_string()))?; + Ok(()) + } + + fn is_bridge(&self) -> bool { + matches!(*self.backend, Backend::Bridge(_)) + } + + /// One control-channel method on the backend. + async fn call(&self, method: &str, p: Value) -> Result { + match &*self.backend { + Backend::Headless(h) => { + let (h, m) = (h.clone(), method.to_string()); + // A panicking command poisons the lock, but edits run on a document snapshot + // (`Session::edit`), so the session stays consistent: keep serving. + tokio::task::spawn_blocking(move || h.lock().unwrap_or_else(PoisonError::into_inner).handle(&m, &p)) + .await + .unwrap_or_else(|_| Err(AutomationError::Other(format!("internal error in `{method}`")))) + } + Backend::Bridge(b) => match method { + "command.run" => { + let id = p.get("id").and_then(Value::as_str).unwrap_or_default(); + authorize_command(id, p.get("params").unwrap_or(&Value::Null))?; + b.call(method, p).await + } + "project.save" if p.get("path").is_none() => b.call("ui.menu.invoke", json!({"id": "file.save"})).await, + "project.save" | "project.open" | "mesh.import" | "resource.import" | "material.save" | "material.load" | "textures.export" + | "command.batch" | "render" => { + Err(bad(format!("`{method}` with paths works in headless mode only: the running app reads and writes its own files (use ui_menu_invoke)"))) + } + _ => b.call(method, p).await, + }, + } + } + + async fn run(&self, id: &str, p: Value) -> Result { + self.call("command.run", json!({"id": id, "params": p})).await + } + + async fn bridge_only(&self, method: &str, p: Value) -> Result { + if self.is_bridge() { + self.call(method, p).await + } else { + Err(bad(format!( + "`{method}` needs the running app: start `texturecraft --control ` and run `texturecraft-cli mcp --bridge ` (headless renders: viewport_render)" + ))) + } + } + + async fn stroke(&self, p: StrokeParams) -> Result { + self.run("tool.paint", json!({})).await?; + if let Some(c) = p.color { + self.run("tool.material", json!({"channel": "baseColor", "color": c})).await?; + } + for (channel, v) in [("roughness", p.roughness), ("metallic", p.metallic), ("height", p.height)] { + if let Some(v) = v { + self.run("tool.material", json!({"channel": channel, "value": v})).await?; + } + } + if p.size.is_some() || p.hardness.is_some() { + self.run("tool.brush", json!({"size": p.size, "hardness": p.hardness})).await?; + } + self.run("tool.stroke", json!({"points": p.points, "viewport": p.viewport})).await + } +} + +// --------------------------------------------------------------------------------------------- +// Tools + +#[tool_router] +impl TexturecraftMcp { + #[tool(description = "The whole session as JSON: project, mesh, texture sets (resolution, channels, baked maps), the layer stack \ + top to bottom (fill sources, masks, effects, painted tile counts), tool, camera, display and history.")] + async fn session_inspect(&self) -> Result { + to_result(self.call("session.inspect", json!({})).await) + } + + #[tool(description = "List engine commands: id, label, menu path, shortcut, params doc and whether it can run now.")] + async fn command_list(&self, Parameters(p): Parameters) -> Result { + to_result(self.call("command.list", params(&p)).await) + } + + #[tool(description = "Run an engine command by id with JSON params (see command_list). Runs with defaults, never opens a \ + dialog. Commands given host paths (file.open, file.save, resource.import, material.save, …) are disabled: use the \ + workspace tools (project_open, project_save, mesh_import, resource_import, material_save, material_load, textures_export).")] + async fn command_run(&self, Parameters(p): Parameters) -> Result { + to_result(self.call("command.run", params(&p)).await) + } + + #[tool(description = "Run several commands in order: steps [{id, params}]. Stops at the first error. Returns {completed, \ + results, failed: {index, id, error} | null}; a failed batch is a tool error.")] + async fn command_batch(&self, Parameters(p): Parameters) -> Result { + if p.steps.len() > MAX_BATCH_STEPS { + return Ok(fail(format!("at most {MAX_BATCH_STEPS} steps per batch"))); + } + let reply = if self.is_bridge() { + let mut results = Vec::new(); + let mut failed = None; + for (i, s) in p.steps.iter().enumerate() { + match self.call("command.run", params(s)).await { + Ok(v) => results.push(v), + Err(e) => { + failed = Some((i, s.id.as_str(), e.to_string())); + break; + } + } + } + batch_reply(results, failed) + } else { + match self.call("command.batch", params(&p)).await { + Ok(v) => v, + Err(e) => return Ok(fail(e)), + } + }; + Ok(if reply["failed"].is_null() { ok_json(&reply) } else { fail(json_text(&reply).unwrap_or_else(|e| e.to_string())) }) + } + + #[tool(description = "Start a new project on a built-in mesh (one texture set per material, each with an empty paint layer). \ + Returns the session.")] + async fn project_new(&self, Parameters(p): Parameters) -> Result { + to_result(self.run("project.new", params(&p)).await) + } + + #[tool(description = "Open a .tcraft project or a mesh file (path relative to the read root). Returns the session.")] + async fn project_open(&self, Parameters(p): Parameters) -> Result { + to_result(self.call("project.open", params(&p)).await) + } + + #[tool(description = "Save the project as .tcraft (path relative to the write root; omit to save to its own file).")] + async fn project_save(&self, Parameters(p): Parameters) -> Result { + to_result(self.call("project.save", params(&p)).await) + } + + #[tool(description = "New project from a mesh file (path relative to the read root): one texture set per material. Returns \ + the session; import warnings are in the session log.")] + async fn mesh_import(&self, Parameters(p): Parameters) -> Result { + to_result(self.call("mesh.import", params(&p)).await) + } + + #[tool(description = "Import an image (png, jpg, tga, tif, bmp, webp, exr, hdr; path relative to the read root) as a project \ + resource. Returns its name, for command_run layer.add.image {name} or tool.stencil {image}.")] + async fn resource_import(&self, Parameters(p): Parameters) -> Result { + to_result(self.call("resource.import", params(&p)).await) + } + + #[tool(description = "Save a layer or folder (default: the selected one) as a smart material file (.tcmat) below the write root.")] + async fn material_save(&self, Parameters(p): Parameters) -> Result { + to_result(self.call("material.save", params(&p)).await) + } + + #[tool(description = "Add a smart material from a .tcmat file (relative to the read root) above the selection. Returns its layer.")] + async fn material_load(&self, Parameters(p): Parameters) -> Result { + to_result(self.call("material.load", params(&p)).await) + } + + #[tool(description = "Export textures with a preset into a folder relative to the write root. Returns the written files and \ + warnings.")] + async fn textures_export(&self, Parameters(p): Parameters) -> Result { + to_result(self.call("textures.export", params(&p)).await) + } + + #[tool(description = "Bake mesh maps (ambient occlusion, curvature, thickness, …) for generators and smart materials.")] + async fn bake(&self, Parameters(p): Parameters) -> Result { + to_result(self.run("bake.run", params(&p)).await) + } + + #[tool(description = "Paint a stroke with the paint tool on the selected layer (or its mask target): sets the brush material \ + (color, roughness, metallic, height) and size/hardness, then strokes through `points` in viewport pixels. See \ + viewport_render for where the mesh is.")] + async fn paint_stroke(&self, Parameters(p): Parameters) -> Result { + to_result(self.stroke(p).await) + } + + #[tool(description = "Headless: render the 3D view (or the 2D UV view) as a PNG image, up to 2048 px a side. Camera yaw/pitch \ + apply to this render only. In bridge mode use viewport_screenshot.")] + async fn viewport_render(&self, Parameters(p): Parameters) -> Result { + let note = format!("render {}", params(&p)); + image_result(self.call("render", params(&p)).await, ¬e, false) + } + + #[tool(description = "Bridge mode: PNG of the live app's 3D or 2D viewport.")] + async fn viewport_screenshot(&self, Parameters(p): Parameters) -> Result { + image_result(self.bridge_only("viewport.screenshot", params(&p)).await, "screenshot of the app viewport", true) + } + + #[tool(description = "Bridge mode: PNG of the whole app window (downscaled to 2048 px a side at most).")] + async fn ui_screenshot(&self) -> Result { + image_result(self.bridge_only("ui.screenshot", json!({})).await, "screenshot of the app window", true) + } + + #[tool(description = "Bridge mode: the app's UI state (panels, windows, mode, layout, viewport rect, toasts).")] + async fn ui_inspect(&self) -> Result { + to_result(self.bridge_only("ui.inspect", json!({})).await) + } + + #[tool(description = "Bridge mode: activate a menu item by id like a click (may open a window). ui.menu.list lists ids.")] + async fn ui_menu_invoke(&self, Parameters(p): Parameters) -> Result { + to_result(self.bridge_only("ui.menu.invoke", params(&p)).await) + } +} + +#[tool_handler( + router = self.tool_router, + name = "texturecraft", + instructions = "TextureCraft 3D texture painter. Every edit is an engine command: `command_list` shows ids and params, \ + `command_run`/`command_batch` run them. Start with `project_new` (built-in mesh) or `project_open`/`mesh_import` (files \ + relative to the workspace root), check your work with `session_inspect` and `viewport_render`, paint with `paint_stroke`, \ + bake with `bake`, then `project_save` (.tcraft) and `textures_export`. Undo: command_run edit.undo." +)] +impl ServerHandler for TexturecraftMcp {} + +#[cfg(test)] +mod tests { + use super::*; + use crate::headless::Filesystem; + + #[tokio::test(flavor = "multi_thread")] + async fn a_panic_does_not_wedge_the_session() { + let s = texturecraft_engine::Session::new_project("t", texturecraft_engine::geom::primitives::cube(2.0), "builtin:cube", None, 128).unwrap(); + let mcp = TexturecraftMcp::headless(Headless::new(s, Filesystem::Denied)); + let Backend::Headless(h) = &*mcp.backend else { panic!("headless") }; + let h2 = h.clone(); + let _ = tokio::task::spawn_blocking(move || { + let _g = h2.lock().unwrap(); + panic!("boom") + }) + .await; + assert!(mcp.call("session.inspect", json!({})).await.is_ok()); + } + + #[test] + fn oversized_replies_become_errors() { + let r = ok_json(&json!("x".repeat(crate::budgets::MAX_RESPONSE_BYTES))); + assert_eq!(r.is_error, Some(true)); + } +} diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs new file mode 100644 index 00000000..2015c090 --- /dev/null +++ b/crates/automation/src/workspace.rs @@ -0,0 +1,259 @@ +//! Capability-based file access for untrusted automation paths (MCP, `serve`). +//! +//! Roots are trusted launch-time configuration (`--read-root`, `--write-root`); request paths +//! are untrusted, forward-slash relative paths resolved by `cap-std` beneath the held directory +//! handle, so `..`, absolute paths, drive prefixes and symlink or junction escapes all fail. + +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use cap_std::ambient_authority; +use cap_std::fs::{Dir, OpenOptions}; +use serde_json::Value; + +use crate::{AutomationError, bad}; + +/// Largest file read through automation (the mesh importer's own limit). +pub const MAX_READ_BYTES: u64 = texturecraft_engine::formats::mesh_import::MAX_MESH_FILE_BYTES; + +/// Separate read and write directory capabilities. A missing authority fails closed. +#[derive(Clone, Default)] +pub struct AuthorizedWorkspace { + read: Option>, + write: Option>, +} + +impl AuthorizedWorkspace { + pub fn new(read_root: Option<&Path>, write_root: Option<&Path>) -> Result { + Ok(Self { read: open_root(read_root, "read")?, write: open_root(write_root, "write")? }) + } + + /// Read one regular file below the read root. + pub fn read(&self, path: &str) -> Result, AutomationError> { + let rel = relative_path(path)?; + let root = self.read.as_ref().ok_or_else(|| denied("read"))?; + let file = root.open(&rel).map_err(|e| file_error("read", path, e))?; + let meta = file.metadata().map_err(|e| file_error("read", path, e))?; + if !meta.is_file() { + return Err(bad(format!("not a regular file: `{path}`"))); + } + if meta.len() > MAX_READ_BYTES { + return Err(bad(format!("`{path}` is {} bytes (limit {MAX_READ_BYTES})", meta.len()))); + } + let mut bytes = Vec::new(); + file.take(MAX_READ_BYTES).read_to_end(&mut bytes).map_err(|e| file_error("read", path, e))?; + Ok(bytes) + } + + /// Create or replace one file below the write root: a temporary file beside the target, + /// synced and renamed over it. The parent directory must exist. + pub fn write(&self, path: &str, bytes: &[u8]) -> Result<(), AutomationError> { + let rel = relative_path(path)?; + let root = self.write.as_ref().ok_or_else(|| denied("write"))?; + let leaf = rel.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); + let tmp = rel.with_file_name(format!(".{leaf}.{}.tmp", std::process::id())); + let mut options = OpenOptions::new(); + options.write(true).create_new(true); + let res = root + .open_with(&tmp, &options) + .and_then(|mut f| { + f.write_all(bytes)?; + f.sync_all() + }) + .and_then(|()| root.rename(&tmp, root, &rel)); + if let Err(e) = res { + let _ = root.remove_file(&tmp); + return Err(file_error("write", path, e)); + } + Ok(()) + } + + /// Create a directory (and its parents) below the write root. + pub fn create_dir_all(&self, path: &str) -> Result<(), AutomationError> { + let rel = relative_path(path)?; + let root = self.write.as_ref().ok_or_else(|| denied("write"))?; + root.create_dir_all(&rel).map_err(|e| file_error("create", path, e)) + } +} + +fn open_root(path: Option<&Path>, authority: &str) -> Result>, AutomationError> { + let Some(path) = path else { return Ok(None) }; + if path.as_os_str().is_empty() { + return Err(bad(format!("the automation {authority} root is empty"))); + } + let dir = + Dir::open_ambient_dir(path, ambient_authority()).map_err(|e| AutomationError::Io(format!("cannot open {authority} root `{}`: {e}", path.display())))?; + Ok(Some(Arc::new(dir))) +} + +/// Validate an untrusted path before any I/O (cap-std then confines resolution to the root). +pub fn relative_path(raw: &str) -> Result { + let reject = |why: &str| bad(format!("path rejected ({why}): `{raw}`; use a forward-slash path relative to the workspace root")); + if raw.is_empty() { + return Err(reject("empty")); + } + if raw.contains('\\') { + return Err(reject("backslash")); + } + if raw.starts_with('/') { + return Err(reject("absolute")); + } + if raw.contains(':') { + return Err(reject("drive, scheme or stream prefix")); + } + for c in raw.split('/') { + if c.is_empty() || c == "." || c == ".." { + return Err(reject("empty, `.` or `..` component")); + } + if c.ends_with(['.', ' ']) || c.chars().any(char::is_control) { + return Err(reject("component ends in a dot or space, or has control characters")); + } + let stem = c.split('.').next().unwrap_or(c).to_ascii_uppercase(); + let device = matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL" | "CONIN$" | "CONOUT$") + || (stem.len() == 4 && (stem.starts_with("COM") || stem.starts_with("LPT")) && stem.ends_with(|ch: char| ch.is_ascii_digit())); + if device { + return Err(reject("reserved device name")); + } + } + Ok(PathBuf::from(raw)) +} + +fn denied(authority: &str) -> AutomationError { + bad(format!("file access is not granted: no {authority} root (start the server with --{authority}-root )")) +} + +fn file_error(op: &str, path: &str, e: std::io::Error) -> AutomationError { + AutomationError::Io(format!("{op} `{path}` failed: {e}")) +} + +/// Engine commands that take host paths (`file.open`, `file.exportTextures`, `resource.import`, +/// `material.save`, …) would bypass the workspace: automation clients use the workspace-scoped +/// methods instead. Any command given a `path` or `dir` is refused, and every `file.*` command +/// but `file.close` and `file.removeUnused` (`file.save` even without `path`: a headless session's path is +/// workspace-relative, not a host path; `file.reimportMesh` without `path` re-reads the +/// embedded mesh). +pub fn authorize_command(id: &str, params: &Value) -> Result<(), AutomationError> { + let has_path = ["path", "dir"].iter().any(|k| params.get(k).is_some_and(|v| !v.is_null())); + let ok = match id { + "file.close" | "file.removeUnused" => true, + "file.reimportMesh" => !has_path, + _ => !id.starts_with("file.") && !has_path, + }; + if ok { + Ok(()) + } else { + Err(bad(format!( + "`{id}` with host paths is disabled for automation; use project_open, project_save, mesh_import, resource_import, material_save, material_load or textures_export" + ))) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn roots(name: &str) -> (PathBuf, PathBuf, AuthorizedWorkspace) { + let base = std::env::temp_dir().join(format!("tc-ws-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&base); + let (inside, outside) = (base.join("inside"), base.join("outside")); + std::fs::create_dir_all(&inside).unwrap(); + std::fs::create_dir_all(&outside).unwrap(); + let ws = AuthorizedWorkspace::new(Some(&inside), Some(&inside)).unwrap(); + (inside, outside, ws) + } + + #[test] + fn reads_and_writes_stay_in_the_root() { + let (inside, _, ws) = roots("valid"); + std::fs::write(inside.join("a.txt"), b"canary").unwrap(); + assert_eq!(ws.read("a.txt").unwrap(), b"canary"); + ws.create_dir_all("out/deep").unwrap(); + ws.write("out/deep/b.txt", b"one").unwrap(); + ws.write("out/deep/b.txt", b"two").unwrap(); + assert_eq!(std::fs::read(inside.join("out/deep/b.txt")).unwrap(), b"two"); + let leftovers = std::fs::read_dir(inside.join("out/deep")).unwrap().count(); + assert_eq!(leftovers, 1, "no temporary files left behind"); + assert!(ws.write("missing/c.txt", b"x").is_err(), "parent must exist"); + } + + #[test] + fn escapes_and_malformed_paths_are_rejected() { + let (_, outside, ws) = roots("reject"); + let abs = outside.join("x.txt").display().to_string(); + for p in ["", "/abs", "../x", "a/../../x", "a\\b", "C:/x", "a//b", "./a", "a:b", "NUL", "con.txt", "x/COM1.log", "dot.", abs.as_str()] { + assert!(ws.read(p).is_err(), "read accepted {p:?}"); + assert!(ws.write(p, b"x").is_err(), "write accepted {p:?}"); + assert!(ws.create_dir_all(p).is_err(), "mkdir accepted {p:?}"); + } + } + + #[test] + fn authorities_are_separate_and_fail_closed() { + let (inside, _, _) = roots("separate"); + std::fs::write(inside.join("r.txt"), b"r").unwrap(); + let ro = AuthorizedWorkspace::new(Some(&inside), None).unwrap(); + assert!(ro.read("r.txt").is_ok()); + assert!(ro.write("w.txt", b"x").is_err()); + let none = AuthorizedWorkspace::default(); + assert!(none.read("r.txt").is_err()); + } + + #[cfg(unix)] + #[test] + fn symlink_escape_is_rejected() { + let (inside, outside, ws) = roots("symlink"); + std::fs::write(outside.join("secret.txt"), b"s").unwrap(); + std::os::unix::fs::symlink(&outside, inside.join("link")).unwrap(); + assert!(ws.read("link/secret.txt").is_err()); + assert!(ws.write("link/new.txt", b"x").is_err()); + assert!(!outside.join("new.txt").exists()); + } + + #[cfg(windows)] + #[test] + fn junction_escape_is_rejected() { + let (inside, outside, ws) = roots("junction"); + std::fs::write(outside.join("secret.txt"), b"s").unwrap(); + let link = inside.join("junction"); + let made = std::process::Command::new("cmd.exe").args(["/D", "/C", "mklink", "/J"]).arg(&link).arg(&outside).output(); + if !made.is_ok_and(|o| o.status.success()) { + eprintln!("skipping: cannot create a junction"); + return; + } + assert!(ws.read("junction/secret.txt").is_err()); + assert!(ws.write("junction/new.txt", b"x").is_err()); + assert!(!outside.join("new.txt").exists()); + let _ = std::fs::remove_dir(link); + } + + #[test] + fn path_commands_are_denied() { + for id in ["file.open", "file.new", "file.save", "file.saveAs", "file.saveCopy", "file.exportTextures"] { + assert!(authorize_command(id, &json!({})).is_err(), "{id}"); + } + for id in ["resource.import", "material.save", "material.load", "anything.new"] { + assert!(authorize_command(id, &json!({"path": "/etc/x"})).is_err(), "{id}"); + assert!(authorize_command(id, &json!({"dir": "/etc"})).is_err(), "{id}"); + } + assert!(authorize_command("file.reimportMesh", &json!({"path": "/etc/x.obj"})).is_err()); + assert!(authorize_command("file.reimportMesh", &json!({})).is_ok()); + assert!(authorize_command("file.close", &json!({})).is_ok()); + assert!(authorize_command("layer.add.fill", &json!({"color": "#ff0000", "path": null})).is_ok()); + } + + /// Every registry command documenting a host path must be classified (fails when a new + /// path-taking command is added without an automation policy). + #[test] + fn registry_path_params_are_classified() { + let probe = json!({"path": "/outside/probe", "dir": "/outside", "mesh": "/outside/m.obj"}); + let open: Vec<&str> = texturecraft_engine::command_specs() + .iter() + .filter(|c| c.params.contains("path") || c.params.contains("{dir")) + .filter(|c| authorize_command(c.id, &probe).is_ok()) + .map(|c| c.id) + .collect(); + assert!(open.is_empty(), "path-taking commands need an automation policy: {open:?}"); + } +} diff --git a/crates/automation/tests/agent_tasks.rs b/crates/automation/tests/agent_tasks.rs new file mode 100644 index 00000000..a202285e --- /dev/null +++ b/crates/automation/tests/agent_tasks.rs @@ -0,0 +1,377 @@ +//! Acceptance: an agent completes realistic texturing tasks over MCP, checked only through MCP +//! results (plus the exported files on disk), and the server holds its security line. + +use base64::Engine as _; +use rmcp::model::{CallToolRequestParams, CallToolResult, ClientConfig}; +use rmcp::service::RunningService; +use rmcp::{ClientHandler, RoleClient, ServiceExt}; +use serde_json::{Value, json}; +use texturecraft_automation::{AuthorizedWorkspace, BridgeClient, Filesystem, Headless, TexturecraftMcp, rpc}; + +#[derive(Clone, Default)] +struct Client; +impl ClientHandler for Client { + fn get_info(&self) -> ClientConfig { + ClientConfig::default() + } +} + +type Conn = RunningService; +type Server = tokio::task::JoinHandle<()>; + +async fn connect(fs: Filesystem) -> (Conn, Server) { + let (s, c) = tokio::io::duplex(1 << 22); + let session = texturecraft_engine::Session::new_project("t", texturecraft_engine::geom::primitives::cube(2.0), "builtin:cube", None, 128).unwrap(); + let server = tokio::spawn(async move { + if let Ok(running) = TexturecraftMcp::headless(Headless::new(session, fs)).serve(s).await { + let _ = running.waiting().await; + } + }); + (Client.serve(c).await.expect("client init"), server) +} + +/// Close the client and wait for the server, so its directory handles are released before the +/// test removes the folder (Windows refuses otherwise). +async fn shutdown(c: Conn, server: Server) { + c.cancel().await.unwrap(); + server.await.unwrap(); +} + +fn tmp(name: &str) -> std::path::PathBuf { + let d = std::env::temp_dir().join(format!("tc-agent-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d +} + +fn workspace(root: &std::path::Path) -> Filesystem { + Filesystem::Workspace(AuthorizedWorkspace::new(Some(root), Some(root)).unwrap()) +} + +fn text(r: &CallToolResult) -> String { + r.content.iter().filter_map(|c| c.as_text()).map(|t| t.text.clone()).collect::>().join("\n") +} + +async fn call(c: &Conn, name: &str, args: Value) -> Result { + let mut p = CallToolRequestParams::new(name.to_owned()); + if let Value::Object(m) = args { + p = p.with_arguments(m); + } + c.call_tool(p).await +} + +/// Call a tool and parse its JSON result, failing the test on a tool error. +async fn tool(c: &Conn, name: &str, args: Value) -> Value { + let r = call(c, name, args).await.expect("transport"); + assert_ne!(r.is_error, Some(true), "{name} failed: {}", text(&r)); + serde_json::from_str(&text(&r)).unwrap_or(Value::String(text(&r))) +} + +/// The tool's error text (the test fails if the tool succeeded). +async fn tool_err(c: &Conn, name: &str, args: Value) -> String { + match call(c, name, args).await { + Ok(r) => { + assert_eq!(r.is_error, Some(true), "{name} should fail: {}", text(&r)); + text(&r) + } + Err(e) => e.to_string(), + } +} + +async fn run(c: &Conn, id: &str, params: Value) -> Value { + tool(c, "command_run", json!({"id": id, "params": params})).await +} + +async fn inspect(c: &Conn) -> Value { + tool(c, "session_inspect", json!({})).await +} + +fn top(s: &Value) -> &Value { + &s["layers"][0] +} + +/// Painted tiles of a paint layer's base color. +fn painted(layer: &Value) -> u64 { + layer["painted"].as_array().into_iter().flatten().filter(|p| p["channel"] == "baseColor").map(|p| p["tiles"].as_u64().unwrap()).sum() +} + +/// Sort `painted` lists by channel: their order follows a hash map in the engine. +fn canon(v: &mut Value) { + match v { + Value::Object(m) => { + if let Some(Value::Array(p)) = m.get_mut("painted") { + p.sort_by(|a, b| a["channel"].as_str().cmp(&b["channel"].as_str())); + } + m.values_mut().for_each(canon); + } + Value::Array(a) => a.iter_mut().for_each(canon), + _ => {} + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn agent_completes_texturing_tasks() { + let dir = tmp("tasks"); + let (c, server) = connect(workspace(&dir)).await; + + // 1. New project on a built-in mesh. + let s = tool(&c, "project_new", json!({"mesh": "sphere", "resolution": 256, "name": "Agent"})).await; + assert_eq!(s["project"], "Agent"); + assert!(s["mesh"]["triangles"].as_u64().unwrap() > 1000, "task 1: {}", s["mesh"]); + assert_eq!(s["textureSets"][0]["resolution"], 256); + assert_eq!(top(&s)["kind"], "paint"); + + // 2. Fill layer with a color and roughness. + run(&c, "layer.add.fill", json!({"color": [0.2, 0.4, 0.6], "roughness": 0.2})).await; + let s = inspect(&c).await; + let fill = &top(&s)["fill"]["sources"]; + assert_eq!(top(&s)["kind"], "fill", "task 2: {}", top(&s)); + assert!((fill["roughness"]["uniform"][0].as_f64().unwrap() - 0.2).abs() < 1e-6, "task 2: {fill}"); + assert!((fill["baseColor"]["uniform"][2].as_f64().unwrap() - 0.6).abs() < 1e-6, "task 2: {fill}"); + + // 3. Bake, then a black mask with an edge-wear generator. + tool(&c, "bake", json!({"resolution": 128})).await; + run(&c, "mask.add.black", json!({})).await; + run(&c, "effect.add.generator", json!({"generator": "edgeWear", "level": 0.4})).await; + let s = inspect(&c).await; + assert!(!s["textureSets"][0]["baked"]["maps"].as_array().unwrap().is_empty(), "task 3: {}", s["textureSets"]); + let mask = &top(&s)["mask"]; + assert_eq!(mask["background"], "black", "task 3: {mask}"); + assert_eq!(mask["effects"][0]["kind"], "Generator", "task 3: {mask}"); + assert_eq!(mask["effects"][0]["generator"]["type"], "EdgeWear", "task 3: {mask}"); + assert!((mask["effects"][0]["generator"]["level"].as_f64().unwrap() - 0.4).abs() < 1e-6); + + // 4. Paint a red stroke across the middle of the view on a new paint layer. + run(&c, "layer.add.paint", json!({"name": "Ink"})).await; + let r = tool(&c, "paint_stroke", json!({"points": [[600, 400], [680, 400]], "color": [1, 0, 0], "roughness": 0.9, "size": 10})).await; + assert!(r["stamps"].as_u64().unwrap() > 1, "task 4: {r}"); + let s = inspect(&c).await; + assert_eq!(top(&s)["name"], "Ink"); + let tiles = painted(top(&s)); + assert!(tiles > 0, "task 4: {}", top(&s)); + + // 5. Undo and redo the stroke. + run(&c, "edit.undo", json!({})).await; + assert_eq!(painted(top(&inspect(&c).await)), 0, "task 5: undo"); + run(&c, "edit.redo", json!({})).await; + assert_eq!(painted(top(&inspect(&c).await)), tiles, "task 5: redo"); + + // 6. A smart material lands as a folder of layers. + run(&c, "material.apply", json!({"material": "iron_rusted"})).await; + let s = inspect(&c).await; + assert_eq!(top(&s)["kind"], "group", "task 6: {}", top(&s)); + assert_eq!(top(&s)["name"], "Iron Rusted"); + assert!(!top(&s)["children"].as_array().unwrap().is_empty()); + + // 7. Export with the glTF preset. + let r = tool(&c, "textures_export", json!({"dir": "tex", "preset": "glTF PBR Metal Roughness"})).await; + let files: Vec<&str> = r["files"].as_array().unwrap().iter().map(|f| f.as_str().unwrap()).collect(); + assert!(files.iter().any(|f| f.contains("BaseColor")), "task 7: {r}"); + assert!(files.iter().any(|f| f.contains("OcclusionRoughnessMetallic")), "task 7: {r}"); + for f in &files { + assert!(std::fs::metadata(dir.join(f)).unwrap().len() > 100, "task 7: {f}"); + } + + std::fs::create_dir_all(dir.join("lib")).unwrap(); + // 11. Import an image resource and use it as a fill layer (kept by save/reopen below). + image::RgbaImage::from_fn(32, 32, |x, y| image::Rgba([(x * 8) as u8, (y * 8) as u8, 90, 255])).save(dir.join("grunge.png")).unwrap(); + let r = tool(&c, "resource_import", json!({"path": "grunge.png"})).await; + let name = r["name"].as_str().unwrap().to_string(); + run(&c, "layer.add.image", json!({"name": name})).await; + let s = inspect(&c).await; + // The selected layer is the material's folder, so the new layer goes inside it. + let layer = &top(&s)["children"][0]; + assert_eq!(layer["fill"]["sources"]["baseColor"]["image"], name.as_str(), "task 11: {layer}"); + + // 12. Save the rust folder as a smart material file and load it back as a new folder. + let rust = top(&s)["id"].clone(); + let saved = tool(&c, "material_save", json!({"path": "lib/rust", "layer": rust, "name": "My Rust"})).await; + assert_eq!(saved["path"], "lib/rust.tcmat", "task 12: parent folders must exist: {saved}"); + let loaded = tool(&c, "material_load", json!({"path": "lib/rust.tcmat"})).await["layer"].clone(); + // Added above the selection: inside the selected folder. + let s = inspect(&c).await; + let copy = top(&s)["children"].as_array().unwrap().iter().find(|l| l["id"] == loaded).cloned().unwrap_or_default(); + assert_eq!(copy["name"], "My Rust", "task 12: {}", top(&s)); + assert_eq!(copy["kind"], "group", "task 12: {copy}"); + + // 8. Save, reopen and compare. + let before = inspect(&c).await; + let saved = tool(&c, "project_save", json!({"path": "agent"})).await; + assert_eq!(saved["path"], "agent.tcraft"); + tool(&c, "project_new", json!({"mesh": "cube", "resolution": 128})).await; + let after = tool(&c, "project_open", json!({"path": "agent.tcraft"})).await; + let (mut before, mut after) = (before, after); + canon(&mut before); + canon(&mut after); + for k in ["project", "mesh", "textureSets", "layers"] { + assert_eq!(before[k], after[k], "task 8: `{k}` differs after reopening"); + } + assert_eq!(after["path"], "agent.tcraft"); + assert_eq!(after["modified"], false); + + // 9. Render a PNG (skipped without a GPU adapter). + match call(&c, "viewport_render", json!({"width": 320, "height": 200, "yaw": 30})).await.unwrap() { + r if r.is_error == Some(true) => { + assert!(text(&r).contains("cannot render"), "task 9: {}", text(&r)); + eprintln!("skipping render: {}", text(&r)); + } + r => { + let img = r.content.iter().find_map(|c| c.as_image()).expect("image content"); + assert_eq!(img.mime_type, "image/png"); + let png = base64::engine::general_purpose::STANDARD.decode(&img.data).unwrap(); + let px = image::load_from_memory(&png).unwrap().into_rgba8(); + assert_eq!(px.dimensions(), (320, 200)); + assert_ne!(px.get_pixel(160, 100), px.get_pixel(2, 2), "task 9: the mesh differs from the background"); + } + } + + // 10. A batch stops at the failing step and says which. + let err = tool_err(&c, "command_batch", json!({"steps": [{"id": "layer.add.paint"}, {"id": "no.such.command"}, {"id": "layer.add.paint"}]})).await; + let v: Value = serde_json::from_str(&err).unwrap(); + assert_eq!(v["completed"], 1, "task 10: {v}"); + assert_eq!(v["failed"]["index"], 1, "task 10: {v}"); + + shutdown(c, server).await; + std::fs::remove_dir_all(&dir).unwrap(); +} + +const TOOLS: [&str; 19] = [ + "resource_import", + "material_save", + "material_load", + "session_inspect", + "command_list", + "command_run", + "command_batch", + "project_new", + "project_open", + "project_save", + "mesh_import", + "textures_export", + "bake", + "paint_stroke", + "viewport_render", + "viewport_screenshot", + "ui_screenshot", + "ui_inspect", + "ui_menu_invoke", +]; + +#[tokio::test(flavor = "multi_thread")] +async fn paths_outside_the_workspace_are_rejected() { + let base = tmp("security"); + let (root, outside) = (base.join("root"), base.join("outside")); + std::fs::create_dir_all(&root).unwrap(); + std::fs::create_dir_all(&outside).unwrap(); + let (c, server) = connect(workspace(&root)).await; + tool(&c, "project_save", json!({"path": "ok.tcraft"})).await; + std::fs::copy(root.join("ok.tcraft"), outside.join("secret.tcraft")).unwrap(); + let abs = outside.join("secret.tcraft").display().to_string(); + for path in ["../outside/secret.tcraft", abs.as_str(), "C:/secret.tcraft", "/etc/passwd", "a/../../outside/secret.tcraft", "a\\..\\x.tcraft"] { + tool_err(&c, "project_open", json!({"path": path})).await; + tool_err(&c, "mesh_import", json!({"path": path})).await; + tool_err(&c, "resource_import", json!({"path": path})).await; + tool_err(&c, "material_load", json!({"path": path})).await; + tool_err(&c, "material_save", json!({"path": path})).await; + tool_err(&c, "project_save", json!({"path": path})).await; + } + let e = tool_err(&c, "textures_export", json!({"dir": "../outside/tex"})).await; + assert!(e.contains("rejected"), "{e}"); + assert!(!outside.join("tex").exists()); + for (id, p) in [ + ("file.open", json!({"path": abs})), + ("file.saveAs", json!({"path": abs})), + ("file.save", json!({})), + ("file.exportTextures", json!({"dir": "x"})), + ("resource.import", json!({"path": abs})), + ("material.save", json!({"path": abs})), + ] { + let e = tool_err(&c, "command_run", json!({"id": id, "params": p})).await; + assert!(e.contains("disabled for automation"), "{id}: {e}"); + } + shutdown(c, server).await; + + // Without roots there is no file access at all. + let (c, server) = connect(Filesystem::Denied).await; + assert!(tool_err(&c, "project_save", json!({"path": "x.tcraft"})).await.contains("not granted")); + assert!(tool_err(&c, "project_open", json!({"path": "x.tcraft"})).await.contains("not granted")); + shutdown(c, server).await; + std::fs::remove_dir_all(&base).unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn every_tool_rejects_junk_without_panicking() { + let (c, server) = connect(Filesystem::Denied).await; + let listed: Vec = c.list_all_tools().await.unwrap().into_iter().map(|t| t.name.to_string()).collect(); + for t in TOOLS { + assert!(listed.iter().any(|l| l == t), "missing tool {t}"); + } + let wrong_types = json!({ + "id": 7, "params": "x", "filter": 3, "steps": "x", "mesh": 1, "resolution": "big", "name": [], "path": 5, "dir": false, + "preset": 1, "ts": "nope", "format": 1, "padding": 2, "size": -1, "maps": "x", "points": "x", "viewport": 1, "color": "nope", + "roughness": "x", "metallic": "x", "height": "x", "hardness": "x", "view": 5, "width": -5, "yaw": "x", "pitch": "x" + }); + let hostile = json!({ + "id": "no.such", "params": {"layer": 1e300}, "filter": "\u{0}", "steps": [{"id": "x"}], "mesh": "../../etc", "resolution": 3, "name": "", + "path": "../../x", "dir": "../x", "preset": "nope", "ts": 99, "format": "zzz", "padding": "?", "size": 3, "maps": ["nope"], "points": [], + "viewport": [0, 0], "color": "#zzzzzz", "roughness": 1e30, "metallic": -1e30, "height": 1e30, "hardness": 1e30, "view": "4d", + "width": 99999, "yaw": 1e30, "pitch": -1e30 + }); + for (n, junk) in [Value::Null, wrong_types, hostile].into_iter().enumerate() { + for t in &listed { + // No arguments means defaults for these (a real project, a real bake): not junk. + if n == 0 && ["project_new", "bake", "viewport_render"].contains(&t.as_str()) { + continue; + } + let may_succeed = t == "session_inspect" || (t == "command_list" && n != 1); + let r = call(&c, t, junk.clone()).await; + let failed = r.as_ref().map_or(true, |r| r.is_error == Some(true)); + assert!(failed || may_succeed, "{t} accepted junk #{n}: {:?}", r.map(|r| text(&r))); + } + } + // Still serving. + assert!(inspect(&c).await["layers"].is_array()); + shutdown(c, server).await; +} + +/// Bridge mode against a control-protocol server (the headless one speaks the app's protocol). +#[tokio::test(flavor = "multi_thread")] +async fn bridge_mode_forwards_to_a_control_server() { + let session = texturecraft_engine::Session::new_project("t", texturecraft_engine::geom::primitives::cube(2.0), "builtin:cube", None, 128).unwrap(); + let h = std::sync::Arc::new(std::sync::Mutex::new(Headless::new(session, Filesystem::Denied))); + let token = "bridge-test-token-0123456789".to_string(); + let (tx, rx) = std::sync::mpsc::channel(); + let t = token.clone(); + std::thread::spawn(move || rpc::serve_tcp(0, h, t, |a| tx.send(a.port()).unwrap())); + let port = rx.recv().unwrap(); + + let (s, c) = tokio::io::duplex(1 << 20); + let server = tokio::spawn(async move { + if let Ok(running) = TexturecraftMcp::bridge(BridgeClient::new(port, token)).serve(s).await { + let _ = running.waiting().await; + } + }); + let c = Client.serve(c).await.unwrap(); + run(&c, "layer.add.fill", json!({"roughness": 0.7})).await; + run(&c, "layer.add.paint", json!({})).await; + tool(&c, "paint_stroke", json!({"points": [[640, 400]], "color": "#ff0000"})).await; + let s = inspect(&c).await; + assert_eq!(s["layers"][1]["kind"], "fill"); + assert!(painted(&s["layers"][0]) > 0, "{}", s["layers"][0]); + let b = tool(&c, "command_batch", json!({"steps": [{"id": "layer.add.paint"}, {"id": "edit.undo"}]})).await; + assert_eq!(b["completed"], 2); + assert!(tool_err(&c, "command_run", json!({"id": "file.open", "params": {"path": "/etc/passwd"}})).await.contains("disabled")); + assert!(tool_err(&c, "project_open", json!({"path": "x.tcraft"})).await.contains("headless mode only")); + assert!(tool_err(&c, "ui_inspect", json!({})).await.contains("running app")); + shutdown(c, server).await; + + let (s, c) = tokio::io::duplex(1 << 20); + let server = tokio::spawn(async move { + if let Ok(running) = TexturecraftMcp::bridge(BridgeClient::new(port, "wrong-token-0123456789".into())).serve(s).await { + let _ = running.waiting().await; + } + }); + let c = Client.serve(c).await.unwrap(); + assert!(tool_err(&c, "session_inspect", json!({})).await.contains("authentication failed")); + shutdown(c, server).await; +} diff --git a/crates/bake/Cargo.toml b/crates/bake/Cargo.toml new file mode 100644 index 00000000..9b893562 --- /dev/null +++ b/crates/bake/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "texturecraft-bake" +description = "TextureCraft mesh-map baking: high-poly transfer (cage, match by name) of normal, height, AO, bent normals, curvature, thickness, ID, opacity, color, position and world-space maps" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lints] +workspace = true + +[dependencies] +texturecraft-core = { workspace = true } +texturecraft-geom = { workspace = true } +texturecraft-texel = { workspace = true } +rayon = { workspace = true } +thiserror = { workspace = true } diff --git a/crates/bake/README.md b/crates/bake/README.md new file mode 100644 index 00000000..38b89fe1 --- /dev/null +++ b/crates/bake/README.md @@ -0,0 +1,22 @@ +# texturecraft-bake + +CPU mesh-map baker: `bake(&BakeRequest, &Progress) -> BakeOutput` turns one texture set's UV layout into float maps (0..1, rows top → bottom), supersampled, padded and deterministic. All requested maps are evaluated in one pass per texel (rayon over rows, no per-texel allocation). + +With a high poly (`BakeRequest::high`, every high-definition mesh appended into one), each low-poly sample casts one ray from the cage (`BakeRequest::cage`, same triangles as the low poly) or from `max_frontal` in front of the surface back through it to `max_rear` behind (both relative to the bounding-box diagonal), along normals averaged over hard edges when `average_normals` is on. With `BakeMatch::ByMeshName`, a low-poly part only sees high-poly parts of the same name once `low_suffix` / `high_suffix` are removed (`wheel_low` ↔ `wheel_high`, case-insensitive). Every map but position and world-space direction then reads the high-poly surface that ray found (AO, thickness and bent normals cast against the high poly). + +| Map | Meaning | Range | +| --- | --- | --- | +| Ambient occlusion | How open the surface is to the sky (cosine-weighted rays, distance falloff) | 0 occluded … 1 open | +| Bent normals | Mean unoccluded direction of the AO rays, world space | packed ·0.5+0.5 | +| Thickness | Mean distance through the mesh along inward rays, relative to the max distance | 0 thin … 1 thick | +| Curvature | Mean curvature of the position-welded mesh, or (`curvature_from_normal`) the divergence of the baked normal map | 0 concave … 0.5 flat … 1 convex | +| Normal | Tangent-space normal from the high poly (OpenGL, +Y up); flat without one | packed ·0.5+0.5 | +| Height | High-poly offset along the ray, scaled by frontal + rear distance | 0 rear … 0.5 surface … 1 front | +| World normal | Shading normal in world space | packed ·0.5+0.5 | +| Position | Position inside the low-poly bounding box, per axis | 0 … 1 | +| ID | A distinct hue per mesh part (submesh) | RGB 0 … 1 | +| Opacity | 1 where the high poly was found (times its vertex alpha), 0 where the ray missed | 0 … 1 | +| Color map | The high poly's vertex colors (white without) | RGB 0 … 1 | +| World space direction | Facing ratio towards `direction` | 0 away … 0.5 perpendicular … 1 facing | + +Uncovered texels beyond the padding get `BakedMap::neutral` values. Benchmark (2K, the 7 default maps, per-map breakdown): `cargo test -p texturecraft-bake --release -- --ignored --nocapture bench`. diff --git a/crates/bake/src/lib.rs b/crates/bake/src/lib.rs new file mode 100644 index 00000000..d76ec85f --- /dev/null +++ b/crates/bake/src/lib.rs @@ -0,0 +1,236 @@ +//! # texturecraft-bake +//! +//! CPU mesh-map baking. [`bake`] rasterizes one texture set's UV layout with a supersampled +//! [`UvGBuffer`], evaluates every requested [`BakedMap`] per sample in one pass (rayon over rows, +//! BVH ray casts for the high-poly transfer, AO, bent normals and thickness), box-downsamples, +//! and pads the UV islands with [`DilationMap`]. With a high poly, every map but position and +//! world-space direction reads the high-poly surface found along the cage / frontal-rear ray. +//! Sampling is deterministic, so the same request always gives the same pixels. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +mod maps; +#[cfg(test)] +mod tests; + +use std::collections::BTreeMap; +use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering::Relaxed}; + +use rayon::prelude::*; +use texturecraft_core::{BakeSettings, BakedMap}; +use texturecraft_geom::glam::Vec2; +use texturecraft_geom::{Bvh, Mesh, UvGBuffer}; +use texturecraft_texel::DilationMap; + +/// Largest accepted output resolution (per axis). +pub const MAX_RESOLUTION: u32 = 8192; +/// Largest accepted ray count per texel (AO, thickness). +pub const MAX_RAYS: u32 = 4096; +/// Supersampled texels rasterized at once: bounds the G-buffer at ~200 MB for any resolution × AA. +const STRIP_TEXELS: usize = 1 << 24; + +/// Shared progress / cancellation flag of a running bake. +#[derive(Debug, Default)] +pub struct Progress { + /// Basis points, 0..=10_000 (all requested maps done). + pub done: AtomicU32, + pub cancel: AtomicBool, +} + +impl Progress { + pub fn new() -> Self { + Self::default() + } + + /// Completed fraction, 0..=1. + pub fn fraction(&self) -> f32 { + self.done.load(Relaxed).min(10_000) as f32 / 10_000.0 + } + + /// Ask the bake to stop; it returns [`BakeError::Cancelled`] at the next row. + pub fn cancel(&self) { + self.cancel.store(true, Relaxed); + } +} + +/// A high-poly source mesh (all high-definition meshes appended into one). +#[derive(Clone, Copy, Debug)] +pub struct HighPoly<'a> { + pub mesh: &'a Mesh, + pub bvh: &'a Bvh, +} + +/// One texture set to bake. +#[derive(Clone, Copy, Debug)] +pub struct BakeRequest<'a> { + pub mesh: &'a Mesh, + /// BVH of the whole low-poly mesh: every material occludes. + pub bvh: &'a Bvh, + pub material: usize, + pub settings: &'a BakeSettings, + pub high: Option>, + /// Cage: the low poly inflated (same triangles); transfer rays start on it. + pub cage: Option<&'a Mesh>, +} + +/// Baked maps: row-major `resolution × resolution`, `BakedMap::components()` floats per texel, +/// rows top → bottom (y = v·H, as [`UvGBuffer`]). +#[derive(Clone, Debug, PartialEq)] +pub struct BakeOutput { + pub resolution: u32, + pub maps: BTreeMap>, +} + +#[derive(Debug, thiserror::Error)] +pub enum BakeError { + #[error("bake cancelled")] + Cancelled, + #[error("invalid bake request: {0}")] + Invalid(String), +} + +fn validate(req: &BakeRequest) -> Result<(), BakeError> { + let s = req.settings; + let invalid = |m: String| Err(BakeError::Invalid(m)); + if s.resolution == 0 || s.resolution > MAX_RESOLUTION { + return invalid(format!("resolution {} is not in 1..={MAX_RESOLUTION}", s.resolution)); + } + if ![1, 2, 4, 8].contains(&s.antialiasing) { + return invalid(format!("antialiasing {} is not 1, 2, 4 or 8", s.antialiasing)); + } + if s.rays == 0 || s.rays > MAX_RAYS { + return invalid(format!("ray count {} is not in 1..={MAX_RAYS}", s.rays)); + } + if !(s.max_distance.is_finite() && s.max_distance > 0.0) { + return invalid(format!("max distance {} must be positive", s.max_distance)); + } + if !s.curvature_strength.is_finite() { + return invalid("curvature strength is not finite".into()); + } + if !(s.direction.iter().all(|c| c.is_finite()) && s.direction.iter().any(|c| *c != 0.0)) { + return invalid(format!("direction {:?} must be finite and non-zero", s.direction)); + } + if !(s.max_frontal.is_finite() && s.max_frontal >= 0.0 && s.max_rear.is_finite() && s.max_rear >= 0.0) { + return invalid(format!("frontal/rear distances {}/{} must be finite and ≥ 0", s.max_frontal, s.max_rear)); + } + req.mesh.validate().map_err(|e| BakeError::Invalid(e.to_string()))?; + if req.material >= req.mesh.materials.len().max(1) { + return invalid(format!("material {} out of range ({} materials)", req.material, req.mesh.materials.len())); + } + if let Some(h) = &req.high { + h.mesh.validate().map_err(|e| BakeError::Invalid(format!("high poly: {e}")))?; + } + if let Some(c) = req.cage { + c.validate().map_err(|e| BakeError::Invalid(format!("cage: {e}")))?; + if c.indices.len() != req.mesh.indices.len() { + return invalid(format!("the cage has {} triangles, the low poly {}", c.triangle_count(), req.mesh.triangle_count())); + } + } + Ok(()) +} + +/// `vec![value; n]` that reports allocation failure instead of aborting. +fn alloc(n: usize, value: T) -> Result, BakeError> { + let mut v = Vec::new(); + v.try_reserve_exact(n).map_err(|_| BakeError::Invalid(format!("out of memory allocating {n} texels")))?; + v.resize(n, value); + Ok(v) +} + +/// Bake the maps in `req.settings.maps` for one material of the low-poly mesh. +pub fn bake(req: &BakeRequest, progress: &Progress) -> Result { + bake_in_strips(req, progress, STRIP_TEXELS) +} + +fn bake_in_strips(req: &BakeRequest, progress: &Progress, strip_texels: usize) -> Result { + validate(req)?; + let s = req.settings; + let (res, aa) = (s.resolution as usize, s.antialiasing as usize); + let ctx = maps::Ctx::new(req)?; + progress.done.store(0, Relaxed); + + // Curvature from the normal map needs the normal map: baked alongside when not requested. + let from_normal = s.curvature_from_normal && s.maps.contains(&BakedMap::Curvature); + let mut maps: Vec = s.maps.iter().copied().collect(); + if from_normal && !maps.contains(&BakedMap::Normal) { + maps.push(BakedMap::Normal); + } + let mut bufs = maps.iter().map(|m| alloc(res * res * m.components(), 0.0f32)).collect::, _>>()?; + let mut covered = alloc(res * res, false)?; + let total = res.max(1) as u64; + let rows_done = AtomicU64::new(0); + let strip_err = || BakeError::Invalid("strip out of range".into()); + + // Rasterize in horizontal strips of the supersampled image: a strip's G-buffer is built from + // UVs shifted so that y = v*H - strip_top, which keeps memory bounded at 8K x 8 AA. + let sw = res * aa; + let strip_rows = (strip_texels / (sw * aa)).max(1).min(res); + let mut strip_mesh = Mesh { indices: req.mesh.indices.clone(), submeshes: req.mesh.submeshes.clone(), ..Mesh::default() }; + for y0 in (0..res).step_by(strip_rows) { + let rows = strip_rows.min(res - y0); + let (sy0, sh) = (y0 * aa, rows * aa); + strip_mesh.uvs = req.mesh.uvs.iter().map(|uv| Vec2::new(uv.x, (uv.y * sw as f32 - sy0 as f32) / sh as f32)).collect(); + let g = UvGBuffer::new(&strip_mesh, req.material, sw as u32, sh as u32); + let subsamples = |r: usize, x: usize| (0..aa * aa).map(move |k| (r * aa + k / aa) * sw + x * aa + k % aa); + + // Per output row: its coverage and its slice of every map. + let cov = covered.get_mut(y0 * res..(y0 + rows) * res).ok_or_else(strip_err)?; + let mut out_rows: Vec<(&mut [bool], Vec<&mut [f32]>)> = cov.chunks_mut(res).map(|c| (c, Vec::with_capacity(maps.len()))).collect(); + for (m, data) in maps.iter().zip(bufs.iter_mut()) { + let comps = m.components(); + let strip = data.get_mut(y0 * res * comps..(y0 + rows) * res * comps).ok_or_else(strip_err)?; + for ((_, row), chunk) in out_rows.iter_mut().zip(strip.chunks_mut(res * comps)) { + row.push(chunk); + } + } + out_rows.par_iter_mut().enumerate().try_for_each(|(r, (cov, row))| { + if progress.cancel.load(Relaxed) { + return Err(BakeError::Cancelled); + } + let mut sample = [[0.0f32; 3]; BakedMap::ALL.len()]; + for x in 0..res { + let mut acc = [[0.0f32; 3]; BakedMap::ALL.len()]; + let mut n = 0u32; + for i in subsamples(r, x) { + let (Some(surf), Some(&bary)) = (g.surface(req.mesh, i), g.bary.get(i)) else { continue }; + // Seed by the global supersample index so strips don't change the noise. + ctx.evaluate(&maps, &surf, bary, (sy0 * sw + i) as u64, &mut sample); + for (a, v) in acc.iter_mut().zip(&sample) { + a.iter_mut().zip(v).for_each(|(a, v)| *a += v); + } + n += 1; + } + if let Some(c) = cov.get_mut(x) { + *c = n > 0; + } + for ((m, dst), a) in maps.iter().zip(row.iter_mut()).zip(&acc) { + let comps = m.components(); + let mean = a.map(|a| a / n.max(1) as f32); + let v = if n == 0 || mean.iter().any(|v| !v.is_finite()) { m.neutral() } else { mean }; + if let Some(d) = dst.get_mut(x * comps..(x + 1) * comps) { + d.iter_mut().zip(v).for_each(|(d, v)| *d = v); + } + } + } + let d = rows_done.fetch_add(1, Relaxed) + 1; + progress.done.fetch_max((d.saturating_mul(10_000) / total).min(10_000) as u32, Relaxed); + Ok(()) + })?; + } + + let mut out: BTreeMap> = maps.into_iter().zip(bufs).collect(); + if from_normal { + let normal = if s.maps.contains(&BakedMap::Normal) { out.get(&BakedMap::Normal).cloned() } else { out.remove(&BakedMap::Normal) }; + if let (Some(normal), Some(curv)) = (normal, out.get_mut(&BakedMap::Curvature)) { + maps::curvature_from_normal(&normal, &covered, res, s.curvature_strength, curv); + } + } + if s.dilation > 0 { + let pad = DilationMap::new(&covered, s.resolution, s.resolution, Some(s.dilation)); + for (m, data) in out.iter_mut() { + pad.apply(data, m.components()); + } + } + progress.done.store(10_000, Relaxed); + Ok(BakeOutput { resolution: s.resolution, maps: out }) +} diff --git a/crates/bake/src/maps.rs b/crates/bake/src/maps.rs new file mode 100644 index 00000000..2b069e9f --- /dev/null +++ b/crates/bake/src/maps.rs @@ -0,0 +1,356 @@ +//! Per-sample evaluation of every baked map. + +use std::collections::HashMap; +use std::f32::consts::TAU; +use std::ops::{Add, Mul}; + +use texturecraft_core::{BakeMatch, BakeSettings, BakedMap}; +use texturecraft_geom::glam::{Vec3, Vec4}; +use texturecraft_geom::{Bvh, Mesh, Surface, surface_at}; + +use crate::{BakeError, BakeRequest}; + +/// Triangles that belong to no submesh (never matched by name). +const NO_KEY: u32 = u32::MAX; + +/// A mesh the bakers sample: the low poly, or the high poly detail is transferred from. +struct Source<'a> { + mesh: &'a Mesh, + bvh: &'a Bvh, + /// Per-vertex curvature offset from 0.5 (empty unless mesh curvature is baked). + curvature: Vec, + /// Submesh (part) index of each triangle. + part: Vec, + /// Match key of each triangle ([`BakeMatch::ByMeshName`] only, else empty). + key: Vec, +} + +impl<'a> Source<'a> { + fn new(mesh: &'a Mesh, bvh: &'a Bvh, s: &BakeSettings, diag: f32, suffix: &str, keys: &mut HashMap) -> Source<'a> { + let mesh_curvature = s.maps.contains(&BakedMap::Curvature) && !s.curvature_from_normal; + let tris = mesh.triangle_count(); + let mut part = vec![NO_KEY; tris]; + for (i, sm) in mesh.submeshes.iter().enumerate() { + let (a, b) = ((sm.start / 3) as usize, (sm.start.saturating_add(sm.count) / 3) as usize); + part.get_mut(a.min(b)..b.min(tris)).unwrap_or_default().fill(i as u32); + } + let key = match s.matching { + BakeMatch::Always => Vec::new(), + BakeMatch::ByMeshName => { + let ids: Vec = mesh.submeshes.iter().map(|sm| match_key(&sm.name, suffix, keys)).collect(); + part.iter().map(|p| ids.get(*p as usize).copied().unwrap_or(NO_KEY)).collect() + } + }; + Source { mesh, bvh, curvature: if mesh_curvature { vertex_curvature(mesh, diag, s.curvature_strength) } else { Vec::new() }, part, key } + } + + fn curvature(&self, s: &Surface, bary: [f32; 2]) -> f32 { + (0.5 + interpolate(self.mesh, &self.curvature, s.tri, bary).unwrap_or(0.0)).clamp(0.0, 1.0) + } + + fn color(&self, s: &Surface, bary: [f32; 2]) -> Vec4 { + interpolate(self.mesh, &self.mesh.colors, s.tri, bary).unwrap_or(Vec4::ONE) + } +} + +/// The part name without its low / high suffix (case-insensitive), as a shared id. +fn match_key(name: &str, suffix: &str, keys: &mut HashMap) -> 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) +} + +/// Barycentric interpolation of a per-vertex attribute over triangle `tri`. +fn interpolate + Add>(mesh: &Mesh, v: &[T], tri: u32, [b1, b2]: [f32; 2]) -> Option { + let [a, b, c] = mesh.tri(tri as usize)?; + Some(*v.get(a)? * (1.0 - b1 - b2) + *v.get(b)? * b1 + *v.get(c)? * b2) +} + +/// The high-poly surface found for a low-poly sample, and where along the ray. +struct Transfer { + surf: Surface, + bary: [f32; 2], + /// Hit distance from the ray origin, and the origin's distance in front of the low poly. + t: f32, + front: f32, +} + +/// Everything a sample needs, computed once per bake. +pub(crate) struct Ctx<'a> { + low: Source<'a>, + high: Option>, + cage: Option<&'a Mesh>, + /// Normals averaged over coincident vertices (rays don't split at hard edges); empty when off. + ray_normals: Vec, + lo: Vec3, + extent: Vec3, + /// Ray offset off the surface. + eps: f32, + /// AO / thickness ray length. + dmax: f32, + frontal: f32, + rear: f32, + rays: u32, + direction: Vec3, + curvature_from_normal: bool, + occlusion: bool, +} + +impl<'a> Ctx<'a> { + pub(crate) fn new(req: &BakeRequest<'a>) -> Result, BakeError> { + let s = req.settings; + let (lo, hi) = req.mesh.bounds(); + let diag = (hi - lo).length().max(1e-6); + if !diag.is_finite() { + return Err(BakeError::Invalid("mesh bounds are too large".into())); + } + let mut keys = HashMap::new(); + let low = Source::new(req.mesh, req.bvh, s, diag, &s.low_suffix, &mut keys); + let high = req.high.map(|h| Source::new(h.mesh, h.bvh, s, diag, &s.high_suffix, &mut keys)); + let ray_normals = match s.average_normals && high.is_some() { + true => { + let (weld, _, nrm) = welded(req.mesh, diag); + weld.iter().map(|w| nrm.get(*w as usize).copied().unwrap_or(Vec3::Y)).collect() + } + false => Vec::new(), + }; + Ok(Ctx { + low, + high, + cage: req.cage, + ray_normals, + lo, + extent: hi - lo, + eps: 1e-4 * diag, + dmax: s.max_distance * diag, + frontal: s.max_frontal * diag, + rear: s.max_rear * diag, + rays: s.rays, + direction: Vec3::from(s.direction).try_normalize().unwrap_or(Vec3::Y), + curvature_from_normal: s.curvature_from_normal, + occlusion: s.maps.contains(&BakedMap::AmbientOcclusion) || s.maps.contains(&BakedMap::BentNormal), + }) + } + + /// Every map of `maps` at one low-poly surface sample, into `out` (scalars fill all three). + pub(crate) fn evaluate(&self, maps: &[BakedMap], s: &Surface, bary: [f32; 2], seed: u64, out: &mut [[f32; 3]]) { + let transfer = self.high.as_ref().and_then(|h| self.transfer(h, s, bary)); + // The surface the bakers read: the high poly where a ray found it, else the low poly. + let (src, surf, sb) = match (&self.high, &transfer) { + (Some(h), Some(tr)) => (h, &tr.surf, tr.bary), + _ => (&self.low, s, bary), + }; + let missed = self.high.is_some() && transfer.is_none(); + let (ao, bent) = if self.occlusion { self.occlusion(src, surf, seed) } else { (1.0, surf.nrm) }; + for (&map, o) in maps.iter().zip(out.iter_mut()) { + *o = match map { + BakedMap::AmbientOcclusion => [ao; 3], + BakedMap::BentNormal => pack(bent), + BakedMap::Thickness => [self.thickness(src, surf, seed); 3], + BakedMap::Curvature if self.curvature_from_normal => [0.5; 3], + BakedMap::Curvature => [src.curvature(surf, sb); 3], + BakedMap::WorldNormal => pack(surf.nrm), + BakedMap::Position => ((surf.pos - self.lo) / self.extent.max(Vec3::splat(1e-20))).clamp(Vec3::ZERO, Vec3::ONE).to_array(), + BakedMap::Id => material_color(src.part.get(surf.tri as usize).copied().unwrap_or(0) as usize), + BakedMap::Normal => transfer.as_ref().map_or(map.neutral(), |tr| tangent_normal(s, tr.surf.nrm)), + BakedMap::Height => [transfer.as_ref().map_or(0.5, |tr| (0.5 + (tr.front - tr.t) / (tr.front + self.rear)).clamp(0.0, 1.0)); 3], + BakedMap::Opacity if missed => [0.0; 3], + BakedMap::Opacity => [src.color(surf, sb).w; 3], + BakedMap::ColorMap if missed => map.neutral(), + BakedMap::ColorMap => src.color(surf, sb).truncate().to_array(), + BakedMap::WorldDirection => [surf.nrm.dot(self.direction) * 0.5 + 0.5; 3], + }; + } + } + + /// Cast from the cage (or `frontal` in front of the low poly) back through it to `rear` + /// behind, and take the closest high-poly surface (of the matching part). + fn transfer(&self, high: &Source, s: &Surface, bary: [f32; 2]) -> Option { + let n = interpolate(self.low.mesh, &self.ray_normals, s.tri, bary).and_then(Vec3::try_normalize).unwrap_or(s.nrm); + let cage = self.cage.and_then(|c| interpolate(c, &c.positions, s.tri, bary)).map(|c| s.pos - c).filter(|v| v.length() > self.eps); + let (o, d, front) = match cage { + Some(v) => (s.pos - v, v.normalize(), v.length()), + None => (s.pos + n * self.frontal, -n, self.frontal), + }; + let tmax = front + self.rear; + let hit = match self.low.key.get(s.tri as usize) { + Some(&k) => high.bvh.closest_filtered(o, d, tmax, &|t| k != NO_KEY && high.key.get(t as usize) == Some(&k))?, + None => high.bvh.closest(o, d, tmax)?, + }; + let surf = surface_at(high.mesh, hit.tri, hit.u, hit.v)?; + Some(Transfer { surf, bary: [hit.u, hit.v], t: hit.t, front }) + } + + /// Geometric normal flipped to the side of the shading normal. + fn geo(s: &Surface) -> Vec3 { + if s.geo_nrm.dot(s.nrm) < 0.0 { -s.geo_nrm } else { s.geo_nrm } + } + + /// Call `f` with cosine-weighted directions around `axis` (Hammersley points with a + /// per-sample Cranley–Patterson rotation), skipping those below the plane of `geo`. + fn directions(&self, axis: Vec3, geo: Vec3, seed: u64, mut f: impl FnMut(Vec3)) { + let h = splitmix64(seed); + let (r1, r2) = ((h >> 40) as f32 / (1u64 << 24) as f32, ((h >> 16) & 0xff_ffff) as f32 / (1u64 << 24) as f32); + let (t, b) = axis.any_orthonormal_pair(); + for i in 0..self.rays { + let u1 = (i as f32 / self.rays as f32 + r1).fract(); + let u2 = (i.reverse_bits() as f32 / 4_294_967_296.0 + r2).fract(); + let (phi, r) = (TAU * u1, u2.sqrt()); + let d = t * (r * phi.cos()) + b * (r * phi.sin()) + axis * (1.0 - u2).max(0.0).sqrt(); + if d.dot(geo) > 0.0 { + f(d); + } + } + } + + /// Ambient occlusion (1 = open, 0 = fully occluded) and the bent normal (mean unoccluded + /// direction), from one set of rays. + fn occlusion(&self, src: &Source, s: &Surface, seed: u64) -> (f32, Vec3) { + let geo = Self::geo(s); + let o = s.pos + geo * self.eps; + let (mut occ, mut bent, mut n) = (0.0f32, Vec3::ZERO, 0u32); + self.directions(s.nrm, geo, seed, |d| { + let v = src.bvh.closest(o, d, self.dmax).map_or(0.0, |h| 1.0 - (h.t / self.dmax).powi(2)); + occ += v; + bent += d * (1.0 - v); + n += 1; + }); + let ao = if n == 0 { 1.0 } else { (1.0 - occ / n as f32).clamp(0.0, 1.0) }; + (ao, bent.try_normalize().unwrap_or(s.nrm)) + } + + /// Mean distance through the mesh along inward rays, relative to the max distance (1 = thick). + fn thickness(&self, src: &Source, s: &Surface, seed: u64) -> f32 { + let geo = Self::geo(s); + let o = s.pos - geo * self.eps; + let (mut sum, mut n) = (0.0f32, 0u32); + self.directions(-s.nrm, -geo, seed, |d| { + sum += src.bvh.closest(o, d, self.dmax).map_or(self.dmax, |h| h.t); + n += 1; + }); + if n == 0 { 1.0 } else { (sum / n as f32 / self.dmax).clamp(0.0, 1.0) } + } +} + +/// A unit vector packed to 0..1. +fn pack(v: Vec3) -> [f32; 3] { + (v * 0.5 + 0.5).clamp(Vec3::ZERO, Vec3::ONE).to_array() +} + +/// `n` in the tangent space of low-poly sample `s` (OpenGL: +Y up the image), packed to 0..1. +fn tangent_normal(s: &Surface, n: Vec3) -> [f32; 3] { + let z = s.nrm; + let t = (s.tangent.truncate() - z * z.dot(s.tangent.truncate())).try_normalize().unwrap_or_else(|| z.any_orthonormal_vector()); + let b = z.cross(t) * s.tangent.w; + pack(Vec3::new(t.dot(n), b.dot(n), z.dot(n))) +} + +/// Curvature from a baked tangent-space normal map: the divergence of its XY over `radius` +/// texels (convex = above 0.5). Writes covered texels of `out` (one float per texel). +pub(crate) fn curvature_from_normal(normal: &[f32], covered: &[bool], res: usize, strength: f32, out: &mut [f32]) { + use rayon::prelude::*; + let r = (res / 512).max(1); + let n = |x: usize, y: usize, c: usize, fallback: f32| match covered.get(y * res + x) { + Some(true) => normal.get((y * res + x) * 3 + c).copied().unwrap_or(fallback), + _ => fallback, + }; + out.par_chunks_mut(res).enumerate().for_each(|(y, row)| { + for (x, v) in row.iter_mut().enumerate() { + if covered.get(y * res + x) != Some(&true) { + continue; + } + let (cx, cy) = (n(x, y, 0, 0.5), n(x, y, 1, 0.5)); + let (l, r_) = (n(x.saturating_sub(r), y, 0, cx), if x + r < res { n(x + r, y, 0, cx) } else { cx }); + let (u, d) = (n(x, y.saturating_sub(r), 1, cy), if y + r < res { n(x, y + r, 1, cy) } else { cy }); + // Image rows run down while +Y (OpenGL) points up: convex bumps tilt +Y above. + *v = (0.5 + strength * ((r_ - l) + (u - d))).clamp(0.0, 1.0); + } + }); +} + +/// Position-welded copy of `mesh` (UV seams and hard edges share neighbours): the weld id of +/// every vertex, welded positions and their averaged normals. +fn welded(mesh: &Mesh, diag: f32) -> (Vec, Vec, Vec) { + let q = diag * 1e-5; + let mut ids: HashMap<[i64; 3], u32> = HashMap::new(); + let (mut pos, mut nrm) = (Vec::new(), Vec::::new()); + let weld: Vec = mesh + .positions + .iter() + .zip(&mesh.normals) + .map(|(p, n)| { + let key = [p.x, p.y, p.z].map(|c| (c / q).round() as i64); + let id = *ids.entry(key).or_insert_with(|| { + pos.push(*p); + nrm.push(Vec3::ZERO); + (pos.len() - 1) as u32 + }); + if let Some(acc) = nrm.get_mut(id as usize) { + *acc += *n; + } + id + }) + .collect(); + let nrm = nrm.into_iter().map(|n| n.try_normalize().unwrap_or(Vec3::ZERO)).collect(); + (weld, pos, nrm) +} + +/// Per-vertex curvature offset `−k · avg_edge · strength`, where `k` is the mean of +/// `n·(p_j − p_i)/|p_j − p_i|²` over the 1-ring of the welded mesh. Convex edges give a +/// positive offset. +fn vertex_curvature(mesh: &Mesh, diag: f32, strength: f32) -> Vec { + let q = diag * 1e-5; + let (weld, pos, nrm) = welded(mesh, diag); + let (mut ksum, mut kcnt) = (vec![0.0f32; pos.len()], vec![0u32; pos.len()]); + let (mut edge_sum, mut edge_cnt) = (0.0f64, 0u64); + for t in mesh.indices.chunks_exact(3) { + for (a, b) in [(0, 1), (1, 2), (2, 0), (1, 0), (2, 1), (0, 2)] { + let (Some(&wa), Some(&wb)) = (t.get(a).and_then(|v| weld.get(*v as usize)), t.get(b).and_then(|v| weld.get(*v as usize))) else { continue }; + let (Some(pa), Some(pb), Some(na)) = (pos.get(wa as usize), pos.get(wb as usize), nrm.get(wa as usize)) else { continue }; + let d = *pb - *pa; + let l2 = d.length_squared(); + if wa == wb || l2 <= q * q { + continue; + } + if let (Some(k), Some(c)) = (ksum.get_mut(wa as usize), kcnt.get_mut(wa as usize)) { + *k += na.dot(d) / l2; + *c += 1; + } + edge_sum += f64::from(l2.sqrt()); + edge_cnt += 1; + } + } + let scale = (edge_sum / edge_cnt.max(1) as f64) as f32 * strength; + weld.iter() + .map(|w| { + let i = *w as usize; + let k = ksum.get(i).copied().unwrap_or(0.0) / kcnt.get(i).copied().unwrap_or(0).max(1) as f32; + if k.is_finite() { -k * scale } else { 0.0 } + }) + .collect() +} + +/// A well-separated hue per part (golden-ratio steps around the HSV wheel). +fn material_color(part: usize) -> [f32; 3] { + let h = ((part as f64 * 0.618_033_988_75 + 0.05).fract() * 6.0) as f32; + let (s, v) = (0.7f32, 0.95f32); + let f = h.fract(); + let (p, q, t) = (v * (1.0 - s), v * (1.0 - s * f), v * (1.0 - s * (1.0 - f))); + match h as u32 { + 0 => [v, t, p], + 1 => [q, v, p], + 2 => [p, v, t], + 3 => [p, q, v], + 4 => [t, p, v], + _ => [v, p, q], + } +} + +fn splitmix64(x: u64) -> u64 { + let mut z = x.wrapping_add(0x9e37_79b9_7f4a_7c15); + z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9); + z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb); + z ^ (z >> 31) +} diff --git a/crates/bake/src/tests.rs b/crates/bake/src/tests.rs new file mode 100644 index 00000000..74a54025 --- /dev/null +++ b/crates/bake/src/tests.rs @@ -0,0 +1,423 @@ +use std::sync::atomic::Ordering::Relaxed; + +use texturecraft_core::BakeMatch; +use texturecraft_geom::glam::{Vec2, Vec3, Vec4}; +use texturecraft_geom::primitives; + +use super::*; + +fn settings(res: u32, maps: &[BakedMap]) -> BakeSettings { + BakeSettings { resolution: res, dilation: 0, rays: 64, maps: maps.iter().copied().collect(), ..BakeSettings::default() } +} + +fn run(mesh: &Mesh, s: &BakeSettings) -> Result { + let bvh = Bvh::new(mesh); + bake(&BakeRequest { mesh, bvh: &bvh, material: 0, settings: s, high: None, cage: None }, &Progress::new()) +} + +/// Values of `map` (first component) at texels the UV layout covers, with their surface positions. +fn covered_values(mesh: &Mesh, out: &BakeOutput, map: BakedMap) -> Vec<(Vec3, f32)> { + let res = out.resolution; + let g = UvGBuffer::new(mesh, 0, res, res); + let data = &out.maps[&map]; + (0..(res * res) as usize).filter_map(|i| g.surface(mesh, i).map(|s| (s.pos, data[i * map.components()]))).collect() +} + +fn mean(v: impl Iterator) -> f32 { + let (s, n) = v.fold((0.0, 0), |(s, n), x| (s + x, n + 1)); + assert!(n > 0, "no samples"); + s / n as f32 +} + +fn quad() -> Mesh { + Mesh::from_parts( + vec![Vec3::new(-1.0, 0.0, -1.0), Vec3::new(1.0, 0.0, -1.0), Vec3::new(1.0, 0.0, 1.0), Vec3::new(-1.0, 0.0, 1.0)], + Some(vec![Vec3::Y; 4]), + vec![Vec2::new(0.0, 0.0), Vec2::new(1.0, 0.0), Vec2::new(1.0, 1.0), Vec2::new(0.0, 1.0)], + vec![0, 2, 1, 0, 3, 2], + "Quad", + ) + .unwrap() +} + +#[test] +fn open_quad_is_unoccluded() { + let m = quad(); + let out = run(&m, &settings(32, &[BakedMap::AmbientOcclusion])).unwrap(); + let ao = covered_values(&m, &out, BakedMap::AmbientOcclusion); + assert!(ao.iter().all(|(_, v)| *v > 0.999), "{:?}", ao.iter().map(|v| v.1).fold(1.0f32, f32::min)); +} + +#[test] +fn inside_of_a_closed_box_is_occluded() { + let mut m = primitives::cube(2.0); + for t in m.indices.chunks_exact_mut(3) { + t.swap(1, 2); + } + for n in &mut m.normals { + *n = -*n; + } + m.compute_tangents(); + let s = BakeSettings { max_distance: 4.0, ..settings(48, &[BakedMap::AmbientOcclusion]) }; + let out = run(&m, &s).unwrap(); + let ao = mean(covered_values(&m, &out, BakedMap::AmbientOcclusion).into_iter().map(|v| v.1)); + assert!(ao < 0.3, "{ao}"); +} + +#[test] +fn thin_slab_is_thinner_than_a_block() { + let thick = |m: &Mesh| mean(covered_values(m, &run(m, &settings(48, &[BakedMap::Thickness])).unwrap(), BakedMap::Thickness).into_iter().map(|v| v.1)); + let slab = thick(&primitives::rounded_box(Vec3::new(1.0, 0.05, 1.0), 0.0)); + let block = thick(&primitives::cube(2.0)); + assert!(slab < 0.4 && block > 0.6, "slab {slab}, block {block}"); +} + +#[test] +fn sphere_curvature_is_nearly_constant() { + let m = primitives::sphere(1.0, 64, 32); + let out = run(&m, &settings(64, &[BakedMap::Curvature])).unwrap(); + let c: Vec = covered_values(&m, &out, BakedMap::Curvature).into_iter().map(|v| v.1).collect(); + let (lo, hi) = c.iter().fold((1.0f32, 0.0f32), |(lo, hi), v| (lo.min(*v), hi.max(*v))); + assert!(lo >= 0.5 && hi < 0.6 && hi - lo < 0.03, "{lo}..{hi}"); +} + +/// Bevel / edge texels (two coordinates near the half extent) are convex-bright; face centres +/// stay mid-grey. +fn check_box_curvature(m: &Mesh, edge: f32) { + let s = BakeSettings { dilation: 4, ..settings(128, &[BakedMap::Curvature]) }; + let c = covered_values(m, &run(m, &s).unwrap(), BakedMap::Curvature); + let near = |p: Vec3, t: f32| p.abs().to_array().iter().filter(|c| **c > t).count(); + let edges: Vec = c.iter().filter(|(p, _)| near(*p, edge) >= 2).map(|v| v.1).collect(); + let faces: Vec = c.iter().filter(|(p, _)| near(*p, 0.5) == 1).map(|v| v.1).collect(); + assert!(!edges.is_empty() && !faces.is_empty()); + let min_edge = edges.iter().copied().fold(1.0, f32::min); + assert!(min_edge > 0.55, "edge min {min_edge}"); + assert!(faces.iter().all(|v| (0.45..=0.55).contains(v)), "face {:?}", faces.iter().copied().fold((1.0f32, 0.0f32), |(a, b), v| (a.min(v), b.max(v)))); +} + +#[test] +fn rounded_box_bevels_are_convex() { + check_box_curvature(&primitives::rounded_box(Vec3::ONE, 0.15), 0.9); +} + +#[test] +fn cube_edges_are_convex() { + check_box_curvature(&primitives::cube(2.0), 0.97); +} + +#[test] +fn two_runs_are_identical_and_sized() { + let m = primitives::material_ball(); + let s = BakeSettings { antialiasing: 2, dilation: 2, rays: 8, maps: BakedMap::ALL.into_iter().collect(), ..settings(40, &[]) }; + let a = run(&m, &s).unwrap(); + let b = run(&m, &s).unwrap(); + assert_eq!(a, b); + assert_eq!(a.maps.len(), BakedMap::ALL.len()); + for (map, data) in &a.maps { + assert_eq!(data.len(), 40 * 40 * map.components(), "{map:?}"); + assert!(data.iter().all(|v| v.is_finite() && (0.0..=1.0).contains(v)), "{map:?}"); + } +} + +#[test] +fn strips_match_a_single_pass() { + let m = primitives::material_ball(); + let bvh = Bvh::new(&m); + let s = BakeSettings { antialiasing: 2, rays: 4, maps: BakedMap::ALL.into_iter().collect(), ..settings(64, &[]) }; + let req = BakeRequest { mesh: &m, bvh: &bvh, material: 0, settings: &s, high: None, cage: None }; + let whole = bake(&req, &Progress::new()).unwrap(); + // 128 × 2 supersampled texels per strip = 1 output row: 64 strips. + let strips = bake_in_strips(&req, &Progress::new(), 128 * 2).unwrap(); + for (map, a) in &whole.maps { + let b = &strips.maps[map]; + let off = a.iter().zip(b).filter(|(a, b)| (*a - *b).abs() > 1e-3).count(); + assert!(off * 200 < a.len(), "{map:?}: {off} of {} differ", a.len()); + } +} + +#[test] +fn cancel_and_progress() { + let m = primitives::sphere(1.0, 16, 8); + let bvh = Bvh::new(&m); + let s = settings(16, &[BakedMap::AmbientOcclusion, BakedMap::Position]); + let req = BakeRequest { mesh: &m, bvh: &bvh, material: 0, settings: &s, high: None, cage: None }; + let p = Progress::new(); + p.cancel(); + assert!(matches!(bake(&req, &p), Err(BakeError::Cancelled))); + let p = Progress::new(); + bake(&req, &p).unwrap(); + assert_eq!(p.done.load(Relaxed), 10_000); + assert_eq!(p.fraction(), 1.0); +} + +#[test] +fn invalid_requests_are_errors() { + let m = quad(); + let ok = settings(8, &[BakedMap::AmbientOcclusion]); + let bad = [ + BakeSettings { resolution: 0, ..ok.clone() }, + BakeSettings { resolution: 8193, ..ok.clone() }, + BakeSettings { antialiasing: 3, ..ok.clone() }, + BakeSettings { antialiasing: 0, ..ok.clone() }, + BakeSettings { rays: 0, ..ok.clone() }, + BakeSettings { max_distance: f32::NAN, ..ok.clone() }, + BakeSettings { max_distance: 0.0, ..ok.clone() }, + BakeSettings { max_rear: -1.0, ..ok.clone() }, + BakeSettings { curvature_strength: f32::INFINITY, ..ok.clone() }, + ]; + for s in &bad { + assert!(matches!(run(&m, s), Err(BakeError::Invalid(_))), "{s:?}"); + } + let bvh = Bvh::new(&m); + let req = |mesh: &Mesh, material| bake(&BakeRequest { mesh, bvh: &bvh, material, settings: &ok, high: None, cage: None }, &Progress::new()); + assert!(matches!(req(&m, 1), Err(BakeError::Invalid(_)))); + let mut broken = m.clone(); + broken.indices[0] = 99; + assert!(matches!(req(&broken, 0), Err(BakeError::Invalid(_)))); + let mut nan = m.clone(); + nan.positions[0].x = f32::NAN; + assert!(matches!(req(&nan, 0), Err(BakeError::Invalid(_)))); + // An empty mesh bakes to neutral values. + let empty = Mesh { materials: vec!["m".into()], ..Mesh::default() }; + let out = req(&empty, 0).unwrap(); + assert!(out.maps[&BakedMap::AmbientOcclusion].iter().all(|v| *v == 1.0)); +} + +#[test] +fn neutral_values_and_padding() { + // The quad covers only the left half of UV space. + let mut m = quad(); + for uv in &mut m.uvs { + uv.x *= 0.5; + } + let maps = [BakedMap::AmbientOcclusion, BakedMap::Curvature, BakedMap::Thickness, BakedMap::Normal, BakedMap::Position]; + let out = run(&m, &BakeSettings { dilation: 2, ..settings(16, &maps) }).unwrap(); + let pos = &out.maps[&BakedMap::Position]; + let at = |x: usize, y: usize| &pos[(y * 16 + x) * 3..(y * 16 + x) * 3 + 3]; + assert_eq!(at(9, 5)[0], at(7, 5)[0], "padded texel copies a texel of the last covered column"); + assert_eq!(at(12, 5), [0.0, 0.0, 0.0], "beyond the padding"); + assert_eq!(out.maps[&BakedMap::AmbientOcclusion][5 * 16 + 12], 1.0); + assert_eq!(out.maps[&BakedMap::Curvature][5 * 16 + 12], 0.5); + assert_eq!(out.maps[&BakedMap::Thickness][5 * 16 + 12], 1.0); + assert_eq!(&out.maps[&BakedMap::Normal][(5 * 16 + 12) * 3..(5 * 16 + 12) * 3 + 3], [0.5, 0.5, 1.0]); +} + +#[test] +fn normal_and_height_transfer_from_a_high_poly() { + let low = quad(); + let mut high = quad(); + for p in &mut high.positions { + p.y += 0.005; + } + let tilt = Vec3::new(0.3, 1.0, 0.0).normalize(); + high.normals = vec![tilt; 4]; + high.compute_tangents(); + let (lb, hb) = (Bvh::new(&low), Bvh::new(&high)); + let s = settings(16, &[BakedMap::Normal, BakedMap::Height]); + let out = + bake(&BakeRequest { mesh: &low, bvh: &lb, material: 0, settings: &s, high: Some(HighPoly { mesh: &high, bvh: &hb }), cage: None }, &Progress::new()) + .unwrap(); + let i = 8 * 16 + 8; + let n = &out.maps[&BakedMap::Normal][i * 3..i * 3 + 3]; + // Tangent = +X (u), bitangent = −Z (up the image), normal = +Y. + assert!((n[0] - (0.5 + 0.5 * tilt.x)).abs() < 1e-3 && (n[1] - 0.5).abs() < 1e-3 && (n[2] - (0.5 + 0.5 * tilt.y)).abs() < 1e-3, "{n:?}"); + let diag = 8.0f32.sqrt(); + let expected = 0.5 + 0.005 / (0.02 * diag); + assert!((out.maps[&BakedMap::Height][i] - expected).abs() < 1e-3, "{}", out.maps[&BakedMap::Height][i]); + // Without a high poly both maps are flat. + let flat = run(&low, &s).unwrap(); + assert_eq!(&flat.maps[&BakedMap::Normal][i * 3..i * 3 + 3], [0.5, 0.5, 1.0]); + assert_eq!(flat.maps[&BakedMap::Height][i], 0.5); +} + +fn transfer(low: &Mesh, high: &Mesh, cage: Option<&Mesh>, s: &BakeSettings) -> Result { + let (lb, hb) = (Bvh::new(low), Bvh::new(high)); + bake(&BakeRequest { mesh: low, bvh: &lb, material: 0, settings: s, high: Some(HighPoly { mesh: high, bvh: &hb }), cage }, &Progress::new()) +} + +/// Texel (x, y) of a `res`² bake of [`quad`] sits over world (2u − 1, 0, 2v − 1). +fn quad_texel(res: u32, x: f32, z: f32) -> usize { + let t = |c: f32| (((c + 1.0) * 0.5 * res as f32) as usize).min(res as usize - 1); + t(z) * res as usize + t(x) +} + +/// [`quad`]'s footprint with a smooth bump `h · exp(−r²/σ²)`. +fn bumped(h: f32, sigma: f32) -> Mesh { + let mut m = primitives::plane(2.0, 128); + for p in &mut m.positions { + p.y = h * (-(p.x * p.x + p.z * p.z) / (sigma * sigma)).exp(); + } + m.compute_normals(); + m.compute_tangents(); + m +} + +#[test] +fn bump_tilts_the_baked_normal() { + let (h, sigma) = (0.1f32, 0.4f32); + let s = BakeSettings { max_frontal: 0.06, max_rear: 0.02, ..settings(64, &[BakedMap::Normal, BakedMap::Height]) }; + let out = transfer(&quad(), &bumped(h, sigma), None, &s).unwrap(); + let normal = &out.maps[&BakedMap::Normal]; + for (x, z) in [(0.3f32, 0.0f32), (-0.3, 0.0), (0.0, 0.3), (0.0, -0.3), (0.25, 0.25)] { + let i = quad_texel(64, x, z); + // Analytic normal of the bump at the texel centre. + let (cx, cz) = (((i % 64) as f32 + 0.5) / 32.0 - 1.0, ((i / 64) as f32 + 0.5) / 32.0 - 1.0); + let f = h * (-(cx * cx + cz * cz) / (sigma * sigma)).exp(); + let (dx, dz) = (-2.0 * cx / (sigma * sigma) * f, -2.0 * cz / (sigma * sigma) * f); + let n = Vec3::new(-dx, 1.0, -dz).normalize(); + // Tangent = +X, bitangent = −Z, normal = +Y. + let expected = [0.5 + 0.5 * n.x, 0.5 - 0.5 * n.z, 0.5 + 0.5 * n.y]; + let got = &normal[i * 3..i * 3 + 3]; + assert!(got.iter().zip(expected).all(|(g, e)| (g - e).abs() < 0.02), "at {x},{z}: {got:?} vs {expected:?}"); + assert!((got[0] - 0.5) * x >= 0.0 && (got[1] - 0.5) * -z >= 0.0, "tilts away from the bump centre at {x},{z}: {got:?}"); + } + let height = &out.maps[&BakedMap::Height]; + assert!(height[quad_texel(64, 0.0, 0.0)] > height[quad_texel(64, 0.6, 0.6)] + 0.2, "the bump top is higher"); +} + +#[test] +fn curvature_from_the_normal_map_sees_the_bump() { + let s = BakeSettings { max_frontal: 0.06, max_rear: 0.02, curvature_from_normal: true, ..settings(64, &[BakedMap::Curvature]) }; + let out = transfer(&quad(), &bumped(0.1, 0.4), None, &s).unwrap(); + assert!(!out.maps.contains_key(&BakedMap::Normal), "the helper normal map is not returned"); + let c = &out.maps[&BakedMap::Curvature]; + assert!(c[quad_texel(64, 0.0, 0.0)] > 0.55, "convex top {}", c[quad_texel(64, 0.0, 0.0)]); + assert!((c[quad_texel(64, 0.9, 0.9)] - 0.5).abs() < 0.02, "flat corner {}", c[quad_texel(64, 0.9, 0.9)]); +} + +/// Two unit boxes 0.2 apart: "A" (material 0, centre x = −0.6) and "B" (material 1, x = +0.6). +fn two_boxes(suffix: &str, colors: Option<[Vec4; 2]>) -> Mesh { + let part = |name: &str, x: f32, color: Option| { + let mut b = primitives::cube(1.0); + b.positions.iter_mut().for_each(|p| p.x += x); + b.submeshes[0].name = format!("{name}{suffix}"); + b.colors = color.map(|c| vec![c; b.positions.len()]).unwrap_or_default(); + b + }; + let mut m = part("A", -0.6, colors.map(|c| c[0])); + m.append(&part("B", 0.6, colors.map(|c| c[1]))); + m +} + +#[test] +fn match_by_mesh_name_pairs_the_right_parts() { + let (red, blue) = (Vec4::new(1.0, 0.0, 0.0, 1.0), Vec4::new(0.0, 0.0, 1.0, 1.0)); + let (low, high) = (two_boxes("_low", None), two_boxes("_HIGH", Some([red, blue]))); + // Rays start 1.3 in front of A's +X face: beyond B, so B's high poly is hit first unless + // parts are matched by name. + let s = BakeSettings { max_frontal: 0.5, ..settings(64, &[BakedMap::ColorMap]) }; + let face = |s: &BakeSettings| { + let out = transfer(&low, &high, None, s).unwrap(); + covered_values(&low, &out, BakedMap::ColorMap) + .into_iter() + .filter(|(p, _)| p.x > -0.105 && p.y.abs() < 0.4 && p.z.abs() < 0.4) + .map(|v| v.1) + .collect::>() + }; + let always = face(&s); + assert!(!always.is_empty() && always.iter().all(|r| *r < 0.01), "always: B's (blue) high poly occludes A's face"); + let by_name = face(&BakeSettings { matching: BakeMatch::ByMeshName, ..s.clone() }); + assert!(by_name.iter().all(|r| *r > 0.99), "by name: A's face gets A's red"); + // Parts without a counterpart get nothing. + let lonely = BakeSettings { matching: BakeMatch::ByMeshName, low_suffix: "_none".into(), ..s }; + assert!(face(&lonely).iter().all(|r| *r == 0.0)); +} + +#[test] +fn bent_normals_point_away_from_occluders() { + // A floor (material 0) with a wall standing at x = +1, facing −X (material 1). + let mut m = quad(); + let wall = Mesh::from_parts( + vec![Vec3::new(1.0, 0.0, -1.0), Vec3::new(1.0, 0.0, 1.0), Vec3::new(1.0, 2.0, 1.0), Vec3::new(1.0, 2.0, -1.0)], + Some(vec![-Vec3::X; 4]), + vec![Vec2::ZERO, Vec2::X, Vec2::ONE, Vec2::Y], + vec![0, 2, 1, 0, 3, 2], + "Wall", + ) + .unwrap(); + m.append(&wall); + let s = BakeSettings { max_distance: 2.0, ..settings(32, &[BakedMap::BentNormal, BakedMap::AmbientOcclusion]) }; + let out = run(&m, &s).unwrap(); + let bent = &out.maps[&BakedMap::BentNormal]; + let ao = covered_values(&m, &out, BakedMap::AmbientOcclusion); + let g = UvGBuffer::new(&m, 0, 32, 32); + let near: Vec<&[f32]> = (0..32 * 32).filter(|i| g.surface(&m, *i).is_some_and(|s| s.pos.x > 0.6)).map(|i| &bent[i * 3..i * 3 + 3]).collect(); + let far: Vec<&[f32]> = (0..32 * 32).filter(|i| g.surface(&m, *i).is_some_and(|s| s.pos.x < -0.6)).map(|i| &bent[i * 3..i * 3 + 3]).collect(); + let mean_x = |v: &[&[f32]]| mean(v.iter().map(|b| b[0] * 2.0 - 1.0)); + assert!(mean_x(&near) < -0.15, "near the wall the bent normal leans away from it: {}", mean_x(&near)); + assert!(mean_x(&near) < mean_x(&far), "{} vs {}", mean_x(&near), mean_x(&far)); + assert!(near.iter().all(|b| b[1] > 0.6), "still points up"); + let ao_near = mean(ao.iter().filter(|(p, _)| p.x > 0.6).map(|v| v.1)); + assert!(ao_near < mean(ao.iter().filter(|(p, _)| p.x < -0.6).map(|v| v.1))); +} + +#[test] +fn opacity_and_color_transfer_where_the_high_poly_is() { + // The high poly covers x < 0 only, colored green. + let mut high = quad(); + high.positions.iter_mut().for_each(|p| p.x = p.x.min(0.0)); + high.colors = vec![Vec4::new(0.0, 1.0, 0.0, 0.5); 4]; + let out = transfer(&quad(), &high, None, &settings(32, &[BakedMap::Opacity, BakedMap::ColorMap])).unwrap(); + let (op, col) = (&out.maps[&BakedMap::Opacity], &out.maps[&BakedMap::ColorMap]); + let (left, right) = (quad_texel(32, -0.5, 0.0), quad_texel(32, 0.5, 0.0)); + assert_eq!((op[left], op[right]), (0.5, 0.0), "vertex alpha where covered, 0 where missed"); + assert_eq!(&col[left * 3..left * 3 + 3], [0.0, 1.0, 0.0]); + assert_eq!(&col[right * 3..right * 3 + 3], BakedMap::ColorMap.neutral()); + // Without a high poly the low poly covers itself (white, opaque). + let own = run(&quad(), &settings(32, &[BakedMap::Opacity, BakedMap::ColorMap])).unwrap(); + assert_eq!((own.maps[&BakedMap::Opacity][right], own.maps[&BakedMap::ColorMap][right * 3]), (1.0, 1.0)); +} + +#[test] +fn ray_distances_and_the_cage_limit_hits() { + let low = quad(); + let mut high = quad(); + high.positions.iter_mut().for_each(|p| p.y += 0.2); + let diag = 8.0f32.sqrt(); + let opacity = |s: &BakeSettings, cage: Option<&Mesh>| transfer(&low, &high, cage, s).map(|o| o.maps[&BakedMap::Opacity][quad_texel(16, 0.0, 0.0)]); + let near = settings(16, &[BakedMap::Opacity]); + assert_eq!(opacity(&near, None).unwrap(), 0.0, "0.2 above is beyond the default frontal distance"); + assert_eq!(opacity(&BakeSettings { max_frontal: 0.25 / diag, ..near.clone() }, None).unwrap(), 1.0); + // The rear distance only reaches below the surface. + let mut below = high.clone(); + below.positions.iter_mut().for_each(|p| p.y -= 0.4); + let rear = |r: f32| transfer(&low, &below, None, &BakeSettings { max_rear: r, ..near.clone() }).unwrap().maps[&BakedMap::Opacity][quad_texel(16, 0.0, 0.0)]; + assert_eq!((rear(0.1 / diag), rear(0.25 / diag)), (0.0, 1.0)); + // A cage 0.3 above the low poly starts the rays above the high poly. + let mut cage = low.clone(); + cage.positions.iter_mut().for_each(|p| p.y += 0.3); + assert_eq!(opacity(&near, Some(&cage)).unwrap(), 1.0); + // A cage 0.1 above starts below it. + cage.positions.iter_mut().for_each(|p| p.y -= 0.2); + assert_eq!(opacity(&near, Some(&cage)).unwrap(), 0.0); + // The cage must have the low poly's triangles. + assert!(matches!(opacity(&near, Some(&primitives::cube(1.0))), Err(BakeError::Invalid(_)))); +} + +#[test] +fn world_space_direction_is_a_facing_ratio() { + let m = quad(); + let at = + |d: [f32; 3]| run(&m, &BakeSettings { direction: d, ..settings(8, &[BakedMap::WorldDirection]) }).map(|o| o.maps[&BakedMap::WorldDirection][4 * 8 + 4]); + assert_eq!(at([0.0, 2.0, 0.0]).unwrap(), 1.0); + assert_eq!(at([1.0, 0.0, 0.0]).unwrap(), 0.5); + assert_eq!(at([0.0, -1.0, 0.0]).unwrap(), 0.0); + assert!(matches!(at([0.0; 3]), Err(BakeError::Invalid(_)))); +} + +/// `cargo test -p texturecraft-bake --release -- --ignored --nocapture bench` +#[test] +#[ignore] +fn bench_material_ball_2k() { + let m = primitives::material_ball(); + let bvh = Bvh::new(&m); + let all = BakeSettings { resolution: 2048, ..BakeSettings::default() }; + for maps in + [vec![BakedMap::Position], vec![BakedMap::AmbientOcclusion], vec![BakedMap::Thickness], vec![BakedMap::Curvature], all.maps.iter().copied().collect()] + { + let s = BakeSettings { maps: maps.into_iter().collect(), ..all.clone() }; + let t = std::time::Instant::now(); + let out = bake(&BakeRequest { mesh: &m, bvh: &bvh, material: 0, settings: &s, high: None, cage: None }, &Progress::new()).unwrap(); + println!("material ball, material 0, {}² × {:?}, {} rays: {:.2?}", out.resolution, out.maps.keys(), s.rays, t.elapsed()); + } +} diff --git a/crates/compose/Cargo.toml b/crates/compose/Cargo.toml new file mode 100644 index 00000000..a2ea4edb --- /dev/null +++ b/crates/compose/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "texturecraft-compose" +description = "TextureCraft layer-stack compositor (CPU reference), patterns, UV padding, display conversion" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lints] +workspace = true + +[dependencies] +texturecraft-core = { workspace = true } +texturecraft-texel = { workspace = true } +texturecraft-geom = { workspace = true } +rayon = { workspace = true } +half = { workspace = true } diff --git a/crates/compose/src/below.rs b/crates/compose/src/below.rs new file mode 100644 index 00000000..8d3db8bd --- /dev/null +++ b/crates/compose/src/below.rs @@ -0,0 +1,222 @@ +//! A per-tile cache of the composited stack below one "hot" layer (the layer being painted or +//! edited), so recompositing a tile only evaluates that layer and the layers above it. The +//! cache is keyed by everything the cached part reads; any change to it empties the cache, so +//! results are always identical to a full evaluation. + +use std::collections::{BTreeSet, HashMap, VecDeque}; +use std::sync::Arc; + +use texturecraft_core::{BakedMap, Channel, Layer, LayerId, LayerKind, Project, TextureSet}; +use texturecraft_texel::TileImage; + +use crate::eval::{Inputs, PaintTarget}; +use crate::store::{BakedMaps, ImageStore, PixelStore}; + +/// Default byte budget of a [`BelowCache`]. +pub const BELOW_BUDGET: usize = 256 << 20; + +/// See the module docs. One per texture set. +#[derive(Debug)] +pub struct BelowCache { + hot: Option, + key: Option, + tiles: HashMap<(u32, u32), Vec>, + /// Insertion order, oldest first (eviction beyond the budget). + order: VecDeque<(u32, u32)>, + bytes: usize, + /// Byte budget; the oldest tiles are dropped beyond it. + pub budget: usize, + /// 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, +} + +impl Default for BelowCache { + fn default() -> Self { + BelowCache { hot: None, key: None, tiles: HashMap::new(), order: VecDeque::new(), bytes: 0, budget: BELOW_BUDGET, pass: 0 } + } +} + +/// Everything compositing `roots` of a texture set reads: equal keys composite identically. +/// Pixels are compared by tile identity (copy-on-write), so building and comparing keys is cheap. +#[derive(Debug, PartialEq)] +pub struct StackKey { + /// The texture set without its name, description, stack and bake settings. + set: TextureSet, + roots: Vec, + /// The layers drawn, without names and other display-only fields. + layers: Vec, + pixels: Vec<(LayerId, Option, Shared)>, + baked: (u32, Vec<(BakedMap, usize)>), + images: Vec<(String, usize)>, +} + +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 { + let mut set = project.texture_set(ts).ok()?.clone(); + set.name.clear(); + set.description.clear(); + set.stack.clear(); + set.bake = Default::default(); + let tree = project.render_tree(roots); + let effects: BTreeSet = tree.iter().filter_map(|id| project.layers.get(id)).flat_map(effect_ids).collect(); + let pixels = tree + .iter() + .chain(&effects) + .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())))) + }) + .collect(); + let mut images: Vec<(String, usize)> = images.iter().map(|(n, i)| (n.clone(), Arc::as_ptr(i) as usize)).collect(); + images.sort(); + Some(StackKey { + set, + roots: roots.to_vec(), + 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, + }) + } +} + +/// An image compared by tile identity: holding the clone keeps its tiles alive, and a tile +/// rewritten since is a different allocation (copy-on-write), so pointer equality means the +/// same pixels without reading them. +#[derive(Debug)] +struct Shared(TileImage); + +impl PartialEq for Shared { + fn eq(&self, other: &Self) -> bool { + let (a, b) = (&self.0, &other.0); + a.width() == b.width() + && a.height() == b.height() + && a.comps() == b.comps() + && (0..a.tiles_y()).all(|ty| { + (0..a.tiles_x()).all(|tx| match (a.tile_handle(tx, ty), b.tile_handle(tx, ty)) { + (None, None) => true, + (Some(x), Some(y)) => Arc::ptr_eq(x, y), + _ => false, + }) + }) + } +} + +/// Ids of a layer's mask and content effects. +fn effect_ids(l: &Layer) -> impl Iterator + '_ { + l.mask.iter().flat_map(|m| m.effects.iter()).chain(l.content_effects.iter()).map(|e| e.id) +} + +/// A layer as far as compositing is concerned (names, color tags and folding don't matter). +fn render_view(l: &Layer) -> Layer { + let mut l = l.clone(); + l.name.clear(); + l.color_tag = None; + if let LayerKind::Group { collapsed, .. } = &mut l.kind { + *collapsed = false; + } + l +} + +impl BelowCache { + /// Cache the stack below `hot` from now on (a different layer empties the cache). + pub fn set_hot(&mut self, hot: Option) { + if self.hot != hot { + self.hot = hot; + self.clear(); + } + } + + pub fn clear(&mut self) { + self.tiles.clear(); + self.order.clear(); + self.bytes = 0; + self.key = None; + } + + pub fn bytes(&self) -> usize { + self.bytes + } + + pub fn len(&self) -> usize { + self.tiles.len() + } + + pub fn is_empty(&self) -> bool { + self.tiles.is_empty() + } + + pub(crate) fn tile(&self, t: (u32, u32)) -> Option<&[f32]> { + self.tiles.get(&t).map(Vec::as_slice) + } + + pub(crate) fn insert(&mut self, t: (u32, u32), data: Vec) { + let size = data.len() * std::mem::size_of::(); + if size > self.budget { + return; + } + while self.bytes + size > self.budget { + let Some(old) = self.order.pop_front() else { break }; + if let Some(d) = self.tiles.remove(&old) { + self.bytes = self.bytes.saturating_sub(d.len() * std::mem::size_of::()); + } + } + if let Some(d) = self.tiles.insert(t, data) { + self.bytes = self.bytes.saturating_sub(d.len() * std::mem::size_of::()); + } else { + self.order.push_back(t); + } + self.bytes += size; + } + + /// Where the stack splits for `inp`: layers of the texture set's top level before the + /// returned index are served from the cache. None when the hot layer is not drawn in the set + /// or the cached part could change (it reads anchors or holds the live stroke). Empties the + /// cache when what the cached part reads changed. + pub(crate) fn prepare(&mut self, inp: &Inputs) -> Option { + let hot = self.hot?; + let p = inp.project; + let set = p.texture_set(inp.ts).ok()?; + let split = set.stack.iter().position(|id| p.render_tree(&[*id]).contains(&hot))?; + let below = set.stack.get(..split)?; + // Anchors carry values across the split. + if p.render_tree(&set.stack).iter().any(|id| p.layers.get(id).is_some_and(Layer::reads_anchor)) { + return None; + } + if let Some(pv) = inp.preview { + let (PaintTarget::Layer(t) | PaintTarget::Mask(t)) = pv.target; + let tree = p.render_tree(below); + if tree.contains(&t) || tree.iter().filter_map(|id| p.layers.get(id)).flat_map(effect_ids).any(|e| e == t) { + return None; + } + } + let key = StackKey::new(p, inp.pixels, inp.baked, inp.images, inp.ts, below)?; + if self.key.as_ref() != Some(&key) { + self.clear(); + self.key = Some(key); + } + Some(split) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn insert_keeps_to_the_budget_dropping_the_oldest() { + let mut c = BelowCache { budget: 3 * 16, ..BelowCache::default() }; + for i in 0..5 { + c.insert((i, 0), vec![0.0; 4]); + } + assert_eq!(c.len(), 3); + assert_eq!(c.bytes(), 48); + assert!(c.tile((0, 0)).is_none() && c.tile((4, 0)).is_some()); + c.insert((9, 9), vec![0.0; 100]); + assert!(c.tile((9, 9)).is_none(), "bigger than the whole budget"); + c.set_hot(Some(LayerId(3))); + assert!(c.is_empty() && c.bytes() == 0); + } +} diff --git a/crates/compose/src/display.rs b/crates/compose/src/display.rs new file mode 100644 index 00000000..6ad9e4f9 --- /dev/null +++ b/crates/compose/src/display.rs @@ -0,0 +1,166 @@ +//! Conversion of composited tiles to the texture formats the viewport samples. + +use texturecraft_core::{BakedMap, Channel}; +use texturecraft_texel::{TILE, TILE_AREA, srgb_lut}; + +use crate::eval::Composite; +use crate::store::BakedMaps; + +/// One of the textures the viewport shader samples. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub enum DisplayKind { + /// sRGB8: base color, alpha = opacity. + BaseColor, + /// RGBA8 linear: ambient occlusion (channel × baked), roughness (1 − glossiness when the set + /// has a Glossiness channel), metallic, specular level. + Orm, + /// RGBA8: painted tangent-space normal. + Normal, + /// RGBA16F: height in r, the linear Specular (F0) color in gba. + Height, + /// sRGB8: emissive color. + Emissive, +} + +impl DisplayKind { + pub const ALL: [DisplayKind; 5] = [DisplayKind::BaseColor, DisplayKind::Orm, DisplayKind::Normal, DisplayKind::Height, DisplayKind::Emissive]; + + /// Bytes per texel of the upload format. + pub fn bytes_per_texel(self) -> usize { + if self == DisplayKind::Height { 8 } else { 4 } + } + + /// Display textures affected by a channel. + pub fn of_channel(c: Channel) -> DisplayKind { + match c { + Channel::BaseColor | Channel::Opacity => DisplayKind::BaseColor, + Channel::Normal => DisplayKind::Normal, + Channel::Height | Channel::Specular => DisplayKind::Height, + Channel::Emissive => DisplayKind::Emissive, + _ => DisplayKind::Orm, + } + } +} + +fn u8_of(v: u16) -> u8 { + (v >> 8) as u8 +} + +/// Tile bytes (`TILE × TILE`, row-major) for `kind` from the composite, or None when the tile is +/// absent in every source (the GPU texture keeps its cleared default). +pub fn tile_bytes(comp: &Composite, baked: &BakedMaps, kind: DisplayKind, tx: u32, ty: u32) -> Vec { + let bpt = kind.bytes_per_texel(); + let mut out = vec![0u8; TILE_AREA * bpt]; + let lut = srgb_lut(); + let get = |c: Channel| comp.channels.get(&c).and_then(|img| img.tile(tx, ty)); + let res = comp.resolution; + let default16 = |c: Channel| texturecraft_texel::to_u16(c.default_value()[0]); + match kind { + DisplayKind::BaseColor | DisplayKind::Emissive => { + let (c, alpha) = if kind == DisplayKind::BaseColor { (Channel::BaseColor, get(Channel::Opacity)) } else { (Channel::Emissive, None) }; + let src = get(c); + let d = c.default_value().map(|v| (texturecraft_texel::linear_to_srgb(v) * 255.0 + 0.5) as u8); + for i in 0..TILE_AREA { + let px = &mut out[i * 4..i * 4 + 4]; + match src { + Some(s) => { + for k in 0..3 { + px[k] = lut[(s.get(i * 3 + k).copied().unwrap_or(0) >> 4) as usize]; + } + } + None => px[..3].copy_from_slice(&d), + } + px[3] = alpha.map(|a| u8_of(a.get(i).copied().unwrap_or(65535))).unwrap_or(255); + } + } + DisplayKind::Orm => { + let (ao, m, s) = (get(Channel::AmbientOcclusion), get(Channel::Metallic), get(Channel::SpecularLevel)); + // Specular/glossiness sets: the viewport shades roughness = 1 − glossiness. + let gloss = comp.channels.contains_key(&Channel::Glossiness); + let (r, r_default) = if gloss { (get(Channel::Glossiness), default16(Channel::Glossiness)) } else { (get(Channel::Roughness), default16(Channel::Roughness)) }; + for i in 0..TILE_AREA { + let (x, y) = (tx * TILE + (i as u32 % TILE), ty * TILE + (i as u32 / TILE)); + let baked_ao = baked.scalar(BakedMap::AmbientOcclusion, x, y, res).unwrap_or(1.0); + let ch_ao = ao.and_then(|a| a.get(i)).map(|v| *v as f32 / 65535.0).unwrap_or(1.0); + let px = &mut out[i * 4..i * 4 + 4]; + px[0] = ((baked_ao * ch_ao).clamp(0.0, 1.0) * 255.0 + 0.5) as u8; + let v = r.and_then(|t| t.get(i).copied()).unwrap_or(r_default); + px[1] = u8_of(if gloss { 65535 - v } else { v }); + px[2] = u8_of(m.and_then(|t| t.get(i).copied()).unwrap_or(0)); + px[3] = u8_of(s.and_then(|t| t.get(i).copied()).unwrap_or(default16(Channel::SpecularLevel))); + } + } + DisplayKind::Normal => { + let n = get(Channel::Normal); + for i in 0..TILE_AREA { + let px = &mut out[i * 4..i * 4 + 4]; + match n { + Some(t) => { + for (k, d) in px.iter_mut().take(3).enumerate() { + *d = u8_of(t.get(i * 3 + k).copied().unwrap_or(0)); + } + } + None => px[..3].copy_from_slice(&[128, 128, 255]), + } + px[3] = 255; + } + } + DisplayKind::Height => { + let (h, spec) = (get(Channel::Height), get(Channel::Specular)); + let spec_default = Channel::Specular.default_value(); + for i in 0..TILE_AREA { + let v = h.and_then(|t| t.get(i)).map(|v| texturecraft_texel::from_u16_signed(*v)).unwrap_or(0.0); + let f0 = |k: usize| spec.and_then(|t| t.get(i * 3 + k)).map(|v| *v as f32 / 65535.0).unwrap_or(spec_default[k]); + for (k, c) in [v, f0(0), f0(1), f0(2)].into_iter().enumerate() { + out[i * 8 + k * 2..i * 8 + k * 2 + 2].copy_from_slice(&half::f16::from_f32(c).to_le_bytes()); + } + } + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + use texturecraft_texel::to_u16; + + #[test] + fn base_color_uses_srgb_and_opacity() { + let mut comp = Composite::new(&[Channel::BaseColor, Channel::Opacity], 64); + comp.channels.get_mut(&Channel::BaseColor).unwrap().set(0, 0, &[to_u16(1.0), to_u16(0.5), 0]); + comp.channels.get_mut(&Channel::Opacity).unwrap().set(0, 0, &[to_u16(0.5)]); + let b = tile_bytes(&comp, &BakedMaps::default(), DisplayKind::BaseColor, 0, 0); + assert_eq!(b[0], 255); + assert!((b[1] as i32 - 188).abs() <= 1, "{}", b[1]); + assert_eq!(b[2], 0); + assert!((b[3] as i32 - 127).abs() <= 1); + } + + #[test] + fn missing_channels_fall_back_to_defaults() { + let comp = Composite::new(&[], 64); + let orm = tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Orm, 0, 0); + assert_eq!(&orm[..4], &[255, 128, 0, 128]); + let n = tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Normal, 0, 0); + assert_eq!(&n[..4], &[128, 128, 255, 255]); + let h = tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Height, 0, 0); + assert_eq!(h.len(), TILE_AREA * 8); + assert_eq!(half::f16::from_le_bytes([h[2], h[3]]).to_f32(), half::f16::from_f32(0.04).to_f32(), "default F0"); + } + + #[test] + fn glossiness_shows_as_roughness_and_specular_rides_in_height() { + let mut comp = Composite::new(&[Channel::Glossiness, Channel::Specular], 64); + comp.channels.get_mut(&Channel::Glossiness).unwrap().set(0, 0, &[to_u16(0.8)]); + comp.channels.get_mut(&Channel::Specular).unwrap().set(0, 0, &[to_u16(1.0), to_u16(0.5), 0]); + let orm = tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Orm, 0, 0); + assert!((orm[1] as i32 - 51).abs() <= 1, "roughness = 1 - 0.8, got {}", orm[1]); + let h = tile_bytes(&comp, &BakedMaps::default(), DisplayKind::Height, 0, 0); + let at = |k: usize| half::f16::from_le_bytes([h[k * 2], h[k * 2 + 1]]).to_f32(); + assert_eq!(at(0), 0.0); + assert!((at(1) - 1.0).abs() < 1e-3 && (at(2) - 0.5).abs() < 1e-3 && at(3) == 0.0); + assert_eq!(DisplayKind::of_channel(Channel::Specular), DisplayKind::Height); + assert_eq!(DisplayKind::of_channel(Channel::Glossiness), DisplayKind::Orm); + } +} diff --git a/crates/compose/src/eval.rs b/crates/compose/src/eval.rs new file mode 100644 index 00000000..2d74065e --- /dev/null +++ b/crates/compose/src/eval.rs @@ -0,0 +1,1642 @@ +//! The layer-stack evaluator: composites a texture set's stack into one image per channel, +//! tile by tile in parallel. This is the reference implementation (the oracle) for the viewport +//! and for export. + +use std::collections::{BTreeMap, HashMap}; + +use rayon::prelude::*; +use texturecraft_core::{ + BakedMap, BlendMode, Channel, ChannelKind, CompareOp, Effect, EffectKind, Fill, Filter, Generator, GeneratorKind, HeightMask, Layer, LayerId, LayerKind, + Mask, MaskBackground, Project, Projection, Source, blend1, blend3, hsl_adjust, lum, mix3, +}; +use texturecraft_geom::glam::{Vec2, Vec3}; +use texturecraft_geom::{Mesh, NO_TRI, UvGBuffer}; +use texturecraft_texel::{DilationMap, TILE, TILE_AREA, TileImage, from_u16, to_u16, to_u16_signed}; + +use crate::below::BelowCache; +use crate::pattern::{Coords, image_samples, procedural, world_noise}; +use crate::store::{BakedMaps, ImageStore, PixelStore, decode}; + +const NCH: usize = Channel::ALL.len(); + +/// What a live stroke paints into. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PaintTarget { + /// A paint layer's channels. + Layer(LayerId), + /// A paint effect inside a mask. + Mask(LayerId), +} + +/// A screen-pinned image painted through the camera (projection tool / stencil). +#[derive(Clone, Debug)] +pub struct Stencil { + pub image: std::sync::Arc, + /// Camera view-projection captured when the stroke started. + pub view_proj: texturecraft_geom::glam::Mat4, + /// Image placement in normalized device coordinates: center, half extents, rotation. + pub center: [f32; 2], + pub half: [f32; 2], + pub rotation: f32, + /// Repeat the image beyond its bounds (otherwise nothing is painted outside it). + pub tile: bool, +} + +impl Stencil { + /// Linear color and alpha of the stencil at world point `p` (None outside the image). + pub fn sample_world(&self, p: Vec3) -> Option<([f32; 3], f32)> { + let c = self.view_proj * p.extend(1.0); + if c.w <= 1e-6 { + return None; + } + let (dx, dy) = (c.x / c.w - self.center[0], c.y / c.w - self.center[1]); + let (sn, cs) = (-self.rotation).sin_cos(); + let lx = (dx * cs - dy * sn) / self.half[0].max(1e-6); + let ly = (dx * sn + dy * cs) / self.half[1].max(1e-6); + if !self.tile && (lx.abs() > 1.0 || ly.abs() > 1.0) { + return None; + } + let s = self.image.sample(lx * 0.5 + 0.5, 0.5 - ly * 0.5); + let rgb = if self.image.srgb { [s[0], s[1], s[2]].map(texturecraft_texel::srgb_to_linear) } else { [s[0], s[1], s[2]] }; + Some((rgb, s[3].clamp(0.0, 1.0))) + } +} + +/// The stroke being drawn, shown before it is committed. +#[derive(Clone, Debug)] +pub struct StrokePreview { + pub target: PaintTarget, + /// Stroke alpha per texel (1 component, 0..1). + pub alpha: TileImage, + /// Stroke opacity cap. + pub opacity: f32, + pub erase: bool, + /// Values painted per channel (layer targets). + pub values: BTreeMap, + /// Gray value painted into a mask. + pub mask_value: f32, + /// Projection tool: base color (and alpha) come from a screen-pinned image. + pub stencil: Option, + /// Clone and smudge: straight values per channel carried by the stroke (replace `values`). + pub carried: BTreeMap, +} + +/// Everything the evaluator reads. +pub struct Inputs<'a> { + pub project: &'a Project, + pub ts: usize, + pub mesh: &'a Mesh, + pub gbuf: &'a UvGBuffer, + pub pixels: &'a PixelStore, + pub baked: &'a BakedMaps, + pub preview: Option<&'a StrokePreview>, + /// Imported image resources (fill sources). + pub images: &'a ImageStore, + /// Mesh bounds, for scale-independent procedural coordinates. + pub bounds: (Vec3, Vec3), +} + +/// The composited channels of one texture set. +#[derive(Clone, Debug, PartialEq)] +pub struct Composite { + pub resolution: u32, + pub channels: BTreeMap, +} + +impl Composite { + pub fn new(channels: &[Channel], resolution: u32) -> Composite { + Composite { resolution, channels: channels.iter().map(|c| (*c, TileImage::new(resolution, resolution, c.components()))).collect() } + } + + /// Channel value at a texel (the channel default when absent). + pub fn value(&self, c: Channel, x: u32, y: u32) -> [f32; 3] { + let Some(img) = self.channels.get(&c) else { return c.default_value() }; + let raw = img.get(x, y); + let k = c.kind(); + if !img.is_allocated(x / TILE, y / TILE) { + return c.default_value(); + } + if c.components() == 3 { [decode(k, raw[0]), decode(k, raw[1]), decode(k, raw[2])] } else { [decode(k, raw[0]); 3] } + } +} + +/// Every tile of an image of `res`. +pub fn all_tiles(res: u32) -> Vec<(u32, u32)> { + let n = res.div_ceil(TILE); + (0..n).flat_map(|y| (0..n).map(move |x| (x, y))).collect() +} + +/// Tiles that hold at least one covered texel. +pub fn covered_tiles(gbuf: &UvGBuffer) -> Vec<(u32, u32)> { + let (tw, th) = (gbuf.width.div_ceil(TILE), gbuf.height.div_ceil(TILE)); + let mut out = Vec::new(); + for ty in 0..th { + for tx in 0..tw { + let any = (ty * TILE..((ty + 1) * TILE).min(gbuf.height)) + .any(|y| (tx * TILE..((tx + 1) * TILE).min(gbuf.width)).any(|x| gbuf.tri.get((y * gbuf.width + x) as usize).is_some_and(|t| *t != NO_TRI))); + if any { + out.push((tx, ty)); + } + } + } + out +} + +// --------------------------------------------------------------------------------------------- +// Per-tile plan: the stack flattened with each layer's tile slices looked up once per tile. + +struct ChanPlan<'a> { + ch: usize, + kind: ChannelKind, + comps: usize, + blend: BlendMode, + opacity: f32, + data: Option<&'a [u16]>, + /// Visible content effects, holding this channel's tile for paint effects (empty, and + /// allocation free, for layers without content effects). + effects: Vec>, + /// A content neighbourhood filter precomputed over the region: continue the effects after + /// effect `.0` starting from the field's value. + start: Option<(usize, &'a Field)>, +} + +enum Content<'a> { + Paint { preview: bool }, + Fill(&'a Fill), + Group(Vec>), +} + +struct EffectPlan<'a> { + kind: &'a EffectKind, + blend: BlendMode, + opacity: f32, + data: Option<&'a [u16]>, + preview: bool, +} + +struct MaskPlan<'a> { + background: f32, + effects: Vec>, + /// A neighbourhood filter (blur, sharpen) precomputed over the region: continue the stack + /// after effect `.0` starting from the field's value. + start: Option<(usize, &'a Field)>, +} + +/// A layer's mask (one value per texel) or one content channel (straight value and coverage) +/// after its last neighbourhood filter, over a texel region. +pub struct Field { + x0: u32, + y0: u32, + w: u32, + h: u32, + comps: usize, + data: Vec, +} + +impl Field { + fn get(&self, x: u32, y: u32) -> Option<&[f32]> { + if x < self.x0 || y < self.y0 || x >= self.x0 + self.w || y >= self.y0 + self.h { + return None; + } + let i = ((y - self.y0) * self.w + (x - self.x0)) as usize * self.comps; + self.data.get(i..i + self.comps) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum FieldKey { + Mask(LayerId), + Content(LayerId, Channel), +} + +pub type Fields = HashMap; + +/// Index (among visible effects) of the last neighbourhood filter of an effect stack, its +/// radius in texels and its sharpen amount. +fn neighbourhood(effects: &[Effect], res: u32) -> Option<(usize, f32, Option)> { + effects + .iter() + .filter(|e| e.visible) + .enumerate() + .filter_map(|(i, e)| match &e.kind { + EffectKind::Filter(Filter::Blur { radius }) if *radius > 0.0 => Some((i, (radius * res as f32 / 2048.0).max(0.5), None)), + EffectKind::Filter(Filter::Sharpen { amount }) => Some((i, (1.5 * res as f32 / 2048.0).max(0.75), Some(*amount))), + _ => None, + }) + .last() +} + +/// Fixed-point scale of the blur. Integer running sums make a texel's result independent of +/// the region it is computed in, so tiles composited in any grouping agree exactly. +const FIX: f64 = (1u64 << 24) as f64; + +/// Three running-sum box blurs (≈ Gaussian) of a `w × h` image along rows then along columns. +/// Rows (and columns, transposed) blur in parallel. +fn blur(data: &mut [f32], w: usize, h: usize, r: usize) { + let fix = |v: &f32| if v.is_finite() { (f64::from(v.clamp(-1e6, 1e6)) * FIX).round() as i64 } else { 0 }; + let mut v: Vec = data.par_iter().map(fix).collect(); + blur_rows(&mut v, w, r); + let mut t = transpose(&v, w, h); + blur_rows(&mut t, h, r); + data.par_iter_mut().zip(transpose(&t, h, w)).for_each(|(d, s)| *d = (s as f64 / FIX) as f32); +} + +/// Three box blurs of radius `r` along every row (`w` long) of `data`. +fn blur_rows(data: &mut [i64], w: usize, r: usize) { + if w == 0 { + return; + } + data.par_chunks_mut(w).for_each(|row| { + let mut tmp = vec![0i64; row.len()]; + for _ in 0..3 { + box_line(row, &mut tmp, r); + row.copy_from_slice(&tmp); + } + }); +} + +/// One box blur of radius `r` of a line (texels past the ends count as 0). +fn box_line(src: &[i64], dst: &mut [i64], r: usize) { + let n = (2 * r + 1) as i64; + // Rounded division of a window sum. + let avg = |acc: i64| (2 * acc + n).div_euclid(2 * n); + let mut acc: i64 = src.iter().take(r + 1).sum(); + for (i, d) in dst.iter_mut().enumerate() { + *d = avg(acc); + acc += src.get(i + r + 1).copied().unwrap_or(0); + if i >= r { + acc -= src.get(i - r).copied().unwrap_or(0); + } + } +} + +/// `src` (`w × h`, row-major) transposed to `h × w`. +fn transpose(src: &[i64], w: usize, h: usize) -> Vec { + let mut out = vec![0i64; src.len()]; + if h > 0 { + out.par_chunks_mut(h).enumerate().for_each(|(x, col)| { + for (y, d) in col.iter_mut().enumerate() { + *d = src.get(y * w + x).copied().unwrap_or(0); + } + }); + } + out +} + +/// Covered texels of tile (tx, ty): (x, y, offset inside the tile). +fn tile_texels(gbuf: &UvGBuffer, tx: u32, ty: u32) -> impl Iterator + '_ { + let res = gbuf.width; + (0..TILE).flat_map(|ly| (0..TILE).map(move |lx| (lx, ly))).filter_map(move |(lx, ly)| { + let (x, y) = (tx * TILE + lx, ty * TILE + ly); + (x < res && y < res && gbuf.tri.get((y * res + x) as usize).is_some_and(|t| *t != NO_TRI)).then_some((x, y, (ly * TILE + lx) as usize)) + }) +} + +/// Live stroke alpha at offset `o` of a preview tile. +fn stroke_at(preview: Option<&[u16]>, o: usize) -> f32 { + preview.and_then(|t| t.get(o)).map(|v| from_u16(*v)).unwrap_or(0.0) +} + +/// The full stack of tile (tx, ty) when `layer` reads anchor points (empty otherwise): evaluated +/// up to the layer, it records the anchors below it. +fn anchor_plan<'a>(inp: &Inputs<'a>, id: LayerId, channels: &[Channel], tx: u32, ty: u32, fields: Option<&'a Fields>) -> Vec> { + match inp.project.texture_set(inp.ts) { + Ok(set) if reads_anchor(inp.project, id) => plan(inp, &set.stack, channels, tx, ty, 0, fields), + _ => Vec::new(), + } +} + +/// Whether layer `id` (its own mask, or the content it draws) reads an anchor point. +pub(crate) fn reads_anchor(p: &Project, id: LayerId) -> bool { + p.layers.get(&id).is_some_and(Layer::reads_anchor) || p.content_layer(id).is_some_and(Layer::reads_anchor) +} + +/// The mask layer `id` draws with: its own, or an instance source's. +fn layer_mask(p: &Project, id: LayerId) -> Option<&Mask> { + p.layers.get(&p.mask_owner(id))?.mask.as_ref() +} + +/// Layers of `list` in drawing order (bottom → top, folder and instanced folder contents after +/// their folder). +fn draw_order(p: &Project, list: &[LayerId], depth: usize, out: &mut Vec) { + if depth > texturecraft_core::project::MAX_DEPTH { + return; + } + for id in list { + out.push(*id); + if let Some(l) = p.content_layer(*id) { + draw_order(p, l.children(), depth + 1, out); + } + } +} + +/// What a field holds. +#[derive(Clone, Copy)] +enum Job<'a> { + Mask(&'a Mask), + Content(Channel), +} + +/// One neighbourhood-filtered field of `layer` over `tiles` plus the filter's reach, built on +/// the fields already computed below it. +fn field(inp: &Inputs, channels: &[Channel], below: &Fields, id: LayerId, job: Job, tiles: &[(u32, u32)]) -> Option { + let res = inp.gbuf.width; + let effects = match job { + Job::Mask(m) => &m.effects, + Job::Content(_) => &inp.project.content_layer(id)?.content_effects, + }; + let (k, radius, sharpen) = neighbourhood(effects, res)?; + let opacity = effects.iter().filter(|e| e.visible).nth(k).map(|e| e.opacity.clamp(0.0, 1.0)).unwrap_or(1.0); + // Clamped: a hostile radius must not overflow the halo and window arithmetic. + let r = radius.round().clamp(1.0, res as f32) as u32; + // Three box passes reach 3r texels. + let halo = (3 * r).div_ceil(TILE); + let (tx0, ty0) = tiles.iter().fold((u32::MAX, u32::MAX), |(a, b), t| (a.min(t.0), b.min(t.1))); + let (tx1, ty1) = tiles.iter().fold((0, 0), |(a, b), t| (a.max(t.0), b.max(t.1))); + let (rx0, ry0) = (tx0.saturating_sub(halo) * TILE, ty0.saturating_sub(halo) * TILE); + let (rx1, ry1) = (((tx1 + 1 + halo) * TILE).min(res), ((ty1 + 1 + halo) * TILE).min(res)); + let (w, h) = (rx1.saturating_sub(rx0), ry1.saturating_sub(ry0)); + if w == 0 || h == 0 { + return None; + } + let comps = match job { + Job::Mask(_) => 1, + Job::Content(_) => 4, + }; + // Evaluate the stack before the filter, tile by tile (content premultiplied by coverage). + let region_tiles: Vec<(u32, u32)> = (ry0 / TILE..ry1.div_ceil(TILE)).flat_map(|y| (rx0 / TILE..rx1.div_ceil(TILE)).map(move |x| (x, y))).collect(); + let parts: Vec> = region_tiles + .par_iter() + .map(|&(tx, ty)| { + let full = anchor_plan(inp, id, channels, tx, ty, Some(below)); + let preview_tile = inp.preview.and_then(|p| p.alpha.tile(tx, ty)); + let mut mask = None; + let mut nodes = Vec::new(); + match job { + Job::Mask(m) => { + let Some(mut mp) = mask_plan(inp, id, m, tx, ty, None) else { return Vec::new() }; + mp.effects.truncate(k); + mask = Some(mp); + } + Job::Content(ch) => { + nodes = plan(inp, &[id], &[ch], tx, ty, 0, Some(below)); + if let Some(c) = nodes.first_mut().and_then(|n| n.chans.first_mut()) { + c.effects.truncate(k); + c.start = None; + } + } + } + tile_texels(inp.gbuf, tx, ty) + .map(|(x, y, o)| { + let mut tex = Texel::new(inp, x, y, o, stroke_at(preview_tile, o)); + if !full.is_empty() { + tex.anchors_below(&full, id); + } + let v = match (&mask, nodes.first()) { + (Some(mp), _) => [tex.mask(mp), 0.0, 0.0, 0.0], + (None, Some(n)) => match n.chans.first().and_then(|c| tex.content(&n.content, c)) { + Some((v, cov)) => [v[0] * cov, v[1] * cov, v[2] * cov, cov], + None => [0.0; 4], + }, + (None, None) => [0.0; 4], + }; + (x, y, v) + }) + .collect() + }) + .collect(); + // Scatter into the region, a row of tiles at a time (the region is tile aligned). + let n = (w * h) as usize; + let mut orig = vec![0.0f32; n * comps]; + let mut cover = vec![0.0f32; n]; + let row_tiles = (rx1.div_ceil(TILE) - rx0 / TILE).max(1) as usize; + let band = (TILE * w) as usize; + orig.par_chunks_mut(band * comps).zip(cover.par_chunks_mut(band)).zip(parts.par_chunks(row_tiles)).for_each(|((orig, cover), row)| { + for &(x, y, v) in row.iter().flatten() { + let i = (((y - ry0) % TILE) * w + (x - rx0)) as usize; + if let (Some(c), Some(px)) = (cover.get_mut(i), orig.get_mut(i * comps..(i + 1) * comps)) { + *c = 1.0; + px.copy_from_slice(&v[..comps]); + } + } + }); + // Weighted so uncovered texels don't bleed in. + let (wu, hu, ru) = (w as usize, h as usize, r as usize); + let mut weights = cover; + blur(&mut weights, wu, hu, ru); + let mut data = orig.clone(); + for p in 0..comps { + let mut vals: Vec = orig.par_chunks(comps).map(|px| px.get(p).copied().unwrap_or(0.0)).collect(); + blur(&mut vals, wu, hu, ru); + data.par_chunks_mut(comps).zip(vals.par_iter()).zip(weights.par_iter()).for_each(|((px, v), wt)| { + if *wt > 1e-6 + && let Some(slot) = px.get_mut(p) + { + let (o, blurred) = (*slot, v / wt); + let filtered = match sharpen { + Some(amount) => o + amount * (o - blurred), + None => blurred, + }; + *slot = o + (filtered - o) * opacity; + } + }); + } + data.par_chunks_mut(comps).for_each(|px| match px { + [v] => *v = v.clamp(0.0, 1.0), + [r, g, b, cov] => { + *cov = cov.clamp(0.0, 1.0); + let inv = if *cov > 1e-5 { 1.0 / *cov } else { 0.0 }; + *r *= inv; + *g *= inv; + *b *= inv; + } + _ => {} + }); + Some(Field { x0: rx0, y0: ry0, w, h, comps, data }) +} + +/// Precompute the neighbourhood-filtered masks and content channels of every layer that needs +/// one, over `tiles` plus the filter's reach. Walks bottom → top so each field sees the fields +/// (and anchors) below it. Cheap when no layer has a blur or sharpen. +pub fn neighbourhood_fields(inp: &Inputs, channels: &[Channel], tiles: &[(u32, u32)]) -> Fields { + let stack = inp.project.texture_set(inp.ts).map(|s| s.stack.as_slice()).unwrap_or_default(); + fields_of(inp, channels, stack, tiles) +} + +/// [`neighbourhood_fields`] of the layers drawn by `list` only. +fn fields_of(inp: &Inputs, channels: &[Channel], list: &[LayerId], tiles: &[(u32, u32)]) -> Fields { + let mut out = Fields::new(); + if tiles.is_empty() || inp.gbuf.width == 0 { + return out; + } + let res = inp.gbuf.width; + let mut order = Vec::new(); + draw_order(inp.project, list, 0, &mut order); + for id in order { + let Some(layer) = inp.project.content_layer(id) else { continue }; + if let Some(m) = layer_mask(inp.project, id).filter(|m| m.enabled && neighbourhood(&m.effects, res).is_some()) + && let Some(f) = field(inp, channels, &out, id, Job::Mask(m), tiles) + { + out.insert(FieldKey::Mask(id), f); + } + if layer.is_group() || neighbourhood(&layer.content_effects, res).is_none() { + continue; + } + for ch in channels.iter().filter(|c| layer.channels.contains_key(c)) { + if let Some(f) = field(inp, channels, &out, id, Job::Content(*ch), tiles) { + out.insert(FieldKey::Content(id, *ch), f); + } + } + } + out +} + +struct Node<'a> { + id: LayerId, + content: Content<'a>, + chans: Vec>, + mask: Option>, + /// Visible anchor points of the layer (recorded after it is applied). + anchors: Vec, + /// The node or one of its children records an anchor. + has_anchor: bool, + height_mask: HeightMask, +} + +/// Plans of the visible effects of a stack: mask effects (`channel` None) or the content +/// effects of one channel (paint effects then read that channel's pixels). +fn effect_plans<'a>(inp: &Inputs<'a>, effects: &'a [Effect], tx: u32, ty: u32, channel: Option) -> Vec> { + effects + .iter() + .filter(|e| e.visible) + .map(|e| { + let (img, target) = match channel { + Some(c) => (inp.pixels.layer(e.id, c), PaintTarget::Layer(e.id)), + None => (inp.pixels.masks.get(&e.id), PaintTarget::Mask(e.id)), + }; + EffectPlan { + kind: &e.kind, + blend: e.blend, + opacity: e.opacity, + data: matches!(e.kind, EffectKind::Paint).then(|| img.filter(|i| i.width() == inp.gbuf.width).and_then(|i| i.tile(tx, ty))).flatten(), + preview: inp.preview.is_some_and(|p| p.target == target), + } + }) + .collect() +} + +fn mask_plan<'a>(inp: &Inputs<'a>, id: LayerId, mask: &'a Mask, tx: u32, ty: u32, fields: Option<&'a Fields>) -> Option> { + if !mask.enabled { + return None; + } + let background = if mask.background == MaskBackground::White { 1.0 } else { 0.0 }; + let effects = effect_plans(inp, &mask.effects, tx, ty, None); + let start = fields.and_then(|f| f.get(&FieldKey::Mask(id))).and_then(|field| neighbourhood(&mask.effects, inp.gbuf.width).map(|(k, _, _)| (k, field))); + Some(MaskPlan { background, effects, start }) +} + +fn plan<'a>(inp: &Inputs<'a>, list: &[LayerId], channels: &[Channel], tx: u32, ty: u32, depth: usize, fields: Option<&'a Fields>) -> Vec> { + let mut out = Vec::new(); + if depth > texturecraft_core::project::MAX_DEPTH { + return out; + } + for id in list { + let Some(own): Option<&'a Layer> = inp.project.layers.get(id) else { continue }; + // Instances draw their source's content (a missing source draws nothing). + let Some(layer): Option<&'a Layer> = inp.project.content_layer(*id) else { continue }; + if !own.visible { + continue; + } + let paint = matches!(layer.kind, LayerKind::Paint); + let fx = !layer.is_group() && layer.content_effects.iter().any(|e| e.visible); + let fx_start = if fx { neighbourhood(&layer.content_effects, inp.gbuf.width).map(|n| n.0) } else { None }; + let chans = channels + .iter() + .filter_map(|c| { + let b = layer.blend(*c)?; + if b.mode == BlendMode::Disable || b.opacity <= 0.0 { + return None; + } + // shortcut: an instance in a set of another resolution draws no painted pixels; + // resample the source's pixels if instances across sizes become common. + let data = + if paint { inp.pixels.layer(layer.id, *c).filter(|img| img.width() == inp.gbuf.width).and_then(|img| img.tile(tx, ty)) } else { None }; + let effects = if fx { effect_plans(inp, &layer.content_effects, tx, ty, Some(*c)) } else { Vec::new() }; + let start = fx_start.zip(fields.and_then(|f| f.get(&FieldKey::Content(*id, *c)))); + Some(ChanPlan { ch: c.index(), kind: c.kind(), comps: c.components(), blend: b.mode, opacity: b.opacity.clamp(0.0, 1.0), data, effects, start }) + }) + .collect::>(); + let content = match &layer.kind { + LayerKind::Paint => Content::Paint { preview: inp.preview.is_some_and(|p| p.target == PaintTarget::Layer(layer.id)) }, + LayerKind::Fill(f) => Content::Fill(f), + LayerKind::Group { children, .. } => Content::Group(plan(inp, children, channels, tx, ty, depth + 1, fields)), + // `content_layer` already followed instance links. + LayerKind::Instance { .. } => continue, + }; + // A paint layer with nothing painted (and not being painted) or added by its content + // effects contributes nothing. + if let Content::Paint { preview: false } = content + && chans.iter().all(|c| c.data.is_none() && c.effects.is_empty()) + { + continue; + } + if chans.is_empty() { + continue; + } + let mask = layer_mask(inp.project, *id).and_then(|m| mask_plan(inp, *id, m, tx, ty, fields)); + let anchors: Vec = + if fx { layer.content_effects.iter().filter(|e| e.visible && matches!(e.kind, EffectKind::Anchor)).map(|e| e.id).collect() } else { Vec::new() }; + let has_anchor = !anchors.is_empty() || matches!(&content, Content::Group(nodes) if nodes.iter().any(|n| n.has_anchor)); + out.push(Node { id: *id, content, chans, mask, anchors, has_anchor, height_mask: layer.height_mask }); + } + out +} + +/// Apply a 0..1 function to each component of a channel value: signed values map through +/// 0..1, normals are left alone. +fn per_unit(kind: ChannelKind, v: [f32; 3], f: impl Fn(f32) -> f32) -> [f32; 3] { + match kind { + ChannelKind::NormalMap => v, + ChannelKind::Signed => v.map(|x| f(x * 0.5 + 0.5) * 2.0 - 1.0), + ChannelKind::Color | ChannelKind::Scalar => v.map(f), + } +} + +#[inline] +fn blend_chan(c: &ChanPlan, mode: BlendMode, a: [f32; 3], b: [f32; 3]) -> [f32; 3] { + if c.comps == 3 { blend3(mode, a, b) } else { [blend1(mode, a[0], b[0]); 3] } +} + +/// Lay value `p` with weight `a` over a straight value and coverage (premultiplied "over"). +fn over(c: &ChanPlan, mode: BlendMode, v: [f32; 3], cov: f32, p: [f32; 3], a: f32) -> ([f32; 3], f32) { + if a <= 0.0 { + return (v, cov); + } + let b = if cov > 0.0 { blend_chan(c, mode, v, p) } else { p }; + let nc = a + cov * (1.0 - a); + (mix3(v, b, a / nc.max(1e-5)), nc) +} + +/// Per-texel context. +struct Texel<'a, 'b> { + inp: &'b Inputs<'a>, + x: u32, + y: u32, + /// Offset of this texel inside a tile (in texels). + o: usize, + res: u32, + coords: Option, + stroke: f32, + /// `eval` stops when it reaches this layer. + stop: Option, + /// Stack values recorded by the anchor points evaluated so far. + anchors: Vec<(LayerId, [[f32; 3]; NCH])>, +} + +impl<'a, 'b> Texel<'a, 'b> { + fn new(inp: &'b Inputs<'a>, x: u32, y: u32, o: usize, stroke: f32) -> Self { + Texel { inp, x, y, o, res: inp.gbuf.width, coords: None, stroke, stop: None, anchors: Vec::new() } + } + + fn coords(&mut self) -> Coords { + if let Some(c) = self.coords { + return c; + } + let g = self.inp.gbuf; + let i = (self.y * g.width + self.x) as usize; + let (lo, hi) = self.inp.bounds; + let center = (lo + hi) * 0.5; + let r = ((hi - lo).length() * 0.5).max(1e-4); + let c = match g.surface(self.inp.mesh, i) { + Some(s) => Coords { uv: s.uv, pos: (s.pos - center) / r, nrm: s.nrm }, + None => Coords { uv: Vec2::new((self.x as f32 + 0.5) / self.res as f32, (self.y as f32 + 0.5) / self.res as f32), pos: Vec3::ZERO, nrm: Vec3::Y }, + }; + self.coords = Some(c); + c + } + + /// World position of this texel (None when uncovered). + fn world(&self) -> Option { + let i = (self.y * self.inp.gbuf.width + self.x) as usize; + self.inp.gbuf.surface(self.inp.mesh, i).map(|s| s.pos) + } + + fn baked(&self, m: BakedMap) -> Option { + self.inp.baked.scalar(m, self.x, self.y, self.res) + } + + fn source(&mut self, s: &Source, proj: &Projection, kind: ChannelKind) -> [f32; 3] { + match s { + Source::Uniform(v) => *v, + Source::Procedural { pattern, low, high } => { + let c = self.coords(); + let t = procedural(pattern, proj, &c); + mix3(*low, *high, t) + } + Source::Image { name } => { + let Some(img) = self.inp.images.get(name) else { return [0.5; 3] }; + let c = self.coords(); + let mut acc = [0.0f32; 4]; + for (uv, w) in image_samples(proj, &c).into_iter().filter(|(_, w)| *w > 0.0) { + let s = img.sample(uv.x, uv.y); + for k in 0..4 { + acc[k] += s[k] * w; + } + } + match kind { + ChannelKind::Color if img.srgb => [acc[0], acc[1], acc[2]].map(texturecraft_texel::srgb_to_linear), + ChannelKind::Color | ChannelKind::NormalMap => [acc[0], acc[1], acc[2]], + ChannelKind::Scalar => [acc[0]; 3], + ChannelKind::Signed => [acc[0] * 2.0 - 1.0; 3], + } + } + Source::Anchor { anchor, channel } => { + // An anchor not evaluated yet (above this layer, hidden) reads the channel default. + let v = self.anchors.iter().rev().find(|(id, _)| id == anchor).and_then(|(_, acc)| acc.get(channel.index()).copied()); + let v = v.unwrap_or_else(|| channel.default_value()); + let gray = matches!(channel.kind(), ChannelKind::Color | ChannelKind::NormalMap) && matches!(kind, ChannelKind::Scalar | ChannelKind::Signed); + if gray { [lum(v); 3] } else { v } + } + } + } + + fn generator(&mut self, g: &Generator) -> f32 { + let c = self.coords(); + let s = match g.kind { + GeneratorKind::EdgeWear => (self.baked(BakedMap::Curvature).unwrap_or(0.5) - 0.5) * 2.0, + GeneratorKind::Curvature => self.baked(BakedMap::Curvature).unwrap_or(0.5), + GeneratorKind::Dirt => 1.0 - self.baked(BakedMap::AmbientOcclusion).unwrap_or(1.0), + GeneratorKind::AmbientOcclusion => self.baked(BakedMap::AmbientOcclusion).unwrap_or(1.0), + GeneratorKind::ThinAreas => 1.0 - self.baked(BakedMap::Thickness).unwrap_or(1.0), + GeneratorKind::Position => c.pos.dot(Vec3::from(g.direction).try_normalize().unwrap_or(Vec3::Y)) * 0.5 + 0.5, + GeneratorKind::Direction => c.nrm.dot(Vec3::from(g.direction).try_normalize().unwrap_or(Vec3::Y)) * 0.5 + 0.5, + }; + let s = if g.noise_amount > 0.0 { s + (world_noise(c.pos, g.noise_scale, g.seed) - 0.5) * g.noise_amount } else { s }; + let th = 1.0 - g.level.clamp(0.0, 1.0); + let soft = g.softness.max(1e-3); + let t = ((s - (th - soft)) / (2.0 * soft)).clamp(0.0, 1.0); + let v = t * t * (3.0 - 2.0 * t); + if g.invert { 1.0 - v } else { v } + } + + fn mask(&mut self, m: &MaskPlan) -> f32 { + let mut v = m.background; + let mut skip = 0; + if let Some((k, field)) = m.start + && let Some(f) = field.get(self.x, self.y).and_then(|d| d.first().copied()) + { + v = f; + skip = k + 1; + } + for e in m.effects.iter().skip(skip) { + let o = e.opacity.clamp(0.0, 1.0); + match e.kind { + EffectKind::Paint => { + let (mut pv, mut cov) = match e.data { + Some(d) => (from_u16(d.get(self.o * 2).copied().unwrap_or(0)), from_u16(d.get(self.o * 2 + 1).copied().unwrap_or(0))), + None => (0.0, 0.0), + }; + if e.preview + && let Some(p) = self.inp.preview + { + let a = (self.stroke * p.opacity).clamp(0.0, 1.0); + if p.erase { + pv *= 1.0 - a; + cov *= 1.0 - a; + } else { + pv = p.mask_value * a + pv * (1.0 - a); + cov = a + cov * (1.0 - a); + } + } + if cov > 0.0 { + let val = pv / cov.max(1e-5); + v += (blend1(e.blend, v, val) - v) * cov * o; + } + } + EffectKind::Fill { source, projection } => { + let val = self.source(source, projection, ChannelKind::Scalar)[0]; + v += (blend1(e.blend, v, val) - v) * o; + } + EffectKind::Generator(g) => { + let val = self.generator(g); + v += (blend1(e.blend, v, val) - v) * o; + } + EffectKind::Levels(l) => v += (l.apply(v) - v) * o, + EffectKind::Filter(f) => { + let val = match f { + Filter::Invert => 1.0 - v, + Filter::Threshold { level } => { + if v >= *level { + 1.0 + } else { + 0.0 + } + } + // A mask is gray: only its lightness changes. + Filter::Hsl { lightness, .. } => hsl_adjust([v; 3], 0.0, 0.0, *lightness)[0], + // Neighbourhood filters come precomputed through `MaskPlan::start`. + Filter::Blur { .. } | Filter::Sharpen { .. } => v, + }; + v += (val - v) * o; + } + EffectKind::ColorSelection { colors, tolerance } => { + let id = self.inp.baked.sample(BakedMap::Id, self.x, self.y, self.res); + let hit = id.is_some_and(|c| colors.iter().any(|k| (k[0] - c[0]).abs().max((k[1] - c[1]).abs()).max((k[2] - c[2]).abs()) <= *tolerance)); + let val = if hit { 1.0 } else { 0.0 }; + v += (blend1(e.blend, v, val) - v) * o; + } + EffectKind::Compare { op, value, tolerance } => { + let pass = match op { + CompareOp::LesserThan => v < *value, + CompareOp::GreaterThan => v > *value, + CompareOp::WithinTolerance => (v - value).abs() <= *tolerance, + }; + let val = if pass { 1.0 } else { 0.0 }; + v += (val - v) * o; + } + // Content-only effects. + EffectKind::Material(_) | EffectKind::Anchor => {} + } + } + v.clamp(0.0, 1.0) + } + + /// Composite `nodes` over `acc`, recording anchors. Returns true when it reached the `stop` + /// layer (`acc` then holds the stack below it). + fn eval(&mut self, nodes: &[Node], acc: &mut [[f32; 3]; NCH]) -> bool { + for n in nodes { + if self.stop == Some(n.id) { + return true; + } + let m = self.node_mask(n); + match &n.content { + // A masked-out group still runs when it records anchors (they hold its inner + // stack) or may hold the stop layer. + Content::Group(children) if m > 0.0 || n.has_anchor || self.stop.is_some() => { + let mut inner = *acc; + if self.eval(children, &mut inner) { + return true; + } + if m > 0.0 { + for c in &n.chans { + if let (Some(a), Some(b)) = (acc.get(c.ch).copied(), inner.get(c.ch).copied()) + && let Some(slot) = acc.get_mut(c.ch) + { + *slot = mix3(a, b, c.opacity * m); + } + } + } + } + Content::Fill(_) | Content::Paint { .. } if m > 0.0 => { + for c in &n.chans { + if let Some((v, cov)) = self.content(&n.content, c) { + apply(acc, c, v, cov, m); + } + } + } + _ => {} + } + for a in &n.anchors { + self.anchors.push((*a, *acc)); + } + } + false + } + + /// The mask of node `n` at this texel, combined with the layer's height when it masks with + /// height. + fn node_mask(&mut self, n: &Node) -> f32 { + let m = match &n.mask { + Some(mp) => self.mask(mp), + None => 1.0, + }; + if !n.height_mask.enabled || m <= 0.0 || m >= 1.0 { + return m; + } + let height = Channel::Height.index(); + let h = n.chans.iter().find(|c| c.ch == height).and_then(|c| self.content(&n.content, c)).map_or(0.0, |(v, _)| v[0]); + n.height_mask.apply(m, h) + } + + /// Record the anchors of the stack below layer `id` (`full` is the planned stack), to + /// evaluate that layer on its own. + fn anchors_below(&mut self, full: &[Node], id: LayerId) { + self.anchors.clear(); + self.stop = Some(id); + let mut acc = Channel::ALL.map(Channel::default_value); + self.eval(full, &mut acc); + self.stop = None; + } + + /// Straight value and coverage of one channel of a paint or fill layer at this texel + /// (content effects and live stroke preview included). None where the layer is empty. + fn content(&mut self, content: &Content, c: &ChanPlan) -> Option<([f32; 3], f32)> { + let (mut v, mut cov, skip) = match c.start.and_then(|(k, f)| f.get(self.x, self.y).map(|d| (k, d))) { + Some((k, d)) => { + let px = |i: usize| d.get(i).copied().unwrap_or(0.0); + ([px(0), px(1), px(2)], px(3), k + 1) + } + None => { + let (v, cov) = match content { + Content::Fill(f) => { + let ch = Channel::ALL.get(c.ch).copied()?; + // A channel without a source fills with its default (no clone per texel). + let v = match f.sources.get(&ch) { + Some(s) => self.source(s, &f.projection, ch.kind()), + None => ch.default_value(), + }; + (v, 1.0) + } + Content::Paint { preview } => self.painted(c.data, c, *preview), + Content::Group(_) => return None, + }; + (v, cov, 0) + } + }; + for e in c.effects.iter().skip(skip) { + (v, cov) = self.content_effect(e, c, v, cov); + } + (cov > 0.0).then_some((v, cov.min(1.0))) + } + + /// Straight value and coverage of painted data of one channel (premultiplied values plus + /// coverage per texel), with the live stroke when `preview`. + fn painted(&mut self, data: Option<&[u16]>, c: &ChanPlan, preview: bool) -> ([f32; 3], f32) { + let pv = if preview { self.inp.preview } else { None }; + let mut sa = pv.map(|p| (self.stroke * p.opacity).clamp(0.0, 1.0)).unwrap_or(0.0); + // Projection: the stencil's alpha shapes the stroke and its color is the base color. + let stencil = match pv.and_then(|p| p.stencil.as_ref()) { + Some(st) if sa > 0.0 => { + let s = self.world().and_then(|w| st.sample_world(w)); + sa *= s.map(|(_, a)| a).unwrap_or(0.0); + s.map(|(c, _)| c) + } + _ => None, + }; + let mut val = [0.0f32; 3]; + let mut cov = 0.0f32; + if let Some(d) = data { + let base = self.o * (c.comps + 1); + for (k, slot) in val.iter_mut().take(c.comps).enumerate() { + *slot = decode(c.kind, d.get(base + k).copied().unwrap_or(0)); + } + cov = from_u16(d.get(base + c.comps).copied().unwrap_or(0)); + } + if let Some(p) = pv + && sa > 0.0 + { + if p.erase { + val = val.map(|v| v * (1.0 - sa)); + cov *= 1.0 - sa; + } else if let Some(ch) = Channel::ALL.get(c.ch) + && let Some(pval) = p.values.get(ch) + { + let pval = match stencil { + Some(col) if *ch == Channel::BaseColor => col, + _ => match p.carried.get(ch) { + Some(img) => { + let raw = img.get(self.x, self.y).map(|v| decode(c.kind, v)); + [raw[0], raw[1], raw[2]] + } + None => *pval, + }, + }; + for k in 0..c.comps { + val[k] = pval[k] * sa + val[k] * (1.0 - sa); + } + cov = sa + cov * (1.0 - sa); + } + } + if cov <= 0.0 { + return ([0.0; 3], 0.0); + } + let inv = 1.0 / cov.max(1e-5); + let v = if c.comps == 3 { val.map(|x| x * inv) } else { [val[0] * inv; 3] }; + (v, cov.min(1.0)) + } + + /// Apply one content effect to a channel's straight value and coverage. + fn content_effect(&mut self, e: &EffectPlan, c: &ChanPlan, v: [f32; 3], cov: f32) -> ([f32; 3], f32) { + let o = e.opacity.clamp(0.0, 1.0); + match e.kind { + EffectKind::Paint => { + let (p, pc) = self.painted(e.data, c, e.preview); + over(c, e.blend, v, cov, p, pc * o) + } + EffectKind::Material(f) => match Channel::ALL.get(c.ch).and_then(|ch| f.sources.get(ch).map(|s| (*ch, s))) { + Some((ch, s)) => { + let p = self.source(s, &f.projection, ch.kind()); + over(c, e.blend, v, cov, p, o) + } + None => (v, cov), + }, + EffectKind::Levels(l) => (mix3(v, per_unit(c.kind, v, |x| l.apply(x)), o), cov), + EffectKind::Filter(f) => { + let out = match f { + Filter::Invert => per_unit(c.kind, v, |x| 1.0 - x), + Filter::Threshold { level } => per_unit(c.kind, v, |x| if x >= *level { 1.0 } else { 0.0 }), + Filter::Hsl { hue, saturation, lightness } => match c.kind { + ChannelKind::Color => hsl_adjust(v, *hue, *saturation, *lightness), + _ => per_unit(c.kind, v, |x| hsl_adjust([x; 3], 0.0, 0.0, *lightness)[0]), + }, + // Neighbourhood filters come precomputed through `ChanPlan::start`. + Filter::Blur { .. } | Filter::Sharpen { .. } => v, + }; + (mix3(v, out, o), cov) + } + // Anchors are recorded by `eval`; mask-only effects leave content as it is. + EffectKind::Anchor | EffectKind::Fill { .. } | EffectKind::Generator(_) | EffectKind::ColorSelection { .. } | EffectKind::Compare { .. } => (v, cov), + } + } +} + +#[inline] +fn apply(acc: &mut [[f32; 3]; NCH], c: &ChanPlan, v: [f32; 3], cov: f32, mask: f32) { + let Some(a) = acc.get(c.ch).copied() else { return }; + let w = if c.blend == BlendMode::Replace { c.opacity * mask } else { cov * c.opacity * mask }; + let blended = blend_chan(c, c.blend, a, v); + if let Some(slot) = acc.get_mut(c.ch) { + *slot = mix3(a, blended, w); + } +} + +/// Composite `tiles` of texture set `inp.ts` into `out` (other tiles are left as they are). +pub fn composite_tiles(inp: &Inputs, out: &mut Composite, tiles: &[(u32, u32)]) { + composite_tiles_cached(inp, out, tiles, None); +} + +/// Tiles per side of the blocks a composite works through (bounds neighbourhood-filter memory). +const BLOCK: u32 = 16; + +/// [`composite_tiles`], serving the stack below the hot layer from `below` where it can (and +/// filling it). Results are identical to [`composite_tiles`]. +pub fn composite_tiles_cached(inp: &Inputs, out: &mut Composite, tiles: &[(u32, u32)], mut below: Option<&mut BelowCache>) { + let Ok(set) = inp.project.texture_set(inp.ts) else { return }; + let res = out.resolution; + if inp.gbuf.width != res || inp.gbuf.height != res { + return; + } + let split = below.as_deref_mut().and_then(|b| b.prepare(inp)); + // A pass over more tiles than the cache holds records only what fits: recording the rest + // would just evict it again. + let tile_bytes = TILE_AREA * set.channel_list().iter().map(|c| c.components()).sum::() * std::mem::size_of::(); + let fits = below.as_deref().is_some_and(|b| tiles.len().max(b.pass) <= b.budget / tile_bytes.max(1)); + let mut blocks: BTreeMap<(u32, u32), Vec<(u32, u32)>> = BTreeMap::new(); + for t in tiles { + blocks.entry((t.1 / BLOCK, t.0 / BLOCK)).or_default().push(*t); + } + for block in blocks.values() { + let quota = match below.as_deref() { + Some(_) if fits => usize::MAX, + Some(b) => b.budget.saturating_sub(b.bytes()) / tile_bytes.max(1), + None => 0, + }; + let snaps = composite_block(inp, &set.stack, out, block, below.as_deref().zip(split), quota); + if let Some(b) = below.as_deref_mut() { + for (t, snap) in snaps { + b.insert(t, snap); + } + } + } +} + +/// Rows of a tile composited as one parallel task. +const BAND: u32 = 16; + +/// One band of a tile: per-channel buffers, the stack-below values to cache (empty when not +/// recorded) and whether any texel is covered. +type BandResult = (Vec>, Vec, bool); + +/// Composite one block of tiles; returns the stack-below values to cache (for at most `quota` +/// tiles). +fn composite_block( + inp: &Inputs, + stack: &[LayerId], + out: &mut Composite, + tiles: &[(u32, u32)], + below: Option<(&BelowCache, usize)>, + quota: usize, +) -> Vec<((u32, u32), Vec)> { + let channels: Vec = inp.project.texture_set(inp.ts).map(|s| s.channel_list()).unwrap_or_default(); + let res = out.resolution; + // Layers below the split are only evaluated on tiles the cache lacks. + let (lo, hi) = stack.split_at(below.map_or(0, |(_, i)| i.min(stack.len()))); + let cached = |t: (u32, u32)| below.and_then(|(b, _)| b.tile(t)); + let fresh: Vec<(u32, u32)> = tiles.iter().copied().filter(|t| cached(*t).is_none()).collect(); + let recorded: std::collections::BTreeSet<(u32, u32)> = if lo.is_empty() { Default::default() } else { fresh.iter().copied().take(quota).collect() }; + let mut fields = fields_of(inp, &channels, lo, &fresh); + fields.extend(fields_of(inp, &channels, hi, tiles)); + let stride: usize = channels.iter().map(|c| c.components()).sum(); + // Tiles split into bands of rows, so a handful of tiles still keeps every thread busy. + let bands = TILE / BAND; + let tasks: Vec<((u32, u32), u32)> = tiles.iter().flat_map(|t| (0..bands).map(move |b| (*t, b))).collect(); + let results: Vec = tasks + .par_iter() + .map(|&((tx, ty), band)| { + let cached = cached((tx, ty)); + let record = recorded.contains(&(tx, ty)); + let lo_nodes = if cached.is_none() { plan(inp, lo, &channels, tx, ty, 0, Some(&fields)) } else { Vec::new() }; + let nodes = plan(inp, hi, &channels, tx, ty, 0, Some(&fields)); + let rows = (BAND * TILE) as usize; + let mut snap = if record { vec![0.0f32; rows * stride] } else { Vec::new() }; + let preview_tile = inp.preview.and_then(|p| p.alpha.tile(tx, ty)); + let mut bufs: Vec> = channels.iter().map(|c| vec![0u16; rows * c.components()]).collect(); + let defaults: [[f32; 3]; NCH] = Channel::ALL.map(Channel::default_value); + let mut anchors = Vec::new(); + let mut any = false; + for ly in band * BAND..(band + 1) * BAND { + let y = ty * TILE + ly; + if y >= res { + break; + } + for lx in 0..TILE { + let x = tx * TILE + lx; + if x >= res { + break; + } + let gi = (y * res + x) as usize; + // Offset in the tile, and in this band. + let o = (ly * TILE + lx) as usize; + let ob = ((ly - band * BAND) * TILE + lx) as usize; + let covered = inp.gbuf.tri.get(gi).is_some_and(|t| *t != NO_TRI); + let mut acc = defaults; + if covered { + any = true; + if let Some(c) = cached { + load_below(c, &channels, stride, o, &mut acc); + } + let mut tex = Texel::new(inp, x, y, o, stroke_at(preview_tile, o)); + // Reuse one anchor buffer per band. + tex.anchors = std::mem::take(&mut anchors); + tex.eval(&lo_nodes, &mut acc); + if record { + store_below(&mut snap, &channels, stride, ob, &acc); + } + tex.eval(&nodes, &mut acc); + anchors = tex.anchors; + anchors.clear(); + } + for (c, buf) in channels.iter().zip(&mut bufs) { + let v = acc.get(c.index()).copied().unwrap_or([0.0; 3]); + let n = c.components(); + for (k, x) in v.iter().take(n).enumerate() { + let enc = if c.kind() == ChannelKind::Signed { to_u16_signed(*x) } else { to_u16(*x) }; + if let Some(slot) = buf.get_mut(ob * n + k) { + *slot = enc; + } + } + } + } + } + (bufs, snap, any) + }) + .collect(); + // Join the bands of each tile (in parallel; uncovered tiles are freed). + type Joined = (Vec>>>, Option>); + let joined: Vec = results + .par_chunks(bands as usize) + .map(|parts| { + let any = parts.iter().any(|p| p.2); + let data = (0..channels.len()) + .map(|k| { + any.then(|| { + let mut v = Vec::with_capacity(parts.iter().map(|p| p.0.get(k).map_or(0, Vec::len)).sum()); + for p in parts { + v.extend_from_slice(p.0.get(k).map_or(&[][..], Vec::as_slice)); + } + std::sync::Arc::new(v) + }) + }) + .collect(); + let snap = parts.first().is_some_and(|p| !p.1.is_empty()).then(|| parts.iter().flat_map(|p| p.1.as_slice()).copied().collect()); + (data, snap) + }) + .collect(); + let mut snaps = Vec::new(); + for (&(tx, ty), (data, snap)) in tiles.iter().zip(joined) { + for (c, d) in channels.iter().zip(data) { + if let Some(img) = out.channels.get_mut(c) { + img.set_tile(tx, ty, d); + } + } + if let Some(snap) = snap { + snaps.push(((tx, ty), snap)); + } + } + snaps +} + +/// Restore the cached stack value of texel `o`. Scalars are cached once: only their first +/// component is ever read. +fn load_below(cache: &[f32], channels: &[Channel], stride: usize, o: usize, acc: &mut [[f32; 3]; NCH]) { + let mut i = o * stride; + for c in channels { + let n = c.components(); + if let (Some(src), Some(slot)) = (cache.get(i..i + n), acc.get_mut(c.index())) { + *slot = if n == 3 { [src[0], src[1], src[2]] } else { [src[0]; 3] }; + } + i += n; + } +} + +fn store_below(cache: &mut [f32], channels: &[Channel], stride: usize, o: usize, acc: &[[f32; 3]; NCH]) { + let mut i = o * stride; + for c in channels { + let n = c.components(); + if let (Some(dst), Some(v)) = (cache.get_mut(i..i + n), acc.get(c.index())) { + dst.copy_from_slice(&v[..n]); + } + i += n; + } +} + +/// UV padding for display: fill uncovered texels in `tiles` and their neighbours from their +/// nearest covered texel. Returns the tiles it wrote. +pub fn pad_tiles(out: &mut Composite, dmap: &DilationMap, tiles: &[(u32, u32)]) -> std::collections::BTreeSet<(u32, u32)> { + let mut written = std::collections::BTreeSet::new(); + let res = out.resolution; + if dmap.width != res { + return written; + } + let n = res.div_ceil(TILE) as i64; + let mut set = std::collections::BTreeSet::new(); + for &(tx, ty) in tiles { + for dy in -1..=1i64 { + for dx in -1..=1i64 { + let (x, y) = (tx as i64 + dx, ty as i64 + dy); + if x >= 0 && y >= 0 && x < n && y < n { + set.insert((x as u32, y as u32)); + } + } + } + } + let set: Vec<(u32, u32)> = set.into_iter().collect(); + for img in out.channels.values_mut() { + let comps = img.comps(); + let src: &TileImage = img; + // Padding writes only uncovered texels and reads only covered ones, so tiles pad + // independently (in parallel) from the unpadded image. + let padded: Vec<((u32, u32), Vec)> = set + .par_iter() + .filter_map(|&(tx, ty)| { + let mut data: Option> = None; + for ly in 0..TILE { + for lx in 0..TILE { + let (x, y) = (tx * TILE + lx, ty * TILE + ly); + if x >= res || y >= res { + continue; + } + let Some((sx, sy)) = dmap.source(x, y) else { continue }; + if (sx, sy) == (x, y) { + continue; + } + let v = src.get(sx, sy); + let d = data.get_or_insert_with(|| src.tile(tx, ty).map_or_else(|| vec![0; TILE_AREA * comps], <[u16]>::to_vec)); + let o = (ly * TILE + lx) as usize * comps; + if let Some(px) = d.get_mut(o..o + comps) { + px.copy_from_slice(&v[..comps]); + } + } + } + Some(((tx, ty), data?)) + }) + .collect(); + for ((tx, ty), d) in padded { + img.set_tile(tx, ty, Some(std::sync::Arc::new(d))); + written.insert((tx, ty)); + } + } + written +} + +/// The mask of layer `id` as an 8-bit grayscale image of `size × size` (nearest texel of the +/// texture set; uncovered texels black). None when the layer has no enabled mask. +pub fn mask_image(inp: &Inputs, id: LayerId, size: u32) -> Option> { + let mask = layer_mask(inp.project, id).filter(|m| m.enabled)?; + let channels = inp.project.texture_set(inp.ts).map(|t| t.channel_list()).unwrap_or_default(); + let res = inp.gbuf.width; + let all = all_tiles(res); + let fields = neighbourhood_fields(inp, &channels, &all); + let tiles: Vec<(u32, u32, Vec)> = all + .par_iter() + .filter_map(|&(tx, ty)| { + let mp = mask_plan(inp, id, mask, tx, ty, Some(&fields))?; + let full = anchor_plan(inp, id, &channels, tx, ty, Some(&fields)); + let mut buf = vec![0u8; TILE_AREA]; + for (x, y, o) in tile_texels(inp.gbuf, tx, ty) { + let mut tex = Texel::new(inp, x, y, o, 0.0); + if !full.is_empty() { + tex.anchors_below(&full, id); + } + if let Some(b) = buf.get_mut(o) { + *b = (tex.mask(&mp) * 255.0 + 0.5) as u8; + } + } + Some((tx, ty, buf)) + }) + .collect(); + let size = size.max(1); + let mut out = vec![0u8; (size * size) as usize]; + let lookup: std::collections::HashMap<(u32, u32), &Vec> = tiles.iter().map(|(x, y, b)| ((*x, *y), b)).collect(); + for y in 0..size { + for x in 0..size { + let (sx, sy) = ((x as u64 * res as u64 / size as u64) as u32, (y as u64 * res as u64 / size as u64) as u32); + if let Some(t) = lookup.get(&(sx / TILE, sy / TILE)) + && let Some(v) = t.get(((sy % TILE) * TILE + sx % TILE) as usize) + && let Some(d) = out.get_mut((y * size + x) as usize) + { + *d = *v; + } + } + } + Some(out) +} + +/// One flattened tile of one channel: (channel, tile, premultiplied value + coverage data). +pub type FlatTile = (Channel, (u32, u32), Vec); + +/// Flatten one paint or fill layer: its content (with content effects) times its mask, as +/// premultiplied (value, coverage) tiles per enabled channel, ready to become a plain paint +/// layer. Groups are not supported (None). +pub fn flatten_layer(inp: &Inputs, id: LayerId, tiles: &[(u32, u32)]) -> Option> { + let set = inp.project.texture_set(inp.ts).ok()?; + if inp.project.content_layer(id)?.is_group() { + return None; + } + let channels: Vec = set.channel_list(); + // Plan the layer forced visible so hidden layers flatten too. + let mut solo = inp.project.clone(); + if let Some(l) = solo.layers.get_mut(&id) { + l.visible = true; + } + let solo_inp = Inputs { project: &solo, preview: None, ..*inp }; + let fields = neighbourhood_fields(&solo_inp, &channels, tiles); + let out: Vec> = tiles + .par_iter() + .map(|&(tx, ty)| { + let nodes = plan(&solo_inp, &[id], &channels, tx, ty, 0, Some(&fields)); + let Some(n) = nodes.first() else { return Vec::new() }; + let full = anchor_plan(&solo_inp, id, &channels, tx, ty, Some(&fields)); + let mut bufs: Vec<(Channel, Vec, bool)> = n + .chans + .iter() + .filter_map(|c| Channel::ALL.get(c.ch).map(|ch| (*ch, vec![0u16; TILE_AREA * (c.comps + 1)], false))) + .collect(); + for (ch, buf, _) in &mut bufs { + if ch.kind() == ChannelKind::Signed { + for px in buf.chunks_exact_mut(2) { + px[0] = to_u16_signed(0.0); + } + } + } + for (x, y, o) in tile_texels(inp.gbuf, tx, ty) { + let mut tex = Texel::new(&solo_inp, x, y, o, 0.0); + if !full.is_empty() { + tex.anchors_below(&full, id); + } + let m = tex.node_mask(n); + for (c, (ch, buf, any)) in n.chans.iter().zip(bufs.iter_mut()) { + let Some((v, cov)) = tex.content(&n.content, c) else { continue }; + let a = (cov * m).clamp(0.0, 1.0); + if a <= 0.0 { + continue; + } + *any = true; + let comps = c.comps; + let Some(px) = buf.get_mut(o * (comps + 1)..(o + 1) * (comps + 1)) else { continue }; + for (slot, pv) in px.iter_mut().zip(v.iter().take(comps)) { + *slot = if ch.kind() == ChannelKind::Signed { to_u16_signed(pv * a) } else { to_u16(pv * a) }; + } + if let Some(slot) = px.get_mut(comps) { + *slot = to_u16(a); + } + } + } + bufs.into_iter().filter(|(_, _, any)| *any).map(|(ch, buf, _)| (ch, (tx, ty), buf)).collect() + }) + .collect(); + Some(out.into_iter().flatten().collect()) +} + +#[cfg(test)] +mod tests { + use super::*; + use texturecraft_core::{ChannelBlend, Effect, Layer, Procedural, ProceduralKind, TextureSet}; + use texturecraft_geom::primitives; + + struct Fixture { + project: Project, + mesh: Mesh, + gbuf: UvGBuffer, + pixels: PixelStore, + baked: BakedMaps, + } + + fn fixture() -> Fixture { + let mesh = primitives::plane(2.0, 2); + let gbuf = UvGBuffer::new(&mesh, 0, 64, 64); + let mut project = Project::new("t", "builtin:plane"); + project.texture_sets.push(TextureSet::new("Plane", 0, 64)); + Fixture { project, mesh, gbuf, pixels: PixelStore::default(), baked: BakedMaps::default() } + } + + fn run(f: &Fixture, preview: Option<&StrokePreview>) -> Composite { + let images = Default::default(); + let inp = Inputs { project: &f.project, ts: 0, mesh: &f.mesh, gbuf: &f.gbuf, pixels: &f.pixels, baked: &f.baked, preview, images: &images, bounds: f.mesh.bounds() }; + let mut out = Composite::new(&f.project.texture_sets[0].channel_list(), 64); + composite_tiles(&inp, &mut out, &all_tiles(64)); + out + } + + fn fill_layer(p: &mut Project, color: [f32; 3], rough: f32) -> LayerId { + let id = p.alloc_id(); + let mut fill = Fill::default(); + fill.sources.insert(Channel::BaseColor, Source::Uniform(color)); + fill.sources.insert(Channel::Roughness, Source::Uniform([rough; 3])); + let l = Layer::new(id, "Fill", LayerKind::Fill(fill), &[Channel::BaseColor, Channel::Roughness]); + p.insert_layer(0, l, None, false).unwrap() + } + + fn close(a: [f32; 3], b: [f32; 3]) -> bool { + (0..3).all(|i| (a[i] - b[i]).abs() < 2e-4) + } + + #[test] + fn empty_stack_gives_channel_defaults() { + let f = fixture(); + let out = run(&f, None); + assert!(close(out.value(Channel::BaseColor, 10, 10), [0.5; 3])); + assert!(close(out.value(Channel::Roughness, 10, 10), [0.5; 3])); + assert!(close(out.value(Channel::Normal, 10, 10), [0.5, 0.5, 1.0])); + } + + #[test] + fn fill_layers_blend_with_opacity_and_modes() { + let mut f = fixture(); + fill_layer(&mut f.project, [1.0, 0.0, 0.0], 0.2); + let top = fill_layer(&mut f.project, [0.0, 0.0, 1.0], 0.8); + f.project.layer_mut(top).unwrap().channels.insert(Channel::BaseColor, ChannelBlend { mode: BlendMode::Normal, opacity: 0.25 }); + let out = run(&f, None); + assert!(close(out.value(Channel::BaseColor, 5, 5), [0.75, 0.0, 0.25])); + assert!(close(out.value(Channel::Roughness, 5, 5), [0.8; 3])); + f.project.layer_mut(top).unwrap().channels.insert(Channel::BaseColor, ChannelBlend { mode: BlendMode::Multiply, opacity: 1.0 }); + let out = run(&f, None); + assert!(close(out.value(Channel::BaseColor, 5, 5), [0.0, 0.0, 0.0])); + } + + #[test] + fn paint_layers_unpremultiply_and_preview_and_masks() { + let mut f = fixture(); + let id = f.project.alloc_id(); + let l = Layer::new(id, "Paint", LayerKind::Paint, &[Channel::BaseColor]); + f.project.insert_layer(0, l, None, false).unwrap(); + // Half-covered red texel: premul (0.5, 0, 0), coverage 0.5. + let img = f.pixels.layer_mut(id, Channel::BaseColor, 64); + img.set(3, 3, &[to_u16(0.5), 0, 0, to_u16(0.5)]); + let out = run(&f, None); + assert!(close(out.value(Channel::BaseColor, 3, 3), [0.75, 0.25, 0.25])); + assert!(close(out.value(Channel::BaseColor, 4, 3), [0.5; 3])); + + // A live stroke painting blue at full alpha on texel (4, 3). + let mut alpha = TileImage::new(64, 64, 1); + alpha.set(4, 3, &[to_u16(1.0)]); + let pv = StrokePreview { + target: PaintTarget::Layer(id), + alpha, + opacity: 1.0, + erase: false, + values: [(Channel::BaseColor, [0.0, 0.0, 1.0])].into_iter().collect(), + mask_value: 1.0, + stencil: None, + carried: BTreeMap::new(), + }; + let out = run(&f, Some(&pv)); + assert!(close(out.value(Channel::BaseColor, 4, 3), [0.0, 0.0, 1.0])); + + // A black mask hides the layer. + f.project.layer_mut(id).unwrap().mask = Some(Mask::new(MaskBackground::Black)); + let out = run(&f, None); + assert!(close(out.value(Channel::BaseColor, 3, 3), [0.5; 3])); + } + + #[test] + fn mask_effects_and_procedural_fills_stay_in_range() { + let mut f = fixture(); + let id = fill_layer(&mut f.project, [1.0, 1.0, 1.0], 1.0); + let eid = f.project.alloc_id(); + let mut mask = Mask::new(MaskBackground::Black); + let pattern = Procedural::new(ProceduralKind::Checker); + mask.effects.push(Effect::new( + eid, + EffectKind::Fill { source: Source::Procedural { pattern, low: [0.0; 3], high: [1.0; 3] }, projection: Projection::default() }, + )); + f.project.layer_mut(id).unwrap().mask = Some(mask); + let out = run(&f, None); + let vals: Vec = (0..64).map(|x| out.value(Channel::BaseColor, x, 20)[0]).collect(); + assert!(vals.iter().any(|v| *v > 0.99) && vals.iter().any(|v| *v < 0.51), "{vals:?}"); + } + + #[test] + fn blur_filter_softens_mask_edges() { + let mut f = fixture(); + let id = fill_layer(&mut f.project, [1.0, 1.0, 1.0], 1.0); + let mut mask = Mask::new(MaskBackground::Black); + let pattern = Procedural::new(ProceduralKind::Checker); + let (e1, e2) = (f.project.alloc_id(), f.project.alloc_id()); + mask.effects.push(Effect::new(e1, EffectKind::Fill { source: Source::Procedural { pattern, low: [0.0; 3], high: [1.0; 3] }, projection: Projection::default() })); + f.project.layer_mut(id).unwrap().mask = Some(mask.clone()); + let hard: Vec = (0..64).map(|x| run(&f, None).value(Channel::BaseColor, x, 20)[0]).collect(); + mask.effects.push(Effect::new(e2, EffectKind::Filter(Filter::Blur { radius: 64.0 }))); + f.project.layer_mut(id).unwrap().mask = Some(mask); + let soft_img = run(&f, None); + let soft: Vec = (0..64).map(|x| soft_img.value(Channel::BaseColor, x, 20)[0]).collect(); + let mids = |v: &[f32]| v.iter().filter(|x| **x > 0.55 && **x < 0.95).count(); + assert!(mids(&soft) > mids(&hard) + 4, "hard {hard:?} +soft {soft:?}"); + } + + #[test] + fn groups_pass_through_with_opacity() { + let mut f = fixture(); + let gid = f.project.alloc_id(); + let g = Layer::new(gid, "G", LayerKind::Group { children: vec![], collapsed: false }, &[Channel::BaseColor]); + f.project.insert_layer(0, g, None, false).unwrap(); + f.project.layer_mut(gid).unwrap().channels.insert(Channel::BaseColor, ChannelBlend { mode: BlendMode::Passthrough, opacity: 0.5 }); + let cid = f.project.alloc_id(); + let mut fill = Fill::default(); + fill.sources.insert(Channel::BaseColor, Source::Uniform([1.0, 1.0, 1.0])); + let child = Layer::new(cid, "C", LayerKind::Fill(fill), &[Channel::BaseColor]); + f.project.insert_layer(0, child, Some(gid), true).unwrap(); + let out = run(&f, None); + assert!(close(out.value(Channel::BaseColor, 1, 1), [0.75; 3])); + } + + #[test] + fn padding_copies_nearest_covered_value() { + let mut out = Composite::new(&[Channel::Roughness], 64); + out.channels.get_mut(&Channel::Roughness).unwrap().set(10, 10, &[to_u16(0.9)]); + let mut covered = vec![false; 64 * 64]; + covered[10 * 64 + 10] = true; + let dmap = DilationMap::new(&covered, 64, 64, Some(4)); + pad_tiles(&mut out, &dmap, &[(0, 0)]); + assert!((out.value(Channel::Roughness, 12, 12)[0] - 0.9).abs() < 1e-3); + assert!(out.value(Channel::Roughness, 30, 30)[0] < 0.1 || !out.channels[&Channel::Roughness].is_allocated(0, 0)); + } + + fn content_fx(f: &mut Fixture, id: LayerId, kind: EffectKind) -> LayerId { + let eid = f.project.alloc_id(); + f.project.layer_mut(id).unwrap().content_effects.push(Effect::new(eid, kind)); + eid + } + + #[test] + fn content_filters_hsl_and_opacity_change_layer_values() { + let mut f = fixture(); + let id = fill_layer(&mut f.project, [1.0, 0.0, 0.0], 0.2); + content_fx(&mut f, id, EffectKind::Filter(Filter::Hsl { hue: 1.0 / 3.0, saturation: 0.0, lightness: 0.0 })); + let out = run(&f, None); + assert!(close(out.value(Channel::BaseColor, 5, 5), [0.0, 1.0, 0.0]), "{:?}", out.value(Channel::BaseColor, 5, 5)); + assert!(close(out.value(Channel::Roughness, 5, 5), [0.2; 3]), "a hue shift leaves scalars alone"); + content_fx(&mut f, id, EffectKind::Filter(Filter::Invert)); + let out = run(&f, None); + assert!(close(out.value(Channel::BaseColor, 5, 5), [1.0, 0.0, 1.0])); + assert!(close(out.value(Channel::Roughness, 5, 5), [0.8; 3])); + // Hidden effects are skipped and opacity fades an effect. + let fx = &mut f.project.layer_mut(id).unwrap().content_effects; + fx[0].visible = false; + fx[1].opacity = 0.5; + let out = run(&f, None); + assert!(close(out.value(Channel::BaseColor, 5, 5), [0.5; 3])); + assert!(close(out.value(Channel::Roughness, 5, 5), [0.5; 3])); + } + + #[test] + fn hsl_on_a_mask_changes_its_lightness() { + let mut f = fixture(); + let id = fill_layer(&mut f.project, [1.0; 3], 1.0); + let mut mask = Mask::new(MaskBackground::White); + mask.effects.push(Effect::new(f.project.alloc_id(), EffectKind::Filter(Filter::Hsl { hue: 0.3, saturation: 1.0, lightness: -0.5 }))); + f.project.layer_mut(id).unwrap().mask = Some(mask); + assert!(close(run(&f, None).value(Channel::BaseColor, 5, 5), [0.75; 3])); + } + + #[test] + fn content_blur_softens_fill_edges() { + let mut f = fixture(); + let id = f.project.alloc_id(); + let mut fill = Fill::default(); + fill.sources.insert(Channel::Roughness, Source::Procedural { pattern: Procedural::new(ProceduralKind::Checker), low: [0.0; 3], high: [1.0; 3] }); + f.project.insert_layer(0, Layer::new(id, "F", LayerKind::Fill(fill), &[Channel::Roughness]), None, false).unwrap(); + let row = |f: &Fixture| { + let out = run(f, None); + (0..64).map(|x| out.value(Channel::Roughness, x, 20)[0]).collect::>() + }; + let hard = row(&f); + content_fx(&mut f, id, EffectKind::Filter(Filter::Blur { radius: 64.0 })); + let soft = row(&f); + let mids = |v: &[f32]| v.iter().filter(|x| **x > 0.05 && **x < 0.95).count(); + assert!(mids(&soft) > mids(&hard) + 4, "hard {hard:?}\nsoft {soft:?}"); + assert!(soft.iter().all(|v| (0.0..=1.0).contains(v))); + } + + #[test] + fn content_paint_and_fill_effects_add_to_layers() { + let mut f = fixture(); + let id = f.project.alloc_id(); + f.project.insert_layer(0, Layer::new(id, "P", LayerKind::Paint, &[Channel::BaseColor, Channel::Roughness]), None, false).unwrap(); + let mut material = Fill::default(); + material.sources.insert(Channel::Roughness, Source::Uniform([0.2; 3])); + content_fx(&mut f, id, EffectKind::Material(material)); + let paint = content_fx(&mut f, id, EffectKind::Paint); + // Roughness 0.9 painted at half coverage (premultiplied). + f.pixels.layer_mut(paint, Channel::Roughness, 64).set(3, 3, &[to_u16(0.45), to_u16(0.5)]); + let out = run(&f, None); + assert!(close(out.value(Channel::Roughness, 10, 10), [0.2; 3]), "the fill effect covers the empty paint layer"); + assert!(close(out.value(Channel::Roughness, 3, 3), [0.55; 3]), "{:?}", out.value(Channel::Roughness, 3, 3)); + assert!(close(out.value(Channel::BaseColor, 3, 3), [0.5; 3]), "channels without a source stay untouched"); + // Flattening bakes the content effects in. + let images = Default::default(); + let inp = Inputs { project: &f.project, ts: 0, mesh: &f.mesh, gbuf: &f.gbuf, pixels: &f.pixels, baked: &f.baked, preview: None, images: &images, bounds: f.mesh.bounds() }; + let flat = flatten_layer(&inp, id, &all_tiles(64)).unwrap(); + assert!(flat.iter().all(|(c, _, _)| *c == Channel::Roughness)); + let (_, _, data) = flat.iter().find(|(_, t, _)| *t == (0, 0)).unwrap(); + let o = 3 * TILE as usize + 3; + assert!((from_u16(data[o * 2]) - 0.55).abs() < 1e-3 && (from_u16(data[o * 2 + 1]) - 1.0).abs() < 1e-3); + } + + #[test] + fn anchors_feed_masks_above_in_stack_order() { + let mut f = fixture(); + let low = f.project.alloc_id(); + let mut fill = Fill::default(); + fill.sources.insert(Channel::Height, Source::Uniform([0.4; 3])); + f.project.insert_layer(0, Layer::new(low, "Low", LayerKind::Fill(fill), &[Channel::Height]), None, false).unwrap(); + let anchor = content_fx(&mut f, low, EffectKind::Anchor); + let top = fill_layer(&mut f.project, [1.0; 3], 0.5); + let mut mask = Mask::new(MaskBackground::Black); + let source = Source::Anchor { anchor, channel: Channel::Height }; + mask.effects.push(Effect::new(f.project.alloc_id(), EffectKind::Fill { source, projection: Projection::default() })); + f.project.layer_mut(top).unwrap().mask = Some(mask.clone()); + assert!(close(run(&f, None).value(Channel::BaseColor, 5, 5), [0.7; 3])); + // Under a blur the anchor is read by the field pre-pass too: a flat height stays flat. + mask.effects.push(Effect::new(f.project.alloc_id(), EffectKind::Filter(Filter::Blur { radius: 32.0 }))); + f.project.layer_mut(top).unwrap().mask = Some(mask); + assert!(close(run(&f, None).value(Channel::BaseColor, 5, 5), [0.7; 3])); + // An anchor above its reader is not evaluated yet: it reads the channel default (0). + f.project.move_layer(low, 1).unwrap(); + assert!(close(run(&f, None).value(Channel::BaseColor, 5, 5), [0.5; 3])); + } + + #[test] + fn anchors_read_colors_as_gray_in_fill_layers() { + let mut f = fixture(); + let low = fill_layer(&mut f.project, [0.2, 0.4, 0.6], 0.5); + let anchor = content_fx(&mut f, low, EffectKind::Anchor); + let id = f.project.alloc_id(); + let mut fill = Fill::default(); + fill.sources.insert(Channel::Roughness, Source::Anchor { anchor, channel: Channel::BaseColor }); + f.project.insert_layer(0, Layer::new(id, "Top", LayerKind::Fill(fill), &[Channel::Roughness]), None, false).unwrap(); + let expect = texturecraft_core::lum([0.2, 0.4, 0.6]); + assert!(close(run(&f, None).value(Channel::Roughness, 9, 9), [expect; 3])); + } +} diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs new file mode 100644 index 00000000..e10ddbc1 --- /dev/null +++ b/crates/compose/src/lib.rs @@ -0,0 +1,19 @@ +//! # texturecraft-compose +//! +//! The CPU layer-stack evaluator: [`eval::composite_tiles`] turns a texture set's layers, masks, +//! fills and painted pixels into one image per channel, tile by tile in parallel, including the +//! live preview of a stroke being drawn. [`display`] converts composited tiles into the texture +//! formats the viewport samples; [`pattern`] evaluates procedurals through projections. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod below; +pub mod display; +pub mod eval; +pub mod pattern; +pub mod store; + +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, flatten_layer, mask_image, pad_tiles}; +pub use store::{BakedMaps, ImageRes, ImageStore, PixelStore}; diff --git a/crates/compose/src/pattern.rs b/crates/compose/src/pattern.rs new file mode 100644 index 00000000..74f80102 --- /dev/null +++ b/crates/compose/src/pattern.rs @@ -0,0 +1,298 @@ +//! Procedural patterns and projections. + +use texturecraft_core::{Procedural, ProceduralKind, Projection}; +use texturecraft_geom::glam::{Vec2, Vec3}; +use texturecraft_texel::noise; + +/// Where a texel is, for pattern evaluation. +#[derive(Clone, Copy, Debug)] +pub struct Coords { + pub uv: Vec2, + /// World position divided by the scene radius (scale-independent). + pub pos: Vec3, + pub nrm: Vec3, +} + +/// Raw pattern value at a 3D point, 0..1. +fn raw(kind: ProceduralKind, p: Vec3, seed: u32) -> f32 { + let a = p.to_array(); + match kind { + ProceduralKind::Clouds => noise::fbm(a, 6, seed, noise::value), + ProceduralKind::Perlin => noise::fbm(a, 4, seed, noise::perlin), + ProceduralKind::Noise => noise::value((p * 16.0).to_array(), seed), + ProceduralKind::Cells => noise::worley(a, seed), + ProceduralKind::Grunge => { + let n = noise::fbm(a, 6, seed, noise::perlin) * 0.7 + noise::fbm((p * 3.7).to_array(), 3, seed ^ 0x55, noise::value) * 0.3; + ((n - 0.45) * 4.0 + 0.5).clamp(0.0, 1.0) + } + ProceduralKind::Scratches => { + // Long thin streaks: noise stretched along one axis, sharpened to lines. + let q = Vec3::new(p.x * 0.15, p.y * 6.0, p.z * 6.0); + let n = noise::perlin(q.to_array(), seed); + let line = 1.0 - ((n - 0.5).abs() * 40.0).min(1.0); + let gate = noise::value((p * 0.8).to_array(), seed ^ 0x99); + line * ((gate - 0.45) * 4.0).clamp(0.0, 1.0) + } + ProceduralKind::Checker => { + let c = p.floor(); + if (c.x + c.y + c.z).rem_euclid(2.0) < 1.0 { 1.0 } else { 0.0 } + } + ProceduralKind::Stripes => { + let t = p.x.rem_euclid(1.0); + if t < 0.5 { 1.0 } else { 0.0 } + } + ProceduralKind::Gradient => p.x.rem_euclid(1.0), + ProceduralKind::Wood => { + // Rings across Y (warped by low-frequency noise), latewood darkening sharply before + // each ring boundary; fine fibers stretched along X. + let warp = noise::fbm([p.x * 0.18, p.y * 0.6, p.z], 3, seed, noise::perlin); + let t = ((p.y + (warp - 0.5) * 0.9) * 5.0).rem_euclid(1.0); + let ring = 1.0 - smoothstep(0.45, 1.0, t); + let fiber = noise::value([p.x * 2.0, p.y * 40.0, p.z], seed ^ 0x3C); + let tone = noise::fbm([p.x * 0.6, p.y * 3.0, p.z], 2, seed ^ 0x71, noise::value); + (ring * 0.6 + fiber * 0.25 + tone * 0.15).clamp(0.0, 1.0) + } + ProceduralKind::Bricks => { + // Bricks 1 × 0.5 in running bond; per-brick tone in the top 15 % so low/high map to + // mortar/brick colors without washing the bricks out. + let row = (p.y * 2.0).floor(); + let x = p.x + if (row as i32).rem_euclid(2) == 1 { 0.5 } else { 0.0 }; + let (col, fx, fy) = (x.floor(), x.rem_euclid(1.0), (p.y * 2.0).rem_euclid(1.0)); + let d = fx.min(1.0 - fx).min(fy.min(1.0 - fy) * 0.5); + let tone = noise::cell_hash(col as i32, row as i32, 0, seed) * 0.6 + noise::value((p * 9.0).to_array(), seed) * 0.4; + smoothstep(0.02, 0.045, d) * (0.85 + 0.15 * tone) + } + ProceduralKind::Tiles => { + let (c, f) = (p.floor(), p - p.floor()); + let d = f.x.min(1.0 - f.x).min(f.y.min(1.0 - f.y)); + smoothstep(0.015, 0.035, d) * (0.9 + 0.1 * noise::cell_hash(c.x as i32, c.y as i32, 0, seed)) + } + ProceduralKind::Weave => { + // Plain weave: in each cell the thread on top alternates (warp along X, weft along Y); + // the top thread shows its round cross-section, bulging mid-span. + let c = (p * 2.0).floor(); + let f = p * 2.0 - c; + let pi = std::f32::consts::PI; + let warp_on_top = (c.x as i32 + c.y as i32).rem_euclid(2) == 0; + let (across, along) = if warp_on_top { (f.y, f.x) } else { (f.x, f.y) }; + let thread = (pi * across).sin().max(0.0).sqrt() * (0.8 + 0.2 * (pi * along).sin()); + let fiber = noise::value([p.x * 40.0, p.y * 40.0, p.z], seed); + (thread * (0.88 + 0.12 * fiber)).clamp(0.0, 1.0) + } + ProceduralKind::Leather => { + let w = noise::perlin((p * 0.7).to_array(), seed ^ 0x2D) - 0.5; + let (f1, f2, _) = noise::cellular([p.x + w * 0.4, p.y - w * 0.4, p.z], seed); + let crease = smoothstep(0.0, 0.16, f2 - f1); + let pebble = 1.0 - f1 * 0.5; + let fine = noise::value((p * 7.0).to_array(), seed ^ 0x13); + (crease * pebble * (0.85 + 0.15 * fine)).clamp(0.0, 1.0) + } + ProceduralKind::Hammered => { + let dent = |q: Vec3, s: u32| { + let (f1, _, _) = noise::cellular(q.to_array(), s); + (f1 * 1.25).min(1.0).powi(2) + }; + (dent(p, seed) * 0.75 + dent(p * 2.3 + Vec3::splat(5.1), seed ^ 0x5A) * 0.25).clamp(0.0, 1.0) + } + ProceduralKind::Brushed => { + let a = noise::value([p.x * 1.5, p.y * 60.0, p.z], seed); + let b = noise::value([p.x * 0.4, p.y * 18.0, p.z], seed ^ 0x77); + (a * 0.6 + b * 0.4).clamp(0.0, 1.0) + } + ProceduralKind::Marble => { + let turb = noise::fbm(a, 5, seed, noise::perlin); + let veins = ((p.x + p.y * 0.5) * 2.5 + turb * 9.0).sin().abs().powf(0.3); + let cloud = noise::fbm((p * 2.0).to_array(), 3, seed ^ 0x19, noise::value); + (veins * 0.85 + cloud * 0.15).clamp(0.0, 1.0) + } + ProceduralKind::Voronoi => noise::cellular(a, seed).2, + ProceduralKind::Streaks => { + // Drips: thin columns along Y, each segment starting at a random height and fading + // as it runs down (Y is up), over two offset column grids. + let drip = |x: f32, salt: u32| { + let cx = x * 6.0; + let col = cx.floor() as i32; + let h = |k: i32| noise::cell_hash(col, k, salt as i32, seed); + let width = 0.08 + 0.25 * h(0); + let across = 1.0 - smoothstep(width * 0.3, width, (cx - cx.floor() - 0.5 - (h(1) - 0.5) * 0.5).abs()); + let yy = p.y * (0.35 + 0.4 * h(2)) + h(3) * 7.0; + let t = yy - yy.floor(); + let on = if noise::cell_hash(col, yy.floor() as i32, 3 + salt as i32, seed) > 0.5 { 1.0 } else { 0.0 }; + across * on * t.powf(1.6) * (1.0 - smoothstep(0.9, 1.0, t)) + }; + let fine = noise::value([p.x * 37.0, p.y * 1.3, p.z], seed ^ 0x2B); + (drip(p.x, 0).max(drip(p.x + 0.37, 1) * 0.8) * (0.6 + 0.4 * fine)).clamp(0.0, 1.0) + } + ProceduralKind::Smudges => { + // Sparse elliptical prints with concentric ridges, over soft greasy smudges. + let w = noise::perlin((p * 1.3).to_array(), seed ^ 0x61) - 0.5; + let q = Vec3::new(p.x * 1.15 + w * 0.3, p.y * 0.85 + w * 0.3, p.z); + let (f1, _, id) = noise::cellular(q.to_array(), seed); + let print = (1.0 - smoothstep(0.18 + 0.1 * id, 0.3 + 0.12 * id, f1)) * if id > 0.4 { 1.0 } else { 0.0 }; + let ridges = 0.5 + 0.5 * (f1 * 190.0 + w * 12.0).sin(); + let smudge = ((noise::fbm((p * 1.7).to_array(), 4, seed ^ 0x33, noise::value) - 0.55) * 3.0).clamp(0.0, 1.0); + (print * ridges * 0.9 + smudge * 0.4).clamp(0.0, 1.0) + } + ProceduralKind::Pores => { + let (f1, _, _) = noise::cellular(a, seed); + let (g1, _, _) = noise::cellular((p * 2.7 + Vec3::splat(13.0)).to_array(), seed ^ 0x6B); + smoothstep(0.08, 0.16, f1).min(smoothstep(0.06, 0.12, g1)) + } + ProceduralKind::Chips => { + let (f1, _, _) = noise::cellular((p * 2.0).to_array(), seed ^ 0x45); + let n = noise::fbm(a, 5, seed, noise::perlin) + (f1 - 0.5) * 0.25; + smoothstep(0.55, 0.58, n) + } + } +} + +fn smoothstep(e0: f32, e1: f32, x: f32) -> f32 { + let t = ((x - e0) / (e1 - e0)).clamp(0.0, 1.0); + t * t * (3.0 - 2.0 * t) +} + +fn shape(pr: &Procedural, v: f32) -> f32 { + let v = ((v - 0.5) * pr.contrast.max(0.0) + 0.5 + pr.balance).clamp(0.0, 1.0); + if pr.invert { 1.0 - v } else { v } +} + +/// Evaluate a procedural through a projection. +pub fn procedural(pr: &Procedural, proj: &Projection, c: &Coords) -> f32 { + let s = pr.scale.max(1e-4); + let v = match proj { + Projection::Uv { scale, rotation, offset } => { + let (sn, cs) = rotation.to_radians().sin_cos(); + let uv = Vec2::new(c.uv.x * cs - c.uv.y * sn, c.uv.x * sn + c.uv.y * cs) * Vec2::from(*scale) + Vec2::from(*offset); + // 2D patterns sample a slice of the 3D pattern. + raw(pr.kind, (uv * s).extend(0.0), pr.seed) + } + Projection::Triplanar { scale, sharpness, offset } => { + let p = c.pos * *scale * s + Vec3::from(*offset); + let w = c.nrm.abs().powf(sharpness.max(1.0)); + let w = w / (w.x + w.y + w.z).max(1e-6); + raw(pr.kind, Vec3::new(p.z, p.y, 0.0), pr.seed) * w.x + + raw(pr.kind, Vec3::new(p.x, p.z, 0.0), pr.seed) * w.y + + raw(pr.kind, Vec3::new(p.x, p.y, 0.0), pr.seed) * w.z + } + Projection::Planar { scale, axis, offset } => { + let p = c.pos * *scale * s + Vec3::from(*offset); + let q = match axis { + 0 => Vec3::new(p.z, p.y, 0.0), + 1 => Vec3::new(p.x, p.z, 0.0), + _ => Vec3::new(p.x, p.y, 0.0), + }; + raw(pr.kind, q, pr.seed) + } + Projection::Spherical { scale } => { + let d = c.pos.try_normalize().unwrap_or(Vec3::Y); + let u = d.z.atan2(d.x) / std::f32::consts::TAU + 0.5; + let v = d.y.clamp(-1.0, 1.0).acos() / std::f32::consts::PI; + raw(pr.kind, Vec3::new(u * 4.0, v * 2.0, 0.0) * *scale * s, pr.seed) + } + Projection::Cylindrical { scale, axis } => { + let p = c.pos; + let (a, h, b) = match axis { + 0 => (p.y, p.x, p.z), + 2 => (p.x, p.z, p.y), + _ => (p.x, p.y, p.z), + }; + let u = b.atan2(a) / std::f32::consts::TAU + 0.5; + raw(pr.kind, Vec3::new(u * 4.0, h, 0.0) * *scale * s, pr.seed) + } + }; + shape(pr, v) +} + +/// Texture coordinates (and blend weights) at which an image is sampled for a projection +/// (unused slots weigh 0; no allocation per texel). +pub fn image_samples(proj: &Projection, c: &Coords) -> [(Vec2, f32); 3] { + let one = |uv: Vec2| [(uv, 1.0), (Vec2::ZERO, 0.0), (Vec2::ZERO, 0.0)]; + match proj { + Projection::Uv { scale, rotation, offset } => { + let (sn, cs) = rotation.to_radians().sin_cos(); + let uv = Vec2::new(c.uv.x * cs - c.uv.y * sn, c.uv.x * sn + c.uv.y * cs) * Vec2::from(*scale) + Vec2::from(*offset); + one(uv) + } + Projection::Triplanar { scale, sharpness, offset } => { + let p = c.pos * *scale + Vec3::from(*offset); + let w = c.nrm.abs().powf(sharpness.max(1.0)); + let w = w / (w.x + w.y + w.z).max(1e-6); + [(Vec2::new(p.z, -p.y), w.x), (Vec2::new(p.x, p.z), w.y), (Vec2::new(p.x, -p.y), w.z)] + } + Projection::Planar { scale, axis, offset } => { + let p = c.pos * *scale + Vec3::from(*offset); + let q = match axis { + 0 => Vec2::new(p.z, -p.y), + 1 => Vec2::new(p.x, p.z), + _ => Vec2::new(p.x, -p.y), + }; + one(q * 0.5 + Vec2::splat(0.5)) + } + Projection::Spherical { scale } => { + let d = c.pos.try_normalize().unwrap_or(Vec3::Y); + let u = d.z.atan2(d.x) / std::f32::consts::TAU + 0.5; + let v = d.y.clamp(-1.0, 1.0).acos() / std::f32::consts::PI; + one(Vec2::new(u, v) * *scale) + } + Projection::Cylindrical { scale, axis } => { + let p = c.pos; + let (a, h, b) = match axis { + 0 => (p.y, p.x, p.z), + 2 => (p.x, p.z, p.y), + _ => (p.x, p.y, p.z), + }; + let u = b.atan2(a) / std::f32::consts::TAU + 0.5; + one(Vec2::new(u, 0.5 - h * 0.5) * *scale) + } + } +} + +/// World-space noise used by generators (seamless across UV islands). +pub fn world_noise(pos: Vec3, scale: f32, seed: u32) -> f32 { + noise::fbm((pos * scale.max(1e-3)).to_array(), 4, seed, noise::perlin) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_pattern_and_projection_stays_in_range() { + let projs = [ + Projection::default(), + Projection::Triplanar { scale: 1.0, sharpness: 4.0, offset: [0.0; 3] }, + Projection::Planar { scale: 1.0, axis: 1, offset: [0.0; 3] }, + Projection::Spherical { scale: 1.0 }, + Projection::Cylindrical { scale: 1.0, axis: 1 }, + ]; + for k in ProceduralKind::ALL { + let pr = Procedural::new(k); + for proj in &projs { + for i in 0..50 { + let t = i as f32 * 0.13; + let c = Coords { + uv: Vec2::new(t.fract(), (t * 0.7).fract()), + pos: Vec3::new(t.sin(), t.cos(), t * 0.1), + nrm: Vec3::new(0.3, 0.9, 0.1).normalize(), + }; + let v = procedural(&pr, proj, &c); + assert!((0.0..=1.0).contains(&v), "{k:?} {proj:?} {v}"); + } + } + } + } + + #[test] + fn every_pattern_varies() { + for k in ProceduralKind::ALL { + let pr = Procedural::new(k); + let (mut lo, mut hi) = (f32::MAX, f32::MIN); + for i in 0..4096 { + let uv = Vec2::new((i % 64) as f32 / 64.0, (i / 64) as f32 / 64.0); + let v = procedural(&pr, &Projection::default(), &Coords { uv, pos: Vec3::ZERO, nrm: Vec3::Y }); + (lo, hi) = (lo.min(v), hi.max(v)); + } + assert!(hi - lo > 0.3, "{k:?} is flat: {lo}..{hi}"); + } + } +} diff --git a/crates/compose/src/store.rs b/crates/compose/src/store.rs new file mode 100644 index 00000000..2a9292eb --- /dev/null +++ b/crates/compose/src/store.rs @@ -0,0 +1,120 @@ +//! Pixel data owned by a document: painted layer channels, painted mask effects and baked mesh +//! maps. Everything is copy-on-write, so cloning the store for undo is cheap. + +use std::collections::{BTreeMap, HashMap}; + +use texturecraft_core::{BakedMap, Channel, ChannelKind, LayerId}; +use texturecraft_texel::{TileImage, from_u16, from_u16_signed}; + +/// Painted data of one paint layer channel: premultiplied value(s) plus coverage, u16 per +/// component (height stored signed). +#[derive(Clone, Debug, Default)] +pub struct PixelStore { + /// (paint layer or content paint effect, channel) → texels. + pub layers: HashMap<(LayerId, Channel), TileImage>, + /// Paint effect of a mask → (premultiplied value, coverage). + pub masks: HashMap, +} + +impl PixelStore { + pub fn layer(&self, id: LayerId, c: Channel) -> Option<&TileImage> { + self.layers.get(&(id, c)) + } + + /// The image for `(id, c)` at `res`, created empty if missing or of another size. + pub fn layer_mut(&mut self, id: LayerId, c: Channel, res: u32) -> &mut TileImage { + let img = self.layers.entry((id, c)).or_insert_with(|| TileImage::new(res, res, c.components() + 1)); + if img.width() != res { + *img = TileImage::new(res, res, c.components() + 1); + } + img + } + + pub fn mask_mut(&mut self, id: LayerId, res: u32) -> &mut TileImage { + let img = self.masks.entry(id).or_insert_with(|| TileImage::new(res, res, 2)); + if img.width() != res { + *img = TileImage::new(res, res, 2); + } + img + } + + /// Drop pixels of layers that no longer exist. + pub fn retain(&mut self, alive: impl Fn(LayerId) -> bool) { + self.layers.retain(|(id, _), _| alive(*id)); + self.masks.retain(|id, _| alive(*id)); + } + + pub fn bytes(&self) -> usize { + self.layers.values().chain(self.masks.values()).map(TileImage::bytes).sum() + } +} + +/// Decode a stored component of channel kind `kind` to its float value. +#[inline] +pub fn decode(kind: ChannelKind, v: u16) -> f32 { + if kind == ChannelKind::Signed { from_u16_signed(v) } else { from_u16(v) } +} + +/// Baked mesh maps of one texture set (dense, row-major, `components()` floats per texel). +#[derive(Clone, Debug, Default, PartialEq)] +pub struct BakedMaps { + pub resolution: u32, + pub maps: BTreeMap>>, +} + +impl BakedMaps { + /// Sample map `m` at texel (x, y) of an image `res` wide (nearest texel of the bake). + pub fn sample(&self, m: BakedMap, x: u32, y: u32, res: u32) -> Option<[f32; 3]> { + let data = self.maps.get(&m)?; + let r = self.resolution.max(1) as u64; + let (bx, by) = ((x as u64 * r / res.max(1) as u64).min(r - 1), (y as u64 * r / res.max(1) as u64).min(r - 1)); + let comps = m.components(); + let i = ((by * r + bx) as usize) * comps; + let s = data.get(i..i + comps)?; + Some(if comps == 3 { [s[0], s[1], s[2]] } else { [s[0]; 3] }) + } + + pub fn scalar(&self, m: BakedMap, x: u32, y: u32, res: u32) -> Option { + self.sample(m, x, y, res).map(|v| v[0]) + } +} + +/// A decoded image resource sampled by fills (values in the file's encoding; see `srgb`). +#[derive(Clone, Debug, PartialEq)] +pub struct ImageRes { + pub width: u32, + pub height: u32, + pub rgba: Vec<[f32; 4]>, + /// 8/16-bit images are sRGB-encoded; color channels linearize them. + pub srgb: bool, +} + +/// Image resources by name. +pub type ImageStore = HashMap>; + +impl ImageRes { + fn texel(&self, x: i64, y: i64) -> [f32; 4] { + let (w, h) = (self.width.max(1) as i64, self.height.max(1) as i64); + let i = (y.rem_euclid(h) * w + x.rem_euclid(w)) as usize; + self.rgba.get(i).copied().unwrap_or([0.0; 4]) + } + + /// Bilinear sample with wrap-around (texture coordinates, v down). + pub fn sample(&self, u: f32, v: f32) -> [f32; 4] { + if !u.is_finite() || !v.is_finite() { + return [0.0; 4]; + } + let fx = u * self.width as f32 - 0.5; + let fy = v * self.height as f32 - 0.5; + let (x0, y0) = (fx.floor() as i64, fy.floor() as i64); + let (tx, ty) = (fx - fx.floor(), fy - fy.floor()); + let (a, b, c, d) = (self.texel(x0, y0), self.texel(x0 + 1, y0), self.texel(x0, y0 + 1), self.texel(x0 + 1, y0 + 1)); + let mut out = [0.0; 4]; + for k in 0..4 { + let top = a[k] + (b[k] - a[k]) * tx; + let bot = c[k] + (d[k] - c[k]) * tx; + out[k] = top + (bot - top) * ty; + } + out + } +} diff --git a/crates/compose/tests/cache_and_instances.rs b/crates/compose/tests/cache_and_instances.rs new file mode 100644 index 00000000..709eed91 --- /dev/null +++ b/crates/compose/tests/cache_and_instances.rs @@ -0,0 +1,201 @@ +//! The below-stack cache and blocked compositing give exactly the unoptimized result; instance +//! layers draw their source; masks combine with height. + +use texturecraft_compose::{BelowCache, Composite, Inputs, PixelStore, 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, +}; +use texturecraft_geom::{Mesh, UvGBuffer, primitives}; +use texturecraft_texel::{TILE, to_u16}; + +const RES: u32 = 256; + +struct Fixture { + project: Project, + mesh: Mesh, + gbuf: UvGBuffer, + pixels: PixelStore, +} + +fn fixture() -> Fixture { + let mesh = primitives::sphere(1.0, 32, 16); + let gbuf = UvGBuffer::new(&mesh, 0, RES, RES); + let mut project = Project::new("t", "builtin:sphere"); + project.texture_sets.push(TextureSet::new("A", 0, RES)); + project.texture_sets.push(TextureSet::new("B", 0, RES)); + Fixture { project, mesh, gbuf, pixels: PixelStore::default() } +} + +fn procedural(kind: ProceduralKind, low: f32, high: f32) -> Source { + Source::Procedural { pattern: Procedural::new(kind), low: [low; 3], high: [high; 3] } +} + +fn add(f: &mut Fixture, ts: usize, kind: LayerKind, mask: Option) -> LayerId { + let id = f.project.alloc_id(); + let mut l = Layer::new(id, "l", kind, &Channel::DEFAULT_SET); + l.mask = mask; + f.project.insert_layer(ts, l, None, false).unwrap() +} + +fn fill(sources: &[(Channel, Source)]) -> LayerKind { + let mut f = Fill::default(); + f.sources = sources.iter().cloned().collect(); + LayerKind::Fill(f) +} + +fn effect(f: &mut Fixture, kind: EffectKind) -> Effect { + Effect::new(f.project.alloc_id(), kind) +} + +/// Procedural fills, a generator mask with a blur, a folder, painted pixels; returns the top +/// paint layer. +fn heavy_stack(f: &mut Fixture) -> LayerId { + add( + f, + 0, + fill(&[(Channel::BaseColor, procedural(ProceduralKind::Clouds, 0.1, 0.9)), (Channel::Height, procedural(ProceduralKind::Cells, -0.3, 0.3))]), + None, + ); + let mut mask = Mask::new(MaskBackground::Black); + mask.effects.push(effect(f, EffectKind::Generator(Generator::new(GeneratorKind::Position)))); + mask.effects.push(effect(f, EffectKind::Filter(Filter::Blur { radius: 40.0 }))); + add(f, 0, fill(&[(Channel::Roughness, procedural(ProceduralKind::Grunge, 0.0, 1.0))]), Some(mask)); + let group = add(f, 0, LayerKind::Group { children: vec![], collapsed: false }, None); + for b in f.project.layer_mut(group).unwrap().channels.values_mut() { + b.mode = BlendMode::Passthrough; + } + let inner = f.project.alloc_id(); + let l = Layer::new(inner, "inner", fill(&[(Channel::Metallic, procedural(ProceduralKind::Scratches, 0.0, 1.0))]), &Channel::DEFAULT_SET); + f.project.insert_layer(0, l, Some(group), true).unwrap(); + let mut blurred = Mask::new(MaskBackground::Black); + blurred.effects.push(effect(f, EffectKind::Fill { source: procedural(ProceduralKind::Checker, 0.0, 1.0), projection: Projection::default() })); + blurred.effects.push(effect(f, EffectKind::Filter(Filter::Blur { radius: 24.0 }))); + let top = add(f, 0, LayerKind::Paint, Some(blurred)); + let img = f.pixels.layer_mut(top, Channel::BaseColor, RES); + for y in 40..200 { + for x in 30..220 { + img.set(x, y, &[to_u16(0.4), to_u16(0.1), 0, to_u16(0.5)]); + } + } + top +} + +fn inputs<'a>(f: &'a Fixture, ts: usize, images: &'a texturecraft_compose::ImageStore, baked: &'a texturecraft_compose::BakedMaps) -> Inputs<'a> { + Inputs { project: &f.project, ts, mesh: &f.mesh, gbuf: &f.gbuf, pixels: &f.pixels, baked, preview: None, images, bounds: f.mesh.bounds() } +} + +fn run(f: &Fixture, ts: usize, tiles: &[(u32, u32)], below: Option<&mut BelowCache>) -> Composite { + let (images, baked) = (Default::default(), Default::default()); + let mut out = Composite::new(&f.project.texture_sets[ts].channel_list(), RES); + composite_tiles_cached(&inputs(f, ts, &images, &baked), &mut out, tiles, below); + out +} + +fn reference(f: &Fixture, ts: usize) -> Composite { + let (images, baked) = (Default::default(), Default::default()); + let mut out = Composite::new(&f.project.texture_sets[ts].channel_list(), RES); + composite_tiles(&inputs(f, ts, &images, &baked), &mut out, &all_tiles(RES)); + out +} + +#[test] +fn below_cache_and_blocks_match_the_full_composite_exactly() { + let mut f = fixture(); + let top = heavy_stack(&mut f); + let want = reference(&f, 0); + // Tile by tile (each its own block and neighbourhood region) gives the same bits. + let mut one_by_one = Composite::new(&f.project.texture_sets[0].channel_list(), RES); + let (images, baked) = (Default::default(), Default::default()); + for t in all_tiles(RES) { + composite_tiles(&inputs(&f, 0, &images, &baked), &mut one_by_one, &[t]); + } + assert!(one_by_one == want, "per-tile composites differ from one pass"); + + let mut cache = BelowCache::default(); + cache.set_hot(Some(top)); + let half: Vec<(u32, u32)> = all_tiles(RES).into_iter().filter(|t| (t.0 + t.1) % 2 == 0).collect(); + // Cold, half warm and fully warm caches. + let mut out = run(&f, 0, &half, Some(&mut cache)); + assert!(!cache.is_empty(), "the stack below the hot layer is cached"); + composite_tiles_cached(&inputs(&f, 0, &images, &baked), &mut out, &all_tiles(RES), Some(&mut cache)); + assert!(out == want, "a partly cached composite differs"); + let warm = run(&f, 0, &all_tiles(RES), Some(&mut cache)); + assert!(warm == want, "a cached composite differs"); + + // Painting the hot layer keeps the cache; editing below it empties it. + f.pixels.layer_mut(top, Channel::BaseColor, RES).set(5, 5, &[to_u16(1.0), 0, 0, to_u16(1.0)]); + let n = cache.len(); + assert!(run(&f, 0, &all_tiles(RES), Some(&mut cache)) == reference(&f, 0)); + assert_eq!(cache.len(), n); + let low = f.project.texture_sets[0].stack[0]; + if let LayerKind::Fill(fl) = &mut f.project.layer_mut(low).unwrap().kind { + fl.sources.insert(Channel::Roughness, Source::Uniform([0.9; 3])); + } + assert!(run(&f, 0, &all_tiles(RES), Some(&mut cache)) == reference(&f, 0), "a stale cache was used"); + // A hot layer inside a folder caches below the folder. + let group = f.project.texture_sets[0].stack[2]; + let inner = f.project.layer(group).unwrap().children()[0]; + cache.set_hot(Some(inner)); + run(&f, 0, &all_tiles(RES), Some(&mut cache)); + assert!(!cache.is_empty()); + assert!(run(&f, 0, &all_tiles(RES), Some(&mut cache)) == reference(&f, 0)); +} + +#[test] +fn instances_draw_their_source_across_texture_sets() { + let mut f = fixture(); + let top = heavy_stack(&mut f); + // Set B instances every top-level layer of set A: same composite. + for id in f.project.texture_sets[0].stack.clone() { + add(&mut f, 1, LayerKind::Instance { source: id }, None); + } + let a = reference(&f, 0); + assert!(reference(&f, 1).channels == a.channels, "instances draw like their sources"); + // An edit to the source shows in the instance. + f.pixels.layer_mut(top, Channel::BaseColor, RES).set(100, 100, &[to_u16(1.0), 0, 0, to_u16(1.0)]); + let (a, b) = (reference(&f, 0), reference(&f, 1)); + assert!(b.channels == a.channels); + // The instance's own mask overrides the source's. + let inst = *f.project.texture_sets[1].stack.last().unwrap(); + f.project.layer_mut(inst).unwrap().mask = Some(Mask::new(MaskBackground::Black)); + let b = reference(&f, 1); + let (x, y) = (100, 100); + assert!((a.value(Channel::BaseColor, x, y)[0] - b.value(Channel::BaseColor, x, y)[0]).abs() > 0.1); + // A hidden instance or a missing source draws nothing. + let mut g = fixture(); + let src = add(&mut g, 0, fill(&[(Channel::Roughness, Source::Uniform([0.9; 3]))]), None); + let inst = add(&mut g, 1, LayerKind::Instance { source: src }, None); + assert!((reference(&g, 1).value(Channel::Roughness, 128, 128)[0] - 0.9).abs() < 1e-3); + g.project.layer_mut(inst).unwrap().visible = false; + assert!((reference(&g, 1).value(Channel::Roughness, 128, 128)[0] - 0.5).abs() < 1e-3); + g.project.layer_mut(inst).unwrap().visible = true; + g.project.layers.remove(&src); + assert!((reference(&g, 1).value(Channel::Roughness, 128, 128)[0] - 0.5).abs() < 1e-3); +} + +#[test] +fn mask_with_height_follows_the_layer_height() { + let mut f = fixture(); + // A white layer over the default gray, half masked, its height falling from 0.25 (left) to + // -0.25 (right). + let mut ramp = Procedural::new(ProceduralKind::Gradient); + ramp.scale = 1.0; + let height = Source::Procedural { pattern: ramp, low: [0.25; 3], high: [-0.25; 3] }; + let id = add(&mut f, 0, fill(&[(Channel::BaseColor, Source::Uniform([1.0; 3])), (Channel::Height, height)]), None); + let mut mask = Mask::new(MaskBackground::Black); + mask.effects.push(effect(&mut f, EffectKind::Fill { source: Source::Uniform([0.5; 3]), projection: Projection::default() })); + f.project.layer_mut(id).unwrap().mask = Some(mask); + let plain = reference(&f, 0); + f.project.layer_mut(id).unwrap().height_mask = HeightMask { enabled: true, contrast: 1.0, offset: 0.0 }; + let hm = reference(&f, 0); + let row = TILE * 2; + let (left, right) = (16, RES - 16); + let v = |c: &Composite, x| c.value(Channel::BaseColor, x, row)[0]; + assert!((v(&plain, left) - 0.75).abs() < 0.01 && (v(&plain, right) - 0.75).abs() < 0.01, "plain mask: 50 % white over gray"); + assert!(v(&hm, left) > 0.84, "high parts show first: {}", v(&hm, left)); + assert!(v(&hm, right) < 0.66, "low parts later: {}", v(&hm, right)); + // Without a mask there is nothing to combine. + f.project.layer_mut(id).unwrap().mask.as_mut().unwrap().enabled = false; + assert!((v(&reference(&f, 0), left) - 1.0).abs() < 1e-3); +} diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml new file mode 100644 index 00000000..c4fa8e1d --- /dev/null +++ b/crates/core/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "texturecraft-core" +description = "TextureCraft document model: projects, texture sets, channels, layers, masks, effects and blend math" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lints] +workspace = true + +[dependencies] +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } diff --git a/crates/core/src/blend.rs b/crates/core/src/blend.rs new file mode 100644 index 00000000..e43fd4b8 --- /dev/null +++ b/crates/core/src/blend.rs @@ -0,0 +1,441 @@ +//! Per-channel blend modes and their CPU reference math (straight values, linear space). +//! +//! `a` is the value below (the stack so far), `b` the layer's value. The compositor mixes the +//! result back over `a` by `coverage × opacity × mask`. + +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Default)] +pub enum BlendMode { + #[default] + Normal, + Passthrough, + Disable, + Replace, + Multiply, + Divide, + InverseDivide, + Darken, + Lighten, + LinearDodge, + Subtract, + InverseSubtract, + Difference, + Exclusion, + SignedAddition, + Overlay, + Screen, + LinearBurn, + ColorBurn, + ColorDodge, + SoftLight, + HardLight, + VividLight, + LinearLight, + PinLight, + Tint, + Saturation, + Color, + Value, + NormalMapCombine, + NormalMapDetail, + NormalMapInverseDetail, +} + +impl BlendMode { + /// Every mode in menu order; `None` entries are the separators between menu groups. + pub const MENU: [Option; 39] = [ + Some(BlendMode::Normal), + Some(BlendMode::Passthrough), + Some(BlendMode::Disable), + Some(BlendMode::Replace), + None, + Some(BlendMode::Multiply), + Some(BlendMode::Divide), + Some(BlendMode::InverseDivide), + Some(BlendMode::Darken), + Some(BlendMode::Lighten), + None, + Some(BlendMode::LinearDodge), + Some(BlendMode::Subtract), + Some(BlendMode::InverseSubtract), + Some(BlendMode::Difference), + Some(BlendMode::Exclusion), + Some(BlendMode::SignedAddition), + None, + Some(BlendMode::Overlay), + Some(BlendMode::Screen), + Some(BlendMode::LinearBurn), + Some(BlendMode::ColorBurn), + Some(BlendMode::ColorDodge), + None, + Some(BlendMode::SoftLight), + Some(BlendMode::HardLight), + Some(BlendMode::VividLight), + Some(BlendMode::LinearLight), + Some(BlendMode::PinLight), + None, + Some(BlendMode::Tint), + Some(BlendMode::Saturation), + Some(BlendMode::Color), + Some(BlendMode::Value), + None, + Some(BlendMode::NormalMapCombine), + Some(BlendMode::NormalMapDetail), + Some(BlendMode::NormalMapInverseDetail), + None, + ]; + + pub fn all() -> impl Iterator { + BlendMode::MENU.into_iter().flatten() + } + + pub fn label(self) -> &'static str { + match self { + BlendMode::Normal => "Normal", + BlendMode::Passthrough => "Passthrough", + BlendMode::Disable => "Disable", + BlendMode::Replace => "Replace", + BlendMode::Multiply => "Multiply", + BlendMode::Divide => "Divide", + BlendMode::InverseDivide => "Inverse divide", + BlendMode::Darken => "Darken (Min)", + BlendMode::Lighten => "Lighten (Max)", + BlendMode::LinearDodge => "Linear dodge (Add)", + BlendMode::Subtract => "Subtract", + BlendMode::InverseSubtract => "Inverse Subtract", + BlendMode::Difference => "Difference", + BlendMode::Exclusion => "Exclusion", + BlendMode::SignedAddition => "Signed addition (AddSub)", + BlendMode::Overlay => "Overlay", + BlendMode::Screen => "Screen", + BlendMode::LinearBurn => "Linear burn", + BlendMode::ColorBurn => "Color burn", + BlendMode::ColorDodge => "Color dodge", + BlendMode::SoftLight => "Soft light", + BlendMode::HardLight => "Hard light", + BlendMode::VividLight => "Vivid light", + BlendMode::LinearLight => "Linear light", + BlendMode::PinLight => "Pin light", + BlendMode::Tint => "Tint", + BlendMode::Saturation => "Saturation", + BlendMode::Color => "Color", + BlendMode::Value => "Value", + BlendMode::NormalMapCombine => "Normal map combine", + BlendMode::NormalMapDetail => "Normal map detail", + BlendMode::NormalMapInverseDetail => "Normal map inverse detail", + } + } + + /// Four-letter abbreviation shown in the compact layer row ("Norm", "Mult", …). + pub fn short(self) -> &'static str { + match self { + BlendMode::Normal => "Norm", + BlendMode::Passthrough => "Pass", + BlendMode::Disable => "Dis", + BlendMode::Replace => "Repl", + BlendMode::Multiply => "Mult", + BlendMode::Divide => "Div", + BlendMode::InverseDivide => "IDiv", + BlendMode::Darken => "Dark", + BlendMode::Lighten => "Ligh", + BlendMode::LinearDodge => "Add", + BlendMode::Subtract => "Sub", + BlendMode::InverseSubtract => "ISub", + BlendMode::Difference => "Diff", + BlendMode::Exclusion => "Excl", + BlendMode::SignedAddition => "AdSb", + BlendMode::Overlay => "Over", + BlendMode::Screen => "Scrn", + BlendMode::LinearBurn => "LBrn", + BlendMode::ColorBurn => "CBrn", + BlendMode::ColorDodge => "CDdg", + BlendMode::SoftLight => "SftL", + BlendMode::HardLight => "HrdL", + BlendMode::VividLight => "VivL", + BlendMode::LinearLight => "LinL", + BlendMode::PinLight => "PinL", + BlendMode::Tint => "Tint", + BlendMode::Saturation => "Sat", + BlendMode::Color => "Colr", + BlendMode::Value => "Val", + BlendMode::NormalMapCombine => "NmCb", + BlendMode::NormalMapDetail => "NmDt", + BlendMode::NormalMapInverseDetail => "NmID", + } + } + + /// Parse a label, short label or variant name (case-insensitive; punctuation ignored). + pub fn parse(s: &str) -> Option { + let norm = |x: &str| x.to_ascii_lowercase().replace([' ', '_', '-', '(', ')'], ""); + let want = norm(s); + BlendMode::all().find(|m| norm(m.label()) == want || norm(m.short()) == want || norm(&format!("{m:?}")) == want) + } + + /// Modes that need all three components at once. + pub fn is_non_separable(self) -> bool { + matches!( + self, + BlendMode::Tint + | BlendMode::Saturation + | BlendMode::Color + | BlendMode::Value + | BlendMode::NormalMapCombine + | BlendMode::NormalMapDetail + | BlendMode::NormalMapInverseDetail + ) + } +} + +fn color_burn(a: f32, b: f32) -> f32 { + if a >= 1.0 { + 1.0 + } else if b <= 0.0 { + 0.0 + } else { + 1.0 - ((1.0 - a) / b).min(1.0) + } +} + +fn color_dodge(a: f32, b: f32) -> f32 { + if a <= 0.0 { + 0.0 + } else if b >= 1.0 { + 1.0 + } else { + (a / (1.0 - b)).min(1.0) + } +} + +fn soft_light(a: f32, b: f32) -> f32 { + let (a, b) = (a.clamp(0.0, 1.0), b.clamp(0.0, 1.0)); + if b <= 0.5 { + return a - (1.0 - 2.0 * b) * a * (1.0 - a); + } + let d = if a <= 0.25 { ((16.0 * a - 12.0) * a + 4.0) * a } else { a.sqrt() }; + a + (2.0 * b - 1.0) * (d - a) +} + +fn overlay(a: f32, b: f32) -> f32 { + if a < 0.5 { 2.0 * a * b } else { 1.0 - 2.0 * (1.0 - a) * (1.0 - b) } +} + +/// One component of a separable blend. Non-separable modes fall back to `Normal`. +pub fn blend1(mode: BlendMode, a: f32, b: f32) -> f32 { + match mode { + BlendMode::Normal | BlendMode::Passthrough | BlendMode::Replace => b, + BlendMode::Disable => a, + BlendMode::Multiply => a * b, + BlendMode::Divide => a / b.max(1e-6), + BlendMode::InverseDivide => b / a.max(1e-6), + BlendMode::Darken => a.min(b), + BlendMode::Lighten => a.max(b), + BlendMode::LinearDodge => a + b, + BlendMode::Subtract => a - b, + BlendMode::InverseSubtract => b - a, + BlendMode::Difference => (a - b).abs(), + BlendMode::Exclusion => a + b - 2.0 * a * b, + BlendMode::SignedAddition | BlendMode::LinearLight => a + 2.0 * b - 1.0, + BlendMode::Overlay => overlay(a, b), + BlendMode::Screen => 1.0 - (1.0 - a) * (1.0 - b), + BlendMode::LinearBurn => a + b - 1.0, + BlendMode::ColorBurn => color_burn(a, b), + BlendMode::ColorDodge => color_dodge(a, b), + BlendMode::SoftLight => soft_light(a, b), + BlendMode::HardLight => overlay(b, a), + BlendMode::VividLight => { + if b < 0.5 { + color_burn(a, 2.0 * b) + } else { + color_dodge(a, 2.0 * (b - 0.5)) + } + } + BlendMode::PinLight => { + if b < 0.5 { + a.min(2.0 * b) + } else { + a.max(2.0 * b - 1.0) + } + } + BlendMode::Tint + | BlendMode::Saturation + | BlendMode::Color + | BlendMode::Value + | BlendMode::NormalMapCombine + | BlendMode::NormalMapDetail + | BlendMode::NormalMapInverseDetail => b, + } +} + +/// Luminance of a color (the W3C non-separable blend weights). +pub fn lum(c: [f32; 3]) -> f32 { + 0.3 * c[0] + 0.59 * c[1] + 0.11 * c[2] +} + +fn clip_color(c: [f32; 3]) -> [f32; 3] { + let l = lum(c); + let n = c[0].min(c[1]).min(c[2]); + let x = c[0].max(c[1]).max(c[2]); + let mut out = c; + if n < 0.0 && l - n > 1e-6 { + out = out.map(|v| l + (v - l) * l / (l - n)); + } + if x > 1.0 && x - l > 1e-6 { + out = out.map(|v| l + (v - l) * (1.0 - l) / (x - l)); + } + out +} + +fn set_lum(c: [f32; 3], l: f32) -> [f32; 3] { + let d = l - lum(c); + clip_color(c.map(|v| v + d)) +} + +fn sat(c: [f32; 3]) -> f32 { + c[0].max(c[1]).max(c[2]) - c[0].min(c[1]).min(c[2]) +} + +fn set_sat(c: [f32; 3], s: f32) -> [f32; 3] { + let mx = c[0].max(c[1]).max(c[2]); + let mn = c[0].min(c[1]).min(c[2]); + if mx - mn <= 1e-6 { + return [0.0; 3]; + } + c.map(|v| (v - mn) * s / (mx - mn)) +} + +fn unpack(n: [f32; 3]) -> [f32; 3] { + n.map(|v| v * 2.0 - 1.0) +} + +fn pack(n: [f32; 3]) -> [f32; 3] { + let len = (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]).sqrt().max(1e-6); + n.map(|v| v / len * 0.5 + 0.5) +} + +/// Reoriented normal mapping (Barré-Brisebois & Hill): `detail` re-oriented onto `base`. +fn rnm(base: [f32; 3], detail: [f32; 3]) -> [f32; 3] { + let t = unpack(base); + let t = [t[0], t[1], t[2] + 1.0]; + let u = unpack(detail); + let u = [-u[0], -u[1], u[2]]; + let d = t[0] * u[0] + t[1] * u[1] + t[2] * u[2]; + pack([t[0] * d - u[0] * t[2], t[1] * d - u[1] * t[2], t[2] * d - u[2] * t[2]]) +} + +/// A three-component blend, including the non-separable modes. +pub fn blend3(mode: BlendMode, a: [f32; 3], b: [f32; 3]) -> [f32; 3] { + match mode { + BlendMode::Color => set_lum(b, lum(a)), + BlendMode::Saturation => set_lum(set_sat(a, sat(b)), lum(a)), + BlendMode::Value => { + let va = a[0].max(a[1]).max(a[2]); + let vb = b[0].max(b[1]).max(b[2]); + if va <= 1e-6 { [vb; 3] } else { a.map(|v| v * vb / va) } + } + BlendMode::Tint => { + let l = lum(a); + b.map(|v| v * l) + } + BlendMode::NormalMapCombine => { + let (na, nb) = (unpack(a), unpack(b)); + pack([na[0] + nb[0], na[1] + nb[1], na[2] * nb[2]]) + } + BlendMode::NormalMapDetail => rnm(a, b), + BlendMode::NormalMapInverseDetail => rnm(b, a), + _ => [blend1(mode, a[0], b[0]), blend1(mode, a[1], b[1]), blend1(mode, a[2], b[2])], + } +} + +/// Mix `blended` over `a` by weight `w` (coverage × opacity × mask). +pub fn mix3(a: [f32; 3], blended: [f32; 3], w: f32) -> [f32; 3] { + [a[0] + (blended[0] - a[0]) * w, a[1] + (blended[1] - a[1]) * w, a[2] + (blended[2] - a[2]) * w] +} + +#[cfg(test)] +mod tests { + use super::*; + + const AS: [f32; 4] = [0.0, 0.25, 0.5, 1.0]; + const BS: [f32; 4] = [0.0, 0.3, 0.7, 1.0]; + + fn close(a: f32, b: f32) -> bool { + (a - b).abs() < 1e-5 + } + + #[test] + fn separable_modes_match_hand_computed_values() { + for a in AS { + for b in BS { + assert!(close(blend1(BlendMode::Normal, a, b), b)); + assert!(close(blend1(BlendMode::Disable, a, b), a)); + assert!(close(blend1(BlendMode::Multiply, a, b), a * b)); + assert!(close(blend1(BlendMode::Screen, a, b), 1.0 - (1.0 - a) * (1.0 - b))); + assert!(close(blend1(BlendMode::LinearDodge, a, b), a + b)); + assert!(close(blend1(BlendMode::Subtract, a, b), a - b)); + assert!(close(blend1(BlendMode::Darken, a, b), a.min(b))); + assert!(close(blend1(BlendMode::Lighten, a, b), a.max(b))); + assert!(close(blend1(BlendMode::Difference, a, b), (a - b).abs())); + assert!(close(blend1(BlendMode::Exclusion, a, b), a + b - 2.0 * a * b)); + } + } + assert!(close(blend1(BlendMode::Overlay, 0.25, 0.3), 2.0 * 0.25 * 0.3)); + assert!(close(blend1(BlendMode::Overlay, 0.5, 0.7), 1.0 - 2.0 * 0.5 * 0.3)); + assert!(close(blend1(BlendMode::HardLight, 0.7, 0.25), 2.0 * 0.25 * 0.7)); + } + + #[test] + fn soft_light_matches_w3c() { + assert!(close(blend1(BlendMode::SoftLight, 0.25, 0.3), 0.25 - 0.4 * 0.25 * 0.75)); + let d = ((16.0 * 0.25 - 12.0) * 0.25 + 4.0) * 0.25; + assert!(close(blend1(BlendMode::SoftLight, 0.25, 0.7), 0.25 + 0.4 * (d - 0.25))); + assert!(close(blend1(BlendMode::SoftLight, 0.5, 0.7), 0.5 + 0.4 * (0.5f32.sqrt() - 0.5))); + } + + #[test] + fn burns_and_dodges_handle_edges() { + assert!(close(blend1(BlendMode::ColorBurn, 1.0, 0.0), 1.0)); + assert!(close(blend1(BlendMode::ColorBurn, 0.5, 0.0), 0.0)); + assert!(close(blend1(BlendMode::ColorDodge, 0.0, 1.0), 0.0)); + assert!(close(blend1(BlendMode::ColorDodge, 0.5, 1.0), 1.0)); + assert!(close(blend1(BlendMode::ColorDodge, 0.25, 0.5), 0.5)); + for m in BlendMode::all() { + for a in AS { + for b in BS { + assert!(blend1(m, a, b).is_finite(), "{m:?} {a} {b}"); + assert!(blend3(m, [a; 3], [b; 3]).iter().all(|v| v.is_finite()), "{m:?}"); + } + } + } + } + + #[test] + fn color_keeps_lower_luminance() { + let a = [0.2, 0.4, 0.6]; + let out = blend3(BlendMode::Color, a, [1.0, 0.0, 0.0]); + assert!(close(lum(out), lum(a))); + } + + #[test] + fn flat_normal_detail_is_identity() { + let flat = [0.5, 0.5, 1.0]; + let n = pack([0.3, -0.2, 0.9]); + for m in [BlendMode::NormalMapDetail, BlendMode::NormalMapCombine] { + let out = blend3(m, n, flat); + for i in 0..3 { + assert!((out[i] - n[i]).abs() < 1e-4, "{m:?} {out:?} {n:?}"); + } + } + } + + #[test] + fn menu_lists_each_mode_once_and_parse_round_trips() { + let all: Vec<_> = BlendMode::all().collect(); + assert_eq!(all.len(), 32); + for m in &all { + assert_eq!(all.iter().filter(|x| *x == m).count(), 1); + assert_eq!(BlendMode::parse(m.label()), Some(*m)); + assert_eq!(BlendMode::parse(&format!("{m:?}")), Some(*m)); + } + } +} diff --git a/crates/core/src/channel.rs b/crates/core/src/channel.rs new file mode 100644 index 00000000..ed8a1392 --- /dev/null +++ b/crates/core/src/channel.rs @@ -0,0 +1,206 @@ +//! Material channels of a texture set. + +use serde::{Deserialize, Serialize}; + +/// A material channel. Order is the display order in the UI. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +pub enum Channel { + BaseColor, + Metallic, + Roughness, + Normal, + Height, + Emissive, + Opacity, + AmbientOcclusion, + SpecularLevel, + /// Specular/glossiness workflow: the F0 color. + Specular, + /// Specular/glossiness workflow: 1 − roughness. + Glossiness, +} + +/// How a channel's values are stored and interpreted. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ChannelKind { + /// Linear RGB color, displayed and exported sRGB-encoded. + Color, + /// Tangent-space normal packed into 0..1 (`n * 0.5 + 0.5`). + NormalMap, + /// A 0..1 scalar. + Scalar, + /// A signed scalar (height). + Signed, +} + +impl Channel { + pub const ALL: [Channel; 11] = [ + Channel::BaseColor, + Channel::Metallic, + Channel::Roughness, + Channel::Normal, + Channel::Height, + Channel::Emissive, + Channel::Opacity, + Channel::AmbientOcclusion, + Channel::SpecularLevel, + Channel::Specular, + Channel::Glossiness, + ]; + + /// The channels of the default "PBR - Metallic Roughness" template. + pub const DEFAULT_SET: [Channel; 5] = [Channel::BaseColor, Channel::Metallic, Channel::Roughness, Channel::Normal, Channel::Height]; + + pub fn kind(self) -> ChannelKind { + match self { + Channel::BaseColor | Channel::Emissive | Channel::Specular => ChannelKind::Color, + Channel::Normal => ChannelKind::NormalMap, + Channel::Height => ChannelKind::Signed, + _ => ChannelKind::Scalar, + } + } + + /// Value components (without coverage): 3 for colors and normals, 1 for scalars. + pub fn components(self) -> usize { + match self.kind() { + ChannelKind::Color | ChannelKind::NormalMap => 3, + ChannelKind::Scalar | ChannelKind::Signed => 1, + } + } + + /// The value a texel has when no layer covers it. + pub fn default_value(self) -> [f32; 3] { + match self { + Channel::BaseColor => [0.5, 0.5, 0.5], + Channel::Metallic | Channel::Height | Channel::Emissive => [0.0; 3], + Channel::Roughness => [0.5; 3], + Channel::Normal => [0.5, 0.5, 1.0], + Channel::Opacity | Channel::AmbientOcclusion => [1.0; 3], + Channel::SpecularLevel | Channel::Glossiness => [0.5; 3], + // F0 of a common dielectric (4 %). + Channel::Specular => [0.04; 3], + } + } + + pub fn label(self) -> &'static str { + match self { + Channel::BaseColor => "Base color", + Channel::Metallic => "Metallic", + Channel::Roughness => "Roughness", + Channel::Normal => "Normal", + Channel::Height => "Height", + Channel::Emissive => "Emissive", + Channel::Opacity => "Opacity", + Channel::AmbientOcclusion => "Ambient occlusion", + Channel::SpecularLevel => "Specular level", + Channel::Specular => "Specular", + Channel::Glossiness => "Glossiness", + } + } + + /// Short label used on material channel toggle buttons. + pub fn short(self) -> &'static str { + match self { + Channel::BaseColor => "color", + Channel::Metallic => "metal", + Channel::Roughness => "rough", + Channel::Normal => "nrm", + Channel::Height => "height", + Channel::Emissive => "emiss", + Channel::Opacity => "op", + Channel::AmbientOcclusion => "ao", + Channel::SpecularLevel => "spec", + Channel::Specular => "specular", + Channel::Glossiness => "gloss", + } + } + + /// Stable machine id (commands, export templates, file names). + pub fn id(self) -> &'static str { + match self { + Channel::BaseColor => "baseColor", + Channel::Metallic => "metallic", + Channel::Roughness => "roughness", + Channel::Normal => "normal", + Channel::Height => "height", + Channel::Emissive => "emissive", + Channel::Opacity => "opacity", + Channel::AmbientOcclusion => "ambientOcclusion", + Channel::SpecularLevel => "specularLevel", + Channel::Specular => "specular", + Channel::Glossiness => "glossiness", + } + } + + /// Parse an id, label or short label (case-insensitive, spaces/underscores ignored). + pub fn parse(s: &str) -> Option { + let norm = |x: &str| x.to_ascii_lowercase().replace([' ', '_', '-'], ""); + let want = norm(s); + Channel::ALL.into_iter().find(|c| norm(c.id()) == want || norm(c.label()) == want || norm(c.short()) == want || norm(&format!("{c:?}")) == want) + } + + pub fn index(self) -> usize { + Channel::ALL.iter().position(|c| *c == self).unwrap_or(0) + } +} + +/// Storage format of a channel in a texture set (what export and the UI report). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum ChannelFormat { + Srgb8, + L8, + Rgb8, + L16, + Rgb16, + L16F, + Rgb16F, + L32F, + Rgb32F, +} + +impl ChannelFormat { + pub fn default_for(c: Channel) -> ChannelFormat { + match c.kind() { + ChannelKind::Color => ChannelFormat::Srgb8, + ChannelKind::NormalMap => ChannelFormat::Rgb16F, + ChannelKind::Scalar => ChannelFormat::L8, + ChannelKind::Signed => ChannelFormat::L16F, + } + } + pub fn label(self) -> &'static str { + match self { + ChannelFormat::Srgb8 => "sRGB8", + ChannelFormat::L8 => "L8", + ChannelFormat::Rgb8 => "RGB8", + ChannelFormat::L16 => "L16", + ChannelFormat::Rgb16 => "RGB16", + ChannelFormat::L16F => "L16F", + ChannelFormat::Rgb16F => "RGB16F", + ChannelFormat::L32F => "L32F", + ChannelFormat::Rgb32F => "RGB32F", + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_accepts_ids_labels_and_shorts() { + for c in Channel::ALL { + assert_eq!(Channel::parse(c.id()), Some(c)); + assert_eq!(Channel::parse(c.label()), Some(c)); + assert_eq!(Channel::parse(c.short()), Some(c)); + } + assert_eq!(Channel::parse("base color"), Some(Channel::BaseColor)); + assert_eq!(Channel::parse("nope"), None); + } + + #[test] + fn index_matches_all_order() { + for (i, c) in Channel::ALL.iter().enumerate() { + assert_eq!(c.index(), i); + } + } +} diff --git a/crates/core/src/layer.rs b/crates/core/src/layer.rs new file mode 100644 index 00000000..916b7cd7 --- /dev/null +++ b/crates/core/src/layer.rs @@ -0,0 +1,716 @@ +//! Layers, masks, effects and fill sources. + +use std::collections::BTreeMap; + +use serde::{Deserialize, Serialize}; + +use crate::{BlendMode, Channel}; + +/// Id of a layer or effect (one id space per project). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +pub struct LayerId(pub u64); + +/// Blend mode and opacity of one channel of a layer. A channel missing from +/// [`Layer::channels`] is disabled for that layer. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +pub struct ChannelBlend { + pub mode: BlendMode, + pub opacity: f32, +} + +impl Default for ChannelBlend { + fn default() -> Self { + ChannelBlend { mode: BlendMode::Normal, opacity: 1.0 } + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Layer { + pub id: LayerId, + pub name: String, + pub visible: bool, + pub kind: LayerKind, + pub channels: BTreeMap, + #[serde(default)] + pub mask: Option, + /// Effects applied to the layer's content, bottom → top. + #[serde(default)] + pub content_effects: Vec, + /// Painter-style color tag (0..8), shown as a stripe on the layer row. + #[serde(default)] + pub color_tag: Option, + /// Combine the mask with the layer's height ("mask with height"). + #[serde(default)] + pub height_mask: HeightMask, +} + +/// Mask with height: partial mask values follow the layer's own height, so a transition +/// reveals the high parts first. With mask `m` and the layer's height `h` (−1..1): +/// `m' = saturate(m + contrast · 4m(1 − m) · (h + offset))`. Fully hidden (0) and fully shown +/// (1) texels are unchanged, as is everything where the layer has no height. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default)] +pub struct HeightMask { + pub enabled: bool, + /// How strongly the height pushes the mask (0 = plain mask), 0..64. + pub contrast: f32, + /// Added to the height before it pushes the mask, −1..1. + pub offset: f32, +} + +impl Default for HeightMask { + fn default() -> Self { + HeightMask { enabled: false, contrast: 1.0, offset: 0.0 } + } +} + +impl HeightMask { + /// The mask `m` combined with height `h` (see [`HeightMask`]). + pub fn apply(&self, m: f32, h: f32) -> f32 { + if !self.enabled || !h.is_finite() { + return m; + } + (m + self.contrast * 4.0 * m * (1.0 - m) * (h + self.offset)).clamp(0.0, 1.0) + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum LayerKind { + /// Painted pixels, stored per channel by the engine under the layer id. + Paint, + /// A procedural or uniform material applied everywhere (shaped by the mask). + Fill(Fill), + /// A folder of layers, bottom → top. + Group { children: Vec, collapsed: bool }, + /// A linked instance of `source` (possibly in another texture set): renders the source's + /// content, content effects and channel blending, and its mask unless the instance has its + /// own. Edits to the source show in every instance. + Instance { source: LayerId }, +} + +impl LayerKind { + pub fn name(&self) -> &'static str { + match self { + LayerKind::Paint => "paint", + LayerKind::Fill(_) => "fill", + LayerKind::Group { .. } => "group", + LayerKind::Instance { .. } => "instance", + } + } +} + +impl Layer { + /// A layer with every channel of `channels` enabled at Normal/100%. + pub fn new(id: LayerId, name: impl Into, kind: LayerKind, channels: &[Channel]) -> Layer { + Layer { + id, + name: name.into(), + visible: true, + kind, + channels: channels.iter().map(|c| (*c, ChannelBlend::default())).collect(), + mask: None, + content_effects: Vec::new(), + color_tag: None, + height_mask: HeightMask::default(), + } + } + + pub fn is_group(&self) -> bool { + matches!(self.kind, LayerKind::Group { .. }) + } + + pub fn children(&self) -> &[LayerId] { + match &self.kind { + LayerKind::Group { children, .. } => children, + _ => &[], + } + } + + pub fn blend(&self, c: Channel) -> Option { + self.channels.get(&c).copied() + } + + /// The layer this one instances, if it is an instance. + pub fn instance_source(&self) -> Option { + match self.kind { + LayerKind::Instance { source } => Some(source), + _ => None, + } + } +} + +/// Where a texture or procedural is sampled from on the surface. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum Projection { + /// Texture coordinates of the mesh. + Uv { scale: [f32; 2], rotation: f32, offset: [f32; 2] }, + /// World-space box projection blended by the normal. + Triplanar { scale: f32, sharpness: f32, offset: [f32; 3] }, + /// World-space planar projection along an axis (0 = X, 1 = Y, 2 = Z). + Planar { scale: f32, axis: u8, offset: [f32; 3] }, + /// Spherical projection around the mesh center. + Spherical { scale: f32 }, + /// Cylindrical projection around an axis. + Cylindrical { scale: f32, axis: u8 }, +} + +impl Default for Projection { + fn default() -> Self { + Projection::Uv { scale: [1.0, 1.0], rotation: 0.0, offset: [0.0, 0.0] } + } +} + +impl Projection { + pub fn label(&self) -> &'static str { + match self { + Projection::Uv { .. } => "UV projection", + Projection::Triplanar { .. } => "Tri-planar projection", + Projection::Planar { .. } => "Planar projection", + Projection::Spherical { .. } => "Spherical projection", + Projection::Cylindrical { .. } => "Cylindrical projection", + } + } +} + +/// Procedural patterns available to fills, generators and masks. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum ProceduralKind { + /// Smooth fractal value noise. + Clouds, + /// Gradient (Perlin-style) noise. + Perlin, + /// Fine high-frequency noise. + Noise, + /// Worley / cellular noise. + Cells, + /// Blotchy grunge: thresholded multi-scale noise. + Grunge, + /// Thin random scratches. + Scratches, + /// Checkerboard. + Checker, + /// Stripes. + Stripes, + /// Linear gradient along U (or the projection axis). + Gradient, + /// Wood grain: growth rings with fibers along X. + Wood, + /// Running-bond bricks with mortar joints (0 = mortar). + Bricks, + /// Square tiles with grout joints (0 = grout). + Tiles, + /// Plain fabric weave. + Weave, + /// Leather grain: pebbled cells with creases. + Leather, + /// Hammered metal dimples. + Hammered, + /// Fine brushed-metal lines along X. + Brushed, + /// Marble veins. + Marble, + /// Flat random tone per Voronoi cell (crystals, spangle, stones). + Voronoi, + /// Vertical streaks and leaks (along the projection's up axis). + Streaks, + /// Fingerprints and smudges. + Smudges, + /// Small round pores and pits (0 = pore). + Pores, + /// Sharp-edged paint chips. + Chips, +} + +impl ProceduralKind { + pub const ALL: [ProceduralKind; 22] = [ + ProceduralKind::Clouds, + ProceduralKind::Perlin, + ProceduralKind::Noise, + ProceduralKind::Cells, + ProceduralKind::Grunge, + ProceduralKind::Scratches, + ProceduralKind::Checker, + ProceduralKind::Stripes, + ProceduralKind::Gradient, + ProceduralKind::Wood, + ProceduralKind::Bricks, + ProceduralKind::Tiles, + ProceduralKind::Weave, + ProceduralKind::Leather, + ProceduralKind::Hammered, + ProceduralKind::Brushed, + ProceduralKind::Marble, + ProceduralKind::Voronoi, + ProceduralKind::Streaks, + ProceduralKind::Smudges, + ProceduralKind::Pores, + ProceduralKind::Chips, + ]; + pub fn label(self) -> &'static str { + match self { + ProceduralKind::Clouds => "Clouds", + ProceduralKind::Perlin => "Perlin noise", + ProceduralKind::Noise => "Fine noise", + ProceduralKind::Cells => "Cells", + ProceduralKind::Grunge => "Grunge", + ProceduralKind::Scratches => "Scratches", + ProceduralKind::Checker => "Checker", + ProceduralKind::Stripes => "Stripes", + ProceduralKind::Gradient => "Gradient", + ProceduralKind::Wood => "Wood grain", + ProceduralKind::Bricks => "Bricks", + ProceduralKind::Tiles => "Tiles", + ProceduralKind::Weave => "Fabric weave", + ProceduralKind::Leather => "Leather grain", + ProceduralKind::Hammered => "Hammered", + ProceduralKind::Brushed => "Brushed lines", + ProceduralKind::Marble => "Marble veins", + ProceduralKind::Voronoi => "Voronoi", + ProceduralKind::Streaks => "Streaks", + ProceduralKind::Smudges => "Fingerprints", + ProceduralKind::Pores => "Pores", + ProceduralKind::Chips => "Paint chips", + } + } + pub fn parse(s: &str) -> Option { + let norm = |x: &str| x.to_ascii_lowercase().replace([' ', '_', '-'], ""); + ProceduralKind::ALL.into_iter().find(|k| norm(k.label()) == norm(s) || norm(&format!("{k:?}")) == norm(s)) + } +} + +/// A procedural grayscale pattern. Its output maps 0..1 between `low` and `high` values. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Procedural { + pub kind: ProceduralKind, + /// Pattern frequency (repeats per projection unit). + pub scale: f32, + pub seed: u32, + /// Contrast around 0.5 (1 = unchanged). + pub contrast: f32, + /// Shifts the pattern brighter (+) or darker (−). + pub balance: f32, + pub invert: bool, +} + +impl Procedural { + pub fn new(kind: ProceduralKind) -> Procedural { + Procedural { kind, scale: 4.0, seed: 1, contrast: 1.0, balance: 0.0, invert: false } + } +} + +/// What a fill writes into one channel. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum Source { + /// A constant value (linear RGB for colors; `[v, v, v]` for scalars). + Uniform([f32; 3]), + /// A procedural pattern mixing two values. + Procedural { pattern: Procedural, low: [f32; 3], high: [f32; 3] }, + /// An imported image resource, by name. + Image { name: String }, + /// The stack value recorded by an anchor point effect (`anchor` is the effect id) for + /// `channel`; colors read as gray where a scalar is needed. + Anchor { anchor: LayerId, channel: Channel }, +} + +impl Source { + pub fn uniform_of(c: Channel) -> Source { + Source::Uniform(c.default_value()) + } + + /// The anchor point this source reads, if any. + pub fn anchor(&self) -> Option { + match self { + Source::Anchor { anchor, .. } => Some(*anchor), + _ => None, + } + } +} + +/// A fill layer's material: one source per channel, plus how it is projected. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)] +pub struct Fill { + pub sources: BTreeMap, + pub projection: Projection, +} + +impl Fill { + /// Value used for `c` when the fill has no source for it: the channel default. + pub fn source(&self, c: Channel) -> Source { + self.sources.get(&c).cloned().unwrap_or_else(|| Source::uniform_of(c)) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum MaskBackground { + #[default] + White, + Black, +} + +/// A layer mask: a background color with a stack of effects on top. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Mask { + pub enabled: bool, + pub background: MaskBackground, + /// Effects bottom → top. Paint effects hold bitmap data keyed by their id. + pub effects: Vec, +} + +impl Mask { + pub fn new(background: MaskBackground) -> Mask { + Mask { enabled: true, background, effects: Vec::new() } + } +} + +/// Levels remap: input range, gamma, output range. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +pub struct Levels { + pub in_low: f32, + pub in_high: f32, + pub gamma: f32, + pub out_low: f32, + pub out_high: f32, +} + +impl Default for Levels { + fn default() -> Self { + Levels { in_low: 0.0, in_high: 1.0, gamma: 1.0, out_low: 0.0, out_high: 1.0 } + } +} + +impl Levels { + pub fn apply(&self, x: f32) -> f32 { + let t = ((x - self.in_low) / (self.in_high - self.in_low).max(1e-5)).clamp(0.0, 1.0); + let t = t.powf(1.0 / self.gamma.max(1e-3)); + self.out_low + (self.out_high - self.out_low) * t + } +} + +/// Mesh-map driven mask generators (the "smart mask" building blocks). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum GeneratorKind { + /// Convex edges from the curvature map. + EdgeWear, + /// Crevices from ambient occlusion. + Dirt, + /// Thin parts from the thickness map. + ThinAreas, + /// Raw curvature. + Curvature, + /// Raw ambient occlusion. + AmbientOcclusion, + /// Gradient along a world axis (bottom → top by default). + Position, + /// Surfaces facing a direction (e.g. up for dust). + Direction, +} + +impl GeneratorKind { + pub const ALL: [GeneratorKind; 7] = [ + GeneratorKind::EdgeWear, + GeneratorKind::Dirt, + GeneratorKind::ThinAreas, + GeneratorKind::Curvature, + GeneratorKind::AmbientOcclusion, + GeneratorKind::Position, + GeneratorKind::Direction, + ]; + pub fn label(self) -> &'static str { + match self { + GeneratorKind::EdgeWear => "Mask Editor: Edge wear", + GeneratorKind::Dirt => "Dirt", + GeneratorKind::ThinAreas => "Thin areas", + GeneratorKind::Curvature => "Curvature", + GeneratorKind::AmbientOcclusion => "Ambient occlusion", + GeneratorKind::Position => "Position gradient", + GeneratorKind::Direction => "Light / direction", + } + } + pub fn parse(s: &str) -> Option { + 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)) + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Generator { + pub kind: GeneratorKind, + /// Where the mask starts (0..1, generator specific threshold). + pub level: f32, + /// Edge softness (0 = hard). + pub softness: f32, + /// Breaks the result up with world-space noise (0 = none). + pub noise_amount: f32, + pub noise_scale: f32, + pub seed: u32, + /// Direction for `Position` / `Direction` generators (world space). + pub direction: [f32; 3], + pub invert: bool, +} + +impl Generator { + pub fn new(kind: GeneratorKind) -> Generator { + let (level, softness, noise_amount) = match kind { + GeneratorKind::EdgeWear => (0.6, 0.12, 0.5), + GeneratorKind::Dirt => (0.55, 0.25, 0.35), + GeneratorKind::ThinAreas => (0.3, 0.2, 0.0), + _ => (0.5, 0.25, 0.0), + }; + Generator { kind, level, softness, noise_amount, noise_scale: 8.0, seed: 7, direction: [0.0, 1.0, 0.0], invert: false } + } +} + +/// Filters usable on masks and content. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum Filter { + Blur { + radius: f32, + }, + Sharpen { + amount: f32, + }, + Invert, + Threshold { + level: f32, + }, + /// Hue/saturation/lightness shift (color channels; lightness only on grays and masks). + /// `hue` is in turns, `saturation` and `lightness` in −1..1; all zero = unchanged. + Hsl { + hue: f32, + saturation: f32, + lightness: f32, + }, +} + +/// Push `x` (0..1) towards 1 (`amount` > 0) or 0 (`amount` < 0); `amount` in −1..1. +fn shift_unit(x: f32, amount: f32) -> f32 { + let a = amount.clamp(-1.0, 1.0); + if a >= 0.0 { x + (1.0 - x) * a } else { x * (1.0 + a) } +} + +fn hue_to_rgb(p: f32, q: f32, t: f32) -> f32 { + let t = t.rem_euclid(1.0); + if t < 1.0 / 6.0 { + p + (q - p) * 6.0 * t + } else if t < 0.5 { + q + } else if t < 2.0 / 3.0 { + p + (q - p) * (2.0 / 3.0 - t) * 6.0 + } else { + p + } +} + +/// HSL adjustment of a 0..1 color (see [`Filter::Hsl`]). A gray stays gray and only its +/// lightness changes. +pub fn hsl_adjust(rgb: [f32; 3], hue: f32, saturation: f32, lightness: f32) -> [f32; 3] { + let [r, g, b] = rgb.map(|v| if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 }); + let (mx, mn) = (r.max(g).max(b), r.min(g).min(b)); + let (d, l) = (mx - mn, (mx + mn) * 0.5); + let (h, s) = if d <= 1e-6 { + (0.0, 0.0) + } else { + let s = if l > 0.5 { d / (2.0 - mx - mn) } else { d / (mx + mn) }; + let h = if mx == r { + ((g - b) / d).rem_euclid(6.0) + } else if mx == g { + (b - r) / d + 2.0 + } else { + (r - g) / d + 4.0 + }; + (h / 6.0, s) + }; + let h = h + if hue.is_finite() { hue } else { 0.0 }; + let (s, l) = (if d <= 1e-6 { 0.0 } else { shift_unit(s, saturation) }, shift_unit(l, lightness)); + if s <= 0.0 { + return [l; 3]; + } + let q = if l < 0.5 { l * (1.0 + s) } else { l + s - l * s }; + let p = 2.0 * l - q; + [hue_to_rgb(p, q, h + 1.0 / 3.0), hue_to_rgb(p, q, h), hue_to_rgb(p, q, h - 1.0 / 3.0)] +} + +impl Filter { + pub fn label(&self) -> &'static str { + match self { + Filter::Blur { .. } => "Blur", + Filter::Sharpen { .. } => "Sharpen", + Filter::Invert => "Invert", + Filter::Threshold { .. } => "Threshold", + Filter::Hsl { .. } => "HSL Perceptive", + } + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum EffectKind { + /// Painted grayscale (mask) or material (content) pixels keyed by the effect id. + Paint, + /// A uniform or procedural grayscale fill. + Fill { + source: Source, + projection: Projection, + }, + Generator(Generator), + Levels(Levels), + Filter(Filter), + /// Select ID-map colors within a tolerance. + ColorSelection { + colors: Vec<[f32; 3]>, + tolerance: f32, + }, + /// Compare the mask with a constant. + Compare { + op: CompareOp, + value: f32, + tolerance: f32, + }, + /// Content only: a material fill (one source per channel; channels without a source are + /// left as they are). + Material(Fill), + /// Content only: records the composited stack right after its layer (that layer and + /// everything below) so masks and fills above can read it ([`Source::Anchor`]). + Anchor, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum CompareOp { + LesserThan, + WithinTolerance, + GreaterThan, +} + +impl EffectKind { + pub fn name(&self) -> &'static str { + match self { + EffectKind::Paint => "Paint", + EffectKind::Fill { .. } => "Fill", + EffectKind::Generator(_) => "Generator", + EffectKind::Levels(_) => "Levels", + EffectKind::Filter(f) => f.label(), + EffectKind::ColorSelection { .. } => "Color selection", + EffectKind::Compare { .. } => "Compare mask", + EffectKind::Material(_) => "Fill", + EffectKind::Anchor => "Anchor point", + } + } + + /// Whether the effect belongs in a layer's content stack (`true`), its mask (`false`), or + /// both (`None`). + pub fn content_only(&self) -> Option { + match self { + EffectKind::Material(_) | EffectKind::Anchor => Some(true), + EffectKind::Fill { .. } | EffectKind::Generator(_) | EffectKind::ColorSelection { .. } | EffectKind::Compare { .. } => Some(false), + EffectKind::Paint | EffectKind::Levels(_) | EffectKind::Filter(_) => None, + } + } + + /// Anchor points read by this effect's sources. + 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()), + _ => false, + } + } +} + +impl Layer { + /// Whether any source of the layer (fill, mask or content effects) reads an anchor point. + pub fn reads_anchor(&self) -> bool { + let fill = match &self.kind { + LayerKind::Fill(f) => f.sources.values().any(|s| s.anchor().is_some()), + _ => false, + }; + fill || self.mask.iter().flat_map(|m| m.effects.iter()).chain(self.content_effects.iter()).any(|e| e.kind.reads_anchor()) + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Effect { + pub id: LayerId, + pub name: String, + pub visible: bool, + pub kind: EffectKind, + pub blend: BlendMode, + pub opacity: f32, +} + +impl Effect { + pub fn new(id: LayerId, kind: EffectKind) -> Effect { + // Generators and fills that build a mask usually add on top of the background. + Effect { id, name: kind.name().to_string(), visible: true, kind, blend: BlendMode::Normal, opacity: 1.0 } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[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 }; + assert!((l.apply(0.4) - 0.5).abs() < 1e-6); + assert_eq!(l.apply(0.0), 0.0); + assert_eq!(l.apply(1.0), 1.0); + let g = Levels { gamma: 2.0, ..Levels::default() }; + assert!((g.apply(0.25) - 0.5).abs() < 1e-6); + // Degenerate ranges never divide by zero. + let d = Levels { in_low: 0.5, in_high: 0.5, ..Levels::default() }; + assert!(d.apply(0.7).is_finite()); + } + + #[test] + fn hsl_adjust_shifts_hue_saturation_and_lightness() { + let close = |a: [f32; 3], b: [f32; 3]| (0..3).all(|i| (a[i] - b[i]).abs() < 1e-4); + let c = [0.8, 0.3, 0.1]; + assert!(close(hsl_adjust(c, 0.0, 0.0, 0.0), c), "identity"); + assert!(close(hsl_adjust([1.0, 0.0, 0.0], 1.0 / 3.0, 0.0, 0.0), [0.0, 1.0, 0.0]), "red → green"); + assert!(close(hsl_adjust([1.0, 0.0, 0.0], -1.0 / 3.0, 0.0, 0.0), [0.0, 0.0, 1.0]), "red → blue"); + let gray = hsl_adjust(c, 0.0, -1.0, 0.0); + assert!((gray[0] - gray[1]).abs() < 1e-6 && (gray[1] - gray[2]).abs() < 1e-6); + // Grays only change lightness, whatever the hue and saturation. + assert!(close(hsl_adjust([0.4; 3], 0.3, 1.0, 0.5), [0.7; 3])); + assert!(close(hsl_adjust([0.4; 3], 0.0, 0.0, -0.5), [0.2; 3])); + assert!(hsl_adjust([f32::NAN, 2.0, -1.0], f32::NAN, 5.0, -5.0).iter().all(|v| v.is_finite())); + } + + #[test] + fn effect_targets_and_anchor_reads() { + assert_eq!(EffectKind::Anchor.content_only(), Some(true)); + assert_eq!(EffectKind::Generator(Generator::new(GeneratorKind::Dirt)).content_only(), Some(false)); + assert_eq!(EffectKind::Filter(Filter::Invert).content_only(), None); + let mut l = Layer::new(LayerId(1), "l", LayerKind::Paint, &[Channel::BaseColor]); + assert!(!l.reads_anchor()); + let source = Source::Anchor { anchor: LayerId(9), channel: Channel::Height }; + l.mask = Some(Mask { effects: vec![Effect::new(LayerId(2), EffectKind::Fill { source, projection: Projection::default() })], ..Mask::new(MaskBackground::Black) }); + assert!(l.reads_anchor()); + } + + #[test] + fn height_mask_keeps_the_ends_and_follows_height() { + let off = HeightMask::default(); + assert_eq!(off.apply(0.3, 1.0), 0.3, "disabled"); + let hm = HeightMask { enabled: true, contrast: 1.0, offset: 0.0 }; + for h in [-1.0, 0.0, 1.0] { + assert_eq!(hm.apply(0.0, h), 0.0); + assert_eq!(hm.apply(1.0, h), 1.0); + } + assert_eq!(hm.apply(0.5, 0.0), 0.5, "no height, no change"); + assert_eq!(hm.apply(0.5, 0.5), 1.0); + assert_eq!(hm.apply(0.5, -0.5), 0.0); + assert!(hm.apply(0.5, 0.1) > 0.5 && hm.apply(0.5, -0.1) < 0.5); + assert!(HeightMask { offset: 0.5, ..hm }.apply(0.5, 0.0) > 0.5); + assert_eq!(hm.apply(0.5, f32::NAN), 0.5); + } + + #[test] + fn generator_kinds_parse() { + for k in GeneratorKind::ALL { + assert_eq!(GeneratorKind::parse(&format!("{k:?}")), Some(k)); + } + for k in ProceduralKind::ALL { + assert_eq!(ProceduralKind::parse(k.label()), Some(k)); + } + } +} diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs new file mode 100644 index 00000000..21a796cd --- /dev/null +++ b/crates/core/src/lib.rs @@ -0,0 +1,38 @@ +//! # texturecraft-core +//! +//! The TextureCraft document model: a [`Project`] holds texture sets (one per mesh material), +//! each with a stack of [`Layer`]s (paint, fill, group) whose channels blend with one of +//! Painter's [`BlendMode`]s. Pure data with serde; pixels live in `texturecraft-texel` images +//! owned by the engine. [`blend`] is the CPU reference for every blend mode. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod blend; +pub mod channel; +pub mod layer; +pub mod project; + +pub use blend::{BlendMode, blend1, blend3, lum, mix3}; +pub use channel::{Channel, ChannelFormat, ChannelKind}; +pub use layer::{ + ChannelBlend, CompareOp, Effect, EffectKind, Fill, Filter, Generator, GeneratorKind, HeightMask, Layer, LayerId, LayerKind, Levels, Mask, MaskBackground, Procedural, + ProceduralKind, Projection, Source, hsl_adjust, +}; +pub use project::{AoMixing, BakeMatch, BakeSettings, BakedMap, DisplaySettings, FORMAT_VERSION, NormalFormat, NormalMixing, Project, TextureSet, ToneMapping}; + +#[derive(Debug, thiserror::Error, Clone, PartialEq)] +pub enum CoreError { + #[error("no layer with id {}", .0.0)] + NoLayer(LayerId), + #[error("no texture set {0}")] + NoTextureSet(usize), + #[error("{0}")] + Invalid(String), +} + +pub type Result = std::result::Result; + +/// Product name for user-facing text. +pub const APP_NAME: &str = "TextureCraft"; +/// Native project file extension. +pub const PROJECT_EXT: &str = "tcraft"; diff --git a/crates/core/src/project.rs b/crates/core/src/project.rs new file mode 100644 index 00000000..493bc96b --- /dev/null +++ b/crates/core/src/project.rs @@ -0,0 +1,744 @@ +//! The project: texture sets, their layer stacks, and project-wide settings. + +use std::collections::{BTreeMap, BTreeSet}; + +use serde::{Deserialize, Serialize}; + +use crate::{Channel, ChannelFormat, CoreError, Effect, Layer, LayerId, LayerKind, Result}; + +pub const FORMAT_VERSION: u32 = 1; + +/// Maximum group nesting (also bounds recursion over hostile project files). +pub const MAX_DEPTH: usize = 16; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum NormalFormat { + #[default] + OpenGl, + DirectX, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +pub enum BakedMap { + Normal, + WorldNormal, + Id, + AmbientOcclusion, + Curvature, + Position, + Thickness, + Height, + /// World-space direction of least occlusion (packed like the world-space normal). + BentNormal, + /// 1 where the high poly covers the low poly (times the high poly's vertex alpha). + Opacity, + /// The high poly's vertex colors. + ColorMap, + /// Facing ratio towards [`BakeSettings::direction`] (0.5 = perpendicular). + WorldDirection, +} + +impl BakedMap { + pub const ALL: [BakedMap; 12] = [ + BakedMap::Normal, + BakedMap::WorldNormal, + BakedMap::Id, + BakedMap::AmbientOcclusion, + BakedMap::Curvature, + BakedMap::Position, + BakedMap::Thickness, + BakedMap::Height, + BakedMap::BentNormal, + BakedMap::Opacity, + BakedMap::ColorMap, + BakedMap::WorldDirection, + ]; + pub fn label(self) -> &'static str { + match self { + BakedMap::Normal => "Normal", + BakedMap::WorldNormal => "World space normal", + BakedMap::Id => "ID", + BakedMap::AmbientOcclusion => "Ambient occlusion", + BakedMap::Curvature => "Curvature", + BakedMap::Position => "Position", + BakedMap::Thickness => "Thickness", + BakedMap::Height => "Height", + BakedMap::BentNormal => "Bent normals", + BakedMap::Opacity => "Opacity", + BakedMap::ColorMap => "Color map", + BakedMap::WorldDirection => "World space direction", + } + } + pub fn id(self) -> &'static str { + match self { + BakedMap::Normal => "normal_base", + BakedMap::WorldNormal => "world_space_normals", + BakedMap::Id => "id", + BakedMap::AmbientOcclusion => "ambient_occlusion", + BakedMap::Curvature => "curvature", + BakedMap::Position => "position", + BakedMap::Thickness => "thickness", + BakedMap::Height => "height", + BakedMap::BentNormal => "bent_normals", + BakedMap::Opacity => "opacity", + BakedMap::ColorMap => "color_map", + BakedMap::WorldDirection => "world_space_direction", + } + } + /// Components stored (3 for vector maps, 1 for scalar maps). + pub fn components(self) -> usize { + match self { + BakedMap::Normal | BakedMap::WorldNormal | BakedMap::Id | BakedMap::Position | BakedMap::BentNormal | BakedMap::ColorMap => 3, + _ => 1, + } + } + /// Value where nothing was baked (uncovered texels, missing maps). + pub fn neutral(self) -> [f32; 3] { + match self { + BakedMap::Normal | BakedMap::WorldNormal | BakedMap::BentNormal => [0.5, 0.5, 1.0], + BakedMap::AmbientOcclusion | BakedMap::Thickness => [1.0; 3], + BakedMap::Curvature | BakedMap::Height | BakedMap::WorldDirection => [0.5; 3], + BakedMap::Id | BakedMap::Position | BakedMap::Opacity | BakedMap::ColorMap => [0.0; 3], + } + } + pub fn parse(s: &str) -> Option { + let norm = |x: &str| x.to_ascii_lowercase().replace([' ', '_', '-'], ""); + BakedMap::ALL.into_iter().find(|m| norm(m.label()) == norm(s) || norm(m.id()) == norm(s) || norm(&format!("{m:?}")) == norm(s)) + } +} + +/// How low-poly parts find their high-poly counterparts (Painter's "Match"). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum BakeMatch { + /// Every low-poly part sees every high-poly part. + #[default] + Always, + /// A low-poly part sees only high-poly parts of the same name once the suffixes are removed + /// (`wheel_low` ↔ `wheel_high`). + ByMeshName, +} + +/// Bake parameters (Painter's "Common parameters" plus the per-map parameters). Fields missing +/// from older projects take their defaults. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default)] +pub struct BakeSettings { + /// Included by "Bake selected textures". + pub enabled: bool, + pub resolution: u32, + /// Supersampling per axis: 1 (none), 2, 4 or 8. + pub antialiasing: u32, + pub dilation: u32, + /// Rays per texel for AO, thickness and bent normals. + pub rays: u32, + /// Max occluder distance relative to the bounding-box diagonal. + pub max_distance: f32, + /// Curvature strength. + pub curvature_strength: f32, + /// Curvature from the baked normal map instead of the mesh. + pub curvature_from_normal: bool, + /// World-space direction of the "World space direction" map. + pub direction: [f32; 3], + pub maps: BTreeSet, + /// Bake from the low poly itself ("Use low poly mesh as high poly"). + pub use_low_as_high: bool, + /// Ray start distance in front of the low poly, relative to the bbox diagonal. + pub max_frontal: f32, + pub max_rear: f32, + /// Cast rays along normals averaged over hard edges (no gaps at split normals). + pub average_normals: bool, + pub matching: BakeMatch, + pub low_suffix: String, + pub high_suffix: String, +} + +impl Default for BakeSettings { + fn default() -> Self { + BakeSettings { + enabled: true, + resolution: 1024, + antialiasing: 1, + dilation: 16, + rays: 32, + max_distance: 0.25, + curvature_strength: 1.0, + curvature_from_normal: false, + direction: [0.0, 1.0, 0.0], + maps: [ + BakedMap::Normal, + BakedMap::WorldNormal, + BakedMap::Id, + BakedMap::AmbientOcclusion, + BakedMap::Curvature, + BakedMap::Position, + BakedMap::Thickness, + ] + .into_iter() + .collect(), + use_low_as_high: true, + max_frontal: 0.01, + max_rear: 0.01, + average_normals: true, + matching: BakeMatch::Always, + low_suffix: "_low".into(), + high_suffix: "_high".into(), + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum NormalMixing { + #[default] + Combine, + Replace, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum AoMixing { + #[default] + Multiply, + None, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct TextureSet { + pub name: String, + #[serde(default)] + pub description: String, + /// Material index in the mesh this set paints. + pub material: usize, + /// Power of two, 128..=8192. + pub resolution: u32, + pub channels: Vec<(Channel, ChannelFormat)>, + /// Top-level layers, bottom → top. + pub stack: Vec, + #[serde(default)] + pub normal_mixing: NormalMixing, + #[serde(default)] + pub ao_mixing: AoMixing, + /// Height-to-normal strength. + #[serde(default = "one")] + pub height_strength: f32, + #[serde(default)] + pub bake: BakeSettings, + /// Mesh maps that have been baked (pixels live with the engine / project file). + #[serde(default)] + pub baked: BTreeSet, + #[serde(default = "yes")] + pub visible: bool, +} + +fn one() -> f32 { + 1.0 +} +fn yes() -> bool { + true +} + +impl TextureSet { + pub fn new(name: impl Into, material: usize, resolution: u32) -> TextureSet { + TextureSet { + name: name.into(), + description: String::new(), + material, + resolution, + channels: Channel::DEFAULT_SET.iter().map(|c| (*c, ChannelFormat::default_for(*c))).collect(), + stack: Vec::new(), + normal_mixing: NormalMixing::Combine, + ao_mixing: AoMixing::Multiply, + height_strength: 1.0, + bake: BakeSettings::default(), + baked: BTreeSet::new(), + visible: true, + } + } + + pub fn has_channel(&self, c: Channel) -> bool { + self.channels.iter().any(|(x, _)| *x == c) + } + + pub fn channel_list(&self) -> Vec { + self.channels.iter().map(|(c, _)| *c).collect() + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum ToneMapping { + #[default] + Linear, + AcesFilmic, +} + +/// Viewport display settings (saved with the project, as Painter does). Fields missing from +/// older projects take their defaults. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default)] +pub struct DisplaySettings { + /// A built-in environment name, or `resource:` for an imported equirect image. + pub environment: String, + /// Background visibility 0..1. + pub env_opacity: f32, + pub env_exposure: f32, + pub env_blur: f32, + /// Degrees. + pub env_rotation: f32, + pub shadows: bool, + pub tone_mapping: ToneMapping, + pub fov: f32, + pub post_effects: bool, + pub bloom: f32, + pub vignette: f32, + pub sharpen: f32, + pub grain: f32, + pub ao_intensity: f32, + /// Accumulate jittered frames while the view is still (temporal anti-aliasing). + pub taa: bool, + /// Shader: PBR specular/glossiness (base color = diffuse, Specular = F0 color, Glossiness) + /// instead of metallic/roughness. + pub spec_gloss: bool, + /// Parallax occlusion mapping from the height channel (same depth as height-to-normal). + pub parallax: bool, +} + +/// Prefix of [`DisplaySettings::environment`] naming an image resource. +pub const ENV_RESOURCE_PREFIX: &str = "resource:"; + +impl DisplaySettings { + /// The image resource used as the environment, if any. + pub fn environment_resource(&self) -> Option<&str> { + self.environment.strip_prefix(ENV_RESOURCE_PREFIX) + } +} + +impl Default for DisplaySettings { + fn default() -> Self { + DisplaySettings { + environment: "Studio Soft".into(), + env_opacity: 0.0, + env_exposure: 0.0, + env_blur: 0.35, + env_rotation: 0.0, + shadows: false, + tone_mapping: ToneMapping::AcesFilmic, + fov: 40.0, + post_effects: false, + bloom: 0.0, + vignette: 0.0, + sharpen: 0.0, + grain: 0.0, + ao_intensity: 0.75, + taa: true, + spec_gloss: false, + parallax: false, + } + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Project { + pub format_version: u32, + pub name: String, + /// Path of the mesh inside the project file (or a built-in primitive id `builtin:`). + pub mesh: String, + pub normal_format: NormalFormat, + pub texture_sets: Vec, + pub layers: BTreeMap, + pub next_id: u64, + pub display: DisplaySettings, +} + +impl Project { + pub fn new(name: impl Into, mesh: impl Into) -> Project { + Project { + format_version: FORMAT_VERSION, + name: name.into(), + mesh: mesh.into(), + normal_format: NormalFormat::OpenGl, + texture_sets: Vec::new(), + layers: BTreeMap::new(), + next_id: 1, + display: DisplaySettings::default(), + } + } + + pub fn alloc_id(&mut self) -> LayerId { + let id = LayerId(self.next_id); + self.next_id = self.next_id.saturating_add(1); + id + } + + pub fn texture_set(&self, ts: usize) -> Result<&TextureSet> { + self.texture_sets.get(ts).ok_or(CoreError::NoTextureSet(ts)) + } + pub fn texture_set_mut(&mut self, ts: usize) -> Result<&mut TextureSet> { + self.texture_sets.get_mut(ts).ok_or(CoreError::NoTextureSet(ts)) + } + + pub fn layer(&self, id: LayerId) -> Result<&Layer> { + self.layers.get(&id).ok_or(CoreError::NoLayer(id)) + } + pub fn layer_mut(&mut self, id: LayerId) -> Result<&mut Layer> { + self.layers.get_mut(&id).ok_or(CoreError::NoLayer(id)) + } + + /// The texture set and parent group (None = top level) holding `id`, plus its index. + pub fn locate(&self, id: LayerId) -> Option<(usize, Option, usize)> { + for (ts, set) in self.texture_sets.iter().enumerate() { + if let Some(i) = set.stack.iter().position(|x| *x == id) { + return Some((ts, None, i)); + } + if let Some((parent, i)) = self.find_in(&set.stack, id, 0) { + return Some((ts, Some(parent), i)); + } + } + None + } + + fn find_in(&self, list: &[LayerId], id: LayerId, depth: usize) -> Option<(LayerId, usize)> { + if depth > MAX_DEPTH { + return None; + } + for g in list { + let Some(layer) = self.layers.get(g) else { continue }; + let kids = layer.children(); + if let Some(i) = kids.iter().position(|x| *x == id) { + return Some((*g, i)); + } + if let Some(found) = self.find_in(kids, id, depth + 1) { + return Some(found); + } + } + None + } + + /// The sibling list holding `id` (a texture set stack or a group's children). + fn siblings_mut(&mut self, ts: usize, parent: Option) -> Result<&mut Vec> { + match parent { + None => Ok(&mut self.texture_set_mut(ts)?.stack), + Some(p) => match &mut self.layer_mut(p)?.kind { + LayerKind::Group { children, .. } => Ok(children), + _ => Err(CoreError::Invalid(format!("layer {} is not a group", p.0))), + }, + } + } + + /// Insert `layer` into texture set `ts` above `above` (None = top of the stack). When + /// `above` is a group and `inside` is true, the layer goes to the top of the group. + pub fn insert_layer(&mut self, ts: usize, layer: Layer, above: Option, inside: bool) -> Result { + self.texture_set(ts)?; + let id = layer.id; + if self.layers.contains_key(&id) { + return Err(CoreError::Invalid(format!("layer id {} already exists", id.0))); + } + self.layers.insert(id, layer); + let placed = match above { + Some(target) if inside && self.layers.get(&target).is_some_and(Layer::is_group) => self.siblings_mut(ts, Some(target)).map(|kids| kids.push(id)), + Some(target) => match self.locate(target) { + Some((t, parent, i)) if t == ts => self.siblings_mut(ts, parent).map(|list| list.insert(i + 1, id)), + _ => self.texture_set_mut(ts).map(|s| s.stack.push(id)), + }, + None => self.texture_set_mut(ts).map(|s| s.stack.push(id)), + }; + if let Err(e) = placed { + self.layers.remove(&id); + return Err(e); + } + Ok(id) + } + + /// Remove a layer (and, for groups, everything inside). Returns the removed ids. + pub fn remove_layer(&mut self, id: LayerId) -> Result> { + let (ts, parent, i) = self.locate(id).ok_or(CoreError::NoLayer(id))?; + let list = self.siblings_mut(ts, parent)?; + if i < list.len() { + list.remove(i); + } + let mut removed = Vec::new(); + self.collect_subtree(id, 0, &mut removed); + for r in &removed { + self.layers.remove(r); + } + Ok(removed) + } + + fn collect_subtree(&self, id: LayerId, depth: usize, out: &mut Vec) { + if depth > MAX_DEPTH || out.contains(&id) { + return; + } + out.push(id); + if let Some(l) = self.layers.get(&id) { + for c in l.children() { + self.collect_subtree(*c, depth + 1, out); + } + } + } + + /// Move `id` within its sibling list to `index` (clamped). + pub fn move_layer(&mut self, id: LayerId, index: usize) -> Result<()> { + let (ts, parent, i) = self.locate(id).ok_or(CoreError::NoLayer(id))?; + let list = self.siblings_mut(ts, parent)?; + if i < list.len() { + list.remove(i); + let to = index.min(list.len()); + list.insert(to, id); + } + Ok(()) + } + + /// Move `id` above `target` anywhere in the same texture set (drag & drop). + pub fn move_above(&mut self, id: LayerId, target: Option, inside: bool) -> Result<()> { + if Some(id) == target { + return Ok(()); + } + let (ts, parent, i) = self.locate(id).ok_or(CoreError::NoLayer(id))?; + if let Some(t) = target { + let mut sub = Vec::new(); + self.collect_subtree(id, 0, &mut sub); + if sub.contains(&t) { + return Err(CoreError::Invalid("cannot move a group into itself".into())); + } + } + let list = self.siblings_mut(ts, parent)?; + if i < list.len() { + list.remove(i); + } + let layer = self.layers.remove(&id).ok_or(CoreError::NoLayer(id))?; + self.insert_layer(ts, layer, target, inside).map(|_| ()) + } + + /// Visible-order walk of a stack: every layer id, depth-first, bottom → top. + pub fn walk(&self, ts: usize) -> Vec<(LayerId, usize)> { + let mut out = Vec::new(); + if let Some(set) = self.texture_sets.get(ts) { + self.walk_list(&set.stack, 0, &mut out); + } + out + } + + fn walk_list(&self, list: &[LayerId], depth: usize, out: &mut Vec<(LayerId, usize)>) { + if depth > MAX_DEPTH { + return; + } + for id in list { + out.push((*id, depth)); + if let Some(l) = self.layers.get(id) { + self.walk_list(l.children(), depth + 1, out); + } + } + } + + /// Find an effect anywhere (mask or content stacks) by id: (owning layer, effect). + pub fn effect(&self, id: LayerId) -> Option<(LayerId, &Effect)> { + self.layers.values().find_map(|l| l.mask.iter().flat_map(|m| m.effects.iter()).chain(l.content_effects.iter()).find(|e| e.id == id).map(|e| (l.id, e))) + } + + pub fn effect_mut(&mut self, id: LayerId) -> Option<&mut Effect> { + self.layers.values_mut().find_map(|l| { + let in_mask = l.mask.as_mut().and_then(|m| m.effects.iter_mut().find(|e| e.id == id)); + match in_mask { + Some(e) => Some(e), + None => l.content_effects.iter_mut().find(|e| e.id == id), + } + }) + } + + /// A unique " " name among the layers of texture set `ts`. + pub fn next_layer_name(&self, ts: usize, base: &str) -> String { + let names: BTreeSet<&str> = self.walk(ts).iter().filter_map(|(id, _)| self.layers.get(id).map(|l| l.name.as_str())).collect(); + (1..).map(|n| format!("{base} {n}")).find(|n| !names.contains(n.as_str())).unwrap_or_else(|| base.to_string()) + } + + /// Check structural invariants (ids exist, no layer appears twice). Used after loading. + pub fn validate(&self) -> Result<()> { + if self.format_version > FORMAT_VERSION { + return Err(CoreError::Invalid(format!("project format {} is newer than this TextureCraft supports ({FORMAT_VERSION})", self.format_version))); + } + let mut seen = BTreeSet::new(); + for ts in 0..self.texture_sets.len() { + for (id, depth) in self.walk(ts) { + if depth > MAX_DEPTH { + return Err(CoreError::Invalid("layer groups nested too deeply".into())); + } + if !self.layers.contains_key(&id) { + return Err(CoreError::Invalid(format!("stack references missing layer {}", id.0))); + } + if !seen.insert(id) { + return Err(CoreError::Invalid(format!("layer {} appears twice", id.0))); + } + } + } + match self.instance_cycle() { + Some(id) => Err(CoreError::Invalid(format!("instance layer {} draws itself", id.0))), + None => Ok(()), + } + } + + /// Every layer drawn when `roots` are drawn: the roots, folder children and instance + /// sources, recursively, each once. + pub fn render_tree(&self, roots: &[LayerId]) -> BTreeSet { + let mut out = BTreeSet::new(); + let mut todo: Vec<(LayerId, usize)> = roots.iter().map(|id| (*id, 0)).collect(); + while let Some((id, depth)) = todo.pop() { + if depth > 2 * MAX_DEPTH || !out.insert(id) { + continue; + } + if let Some(l) = self.layers.get(&id) { + todo.extend(l.children().iter().chain(l.instance_source().as_ref()).map(|c| (*c, depth + 1))); + } + } + out + } + + /// An instance layer that draws itself (through folders and other instances), if any. + pub fn instance_cycle(&self) -> Option { + self.layers.values().filter_map(|l| Some((l.id, l.instance_source()?))).find(|(id, s)| self.render_tree(&[*s]).contains(id)).map(|(id, _)| id) + } + + /// The layer whose content `id` draws: itself, or an instance's source. + pub fn content_layer(&self, id: LayerId) -> Option<&Layer> { + let mut l = self.layers.get(&id)?; + for _ in 0..=MAX_DEPTH { + match l.instance_source() { + Some(s) => l = self.layers.get(&s)?, + None => return Some(l), + } + } + None + } + + /// The layer holding the mask `id` draws with: itself when it has a mask (an instance's + /// override), else the instance's source. + pub fn mask_owner(&self, id: LayerId) -> LayerId { + match self.layers.get(&id) { + Some(l) if l.mask.is_none() => self.content_layer(id).map_or(id, |c| c.id), + _ => id, + } + } + + /// Texture sets that draw layer `id`: the set holding it and every set with an instance of + /// it (or of a folder holding it). + pub fn sets_drawing(&self, id: LayerId) -> Vec { + (0..self.texture_sets.len()).filter(|ts| self.texture_sets.get(*ts).is_some_and(|set| self.render_tree(&set.stack).contains(&id))).collect() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn project() -> Project { + let mut p = Project::new("t", "builtin:sphere"); + p.texture_sets.push(TextureSet::new("Body", 0, 512)); + p + } + + fn paint(p: &mut Project, name: &str) -> Layer { + Layer::new(p.alloc_id(), name, LayerKind::Paint, &Channel::DEFAULT_SET) + } + + #[test] + fn insert_move_remove_keep_tree_consistent() { + let mut p = project(); + let a = paint(&mut p, "a"); + let a = p.insert_layer(0, a, None, false).unwrap(); + let b = paint(&mut p, "b"); + let b = p.insert_layer(0, b, None, false).unwrap(); + let gid = p.alloc_id(); + let g = Layer::new(gid, "g", LayerKind::Group { children: vec![], collapsed: false }, &Channel::DEFAULT_SET); + p.insert_layer(0, g, Some(a), false).unwrap(); + assert_eq!(p.texture_sets[0].stack, vec![a, gid, b]); + p.move_above(b, Some(gid), true).unwrap(); + assert_eq!(p.texture_sets[0].stack, vec![a, gid]); + assert_eq!(p.layer(gid).unwrap().children(), &[b]); + assert_eq!(p.locate(b), Some((0, Some(gid), 0))); + assert!(p.move_above(gid, Some(b), true).is_err(), "a group cannot go inside itself"); + p.validate().unwrap(); + let removed = p.remove_layer(gid).unwrap(); + assert_eq!(removed, vec![gid, b]); + assert_eq!(p.texture_sets[0].stack, vec![a]); + assert!(p.layer(b).is_err()); + p.validate().unwrap(); + } + + #[test] + fn names_are_unique() { + let mut p = project(); + let l = Layer::new(p.alloc_id(), "Layer 1", LayerKind::Paint, &[]); + p.insert_layer(0, l, None, false).unwrap(); + assert_eq!(p.next_layer_name(0, "Layer"), "Layer 2"); + } + + #[test] + fn serde_round_trip_is_stable() { + let mut p = project(); + let l = paint(&mut p, "x"); + p.insert_layer(0, l, None, false).unwrap(); + let json = serde_json::to_string(&p).unwrap(); + let back: Project = serde_json::from_str(&json).unwrap(); + assert_eq!(back, p); + assert_eq!(serde_json::to_string(&back).unwrap(), json); + } + + #[test] + fn content_effects_and_anchors_round_trip_and_old_projects_load() { + use crate::{Effect, EffectKind, Fill, Filter, Mask, MaskBackground, Projection, Source}; + let mut p = project(); + let mut l = paint(&mut p, "x"); + let (anchor, hsl, fill) = (p.alloc_id(), p.alloc_id(), p.alloc_id()); + let mut material = Fill::default(); + material.sources.insert(Channel::Roughness, Source::Uniform([0.2; 3])); + l.content_effects = vec![ + Effect::new(fill, EffectKind::Material(material)), + Effect::new(hsl, EffectKind::Filter(Filter::Hsl { hue: 0.25, saturation: -0.5, lightness: 0.1 })), + Effect::new(anchor, EffectKind::Anchor), + ]; + let id = p.insert_layer(0, l, None, false).unwrap(); + let mut top = paint(&mut p, "y"); + let source = Source::Anchor { anchor, channel: Channel::Height }; + top.mask = Some(Mask { effects: vec![Effect::new(p.alloc_id(), EffectKind::Fill { source, projection: Projection::default() })], ..Mask::new(MaskBackground::Black) }); + p.insert_layer(0, top, Some(id), false).unwrap(); + let json = serde_json::to_string(&p).unwrap(); + let back: Project = serde_json::from_str(&json).unwrap(); + assert_eq!(back, p); + assert_eq!(back.effect(anchor).map(|(owner, _)| owner), Some(id)); + // Projects saved before content effects existed load with empty stacks. + let mut v: serde_json::Value = serde_json::from_str(&json).unwrap(); + for l in v["layers"].as_object_mut().unwrap().values_mut() { + l.as_object_mut().unwrap().remove("content_effects"); + } + let old: Project = serde_json::from_value(v).unwrap(); + assert!(old.layers.values().all(|l| l.content_effects.is_empty())); + } + + #[test] + fn instances_resolve_draw_in_their_sets_and_cycles_are_found() { + let mut p = project(); + p.texture_sets.push(TextureSet::new("Head", 1, 512)); + let a = paint(&mut p, "a"); + let a = p.insert_layer(0, a, None, false).unwrap(); + let inst = Layer::new(p.alloc_id(), "a", LayerKind::Instance { source: a }, &[]); + let inst = p.insert_layer(1, inst, None, false).unwrap(); + assert_eq!(p.content_layer(inst).map(|l| l.id), Some(a)); + assert_eq!(p.sets_drawing(a), vec![0, 1]); + assert_eq!(p.sets_drawing(inst), vec![1]); + p.validate().unwrap(); + // A folder holding an instance of itself draws itself. + let gid = p.alloc_id(); + p.insert_layer(1, Layer::new(gid, "g", LayerKind::Group { children: vec![], collapsed: false }, &[]), None, false).unwrap(); + let loop_id = p.alloc_id(); + p.insert_layer(1, Layer::new(loop_id, "g", LayerKind::Instance { source: gid }, &[]), Some(gid), true).unwrap(); + assert_eq!(p.instance_cycle(), Some(loop_id)); + assert!(p.validate().is_err()); + assert!(p.content_layer(LayerId(999)).is_none()); + } + + #[test] + fn validate_rejects_newer_formats_and_dangling_ids() { + let mut p = project(); + p.texture_sets[0].stack.push(LayerId(99)); + assert!(p.validate().is_err()); + let mut q = project(); + q.format_version = FORMAT_VERSION + 1; + assert!(q.validate().is_err()); + } +} diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml new file mode 100644 index 00000000..9e769306 --- /dev/null +++ b/crates/engine/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "texturecraft-engine" +description = "TextureCraft session, command registry, painting, baking and inspection" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lints] +workspace = true + +[dependencies] +texturecraft-core = { workspace = true } +texturecraft-texel = { workspace = true } +texturecraft-geom = { workspace = true } +texturecraft-compose = { workspace = true } +texturecraft-paint = { workspace = true } +texturecraft-bake = { workspace = true } +texturecraft-formats = { workspace = true } +rayon = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } + +[dev-dependencies] +image = { workspace = true } diff --git a/crates/engine/src/bake_cmds.rs b/crates/engine/src/bake_cmds.rs new file mode 100644 index 00000000..21c22acf --- /dev/null +++ b/crates/engine/src/bake_cmds.rs @@ -0,0 +1,390 @@ +//! Mesh-map baking commands (Painter's "Bake Mesh Maps" window): bake settings, the +//! high-definition mesh list, the cage, and running a bake. + +use std::collections::BTreeSet; +use std::sync::Arc; + +use serde_json::{Value, json}; +use texturecraft_bake::{BakeOutput, BakeRequest, HighPoly, MAX_RAYS, Progress, bake}; +use texturecraft_core::{BakeMatch, BakeSettings, BakedMap}; +use texturecraft_geom::{Bvh, Mesh}; + +use crate::commands::{CommandSpec, always, bad, boolp, f32p, strp, u64p}; +use crate::{EngineError, Result, Session, cmd}; + +pub fn specs() -> Vec { + vec![ + cmd!("bake.run", "Bake mesh maps", ["Mode"], None, "{ts?: index|[indices]|\"all\"|\"selected\", ...any bake.settings parameter}", always, bake_run), + cmd!( + "bake.settings", + "Bake settings", + [], + None, + "{ts?: index|[indices]|\"all\"|\"selected\" (default: the active set), resolution?, antialiasing?: 1|2|4|8, dilation?, rays?, maxDistance?, curvatureStrength?, curvatureFromNormal?, direction?: [x,y,z], maps?: [ids], enabled?, useLowAsHigh?, maxFrontal?, maxRear?, averageNormals?, matching?: always|byMeshName, lowSuffix?, highSuffix?}", + always, + settings + ), + cmd!("bake.highPoly.add", "Add high definition mesh", [], None, "{path} — a mesh file (glb, gltf, fbx, dae, obj, ply, stl)", always, high_add), + cmd!("bake.highPoly.remove", "Remove high definition mesh", [], None, "{index? | name? | all?: true}", always, high_remove), + cmd!("bake.highPoly.list", "List high definition meshes", [], None, "{}", always, |s, _| Ok(meshes_json(s))), + cmd!("bake.cage.set", "Load cage mesh", [], None, "{path} — a mesh with the low poly's triangles", always, cage_set), + cmd!("bake.cage.clear", "Remove cage mesh", [], None, "{}", always, |s, _| { + s.bake_meshes.cage = None; + s.modified = true; + Ok(meshes_json(s)) + }), + ] +} + +/// A mesh file as saved in a project: (file name, bytes). +type MeshFile = (String, Vec); + +/// A mesh file the baker reads (high poly or cage): its name, original bytes (saved in the +/// project) and parsed mesh. +#[derive(Clone, Debug)] +pub struct BakeMesh { + pub name: String, + pub bytes: Arc>, + pub mesh: Arc, +} + +/// The high-definition meshes and the cage. Like the low-poly mesh they are not part of the +/// undo history. +#[derive(Clone, Debug, Default)] +pub struct BakeMeshes { + pub high: Vec, + pub cage: Option, + /// Every high poly appended into one mesh, with its BVH (rebuilt when the list changes). + merged: Option<(Arc, Arc)>, +} + +impl BakeMeshes { + // shortcut: the merged copy doubles high-poly memory; build the BVH over the parts in place if + // multi-million-triangle sets run out of memory. + fn rebuild(&mut self) { + self.merged = (!self.high.is_empty()).then(|| { + let mut all = Mesh::default(); + for h in &self.high { + all.append(&h.mesh); + } + let bvh = Bvh::new(&all); + (Arc::new(all), Arc::new(bvh)) + }); + } +} + +/// A self-contained bake task that can run on another thread. +pub struct BakeTask { + pub ts: usize, + pub mesh: Arc, + pub bvh: Arc, + pub material: usize, + pub settings: BakeSettings, + /// The merged high polys, unless the set bakes from the low poly. + pub high: Option<(Arc, Arc)>, + pub cage: Option>, +} + +impl BakeTask { + pub fn run(&self, progress: &Progress) -> Result { + let high = self.high.as_ref().map(|(mesh, bvh)| HighPoly { mesh, bvh }); + let req = BakeRequest { mesh: &self.mesh, bvh: &self.bvh, material: self.material, settings: &self.settings, high, cage: self.cage.as_deref() }; + bake(&req, progress).map_err(|e| EngineError::Other(format!("bake failed: {e}"))) + } +} + +/// A mesh file name made unique among `taken` (and safe as a project file entry). +fn unique_name(file_name: &str, taken: &[&str]) -> String { + let leaf = std::path::Path::new(file_name).file_name().and_then(|n| n.to_str()).unwrap_or("mesh"); + let base = texturecraft_formats::sanitize_resource_name(leaf); + let mut name = base.clone(); + let mut n = 2; + while taken.contains(&name.as_str()) { + name = format!("{n}_{base}"); + n += 1; + } + name +} + +fn parse_mesh(file_name: &str, bytes: Vec) -> Result<(Arc>, Arc)> { + let mesh = texturecraft_formats::import_high_poly_bytes(file_name, &bytes).map_err(|e| EngineError::Other(format!("{file_name}: {e}")))?; + Ok((Arc::new(bytes), Arc::new(mesh))) +} + +impl Session { + pub fn bake_task(&self, ts: usize) -> Result { + let set = self.doc.project.texture_set(ts)?; + let high = if set.bake.use_low_as_high { None } else { self.bake_meshes.merged.clone() }; + let cage = high.as_ref().and(self.bake_meshes.cage.as_ref()).map(|c| c.mesh.clone()); + Ok(BakeTask { ts, mesh: self.mesh.clone(), bvh: self.bvh.clone(), material: set.material, settings: set.bake.clone(), high, cage }) + } + + /// Store a finished bake (one undo step). + pub fn apply_bake(&mut self, ts: usize, out: BakeOutput) -> Result<()> { + self.edit("Bake mesh maps", |s| { + let maps: BTreeSet = out.maps.keys().copied().collect(); + let entry = s.doc.baked.entry(ts).or_default(); + entry.resolution = out.resolution; + for (m, data) in out.maps { + entry.maps.insert(m, Arc::new(data)); + } + s.doc.project.texture_set_mut(ts)?.baked.extend(maps); + Ok(()) + }) + } + + /// Bake texture set `ts` now (blocking). + pub fn bake_now(&mut self, ts: usize) -> Result<()> { + let task = self.bake_task(ts)?; + let out = task.run(&Progress::new())?; + self.apply_bake(ts, out) + } + + /// Add a high-definition mesh from file bytes (the web picker, the desktop dialog and + /// `bake.highPoly.add`); every texture set then bakes from the high polys. Returns its name. + pub fn add_high_poly_bytes(&mut self, file_name: &str, bytes: Vec) -> Result { + let (bytes, mesh) = parse_mesh(file_name, bytes)?; + let taken: Vec<&str> = self.bake_meshes.high.iter().map(|h| h.name.as_str()).collect(); + let name = unique_name(file_name, &taken); + self.log.push(format!("High poly {name}: {} triangles", mesh.triangle_count())); + self.bake_meshes.high.push(BakeMesh { name: name.clone(), bytes, mesh }); + self.bake_meshes.rebuild(); + for set in &mut self.doc.project.texture_sets { + set.bake.use_low_as_high = false; + } + self.modified = true; + Ok(name) + } + + /// Remove high-definition mesh `index`. + pub fn remove_high_poly(&mut self, index: usize) -> Result { + if index >= self.bake_meshes.high.len() { + return Err(EngineError::Other(format!("no high poly mesh {index}"))); + } + let removed = self.bake_meshes.high.remove(index); + self.bake_meshes.rebuild(); + self.modified = true; + Ok(removed) + } + + /// Use a cage mesh (same triangles as the low poly) from file bytes. + pub fn set_cage_bytes(&mut self, file_name: &str, bytes: Vec) -> Result { + let (bytes, mesh) = parse_mesh(file_name, bytes)?; + if mesh.triangle_count() != self.mesh.triangle_count() { + return Err(EngineError::Other(format!( + "the cage has {} triangles but the low poly has {}; export the cage from the low poly", + mesh.triangle_count(), + self.mesh.triangle_count() + ))); + } + let name = unique_name(file_name, &[]); + self.bake_meshes.cage = Some(BakeMesh { name: name.clone(), bytes, mesh }); + self.modified = true; + Ok(name) + } + + /// Restore the bake meshes saved in a project file; unreadable ones are logged and skipped. + pub(crate) fn load_bake_meshes(&mut self, high: Vec, cage: Option) { + for (name, bytes) in high { + match parse_mesh(&name, bytes) { + Ok((bytes, mesh)) => self.bake_meshes.high.push(BakeMesh { name, bytes, mesh }), + Err(e) => self.log.push(format!("skipped high poly mesh: {e}")), + } + } + self.bake_meshes.rebuild(); + if let Some((name, bytes)) = cage + && let Err(e) = self.set_cage_bytes(&name, bytes) + { + self.log.push(format!("skipped cage mesh: {e}")); + } + } + + /// The bake meshes for a project file: (high polys, cage). + pub(crate) fn saved_bake_meshes(&self) -> (Vec, Option) { + let entry = |m: &BakeMesh| (m.name.clone(), m.bytes.as_ref().clone()); + (self.bake_meshes.high.iter().map(entry).collect(), self.bake_meshes.cage.as_ref().map(entry)) + } +} + +/// `bake.highPoly.list`: the high polys and the cage (also part of `project.inspect`). +pub fn meshes_json(s: &Session) -> Value { + let one = |m: &BakeMesh| json!({ "name": m.name, "triangles": m.mesh.triangle_count(), "parts": m.mesh.submeshes.iter().map(|p| p.name.as_str()).collect::>() }); + json!({ "highPoly": s.bake_meshes.high.iter().map(one).collect::>(), "cage": s.bake_meshes.cage.as_ref().map(one) }) +} + +#[cfg(not(target_arch = "wasm32"))] +fn read_mesh_file(cmd: &str, p: &Value) -> Result<(String, Vec)> { + let path = strp(p, "path").ok_or_else(|| bad(cmd, "`path` is required"))?; + let meta = std::fs::metadata(path).map_err(|e| bad(cmd, format!("cannot read {path}: {e}")))?; + if meta.len() > texturecraft_formats::mesh_import::MAX_MESH_FILE_BYTES { + return Err(bad(cmd, format!("{path} is too large"))); + } + let bytes = std::fs::read(path).map_err(|e| bad(cmd, format!("cannot read {path}: {e}")))?; + Ok((path.to_string(), bytes)) +} + +#[cfg(target_arch = "wasm32")] +fn read_mesh_file(_: &str, _: &Value) -> Result<(String, Vec)> { + Err(EngineError::Other("file paths are not available in the browser; use the bake panel's file picker".into())) +} + +fn high_add(s: &mut Session, p: &Value) -> Result { + let (path, bytes) = read_mesh_file("bake.highPoly.add", p)?; + let name = s.add_high_poly_bytes(&path, bytes)?; + Ok(json!({ "name": name, "meshes": meshes_json(s) })) +} + +fn high_remove(s: &mut Session, p: &Value) -> Result { + if boolp(p, "all") == Some(true) { + s.bake_meshes.high.clear(); + s.bake_meshes.rebuild(); + s.modified = true; + return Ok(meshes_json(s)); + } + let index = match (u64p(p, "index"), strp(p, "name")) { + (Some(i), _) => usize::try_from(i).unwrap_or(usize::MAX), + (None, Some(n)) => s.bake_meshes.high.iter().position(|h| h.name == n).ok_or_else(|| bad("bake.highPoly.remove", format!("no high poly `{n}`")))?, + (None, None) => return Err(bad("bake.highPoly.remove", "`index`, `name` or `all` is required")), + }; + s.remove_high_poly(index)?; + Ok(meshes_json(s)) +} + +fn cage_set(s: &mut Session, p: &Value) -> Result { + let (path, bytes) = read_mesh_file("bake.cage.set", p)?; + let name = s.set_cage_bytes(&path, bytes)?; + Ok(json!({ "name": name, "meshes": meshes_json(s) })) +} + +/// Texture sets named by `ts` (default: the active one). +fn sets_param(s: &Session, cmd: &str, p: &Value) -> Result> { + let n = s.set_count(); + let one = |v: &Value| v.as_u64().map(|t| t as usize).filter(|t| *t < n).ok_or_else(|| bad(cmd, format!("invalid texture set {v}"))); + match p.get("ts") { + Some(Value::String(a)) if a == "all" => Ok((0..n).collect()), + Some(Value::String(a)) if a == "selected" => Ok((0..n).filter(|t| s.doc.project.texture_sets.get(*t).is_some_and(|set| set.bake.enabled)).collect()), + Some(Value::Array(a)) => a.iter().map(one).collect(), + Some(v) => Ok(vec![one(v)?]), + None => Ok(vec![s.ts()]), + } +} + +/// `b` as `bake.settings` parameters (what the command returns and the UI sends back). +pub fn settings_params(b: &BakeSettings) -> Value { + json!({ + "resolution": b.resolution, + "antialiasing": b.antialiasing, + "dilation": b.dilation, + "rays": b.rays, + "maxDistance": b.max_distance, + "curvatureStrength": b.curvature_strength, + "curvatureFromNormal": b.curvature_from_normal, + "direction": b.direction, + "maps": b.maps.iter().map(|m| m.id()).collect::>(), + "enabled": b.enabled, + "useLowAsHigh": b.use_low_as_high, + "maxFrontal": b.max_frontal, + "maxRear": b.max_rear, + "averageNormals": b.average_normals, + "matching": match b.matching { BakeMatch::Always => "always", BakeMatch::ByMeshName => "byMeshName" }, + "lowSuffix": b.low_suffix, + "highSuffix": b.high_suffix, + }) +} + +/// Apply the `bake.settings` parameters present in `p` to `b`. +fn apply_settings(b: &mut BakeSettings, p: &Value, cmd: &str) -> Result<()> { + if let Some(r) = u64p(p, "resolution") { + b.resolution = crate::session::check_resolution(u32::try_from(r).unwrap_or(0))?; + } + if let Some(a) = u64p(p, "antialiasing") { + if ![1, 2, 4, 8].contains(&a) { + return Err(bad(cmd, "antialiasing must be 1, 2, 4 or 8")); + } + b.antialiasing = a as u32; + } + if let Some(d) = u64p(p, "dilation") { + b.dilation = d.min(256) as u32; + } + if let Some(r) = u64p(p, "rays") { + b.rays = r.clamp(1, u64::from(MAX_RAYS)) as u32; + } + for (k, slot, lo, hi) in [ + ("maxDistance", &mut b.max_distance, 0.001, 10.0), + ("curvatureStrength", &mut b.curvature_strength, 0.0, 16.0), + ("maxFrontal", &mut b.max_frontal, 0.0, 1.0), + ("maxRear", &mut b.max_rear, 0.0, 1.0), + ] { + if let Some(v) = f32p(p, k).filter(|v| v.is_finite()) { + *slot = v.clamp(lo, hi); + } + } + for (k, slot) in [ + ("curvatureFromNormal", &mut b.curvature_from_normal), + ("enabled", &mut b.enabled), + ("useLowAsHigh", &mut b.use_low_as_high), + ("averageNormals", &mut b.average_normals), + ] { + if let Some(v) = boolp(p, k) { + *slot = v; + } + } + if let Some(d) = p.get("direction") { + let v: Vec = d.as_array().into_iter().flatten().filter_map(Value::as_f64).map(|x| x as f32).collect(); + match v[..] { + [x, y, z] if [x, y, z].iter().all(|c| c.is_finite()) && [x, y, z] != [0.0; 3] => b.direction = [x, y, z], + _ => return Err(bad(cmd, "`direction` must be three finite numbers, not all zero")), + } + } + if let Some(a) = p.get("maps").and_then(Value::as_array) { + b.maps = a.iter().map(|v| v.as_str().and_then(BakedMap::parse).ok_or_else(|| bad(cmd, format!("unknown mesh map {v}")))).collect::>()?; + } + match strp(p, "matching") { + Some("always") => b.matching = BakeMatch::Always, + Some("byMeshName") | Some("byName") => b.matching = BakeMatch::ByMeshName, + Some(o) => return Err(bad(cmd, format!("unknown matching `{o}` (always|byMeshName)"))), + None => {} + } + for (k, slot) in [("lowSuffix", &mut b.low_suffix), ("highSuffix", &mut b.high_suffix)] { + if let Some(v) = strp(p, k) { + *slot = v.chars().take(64).collect(); + } + } + Ok(()) +} + +/// Validate and apply the settings parameters to every set first, so a bad parameter changes +/// nothing. +fn set_settings(s: &mut Session, p: &Value, cmd: &str) -> Result> { + let sets = sets_param(s, cmd, p)?; + let mut next = Vec::with_capacity(sets.len()); + for &ts in &sets { + let mut b = s.doc.project.texture_set(ts)?.bake.clone(); + apply_settings(&mut b, p, cmd)?; + next.push((ts, b)); + } + for (ts, b) in next { + let set = s.doc.project.texture_set_mut(ts)?; + if set.bake != b { + set.bake = b; + s.modified = true; + } + } + Ok(sets) +} + +fn settings(s: &mut Session, p: &Value) -> Result { + let sets = set_settings(s, p, "bake.settings")?; + let out: Vec = + sets.iter().filter_map(|t| s.doc.project.texture_sets.get(*t).map(|set| json!({ "ts": t, "settings": settings_params(&set.bake) }))).collect(); + Ok(json!(out)) +} + +fn bake_run(s: &mut Session, p: &Value) -> Result { + let sets = set_settings(s, p, "bake.run")?; + for &ts in &sets { + s.bake_now(ts)?; + } + s.update(); + Ok(json!({ "baked": sets })) +} diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs new file mode 100644 index 00000000..191ad10e --- /dev/null +++ b/crates/engine/src/commands.rs @@ -0,0 +1,177 @@ +//! The command registry. Every user-visible action is a command with a stable id; the menus, +//! shortcuts, CLI, control channel and MCP server all dispatch them by id. Ids follow +//! Painter's menu structure (see `menu_catalog`). + +use serde_json::{Value, json}; +use texturecraft_core::{BlendMode, Channel}; + +use crate::{EngineError, Result, Session}; + +pub type Run = fn(&mut Session, &Value) -> Result; +pub type Enabled = fn(&Session) -> std::result::Result<(), String>; + +pub struct CommandSpec { + pub id: &'static str, + pub label: &'static str, + /// Menu placement, e.g. `["Edit"]`. Empty = not in menus. + pub menu: &'static [&'static str], + /// Default shortcut in egui notation (`Ctrl+Shift+E`), mapped per platform by the UI. + pub shortcut: Option<&'static str>, + /// Parameters, for humans and agents. + pub params: &'static str, + pub enabled: Enabled, + pub run: Run, +} + +pub(crate) fn always(_: &Session) -> std::result::Result<(), String> { + Ok(()) +} + +pub(crate) fn has_layer(s: &Session) -> std::result::Result<(), String> { + s.selected_layer().map(|_| ()).ok_or_else(|| "no layer selected".into()) +} + +pub(crate) fn can_undo(s: &Session) -> std::result::Result<(), String> { + if s.can_undo() { Ok(()) } else { Err("nothing to undo".into()) } +} + +pub(crate) fn can_redo(s: &Session) -> std::result::Result<(), String> { + if s.can_redo() { Ok(()) } else { Err("nothing to redo".into()) } +} + +#[macro_export] +#[doc(hidden)] +macro_rules! cmd { + ($id:literal, $label:literal, [$($m:literal),*], $sc:expr, $params:literal, $en:expr, $run:expr) => { + $crate::commands::CommandSpec { id: $id, label: $label, menu: &[$($m),*], shortcut: $sc, params: $params, enabled: $en, run: $run } + }; +} + +pub fn command_specs() -> &'static [CommandSpec] { + static SPECS: std::sync::OnceLock> = std::sync::OnceLock::new(); + SPECS.get_or_init(|| { + let mut v = Vec::new(); + v.extend(crate::view_cmds::edit_specs()); + v.extend(crate::layer_cmds::specs()); + v.extend(crate::layer_ops::specs()); + v.extend(crate::instance_cmds::specs()); + v.extend(crate::mask_cmds::specs()); + v.extend(crate::tool_cmds::specs()); + v.extend(crate::path_tool::specs()); + v.extend(crate::view_cmds::specs()); + v.extend(crate::quick_mask::specs()); + v.extend(crate::set_cmds::specs()); + v.extend(crate::project_cmds::specs()); + v.extend(crate::bake_cmds::specs()); + v.extend(crate::file_cmds::specs()); + v.extend(crate::resource_cmds::specs()); + v.extend(crate::material_lib::specs()); + v.extend(crate::smart_masks::specs()); + v + }) +} + +pub fn find(id: &str) -> Option<&'static CommandSpec> { + command_specs().iter().find(|c| c.id == id) +} + +/// Run a command by id with JSON params (empty params = defaults, never a dialog). +pub fn execute(s: &mut Session, id: &str, params: &Value) -> Result { + let spec = find(id).ok_or_else(|| EngineError::UnknownCommand(id.to_string()))?; + (spec.enabled)(s).map_err(|why| EngineError::Disabled { cmd: id.to_string(), why })?; + (spec.run)(s, params) +} + +/// Every command, as JSON (for `commands` listings and MCP). +pub fn list(s: &Session, filter: Option<&str>) -> Value { + let f = filter.map(str::to_ascii_lowercase); + let items: Vec = command_specs() + .iter() + .filter(|c| f.as_ref().is_none_or(|f| c.id.to_ascii_lowercase().contains(f) || c.label.to_ascii_lowercase().contains(f))) + .map(|c| { + json!({ + "id": c.id, + "label": c.label, + "menu": c.menu, + "shortcut": c.shortcut, + "params": c.params, + "enabled": (c.enabled)(s).is_ok(), + }) + }) + .collect(); + Value::Array(items) +} + +// ---------- param helpers ---------- + +pub(crate) fn bad(cmd: &str, msg: impl Into) -> EngineError { + EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } +} + +pub(crate) fn f32p(p: &Value, key: &str) -> Option { + p.get(key).and_then(Value::as_f64).map(|v| v as f32).filter(|v| v.is_finite()) +} + +pub(crate) fn u64p(p: &Value, key: &str) -> Option { + p.get(key).and_then(|v| v.as_u64().or_else(|| v.as_f64().filter(|f| f.is_finite() && *f >= 0.0).map(|f| f.round() as u64))) +} + +pub(crate) fn boolp(p: &Value, key: &str) -> Option { + p.get(key).and_then(Value::as_bool) +} + +pub(crate) fn strp<'a>(p: &'a Value, key: &str) -> Option<&'a str> { + p.get(key).and_then(Value::as_str) +} + +/// A layer id param (`"layer"`), defaulting to the selected layer. +pub(crate) fn layer_param(s: &Session, cmd: &str, p: &Value) -> Result { + match u64p(p, "layer") { + Some(id) => { + let id = texturecraft_core::LayerId(id); + s.doc.project.layer(id).map_err(|_| bad(cmd, format!("no layer {}", id.0)))?; + Ok(id) + } + None => s.selected_layer().ok_or_else(|| bad(cmd, "no layer selected and no `layer` given")), + } +} + +/// [`layer_param`], following an instance to the layer whose content it draws. +pub(crate) fn content_param(s: &Session, cmd: &str, p: &Value) -> Result { + let id = layer_param(s, cmd, p)?; + Ok(s.doc.project.content_layer(id).map_or(id, |l| l.id)) +} + +pub(crate) fn channel_param(cmd: &str, p: &Value, key: &str) -> Result> { + match strp(p, key) { + None => Ok(None), + Some(s) => Channel::parse(s).map(Some).ok_or_else(|| bad(cmd, format!("unknown channel `{s}`"))), + } +} + +pub(crate) fn blend_param(cmd: &str, p: &Value, key: &str) -> Result> { + match strp(p, key) { + None => Ok(None), + Some(s) => BlendMode::parse(s).map(Some).ok_or_else(|| bad(cmd, format!("unknown blend mode `{s}`"))), + } +} + +/// 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]> { + match p.get(key)? { + Value::Array(a) if a.len() >= 3 => { + let v: Vec = 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 } + } + Value::Number(n) => n.as_f64().map(|v| [v as f32; 3]).filter(|v| v[0].is_finite()), + Value::String(s) => { + let s = s.trim_start_matches('#'); + if s.len() != 6 { + return None; + } + let c = |i: usize| u8::from_str_radix(s.get(i..i + 2)?, 16).ok().map(|v| texturecraft_texel::srgb_to_linear(v as f32 / 255.0)); + Some([c(0)?, c(2)?, c(4)?]) + } + _ => None, + } +} diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs new file mode 100644 index 00000000..a3924cf2 --- /dev/null +++ b/crates/engine/src/file_cmds.rs @@ -0,0 +1,432 @@ +//! Files: projects (`.tcraft`), mesh import, texture export. Byte-based functions work on the +//! web; path-based commands are native only. + +use serde_json::{Value, json}; +use texturecraft_core::NormalFormat; +#[cfg(not(target_arch = "wasm32"))] +use texturecraft_core::PROJECT_EXT; +use texturecraft_formats::{ExportInput, ExportOptions, ExportedFile, FileFormat, Padding, ProjectFile}; +use texturecraft_geom::{Mesh, primitives}; + +use crate::commands::{CommandSpec, always, bad, strp, u64p}; +use crate::session::{Document, Session}; +use crate::{EngineError, Result, cmd}; + +pub fn specs() -> Vec { + vec![ + cmd!( + "file.new", + "New project from a mesh file", + [], + None, + "{mesh: path to .glb/.gltf/.fbx/.dae/.obj/.ply/.stl, resolution?: 2048, name?}", + always, + new_from_mesh + ), + cmd!("file.open", "Open...", ["File"], Some("Ctrl+O"), "{path: .tcraft project or mesh file}", always, open), + cmd!("file.save", "Save", ["File"], Some("Ctrl+S"), "{path?: .tcraft (default: the project's own file)}", always, save), + cmd!("file.saveAs", "Save as...", ["File"], Some("Ctrl+Shift+S"), "{path}", always, save_as), + cmd!("file.saveCopy", "Save a copy...", ["File"], None, "{path}", always, save_copy), + cmd!("file.close", "Close", ["File"], Some("Ctrl+F4"), "{}", always, close), + cmd!("file.reimportMesh", "Reimport mesh", ["Edit"], Some("Ctrl+Shift+R"), "{path?}", always, reimport), + cmd!( + "file.exportTextures", + "Export textures", + ["File"], + None, + "{dir, preset?: name, ts?: index|\"all\", format?: png|jpg|tga|tif|exr|..., bitDepth?, padding?: none|dilation|dilationDefault|infinite, dilation?: px, size?: px, normalFormat?: openGl|directX}", + always, + export_textures + ), + cmd!( + "file.exportMesh", + "Export Mesh...", + ["File"], + None, + "{path: .obj or .glb file, format?: obj|glb (default: from the extension, else obj)}", + always, + export_mesh + ), + cmd!("export.presets", "Export presets", [], None, "{}", always, |_, _| Ok(json!( + texturecraft_formats::builtin_presets().iter().map(|p| json!({"name": p.name, "maps": p.maps.len()})).collect::>() + ))), + cmd!("export.plan", "List export outputs", [], None, "{preset?, ts?, format?}", always, plan), + ] +} + +fn fe(e: texturecraft_formats::FormatError) -> EngineError { + EngineError::Other(e.to_string()) +} + +fn ext_of(name: &str) -> String { + std::path::Path::new(name).extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase() +} + +fn stem_of(name: &str) -> String { + std::path::Path::new(name).file_stem().and_then(|e| e.to_str()).unwrap_or("Untitled").to_string() +} + +impl Session { + /// The working mesh as Wavefront OBJ. + pub fn mesh_obj_bytes(&self) -> Vec { + texturecraft_formats::obj_bytes(&self.mesh, &self.doc.project.name) + } + + /// The working mesh as binary glTF with each material's textures (from its texture set's + /// composite, through the texture export pipeline) embedded. + pub fn mesh_glb_bytes(&mut self) -> Result> { + let opts = texturecraft_formats::glb_export_options(); + let mut textures = Vec::with_capacity(self.mesh.materials.len()); + for m in 0..self.mesh.materials.len() { + let ts = self.doc.project.texture_sets.iter().position(|t| t.material == m); + let files = match ts { + Some(ts) => self.export_files(&[ts], &opts)?, + None => Vec::new(), + }; + textures.push(texturecraft_formats::GlbTextures::from_files(files)); + } + texturecraft_formats::glb_bytes(&self.mesh, &self.doc.project.name, &textures).map_err(fe) + } + + /// A new project from mesh file bytes (one texture set per material). + pub fn from_mesh_bytes(file_name: &str, bytes: &[u8], resolution: u32, name: &str) -> Result { + let imported = texturecraft_formats::import_mesh_bytes(file_name, bytes).map_err(fe)?; + let ext = ext_of(file_name); + let mut s = Session::new_project(name, imported.mesh, &format!("mesh.{ext}"), Some((ext, bytes.to_vec())), resolution)?; + s.log.extend(imported.report.warnings.iter().cloned()); + s.log.push(format!( + "Imported {} ({} triangles, {} materials{})", + file_name, + imported.report.triangles, + imported.report.materials, + if imported.report.uvs_generated { ", UVs generated" } else { "" } + )); + Ok(s) + } + + /// Open a `.tcraft` project from bytes. + pub fn from_project_bytes(bytes: &[u8]) -> Result { + let pf = texturecraft_formats::load_project_bytes(bytes).map_err(fe)?; + Session::from_project_file(pf) + } + + pub fn from_project_file(pf: ProjectFile) -> Result { + let mesh = match (&pf.mesh, pf.project.mesh.strip_prefix("builtin:")) { + (_, Some(id)) => primitives::builtin(id).ok_or_else(|| EngineError::Other(format!("unknown built-in mesh `{id}`")))?, + (Some((ext, bytes)), None) => texturecraft_formats::import_mesh_bytes(&format!("mesh.{ext}"), bytes).map_err(fe)?.mesh, + (None, None) => return Err(EngineError::Other("the project has no mesh".into())), + }; + check_materials(&pf.project, &mesh)?; + let source = pf.mesh.clone(); + let resources = pf.resources.into_iter().map(|(k, v)| (k, std::sync::Arc::new(v))).collect(); + let mut s = Session::new(Document { project: pf.project, pixels: pf.pixels, baked: pf.baked, resources }, mesh, source)?; + s.sync_images(); + s.load_bake_meshes(pf.high_poly, pf.cage); + Ok(s) + } + + /// The project as a `ProjectFile` (for saving). + pub fn project_file(&self) -> ProjectFile { + let mut pixels = self.doc.pixels.clone(); + // Drop pixel images whose size no longer matches their texture set (never unloadable). + let res_of = |id: texturecraft_core::LayerId| { + self.doc.project.locate(id).and_then(|(ts, _, _)| self.doc.project.texture_sets.get(ts)).map(|t| t.resolution).or_else(|| { + self.doc + .project + .effect(id) + .and_then(|(owner, _)| self.doc.project.locate(owner)) + .and_then(|(ts, _, _)| self.doc.project.texture_sets.get(ts)) + .map(|t| t.resolution) + }) + }; + pixels.layers.retain(|(id, _), img| res_of(*id) == Some(img.width())); + pixels.masks.retain(|id, img| res_of(*id) == Some(img.width())); + let (high_poly, cage) = self.saved_bake_meshes(); + ProjectFile { + project: self.doc.project.clone(), + pixels, + baked: self.doc.baked.clone(), + mesh: self.mesh_source.clone(), + high_poly, + cage, + thumbnail: None, + resources: self.doc.resources.iter().map(|(k, v)| (k.clone(), v.as_ref().clone())).collect(), + } + } + + pub fn to_project_bytes(&self) -> Result> { + texturecraft_formats::save_project_bytes(&self.project_file()).map_err(fe) + } + + /// Render every export file for texture sets `sets` (in memory; works on the web). + pub fn export_files(&mut self, sets: &[usize], opts: &ExportOptions) -> Result> { + self.update(); + let mesh_name = stem_of(self.doc.project.mesh.trim_start_matches("builtin:")); + let mut out = Vec::new(); + let empty = Default::default(); + for &ts in sets { + let cache = self.caches.get(ts).ok_or_else(|| EngineError::Other(format!("no texture set {ts}")))?; + let coverage = cache.gbuf.coverage_mask(); + let input = ExportInput { + project: &self.doc.project, + ts, + composite: &cache.composite, + baked: self.doc.baked.get(&ts).unwrap_or(&empty), + coverage: &coverage, + mesh_name: &mesh_name, + }; + out.extend(texturecraft_formats::render_maps(&input, opts).map_err(fe)?); + } + Ok(out) + } +} + +fn check_materials(p: &texturecraft_core::Project, mesh: &Mesh) -> Result<()> { + for t in &p.texture_sets { + if t.material >= mesh.materials.len().max(1) { + return Err(EngineError::Other(format!("texture set \"{}\" refers to a material the mesh doesn't have", t.name))); + } + } + Ok(()) +} + +/// Export options from command params. +pub fn export_options(p: &Value, cmd: &str) -> Result { + let name = strp(p, "preset").unwrap_or("Document channels + Normal + AO"); + let preset = texturecraft_formats::builtin_preset(name).ok_or_else(|| bad(cmd, format!("unknown export preset `{name}`")))?; + let mut o = ExportOptions::new(preset); + if let Some(f) = strp(p, "format") { + o.format_override = Some(FileFormat::from_extension(f).ok_or_else(|| bad(cmd, format!("unknown format `{f}`")))?); + } + if let Some(b) = u64p(p, "bitDepth") { + o.bit_depth_override = Some(b.min(32) as u8); + } + let px = u64p(p, "dilation").map(|v| v.min(256) as u32).unwrap_or(16); + if let Some(pad) = strp(p, "padding") { + o.padding = match pad { + "none" => Padding::None, + "dilation" => Padding::Dilation(px), + "dilationDefault" => Padding::DilationDefault(px), + "infinite" => Padding::Infinite, + other => return Err(bad(cmd, format!("unknown padding `{other}`"))), + }; + } + if let Some(sz) = u64p(p, "size") { + o.size = Some(crate::session::check_resolution(u32::try_from(sz).unwrap_or(0))?); + } + o.normal_format = match strp(p, "normalFormat") { + Some("openGl") | Some("opengl") => Some(NormalFormat::OpenGl), + Some("directX") | Some("directx") => Some(NormalFormat::DirectX), + Some(other) => return Err(bad(cmd, format!("unknown normal format `{other}`"))), + None => None, + }; + Ok(o) +} + +fn sets_param(s: &Session, p: &Value, cmd: &str) -> Result> { + match p.get("ts") { + None => Ok((0..s.set_count()).collect()), + Some(Value::String(a)) if a == "all" => Ok((0..s.set_count()).collect()), + Some(v) => Ok(vec![v.as_u64().map(|t| t as usize).filter(|t| *t < s.set_count()).ok_or_else(|| bad(cmd, "invalid `ts`"))?]), + } +} + +fn plan(s: &mut Session, p: &Value) -> Result { + let opts = export_options(p, "export.plan")?; + let mesh_name = stem_of(s.doc.project.mesh.trim_start_matches("builtin:")); + let mut names = Vec::new(); + for ts in sets_param(s, p, "export.plan")? { + for f in texturecraft_formats::list_outputs(&s.doc.project, ts, &mesh_name, &opts).map_err(fe)? { + names.push(json!({"ts": ts, "file": f.name, "format": f.format.extension(), "bitDepth": f.bit_depth, "warnings": f.warnings})); + } + } + Ok(json!(names)) +} + +#[cfg(not(target_arch = "wasm32"))] +mod native { + use super::*; + use std::path::Path; + + pub fn path_param<'a>(p: &'a Value, key: &str, cmd: &str) -> Result<&'a str> { + strp(p, key).filter(|s| !s.trim().is_empty()).ok_or_else(|| bad(cmd, format!("`{key}` is required"))) + } + + /// Open a project or a mesh file. + pub fn open_path(path: &str) -> Result { + let p = Path::new(path); + if ext_of(path) == PROJECT_EXT { + let pf = texturecraft_formats::load_project(p).map_err(fe)?; + let mut s = Session::from_project_file(pf)?; + s.path = Some(path.to_string()); + s.modified = false; + Ok(s) + } else { + let bytes = std::fs::read(p).map_err(|e| EngineError::Other(format!("cannot read {path}: {e}")))?; + Session::from_mesh_bytes(path, &bytes, 2048, &stem_of(path)) + } + } + + pub fn save_to(s: &mut Session, path: &str, adopt: bool) -> Result { + let path = if ext_of(path) == PROJECT_EXT { path.to_string() } else { format!("{path}.{PROJECT_EXT}") }; + texturecraft_formats::save_project(Path::new(&path), &s.project_file()).map_err(fe)?; + if adopt { + s.path = Some(path.clone()); + s.modified = false; + } + s.log.push(format!("Saved {path}")); + Ok(json!({ "path": path })) + } +} + +#[cfg(not(target_arch = "wasm32"))] +pub use native::open_path; + +#[cfg(not(target_arch = "wasm32"))] +fn new_from_mesh(s: &mut Session, p: &Value) -> Result { + let path = native::path_param(p, "mesh", "file.new")?; + let bytes = std::fs::read(path).map_err(|e| bad("file.new", format!("cannot read {path}: {e}")))?; + let res = u32::try_from(u64p(p, "resolution").unwrap_or(2048)).unwrap_or(0); + let name = strp(p, "name").map(str::to_string).unwrap_or_else(|| stem_of(path)); + let mut next = Session::from_mesh_bytes(path, &bytes, res, &name)?; + next.viewport = s.viewport; + *s = next; + Ok(crate::inspect::session(s)) +} + +#[cfg(not(target_arch = "wasm32"))] +fn open(s: &mut Session, p: &Value) -> Result { + let path = native::path_param(p, "path", "file.open")?; + let mut next = native::open_path(path)?; + next.viewport = s.viewport; + *s = next; + Ok(crate::inspect::session(s)) +} + +#[cfg(not(target_arch = "wasm32"))] +fn save(s: &mut Session, p: &Value) -> Result { + let path = match strp(p, "path") { + Some(x) => x.to_string(), + None => s.path.clone().ok_or_else(|| bad("file.save", "the project has no file yet: give `path` (Save as)"))?, + }; + native::save_to(s, &path, true) +} + +#[cfg(not(target_arch = "wasm32"))] +fn save_as(s: &mut Session, p: &Value) -> Result { + let path = native::path_param(p, "path", "file.saveAs")?.to_string(); + native::save_to(s, &path, true) +} + +#[cfg(not(target_arch = "wasm32"))] +fn save_copy(s: &mut Session, p: &Value) -> Result { + let path = native::path_param(p, "path", "file.saveCopy")?.to_string(); + native::save_to(s, &path, false) +} + +#[cfg(not(target_arch = "wasm32"))] +fn reimport(s: &mut Session, p: &Value) -> Result { + // Re-read the mesh and keep the project (texture sets map by material index). + let mesh = match strp(p, "path") { + Some(path) => { + let bytes = std::fs::read(path).map_err(|e| bad("file.reimportMesh", format!("cannot read {path}: {e}")))?; + let m = texturecraft_formats::import_mesh_bytes(path, &bytes).map_err(fe)?.mesh; + s.mesh_source = Some((ext_of(path), bytes)); + s.doc.project.mesh = format!("mesh.{}", ext_of(path)); + m + } + None => match &s.mesh_source { + Some((ext, bytes)) => texturecraft_formats::import_mesh_bytes(&format!("mesh.{ext}"), bytes).map_err(fe)?.mesh, + None => return Err(bad("file.reimportMesh", "built-in meshes can't be reimported; give `path`")), + }, + }; + check_materials(&s.doc.project, &mesh)?; + let doc = s.doc.clone(); + let source = s.mesh_source.clone(); + let mut next = Session::new(doc, mesh, source)?; + next.path = s.path.clone(); + next.viewport = s.viewport; + *s = next; + Ok(Value::Null) +} + +#[cfg(not(target_arch = "wasm32"))] +fn export_textures(s: &mut Session, p: &Value) -> Result { + let dir = native::path_param(p, "dir", "file.exportTextures")?.to_string(); + let opts = export_options(p, "file.exportTextures")?; + let sets = sets_param(s, p, "file.exportTextures")?; + std::fs::create_dir_all(&dir).map_err(|e| bad("file.exportTextures", format!("cannot create {dir}: {e}")))?; + let files = s.export_files(&sets, &opts)?; + let warnings: Vec = files.iter().flat_map(|f| f.warnings.iter().cloned()).collect(); + let written = texturecraft_formats::write_files(&files, std::path::Path::new(&dir)).map_err(fe)?; + s.log.push(format!("Exported {} textures to {dir}", written.len())); + Ok(json!({ "files": written.iter().map(|p| p.display().to_string()).collect::>(), "warnings": warnings })) +} + +#[cfg(not(target_arch = "wasm32"))] +fn export_mesh(s: &mut Session, p: &Value) -> Result { + let path = native::path_param(p, "path", "file.exportMesh")?; + let ext = match strp(p, "format") { + Some(f @ ("obj" | "glb")) => f.to_string(), + Some(other) => return Err(bad("file.exportMesh", format!("unknown mesh format `{other}` (obj or glb)"))), + None if ext_of(path) == "glb" => "glb".into(), + None => "obj".into(), + }; + let path = if ext_of(path) == ext { path.to_string() } else { format!("{path}.{ext}") }; + let bytes = if ext == "glb" { s.mesh_glb_bytes()? } else { s.mesh_obj_bytes() }; + std::fs::write(&path, bytes).map_err(|e| bad("file.exportMesh", format!("cannot write {path}: {e}")))?; + s.log.push(format!("Exported mesh to {path}")); + Ok(json!({ "path": path })) +} + +#[cfg(target_arch = "wasm32")] +fn unsupported(_: &mut Session, _: &Value) -> Result { + Err(EngineError::Other("file paths are not available in the browser; use the web file buttons".into())) +} +#[cfg(target_arch = "wasm32")] +use unsupported as new_from_mesh; +#[cfg(target_arch = "wasm32")] +use unsupported as open; +#[cfg(target_arch = "wasm32")] +use unsupported as save; +#[cfg(target_arch = "wasm32")] +use unsupported as save_as; +#[cfg(target_arch = "wasm32")] +use unsupported as save_copy; +#[cfg(target_arch = "wasm32")] +use unsupported as reimport; +#[cfg(target_arch = "wasm32")] +use unsupported as export_textures; +#[cfg(target_arch = "wasm32")] +use unsupported as export_mesh; + +fn close(s: &mut Session, _: &Value) -> Result { + let mut next = Session::new_project("Untitled", primitives::material_ball(), "builtin:material_ball", None, 2048)?; + next.viewport = s.viewport; + *s = next; + Ok(Value::Null) +} + +#[cfg(all(test, not(target_arch = "wasm32")))] +mod tests { + use super::*; + + #[test] + fn export_mesh_writes_obj_by_default_and_textured_glb() { + let mut s = Session::new_project("t", primitives::material_ball(), "builtin:material_ball", None, 128).unwrap(); + let dir = std::env::temp_dir().join(format!("tc_export_mesh_{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let base = dir.join("ball").display().to_string(); + let obj = crate::execute(&mut s, "file.exportMesh", &json!({ "path": base })).unwrap(); + assert!(obj["path"].as_str().unwrap().ends_with("ball.obj")); + let glb = crate::execute(&mut s, "file.exportMesh", &json!({ "path": base, "format": "glb" })).unwrap(); + let bytes = std::fs::read(glb["path"].as_str().unwrap()).unwrap(); + let back = texturecraft_formats::import_mesh_bytes("ball.glb", &bytes).unwrap(); + assert_eq!(back.mesh.triangle_count(), s.mesh.triangle_count()); + assert_eq!(back.mesh.materials, s.mesh.materials); + // Base color, ORM and normal per texture set. + assert_eq!(bytes.windows(9).filter(|w| *w == b"image/png").count(), 3 * s.mesh.materials.len()); + assert!(crate::execute(&mut s, "file.exportMesh", &json!({ "path": base, "format": "fbx" })).is_err()); + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/crates/engine/src/inspect.rs b/crates/engine/src/inspect.rs new file mode 100644 index 00000000..192e181b --- /dev/null +++ b/crates/engine/src/inspect.rs @@ -0,0 +1,143 @@ +//! JSON views of the session for agents: everything an agent changed must be visible here +//! without a screenshot. + +use serde_json::{Value, json}; +use texturecraft_core::{Effect, EffectKind, Fill, Filter, Layer, LayerKind, Source}; + +use crate::Session; +use crate::session::StackTarget; + +fn source_json(s: &Source) -> Value { + match s { + Source::Uniform(v) => json!({ "uniform": v }), + Source::Procedural { pattern, low, high } => json!({ + "procedural": pattern.kind.label(), "scale": pattern.scale, "seed": pattern.seed, "contrast": pattern.contrast, "low": low, "high": high + }), + Source::Image { name } => json!({ "image": name }), + Source::Anchor { anchor, channel } => json!({ "anchor": anchor.0, "channel": channel.id() }), + } +} + +fn fill_json(f: &Fill) -> Value { + json!({ + "projection": f.projection.label(), + "sources": f.sources.iter().map(|(c, s)| (c.id().to_string(), source_json(s))).collect::>(), + }) +} + +/// A mask or content (`content`) effect. +fn effect_json(s: &Session, e: &Effect, content: bool) -> Value { + let mut ej = json!({ "id": e.id.0, "name": e.name, "kind": e.kind.name(), "visible": e.visible, "blend": e.blend.label(), "opacity": e.opacity }); + match &e.kind { + EffectKind::Generator(g) => ej["generator"] = json!({ "type": format!("{:?}", g.kind), "level": g.level, "softness": g.softness, "noise": g.noise_amount, "invert": g.invert }), + EffectKind::Levels(lv) => ej["levels"] = json!([lv.in_low, lv.in_high, lv.gamma, lv.out_low, lv.out_high]), + EffectKind::Paint if content => { + ej["tiles"] = json!(s.doc.pixels.layers.iter().filter(|((id, _), _)| *id == e.id).map(|(_, img)| img.allocated_count()).sum::()) + } + EffectKind::Paint => ej["tiles"] = json!(s.doc.pixels.masks.get(&e.id).map(|i| i.allocated_count()).unwrap_or(0)), + EffectKind::Fill { source, .. } => ej["source"] = source_json(source), + EffectKind::Material(f) => ej["fill"] = fill_json(f), + EffectKind::Filter(Filter::Hsl { hue, saturation, lightness }) => ej["hsl"] = json!([hue, saturation, lightness]), + EffectKind::Filter(Filter::Blur { radius }) => ej["radius"] = json!(radius), + _ => {} + } + ej +} + +fn layer_json(s: &Session, l: &Layer, depth: usize) -> Value { + let channels: serde_json::Map = + l.channels.iter().map(|(c, b)| (c.id().to_string(), json!({ "mode": b.mode.label(), "opacity": b.opacity }))).collect(); + let mut v = json!({ + "id": l.id.0, + "name": l.name, + "kind": l.kind.name(), + "visible": l.visible, + "channels": channels, + "colorTag": l.color_tag, + }); + if let LayerKind::Fill(f) = &l.kind { + v["fill"] = fill_json(f); + } + if let Some(src) = l.instance_source() { + let source = s.doc.project.layers.get(&src); + let set = s.doc.project.locate(src).map(|(ts, _, _)| ts); + v["instanceOf"] = json!({ "layer": src.0, "name": source.map(|x| x.name.as_str()), "textureSet": set }); + } + if l.height_mask.enabled { + v["heightMask"] = json!({ "contrast": l.height_mask.contrast, "offset": l.height_mask.offset }); + } + if matches!(l.kind, LayerKind::Paint) { + // Sorted: the store is a hash map, and inspect output must be stable. + 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(); + v["painted"] = json!(painted.iter().map(|(c, n)| json!({ "channel": c, "tiles": n })).collect::>()); + } + if let Some(m) = &l.mask { + v["mask"] = json!({ + "enabled": m.enabled, + "background": format!("{:?}", m.background).to_lowercase(), + "effects": m.effects.iter().map(|e| effect_json(s, e, false)).collect::>(), + }); + } + if !l.content_effects.is_empty() { + v["contentEffects"] = json!(l.content_effects.iter().map(|e| effect_json(s, e, true)).collect::>()); + } + if depth < texturecraft_core::project::MAX_DEPTH { + let kids: Vec = l.children().iter().rev().filter_map(|c| s.doc.project.layers.get(c)).map(|c| layer_json(s, c, depth + 1)).collect(); + if l.is_group() { + v["children"] = json!(kids); + } + } + v +} + +/// The layer stack of texture set `ts`, top → bottom (as displayed). +pub fn stack(s: &Session, ts: usize) -> Value { + let Some(set) = s.doc.project.texture_sets.get(ts) else { return Value::Null }; + json!(set.stack.iter().rev().filter_map(|id| s.doc.project.layers.get(id)).map(|l| layer_json(s, l, 0)).collect::>()) +} + +pub fn texture_sets(s: &Session) -> Value { + json!( + s.doc + .project + .texture_sets + .iter() + .enumerate() + .map(|(i, t)| json!({ + "index": i, + "name": t.name, + "resolution": t.resolution, + "channels": t.channels.iter().map(|(c, f)| json!({ "channel": c.id(), "format": f.label() })).collect::>(), + "layers": s.doc.project.walk(i).len(), + "baked": crate::project_cmds::baked_json(s.doc.baked.get(&i)), + "visible": t.visible, + "active": i == s.ts(), + })) + .collect::>() + ) +} + +/// The whole session. +pub fn session(s: &Session) -> Value { + let ts = s.ts(); + json!({ + "project": s.doc.project.name, + "mesh": { "source": s.doc.project.mesh, "triangles": s.mesh.triangle_count(), "vertices": s.mesh.vertex_count(), "materials": s.mesh.materials }, + "path": s.path, + "modified": s.modified, + "textureSets": texture_sets(s), + "bakeMeshes": crate::bake_cmds::meshes_json(s), + "activeTextureSet": ts, + "selectedLayer": s.selected_layer().map(|l| l.0), + "target": match s.target { StackTarget::Content => json!("content"), StackTarget::Mask => json!("mask"), StackTarget::Effect(e) => json!(e.0) }, + "layers": stack(s, ts), + "tool": { "tool": s.tool.tool.id(), "brush": crate::tool_cmds::brush_json(s) }, + "view": s.view.label(), + "camera": crate::view_cmds::camera_json(s), + "display": s.doc.project.display, + "history": { "undo": s.undo_label(), "redo": s.redo_label(), "depth": s.history_labels().len() }, + "memoryBytes": s.memory().total(), + "pendingTiles": s.pending_tiles(), + }) +} diff --git a/crates/engine/src/instance_cmds.rs b/crates/engine/src/instance_cmds.rs new file mode 100644 index 00000000..eb0a6a4a --- /dev/null +++ b/crates/engine/src/instance_cmds.rs @@ -0,0 +1,163 @@ +//! Instance layers (linked copies that draw another layer, possibly in another texture set) and +//! per-layer properties set through `layer.set`. + +use serde_json::{Value, json}; +use texturecraft_core::{Layer, LayerId, LayerKind}; + +use crate::commands::{CommandSpec, bad, boolp, content_param, f32p, has_layer, layer_param, u64p}; +use crate::session::{Document, StackTarget}; +use crate::{EngineError, Result, Session, cmd}; + +pub fn specs() -> Vec { + vec![ + cmd!( + "layer.instance", + "Create instance", + ["Layers"], + None, + "{layer?, ts?: texture set (default active)} — a linked instance above that set's selection; edits to the source show in every instance", + has_layer, + instance + ), + cmd!( + "layer.pasteInstance", + "Paste as instance", + ["Layers"], + None, + "{} — an instance of the copied layer above the selection", + can_paste_instance, + paste_instance + ), + cmd!( + "layer.set", + "Set layer properties", + [], + None, + "{layer?, heightMask?: {enabled?, contrast?: 0..64, offset?: -1..1} | bool} — mask with height: m' = saturate(m + contrast·4m(1−m)·(height + offset))", + has_layer, + set + ), + ] +} + +/// Insert an instance of `source` (an instance's own source) into texture set `ts` above that +/// set's selection, and select it. +pub(crate) fn make_instance(s: &mut Session, source: LayerId, ts: usize) -> Result { + let src = s.doc.project.content_layer(source).ok_or_else(|| EngineError::Other(format!("layer {} has no source to instance", source.0)))?; + let (src_id, name) = (src.id, src.name.clone()); + let id = s.doc.project.alloc_id(); + let sel = s.selected.get(&ts).copied().filter(|l| s.doc.project.locate(*l).is_some_and(|(t, _, _)| t == ts)); + let inside = sel.and_then(|l| s.doc.project.layer(l).ok()).is_some_and(Layer::is_group); + s.doc.project.insert_layer(ts, Layer::new(id, name, LayerKind::Instance { source: src_id }, &[]), sel, inside)?; + s.selected.insert(ts, id); + if ts == s.ts() { + s.target = StackTarget::Content; + } + Ok(id) +} + +fn instance(s: &mut Session, p: &Value) -> Result { + let source = layer_param(s, "layer.instance", p)?; + let ts = match u64p(p, "ts") { + Some(t) => usize::try_from(t).ok().filter(|t| *t < s.set_count()).ok_or_else(|| bad("layer.instance", format!("no texture set {t}")))?, + None => s.ts(), + }; + let id = s.edit("Create instance", |s| make_instance(s, source, ts))?; + Ok(json!({ "layer": id.0, "textureSet": ts })) +} + +fn can_paste_instance(s: &Session) -> std::result::Result<(), String> { + match &s.clipboard { + Some((_, id)) if s.doc.project.locate(*id).is_some() => Ok(()), + Some(_) => Err("the copied layer is no longer in the project (paste it instead)".into()), + None => Err("the layer clipboard is empty".into()), + } +} + +fn paste_instance(s: &mut Session, _: &Value) -> Result { + let id = s.clipboard.as_ref().map(|(_, id)| *id).ok_or_else(|| EngineError::Other("the layer clipboard is empty".into()))?; + let ts = s.ts(); + let new = s.edit("Paste as instance", |s| make_instance(s, id, ts))?; + Ok(json!({ "layer": new.0 })) +} + +/// Replace instance `inst` with an independent copy of its source taken from `src` (its name, +/// visibility, color tag and own mask are kept). Returns the copy. +pub(crate) fn detach(s: &mut Session, src: &Document, inst: LayerId) -> Result { + let layer = s.doc.project.layer(inst)?.clone(); + let source = layer.instance_source().ok_or_else(|| EngineError::Other(format!("\"{}\" is not an instance", layer.name)))?; + let (ts, _, _) = s.doc.project.locate(inst).ok_or_else(|| EngineError::Other("layer not in a stack".into()))?; + let copy = crate::layer_ops::copy_tree(src, source, &mut s.doc, ts, Some(inst), false, 0)?; + let l = s.doc.project.layer_mut(copy)?; + l.name = layer.name; + l.visible = layer.visible; + l.color_tag = layer.color_tag; + if layer.mask.is_some() { + l.mask = layer.mask; + } + s.doc.project.remove_layer(inst)?; + if s.selected.get(&ts) == Some(&inst) { + s.selected.insert(ts, copy); + } + Ok(copy) +} + +/// After layers were removed: instances of a removed layer become independent copies (taken +/// from `before`, the document before the removal), then pixels of removed layers are dropped. +pub(crate) fn detach_orphans(s: &mut Session, before: &Document) -> Result<()> { + // Copies can hold instances of removed layers in turn; the depth bounds the rounds. + for _ in 0..=texturecraft_core::project::MAX_DEPTH { + let orphans: Vec = s + .doc + .project + .layers + .values() + .filter(|l| l.instance_source().is_some_and(|src| !s.doc.project.layers.contains_key(&src) && before.project.layers.contains_key(&src))) + .map(|l| l.id) + .collect(); + if orphans.is_empty() { + break; + } + for inst in orphans { + detach(s, before, inst)?; + } + } + prune_pixels(&mut s.doc); + Ok(()) +} + +/// Drop pixels of layers and effects that no longer exist. +pub(crate) fn prune_pixels(doc: &mut Document) { + let p = &doc.project; + let alive: std::collections::BTreeSet = p + .layers + .values() + .flat_map(|l| std::iter::once(l.id).chain(l.mask.iter().flat_map(|m| m.effects.iter()).chain(l.content_effects.iter()).map(|e| e.id))) + .collect(); + doc.pixels.retain(|x| alive.contains(&x)); +} + +fn set(s: &mut Session, p: &Value) -> Result { + // Linked properties live on an instance's source. + let id = content_param(s, "layer.set", p)?; + let hm = match p.get("heightMask") { + Some(Value::Bool(on)) => json!({ "enabled": on }), + Some(v @ Value::Object(_)) => v.clone(), + Some(_) => return Err(bad("layer.set", "`heightMask` must be an object {enabled?, contrast?, offset?} or a bool")), + None => return Err(bad("layer.set", "give `heightMask`")), + }; + let out = s.edit("Layer properties", |s| { + let h = &mut s.doc.project.layer_mut(id)?.height_mask; + if let Some(on) = boolp(&hm, "enabled") { + h.enabled = on; + } + if let Some(c) = f32p(&hm, "contrast") { + h.contrast = c.clamp(0.0, 64.0); + } + if let Some(o) = f32p(&hm, "offset") { + h.offset = o.clamp(-1.0, 1.0); + } + Ok(json!({ "layer": id.0, "heightMask": { "enabled": h.enabled, "contrast": h.contrast, "offset": h.offset } })) + })?; + Ok(out) +} diff --git a/crates/engine/src/layer_cmds.rs b/crates/engine/src/layer_cmds.rs new file mode 100644 index 00000000..e7831f12 --- /dev/null +++ b/crates/engine/src/layer_cmds.rs @@ -0,0 +1,404 @@ +//! Layer stack commands: add, remove, duplicate, select, rename, visibility, reorder, group, +//! per-channel blending and fill sources. + +use serde_json::{Value, json}; +use texturecraft_core::{BlendMode, Channel, ChannelBlend, EffectKind, Fill, Layer, LayerId, LayerKind, Procedural, ProceduralKind, Projection, Source}; + +use crate::commands::{CommandSpec, always, bad, blend_param, boolp, channel_param, color_param, content_param, f32p, has_layer, layer_param, strp, u64p}; +use crate::{EngineError, Result, Session, cmd, session::StackTarget}; + +pub fn specs() -> Vec { + vec![ + cmd!("layer.add.paint", "Add paint layer", ["Layers"], None, "{name?}", always, add_paint), + cmd!("layer.add.fill", "Add fill layer", ["Layers"], None, "{name?, color?, roughness?, metallic?}", always, add_fill), + cmd!("layer.add.group", "Add folder", ["Layers"], None, "{name?}", always, add_group), + cmd!("layer.remove", "Remove layer(s)", ["Layers"], Some("Delete"), "{layer?}", has_layer, remove), + cmd!("layer.duplicate", "Duplicate layer(s)", ["Layers"], Some("Ctrl+D"), "{layer?}", has_layer, duplicate), + cmd!("layer.group", "Group layer(s)", ["Layers"], Some("Ctrl+G"), "{layer?}", has_layer, group), + cmd!("layer.select", "Select layer", [], None, "{layer, target?: content|mask|}", always, select), + cmd!("layer.rename", "Rename layer", [], None, "{layer?, name}", has_layer, rename), + cmd!("layer.visible", "Toggle layer visibility", [], None, "{layer?, visible?}", has_layer, visible), + cmd!("layer.move", "Move layer", [], None, "{layer?, index?} | {layer?, above?: id|null, inside?: bool} | {layer?, delta: ±n}", has_layer, move_layer), + cmd!("layer.blend", "Set channel blending", [], None, "{layer?, channel?: id|all, mode?, opacity?: 0..1, enabled?}", has_layer, blend), + cmd!( + "layer.fill.set", + "Set fill source", + [], + None, + "{layer? | effect?: content fill effect id, channel, color?|value?|procedural?: kind, scale?, seed?, contrast?, balance?, invert?, low?, high?|image?|anchor?: anchor effect id, anchorChannel?, enabled?}", + has_layer, + fill_set + ), + cmd!( + "layer.fill.projection", + "Set fill projection", + [], + None, + "{layer? | effect?: content fill effect id, projection: uv|triplanar|planar|spherical|cylindrical, scale?, rotation?, sharpness?, axis?}", + has_layer, + fill_projection + ), + cmd!("layer.colorTag", "Set layer color tag", [], None, "{layer?, tag?: 0..7 (absent clears)}", has_layer, color_tag), + cmd!("layer.list", "List layers", [], None, "{ts?}", always, list), + ] +} + +/// Insert a layer above the selection (inside it when it is a group) and select it. +pub(crate) fn insert_selected(s: &mut Session, layer: Layer) -> Result { + let ts = s.ts(); + let sel = s.selected_layer(); + let inside = sel.and_then(|id| s.doc.project.layer(id).ok()).is_some_and(|l| l.is_group()); + let id = s.doc.project.insert_layer(ts, layer, sel, inside)?; + s.selected.insert(ts, id); + s.target = StackTarget::Content; + Ok(id) +} + +fn channels_of(s: &Session) -> Vec { + s.doc.project.texture_set(s.ts()).map(|t| t.channel_list()).unwrap_or_default() +} + +fn add_paint(s: &mut Session, p: &Value) -> Result { + let ts = s.ts(); + let name = strp(p, "name").map(str::to_string).unwrap_or_else(|| s.doc.project.next_layer_name(ts, "Layer")); + let id = s.edit("Add paint layer", |s| { + let id = s.doc.project.alloc_id(); + insert_selected(s, Layer::new(id, name, LayerKind::Paint, &channels_of(s))) + })?; + Ok(json!({ "layer": id.0 })) +} + +/// A uniform fill from the `color`, `roughness`, `metallic`, `height` and `opacity` params. +pub(crate) fn fill_from(p: &Value) -> Fill { + let mut fill = Fill::default(); + if let Some(c) = color_param(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)] { + if let Some(v) = f32p(p, key) { + fill.sources.insert(ch, Source::Uniform([v; 3])); + } + } + fill +} + +fn add_fill(s: &mut Session, p: &Value) -> Result { + 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 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))) + })?; + Ok(json!({ "layer": id.0 })) +} + +fn add_group(s: &mut Session, p: &Value) -> Result { + let ts = s.ts(); + let name = strp(p, "name").map(str::to_string).unwrap_or_else(|| s.doc.project.next_layer_name(ts, "Folder")); + let id = s.edit("Add folder", |s| { + let id = s.doc.project.alloc_id(); + let mut l = Layer::new(id, name, LayerKind::Group { children: Vec::new(), collapsed: false }, &channels_of(s)); + for b in l.channels.values_mut() { + b.mode = BlendMode::Passthrough; + } + insert_selected(s, l) + })?; + Ok(json!({ "layer": id.0 })) +} + +fn remove(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "layer.remove", p)?; + let before = s.doc.clone(); + let removed = s.edit("Remove layer", |s| { + let (ts, parent, i) = s.doc.project.locate(id).ok_or(EngineError::Other("layer not in a stack".into()))?; + let removed = s.doc.project.remove_layer(id)?; + // Instances of what was removed keep their look as independent layers. + crate::instance_cmds::detach_orphans(s, &before)?; + // Select the layer below (or above) the removed one. + let list = match parent { + Some(g) => s.doc.project.layer(g).map(|l| l.children().to_vec()).unwrap_or_default(), + None => s.doc.project.texture_set(ts).map(|t| t.stack.clone()).unwrap_or_default(), + }; + let next = list.get(i.saturating_sub(1)).or(list.first()).copied().or(parent); + match next { + Some(n) => s.selected.insert(ts, n), + None => s.selected.remove(&ts), + }; + Ok(removed) + })?; + Ok(json!({ "removed": removed.iter().map(|l| l.0).collect::>() })) +} + +fn duplicate(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "layer.duplicate", p)?; + let (ts, _, _) = s.doc.project.locate(id).ok_or(EngineError::Other("layer not in a stack".into()))?; + let src = s.doc.clone(); + let new = s.edit("Duplicate layer", |s| { + let new = crate::layer_ops::copy_tree(&src, id, &mut s.doc, ts, Some(id), false, 0)?; + let name = format!("{} copy", src.project.layer(id)?.name); + s.doc.project.layer_mut(new)?.name = name; + Ok(new) + })?; + s.selected.insert(ts, new); + Ok(json!({ "layer": new.0 })) +} + +fn group(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "layer.group", p)?; + let g = s.edit("Group layers", |s| { + let (ts, _, _) = s.doc.project.locate(id).ok_or(EngineError::Other("layer not in a stack".into()))?; + let gid = s.doc.project.alloc_id(); + let name = s.doc.project.next_layer_name(ts, "Folder"); + let channels = s.doc.project.texture_set(ts)?.channel_list(); + let mut gl = Layer::new(gid, name, LayerKind::Group { children: Vec::new(), collapsed: false }, &channels); + for b in gl.channels.values_mut() { + b.mode = BlendMode::Passthrough; + } + s.doc.project.insert_layer(ts, gl, Some(id), false)?; + s.doc.project.move_above(id, Some(gid), true)?; + s.selected.insert(ts, gid); + Ok(gid) + })?; + Ok(json!({ "layer": g.0 })) +} + +fn select(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "layer.select", p)?; + let (ts, _, _) = s.doc.project.locate(id).ok_or_else(|| bad("layer.select", "layer not in a stack"))?; + s.active_set = ts; + s.selected.insert(ts, id); + s.target = match p.get("target") { + Some(Value::String(t)) if t == "mask" => { + if s.doc.project.layer(s.doc.project.mask_owner(id))?.mask.is_none() { + return Err(bad("layer.select", "the layer has no mask")); + } + StackTarget::Mask + } + Some(Value::String(t)) if t == "content" => StackTarget::Content, + Some(v) => match v.as_u64() { + Some(e) if s.doc.project.effect(LayerId(e)).is_some_and(|(owner, _)| owner == id) => StackTarget::Effect(LayerId(e)), + _ => return Err(bad("layer.select", "target must be content, mask or an effect id of this layer")), + }, + None => StackTarget::Content, + }; + s.revision = s.revision.wrapping_add(1); + Ok(json!({ "layer": id.0, "textureSet": ts })) +} + +fn rename(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "layer.rename", p)?; + let name = strp(p, "name").map(str::trim).filter(|n| !n.is_empty()).ok_or_else(|| bad("layer.rename", "`name` is required"))?.to_string(); + if name.chars().count() > 256 { + return Err(bad("layer.rename", "name is longer than 256 characters")); + } + s.edit("Rename layer", |s| { + s.doc.project.layer_mut(id)?.name = name; + Ok(()) + })?; + Ok(Value::Null) +} + +fn visible(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "layer.visible", p)?; + let v = s.edit("Layer visibility", |s| { + let l = s.doc.project.layer_mut(id)?; + l.visible = boolp(p, "visible").unwrap_or(!l.visible); + Ok(l.visible) + })?; + Ok(json!({ "visible": v })) +} + +fn move_layer(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "layer.move", p)?; + s.edit("Move layer", |s| { + if let Some(d) = p.get("delta").and_then(Value::as_i64) { + let (_, _, i) = s.doc.project.locate(id).ok_or(EngineError::Other("layer not in a stack".into()))?; + let to = (i as i64 + d).max(0) as usize; + s.doc.project.move_layer(id, to)?; + } else if let Some(i) = u64p(p, "index") { + s.doc.project.move_layer(id, usize::try_from(i).unwrap_or(usize::MAX))?; + } else if p.get("above").is_some() { + let above = u64p(p, "above").map(LayerId); + s.doc.project.move_above(id, above, boolp(p, "inside").unwrap_or(false))?; + } else { + return Err(bad("layer.move", "give `delta`, `index` or `above`")); + } + Ok(()) + })?; + Ok(Value::Null) +} + +fn blend(s: &mut Session, p: &Value) -> Result { + let id = content_param(s, "layer.blend", p)?; + let all = strp(p, "channel") == Some("all"); + let ch = if all { None } else { channel_param("layer.blend", p, "channel")? }; + let mode = blend_param("layer.blend", p, "mode")?; + let opacity = f32p(p, "opacity").map(|o| o.clamp(0.0, 1.0)); + let enabled = boolp(p, "enabled"); + let ts_channels = channels_of(s); + s.edit("Layer blending", |s| { + let l = s.doc.project.layer_mut(id)?; + let targets: Vec = match ch { + Some(c) => vec![c], + None => ts_channels.clone(), + }; + for c in targets { + match enabled { + Some(false) => { + l.channels.remove(&c); + continue; + } + Some(true) => { + let mode = if l.is_group() { BlendMode::Passthrough } else { BlendMode::Normal }; + l.channels.entry(c).or_insert(ChannelBlend { mode, opacity: 1.0 }); + } + None => {} + } + if let Some(b) = l.channels.get_mut(&c) { + if let Some(m) = mode { + b.mode = m; + } + if let Some(o) = opacity { + b.opacity = o; + } + } + } + Ok(()) + })?; + Ok(Value::Null) +} + +/// Which fill `layer.fill.*` edits: a fill layer's (`layer`) or a content fill effect's +/// (`effect`). +#[derive(Clone, Copy)] +enum FillTarget { + Layer(LayerId), + Effect(LayerId), +} + +fn fill_target(s: &Session, p: &Value, cmd: &str) -> Result { + match u64p(p, "effect").map(LayerId) { + Some(e) => match s.doc.project.effect(e) { + Some((_, x)) if matches!(x.kind, EffectKind::Material(_)) => Ok(FillTarget::Effect(e)), + _ => Err(bad(cmd, format!("{} is not a content fill effect", e.0))), + }, + None => Ok(FillTarget::Layer(content_param(s, cmd, p)?)), + } +} + +fn fill_mut(s: &mut Session, target: FillTarget) -> Result<&mut Fill> { + match target { + FillTarget::Layer(id) => match &mut s.doc.project.layer_mut(id)?.kind { + LayerKind::Fill(f) => Ok(f), + _ => Err(EngineError::Other("not a fill layer".into())), + }, + FillTarget::Effect(e) => match s.doc.project.effect_mut(e).map(|x| &mut x.kind) { + Some(EffectKind::Material(f)) => Ok(f), + _ => Err(EngineError::Other("not a content fill effect".into())), + }, + } +} + +fn fill_set(s: &mut Session, p: &Value) -> Result { + let target = fill_target(s, p, "layer.fill.set")?; + let ch = channel_param("layer.fill.set", p, "channel")?.ok_or_else(|| bad("layer.fill.set", "`channel` is required"))?; + let enabled = boolp(p, "enabled"); + 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); + if let Some(v) = f32p(p, "scale") { + pr.scale = v.clamp(0.001, 1000.0); + } + if let Some(v) = u64p(p, "seed") { + pr.seed = v as u32; + } + if let Some(v) = f32p(p, "contrast") { + pr.contrast = v.clamp(0.0, 20.0); + } + if let Some(v) = f32p(p, "balance") { + 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::Uniform(c)) + } else if let Some(name) = strp(p, "image") { + Some(Source::Image { name: name.to_string() }) + } else if let Some(a) = u64p(p, "anchor") { + let reader = match target { + FillTarget::Layer(id) => Some(id), + FillTarget::Effect(e) => s.doc.project.effect(e).map(|(owner, _)| owner), + }; + let reader = reader.ok_or_else(|| bad("layer.fill.set", "the fill is not in a layer"))?; + let anchor_channel = channel_param("layer.fill.set", p, "anchorChannel")?.unwrap_or(ch); + Some(crate::mask_cmds::anchor_source(s, "layer.fill.set", reader, LayerId(a), anchor_channel)?) + } else if enabled.is_some() { + None + } else { + return Err(bad("layer.fill.set", "give `color`, `value`, `procedural`, `image`, `anchor` or `enabled`")); + }; + s.edit("Set fill", |s| { + let f = fill_mut(s, target)?; + match target { + // A fill effect's channel is on while it has a source. + FillTarget::Effect(_) if enabled == Some(false) => { + f.sources.remove(&ch); + } + FillTarget::Effect(_) => { + let src = source.unwrap_or_else(|| f.source(ch)); + f.sources.insert(ch, src); + } + FillTarget::Layer(id) => { + if let Some(src) = source { + f.sources.insert(ch, src); + } + let l = s.doc.project.layer_mut(id)?; + if enabled == Some(false) { + l.channels.remove(&ch); + } else { + l.channels.entry(ch).or_default(); + } + } + } + Ok(()) + })?; + Ok(Value::Null) +} + +pub(crate) fn projection_from(p: &Value, cmd: &str) -> Result { + let scale = f32p(p, "scale").unwrap_or(1.0).clamp(0.001, 1000.0); + Ok(match strp(p, "projection").unwrap_or("uv") { + "uv" => Projection::Uv { scale: [scale, scale], rotation: f32p(p, "rotation").unwrap_or(0.0), offset: [0.0, 0.0] }, + "triplanar" | "tri-planar" => Projection::Triplanar { scale, sharpness: f32p(p, "sharpness").unwrap_or(8.0).clamp(1.0, 64.0), offset: [0.0; 3] }, + "planar" => Projection::Planar { scale, axis: u64p(p, "axis").unwrap_or(2).min(2) as u8, offset: [0.0; 3] }, + "spherical" => Projection::Spherical { scale }, + "cylindrical" => Projection::Cylindrical { scale, axis: u64p(p, "axis").unwrap_or(1).min(2) as u8 }, + other => return Err(bad(cmd, format!("unknown projection `{other}`"))), + }) +} + +fn fill_projection(s: &mut Session, p: &Value) -> Result { + let target = fill_target(s, p, "layer.fill.projection")?; + let proj = projection_from(p, "layer.fill.projection")?; + s.edit("Fill projection", |s| { + fill_mut(s, target)?.projection = proj; + Ok(()) + })?; + Ok(Value::Null) +} + +fn color_tag(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "layer.colorTag", p)?; + let tag = u64p(p, "tag").map(|t| t.min(7) as u8); + s.edit("Layer color tag", |s| { + s.doc.project.layer_mut(id)?.color_tag = tag; + Ok(()) + })?; + Ok(Value::Null) +} + +fn list(s: &mut Session, p: &Value) -> Result { + let ts = u64p(p, "ts").map(|t| t as usize).unwrap_or(s.ts()); + Ok(crate::inspect::stack(s, ts)) +} diff --git a/crates/engine/src/layer_ops.rs b/crates/engine/src/layer_ops.rs new file mode 100644 index 00000000..9891a437 --- /dev/null +++ b/crates/engine/src/layer_ops.rs @@ -0,0 +1,217 @@ +//! Layer clipboard (copy, cut, paste across texture sets), flatten, and resampling of painted +//! pixels when a texture set changes size. + +use serde_json::{Value, json}; +use texturecraft_core::{Channel, ChannelKind, LayerId, LayerKind}; +use texturecraft_texel::TileImage; + +use crate::commands::{CommandSpec, boolp, has_layer, layer_param}; +use crate::session::{Document, StackTarget}; +use crate::{EngineError, Result, Session, cmd}; + +pub fn specs() -> Vec { + vec![ + cmd!("layer.copy", "Copy layer(s)", ["Layers"], Some("Ctrl+C"), "{layer?}", has_layer, copy), + cmd!("layer.cut", "Cut layer(s)", ["Layers"], Some("Ctrl+X"), "{layer?}", has_layer, cut), + cmd!("layer.paste", "Paste layer(s)", ["Layers"], Some("Ctrl+V"), "{} — above the selection, in the active texture set", can_paste, paste), + cmd!("layer.flatten", "Flatten layer", ["Layers"], Some("Ctrl+M"), "{layer?} — a fill or masked paint layer becomes a plain paint layer", has_layer, flatten), + cmd!( + "layer.instantiateAll", + "Instantiate across texture sets", + ["Layers"], + Some("Ctrl+Shift+D"), + "{layer?, copy?: bool} — a linked instance of the layer on top of every other texture set (`copy`: independent copies)", + has_layer, + to_all_sets + ), + ] +} + +fn can_paste(s: &Session) -> std::result::Result<(), String> { + if s.clipboard.is_some() { Ok(()) } else { Err("the layer clipboard is empty".into()) } +} + +/// Encoded "nothing painted" texel for a channel image (value 0, coverage 0). +pub(crate) fn empty_texel(c: Channel) -> Vec { + let zero = if c.kind() == ChannelKind::Signed { texturecraft_texel::to_u16_signed(0.0) } else { 0 }; + let mut v = vec![zero; c.components()]; + v.push(0); + v +} + +fn fit(img: &TileImage, res: u32, empty: &[u16]) -> TileImage { + if img.width() == res { img.clone() } else { img.resampled(res, res, empty) } +} + +/// Copy layer `id` of document `src` (recursively, with pixels) into the session's texture set +/// `ts`, above `above` (or inside it when `inside`). Pixels are resampled to the target set's +/// resolution. Returns the new id. +pub(crate) fn copy_tree(src: &Document, id: LayerId, s: &mut Document, ts: usize, above: Option, inside: bool, depth: usize) -> Result { + if depth > texturecraft_core::project::MAX_DEPTH { + return Err(EngineError::Other("layer groups nested too deeply".into())); + } + let res = s.project.texture_set(ts)?.resolution; + let channels = s.project.texture_set(ts)?.channel_list(); + let layer = src.project.layer(id)?.clone(); + let new = s.project.alloc_id(); + let mut copy = layer.clone(); + copy.id = new; + // Channels the target set doesn't have are dropped; ones it has but the source didn't + // stay disabled. + copy.channels.retain(|c, _| channels.contains(c)); + for e in copy.mask.iter_mut().flat_map(|m| m.effects.iter_mut()).chain(copy.content_effects.iter_mut()) { + let old = e.id; + e.id = s.project.alloc_id(); + if let Some(img) = src.pixels.masks.get(&old) { + s.pixels.masks.insert(e.id, fit(img, res, &[0, 0])); + } + // Content paint effects keep per-channel pixels. + for c in channels.iter().copied() { + if let Some(img) = src.pixels.layer(old, c) { + s.pixels.layers.insert((e.id, c), fit(img, res, &empty_texel(c))); + } + } + } + let children = layer.children().to_vec(); + if let LayerKind::Group { children: c, .. } = &mut copy.kind { + c.clear(); + } + for c in Channel::ALL { + if let Some(img) = src.pixels.layer(id, c) + && channels.contains(&c) + { + s.pixels.layers.insert((new, c), fit(img, res, &empty_texel(c))); + } + } + s.project.insert_layer(ts, copy, above, inside)?; + let mut below = None; + for c in children { + // Children keep their bottom → top order inside the new group. + let placed = match below { + None => copy_tree(src, c, s, ts, Some(new), true, depth + 1)?, + Some(b) => copy_tree(src, c, s, ts, Some(b), false, depth + 1)?, + }; + below = Some(placed); + } + Ok(new) +} + +fn copy(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "layer.copy", p)?; + s.clipboard = Some((s.doc.clone(), id)); + Ok(json!({ "copied": id.0 })) +} + +fn cut(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "layer.cut", p)?; + s.clipboard = Some((s.doc.clone(), id)); + crate::execute(s, "layer.remove", &json!({ "layer": id.0 }))?; + Ok(json!({ "cut": id.0 })) +} + +fn paste(s: &mut Session, _: &Value) -> Result { + let (src, id) = s.clipboard.clone().ok_or_else(|| EngineError::Other("the layer clipboard is empty".into()))?; + let new = s.edit("Paste layer", |s| { + let ts = s.ts(); + let sel = s.selected_layer(); + let inside = sel.and_then(|l| s.doc.project.layer(l).ok()).is_some_and(|l| l.is_group()); + let new = copy_tree(&src, id, &mut s.doc, ts, sel, inside, 0)?; + s.selected.insert(ts, new); + s.target = StackTarget::Content; + Ok(new) + })?; + Ok(json!({ "layer": new.0 })) +} + +fn to_all_sets(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "layer.instantiateAll", p)?; + let (from, _, _) = s.doc.project.locate(id).ok_or_else(|| EngineError::Other("layer not in a stack".into()))?; + let src = s.doc.clone(); + let copy = boolp(p, "copy").unwrap_or(false); + let label = if copy { "Copy layer to all texture sets" } else { "Instantiate across texture sets" }; + let made = s.edit(label, |s| { + let mut made = Vec::new(); + for ts in (0..s.set_count()).filter(|ts| *ts != from) { + let new = if copy { + copy_tree(&src, id, &mut s.doc, ts, None, false, 0)? + } else { + // On top of the set: clear its selection first. + s.selected.remove(&ts); + crate::instance_cmds::make_instance(s, id, ts)? + }; + made.push(new.0); + } + Ok(made) + })?; + Ok(json!({ "layers": made })) +} + +fn flatten(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "layer.flatten", p)?; + let (ts, _, _) = s.doc.project.locate(id).ok_or_else(|| EngineError::Other("layer not in a stack".into()))?; + let layer = s.doc.project.layer(id)?.clone(); + // An instance flattens what it draws into a plain layer of its own. + let content = s.doc.project.content_layer(id).cloned().ok_or_else(|| EngineError::Other("the instance's source is missing".into()))?; + if content.is_group() { + return Err(EngineError::Other("flattening a folder is not supported yet; flatten its layers".into())); + } + s.update(); + let cache = s.caches.get(ts).ok_or_else(|| EngineError::Other("texture set not ready".into()))?; + let res = cache.resolution; + let tiles: Vec<(u32, u32)> = cache.covered_tiles.iter().copied().collect(); + let empty = Default::default(); + let inputs = texturecraft_compose::Inputs { + project: &s.doc.project, + ts, + mesh: &s.mesh, + gbuf: &cache.gbuf, + pixels: &s.doc.pixels, + baked: s.doc.baked.get(&ts).unwrap_or(&empty), + preview: None, + images: &s.images, + bounds: s.mesh.bounds(), + }; + let flat = texturecraft_compose::flatten_layer(&inputs, id, &tiles).ok_or_else(|| EngineError::Other("nothing to flatten".into()))?; + s.edit("Flatten layer", |s| { + for c in Channel::ALL { + s.doc.pixels.layers.remove(&(id, c)); + } + let effects: Vec = layer.mask.iter().flat_map(|m| m.effects.iter()).chain(layer.content_effects.iter()).map(|e| e.id).collect(); + s.doc.pixels.masks.retain(|e, _| !effects.contains(e)); + s.doc.pixels.layers.retain(|(e, _), _| !effects.contains(e)); + for (c, (tx, ty), data) in flat { + let img = s.doc.pixels.layer_mut(id, c, res); + img.set_tile(tx, ty, Some(std::sync::Arc::new(data))); + } + let channels = s.doc.project.texture_set(ts)?.channel_list(); + let l = s.doc.project.layer_mut(id)?; + l.kind = LayerKind::Paint; + l.mask = None; + l.content_effects.clear(); + l.height_mask = Default::default(); + l.channels = content.channels.into_iter().filter(|(c, _)| channels.contains(c)).collect(); + s.target = StackTarget::Content; + Ok(()) + })?; + Ok(json!({ "layer": id.0 })) +} + +/// Resample every painted image of texture set `ts` to `res` (after a size change). +pub(crate) fn resample_set(s: &mut Session, ts: usize, res: u32) { + let ids: Vec = s.doc.project.walk(ts).into_iter().map(|(id, _)| id).collect(); + let effect_ids: Vec = ids + .iter() + .filter_map(|id| s.doc.project.layers.get(id)) + .flat_map(|l| l.mask.iter().flat_map(|m| m.effects.iter()).chain(l.content_effects.iter()).map(|e| e.id)) + .collect(); + for ((id, c), img) in s.doc.pixels.layers.iter_mut() { + if (ids.contains(id) || effect_ids.contains(id)) && img.width() != res { + *img = img.resampled(res, res, &empty_texel(*c)); + } + } + for (id, img) in s.doc.pixels.masks.iter_mut() { + if effect_ids.contains(id) && img.width() != res { + *img = img.resampled(res, res, &[0, 0]); + } + } +} diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs new file mode 100644 index 00000000..959350db --- /dev/null +++ b/crates/engine/src/lib.rs @@ -0,0 +1,64 @@ +//! # texturecraft-engine +//! +//! The TextureCraft session: a [`Session`] owns the document (project + copy-on-write pixels + +//! baked maps), the mesh with its BVH and per-texture-set caches (UV G-buffer, padding map, +//! composite), snapshot-based undo, the tool state and the active stroke. Every user-visible +//! action is a command in the [`commands`] registry, dispatched by id from the UI, the CLI, +//! the control channel and the MCP server alike. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod bake_cmds; +pub mod commands; +pub mod file_cmds; +pub mod inspect; +pub mod instance_cmds; +pub mod layer_cmds; +pub mod layer_ops; +pub mod mask_cmds; +pub mod material_lib; +pub mod materials; +pub mod painting; +pub mod path_tool; +pub mod preview; +pub mod project_cmds; +pub mod quick_mask; +pub mod resource_cmds; +pub mod session; +pub mod set_cmds; +pub mod smart_masks; +pub mod tool_cmds; +pub mod view_cmds; + +pub use bake_cmds::{BakeMesh, BakeMeshes, BakeTask}; +pub use commands::{CommandSpec, command_specs, execute, find}; +pub use path_tool::PathState; +pub use project_cmds::sample_session; +pub use session::{CloneSource, Document, FillMode, Session, SetCache, StackTarget, StencilState, Tool, ToolState, ViewMode}; + +pub use texturecraft_bake as bake; +pub use texturecraft_compose as compose; +pub use texturecraft_core as core; +pub use texturecraft_formats as formats; +pub use texturecraft_geom as geom; +pub use texturecraft_paint as paint; +pub use texturecraft_texel as texel; + +#[derive(Debug, thiserror::Error)] +pub enum EngineError { + #[error("unknown command `{0}`")] + UnknownCommand(String), + #[error("`{cmd}` is not available: {why}")] + Disabled { cmd: String, why: String }, + #[error("`{cmd}`: {msg}")] + BadParams { cmd: String, msg: String }, + #[error(transparent)] + Core(#[from] texturecraft_core::CoreError), + #[error("{0}")] + Other(String), +} + +pub type Result = std::result::Result; + +/// Version string shown in About, `--version` and window titles. +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/crates/engine/src/mask_cmds.rs b/crates/engine/src/mask_cmds.rs new file mode 100644 index 00000000..ea65c0ea --- /dev/null +++ b/crates/engine/src/mask_cmds.rs @@ -0,0 +1,485 @@ +//! Mask and effect commands (the layer context menu's mask section and "Add "). + +use serde_json::{Value, json}; +use texturecraft_core::{ + BlendMode, Channel, CompareOp, Effect, EffectKind, Filter, Generator, GeneratorKind, LayerId, Levels, Mask, MaskBackground, Procedural, ProceduralKind, + Source, +}; + +use crate::commands::{CommandSpec, bad, blend_param, boolp, channel_param, color_param, f32p, has_layer, layer_param, strp, u64p}; +use crate::layer_cmds::projection_from; +use crate::session::StackTarget; +use crate::{EngineError, Result, Session, cmd}; + +pub fn specs() -> Vec { + vec![ + cmd!("mask.add.white", "Add white mask", ["Layers", "Mask"], None, "{layer?}", has_layer, |s, p| add_mask(s, p, MaskBackground::White)), + cmd!("mask.add.black", "Add black mask", ["Layers", "Mask"], None, "{layer?}", has_layer, |s, p| add_mask(s, p, MaskBackground::Black)), + cmd!( + "mask.add.colorSelection", + "Add mask with color selection", + ["Layers", "Mask"], + None, + "{layer?, color?: [r,g,b] ID color, tolerance?}", + has_layer, + add_color_mask + ), + cmd!("mask.toggle", "Toggle mask", ["Layers", "Mask"], None, "{layer?, enabled?}", has_layer, toggle), + cmd!("mask.invertBackground", "Invert mask background", ["Layers", "Mask"], None, "{layer?}", has_layer, invert_background), + cmd!("mask.invert", "Invert mask", ["Layers", "Mask"], None, "{layer?}", has_layer, invert), + cmd!("mask.clear", "Clear mask", ["Layers", "Mask"], None, "{layer?}", has_layer, clear), + cmd!("mask.remove", "Remove mask", ["Layers", "Mask"], None, "{layer?}", has_layer, remove), + cmd!( + "effect.add.paint", + "Add paint", + ["Layers", "Effects"], + None, + "{layer?, target?: mask|content, name?}", + has_layer, + |s, p| push_effect(s, p, EffectKind::Paint, BlendMode::Normal) + ), + cmd!( + "effect.add.fill", + "Add fill", + ["Layers", "Effects"], + None, + "{layer?, target?: mask|content, name?, mask: value?: 0..1 | procedural?: kind, scale?, seed? | anchor?: anchor effect id, anchorChannel?: channel (height); content: color?, roughness?, metallic?, height?, opacity? (default: every layer channel); projection?}", + has_layer, + add_fill + ), + cmd!( + "effect.add.generator", + "Add generator", + ["Layers", "Effects"], + None, + "{layer?, name?, generator?: edgeWear|dirt|thinAreas|curvature|ambientOcclusion|position|direction, level?, softness?, noise?, invert?}", + has_layer, + add_generator + ), + cmd!( + "effect.add.levels", + "Add levels", + ["Layers", "Effects"], + None, + "{layer?, target?: mask|content, name?, inLow?, inHigh?, gamma?, outLow?, outHigh?}", + has_layer, + add_levels + ), + cmd!( + "effect.add.filter", + "Add filter", + ["Layers", "Effects"], + None, + "{layer?, target?: mask|content, name?, filter?: invert|threshold|blur|sharpen|hsl, level?, radius?, amount?, hue?: turns -1..1, saturation?: -1..1, lightness?: -1..1}", + has_layer, + add_filter + ), + cmd!( + "effect.add.compare", + "Add compare mask", + ["Layers", "Effects"], + None, + "{layer?, name?, op?: lesser|within|greater, value?, tolerance?}", + has_layer, + add_compare + ), + cmd!( + "effect.add.anchor", + "Add anchor point", + ["Layers", "Effects"], + None, + "{layer?, name?} (records the stack up to this layer for fills and masks above: effect.add.fill {anchor})", + has_layer, + |s, p| push_effect(s, p, EffectKind::Anchor, BlendMode::Normal) + ), + cmd!( + "effect.set", + "Edit effect", + [], + None, + "{effect, visible?, blend?, opacity?, name?, level?, softness?, noise?, noiseScale?, invert?, value?, anchor?, anchorChannel?, inLow?, inHigh?, gamma?, outLow?, outHigh?, radius?, amount?, hue?, saturation?, lightness?, tolerance?}", + has_layer, + set + ), + cmd!("effect.remove", "Remove effect", [], None, "{effect}", has_layer, remove_effect), + ] +} + +fn with_mask(s: &mut Session, p: &Value, cmd: &str, label: &str, f: impl FnOnce(&mut Session, LayerId) -> Result) -> Result { + let id = layer_param(s, cmd, p)?; + s.edit(label, |s| f(s, id)) +} + +fn add_mask(s: &mut Session, p: &Value, bg: MaskBackground) -> Result { + let label = if bg == MaskBackground::White { "Add white mask" } else { "Add black mask" }; + with_mask(s, p, "mask.add", label, |s, id| { + let l = s.doc.project.layer_mut(id)?; + if l.mask.is_some() { + return Err(EngineError::Other(format!("\"{}\" already has a mask", l.name))); + } + l.mask = Some(Mask::new(bg)); + s.target = StackTarget::Mask; + Ok(()) + })?; + Ok(Value::Null) +} + +fn add_color_mask(s: &mut Session, p: &Value) -> Result { + let color = color_param(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(); + let l = s.doc.project.layer_mut(s.doc.project.mask_owner(id))?; + let m = l.mask.get_or_insert_with(|| Mask::new(MaskBackground::Black)); + m.effects.push(Effect::new(eid, EffectKind::ColorSelection { colors: vec![color], tolerance })); + Ok(()) + })?; + Ok(Value::Null) +} + +/// The mask layer `id` draws with (an instance's source's unless it has its own). +fn mask_of(s: &mut Session, id: LayerId) -> Result<&mut Mask> { + let l = s.doc.project.layer_mut(s.doc.project.mask_owner(id))?; + let name = l.name.clone(); + l.mask.as_mut().ok_or_else(|| EngineError::Other(format!("\"{name}\" has no mask"))) +} + +fn toggle(s: &mut Session, p: &Value) -> Result { + let on = with_mask(s, p, "mask.toggle", "Toggle mask", |s, id| { + let m = mask_of(s, id)?; + m.enabled = boolp(p, "enabled").unwrap_or(!m.enabled); + Ok(m.enabled) + })?; + Ok(json!({ "enabled": on })) +} + +fn invert_background(s: &mut Session, p: &Value) -> Result { + with_mask(s, p, "mask.invertBackground", "Invert mask background", |s, id| { + let m = mask_of(s, id)?; + m.background = if m.background == MaskBackground::White { MaskBackground::Black } else { MaskBackground::White }; + Ok(()) + })?; + Ok(Value::Null) +} + +fn invert(s: &mut Session, p: &Value) -> Result { + with_mask(s, p, "mask.invert", "Invert mask", |s, id| { + let eid = s.doc.project.alloc_id(); + let m = mask_of(s, id)?; + // Inverting twice cancels out. + if m.effects.last().is_some_and(|e| matches!(e.kind, EffectKind::Filter(Filter::Invert))) { + m.effects.pop(); + } else { + m.effects.push(Effect::new(eid, EffectKind::Filter(Filter::Invert))); + } + Ok(()) + })?; + Ok(Value::Null) +} + +fn clear(s: &mut Session, p: &Value) -> Result { + with_mask(s, p, "mask.clear", "Clear mask", |s, id| { + let m = mask_of(s, id)?; + let ids: Vec = m.effects.drain(..).map(|e| e.id).collect(); + for e in ids { + s.doc.pixels.masks.remove(&e); + } + Ok(()) + })?; + Ok(Value::Null) +} + +fn remove(s: &mut Session, p: &Value) -> Result { + with_mask(s, p, "mask.remove", "Remove mask", |s, id| { + let m = s.doc.project.layer_mut(s.doc.project.mask_owner(id))?.mask.take().ok_or_else(|| EngineError::Other("the layer has no mask".into()))?; + for e in m.effects { + s.doc.pixels.masks.remove(&e.id); + } + s.target = StackTarget::Content; + Ok(()) + })?; + Ok(Value::Null) +} + +/// Whether `effect.add.*` targets the layer content (`target: "content"`) or its mask (the +/// default). +fn content_target(p: &Value, cmd: &str) -> Result { + match strp(p, "target") { + None | Some("mask") => Ok(false), + Some("content") => Ok(true), + Some(other) => Err(bad(cmd, format!("unknown target `{other}`: use content or mask"))), + } +} + +/// A valid `name` param. +fn name_param(p: &Value) -> Option<&str> { + strp(p, "name").filter(|n| !n.trim().is_empty() && n.chars().count() <= 256) +} + +/// Add an effect to the selected layer's mask (creating a mask if there is none) or, with +/// `target: "content"` or a content-only effect, to its content stack. +fn push_effect(s: &mut Session, p: &Value, kind: EffectKind, blend: BlendMode) -> Result { + let content = kind.content_only() == Some(true) || content_target(p, "effect.add")?; + if kind.content_only().is_some_and(|c| c != content) { + return Err(bad("effect.add", format!("{} effects go on masks, not on layer content", kind.name()))); + } + 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. + 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 }; + let mut e = Effect::new(eid, kind); + e.blend = blend; + if let Some(n) = name_param(p) { + e.name = n.to_string(); + } + if !content { + l.mask.get_or_insert_with(|| Mask::new(bg)).effects.push(e); + } else if l.is_group() { + return Err(EngineError::Other(format!("\"{}\" is a folder: content effects go on paint and fill layers", l.name))); + } else { + l.content_effects.push(e); + } + s.target = StackTarget::Effect(eid); + Ok(eid) + })?; + Ok(json!({ "effect": eid.0 })) +} + +/// A source reading anchor point `anchor` for layer `reader`: the anchor must sit on a layer +/// below the reader in the same texture set (the compositor walks bottom → top). +pub(crate) fn anchor_source(s: &Session, cmd: &str, reader: LayerId, anchor: LayerId, channel: Channel) -> Result { + let owner = match s.doc.project.effect(anchor) { + Some((owner, e)) if matches!(e.kind, EffectKind::Anchor) => owner, + _ => return Err(bad(cmd, format!("{} is not an anchor point", anchor.0))), + }; + let (ts, _, _) = s.doc.project.locate(reader).ok_or_else(|| bad(cmd, "layer not in a stack"))?; + let order: Vec = s.doc.project.walk(ts).into_iter().map(|(id, _)| id).collect(); + let pos = |id: LayerId| order.iter().position(|x| *x == id); + match (pos(owner), pos(reader)) { + (Some(o), Some(r)) if o < r => Ok(Source::Anchor { anchor, channel }), + _ => Err(bad(cmd, "the anchor point must be on a layer below this one in the same texture set")), + } +} + +fn add_fill(s: &mut Session, p: &Value) -> Result { + let projection = projection_from(p, "effect.add.fill")?; + 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); + if fill.sources.is_empty() { + fill.sources = s.doc.project.layer(id)?.channels.keys().map(|c| (*c, Source::uniform_of(*c))).collect(); + } + fill.projection = projection; + return push_effect(s, p, EffectKind::Material(fill), blend); + } + if let Some(a) = u64p(p, "anchor") { + let channel = channel_param("effect.add.fill", p, "anchorChannel")?.unwrap_or(Channel::Height); + let source = anchor_source(s, "effect.add.fill", id, LayerId(a), channel)?; + return push_effect(s, p, EffectKind::Fill { source, projection }, blend); + } + let source = match strp(p, "procedural") { + Some(k) => { + let kind = ProceduralKind::parse(k).ok_or_else(|| bad("effect.add.fill", format!("unknown procedural `{k}`")))?; + let mut pr = Procedural::new(kind); + if let Some(v) = f32p(p, "scale") { + pr.scale = v.clamp(0.001, 1000.0); + } + if let Some(v) = u64p(p, "seed") { + pr.seed = v as u32; + } + Source::Procedural { pattern: pr, low: [0.0; 3], high: [1.0; 3] } + } + None => Source::Uniform([f32p(p, "value").unwrap_or(1.0).clamp(0.0, 1.0); 3]), + }; + push_effect(s, p, EffectKind::Fill { source, projection }, blend) +} + +fn generator_from(p: &Value, g: &mut Generator) { + if let Some(v) = f32p(p, "level") { + g.level = v.clamp(0.0, 1.0); + } + if let Some(v) = f32p(p, "softness") { + g.softness = v.clamp(0.0, 1.0); + } + if let Some(v) = f32p(p, "noise") { + g.noise_amount = v.clamp(0.0, 2.0); + } + if let Some(v) = f32p(p, "noiseScale") { + g.noise_scale = v.clamp(0.01, 1000.0); + } + if let Some(v) = u64p(p, "seed") { + g.seed = v as u32; + } + if let Some(v) = boolp(p, "invert") { + g.invert = v; + } + if let Some(d) = color_param(p, "direction") { + g.direction = d; + } +} + +fn add_generator(s: &mut Session, p: &Value) -> Result { + let kind = match strp(p, "generator") { + Some(k) => GeneratorKind::parse(k).ok_or_else(|| bad("effect.add.generator", format!("unknown generator `{k}`")))?, + None => GeneratorKind::EdgeWear, + }; + let mut g = Generator::new(kind); + generator_from(p, &mut g); + let blend = blend_param("effect.add.generator", p, "blend")?.unwrap_or(BlendMode::Normal); + push_effect(s, p, EffectKind::Generator(g), blend) +} + +fn levels_from(p: &Value, l: &mut Levels) { + for (k, slot) in [("inLow", &mut l.in_low), ("inHigh", &mut l.in_high), ("gamma", &mut l.gamma), ("outLow", &mut l.out_low), ("outHigh", &mut l.out_high)] { + if let Some(v) = f32p(p, k) { + *slot = if k == "gamma" { v.clamp(0.01, 10.0) } else { v.clamp(0.0, 1.0) }; + } + } +} + +fn add_levels(s: &mut Session, p: &Value) -> Result { + let mut l = Levels::default(); + levels_from(p, &mut l); + push_effect(s, p, EffectKind::Levels(l), BlendMode::Normal) +} + +fn add_filter(s: &mut Session, p: &Value) -> Result { + let f = match strp(p, "filter").unwrap_or("invert") { + "invert" => Filter::Invert, + "threshold" => Filter::Threshold { level: f32p(p, "level").unwrap_or(0.5).clamp(0.0, 1.0) }, + "blur" => Filter::Blur { radius: f32p(p, "radius").unwrap_or(2.0).clamp(0.0, 64.0) }, + "sharpen" => Filter::Sharpen { amount: f32p(p, "amount").unwrap_or(1.0).clamp(0.0, 10.0) }, + "hsl" => { + let mut f = Filter::Hsl { hue: 0.0, saturation: 0.0, lightness: 0.0 }; + hsl_from(p, &mut f); + f + } + other => return Err(bad("effect.add.filter", format!("unknown filter `{other}`"))), + }; + push_effect(s, p, EffectKind::Filter(f), BlendMode::Normal) +} + +fn hsl_from(p: &Value, f: &mut Filter) { + if let Filter::Hsl { hue, saturation, lightness } = f { + for (k, slot) in [("hue", hue), ("saturation", saturation), ("lightness", lightness)] { + if let Some(v) = f32p(p, k) { + *slot = v.clamp(-1.0, 1.0); + } + } + } +} + +fn add_compare(s: &mut Session, p: &Value) -> Result { + let op = match strp(p, "op").unwrap_or("greater") { + "lesser" | "less" => CompareOp::LesserThan, + "within" => CompareOp::WithinTolerance, + "greater" => CompareOp::GreaterThan, + other => return Err(bad("effect.add.compare", format!("unknown op `{other}`"))), + }; + let kind = + EffectKind::Compare { op, value: f32p(p, "value").unwrap_or(0.5).clamp(0.0, 1.0), tolerance: f32p(p, "tolerance").unwrap_or(0.1).clamp(0.0, 1.0) }; + push_effect(s, p, kind, BlendMode::Normal) +} + +fn effect_id(p: &Value, cmd: &str) -> Result { + u64p(p, "effect").map(LayerId).ok_or_else(|| bad(cmd, "`effect` id is required")) +} + +fn set(s: &mut Session, p: &Value) -> Result { + let eid = effect_id(p, "effect.set")?; + let blend = blend_param("effect.set", p, "blend")?; + // A fill reading an anchor: `anchor` picks the anchor point, `anchorChannel` alone + // re-targets the current one. + let anchor = match s.doc.project.effect(eid) { + Some((owner, Effect { kind: EffectKind::Fill { source, .. }, .. })) => { + let (current, channel) = match source { + Source::Anchor { anchor, channel } => (Some(*anchor), *channel), + _ => (None, Channel::Height), + }; + let channel_param = channel_param("effect.set", p, "anchorChannel")?; + match u64p(p, "anchor").map(LayerId).or(current.filter(|_| channel_param.is_some())) { + Some(a) => Some(anchor_source(s, "effect.set", owner, a, channel_param.unwrap_or(channel))?), + None => None, + } + } + _ => None, + }; + s.edit("Edit effect", |s| { + let e = s.doc.project.effect_mut(eid).ok_or_else(|| bad("effect.set", format!("no effect {}", eid.0)))?; + if let Some(v) = boolp(p, "visible") { + e.visible = v; + } + if let Some(b) = blend { + e.blend = b; + } + if let Some(o) = f32p(p, "opacity") { + e.opacity = o.clamp(0.0, 1.0); + } + if let Some(n) = name_param(p) { + e.name = n.to_string(); + } + match &mut e.kind { + EffectKind::Generator(g) => generator_from(p, g), + EffectKind::Levels(l) => levels_from(p, l), + EffectKind::Filter(f @ Filter::Hsl { .. }) => hsl_from(p, f), + EffectKind::Fill { source, .. } => { + if let Some(a) = anchor { + *source = a; + } else if let Some(x) = f32p(p, "value") + && matches!(source, Source::Uniform(_) | Source::Anchor { .. }) + { + *source = Source::Uniform([x.clamp(0.0, 1.0); 3]); + } + } + EffectKind::Filter(Filter::Threshold { level }) => { + if let Some(x) = f32p(p, "level") { + *level = x.clamp(0.0, 1.0); + } + } + EffectKind::Filter(Filter::Blur { radius }) => { + if let Some(x) = f32p(p, "radius") { + *radius = x.clamp(0.0, 256.0); + } + } + EffectKind::Filter(Filter::Sharpen { amount }) => { + if let Some(x) = f32p(p, "amount") { + *amount = x.clamp(0.0, 10.0); + } + } + EffectKind::ColorSelection { tolerance, .. } | EffectKind::Compare { tolerance, .. } => { + if let Some(x) = f32p(p, "tolerance") { + *tolerance = x.clamp(0.0, 1.0); + } + } + _ => {} + } + Ok(()) + })?; + Ok(Value::Null) +} + +fn remove_effect(s: &mut Session, p: &Value) -> Result { + let eid = effect_id(p, "effect.remove")?; + s.edit("Remove effect", |s| { + let content = match s.doc.project.effect(eid) { + Some((owner, _)) => s.doc.project.layer(owner)?.content_effects.iter().any(|e| e.id == eid), + None => return Err(bad("effect.remove", format!("no effect {}", eid.0))), + }; + for l in s.doc.project.layers.values_mut() { + for list in l.mask.iter_mut().map(|m| &mut m.effects).chain(std::iter::once(&mut l.content_effects)) { + list.retain(|e| e.id != eid); + } + } + s.doc.pixels.masks.remove(&eid); + s.doc.pixels.layers.retain(|(id, _), _| *id != eid); + if s.target == StackTarget::Effect(eid) { + s.target = if content { StackTarget::Content } else { StackTarget::Mask }; + } + Ok(()) + })?; + Ok(Value::Null) +} diff --git a/crates/engine/src/material_lib.rs b/crates/engine/src/material_lib.rs new file mode 100644 index 00000000..f3d4b3bd --- /dev/null +++ b/crates/engine/src/material_lib.rs @@ -0,0 +1,142 @@ +//! User smart materials: a layer (or folder) saved with its pixels and images as a `.tcmat` file +//! (the `.tcraft` container holding a one-texture-set project), and added back above the +//! selection in any project. + +use std::collections::BTreeMap; +use std::sync::Arc; + +use serde_json::Value; +use texturecraft_compose::PixelStore; +use texturecraft_core::{EffectKind, LayerId, LayerKind, Project, Source, TextureSet}; + +use crate::commands::{CommandSpec, always, has_layer}; +use crate::session::{Document, StackTarget}; +use crate::{EngineError, Result, Session, cmd}; + +/// Smart material file extension. +pub const SMART_MATERIAL_EXT: &str = "tcmat"; + +pub fn specs() -> Vec { + vec![ + cmd!("material.save", "Create smart material", ["Layers"], None, "{layer?, path: .tcmat file, name?}", has_layer, save), + cmd!("material.load", "Add smart material from file", [], None, "{path: .tcmat file}", always, load), + ] +} + +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, 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 { + out.push(name.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 c in l.children() { + images_used(p, *c, out, depth + 1); + } +} + +impl Session { + /// Serialize layer `id` (with children, pixels and images) as a smart material file. + pub fn smart_material_bytes(&self, id: LayerId, name: &str) -> Result> { + let (ts, _, _) = self.doc.project.locate(id).ok_or_else(|| EngineError::Other("layer not in a stack".into()))?; + let src_set = self.doc.project.texture_set(ts)?; + let mut project = Project::new(name, "builtin:none"); + let mut set = TextureSet::new(name, 0, src_set.resolution); + set.channels = src_set.channels.clone(); + project.texture_sets.push(set); + let mut mini = Document { project, pixels: PixelStore::default(), baked: BTreeMap::new(), resources: BTreeMap::new() }; + let new = crate::layer_ops::copy_tree(&self.doc, id, &mut mini, 0, None, false, 0)?; + if let Ok(l) = mini.project.layer_mut(new) { + l.name = name.to_string(); + } + let mut used = Vec::new(); + images_used(&mini.project, new, &mut used, 0); + let resources = used.iter().filter_map(|n| self.doc.resources.get(n).map(|b| (n.clone(), b.as_ref().clone()))).collect(); + let pf = texturecraft_formats::ProjectFile { + project: mini.project, + pixels: mini.pixels, + baked: BTreeMap::new(), + mesh: None, + high_poly: Vec::new(), + cage: None, + thumbnail: None, + resources, + }; + texturecraft_formats::save_project_bytes(&pf).map_err(fe) + } + + /// Add a smart material file above the selection (one undo step). Returns the new layer. + pub fn add_smart_material_bytes(&mut self, bytes: &[u8]) -> Result { + let pf = texturecraft_formats::load_project_bytes(bytes).map_err(fe)?; + let root = pf.project.texture_sets.first().and_then(|t| t.stack.last().copied()).ok_or_else(|| EngineError::Other("the smart material file has no layer".into()))?; + let resources: BTreeMap>> = pf.resources.into_iter().map(|(k, v)| (k, Arc::new(v))).collect(); + let src = Document { project: pf.project, pixels: pf.pixels, baked: BTreeMap::new(), resources: resources.clone() }; + let id = self.edit("Add smart material", |s| { + for (k, v) in &resources { + s.doc.resources.entry(k.clone()).or_insert_with(|| v.clone()); + } + let ts = s.ts(); + let sel = s.selected_layer(); + let inside = sel.and_then(|l| s.doc.project.layer(l).ok()).is_some_and(|l| l.is_group()); + let id = crate::layer_ops::copy_tree(&src, root, &mut s.doc, ts, sel, inside, 0)?; + s.selected.insert(ts, id); + s.target = StackTarget::Content; + Ok(id) + })?; + self.sync_images(); + Ok(id) + } +} + +#[cfg(not(target_arch = "wasm32"))] +use crate::commands::{bad, layer_param, strp}; +#[cfg(not(target_arch = "wasm32"))] +use serde_json::json; + +#[cfg(not(target_arch = "wasm32"))] +fn save(s: &mut Session, p: &Value) -> Result { + let id = layer_param(s, "material.save", p)?; + let path = strp(p, "path").ok_or_else(|| bad("material.save", "`path` is required"))?; + let name = strp(p, "name").map(str::to_string).unwrap_or_else(|| s.doc.project.layer(id).map(|l| l.name.clone()).unwrap_or_default()); + let bytes = s.smart_material_bytes(id, &name)?; + let path = if path.ends_with(&format!(".{SMART_MATERIAL_EXT}")) { path.to_string() } else { format!("{path}.{SMART_MATERIAL_EXT}") }; + if let Some(dir) = std::path::Path::new(&path).parent().filter(|d| !d.as_os_str().is_empty()) { + std::fs::create_dir_all(dir).map_err(|e| bad("material.save", format!("cannot create {}: {e}", dir.display())))?; + } + let tmp = format!("{path}.tmp"); + std::fs::write(&tmp, bytes).and_then(|_| std::fs::rename(&tmp, &path)).map_err(|e| bad("material.save", format!("cannot write {path}: {e}")))?; + Ok(json!({ "path": path })) +} + +#[cfg(not(target_arch = "wasm32"))] +fn load(s: &mut Session, p: &Value) -> Result { + let path = strp(p, "path").ok_or_else(|| bad("material.load", "`path` is required"))?; + let bytes = std::fs::read(path).map_err(|e| bad("material.load", format!("cannot read {path}: {e}")))?; + let id = s.add_smart_material_bytes(&bytes)?; + Ok(json!({ "layer": id.0 })) +} + +#[cfg(target_arch = "wasm32")] +fn save(_: &mut Session, _: &Value) -> Result { + Err(EngineError::Other("file paths are not available in the browser".into())) +} + +#[cfg(target_arch = "wasm32")] +fn load(_: &mut Session, _: &Value) -> Result { + Err(EngineError::Other("file paths are not available in the browser".into())) +} diff --git a/crates/engine/src/materials.rs b/crates/engine/src/materials.rs new file mode 100644 index 00000000..2a1997b7 --- /dev/null +++ b/crates/engine/src/materials.rs @@ -0,0 +1,579 @@ +//! Built-in materials and smart materials: fill layers (and folders of fill layers with +//! mesh-map-driven masks), all procedural (no bitmap assets), so they are original to +//! TextureCraft and work at any resolution. +//! +//! Values follow physically based conventions: metals are metallic 1 with base colors from +//! published measured reflectance (F0) tables; dielectrics are metallic 0 with linear albedo +//! between about 0.02 (charcoal, rubber) and 0.9 (fresh snow). + +use texturecraft_core::{ + BlendMode, Channel, ChannelBlend, EffectKind, Fill, GeneratorKind, Layer, LayerId, LayerKind, MaskBackground, Procedural, ProceduralKind, Project, + Projection, Source, +}; + +use crate::smart_masks::{build_mask, directional, generator, pattern}; +use crate::{EngineError, Result, Session}; + +/// One material in the shelf: id, label, category, and a representative color/metal/roughness. +pub struct SmartMaterial { + pub id: &'static str, + pub label: &'static str, + /// Shelf group ("Metal", "Wood", …). + pub category: &'static str, + pub base: [f32; 3], + pub metallic: f32, + pub roughness: f32, + /// True for multi-layer "smart" materials (worn, dirty…), false for simple materials. + pub smart: bool, +} + +const fn m(id: &'static str, label: &'static str, category: &'static str, base: [f32; 3], metallic: f32, roughness: f32) -> SmartMaterial { + SmartMaterial { id, label, category, base, metallic, roughness, smart: false } +} + +const fn sm(id: &'static str, label: &'static str, category: &'static str, base: [f32; 3], metallic: f32, roughness: f32) -> SmartMaterial { + SmartMaterial { id, label, category, base, metallic, roughness, smart: true } +} + +const METAL: &str = "Metal"; +const WOOD: &str = "Wood"; +const STONE: &str = "Stone & Masonry"; +const PLASTIC: &str = "Plastic & Rubber"; +const FABRIC: &str = "Fabric & Leather"; +const GLASS: &str = "Ceramic & Glass"; + +pub const MATERIALS: &[SmartMaterial] = &[ + // Metals (base color = measured F0, linear). + m("steel", "Steel", METAL, [0.56, 0.57, 0.58], 1.0, 0.34), + m("steel_stainless", "Steel Stainless", METAL, [0.65, 0.64, 0.62], 1.0, 0.24), + m("steel_dark", "Steel Dark", METAL, [0.48, 0.48, 0.5], 1.0, 0.3), + m("iron", "Iron Raw", METAL, [0.53, 0.53, 0.54], 1.0, 0.5), + m("cast_iron", "Cast Iron", METAL, [0.48, 0.48, 0.48], 1.0, 0.65), + m("copper", "Copper", METAL, [0.955, 0.638, 0.538], 1.0, 0.26), + m("copper_hammered", "Copper Hammered", METAL, [0.92, 0.6, 0.5], 1.0, 0.3), + m("brass", "Brass", METAL, [0.91, 0.778, 0.423], 1.0, 0.27), + m("bronze", "Bronze", METAL, [0.8, 0.56, 0.32], 1.0, 0.36), + m("gold_polished", "Gold Polished", METAL, [1.0, 0.766, 0.336], 1.0, 0.17), + m("silver", "Silver", METAL, [0.972, 0.96, 0.915], 1.0, 0.22), + m("aluminium", "Aluminium Raw", METAL, [0.913, 0.922, 0.924], 1.0, 0.42), + m("aluminium_brushed", "Aluminium Brushed", METAL, [0.913, 0.922, 0.924], 1.0, 0.32), + m("chrome", "Chrome", METAL, [0.55, 0.556, 0.554], 1.0, 0.05), + m("titanium", "Titanium", METAL, [0.542, 0.497, 0.449], 1.0, 0.32), + m("nickel", "Nickel", METAL, [0.66, 0.609, 0.526], 1.0, 0.25), + m("galvanized", "Steel Galvanized", METAL, [0.69, 0.71, 0.72], 1.0, 0.4), + // Woods. + m("wood", "Wood Varnished", WOOD, [0.29, 0.15, 0.06], 0.0, 0.26), + m("wood_oak", "Oak", WOOD, [0.33, 0.2, 0.09], 0.0, 0.62), + m("wood_walnut", "Walnut", WOOD, [0.12, 0.06, 0.03], 0.0, 0.5), + m("wood_pine", "Pine", WOOD, [0.52, 0.33, 0.16], 0.0, 0.65), + m("plywood", "Plywood", WOOD, [0.55, 0.38, 0.21], 0.0, 0.75), + m("wood_painted", "Wood Painted", WOOD, [0.61, 0.59, 0.54], 0.0, 0.48), + // Stone, masonry and earth. + m("concrete", "Concrete", STONE, [0.27, 0.26, 0.25], 0.0, 0.9), + m("brick", "Brick Red", STONE, [0.3, 0.075, 0.04], 0.0, 0.85), + m("tiles_ceramic", "Tiles Ceramic", STONE, [0.8, 0.8, 0.78], 0.0, 0.1), + m("marble", "Marble", STONE, [0.78, 0.78, 0.76], 0.0, 0.14), + m("marble_black", "Marble Black", STONE, [0.03, 0.03, 0.032], 0.0, 0.1), + m("granite", "Granite", STONE, [0.3, 0.27, 0.25], 0.0, 0.3), + m("asphalt", "Asphalt", STONE, [0.06, 0.06, 0.058], 0.0, 0.9), + m("clay", "Clay Earthenware", STONE, [0.45, 0.18, 0.09], 0.0, 0.8), + // Plastics, rubber, composites. + m("plastic_glossy", "Plastic Glossy", PLASTIC, [0.8, 0.12, 0.08], 0.0, 0.16), + m("plastic_matte", "Plastic Matte", PLASTIC, [0.18, 0.18, 0.19], 0.0, 0.6), + m("plastic_textured", "Plastic Textured", PLASTIC, [0.035, 0.035, 0.04], 0.0, 0.55), + m("rubber", "Rubber", PLASTIC, [0.03, 0.03, 0.03], 0.0, 0.88), + m("carbon_fiber", "Carbon Fiber", PLASTIC, [0.035, 0.035, 0.04], 0.0, 0.15), + // Fabric and leather. + m("fabric", "Fabric Cotton", FABRIC, [0.45, 0.47, 0.54], 0.0, 0.95), + m("denim", "Denim", FABRIC, [0.06, 0.09, 0.19], 0.0, 0.92), + m("leather", "Leather", FABRIC, [0.17, 0.08, 0.04], 0.0, 0.52), + m("leather_black", "Leather Black", FABRIC, [0.025, 0.025, 0.028], 0.0, 0.42), + // Ceramic and glass. + m("ceramic", "Ceramic White", GLASS, [0.8, 0.8, 0.78], 0.0, 0.08), + m("glass", "Glass Clear", GLASS, [0.02, 0.022, 0.022], 0.0, 0.03), + m("glass_frosted", "Glass Frosted", GLASS, [0.6, 0.64, 0.64], 0.0, 0.45), + // Smart materials. + sm("steel_painted_worn", "Steel Painted Worn", METAL, [0.42, 0.06, 0.04], 0.2, 0.45), + sm("steel_painted_rust", "Steel Painted Rust Leaks", METAL, [0.62, 0.62, 0.6], 0.1, 0.5), + sm("military_paint", "Military Paint", METAL, [0.07, 0.08, 0.035], 0.1, 0.6), + sm("iron_rusted", "Iron Rusted", METAL, [0.38, 0.14, 0.05], 0.3, 0.8), + sm("aluminium_scratched", "Aluminium Scratched", METAL, [0.913, 0.922, 0.924], 1.0, 0.3), + sm("copper_patina", "Copper Oxidized", METAL, [0.6, 0.45, 0.35], 0.6, 0.5), + sm("bronze_statue", "Bronze Statue", METAL, [0.35, 0.4, 0.3], 0.5, 0.6), + sm("brass_tarnished", "Brass Tarnished", METAL, [0.7, 0.58, 0.3], 0.8, 0.4), + sm("gold_dirty", "Gold Dirty", METAL, [0.9, 0.68, 0.3], 0.9, 0.3), + sm("chrome_smudged", "Chrome Smudged", METAL, [0.55, 0.556, 0.554], 1.0, 0.12), + sm("wood_weathered", "Wood Weathered", WOOD, [0.33, 0.3, 0.27], 0.0, 0.8), + sm("wood_chipped_paint", "Wood Chipped Paint", WOOD, [0.04, 0.22, 0.22], 0.0, 0.55), + sm("concrete_dirty", "Concrete Dirty", STONE, [0.28, 0.26, 0.23], 0.0, 0.92), + sm("brick_mossy", "Brick Mossy", STONE, [0.22, 0.12, 0.05], 0.0, 0.9), + sm("plastic_dusty", "Plastic Dusty", PLASTIC, [0.05, 0.2, 0.45], 0.0, 0.4), + sm("plastic_scuffed", "Plastic Scuffed", PLASTIC, [0.06, 0.06, 0.065], 0.0, 0.5), + sm("leather_worn", "Leather Worn", FABRIC, [0.22, 0.12, 0.07], 0.0, 0.5), +]; + +fn norm(x: &str) -> String { + x.to_ascii_lowercase().replace([' ', '_', '-'], "") +} + +pub fn find(id: &str) -> Option<&'static SmartMaterial> { + MATERIALS.iter().find(|m| norm(m.id) == norm(id) || norm(m.label) == norm(id)) +} + +fn u(v: f32) -> Source { + Source::Uniform([v; 3]) +} + +fn rgb(c: [f32; 3]) -> Source { + Source::Uniform(c) +} + +fn pat(kind: ProceduralKind, scale: f32, seed: u32, contrast: f32, low: [f32; 3], high: [f32; 3]) -> Source { + Source::Procedural { pattern: Procedural { kind, scale, seed, contrast, balance: 0.0, invert: false }, low, high } +} + +/// A scalar pattern between `lo` and `hi`. +fn gray(kind: ProceduralKind, scale: f32, seed: u32, lo: f32, hi: f32) -> Source { + pat(kind, scale, seed, 1.0, [lo; 3], [hi; 3]) +} + +type Sources = Vec<(Channel, Source)>; + +fn surface(base: Source, metallic: f32, rough: Source) -> Sources { + vec![(Channel::BaseColor, base), (Channel::Metallic, u(metallic)), (Channel::Roughness, rough)] +} + +fn metal(base: Source, rough: Source) -> Sources { + surface(base, 1.0, rough) +} + +fn dielectric(base: Source, rough: Source) -> Sources { + surface(base, 0.0, rough) +} + +/// `sources` plus `extra` channels (height, opacity…). +fn plus(mut sources: Sources, extra: impl IntoIterator) -> Sources { + sources.extend(extra); + sources +} + +fn height(s: Source) -> [(Channel, Source); 1] { + [(Channel::Height, s)] +} + +pub(crate) struct Builder<'a> { + pub p: &'a mut Project, + /// Channels of the target texture set; layers only enable these. + pub channels: Vec, +} + +impl Builder<'_> { + fn fill(&mut self, name: &str, sources: Sources) -> Layer { + let id = self.p.alloc_id(); + let enabled: Vec = sources.iter().map(|(c, _)| *c).filter(|c| self.channels.contains(c)).collect(); + let projection = Projection::Triplanar { scale: 1.0, sharpness: 8.0, offset: [0.0; 3] }; + Layer::new(id, name, LayerKind::Fill(Fill { sources: sources.into_iter().collect(), projection }), &enabled) + } + + /// A fill masked by smart mask preset `mask` (inverted: present where the preset is not). + fn preset(&mut self, name: &str, sources: Sources, mask: &str, invert: bool) -> Result { + let mut l = self.fill(name, sources); + l.mask = Some(crate::smart_masks::mask(self.p, mask, invert)?); + Ok(l) + } + + /// A fill with a custom black-background mask. + fn masked(&mut self, name: &str, sources: Sources, effects: Vec<(EffectKind, BlendMode)>) -> Layer { + let mut l = self.fill(name, sources); + l.mask = Some(build_mask(self.p, MaskBackground::Black, effects)); + l + } + + /// The common grime layer: dark dirt settled in cavities. + fn dirt(&mut self, color: [f32; 3]) -> Result { + self.preset("Dirt", dielectric(rgb(color), u(0.9)), "dirt_cavities", false) + } +} + +/// Layers (bottom → top) making up material `id`. +pub(crate) fn build(b: &mut Builder, id: &str) -> Result> { + use BlendMode::{Lighten, Multiply, Normal}; + use Channel::*; + use GeneratorKind as G; + use ProceduralKind as P; + let m = find(id).ok_or_else(|| EngineError::Other(format!("unknown material `{id}`")))?; + let one = |b: &mut Builder, s: Sources| -> Result> { Ok(vec![b.fill(m.label, s)]) }; + // A simple metal: subtle cloudy tone and roughness variation around the table values. + let plain_metal = |b: &mut Builder, rough: Source| { + let lo = m.base.map(|v| v * 0.94); + one(b, metal(pat(P::Clouds, 3.0, 5, 1.0, lo, m.base), rough)) + }; + let r = m.roughness; + let clouds_rough = |seed: u32| gray(P::Clouds, 6.0, seed, r * 0.85, (r * 1.2).min(1.0)); + match m.id { + // ---- Metals ------------------------------------------------------------------------- + "steel" | "steel_dark" | "iron" | "copper" | "brass" | "bronze" | "gold_polished" | "silver" | "aluminium" | "chrome" | "nickel" => { + plain_metal(b, clouds_rough(7)) + } + "steel_stainless" | "aluminium_brushed" | "titanium" => plain_metal(b, gray(P::Brushed, 3.0, 3, r * 0.75, r * 1.25)), + "cast_iron" => one( + b, + plus(metal(pat(P::Noise, 1.0, 4, 1.0, [0.44; 3], [0.52; 3]), gray(P::Grunge, 3.0, 9, 0.55, 0.75)), height(gray(P::Pores, 14.0, 3, -0.02, 0.0))), + ), + "copper_hammered" => one( + b, + plus( + metal(pat(P::Hammered, 6.0, 1, 1.0, [0.86, 0.55, 0.45], [0.955, 0.638, 0.538]), gray(P::Hammered, 6.0, 1, 0.38, 0.24)), + height(gray(P::Hammered, 6.0, 1, -0.04, 0.0)), + ), + ), + "galvanized" => one(b, metal(pat(P::Voronoi, 10.0, 4, 1.0, [0.6, 0.62, 0.63], [0.78, 0.8, 0.81]), gray(P::Voronoi, 10.0, 4, 0.5, 0.28))), + // ---- Woods (grain along X, rings across Y) --------------------------------------------- + "wood" => one( + b, + plus(dielectric(pat(P::Wood, 2.0, 7, 1.0, [0.12, 0.05, 0.018], [0.42, 0.22, 0.09]), clouds_rough(3)), height(gray(P::Wood, 2.0, 7, -0.004, 0.0))), + ), + "wood_oak" => one( + b, + plus( + dielectric(pat(P::Wood, 2.0, 7, 1.0, [0.2, 0.11, 0.045], [0.46, 0.29, 0.14]), gray(P::Wood, 2.0, 7, 0.74, 0.56)), + height(gray(P::Wood, 2.0, 7, -0.012, 0.0)), + ), + ), + "wood_walnut" => one( + b, + plus( + dielectric(pat(P::Wood, 2.5, 21, 1.0, [0.05, 0.025, 0.012], [0.2, 0.1, 0.05]), gray(P::Wood, 2.5, 21, 0.56, 0.46)), + height(gray(P::Wood, 2.5, 21, -0.008, 0.0)), + ), + ), + "wood_pine" => one( + b, + plus( + dielectric(pat(P::Wood, 1.5, 4, 1.0, [0.33, 0.17, 0.065], [0.7, 0.48, 0.25]), gray(P::Wood, 1.5, 4, 0.72, 0.62)), + height(gray(P::Wood, 1.5, 4, -0.012, 0.0)), + ), + ), + "plywood" => one(b, dielectric(pat(P::Wood, 1.2, 15, 0.6, [0.42, 0.27, 0.13], [0.66, 0.47, 0.27]), gray(P::Wood, 1.2, 15, 0.8, 0.7))), + "wood_painted" => one( + b, + plus( + dielectric(pat(P::Clouds, 3.0, 4, 1.0, [0.57, 0.55, 0.5], [0.64, 0.62, 0.57]), gray(P::Wood, 2.0, 7, 0.56, 0.42)), + height(gray(P::Wood, 2.0, 7, -0.015, 0.0)), + ), + ), + // ---- Stone, masonry, earth ---------------------------------------------------------- + "concrete" => one( + b, + plus( + dielectric(pat(P::Clouds, 6.0, 4, 1.3, [0.2, 0.19, 0.18], [0.35, 0.34, 0.32]), gray(P::Noise, 1.0, 6, 0.85, 0.95)), + height(gray(P::Pores, 14.0, 6, -0.03, 0.0)), + ), + ), + "brick" => one( + b, + plus( + dielectric(pat(P::Bricks, 3.0, 4, 1.0, [0.27, 0.26, 0.24], [0.3, 0.075, 0.04]), gray(P::Bricks, 3.0, 4, 0.95, 0.82)), + height(gray(P::Bricks, 3.0, 4, -0.04, 0.0)), + ), + ), + "tiles_ceramic" => one( + b, + plus( + dielectric(pat(P::Tiles, 4.0, 2, 1.0, [0.33, 0.32, 0.3], [0.82, 0.82, 0.8]), gray(P::Tiles, 4.0, 2, 0.85, 0.08)), + height(gray(P::Tiles, 4.0, 2, -0.03, 0.0)), + ), + ), + "marble" => one(b, dielectric(pat(P::Marble, 2.0, 12, 1.0, [0.38, 0.38, 0.37], [0.86, 0.86, 0.84]), clouds_rough(12))), + "marble_black" => one(b, dielectric(pat(P::Marble, 2.0, 5, 1.0, [0.55, 0.55, 0.53], [0.015, 0.015, 0.017]), clouds_rough(5))), + "granite" => one(b, dielectric(pat(P::Voronoi, 70.0, 8, 1.0, [0.1, 0.095, 0.09], [0.5, 0.47, 0.45]), gray(P::Voronoi, 70.0, 8, 0.25, 0.35))), + "asphalt" => one( + b, + plus( + dielectric(pat(P::Voronoi, 90.0, 3, 1.0, [0.025; 3], [0.11, 0.105, 0.1]), gray(P::Voronoi, 90.0, 3, 0.95, 0.75)), + height(gray(P::Pores, 10.0, 5, -0.02, 0.0)), + ), + ), + "clay" => one( + b, + plus(dielectric(pat(P::Clouds, 5.0, 2, 1.0, [0.4, 0.15, 0.07], [0.5, 0.21, 0.11]), clouds_rough(2)), height(gray(P::Noise, 0.8, 3, -0.005, 0.005))), + ), + // ---- Plastics, rubber, composites --------------------------------------------------- + "plastic_glossy" | "plastic_matte" | "ceramic" => one(b, dielectric(rgb(m.base), clouds_rough(9))), + "plastic_textured" => one(b, plus(dielectric(rgb(m.base), gray(P::Leather, 40.0, 5, 0.66, 0.5)), height(gray(P::Leather, 40.0, 5, -0.01, 0.0)))), + "rubber" => one(b, dielectric(pat(P::Noise, 0.5, 3, 1.0, [0.025; 3], [0.035; 3]), gray(P::Noise, 0.5, 3, 0.84, 0.94))), + "carbon_fiber" => { + one(b, plus(dielectric(pat(P::Weave, 24.0, 1, 1.0, [0.01, 0.01, 0.012], [0.06, 0.06, 0.065]), u(r)), height(gray(P::Weave, 24.0, 1, 0.0, 0.006)))) + } + // ---- Fabric and leather ------------------------------------------------------------- + "fabric" => { + one(b, plus(dielectric(pat(P::Weave, 18.0, 2, 1.0, [0.27, 0.29, 0.34], [0.6, 0.62, 0.7]), u(r)), height(gray(P::Weave, 18.0, 2, -0.01, 0.0)))) + } + "denim" => { + one(b, plus(dielectric(pat(P::Weave, 22.0, 5, 1.0, [0.02, 0.03, 0.08], [0.12, 0.16, 0.3]), u(r)), height(gray(P::Weave, 22.0, 5, -0.01, 0.0)))) + } + "leather" => one( + b, + plus( + dielectric(pat(P::Leather, 42.0, 2, 1.0, [0.07, 0.03, 0.013], [0.25, 0.12, 0.06]), gray(P::Leather, 42.0, 2, 0.62, 0.46)), + height(gray(P::Leather, 42.0, 2, -0.02, 0.0)), + ), + ), + "leather_black" => one( + b, + plus( + dielectric(pat(P::Leather, 42.0, 6, 1.0, [0.01; 3], [0.04, 0.04, 0.045]), gray(P::Leather, 42.0, 6, 0.52, 0.36)), + height(gray(P::Leather, 42.0, 6, -0.02, 0.0)), + ), + ), + // ---- Glass (opaque approximation; uses Opacity when the texture set has it) ------------ + "glass" => one(b, plus(dielectric(rgb(m.base), u(r)), [(Opacity, u(0.2))])), + "glass_frosted" => one(b, plus(dielectric(rgb(m.base), gray(P::Noise, 1.0, 2, 0.4, 0.5)), [(Opacity, u(0.6))])), + // ---- Smart materials ---------------------------------------------------------------- + "steel_painted_worn" => { + let mut v = build(b, "steel")?; + v.push(b.preset("Paint", plus(dielectric(rgb(m.base), gray(P::Clouds, 3.0, 9, 0.38, 0.55)), height(u(0.004))), "edge_chipped", true)?); + v.push(b.dirt([0.12, 0.09, 0.06])?); + Ok(v) + } + "steel_painted_rust" => { + let mut v = build(b, "steel")?; + v.push(b.preset( + "Paint", + plus(dielectric(pat(P::Clouds, 4.0, 3, 1.0, [0.58, 0.58, 0.56], [0.66, 0.66, 0.64]), u(0.45)), height(u(0.004))), + "edge_wear", + true, + )?); + v.push(b.preset("Rust leaks", dielectric(pat(P::Grunge, 4.0, 2, 1.0, [0.1, 0.03, 0.01], [0.24, 0.08, 0.025]), u(0.85)), "rust_leaks", false)?); + Ok(v) + } + "military_paint" => { + let mut v = build(b, "steel_dark")?; + v.push(b.preset( + "Olive drab", + plus(dielectric(pat(P::Clouds, 3.0, 6, 1.0, [0.06, 0.07, 0.03], [0.085, 0.095, 0.042]), u(0.62)), height(u(0.004))), + "edge_chipped", + true, + )?); + v.push(b.dirt([0.06, 0.05, 0.035])?); + v.push(b.preset("Dust", dielectric(rgb([0.36, 0.31, 0.23]), u(0.95)), "dust_top", false)?); + Ok(v) + } + "iron_rusted" => { + let mut v = build(b, "cast_iron")?; + let rust = + plus(dielectric(pat(P::Grunge, 5.0, 3, 0.8, [0.045, 0.018, 0.008], [0.14, 0.05, 0.018]), u(0.88)), height(gray(P::Noise, 1.0, 1, 0.0, 0.04))); + v.push(b.masked("Rust", rust, vec![(pattern(P::Grunge, 2.0, 8, 1.2), Normal), (generator(G::Dirt, 0.5, 0.25, 0.3), Lighten)])); + v.push(b.preset("Rust leaks", dielectric(rgb([0.11, 0.04, 0.014]), u(0.8)), "rust_leaks", false)?); + Ok(v) + } + "aluminium_scratched" => { + let mut v = build(b, "aluminium_brushed")?; + v.push(b.preset("Polished edges", metal(rgb([0.95, 0.95, 0.95]), u(0.14)), "edge_wear", false)?); + v.push(b.preset("Scratches", plus(metal(rgb([0.95, 0.95, 0.95]), u(0.12)), height(u(-0.006))), "scratches", false)?); + Ok(v) + } + "copper_patina" => { + let mut v = build(b, "copper")?; + v.push(b.preset("Tarnish", dielectric(pat(P::Clouds, 4.0, 2, 1.0, [0.18, 0.07, 0.04], [0.3, 0.13, 0.07]), u(0.6)), "grunge", false)?); + let patina = plus(dielectric(pat(P::Clouds, 4.0, 11, 1.4, [0.15, 0.38, 0.32], [0.33, 0.6, 0.5]), u(0.85)), height(u(0.02))); + v.push(b.masked("Patina", patina, vec![(generator(G::Dirt, 0.65, 0.25, 0.6), Normal), (pattern(P::Streaks, 3.0, 9, 1.0), Lighten)])); + Ok(v) + } + "bronze_statue" => { + let mut v = build(b, "bronze")?; + v.push(b.preset("Dark oxide", dielectric(pat(P::Clouds, 4.0, 5, 1.0, [0.05, 0.035, 0.02], [0.12, 0.08, 0.04]), u(0.6)), "grunge", false)?); + v.push(b.preset( + "Verdigris", + plus(dielectric(pat(P::Clouds, 5.0, 8, 1.3, [0.1, 0.3, 0.24], [0.25, 0.5, 0.42]), u(0.88)), height(u(0.015))), + "rust_leaks", + false, + )?); + Ok(v) + } + "brass_tarnished" => { + let mut v = build(b, "brass")?; + v.push(b.masked( + "Tarnish", + dielectric(pat(P::Clouds, 4.0, 4, 1.0, [0.08, 0.06, 0.025], [0.16, 0.12, 0.05]), u(0.7)), + vec![(generator(G::Dirt, 0.6, 0.25, 0.5), Normal), (pattern(P::Grunge, 2.5, 3, 0.9), Lighten)], + )); + v.push(b.preset("Fingerprints", metal(rgb([0.8, 0.68, 0.37]), u(0.45)), "fingerprints", false)?); + Ok(v) + } + "gold_dirty" => { + let mut v = build(b, "gold_polished")?; + v.push(b.preset("Smudges", metal(rgb([0.85, 0.64, 0.28]), u(0.4)), "fingerprints", false)?); + v.push(b.dirt([0.06, 0.045, 0.03])?); + Ok(v) + } + "chrome_smudged" => { + let mut v = build(b, "chrome")?; + v.push(b.preset("Smudges", metal(rgb([0.5, 0.505, 0.5]), u(0.3)), "fingerprints", false)?); + v.push(b.preset("Dust", dielectric(rgb([0.5, 0.48, 0.45]), u(0.9)), "dust_top", false)?); + Ok(v) + } + "wood_weathered" => { + let mut v = build(b, "wood_pine")?; + let gray_wood = + plus(dielectric(pat(P::Wood, 1.5, 4, 1.0, [0.17, 0.16, 0.14], [0.42, 0.4, 0.37]), u(0.85)), height(gray(P::Wood, 1.5, 4, -0.03, 0.0))); + v.push(b.masked( + "Weathered", + gray_wood, + vec![(pattern(P::Grunge, 2.0, 15, 0.8), Normal), (directional(G::Direction, [0.0, 1.0, 0.0], 0.45, 0.2, 0.4, false), Lighten)], + )); + v.push(b.dirt([0.05, 0.04, 0.03])?); + Ok(v) + } + "wood_chipped_paint" => { + let mut v = build(b, "wood_oak")?; + v.push(b.preset( + "Paint", + plus(dielectric(pat(P::Clouds, 3.0, 2, 1.0, [0.035, 0.19, 0.19], [0.05, 0.25, 0.25]), u(0.55)), height(u(0.01))), + "paint_chips", + true, + )?); + v.push(b.dirt([0.05, 0.04, 0.03])?); + Ok(v) + } + "concrete_dirty" => { + let mut v = build(b, "concrete")?; + v.push(b.preset("Water stains", dielectric(rgb([0.13, 0.13, 0.12]), u(0.9)), "rust_leaks", false)?); + v.push(b.dirt([0.08, 0.065, 0.05])?); + v.push(b.preset("Ground mud", dielectric(rgb([0.12, 0.09, 0.06]), u(0.95)), "ground_dirt", false)?); + Ok(v) + } + "brick_mossy" => { + let mut v = build(b, "brick")?; + v.push(b.preset("Grime", dielectric(rgb([0.07, 0.06, 0.05]), u(0.95)), "ground_dirt", false)?); + let moss = plus(dielectric(pat(P::Noise, 1.0, 7, 1.0, [0.02, 0.035, 0.008], [0.05, 0.075, 0.015]), u(0.95)), height(u(0.02))); + v.push(b.masked( + "Moss", + moss, + vec![ + (directional(G::Direction, [0.0, 1.0, 0.0], 0.45, 0.15, 0.3, false), Normal), + (pattern(P::Grunge, 2.0, 5, 1.0), Multiply), + (generator(G::Dirt, 0.4, 0.2, 0.5), Lighten), + ], + )); + Ok(v) + } + "plastic_dusty" => { + let mut v = one(b, dielectric(rgb(m.base), u(0.35)))?; + v.push(b.preset("Dust", dielectric(rgb([0.5, 0.46, 0.4]), u(0.95)), "dust_top", false)?); + v.push(b.dirt([0.1, 0.09, 0.08])?); + Ok(v) + } + "plastic_scuffed" => { + let mut v = one(b, dielectric(rgb(m.base), gray(P::Leather, 40.0, 5, 0.45, 0.35)))?; + v.push(b.preset("Scuffed edges", dielectric(rgb([0.2, 0.2, 0.21]), u(0.7)), "edge_wear", false)?); + v.push(b.preset("Scratches", dielectric(rgb([0.14, 0.14, 0.15]), u(0.6)), "scratches", false)?); + Ok(v) + } + "leather_worn" => { + let mut v = build(b, "leather")?; + v.push(b.preset("Worn", dielectric(rgb([0.3, 0.18, 0.1]), u(0.36)), "edge_wear", false)?); + v.push(b.dirt([0.035, 0.022, 0.014])?); + Ok(v) + } + other => Err(EngineError::Other(format!("material `{other}` has no definition"))), + } +} + +/// Add material `id` above the selection: a single fill layer, or a folder for multi-layer +/// materials. Returns the new top-level layer id. +pub fn apply(s: &mut Session, id: &str) -> Result { + let m = find(id).ok_or_else(|| EngineError::Other(format!("unknown material `{id}`")))?; + let label = format!("Add {}", m.label); + s.edit(&label, |s| { + let channels = s.doc.project.texture_set(s.ts())?.channel_list(); + let layers = build(&mut Builder { p: &mut s.doc.project, channels: channels.clone() }, m.id)?; + if layers.len() == 1 { + let l = layers.into_iter().next().ok_or_else(|| EngineError::Other("empty material".into()))?; + return crate::layer_cmds::insert_selected(s, l); + } + let gid = s.doc.project.alloc_id(); + let mut group = Layer::new(gid, m.label, LayerKind::Group { children: Vec::new(), collapsed: false }, &channels); + for c in group.channels.values_mut() { + *c = ChannelBlend { mode: BlendMode::Passthrough, opacity: 1.0 }; + } + let gid = crate::layer_cmds::insert_selected(s, group)?; + let ts = s.ts(); + for l in layers { + s.doc.project.insert_layer(ts, l, Some(gid), true)?; + } + s.selected.insert(ts, gid); + Ok(gid) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ids_and_labels_are_unique_and_values_physical() { + let (mut ids, mut labels) = (std::collections::BTreeSet::new(), std::collections::BTreeSet::new()); + for m in MATERIALS { + assert!(ids.insert(norm(m.id)), "duplicate id {}", m.id); + assert!(labels.insert(norm(m.label)), "duplicate label {}", m.label); + assert_eq!(find(m.label).map(|f| f.id), Some(m.id)); + if !m.smart { + // Metals: measured F0 is at least ~0.45 in some channel; dielectrics: 0.01..0.9. + let hi = m.base.iter().copied().fold(0.0, f32::max); + if m.metallic == 1.0 { + assert!(hi >= 0.45, "{} too dark for a metal", m.id); + } else { + assert_eq!(m.metallic, 0.0, "{} must be metal or dielectric", m.id); + assert!(m.base.iter().all(|v| (0.01..=0.9).contains(v)), "{} albedo out of range", m.id); + } + } + } + assert!(MATERIALS.iter().filter(|m| !m.smart).count() >= 40); + assert!(MATERIALS.iter().filter(|m| m.smart).count() >= 15); + assert!(find("Gold Polished").is_some()); + assert!(find("nope").is_none()); + } + + /// Base color, roughness and metallic at a grid of texels of texture set `ts`. + fn snapshot(s: &Session, ts: usize) -> Vec<[f32; 3]> { + let c = &s.caches[ts].composite; + let n = c.resolution; + (0..n) + .step_by(3) + .flat_map(|y| (0..n).step_by(3).map(move |x| (x, y))) + .flat_map(|(x, y)| [Channel::BaseColor, Channel::Roughness, Channel::Metallic].map(|ch| c.value(ch, x, y))) + .collect() + } + + #[test] + fn every_material_and_smart_mask_applies_on_the_sample() { + let mut s = crate::sample_session(256, 256).unwrap(); + // A baseline no material matches, so every material must visibly change the composite. + crate::execute(&mut s, "layer.add.fill", &serde_json::json!({"color": [1.0, 0.0, 1.0], "roughness": 0.123})).unwrap(); + s.update(); + let before = snapshot(&s, 0); + for m in MATERIALS { + apply(&mut s, m.id).unwrap_or_else(|e| panic!("{}: {e}", m.id)); + s.doc.project.validate().unwrap(); + s.update(); + let after = snapshot(&s, 0); + assert!(after.iter().flatten().all(|v| v.is_finite() && (-1.0..=1.0).contains(v)), "{} produced invalid values", m.id); + assert_ne!(after, before, "{} changed nothing", m.id); + s.undo().unwrap(); + } + assert!(apply(&mut s, "nope").is_err()); + // Smart masks on the pedestal: it has edges and cavities (the ball is a plain sphere). + s.active_set = 1; + apply(&mut s, "gold_polished").unwrap(); + let layer = s.selected_layer().unwrap(); + for sm in crate::smart_masks::SMART_MASKS { + crate::smart_masks::apply(&mut s, layer, sm.id).unwrap(); + s.update(); + let view = s.mask_view_rgba(layer, 64).unwrap(); + assert!(view.chunks(4).any(|px| px[0] > 0), "{} is empty", sm.id); + assert!(snapshot(&s, 1).iter().flatten().all(|v| v.is_finite()), "{}", sm.id); + } + assert!(crate::smart_masks::apply(&mut s, layer, "nope").is_err()); + } +} diff --git a/crates/engine/src/painting.rs b/crates/engine/src/painting.rs new file mode 100644 index 00000000..75e3b63e --- /dev/null +++ b/crates/engine/src/painting.rs @@ -0,0 +1,511 @@ +//! 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. + +use std::collections::{BTreeMap, BTreeSet}; + +use texturecraft_compose::PaintTarget; +use texturecraft_core::{Channel, Effect, EffectKind, LayerId, LayerKind, Mask, MaskBackground}; +use texturecraft_geom::glam::Vec2; +use texturecraft_paint::{ + CommitSource, EyeDepth, PickCtx, Pickup, PickupMode, RasterCtx, Shape, Stroke, StrokeCanvas, SurfaceHit, commit_layer, commit_mask, fill_triangles, pick, + rasterize, +}; + +use crate::session::{ActiveStroke, FillMode, Session, StackTarget, Tool}; +use crate::{EngineError, Result}; + +impl Session { + pub fn pick(&self, screen: Vec2, viewport: Vec2) -> Option { + let ctx = PickCtx { mesh: &self.mesh, bvh: &self.bvh, camera: &self.camera, viewport }; + pick(&ctx, screen) + } + + /// Resolve what a stroke on the selected layer paints into, creating the mask paint effect + /// when painting a mask. Must run inside the stroke's history snapshot. + fn paint_target(&mut self) -> Result { + let id = self.selected_layer().ok_or_else(|| EngineError::Other("select a layer to paint on".into()))?; + // Painting an instance paints its source (its own mask when it overrides the mask). + let p = &self.doc.project; + let id = if self.target == StackTarget::Mask { p.mask_owner(id) } else { p.content_layer(id).map_or(id, |l| l.id) }; + match self.target { + StackTarget::Content => { + let l = self.doc.project.layer(id)?; + if matches!(l.kind, LayerKind::Paint) { + Ok(PaintTarget::Layer(id)) + } else { + Err(EngineError::Other(format!("\"{}\" is a {} layer: paint on a paint layer or on its mask", l.name, l.kind.name()))) + } + } + StackTarget::Effect(e) => match self.doc.project.effect(e) { + // Content paint effects hold channel pixels like a paint layer. + Some((owner, eff)) if matches!(eff.kind, EffectKind::Paint) => { + let content = self.doc.project.layer(owner)?.content_effects.iter().any(|x| x.id == e); + Ok(if content { PaintTarget::Layer(e) } else { PaintTarget::Mask(e) }) + } + _ => Err(EngineError::Other("the selected effect is not a paint effect".into())), + }, + StackTarget::Mask => { + let eid = self.doc.project.alloc_id(); + let layer = self.doc.project.layer_mut(id)?; + let mask = layer.mask.get_or_insert_with(|| Mask::new(MaskBackground::Black)); + if let Some(top) = mask.effects.last().filter(|e| matches!(e.kind, EffectKind::Paint)) { + return Ok(PaintTarget::Mask(top.id)); + } + mask.effects.push(Effect::new(eid, EffectKind::Paint)); + Ok(PaintTarget::Mask(eid)) + } + } + } + + fn stroke_values(&self, target: PaintTarget) -> BTreeMap { + let ts = self.ts(); + let Ok(set) = self.doc.project.texture_set(ts) else { return BTreeMap::new() }; + let layer_channels = match target { + // A content paint effect paints its owner layer's channels. + PaintTarget::Layer(id) => { + let owner = self.doc.project.effect(id).map(|(owner, _)| owner).unwrap_or(id); + self.doc.project.layer(owner).map(|l| l.channels.keys().copied().collect::>()).unwrap_or_default() + } + PaintTarget::Mask(_) => BTreeSet::new(), + }; + self.tool + .values + .iter() + .filter(|(c, _)| self.tool.enabled.contains(c) && set.has_channel(**c) && layer_channels.contains(c)) + .map(|(c, v)| (*c, *v)) + .collect() + } + + /// Common setup of 3D and 2D strokes. `first_uv` is where the stroke starts on the mesh + /// (the Clone tool's destination anchor). + fn start_stroke(&mut self, viewport: Vec2, uv_mode: bool, first_uv: Option) -> Result<()> { + let tool = self.tool.tool; + let erase = tool == Tool::Eraser; + let quick_mask = self.quick_mask.editing; + let ts = self.ts(); + let res = self.caches.get(ts).map(|c| c.resolution).unwrap_or(1024); + let pickup = if quick_mask { None } else { self.pickup_mode(first_uv)? }; + if pickup.is_some() { + // Clone and smudge read the composite: bring it up to date first. + self.update(); + } + let before = self.doc.clone(); + let target = if quick_mask { + None + } else { + match self.paint_target() { + Ok(t) => Some(t), + Err(e) => { + self.doc = before; + return Err(e); + } + } + }; + let values = target.map(|t| self.stroke_values(t)).unwrap_or_default(); + let stencil = if tool == Tool::Projection && !quick_mask { self.capture_stencil(viewport) } else { None }; + let problem = if !erase && values.is_empty() && matches!(target, Some(PaintTarget::Layer(_))) { + Some("no channel enabled to paint: enable channels in the tool's material") + } else if pickup.is_some() && matches!(target, Some(PaintTarget::Mask(_))) { + Some("the Clone and Smudge tools paint layer content: select the layer instead of its mask") + } else if tool == Tool::Projection && !quick_mask && stencil.is_none() && !uv_mode { + Some("the Projection tool needs a stencil: pick an image in the tool properties (tool.stencil)") + } else { + None + }; + if let Some(msg) = problem { + self.doc = before; + return Err(EngineError::Other(msg.into())); + } + let depth = (!uv_mode).then(|| EyeDepth::render(&self.mesh, &self.camera, (viewport.x / 2.0).max(64.0) as u32, (viewport.y / 2.0).max(64.0) as u32)); + let mut brush = self.tool.brush.clone(); + if tool == Tool::Smudge { + // shortcut: smudge drags one stamp trail; mirrored trails would need one pickup each. + brush.symmetry = Default::default(); + } + let stroke = Stroke::new(&brush, &self.mesh); + let mut canvas = StrokeCanvas::new(res); + if !quick_mask { + canvas.limit = self.stroke_limit(ts, res); + } + if let Some((mode, offset)) = pickup + && let Some(cache) = self.caches.get(ts) + { + let r = res as f32; + let mode = match mode { + PickupMode::Clone { .. } => PickupMode::Clone { offset: [(offset[0] * r).round() as i64, (offset[1] * r).round() as i64] }, + m => m, + }; + canvas.pickup = Some(Pickup::new(mode, cache.composite.clone(), cache.gbuf.clone(), values.keys().copied())); + if tool == Tool::Clone + && let Some(src) = self.tool.clone_source.as_mut() + { + src.offset.get_or_insert(offset); + } + } + let label = if quick_mask { + "Quick mask stroke" + } else if erase { + "Eraser stroke" + } else if matches!(target, Some(PaintTarget::Mask(_))) { + "Mask stroke" + } else { + "Paint stroke" + }; + self.stroke = Some(ActiveStroke { + ts, + target, + stroke, + canvas, + depth, + uv: uv_mode.then_some((None, 0.0)), + camera: self.camera, + viewport, + values, + erase, + stencil, + before, + label: label.to_string(), + }); + Ok(()) + } + + /// Clone / smudge pickup for the current tool, with the clone offset (source minus + /// destination) in texture coordinates. + fn pickup_mode(&self, first_uv: Option) -> Result> { + match self.tool.tool { + Tool::Smudge => Ok(Some((PickupMode::Smudge { strength: self.tool.smudge_strength }, [0.0; 2]))), + Tool::Clone => { + let src = + self.tool.clone_source.ok_or_else(|| EngineError::Other("set a clone source first: V + click on the mesh (tool.cloneSource)".into()))?; + let offset = match (src.offset, first_uv) { + (Some(o), _) => o, + (None, Some(p)) => [src.uv[0] - p.x, src.uv[1] - p.y], + (None, None) => return Err(EngineError::Other("start the clone stroke on the mesh".into())), + }; + Ok(Some((PickupMode::Clone { offset: [0; 2] }, offset))) + } + _ => Ok(None), + } + } + + /// The brush shape: the alpha image when one is set and loaded, else the procedural alpha. + fn shape<'a>(images: &'a texturecraft_compose::ImageStore, b: &texturecraft_paint::BrushSettings) -> Shape<'a> { + Shape { alpha: b.alpha, image: b.alpha_image.as_ref().and_then(|n| images.get(n)).map(|i| &**i) } + } + + /// The stencil placed for the current camera and viewport. + fn capture_stencil(&self, viewport: Vec2) -> Option { + let st = self.tool.stencil.as_ref()?; + let image = self.images.get(&st.image)?.clone(); + let aspect = viewport.x.max(1.0) / viewport.y.max(1.0); + let (center, half, rotation) = st.placement(image.width as f32 / image.height.max(1) as f32, aspect); + Some(texturecraft_compose::Stencil { image, view_proj: self.camera.view_proj(aspect), center, half, rotation, tile: st.tile }) + } + + /// Start a stroke at `screen` (viewport pixels). Returns false when the cursor misses the + /// mesh (the stroke starts anyway and resumes when the cursor enters the mesh). + pub fn begin_stroke(&mut self, screen: Vec2, viewport: Vec2, pressure: f32) -> Result { + self.cancel_stroke(); + match self.tool.tool { + Tool::PolygonFill => return self.polygon_fill(screen, viewport).map(|_| true), + Tool::ColorPicker => return self.color_pick(screen, viewport).map(|_| true), + _ => {} + } + let first_uv = if self.tool.tool == Tool::Clone { self.pick(screen, viewport).map(|h| h.uv) } else { None }; + self.start_stroke(viewport, false, first_uv)?; + Ok(self.continue_stroke(screen, pressure)) + } + + /// Start a stroke in the 2D view at texture coordinate `uv`. + pub fn begin_stroke_uv(&mut self, uv: Vec2, pressure: f32) -> Result<()> { + self.cancel_stroke(); + if matches!(self.tool.tool, Tool::PolygonFill | Tool::ColorPicker) { + return Err(EngineError::Other("use this tool in the 3D view".into())); + } + self.start_stroke(self.viewport, true, Some(uv))?; + self.continue_stroke_uv(uv, pressure); + Ok(()) + } + + /// Add a 2D view sample (texture coordinates). Brush size is relative to the UV square. + pub fn continue_stroke_uv(&mut self, uv: Vec2, pressure: f32) { + let Some(st) = self.stroke.as_mut() else { return }; + let Some((last, carry)) = st.uv.as_mut() else { return }; + let Some(cache) = self.caches.get_mut(st.ts) else { return }; + let b = &st.stroke.settings; + let p = pressure.clamp(0.0, 1.0); + let radius = b.size * 2.0 * if p < 1.0 { b.min_size + (1.0 - b.min_size) * p } else { 1.0 }; + let step = (b.spacing * 2.0 * radius).max(1e-5); + let mut points = Vec::new(); + match *last { + None => points.push(uv), + Some(prev) => { + let seg = uv - prev; + let len = seg.length(); + let mut t = step - *carry; + while t <= len && points.len() < 4096 { + points.push(prev + seg * (t / len.max(1e-9))); + t += step; + } + *carry = len - (t - step); + } + } + // Follow path: turn the stamp with the stroke direction (y up on screen, v grows down). + let angle = match *last { + Some(prev) if b.follow_path && uv != prev => b.angle + (prev.y - uv.y).atan2(uv.x - prev.x).to_degrees() - 90.0, + _ => b.angle, + }; + *last = Some(uv); + let n0 = st.stroke.stamps.len() as u32; + let stamps: Vec = points + .iter() + .enumerate() + .map(|(i, c)| texturecraft_paint::UvStamp { center: *c, radius, angle, flow: b.flow, hardness: b.hardness, seed: n0 + i as u32 }) + .collect(); + texturecraft_paint::rasterize_uv(&mut st.canvas, &cache.gbuf, Self::shape(&self.images, b), &stamps); + let recent = std::mem::take(&mut st.canvas.recent); + let (ts, target) = (st.ts, st.target); + if target.is_some() { + self.mark_painted(ts, target, recent); + } else { + self.quick_mask.revision = self.quick_mask.revision.wrapping_add(1); + } + } + + /// Add a pointer sample to the active stroke. Returns whether it hit the mesh. + pub fn continue_stroke(&mut self, screen: Vec2, pressure: f32) -> bool { + let Some(st) = self.stroke.as_mut() else { return false }; + let Some(cache) = self.caches.get_mut(st.ts) else { return false }; + let material = self.doc.project.texture_sets.get(st.ts).map(|t| t.material).unwrap_or(0); + let ctx = PickCtx { mesh: &self.mesh, bvh: &self.bvh, camera: &st.camera, viewport: st.viewport }; + let stamps = st.stroke.add(&ctx, screen, pressure); + let hit = st.stroke.last_hit().is_some(); + if stamps.is_empty() { + return hit; + } + let rctx = RasterCtx { + mesh: &self.mesh, + bvh: &self.bvh, + gbuf: &cache.gbuf, + material, + depth: st.depth.as_ref(), + eye: st.camera.eye(), + backface_culling: st.stroke.settings.backface_culling, + angle_fade: st.stroke.settings.angle_fade, + shape: Self::shape(&self.images, &st.stroke.settings), + }; + rasterize(&mut st.canvas, &rctx, &stamps); + let recent = std::mem::take(&mut st.canvas.recent); + let (ts, target) = (st.ts, st.target); + if target.is_some() { + self.mark_painted(ts, target, recent); + } else { + self.quick_mask.revision = self.quick_mask.revision.wrapping_add(1); + } + hit + } + + pub fn stroke_active(&self) -> bool { + self.stroke.is_some() + } + + /// Commit the active stroke (one undo step). + pub fn end_stroke(&mut self) -> Result<()> { + let Some(st) = self.stroke.take() else { return Ok(()) }; + let res = self.caches.get(st.ts).map(|c| c.resolution).unwrap_or(1024); + if st.canvas.dirty.is_empty() { + // Nothing painted: drop any mask effect the stroke created. + self.doc = st.before; + return Ok(()); + } + let gbuf = self.caches.get(st.ts).map(|c| c.gbuf.clone()); + let mesh = self.mesh.clone(); + let stencil_fn = |x: u32, y: u32| -> Option<([f32; 3], f32)> { + let g = gbuf.as_ref()?; + let p = g.surface(&mesh, (y * g.width + x) as usize)?.pos; + st.stencil.as_ref()?.sample_world(p) + }; + let per_texel: Option = if st.stencil.is_some() { Some(&stencil_fn) } else { None }; + let carried = st.canvas.pickup.as_ref().map(|p| &p.values); + let src = CommitSource { alpha: &st.canvas.alpha, tiles: &st.canvas.dirty, opacity: st.stroke.settings.opacity, erase: st.erase, per_texel, carried }; + match st.target { + Some(PaintTarget::Layer(id)) => commit_layer(&mut self.doc.pixels, id, res, &st.values, src), + Some(PaintTarget::Mask(eid)) => commit_mask(&mut self.doc.pixels, eid, res, self.tool.mask_value, src), + None => { + self.commit_quick_mask(st.ts, res, src); + return Ok(()); + } + } + self.undo.push((st.label.clone(), st.before)); + if self.undo.len() > crate::session::MAX_HISTORY { + self.undo.remove(0); + } + self.redo.clear(); + self.modified = true; + self.revision = self.revision.wrapping_add(1); + self.mark_painted(st.ts, st.target, st.canvas.dirty.iter().copied()); + Ok(()) + } + + /// Abandon the active stroke (Esc). + pub fn cancel_stroke(&mut self) { + if let Some(st) = self.stroke.take() { + self.mark_painted(st.ts, st.target, st.canvas.dirty.iter().copied()); + if st.target.is_none() { + self.quick_mask.revision = self.quick_mask.revision.wrapping_add(1); + } + self.doc = st.before; + } + } + + /// Paint a whole stroke through viewport points (scripted strokes from CLI/MCP). + pub fn stroke_points(&mut self, points: &[Vec2], viewport: Vec2) -> Result { + let Some(first) = points.first() else { return Err(EngineError::Other("a stroke needs at least one point".into())) }; + self.begin_stroke(*first, viewport, 1.0)?; + let mut hits = 0; + for p in points.iter().skip(1) { + if self.continue_stroke(*p, 1.0) { + hits += 1; + } + } + let stamps = self.stroke.as_ref().map(|s| s.stroke.stamps.len()).unwrap_or(0); + self.end_stroke()?; + let _ = hits; + Ok(stamps) + } + + /// Paint a whole 2D view stroke through texture coordinates. Returns the touched tiles. + pub fn stroke_uv_points(&mut self, points: &[Vec2]) -> Result { + let Some(first) = points.first() else { return Err(EngineError::Other("a stroke needs at least one point".into())) }; + self.begin_stroke_uv(*first, 1.0)?; + for q in points.iter().skip(1) { + self.continue_stroke_uv(*q, 1.0); + } + let tiles = self.stroke_tiles(); + self.end_stroke()?; + Ok(tiles) + } + + /// Polygon fill: fill the triangle / UV island / whole mesh under `screen`. + pub fn polygon_fill(&mut self, screen: Vec2, viewport: Vec2) -> Result { + let hit = self.pick(screen, viewport).ok_or_else(|| EngineError::Other("nothing under the cursor".into()))?; + let ts = self.ts(); + let material = self.doc.project.texture_set(ts)?.material; + if hit.material != material { + return Err(EngineError::Other("that surface belongs to another texture set".into())); + } + let tris = self.fill_set(hit.tri, material); + let (gbuf, mut canvas) = self.fill_canvas(ts)?; + fill_triangles(&mut canvas, &gbuf, &tris); + self.commit_fill("Polygon fill", ts, &canvas)?; + Ok(tris.len()) + } + + /// The G-buffer of texture set `ts` and an empty canvas for a one-shot fill, limited by the + /// quick mask unless the fill edits it. + pub(crate) fn fill_canvas(&self, ts: usize) -> Result<(std::sync::Arc, StrokeCanvas)> { + let gbuf = self.caches.get(ts).map(|c| c.gbuf.clone()).ok_or_else(|| EngineError::Other("texture set not ready".into()))?; + let mut canvas = StrokeCanvas::new(gbuf.width); + if !self.quick_mask.editing { + canvas.limit = self.stroke_limit(ts, gbuf.width); + } + Ok((gbuf, canvas)) + } + + /// Commit a one-shot fill with the tool's material (one undo step), or into the quick mask. + pub(crate) fn commit_fill(&mut self, label: &str, ts: usize, canvas: &StrokeCanvas) -> Result<()> { + let res = canvas.alpha.width(); + let src = |opacity| CommitSource { alpha: &canvas.alpha, tiles: &canvas.dirty, opacity, erase: false, per_texel: None, carried: None }; + if self.quick_mask.editing { + self.commit_quick_mask(ts, res, src(self.tool.brush.opacity)); + return Ok(()); + } + self.edit(label, |s| { + let target = s.paint_target()?; + let values = s.stroke_values(target); + match target { + PaintTarget::Layer(id) => commit_layer(&mut s.doc.pixels, id, res, &values, src(s.tool.brush.opacity)), + PaintTarget::Mask(eid) => commit_mask(&mut s.doc.pixels, eid, res, s.tool.mask_value, src(s.tool.brush.opacity)), + } + Ok(()) + }) + } + + /// Triangles filled by the current fill mode starting from `tri`. + fn fill_set(&self, tri: u32, material: usize) -> BTreeSet { + let m = &self.mesh; + match self.tool.fill_mode { + FillMode::Triangle => [tri].into_iter().collect(), + FillMode::Mesh => m.triangles_of(material).map(|t| t as u32).collect(), + FillMode::UvIsland => { + // Triangles sharing vertex indices form a UV island (seams split vertices). + let mut by_vertex: BTreeMap> = BTreeMap::new(); + for t in m.triangles_of(material) { + if let Some(v) = m.tri(t) { + for i in v { + by_vertex.entry(i).or_default().push(t as u32); + } + } + } + let mut seen = BTreeSet::new(); + let mut stack = vec![tri]; + while let Some(t) = stack.pop() { + if !seen.insert(t) { + continue; + } + if let Some(v) = m.tri(t as usize) { + for i in v { + for n in by_vertex.get(&i).map(Vec::as_slice).unwrap_or(&[]) { + if !seen.contains(n) { + stack.push(*n); + } + } + } + } + } + seen + } + } + } + + /// Color picker: copy the material under the cursor into the tool's paint values. + pub fn color_pick(&mut self, screen: Vec2, viewport: Vec2) -> Result> { + let hit = self.pick(screen, viewport).ok_or_else(|| EngineError::Other("nothing under the cursor".into()))?; + let ts = self.doc.project.texture_sets.iter().position(|t| t.material == hit.material).unwrap_or(self.ts()); + let cache = self.caches.get(ts).ok_or_else(|| EngineError::Other("texture set not ready".into()))?; + let res = cache.resolution as f32; + let (x, y) = ((hit.uv.x * res).clamp(0.0, res - 1.0) as u32, (hit.uv.y * res).clamp(0.0, res - 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); + } + Ok(out) + } + + /// The stroke canvas alpha of the active stroke (for overlays and tests). + pub fn stroke_tiles(&self) -> usize { + self.stroke.as_ref().map(|s| s.canvas.dirty.len()).unwrap_or(0) + } + + /// Where the Clone tool reads for destination `at` (texture coordinates) as a surface + /// point: the source itself until the first stroke fixes the offset, or when `at` is None. + pub fn clone_source_point(&self, at: Option) -> Option { + let src = self.tool.clone_source?; + let uv = match (src.offset, at) { + (Some(o), Some(p)) => Vec2::new(p.x + o[0], p.y + o[1]), + _ => Vec2::from(src.uv), + }; + if !((0.0..1.0).contains(&uv.x) && (0.0..1.0).contains(&uv.y)) { + return None; + } + let g = &self.caches.get(self.ts())?.gbuf; + let (x, y) = ((uv.x * g.width as f32) as usize, (uv.y * g.height as f32) as usize); + Some(g.surface(&self.mesh, y * g.width as usize + x)?.pos) + } + + pub fn selected_layer_id(&self) -> Option { + self.selected_layer() + } +} diff --git a/crates/engine/src/path_tool.rs b/crates/engine/src/path_tool.rs new file mode 100644 index 00000000..0f282e1c --- /dev/null +++ b/crates/engine/src/path_tool.rs @@ -0,0 +1,184 @@ +//! The Path tool: control points placed on the surface (3D view) or in texture space (2D view) +//! and a smooth curve through them, committed as one brush stroke, or, when closed and filled, +//! as an antialiased lasso fill of the visible surface (3D) or of the UV region (2D). + +use serde::{Deserialize, Serialize}; +use serde_json::{Value, json}; +use texturecraft_geom::glam::{Vec2, Vec3}; +use texturecraft_paint::{Lasso, fill_coverage, spline}; + +use crate::commands::{CommandSpec, always, bad, boolp}; +use crate::session::Tool; +use crate::tool_cmds::{points_param, set_tool, viewport_of}; +use crate::{EngineError, Result, Session, cmd}; + +/// Control points kept at most. +pub const MAX_PATH_POINTS: usize = 4096; +/// Curve sampling step in viewport pixels (3D view) and texture units (2D view). +const STEP_PX: f32 = 2.0; +const STEP_UV: f32 = 0.001; + +/// The curve being edited with the Path tool. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct PathState { + /// Control points: surface positions (3D view) or texture coordinates with z = 0 (2D view). + pub points: Vec, + /// The points are texture coordinates (placed in the 2D view). + pub uv: bool, + /// The curve loops back to its first point. + pub closed: bool, + /// A closed path fills its interior instead of stroking the curve. + pub fill: bool, +} + +impl PathState { + /// Append a control point; a point placed in the other view starts a new path. + pub fn push(&mut self, p: Vec3, uv: bool) { + if uv != self.uv { + self.points.clear(); + self.uv = uv; + } + if self.points.len() < MAX_PATH_POINTS { + self.points.push(p); + } + } +} + +pub fn specs() -> Vec { + vec![cmd!( + "tool.path", + "Path", + ["Tools"], + Some("7"), + "{points?: [[x,y],...] viewport pixels on the mesh | uvPoints?: [[u,v],...], viewport?: [w,h], closed?, fill?, commit?: bool, clear?: bool} — selects the Path tool and edits its curve; commit paints the curve with the current brush, or fills a closed, filled path", + always, + path + )] +} + +fn path(s: &mut Session, p: &Value) -> Result { + const CMD: &str = "tool.path"; + if s.tool.tool != Tool::Path { + set_tool(s, Tool::Path)?; + } + let vp = viewport_of(s, p); + if boolp(p, "clear") == Some(true) { + s.tool.path.points.clear(); + } + let placed = match (points_param(CMD, p, "points")?, points_param(CMD, p, "uvPoints")?) { + (Some(pts), _) => Some(( + pts.iter() + .enumerate() + .map(|(i, q)| s.pick(*q, vp).map(|h| h.pos).ok_or_else(|| bad(CMD, format!("point {i} misses the mesh")))) + .collect::>>()?, + false, + )), + (None, Some(pts)) => Some((pts.iter().map(|q| q.clamp(Vec2::ZERO, Vec2::ONE).extend(0.0)).collect(), true)), + (None, None) => None, + }; + if let Some((points, uv)) = placed { + if points.len() > MAX_PATH_POINTS { + return Err(bad(CMD, format!("at most {MAX_PATH_POINTS} control points"))); + } + s.tool.path.points = points; + s.tool.path.uv = uv; + } + if let Some(v) = boolp(p, "closed") { + s.tool.path.closed = v; + } + if let Some(v) = boolp(p, "fill") { + s.tool.path.fill = v; + } + s.revision = s.revision.wrapping_add(1); + let committed = if boolp(p, "commit") == Some(true) { + let r = s.commit_path(vp)?; + s.update(); + r + } else { + Value::Null + }; + let st = &s.tool.path; + Ok(json!({ "points": st.points.len(), "uv": st.uv, "closed": st.closed, "fill": st.fill, "committed": committed })) +} + +/// NDC (y up) to viewport pixels (origin top-left). +fn to_pixels(ndc: Vec2, viewport: Vec2) -> Vec2 { + Vec2::new((ndc.x * 0.5 + 0.5) * viewport.x, (0.5 - ndc.y * 0.5) * viewport.y) +} + +impl Session { + /// The path's control points in view space: viewport pixels for a 3D path (None behind the + /// camera), texture coordinates for a 2D one. + pub fn path_view_points(&self, viewport: Vec2) -> Vec> { + let path = &self.tool.path; + if path.uv { + return path.points.iter().map(|p| Some(p.truncate())).collect(); + } + let aspect = viewport.x.max(1.0) / viewport.y.max(1.0); + path.points.iter().map(|p| self.camera.project(*p, aspect).map(|n| to_pixels(n.truncate(), viewport))).collect() + } + + /// Paint the path with the current brush (or fill it when closed and filled), then clear it. + pub fn commit_path(&mut self, viewport: Vec2) -> Result { + let (uv, closed) = (self.tool.path.uv, self.tool.path.closed); + let fill = closed && self.tool.path.fill; + let pts: Vec = self.path_view_points(viewport).into_iter().flatten().collect(); + if pts.is_empty() { + return Err(EngineError::Other("the path has no points: click the surface to add some".into())); + } + if fill && pts.len() < 3 { + return Err(EngineError::Other("a filled path needs at least three points".into())); + } + let curve = spline(&pts, closed, if uv { STEP_UV } else { STEP_PX }); + let out = if fill { + json!({ "filledTiles": self.lasso_fill(curve, uv, viewport)? }) + } else if uv { + json!({ "tiles": self.stroke_uv_points(&curve)? }) + } else { + json!({ "stamps": self.stroke_points(&curve, viewport)? }) + }; + self.tool.path.points.clear(); + Ok(out) + } + + /// Fill the inside of `poly`: viewport pixels over the visible surface, or texture + /// coordinates. Returns the touched tiles. + fn lasso_fill(&mut self, poly: Vec, uv: bool, viewport: Vec2) -> Result { + let ts = self.ts(); + let (gbuf, mut canvas) = self.fill_canvas(ts)?; + let w = gbuf.width.max(1); + if uv { + let lasso = Lasso::new(poly.iter().map(|p| *p * w as f32).collect()); + fill_coverage(&mut canvas, &gbuf, |i, _| lasso.coverage(Vec2::new((i as u32 % w) as f32 + 0.5, (i as u32 / w) as f32 + 0.5))); + } else { + let lasso = Lasso::new(poly); + let view_proj = self.camera.view_proj(viewport.x.max(1.0) / viewport.y.max(1.0)); + let inv = view_proj.inverse(); + let (cull, mesh, bvh) = (self.tool.brush.backface_culling, &self.mesh, &self.bvh); + fill_coverage(&mut canvas, &gbuf, |i, _| { + let Some(s) = gbuf.surface(mesh, i) else { return 0.0 }; + let c = view_proj * s.pos.extend(1.0); + if c.w <= 1e-6 { + return 0.0; + } + let ndc = Vec2::new(c.x, c.y) / c.w; + let a = lasso.coverage(to_pixels(ndc, viewport)); + if a <= 0.0 { + return 0.0; + } + // Exact visibility (a depth buffer flickers on grazing surfaces): nothing between + // the near plane and the texel. + let o = inv * ndc.extend(0.0).extend(1.0); + let origin = o.truncate() / o.w; + let to = s.pos - origin; + let d = to.length(); + let dir = to / d.max(1e-9); + let hidden = (cull && s.nrm.dot(dir) >= 0.0) || bvh.closest(origin, dir, d * 0.999).is_some(); + if hidden { 0.0 } else { a } + }); + } + let tiles = canvas.dirty.len(); + self.commit_fill("Path fill", ts, &canvas)?; + Ok(tiles) + } +} diff --git a/crates/engine/src/preview.rs b/crates/engine/src/preview.rs new file mode 100644 index 00000000..f1ab3300 --- /dev/null +++ b/crates/engine/src/preview.rs @@ -0,0 +1,143 @@ +//! Shelf previews: built-in materials and smart masks evaluated on a unit sphere seen from +Z, +//! without a mesh or baked maps (generators that need mesh maps are approximated with world-space +//! noise). Cheap enough for lazily rendered thumbnails. + +use texturecraft_compose::pattern::{Coords, procedural, world_noise}; +use texturecraft_core::{Channel, EffectKind, Filter, Generator, GeneratorKind, LayerKind, Mask, MaskBackground, Project, Projection, Source, blend1}; +use texturecraft_geom::glam::{Vec2, Vec3}; + +use crate::materials::{Builder, build}; + +/// Linear material values of one preview pixel. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Sample { + pub base: [f32; 3], + pub metallic: f32, + pub roughness: f32, + pub height: f32, +} + +/// The view-space normal (= position on the unit sphere) of each pixel of a `size²` image, +/// row-major, None outside the sphere. +pub fn sphere_normals(size: usize) -> impl Iterator> { + let r = (size as f32 * 0.5 - 1.0).max(0.5); + (0..size * size).map(move |i| { + let (x, y) = ((i % size) as f32 + 0.5 - size as f32 * 0.5, size as f32 * 0.5 - (i / size) as f32 - 0.5); + let (dx, dy) = (x / r, y / r); + let d2 = dx * dx + dy * dy; + (d2 <= 1.0).then(|| Vec3::new(dx, dy, (1.0 - d2).max(0.0).sqrt())) + }) +} + +fn coords(n: Vec3) -> Coords { + let uv = Vec2::new(n.z.atan2(n.x) / std::f32::consts::TAU + 0.5, n.y.clamp(-1.0, 1.0).acos() / std::f32::consts::PI); + Coords { uv, pos: n, nrm: n } +} + +fn source(s: &Source, proj: &Projection, c: &Coords) -> Option<[f32; 3]> { + match s { + Source::Uniform(v) => Some(*v), + Source::Procedural { pattern, low, high } => { + let t = procedural(pattern, proj, c); + Some([0, 1, 2].map(|k| low[k] + (high[k] - low[k]) * t)) + } + _ => None, + } +} + +/// Approximate generator: mesh-map signals become world noise (a sphere has no edges or cavities). +fn generator(g: &Generator, c: &Coords) -> f32 { + let dir = Vec3::from(g.direction).try_normalize().unwrap_or(Vec3::Y); + let wear = (world_noise(c.pos, 3.0, 91) - 0.5) * 2.2; + let cavity = (world_noise(c.pos, 3.0, 57) - 0.3).clamp(0.0, 1.0) + (1.0 - c.nrm.y) * 0.1; + let s = match g.kind { + GeneratorKind::Position => c.pos.dot(dir) * 0.5 + 0.5, + GeneratorKind::Direction => c.nrm.dot(dir) * 0.5 + 0.5, + GeneratorKind::EdgeWear => wear, + GeneratorKind::Curvature => 0.5 + wear * 0.5, + GeneratorKind::AmbientOcclusion => 1.0 - cavity, + _ => cavity, + }; + // Same thresholding as the compositor. + let s = if g.noise_amount > 0.0 { s + (world_noise(c.pos, g.noise_scale, g.seed) - 0.5) * g.noise_amount } else { s }; + let soft = g.softness.max(1e-3); + let t = ((s - (1.0 - g.level.clamp(0.0, 1.0) - soft)) / (2.0 * soft)).clamp(0.0, 1.0); + let v = t * t * (3.0 - 2.0 * t); + if g.invert { 1.0 - v } else { v } +} + +fn mask(m: &Mask, c: &Coords) -> f32 { + if !m.enabled { + return 1.0; + } + let mut v = if m.background == MaskBackground::White { 1.0 } else { 0.0 }; + for e in m.effects.iter().filter(|e| e.visible) { + let val = match &e.kind { + EffectKind::Fill { source: s, projection } => source(s, projection, c).map(|x| x[0]), + EffectKind::Generator(g) => Some(generator(g, c)), + EffectKind::Levels(l) => Some(l.apply(v)), + EffectKind::Filter(Filter::Invert) => Some(1.0 - v), + _ => None, + }; + if let Some(val) = val { + let blended = if matches!(e.kind, EffectKind::Levels(_) | EffectKind::Filter(_)) { val } else { blend1(e.blend, v, val) }; + v += (blended - v) * e.opacity.clamp(0.0, 1.0); + } + } + v.clamp(0.0, 1.0) +} + +const CHANNELS: [Channel; 4] = [Channel::BaseColor, Channel::Metallic, Channel::Roughness, Channel::Height]; + +/// Preview of built-in material `id` (None for an unknown id). +pub fn material(id: &str, size: usize) -> Option>> { + let mut p = Project::new("preview", ""); + let layers = build(&mut Builder { p: &mut p, channels: CHANNELS.to_vec() }, id).ok()?; + Some( + sphere_normals(size) + .map(|n| { + let c = coords(n?); + let mut acc = CHANNELS.map(Channel::default_value); + for l in layers.iter().filter(|l| l.visible) { + let LayerKind::Fill(f) = &l.kind else { continue }; + let w = l.mask.as_ref().map_or(1.0, |m| mask(m, &c)); + for (a, ch) in acc.iter_mut().zip(CHANNELS) { + if let (Some(b), Some(v)) = (l.blend(ch), f.sources.get(&ch).and_then(|s| source(s, &f.projection, &c))) { + let t = w * b.opacity.clamp(0.0, 1.0); + *a = [0, 1, 2].map(|k| a[k] + (v[k] - a[k]) * t); + } + } + } + let [base, metal, rough, height] = acc; + Some(Sample { base, metallic: metal[0], roughness: rough[0], height: height[0] }) + }) + .collect(), + ) +} + +/// Preview of smart mask `id`: the mask value per pixel (None for an unknown id). +pub fn smart_mask(id: &str, size: usize) -> Option>> { + let (bg, fx) = crate::smart_masks::preset(id)?; + let m = crate::smart_masks::build_mask(&mut Project::new("preview", ""), bg, fx); + Some(sphere_normals(size).map(|n| n.map(|n| mask(&m, &coords(n)))).collect()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn previews_cover_the_sphere_with_finite_values() { + for m in crate::materials::MATERIALS { + let px = material(m.id, 16).unwrap_or_else(|| panic!("{}", m.id)); + let center = px[8 * 16 + 8].unwrap_or_else(|| panic!("{}", m.id)); + assert!(center.base.iter().chain([¢er.metallic, ¢er.roughness, ¢er.height]).all(|v| v.is_finite()), "{}", m.id); + assert!(px[0].is_none(), "corners are outside the sphere"); + } + for sm in crate::smart_masks::SMART_MASKS { + let px = smart_mask(sm.id, 16).unwrap_or_else(|| panic!("{}", sm.id)); + assert!(px.iter().flatten().all(|v| (0.0..=1.0).contains(v)), "{}", sm.id); + } + assert!(material("nope", 8).is_none() && smart_mask("nope", 8).is_none()); + } +} diff --git a/crates/engine/src/project_cmds.rs b/crates/engine/src/project_cmds.rs new file mode 100644 index 00000000..ebb9c58a --- /dev/null +++ b/crates/engine/src/project_cmds.rs @@ -0,0 +1,77 @@ +//! Project-level commands: new project, sample project, smart materials, inspection. + +use serde_json::{Value, json}; +use texturecraft_compose::BakedMaps; +use texturecraft_geom::primitives; + +use crate::commands::{CommandSpec, always, bad, strp, u64p}; +use crate::{Result, Session, cmd}; + +pub fn specs() -> Vec { + vec![ + cmd!( + "project.new", + "New project from a built-in mesh", + [], + None, + "{mesh?: material_ball|sphere|cube|rounded_cube|cylinder|torus|plane|crate, resolution?: 1024, name?}", + always, + new + ), + cmd!("project.sample", "Open sample project", ["File"], None, "{}", always, sample), + cmd!("project.inspect", "Inspect project", [], None, "{}", always, |s, _| Ok(crate::inspect::session(s))), + cmd!("material.apply", "Add smart material", [], None, "{material: id or label}", always, material), + cmd!("material.list", "List materials", [], None, "{}", always, |_, _| Ok(json!( + crate::materials::MATERIALS.iter().map(|m| json!({"id": m.id, "label": m.label, "smart": m.smart})).collect::>() + ))), + ] +} + +fn new(s: &mut Session, p: &Value) -> Result { + let mesh_id = strp(p, "mesh").unwrap_or("material_ball"); + let mesh = primitives::builtin(mesh_id).ok_or_else(|| bad("project.new", format!("unknown built-in mesh `{mesh_id}`")))?; + let res = u64p(p, "resolution").unwrap_or(1024); + let name = strp(p, "name").unwrap_or("Untitled"); + let mut next = Session::new_project(name, mesh, &format!("builtin:{mesh_id}"), None, u32::try_from(res).unwrap_or(0))?; + next.viewport = s.viewport; + *s = next; + Ok(crate::inspect::session(s)) +} + +fn material(s: &mut Session, p: &Value) -> Result { + let id = strp(p, "material").ok_or_else(|| bad("material.apply", "`material` is required"))?; + let l = crate::materials::apply(s, id)?; + Ok(json!({ "layer": l.0 })) +} + +/// The sample project: the material ball, baked, with smart materials on both texture sets. +pub fn sample_session(resolution: u32, bake_resolution: u32) -> Result { + let mut s = Session::new_project("Material Ball", primitives::material_ball(), "builtin:material_ball", None, resolution)?; + for ts in 0..s.set_count() { + let set = s.doc.project.texture_set_mut(ts)?; + set.bake.resolution = bake_resolution; + set.bake.rays = 24; + s.bake_now(ts)?; + } + s.active_set = 1; + crate::materials::apply(&mut s, "iron_rusted")?; + s.active_set = 0; + crate::materials::apply(&mut s, "steel_painted_worn")?; + s.undo.clear(); + s.modified = false; + Ok(s) +} + +fn sample(s: &mut Session, _: &Value) -> Result { + let mut next = sample_session(2048, 1024)?; + next.viewport = s.viewport; + *s = next; + Ok(crate::inspect::session(s)) +} + +pub(crate) fn baked_json(b: Option<&BakedMaps>) -> Value { + match b { + Some(b) => json!({ "resolution": b.resolution, "maps": b.maps.keys().map(|m| m.id()).collect::>() }), + None => Value::Null, + } +} diff --git a/crates/engine/src/quick_mask.rs b/crates/engine/src/quick_mask.rs new file mode 100644 index 00000000..a063b3b8 --- /dev/null +++ b/crates/engine/src/quick_mask.rs @@ -0,0 +1,137 @@ +//! Quick mask (Painter: Y enables, U edits, Ctrl+Shift+I inverts): a temporary texel mask per +//! texture set, painted with the normal brush while editing, that limits every paint stroke +//! while enabled. Session state only: not saved and not in the undo history. + +use std::collections::BTreeMap; + +use rayon::prelude::*; +use serde_json::{Value, json}; +use texturecraft_core::Channel; +use texturecraft_paint::{CommitSource, commit_coverage}; +use texturecraft_texel::{TileImage, from_u16, srgb_lut}; + +use crate::commands::{CommandSpec, always, boolp}; +use crate::{Result, Session, cmd}; + +#[derive(Clone, Debug, Default)] +pub struct QuickMask { + /// Strokes are limited to the mask. + pub enabled: bool, + /// Strokes paint the mask instead of the layer stack. + pub editing: bool, + /// Strokes are limited to where the mask is not painted. + pub inverted: bool, + /// Painted coverage per texture set (1 component). + pub masks: BTreeMap, + /// Bumps on every change (overlay refresh). + pub revision: u64, +} + +pub fn specs() -> Vec { + vec![ + cmd!("viewport.quickMask", "Enable quick mask", ["Viewport"], Some("Y"), "{enabled?: bool} — toggles when omitted", always, enable), + cmd!( + "viewport.quickMask.edit", + "Edit quick mask", + ["Viewport"], + Some("U"), + "{editing?: bool} — toggles when omitted; editing enables the mask", + always, + edit + ), + cmd!("viewport.quickMask.invert", "Invert quick mask", ["Viewport"], Some("Ctrl+Shift+I"), "{}", always, invert), + ] +} + +fn state(s: &Session) -> Value { + let q = &s.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), + }) +} + +fn changed(s: &mut Session) -> Result { + s.quick_mask.revision = s.quick_mask.revision.wrapping_add(1); + s.revision = s.revision.wrapping_add(1); + Ok(state(s)) +} + +fn enable(s: &mut Session, p: &Value) -> Result { + s.quick_mask.enabled = boolp(p, "enabled").unwrap_or(!s.quick_mask.enabled); + changed(s) +} + +fn edit(s: &mut Session, p: &Value) -> Result { + 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 invert(s: &mut Session, _: &Value) -> Result { + s.quick_mask.inverted = !s.quick_mask.inverted; + changed(s) +} + +impl Session { + /// The quick mask of texture set `ts` at resolution `res` (empty when never painted). + fn quick_mask_image(&self, ts: usize, res: u32) -> TileImage { + match self.quick_mask.masks.get(&ts) { + Some(m) if m.width() == res => m.clone(), + Some(m) => m.resampled(res, res, &[0]), + None => TileImage::new(res, res, 1), + } + } + + /// What limits a paint stroke on `ts`: the mask and whether it is inverted (None when the + /// quick mask is off or being edited). + pub(crate) fn stroke_limit(&self, ts: usize, res: u32) -> Option<(TileImage, bool)> { + let q = &self.quick_mask; + (q.enabled && !q.editing).then(|| (self.quick_mask_image(ts, res), q.inverted)) + } + + /// Commit a stroke into the quick mask of `ts`. + pub(crate) fn commit_quick_mask(&mut self, ts: usize, res: u32, src: CommitSource) { + let mut mask = self.quick_mask_image(ts, res); + commit_coverage(&mut mask, src); + self.quick_mask.masks.insert(ts, mask); + self.quick_mask.revision = self.quick_mask.revision.wrapping_add(1); + } + + /// RGBA8 overlay of texture set `ts` while editing the quick mask (`size²`, display-encoded): + /// the base color, tinted red where painting is blocked. Includes the stroke in progress. + pub fn quick_mask_rgba(&self, ts: usize, size: u32) -> Option> { + let cache = self.caches.get(ts)?; + let res = cache.resolution; + let size = size.clamp(1, res); + let mask = self.quick_mask_image(ts, res); + let live = self.stroke.as_ref().filter(|s| s.ts == ts && s.target.is_none()).map(|s| (&s.canvas.alpha, s.stroke.settings.opacity, s.erase)); + let base = cache.composite.channels.get(&Channel::BaseColor); + let lut = srgb_lut(); + let inverted = self.quick_mask.inverted; + let mut out = vec![255u8; size as usize * size as usize * 4]; + out.par_chunks_mut(size as usize * 4).enumerate().for_each(|(y, row)| { + let ty = (y as u64 * u64::from(res) / u64::from(size)) as u32; + for (x, px) in row.chunks_exact_mut(4).enumerate() { + let tx = (x as u64 * u64::from(res) / u64::from(size)) as u32; + let mut m = from_u16(mask.get(tx, ty)[0]); + if let Some((alpha, opacity, erase)) = live { + let a = from_u16(alpha.get(tx, ty)[0]) * opacity; + m = if erase { m * (1.0 - a) } else { a + m * (1.0 - a) }; + } + let blocked = if inverted { m } else { 1.0 - m }; + let raw = base.map(|b| b.get(tx, ty)).unwrap_or([32768; 4]); + let t = 0.55 * blocked; + for (k, tint) in [0.9, 0.1, 0.15].into_iter().enumerate() { + let c = f32::from(lut.get(usize::from(raw[k] >> 4)).copied().unwrap_or(0)) / 255.0; + if let Some(slot) = px.get_mut(k) { + *slot = ((c + (tint - c) * t).clamp(0.0, 1.0) * 255.0 + 0.5) as u8; + } + } + } + }); + Some(out) + } +} diff --git a/crates/engine/src/resource_cmds.rs b/crates/engine/src/resource_cmds.rs new file mode 100644 index 00000000..c7b156a7 --- /dev/null +++ b/crates/engine/src/resource_cmds.rs @@ -0,0 +1,152 @@ +//! Image resources: import (PNG, JPEG, TGA, TIFF, BMP, WEBP, EXR, HDR), list, remove, and use as +//! fill sources. + +use std::sync::Arc; + +use serde_json::{Value, json}; +use texturecraft_compose::ImageRes; +use texturecraft_core::{Channel, Fill, Layer, LayerKind, Projection, Source}; + +use crate::commands::{CommandSpec, always, bad, channel_param, strp}; +use crate::{EngineError, Result, Session, cmd}; + +pub fn specs() -> Vec { + vec![ + cmd!("resource.import", "Import resources", ["File"], None, "{path} — an image file (png, jpg, tga, tif, bmp, webp, exr, hdr)", always, import), + cmd!("resource.list", "List resources", [], None, "{}", always, |s, _| Ok(list(s))), + cmd!("resource.remove", "Remove resource", [], None, "{name}", always, remove), + cmd!("file.removeUnused", "Remove unused resources...", ["File"], None, "{}", always, remove_unused), + cmd!("layer.add.image", "Add fill layer from image", [], None, "{name, channel?: baseColor, projection?: uv|triplanar|planar, scale?}", always, add_image_fill), + ] +} + +/// Largest accepted resource file. +const MAX_BYTES: usize = 512 << 20; + +fn decoded(d: texturecraft_formats::DecodedImage) -> Arc { + Arc::new(ImageRes { width: d.width, height: d.height, rgba: d.rgba, srgb: d.srgb }) +} + +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) -> Result { + if bytes.len() > MAX_BYTES { + return Err(EngineError::Other(format!("{file_name} is larger than {} MB", MAX_BYTES >> 20))); + } + let image = texturecraft_formats::decode_image(&bytes).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 base = texturecraft_formats::sanitize_resource_name(leaf); + let mut name = base.clone(); + let mut n = 2; + while self.doc.resources.contains_key(&name) { + name = format!("{n}_{base}"); + n += 1; + } + let key = name.clone(); + self.edit("Import resource", |s| { + s.doc.resources.insert(key, Arc::new(bytes)); + Ok(()) + })?; + 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 = self.doc.resources.keys().cloned().collect(); + self.images.retain(|k, _| keep.contains(k)); + let missing: Vec<(String, Arc>)> = self.doc.resources.iter().filter(|(k, _)| !self.images.contains_key(*k)).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)); + } + Err(e) => self.log.push(format!("resource {k}: {e}")), + } + } + } +} + +fn list(s: &Session) -> Value { + json!( + s.doc + .resources + .iter() + .map(|(k, v)| { + let (w, h) = s.images.get(k).map(|i| (i.width, i.height)).unwrap_or((0, 0)); + json!({"name": k, "bytes": v.len(), "width": w, "height": h}) + }) + .collect::>() + ) +} + +#[cfg(not(target_arch = "wasm32"))] +fn import(s: &mut Session, p: &Value) -> Result { + let path = strp(p, "path").ok_or_else(|| bad("resource.import", "`path` is required"))?; + let meta = std::fs::metadata(path).map_err(|e| bad("resource.import", format!("cannot read {path}: {e}")))?; + if meta.len() > MAX_BYTES as u64 { + return Err(bad("resource.import", format!("{path} is too large"))); + } + let bytes = std::fs::read(path).map_err(|e| bad("resource.import", format!("cannot read {path}: {e}")))?; + let name = s.import_resource_bytes(path, bytes)?; + Ok(json!({ "name": name })) +} + +#[cfg(target_arch = "wasm32")] +fn import(_: &mut Session, _: &Value) -> Result { + Err(EngineError::Other("file paths are not available in the browser; use File > Import resources".into())) +} + +fn remove(s: &mut Session, p: &Value) -> Result { + let name = strp(p, "name").ok_or_else(|| bad("resource.remove", "`name` is required"))?.to_string(); + if !s.doc.resources.contains_key(&name) { + return Err(bad("resource.remove", format!("no resource `{name}`"))); + } + s.edit("Remove resource", |s| { + s.doc.resources.remove(&name); + Ok(()) + })?; + s.sync_images(); + Ok(Value::Null) +} + +/// Drop resources no layer, mask effect, the projection stencil or the environment uses (one undo +/// step). +fn remove_unused(s: &mut Session, _: &Value) -> Result { + let mut used = Vec::new(); + for set in &s.doc.project.texture_sets { + for root in &set.stack { + crate::material_lib::images_used(&s.doc.project, *root, &mut used, 0); + } + } + used.extend(s.tool.stencil.as_ref().map(|st| st.image.clone())); + used.extend(s.doc.project.display.environment_resource().map(str::to_string)); + let unused: Vec = s.doc.resources.keys().filter(|k| !used.contains(k)).cloned().collect(); + if !unused.is_empty() { + s.edit("Remove unused resources", |s| { + s.doc.resources.retain(|k, _| !unused.contains(k)); + Ok(()) + })?; + s.sync_images(); + } + Ok(json!({ "removed": unused })) +} + +fn add_image_fill(s: &mut Session, p: &Value) -> Result { + 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}`"))); + } + let ch = channel_param("layer.add.image", p, "channel")?.unwrap_or(Channel::BaseColor); + let projection = if p.get("projection").is_some() { crate::layer_cmds::projection_from(p, "layer.add.image")? } else { Projection::default() }; + let ts = s.ts(); + let label = std::path::Path::new(&name).file_stem().and_then(|n| n.to_str()).unwrap_or("Image").to_string(); + let layer_name = s.doc.project.next_layer_name(ts, &label); + let id = s.edit("Add image fill", |s| { + let id = s.doc.project.alloc_id(); + let mut fill = Fill { projection, ..Fill::default() }; + fill.sources.insert(ch, Source::Image { name: name.clone() }); + crate::layer_cmds::insert_selected(s, Layer::new(id, layer_name, LayerKind::Fill(fill), &[ch])) + })?; + Ok(json!({ "layer": id.0 })) +} diff --git a/crates/engine/src/session.rs b/crates/engine/src/session.rs new file mode 100644 index 00000000..5f7ca4f8 --- /dev/null +++ b/crates/engine/src/session.rs @@ -0,0 +1,698 @@ +//! The editing session: document, mesh, per-texture-set caches, history, tool state. + +use std::collections::{BTreeMap, BTreeSet}; +use std::sync::Arc; + +use serde::{Deserialize, Serialize}; +use texturecraft_compose::{BakedMaps, BelowCache, Composite, PaintTarget, PixelStore, StackKey, StrokePreview, covered_tiles, pad_tiles}; +use texturecraft_core::{Channel, Layer, LayerId, LayerKind, Project, TextureSet}; +use texturecraft_geom::glam::Vec2; +use texturecraft_geom::{Bvh, Mesh, OrbitCamera, UvGBuffer}; +use texturecraft_paint::{BrushSettings, EyeDepth, Stroke, StrokeCanvas}; +use texturecraft_texel::DilationMap; + +use crate::{EngineError, Result}; + +/// Padding (texels) applied to composites for display. +pub const DISPLAY_PADDING: u32 = 8; +/// Undo steps kept. +pub const MAX_HISTORY: usize = 200; + +/// Everything that undo/redo restores. Pixel data is copy-on-write, so snapshots are cheap. +#[derive(Clone, Debug)] +pub struct Document { + pub project: Project, + pub pixels: PixelStore, + pub baked: BTreeMap, + /// Imported image resources by name: original file bytes. + pub resources: BTreeMap>>, +} + +/// Derived per-texture-set data (rebuilt when the mesh or resolution changes). +pub struct SetCache { + pub resolution: u32, + pub gbuf: Arc, + pub dilation: Arc, + pub covered_tiles: Arc>, + pub composite: Composite, + /// Tiles to recomposite. + pub dirty: BTreeSet<(u32, u32)>, + /// Tiles whose display textures must be re-uploaded. + pub upload: BTreeSet<(u32, u32)>, + /// Bumps on every composite change (thumbnails, exports, caches). + pub revision: u64, + /// The stack below the layer being edited, per tile (repaints skip it). + pub below: BelowCache, +} + +impl SetCache { + fn new(mesh: &Mesh, ts: &TextureSet) -> SetCache { + let res = ts.resolution; + let gbuf = UvGBuffer::new(mesh, ts.material, res, res); + let dilation = DilationMap::new(&gbuf.coverage_mask(), res, res, Some(DISPLAY_PADDING)); + let covered = covered_tiles(&gbuf); + let all: BTreeSet<(u32, u32)> = texturecraft_compose::all_tiles(res).into_iter().collect(); + SetCache { + resolution: res, + gbuf: Arc::new(gbuf), + dilation: Arc::new(dilation), + covered_tiles: Arc::new(covered), + composite: Composite::new(&ts.channel_list(), res), + dirty: all.clone(), + upload: all, + revision: 0, + below: BelowCache::default(), + } + } + + pub fn invalidate_all(&mut self) { + self.dirty.extend(self.covered_tiles.iter().copied()); + } +} + +/// Active tool. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum Tool { + #[default] + Paint, + Eraser, + Projection, + PolygonFill, + Smudge, + Clone, + ColorPicker, + /// Paint along an editable curve (or fill a closed one). + Path, +} + +impl Tool { + pub const ALL: [Tool; 8] = [Tool::Paint, Tool::Eraser, Tool::Projection, Tool::PolygonFill, Tool::Smudge, Tool::Clone, Tool::ColorPicker, Tool::Path]; + pub fn label(self) -> &'static str { + match self { + Tool::Paint => "Paint", + Tool::Eraser => "Eraser", + Tool::Projection => "Projection", + Tool::PolygonFill => "Polygon fill", + Tool::Smudge => "Smudge", + Tool::Clone => "Clone", + Tool::ColorPicker => "Color picker", + Tool::Path => "Path", + } + } + pub fn id(self) -> &'static str { + match self { + Tool::Paint => "paint", + Tool::Eraser => "eraser", + Tool::Projection => "projection", + Tool::PolygonFill => "polygonFill", + Tool::Smudge => "smudge", + Tool::Clone => "clone", + Tool::ColorPicker => "colorPicker", + Tool::Path => "path", + } + } + pub fn parse(s: &str) -> Option { + let n = |x: &str| x.to_ascii_lowercase().replace([' ', '_', '-'], ""); + Tool::ALL.into_iter().find(|t| n(t.id()) == n(s) || n(t.label()) == n(s)) + } +} + +/// What the viewport displays. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum ViewMode { + /// The lit material. + #[default] + Material, + /// One channel, unlit. + Channel(Channel), + /// A baked mesh map. + MeshMap(texturecraft_core::BakedMap), + /// UV checker (texel density and seams). + UvChecker, + /// One layer's mask, in grayscale (Painter: Alt+click a mask). + Mask(LayerId), +} + +impl ViewMode { + pub fn label(&self) -> String { + match self { + ViewMode::Material => "Material".into(), + ViewMode::Channel(c) => c.label().into(), + ViewMode::MeshMap(m) => m.label().into(), + ViewMode::UvChecker => "UV checker".into(), + ViewMode::Mask(_) => "Layer mask".into(), + } + } +} + +/// Polygon fill granularity. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum FillMode { + #[default] + Triangle, + UvIsland, + Mesh, +} + +/// A screen-pinned image for the Projection tool (Painter's stencil). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct StencilState { + /// Image resource name. + pub image: String, + /// Center in normalized device coordinates (-1..1, y up). + pub center: [f32; 2], + /// Height of the image as a fraction of the viewport height (1 = full height). + pub scale: f32, + /// Rotation in degrees. + pub rotation: f32, + /// Repeat the image across the screen. + pub tile: bool, + /// Overlay opacity in the viewport (display only). + pub opacity: f32, +} + +impl StencilState { + pub fn new(image: &str) -> StencilState { + StencilState { image: image.to_string(), center: [0.0, 0.0], scale: 0.8, rotation: 0.0, tile: false, opacity: 0.5 } + } + + /// Placement for a viewport aspect (width / height): center, half extents in NDC, radians. + pub fn placement(&self, image_aspect: f32, viewport_aspect: f32) -> ([f32; 2], [f32; 2], f32) { + let hy = self.scale.max(0.01); + let hx = hy * image_aspect / viewport_aspect.max(1e-3); + (self.center, [hx, hy], self.rotation.to_radians()) + } +} + +/// Tool settings shared by the UI, CLI and agents. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct ToolState { + pub tool: Tool, + pub brush: BrushSettings, + /// Material painted per channel (linear values) and whether each channel is painted. + pub values: BTreeMap, + pub enabled: BTreeSet, + /// Gray painted into masks. + pub mask_value: f32, + pub fill_mode: FillMode, + /// Stencil used by the Projection tool. + #[serde(default)] + pub stencil: Option, + /// Smudge tool: how much of the picked-up values each stamp deposits (0..1). + #[serde(default = "default_smudge_strength")] + pub smudge_strength: f32, + /// Clone tool source (Painter: V + click). + #[serde(default)] + pub clone_source: Option, + /// The Path tool's curve being edited. + #[serde(default)] + pub path: crate::path_tool::PathState, +} + +fn default_smudge_strength() -> f32 { + 0.5 +} + +/// Where the Clone tool copies from, in texture coordinates. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +pub struct CloneSource { + pub uv: [f32; 2], + /// Source minus destination, fixed by the first stroke after the source was set and kept + /// for the following strokes (aligned cloning). + pub offset: Option<[f32; 2]>, +} + +impl Default for ToolState { + fn default() -> Self { + let values = Channel::ALL.iter().map(|c| (*c, c.default_value())).collect(); + let mut t = ToolState { + tool: Tool::Paint, + brush: BrushSettings::default(), + values, + enabled: [Channel::BaseColor, Channel::Metallic, Channel::Roughness, Channel::Height, Channel::Normal].into_iter().collect(), + mask_value: 1.0, + fill_mode: FillMode::Triangle, + stencil: None, + smudge_strength: default_smudge_strength(), + clone_source: None, + path: Default::default(), + }; + t.values.insert(Channel::BaseColor, [0.8, 0.8, 0.8]); + t.values.insert(Channel::Roughness, [0.3; 3]); + t + } +} + +/// What is selected in the layer stack. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum StackTarget { + #[default] + Content, + /// The layer's mask (painting adds/uses a paint effect). + Mask, + /// A specific effect. + Effect(LayerId), +} + +pub(crate) struct ActiveStroke { + pub ts: usize, + /// None: the stroke edits the quick mask. + pub target: Option, + pub stroke: Stroke, + pub canvas: StrokeCanvas, + /// Camera visibility for 3D strokes (None for strokes in the 2D view). + pub depth: Option, + /// 2D view strokes: last UV point and the distance carried to the next stamp. + pub uv: Option<(Option, f32)>, + pub camera: OrbitCamera, + pub viewport: Vec2, + pub values: BTreeMap, + pub erase: bool, + /// Projection tool: the stencil captured with the stroke's camera. + pub stencil: Option, + /// History snapshot taken when the stroke began. + pub before: Document, + pub label: String, +} + +pub struct Session { + pub doc: Document, + pub mesh: Arc, + /// Unique per loaded mesh (GPU caches compare it). + pub mesh_id: u64, + pub bvh: Arc, + /// Original mesh file (saved inside the project); None for built-in meshes. + pub mesh_source: Option<(String, Vec)>, + /// High-definition meshes and cage for baking (saved inside the project). + pub bake_meshes: crate::BakeMeshes, + pub caches: Vec, + pub(crate) undo: Vec<(String, Document)>, + pub(crate) redo: Vec<(String, Document)>, + pub tool: ToolState, + pub active_set: usize, + /// Selected layer per texture set. + pub selected: BTreeMap, + pub target: StackTarget, + pub camera: OrbitCamera, + pub view: ViewMode, + /// Viewport size (pixels) used by scripted strokes. + pub viewport: Vec2, + pub(crate) stroke: Option, + /// Layer clipboard: a document snapshot and the copied layer. + pub clipboard: Option<(Document, LayerId)>, + /// Decoded image resources (rebuilt from `doc.resources` by `Session::sync_images`). + pub images: texturecraft_compose::ImageStore, + /// Temporary texel mask limiting paint strokes (not saved, not in the history). + pub quick_mask: crate::quick_mask::QuickMask, + pub path: Option, + pub modified: bool, + /// Bumps on every document change. + pub revision: u64, + /// Messages for the UI (status bar / toasts). + pub log: Vec, +} + +impl Session { + /// A session for `project` on `mesh` (texture sets must reference mesh materials). + pub fn new(doc: Document, mesh: Mesh, mesh_source: Option<(String, Vec)>) -> Result { + mesh.validate().map_err(|e| EngineError::Other(e.to_string()))?; + let bvh = Bvh::new(&mesh); + let mut camera = OrbitCamera { yaw: 0.6, pitch: 0.25, ..OrbitCamera::default() }; + let (lo, hi) = mesh.bounds(); + camera.frame(lo, hi); + let caches = doc.project.texture_sets.iter().map(|ts| SetCache::new(&mesh, ts)).collect(); + let selected = doc.project.texture_sets.iter().enumerate().filter_map(|(i, ts)| ts.stack.last().map(|id| (i, *id))).collect(); + static NEXT_MESH: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); + Ok(Session { + doc, + mesh_id: NEXT_MESH.fetch_add(1, std::sync::atomic::Ordering::Relaxed), + mesh: Arc::new(mesh), + bvh: Arc::new(bvh), + mesh_source, + bake_meshes: Default::default(), + caches, + undo: Vec::new(), + redo: Vec::new(), + tool: ToolState::default(), + active_set: 0, + selected, + target: StackTarget::Content, + camera, + view: ViewMode::Material, + viewport: Vec2::new(1280.0, 800.0), + stroke: None, + clipboard: None, + images: Default::default(), + quick_mask: Default::default(), + path: None, + modified: false, + revision: 1, + log: Vec::new(), + }) + } + + /// A new project: one texture set per mesh material, each with an empty paint layer. + pub fn new_project(name: &str, mesh: Mesh, mesh_ref: &str, mesh_source: Option<(String, Vec)>, resolution: u32) -> Result { + let res = check_resolution(resolution)?; + let mut project = Project::new(name, mesh_ref); + let mats = if mesh.materials.is_empty() { vec!["Default".to_string()] } else { mesh.materials.clone() }; + for (i, m) in mats.iter().enumerate() { + let mut ts = TextureSet::new(m.clone(), i, res); + let id = project.alloc_id(); + let layer = Layer::new(id, "Layer 1", LayerKind::Paint, &ts.channel_list()); + project.layers.insert(id, layer); + ts.stack.push(id); + project.texture_sets.push(ts); + } + Session::new(Document { project, pixels: PixelStore::default(), baked: BTreeMap::new(), resources: BTreeMap::new() }, mesh, mesh_source) + } + + pub fn project(&self) -> &Project { + &self.doc.project + } + + pub fn set_count(&self) -> usize { + self.doc.project.texture_sets.len() + } + + /// The active texture set index, clamped. + pub fn ts(&self) -> usize { + self.active_set.min(self.set_count().saturating_sub(1)) + } + + pub fn selected_layer(&self) -> Option { + let ts = self.ts(); + self.selected.get(&ts).copied().filter(|id| self.doc.project.locate(*id).is_some_and(|(t, _, _)| t == ts)) + } + + // ----------------------------------------------------------------------------------------- + // History + + /// Run an undoable edit. On error the document is left untouched. + pub fn edit(&mut self, label: &str, f: impl FnOnce(&mut Session) -> Result) -> Result { + let before = self.doc.clone(); + let result = f(self).and_then(|v| match self.doc.project.instance_cycle() { + Some(id) => Err(EngineError::Other(format!("instance layer {} would draw itself", id.0))), + None => Ok(v), + }); + match result { + Ok(v) => { + self.push_history(label, before); + Ok(v) + } + Err(e) => { + self.doc = before; + Err(e) + } + } + } + + pub(crate) fn push_history(&mut self, label: &str, before: Document) { + self.modified = true; + self.revision = self.revision.wrapping_add(1); + self.invalidate_changed(&before); + self.undo.push((label.to_string(), before)); + if self.undo.len() > MAX_HISTORY { + self.undo.remove(0); + } + self.redo.clear(); + } + + /// Mark dirty every texture set whose composite differs between `other` and the current + /// document (after an edit, undo or redo). + fn invalidate_changed(&mut self, other: &Document) { + self.sync_caches(); + let resources = |d: &Document| d.resources.iter().map(|(n, b)| (n.clone(), Arc::as_ptr(b))).collect::>(); + 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) + }; + for ts in 0..self.caches.len() { + if (all || key(other, ts) != key(&self.doc, ts)) + && let Some(c) = self.caches.get_mut(ts) + { + c.invalidate_all(); + } + } + } + + /// Mark the document changed and every texture set dirty. + pub fn touched(&mut self) { + self.modified = true; + self.revision = self.revision.wrapping_add(1); + self.sync_caches(); + for c in &mut self.caches { + c.invalidate_all(); + } + } + + /// Make caches agree with the project's texture sets (resolution or channel changes). + pub fn sync_caches(&mut self) { + let sets = self.doc.project.texture_sets.clone(); + self.caches.truncate(sets.len()); + for (i, ts) in sets.iter().enumerate() { + match self.caches.get_mut(i) { + Some(c) if c.resolution == ts.resolution => { + let want = ts.channel_list(); + if c.composite.channels.keys().copied().collect::>() != want { + c.composite = Composite::new(&want, ts.resolution); + c.upload.extend(texturecraft_compose::all_tiles(ts.resolution)); + c.invalidate_all(); + } + } + _ => { + let cache = SetCache::new(&self.mesh, ts); + if i < self.caches.len() { + self.caches[i] = cache; + } else { + self.caches.push(cache); + } + } + } + } + } + + pub fn can_undo(&self) -> bool { + !self.undo.is_empty() + } + pub fn can_redo(&self) -> bool { + !self.redo.is_empty() + } + pub fn undo_label(&self) -> Option<&str> { + self.undo.last().map(|(l, _)| l.as_str()) + } + pub fn redo_label(&self) -> Option<&str> { + self.redo.last().map(|(l, _)| l.as_str()) + } + pub fn history_labels(&self) -> Vec { + self.undo.iter().map(|(l, _)| l.clone()).collect() + } + + pub fn undo(&mut self) -> Result { + self.cancel_stroke(); + let (label, doc) = self.undo.pop().ok_or_else(|| EngineError::Other("nothing to undo".into()))?; + let now = std::mem::replace(&mut self.doc, doc); + self.after_history(&now); + self.redo.push((label.clone(), now)); + self.sync_images(); + Ok(label) + } + + pub fn redo(&mut self) -> Result { + self.cancel_stroke(); + let (label, doc) = self.redo.pop().ok_or_else(|| EngineError::Other("nothing to redo".into()))?; + let now = std::mem::replace(&mut self.doc, doc); + self.after_history(&now); + self.undo.push((label.clone(), now)); + self.sync_images(); + Ok(label) + } + + fn after_history(&mut self, other: &Document) { + self.modified = true; + self.revision = self.revision.wrapping_add(1); + self.invalidate_changed(other); + } + + // ----------------------------------------------------------------------------------------- + // Compositing + + /// Recomposite dirty tiles of every texture set (with the live stroke preview). Returns + /// whether anything changed. + pub fn update(&mut self) -> bool { + self.update_for(f64::INFINITY, || 0.0) + } + + /// [`Session::update`] within about `budget_ms` of `clock` (milliseconds): tiles go in + /// chunks sized from the time the previous chunk took, and what is left stays dirty for the + /// next call (at least one chunk runs). The UI calls this every frame so a full recomposite + /// of a large texture set spreads over frames instead of freezing. The active texture set + /// goes first. + pub fn update_for(&mut self, budget_ms: f64, clock: impl Fn() -> f64) -> bool { + let start = clock(); + let mut chunk = if budget_ms.is_finite() { rayon::current_num_threads().clamp(1, CHUNK) } else { CHUNK }; + // The layer being edited: the stack below it 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_res = self.stroke.as_ref().and_then(|s| self.caches.get(s.ts)).map(|c| c.resolution); + let preview = self.stroke.as_ref().and_then(|s| { + Some(StrokePreview { + target: s.target?, + alpha: s.canvas.alpha.clone(), + opacity: s.stroke.settings.opacity, + erase: s.erase, + values: s.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(), + }) + }); + let active = self.ts(); + let order = std::iter::once(active).chain((0..self.caches.len()).filter(|ts| *ts != active)); + let mut changed = false; + for ts in order { + let Some(cache) = self.caches.get_mut(ts) else { continue }; + cache.below.set_hot(hot); + cache.below.pass = cache.dirty.len(); + // Instances in sets of the stroke's size show the live stroke too. + let preview = preview.as_ref().filter(|_| Some(cache.resolution) == stroke_res); + while !cache.dirty.is_empty() { + let t0 = clock(); + let tiles = next_chunk(&mut cache.dirty, chunk); + let empty = BakedMaps::default(); + let inputs = texturecraft_compose::Inputs { + project: &self.doc.project, + ts, + mesh: &self.mesh, + gbuf: &cache.gbuf, + pixels: &self.doc.pixels, + baked: self.doc.baked.get(&ts).unwrap_or(&empty), + preview, + images: &self.images, + bounds: self.mesh.bounds(), + }; + texturecraft_compose::composite_tiles_cached(&inputs, &mut cache.composite, &tiles, Some(&mut cache.below)); + // 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); + if left <= 0.0 { + return changed; + } + // 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; + let fit = if per_tile > 0.0 { (left / per_tile) as usize } else { CHUNK }; + if fit == 0 { + return changed; + } + chunk = fit.min(CHUNK); + } + } + changed + } + + /// Tiles still waiting to be composited, over every texture set. + pub fn pending_tiles(&self) -> usize { + self.caches.iter().map(|c| c.dirty.len()).sum() + } + + /// The layer a stroke target belongs to (an effect's owner). + pub(crate) fn paint_owner(&self, t: PaintTarget) -> LayerId { + let (PaintTarget::Layer(id) | PaintTarget::Mask(id)) = t; + self.doc.project.effect(id).map_or(id, |(owner, _)| owner) + } + + /// Mark `tiles` of texture set `ts` dirty after painting `target`, and the same tiles of + /// other sets of that size drawing it through an instance. + pub(crate) fn mark_painted(&mut self, ts: usize, target: Option, tiles: impl IntoIterator) { + let res = self.caches.get(ts).map(|c| c.resolution); + let sets = target.map(|t| self.doc.project.sets_drawing(self.paint_owner(t))).unwrap_or_default(); + let tiles: Vec<(u32, u32)> = tiles.into_iter().collect(); + for (i, c) in self.caches.iter_mut().enumerate() { + if i == ts || (sets.contains(&i) && Some(c.resolution) == res) { + c.dirty.extend(tiles.iter().copied()); + } + } + } + + /// Drain tiles needing upload for texture set `ts`. + pub fn take_uploads(&mut self, ts: usize) -> Vec<(u32, u32)> { + self.caches.get_mut(ts).map(|c| std::mem::take(&mut c.upload).into_iter().collect()).unwrap_or_default() + } + + /// Grayscale RGBA8 of layer `id`'s mask at `size²` for the mask view (None without a mask). + pub fn mask_view_rgba(&self, id: LayerId, size: u32) -> Option> { + let (ts, _, _) = self.doc.project.locate(id)?; + let cache = self.caches.get(ts)?; + let empty = BakedMaps::default(); + let inputs = texturecraft_compose::Inputs { + project: &self.doc.project, + ts, + mesh: &self.mesh, + gbuf: &cache.gbuf, + pixels: &self.doc.pixels, + baked: self.doc.baked.get(&ts).unwrap_or(&empty), + preview: None, + images: &self.images, + bounds: self.mesh.bounds(), + }; + let gray = texturecraft_compose::mask_image(&inputs, id, size)?; + Some(gray.iter().flat_map(|g| [*g, *g, *g, 255]).collect()) + } + + pub fn baked(&self, ts: usize) -> Option<&BakedMaps> { + self.doc.baked.get(&ts) + } + + /// Bytes held by painted layer pixels, the composites of every texture set and their + /// below-stack caches. + pub fn memory(&self) -> Memory { + let composites = self.caches.iter().flat_map(|c| c.composite.channels.values()).map(texturecraft_texel::TileImage::bytes).sum(); + Memory { pixels: self.doc.pixels.bytes(), composites, cache: self.caches.iter().map(|c| c.below.bytes()).sum() } + } +} + +/// Memory use reported by [`Session::memory`]. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Memory { + pub pixels: usize, + pub composites: usize, + pub cache: usize, +} + +impl Memory { + pub fn total(self) -> usize { + self.pixels + self.composites + self.cache + } +} + +/// Most tiles per composite chunk (a 1024² region). +const CHUNK: usize = 256; + +/// 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 { + 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); + } + tiles +} + +/// Valid texture set resolutions: powers of two from 128 to 8192. +pub fn check_resolution(r: u32) -> Result { + if (128..=8192).contains(&r) && r.is_power_of_two() { + Ok(r) + } else { + Err(EngineError::Other(format!("resolution {r} must be a power of two between 128 and 8192"))) + } +} diff --git a/crates/engine/src/set_cmds.rs b/crates/engine/src/set_cmds.rs new file mode 100644 index 00000000..0e9a31c6 --- /dev/null +++ b/crates/engine/src/set_cmds.rs @@ -0,0 +1,127 @@ +//! Texture set commands (Texture Set List / Texture Set Settings). + +use serde_json::{Value, json}; +use texturecraft_core::{Channel, ChannelBlend, ChannelFormat, NormalFormat}; + +use crate::commands::{CommandSpec, always, bad, boolp, channel_param, f32p, strp, u64p}; +use crate::session::{StackTarget, check_resolution}; +use crate::{Result, Session, cmd}; + +pub fn specs() -> Vec { + vec![ + cmd!("textureSet.select", "Select texture set", [], None, "{ts}", always, select), + cmd!("textureSet.list", "List texture sets", [], None, "{}", always, |s, _| Ok(crate::inspect::texture_sets(s))), + cmd!("textureSet.resolution", "Texture set size", [], None, "{ts?, resolution: 128..8192 (power of two)}", always, resolution), + cmd!("textureSet.channel.add", "Add channel", [], None, "{ts?, channel}", always, |s, p| channel(s, p, true)), + cmd!("textureSet.channel.remove", "Remove channel", [], None, "{ts?, channel}", always, |s, p| channel(s, p, false)), + cmd!("textureSet.visible", "Toggle texture set visibility", [], None, "{ts?, visible?}", always, visible), + cmd!("textureSet.rename", "Rename texture set", [], None, "{ts?, name}", always, rename), + cmd!("textureSet.settings", "Texture set settings", [], None, "{ts?, description?, heightStrength?, normalFormat?: openGl|directX}", always, settings), + ] +} + +fn ts_param(s: &Session, p: &Value, cmd: &str) -> Result { + match u64p(p, "ts") { + Some(t) if (t as usize) < s.set_count() => Ok(t as usize), + Some(t) => Err(bad(cmd, format!("no texture set {t}"))), + None => Ok(s.ts()), + } +} + +fn select(s: &mut Session, p: &Value) -> Result { + let ts = u64p(p, "ts").map(|t| t as usize).filter(|t| *t < s.set_count()).ok_or_else(|| bad("textureSet.select", "valid `ts` index required"))?; + s.cancel_stroke(); + s.active_set = ts; + s.target = StackTarget::Content; + s.revision = s.revision.wrapping_add(1); + Ok(json!({ "ts": ts })) +} + +fn resolution(s: &mut Session, p: &Value) -> Result { + let ts = ts_param(s, p, "textureSet.resolution")?; + let r = u64p(p, "resolution").ok_or_else(|| bad("textureSet.resolution", "`resolution` is required"))?; + let r = check_resolution(u32::try_from(r).unwrap_or(0))?; + s.edit("Texture set size", |s| { + s.doc.project.texture_set_mut(ts)?.resolution = r; + // Painted pixels are resampled (bilinear); strokes are not replayed. + crate::layer_ops::resample_set(s, ts, r); + Ok(()) + })?; + Ok(json!({ "ts": ts, "resolution": r })) +} + +fn channel(s: &mut Session, p: &Value, add: bool) -> Result { + let ts = ts_param(s, p, "textureSet.channel")?; + let c = channel_param("textureSet.channel", p, "channel")?.ok_or_else(|| bad("textureSet.channel", "`channel` is required"))?; + s.edit(if add { "Add channel" } else { "Remove channel" }, |s| { + let set = s.doc.project.texture_set_mut(ts)?; + if add { + if !set.has_channel(c) { + set.channels.push((c, ChannelFormat::default_for(c))); + set.channels.sort_by_key(|(c, _)| c.index()); + } + // Existing layers get the new channel enabled. + let ids: Vec<_> = s.doc.project.walk(ts).into_iter().map(|(id, _)| id).collect(); + for id in ids { + if let Ok(l) = s.doc.project.layer_mut(id) { + let mode = if l.is_group() { texturecraft_core::BlendMode::Passthrough } else { texturecraft_core::BlendMode::Normal }; + l.channels.entry(c).or_insert(ChannelBlend { mode, opacity: 1.0 }); + } + } + } else { + if c == Channel::BaseColor { + return Err(bad("textureSet.channel.remove", "base color can't be removed")); + } + set.channels.retain(|(x, _)| *x != c); + } + Ok(()) + })?; + Ok(Value::Null) +} + +fn visible(s: &mut Session, p: &Value) -> Result { + let ts = ts_param(s, p, "textureSet.visible")?; + let set = s.doc.project.texture_set_mut(ts)?; + set.visible = boolp(p, "visible").unwrap_or(!set.visible); + let v = set.visible; + s.revision = s.revision.wrapping_add(1); + Ok(json!({ "visible": v })) +} + +fn rename(s: &mut Session, p: &Value) -> Result { + let ts = ts_param(s, p, "textureSet.rename")?; + let name = strp(p, "name") + .map(str::trim) + .filter(|n| !n.is_empty() && n.chars().count() <= 128) + .ok_or_else(|| bad("textureSet.rename", "`name` is required"))? + .to_string(); + s.edit("Rename texture set", |s| { + s.doc.project.texture_set_mut(ts)?.name = name; + Ok(()) + })?; + Ok(Value::Null) +} + +fn settings(s: &mut Session, p: &Value) -> Result { + let ts = ts_param(s, p, "textureSet.settings")?; + let nf = match strp(p, "normalFormat") { + Some("openGl") | Some("opengl") => Some(NormalFormat::OpenGl), + Some("directX") | Some("directx") => Some(NormalFormat::DirectX), + Some(o) => return Err(bad("textureSet.settings", format!("unknown normal format `{o}`"))), + None => None, + }; + s.edit("Texture set settings", |s| { + if let Some(n) = nf { + s.doc.project.normal_format = n; + } + let set = s.doc.project.texture_set_mut(ts)?; + if let Some(d) = strp(p, "description") { + set.description = d.chars().take(4096).collect(); + } + if let Some(h) = f32p(p, "heightStrength") { + set.height_strength = h.clamp(0.0, 100.0); + } + Ok(()) + })?; + Ok(Value::Null) +} diff --git a/crates/engine/src/smart_masks.rs b/crates/engine/src/smart_masks.rs new file mode 100644 index 00000000..346ec944 --- /dev/null +++ b/crates/engine/src/smart_masks.rs @@ -0,0 +1,152 @@ +//! Smart masks: procedural mask presets (generators and patterns driven by the baked mesh maps) +//! that replace a layer's mask in one step. Smart materials build their masks from the same +//! presets. + +use serde_json::{Value, json}; +use texturecraft_core::{ + BlendMode, Effect, EffectKind, Filter, Generator, GeneratorKind, LayerId, Mask, MaskBackground, Procedural, ProceduralKind, Project, Projection, Source, +}; + +use crate::commands::{CommandSpec, always, bad, has_layer, layer_param, strp}; +use crate::session::StackTarget; +use crate::{EngineError, Result, Session, cmd}; + +pub fn specs() -> Vec { + vec![ + cmd!("mask.smart.apply", "Apply smart mask", [], None, "{name: smart mask id or label, layer?}", has_layer, apply_cmd), + cmd!("mask.smart.list", "List smart masks", [], None, "{}", always, |_, _| Ok(json!( + SMART_MASKS.iter().map(|m| json!({"id": m.id, "label": m.label})).collect::>() + ))), + ] +} + +pub struct SmartMask { + pub id: &'static str, + pub label: &'static str, +} + +pub const SMART_MASKS: &[SmartMask] = &[ + SmartMask { id: "edge_wear", label: "Edges Worn" }, + SmartMask { id: "edge_chipped", label: "Edges Chipped" }, + SmartMask { id: "dirt_cavities", label: "Dirt in Cavities" }, + SmartMask { id: "dust_top", label: "Dust from Top" }, + SmartMask { id: "ground_dirt", label: "Ground Dirt" }, + SmartMask { id: "rust_leaks", label: "Rust Leaks" }, + SmartMask { id: "scratches", label: "Scratches" }, + SmartMask { id: "grunge", label: "Grunge Blotches" }, + SmartMask { id: "paint_chips", label: "Paint Chips" }, + SmartMask { id: "fingerprints", label: "Fingerprints" }, +]; + +fn norm(x: &str) -> String { + x.to_ascii_lowercase().replace([' ', '_', '-'], "") +} + +pub fn find(name: &str) -> Option<&'static SmartMask> { + SMART_MASKS.iter().find(|m| norm(m.id) == norm(name) || norm(m.label) == norm(name)) +} + +const UP: [f32; 3] = [0.0, 1.0, 0.0]; + +/// A mesh-map generator with explicit threshold, softness and noise breakup. +pub(crate) fn generator(kind: GeneratorKind, level: f32, softness: f32, noise: f32) -> EffectKind { + EffectKind::Generator(Generator { level, softness, noise_amount: noise, ..Generator::new(kind) }) +} + +/// A direction or position generator (`invert` flips the result). +pub(crate) fn directional(kind: GeneratorKind, direction: [f32; 3], level: f32, softness: f32, noise: f32, invert: bool) -> EffectKind { + EffectKind::Generator(Generator { level, softness, noise_amount: noise, direction, invert, ..Generator::new(kind) }) +} + +/// A triplanar grayscale pattern fill. +pub(crate) fn pattern(kind: ProceduralKind, scale: f32, seed: u32, contrast: f32) -> EffectKind { + EffectKind::Fill { + source: Source::Procedural { pattern: Procedural { kind, scale, seed, contrast, balance: 0.0, invert: false }, low: [0.0; 3], high: [1.0; 3] }, + projection: Projection::Triplanar { scale: 1.0, sharpness: 8.0, offset: [0.0; 3] }, + } +} + +/// Background and effects (bottom → top) of preset `id`. +pub(crate) fn preset(id: &str) -> Option<(MaskBackground, Vec<(EffectKind, BlendMode)>)> { + use BlendMode::*; + use GeneratorKind as G; + use ProceduralKind as P; + let fx = match find(id)?.id { + "edge_wear" => vec![(generator(G::EdgeWear, 0.55, 0.12, 0.5), Normal)], + // Wide edge band cut by chips, plus a thin solid line right on the edge. + "edge_chipped" => vec![ + (generator(G::EdgeWear, 0.8, 0.2, 0.3), Normal), + (pattern(P::Chips, 3.0, 5, 1.0), Multiply), + (generator(G::EdgeWear, 0.45, 0.06, 0.6), Lighten), + ], + "dirt_cavities" => vec![(generator(G::Dirt, 0.55, 0.25, 0.4), Normal)], + "dust_top" => vec![(directional(G::Direction, UP, 0.3, 0.15, 0.3, false), Normal), (pattern(P::Clouds, 3.0, 9, 1.4), Multiply)], + "ground_dirt" => vec![(directional(G::Position, [0.0, -1.0, 0.0], 0.35, 0.25, 0.5, false), Normal), (pattern(P::Grunge, 2.0, 12, 0.8), Multiply)], + // Streaks on surfaces that don't face up, fed by the crevices they run out of. + "rust_leaks" => vec![ + (pattern(P::Streaks, 3.0, 4, 1.2), Normal), + (directional(G::Direction, UP, 0.2, 0.1, 0.0, true), Multiply), + (generator(G::Dirt, 0.45, 0.2, 0.5), Lighten), + ], + "scratches" => vec![(pattern(P::Scratches, 3.0, 6, 1.0), Normal)], + "grunge" => vec![(pattern(P::Grunge, 2.0, 8, 1.0), Normal)], + "paint_chips" => vec![(pattern(P::Chips, 5.0, 2, 1.0), Normal), (generator(G::EdgeWear, 0.6, 0.12, 0.5), Lighten)], + "fingerprints" => vec![(pattern(P::Smudges, 4.0, 3, 1.0), Normal)], + _ => return None, + }; + Some((MaskBackground::Black, fx)) +} + +/// Build a mask from a background and effects, allocating effect ids in `p`. +pub(crate) fn build_mask(p: &mut Project, bg: MaskBackground, effects: Vec<(EffectKind, BlendMode)>) -> Mask { + let mut m = Mask::new(bg); + for (k, blend) in effects { + m.effects.push(Effect { blend, ..Effect::new(p.alloc_id(), k) }); + } + m +} + +/// Preset `id` as a mask, optionally inverted (e.g. paint that survives where wear is not). +pub(crate) fn mask(p: &mut Project, id: &str, invert: bool) -> Result { + let (bg, mut fx) = preset(id).ok_or_else(|| EngineError::Other(format!("unknown smart mask `{id}`")))?; + if invert { + fx.push((EffectKind::Filter(Filter::Invert), BlendMode::Normal)); + } + Ok(build_mask(p, bg, fx)) +} + +/// Replace layer `layer`'s mask with smart mask `name` (one undo step). +pub fn apply(s: &mut Session, layer: LayerId, name: &str) -> Result<()> { + let sm = find(name).ok_or_else(|| EngineError::Other(format!("unknown smart mask `{name}`")))?; + s.edit(&format!("Apply smart mask {}", sm.label), |s| { + let m = mask(&mut s.doc.project, sm.id, false)?; + let old = s.doc.project.layer_mut(layer)?.mask.replace(m); + for e in old.map(|m| m.effects).unwrap_or_default() { + s.doc.pixels.masks.remove(&e.id); + } + s.target = StackTarget::Mask; + Ok(()) + }) +} + +fn apply_cmd(s: &mut Session, p: &Value) -> Result { + let name = strp(p, "name").ok_or_else(|| bad("mask.smart.apply", "`name` is required"))?; + let layer = layer_param(s, "mask.smart.apply", p)?; + apply(s, layer, name)?; + Ok(json!({ "layer": layer.0 })) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_preset_resolves_and_ids_are_unique() { + let (mut ids, mut labels) = (std::collections::BTreeSet::new(), std::collections::BTreeSet::new()); + for m in SMART_MASKS { + assert!(preset(m.id).is_some(), "{}", m.id); + assert!(ids.insert(norm(m.id)) && labels.insert(norm(m.label)), "duplicate {}", m.id); + assert_eq!(find(m.label).map(|f| f.id), Some(m.id)); + } + } +} diff --git a/crates/engine/src/tool_cmds.rs b/crates/engine/src/tool_cmds.rs new file mode 100644 index 00000000..783f269d --- /dev/null +++ b/crates/engine/src/tool_cmds.rs @@ -0,0 +1,322 @@ +//! Tool commands: pick a tool, set brush and material values, and paint scripted strokes (the +//! way agents and the CLI paint). + +use serde_json::{Value, json}; +use texturecraft_core::Channel; +use texturecraft_geom::glam::Vec2; +use texturecraft_paint::{Alignment, BrushAlpha}; + +use crate::commands::{CommandSpec, always, bad, boolp, channel_param, color_param, f32p, strp}; +use crate::session::{CloneSource, FillMode, Tool}; +use crate::{Result, Session, cmd}; + +pub fn specs() -> Vec { + vec![ + cmd!("tool.paint", "Paint", ["Tools"], Some("1"), "{}", always, |s, _| set_tool(s, Tool::Paint)), + cmd!("tool.eraser", "Eraser", ["Tools"], Some("2"), "{}", always, |s, _| set_tool(s, Tool::Eraser)), + cmd!("tool.projection", "Projection", ["Tools"], Some("3"), "{}", always, |s, _| set_tool(s, Tool::Projection)), + 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.select", "Select tool", [], None, "{tool: paint|eraser|projection|polygonFill|smudge|clone|colorPicker|path}", always, select), + cmd!( + "tool.brush", + "Brush settings", + [], + None, + "{size?: 0..100, flow?: 0..100, opacity?: 0..100, spacing?: 1..1000, hardness?: 0..100, angle?, followPath?, alignment?: tangent|camera, lazy?, jitterSize?, jitterAngle?, jitterFlow?, jitterPosition?, alpha?: name | {image: resource name}, backfaceCulling?, symmetryX?, symmetryY?, symmetryZ?, minSize?, smudgeStrength?: 0..100, particles?: {enabled?, count?: 1..16, lifetime?: 1..128 steps, gravity?: 0..2}}", + always, + brush + ), + cmd!("tool.material", "Tool material", [], None, "{channel, color?|value?, enabled?} or {maskValue?: 0..1}", always, material), + cmd!("tool.stencil", "Projection stencil", [], None, "{image?: resource name, center?: [x,y] NDC, scale?: 0.01..10, rotation?: degrees, tile?, opacity?, clear?: bool}", always, stencil), + cmd!( + "tool.cloneSource", + "Set clone source", + [], + None, + "{x, y, viewport?: [w,h]} a point on the mesh, or {uv: [u,v]}, or {clear: true} — the Clone tool copies from here (V + click)", + always, + clone_source + ), + cmd!("tool.fillMode", "Polygon fill mode", [], None, "{mode: triangle|uvIsland|mesh}", always, fill_mode), + cmd!("tool.stroke", "Paint stroke", [], None, "{points: [[x,y],...] viewport pixels, viewport?: [w,h]} — paints with the current tool", always, stroke), + cmd!("tool.strokeUv", "Paint stroke in the 2D view", [], None, "{points: [[u,v],...] texture coordinates 0..1} — paints in UV space like the 2D view", always, stroke_uv), + cmd!("tool.click", "Tool click", [], None, "{x, y, viewport?: [w,h]} — polygon fill / color picker / single dab", always, click), + cmd!("tool.state", "Tool state", [], None, "{}", always, |s, _| Ok(serde_json::to_value(&s.tool).unwrap_or(Value::Null))), + ] +} + +pub(crate) fn set_tool(s: &mut Session, t: Tool) -> Result { + s.cancel_stroke(); + s.tool.tool = t; + s.revision = s.revision.wrapping_add(1); + Ok(json!({ "tool": t.id() })) +} + +fn select(s: &mut Session, p: &Value) -> Result { + let name = strp(p, "tool").ok_or_else(|| bad("tool.select", "`tool` is required"))?; + let t = Tool::parse(name).ok_or_else(|| bad("tool.select", format!("unknown tool `{name}`")))?; + set_tool(s, t) +} + +/// Brush values use the UI's 0–100 scales, so agents and the sliders speak the same numbers. +fn brush(s: &mut Session, p: &Value) -> Result { + let b = &mut s.tool.brush; + let pct = |k: &str| f32p(p, k).map(|v| v / 100.0); + if let Some(v) = pct("size") { + b.size = v * 0.25; + } + if let Some(v) = pct("minSize") { + b.min_size = v; + } + if let Some(v) = pct("flow") { + b.flow = v; + } + if let Some(v) = pct("opacity") { + b.opacity = v; + } + if let Some(v) = pct("spacing") { + b.spacing = v; + } + if let Some(v) = pct("hardness") { + b.hardness = v; + } + if let Some(v) = f32p(p, "angle") { + b.angle = v; + } + if let Some(v) = boolp(p, "followPath") { + b.follow_path = v; + } + if let Some(a) = strp(p, "alignment") { + b.alignment = match a { + "camera" => Alignment::Camera, + "tangent" | "uv" => Alignment::Tangent, + other => return Err(bad("tool.brush", format!("unknown alignment `{other}`"))), + }; + } + if let Some(v) = f32p(p, "lazy") { + b.lazy = v; + } + for (k, slot) in [("jitterSize", &mut b.jitter_size), ("jitterAngle", &mut b.jitter_angle), ("jitterFlow", &mut b.jitter_flow)] { + if let Some(v) = f32p(p, k) { + *slot = v / 100.0; + } + } + if let Some(v) = f32p(p, "jitterPosition") { + b.jitter_position = v / 100.0; + } + if let Some(a) = strp(p, "alpha") { + b.alpha = BrushAlpha::parse(a).ok_or_else(|| bad("tool.brush", format!("unknown alpha `{a}`")))?; + b.alpha_image = None; + } else if let Some(name) = p.get("alpha").and_then(|a| a.get("image")).and_then(Value::as_str) { + if !s.images.contains_key(name) { + return Err(bad("tool.brush", format!("no image resource `{name}` (import it first)"))); + } + b.alpha_image = Some(name.to_string()); + } + if let Some(v) = boolp(p, "backfaceCulling") { + b.backface_culling = v; + } + for (k, slot) in [("symmetryX", &mut b.symmetry.x), ("symmetryY", &mut b.symmetry.y), ("symmetryZ", &mut b.symmetry.z)] { + if let Some(v) = boolp(p, k) { + *slot = v; + } + } + if let Some(v) = boolp(p, "screenSpace") { + b.screen_space = v; + } + if let Some(q) = p.get("particles") { + let pt = &mut b.particles; + pt.enabled = boolp(q, "enabled").unwrap_or(pt.enabled); + pt.count = f32p(q, "count").map_or(pt.count, |v| v as u32); + pt.lifetime = f32p(q, "lifetime").map_or(pt.lifetime, |v| v as u32); + pt.gravity = f32p(q, "gravity").unwrap_or(pt.gravity); + } + *b = b.sanitized(); + if let Some(v) = f32p(p, "smudgeStrength") { + s.tool.smudge_strength = (v / 100.0).clamp(0.0, 1.0); + } + s.revision = s.revision.wrapping_add(1); + Ok(brush_json(s)) +} + +pub(crate) fn brush_json(s: &Session) -> Value { + let b = &s.tool.brush; + json!({ + "size": b.size / 0.25 * 100.0, "minSize": b.min_size * 100.0, "flow": b.flow * 100.0, "opacity": b.opacity * 100.0, + "spacing": b.spacing * 100.0, "hardness": b.hardness * 100.0, "angle": b.angle, "followPath": b.follow_path, + "alignment": format!("{:?}", b.alignment).to_lowercase(), "lazy": b.lazy, + "alpha": match &b.alpha_image { Some(name) => json!({ "image": name }), None => json!(b.alpha.label()) }, + "backfaceCulling": b.backface_culling, "symmetry": [b.symmetry.x, b.symmetry.y, b.symmetry.z], + "smudgeStrength": s.tool.smudge_strength * 100.0, "particles": b.particles, + }) +} + +fn material(s: &mut Session, p: &Value) -> Result { + 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")) { + 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); + } + match boolp(p, "enabled") { + Some(true) => { + s.tool.enabled.insert(c); + } + Some(false) => { + s.tool.enabled.remove(&c); + } + None => {} + } + } + s.revision = s.revision.wrapping_add(1); + Ok(json!({ + "values": s.tool.values.iter().map(|(c, v)| (c.id().to_string(), json!(v))).collect::>(), + "enabled": s.tool.enabled.iter().map(|c| c.id()).collect::>(), + "maskValue": s.tool.mask_value, + })) +} + +fn stencil(s: &mut Session, p: &Value) -> Result { + if boolp(p, "clear") == Some(true) { + s.tool.stencil = None; + return Ok(Value::Null); + } + if let Some(name) = strp(p, "image") { + if !s.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 }; + } + s.tool.stencil = Some(st); + } + 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)); + } + if let Some(v) = f32p(p, "scale") { + st.scale = v.clamp(0.01, 10.0); + } + if let Some(v) = f32p(p, "rotation") { + st.rotation = v.rem_euclid(360.0); + } + if let Some(v) = boolp(p, "tile") { + st.tile = v; + } + if let Some(v) = f32p(p, "opacity") { + st.opacity = v.clamp(0.0, 1.0); + } + s.revision = s.revision.wrapping_add(1); + Ok(serde_json::to_value(&s.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 = 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, + } +} + +fn clone_source(s: &mut Session, p: &Value) -> Result { + if boolp(p, "clear") == Some(true) { + s.tool.clone_source = None; + return Ok(Value::Null); + } + let uv = match (pair(p, "uv"), f32p(p, "x"), f32p(p, "y")) { + (Some(uv), _, _) => uv.map(|v| v.clamp(0.0, 1.0)), + (None, Some(x), Some(y)) => { + let hit = s.pick(Vec2::new(x, y), viewport_of(s, p)).ok_or_else(|| bad("tool.cloneSource", "nothing under that point"))?; + if s.doc.project.texture_set(s.ts()).ok().map(|t| t.material) != Some(hit.material) { + return Err(bad("tool.cloneSource", "that surface belongs to another texture set")); + } + [hit.uv.x, hit.uv.y] + } + _ => return Err(bad("tool.cloneSource", "give `uv: [u, v]` or `x` and `y`")), + }; + s.tool.clone_source = Some(CloneSource { uv, offset: None }); + s.revision = s.revision.wrapping_add(1); + Ok(serde_json::to_value(s.tool.clone_source).unwrap_or(Value::Null)) +} + +fn fill_mode(s: &mut Session, p: &Value) -> Result { + s.tool.fill_mode = match strp(p, "mode").unwrap_or("triangle") { + "triangle" => FillMode::Triangle, + "uvIsland" | "island" => FillMode::UvIsland, + "mesh" => FillMode::Mesh, + other => return Err(bad("tool.fillMode", format!("unknown fill mode `{other}`"))), + }; + Ok(Value::Null) +} + +pub(crate) fn viewport_of(s: &Session, p: &Value) -> Vec2 { + match p.get("viewport").and_then(Value::as_array) { + Some(a) if a.len() == 2 => { + let w = a[0].as_f64().unwrap_or(0.0) as f32; + let h = a[1].as_f64().unwrap_or(0.0) as f32; + if w.is_finite() && h.is_finite() && w >= 16.0 && h >= 16.0 { Vec2::new(w.min(16384.0), h.min(16384.0)) } else { s.viewport } + } + _ => s.viewport, + } +} + +/// A `[[x, y], ...]` param of at most 100000 finite points, None when the key is absent. +pub(crate) fn points_param(cmd: &str, p: &Value, key: &str) -> Result>> { + let Some(v) = p.get(key) else { return Ok(None) }; + let pts = v.as_array().ok_or_else(|| bad(cmd, format!("`{key}` must be an array of [x, y]")))?; + if pts.len() > 100_000 { + return Err(bad(cmd, "at most 100000 points")); + } + let points: Vec = pts + .iter() + .filter_map(|v| { + let a = v.as_array()?; + let (x, y) = (a.first()?.as_f64()? as f32, a.get(1)?.as_f64()? as f32); + (x.is_finite() && y.is_finite()).then_some(Vec2::new(x, y)) + }) + .collect(); + if points.is_empty() { + return Err(bad(cmd, "no valid points")); + } + Ok(Some(points)) +} + +fn stroke(s: &mut Session, p: &Value) -> Result { + let points = points_param("tool.stroke", p, "points")?.ok_or_else(|| bad("tool.stroke", "`points` must be an array of [x, y]"))?; + let vp = viewport_of(s, p); + let stamps = s.stroke_points(&points, vp)?; + s.update(); + Ok(json!({ "stamps": stamps })) +} + +fn click(s: &mut Session, p: &Value) -> Result { + let (Some(x), Some(y)) = (f32p(p, "x"), f32p(p, "y")) else { return Err(bad("tool.click", "`x` and `y` are required")) }; + let vp = viewport_of(s, p); + let out = match s.tool.tool { + Tool::PolygonFill => json!({ "triangles": s.polygon_fill(Vec2::new(x, y), vp)? }), + Tool::ColorPicker => { + let v = s.color_pick(Vec2::new(x, y), vp)?; + json!(v.iter().map(|(c, v)| (c.id().to_string(), json!(v))).collect::>()) + } + _ => json!({ "stamps": s.stroke_points(&[Vec2::new(x, y)], vp)? }), + }; + s.update(); + Ok(out) +} + +fn stroke_uv(s: &mut Session, p: &Value) -> Result { + let points = points_param("tool.strokeUv", p, "points")?.ok_or_else(|| bad("tool.strokeUv", "`points` must be an array of [u, v]"))?; + let tiles = s.stroke_uv_points(&points)?; + s.update(); + Ok(json!({ "tiles": tiles })) +} diff --git a/crates/engine/src/view_cmds.rs b/crates/engine/src/view_cmds.rs new file mode 100644 index 00000000..994e8ac1 --- /dev/null +++ b/crates/engine/src/view_cmds.rs @@ -0,0 +1,227 @@ +//! Edit, viewport and display commands. + +use serde_json::{Value, json}; +use texturecraft_core::{BakedMap, Channel, ToneMapping}; +use texturecraft_geom::glam::Vec3; + +use crate::commands::{CommandSpec, always, bad, boolp, can_redo, can_undo, f32p, strp}; +use crate::session::ViewMode; +use crate::{Result, Session, cmd}; + +pub fn specs() -> Vec { + vec![ + cmd!("viewport.frame", "Show entire mesh", ["Viewport"], Some("F"), "{}", always, frame), + cmd!("viewport.displayMaterial", "Display material", ["Viewport"], Some("M"), "{}", always, |s, _| set_view(s, ViewMode::Material)), + cmd!("viewport.nextChannel", "Display next channel", ["Viewport"], Some("C"), "{}", always, |s, _| cycle_channel(s, 1)), + cmd!("viewport.previousChannel", "Display previous channel", ["Viewport"], Some("Shift+C"), "{}", always, |s, _| cycle_channel(s, -1)), + cmd!("viewport.nextMeshMap", "Display next mesh map", ["Viewport"], Some("B"), "{}", always, |s, _| cycle_map(s, 1)), + cmd!("viewport.previousMeshMap", "Display previous mesh map", ["Viewport"], Some("Shift+B"), "{}", always, |s, _| cycle_map(s, -1)), + cmd!("viewport.view", "Set view mode", [], None, "{mode: material|uvChecker|mask||map:, layer? (for mask)}", always, view), + cmd!("camera.set", "Set camera", [], None, "{yaw?, pitch? (degrees), distance?, fov?, target?: [x,y,z], ortho?}", always, camera_set), + cmd!("camera.get", "Get camera", [], None, "{}", always, |s, _| Ok(camera_json(s))), + cmd!("camera.orbit", "Orbit camera", [], None, "{dx?, dy? (degrees)}", always, orbit), + cmd!( + "display.set", + "Display settings", + [], + None, + "{environment?: |resource:, exposure?, blur?, rotation?, opacity?, toneMapping?: linear|aces, fov?, shadows?, aoIntensity?, bloom?, vignette?, sharpen?, grain?, postEffects?, taa?, specGloss?, parallax?}", + always, + display + ), + ] +} + +pub(crate) fn edit_specs() -> Vec { + vec![ + cmd!("edit.undo", "Undo", ["Edit"], Some("Ctrl+Z"), "{}", can_undo, |s, _| Ok(json!({ "undone": s.undo()? }))), + cmd!("edit.redo", "Redo", ["Edit"], Some("Ctrl+Y"), "{}", can_redo, |s, _| Ok(json!({ "redone": s.redo()? }))), + cmd!("edit.history", "History", [], None, "{}", always, |s, _| Ok( + json!({ "undo": s.history_labels(), "redo": s.redo.iter().rev().map(|(l, _)| l.clone()).collect::>() }) + )), + ] +} + +fn frame(s: &mut Session, _: &Value) -> Result { + let (lo, hi) = s.mesh.bounds(); + s.camera.frame(lo, hi); + s.revision = s.revision.wrapping_add(1); + Ok(camera_json(s)) +} + +fn set_view(s: &mut Session, v: ViewMode) -> Result { + s.view = v; + s.revision = s.revision.wrapping_add(1); + Ok(json!({ "view": v.label() })) +} + +fn cycle_channel(s: &mut Session, d: i64) -> Result { + let chans = s.doc.project.texture_set(s.ts()).map(|t| t.channel_list()).unwrap_or_default(); + if chans.is_empty() { + return set_view(s, ViewMode::Material); + } + let n = chans.len() as i64; + let i = match s.view { + ViewMode::Channel(c) => chans.iter().position(|x| *x == c).map(|i| (i as i64 + d).rem_euclid(n)).unwrap_or(0), + _ => { + if d > 0 { + 0 + } else { + n - 1 + } + } + }; + set_view(s, ViewMode::Channel(chans[i as usize])) +} + +fn cycle_map(s: &mut Session, d: i64) -> Result { + let maps = BakedMap::ALL; + let n = maps.len() as i64; + let i = match s.view { + ViewMode::MeshMap(m) => maps.iter().position(|x| *x == m).map(|i| (i as i64 + d).rem_euclid(n)).unwrap_or(0), + _ => { + if d > 0 { + 0 + } else { + n - 1 + } + } + }; + set_view(s, ViewMode::MeshMap(maps[i as usize])) +} + +fn view(s: &mut Session, p: &Value) -> Result { + let m = strp(p, "mode").ok_or_else(|| bad("viewport.view", "`mode` is required"))?; + let v = match m { + "material" => ViewMode::Material, + "uvChecker" | "uv" | "checker" => ViewMode::UvChecker, + "mask" => { + let id = crate::commands::layer_param(s, "viewport.view", p)?; + if s.doc.project.layer(id)?.mask.is_none() { + return Err(bad("viewport.view", "the layer has no mask")); + } + ViewMode::Mask(id) + } + _ => match m.strip_prefix("map:") { + Some(id) => ViewMode::MeshMap(BakedMap::parse(id).ok_or_else(|| bad("viewport.view", format!("unknown mesh map `{id}`")))?), + None => ViewMode::Channel(Channel::parse(m).ok_or_else(|| bad("viewport.view", format!("unknown view mode `{m}`")))?), + }, + }; + set_view(s, v) +} + +pub(crate) fn camera_json(s: &Session) -> Value { + let c = &s.camera; + json!({ + "yaw": c.yaw.to_degrees(), "pitch": c.pitch.to_degrees(), "distance": c.distance, "fov": c.fov, + "target": c.target.to_array(), "eye": c.eye().to_array(), "ortho": c.ortho, + }) +} + +fn camera_set(s: &mut Session, p: &Value) -> Result { + let c = &mut s.camera; + if let Some(v) = f32p(p, "yaw") { + c.yaw = v.to_radians(); + } + if let Some(v) = f32p(p, "pitch") { + c.pitch = v.clamp(-89.0, 89.0).to_radians(); + } + if let Some(v) = f32p(p, "distance") { + c.distance = v.clamp(c.scene_radius * 0.05, c.scene_radius * 200.0); + } + if let Some(v) = f32p(p, "fov") { + c.fov = v.clamp(5.0, 150.0); + } + if let Some(t) = p.get("target").and_then(Value::as_array) { + let v: Vec = t.iter().filter_map(Value::as_f64).map(|x| x as f32).collect(); + if v.len() == 3 && v.iter().all(|x| x.is_finite()) { + c.target = Vec3::new(v[0], v[1], v[2]); + } + } + if let Some(o) = boolp(p, "ortho") { + c.ortho = o; + } + s.revision = s.revision.wrapping_add(1); + Ok(camera_json(s)) +} + +fn orbit(s: &mut Session, p: &Value) -> Result { + s.camera.orbit(f32p(p, "dx").unwrap_or(0.0).to_radians(), f32p(p, "dy").unwrap_or(0.0).to_radians()); + s.revision = s.revision.wrapping_add(1); + Ok(camera_json(s)) +} + +fn display(s: &mut Session, p: &Value) -> Result { + let tm = match strp(p, "toneMapping") { + Some("linear") => Some(ToneMapping::Linear), + Some("aces") | Some("acesFilmic") => Some(ToneMapping::AcesFilmic), + Some(o) => return Err(bad("display.set", format!("unknown tone mapping `{o}`"))), + None => None, + }; + let env = strp(p, "environment").map(str::to_string); + if let Some(name) = env.as_deref().and_then(|e| e.strip_prefix(texturecraft_core::project::ENV_RESOURCE_PREFIX)) + && !s.images.contains_key(name) + { + return Err(bad("display.set", format!("no image resource `{name}` (import it with resource.import)"))); + } + let d = &mut s.doc.project.display; + if let Some(e) = env { + d.environment = e; + } + for (k, slot, lo, hi) in [ + ("exposure", &mut d.env_exposure, -10.0, 10.0), + ("blur", &mut d.env_blur, 0.0, 1.0), + ("rotation", &mut d.env_rotation, -360.0, 360.0), + ("opacity", &mut d.env_opacity, 0.0, 1.0), + ("fov", &mut d.fov, 5.0, 150.0), + ("aoIntensity", &mut d.ao_intensity, 0.0, 1.0), + ("bloom", &mut d.bloom, 0.0, 1.0), + ("vignette", &mut d.vignette, 0.0, 1.0), + ("sharpen", &mut d.sharpen, 0.0, 1.0), + ("grain", &mut d.grain, 0.0, 1.0), + ] { + if let Some(v) = f32p(p, k) { + *slot = v.clamp(lo, hi); + } + } + if let Some(t) = tm { + d.tone_mapping = t; + } + if let Some(b) = boolp(p, "shadows") { + d.shadows = b; + } + for (k, slot) in [("postEffects", &mut d.post_effects), ("taa", &mut d.taa), ("specGloss", &mut d.spec_gloss), ("parallax", &mut d.parallax)] { + if let Some(b) = boolp(p, k) { + *slot = b; + } + } + s.camera.fov = s.doc.project.display.fov; + s.revision = s.revision.wrapping_add(1); + Ok(serde_json::to_value(&s.doc.project.display).unwrap_or(Value::Null)) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::execute; + + #[test] + fn hdr_resources_become_the_environment() { + let mut s = Session::new_project("t", texturecraft_geom::primitives::sphere(1.0, 16, 8), "builtin:sphere", None, 128).unwrap(); + assert!(execute(&mut s, "display.set", &json!({"environment": "resource:sky.hdr"})).is_err(), "unknown resources are rejected"); + let img = image::Rgb32FImage::from_fn(64, 32, |_, y| image::Rgb([if y < 8 { 40.0 } else { 0.5 }; 3])); + let mut hdr = Vec::new(); + image::DynamicImage::ImageRgb32F(img).write_to(&mut std::io::Cursor::new(&mut hdr), image::ImageFormat::Hdr).unwrap(); + let name = s.import_resource_bytes("sky.hdr", hdr).unwrap(); + let res = &s.images[&name]; + assert!(!res.srgb && res.rgba[0][0] > 30.0, "HDR keeps values above 1"); + let env = format!("resource:{name}"); + let r = execute(&mut s, "display.set", &json!({"environment": env, "specGloss": true, "parallax": true, "taa": false})).unwrap(); + assert_eq!(r["environment"], json!(env)); + let d = &s.doc.project.display; + assert!(d.spec_gloss && d.parallax && !d.taa); + assert_eq!(d.environment_resource(), Some(name.as_str())); + execute(&mut s, "file.removeUnused", &Value::Null).unwrap(); + assert!(s.doc.resources.contains_key(&name), "the environment's image counts as used"); + } +} diff --git a/crates/engine/tests/bake.rs b/crates/engine/tests/bake.rs new file mode 100644 index 00000000..5ac53339 --- /dev/null +++ b/crates/engine/tests/bake.rs @@ -0,0 +1,129 @@ +//! Bake commands: settings, high-definition meshes, cage, and project round trips. + +use serde_json::{Value, json}; +use texturecraft_engine::core::{BakeMatch, BakedMap}; +use texturecraft_engine::geom::{Mesh, primitives}; +use texturecraft_engine::{Session, execute}; + +fn plane_session() -> Session { + let mesh = primitives::builtin("plane").unwrap(); + Session::new_project("t", mesh, "builtin:plane", None, 256).unwrap() +} + +/// `mesh` as OBJ text, lifted by `dy`, keeping only vertices with x ≤ `max_x` (triangles that +/// lose a vertex are dropped), every vertex colored `rgb`. +fn obj(mesh: &Mesh, dy: f32, max_x: f32, rgb: [f32; 3]) -> String { + let mut s = String::from("o Part_high\n"); + for p in &mesh.positions { + s += &format!("v {} {} {} {} {} {}\n", p.x.min(max_x), p.y + dy, p.z, rgb[0], rgb[1], rgb[2]); + } + for t in mesh.indices.chunks_exact(3) { + s += &format!("f {} {} {}\n", t[0] + 1, t[1] + 1, t[2] + 1); + } + s +} + +fn temp_file(name: &str, text: &str) -> String { + let dir = std::env::temp_dir().join(format!("texturecraft-bake-test-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join(name); + std::fs::write(&path, text).unwrap(); + path.display().to_string() +} + +#[test] +fn high_poly_meshes_drive_the_bake_and_round_trip() { + let mut s = plane_session(); + let low = s.mesh.as_ref().clone(); + let (lo, hi) = low.bounds(); + let mid = (lo.x + hi.x) * 0.5; + // A red high poly over the left half of the plane, just above it. + let high = temp_file("hp_high.obj", &obj(&low, 0.002, mid, [1.0, 0.0, 0.0])); + let r = execute(&mut s, "bake.highPoly.add", &json!({ "path": high })).unwrap(); + assert_eq!(r["name"], "hp_high.obj"); + assert!(s.doc.project.texture_sets.iter().all(|t| !t.bake.use_low_as_high), "adding a high poly bakes from it"); + let list = execute(&mut s, "bake.highPoly.list", &Value::Null).unwrap(); + assert_eq!(list["highPoly"][0]["parts"][0], "Part_high"); + assert!(execute(&mut s, "project.inspect", &Value::Null).unwrap()["bakeMeshes"]["highPoly"].as_array().is_some_and(|a| a.len() == 1)); + + execute(&mut s, "bake.run", &json!({ "resolution": 128, "maps": ["opacity", "color_map"], "dilation": 0 })).unwrap(); + let baked = &s.doc.baked[&0]; + let opacity = &baked.maps[&BakedMap::Opacity]; + let covered = opacity.iter().filter(|v| **v > 0.99).count() as f32 / opacity.len() as f32; + assert!((covered - 0.5).abs() < 0.08, "the high poly covers half the plane: {covered}"); + let color = &baked.maps[&BakedMap::ColorMap]; + assert!(opacity.iter().zip(color.chunks(3)).filter(|(o, _)| **o > 0.99).all(|(_, c)| c == [1.0, 0.0, 0.0]), "red where covered"); + + // A cage must have the low poly's triangles. + let tri = temp_file("tri.obj", "v 0 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\n"); + assert!(execute(&mut s, "bake.cage.set", &json!({ "path": tri })).is_err()); + let cage = temp_file("cage.obj", &obj(&low, 0.05, f32::MAX, [1.0; 3])); + execute(&mut s, "bake.cage.set", &json!({ "path": cage })).unwrap(); + execute(&mut s, "bake.settings", &json!({ "matching": "byMeshName", "maxFrontal": 0.05 })).unwrap(); + + // Save and reopen: meshes, cage and settings come back. + let back = Session::from_project_bytes(&s.to_project_bytes().unwrap()).unwrap(); + let names = |s: &Session| s.bake_meshes.high.iter().map(|h| (h.name.clone(), h.mesh.triangle_count())).collect::>(); + assert_eq!(names(&back), names(&s)); + assert_eq!(back.bake_meshes.cage.as_ref().map(|c| c.name.as_str()), Some("cage.obj")); + assert_eq!(back.doc.project.texture_sets[0].bake, s.doc.project.texture_sets[0].bake); + assert_eq!(back.doc.project.texture_sets[0].bake.matching, BakeMatch::ByMeshName); + + execute(&mut s, "bake.highPoly.remove", &json!({ "name": "hp_high.obj" })).unwrap(); + assert!(s.bake_meshes.high.is_empty()); + execute(&mut s, "bake.cage.clear", &Value::Null).unwrap(); + assert!(s.bake_meshes.cage.is_none()); + // Without high polys the bake falls back to the low poly. + execute(&mut s, "bake.run", &json!({ "resolution": 128, "maps": ["opacity"] })).unwrap(); + assert!(s.doc.baked[&0].maps[&BakedMap::Opacity].iter().all(|v| *v == 1.0)); + let _ = std::fs::remove_dir_all(std::path::Path::new(&high).parent().unwrap()); +} + +#[test] +fn malformed_high_poly_input_errors_cleanly() { + let mut s = plane_session(); + assert!(s.add_high_poly_bytes("bad.obj", b"this is not a mesh".to_vec()).is_err()); + assert!(s.add_high_poly_bytes("bad.glb", b"glTF\x02\0\0\0\xff\xff\xff\xff".to_vec()).is_err()); + assert!(s.add_high_poly_bytes("bad.xyz", vec![1, 2, 3]).is_err()); + assert!(s.set_cage_bytes("bad.stl", vec![0; 10]).is_err()); + assert!(execute(&mut s, "bake.highPoly.add", &json!({ "path": "/definitely/missing.obj" })).is_err()); + assert!(execute(&mut s, "bake.highPoly.remove", &json!({ "index": 3 })).is_err()); + assert!(execute(&mut s, "bake.highPoly.remove", &Value::Null).is_err()); + assert!(s.bake_meshes.high.is_empty() && s.bake_meshes.cage.is_none()); + assert!(s.doc.project.texture_sets.iter().all(|t| t.bake.use_low_as_high), "failed adds change nothing"); +} + +#[test] +fn bake_settings_apply_atomically() { + let mut s = Session::new_project("t", primitives::material_ball(), "builtin:material_ball", None, 256).unwrap(); + let r = + execute(&mut s, "bake.settings", &json!({ "ts": "all", "antialiasing": 4, "direction": [1, 0, 0], "maps": ["bent_normals", "World space direction"] })) + .unwrap(); + assert_eq!(r.as_array().map(Vec::len), Some(2)); + for t in &s.doc.project.texture_sets { + assert_eq!((t.bake.antialiasing, t.bake.direction), (4, [1.0, 0.0, 0.0])); + assert_eq!(t.bake.maps.iter().copied().collect::>(), [BakedMap::BentNormal, BakedMap::WorldDirection]); + } + let before = s.doc.project.texture_sets[0].bake.clone(); + for bad in [ + json!({ "rays": 4, "antialiasing": 3 }), + json!({ "maps": ["nope"] }), + json!({ "direction": [0, 0, 0] }), + json!({ "matching": "?" }), + json!({ "ts": [0, 9] }), + ] { + assert!(execute(&mut s, "bake.settings", &bad).is_err(), "{bad}"); + assert_eq!(s.doc.project.texture_sets[0].bake, before, "{bad} changed nothing"); + } + // "selected" = the sets checked for baking. + execute(&mut s, "bake.settings", &json!({ "ts": 1, "enabled": false })).unwrap(); + let r = execute(&mut s, "bake.settings", &json!({ "ts": "selected", "rays": 8 })).unwrap(); + assert_eq!(r[0]["ts"], 0); + assert_eq!(r.as_array().map(Vec::len), Some(1)); + // The returned parameters set the same settings again (what the UI sends back). + let mut copy = r[0]["settings"].clone(); + copy["ts"] = 1.into(); + execute(&mut s, "bake.settings", ©).unwrap(); + let (a, b) = (&s.doc.project.texture_sets[0].bake, &s.doc.project.texture_sets[1].bake); + assert_eq!(a, b); +} diff --git a/crates/engine/tests/content_effects.rs b/crates/engine/tests/content_effects.rs new file mode 100644 index 00000000..74011e9e --- /dev/null +++ b/crates/engine/tests/content_effects.rs @@ -0,0 +1,113 @@ +//! Content effects, HSL and anchor points through engine commands. + +use serde_json::{Value, json}; +use texturecraft_engine::core::{Channel, LayerId}; +use texturecraft_engine::geom::primitives; +use texturecraft_engine::{Session, StackTarget, execute}; + +fn session() -> Session { + let mut s = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:sphere", None, 256).unwrap(); + s.viewport = texturecraft_engine::geom::glam::Vec2::new(400.0, 400.0); + s.camera.yaw = 0.0; + s.camera.pitch = 0.0; + s.update(); + s +} + +/// Composited value of `c` under the viewport center. +fn center(s: &mut Session, c: Channel) -> [f32; 3] { + s.update(); + let hit = s.pick(texturecraft_engine::geom::glam::Vec2::new(200.0, 200.0), s.viewport).unwrap(); + s.caches[0].composite.value(c, (hit.uv.x * 256.0) as u32, (hit.uv.y * 256.0) as u32) +} + +fn close(a: [f32; 3], b: [f32; 3]) -> bool { + (0..3).all(|i| (a[i] - b[i]).abs() < 2e-3) +} + +fn id(v: Value, key: &str) -> u64 { + v[key].as_u64().unwrap() +} + +#[test] +fn content_effects_commands_inspect_and_round_trip() { + let mut s = session(); + let layer = id(execute(&mut s, "layer.add.fill", &json!({"color": [1.0, 0.0, 0.0], "roughness": 0.2})).unwrap(), "layer"); + let hsl = id(execute(&mut s, "effect.add.filter", &json!({"target": "content", "filter": "hsl", "hue": 1.0 / 3.0})).unwrap(), "effect"); + assert!(close(center(&mut s, Channel::BaseColor), [0.0, 1.0, 0.0]), "{:?}", center(&mut s, Channel::BaseColor)); + assert!(s.doc.project.layer(LayerId(layer)).unwrap().mask.is_none(), "content effects don't create a mask"); + assert_eq!(s.target, StackTarget::Effect(LayerId(hsl))); + // Mask-only effects refuse the content, unknown targets are errors. + assert!(execute(&mut s, "effect.add.generator", &json!({"target": "content"})).is_err()); + assert!(execute(&mut s, "effect.add.filter", &json!({"target": "nope"})).is_err()); + execute(&mut s, "effect.set", &json!({"effect": hsl, "hue": 0.0, "lightness": -1.0})).unwrap(); + assert!(close(center(&mut s, Channel::BaseColor), [0.0; 3])); + execute(&mut s, "effect.set", &json!({"effect": hsl, "visible": false})).unwrap(); + + // A content fill effect, edited through layer.fill.set. + let fill = id(execute(&mut s, "effect.add.fill", &json!({"target": "content", "roughness": 0.7})).unwrap(), "effect"); + assert!(close(center(&mut s, Channel::Roughness), [0.7; 3])); + assert!(close(center(&mut s, Channel::BaseColor), [1.0, 0.0, 0.0]), "channels without a source stay"); + execute(&mut s, "layer.fill.set", &json!({"effect": fill, "channel": "roughness", "value": 0.9})).unwrap(); + assert!(close(center(&mut s, Channel::Roughness), [0.9; 3])); + execute(&mut s, "layer.fill.set", &json!({"effect": fill, "channel": "roughness", "enabled": false})).unwrap(); + assert!(close(center(&mut s, Channel::Roughness), [0.2; 3])); + assert!(execute(&mut s, "layer.fill.set", &json!({"effect": hsl, "channel": "roughness", "value": 0.5})).is_err(), "not a fill effect"); + + // Inspect shows the content stack; the project survives a save/load round trip. + let inspect = execute(&mut s, "project.inspect", &Value::Null).unwrap(); + let row = inspect["layers"].as_array().unwrap().iter().find(|l| l["id"] == layer).unwrap().clone(); + let kinds: Vec<&str> = row["contentEffects"].as_array().unwrap().iter().map(|e| e["kind"].as_str().unwrap()).collect(); + assert_eq!(kinds, ["HSL Perceptive", "Fill"], "{row}"); + let back = Session::from_project_bytes(&s.to_project_bytes().unwrap()).unwrap(); + assert_eq!(back.doc.project, s.doc.project); + + execute(&mut s, "layer.select", &json!({"layer": layer, "target": fill})).unwrap(); + execute(&mut s, "effect.remove", &json!({"effect": fill})).unwrap(); + assert_eq!(s.target, StackTarget::Content); + execute(&mut s, "edit.undo", &Value::Null).unwrap(); + assert_eq!(s.doc.project.layer(LayerId(layer)).unwrap().content_effects.len(), 2); +} + +#[test] +fn anchor_points_feed_masks_above() { + let mut s = session(); + let low = id(execute(&mut s, "layer.add.fill", &json!({"height": 0.5})).unwrap(), "layer"); + let anchor = id(execute(&mut s, "effect.add.anchor", &json!({"name": "Height"})).unwrap(), "effect"); + assert_eq!(s.doc.project.effect(LayerId(anchor)).unwrap().1.name, "Height"); + execute(&mut s, "layer.add.fill", &json!({"color": [1.0, 1.0, 1.0]})).unwrap(); + // A layer cannot read an anchor on itself or above it. + assert!(execute(&mut s, "effect.add.fill", &json!({"layer": low, "anchor": anchor})).is_err()); + assert!(execute(&mut s, "effect.add.fill", &json!({"anchor": low})).is_err(), "not an anchor point"); + execute(&mut s, "mask.add.black", &Value::Null).unwrap(); + let reader = id(execute(&mut s, "effect.add.fill", &json!({"anchor": anchor, "anchorChannel": "height"})).unwrap(), "effect"); + // Base color: the low fill's default gray, then white through a 0.5 mask. + assert!(close(center(&mut s, Channel::BaseColor), [0.75; 3]), "{:?}", center(&mut s, Channel::BaseColor)); + // Reading the anchor's base color as gray instead. + execute(&mut s, "effect.set", &json!({"effect": reader, "anchorChannel": "baseColor"})).unwrap(); + assert!(close(center(&mut s, Channel::BaseColor), [0.75; 3])); + execute(&mut s, "effect.set", &json!({"effect": reader, "value": 1.0})).unwrap(); + assert!(close(center(&mut s, Channel::BaseColor), [1.0; 3]), "back to a uniform fill"); + // A blurred mask still reads the anchor (a flat height stays flat). + execute(&mut s, "effect.set", &json!({"effect": reader, "anchor": anchor})).unwrap(); + execute(&mut s, "effect.add.filter", &json!({"filter": "blur", "radius": 8})).unwrap(); + assert!(close(center(&mut s, Channel::BaseColor), [0.75; 3])); + let inspect = execute(&mut s, "project.inspect", &Value::Null).unwrap(); + assert!(inspect.to_string().contains(&format!("\"anchor\":{anchor}")), "{inspect}"); +} + +#[test] +fn painting_into_a_content_paint_effect() { + let mut s = session(); + execute(&mut s, "layer.add.fill", &json!({"roughness": 0.3})).unwrap(); + let e = id(execute(&mut s, "effect.add.paint", &json!({"target": "content"})).unwrap(), "effect"); + execute(&mut s, "tool.material", &json!({"channel": "roughness", "value": 0.9})).unwrap(); + execute(&mut s, "tool.brush", &json!({"size": 20, "hardness": 90})).unwrap(); + execute(&mut s, "tool.stroke", &json!({"points": [[180, 200], [220, 200]]})).unwrap(); + assert!(s.doc.pixels.layer(LayerId(e), Channel::Roughness).is_some()); + assert!(center(&mut s, Channel::Roughness)[0] > 0.85, "{:?}", center(&mut s, Channel::Roughness)); + // Flattening bakes the effect into a paint layer and drops its pixels. + execute(&mut s, "layer.flatten", &Value::Null).unwrap(); + assert!(s.doc.pixels.layer(LayerId(e), Channel::Roughness).is_none()); + assert!(center(&mut s, Channel::Roughness)[0] > 0.85); +} diff --git a/crates/engine/tests/instances.rs b/crates/engine/tests/instances.rs new file mode 100644 index 00000000..b9f14bcd --- /dev/null +++ b/crates/engine/tests/instances.rs @@ -0,0 +1,232 @@ +//! Instance layers across texture sets, mask with height, change-driven invalidation and the +//! frame-budgeted compositor, through engine commands. + +use serde_json::{Value, json}; +use texturecraft_engine::core::{Channel, LayerId, LayerKind}; +use texturecraft_engine::geom::glam::Vec2; +use texturecraft_engine::geom::primitives; +use texturecraft_engine::{Session, execute}; + +/// Two texture sets (ball and base) at 256². +fn session() -> Session { + let mut s = Session::new_project("t", primitives::material_ball(), "builtin:material_ball", None, 256).unwrap(); + s.viewport = Vec2::new(400.0, 400.0); + s.update(); + s +} + +fn id(v: Value, key: &str) -> LayerId { + LayerId(v[key].as_u64().unwrap()) +} + +/// A texel covered by texture set `ts`. +fn covered(s: &Session, ts: usize) -> (u32, u32) { + let g = &s.caches[ts].gbuf; + let i = g.tri.iter().position(|t| *t != texturecraft_engine::geom::NO_TRI).unwrap(); + (i as u32 % g.width, i as u32 / g.width) +} + +fn rough(s: &mut Session, ts: usize) -> f32 { + s.update(); + let (x, y) = covered(s, ts); + s.caches[ts].composite.value(Channel::Roughness, x, y)[0] +} + +/// Texels of set `ts` whose base color is red. +fn reds(s: &Session, ts: usize) -> usize { + let c = &s.caches[ts].composite; + (0..256) + .flat_map(|y| (0..256).map(move |x| (x, y))) + .filter(|&(x, y)| s.caches[ts].gbuf.tri[(y * 256 + x) as usize] != u32::MAX) + .filter(|&(x, y)| { + let v = c.value(Channel::BaseColor, x, y); + v[0] > 0.8 && v[1] < 0.2 + }) + .count() +} + +#[test] +fn instances_follow_their_source_across_texture_sets() { + let mut s = session(); + let src = id(execute(&mut s, "layer.add.fill", &json!({"roughness": 0.8})).unwrap(), "layer"); + let inst = id(execute(&mut s, "layer.instance", &json!({"layer": src.0, "ts": 1})).unwrap(), "layer"); + assert!(matches!(s.doc.project.layer(inst).unwrap().kind, LayerKind::Instance { source } if source == src)); + assert_eq!(s.doc.project.locate(inst).map(|l| l.0), Some(1)); + assert!((rough(&mut s, 1) - 0.8).abs() < 1e-3); + // Editing the source (directly or through the instance) updates the instance's set. + execute(&mut s, "layer.fill.set", &json!({"layer": src.0, "channel": "roughness", "value": 0.3})).unwrap(); + assert!(!s.caches[1].dirty.is_empty(), "the instance's set is invalidated"); + assert!((rough(&mut s, 1) - 0.3).abs() < 1e-3); + execute(&mut s, "layer.fill.set", &json!({"layer": inst.0, "channel": "roughness", "value": 0.6})).unwrap(); + assert!((rough(&mut s, 0) - 0.6).abs() < 1e-3 && (rough(&mut s, 1) - 0.6).abs() < 1e-3); + execute(&mut s, "layer.blend", &json!({"layer": inst.0, "channel": "roughness", "opacity": 0.5})).unwrap(); + assert_eq!(s.doc.project.layer(src).unwrap().blend(Channel::Roughness).unwrap().opacity, 0.5, "blending is linked"); + // Inspect shows the link; an instance of an instance links to the source. + let stack = execute(&mut s, "layer.list", &json!({"ts": 1})).unwrap(); + assert_eq!(stack[0]["instanceOf"]["layer"], src.0, "{stack}"); + let again = id(execute(&mut s, "layer.instance", &json!({"layer": inst.0, "ts": 1})).unwrap(), "layer"); + assert_eq!(s.doc.project.layer(again).unwrap().instance_source(), Some(src)); + // Save/load keeps instances; undo removes them. + let back = Session::from_project_bytes(&s.to_project_bytes().unwrap()).unwrap(); + assert_eq!(back.doc.project, s.doc.project); + execute(&mut s, "edit.undo", &Value::Null).unwrap(); + assert!(s.doc.project.layer(again).is_err()); +} + +#[test] +fn painting_a_source_paints_its_instances() { + let mut s = session(); + let paint = s.selected_layer().unwrap(); + execute(&mut s, "layer.instance", &json!({"layer": paint.0, "ts": 1})).unwrap(); + execute(&mut s, "textureSet.select", &json!({"ts": 0})).unwrap(); + execute(&mut s, "layer.select", &json!({"layer": paint.0})).unwrap(); + execute(&mut s, "tool.material", &json!({"channel": "baseColor", "color": [1.0, 0.0, 0.0]})).unwrap(); + execute(&mut s, "tool.brush", &json!({"size": 30, "hardness": 90})).unwrap(); + assert_eq!(reds(&s, 1), 0); + // The live stroke shows in the instance before it is committed. + s.begin_stroke(Vec2::new(200.0, 170.0), s.viewport, 1.0).unwrap(); + s.continue_stroke(Vec2::new(230.0, 170.0), 1.0); + s.update(); + let live = reds(&s, 1); + assert!(live > 0, "the instance shows the live stroke"); + s.end_stroke().unwrap(); + s.update(); + assert!(reds(&s, 1) >= live); + // Painting through the instance paints the source. + let inst = s.doc.project.texture_sets[1].stack.last().copied().unwrap(); + execute(&mut s, "layer.select", &json!({"layer": inst.0})).unwrap(); + let before = s.doc.pixels.layer(paint, Channel::BaseColor).map(|i| i.allocated_count()).unwrap_or(0); + execute(&mut s, "tool.stroke", &json!({"points": [[150, 330], [250, 330]]})).unwrap(); + assert!(s.doc.pixels.layer(paint, Channel::BaseColor).unwrap().allocated_count() >= before); + assert!(s.doc.pixels.layers.keys().all(|(l, _)| *l != inst), "instances hold no pixels"); +} + +#[test] +fn cycles_are_refused_and_removed_sources_detach() { + let mut s = session(); + let g = id(execute(&mut s, "layer.add.group", &Value::Null).unwrap(), "layer"); + let fill = id(execute(&mut s, "layer.add.fill", &json!({"roughness": 0.9})).unwrap(), "layer"); + assert_eq!(s.doc.project.locate(fill).unwrap().1, Some(g)); + // The folder selected: the instance would land inside it and draw itself. + execute(&mut s, "layer.select", &json!({"layer": g.0})).unwrap(); + let doc = s.doc.project.clone(); + assert!(execute(&mut s, "layer.instance", &json!({"layer": g.0})).is_err()); + assert_eq!(s.doc.project, doc, "a refused edit leaves the document as it was"); + // Moving an instance of the folder into it is refused too. + execute(&mut s, "layer.select", &json!({"layer": fill.0})).unwrap(); + execute(&mut s, "layer.move", &json!({"layer": fill.0, "above": Value::Null})).unwrap(); + execute(&mut s, "layer.select", &json!({"layer": fill.0})).unwrap(); + let inst = id(execute(&mut s, "layer.instance", &json!({"layer": g.0})).unwrap(), "layer"); + assert!(execute(&mut s, "layer.move", &json!({"layer": inst.0, "above": Value::Null, "inside": false})).is_ok()); + assert!(execute(&mut s, "layer.move", &json!({"layer": inst.0, "above": g.0, "inside": true})).is_err()); + + // Removing a source turns its instances into copies that look the same. + let src = id(execute(&mut s, "layer.add.fill", &json!({"roughness": 0.15})).unwrap(), "layer"); + execute(&mut s, "layer.instantiateAll", &json!({"layer": src.0})).unwrap(); + let inst = s.doc.project.texture_sets[1].stack.last().copied().unwrap(); + assert!(s.doc.project.layer(inst).unwrap().instance_source() == Some(src)); + let look = rough(&mut s, 1); + assert!((look - 0.15).abs() < 1e-3); + execute(&mut s, "layer.remove", &json!({"layer": src.0})).unwrap(); + assert!(s.doc.project.layer(inst).is_err()); + let copy = s.doc.project.texture_sets[1].stack.last().copied().unwrap(); + assert!(matches!(s.doc.project.layer(copy).unwrap().kind, LayerKind::Fill(_))); + assert!((rough(&mut s, 1) - look).abs() < 1e-4); + s.doc.project.validate().unwrap(); + execute(&mut s, "edit.undo", &Value::Null).unwrap(); + assert_eq!(s.doc.project.layer(inst).unwrap().instance_source(), Some(src)); +} + +#[test] +fn paste_as_instance_and_instantiate_or_copy_across_sets() { + let mut s = session(); + let src = id(execute(&mut s, "layer.add.fill", &json!({"roughness": 0.7})).unwrap(), "layer"); + assert!(execute(&mut s, "layer.pasteInstance", &Value::Null).is_err(), "empty clipboard"); + execute(&mut s, "layer.copy", &json!({"layer": src.0})).unwrap(); + execute(&mut s, "textureSet.select", &json!({"ts": 1})).unwrap(); + let pasted = id(execute(&mut s, "layer.pasteInstance", &Value::Null).unwrap(), "layer"); + assert_eq!(s.doc.project.layer(pasted).unwrap().instance_source(), Some(src)); + assert_eq!(s.selected_layer(), Some(pasted)); + // A cut layer is gone: paste it, not an instance of it. + execute(&mut s, "layer.cut", &json!({"layer": pasted.0})).unwrap(); + assert!(execute(&mut s, "layer.pasteInstance", &Value::Null).is_err()); + + let made = execute(&mut s, "layer.instantiateAll", &json!({"layer": src.0})).unwrap(); + let inst = LayerId(made["layers"][0].as_u64().unwrap()); + assert_eq!(s.doc.project.layer(inst).unwrap().instance_source(), Some(src)); + let made = execute(&mut s, "layer.instantiateAll", &json!({"layer": src.0, "copy": true})).unwrap(); + let copy = LayerId(made["layers"][0].as_u64().unwrap()); + assert!(matches!(s.doc.project.layer(copy).unwrap().kind, LayerKind::Fill(_))); + // Flattening an instance makes it an independent paint layer with the same look. + let look = rough(&mut s, 1); + execute(&mut s, "layer.remove", &json!({"layer": copy.0})).unwrap(); + execute(&mut s, "layer.flatten", &json!({"layer": inst.0})).unwrap(); + let l = s.doc.project.layer(inst).unwrap(); + assert!(matches!(l.kind, LayerKind::Paint) && l.blend(Channel::Roughness).is_some()); + assert!((rough(&mut s, 1) - look).abs() < 2e-3); +} + +#[test] +fn mask_with_height_through_layer_set() { + let mut s = session(); + let l = id(execute(&mut s, "layer.add.fill", &json!({"color": [1.0, 1.0, 1.0], "height": 0.5})).unwrap(), "layer"); + execute(&mut s, "mask.add.black", &Value::Null).unwrap(); + execute(&mut s, "effect.add.fill", &json!({"value": 0.5})).unwrap(); + let (x, y) = covered(&s, 0); + s.update(); + let plain = s.caches[0].composite.value(Channel::BaseColor, x, y)[0]; + let r = execute(&mut s, "layer.set", &json!({"layer": l.0, "heightMask": {"enabled": true, "contrast": 1.0, "offset": 0.0}})).unwrap(); + assert_eq!(r["heightMask"]["enabled"], true); + s.update(); + let with = s.caches[0].composite.value(Channel::BaseColor, x, y)[0]; + assert!((plain - 0.75).abs() < 0.01 && (with - 1.0).abs() < 0.01, "{plain} {with}"); + let inspect = execute(&mut s, "layer.list", &Value::Null).unwrap(); + assert_eq!(inspect[0]["heightMask"]["contrast"], 1.0); + execute(&mut s, "layer.set", &json!({"layer": l.0, "heightMask": false})).unwrap(); + assert!(!s.doc.project.layer(l).unwrap().height_mask.enabled); + assert!(execute(&mut s, "layer.set", &json!({"layer": l.0})).is_err()); + assert!(execute(&mut s, "layer.set", &json!({"layer": l.0, "heightMask": 3})).is_err()); + let hostile = execute(&mut s, "layer.set", &json!({"layer": l.0, "heightMask": {"contrast": 1e30, "offset": -1e30}})).unwrap(); + assert_eq!(hostile["heightMask"]["contrast"], 64.0); + assert_eq!(hostile["heightMask"]["offset"], -1.0); +} + +#[test] +fn edits_invalidate_only_the_sets_they_change() { + let mut s = session(); + let l = id(execute(&mut s, "layer.add.fill", &json!({"roughness": 0.4})).unwrap(), "layer"); + s.update(); + execute(&mut s, "layer.rename", &json!({"layer": l.0, "name": "Renamed"})).unwrap(); + assert_eq!(s.pending_tiles(), 0, "a rename changes no pixels"); + execute(&mut s, "layer.fill.set", &json!({"layer": l.0, "channel": "roughness", "value": 0.2})).unwrap(); + assert!(!s.caches[0].dirty.is_empty() && s.caches[1].dirty.is_empty(), "only the edited set"); + s.update(); + execute(&mut s, "edit.undo", &Value::Null).unwrap(); + assert!(!s.caches[0].dirty.is_empty() && s.caches[1].dirty.is_empty()); + assert!((rough(&mut s, 0) - 0.4).abs() < 1e-3); +} + +#[test] +fn budgeted_updates_spread_work_and_end_identical() { + let mut s = session(); + execute(&mut s, "textureSet.resolution", &json!({"ts": 0, "resolution": 1024})).unwrap(); + execute(&mut s, "layer.add.fill", &Value::Null).unwrap(); + execute(&mut s, "layer.fill.set", &json!({"channel": "baseColor", "procedural": "grunge"})).unwrap(); + execute(&mut s, "mask.add.black", &Value::Null).unwrap(); + execute(&mut s, "effect.add.generator", &json!({"generator": "position"})).unwrap(); + execute(&mut s, "effect.add.filter", &json!({"filter": "blur", "radius": 30})).unwrap(); + s.update(); + let full = s.caches[0].composite.clone(); + s.caches[0].invalidate_all(); + // A clock that is always over budget: one chunk per call. + let mut calls = 0; + while s.pending_tiles() > 0 { + let before = s.pending_tiles(); + s.update_for(0.0, || 1.0); + assert!(s.pending_tiles() < before); + calls += 1; + } + assert!(calls > 1, "the work was spread over calls"); + assert!(s.caches[0].composite == full, "chunked compositing gives the same pixels"); + assert!(s.memory().composites > 0); +} diff --git a/crates/engine/tests/path_tool.rs b/crates/engine/tests/path_tool.rs new file mode 100644 index 00000000..72f485df --- /dev/null +++ b/crates/engine/tests/path_tool.rs @@ -0,0 +1,131 @@ +//! The Path tool (stroked and filled paths, 3D and 2D) and the particle brush, driven through +//! engine commands the way agents paint. + +use serde_json::{Value, json}; +use texturecraft_engine::core::Channel; +use texturecraft_engine::geom::glam::{Vec2, Vec3, Vec4}; +use texturecraft_engine::geom::{Mesh, primitives}; +use texturecraft_engine::{Session, execute}; + +const RES: f32 = 256.0; + +fn session(mesh: Mesh) -> Session { + let mut s = Session::new_project("t", mesh, "builtin:test", None, 256).unwrap(); + s.viewport = Vec2::new(400.0, 400.0); + s.camera.yaw = 0.0; + s.camera.pitch = 0.0; + s.update(); + execute(&mut s, "tool.material", &json!({"channel": "baseColor", "color": [1.0, 0.0, 0.0]})).unwrap(); + execute(&mut s, "tool.brush", &json!({"size": 6, "hardness": 90})).unwrap(); + s +} + +fn red_uv(s: &Session, u: f32, v: f32) -> bool { + let c = s.caches[0].composite.value(Channel::BaseColor, (u * RES) as u32, (v * RES) as u32); + c[0] > 0.85 && c[1] < 0.15 +} + +/// Is the surface under a viewport point painted red? +fn red_at(s: &Session, x: f32, y: f32) -> bool { + let hit = s.pick(Vec2::new(x, y), s.viewport).unwrap(); + red_uv(s, hit.uv.x, hit.uv.y) +} + +#[test] +fn path_stroke_paints_along_the_curve() { + let mut s = session(primitives::sphere(1.0, 48, 24)); + let r = execute(&mut s, "tool.path", &json!({"points": [[140, 210], [200, 180], [260, 210]]})).unwrap(); + assert_eq!(r["points"], 3); + assert_eq!(s.tool.tool, texturecraft_engine::Tool::Path); + assert!(!red_at(&s, 200.0, 180.0), "nothing painted before commit"); + let r = execute(&mut s, "tool.path", &json!({"commit": true})).unwrap(); + assert!(r["committed"]["stamps"].as_u64().unwrap() > 10, "{r}"); + assert_eq!(r["points"], 0, "commit clears the path"); + for (x, y) in [(140.0, 210.0), (200.0, 180.0), (260.0, 210.0), (170.0, 191.0)] { + assert!(red_at(&s, x, y), "on the curve at {x},{y}"); + } + assert!(!red_at(&s, 200.0, 240.0), "below the curve"); + assert!(!red_at(&s, 200.0, 130.0), "above the curve"); + // One undo step. + execute(&mut s, "edit.undo", &Value::Null).unwrap(); + s.update(); + assert!(!red_at(&s, 200.0, 180.0)); + // Errors: no points, points off the mesh. + assert!(execute(&mut s, "tool.path", &json!({"commit": true})).is_err()); + assert!(execute(&mut s, "tool.path", &json!({"points": [[2, 2]]})).is_err()); +} + +#[test] +fn uv_path_strokes_in_texture_space() { + let mut s = session(primitives::sphere(1.0, 48, 24)); + execute(&mut s, "tool.path", &json!({"uvPoints": [[0.2, 0.5], [0.5, 0.45], [0.8, 0.5]], "commit": true})).unwrap(); + assert!(red_uv(&s, 0.5, 0.45) && red_uv(&s, 0.2, 0.5) && red_uv(&s, 0.8, 0.5)); + assert!(!red_uv(&s, 0.5, 0.6)); +} + +#[test] +fn closed_filled_path_fills_the_inside_only() { + let mut s = session(primitives::sphere(1.0, 48, 24)); + let square = json!([[160, 160], [240, 160], [240, 240], [160, 240]]); + execute(&mut s, "tool.path", &json!({"points": square, "closed": true, "fill": true, "commit": true})).unwrap(); + for (x, y) in [(200.0, 200.0), (170.0, 170.0), (230.0, 230.0)] { + assert!(red_at(&s, x, y), "inside at {x},{y}"); + } + for (x, y) in [(200.0, 125.0), (275.0, 200.0), (125.0, 200.0), (200.0, 275.0)] { + assert!(!red_at(&s, x, y), "outside at {x},{y}"); + } + // The far side of the sphere, hidden from the camera, stays clean. + s.camera.yaw = std::f32::consts::PI; + assert!(!red_at(&s, 200.0, 200.0), "hidden back side"); + + // A 2D lasso fills the UV region. + let mut s = session(primitives::sphere(1.0, 48, 24)); + execute(&mut s, "tool.path", &json!({"uvPoints": [[0.3, 0.3], [0.7, 0.3], [0.7, 0.7], [0.3, 0.7]], "closed": true, "fill": true, "commit": true})).unwrap(); + assert!(red_uv(&s, 0.5, 0.5) && red_uv(&s, 0.35, 0.65)); + assert!(!red_uv(&s, 0.2, 0.5) && !red_uv(&s, 0.5, 0.85)); + // Options persist between paths; a filled path needs three points. + assert!(execute(&mut s, "tool.path", &json!({"uvPoints": [[0.1, 0.1], [0.2, 0.2]], "closed": false, "commit": true})).is_ok()); + assert!(execute(&mut s, "tool.path", &json!({"uvPoints": [[0.1, 0.1], [0.2, 0.2]], "closed": true, "fill": true, "commit": true})).is_err()); +} + +/// A 2×2 wall facing the camera (+Z): v grows downward (world −Y). +fn wall() -> Mesh { + let mut m = primitives::plane(2.0, 8); + for p in &mut m.positions { + *p = Vec3::new(p.x, -p.z, 0.0); + } + m.normals.iter_mut().for_each(|n| *n = Vec3::Z); + m.tangents.iter_mut().for_each(|t| *t = Vec4::new(1.0, 0.0, 0.0, 1.0)); + m +} + +/// Painted texel rows (v) after one particle dab at the wall's center. +fn particle_rows() -> Vec { + let mut s = session(wall()); + execute(&mut s, "tool.brush", &json!({"size": 10, "particles": {"enabled": true, "count": 6, "lifetime": 40, "gravity": 1.5}})).unwrap(); + let hit = s.pick(Vec2::new(200.0, 200.0), s.viewport).unwrap(); + assert!((hit.uv.y - 0.5).abs() < 0.02, "{hit:?}"); + execute(&mut s, "tool.stroke", &json!({"points": [[200, 200]]})).unwrap(); + let mut rows = Vec::new(); + for y in 0..RES as u32 { + for x in 0..RES as u32 { + let c = s.caches[0].composite.value(Channel::BaseColor, x, y); + if c[0] > 0.85 && c[1] < 0.5 { + rows.push(y); + } + } + } + rows +} + +#[test] +fn particle_brush_paints_below_the_start_and_is_deterministic() { + let rows = particle_rows(); + assert!(!rows.is_empty()); + let start = (0.5 * RES) as u32; + let lowest = rows.iter().max().copied().unwrap(); + let highest = rows.iter().min().copied().unwrap(); + assert!(lowest > start + 40, "trails run down the wall: {lowest}"); + assert!(highest + 20 > start, "nothing well above the start: {highest}"); + assert_eq!(rows, particle_rows()); +} diff --git a/crates/engine/tests/perf.rs b/crates/engine/tests/perf.rs new file mode 100644 index 00000000..19f50247 --- /dev/null +++ b/crates/engine/tests/perf.rs @@ -0,0 +1,137 @@ +//! Compositing benchmarks at 4K and 8K (run with `--ignored --nocapture` in release). + +use std::time::{Duration, Instant}; + +use rayon::prelude::*; +use serde_json::{Value, json}; +use texturecraft_engine::compose::{DisplayKind, tile_bytes}; +use texturecraft_engine::geom::glam::Vec2; +use texturecraft_engine::geom::primitives; +use texturecraft_engine::{Session, execute}; + +/// A ~10 layer stack: procedural fills, generators, a blurred mask, a folder and painted layers. +fn stack(res: u32) -> Session { + let mut s = Session::new_project("perf", primitives::sphere(1.0, 64, 32), "builtin:sphere", None, res).unwrap(); + s.viewport = Vec2::new(800.0, 800.0); + let run = |s: &mut Session, id: &str, p: Value| execute(s, id, &p).unwrap(); + run(&mut s, "layer.add.fill", json!({"color": [0.6, 0.5, 0.4], "roughness": 0.6})); + run(&mut s, "layer.fill.set", json!({"channel": "baseColor", "procedural": "clouds", "low": [0.2, 0.2, 0.2], "high": [0.8, 0.7, 0.6]})); + run(&mut s, "layer.fill.set", json!({"channel": "height", "procedural": "cells", "low": [-0.2, -0.2, -0.2], "high": [0.2, 0.2, 0.2]})); + run(&mut s, "layer.add.fill", json!({"color": [0.1, 0.3, 0.6]})); + run(&mut s, "layer.fill.set", json!({"channel": "roughness", "procedural": "grunge"})); + run(&mut s, "mask.add.black", Value::Null); + run(&mut s, "effect.add.generator", json!({"generator": "position", "level": 0.5})); + run(&mut s, "effect.add.filter", json!({"filter": "blur", "radius": 8})); + run(&mut s, "layer.add.fill", json!({"roughness": 0.3})); + run(&mut s, "layer.fill.set", json!({"channel": "baseColor", "procedural": "wood"})); + run(&mut s, "layer.fill.projection", json!({"projection": "triplanar", "scale": 2})); + run(&mut s, "mask.add.black", Value::Null); + run(&mut s, "effect.add.fill", json!({"procedural": "scratches", "scale": 6})); + run(&mut s, "layer.add.fill", json!({"color": [0.2, 0.2, 0.2], "metallic": 1.0})); + run(&mut s, "mask.add.black", Value::Null); + run(&mut s, "effect.add.generator", json!({"generator": "dirt"})); + run(&mut s, "layer.add.fill", json!({"roughness": 0.8})); + run(&mut s, "layer.fill.set", json!({"channel": "roughness", "procedural": "marble"})); + run(&mut s, "layer.add.group", Value::Null); + run(&mut s, "layer.add.fill", json!({"color": [0.9, 0.9, 0.2]})); + run(&mut s, "mask.add.black", Value::Null); + run(&mut s, "effect.add.generator", json!({"generator": "direction"})); + run(&mut s, "effect.add.levels", json!({"inLow": 0.3, "inHigh": 0.7})); + let top = s.doc.project.texture_sets[0].stack.last().copied().unwrap(); + run(&mut s, "layer.select", json!({"layer": top.0})); + run(&mut s, "layer.add.paint", Value::Null); + run(&mut s, "tool.brush", json!({"size": 6, "hardness": 80})); + run(&mut s, "tool.stroke", json!({"points": [[200, 300], [600, 500]]})); + run(&mut s, "layer.add.paint", Value::Null); + s.update(); + s +} + +fn upload_bytes(s: &mut Session) -> usize { + let tiles = s.take_uploads(0); + let baked = s.baked(0).cloned().unwrap_or_default(); + let comp = &s.caches[0].composite; + // As the UI does it (in parallel). + tiles.par_iter().flat_map_iter(|&(tx, ty)| DisplayKind::ALL.map(|k| tile_bytes(comp, &baked, k, tx, ty).len())).sum() +} + +fn mb(b: usize) -> f64 { + b as f64 / (1024.0 * 1024.0) +} + +fn bench(res: u32) { + let t = Instant::now(); + let mut s = stack(res); + println!("[{res}²] setup {:.2?} ({} layers, {} covered tiles)", t.elapsed(), s.doc.project.walk(0).len(), s.caches[0].covered_tiles.len()); + s.caches[0].invalidate_all(); + let t = Instant::now(); + s.update(); + println!("[{res}²] full recomposite (Session::update) {:.2?}", t.elapsed()); + let t = Instant::now(); + let up = upload_bytes(&mut s); + println!("[{res}²] full display tile conversion {:.2?} ({:.0} MB)", t.elapsed(), mb(up)); + + // The UI's budgeted update: a full recomposite spread over frames. + s.caches[0].invalidate_all(); + let clock = Instant::now(); + let ms = || clock.elapsed().as_secs_f64() * 1000.0; + let (mut frames, mut worst) = (0, Duration::ZERO); + while s.pending_tiles() > 0 { + let t = Instant::now(); + s.update_for(30.0, ms); + worst = worst.max(t.elapsed()); + frames += 1; + } + println!("[{res}²] budgeted full recomposite: {frames} frames, worst frame {worst:.2?}, total {:.2?}", clock.elapsed()); + + // Strokes on the top paint layer, twice over the same path: per-frame dirty-tile composite + // + display conversion, then the commit. Brush sizes are relative to the mesh: the largest + // one covers a third of the texture per stamp. + for (size, brush) in [(0.5, "largest brush"), (0.02, "default brush")] { + s.tool.brush.size = size; + for pass in ["1st stroke", "2nd stroke"] { + let (mut comp, mut conv, mut worst, mut tiles) = (Duration::ZERO, Duration::ZERO, Duration::ZERO, 0usize); + s.begin_stroke(Vec2::new(250.0, 400.0), s.viewport, 1.0).unwrap(); + let frames = 40; + for i in 0..frames { + let p = Vec2::new(250.0 + i as f32 * 8.0, 400.0 + (i as f32 * 0.3).sin() * 40.0); + s.continue_stroke(p, 1.0); + tiles += s.caches[0].dirty.len(); + let t = Instant::now(); + s.update(); + let c = t.elapsed(); + let t = Instant::now(); + upload_bytes(&mut s); + let u = t.elapsed(); + comp += c; + conv += u; + worst = worst.max(c + u); + } + s.end_stroke().unwrap(); + let t = Instant::now(); + s.update(); + let commit = t.elapsed(); + let f = frames as u32; + println!( + "[{res}²] {brush}, {pass}: frame composite {:.2?} + display {:.2?} avg, worst {worst:.2?} ({:.0} dirty tiles/frame); commit {commit:.2?}", + comp / f, + conv / f, + tiles as f64 / frames as f64 + ); + } + } + let m = s.memory(); + println!("[{res}²] memory: layer pixels {:.1} MB, composites {:.1} MB, below-stack cache {:.1} MB", mb(m.pixels), mb(m.composites), mb(m.cache)); +} + +#[test] +#[ignore] +fn bench_4k() { + bench(4096); +} + +#[test] +#[ignore] +fn bench_8k() { + bench(8192); +} diff --git a/crates/engine/tests/session.rs b/crates/engine/tests/session.rs new file mode 100644 index 00000000..612b8cd0 --- /dev/null +++ b/crates/engine/tests/session.rs @@ -0,0 +1,193 @@ +use serde_json::{Value, json}; +use texturecraft_engine::core::Channel; +use texturecraft_engine::geom::primitives; +use texturecraft_engine::{Session, command_specs, execute}; + +fn session() -> Session { + let mut s = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:sphere", None, 256).unwrap(); + s.viewport = texturecraft_engine::geom::glam::Vec2::new(400.0, 400.0); + s.camera.yaw = 0.0; + s.camera.pitch = 0.0; + s.update(); + s +} + +#[test] +fn scripted_stroke_paints_and_undo_restores() { + let mut s = session(); + execute(&mut s, "tool.material", &json!({"channel": "baseColor", "color": [1.0, 0.0, 0.0]})).unwrap(); + execute(&mut s, "tool.brush", &json!({"size": 20, "hardness": 90})).unwrap(); + let r = execute(&mut s, "tool.stroke", &json!({"points": [[180, 200], [220, 200]]})).unwrap(); + assert!(r["stamps"].as_u64().unwrap() > 1); + // The texel under the screen center is now red. + let hit = s.pick(texturecraft_engine::geom::glam::Vec2::new(200.0, 200.0), s.viewport).unwrap(); + let res = 256.0; + let (x, y) = ((hit.uv.x * res) as u32, (hit.uv.y * res) as u32); + let v = s.caches[0].composite.value(Channel::BaseColor, x, y); + assert!(v[0] > 0.9 && v[1] < 0.1, "{v:?}"); + execute(&mut s, "edit.undo", &Value::Null).unwrap(); + s.update(); + let v = s.caches[0].composite.value(Channel::BaseColor, x, y); + assert!((v[0] - 0.5).abs() < 0.01, "{v:?}"); + execute(&mut s, "edit.redo", &Value::Null).unwrap(); + s.update(); + assert!(s.caches[0].composite.value(Channel::BaseColor, x, y)[0] > 0.9); +} + +#[test] +fn layers_masks_materials_and_bake() { + let mut s = session(); + execute(&mut s, "layer.add.fill", &json!({"color": "#336699", "roughness": 0.2})).unwrap(); + execute(&mut s, "mask.add.black", &Value::Null).unwrap(); + execute(&mut s, "effect.add.generator", &json!({"generator": "position", "level": 0.5})).unwrap(); + execute(&mut s, "bake.run", &json!({"resolution": 128, "rays": 8})).unwrap(); + execute(&mut s, "material.apply", &json!({"material": "steel_painted_worn"})).unwrap(); + execute(&mut s, "layer.group", &Value::Null).unwrap(); + execute(&mut s, "layer.duplicate", &Value::Null).unwrap(); + let inspect = execute(&mut s, "project.inspect", &Value::Null).unwrap(); + assert!(inspect["layers"].as_array().unwrap().len() >= 3, "{inspect}"); + s.doc.project.validate().unwrap(); + s.update(); + for _ in 0..7 { + execute(&mut s, "edit.undo", &Value::Null).unwrap(); + } + assert_eq!(s.doc.project.walk(0).len(), 1, "back to the initial paint layer"); +} + +#[test] +fn polygon_fill_and_color_picker() { + let mut s = session(); + execute(&mut s, "tool.material", &json!({"channel": "roughness", "value": 0.9})).unwrap(); + execute(&mut s, "tool.polygonFill", &Value::Null).unwrap(); + execute(&mut s, "tool.fillMode", &json!({"mode": "mesh"})).unwrap(); + execute(&mut s, "tool.click", &json!({"x": 200, "y": 200})).unwrap(); + execute(&mut s, "tool.colorPicker", &Value::Null).unwrap(); + let r = execute(&mut s, "tool.click", &json!({"x": 200, "y": 200})).unwrap(); + assert!((r["roughness"][0].as_f64().unwrap() - 0.9).abs() < 0.01, "{r}"); +} + +/// Every command must return Ok or Err — never panic — for hostile params. +#[test] +fn panic_hunt() { + let hostile = [ + Value::Null, + json!({}), + json!({"layer": u64::MAX, "ts": 99, "channel": "nope", "mode": "???", "opacity": f64::MAX}), + json!({"layer": 1, "index": -5, "delta": i64::MIN, "above": "x", "points": [[f64::MAX, -1e300]], "size": -1e30}), + json!({"resolution": 0, "maps": ["x"], "rays": u64::MAX, "antialiasing": 3, "color": [1], "value": "nan"}), + json!({"effect": 0, "x": 1e30, "y": -1e30, "viewport": [0, 0], "mesh": "../../etc", "material": ""}), + ]; + for spec in command_specs() { + if spec.id == "project.sample" { + continue; // slow (bakes); covered elsewhere + } + for p in &hostile { + let mut s = session(); + let _ = execute(&mut s, spec.id, p); + s.update(); + } + } +} + +#[test] +fn copy_paste_flatten_and_resize_keep_the_look() { + let mut s = Session::new_project("t", primitives::material_ball(), "builtin:material_ball", None, 256).unwrap(); + s.viewport = texturecraft_engine::geom::glam::Vec2::new(400.0, 400.0); + execute(&mut s, "layer.add.fill", &json!({"color": [0.9, 0.1, 0.1], "roughness": 0.3})).unwrap(); + execute(&mut s, "mask.add.black", &Value::Null).unwrap(); + execute(&mut s, "effect.add.fill", &json!({"procedural": "checker", "scale": 4})).unwrap(); + s.update(); + let before: Vec<[f32; 3]> = (0..256).step_by(9).map(|x| s.caches[0].composite.value(Channel::BaseColor, x, 100)).collect(); + // Flatten keeps the composite identical (within 8-bit-ish tolerance) and makes a paint layer. + let id = s.selected_layer().unwrap(); + execute(&mut s, "layer.flatten", &json!({"layer": id.0})).unwrap(); + s.update(); + let after: Vec<[f32; 3]> = (0..256).step_by(9).map(|x| s.caches[0].composite.value(Channel::BaseColor, x, 100)).collect(); + for (a, b) in before.iter().zip(&after) { + for k in 0..3 { + assert!((a[k] - b[k]).abs() < 2e-3, "{a:?} {b:?}"); + } + } + assert!(matches!(s.doc.project.layer(id).unwrap().kind, texturecraft_engine::core::LayerKind::Paint)); + // Copy into the other texture set (different resolution). + execute(&mut s, "textureSet.resolution", &json!({"ts": 1, "resolution": 128})).unwrap(); + execute(&mut s, "layer.copy", &Value::Null).unwrap(); + execute(&mut s, "textureSet.select", &json!({"ts": 1})).unwrap(); + let r = execute(&mut s, "layer.paste", &Value::Null).unwrap(); + let pasted = texturecraft_engine::core::LayerId(r["layer"].as_u64().unwrap()); + assert_eq!(s.doc.pixels.layer(pasted, Channel::BaseColor).unwrap().width(), 128); + // Resizing resamples instead of clearing. + execute(&mut s, "textureSet.resolution", &json!({"ts": 0, "resolution": 512})).unwrap(); + assert_eq!(s.doc.pixels.layer(id, Channel::BaseColor).unwrap().width(), 512); + s.update(); + let big = s.caches[0].composite.value(Channel::BaseColor, 2 * 9 * 5, 200); + let small = before[5]; + assert!((big[0] - small[0]).abs() < 0.2, "{big:?} {small:?}"); + execute(&mut s, "layer.instantiateAll", &json!({"layer": id.0})).unwrap(); + s.doc.project.validate().unwrap(); +} + +#[test] +fn image_resources_fill_layers_and_round_trip() { + let mut s = session(); + let mut png = Vec::new(); + let img = image::RgbaImage::from_fn(8, 8, |_, _| image::Rgba([255, 0, 0, 255])); + img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap(); + let name = s.import_resource_bytes("C:/x/red bricks.png", png).unwrap(); + assert_eq!(name, "red_bricks.png"); + let r = execute(&mut s, "layer.add.image", &json!({"name": name})).unwrap(); + assert!(r["layer"].as_u64().is_some()); + s.update(); + let v = s.caches[0].composite.value(Channel::BaseColor, 128, 128); + assert!(v[0] > 0.95 && v[1] < 0.05, "{v:?}"); + let bytes = s.to_project_bytes().unwrap(); + let back = Session::from_project_bytes(&bytes).unwrap(); + assert_eq!(back.doc.resources.len(), 1); + assert_eq!(back.images.len(), 1); + execute(&mut s, "edit.undo", &Value::Null).unwrap(); + execute(&mut s, "edit.undo", &Value::Null).unwrap(); + assert!(s.images.is_empty(), "undoing the import forgets the decoded image"); + assert!(s.import_resource_bytes("bad.png", vec![1, 2, 3]).is_err()); +} + +#[test] +fn projection_tool_paints_the_stencil_colors() { + let mut s = session(); + let mut png = Vec::new(); + let img = image::RgbaImage::from_fn(16, 16, |x, _| if x < 8 { image::Rgba([255, 0, 0, 255]) } else { image::Rgba([0, 255, 0, 255]) }); + img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap(); + let name = s.import_resource_bytes("split.png", png).unwrap(); + execute(&mut s, "tool.projection", &Value::Null).unwrap(); + // Without a stencil the tool explains what's missing. + assert!(execute(&mut s, "tool.stroke", &json!({"points": [[150, 200]]})).is_err()); + execute(&mut s, "tool.stencil", &json!({"image": name, "scale": 1.0, "tile": true})).unwrap(); + execute(&mut s, "tool.brush", &json!({"size": 6, "hardness": 90})).unwrap(); + for x in [150.0f32, 250.0] { + execute(&mut s, "tool.stroke", &json!({"points": [[x, 200.0]]})).unwrap(); + } + let vp = s.viewport; + let at = |s: &Session, x: f32| { + let hit = s.pick(texturecraft_engine::geom::glam::Vec2::new(x, 200.0), vp).unwrap(); + s.caches[0].composite.value(Channel::BaseColor, (hit.uv.x * 256.0) as u32, (hit.uv.y * 256.0) as u32) + }; + let left = at(&s, 150.0); + let right = at(&s, 250.0); + assert!(left[0] > 0.8 && left[1] < 0.2, "left {left:?}"); + assert!(right[1] > 0.8 && right[0] < 0.2, "right {right:?}"); +} + +#[test] +fn smart_materials_save_and_load_with_pixels() { + let mut s = Session::new_project("t", primitives::material_ball(), "builtin:material_ball", None, 256).unwrap(); + s.viewport = texturecraft_engine::geom::glam::Vec2::new(400.0, 400.0); + let group = texturecraft_engine::materials::apply(&mut s, "steel_painted_worn").unwrap(); + let before = s.doc.project.walk(0).len(); + let bytes = s.smart_material_bytes(group, "My Steel").unwrap(); + execute(&mut s, "textureSet.select", &json!({"ts": 1})).unwrap(); + let id = s.add_smart_material_bytes(&bytes).unwrap(); + assert_eq!(s.doc.project.layer(id).unwrap().name, "My Steel"); + let walked = s.doc.project.walk(1).len(); + assert_eq!(walked, 1 + (before - 1), "the folder and its children arrive"); + assert!(s.add_smart_material_bytes(b"junk").is_err()); + s.doc.project.validate().unwrap(); +} diff --git a/crates/engine/tests/tools.rs b/crates/engine/tests/tools.rs new file mode 100644 index 00000000..d56d67a8 --- /dev/null +++ b/crates/engine/tests/tools.rs @@ -0,0 +1,130 @@ +//! Clone, smudge, image alphas and the quick mask, driven through engine commands the way +//! agents paint. + +use serde_json::{Value, json}; +use texturecraft_engine::core::Channel; +use texturecraft_engine::geom::glam::Vec2; +use texturecraft_engine::geom::primitives; +use texturecraft_engine::{Session, execute}; + +const RES: f32 = 256.0; + +fn session() -> Session { + let mut s = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:sphere", None, 256).unwrap(); + s.viewport = Vec2::new(400.0, 400.0); + s.camera.yaw = 0.0; + s.camera.pitch = 0.0; + s.update(); + s +} + +/// Base color of the composite under a viewport point. +fn color_at(s: &Session, x: f32, y: f32) -> [f32; 3] { + let hit = s.pick(Vec2::new(x, y), s.viewport).unwrap(); + s.caches[0].composite.value(Channel::BaseColor, (hit.uv.x * RES) as u32, (hit.uv.y * RES) as u32) +} + +fn red(v: [f32; 3]) -> bool { + v[0] > 0.85 && v[1] < 0.15 +} + +fn dab(s: &mut Session, x: f32, y: f32) { + execute(s, "tool.stroke", &json!({"points": [[x, y]]})).unwrap(); +} + +fn paint_red(s: &mut Session, size: f32) { + execute(s, "tool.paint", &Value::Null).unwrap(); + execute(s, "tool.material", &json!({"channel": "baseColor", "color": [1.0, 0.0, 0.0]})).unwrap(); + execute(s, "tool.brush", &json!({"size": size, "hardness": 90})).unwrap(); +} + +#[test] +fn clone_copies_values_from_the_source_offset() { + let mut s = session(); + paint_red(&mut s, 20.0); + dab(&mut s, 160.0, 200.0); + assert!(red(color_at(&s, 160.0, 200.0))); + assert!(!red(color_at(&s, 240.0, 200.0))); + + execute(&mut s, "tool.clone", &Value::Null).unwrap(); + assert!(execute(&mut s, "tool.stroke", &json!({"points": [[240, 200]]})).is_err(), "no source yet"); + execute(&mut s, "tool.cloneSource", &json!({"x": 160, "y": 200})).unwrap(); + execute(&mut s, "tool.brush", &json!({"size": 6})).unwrap(); + dab(&mut s, 240.0, 200.0); + let v = color_at(&s, 240.0, 200.0); + assert!(red(v), "cloned red: {v:?}"); + // The offset stays for the next strokes: the source moved along with the destination. + let off = s.tool.clone_source.unwrap().offset.unwrap(); + assert!(off[0].abs() > 0.01, "{off:?}"); + // One undo step per clone stroke. + execute(&mut s, "edit.undo", &Value::Null).unwrap(); + s.update(); + assert!(!red(color_at(&s, 240.0, 200.0))); +} + +#[test] +fn smudge_drags_color_along_the_stroke() { + let mut s = session(); + paint_red(&mut s, 12.0); + dab(&mut s, 170.0, 200.0); + let before = color_at(&s, 205.0, 200.0); + assert!(!red(before), "{before:?}"); + execute(&mut s, "tool.smudge", &Value::Null).unwrap(); + execute(&mut s, "tool.brush", &json!({"size": 10, "smudgeStrength": 100, "spacing": 5})).unwrap(); + let pts: Vec<[f32; 2]> = (0..=40).map(|i| [170.0 + i as f32, 200.0]).collect(); + execute(&mut s, "tool.stroke", &json!({"points": pts})).unwrap(); + let after = color_at(&s, 205.0, 200.0); + assert!(after[0] - before[0] > 0.2 && after[1] < before[1], "red dragged right: {before:?} -> {after:?}"); +} + +#[test] +fn image_alpha_shapes_the_stamp() { + let mut s = session(); + let mut png = Vec::new(); + // Top half white, bottom half black. + let img = image::RgbaImage::from_fn(16, 16, |_, y| if y < 8 { image::Rgba([255; 4]) } else { image::Rgba([0, 0, 0, 255]) }); + img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap(); + let name = s.import_resource_bytes("half.png", png).unwrap(); + paint_red(&mut s, 20.0); + assert!(execute(&mut s, "tool.brush", &json!({"alpha": {"image": "missing.png"}})).is_err()); + let r = execute(&mut s, "tool.brush", &json!({"alpha": {"image": name}})).unwrap(); + assert_eq!(r["alpha"]["image"], json!(name)); + execute(&mut s, "tool.strokeUv", &json!({"points": [[0.5, 0.5]]})).unwrap(); + // In the 2D view +v points down: the image's top half lands above the center. + let at = |s: &Session, dy: i32| s.caches[0].composite.value(Channel::BaseColor, 128, (128 + dy) as u32); + assert!(red(at(&s, -12)), "{:?}", at(&s, -12)); + assert!(!red(at(&s, 12)), "{:?}", at(&s, 12)); + // A procedural alpha clears the image. + let r = execute(&mut s, "tool.brush", &json!({"alpha": "round"})).unwrap(); + assert_eq!(r["alpha"], json!("Round")); +} + +#[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(); + + 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"); + + 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"); + + 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"); +} diff --git a/crates/formats/Cargo.toml b/crates/formats/Cargo.toml new file mode 100644 index 00000000..726d26b9 --- /dev/null +++ b/crates/formats/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "texturecraft-formats" +description = "TextureCraft file formats: mesh import (glTF, FBX, COLLADA, OBJ, PLY, STL), glTF/OBJ mesh export, texture export presets, the .tcraft project file" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lints] +workspace = true + +[dependencies] +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 } +quick-xml = { workspace = true } +zip = { workspace = true } +exr = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } +rayon = { workspace = true } diff --git a/crates/formats/README.md b/crates/formats/README.md new file mode 100644 index 00000000..c588341a --- /dev/null +++ b/crates/formats/README.md @@ -0,0 +1,131 @@ +# texturecraft-formats + +File formats for TextureCraft: mesh import and export, texture export with presets, and the +`.tcraft` project file. Every reader takes untrusted bytes and returns `Result`; nothing panics on bad +input. Byte-based functions work on every target (the web build loads and saves through bytes); +path-based functions are native only (`#[cfg(not(target_arch = "wasm32"))]`). + +## Public API + +### Mesh import (`mesh_import`) + +```rust +pub fn import_mesh_bytes(file_name: &str, bytes: &[u8]) -> Result; +pub fn import_mesh(path: &Path) -> Result; // native +pub struct ImportedMesh { pub mesh: Mesh, pub report: MeshReport } +pub struct MeshReport { + pub triangles: usize, pub vertices: usize, pub materials: usize, + pub uvs_generated: bool, pub uv_out_of_range: usize, pub overlap_ratio: f32, + pub zero_area_uv_triangles: usize, pub warnings: Vec, +} +``` + +| Format | Notes | +|---|---| +| `.glb` / `.gltf` | Default scene's node tree flattened to world space; normals by inverse transpose; winding flipped under mirroring transforms; one submesh per material (`Default` for none, `Material_` when unnamed); missing normals are flat (glTF rule). Bytes: GLB or data-URI buffers; from a path, external `.bin` files too. Triangle primitives only; sparse accessors are refused. | +| `.fbx` | FBX 7.x (2011 and newer), binary (32- and 64-bit node records, zlib arrays) and ASCII. Every `Mesh` geometry connected to a Model, with the Model hierarchy's transforms (`Lcl Translation/Rotation/Scaling`, `RotationOrder`, `Pre/PostRotation`, pivots and offsets, geometric transform); normals and UV set 0 from the layer elements (`ByPolygonVertex`, `ByControlPoint`, `ByPolygon`, `AllSame`; `Direct` / `IndexToDirect`); per-polygon materials named by the Materials connected to the Model. Units (`UnitScaleFactor`, cm by default) become meters and the axis system becomes Y up. FBX 6 and older are refused; decoded data is capped at 2 GiB. | +| `.dae` | COLLADA: `triangles`, `polylist`, `polygons` (holes ignored) with `VERTEX` / `NORMAL` / `TEXCOORD` inputs at any offset (lowest `set`); materials by `bind_material` symbol; the visual scene's node tree (`matrix`, `translate`, `rotate`, `scale`, `instance_node`, skinned `instance_controller` with its bind shape); `unit` to meters, `Z_UP` / `X_UP` to Y up. Lines and strips are skipped with a warning. | +| `.obj` | Triangulated, one vertex per unique `v/vt/vn`, **v flipped** (OBJ is bottom-left). Material names come from `usemtl` (MTL files are never read; they only carry shading data). | +| `.ply` | ascii, binary little/big endian; `x y z`, `nx ny nz`, `s t` / `u v` / `texture_u texture_v`; n-gons fan-triangulated. | +| `.stl` | ascii and binary (size-checked, so binary files whose header says `solid` work); flat normals; no UVs. | + +FBX and COLLADA polygons are fan-triangulated with one vertex per unique corner. Missing +normals (OBJ/PLY/FBX/COLLADA) are smooth over welded positions. Missing UVs run +`texturecraft_geom::unwrap::auto_unwrap` (charts within 60° of their seed normal, principal-axis +rotation, shelf packing per material with one texel density). Tangents are MikkTSpace. Limits: +2 GiB files, `MAX_TRIANGLES` triangles, 3 × that many vertices; meshes whose triangles are all +degenerate are refused (MikkTSpace crashes on them). + +### Mesh export (`mesh_export`) + +```rust +pub fn obj_bytes(mesh: &Mesh, name: &str) -> Vec; +pub fn glb_bytes(mesh: &Mesh, name: &str, textures: &[GlbTextures]) -> Result>; +pub fn glb_export_options() -> ExportOptions; // render GlbTextures with these +pub struct GlbTextures { pub base_color, pub orm, pub normal, pub emissive: Option> } // PNG +``` + +GLB: one primitive per submesh, one metallic-roughness material per mesh material with its +textures embedded in the binary chunk (base color sRGB, `alphaMode: BLEND` when it has alpha; +ORM as occlusion + metallic-roughness; OpenGL normal; emissive only when not black). + +### Texture export (`export`, `presets`) + +```rust +pub fn list_outputs(project: &Project, ts: usize, mesh_name: &str, opts: &ExportOptions) -> Result>; +pub fn render_maps(input: &ExportInput, opts: &ExportOptions) -> Result>; +pub fn write_files(files: &[ExportedFile], dir: &Path) -> Result>; // native, atomic +pub fn builtin_presets() -> Vec; +pub fn builtin_preset(name: &str) -> Option; +pub fn preset_from_json(json: &str) -> Result; +pub fn preset_to_json(preset: &ExportPreset) -> Result; +``` + +- `ExportPreset { name, maps: Vec }`, `MapSpec { file, format, bit_depth, channels: Vec<(Dest, SourceRef)> }` + (serde JSON, so presets can be user files). +- `Dest`: `R G B A` or `L` (gray; `L` + `A` = gray + alpha). +- `SourceRef`: `Channel { channel, component }` (`Normal` = the **final** normal in the export + normal format), `Virtual { map, component }` (`NormalOpenGl`, `NormalDirectX`, `Glossiness` = + 1 − roughness, `AoMixed` = baked AO × AO channel, `F0` = 0.08 × specular level), + `Baked { map, component }`, `Constant(f32)`. +- `ExportOptions { preset, format_override, bit_depth_override, padding, size, normal_format }`; + `Padding::{None, Dilation(n), DilationDefault(n), Infinite}`; `size` must be a power of two + (box filter down, bilinear up). +- File templates: `$project`, `$mesh`, `$textureSet`, `$channel`, `$colorSpace`; names are + sanitized to `[A-Za-z0-9_.-]` (reserved device names get a `_` prefix); case-insensitive + collisions get `_2`, `_3`, … plus a warning. +- A map is written only when one of its sources has data in the texture set (channel present, + map baked, …); alpha from a missing channel is dropped. +- Formats: PNG 8/16, JPEG 8 (q 95), TGA 8, TIFF 8/16, BMP 8, WebP 8 (lossless), EXR 16f/32f + (`Y`, `YA`, `RGB`, `RGBA`, ZIP), HDR 32f. Color maps are sRGB in 8/16-bit and linear in float + formats; data maps are linear; height is stored as `h · 0.5 + 0.5`. +- Final normal: Sobel normal from the height channel (strength = `height_strength`; with 1, a + height change of 1 over 1/16 of the texture width tilts 45°; `+Y` up the image), combined with + the normal channel, then with the baked normal map, by Reoriented Normal Mapping. DirectX + flips green. + +Built-in presets: + +| Preset | Files (`$mesh_$textureSet_…`) | +|---|---| +| Document channels + Normal + AO | one per document channel (`$channel`), plus `normal` and `ambientOcclusion` (mixed) | +| glTF PBR Metal Roughness | `BaseColor` (sRGB, A = opacity), `OcclusionRoughnessMetallic`, `Normal` (OpenGL), `Emissive` (if present) | +| Unreal Engine (Packed) | `BaseColor`, `OcclusionRoughnessMetallic`, `Normal` (DirectX) | +| Unity HDRP (Mask Map) | `BaseMap`, `MaskMap` (R metal, G AO, B detail = 0, A smoothness), `Normal` (OpenGL) | +| Unity Standard (Metallic Smoothness) | `Albedo`, `MetallicSmoothness` (R metal, A smoothness), `Normal` (OpenGL), `Occlusion` | +| Blender (Principled BSDF) | `BaseColor`, `Metallic`, `Roughness`, `Normal` (OpenGL), `Height` (16-bit), `Emission`, `Specular` | +| Separate maps | one file per channel, gray where scalar, height 16-bit | +| Mesh maps | every baked map (`$channel` = map id), 16-bit | + +### Project file (`project_file`) + +```rust +pub struct ProjectFile { + pub project: Project, pub pixels: PixelStore, pub baked: BTreeMap, + pub mesh: Option<(String /* ext */, Vec)>, pub thumbnail: Option>, +} +pub fn save_project_bytes(pf: &ProjectFile) -> Result>; +pub fn load_project_bytes(bytes: &[u8]) -> Result; +pub fn save_project(path: &Path, pf: &ProjectFile) -> Result<()>; // native, atomic +pub fn load_project(path: &Path) -> Result; // native +``` + +`.tcraft` layout (zip, deflate): + +| Entry | Content | +|---|---| +| `project.json` | serde JSON of `Project` | +| `mesh.` | the original mesh file bytes (absent for `builtin:` meshes) | +| `pixels/layer__.bin` | painted layer (or content paint effect) channel | +| `pixels/mask_.bin` | painted mask effect | +| `baked/_.bin` | baked mesh map of texture set `` | +| `thumbnail.png` | optional, stored | + +`.bin`: header `TCTX`, version, width, height, components, tile count (u32 LE each), then per +allocated 64×64 tile `tx`, `ty` (u32 LE) and the raw `u16` LE components (sparse, lossless). +Baked maps: the same header with 0 tiles, then `width × height × components` f32 LE. + +Loading runs `Project::validate`, refuses a newer `format_version` (`FormatError::NewerVersion`), +checks every tile against its texture set's resolution and component count, rejects repeated or +out-of-range tiles and trailing data, drops pixels of layers that are in no stack, and caps the +total decompressed size at 8 GiB. diff --git a/crates/formats/src/export/mod.rs b/crates/formats/src/export/mod.rs new file mode 100644 index 00000000..1879ea74 --- /dev/null +++ b/crates/formats/src/export/mod.rs @@ -0,0 +1,698 @@ +//! Texture export: render the maps of an [`ExportPreset`] from a texture set's composite, baked +//! mesh maps and UV coverage, entirely in memory (so the web build can export too). +//! +//! Each [`MapSpec`] packs sources into the R, G, B, A (or L) channels of one file. Sources are +//! composite channels, virtual maps (final normal in OpenGL or DirectX convention, glossiness, +//! mixed AO, F0), baked mesh maps and constants. The final normal is the baked normal, the +//! composited normal channel and the normal derived from height combined with Reoriented Normal +//! Mapping. Color maps are sRGB-encoded in 8/16-bit formats and linear in float formats; data +//! maps are always linear. Height is stored with 0.5 = zero (`h * 0.5 + 0.5`) in every format. + +use std::collections::BTreeSet; +use std::io::Cursor; + +use rayon::prelude::*; +use serde::{Deserialize, Serialize}; +use texturecraft_compose::{BakedMaps, Composite}; +use texturecraft_core::{AoMixing, BakedMap, Channel, ChannelKind, NormalFormat, NormalMixing, Project, TextureSet}; +use texturecraft_geom::glam::Vec3; +use texturecraft_texel::{DilationMap, linear_to_srgb}; + +use crate::{FormatError, MAX_IMAGE_SIZE, Result}; + +/// Height-to-normal slope: with strength 1, a height change of 1 across 1/16 of the texture +/// width tilts the normal 45°, at any resolution. +const HEIGHT_SPAN: f32 = 16.0; +const JPEG_QUALITY: u8 = 95; +/// Preset limits (user presets are untrusted JSON). +const MAX_MAPS: usize = 64; +const MAX_TEMPLATE: usize = 256; +const MAX_STEM: usize = 200; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum FileFormat { + Png, + Jpeg, + Tga, + Tiff, + Bmp, + Webp, + Exr, + Hdr, +} + +impl FileFormat { + pub const ALL: [FileFormat; 8] = + [FileFormat::Png, FileFormat::Jpeg, FileFormat::Tga, FileFormat::Tiff, FileFormat::Bmp, FileFormat::Webp, FileFormat::Exr, FileFormat::Hdr]; + + pub fn extension(self) -> &'static str { + match self { + FileFormat::Png => "png", + FileFormat::Jpeg => "jpg", + FileFormat::Tga => "tga", + FileFormat::Tiff => "tif", + FileFormat::Bmp => "bmp", + FileFormat::Webp => "webp", + FileFormat::Exr => "exr", + FileFormat::Hdr => "hdr", + } + } + + pub fn from_extension(ext: &str) -> Option { + let ext = ext.trim_start_matches('.').to_ascii_lowercase(); + match ext.as_str() { + "jpeg" => Some(FileFormat::Jpeg), + "tiff" => Some(FileFormat::Tiff), + _ => FileFormat::ALL.into_iter().find(|f| f.extension() == ext), + } + } + + /// Bit depths this format is written with (16 and 32 are half and full float for EXR). + pub fn bit_depths(self) -> &'static [u8] { + match self { + FileFormat::Png | FileFormat::Tiff => &[8, 16], + FileFormat::Exr => &[16, 32], + FileFormat::Hdr => &[32], + _ => &[8], + } + } + + /// Floating-point formats hold linear values. + pub fn is_float(self) -> bool { + matches!(self, FileFormat::Exr | FileFormat::Hdr) + } + + /// The supported depth closest to `want` (ties go to the deeper one). + pub fn depth(self, want: u8) -> u8 { + self.bit_depths().iter().copied().min_by_key(|d| (d.abs_diff(want), std::cmp::Reverse(*d))).unwrap_or(8) + } +} + +/// Destination channel of an output file. `L` alone writes grayscale; `L` + `A` gray + alpha. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +pub enum Dest { + R, + G, + B, + A, + L, +} + +/// Maps computed at export time from several channels. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum VirtualMap { + /// Final tangent-space normal, +Y up the image. + NormalOpenGl, + /// Final tangent-space normal, green flipped. + NormalDirectX, + /// 1 − roughness. + Glossiness, + /// Baked AO × AO channel (per the texture set's AO mixing). + AoMixed, + /// Dielectric reflectance at normal incidence: 0.08 × specular level. + F0, +} + +impl VirtualMap { + pub fn id(self) -> &'static str { + match self { + VirtualMap::NormalOpenGl | VirtualMap::NormalDirectX => "normal", + VirtualMap::Glossiness => "glossiness", + VirtualMap::AoMixed => "ambientOcclusion", + VirtualMap::F0 => "f0", + } + } +} + +/// Where one output component comes from. `component` picks r/g/b of 3-component sources. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum SourceRef { + /// A composited channel. `Normal` means the final normal in the export normal format. + Channel { + channel: Channel, + #[serde(default)] + component: u8, + }, + Virtual { + map: VirtualMap, + #[serde(default)] + component: u8, + }, + Baked { + map: BakedMap, + #[serde(default)] + component: u8, + }, + /// A constant (0 = black, 0.5 = gray, 1 = white). + Constant(f32), +} + +impl SourceRef { + fn component(&self) -> usize { + match self { + SourceRef::Channel { component, .. } | SourceRef::Virtual { component, .. } | SourceRef::Baked { component, .. } => (*component).min(2) as usize, + SourceRef::Constant(_) => 0, + } + } +} + +/// One exported file. `file` is a name template without extension (see [`list_outputs`]). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct MapSpec { + pub file: String, + pub format: FileFormat, + pub bit_depth: u8, + pub channels: Vec<(Dest, SourceRef)>, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct ExportPreset { + pub name: String, + pub maps: Vec, +} + +impl ExportPreset { + /// Structural checks for (untrusted) presets. + pub fn validate(&self) -> Result<()> { + let bad = |m: String| Err(FormatError::Invalid(format!("export preset \"{}\": {m}", self.name))); + if self.maps.len() > MAX_MAPS { + return bad(format!("more than {MAX_MAPS} maps")); + } + for m in &self.maps { + if m.file.trim().is_empty() || m.file.len() > MAX_TEMPLATE { + return bad(format!("file template \"{}\" is empty or too long", m.file)); + } + let dests: BTreeSet = m.channels.iter().map(|(d, _)| *d).collect(); + if dests.len() != m.channels.len() || m.channels.is_empty() { + return bad(format!("map \"{}\" needs distinct destination channels", m.file)); + } + if dests.contains(&Dest::L) && dests.iter().any(|d| matches!(d, Dest::R | Dest::G | Dest::B)) { + return bad(format!("map \"{}\" mixes L with R/G/B", m.file)); + } + if m.channels.iter().any(|(_, s)| matches!(s, SourceRef::Constant(v) if !v.is_finite())) { + return bad(format!("map \"{}\" has a non-finite constant", m.file)); + } + } + Ok(()) + } +} + +/// UV padding of exported maps (texels outside the UV islands). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum Padding { + /// Uncovered texels are zero (transparent where there is alpha). + None, + /// Islands are dilated this many texels; beyond, texels are zero. + Dilation(u32), + /// Islands are dilated this many texels; beyond, texels hold the channel default. + DilationDefault(u32), + /// Every texel takes its nearest island's value. + Infinite, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct ExportOptions { + pub preset: ExportPreset, + /// Write every map in this format instead of the preset's. + pub format_override: Option, + pub bit_depth_override: Option, + pub padding: Padding, + /// Output size (power of two); None keeps the texture set resolution. + pub size: Option, + /// Normal convention for every normal output instead of the project's / preset's. + pub normal_format: Option, +} + +impl ExportOptions { + pub fn new(preset: ExportPreset) -> ExportOptions { + ExportOptions { preset, format_override: None, bit_depth_override: None, padding: Padding::Infinite, size: None, normal_format: None } + } +} + +/// What is exported: one texture set's composite with its baked maps and UV coverage +/// (`resolution²` flags, row-major). +#[derive(Clone, Copy)] +pub struct ExportInput<'a> { + pub project: &'a Project, + pub ts: usize, + pub composite: &'a Composite, + pub baked: &'a BakedMaps, + pub coverage: &'a [bool], + pub mesh_name: &'a str, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExportedFile { + pub name: String, + pub bytes: Vec, + pub width: u32, + pub height: u32, + pub warnings: Vec, +} + +/// A file a preset produces for a texture set (see [`list_outputs`]). +#[derive(Clone, Debug, PartialEq)] +pub struct PlannedFile { + pub name: String, + /// Index into the preset's maps. + pub map: usize, + pub format: FileFormat, + pub bit_depth: u8, + pub warnings: Vec, +} + +// --------------------------------------------------------------------------------------------- +// Planning: which maps exist for a texture set and what their files are called. + +fn normal_present(set: &TextureSet) -> bool { + set.has_channel(Channel::Normal) || set.has_channel(Channel::Height) || set.baked.contains(&BakedMap::Normal) +} + +/// A source with real data in this texture set (constants never count). +fn source_present(set: &TextureSet, s: &SourceRef) -> bool { + match s { + SourceRef::Channel { channel: Channel::Normal, .. } => normal_present(set), + SourceRef::Channel { channel, .. } => set.has_channel(*channel), + SourceRef::Virtual { map: VirtualMap::NormalOpenGl | VirtualMap::NormalDirectX, .. } => normal_present(set), + SourceRef::Virtual { map: VirtualMap::Glossiness, .. } => set.has_channel(Channel::Roughness), + SourceRef::Virtual { map: VirtualMap::AoMixed, .. } => set.has_channel(Channel::AmbientOcclusion) || set.baked.contains(&BakedMap::AmbientOcclusion), + SourceRef::Virtual { map: VirtualMap::F0, .. } => set.has_channel(Channel::SpecularLevel), + SourceRef::Baked { map, .. } => set.baked.contains(map), + SourceRef::Constant(_) => false, + } +} + +/// The written components in order: L | L A | R G B | R G B A. Alpha from a channel the set +/// does not have is dropped. +fn layout<'m>(set: &TextureSet, map: &'m MapSpec) -> Vec<(Dest, &'m SourceRef)> { + let get = |d: Dest| map.channels.iter().find(|(x, _)| *x == d).map(|(_, s)| s); + let alpha = get(Dest::A).filter(|s| !matches!(s, SourceRef::Channel { channel, .. } if !set.has_channel(*channel))); + let black = &SourceRef::Constant(0.0); + let mut out = match get(Dest::L) { + Some(l) => vec![(Dest::L, l)], + None => [Dest::R, Dest::G, Dest::B].into_iter().map(|d| (d, get(d).unwrap_or(black))).collect(), + }; + if let Some(a) = alpha { + out.push((Dest::A, a)); + } + out +} + +fn is_color(map: &MapSpec) -> bool { + map.channels.iter().any(|(d, s)| *d != Dest::A && matches!(s, SourceRef::Channel { channel, .. } if channel.kind() == ChannelKind::Color)) +} + +fn channel_label(map: &MapSpec) -> &'static str { + map.channels + .iter() + .find_map(|(_, s)| match s { + SourceRef::Channel { channel, .. } => Some(channel.id()), + SourceRef::Virtual { map, .. } => Some(map.id()), + SourceRef::Baked { map, .. } => Some(map.id()), + SourceRef::Constant(_) => None, + }) + .unwrap_or("constant") +} + +/// Keep `[A-Za-z0-9_.-]`, replace everything else with `_`; never empty or a reserved device name. +pub fn sanitize_file_stem(s: &str) -> String { + let mut out: String = s.chars().take(MAX_STEM).map(|c| if c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '-') { c } else { '_' }).collect(); + if out.trim_matches('.').is_empty() { + out = "texture".into(); + } + let device = out.split('.').next().unwrap_or_default().to_ascii_uppercase(); + let reserved = matches!(device.as_str(), "CON" | "PRN" | "AUX" | "NUL") + || (device.len() == 4 && (device.starts_with("COM") || device.starts_with("LPT")) && device.ends_with(|c: char| c.is_ascii_digit())); + if reserved { + out.insert(0, '_'); + } + out +} + +/// The files `opts` produces for texture set `ts`, without rendering (for the export dialog). +/// Template variables: `$project`, `$mesh`, `$textureSet`, `$channel`, `$colorSpace`. +pub fn list_outputs(project: &Project, ts: usize, mesh_name: &str, opts: &ExportOptions) -> Result> { + opts.preset.validate()?; + let set = project.texture_set(ts)?; + if let Some(s) = opts.size + && (!s.is_power_of_two() || s > MAX_IMAGE_SIZE) + { + return Err(FormatError::Invalid(format!("export size {s} must be a power of two up to {MAX_IMAGE_SIZE}"))); + } + let mut used = BTreeSet::new(); + let mut out = Vec::new(); + for (i, map) in opts.preset.maps.iter().enumerate() { + if !map.channels.iter().any(|(_, s)| source_present(set, s)) { + continue; + } + let format = opts.format_override.unwrap_or(map.format); + let bit_depth = format.depth(opts.bit_depth_override.unwrap_or(map.bit_depth)); + let color_space = if is_color(map) && !format.is_float() { "sRGB" } else { "linear" }; + let stem = map + .file + .replace("$project", &project.name) + .replace("$mesh", mesh_name) + .replace("$textureSet", &set.name) + .replace("$channel", channel_label(map)) + .replace("$colorSpace", color_space); + let stem = sanitize_file_stem(&stem); + let ext = format.extension(); + let mut name = format!("{stem}.{ext}"); + let mut warnings = Vec::new(); + // Case-insensitive: Windows and macOS file systems are. + let mut k = 2; + while used.contains(&name.to_ascii_lowercase()) { + name = format!("{stem}_{k}.{ext}"); + k += 1; + } + if k > 2 { + warnings.push(format!("\"{stem}.{ext}\" is produced by more than one map; this one was renamed to \"{name}\"")); + } + used.insert(name.to_ascii_lowercase()); + out.push(PlannedFile { name, map: i, format, bit_depth, warnings }); + } + Ok(out) +} + +// --------------------------------------------------------------------------------------------- +// Sampling. + +/// Which texel a value is taken from. +#[derive(Clone, Copy, PartialEq)] +enum At { + Texel(u32, u32), + /// The channel default (padding beyond the dilation distance). + Default, +} + +struct Sampler<'a> { + set: &'a TextureSet, + comp: &'a Composite, + baked: &'a BakedMaps, + coverage: &'a [bool], + res: u32, + /// Normal convention overriding every normal output. + normal_override: Option, + project_normal: NormalFormat, +} + +fn unpack(v: [f32; 3]) -> Vec3 { + (Vec3::from(v) * 2.0 - Vec3::ONE).try_normalize().unwrap_or(Vec3::Z) +} + +/// Reoriented Normal Mapping: `detail` re-expressed around `base` (unit tangent-space vectors). +pub fn rnm(base: Vec3, detail: Vec3) -> Vec3 { + let t = base + Vec3::Z; + let u = detail * Vec3::new(-1.0, -1.0, 1.0); + (t * t.dot(u) / t.z.max(1e-4) - u).try_normalize().unwrap_or(base) +} + +impl Sampler<'_> { + fn channel(&self, c: Channel, at: At) -> [f32; 3] { + match at { + At::Texel(x, y) if self.set.has_channel(c) => self.comp.value(c, x, y), + _ => c.default_value(), + } + } + + fn baked(&self, m: BakedMap, at: At) -> [f32; 3] { + match at { + At::Texel(x, y) if self.set.baked.contains(&m) => self.baked.sample(m, x, y, self.res).unwrap_or(m.neutral()), + _ => m.neutral(), + } + } + + fn covered(&self, x: u32, y: u32) -> bool { + self.coverage.get((y as usize) * self.res as usize + x as usize).copied().unwrap_or(false) + } + + /// Normal from the height channel (3×3 Sobel; uncovered neighbours repeat the centre). + fn height_normal(&self, x: u32, y: u32) -> Vec3 { + let center = self.channel(Channel::Height, At::Texel(x, y))[0]; + let last = self.res.saturating_sub(1) as i64; + let h = |dx: i64, dy: i64| { + let (sx, sy) = ((x as i64 + dx).clamp(0, last) as u32, (y as i64 + dy).clamp(0, last) as u32); + if self.covered(sx, sy) { self.channel(Channel::Height, At::Texel(sx, sy))[0] } else { center } + }; + let gx = (h(1, -1) + 2.0 * h(1, 0) + h(1, 1) - h(-1, -1) - 2.0 * h(-1, 0) - h(-1, 1)) / 8.0; + let gy = (h(-1, 1) + 2.0 * h(0, 1) + h(1, 1) - h(-1, -1) - 2.0 * h(0, -1) - h(1, -1)) / 8.0; + let s = self.set.height_strength * self.res as f32 / HEIGHT_SPAN; + // Image y points down; OpenGL normals have +Y up the image. + Vec3::new(-gx * s, gy * s, 1.0).try_normalize().unwrap_or(Vec3::Z) + } + + /// The final tangent-space normal (OpenGL): baked normal ⊕ (height ⊕ normal channel). + fn final_normal(&self, at: At) -> Vec3 { + let At::Texel(x, y) = at else { return Vec3::Z }; + let base = if self.set.baked.contains(&BakedMap::Normal) { unpack(self.baked(BakedMap::Normal, at)) } else { Vec3::Z }; + let mut detail = if self.set.has_channel(Channel::Normal) { unpack(self.channel(Channel::Normal, at)) } else { Vec3::Z }; + if self.set.normal_mixing == NormalMixing::Combine && self.set.has_channel(Channel::Height) && self.set.height_strength != 0.0 { + detail = rnm(self.height_normal(x, y), detail); + } + rnm(base, detail) + } + + fn normal(&self, at: At, format: NormalFormat, cache: &mut Option) -> [f32; 3] { + let n = *cache.get_or_insert_with(|| self.final_normal(at)); + let y = if self.normal_override.unwrap_or(format) == NormalFormat::DirectX { -n.y } else { n.y }; + [n.x * 0.5 + 0.5, y * 0.5 + 0.5, n.z * 0.5 + 0.5] + } + + fn sample(&self, src: &SourceRef, at: At, cache: &mut Option) -> f32 { + let k = src.component(); + let v = match src { + SourceRef::Constant(v) => return *v, + SourceRef::Channel { channel: Channel::Normal, .. } => self.normal(at, self.project_normal, cache), + SourceRef::Channel { channel, .. } => { + let v = self.channel(*channel, at); + if channel.kind() == ChannelKind::Signed { v.map(|h| h * 0.5 + 0.5) } else { v } + } + SourceRef::Virtual { map, .. } => match map { + VirtualMap::NormalOpenGl => self.normal(at, NormalFormat::OpenGl, cache), + VirtualMap::NormalDirectX => self.normal(at, NormalFormat::DirectX, cache), + VirtualMap::Glossiness => [1.0 - self.channel(Channel::Roughness, at)[0]; 3], + VirtualMap::F0 => [0.08 * self.channel(Channel::SpecularLevel, at)[0]; 3], + VirtualMap::AoMixed => { + let ch = self.channel(Channel::AmbientOcclusion, at)[0]; + let baked = self.baked(BakedMap::AmbientOcclusion, at)[0]; + [if self.set.ao_mixing == AoMixing::Multiply { ch * baked } else { ch }; 3] + } + }, + SourceRef::Baked { map, .. } => self.baked(*map, at), + }; + v.get(k).copied().unwrap_or(0.0) + } +} + +// --------------------------------------------------------------------------------------------- +// Rendering. + +/// Render every file of `opts` for `input`'s texture set. +pub fn render_maps(input: &ExportInput, opts: &ExportOptions) -> Result> { + let planned = list_outputs(input.project, input.ts, input.mesh_name, opts)?; + let set = input.project.texture_set(input.ts)?; + let res = input.composite.resolution; + if res == 0 || res > MAX_IMAGE_SIZE { + return Err(FormatError::Invalid(format!("composite resolution {res} is out of range"))); + } + if input.coverage.len() != res as usize * res as usize { + return Err(FormatError::Invalid(format!("coverage has {} texels, expected {res}²", input.coverage.len()))); + } + let dmap = match opts.padding { + Padding::None => None, + Padding::Dilation(d) | Padding::DilationDefault(d) => Some(DilationMap::new(input.coverage, res, res, Some(d))), + Padding::Infinite => Some(DilationMap::new(input.coverage, res, res, None)), + }; + let beyond_default = matches!(opts.padding, Padding::DilationDefault(_) | Padding::Infinite); + let sampler = Sampler { + set, + comp: input.composite, + baked: input.baked, + coverage: input.coverage, + res, + normal_override: opts.normal_format, + project_normal: input.project.normal_format, + }; + planned + .into_iter() + .map(|p| { + let map = opts.preset.maps.get(p.map).ok_or_else(|| FormatError::Invalid("planned map out of range".into()))?; + let lay = layout(set, map); + let n = lay.len(); + let mut data = vec![0.0f32; res as usize * res as usize * n]; + data.par_chunks_mut(res as usize * n).enumerate().for_each(|(y, row)| { + let y = y as u32; + for x in 0..res { + let at = if sampler.covered(x, y) { + Some(At::Texel(x, y)) + } else { + match dmap.as_ref().and_then(|d| d.source(x, y)) { + Some((sx, sy)) => Some(At::Texel(sx, sy)), + None => beyond_default.then_some(At::Default), + } + }; + let Some(at) = at else { continue }; + let mut cache = None; + for (k, (_, src)) in lay.iter().enumerate() { + if let Some(v) = row.get_mut(x as usize * n + k) { + *v = sampler.sample(src, at, &mut cache); + } + } + } + }); + let size = opts.size.unwrap_or(res); + let data = resize(data, res, n, size); + let srgb: Vec = lay.iter().map(|(d, _)| *d != Dest::A && is_color(map) && !p.format.is_float()).collect(); + let mut warnings = p.warnings; + let bytes = encode(p.format, p.bit_depth, size, n, &data, &srgb, &mut warnings)?; + Ok(ExportedFile { name: p.name, bytes, width: size, height: size, warnings }) + }) + .collect() +} + +/// Resample a square `res²×n` image to `to²`: box filter when shrinking by an integer factor, +/// bilinear otherwise. +fn resize(data: Vec, res: u32, n: usize, to: u32) -> Vec { + if to == res || res == 0 || to == 0 { + return data; + } + let (r, t) = (res as usize, to as usize); + let mut out = vec![0.0f32; t * t * n]; + let px = |x: usize, y: usize, k: usize| data.get((y * r + x) * n + k).copied().unwrap_or(0.0); + out.par_chunks_mut(t * n).enumerate().for_each(|(y, row)| { + for x in 0..t { + for k in 0..n { + let v = if r > t && r % t == 0 { + let f = r / t; + let sum: f32 = (0..f).flat_map(|dy| (0..f).map(move |dx| (dx, dy))).map(|(dx, dy)| px(x * f + dx, y * f + dy, k)).sum(); + sum / (f * f) as f32 + } else { + let s = r as f32 / t as f32; + let (fx, fy) = (((x as f32 + 0.5) * s - 0.5).max(0.0), ((y as f32 + 0.5) * s - 0.5).max(0.0)); + let (x0, y0) = ((fx as usize).min(r - 1), (fy as usize).min(r - 1)); + let (x1, y1) = ((x0 + 1).min(r - 1), (y0 + 1).min(r - 1)); + let (ax, ay) = (fx - x0 as f32, fy - y0 as f32); + let top = px(x0, y0, k) * (1.0 - ax) + px(x1, y0, k) * ax; + let bottom = px(x0, y1, k) * (1.0 - ax) + px(x1, y1, k) * ax; + top * (1.0 - ay) + bottom * ay + }; + if let Some(o) = row.get_mut(x * n + k) { + *o = v; + } + } + } + }); + out +} + +/// Encode a square `size²×n` image (n = 1 L, 2 LA, 3 RGB, 4 RGBA; linear values). +fn encode(format: FileFormat, depth: u8, size: u32, n: usize, data: &[f32], srgb: &[bool], warnings: &mut Vec) -> Result> { + let failed = |e: &dyn std::fmt::Display| FormatError::Invalid(format!("{} encoding failed: {e}", format.extension())); + match format { + FileFormat::Exr => return write_exr(size, n, data, depth == 16).map_err(|e| failed(&e)), + FileFormat::Hdr => { + if n == 2 || n == 4 { + warnings.push("HDR has no alpha channel; alpha was dropped".into()); + } + let rgb = remap(data, n, if n <= 2 { &[0, 0, 0] } else { &[0, 1, 2] }); + let bytes: Vec = rgb.iter().flat_map(|v| v.to_ne_bytes()).collect(); + return write_image(format, &bytes, size, image::ExtendedColorType::Rgb32F).map_err(|e| failed(&e)); + } + _ => {} + } + // Integer formats: transfer function and quantization first, then fit the channel layout. + let encoded: Vec = + data.iter().enumerate().map(|(i, v)| if srgb.get(i % n).copied().unwrap_or(false) { linear_to_srgb(*v) } else { v.clamp(0.0, 1.0) }).collect(); + let (encoded, n) = match (format, n) { + (FileFormat::Jpeg, 2 | 4) => { + warnings.push("JPEG has no alpha channel; alpha was dropped".into()); + (remap(&encoded, n, if n == 2 { &[0] } else { &[0, 1, 2] }), n - 1) + } + (FileFormat::Tiff, 2) => (remap(&encoded, 2, &[0, 0, 0, 1]), 4), + _ => (encoded, n), + }; + use image::ExtendedColorType as C; + let (bytes, color) = if depth == 16 { + let b: Vec = encoded.iter().flat_map(|v| ((v * 65535.0 + 0.5) as u16).to_ne_bytes()).collect(); + (b, [C::L16, C::La16, C::Rgb16, C::Rgba16].get(n.wrapping_sub(1)).copied().unwrap_or(C::Rgba16)) + } else { + let b: Vec = encoded.iter().map(|v| (v * 255.0 + 0.5) as u8).collect(); + (b, [C::L8, C::La8, C::Rgb8, C::Rgba8].get(n.wrapping_sub(1)).copied().unwrap_or(C::Rgba8)) + }; + write_image(format, &bytes, size, color).map_err(|e| failed(&e)) +} + +/// Pick components `pick` from every texel of an `n`-component image. +fn remap(data: &[f32], n: usize, pick: &[usize]) -> Vec { + data.chunks_exact(n.max(1)).flat_map(|px| pick.iter().map(|k| px.get(*k).copied().unwrap_or(0.0))).collect() +} + +fn write_image(format: FileFormat, bytes: &[u8], size: u32, color: image::ExtendedColorType) -> image::ImageResult> { + let mut out = Cursor::new(Vec::new()); + let fmt = match format { + FileFormat::Jpeg => { + use image::ImageEncoder; + image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, JPEG_QUALITY).write_image(bytes, size, size, color)?; + return Ok(out.into_inner()); + } + FileFormat::Png => image::ImageFormat::Png, + FileFormat::Tga => image::ImageFormat::Tga, + FileFormat::Tiff => image::ImageFormat::Tiff, + FileFormat::Bmp => image::ImageFormat::Bmp, + FileFormat::Webp => image::ImageFormat::WebP, + FileFormat::Hdr => image::ImageFormat::Hdr, + FileFormat::Exr => image::ImageFormat::OpenExr, + }; + image::write_buffer_with_format(&mut out, bytes, size, size, color, fmt)?; + Ok(out.into_inner()) +} + +/// OpenEXR with Y / YA / RGB / RGBA channels, half or full float, ZIP compression. +fn write_exr(size: u32, n: usize, data: &[f32], half: bool) -> exr::error::Result> { + use exr::prelude::*; + let names: &[&str] = match n { + 1 => &["Y"], + 2 => &["Y", "A"], + 3 => &["R", "G", "B"], + _ => &["R", "G", "B", "A"], + }; + let channels: SmallVec<[AnyChannel; 4]> = names + .iter() + .enumerate() + .map(|(k, name)| { + let plane = data.iter().skip(k).step_by(n.max(1)); + let samples = if half { FlatSamples::F16(plane.map(|v| f16::from_f32(*v)).collect()) } else { FlatSamples::F32(plane.copied().collect()) }; + AnyChannel::new(*name, samples) + }) + .collect(); + let layer = Layer::new((size as usize, size as usize), LayerAttributes::default(), Encoding::SMALL_LOSSLESS, AnyChannels::sort(channels)); + let mut out = Cursor::new(Vec::new()); + let image = Image::from_layer(layer); + let writer = image.write(); + // No worker threads on the web. + #[cfg(target_arch = "wasm32")] + let writer = writer.non_parallel(); + writer.to_buffered(&mut out)?; + Ok(out.into_inner()) +} + +/// Write exported files into `dir` (created if missing), each atomically (`name.tmp`, then +/// rename). Names must be plain file names. +#[cfg(not(target_arch = "wasm32"))] +pub fn write_files(files: &[ExportedFile], dir: &std::path::Path) -> Result> { + std::fs::create_dir_all(dir)?; + files + .iter() + .map(|f| { + if f.name.is_empty() || f.name != sanitize_file_stem(&f.name) || f.name.trim_matches('.').is_empty() { + return Err(FormatError::Invalid(format!("\"{}\" is not a plain file name", f.name))); + } + let path = dir.join(&f.name); + crate::write_atomic(&path, &f.bytes)?; + Ok(path) + }) + .collect() +} + +#[cfg(test)] +mod tests; diff --git a/crates/formats/src/export/tests.rs b/crates/formats/src/export/tests.rs new file mode 100644 index 00000000..c52410bf --- /dev/null +++ b/crates/formats/src/export/tests.rs @@ -0,0 +1,355 @@ +use image::{DynamicImage, GenericImageView, ImageFormat}; +use texturecraft_core::{ChannelFormat, Project, TextureSet}; +use texturecraft_texel::{to_u16, to_u16_signed}; + +use super::*; +use crate::presets::{builtin_preset, builtin_presets, preset_from_json, preset_to_json}; + +const RES: u32 = 64; + +struct Fx { + project: Project, + comp: Composite, + baked: BakedMaps, + coverage: Vec, +} + +impl Fx { + fn new(channels: &[Channel]) -> Fx { + let mut project = Project::new("My Project", "builtin:sphere"); + let mut ts = TextureSet::new("Body Set", 0, RES); + ts.channels = channels.iter().map(|c| (*c, ChannelFormat::default_for(*c))).collect(); + project.texture_sets.push(ts); + let mut fx = Fx { project, comp: Composite::new(channels, RES), baked: BakedMaps::default(), coverage: vec![true; (RES * RES) as usize] }; + for y in 0..RES { + for x in 0..RES { + fx.set(Channel::BaseColor, x, y, [x as f32 / 63.0, y as f32 / 63.0, 0.2]); + fx.set(Channel::Roughness, x, y, [0.25; 3]); + fx.set(Channel::Metallic, x, y, [0.75; 3]); + fx.set(Channel::Normal, x, y, [0.5, 0.5, 1.0]); + fx.set(Channel::Height, x, y, [0.0; 3]); + } + } + fx + } + + fn set(&mut self, c: Channel, x: u32, y: u32, v: [f32; 3]) { + let Some(img) = self.comp.channels.get_mut(&c) else { return }; + let vals: Vec = v.iter().take(c.components()).map(|v| if c.kind() == ChannelKind::Signed { to_u16_signed(*v) } else { to_u16(*v) }).collect(); + img.set(x, y, &vals); + } + + fn set_all(&mut self, c: Channel, f: impl Fn(u32, u32) -> [f32; 3]) { + for y in 0..RES { + for x in 0..RES { + self.set(c, x, y, f(x, y)); + } + } + } + + fn input(&self) -> ExportInput<'_> { + ExportInput { project: &self.project, ts: 0, composite: &self.comp, baked: &self.baked, coverage: &self.coverage, mesh_name: "sphere" } + } + + fn render(&self, opts: &ExportOptions) -> Vec { + render_maps(&self.input(), opts).unwrap() + } + + fn render_preset(&self, name: &str) -> Vec { + self.render(&ExportOptions::new(builtin_preset(name).unwrap())) + } +} + +fn find<'a>(files: &'a [ExportedFile], suffix: &str) -> &'a ExportedFile { + files.iter().find(|f| f.name.contains(suffix)).unwrap_or_else(|| panic!("no {suffix} in {:?}", files.iter().map(|f| &f.name).collect::>())) +} + +fn decode(f: &ExportedFile) -> DynamicImage { + let ext = f.name.rsplit('.').next().unwrap(); + image::load_from_memory_with_format(&f.bytes, ImageFormat::from_extension(ext).unwrap()).unwrap() +} + +fn default_channels() -> Vec { + Channel::DEFAULT_SET.to_vec() +} + +#[test] +fn gltf_png_matches_the_composite() { + let fx = Fx::new(&default_channels()); + let files = fx.render_preset("glTF PBR Metal Roughness"); + let names: Vec<&str> = files.iter().map(|f| f.name.as_str()).collect(); + assert_eq!(names, ["sphere_Body_Set_BaseColor.png", "sphere_Body_Set_OcclusionRoughnessMetallic.png", "sphere_Body_Set_Normal.png"]); + let base = decode(&files[0]); + assert_eq!(base.color(), image::ColorType::Rgb8, "no opacity channel → no alpha"); + for (x, y) in [(0, 0), (10, 40), (63, 63), (32, 7)] { + let v = fx.comp.value(Channel::BaseColor, x, y); + let px = base.get_pixel(x, y); + for k in 0..3 { + let want = linear_to_srgb(v[k]) * 255.0; + assert!((px[k] as f32 - want).abs() <= 1.0, "base ({x},{y})[{k}] {} vs {want}", px[k]); + } + } + let orm = decode(&files[1]).get_pixel(5, 5); + assert_eq!(orm[0], 255, "no AO anywhere → white"); + assert!((orm[1] as i32 - 64).abs() <= 1 && (orm[2] as i32 - 191).abs() <= 1, "{orm:?}"); + let n = decode(&files[2]).get_pixel(5, 5); + assert!((n[0] as i32 - 128).abs() <= 1 && (n[1] as i32 - 128).abs() <= 1 && n[2] == 255, "{n:?}"); + // Emissive appears only when the set has the channel. + let mut channels = default_channels(); + channels.push(Channel::Emissive); + let fx = Fx::new(&channels); + assert!(fx.render_preset("glTF PBR Metal Roughness").iter().any(|f| f.name.ends_with("_Emissive.png"))); +} + +#[test] +fn exr_values_are_linear() { + let fx = Fx::new(&default_channels()); + for (depth, tol) in [(32u8, 1e-4f32), (16, 2e-3)] { + let mut opts = ExportOptions::new(builtin_preset("glTF PBR Metal Roughness").unwrap()); + opts.format_override = Some(FileFormat::Exr); + opts.bit_depth_override = Some(depth); + let files = fx.render(&opts); + let base = find(&files, "_BaseColor.exr"); + let img = decode(base).to_rgb32f(); + for (x, y) in [(0, 0), (10, 40), (63, 63)] { + let v = fx.comp.value(Channel::BaseColor, x, y); + let px = img.get_pixel(x, y); + for k in 0..3 { + assert!((px[k] - v[k]).abs() <= tol.max(v[k] * tol), "depth {depth} ({x},{y})[{k}]: {} vs {}", px[k], v[k]); + } + } + } + // A single-channel map is written as a luminance (Y) EXR. + let mut opts = ExportOptions::new(builtin_preset("Separate maps").unwrap()); + opts.format_override = Some(FileFormat::Exr); + let files = fx.render(&opts); + let rough = find(&files, "_roughness.exr"); + use exr::prelude::{ReadChannels, ReadLayers}; + let img = exr::prelude::read() + .no_deep_data() + .largest_resolution_level() + .all_channels() + .first_valid_layer() + .all_attributes() + .from_buffered(std::io::Cursor::new(&rough.bytes)) + .unwrap(); + let ch = &img.layer_data.channel_data.list; + assert_eq!(ch.len(), 1); + assert_eq!(ch[0].name.to_string(), "Y"); + assert!((ch[0].sample_data.value_by_flat_index(100).to_f32() - 0.25).abs() < 1e-3); +} + +#[test] +fn directx_flips_green() { + let mut fx = Fx::new(&default_channels()); + let n = Vec3::new(0.3, 0.4, 0.0); + let n = Vec3::new(n.x, n.y, (1.0 - n.length_squared()).sqrt()); + fx.set_all(Channel::Normal, |_, _| (n * 0.5 + Vec3::splat(0.5)).to_array()); + let gl = decode(find(&fx.render_preset("glTF PBR Metal Roughness"), "_Normal.png")).to_rgb8(); + let dx = decode(find(&fx.render_preset("Unreal Engine (Packed)"), "_Normal.png")).to_rgb8(); + let (a, b) = (gl.get_pixel(9, 9), dx.get_pixel(9, 9)); + assert!((a[0] as i32 - ((n.x * 0.5 + 0.5) * 255.0).round() as i32).abs() <= 1); + assert!((a[1] as i32 - ((n.y * 0.5 + 0.5) * 255.0).round() as i32).abs() <= 1, "{a:?}"); + assert_eq!(a[0], b[0]); + assert!((a[1] as i32 + b[1] as i32 - 255).abs() <= 1, "{a:?} vs {b:?}"); + assert_eq!(a[2], b[2]); + // The normal-format override applies to every normal output. + let mut opts = ExportOptions::new(builtin_preset("glTF PBR Metal Roughness").unwrap()); + opts.normal_format = Some(NormalFormat::DirectX); + assert_eq!(decode(find(&fx.render(&opts), "_Normal.png")).to_rgb8(), dx); +} + +#[test] +fn height_becomes_normal_detail() { + let mut fx = Fx::new(&[Channel::BaseColor, Channel::Height]); + // Height rises to the right and down the image. + fx.set_all(Channel::Height, |x, y| [(x as f32 + y as f32) / 160.0; 3]); + fx.project.texture_sets[0].height_strength = 8.0; + let files = fx.render_preset("Separate maps"); + let n = decode(find(&files, "_normal.png")).to_rgb8(); + let p = n.get_pixel(30, 30); + assert!(p[0] < 120, "slope up to the right tilts the normal left: {p:?}"); + assert!(p[1] > 136, "slope up toward the image bottom tilts an OpenGL normal up: {p:?}"); + let h = decode(find(&files, "_height.png")); + assert_eq!(h.color(), image::ColorType::L16); + let v = h.to_luma16().get_pixel(30, 30)[0] as f32 / 65535.0; + assert!((v - (60.0 / 160.0 * 0.5 + 0.5)).abs() < 1e-3, "height is stored as h·0.5+0.5: {v}"); + fx.project.texture_sets[0].height_strength = 0.0; + let flat = decode(find(&fx.render_preset("Separate maps"), "_normal.png")).to_rgb8(); + assert_eq!(flat.get_pixel(30, 30).0, [128, 128, 255]); +} + +#[test] +fn reoriented_normal_mapping_identities() { + let d = Vec3::new(0.2, -0.3, 0.9).normalize(); + assert!((rnm(Vec3::Z, d) - d).length() < 1e-5); + assert!((rnm(d, Vec3::Z) - d).length() < 1e-5); + assert!(rnm(Vec3::new(0.0, 0.0, -1.0), d).is_finite()); +} + +#[test] +fn ao_mixes_baked_and_channel() { + let mut channels = default_channels(); + channels.push(Channel::AmbientOcclusion); + let mut fx = Fx::new(&channels); + fx.set_all(Channel::AmbientOcclusion, |_, _| [0.5; 3]); + fx.baked = BakedMaps { resolution: RES, maps: [(BakedMap::AmbientOcclusion, std::sync::Arc::new(vec![0.5; (RES * RES) as usize]))].into() }; + fx.project.texture_sets[0].baked.insert(BakedMap::AmbientOcclusion); + let orm = decode(find(&fx.render_preset("glTF PBR Metal Roughness"), "Metallic.png")).to_rgb8(); + assert!((orm.get_pixel(3, 3)[0] as i32 - 64).abs() <= 1); + let mesh_maps = fx.render_preset("Mesh maps"); + assert_eq!(mesh_maps.len(), 1, "only baked maps are exported"); + let ao = decode(&mesh_maps[0]); + assert_eq!(mesh_maps[0].name, "sphere_Body_Set_ambient_occlusion.png"); + assert_eq!(ao.color(), image::ColorType::L16); +} + +#[test] +fn templates_are_sanitized_and_collisions_suffixed() { + let fx = Fx::new(&default_channels()); + let rough = |file: &str| MapSpec { + file: file.into(), + format: FileFormat::Png, + bit_depth: 8, + channels: vec![(Dest::L, SourceRef::Channel { channel: Channel::Roughness, component: 0 })], + }; + let preset = ExportPreset { + name: "t".into(), + maps: vec![ + rough("$project/..\\$textureSet:$channel ($colorSpace)"), + rough("$project/..\\$textureSet:$channel ($colorSpace)"), + rough("con"), + rough("A"), + rough("a"), + rough("..."), + ], + }; + let opts = ExportOptions::new(preset); + let planned = list_outputs(&fx.project, 0, "sphere", &opts).unwrap(); + let names: Vec<&str> = planned.iter().map(|p| p.name.as_str()).collect(); + assert_eq!( + names, + ["My_Project_.._Body_Set_roughness__linear_.png", "My_Project_.._Body_Set_roughness__linear__2.png", "_con.png", "A.png", "a_2.png", "texture.png"] + ); + assert!(planned[0].warnings.is_empty()); + assert_eq!(planned[1].warnings.len(), 1); + assert_eq!(planned[4].warnings.len(), 1); + let rendered: Vec = fx.render(&opts).into_iter().map(|f| f.name).collect(); + assert_eq!(rendered, names); +} + +#[test] +fn padding_modes() { + let mut fx = Fx::new(&default_channels()); + fx.coverage = (0..RES * RES).map(|i| i % RES < 32).collect(); + let rough_at = |fx: &Fx, padding: Padding, x: u32| { + let mut opts = ExportOptions::new(builtin_preset("Separate maps").unwrap()); + opts.padding = padding; + decode(find(&fx.render(&opts), "_roughness.png")).to_luma8().get_pixel(x, 10)[0] + }; + assert_eq!(rough_at(&fx, Padding::None, 10), 64); + assert_eq!(rough_at(&fx, Padding::None, 40), 0); + assert_eq!(rough_at(&fx, Padding::Infinite, 63), 64); + assert_eq!(rough_at(&fx, Padding::Dilation(2), 33), 64); + assert_eq!(rough_at(&fx, Padding::Dilation(2), 40), 0); + assert_eq!(rough_at(&fx, Padding::DilationDefault(2), 33), 64); + assert_eq!(rough_at(&fx, Padding::DilationDefault(2), 40), 128, "roughness default is 0.5"); + let bad = Fx { coverage: vec![true; 10], ..Fx::new(&default_channels()) }; + assert!(render_maps(&bad.input(), &ExportOptions::new(builtin_preset("Separate maps").unwrap())).is_err()); +} + +#[test] +fn output_size_override() { + let fx = Fx::new(&default_channels()); + for size in [32u32, 128] { + let mut opts = ExportOptions::new(builtin_preset("Separate maps").unwrap()); + opts.size = Some(size); + let files = fx.render(&opts); + let r = find(&files, "_roughness.png"); + assert_eq!((r.width, r.height), (size, size)); + let img = decode(r).to_luma8(); + assert_eq!(img.dimensions(), (size, size)); + assert_eq!(img.get_pixel(size / 2, size / 3)[0], 64); + } + let mut opts = ExportOptions::new(builtin_preset("Separate maps").unwrap()); + opts.size = Some(48); + assert!(render_maps(&fx.input(), &opts).is_err()); +} + +#[test] +fn every_format_encodes_and_decodes() { + let mut channels = default_channels(); + channels.push(Channel::Opacity); + let fx = Fx::new(&channels); + let gray_alpha = MapSpec { + file: "la".into(), + format: FileFormat::Png, + bit_depth: 8, + channels: vec![(Dest::L, SourceRef::Channel { channel: Channel::Roughness, component: 0 }), (Dest::A, SourceRef::Constant(0.5))], + }; + let mut preset = builtin_preset("glTF PBR Metal Roughness").unwrap(); + preset.maps.push(gray_alpha); + for format in FileFormat::ALL { + for &depth in format.bit_depths() { + let mut opts = ExportOptions::new(preset.clone()); + opts.format_override = Some(format); + opts.bit_depth_override = Some(depth); + let files = fx.render(&opts); + assert_eq!(files.len(), 4, "{format:?}"); + for f in &files { + assert!(f.name.ends_with(format.extension())); + if format == FileFormat::Exr && f.name.starts_with("la") { + continue; // Y/A EXR: covered by `exr_values_are_linear`. + } + assert_eq!(decode(f).dimensions(), (RES, RES), "{}", f.name); + } + // Roughness 0.25 everywhere survives every format (JPEG within its loss). + let px = decode(find(&files, "OcclusionRoughnessMetallic.")).to_rgba32f().get_pixel(1, 1)[1]; + assert!((px - 0.25).abs() < 0.01, "{format:?}/{depth}: {px}"); + let base = find(&files, "_BaseColor."); + match format { + FileFormat::Jpeg | FileFormat::Hdr => assert!(base.warnings.iter().any(|w| w.contains("alpha")), "{format:?}"), + _ => assert!(decode(base).color().has_alpha(), "{format:?} keeps opacity"), + } + } + } +} + +#[test] +fn list_outputs_matches_render_and_presets_round_trip() { + let fx = Fx::new(&default_channels()); + for preset in builtin_presets() { + preset.validate().unwrap(); + assert_eq!(preset_from_json(&preset_to_json(&preset).unwrap()).unwrap(), preset); + let opts = ExportOptions::new(preset.clone()); + let planned: Vec = list_outputs(&fx.project, 0, "sphere", &opts).unwrap().into_iter().map(|p| p.name).collect(); + let rendered: Vec = fx.render(&opts).into_iter().map(|f| f.name).collect(); + assert_eq!(planned, rendered, "{}", preset.name); + } + assert!(builtin_preset("unity hdrp (mask map)").is_some()); + assert!(preset_from_json("{").is_err()); + let dup = r#"{"name":"x","maps":[{"file":"a","format":"Png","bit_depth":8,"channels":[["R",{"Constant":1.0}],["R",{"Constant":0.0}]]}]}"#; + assert!(preset_from_json(dup).is_err()); + let mixed = r#"{"name":"x","maps":[{"file":"a","format":"Png","bit_depth":8,"channels":[["L",{"Constant":1.0}],["G",{"Constant":0.0}]]}]}"#; + assert!(preset_from_json(mixed).is_err()); + let ok = r#"{"name":"x","maps":[{"file":"a","format":"Tga","bit_depth":8,"channels":[["L",{"Channel":{"channel":"Metallic"}}]]}]}"#; + assert!(preset_from_json(ok).is_ok()); + assert!(list_outputs(&fx.project, 5, "m", &ExportOptions::new(builtin_presets().remove(0))).is_err()); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn write_files_is_atomic_and_refuses_paths() { + let fx = Fx::new(&default_channels()); + let files = fx.render_preset("Unreal Engine (Packed)"); + let dir = std::env::temp_dir().join(format!("tc-formats-export-{}", std::process::id())); + let paths = write_files(&files, &dir).unwrap(); + assert_eq!(paths.len(), 3); + for (p, f) in paths.iter().zip(&files) { + assert_eq!(std::fs::read(p).unwrap(), f.bytes); + } + assert!(std::fs::read_dir(&dir).unwrap().all(|e| !e.unwrap().file_name().to_string_lossy().ends_with(".tmp"))); + let evil = ExportedFile { name: "../evil.png".into(), ..files[0].clone() }; + assert!(write_files(&[evil], &dir).is_err()); + std::fs::remove_dir_all(&dir).unwrap(); +} diff --git a/crates/formats/src/image_res.rs b/crates/formats/src/image_res.rs new file mode 100644 index 00000000..a2bc2ebb --- /dev/null +++ b/crates/formats/src/image_res.rs @@ -0,0 +1,49 @@ +//! Decoding imported image resources (fills, alphas, stencils). + +use crate::{FormatError, MAX_IMAGE_SIZE, 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. +#[derive(Clone, Debug, PartialEq)] +pub struct DecodedImage { + pub width: u32, + pub height: u32, + pub rgba: Vec<[f32; 4]>, + /// True for 8/16-bit images (sRGB-encoded); false for float images (linear). + pub srgb: bool, +} + +/// Decode PNG, JPEG, TGA, TIFF, BMP, WEBP, EXR or HDR bytes. +pub fn decode_image(bytes: &[u8]) -> Result { + let reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() })?; + let (w, h) = reader.into_dimensions().map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() })?; + if w == 0 || h == 0 || w > MAX_IMAGE_SIZE || h > MAX_IMAGE_SIZE { + return Err(FormatError::TooLarge(format!("image is {w}x{h} (limit {MAX_IMAGE_SIZE})"))); + } + let img = crate::guard("image", || { + image::load_from_memory(bytes).map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() }) + })?; + let srgb = !matches!(img, image::DynamicImage::ImageRgb32F(_) | image::DynamicImage::ImageRgba32F(_)); + let f = img.into_rgba32f(); + let (width, height) = f.dimensions(); + let rgba = f.pixels().map(|p| p.0).collect(); + Ok(DecodedImage { width, height, rgba, srgb }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[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); + assert!(decode_image(b"not an image").is_err()); + assert!(decode_image(&png[..png.len() / 2]).is_err()); + } +} diff --git a/crates/formats/src/lib.rs b/crates/formats/src/lib.rs new file mode 100644 index 00000000..3ce17b3f --- /dev/null +++ b/crates/formats/src/lib.rs @@ -0,0 +1,103 @@ +//! # texturecraft-formats +//! +//! Everything TextureCraft reads from or writes to disk: +//! +//! - [`mesh_import`]: glTF/GLB, FBX (7.x binary and ASCII), COLLADA, OBJ, PLY and STL into a +//! [`texturecraft_geom::Mesh`] (world space in meters, Y up, glTF UV convention, one submesh +//! per material), with an automatic unwrap ([`texturecraft_geom::unwrap::auto_unwrap`]) when +//! the file has no UVs and a [`MeshReport`]. +//! - [`mesh_export`]: the working mesh as OBJ, or as GLB with its textures embedded. +//! - [`export`]: texture export from a texture set's composite through [`ExportPreset`]s (file +//! name templates, channel packing, virtual maps such as the final normal, padding, resizing) +//! to PNG, JPEG, TGA, TIFF, BMP, WebP, EXR and HDR bytes. +//! - [`presets`]: the built-in export presets and JSON (de)serialization of user presets. +//! - [`project_file`]: the `.tcraft` project zip (project JSON, sparse lossless pixel tiles, +//! baked maps, the original mesh and a thumbnail). +//! +//! Every reader takes untrusted bytes and returns an error, never panics. Byte-based functions +//! work everywhere (including the web build); path-based ones are native only. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod export; +pub mod image_res; +pub mod mesh_export; +pub mod mesh_import; +pub mod presets; +pub mod project_file; + +pub use export::{ + Dest, ExportInput, ExportOptions, ExportPreset, ExportedFile, FileFormat, MapSpec, Padding, PlannedFile, SourceRef, VirtualMap, list_outputs, render_maps, +}; +pub use image_res::{DecodedImage, decode_image}; +pub use mesh_export::{GlbTextures, glb_bytes, glb_export_options, obj_bytes}; +pub use mesh_import::{ImportedMesh, MeshReport, import_high_poly_bytes, import_mesh_bytes}; +pub use presets::{builtin_preset, builtin_presets, preset_from_json, preset_to_json}; +pub use project_file::{ProjectFile, load_project_bytes, sanitize_resource_name, save_project_bytes}; + +#[cfg(not(target_arch = "wasm32"))] +pub use export::write_files; +#[cfg(not(target_arch = "wasm32"))] +pub use mesh_import::import_mesh; +#[cfg(not(target_arch = "wasm32"))] +pub use project_file::{load_project, save_project}; + +#[derive(Debug, thiserror::Error)] +pub enum FormatError { + #[error("unsupported: {0}")] + Unsupported(String), + #[error("malformed {kind}: {msg}")] + Malformed { kind: &'static str, msg: String }, + #[error("too large: {0}")] + TooLarge(String), + #[error("invalid: {0}")] + Invalid(String), + #[error("this file was saved by a newer TextureCraft (format {found}; this version reads up to {supported})")] + NewerVersion { found: u32, supported: u32 }, + #[error("I/O error: {0}")] + Io(#[from] std::io::Error), + #[error(transparent)] + Geom(#[from] texturecraft_geom::GeomError), + #[error(transparent)] + Core(#[from] texturecraft_core::CoreError), +} + +pub type Result = std::result::Result; + +pub(crate) fn malformed(kind: &'static str, msg: impl std::fmt::Display) -> FormatError { + FormatError::Malformed { kind, msg: msg.to_string() } +} + +/// Last-resort guard around third-party parsers: a panic inside them becomes an error. +pub(crate) fn guard(kind: &'static str, f: impl FnOnce() -> Result) -> Result { + std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).unwrap_or_else(|_| Err(malformed(kind, "the parser failed on this file"))) +} + +/// Largest image edge accepted or produced anywhere in this crate. +pub const MAX_IMAGE_SIZE: u32 = 16_384; + +/// 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> { + let meta = std::fs::metadata(path)?; + if !meta.is_file() { + return Err(FormatError::Invalid(format!("{} is not a regular file", path.display()))); + } + if meta.len() > cap { + return Err(FormatError::TooLarge(format!("{} is {} bytes (limit {cap})", path.display(), meta.len()))); + } + Ok(std::fs::read(path)?) +} + +/// Write `bytes` to `path` atomically: `.tmp` first, then rename over the target. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) 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); + } + Ok(res?) +} diff --git a/crates/formats/src/mesh_export.rs b/crates/formats/src/mesh_export.rs new file mode 100644 index 00000000..9922f9f4 --- /dev/null +++ b/crates/formats/src/mesh_export.rs @@ -0,0 +1,284 @@ +//! Mesh export of the working mesh: Wavefront OBJ (positions, UVs, normals, one group per +//! submesh) and binary glTF (one primitive per submesh, a metallic-roughness material per +//! material with its textures embedded). + +use std::fmt::Write as _; + +use serde_json::{Map, Value, json}; +use texturecraft_core::NormalFormat; +use texturecraft_geom::glam::Vec3; +use texturecraft_geom::{Mesh, SubMesh}; + +use crate::presets::{GLTF_BASE_COLOR, GLTF_EMISSIVE, GLTF_NORMAL, GLTF_ORM, gltf_pbr_maps}; +use crate::{ExportOptions, ExportPreset, ExportedFile, FileFormat, FormatError, Result}; + +/// The mesh as OBJ text (UV v flipped back to OBJ's bottom-up convention). +pub fn obj_bytes(mesh: &Mesh, name: &str) -> Vec { + let mut s = String::with_capacity(mesh.positions.len() * 96 + mesh.indices.len() * 12); + let _ = writeln!(s, "# {name}, exported by TextureCraft\no {}", token(name)); + for p in &mesh.positions { + let _ = writeln!(s, "v {} {} {}", p.x, p.y, p.z); + } + let uvs = mesh.uvs.len() == mesh.positions.len(); + let normals = mesh.normals.len() == mesh.positions.len(); + if uvs { + for t in &mesh.uvs { + let _ = writeln!(s, "vt {} {}", t.x, 1.0 - t.y); + } + } + if normals { + for n in &mesh.normals { + let _ = writeln!(s, "vn {} {} {}", n.x, n.y, n.z); + } + } + for sm in &parts(mesh, name) { + let material = mesh.materials.get(sm.material).map_or("default", String::as_str); + let _ = writeln!(s, "g {}\nusemtl {}", token(&sm.name), token(material)); + let (a, b) = (sm.start as usize, sm.start as usize + sm.count as usize); + for tri in mesh.indices.get(a..b).unwrap_or_default().chunks_exact(3) { + s.push('f'); + for i in tri { + let i = i + 1; + let _ = match (uvs, normals) { + (true, true) => write!(s, " {i}/{i}/{i}"), + (true, false) => write!(s, " {i}/{i}"), + (false, true) => write!(s, " {i}//{i}"), + (false, false) => write!(s, " {i}"), + }; + } + s.push('\n'); + } + } + s.into_bytes() +} + +/// The submeshes, or one covering the whole mesh when it has none. +fn parts(mesh: &Mesh, name: &str) -> Vec { + if mesh.submeshes.is_empty() { + vec![SubMesh { name: name.into(), material: 0, start: 0, count: mesh.indices.len() as u32 }] + } else { + mesh.submeshes.clone() + } +} + +/// Textures of one glTF material (PNG bytes), from the files of [`glb_export_options`]. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct GlbTextures { + pub base_color: Option>, + /// Occlusion (R), roughness (G), metallic (B). + pub orm: Option>, + /// OpenGL convention (+Y up the image), as glTF expects. + pub normal: Option>, + pub emissive: Option>, +} + +impl GlbTextures { + /// Sort rendered files into slots; an all-black emissive map is dropped. + pub fn from_files(files: Vec) -> GlbTextures { + let mut t = GlbTextures::default(); + for f in files { + let slot = match f.name.strip_suffix(".png").unwrap_or_default() { + GLTF_BASE_COLOR => &mut t.base_color, + GLTF_ORM => &mut t.orm, + GLTF_NORMAL => &mut t.normal, + GLTF_EMISSIVE if !is_black(&f.bytes) => &mut t.emissive, + _ => continue, + }; + *slot = Some(f.bytes); + } + t + } +} + +/// Texture export options for [`GlbTextures`]: the glTF maps as 8-bit PNG named by slot. +pub fn glb_export_options() -> ExportOptions { + let mut o = ExportOptions::new(ExportPreset { name: "glTF binary".into(), maps: gltf_pbr_maps(str::to_string) }); + o.format_override = Some(FileFormat::Png); + o.bit_depth_override = Some(8); + o.normal_format = Some(NormalFormat::OpenGl); + o +} + +fn is_black(png: &[u8]) -> bool { + image::load_from_memory_with_format(png, image::ImageFormat::Png).map_or(true, |img| img.to_rgb8().iter().all(|v| *v == 0)) +} + +/// PNG color type 4 (gray + alpha) or 6 (RGBA), from the IHDR chunk. +fn png_has_alpha(png: &[u8]) -> bool { + matches!(png.get(25), Some(4 | 6)) +} + +/// The binary chunk and the JSON arrays that index into it. +#[derive(Default)] +struct GlbBuilder { + bin: Vec, + views: Vec, + accessors: Vec, + images: Vec, + textures: Vec, +} + +impl GlbBuilder { + fn view(&mut self, bytes: &[u8], target: Option) -> usize { + while !self.bin.len().is_multiple_of(4) { + self.bin.push(0); + } + let mut v = json!({ "buffer": 0, "byteOffset": self.bin.len(), "byteLength": bytes.len() }); + if let Some(t) = target { + v["target"] = t.into(); + } + self.bin.extend_from_slice(bytes); + self.views.push(v); + self.views.len() - 1 + } + + fn accessor(&mut self, values: impl Iterator, target: u32, component: u32, kind: &str, count: usize) -> Value { + let bytes: Vec = values.flatten().collect(); + let view = self.view(&bytes, Some(target)); + self.accessors.push(json!({ "bufferView": view, "componentType": component, "count": count, "type": kind })); + Value::from(self.accessors.len() - 1) + } + + /// A float vertex attribute of `kind` (VEC2 / VEC3) over `n` vertices. + fn attribute(&mut self, values: impl Iterator, kind: &str, n: usize) -> Value { + self.accessor(values.map(f32::to_le_bytes), 34962, 5126, kind, n) + } + + /// `{"index": n}` of a new texture holding `png`. + fn texture(&mut self, png: &[u8]) -> Value { + let view = self.view(png, None); + self.images.push(json!({ "bufferView": view, "mimeType": "image/png" })); + self.textures.push(json!({ "source": self.images.len() - 1, "sampler": 0 })); + json!({ "index": self.textures.len() - 1 }) + } + + fn material(&mut self, name: &str, t: &GlbTextures) -> Value { + let (mut m, mut pbr) = (Map::new(), Map::new()); + m.insert("name".into(), name.into()); + if let Some(png) = &t.base_color { + pbr.insert("baseColorTexture".into(), self.texture(png)); + if png_has_alpha(png) { + m.insert("alphaMode".into(), "BLEND".into()); + } + } + match &t.orm { + Some(png) => { + let tex = self.texture(png); + pbr.insert("metallicRoughnessTexture".into(), tex.clone()); + m.insert("occlusionTexture".into(), tex); + } + None => { + pbr.insert("metallicFactor".into(), 0.0.into()); + } + } + if let Some(png) = &t.normal { + m.insert("normalTexture".into(), self.texture(png)); + } + if let Some(png) = &t.emissive { + m.insert("emissiveTexture".into(), self.texture(png)); + m.insert("emissiveFactor".into(), json!([1.0, 1.0, 1.0])); + } + m.insert("pbrMetallicRoughness".into(), pbr.into()); + m.into() + } +} + +/// The mesh as binary glTF (GLB) named `name`, with `textures[i]` on material `i` (materials +/// without an entry get a plain one). Vertex data is written as is: the mesh already follows +/// glTF's conventions. +pub fn glb_bytes(mesh: &Mesh, name: &str, textures: &[GlbTextures]) -> Result> { + let n = mesh.positions.len(); + if n == 0 || mesh.indices.len() < 3 { + return Err(FormatError::Invalid("the mesh is empty".into())); + } + let mut g = GlbBuilder::default(); + let vec3s = |v: &[Vec3]| v.iter().flat_map(|p| p.to_array()).collect::>(); + let mut attributes = Map::new(); + let position = g.attribute(vec3s(&mesh.positions).into_iter(), "VEC3", n); + let (lo, hi) = mesh.bounds(); + if let Some(a) = g.accessors.last_mut() { + a["min"] = json!(lo.to_array()); + a["max"] = json!(hi.to_array()); + } + attributes.insert("POSITION".into(), position); + if mesh.normals.len() == n { + attributes.insert("NORMAL".into(), g.attribute(vec3s(&mesh.normals).into_iter(), "VEC3", n)); + } + if mesh.uvs.len() == n { + attributes.insert("TEXCOORD_0".into(), g.attribute(mesh.uvs.iter().flat_map(|t| t.to_array()), "VEC2", n)); + } + let mut primitives = Vec::new(); + for sm in parts(mesh, name) { + let tris = mesh.indices.get(sm.start as usize..(sm.start as usize).saturating_add(sm.count as usize)).unwrap_or_default(); + let tris = tris.get(..tris.len() / 3 * 3).unwrap_or_default(); + if tris.is_empty() { + continue; + } + let indices = g.accessor(tris.iter().map(|i| i.to_le_bytes()), 34963, 5125, "SCALAR", tris.len()); + let mut p = json!({ "attributes": attributes, "indices": indices }); + if sm.material < mesh.materials.len() { + p["material"] = sm.material.into(); + } + primitives.push(p); + } + let none = GlbTextures::default(); + let materials: Vec = mesh.materials.iter().enumerate().map(|(i, m)| g.material(m, textures.get(i).unwrap_or(&none))).collect(); + let mut doc = json!({ + "asset": { "version": "2.0", "generator": "TextureCraft" }, + "scene": 0, + "scenes": [{ "nodes": [0] }], + "nodes": [{ "mesh": 0, "name": name }], + "meshes": [{ "name": name, "primitives": primitives }], + "buffers": [{ "byteLength": g.bin.len() }], + "bufferViews": g.views, + "accessors": g.accessors, + }); + if !g.textures.is_empty() { + doc["samplers"] = json!([{ "magFilter": 9729, "minFilter": 9987, "wrapS": 10497, "wrapT": 10497 }]); + } + // glTF forbids empty arrays. + for (key, list) in [("materials", materials), ("images", g.images), ("textures", g.textures)] { + if !list.is_empty() { + doc[key] = list.into(); + } + } + let mut js = serde_json::to_vec(&doc).map_err(|e| FormatError::Invalid(format!("glTF JSON: {e}")))?; + while !js.len().is_multiple_of(4) { + js.push(b' '); + } + let mut bin = g.bin; + while !bin.len().is_multiple_of(4) { + bin.push(0); + } + let total = 12 + 8 + js.len() + 8 + bin.len(); + let len32 = |v: usize| u32::try_from(v).map(u32::to_le_bytes).map_err(|_| FormatError::TooLarge(format!("the GLB would be {total} bytes (limit 4 GiB)"))); + let mut out = Vec::with_capacity(total); + for chunk in [&b"glTF"[..], &2u32.to_le_bytes(), &len32(total)?, &len32(js.len())?, b"JSON", &js, &len32(bin.len())?, b"BIN\0", &bin] { + out.extend_from_slice(chunk); + } + Ok(out) +} + +/// OBJ names end at whitespace. +fn token(name: &str) -> String { + let t: String = name.trim().chars().map(|c| if c.is_whitespace() { '_' } else { c }).collect(); + if t.is_empty() { "unnamed".into() } else { t } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn obj_round_trips_through_the_importer() { + let mesh = texturecraft_geom::primitives::rounded_box(texturecraft_geom::glam::Vec3::splat(1.0), 0.1); + let bytes = obj_bytes(&mesh, "My box"); + let back = crate::import_mesh_bytes("box.obj", &bytes).unwrap().mesh; + assert_eq!(back.triangle_count(), mesh.triangle_count()); + let (a, b) = (mesh.bounds(), back.bounds()); + assert!((a.0 - b.0).length() < 1e-4 && (a.1 - b.1).length() < 1e-4); + let uv_sum = |m: &Mesh| m.indices.iter().map(|&i| m.uvs[i as usize].y as f64).sum::(); + assert!((uv_sum(&mesh) - uv_sum(&back)).abs() < 1e-3 * mesh.indices.len() as f64); + assert!(String::from_utf8(bytes).unwrap().contains("o My_box")); + } +} diff --git a/crates/formats/src/mesh_import/dae.rs b/crates/formats/src/mesh_import/dae.rs new file mode 100644 index 00000000..31af1865 --- /dev/null +++ b/crates/formats/src/mesh_import/dae.rs @@ -0,0 +1,344 @@ +//! COLLADA (.dae): `triangles`, `polylist` and `polygons` with VERTEX / NORMAL / TEXCOORD inputs, +//! materials bound through `instance_geometry` (and skinned `instance_controller`), the visual +//! scene's node transforms (matrix, translate, rotate, scale), and the asset's unit and up axis +//! (converted to meters, Y up, like glTF). + +use std::collections::HashMap; +use std::rc::Rc; + +use quick_xml::events::{BytesStart, Event}; +use texturecraft_geom::glam::{Mat3, Mat4, Vec2, Vec3, Vec4}; + +use super::{Corner, PolygonSink, RawMesh, check_vertices}; +use crate::{FormatError, Result, malformed}; + +const KIND: &str = "COLLADA"; +/// Deepest element nesting and node hierarchy followed. +const MAX_DEPTH: usize = 256; +/// Elements read from a document. +const MAX_ELEMENTS: usize = 2_000_000; +/// Node instances visited (bounds exponential instancing through `instance_node`). +const MAX_NODE_VISITS: usize = 1_000_000; + +#[derive(Default)] +struct El { + name: String, + attrs: Vec<(String, String)>, + text: String, + children: Vec, +} + +impl El { + fn attr(&self, key: &str) -> Option<&str> { + self.attrs.iter().find(|(k, _)| k == key).map(|(_, v)| v.as_str()) + } + + fn child(&self, name: &str) -> Option<&El> { + self.children.iter().find(|c| c.name == name) + } + + fn all<'a>(&'a self, name: &'a str) -> impl Iterator { + self.children.iter().filter(move |c| c.name == name) + } + + fn floats(&self) -> Result> { + self.text.split_whitespace().map(|w| w.parse::().map_err(|_| malformed(KIND, format!("bad number \"{w}\" in <{}>", self.name)))).collect() + } + + fn indices(&self) -> Result> { + self.text.split_whitespace().map(|w| w.parse::().map_err(|_| malformed(KIND, format!("bad index \"{w}\" in <{}>", self.name)))).collect() + } +} + +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()), text(&a.value))).collect(); + El { name: text(e.local_name().as_ref()), attrs, ..El::default() } +} + +/// The document as an element tree (the root is an unnamed element holding the top level). +fn parse_xml(bytes: &[u8]) -> Result { + 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 COLLADA document has more than {MAX_ELEMENTS} elements or is nested too deeply"))); + } + } + 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::Text(t) => push_text(&mut stack, &t), + Event::CData(t) => push_text(&mut stack, &t), + 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")), + } +} + +fn push_text(stack: &mut [El], t: &[u8]) { + if let Some(el) = stack.last_mut() { + el.text.push_str(&String::from_utf8_lossy(t)); + // Whitespace keeps numbers split across text events apart. + el.text.push(' '); + } +} + +/// A ``'s float array read through its accessor. +struct Source { + data: Vec, + stride: usize, + offset: usize, +} + +impl Source { + fn get(&self, i: u32) -> Option<[f32; W]> { + let start = (i as usize).checked_mul(self.stride)?.checked_add(self.offset)?; + self.data.get(start..start.checked_add(W)?)?.try_into().ok() + } +} + +struct Doc<'a> { + ids: HashMap<&'a str, &'a El>, + sources: HashMap<&'a str, Rc>, +} + +impl<'a> Doc<'a> { + /// The element a local URL (`#id`) points at. + fn get(&self, url: Option<&str>) -> Option<&'a El> { + url.and_then(|u| u.strip_prefix('#')).and_then(|id| self.ids.get(id)).copied() + } + + fn source(&mut self, url: Option<&str>) -> Result> { + let el = self.get(url).ok_or_else(|| malformed(KIND, format!("missing source {}", url.unwrap_or("(none)"))))?; + let id = el.attr("id").unwrap_or_default(); + if let Some(s) = self.sources.get(id) { + return Ok(s.clone()); + } + let acc = el.child("technique_common").and_then(|t| t.child("accessor")); + let array = acc.and_then(|a| self.get(a.attr("source"))).or_else(|| el.child("float_array")); + let array = array.ok_or_else(|| malformed(KIND, format!("source \"{id}\" has no float_array")))?; + let num = |k: &str, default: usize| acc.and_then(|a| a.attr(k)).and_then(|v| v.parse().ok()).unwrap_or(default); + let data = array.floats()?; + check_vertices(data.len() / 3)?; + let s = Rc::new(Source { data, stride: num("stride", 1).max(1), offset: num("offset", 0) }); + self.sources.insert(id, s.clone()); + Ok(s) + } +} + +pub(crate) fn read(bytes: &[u8]) -> Result { + crate::guard(KIND, || read_inner(bytes)) +} + +fn read_inner(bytes: &[u8]) -> Result { + let doc = parse_xml(bytes)?; + let root = doc.child("COLLADA").ok_or_else(|| malformed(KIND, "not a COLLADA document"))?; + let mut ids = HashMap::new(); + let mut stack = vec![root]; + while let Some(el) = stack.pop() { + if let Some(id) = el.attr("id") { + ids.entry(id).or_insert(el); + } + stack.extend(&el.children); + } + let mut d = Doc { ids, sources: HashMap::new() }; + let asset = root.child("asset"); + let meter = asset.and_then(|a| a.child("unit")).and_then(|u| u.attr("meter")).and_then(|m| m.trim().parse::().ok()); + let meter = meter.filter(|m| m.is_finite() && *m > 0.0).unwrap_or(1.0); + let up = match asset.and_then(|a| a.child("up_axis")).map(|u| u.text.trim()) { + Some("Z_UP") => Mat4::from_cols(Vec4::X, -Vec4::Z, Vec4::Y, Vec4::W), + Some("X_UP") => Mat4::from_cols(Vec4::Y, -Vec4::X, Vec4::Z, Vec4::W), + _ => Mat4::IDENTITY, + }; + let global = up * Mat4::from_scale(Vec3::splat(meter)); + let mut sink = PolygonSink::default(); + let scene = root.child("scene").and_then(|s| s.child("instance_visual_scene")).and_then(|i| d.get(i.attr("url"))); + let scene = scene.or_else(|| root.child("library_visual_scenes").and_then(|l| l.child("visual_scene"))); + let Some(scene) = scene else { + // No scene: every geometry as stored. + for g in root.all("library_geometries").flat_map(|l| l.all("geometry")) { + add_geometry(&mut d, &mut sink, g, global, &HashMap::new())?; + } + return Ok(sink.finish()); + }; + let mut stack: Vec<(&El, Mat4, usize)> = scene.all("node").map(|n| (n, global, 0)).collect(); + stack.reverse(); + let mut visits = 0usize; + while let Some((node, parent, depth)) = stack.pop() { + visits += 1; + if visits > MAX_NODE_VISITS || depth > MAX_DEPTH { + return Err(FormatError::TooLarge("the COLLADA node hierarchy is too large, too deep or cyclic".into())); + } + let m = parent * node_transform(node)?; + sink.part = node.attr("name").or_else(|| node.attr("id")).unwrap_or_default().to_string(); + for inst in &node.children { + let (geom, bind_shape) = match inst.name.as_str() { + "instance_geometry" => (d.get(inst.attr("url")), Mat4::IDENTITY), + "instance_controller" => { + let skin = d.get(inst.attr("url")).and_then(|c| c.child("skin")); + let bind = skin.and_then(|s| s.child("bind_shape_matrix")).map(El::floats).transpose()?.and_then(|v| matrix(&v)); + (skin.and_then(|s| d.get(s.attr("source"))), bind.unwrap_or(Mat4::IDENTITY)) + } + _ => continue, + }; + let Some(geom) = geom else { + sink.warn("an instanced geometry is missing from the file".into()); + continue; + }; + let bind = bindings(&d, inst); + add_geometry(&mut d, &mut sink, geom, m * bind_shape, &bind)?; + } + let children = node.all("node").chain(node.all("instance_node").filter_map(|i| d.get(i.attr("url")))); + let children: Vec<&El> = children.collect(); + stack.extend(children.into_iter().rev().map(|c| (c, m, depth + 1))); + } + Ok(sink.finish()) +} + +/// Row-major 4×4 floats to a matrix. +fn matrix(v: &[f32]) -> Option { + let a: [f32; 16] = v.get(..16)?.try_into().ok()?; + Some(Mat4::from_cols_array(&a).transpose()) +} + +/// A node's transform elements, applied in document order. +fn node_transform(node: &El) -> Result { + let mut m = Mat4::IDENTITY; + for t in &node.children { + let v = match t.name.as_str() { + "matrix" | "translate" | "rotate" | "scale" => t.floats()?, + _ => continue, + }; + let step = match (t.name.as_str(), v.as_slice()) { + ("matrix", _) => matrix(&v), + ("translate", [x, y, z, ..]) => Some(Mat4::from_translation(Vec3::new(*x, *y, *z))), + ("scale", [x, y, z, ..]) => Some(Mat4::from_scale(Vec3::new(*x, *y, *z))), + ("rotate", [x, y, z, deg, ..]) => Vec3::new(*x, *y, *z).try_normalize().map(|axis| Mat4::from_axis_angle(axis, deg.to_radians())), + _ => None, + }; + m *= step.unwrap_or(Mat4::IDENTITY); + } + Ok(m) +} + +/// Material symbol → material name, from an instance's `bind_material`. +fn bindings(d: &Doc, inst: &El) -> HashMap { + let list = inst.child("bind_material").and_then(|b| b.child("technique_common")); + list.into_iter() + .flat_map(|t| t.all("instance_material")) + .filter_map(|im| { + let symbol = im.attr("symbol")?; + let mat = d.get(im.attr("target")); + let name = mat.and_then(|m| m.attr("name").or_else(|| m.attr("id"))).unwrap_or(symbol); + Some((symbol.to_string(), name.to_string())) + }) + .collect() +} + +/// An input's index offset and source. +struct Input { + offset: usize, + source: Rc, +} + +fn add_geometry(d: &mut Doc, sink: &mut PolygonSink, geom: &El, m: Mat4, bind: &HashMap) -> Result<()> { + let Some(mesh) = geom.child("mesh") else { return Ok(()) }; + let nm = Mat3::from_mat4(m).inverse().transpose(); + let flip = m.determinant() < 0.0; + for prim in &mesh.children { + if !matches!(prim.name.as_str(), "triangles" | "polylist" | "polygons") { + if matches!(prim.name.as_str(), "lines" | "linestrips" | "trifans" | "tristrips") { + sink.warn(format!("skipped COLLADA <{}> (only triangles and polygons are supported)", prim.name)); + } + continue; + } + let (mut position, mut normal, mut uv): (Option, Option, Option<(u32, Input)>) = (None, None, None); + let mut stride = 1usize; + for input in prim.all("input") { + let offset: usize = input.attr("offset").and_then(|o| o.parse().ok()).unwrap_or(0); + stride = stride.max(offset.saturating_add(1)); + // VERTEX names a , whose own inputs share the VERTEX index. + let nested = match input.attr("semantic") { + Some("VERTEX") => d.get(input.attr("source")).map(|v| v.all("input").collect()).unwrap_or_default(), + _ => vec![input], + }; + for i in nested { + let set: u32 = input.attr("set").or_else(|| i.attr("set")).and_then(|s| s.parse().ok()).unwrap_or(0); + match i.attr("semantic") { + Some("POSITION") => position = Some(Input { offset, source: d.source(i.attr("source"))? }), + Some("NORMAL") if normal.is_none() => normal = Some(Input { offset, source: d.source(i.attr("source"))? }), + Some("TEXCOORD") if uv.as_ref().is_none_or(|(s, _)| set < *s) => uv = Some((set, Input { offset, source: d.source(i.attr("source"))? })), + _ => {} + } + } + } + let Some(position) = position else { + sink.warn("skipped COLLADA primitives without positions".into()); + continue; + }; + let material = prim.attr("material").map(|s| bind.get(s).map_or(s, String::as_str)).unwrap_or("Default"); + // Each

with the corner count of every polygon in it. + let lists: Vec<(Vec, Vec)> = match prim.name.as_str() { + "polygons" => prim + .all("p") + .map(|p| { + let p = p.indices()?; + let n = p.len() / stride; + Ok((p, vec![n])) + }) + .collect::>()?, + _ => { + let p = prim.child("p").map(El::indices).transpose()?.unwrap_or_default(); + let sizes = match prim.child("vcount") { + Some(v) if prim.name == "polylist" => v.indices()?.into_iter().map(|c| c as usize).collect(), + _ => vec![3; p.len() / stride / 3], + }; + vec![(p, sizes)] + } + }; + let mut corners = Vec::new(); + for (p, sizes) in &lists { + let mut next = 0usize; + for &size in sizes { + corners.clear(); + for c in next..next.saturating_add(size) { + let idx = |input: &Input| c.checked_mul(stride).and_then(|b| b.checked_add(input.offset)).and_then(|k| p.get(k)).copied(); + let at = idx(&position).and_then(|i| position.source.get::<3>(i)); + let pos = at.ok_or_else(|| malformed(KIND, "polygon index out of range"))?; + let n = normal.as_ref().and_then(|n| idx(n).and_then(|i| n.source.get::<3>(i))); + let t = uv.as_ref().and_then(|(_, t)| idx(t).and_then(|i| t.source.get::<2>(i))); + corners.push(Corner { + 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)), + }); + } + next = next.saturating_add(size); + if flip { + corners.reverse(); + } + sink.polygon(material, &corners)?; + } + } + } + Ok(()) +} diff --git a/crates/formats/src/mesh_import/fbx.rs b/crates/formats/src/mesh_import/fbx.rs new file mode 100644 index 00000000..dd0d63cc --- /dev/null +++ b/crates/formats/src/mesh_import/fbx.rs @@ -0,0 +1,613 @@ +//! Autodesk FBX 7.x, binary (32- and 64-bit node records, zlib-compressed arrays) and ASCII: +//! mesh geometry with normals, UVs and per-polygon materials from the layer elements, every +//! Model's transform and parent hierarchy, and the file's unit and axis system (converted to +//! meters, Y up, like glTF). + +use std::borrow::Cow; +use std::collections::{HashMap, HashSet}; +use std::io::Read as _; + +use texturecraft_geom::glam::{Mat3, Mat4, Vec2, Vec3}; + +use super::{Corner, PolygonSink, RawMesh, check_vertices}; +use crate::{FormatError, Result, malformed}; + +const KIND: &str = "FBX"; +const MAGIC: &[u8] = b"Kaydara FBX Binary \0"; +/// First FBX version with the object/connection layout read here. +const MIN_VERSION: i64 = 7000; +/// Deepest node nesting (and Model parent chain) followed. +const MAX_DEPTH: usize = 64; +/// Memory budget for parsed nodes and properties (arrays are held as f64). +const MAX_DECODED_BYTES: usize = 1 << 31; +/// Approximate memory charged per node and per scalar property. +const NODE_COST: usize = 96; +const PROP_COST: usize = 32; + +enum Prop { + I(i64), + F(f64), + S(String), + Arr(Vec), +} + +impl Prop { + fn num(&self) -> Option { + match self { + Prop::I(v) => Some(*v as f64), + Prop::F(v) => Some(*v), + _ => None, + } + } +} + +struct Node { + name: String, + props: Vec, + children: Vec, +} + +impl Node { + fn child(&self, name: &str) -> Option<&Node> { + self.children.iter().find(|c| c.name == name) + } + + fn all<'a>(&'a self, name: &'a str) -> impl Iterator { + self.children.iter().filter(move |c| c.name == name) + } + + fn int_at(&self, i: usize) -> Option { + match self.props.get(i) { + Some(Prop::I(v)) => Some(*v), + _ => None, + } + } + + fn str_at(&self, i: usize) -> &str { + match self.props.get(i) { + Some(Prop::S(s)) => s, + _ => "", + } + } + + fn child_str(&self, name: &str) -> &str { + self.child(name).map_or("", |c| c.str_at(0)) + } + + /// Array payload: a binary array, an ASCII `*N { a: ... }` block or a bare value list. + fn arr(&self) -> Cow<'_, [f64]> { + if let Some(Prop::Arr(a)) = self.props.first() { + return Cow::Borrowed(a); + } + if let Some(a) = self.child("a") { + return a.arr(); + } + Cow::Owned(self.props.iter().filter_map(Prop::num).collect()) + } + + /// The numbers of entry `name` in Properties70 (`P: name, type, subtype, flags, values`). + fn property(&self, name: &str) -> Option> { + let list = self.child("Properties70")?; + list.children.iter().find(|p| p.str_at(0) == name).map(|p| p.props.iter().filter_map(Prop::num).collect()) + } + + fn vec3(&self, name: &str, default: Vec3) -> Vec3 { + match self.property(name).as_deref() { + Some([x, y, z, ..]) => Vec3::new(*x as f32, *y as f32, *z as f32), + _ => default, + } + } + + fn number(&self, name: &str) -> Option { + self.property(name)?.first().copied() + } +} + +/// Charges parsed data against [`MAX_DECODED_BYTES`]. +struct Budget(usize); + +impl Budget { + fn charge(&mut self, bytes: usize) -> Result<()> { + self.0 = self.0.checked_sub(bytes).ok_or_else(|| FormatError::TooLarge(format!("the FBX file decodes to more than {MAX_DECODED_BYTES} bytes")))?; + Ok(()) + } +} + +// --------------------------------------------------------------------------------------------- +// Binary. + +struct Binary<'a> { + b: &'a [u8], + pos: usize, + /// 64-bit node records (version 7500 and later). + wide: bool, + budget: Budget, +} + +impl<'a> Binary<'a> { + fn take(&mut self, n: usize) -> Result<&'a [u8]> { + let s = self.pos.checked_add(n).and_then(|end| self.b.get(self.pos..end)).ok_or_else(|| malformed(KIND, "unexpected end of file"))?; + self.pos += n; + Ok(s) + } + + fn bytes(&mut self) -> Result<[u8; N]> { + let mut a = [0u8; N]; + a.copy_from_slice(self.take(N)?); + Ok(a) + } + + fn u32(&mut self) -> Result { + Ok(u32::from_le_bytes(self.bytes()?) as usize) + } + + fn uint(&mut self) -> Result { + Ok(if self.wide { u64::from_le_bytes(self.bytes()?) } else { u32::from_le_bytes(self.bytes()?) as u64 }) + } + + /// The next node ending at or before `limit`, or None at a null record / the end. + fn node(&mut self, limit: usize, depth: usize) -> Result> { + let header = if self.wide { 25 } else { 13 }; + if limit.saturating_sub(self.pos) < header { + return Ok(None); + } + let (end, nprops, plen) = (self.uint()?, self.uint()?, self.uint()?); + let [name_len] = self.bytes()?; + if end == 0 { + return Ok(None); + } + if depth > MAX_DEPTH { + return Err(malformed(KIND, "nodes are nested too deeply")); + } + let end = usize::try_from(end).ok().filter(|e| *e > self.pos && *e <= limit).ok_or_else(|| malformed(KIND, "node end offset out of range"))?; + self.budget.charge(NODE_COST)?; + let name = String::from_utf8_lossy(self.take(name_len as usize)?).into_owned(); + let props_end = usize::try_from(plen).ok().and_then(|p| self.pos.checked_add(p)).filter(|e| *e <= end); + let props_end = props_end.ok_or_else(|| malformed(KIND, format!("properties of \"{name}\" overrun the node")))?; + let mut props = Vec::new(); + for _ in 0..nprops { + if self.pos >= props_end { + return Err(malformed(KIND, format!("\"{name}\" claims more properties than it holds"))); + } + props.push(self.prop()?); + } + if self.pos > props_end { + return Err(malformed(KIND, format!("properties of \"{name}\" overrun their list"))); + } + self.pos = props_end; + let mut children = Vec::new(); + while let Some(c) = self.node(end, depth + 1)? { + children.push(c); + } + self.pos = end; + Ok(Some(Node { name, props, children })) + } + + fn prop(&mut self) -> Result { + self.budget.charge(PROP_COST)?; + let [ty] = self.bytes()?; + Ok(match ty { + b'Y' => Prop::I(i16::from_le_bytes(self.bytes()?) as i64), + b'C' | b'B' => Prop::I(self.bytes::<1>()?[0] as i64), + b'I' => Prop::I(i32::from_le_bytes(self.bytes()?) as i64), + b'L' => Prop::I(i64::from_le_bytes(self.bytes()?)), + b'F' => Prop::F(f32::from_le_bytes(self.bytes()?) as f64), + b'D' => Prop::F(f64::from_le_bytes(self.bytes()?)), + b'S' | b'R' => { + let n = self.u32()?; + Prop::S(String::from_utf8_lossy(self.take(n)?).into_owned()) + } + b'f' | b'd' | b'l' | b'i' | b'b' => Prop::Arr(self.array(ty)?), + other => return Err(malformed(KIND, format!("unknown property type 0x{other:02x}"))), + }) + } + + fn array(&mut self, ty: u8) -> Result> { + let (count, encoding, stored) = (self.u32()?, self.u32()?, self.u32()?); + let size = match ty { + b'd' | b'l' => 8, + b'f' | b'i' => 4, + _ => 1, + }; + let len = count.checked_mul(size).ok_or_else(|| malformed(KIND, "array too large"))?; + self.budget.charge(count.saturating_mul(8))?; + let data = self.take(stored)?; + let raw: Cow<[u8]> = match encoding { + 0 if stored == len => Cow::Borrowed(data), + 1 => { + let mut out = Vec::new(); + flate2::read::ZlibDecoder::new(data) + .take(len as u64 + 1) + .read_to_end(&mut out) + .map_err(|e| malformed(KIND, format!("bad compressed array: {e}")))?; + if out.len() != len { + return Err(malformed(KIND, "compressed array has the wrong length")); + } + Cow::Owned(out) + } + _ => return Err(malformed(KIND, format!("array encoding {encoding} with {stored} bytes for {count} items"))), + }; + fn le(c: &[u8]) -> [u8; N] { + <[u8; N]>::try_from(c).unwrap_or([0; N]) + } + Ok(match ty { + b'd' => raw.chunks_exact(8).map(|c| f64::from_le_bytes(le(c))).collect(), + b'l' => raw.chunks_exact(8).map(|c| i64::from_le_bytes(le(c)) as f64).collect(), + b'f' => raw.chunks_exact(4).map(|c| f32::from_le_bytes(le(c)) as f64).collect(), + b'i' => raw.chunks_exact(4).map(|c| i32::from_le_bytes(le(c)) as f64).collect(), + _ => raw.iter().map(|b| *b as f64).collect(), + }) + } +} + +fn parse_binary(b: &[u8]) -> Result<(Vec, i64)> { + let mut r = Binary { b, pos: MAGIC.len(), wide: false, budget: Budget(MAX_DECODED_BYTES) }; + let _reserved: [u8; 2] = r.bytes()?; + let version = u32::from_le_bytes(r.bytes()?); + r.wide = version >= 7500; + let mut nodes = Vec::new(); + while let Some(n) = r.node(b.len(), 0)? { + nodes.push(n); + } + Ok((nodes, version as i64)) +} + +// --------------------------------------------------------------------------------------------- +// ASCII. + +#[derive(Clone, Copy)] +enum Tok<'a> { + /// `name:` + Key(&'a str), + Str(&'a [u8]), + Word(&'a str), + Comma, + Open, + Close, +} + +struct Ascii<'a> { + s: &'a [u8], + pos: usize, + budget: Budget, +} + +impl<'a> Ascii<'a> { + /// The next token and the position after it (the position is not advanced). + fn lex(&self) -> Result, usize)>> { + let mut i = self.pos; + loop { + match self.s.get(i) { + None => return Ok(None), + Some(c) if c.is_ascii_whitespace() => i += 1, + Some(b';') => i += self.s.get(i..).unwrap_or_default().iter().position(|c| *c == b'\n').unwrap_or(self.s.len() - i), + Some(_) => break, + } + } + let rest = self.s.get(i..).unwrap_or_default(); + Ok(Some(match rest.first() { + Some(b',') => (Tok::Comma, i + 1), + Some(b'{') => (Tok::Open, i + 1), + Some(b'}') => (Tok::Close, i + 1), + Some(b'"') => { + let len = rest.get(1..).unwrap_or_default().iter().position(|c| *c == b'"').ok_or_else(|| malformed(KIND, "unterminated string"))?; + (Tok::Str(rest.get(1..1 + len).unwrap_or_default()), i + len + 2) + } + _ => { + let len = rest.iter().position(|c| c.is_ascii_whitespace() || b",{}\":;".contains(c)).unwrap_or(rest.len()); + if len == 0 { + return Err(malformed(KIND, format!("unexpected character at byte {i}"))); + } + let word = std::str::from_utf8(rest.get(..len).unwrap_or_default()).map_err(|_| malformed(KIND, "text is not UTF-8"))?; + if rest.get(len) == Some(&b':') { (Tok::Key(word), i + len + 1) } else { (Tok::Word(word), i + len) } + } + })) + } + + /// Nodes until `}` (left for the caller) or the end. + fn nodes(&mut self, depth: usize) -> Result> { + if depth > MAX_DEPTH { + return Err(malformed(KIND, "nodes are nested too deeply")); + } + let mut out = Vec::new(); + while let Some((tok, after)) = self.lex()? { + match tok { + Tok::Close => break, + Tok::Key(name) => { + self.pos = after; + out.push(self.node(name, depth)?); + } + _ => return Err(malformed(KIND, format!("expected a property name at byte {}", self.pos))), + } + } + Ok(out) + } + + fn node(&mut self, name: &str, depth: usize) -> Result { + self.budget.charge(NODE_COST)?; + let mut props = Vec::new(); + while let Some((tok, after)) = self.lex()? { + let prop = match tok { + Tok::Str(s) => Some(Prop::S(String::from_utf8_lossy(s).into_owned())), + // `*N` announces an array's length. + Tok::Word(w) if w.starts_with('*') => None, + Tok::Word(w) => Some(w.parse::().map(Prop::I).or_else(|_| w.parse::().map(Prop::F)).unwrap_or_else(|_| Prop::S(w.to_string()))), + _ => break, + }; + self.pos = after; + if let Some(p) = prop { + self.budget.charge(PROP_COST)?; + props.push(p); + } + match self.lex()? { + Some((Tok::Comma, after)) => self.pos = after, + _ => break, + } + } + let mut children = Vec::new(); + if let Some((Tok::Open, after)) = self.lex()? { + self.pos = after; + children = self.nodes(depth + 1)?; + match self.lex()? { + Some((Tok::Close, after)) => self.pos = after, + _ => return Err(malformed(KIND, format!("\"{name}\" is missing its closing brace"))), + } + } + // Long value lists (arrays) are stored compactly. + if props.len() > 1 && props.iter().all(|p| p.num().is_some()) { + props = vec![Prop::Arr(props.iter().filter_map(Prop::num).collect())]; + } + Ok(Node { name: name.to_string(), props, children }) + } +} + +fn parse_ascii(s: &[u8]) -> Result<(Vec, i64)> { + let mut a = Ascii { s, pos: 0, budget: Budget(MAX_DECODED_BYTES) }; + let nodes = a.nodes(0)?; + if a.lex()?.is_some() { + return Err(malformed(KIND, "unbalanced closing brace")); + } + let version = nodes.iter().find(|n| n.name == "FBXHeaderExtension").and_then(|h| h.child("FBXVersion")).and_then(|v| v.int_at(0)); + let version = version.ok_or_else(|| malformed(KIND, "not an FBX file (no FBXHeaderExtension / FBXVersion)"))?; + Ok((nodes, version)) +} + +// --------------------------------------------------------------------------------------------- +// Scene. + +pub(crate) fn read(bytes: &[u8]) -> Result { + crate::guard(KIND, || read_inner(bytes)) +} + +fn read_inner(bytes: &[u8]) -> Result { + let (nodes, version) = if bytes.starts_with(MAGIC) { parse_binary(bytes)? } else { parse_ascii(bytes)? }; + if version < MIN_VERSION { + return Err(FormatError::Unsupported(format!("FBX version {version} (re-export as FBX 2011 / 7.x or newer)"))); + } + let root = |name: &str| nodes.iter().find(|n| n.name == name); + let objects = root("Objects").ok_or_else(|| malformed(KIND, "the file has no Objects"))?; + let (mut geometries, mut models, mut materials) = (HashMap::new(), HashMap::new(), HashMap::new()); + for o in &objects.children { + let Some(id) = o.int_at(0) else { continue }; + match o.name.as_str() { + "Geometry" if o.child("Vertices").is_some() => geometries.insert(id, o), + "Model" => models.insert(id, o), + "Material" => materials.insert(id, o), + _ => None, + }; + } + // (geometry, model) instances, Model parents, and each Model's materials in slot order. + let mut instances: Vec<(i64, Option)> = Vec::new(); + let mut parents: HashMap = HashMap::new(); + let mut slots: HashMap> = HashMap::new(); + for c in root("Connections").into_iter().flat_map(|c| c.all("C")) { + let (Some(child), Some(parent)) = (c.int_at(1), c.int_at(2)) else { continue }; + if c.str_at(0) != "OO" || !models.contains_key(&parent) { + continue; + } + if geometries.contains_key(&child) { + instances.push((child, Some(parent))); + } else if let Some(m) = materials.get(&child) { + let name = clean_name(m.str_at(1)); + slots.entry(parent).or_default().push(if name.is_empty() { format!("Material_{child}") } else { name.to_string() }); + } else if models.contains_key(&child) { + parents.insert(child, parent); + } + } + let placed: HashSet = instances.iter().map(|(g, _)| *g).collect(); + let mut unplaced: Vec = geometries.keys().filter(|g| !placed.contains(g)).copied().collect(); + unplaced.sort_unstable(); + instances.extend(unplaced.into_iter().map(|g| (g, None))); + + let global = global_transform(root("GlobalSettings")); + let mut sink = PolygonSink::default(); + let no_slots = Vec::new(); + for (geom, model) in instances { + let Some(g) = geometries.get(&geom) else { continue }; + let m = match model.and_then(|m| models.get(&m).map(|n| (m, n))) { + Some((id, node)) => { + sink.part = clean_name(node.str_at(1)).to_string(); + world(&models, &parents, id)? * geometric(node) + } + None => { + sink.part.clear(); + Mat4::IDENTITY + } + }; + let names = model.and_then(|m| slots.get(&m)).unwrap_or(&no_slots); + add_geometry(&mut sink, g, global * m, names)?; + } + Ok(sink.finish()) +} + +/// `Name\0\1Class` (binary) or `Class::Name` (ASCII) to `Name`. +fn clean_name(s: &str) -> &str { + match s.split_once("\u{0}\u{1}") { + Some((name, _)) => name, + None => s.split_once("::").map_or(s, |(_, name)| name), + } +} + +/// Unit scale to meters and the file's axis system to Y up (+Z front, +X right). +fn global_transform(settings: Option<&Node>) -> Mat4 { + let get = |name: &str, default: f64| settings.and_then(|s| s.number(name)).filter(|v| v.is_finite()).unwrap_or(default); + let unit = get("UnitScaleFactor", 1.0); + let unit = if unit > 0.0 { unit / 100.0 } else { 0.01 }; + let axis = |a: &str, s: &str, default: f64| (get(a, default) as i64, if get(s, 1.0) < 0.0 { -1.0 } else { 1.0 }); + let [(right, rs), (up, us), (front, fs)] = + [axis("CoordAxis", "CoordAxisSign", 0.0), axis("UpAxis", "UpAxisSign", 1.0), axis("FrontAxis", "FrontAxisSign", 2.0)]; + let basis = |a: i64| [Vec3::X, Vec3::Y, Vec3::Z].get(a as usize).copied().filter(|_| a >= 0); + let rows = match (basis(right), basis(up), basis(front)) { + (Some(r), Some(u), Some(f)) if r.cross(u).dot(f).abs() > 0.5 => Mat3::from_cols(r * rs, u * us, f * fs).transpose(), + _ => Mat3::IDENTITY, + }; + Mat4::from_mat3(rows) * Mat4::from_scale(Vec3::splat(unit as f32)) +} + +/// Euler rotation in degrees, `order` as FBX RotationOrder (0 = XYZ: X applied first). +fn euler(deg: Vec3, order: i64) -> Mat4 { + use Vec3 as V; + let axes = match order { + 1 => [V::X, V::Z, V::Y], + 2 => [V::Y, V::Z, V::X], + 3 => [V::Y, V::X, V::Z], + 4 => [V::Z, V::X, V::Y], + 5 => [V::Z, V::Y, V::X], + _ => [V::X, V::Y, V::Z], + }; + axes.iter().fold(Mat4::IDENTITY, |m, a| Mat4::from_axis_angle(*a, deg.dot(*a).to_radians()) * m) +} + +/// A Model's local transform: T · Roff · Rp · Rpre · R · Rpost⁻¹ · Rp⁻¹ · Soff · Sp · S · Sp⁻¹. +fn local(m: &Node) -> Mat4 { + let t = |name: &str| Mat4::from_translation(m.vec3(name, Vec3::ZERO)); + let order = m.number("RotationOrder").unwrap_or(0.0) as i64; + let (rp, sp) = (m.vec3("RotationPivot", Vec3::ZERO), m.vec3("ScalingPivot", Vec3::ZERO)); + t("Lcl Translation") + * t("RotationOffset") + * Mat4::from_translation(rp) + * euler(m.vec3("PreRotation", Vec3::ZERO), 0) + * euler(m.vec3("Lcl Rotation", Vec3::ZERO), order) + * euler(m.vec3("PostRotation", Vec3::ZERO), 0).inverse() + * Mat4::from_translation(-rp) + * t("ScalingOffset") + * Mat4::from_translation(sp) + * Mat4::from_scale(m.vec3("Lcl Scaling", Vec3::ONE)) + * Mat4::from_translation(-sp) +} + +/// The geometry offset of a Model (not inherited by its children). +fn geometric(m: &Node) -> Mat4 { + Mat4::from_translation(m.vec3("GeometricTranslation", Vec3::ZERO)) + * euler(m.vec3("GeometricRotation", Vec3::ZERO), 0) + * Mat4::from_scale(m.vec3("GeometricScaling", Vec3::ONE)) +} + +fn world(models: &HashMap, parents: &HashMap, id: i64) -> Result { + let mut m = Mat4::IDENTITY; + let mut cur = Some(id); + for _ in 0..=MAX_DEPTH { + let Some(node) = cur.and_then(|c| models.get(&c)) else { return Ok(m) }; + m = local(node) * m; + cur = cur.and_then(|c| parents.get(&c)).copied(); + } + Err(malformed(KIND, "the Model hierarchy is nested too deeply (or cyclic)")) +} + +#[derive(Clone, Copy)] +enum Mapping { + PolygonVertex, + ControlPoint, + Polygon, + AllSame, +} + +/// One LayerElement's items, optionally through an index array. +struct Layer<'a> { + mapping: Mapping, + data: Cow<'a, [f64]>, + index: Option>, +} + +impl Layer<'_> { + /// Element `index` names the index array (empty: the data is never indexed). + fn read<'a>(geom: &'a Node, element: &str, data: &str, index: &str) -> Option> { + let el = geom.children.iter().filter(|c| c.name == element).min_by_key(|e| e.int_at(0) != Some(0))?; + let mapping = match el.child_str("MappingInformationType") { + "ByPolygonVertex" => Mapping::PolygonVertex, + "ByVertice" | "ByVertex" | "ByControlPoint" => Mapping::ControlPoint, + "ByPolygon" => Mapping::Polygon, + "AllSame" => Mapping::AllSame, + _ => return None, + }; + let indexed = !index.is_empty() && matches!(el.child_str("ReferenceInformationType"), "IndexToDirect" | "Index"); + let index = if indexed { Some(el.child(index)?.arr()) } else { None }; + Some(Layer { mapping, data: el.child(data)?.arr(), index }) + } + + /// The `W` numbers of polygon vertex `k` (control point `cp`, polygon `poly`). + fn get(&self, k: usize, cp: usize, poly: usize) -> Option<[f64; W]> { + let i = match self.mapping { + Mapping::PolygonVertex => k, + Mapping::ControlPoint => cp, + Mapping::Polygon => poly, + Mapping::AllSame => 0, + }; + let i = match &self.index { + Some(ix) => Some(*ix.get(i)?).filter(|v| *v >= 0.0)? as usize, + None => i, + }; + let start = i.checked_mul(W)?; + self.data.get(start..start.checked_add(W)?)?.try_into().ok() + } +} + +fn add_geometry(sink: &mut PolygonSink, g: &Node, m: Mat4, slots: &[String]) -> Result<()> { + let verts = g.child("Vertices").map(Node::arr).unwrap_or_default(); + check_vertices(verts.len() / 3)?; + let points: Vec = verts.chunks_exact(3).map(|c| m.transform_point3(Vec3::new(c[0] as f32, c[1] as f32, c[2] as f32))).collect(); + let pvi = g.child("PolygonVertexIndex").map(Node::arr).unwrap_or_default(); + let normals = Layer::read(g, "LayerElementNormal", "Normals", "NormalsIndex"); + let uvs = Layer::read(g, "LayerElementUV", "UV", "UVIndex"); + let mats = Layer::read(g, "LayerElementMaterial", "Materials", ""); + let nm = Mat3::from_mat4(m).inverse().transpose(); + let flip = m.determinant() < 0.0; + let (mut corners, mut poly, mut bad_normals, mut bad_uvs) = (Vec::new(), 0usize, 0usize, 0usize); + for (k, &v) in pvi.iter().enumerate() { + // The last corner of each polygon is stored as -(index + 1). + let (cp, last) = if v < 0.0 { (-v - 1.0, true) } else { (v, false) }; + let p = points.get(cp as usize).filter(|_| cp.fract() == 0.0).ok_or_else(|| malformed(KIND, format!("polygon vertex index {v} out of range")))?; + let cp = cp as usize; + let n = normals.as_ref().and_then(|l| { + let n = l.get(k, cp, poly).map(|[x, y, z]| nm * Vec3::new(x as f32, y as f32, z as f32)); + bad_normals += usize::from(n.is_none()); + n + }); + // A UV layer with holes keeps its layout: missing items read as 0, 0. + let uv = uvs.as_ref().map(|l| match l.get(k, cp, poly) { + Some([u, v]) => Vec2::new(u as f32, 1.0 - v as f32), + None => { + bad_uvs += 1; + Vec2::ZERO + } + }); + corners.push(Corner { p: *p, n, uv }); + if last || k + 1 == pvi.len() { + if flip { + corners.reverse(); + } + let slot = mats.as_ref().and_then(|l| l.get(poly, poly, poly)).map_or(0.0, |[s]| s); + let name = slots.get(slot as usize).filter(|_| slot >= 0.0).map_or("Default", String::as_str); + sink.polygon(name, &corners)?; + corners.clear(); + poly += 1; + } + } + if bad_normals > 0 { + sink.warn(format!("{bad_normals} polygon corners have no normal in the FBX normal layer; normals were recomputed")); + } + if bad_uvs > 0 { + sink.warn(format!("{bad_uvs} polygon corners have no UV in the FBX UV layer (set to 0, 0)")); + } + Ok(()) +} diff --git a/crates/formats/src/mesh_import/gltf.rs b/crates/formats/src/mesh_import/gltf.rs new file mode 100644 index 00000000..8e39097b --- /dev/null +++ b/crates/formats/src/mesh_import/gltf.rs @@ -0,0 +1,250 @@ +//! glTF 2.0 / GLB: the default scene's node tree, flattened to world space. + +use std::path::Path; + +use gltf::accessor::{Accessor, DataType, Dimensions}; +use gltf::mesh::Mode; +use gltf::{Node, Semantic}; +use texturecraft_geom::glam::{Mat3, Mat4, Vec2, Vec3, Vec4}; + +use super::{RawMesh, check_triangles, check_vertices}; +use crate::{FormatError, Result, malformed}; + +const KIND: &str = "glTF"; +/// Deepest node hierarchy followed (also stops cyclic node graphs). +const MAX_DEPTH: usize = 64; +/// Node instances visited (bounds exponential instancing in shared node graphs). +const MAX_NODE_VISITS: usize = 1_000_000; + +pub(crate) fn read(bytes: &[u8], base: Option<&Path>) -> Result { + crate::guard(KIND, || read_inner(bytes, base)) +} + +fn read_inner(bytes: &[u8], base: Option<&Path>) -> Result { + let gltf::Gltf { document, blob } = gltf::Gltf::from_slice(bytes).map_err(|e| malformed(KIND, e))?; + check_buffer_uris(&document, base)?; + let buffers = gltf::import_buffers(&document, base, blob).map_err(|e| match e { + gltf::Error::ExternalReferenceInSliceImport => FormatError::Unsupported("this glTF references external files; open it from disk or use a .glb".into()), + e => malformed(KIND, e), + })?; + + let roots: Vec = match document.default_scene().or_else(|| document.scenes().next()) { + Some(scene) => scene.nodes().collect(), + None => { + // No scene: every node that is nobody's child. + let mut is_child = vec![false; document.nodes().len()]; + for n in document.nodes() { + for c in n.children() { + if let Some(f) = is_child.get_mut(c.index()) { + *f = true; + } + } + } + document.nodes().filter(|n| !is_child.get(n.index()).copied().unwrap_or(true)).collect() + } + }; + let mut raw = RawMesh { normals: Some(Vec::new()), ..RawMesh::default() }; + let mut attrs = Attrs::default(); + let mut stack: Vec<(Node, Mat4, usize)> = roots.into_iter().rev().map(|n| (n, Mat4::IDENTITY, 0)).collect(); + let mut visits = 0usize; + while let Some((node, parent, depth)) = stack.pop() { + visits += 1; + if visits > MAX_NODE_VISITS { + return Err(FormatError::TooLarge(format!("the glTF scene has more than {MAX_NODE_VISITS} node instances"))); + } + let m = parent * Mat4::from_cols_array_2d(&node.transform().matrix()); + if let Some(mesh) = node.mesh() { + let part = node.name().or_else(|| mesh.name()).unwrap_or_default(); + for prim in mesh.primitives() { + add_primitive(&mut raw, &mut attrs, &prim, m, &buffers, part)?; + } + } + if depth >= MAX_DEPTH { + return Err(malformed(KIND, "the node hierarchy is nested too deeply (or cyclic)")); + } + stack.extend(node.children().collect::>().into_iter().rev().map(|c| (c, m, depth + 1))); + } + match attrs.uv_seen { + (_, false) => raw.uvs = Some(attrs.uvs), + (true, true) => raw.warnings.push("some primitives have no UVs; the whole mesh was unwrapped automatically".into()), + (false, true) => {} + } + raw.colors = attrs.color_seen.then_some(attrs.colors); + Ok(raw) +} + +/// Per-vertex attributes gathered across primitives. +#[derive(Default)] +struct Attrs { + uvs: Vec, + /// (some primitive had UVs, some primitive had none) + uv_seen: (bool, bool), + /// White where a primitive has no COLOR_0. + colors: Vec, + color_seen: bool, +} + +fn add_primitive(raw: &mut RawMesh, attrs: &mut Attrs, prim: &gltf::Primitive, m: Mat4, buffers: &[gltf::buffer::Data], part: &str) -> Result<()> { + if prim.mode() != Mode::Triangles { + raw.warnings.push(format!("skipped a primitive drawn as {:?} (only triangles are supported)", prim.mode())); + return Ok(()); + } + let Some(pacc) = prim.get(&Semantic::Positions) else { + raw.warnings.push("skipped a primitive without positions".into()); + return Ok(()); + }; + check_accessor(&pacc, buffers, Dimensions::Vec3, &[DataType::F32], "position")?; + let nacc = prim.get(&Semantic::Normals); + if let Some(a) = &nacc { + check_accessor(a, buffers, Dimensions::Vec3, &[DataType::F32], "normal")?; + } + let tacc = prim.get(&Semantic::TexCoords(0)); + if let Some(a) = &tacc { + check_accessor(a, buffers, Dimensions::Vec2, &[DataType::F32, DataType::U8, DataType::U16], "texcoord")?; + } + let cacc = prim.get(&Semantic::Colors(0)); + if let Some(a) = &cacc { + let dims = if a.dimensions() == Dimensions::Vec3 { Dimensions::Vec3 } else { Dimensions::Vec4 }; + check_accessor(a, buffers, dims, &[DataType::F32, DataType::U8, DataType::U16], "color")?; + } + if let Some(a) = prim.indices() { + check_accessor(&a, buffers, Dimensions::Scalar, &[DataType::U8, DataType::U16, DataType::U32], "index")?; + } + let reader = prim.reader(|b| buffers.get(b.index()).map(|d| &d.0[..])); + let positions: Vec = reader.read_positions().ok_or_else(|| malformed(KIND, "unreadable positions"))?.map(Vec3::from).collect(); + let n = positions.len(); + check_vertices(raw.positions.len().saturating_add(n))?; + let mut indices: Vec = match reader.read_indices() { + Some(i) => i.into_u32().collect(), + None => (0..n as u32).collect(), + }; + indices.truncate(indices.len() / 3 * 3); + if let Some(bad) = indices.iter().find(|i| **i as usize >= n) { + return Err(malformed(KIND, format!("index {bad} out of range ({n} vertices)"))); + } + check_triangles(raw.triangle_count(), indices.len() / 3)?; + let normals: Option> = nacc.and_then(|_| reader.read_normals()).map(|it| it.map(Vec3::from).collect()); + let texcoords: Option> = tacc.and_then(|_| reader.read_tex_coords(0)).map(|t| t.into_f32().map(Vec2::from).collect()); + let colors: Option> = + cacc.and_then(|_| reader.read_colors(0)).map(|c| c.into_rgba_f32().map(Vec4::from).collect()).filter(|c: &Vec| c.len() == n); + attrs.color_seen |= colors.is_some(); + let color_of = |i: usize| colors.as_ref().and_then(|c| c.get(i)).copied().unwrap_or(Vec4::ONE); + + let m3 = Mat3::from_mat4(m); + let nm = m3.inverse().transpose(); + let flip = m3.determinant() < 0.0; + let world: Vec = positions.iter().map(|p| m.transform_point3(*p)).collect(); + let name = match prim.material().index() { + None => "Default".to_string(), + Some(i) => prim.material().name().map(str::to_string).unwrap_or_else(|| format!("Material_{i}")), + }; + let base = raw.positions.len() as u32; + let tex_ok = texcoords.as_ref().is_some_and(|t| t.len() == n); + if tex_ok { + attrs.uv_seen.0 = true + } else { + attrs.uv_seen.1 = true + } + let uv_of = |i: usize| texcoords.as_ref().and_then(|t| t.get(i)).copied().unwrap_or_default(); + let raw_normals = raw.normals.get_or_insert_with(Vec::new); + match normals.filter(|v| v.len() == n) { + Some(nrm) => { + raw.positions.extend_from_slice(&world); + raw_normals.extend(nrm.iter().map(|v| (nm * *v).try_normalize().unwrap_or(Vec3::Y))); + attrs.uvs.extend((0..n).map(uv_of)); + attrs.colors.extend((0..n).map(color_of)); + let group = raw.group(&name, part); + for t in indices.chunks_exact(3) { + let (a, b, c) = (t[0] + base, t[1] + base, t[2] + base); + group.extend_from_slice(&if flip { [a, c, b] } else { [a, b, c] }); + } + } + None => { + // glTF: flat normals when none are given, so every triangle gets its own corners. + let mut tris = Vec::with_capacity(indices.len()); + for t in indices.chunks_exact(3) { + let order = if flip { [t[0], t[2], t[1]] } else { [t[0], t[1], t[2]] }; + let p = order.map(|i| world.get(i as usize).copied().unwrap_or_default()); + let face = (p[1] - p[0]).cross(p[2] - p[0]).try_normalize().unwrap_or(Vec3::Y); + for (k, i) in order.iter().enumerate() { + tris.push(raw.positions.len() as u32); + raw.positions.push(p[k]); + raw_normals.push(face); + attrs.uvs.push(uv_of(*i as usize)); + attrs.colors.push(color_of(*i as usize)); + } + } + raw.group(&name, part).extend_from_slice(&tris); + } + } + Ok(()) +} + +/// Reject accessors the `gltf` reader would panic on or read out of bounds. +fn check_accessor(acc: &Accessor, buffers: &[gltf::buffer::Data], dims: Dimensions, types: &[DataType], what: &str) -> Result<()> { + let bad = |msg: &str| malformed(KIND, format!("{what} accessor {}: {msg}", acc.index())); + if acc.sparse().is_some() { + return Err(FormatError::Unsupported(format!("sparse glTF accessors ({what})"))); + } + if acc.dimensions() != dims || !types.contains(&acc.data_type()) { + return Err(bad("unexpected element type")); + } + let view = acc.view().ok_or_else(|| bad("has no buffer view"))?; + let data_len = buffers.get(view.buffer().index()).map(|d| d.0.len()).ok_or_else(|| bad("buffer is missing"))?; + if view.offset().checked_add(view.length()).is_none_or(|end| end > data_len) { + return Err(bad("buffer view exceeds its buffer")); + } + let size = acc.size(); + let stride = view.stride().unwrap_or(size); + let end = acc.count().checked_sub(1).and_then(|k| k.checked_mul(stride)).and_then(|x| x.checked_add(acc.offset())).and_then(|x| x.checked_add(size)); + if stride < size || end.is_none_or(|e| e > view.length()) { + return Err(bad("data exceeds its buffer view")); + } + Ok(()) +} + +/// Check buffer URIs before `gltf` resolves them: percent-decoding must give UTF-8 (`gltf` +/// panics otherwise), and files read from disk must be regular files of sane size. +fn check_buffer_uris(doc: &gltf::Document, base: Option<&Path>) -> Result<()> { + for buf in doc.buffers() { + let gltf::buffer::Source::Uri(uri) = buf.source() else { continue }; + if uri.starts_with("data:") { + continue; + } + let path = if let Some(rest) = uri.strip_prefix("file://").or_else(|| uri.strip_prefix("file:")) { + Some(std::path::PathBuf::from(rest)) + } else if uri.contains(':') { + None + } else { + let decoded = percent_decode(uri).ok_or_else(|| malformed(KIND, format!("buffer URI \"{uri}\" is not valid UTF-8")))?; + base.map(|b| b.join(decoded)) + }; + if let (Some(path), Some(_)) = (path, base) { + let meta = std::fs::metadata(&path).map_err(|e| malformed(KIND, format!("buffer {}: {e}", path.display())))?; + if !meta.is_file() || meta.len() > super::MAX_MESH_FILE_BYTES { + return Err(malformed(KIND, format!("buffer {} is not a regular file of acceptable size", path.display()))); + } + } + } + Ok(()) +} + +fn percent_decode(s: &str) -> Option { + let b = s.as_bytes(); + let mut out = Vec::with_capacity(b.len()); + let mut i = 0; + while let Some(&c) = b.get(i) { + let hex = |k: usize| b.get(k).and_then(|h| (*h as char).to_digit(16)); + match (c, hex(i + 1), hex(i + 2)) { + (b'%', Some(h), Some(l)) => { + out.push((h * 16 + l) as u8); + i += 3; + } + _ => { + out.push(c); + i += 1; + } + } + } + String::from_utf8(out).ok() +} diff --git a/crates/formats/src/mesh_import/mod.rs b/crates/formats/src/mesh_import/mod.rs new file mode 100644 index 00000000..810dd953 --- /dev/null +++ b/crates/formats/src/mesh_import/mod.rs @@ -0,0 +1,359 @@ +//! Mesh import: glTF/GLB, FBX, COLLADA, OBJ, PLY and STL into a world-space [`Mesh`] with one +//! submesh per material and part (glTF node, OBJ object, FBX model, COLLADA node; 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 +//! missing) and MikkTSpace tangents. + +mod dae; +mod fbx; +mod gltf; +mod obj; +mod ply; +#[cfg(test)] +mod scene_tests; +mod stl; +#[cfg(test)] +mod tests; + +use std::collections::HashMap; +use std::collections::hash_map::Entry; + +use texturecraft_geom::glam::{Vec2, Vec3, Vec4}; +use texturecraft_geom::unwrap::{all_triangles_degenerate, auto_unwrap, weld_ids}; +use texturecraft_geom::{MAX_TRIANGLES, Mesh, SubMesh, UvGBuffer}; + +use crate::{FormatError, Result}; + +/// Mesh files larger than this are refused before reading. +pub const MAX_MESH_FILE_BYTES: u64 = 2 << 30; +/// Vertices accepted from a file. +pub const MAX_VERTICES: usize = 3 * MAX_TRIANGLES; +/// Layout parameters of the automatic unwrap (padding in texels at this resolution). +const UNWRAP_RESOLUTION: u32 = 1024; +const UNWRAP_PADDING: f32 = 4.0; +/// Resolution of the overlap check in the report. +const REPORT_RESOLUTION: u32 = 512; + +/// File extensions [`import_mesh_bytes`] understands. +pub const MESH_EXTENSIONS: [&str; 7] = ["gltf", "glb", "fbx", "dae", "obj", "ply", "stl"]; + +#[derive(Clone, Debug, PartialEq)] +pub struct ImportedMesh { + pub mesh: Mesh, + pub report: MeshReport, +} + +/// What the importer found and fixed, for the import dialog. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct MeshReport { + pub triangles: usize, + pub vertices: usize, + pub materials: usize, + /// The file had no UVs; they were generated by the automatic unwrap. + pub uvs_generated: bool, + /// Triangles with a UV outside 0..1. + pub uv_out_of_range: usize, + /// Overlapping texels / covered texels (512² per material). + pub overlap_ratio: f32, + pub zero_area_uv_triangles: usize, + pub warnings: Vec, +} + +/// Geometry as read from a file, before normals, UVs and tangents are completed. +#[derive(Default)] +pub(crate) struct RawMesh { + pub positions: Vec, + /// Per-vertex normals for every vertex, or None to compute smooth (welded) normals. + pub normals: Option>, + /// Per-vertex UVs (glTF convention) for every vertex, or None to auto-unwrap. + pub uvs: Option>, + /// Per-vertex linear RGBA colors for every vertex, or None. + pub colors: Option>, + /// Triangles (vertex indices) per (material, part name); an empty part is unnamed. + pub groups: Vec<(String, String, Vec)>, + pub warnings: Vec, +} + +impl RawMesh { + /// Triangle list of `material` in `part`, created on first use. + pub fn group(&mut self, material: &str, part: &str) -> &mut Vec { + let i = match self.groups.iter().position(|(m, p, _)| m == material && p == part) { + Some(i) => i, + None => { + self.groups.push((material.to_string(), part.to_string(), Vec::new())); + self.groups.len() - 1 + } + }; + &mut self.groups[i].2 + } + + pub fn triangle_count(&self) -> usize { + self.groups.iter().map(|(_, _, g)| g.len() / 3).sum() + } +} + +/// One polygon corner of a scene format (FBX, COLLADA), already in world space. +#[derive(Clone, Copy)] +pub(crate) struct Corner { + pub p: Vec3, + pub n: Option, + pub uv: Option, +} + +/// 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). +#[derive(Default)] +pub(crate) struct PolygonSink { + /// Part name of the polygons that follow. + pub part: String, + raw: RawMesh, + normals: Vec, + uvs: Vec, + missing_normals: bool, + missing_uvs: bool, + some_uvs: bool, + ids: HashMap<[u32; 8], u32>, + polygon: Vec, + triangles: usize, +} + +impl PolygonSink { + pub fn polygon(&mut self, material: &str, corners: &[Corner]) -> Result<()> { + if corners.len() < 3 { + return Ok(()); + } + check_triangles(self.triangles, corners.len() - 2)?; + self.triangles += corners.len() - 2; + self.polygon.clear(); + for c in corners { + self.missing_normals |= c.n.is_none(); + self.missing_uvs |= c.uv.is_none(); + self.some_uvs |= c.uv.is_some(); + let (n, uv) = (c.n.unwrap_or_default(), c.uv.unwrap_or_default()); + 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 id = match self.ids.entry(key) { + Entry::Occupied(e) => *e.get(), + Entry::Vacant(e) => { + let id = self.raw.positions.len(); + check_vertices(id + 1)?; + self.raw.positions.push(c.p); + self.normals.push(n); + self.uvs.push(uv); + *e.insert(id as u32) + } + }; + self.polygon.push(id); + } + let group = self.raw.group(material, &self.part); + let (first, rest) = self.polygon.split_first().unwrap_or((&0, &[])); + for w in rest.windows(2) { + group.extend_from_slice(&[*first, w[0], w[1]]); + } + Ok(()) + } + + pub fn warn(&mut self, msg: String) { + if !self.raw.warnings.contains(&msg) { + self.raw.warnings.push(msg); + } + } + + pub fn finish(mut self) -> RawMesh { + if self.missing_uvs && self.some_uvs { + self.warn("some polygons have no UVs; the whole mesh was unwrapped automatically".into()); + } + self.raw.normals = (!self.missing_normals).then_some(self.normals); + self.raw.uvs = (!self.missing_uvs).then_some(self.uvs); + self.raw + } +} + +/// Error unless `n` more triangles keep the total within [`MAX_TRIANGLES`]. +pub(crate) fn check_triangles(have: usize, more: usize) -> Result<()> { + if have.saturating_add(more) > MAX_TRIANGLES { + return Err(FormatError::TooLarge(format!("mesh has more than {MAX_TRIANGLES} triangles"))); + } + Ok(()) +} + +pub(crate) fn check_vertices(n: usize) -> Result<()> { + if n > MAX_VERTICES { + return Err(FormatError::TooLarge(format!("mesh has more than {MAX_VERTICES} vertices"))); + } + Ok(()) +} + +fn extension(file_name: &str) -> String { + std::path::Path::new(file_name).extension().and_then(|e| e.to_str()).unwrap_or_default().to_ascii_lowercase() +} + +/// Import a mesh from bytes; the format comes from `file_name`'s extension. glTF must be GLB or +/// self-contained (data-URI buffers); OBJ material libraries are not read (usemtl names are). +pub fn import_mesh_bytes(file_name: &str, bytes: &[u8]) -> Result { + finish(read(file_name, bytes, None)?) +} + +/// Import a mesh file. A `.gltf` may reference external `.bin` buffers next to it. +#[cfg(not(target_arch = "wasm32"))] +pub fn import_mesh(path: &std::path::Path) -> Result { + let bytes = crate::read_capped(path, MAX_MESH_FILE_BYTES)?; + let name = path.file_name().and_then(|n| n.to_str()).unwrap_or_default(); + finish(read(name, &bytes, path.parent())?) +} + +/// Import a high-poly (or cage) mesh for baking: like [`import_mesh_bytes`], but missing UVs stay +/// at 0 and no tangents are generated (bakers read positions, normals, colors and part names), so +/// million-triangle meshes import quickly. +pub fn import_high_poly_bytes(file_name: &str, bytes: &[u8]) -> Result { + let (mut mesh, _, uvs) = geometry(read(file_name, bytes, None)?)?; + let n = mesh.positions.len(); + mesh.uvs = uvs.filter(|uv| uv.len() == n && uv.iter().all(|v| v.is_finite())).unwrap_or_else(|| vec![Vec2::ZERO; n]); + mesh.tangents = vec![Vec4::new(1.0, 0.0, 0.0, 1.0); n]; + mesh.validate()?; + Ok(mesh) +} + +fn read(file_name: &str, bytes: &[u8], base: Option<&std::path::Path>) -> Result { + let mut raw = match extension(file_name).as_str() { + "gltf" | "glb" => gltf::read(bytes, base)?, + "fbx" => fbx::read(bytes)?, + "dae" => dae::read(bytes)?, + "obj" => obj::read(bytes)?, + "ply" => ply::read(bytes)?, + "stl" => stl::read(bytes)?, + other => return Err(FormatError::Unsupported(format!("mesh format \".{other}\" (supported: {})", MESH_EXTENSIONS.join(", ")))), + }; + let stem = std::path::Path::new(file_name).file_stem().and_then(|s| s.to_str()).unwrap_or_default(); + for (material, part, _) in &mut raw.groups { + if part.is_empty() { + *part = if stem.is_empty() { material.clone() } else { stem.to_string() }; + } + } + Ok(raw) +} + +/// Positions, submeshes (materials contiguous), normals and colors of a raw mesh, validated; +/// plus its warnings and UVs (the mesh's UVs and tangents are left empty). +fn geometry(raw: RawMesh) -> Result<(Mesh, Vec, Option>)> { + let RawMesh { positions, normals, uvs, colors, groups, warnings } = raw; + check_vertices(positions.len())?; + check_triangles(0, groups.iter().map(|(_, _, g)| g.len() / 3).sum())?; + let mut mesh = Mesh { positions, ..Mesh::default() }; + let mut groups: Vec<(usize, String, Vec)> = groups + .into_iter() + .filter(|(_, _, g)| g.len() >= 3) + .map(|(material, part, tris)| { + let m = mesh.materials.iter().position(|x| *x == material).unwrap_or(mesh.materials.len()); + if m == mesh.materials.len() { + mesh.materials.push(material); + } + (m, part, tris) + }) + .collect(); + groups.sort_by_key(|g| g.0); + for (material, name, tris) in groups { + let start = u32::try_from(mesh.indices.len()).map_err(|_| FormatError::TooLarge("index count".into()))?; + let count = (tris.len() / 3 * 3) as u32; + mesh.indices.extend_from_slice(tris.get(..count as usize).unwrap_or_default()); + mesh.submeshes.push(SubMesh { name, material, start, count }); + } + if mesh.indices.is_empty() { + return Err(FormatError::Invalid("the file contains no triangles".into())); + } + if let Some(bad) = mesh.indices.iter().find(|i| **i as usize >= mesh.positions.len()) { + return Err(FormatError::Invalid(format!("vertex index {bad} out of range ({} vertices)", mesh.positions.len()))); + } + if mesh.positions.iter().any(|p| !p.is_finite()) { + return Err(FormatError::Invalid("the mesh has non-finite vertex positions".into())); + } + if all_triangles_degenerate(&mesh) { + return Err(FormatError::Invalid("every triangle of the mesh is degenerate (zero area)".into())); + } + mesh.normals = match normals { + Some(n) if n.len() == mesh.positions.len() => n.into_iter().map(|v| v.try_normalize().unwrap_or(Vec3::Y)).collect(), + _ => smooth_normals(&mesh), + }; + if let Some(c) = colors.filter(|c| c.len() == mesh.positions.len()) { + mesh.colors = c.into_iter().map(|v| if v.is_finite() { v.clamp(Vec4::ZERO, Vec4::ONE) } else { Vec4::ONE }).collect(); + } + Ok((mesh, warnings, uvs)) +} + +/// Complete a raw mesh: submeshes, normals, UVs, tangents, validation and the report. +pub(crate) fn finish(raw: RawMesh) -> Result { + let (mut mesh, mut warnings, uvs) = geometry(raw)?; + let uvs_generated = match uvs { + Some(uv) if uv.len() == mesh.positions.len() => { + if uv.iter().any(|v| !v.is_finite()) { + warnings.push("non-finite UVs were replaced with 0".into()); + } + mesh.uvs = uv.into_iter().map(|v| if v.is_finite() { v } else { Vec2::ZERO }).collect(); + mesh.compute_tangents(); + false + } + _ => { + let charts = auto_unwrap(&mut mesh, UNWRAP_PADDING, UNWRAP_RESOLUTION)?; + warnings.push(format!("the mesh has no UVs; generated an automatic unwrap with {charts} charts")); + true + } + }; + mesh.validate()?; + let report = report(&mesh, uvs_generated, warnings); + Ok(ImportedMesh { mesh, report }) +} + +/// Area-weighted smooth normals over welded positions (UV seams do not split shading). +fn smooth_normals(mesh: &Mesh) -> Vec { + let weld = weld_ids(&mesh.positions); + let mut acc = vec![Vec3::ZERO; mesh.positions.len()]; + for t in mesh.indices.chunks_exact(3) { + let p = |i: u32| mesh.positions.get(i as usize).copied().unwrap_or_default(); + let face = (p(t[1]) - p(t[0])).cross(p(t[2]) - p(t[0])); + for v in t { + if let Some(a) = weld.get(*v as usize).and_then(|w| acc.get_mut(*w as usize)) { + *a += face; + } + } + } + weld.iter().map(|w| acc.get(*w as usize).and_then(|n| n.try_normalize()).unwrap_or(Vec3::Y)).collect() +} + +fn report(mesh: &Mesh, uvs_generated: bool, mut warnings: Vec) -> MeshReport { + let (mut out_of_range, mut zero_area) = (0, 0); + for t in 0..mesh.triangle_count() { + let Some(vs) = mesh.tri(t) else { continue }; + let uv = vs.map(|v| mesh.uvs.get(v).copied().unwrap_or_default()); + if uv.iter().any(|u| u.min_element() < -1e-4 || u.max_element() > 1.0 + 1e-4) { + out_of_range += 1; + } + if (uv[1] - uv[0]).perp_dot(uv[2] - uv[0]).abs() < 1e-12 { + zero_area += 1; + } + } + let (mut overlaps, mut covered) = (0u64, 0u64); + for m in 0..mesh.materials.len() { + let g = UvGBuffer::new(mesh, m, REPORT_RESOLUTION, REPORT_RESOLUTION); + overlaps += g.overlaps as u64; + covered += g.covered_count() as u64; + } + let overlap_ratio = if covered == 0 { 0.0 } else { overlaps as f32 / covered as f32 }; + if out_of_range > 0 { + warnings.push(format!("{out_of_range} triangles have UVs outside 0..1 (UDIM tiles are not supported; those parts will not be painted)")); + } + if overlap_ratio > 0.01 { + warnings.push(format!("{:.1}% of the UV layout overlaps; painting there affects several places at once", overlap_ratio * 100.0)); + } + if zero_area > 0 { + warnings.push(format!("{zero_area} triangles have zero UV area and cannot be painted")); + } + MeshReport { + triangles: mesh.triangle_count(), + vertices: mesh.vertex_count(), + materials: mesh.materials.len(), + uvs_generated, + uv_out_of_range: out_of_range, + overlap_ratio, + zero_area_uv_triangles: zero_area, + warnings, + } +} diff --git a/crates/formats/src/mesh_import/obj.rs b/crates/formats/src/mesh_import/obj.rs new file mode 100644 index 00000000..ce259a5a --- /dev/null +++ b/crates/formats/src/mesh_import/obj.rs @@ -0,0 +1,85 @@ +//! Wavefront OBJ (via `tobj`): polygons fan-triangulated, one vertex per unique v/vt/vn. + +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 { + crate::guard(KIND, || read_inner(bytes)) +} + +fn read_inner(bytes: &[u8]) -> Result { + let text = String::from_utf8_lossy(bytes); + // Material names come from `usemtl` (MTL files are never read): tobj only assigns a face's + // material when a library defines it, so every library request gets a synthetic one. + 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(|n| format!("newmtl {n}\n")).collect(); + let source = format!("mtllib texturecraft-usemtl.mtl\n{text}"); + let opts = tobj::LoadOptions { single_index: true, triangulate: true, ignore_points: true, ignore_lines: true }; + let (models, materials) = + tobj::load_obj_buf(&mut source.as_bytes(), &opts, |_| tobj::load_mtl_buf(&mut library.as_bytes())).map_err(|e| malformed(KIND, e))?; + let materials = materials.unwrap_or_default(); + + let mut raw = RawMesh::default(); + let (mut normals, mut uvs, mut colors) = (Some(Vec::new()), Some(Vec::new()), Vec::new()); + let mut partial_uvs = false; + for model in &models { + let m = &model.mesh; + let n = m.positions.len() / 3; + check_vertices(raw.positions.len().saturating_add(n))?; + check_triangles(raw.triangle_count(), m.indices.len() / 3)?; + let base = raw.positions.len() as u32; + raw.positions.extend(m.positions.chunks_exact(3).map(|p| Vec3::new(p[0], p[1], p[2]))); + if m.normals.len() == n * 3 { + if let Some(v) = &mut normals { + v.extend(m.normals.chunks_exact(3).map(|p| Vec3::new(p[0], p[1], p[2]))); + } + } else { + normals = None; + } + if m.texcoords.len() == n * 2 { + if let Some(v) = &mut uvs { + // OBJ UVs have their origin bottom-left. + v.extend(m.texcoords.chunks_exact(2).map(|t| Vec2::new(t[0], 1.0 - t[1]))); + } + } else { + partial_uvs |= n > 0 && !m.indices.is_empty(); + uvs = None; + } + // `v x y z r g b`: objects without colors are white. + match m.vertex_color.len() == n * 3 { + true => colors.extend(m.vertex_color.chunks_exact(3).map(|c| Vec4::new(c[0], c[1], c[2], 1.0))), + false => colors.extend(std::iter::repeat_n(Vec4::ONE, n)), + } + let name = match m.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(), + }; + let group = raw.group(&name, &model.name); + for &i in m.indices.get(..m.indices.len() / 3 * 3).unwrap_or_default() { + if i as usize >= n { + return Err(malformed(KIND, format!("face index {i} out of range"))); + } + group.push(base + i); + } + } + if partial_uvs && models.iter().any(|m| !m.mesh.texcoords.is_empty()) { + raw.warnings.push("some objects have no UVs; the whole mesh was unwrapped automatically".into()); + } + raw.normals = normals; + raw.uvs = uvs; + raw.colors = models.iter().any(|m| !m.mesh.vertex_color.is_empty()).then_some(colors); + Ok(raw) +} diff --git a/crates/formats/src/mesh_import/ply.rs b/crates/formats/src/mesh_import/ply.rs new file mode 100644 index 00000000..1452aa41 --- /dev/null +++ b/crates/formats/src/mesh_import/ply.rs @@ -0,0 +1,262 @@ +//! Stanford PLY (ascii, binary little and big endian): vertex positions, normals, UVs and colors, +//! and faces of any size (fan-triangulated). Other elements and properties are skipped. + +use texturecraft_geom::glam::{Vec2, Vec3, Vec4}; + +use super::{RawMesh, check_triangles, check_vertices}; +use crate::{Result, malformed}; + +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, +} + +impl Ty { + fn parse(s: &str) -> Option { + 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, + } + } +} + +struct Property { + name: String, + ty: Ty, + /// Count type of a list property. + list: Option, +} + +struct Element { + name: String, + count: usize, + props: Vec, +} + +#[derive(Clone, Copy, PartialEq)] +enum Encoding { + Ascii, + Le, + 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 { + if self.enc == Encoding::Ascii { + let tok = self.next_token().ok_or_else(|| malformed(KIND, "unexpected end of data"))?; + return tok.parse::().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 { + let (elements, enc, body_start) = header(bytes)?; + let mut body = Body { data: bytes.get(body_start..).unwrap_or_default(), pos: 0, enc }; + let mut raw = RawMesh::default(); + let (mut normals, mut uvs, mut colors) = (Vec::new(), Vec::new(), Vec::new()); + let mut faces: Vec = Vec::new(); + let mut nverts = 0usize; + for el in &elements { + // 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))); + } + let find = |names: &[&str]| el.props.iter().position(|p| p.list.is_none() && names.contains(&p.name.as_str())); + let (px, py, pz) = (find(&["x"]), find(&["y"]), find(&["z"])); + let (nx, ny, nz) = (find(&["nx"]), find(&["ny"]), find(&["nz"])); + let (tu, tv) = (find(&["s", "u", "texture_u"]), find(&["t", "v", "texture_v"])); + let (cr, cg, cb, ca) = (find(&["red", "r"]), find(&["green", "g"]), find(&["blue", "b"]), find(&["alpha", "a"])); + let has_color = cr.is_some() && cg.is_some() && cb.is_some(); + let vertex = el.name == "vertex"; + let has_n = nx.is_some() && ny.is_some() && nz.is_some(); + let has_uv = tu.is_some() && tv.is_some(); + if vertex { + if px.is_none() || py.is_none() || pz.is_none() { + return Err(malformed(KIND, "vertices have no x, y, z")); + } + check_vertices(el.count)?; + nverts = el.count; + raw.positions.reserve(el.count); + } + let face_list = + (el.name == "face").then(|| el.props.iter().position(|p| p.list.is_some() && (p.name == "vertex_indices" || p.name == "vertex_index"))).flatten(); + let mut vals = vec![0.0f64; el.props.len()]; + let mut poly: Vec = Vec::new(); + for _ in 0..el.count { + for (k, p) in el.props.iter().enumerate() { + match p.list { + None => { + if let Some(v) = vals.get_mut(k) { + *v = body.read(p.ty)?; + } + } + Some(ct) => { + let len = body.read(ct)?; + 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)?; + if face_list == Some(k) { + if !(0.0..nverts as f64).contains(&v) { + return Err(malformed(KIND, format!("face index {v} out of range"))); + } + poly.push(v as u32); + } + } + if face_list == Some(k) && poly.len() >= 3 { + check_triangles(faces.len() / 3, poly.len() - 2)?; + for i in 1..poly.len() - 1 { + faces.extend_from_slice(&[poly[0], poly[i], poly[i + 1]]); + } + } + } + } + } + if vertex { + let g = |i: Option| i.and_then(|i| vals.get(i)).copied().unwrap_or(0.0) as f32; + raw.positions.push(Vec3::new(g(px), g(py), g(pz))); + if has_n { + normals.push(Vec3::new(g(nx), g(ny), g(nz))); + } + if has_uv { + // PLY texture coordinates follow OpenGL (origin bottom-left). + uvs.push(Vec2::new(g(tu), 1.0 - g(tv))); + } + if has_color { + // Integer channels are normalized by their type's maximum; floats are 0..1. + let c = |i: Option, default: f32| match i.and_then(|i| el.props.get(i)) { + Some(p) => g(i) / color_scale(p.ty), + None => default, + }; + colors.push(Vec4::new(c(cr, 1.0), c(cg, 1.0), c(cb, 1.0), c(ca, 1.0))); + } + } + } + } + let n = raw.positions.len(); + raw.normals = (normals.len() == n).then_some(normals); + raw.uvs = (uvs.len() == n).then_some(uvs); + raw.colors = (colors.len() == n).then_some(colors); + raw.groups.push(("Default".into(), String::new(), faces)); + Ok(raw) +} + +fn color_scale(ty: Ty) -> f32 { + match ty { + Ty::U8 | Ty::I8 => 255.0, + Ty::U16 | Ty::I16 => 65535.0, + Ty::U32 | Ty::I32 => u32::MAX as f32, + Ty::F32 | Ty::F64 => 1.0, + } +} + +fn header(bytes: &[u8]) -> Result<(Vec, Encoding, usize)> { + if !bytes.starts_with(b"ply") { + return Err(malformed(KIND, "missing \"ply\" signature")); + } + let mut pos = 0usize; + let mut enc = None; + let mut elements: Vec = Vec::new(); + loop { + let rest = bytes.get(pos..).unwrap_or_default(); + let Some(nl) = rest.iter().position(|b| *b == b'\n') else { return Err(malformed(KIND, "header has no end_header")) }; + let line = std::str::from_utf8(rest.get(..nl).unwrap_or_default()).map_err(|_| malformed(KIND, "header is not text"))?.trim(); + pos += nl + 1; + let words: Vec<&str> = line.split_whitespace().collect(); + match words.as_slice() { + ["end_header"] => break, + ["format", f, ..] => { + enc = Some(match *f { + "ascii" => Encoding::Ascii, + "binary_little_endian" => Encoding::Le, + "binary_big_endian" => Encoding::Be, + other => return Err(malformed(KIND, format!("unknown format {other}"))), + }) + } + ["element", name, count] => { + let count = count.parse::().map_err(|_| malformed(KIND, format!("bad element count {count}")))?; + elements.push(Element { name: name.to_string(), count, props: Vec::new() }); + } + ["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 (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}")))?; + el.props.push(Property { name: name.to_string(), ty, list: None }); + } + ["ply"] | [] | ["comment", ..] | ["obj_info", ..] => {} + _ => return Err(malformed(KIND, format!("unexpected header line \"{line}\""))), + } + } + let enc = enc.ok_or_else(|| malformed(KIND, "header has no format line"))?; + Ok((elements, enc, pos)) +} diff --git a/crates/formats/src/mesh_import/scene_tests.rs b/crates/formats/src/mesh_import/scene_tests.rs new file mode 100644 index 00000000..593926d2 --- /dev/null +++ b/crates/formats/src/mesh_import/scene_tests.rs @@ -0,0 +1,455 @@ +//! FBX and COLLADA import, built from tiny files written here (binary FBX through a minimal +//! node writer, ASCII FBX from the same tree, COLLADA as text). + +use std::io::Write as _; + +use texturecraft_geom::glam::{Vec2, Vec3}; + +use super::tests::hammer; +use super::*; + +enum P { + I(i32), + L(i64), + D(f64), + S(&'static str), + Doubles(Vec), + Ints(Vec), +} + +struct N { + name: &'static str, + props: Vec

, + children: Vec, +} + +fn n(name: &'static str, props: Vec

, children: Vec) -> N { + N { name, props, children } +} + +/// `P: name, type, subtype, flags, values...` in a Properties70 block. +fn prop(name: &'static str, values: &[f64]) -> N { + let mut props = vec![P::S(name), P::S(name), P::S(""), P::S("A")]; + props.extend(values.iter().map(|v| P::D(*v))); + n("P", props, vec![]) +} + +fn props70(entries: Vec) -> N { + n("Properties70", vec![], entries) +} + +fn layer(name: &'static str, mapping: &'static str, reference: &'static str, data: Vec) -> N { + let mut children = vec![n("MappingInformationType", vec![P::S(mapping)], vec![]), n("ReferenceInformationType", vec![P::S(reference)], vec![])]; + children.extend(data); + n(name, vec![P::I(0)], children) +} + +/// A quad (material slot 0) and a triangle (slot 1) under a parented Model, two materials. +fn scene(z_up: bool) -> Vec { + // The Z-up variant also turns the parent with PreRotation instead of Lcl Rotation. + let rotation = if z_up { "PreRotation" } else { "Lcl Rotation" }; + // (UpAxis, FrontAxis, FrontAxisSign) + let (up, front, front_sign) = if z_up { (2.0, 1.0, -1.0) } else { (1.0, 2.0, 1.0) }; + let verts = vec![0.0, 0.0, 0.0, 100.0, 0.0, 0.0, 100.0, 100.0, 0.0, 0.0, 100.0, 0.0, 200.0, 0.0, 0.0, 300.0, 0.0, 0.0, 200.0, 100.0, 0.0]; + let geometry = n( + "Geometry", + vec![P::L(10), P::S("Quad\u{0}\u{1}Geometry"), P::S("Mesh")], + vec![ + n("Vertices", vec![P::Doubles(verts)], vec![]), + n("PolygonVertexIndex", vec![P::Ints(vec![0, 1, 2, -4, 4, 5, -7])], vec![]), + layer("LayerElementNormal", "ByPolygonVertex", "Direct", vec![n("Normals", vec![P::Doubles([0.0, 0.0, 1.0].repeat(7))], vec![])]), + layer( + "LayerElementUV", + "ByPolygonVertex", + "IndexToDirect", + vec![ + n("UV", vec![P::Doubles(vec![0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0])], vec![]), + n("UVIndex", vec![P::Ints(vec![0, 1, 2, 3, 0, 1, 3])], vec![]), + ], + ), + layer("LayerElementMaterial", "ByPolygon", "IndexToDirect", vec![n("Materials", vec![P::Ints(vec![0, 1])], vec![])]), + ], + ); + let model = |id: i64, name: &'static str, entries: Vec| n("Model", vec![P::L(id), P::S(name), P::S("Mesh")], vec![props70(entries)]); + let material = |id: i64, name: &'static str| n("Material", vec![P::L(id), P::S(name), P::S("")], vec![]); + let c = |child: i64, parent: i64| n("C", vec![P::S("OO"), P::L(child), P::L(parent)], vec![]); + vec![ + n("FBXHeaderExtension", vec![], vec![n("FBXVersion", vec![P::I(7400)], vec![])]), + n( + "GlobalSettings", + vec![], + vec![props70(vec![ + prop("UpAxis", &[up]), + prop("FrontAxis", &[front]), + prop("FrontAxisSign", &[front_sign]), + prop("CoordAxis", &[0.0]), + prop("UnitScaleFactor", &[1.0]), + ])], + ), + n( + "Objects", + vec![], + vec![ + geometry, + model(20, "Parent\u{0}\u{1}Model", vec![prop("Lcl Translation", &[0.0, 0.0, 100.0]), prop(rotation, &[0.0, 0.0, 90.0])]), + model(21, "Child\u{0}\u{1}Model", vec![prop("Lcl Translation", &[100.0, 0.0, 0.0])]), + material(30, "Red\u{0}\u{1}Material"), + material(31, "Blue\u{0}\u{1}Material"), + ], + ), + n("Connections", vec![], vec![c(10, 21), c(21, 20), c(20, 0), c(30, 21), c(31, 21)]), + ] +} + +/// Minimal binary FBX writer: 64-bit node records from version 7500, optional zlib arrays. +struct FbxWriter { + wide: bool, + compress: bool, +} + +impl FbxWriter { + fn uint(&self, out: &mut Vec, v: usize) { + if self.wide { out.extend((v as u64).to_le_bytes()) } else { out.extend((v as u32).to_le_bytes()) } + } + + fn null_record(&self, out: &mut Vec) { + out.extend(vec![0u8; if self.wide { 25 } else { 13 }]); + } + + fn array(&self, out: &mut Vec, ty: u8, count: usize, raw: Vec) { + out.push(ty); + out.extend((count as u32).to_le_bytes()); + let data = if self.compress { + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); + z.write_all(&raw).unwrap(); + z.finish().unwrap() + } else { + raw + }; + out.extend((self.compress as u32).to_le_bytes()); + out.extend((data.len() as u32).to_le_bytes()); + out.extend(data); + } + + fn node(&self, out: &mut Vec, node: &N) { + let mut props = Vec::new(); + for p in &node.props { + match p { + P::I(v) => { + props.push(b'I'); + props.extend(v.to_le_bytes()); + } + P::L(v) => { + props.push(b'L'); + props.extend(v.to_le_bytes()); + } + P::D(v) => { + props.push(b'D'); + props.extend(v.to_le_bytes()); + } + P::S(s) => { + props.push(b'S'); + props.extend((s.len() as u32).to_le_bytes()); + props.extend(s.as_bytes()); + } + P::Doubles(v) => self.array(&mut props, b'd', v.len(), v.iter().flat_map(|x| x.to_le_bytes()).collect()), + P::Ints(v) => self.array(&mut props, b'i', v.len(), v.iter().flat_map(|x| x.to_le_bytes()).collect()), + } + } + let start = out.len(); + self.uint(out, 0); + self.uint(out, node.props.len()); + self.uint(out, props.len()); + out.push(node.name.len() as u8); + out.extend(node.name.as_bytes()); + out.extend(props); + if !node.children.is_empty() { + for c in &node.children { + self.node(out, c); + } + self.null_record(out); + } + let mut end = Vec::new(); + self.uint(&mut end, out.len()); + out[start..start + end.len()].copy_from_slice(&end); + } +} + +fn binary_fbx(nodes: &[N], version: u32, compress: bool) -> Vec { + let w = FbxWriter { wide: version >= 7500, compress }; + let mut out = b"Kaydara FBX Binary \0\x1a\0".to_vec(); + out.extend(version.to_le_bytes()); + for node in nodes { + w.node(&mut out, node); + } + w.null_record(&mut out); + out.extend(b"footer padding"); + out +} + +fn ascii_fbx(nodes: &[N]) -> String { + fn write(s: &mut String, node: &N, indent: usize) { + let num = |v: f64| format!("{v}"); + let values: Vec = node + .props + .iter() + .map(|p| match p { + P::I(v) => v.to_string(), + P::L(v) => v.to_string(), + P::D(v) => num(*v), + P::S(t) => format!("\"{}\"", t.split_once("\u{0}\u{1}").map_or(t.to_string(), |(name, class)| format!("{class}::{name}"))), + P::Doubles(v) => format!("*{} {{\n a: {}\n}}", v.len(), v.iter().map(|x| num(*x)).collect::>().join(",")), + P::Ints(v) => format!("*{} {{\n a: {}\n}}", v.len(), v.iter().map(|x| x.to_string()).collect::>().join(",")), + }) + .collect(); + s.push_str(&format!("{}{}: {}", " ".repeat(indent), node.name, values.join(", "))); + if !node.children.is_empty() { + s.push_str(" {\n"); + for c in &node.children { + write(s, c, indent + 1); + } + s.push_str(&format!("{}}}", " ".repeat(indent))); + } + s.push('\n'); + } + let mut s = "; FBX 7.4.0 project file\n; ----------------------------------------------------\n\n".to_string(); + for node in nodes { + write(&mut s, node, 0); + } + s +} + +/// Bounds of the vertices material `name` uses. +fn material_bounds(m: &Mesh, name: &str) -> (Vec3, Vec3) { + let sm = m.submeshes.iter().find(|s| m.materials[s.material] == name).unwrap(); + let ids = &m.indices[sm.start as usize..(sm.start + sm.count) as usize]; + ids.iter().fold((Vec3::splat(f32::MAX), Vec3::splat(f32::MIN)), |(lo, hi), i| (lo.min(m.positions[*i as usize]), hi.max(m.positions[*i as usize]))) +} + +fn close(a: Vec3, b: Vec3) -> bool { + (a - b).length() < 1e-4 +} + +fn check_fbx_scene(m: &Mesh, z_up: bool) { + assert_eq!(m.materials, vec!["Red".to_string(), "Blue".to_string()]); + assert_eq!(m.triangle_count(), 3); + assert_eq!(m.vertex_count(), 7); + assert!(m.submeshes.iter().all(|s| s.name == "Child"), "parts are named after their model"); + // cm to m, child (1, 0, 0) under a parent rotated 90° about Z and lifted 1 m. + let (lo, hi) = m.bounds(); + let (want_lo, want_hi) = if z_up { (Vec3::new(-1.0, 1.0, -4.0), Vec3::new(0.0, 1.0, -1.0)) } else { (Vec3::new(-1.0, 1.0, 1.0), Vec3::new(0.0, 4.0, 1.0)) }; + assert!(close(lo, want_lo) && close(hi, want_hi), "bounds {lo} {hi}"); + let up = if z_up { Vec3::Y } else { Vec3::Z }; + assert!(m.normals.iter().all(|nrm| close(*nrm, up)), "normals {:?}", m.normals); + let (red_lo, red_hi) = material_bounds(m, "Red"); + assert!(close(red_hi - red_lo, if z_up { Vec3::new(1.0, 0.0, 1.0) } else { Vec3::new(1.0, 1.0, 0.0) })); + // UVs through UVIndex, v flipped to glTF's convention: every corner of the quad is used. + let mut uvs: Vec = m.uvs.clone(); + uvs.sort_by(|a, b| a.x.total_cmp(&b.x).then(a.y.total_cmp(&b.y))); + uvs.dedup(); + assert_eq!(uvs, vec![Vec2::new(0.0, 0.0), Vec2::new(0.0, 1.0), Vec2::new(1.0, 0.0), Vec2::new(1.0, 1.0)]); + // Winding stays outward (front faces toward the normals). + for t in m.indices.chunks(3) { + let p = |i: u32| m.positions[i as usize]; + let face = (p(t[1]) - p(t[0])).cross(p(t[2]) - p(t[0])); + assert!(face.dot(m.normals[t[0] as usize]) > 0.0); + } +} + +#[test] +fn fbx_binary_32_and_64_bit_compressed_or_not() { + let nodes = scene(false); + for (version, compress) in [(7400, false), (7400, true), (7500, false), (7500, true)] { + let bytes = binary_fbx(&nodes, version, compress); + let m = import_mesh_bytes("scene.fbx", &bytes).unwrap_or_else(|e| panic!("{version} {compress}: {e}")).mesh; + check_fbx_scene(&m, false); + } +} + +#[test] +fn fbx_ascii_and_z_up() { + let text = ascii_fbx(&scene(false)); + check_fbx_scene(&import_mesh_bytes("scene.FBX", text.as_bytes()).unwrap().mesh, false); + check_fbx_scene(&import_mesh_bytes("z.fbx", ascii_fbx(&scene(true)).as_bytes()).unwrap().mesh, true); + check_fbx_scene(&import_mesh_bytes("z.fbx", &binary_fbx(&scene(true), 7700, true)).unwrap().mesh, true); +} + +#[test] +fn fbx_old_versions_and_garbage_are_errors() { + let mut old = binary_fbx(&scene(false), 7400, false); + old[23..27].copy_from_slice(&6100u32.to_le_bytes()); + assert!(matches!(import_mesh_bytes("old.fbx", &old), Err(FormatError::Unsupported(_)))); + assert!(import_mesh_bytes("a.fbx", b"; FBX 6.1.0 project file\nObjects: {\n}\n").is_err()); + // An array claiming 2^32-1 doubles never allocates its claim. + let mut huge = binary_fbx(&[n("Objects", vec![P::Doubles(vec![1.0])], vec![])], 7400, false); + let at = huge.windows(5).position(|w| w == [b'd', 1, 0, 0, 0]).unwrap() + 1; + huge[at..at + 4].copy_from_slice(&u32::MAX.to_le_bytes()); + assert!(import_mesh_bytes("h.fbx", &huge).is_err()); +} + +#[test] +fn malformed_fbx_and_dae_never_panic() { + hammer("x.fbx", &binary_fbx(&scene(false), 7400, false)); + hammer("x.fbx", &binary_fbx(&scene(true), 7500, true)); + hammer("x.fbx", ascii_fbx(&scene(false)).as_bytes()); + hammer("x.dae", DAE.as_bytes()); + // 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()] { + let name = if good.starts_with(b" + + Z_UP + + + + + + + + + 0 0 0 2 0 0 2 2 0 0 2 0 + + + + 0 0 1 + + + + 0 0 1 0 1 1 0 1 + + + + + + + + 4 +

0 0 0 1 0 1 2 0 2 3 0 3

+ +
+ + + + + 4 0 0 6 0 0 4 2 0 8 0 0 10 0 0 10 2 0 8 2 0 + + + + 0 0 1 0 0 1 0 0 1 0 1 1 0 1 + + + + + + +

0 1 2

+

3 4 5 6

+

0 1

+
+
+ + + + + 0 0 2 + 0 0 1 90 + + 2 2 2 + + + + + + + + + + + + + + +"##; + +#[test] +fn dae_polylist_z_up_units_and_node_transforms() { + let r = import_mesh_bytes("scene.dae", DAE.as_bytes()).unwrap(); + let m = &r.mesh; + assert_eq!(m.materials, vec!["Red".to_string(), "Blue".to_string()]); + assert_eq!(m.triangle_count(), 2 + 1 + 2); + assert!(!r.report.uvs_generated); + assert!(r.report.warnings.iter().any(|w| w.contains("lines"))); + // Quad: ×2 scale, 90° about Z, lifted 2 (Z up), half-meter units, then Z up → Y up. + let (lo, hi) = material_bounds(m, "Red"); + assert!(close(lo, Vec3::new(-2.0, 1.0, -2.0)) && close(hi, Vec3::new(0.0, 1.0, 0.0)), "{lo} {hi}"); + let (lo, hi) = material_bounds(m, "Blue"); + assert!(close(lo, Vec3::new(2.0, 0.0, -1.0)) && close(hi, Vec3::new(5.0, 0.0, 0.0)), "{lo} {hi}"); + for t in m.indices.chunks(3) { + for i in t { + assert!(close(m.normals[*i as usize], Vec3::Y), "normal {}", m.normals[*i as usize]); + } + } + assert!(m.uvs.iter().all(|uv| uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0)); + // Parts are named after their nodes (ids when unnamed). + let parts: Vec<&str> = m.submeshes.iter().map(|s| s.name.as_str()).collect(); + assert!(parts.contains(&"child") && parts.contains(&"other"), "{parts:?}"); +} + +#[test] +fn glb_export_round_trips_with_textures() { + let mesh = texturecraft_geom::primitives::material_ball(); + let png = |color: image::ColorType| { + let mut out = std::io::Cursor::new(Vec::new()); + let channels = color.channel_count() as usize; + image::write_buffer_with_format(&mut out, &vec![200u8; 4 * channels], 2, 2, color, image::ImageFormat::Png).unwrap(); + out.into_inner() + }; + let black = { + let mut out = std::io::Cursor::new(Vec::new()); + image::write_buffer_with_format(&mut out, &[0u8; 12], 2, 2, image::ColorType::Rgb8, image::ImageFormat::Png).unwrap(); + out.into_inner() + }; + let file = |name: &str, bytes: Vec| crate::ExportedFile { name: name.into(), bytes, width: 2, height: 2, warnings: vec![] }; + let files = vec![ + file("BaseColor.png", png(image::ColorType::Rgba8)), + file("OcclusionRoughnessMetallic.png", png(image::ColorType::Rgb8)), + file("Normal.png", png(image::ColorType::Rgb8)), + file("Emissive.png", black), + ]; + let first = crate::GlbTextures::from_files(files); + assert!(first.emissive.is_none(), "black emissive maps are dropped"); + let bytes = crate::glb_bytes(&mesh, "Ball", &[first]).unwrap(); + let back = import_mesh_bytes("ball.glb", &bytes).unwrap().mesh; + assert_eq!(back.triangle_count(), mesh.triangle_count()); + assert_eq!(back.materials, mesh.materials); + let doc = ::gltf::Gltf::from_slice(&bytes).unwrap(); + assert_eq!(doc.images().count(), 3); + let mat = doc.materials().next().unwrap(); + assert!(mat.pbr_metallic_roughness().base_color_texture().is_some() && mat.normal_texture().is_some() && mat.occlusion_texture().is_some()); + assert_eq!(mat.alpha_mode(), ::gltf::material::AlphaMode::Blend); + // The second material has no textures and is not metallic. + let plain = doc.materials().nth(1).unwrap(); + assert!(plain.pbr_metallic_roughness().base_color_texture().is_none()); + assert_eq!(plain.pbr_metallic_roughness().metallic_factor(), 0.0); + for img in doc.images() { + assert!(matches!(img.source(), ::gltf::image::Source::View { mime_type: "image/png", .. })); + } +} diff --git a/crates/formats/src/mesh_import/stl.rs b/crates/formats/src/mesh_import/stl.rs new file mode 100644 index 00000000..9b39bf2c --- /dev/null +++ b/crates/formats/src/mesh_import/stl.rs @@ -0,0 +1,78 @@ +//! STL (binary and ascii). Facets keep their own corners, so shading is flat; there are no UVs. + +use texturecraft_geom::glam::Vec3; + +use super::{RawMesh, check_triangles}; +use crate::{Result, malformed}; + +const KIND: &str = "STL"; + +pub(crate) fn read(bytes: &[u8]) -> Result { + 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 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.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> { + check_triangles(0, count)?; + let mut out = Vec::with_capacity(count); + for i in 0..count { + let f = |k: usize| -> Result { + 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 { Ok(Vec3::new(f(c * 3)?, f(c * 3 + 1)?, f(c * 3 + 2)?)) }; + out.push([v(0)?, v(1)?, v(2)?]); + } + Ok(out) +} + +fn ascii(bytes: &[u8]) -> Result> { + 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 = 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(); + } + _ => {} + } + } + if out.is_empty() { + return Err(malformed(KIND, "no facets")); + } + Ok(out) +} diff --git a/crates/formats/src/mesh_import/tests.rs b/crates/formats/src/mesh_import/tests.rs new file mode 100644 index 00000000..62cd3f86 --- /dev/null +++ b/crates/formats/src/mesh_import/tests.rs @@ -0,0 +1,415 @@ +use texturecraft_geom::glam::{Vec2, Vec3, Vec4}; + +use super::*; + +/// A GLB with one mesh of two primitives (materials "Red" and an unnamed one), placed under a +/// mirrored parent node (negative determinant) and a translated child. +fn glb(with_normals: bool, with_uvs: bool) -> Vec { + let mut bin = Vec::new(); + for p in [[0.0f32, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]] { + bin.extend(p.iter().flat_map(|v| v.to_le_bytes())); + } + for _ in 0..3 { + bin.extend([0.0f32, 0.0, 1.0].iter().flat_map(|v| v.to_le_bytes())); + } + for uv in [[0.0f32, 1.0], [1.0, 1.0], [0.0, 0.0]] { + bin.extend(uv.iter().flat_map(|v| v.to_le_bytes())); + } + for i in [0u16, 1, 2, 0] { + bin.extend(i.to_le_bytes()); + } + let mut attrs = serde_json::json!({ "POSITION": 0 }); + if with_normals { + attrs["NORMAL"] = 1.into(); + } + if with_uvs { + attrs["TEXCOORD_0"] = 2.into(); + } + let json = serde_json::json!({ + "asset": { "version": "2.0" }, + "scene": 0, + "scenes": [{ "nodes": [0] }], + "nodes": [{ "scale": [-1.0, 1.0, 1.0], "children": [1] }, { "translation": [1.0, 0.0, 0.0], "mesh": 0 }], + "meshes": [{ "primitives": [ + { "attributes": attrs, "indices": 3, "material": 0 }, + { "attributes": attrs, "indices": 3, "material": 1 } + ] }], + "materials": [{ "name": "Red" }, {}], + "buffers": [{ "byteLength": bin.len() }], + "bufferViews": [ + { "buffer": 0, "byteOffset": 0, "byteLength": 36 }, + { "buffer": 0, "byteOffset": 36, "byteLength": 36 }, + { "buffer": 0, "byteOffset": 72, "byteLength": 24 }, + { "buffer": 0, "byteOffset": 96, "byteLength": 6 } + ], + "accessors": [ + { "bufferView": 0, "componentType": 5126, "count": 3, "type": "VEC3", "min": [0.0, 0.0, 0.0], "max": [1.0, 1.0, 0.0] }, + { "bufferView": 1, "componentType": 5126, "count": 3, "type": "VEC3" }, + { "bufferView": 2, "componentType": 5126, "count": 3, "type": "VEC2" }, + { "bufferView": 3, "componentType": 5123, "count": 3, "type": "SCALAR" } + ] + }); + let mut js = serde_json::to_vec(&json).unwrap(); + while !js.len().is_multiple_of(4) { + js.push(b' '); + } + let total = 12 + 8 + js.len() + 8 + bin.len(); + let mut out = Vec::new(); + out.extend(b"glTF"); + out.extend(2u32.to_le_bytes()); + out.extend((total as u32).to_le_bytes()); + out.extend((js.len() as u32).to_le_bytes()); + out.extend(b"JSON"); + out.extend(&js); + out.extend((bin.len() as u32).to_le_bytes()); + out.extend(b"BIN\0"); + out.extend(&bin); + out +} + +fn base64(data: &[u8]) -> String { + const T: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let mut s = String::new(); + for c in data.chunks(3) { + let b = [c[0], *c.get(1).unwrap_or(&0), *c.get(2).unwrap_or(&0)]; + let n = (b[0] as u32) << 16 | (b[1] as u32) << 8 | b[2] as u32; + for k in 0..4 { + s.push(if k <= c.len() { T[(n >> (18 - 6 * k) & 63) as usize] as char } else { '=' }); + } + } + s +} + +/// The same scene as an embedded .gltf (data-URI buffer). +fn embedded_gltf() -> Vec { + let g = glb(true, true); + let jlen = u32::from_le_bytes(g[12..16].try_into().unwrap()) as usize; + let mut json: serde_json::Value = serde_json::from_slice(&g[20..20 + jlen]).unwrap(); + let bin = &g[20 + jlen + 8..]; + json["buffers"][0]["uri"] = format!("data:application/octet-stream;base64,{}", base64(bin)).into(); + serde_json::to_vec(&json).unwrap() +} + +fn assert_outward(m: &Mesh) { + for t in 0..m.triangle_count() { + let [a, b, c] = m.tri(t).unwrap(); + let face = (m.positions[b] - m.positions[a]).cross(m.positions[c] - m.positions[a]); + assert!(face.dot(m.normals[a]) > 0.0, "triangle {t} winding disagrees with its normal"); + } +} + +#[test] +fn glb_two_materials_mirrored_node() { + let im = import_mesh_bytes("scene.glb", &glb(true, true)).unwrap(); + let m = &im.mesh; + assert_eq!(m.materials, vec!["Red".to_string(), "Material_1".to_string()]); + assert_eq!(m.submeshes.len(), 2); + assert_eq!((m.submeshes[1].start, m.submeshes[1].count), (3, 3)); + assert_eq!(im.report.triangles, 2); + assert!(!im.report.uvs_generated); + // Translation, then mirror in x. + assert!(m.positions.contains(&Vec3::new(-1.0, 0.0, 0.0))); + assert!(m.positions.contains(&Vec3::new(-2.0, 0.0, 0.0))); + for n in &m.normals { + assert!((*n - Vec3::Z).length() < 1e-5, "{n}"); + } + assert_outward(m); + assert!(m.uvs.contains(&Vec2::new(0.0, 1.0))); + m.validate().unwrap(); +} + +#[test] +fn gltf_embedded_buffers_and_missing_attributes() { + let im = import_mesh_bytes("scene.gltf", &embedded_gltf()).unwrap(); + assert_eq!(im.mesh.triangle_count(), 2); + // No normals: flat normals with consistent winding; no UVs: automatic unwrap. + let im = import_mesh_bytes("x.glb", &glb(false, false)).unwrap(); + assert!(im.report.uvs_generated); + assert_outward(&im.mesh); + assert!(im.mesh.uvs.iter().all(|uv| (0.0..=1.0).contains(&uv.x) && (0.0..=1.0).contains(&uv.y))); + assert_eq!(im.report.uv_out_of_range, 0); +} + +#[test] +fn gltf_external_buffer_needs_a_path() { + let mut json: serde_json::Value = serde_json::from_slice(&embedded_gltf()).unwrap(); + json["buffers"][0]["uri"] = "scene.bin".into(); + let err = import_mesh_bytes("scene.gltf", &serde_json::to_vec(&json).unwrap()).unwrap_err(); + assert!(matches!(err, FormatError::Unsupported(_)), "{err}"); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn gltf_external_buffer_from_disk() { + let g = glb(true, true); + let jlen = u32::from_le_bytes(g[12..16].try_into().unwrap()) as usize; + let dir = std::env::temp_dir().join(format!("tc-formats-gltf-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("my scene.bin"), &g[20 + jlen + 8..]).unwrap(); + let mut json: serde_json::Value = serde_json::from_slice(&g[20..20 + jlen]).unwrap(); + json["buffers"][0]["uri"] = "my%20scene.bin".into(); + std::fs::write(dir.join("scene.gltf"), serde_json::to_vec(&json).unwrap()).unwrap(); + let im = import_mesh(&dir.join("scene.gltf")).unwrap(); + assert_eq!(im.mesh.triangle_count(), 2); + // A buffer that is a directory is refused before reading. + json["buffers"][0]["uri"] = ".".into(); + std::fs::write(dir.join("bad.gltf"), serde_json::to_vec(&json).unwrap()).unwrap(); + assert!(import_mesh(&dir.join("bad.gltf")).is_err()); + std::fs::remove_dir_all(&dir).unwrap(); +} + +const OBJ: &str = "mtllib missing.mtl +o quad +v 0 0 0 +v 1 0 0 +v 1 1 0 +v 0 1 0 +v 2 0 0 +vt 0 0 +vt 1 0 +vt 1 1 +vt 0 1 +vn 0 0 1 +usemtl Wood +f 1/1/1 2/2/1 3/3/1 +usemtl Metal +f 1/1/1 3/3/1 4/4/1 +usemtl Wood +f 2/2/1 5/2/1 3/3/1 4/4/1 +"; + +#[test] +fn obj_flips_v_and_keeps_usemtl_materials() { + let im = import_mesh_bytes("a.obj", OBJ.as_bytes()).unwrap(); + let m = &im.mesh; + assert_eq!(m.materials, vec!["Wood".to_string(), "Metal".to_string()]); + // Wood: one triangle + a fan-triangulated quad; Metal: one triangle. + assert_eq!(m.submeshes.iter().map(|s| s.count / 3).collect::>(), vec![3, 1]); + for (p, uv) in m.positions.iter().zip(&m.uvs) { + if *p == Vec3::ZERO { + assert_eq!(*uv, Vec2::new(0.0, 1.0), "OBJ v must be flipped"); + } + if *p == Vec3::new(1.0, 1.0, 0.0) { + assert_eq!(*uv, Vec2::new(1.0, 0.0)); + } + } + assert_outward(m); + m.validate().unwrap(); +} + +#[test] +fn obj_without_uvs_or_normals_is_completed() { + let im = import_mesh_bytes("b.OBJ", b"v 0 0 0\nv 1 0 0\nv 0 1 0\nv 0 0 1\nf 1 3 2\nf 1 2 4\nf 1 4 3\nf 2 3 4\n").unwrap(); + assert!(im.report.uvs_generated); + assert_eq!(im.mesh.materials, vec!["Default".to_string()]); + im.mesh.validate().unwrap(); + // Smooth normals over welded positions: every copy of a corner shares one normal. + let n0: Vec = im.mesh.positions.iter().zip(&im.mesh.normals).filter(|(p, _)| **p == Vec3::ZERO).map(|(_, n)| *n).collect(); + assert!(n0.len() > 1 && n0.iter().all(|n| (*n - n0[0]).length() < 1e-5)); +} + +fn ply_ascii() -> String { + "ply\nformat ascii 1.0\ncomment test\nelement vertex 4\nproperty float x\nproperty float y\nproperty float z\nproperty float s\nproperty float t\n\ + element face 1\nproperty list uchar int vertex_indices\nend_header\n\ + 0 0 0 0 0\n1 0 0 1 0\n1 1 0 1 1\n0 1 0 0 1\n4 0 1 2 3\n" + .to_string() +} + +fn ply_binary(big_endian: bool) -> Vec { + let enc = if big_endian { "binary_big_endian" } else { "binary_little_endian" }; + let mut b = format!( + "ply\r\nformat {enc} 1.0\r\nelement vertex 4\r\nproperty double x\r\nproperty double y\r\nproperty double z\r\nproperty float nx\r\nproperty float ny\r\nproperty float nz\r\n\ + property uchar red\r\nelement face 1\r\nproperty list uchar uint vertex_indices\r\nend_header\r\n" + ) + .into_bytes(); + let f64b = |v: f64| if big_endian { v.to_be_bytes().to_vec() } else { v.to_le_bytes().to_vec() }; + let f32b = |v: f32| if big_endian { v.to_be_bytes().to_vec() } else { v.to_le_bytes().to_vec() }; + let u32b = |v: u32| if big_endian { v.to_be_bytes().to_vec() } else { v.to_le_bytes().to_vec() }; + for p in [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0]] { + for v in p { + b.extend(f64b(v)); + } + for v in [0.0, 0.0, 1.0] { + b.extend(f32b(v)); + } + b.push(200); + } + b.push(4); + for i in [0, 1, 2, 3] { + b.extend(u32b(i)); + } + b +} + +#[test] +fn ply_ascii_and_binary() { + let a = import_mesh_bytes("a.ply", ply_ascii().as_bytes()).unwrap(); + assert_eq!(a.mesh.triangle_count(), 2); + assert!(!a.report.uvs_generated); + // PLY t is bottom-up like OBJ. + assert_eq!(a.mesh.uvs[0], Vec2::new(0.0, 1.0)); + for be in [false, true] { + let b = import_mesh_bytes("b.ply", &ply_binary(be)).unwrap(); + assert_eq!(b.mesh.triangle_count(), 2); + assert!(b.report.uvs_generated); + assert!(b.mesh.positions.contains(&Vec3::new(1.0, 1.0, 0.0))); + assert_outward(&b.mesh); + } +} + +fn stl_binary() -> Vec { + let mut b = vec![0u8; 80]; + b.extend(2u32.to_le_bytes()); + for tri in [[[0.0f32, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0]], [[0.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0]]] { + b.extend([0.0f32; 3].iter().flat_map(|v| v.to_le_bytes())); + for v in tri { + b.extend(v.iter().flat_map(|x| x.to_le_bytes())); + } + b.extend([0u8, 0]); + } + b +} + +const STL_ASCII: &str = "solid test +facet normal 0 0 1 + outer loop + vertex 0 0 0 + vertex 1 0 0 + vertex 1 1 0 + endloop +endfacet +facet normal 0 0 1 + outer loop + vertex 0 0 0 + vertex 1 1 0 + vertex 0 1 0 + endloop +endfacet +endsolid test +"; + +#[test] +fn stl_ascii_and_binary() { + for (name, bytes) in [("a.stl", STL_ASCII.as_bytes().to_vec()), ("b.STL", stl_binary())] { + let im = import_mesh_bytes(name, &bytes).unwrap(); + assert_eq!(im.mesh.triangle_count(), 2, "{name}"); + assert!(im.report.uvs_generated); + assert_outward(&im.mesh); + } + // A binary file whose header starts with "solid" is still binary. + let mut b = stl_binary(); + b[..5].copy_from_slice(b"solid"); + assert_eq!(import_mesh_bytes("c.stl", &b).unwrap().mesh.triangle_count(), 2); +} + +/// 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(super) fn hammer(name: &str, good: &[u8]) { + let check = |bytes: &[u8]| { + if std::env::var("TC_TRACE").is_ok() { + std::fs::write(std::env::temp_dir().join("tc_last_case.bin"), bytes).unwrap(); + eprintln!("{name} {}", bytes.len()); + } + if let Err(e) = import_mesh_bytes(name, bytes) { + assert!(!e.to_string().contains("parser failed"), "{name}: a parser panicked on {} bytes", bytes.len()); + } + }; + for len in 0..good.len() { + check(&good[..len]); + } + for i in 0..good.len() { + let mut b = good.to_vec(); + b[i] ^= 0xA5; + check(&b); + b[i] = 0xFF; + check(&b); + } + assert!(import_mesh_bytes(name, &[]).is_err()); + assert!(import_mesh_bytes(name, b"\x00\x01garbage\xff\xfe").is_err()); +} + +#[test] +fn malformed_meshes_are_errors() { + hammer("x.glb", &glb(true, true)); + hammer("x.gltf", &embedded_gltf()); + hammer("x.obj", OBJ.as_bytes()); + hammer("x.ply", ply_ascii().as_bytes()); + hammer("x.ply", &ply_binary(false)); + hammer("x.ply", &ply_binary(true)); + hammer("x.stl", STL_ASCII.as_bytes()); + hammer("x.stl", &stl_binary()); + assert!(matches!(import_mesh_bytes("x.blend", b"whatever"), Err(FormatError::Unsupported(_)))); + // Hostile counts never allocate their claim. + let huge = "ply\nformat binary_little_endian 1.0\nelement vertex 4000000000\nproperty float x\nproperty float y\nproperty float z\nend_header\n"; + assert!(import_mesh_bytes("h.ply", huge.as_bytes()).is_err()); + let mut stl = vec![0u8; 80]; + stl.extend(u32::MAX.to_le_bytes()); + assert!(import_mesh_bytes("h.stl", &stl).is_err()); + // Meshes of only degenerate triangles are refused instead of crashing tangent generation. + assert!(import_mesh_bytes("p.obj", b"v 1 1 1\nv 1 1 1\nv 1 1 1\nf 1 2 3\n").is_err()); + assert!(import_mesh_bytes("q.obj", b"v 0 0 0\nv 0 0 0\nv 0 1 0\nf 1 2 3\nf 2 1 3\n").is_err()); +} + +#[test] +fn report_counts_uv_problems() { + let obj = "v 0 0 0\nv 1 0 0\nv 0 1 0\nv 1 1 0\nvt 0 0\nvt 2 0\nvt 0 1\nvt 0 0\nf 1/1 2/2 3/3\nf 2/4 4/4 3/4\n"; + let r = import_mesh_bytes("r.obj", obj.as_bytes()).unwrap().report; + assert_eq!(r.uv_out_of_range, 1); + assert_eq!(r.zero_area_uv_triangles, 1); + assert!(!r.warnings.is_empty()); +} + +#[test] +fn gltf_vertex_colors_and_node_names() { + // The embedded scene plus a COLOR_0 (RGBA float) on the first primitive and a named node. + let mut json: serde_json::Value = serde_json::from_slice(&embedded_gltf()).unwrap(); + let colors: Vec = [[1.0f32, 0.0, 0.0, 0.5]; 3].iter().flatten().flat_map(|v| v.to_le_bytes()).collect(); + json["buffers"] + .as_array_mut() + .unwrap() + .push(serde_json::json!({ "byteLength": 48, "uri": format!("data:application/octet-stream;base64,{}", base64(&colors)) })); + json["bufferViews"].as_array_mut().unwrap().push(serde_json::json!({ "buffer": 1, "byteOffset": 0, "byteLength": 48 })); + json["accessors"].as_array_mut().unwrap().push(serde_json::json!({ "bufferView": 4, "componentType": 5126, "count": 3, "type": "VEC4" })); + json["meshes"][0]["primitives"][0]["attributes"]["COLOR_0"] = 4.into(); + json["nodes"][1]["name"] = "Body_low".into(); + let m = import_mesh_bytes("scene.gltf", &serde_json::to_vec(&json).unwrap()).unwrap().mesh; + assert_eq!(m.colors.len(), m.vertex_count()); + let red = Vec4::new(1.0, 0.0, 0.0, 0.5); + // The colored primitive's vertices are red, the other primitive's white. + let first = &m.indices[m.submeshes[0].start as usize..][..3]; + assert!(first.iter().all(|i| m.colors[*i as usize] == red), "{:?}", m.colors); + assert!(m.colors.contains(&Vec4::ONE)); + assert!(m.submeshes.iter().all(|s| s.name == "Body_low")); + // Unnamed parts take the file stem; a mesh without COLOR_0 has no colors. + let plain = import_mesh_bytes("scene.glb", &glb(true, true)).unwrap().mesh; + assert!(plain.submeshes.iter().all(|s| s.name == "scene") && plain.colors.is_empty()); +} + +#[test] +fn obj_and_ply_vertex_colors_and_objects() { + let obj = "o Wheel_low\nv 0 0 0 1 0 0\nv 1 0 0 1 0 0\nv 0 1 0 1 0 0\nf 1 2 3\no Body_low\nv 0 0 1 0 1 0\nv 1 0 1 0 1 0\nv 0 1 1 0 1 0\nf 4 5 6\n"; + let m = import_mesh_bytes("car.obj", obj.as_bytes()).unwrap().mesh; + assert_eq!(m.submeshes.iter().map(|s| s.name.as_str()).collect::>(), ["Wheel_low", "Body_low"]); + assert_eq!(m.materials.len(), 1, "one material, two parts"); + assert_eq!(m.colors.len(), m.vertex_count()); + for (p, c) in m.positions.iter().zip(&m.colors) { + assert_eq!(*c, if p.z == 0.0 { Vec4::new(1.0, 0.0, 0.0, 1.0) } else { Vec4::new(0.0, 1.0, 0.0, 1.0) }); + } + let ply = "ply\nformat ascii 1.0\nelement vertex 3\nproperty float x\nproperty float y\nproperty float z\nproperty uchar red\nproperty uchar green\nproperty uchar blue\n\ + element face 1\nproperty list uchar int vertex_indices\nend_header\n0 0 0 255 0 51\n1 0 0 255 0 51\n0 1 0 255 0 51\n3 0 1 2\n"; + let m = import_mesh_bytes("rock_high.ply", ply.as_bytes()).unwrap().mesh; + assert!(m.colors.iter().all(|c| (*c - Vec4::new(1.0, 0.0, 0.2, 1.0)).length() < 1e-6), "{:?}", m.colors); + assert_eq!(m.submeshes[0].name, "rock_high"); +} + +#[test] +fn high_poly_import_skips_unwrap_and_rejects_garbage() { + let m = import_high_poly_bytes("x.glb", &glb(false, false)).unwrap(); + m.validate().unwrap(); + assert!(m.uvs.iter().all(|uv| *uv == Vec2::ZERO), "no automatic unwrap"); + assert_eq!(m.tangents.len(), m.vertex_count()); + assert!(import_high_poly_bytes("x.glb", b"glTF\x02\0\0\0garbage").is_err()); + assert!(import_high_poly_bytes("x.obj", b"v 0 0 0\nv nan 1 0\nv 0 1 0\nf 1 2 3\n").is_err()); + assert!(import_high_poly_bytes("x.ply", b"ply\nformat ascii 1.0\nelement vertex 99999999\n").is_err()); + assert!(import_high_poly_bytes("x.txt", b"").is_err()); +} diff --git a/crates/formats/src/presets.rs b/crates/formats/src/presets.rs new file mode 100644 index 00000000..dfaba47d --- /dev/null +++ b/crates/formats/src/presets.rs @@ -0,0 +1,170 @@ +//! Built-in export presets and user preset JSON. Preset names are ours; their contents follow +//! each engine's public material conventions. + +use texturecraft_core::{BakedMap, Channel, ChannelKind}; + +use crate::export::{Dest, ExportPreset, FileFormat, MapSpec, SourceRef, VirtualMap}; +use crate::{FormatError, Result}; + +/// Largest preset JSON accepted. +const MAX_PRESET_JSON: usize = 1 << 20; + +fn ch(channel: Channel, component: u8) -> SourceRef { + SourceRef::Channel { channel, component } +} + +fn virt(map: VirtualMap, component: u8) -> SourceRef { + SourceRef::Virtual { map, component } +} + +/// `$mesh_$textureSet_` PNG. +fn map(suffix: &str, bit_depth: u8, channels: Vec<(Dest, SourceRef)>) -> MapSpec { + MapSpec { file: format!("$mesh_$textureSet_{suffix}"), format: FileFormat::Png, bit_depth, channels } +} + +fn rgb(f: impl Fn(u8) -> SourceRef) -> Vec<(Dest, SourceRef)> { + vec![(Dest::R, f(0)), (Dest::G, f(1)), (Dest::B, f(2))] +} + +fn gray(s: SourceRef) -> Vec<(Dest, SourceRef)> { + vec![(Dest::L, s)] +} + +/// Base color RGB with opacity in alpha (alpha is dropped when the set has no opacity). +fn base_with_alpha() -> Vec<(Dest, SourceRef)> { + let mut c = rgb(|k| ch(Channel::BaseColor, k)); + c.push((Dest::A, ch(Channel::Opacity, 0))); + c +} + +fn normal(map_kind: VirtualMap) -> Vec<(Dest, SourceRef)> { + rgb(|k| virt(map_kind, k)) +} + +/// One file per channel: RGB for colors and normals, grayscale (16-bit for height) otherwise. +fn channel_map(c: Channel) -> MapSpec { + let (channels, depth) = match c.kind() { + ChannelKind::Color | ChannelKind::NormalMap => (rgb(|k| ch(c, k)), 8), + ChannelKind::Signed => (gray(ch(c, 0)), 16), + ChannelKind::Scalar => (gray(ch(c, 0)), 8), + }; + map("$channel", depth, channels) +} + +fn orm() -> Vec<(Dest, SourceRef)> { + vec![(Dest::R, virt(VirtualMap::AoMixed, 0)), (Dest::G, ch(Channel::Roughness, 0)), (Dest::B, ch(Channel::Metallic, 0))] +} + +/// File stems of the glTF metallic-roughness maps (also the GLB export's texture slots). +pub(crate) const GLTF_BASE_COLOR: &str = "BaseColor"; +pub(crate) const GLTF_ORM: &str = "OcclusionRoughnessMetallic"; +pub(crate) const GLTF_NORMAL: &str = "Normal"; +pub(crate) const GLTF_EMISSIVE: &str = "Emissive"; + +/// glTF metallic-roughness maps (8-bit PNG); `file` turns a stem above into a file template. +pub(crate) fn gltf_pbr_maps(file: impl Fn(&str) -> String) -> Vec { + let spec = |stem: &str, channels| MapSpec { file: file(stem), format: FileFormat::Png, bit_depth: 8, channels }; + vec![ + spec(GLTF_BASE_COLOR, base_with_alpha()), + spec(GLTF_ORM, orm()), + spec(GLTF_NORMAL, normal(VirtualMap::NormalOpenGl)), + spec(GLTF_EMISSIVE, rgb(|k| ch(Channel::Emissive, k))), + ] +} + +/// Every built-in preset, in menu order. +pub fn builtin_presets() -> Vec { + let preset = |name: &str, maps: Vec| ExportPreset { name: name.into(), maps }; + vec![ + preset("Document channels + Normal + AO", { + let mut maps: Vec = + Channel::ALL.into_iter().filter(|c| !matches!(c, Channel::Normal | Channel::AmbientOcclusion)).map(channel_map).collect(); + maps.push(map("normal", 8, rgb(|k| ch(Channel::Normal, k)))); + maps.push(map("ambientOcclusion", 8, gray(virt(VirtualMap::AoMixed, 0)))); + maps + }), + preset("glTF PBR Metal Roughness", gltf_pbr_maps(|s| format!("$mesh_$textureSet_{s}"))), + preset( + "Unreal Engine (Packed)", + vec![map("BaseColor", 8, base_with_alpha()), map("OcclusionRoughnessMetallic", 8, orm()), map("Normal", 8, normal(VirtualMap::NormalDirectX))], + ), + preset( + "Unity HDRP (Mask Map)", + vec![ + map("BaseMap", 8, base_with_alpha()), + map( + "MaskMap", + 8, + vec![ + (Dest::R, ch(Channel::Metallic, 0)), + (Dest::G, virt(VirtualMap::AoMixed, 0)), + // Detail mask: none. + (Dest::B, SourceRef::Constant(0.0)), + (Dest::A, virt(VirtualMap::Glossiness, 0)), + ], + ), + map("Normal", 8, normal(VirtualMap::NormalOpenGl)), + ], + ), + preset( + "Unity Standard (Metallic Smoothness)", + vec![ + map("Albedo", 8, base_with_alpha()), + map( + "MetallicSmoothness", + 8, + vec![ + (Dest::R, ch(Channel::Metallic, 0)), + (Dest::G, SourceRef::Constant(0.0)), + (Dest::B, SourceRef::Constant(0.0)), + (Dest::A, virt(VirtualMap::Glossiness, 0)), + ], + ), + map("Normal", 8, normal(VirtualMap::NormalOpenGl)), + map("Occlusion", 8, gray(virt(VirtualMap::AoMixed, 0))), + ], + ), + preset( + "Blender (Principled BSDF)", + vec![ + map("BaseColor", 8, base_with_alpha()), + map("Metallic", 8, gray(ch(Channel::Metallic, 0))), + map("Roughness", 8, gray(ch(Channel::Roughness, 0))), + map("Normal", 8, normal(VirtualMap::NormalOpenGl)), + map("Height", 16, gray(ch(Channel::Height, 0))), + map("Emission", 8, rgb(|k| ch(Channel::Emissive, k))), + map("Specular", 8, gray(ch(Channel::SpecularLevel, 0))), + ], + ), + preset("Separate maps", Channel::ALL.into_iter().map(channel_map).collect()), + preset( + "Mesh maps", + BakedMap::ALL + .into_iter() + .map(|m| { + let src = |component| SourceRef::Baked { map: m, component }; + map("$channel", 16, if m.components() == 3 { rgb(src) } else { gray(src(0)) }) + }) + .collect(), + ), + ] +} + +/// A built-in preset by name (case-insensitive). +pub fn builtin_preset(name: &str) -> Option { + builtin_presets().into_iter().find(|p| p.name.eq_ignore_ascii_case(name.trim())) +} + +/// Parse and validate a user preset. +pub fn preset_from_json(json: &str) -> Result { + if json.len() > MAX_PRESET_JSON { + return Err(FormatError::TooLarge("export preset JSON over 1 MiB".into())); + } + let p: ExportPreset = serde_json::from_str(json).map_err(|e| crate::malformed("export preset", e))?; + p.validate()?; + Ok(p) +} + +pub fn preset_to_json(preset: &ExportPreset) -> Result { + serde_json::to_string_pretty(preset).map_err(|e| FormatError::Invalid(e.to_string())) +} diff --git a/crates/formats/src/project_file/mod.rs b/crates/formats/src/project_file/mod.rs new file mode 100644 index 00000000..f05e07d4 --- /dev/null +++ b/crates/formats/src/project_file/mod.rs @@ -0,0 +1,387 @@ +//! The `.tcraft` project file: a deflate zip holding +//! +//! | entry | content | +//! |---|---| +//! | `project.json` | serde JSON of [`Project`] | +//! | `mesh.` | the original imported mesh file (absent for built-in meshes, `Project.mesh = "builtin:"`) | +//! | `highpoly/` | a high-definition mesh file for baking, in list order | +//! | `cage/` | the baking cage mesh file | +//! | `pixels/layer__.bin` | painted pixels of a paint layer (or content paint effect) channel | +//! | `pixels/mask_.bin` | painted pixels of a mask paint effect | +//! | `baked/_.bin` | a baked mesh map of texture set `` | +//! | `thumbnail.png` | optional preview supplied by the caller | +//! +//! Pixel `.bin` files are sparse and lossless: a header (magic `TCTX`, version, width, height, +//! components, tile count; u32 LE each) followed, for every allocated 64×64 tile, by `tx`, `ty` +//! (u32 LE) and the tile's raw `u16` LE components. Baked maps use the same header with a tile +//! count of 0, followed by `width × height × components` f32 LE. + +use std::collections::BTreeMap; +use std::io::{Cursor, Read, Seek, Write}; +use std::sync::Arc; + +use texturecraft_compose::{BakedMaps, PixelStore}; +use texturecraft_core::{BakedMap, Channel, FORMAT_VERSION, LayerId, Project}; +use texturecraft_texel::{TILE_AREA, TileImage}; +use zip::write::SimpleFileOptions; +use zip::{CompressionMethod, ZipArchive, ZipWriter}; + +use crate::{FormatError, MAX_IMAGE_SIZE, Result, malformed}; + +const KIND: &str = "project file"; +const MAGIC: &[u8; 4] = b"TCTX"; +/// Version of the `.bin` layout. +const BIN_VERSION: u32 = 1; +const MAX_JSON: u64 = 256 << 20; +const MAX_THUMBNAIL: u64 = 64 << 20; +const MAX_RESOURCE: u64 = 512 << 20; +const MAX_ENTRIES: usize = 100_000; +/// Total decompressed bytes accepted from one project file (zip-bomb guard). +const MAX_TOTAL: u64 = 8 << 30; + +/// Everything a project file holds. +#[derive(Clone, Debug)] +pub struct ProjectFile { + pub project: Project, + pub pixels: PixelStore, + /// Baked mesh maps per texture set index. + pub baked: BTreeMap, + /// Original mesh file: (extension, bytes). + pub mesh: Option<(String, Vec)>, + /// High-definition mesh files for baking: (file name, bytes). Names are + /// [`sanitize_resource_name`]d on save. + pub high_poly: Vec<(String, Vec)>, + /// Baking cage mesh file: (file name, bytes). + pub cage: Option<(String, Vec)>, + pub thumbnail: Option>, + /// Imported resources (images used by fills, alphas, stencils), by name: original bytes. + pub resources: BTreeMap>, +} + +/// A resource name safe as a zip entry and a file name: `[A-Za-z0-9_.-]`, at most 120 chars. +pub fn sanitize_resource_name(name: &str) -> String { + let s: String = name.chars().map(|c| if c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '-') { c } else { '_' }).take(120).collect(); + let s = s.trim_start_matches('.').to_string(); + if s.is_empty() { "resource".into() } else { s } +} + +// --------------------------------------------------------------------------------------------- +// Save. + +pub fn save_project_bytes(pf: &ProjectFile) -> Result> { + let mut out = Cursor::new(Vec::new()); + write_project(&mut out, pf)?; + Ok(out.into_inner()) +} + +/// Save atomically: `.tmp` first, then rename over `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)?; + Ok(()) + })(); + if res.is_err() { + let _ = std::fs::remove_file(&tmp); + } + res +} + +fn zip_err(e: zip::result::ZipError) -> FormatError { + match e { + zip::result::ZipError::Io(e) => FormatError::Io(e), + e => malformed(KIND, e), + } +} + +fn header(w: u32, h: u32, comps: usize, count: usize) -> Vec { + let mut b = MAGIC.to_vec(); + for v in [BIN_VERSION, w, h, comps as u32, count as u32] { + b.extend_from_slice(&v.to_le_bytes()); + } + 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)?; + } + Ok(()) +} + +/// Keep `[a-z0-9]`, at most 8 characters (the mesh entry's extension). +fn sanitize_ext(ext: &str) -> String { + let e: String = ext.trim_start_matches('.').chars().filter(char::is_ascii_alphanumeric).take(8).collect::().to_ascii_lowercase(); + if e.is_empty() { "bin".into() } else { e } +} + +fn write_project(out: W, pf: &ProjectFile) -> Result<()> { + let mut zip = ZipWriter::new(out); + let deflate = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated).large_file(true); + let stored = SimpleFileOptions::default().compression_method(CompressionMethod::Stored); + let json = serde_json::to_vec_pretty(&pf.project).map_err(|e| FormatError::Invalid(e.to_string()))?; + zip.start_file("project.json", deflate).map_err(zip_err)?; + zip.write_all(&json)?; + if let Some((ext, bytes)) = &pf.mesh { + zip.start_file(format!("mesh.{}", sanitize_ext(ext)), deflate).map_err(zip_err)?; + zip.write_all(bytes)?; + } + for (dir, (name, bytes)) in pf.high_poly.iter().map(|m| ("highpoly", m)).chain(pf.cage.iter().map(|m| ("cage", m))) { + zip.start_file(format!("{dir}/{}", sanitize_resource_name(name)), deflate).map_err(zip_err)?; + zip.write_all(bytes)?; + } + let mut layers: Vec<_> = pf.pixels.layers.iter().collect(); + layers.sort_by_key(|(k, _)| **k); + for ((id, c), img) in layers { + zip.start_file(format!("pixels/layer_{}_{}.bin", id.0, c.id()), deflate).map_err(zip_err)?; + write_tiles(&mut zip, img)?; + } + let mut masks: Vec<_> = pf.pixels.masks.iter().collect(); + masks.sort_by_key(|(k, _)| **k); + for (id, img) in masks { + zip.start_file(format!("pixels/mask_{}.bin", id.0), deflate).map_err(zip_err)?; + write_tiles(&mut zip, img)?; + } + for (ts, maps) in &pf.baked { + let r = maps.resolution; + for (m, data) in &maps.maps { + // A map whose size disagrees with its resolution cannot be loaded; it can be re-baked. + if data.len() != (r as usize) * (r as usize) * m.components() { + continue; + } + zip.start_file(format!("baked/{ts}_{}.bin", m.id()), deflate).map_err(zip_err)?; + zip.write_all(&header(r, r, m.components(), 0))?; + let bytes: Vec = data.iter().flat_map(|v| v.to_le_bytes()).collect(); + zip.write_all(&bytes)?; + } + } + if let Some(png) = &pf.thumbnail { + zip.start_file("thumbnail.png", stored).map_err(zip_err)?; + zip.write_all(png)?; + } + for (name, bytes) in &pf.resources { + zip.start_file(format!("resources/{}", sanitize_resource_name(name)), stored).map_err(zip_err)?; + zip.write_all(bytes)?; + } + zip.finish().map_err(zip_err)?; + Ok(()) +} + +// --------------------------------------------------------------------------------------------- +// Load. + +pub fn load_project_bytes(bytes: &[u8]) -> Result { + crate::guard(KIND, || load(Cursor::new(bytes))) +} + +#[cfg(not(target_arch = "wasm32"))] +pub fn load_project(path: &std::path::Path) -> Result { + let file = std::fs::File::open(path)?; + crate::guard(KIND, || load(std::io::BufReader::new(file))) +} + +/// Counts decompressed bytes against [`MAX_TOTAL`]. +struct Budget(u64); + +impl Budget { + fn take(&mut self, n: u64) -> Result<()> { + self.0 = self.0.saturating_add(n); + if self.0 > MAX_TOTAL { + return Err(FormatError::TooLarge(format!("project file decompresses to more than {MAX_TOTAL} bytes"))); + } + Ok(()) + } +} + +fn read_all(r: &mut impl Read, cap: u64, budget: &mut Budget, what: &str) -> Result> { + let mut out = Vec::new(); + r.take(cap.saturating_add(1)).read_to_end(&mut out).map_err(|e| malformed(KIND, format!("{what}: {e}")))?; + if out.len() as u64 > cap { + return Err(FormatError::TooLarge(format!("{what} is larger than {cap} bytes"))); + } + budget.take(out.len() as u64)?; + Ok(out) +} + +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; 24]; + 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}"))), + } +} + +fn read_tiles(r: &mut impl Read, res: u32, comps: usize, budget: &mut Budget, what: &str) -> Result { + let (w, h, c, count) = read_header(r, what)?; + if w != res || h != res || c != comps { + return Err(malformed(KIND, format!("{what}: {w}×{h}×{c} does not match the texture set ({res}×{res}×{comps})"))); + } + let mut img = TileImage::new(res, res, comps); + if count > (img.tiles_x() as usize) * (img.tiles_y() as usize) { + return Err(malformed(KIND, format!("{what}: {count} tiles is more than the image holds"))); + } + let mut buf = vec![0u8; TILE_AREA * comps * 2]; + let mut pos = [0u8; 8]; + for _ in 0..count { + read_exact(r, &mut pos, what)?; + let (tx, ty) = (u32_at(&pos, 0), u32_at(&pos, 1)); + if tx >= img.tiles_x() || ty >= img.tiles_y() || img.is_allocated(tx, ty) { + return Err(malformed(KIND, format!("{what}: bad or repeated tile ({tx}, {ty})"))); + } + read_exact(r, &mut buf, what)?; + budget.take(buf.len() as u64)?; + let data: Vec = buf.chunks_exact(2).map(|b| u16::from_le_bytes([b[0], b[1]])).collect(); + img.set_tile(tx, ty, Some(Arc::new(data))); + } + expect_end(r, what)?; + Ok(img) +} + +fn read_baked(r: &mut impl Read, comps: usize, budget: &mut Budget, what: &str) -> Result<(u32, Vec)> { + let (w, h, c, _) = read_header(r, what)?; + if w != h || w == 0 || w > MAX_IMAGE_SIZE || c != comps { + return Err(malformed(KIND, format!("{what}: bad size {w}×{h}×{c}"))); + } + let len = (w as usize) * (h as usize) * comps; + budget.take(len as u64 * 4)?; + let mut bytes = vec![0u8; len * 4]; + read_exact(r, &mut bytes, what)?; + expect_end(r, what)?; + Ok((w, bytes.chunks_exact(4).map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])).collect())) +} + +/// Texture set holding a paint layer, a content effect or a mask effect id. +fn texture_set_of(project: &Project, id: LayerId) -> Option { + let owner = if project.layers.contains_key(&id) { id } else { project.effect(id)?.0 }; + project.locate(owner).map(|(ts, _, _)| ts) +} + +fn load(reader: R) -> Result { + let mut zip = ZipArchive::new(reader).map_err(zip_err)?; + if zip.len() > MAX_ENTRIES { + return Err(FormatError::TooLarge(format!("project file has more than {MAX_ENTRIES} entries"))); + } + let mut budget = Budget(0); + 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 project: Project = serde_json::from_slice(&json).map_err(|e| malformed(KIND, format!("project.json: {e}")))?; + project.validate()?; + if let Some(ts) = project.texture_sets.iter().find(|t| t.resolution == 0 || t.resolution > MAX_IMAGE_SIZE) { + return Err(malformed(KIND, format!("texture set \"{}\" has resolution {}", ts.name, ts.resolution))); + } + + let mut pf = ProjectFile { + pixels: PixelStore::default(), + baked: BTreeMap::new(), + mesh: None, + high_poly: Vec::new(), + cage: None, + thumbnail: None, + resources: BTreeMap::new(), + project, + }; + for i in 0..zip.len() { + let mut f = zip.by_index(i).map_err(zip_err)?; + let name = f.name().to_string(); + let res_of = |id: LayerId| texture_set_of(&pf.project, id).and_then(|ts| pf.project.texture_sets.get(ts)).map(|t| t.resolution); + if name == "thumbnail.png" { + pf.thumbnail = Some(read_all(&mut f, MAX_THUMBNAIL, &mut budget, &name)?); + } else if let Some(res_name) = name.strip_prefix("resources/") { + if res_name.is_empty() || sanitize_resource_name(res_name) != res_name { + return Err(malformed(KIND, format!("bad resource name {name}"))); + } + let bytes = read_all(&mut f, MAX_RESOURCE, &mut budget, &name)?; + pf.resources.insert(res_name.to_string(), bytes); + } else if let Some((dir, file)) = name.split_once('/').filter(|(d, _)| *d == "highpoly" || *d == "cage") { + if file.is_empty() || sanitize_resource_name(file) != file { + return Err(malformed(KIND, format!("bad mesh name {name}"))); + } + let entry = (file.to_string(), read_all(&mut f, crate::mesh_import::MAX_MESH_FILE_BYTES, &mut budget, &name)?); + match dir { + "cage" => pf.cage = Some(entry), + _ => pf.high_poly.push(entry), + } + } else if let Some(ext) = name.strip_prefix("mesh.").filter(|e| !e.contains('/')) { + let bytes = read_all(&mut f, crate::mesh_import::MAX_MESH_FILE_BYTES, &mut budget, &name)?; + pf.mesh = Some((ext.to_string(), bytes)); + } else if let Some(rest) = name.strip_prefix("pixels/layer_").and_then(|r| r.strip_suffix(".bin")) { + let (id, chan) = rest.split_once('_').ok_or_else(|| malformed(KIND, format!("bad entry name {name}")))?; + let id = LayerId(id.parse().map_err(|_| malformed(KIND, format!("bad entry name {name}")))?); + let c = Channel::ALL.into_iter().find(|c| c.id() == chan).ok_or_else(|| malformed(KIND, format!("unknown channel in {name}")))?; + // Pixels of layers that are no longer in any stack are dropped. + let Some(res) = res_of(id) else { continue }; + let img = read_tiles(&mut f, res, c.components() + 1, &mut budget, &name)?; + pf.pixels.layers.insert((id, c), img); + } else if let Some(rest) = name.strip_prefix("pixels/mask_").and_then(|r| r.strip_suffix(".bin")) { + let id = LayerId(rest.parse().map_err(|_| malformed(KIND, format!("bad entry name {name}")))?); + let Some(res) = res_of(id) else { continue }; + let img = read_tiles(&mut f, res, 2, &mut budget, &name)?; + pf.pixels.masks.insert(id, img); + } else if let Some(rest) = name.strip_prefix("baked/").and_then(|r| r.strip_suffix(".bin")) { + let (ts, map) = rest.split_once('_').ok_or_else(|| malformed(KIND, format!("bad entry name {name}")))?; + let ts: usize = ts.parse().map_err(|_| malformed(KIND, format!("bad entry name {name}")))?; + let m = BakedMap::ALL.into_iter().find(|m| m.id() == map).ok_or_else(|| malformed(KIND, format!("unknown baked map in {name}")))?; + if ts >= pf.project.texture_sets.len() { + continue; + } + let (res, data) = read_baked(&mut f, m.components(), &mut budget, &name)?; + let maps = pf.baked.entry(ts).or_default(); + if maps.maps.is_empty() { + maps.resolution = res; + } else if maps.resolution != res { + return Err(malformed(KIND, format!("{name}: baked maps of one texture set differ in size"))); + } + maps.maps.insert(m, Arc::new(data)); + } + // Other entries (from newer minor revisions) are ignored. + } + Ok(pf) +} + +#[cfg(test)] +mod tests; diff --git a/crates/formats/src/project_file/tests.rs b/crates/formats/src/project_file/tests.rs new file mode 100644 index 00000000..aa76eea8 --- /dev/null +++ b/crates/formats/src/project_file/tests.rs @@ -0,0 +1,168 @@ +use std::sync::Arc; + +use texturecraft_core::{Effect, EffectKind, Fill, Layer, LayerKind, Mask, MaskBackground, TextureSet}; + +use super::*; + +struct Sample { + pf: ProjectFile, + paint: LayerId, +} + +fn sample() -> Sample { + let mut p = Project::new("Demo", "mesh.glb"); + p.texture_sets.push(TextureSet::new("A", 0, 128)); + p.texture_sets.push(TextureSet::new("B", 1, 64)); + let layer = Layer::new(p.alloc_id(), "Paint", LayerKind::Paint, &Channel::DEFAULT_SET); + let paint = p.insert_layer(0, layer, None, false).unwrap(); + let mut masked = Layer::new(p.alloc_id(), "Masked", LayerKind::Fill(Fill::default()), &Channel::DEFAULT_SET); + let effect = p.alloc_id(); + masked.mask = Some(Mask { effects: vec![Effect::new(effect, EffectKind::Paint)], ..Mask::new(MaskBackground::Black) }); + p.insert_layer(1, masked, None, false).unwrap(); + let mut pixels = PixelStore::default(); + let img = pixels.layer_mut(paint, Channel::BaseColor, 128); + img.set(0, 0, &[1, 2, 3, 4]); + img.set(127, 127, &[65535, 0, 9, 65535]); + pixels.layer_mut(paint, Channel::Height, 128).set(70, 3, &[40000, 65535]); + pixels.mask_mut(effect, 64).set(10, 60, &[30000, 65535]); + let ao: Vec = (0..256).map(|i| i as f32 / 255.0).collect(); + let baked = BakedMaps { resolution: 16, maps: [(BakedMap::AmbientOcclusion, Arc::new(ao)), (BakedMap::Normal, Arc::new(vec![0.5; 16 * 16 * 3]))].into() }; + let pf = ProjectFile { + project: p, + pixels, + baked: [(0, baked)].into(), + mesh: Some(("glb".into(), vec![1, 2, 3, 4, 5])), + high_poly: vec![("b_high.obj".into(), vec![7; 9]), ("a_high.ply".into(), vec![6])], + cage: Some(("cage.obj".into(), vec![5, 5])), + thumbnail: Some(vec![137, 80, 78, 71]), + resources: [("bricks.png".to_string(), vec![9, 8, 7])].into(), + }; + Sample { pf, paint } +} + +fn assert_same(a: &ProjectFile, b: &ProjectFile) { + assert_eq!(a.project, b.project); + assert_eq!(a.pixels.layers.len(), b.pixels.layers.len()); + for (k, img) in &a.pixels.layers { + assert_eq!(Some(img), b.pixels.layers.get(k), "{k:?}"); + } + assert_eq!(a.pixels.masks.len(), b.pixels.masks.len()); + for (k, img) in &a.pixels.masks { + assert_eq!(Some(img), b.pixels.masks.get(k), "{k:?}"); + } + assert_eq!(a.baked, b.baked); + assert_eq!(a.mesh, b.mesh); + assert_eq!((&a.high_poly, &a.cage), (&b.high_poly, &b.cage), "bake meshes keep their order"); + assert_eq!(a.thumbnail, b.thumbnail); + assert_eq!(a.resources, b.resources); +} + +fn is_guard_error(e: &FormatError) -> bool { + e.to_string().contains("parser failed") +} + +#[test] +fn round_trip_is_identical_and_deterministic() { + let s = sample(); + let bytes = save_project_bytes(&s.pf).unwrap(); + let back = load_project_bytes(&bytes).unwrap(); + assert_same(&s.pf, &back); + assert_eq!(back.pixels.layer(s.paint, Channel::BaseColor).unwrap().get(127, 127), [65535, 0, 9, 65535]); + assert_eq!(save_project_bytes(&back).unwrap(), bytes); + // Built-in mesh projects carry no mesh entry. + let mut builtin = s.pf.clone(); + builtin.mesh = None; + builtin.thumbnail = None; + builtin.project.mesh = "builtin:sphere".into(); + assert_same(&builtin, &load_project_bytes(&save_project_bytes(&builtin).unwrap()).unwrap()); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn save_and_load_through_files() { + let s = sample(); + let dir = std::env::temp_dir().join(format!("tc-formats-project-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("demo.tcraft"); + save_project(&path, &s.pf).unwrap(); + save_project(&path, &s.pf).unwrap(); // overwrite in place + assert!(!dir.join("demo.tcraft.tmp").exists()); + assert_same(&s.pf, &load_project(&path).unwrap()); + std::fs::remove_dir_all(&dir).unwrap(); +} + +#[test] +fn newer_format_is_refused() { + let mut s = sample(); + s.pf.project.format_version = FORMAT_VERSION + 1; + let err = load_project_bytes(&save_project_bytes(&s.pf).unwrap()).unwrap_err(); + assert!(matches!(err, FormatError::NewerVersion { found, .. } if found == FORMAT_VERSION + 1), "{err}"); + assert!(err.to_string().contains("newer")); +} + +#[test] +fn malformed_project_files_are_errors() { + let bytes = save_project_bytes(&sample().pf).unwrap(); + for len in 0..bytes.len() { + let e = load_project_bytes(&bytes[..len]).unwrap_err(); + assert!(!is_guard_error(&e), "prefix {len}: {e}"); + } + for i in (0..bytes.len()).step_by(3) { + let mut b = bytes.clone(); + b[i] ^= 0x5A; + if let Err(e) = load_project_bytes(&b) { + assert!(!is_guard_error(&e), "flip at {i}: {e}"); + } + } + assert!(load_project_bytes(b"PK\x03\x04 definitely not a zip").is_err()); +} + +/// A project zip with `project.json` from `sample()` plus raw extra entries. +fn zip_with(entries: &[(&str, Vec)]) -> Vec { + let s = sample(); + let mut zip = ZipWriter::new(Cursor::new(Vec::new())); + zip.start_file("project.json", SimpleFileOptions::default()).unwrap(); + zip.write_all(&serde_json::to_vec(&s.pf.project).unwrap()).unwrap(); + for (name, data) in entries { + zip.start_file(*name, SimpleFileOptions::default()).unwrap(); + zip.write_all(data).unwrap(); + } + zip.finish().unwrap().into_inner() +} + +fn tile_bin(res: u32, comps: usize, tiles: &[(u32, u32)]) -> Vec { + let mut b = header(res, res, comps, tiles.len()); + for (tx, ty) in tiles { + b.extend(tx.to_le_bytes()); + b.extend(ty.to_le_bytes()); + b.extend(vec![7u8; TILE_AREA * comps * 2]); + } + b +} + +#[test] +fn tiles_are_validated_against_the_texture_set() { + let paint = sample().paint; + let name = format!("pixels/layer_{}_baseColor.bin", paint.0); + let ok = load_project_bytes(&zip_with(&[(&name, tile_bin(128, 4, &[(1, 1)]))])).unwrap(); + assert_eq!(ok.pixels.layer(paint, Channel::BaseColor).unwrap().get(64, 64), [0x0707; 4]); + for (what, bin) in [ + ("tile out of range", tile_bin(128, 4, &[(2, 0)])), + ("repeated tile", tile_bin(128, 4, &[(0, 0), (0, 0)])), + ("wrong resolution", tile_bin(64, 4, &[(0, 0)])), + ("wrong components", tile_bin(128, 3, &[(0, 0)])), + ("truncated", tile_bin(128, 4, &[(0, 0)])[..100].to_vec()), + ("trailing data", [tile_bin(128, 4, &[]), vec![1]].concat()), + ] { + assert!(load_project_bytes(&zip_with(&[(&name, bin)])).is_err(), "{what}"); + } + let mut newer = tile_bin(128, 4, &[]); + newer[4] = 9; + assert!(matches!(load_project_bytes(&zip_with(&[(&name, newer)])), Err(FormatError::NewerVersion { .. }))); + // Pixels of a layer that is in no stack are dropped, not an error. + let orphan = load_project_bytes(&zip_with(&[("pixels/layer_999_baseColor.bin", tile_bin(128, 4, &[(0, 0)]))])).unwrap(); + assert!(orphan.pixels.layers.is_empty()); + assert!(load_project_bytes(&zip_with(&[("pixels/layer_x_baseColor.bin", vec![])])).is_err()); + assert!(load_project_bytes(&zip_with(&[("baked/0_ambient_occlusion.bin", header(16, 16, 1, 0))])).is_err()); + assert!(load_project_bytes(&zip_with(&[("baked/0_ambient_occlusion.bin", header(100_000, 100_000, 1, 0))])).is_err()); +} diff --git a/crates/geom/Cargo.toml b/crates/geom/Cargo.toml new file mode 100644 index 00000000..d5524df1 --- /dev/null +++ b/crates/geom/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "texturecraft-geom" +description = "TextureCraft geometry: meshes, primitives, tangents, BVH ray casts, UV G-buffer, camera" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lints] +workspace = true + +[dependencies] +glam = { workspace = true } +rayon = { workspace = true } +serde = { workspace = true } +thiserror = { workspace = true } +mikktspace = { workspace = true } diff --git a/crates/geom/src/bvh.rs b/crates/geom/src/bvh.rs new file mode 100644 index 00000000..33e8188a --- /dev/null +++ b/crates/geom/src/bvh.rs @@ -0,0 +1,379 @@ +//! Bounding volume hierarchy over a mesh's triangles: closest-hit and any-hit ray casts, and +//! box queries (used by picking, baking and brush stamps). + +use glam::{Vec3, Vec3A}; + +use crate::Mesh; + +#[derive(Clone, Copy, Debug)] +struct Node { + lo: Vec3A, + hi: Vec3A, + /// Leaf: first triangle in `order`; inner: index of the left child (right = left + 1). + first: u32, + /// Triangles in a leaf; 0 for inner nodes. + count: u32, +} + +/// A ray hit: distance, triangle and barycentrics (weights of vertices 1 and 2). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Hit { + pub t: f32, + pub tri: u32, + pub u: f32, + pub v: f32, +} + +#[derive(Clone, Debug)] +pub struct Bvh { + nodes: Vec, + /// Triangle vertices in BVH order. + tris: Vec<[Vec3; 3]>, + /// Original triangle index of each entry in `tris`. + order: Vec, +} + +const LEAF: usize = 4; + +/// Traversal stack of (node, entry distance): inline for typical depths (no allocation per +/// ray), spilling to the heap for degenerate trees. +struct Stack { + inline: [(u32, f32); 64], + len: usize, + spill: Vec<(u32, f32)>, +} + +impl Default for Stack { + fn default() -> Self { + Stack { inline: [(0, 0.0); 64], len: 0, spill: Vec::new() } + } +} + +impl Stack { + fn push(&mut self, v: (u32, f32)) { + match self.inline.get_mut(self.len) { + Some(slot) if self.spill.is_empty() => { + *slot = v; + self.len += 1; + } + _ => self.spill.push(v), + } + } + + fn pop(&mut self) -> Option<(u32, f32)> { + self.spill.pop().or_else(|| { + self.len = self.len.checked_sub(1)?; + self.inline.get(self.len).copied() + }) + } +} +const BINS: usize = 12; + +fn grow(lo: &mut Vec3, hi: &mut Vec3, p: Vec3) { + *lo = lo.min(p); + *hi = hi.max(p); +} + +fn area(lo: Vec3, hi: Vec3) -> f32 { + let d = (hi - lo).max(Vec3::ZERO); + d.x * d.y + d.y * d.z + d.z * d.x +} + +impl Bvh { + pub fn new(mesh: &Mesh) -> Bvh { + let n = mesh.triangle_count(); + let mut tris = Vec::with_capacity(n); + for t in 0..n { + let Some([a, b, c]) = mesh.tri(t) else { continue }; + let p = |i: usize| mesh.positions.get(i).copied().unwrap_or(Vec3::ZERO); + tris.push([p(a), p(b), p(c)]); + } + let mut order: Vec = (0..tris.len() as u32).collect(); + let cent: Vec = tris.iter().map(|t| (t[0] + t[1] + t[2]) / 3.0).collect(); + let mut nodes = vec![Node { lo: Vec3A::ZERO, hi: Vec3A::ZERO, first: 0, count: tris.len() as u32 }]; + let mut stack = vec![0usize]; + while let Some(ni) = stack.pop() { + let Node { first, count, .. } = nodes[ni]; + let (f, c) = (first as usize, count as usize); + let (mut lo, mut hi) = (Vec3::splat(f32::MAX), Vec3::splat(f32::MIN)); + let (mut clo, mut chi) = (Vec3::splat(f32::MAX), Vec3::splat(f32::MIN)); + for &t in &order[f..f + c] { + for p in tris[t as usize] { + grow(&mut lo, &mut hi, p); + } + grow(&mut clo, &mut chi, cent[t as usize]); + } + nodes[ni].lo = lo.into(); + nodes[ni].hi = hi.into(); + if c <= LEAF { + continue; + } + // Binned SAH along the widest centroid axis. + let ext = chi - clo; + let axis = if ext.x >= ext.y && ext.x >= ext.z { + 0 + } else if ext.y >= ext.z { + 1 + } else { + 2 + }; + if ext[axis] <= 1e-12 { + continue; + } + let mut bins = [(0u32, Vec3::splat(f32::MAX), Vec3::splat(f32::MIN)); BINS]; + let bin_of = |p: Vec3| (((p[axis] - clo[axis]) / ext[axis] * BINS as f32) as usize).min(BINS - 1); + for &t in &order[f..f + c] { + let b = &mut bins[bin_of(cent[t as usize])]; + b.0 += 1; + for p in tris[t as usize] { + grow(&mut b.1, &mut b.2, p); + } + } + let (mut best, mut best_cost) = (0usize, f32::MAX); + for split in 1..BINS { + let side = |r: std::ops::Range| { + r.fold((0u32, Vec3::splat(f32::MAX), Vec3::splat(f32::MIN)), |(n, l, h), i| (n + bins[i].0, l.min(bins[i].1), h.max(bins[i].2))) + }; + let (nl, ll, lh) = side(0..split); + let (nr, rl, rh) = side(split..BINS); + if nl == 0 || nr == 0 { + continue; + } + let cost = nl as f32 * area(ll, lh) + nr as f32 * area(rl, rh); + if cost < best_cost { + best_cost = cost; + best = split; + } + } + if best == 0 || best_cost >= c as f32 * area(lo, hi) { + continue; + } + let slice = &mut order[f..f + c]; + let mut i = 0; + for j in 0..slice.len() { + if bin_of(cent[slice[j] as usize]) < best { + slice.swap(i, j); + i += 1; + } + } + if i == 0 || i == c { + continue; + } + let left = nodes.len(); + nodes.push(Node { lo: Vec3A::ZERO, hi: Vec3A::ZERO, first, count: i as u32 }); + nodes.push(Node { lo: Vec3A::ZERO, hi: Vec3A::ZERO, first: first + i as u32, count: (c - i) as u32 }); + nodes[ni].first = left as u32; + nodes[ni].count = 0; + stack.push(left); + stack.push(left + 1); + } + let tris = order.iter().map(|t| tris[*t as usize]).collect(); + Bvh { nodes, tris, order } + } + + pub fn is_empty(&self) -> bool { + self.tris.is_empty() + } + + fn slab(lo: Vec3A, hi: Vec3A, o: Vec3A, inv: Vec3A, tmax: f32) -> Option { + let t0 = (lo - o) * inv; + let t1 = (hi - o) * inv; + // A ray lying in a slab plane gives 0·∞ = NaN: it is inside that slab, so the axis does + // not bound the hit. + let open = t0.is_nan_mask() | t1.is_nan_mask(); + let tn = Vec3A::select(open, Vec3A::NEG_INFINITY, t0.min(t1)).max_element().max(0.0); + let tf = Vec3A::select(open, Vec3A::INFINITY, t0.max(t1)).min_element().min(tmax); + (tn <= tf).then_some(tn) + } + + /// Möller–Trumbore, double sided. + fn hit_tri(t: &[Vec3; 3], o: Vec3, d: Vec3) -> Option<(f32, f32, f32)> { + let e1 = t[1] - t[0]; + let e2 = t[2] - t[0]; + let p = d.cross(e2); + let det = e1.dot(p); + if det.abs() < 1e-12 { + return None; + } + let inv = 1.0 / det; + let s = o - t[0]; + // A little slack on the edges: rays through a shared edge must not slip between its + // two triangles to a surface behind. + const EDGE: f32 = 1e-5; + let u = s.dot(p) * inv; + if !(-EDGE..=1.0 + EDGE).contains(&u) { + return None; + } + let q = s.cross(e1); + let v = d.dot(q) * inv; + if v < -EDGE || u + v > 1.0 + EDGE { + return None; + } + let tt = e2.dot(q) * inv; + (tt > 0.0).then_some((tt, u, v)) + } + + fn traverse(&self, o: Vec3, d: Vec3, mut tmax: f32, any: bool, filter: &dyn Fn(u32) -> bool) -> Option { + let root = self.nodes.first().filter(|_| !self.tris.is_empty())?; + let (oa, inv) = (Vec3A::from(o), Vec3A::ONE / Vec3A::from(d)); + let mut best = None; + let mut stack = Stack::default(); + stack.push((0, Self::slab(root.lo, root.hi, oa, inv, tmax)?)); + // Entries carry their entry distance: nodes behind a closer hit are skipped unopened. + while let Some((ni, tn)) = stack.pop() { + let Some(n) = self.nodes.get(ni as usize).filter(|_| tn <= tmax) else { continue }; + if n.count > 0 { + let (f, c) = (n.first as usize, n.count as usize); + for i in f..(f + c).min(self.tris.len()) { + let (Some(tri), Some(&orig)) = (self.tris.get(i), self.order.get(i)) else { continue }; + if let Some((t, u, v)) = Self::hit_tri(tri, o, d) + && t < tmax + && filter(orig) + { + tmax = t; + best = Some(Hit { t, tri: orig, u, v }); + if any { + return best; + } + } + } + } else { + // Visit the nearer child first. + let (l, r) = (n.first, n.first + 1); + let dl = self.nodes.get(l as usize).and_then(|c| Self::slab(c.lo, c.hi, oa, inv, tmax)); + let dr = self.nodes.get(r as usize).and_then(|c| Self::slab(c.lo, c.hi, oa, inv, tmax)); + match (dl, dr) { + (Some(a), Some(b)) if a <= b => { + stack.push((r, b)); + stack.push((l, a)); + } + (Some(a), Some(b)) => { + stack.push((l, a)); + stack.push((r, b)); + } + (Some(a), None) => stack.push((l, a)), + (None, Some(b)) => stack.push((r, b)), + (None, None) => {} + } + } + } + best + } + + /// Closest hit along `o + t·d` for `t` in (0, tmax). + pub fn closest(&self, o: Vec3, d: Vec3, tmax: f32) -> Option { + self.traverse(o, d, tmax, false, &|_| true) + } + + /// Closest hit on triangles accepted by `filter` (original triangle index). + pub fn closest_filtered(&self, o: Vec3, d: Vec3, tmax: f32, filter: &dyn Fn(u32) -> bool) -> Option { + self.traverse(o, d, tmax, false, filter) + } + + /// Whether anything is hit before `tmax` (occlusion rays). + pub fn occluded(&self, o: Vec3, d: Vec3, tmax: f32) -> bool { + self.traverse(o, d, tmax, true, &|_| true).is_some() + } + + /// Call `f` with every triangle (original index) whose bounds overlap the box. + pub fn query_box(&self, lo: Vec3, hi: Vec3, mut f: impl FnMut(u32)) { + let mut stack = vec![0u32]; + while let Some(i) = stack.pop() { + let Some(n) = self.nodes.get(i as usize) else { continue }; + if self.tris.is_empty() || n.hi.cmplt(lo.into()).any() || n.lo.cmpgt(hi.into()).any() { + continue; + } + if n.count > 0 { + for k in n.first as usize..(n.first + n.count) as usize { + let (Some(t), Some(o)) = (self.tris.get(k), self.order.get(k)) else { continue }; + let tlo = t[0].min(t[1]).min(t[2]); + let thi = t[0].max(t[1]).max(t[2]); + if !(thi.cmplt(lo).any() || tlo.cmpgt(hi).any()) { + f(*o); + } + } + } else { + stack.push(n.first); + stack.push(n.first + 1); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::primitives; + + #[test] + fn traversal_stack_spills_in_lifo_order() { + let mut st = Stack::default(); + for v in 0..200 { + st.push((v, v as f32)); + } + assert!((0..200).rev().all(|v| st.pop() == Some((v, v as f32)))); + assert_eq!(st.pop(), None); + } + + #[test] + fn ray_hits_sphere_at_expected_distance() { + let m = primitives::sphere(1.0, 64, 32); + let bvh = Bvh::new(&m); + let h = bvh.closest(Vec3::new(0.0, 0.0, 5.0), Vec3::NEG_Z, 100.0).unwrap(); + assert!((h.t - 4.0).abs() < 0.01, "{}", h.t); + assert!(bvh.closest(Vec3::new(0.0, 3.0, 5.0), Vec3::NEG_Z, 100.0).is_none()); + // Rays lying exactly in a node's bounding plane (y = 0 is the equator) hit the front. + // Axis-aligned rays lying in a node's bounding plane (y = 0) still find the front. + for x in [-0.5f32, -0.4, 0.3] { + let h = bvh.closest(Vec3::new(x, 0.0, 5.0), Vec3::NEG_Z, 100.0).unwrap(); + assert!(h.t < 5.0, "x {x}: hit the back at t {}", h.t); + } + // Rays through shared edges (the equator) hit the front, never slip through to the back. + let m = primitives::sphere(1.0, 48, 24); + let bvh = Bvh::new(&m); + for i in 0..200 { + let x = i as f32 / 100.0 - 1.0; + let o = Vec3::new(0.0, 0.0, 5.0); + let d = (Vec3::new(x * 0.9, 0.0, 0.0) - o).normalize(); + let h = bvh.closest(o, d, 100.0).unwrap(); + assert!(h.t < 5.0, "x {x}: hit the back at t {}", h.t); + } + assert!(bvh.occluded(Vec3::new(0.0, 0.0, 5.0), Vec3::NEG_Z, 10.0)); + assert!(!bvh.occluded(Vec3::new(0.0, 0.0, 5.0), Vec3::NEG_Z, 3.0)); + } + + #[test] + fn matches_brute_force() { + let m = primitives::material_ball(); + let bvh = Bvh::new(&m); + for i in 0..200 { + let a = i as f32 * 0.731; + let o = Vec3::new(a.cos() * 6.0, (i as f32 * 0.37).sin() * 3.0 + 1.0, a.sin() * 6.0); + let d = (Vec3::new(0.0, 1.0, 0.0) + Vec3::new((a * 3.1).sin(), (a * 1.7).cos(), (a * 2.3).sin()) * 0.6 - o).normalize(); + let fast = bvh.closest(o, d, 1e9).map(|h| h.t); + let mut slow: Option = None; + for t in 0..m.triangle_count() { + let [x, y, z] = m.tri(t).unwrap(); + if let Some((tt, _, _)) = Bvh::hit_tri(&[m.positions[x], m.positions[y], m.positions[z]], o, d) { + slow = Some(slow.map_or(tt, |s: f32| s.min(tt))); + } + } + match (fast, slow) { + (Some(a), Some(b)) => assert!((a - b).abs() < 1e-4, "{a} {b}"), + (None, None) => {} + other => panic!("mismatch {other:?}"), + } + } + } + + #[test] + fn box_query_finds_overlapping_triangles() { + let m = primitives::plane(2.0, 4); + let bvh = Bvh::new(&m); + let mut found = Vec::new(); + bvh.query_box(Vec3::new(-0.1, -0.1, -0.1), Vec3::new(0.1, 0.1, 0.1), |t| found.push(t)); + assert!(!found.is_empty() && found.len() <= 8, "{found:?}"); + let empty = Bvh::new(&Mesh::default()); + assert!(empty.closest(Vec3::ZERO, Vec3::X, 1.0).is_none()); + } +} diff --git a/crates/geom/src/camera.rs b/crates/geom/src/camera.rs new file mode 100644 index 00000000..93f27e27 --- /dev/null +++ b/crates/geom/src/camera.rs @@ -0,0 +1,140 @@ +//! Orbit camera (Painter-style navigation) and picking rays. + +use glam::{Mat4, Vec2, Vec3, Vec4}; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +pub struct OrbitCamera { + pub target: Vec3, + /// Radians around Y. + pub yaw: f32, + /// Radians above the horizon, clamped to ±89°. + pub pitch: f32, + pub distance: f32, + /// Vertical field of view, degrees. + pub fov: f32, + /// Scene radius (sets the clip planes). + pub scene_radius: f32, + /// Orthographic projection instead of perspective. + #[serde(default)] + pub ortho: bool, +} + +impl Default for OrbitCamera { + fn default() -> Self { + OrbitCamera { target: Vec3::ZERO, yaw: 0.0, pitch: 0.12, distance: 5.0, fov: 40.0, scene_radius: 1.0, ortho: false } + } +} + +/// A ray in world space. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Ray { + pub origin: Vec3, + pub dir: Vec3, +} + +impl OrbitCamera { + pub fn eye(&self) -> Vec3 { + let (sy, cy) = self.yaw.sin_cos(); + let (sp, cp) = self.pitch.sin_cos(); + self.target + Vec3::new(cp * sy, sp, cp * cy) * self.distance + } + + pub fn forward(&self) -> Vec3 { + (self.target - self.eye()).try_normalize().unwrap_or(Vec3::NEG_Z) + } + + pub fn view(&self) -> Mat4 { + Mat4::look_at_rh(self.eye(), self.target, Vec3::Y) + } + + pub fn proj(&self, aspect: f32) -> Mat4 { + let r = self.scene_radius.max(1e-3); + let near = (self.distance - r * 4.0).max(r * 0.01).max(1e-4); + let far = self.distance + r * 50.0; + let aspect = if aspect.is_finite() && aspect > 1e-4 { aspect } else { 1.0 }; + if self.ortho { + let h = self.distance * (self.fov.to_radians() * 0.5).tan(); + Mat4::orthographic_rh(-h * aspect, h * aspect, -h, h, near.min(0.01), far) + } else { + Mat4::perspective_rh(self.fov.clamp(5.0, 150.0).to_radians(), aspect, near, far) + } + } + + pub fn view_proj(&self, aspect: f32) -> Mat4 { + self.proj(aspect) * self.view() + } + + /// Frame a bounding box, keeping the current angles. + pub fn frame(&mut self, lo: Vec3, hi: Vec3) { + self.target = (lo + hi) * 0.5; + let r = ((hi - lo).length() * 0.5).max(1e-3); + self.scene_radius = r; + self.distance = r / (self.fov.clamp(5.0, 150.0).to_radians() * 0.5).sin() * 1.1; + } + + pub fn orbit(&mut self, dx: f32, dy: f32) { + self.yaw -= dx; + self.pitch = (self.pitch + dy).clamp(-89f32.to_radians(), 89f32.to_radians()); + } + + /// Pan by a screen fraction (dx, dy in viewport heights). + pub fn pan(&mut self, dx: f32, dy: f32) { + let f = self.forward(); + let right = f.cross(Vec3::Y).try_normalize().unwrap_or(Vec3::X); + let up = right.cross(f); + let scale = self.distance * (self.fov.to_radians() * 0.5).tan() * 2.0; + self.target += (-right * dx + up * dy) * scale; + } + + /// Dolly by a factor exponent (positive zooms in). + pub fn zoom(&mut self, amount: f32) { + let r = self.scene_radius.max(1e-3); + self.distance = (self.distance * (-amount).exp()).clamp(r * 0.05, r * 200.0); + } + + /// Ray through a viewport point given in normalized device coordinates (-1..1, y up). + pub fn ray(&self, ndc: Vec2, aspect: f32) -> Ray { + let inv = self.view_proj(aspect).inverse(); + let a = inv * Vec4::new(ndc.x, ndc.y, 0.0, 1.0); + let b = inv * Vec4::new(ndc.x, ndc.y, 1.0, 1.0); + let (a, b) = (a.truncate() / a.w, b.truncate() / b.w); + Ray { origin: a, dir: (b - a).try_normalize().unwrap_or(self.forward()) } + } + + /// Project a world point to NDC (x, y in -1..1, y up; z depth). None behind the camera. + pub fn project(&self, p: Vec3, aspect: f32) -> Option { + let c = self.view_proj(aspect) * p.extend(1.0); + (c.w > 1e-6).then(|| c.truncate() / c.w) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn center_ray_points_at_target_and_projection_inverts() { + let mut c = OrbitCamera::default(); + c.frame(Vec3::splat(-1.0), Vec3::splat(1.0)); + let r = c.ray(Vec2::ZERO, 1.5); + assert!((r.dir - c.forward()).length() < 1e-4); + let p = Vec3::new(0.3, -0.2, 0.1); + let ndc = c.project(p, 1.5).unwrap(); + let r = c.ray(ndc.truncate(), 1.5); + let to_p = (p - r.origin).normalize(); + assert!(to_p.dot(r.dir) > 0.9999); + } + + #[test] + fn pitch_is_clamped_and_zoom_bounded() { + let mut c = OrbitCamera::default(); + c.orbit(0.0, 10.0); + assert!(c.pitch <= 89f32.to_radians() + 1e-6); + for _ in 0..1000 { + c.zoom(1.0); + } + assert!(c.distance > 0.0 && c.distance.is_finite()); + let _ = c.proj(f32::NAN); + } +} diff --git a/crates/geom/src/gbuffer.rs b/crates/geom/src/gbuffer.rs new file mode 100644 index 00000000..e26b923f --- /dev/null +++ b/crates/geom/src/gbuffer.rs @@ -0,0 +1,195 @@ +//! The UV-space G-buffer: which triangle (and where on it) each texel of a texture set maps +//! to. Painting, compositing, baking and padding all work from this one buffer, so they agree +//! on which texel is which surface point. + +use glam::{Vec2, Vec3, Vec4}; +use rayon::prelude::*; + +use crate::Mesh; + +/// Texel not covered by any triangle. +pub const NO_TRI: u32 = u32::MAX; + +const BAND: u32 = 32; + +#[derive(Clone, Debug, PartialEq)] +pub struct UvGBuffer { + pub width: u32, + pub height: u32, + /// Triangle under each texel center, [`NO_TRI`] when uncovered. + pub tri: Vec, + /// Barycentric weights of the triangle's second and third vertices. + pub bary: Vec<[f32; 2]>, + /// Texels written by more than one triangle (overlapping UVs). + pub overlaps: u32, +} + +/// Interpolated surface attributes at a texel. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Surface { + pub pos: Vec3, + /// Interpolated shading normal (normalized). + pub nrm: Vec3, + /// Flat triangle normal (for ray offsets). + pub geo_nrm: Vec3, + pub tangent: Vec4, + pub uv: Vec2, + pub tri: u32, +} + +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) +} + +impl UvGBuffer { + /// 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![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() { + 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 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 overlaps: u32 = tri + .par_chunks_mut(band_len) + .zip(bary.par_chunks_mut(band_len)) + .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 { + 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 { + 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]; + } + } + } + } + overlaps + }) + .sum(); + UvGBuffer { width: w, height: h, tri, bary, overlaps } + } + + pub fn covered(&self, i: usize) -> bool { + self.tri.get(i).is_some_and(|t| *t != NO_TRI) + } + + pub fn coverage_mask(&self) -> Vec { + self.tri.iter().map(|t| *t != NO_TRI).collect() + } + + pub fn covered_count(&self) -> usize { + self.tri.iter().filter(|t| **t != NO_TRI).count() + } + + /// Surface attributes at texel index `i` (None when uncovered). + pub fn surface(&self, mesh: &Mesh, i: usize) -> Option { + let t = *self.tri.get(i)?; + if t == NO_TRI { + return None; + } + let [b1, b2] = *self.bary.get(i)?; + surface_at(mesh, t, b1, b2) + } +} + +/// Interpolate triangle `t` at barycentrics (weights of vertices 1 and 2). +pub fn surface_at(mesh: &Mesh, t: u32, b1: f32, b2: f32) -> Option { + let [a, b, c] = mesh.tri(t as usize)?; + let b0 = 1.0 - b1 - b2; + let p = |v: &[Vec3]| -> Option { Some(*v.get(a)? * b0 + *v.get(b)? * b1 + *v.get(c)? * b2) }; + let pos = p(&mesh.positions)?; + let nrm = p(&mesh.normals)?.try_normalize().unwrap_or(Vec3::Y); + let (pa, pb, pc) = (*mesh.positions.get(a)?, *mesh.positions.get(b)?, *mesh.positions.get(c)?); + let geo_nrm = (pb - pa).cross(pc - pa).try_normalize().unwrap_or(nrm); + let tan = *mesh.tangents.get(a)? * b0 + *mesh.tangents.get(b)? * b1 + *mesh.tangents.get(c)? * b2; + let uv = *mesh.uvs.get(a)? * b0 + *mesh.uvs.get(b)? * b1 + *mesh.uvs.get(c)? * b2; + let tdir = tan.truncate().try_normalize().unwrap_or_else(|| nrm.any_orthonormal_vector()); + let w = mesh.tangents.get(a).map(|t| if t.w < 0.0 { -1.0 } else { 1.0 }).unwrap_or(1.0); + Some(Surface { pos, nrm, geo_nrm, tangent: tdir.extend(w), uv, tri: t }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::primitives; + + #[test] + fn half_triangle_covers_texels_with_centers_inside() { + // One triangle covering the lower-left half of UV space (v down: y = v·H). + 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(); + let g = UvGBuffer::new(&m, 0, 8, 8); + for y in 0..8u32 { + for x in 0..8u32 { + let inside = (x as f32 + 0.5) <= (y as f32 + 0.5); + assert_eq!(g.covered((y * 8 + x) as usize), inside, "({x},{y})"); + } + } + assert_eq!(g.overlaps, 0); + } + + #[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)]; + let m = Mesh::from_parts(vec![Vec3::ZERO, Vec3::X, Vec3::Y, Vec3::Z], None, uv, vec![0, 1, 2, 0, 1, 2, 0, 3, 3], "m").unwrap(); + let g = UvGBuffer::new(&m, 0, 16, 16); + assert!(g.overlaps > 0); + assert!(g.covered_count() > 0); + } + + #[test] + fn sphere_surface_lies_on_the_sphere() { + let m = primitives::sphere(2.0, 64, 32); + let g = UvGBuffer::new(&m, 0, 128, 128); + assert!(g.covered_count() > 128 * 128 * 9 / 10, "{}", g.covered_count()); + for i in (0..g.tri.len()).step_by(97) { + if let Some(s) = g.surface(&m, i) { + assert!((s.pos.length() - 2.0).abs() < 0.02); + assert!(s.nrm.dot(s.pos.normalize()) > 0.99); + } + } + } +} diff --git a/crates/geom/src/lib.rs b/crates/geom/src/lib.rs new file mode 100644 index 00000000..08ed2624 --- /dev/null +++ b/crates/geom/src/lib.rs @@ -0,0 +1,28 @@ +//! # texturecraft-geom +//! +//! Geometry for TextureCraft: the [`Mesh`] type with normals and MikkTSpace tangents, built-in +//! [`primitives`] (material ball, sphere, cube, …), a [`Bvh`] for ray casts and box queries, +//! the UV-space [`UvGBuffer`] that maps texels to surface points, and the [`OrbitCamera`]. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod bvh; +pub mod camera; +pub mod gbuffer; +pub mod mesh; +pub mod primitives; +pub mod unwrap; + +pub use bvh::{Bvh, Hit}; +pub use camera::{OrbitCamera, Ray}; +pub use gbuffer::{NO_TRI, Surface, UvGBuffer, surface_at}; +pub use glam; +pub use mesh::{MAX_TRIANGLES, Mesh, SubMesh}; + +#[derive(Debug, thiserror::Error, Clone, PartialEq)] +pub enum GeomError { + #[error("invalid mesh: {0}")] + Invalid(String), +} + +pub type Result = std::result::Result; diff --git a/crates/geom/src/mesh.rs b/crates/geom/src/mesh.rs new file mode 100644 index 00000000..c30c51bd --- /dev/null +++ b/crates/geom/src/mesh.rs @@ -0,0 +1,325 @@ +//! The internal triangle mesh. + +use glam::{Vec2, Vec3, Vec4}; + +use crate::{GeomError, Result}; + +/// A contiguous index range drawn with one material. +#[derive(Clone, Debug, PartialEq)] +pub struct SubMesh { + pub name: String, + pub material: usize, + /// First index (into [`Mesh::indices`]) and index count. + pub start: u32, + pub count: u32, +} + +/// Triangle mesh in world space. UVs use the glTF convention (origin top-left, v down). +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Mesh { + pub positions: Vec, + pub normals: Vec, + /// xyz tangent, w bitangent sign. + pub tangents: Vec, + pub uvs: Vec, + /// RGBA vertex colors in 0..1 (glTF: linear; OBJ, PLY: as stored): one per vertex, or empty. + pub colors: Vec, + pub indices: Vec, + /// Index ranges per material and part (node / object); `name` is the part name. + pub submeshes: Vec, + pub materials: Vec, +} + +/// Hard cap on mesh size accepted from files (triangles). +pub const MAX_TRIANGLES: usize = 20_000_000; + +impl Mesh { + pub fn triangle_count(&self) -> usize { + self.indices.len() / 3 + } + + pub fn vertex_count(&self) -> usize { + self.positions.len() + } + + /// Axis-aligned bounds (zero box for an empty mesh). + pub fn bounds(&self) -> (Vec3, Vec3) { + if self.positions.is_empty() { + return (Vec3::ZERO, Vec3::ZERO); + } + self.positions.iter().fold((Vec3::splat(f32::MAX), Vec3::splat(f32::MIN)), |(lo, hi), p| (lo.min(*p), hi.max(*p))) + } + + /// Bounding-sphere radius around the bounds center (at least a small epsilon). + pub fn radius(&self) -> f32 { + let (lo, hi) = self.bounds(); + ((hi - lo).length() * 0.5).max(1e-4) + } + + /// Vertex indices of triangle `t`. + pub fn tri(&self, t: usize) -> Option<[usize; 3]> { + let i = self.indices.get(t * 3..t * 3 + 3)?; + Some([i[0] as usize, i[1] as usize, i[2] as usize]) + } + + /// Material of triangle `t`. + pub fn material_of(&self, t: usize) -> Option { + let idx = (t * 3) as u32; + self.submeshes.iter().find(|s| idx >= s.start && idx < s.start.saturating_add(s.count)).map(|s| s.material) + } + + /// Triangle index range belonging to material `m` (all submeshes of that material). + pub fn triangles_of(&self, m: usize) -> impl Iterator + '_ { + self.submeshes.iter().filter(move |s| s.material == m).flat_map(|s| (s.start / 3) as usize..((s.start + s.count) / 3) as usize) + } + + /// Check that attribute arrays agree and every index is in range. + pub fn validate(&self) -> Result<()> { + let n = self.positions.len(); + if self.normals.len() != n || self.uvs.len() != n || self.tangents.len() != n { + return Err(GeomError::Invalid(format!( + "attribute counts differ: {} positions, {} normals, {} uvs, {} tangents", + n, + self.normals.len(), + self.uvs.len(), + self.tangents.len() + ))); + } + if !self.colors.is_empty() && self.colors.len() != n { + return Err(GeomError::Invalid(format!("{} vertex colors for {n} vertices", self.colors.len()))); + } + if !self.indices.len().is_multiple_of(3) { + return Err(GeomError::Invalid("index count is not a multiple of 3".into())); + } + if self.triangle_count() > MAX_TRIANGLES { + return Err(GeomError::Invalid(format!("mesh has more than {MAX_TRIANGLES} triangles"))); + } + if let Some(bad) = self.indices.iter().find(|i| **i as usize >= n) { + return Err(GeomError::Invalid(format!("index {bad} out of range ({n} vertices)"))); + } + if self.positions.iter().any(|p| !p.is_finite()) { + return Err(GeomError::Invalid("mesh has non-finite positions".into())); + } + for s in &self.submeshes { + if s.start as usize + s.count as usize > self.indices.len() || s.material >= self.materials.len().max(1) { + return Err(GeomError::Invalid(format!("submesh {} is out of range", s.name))); + } + } + Ok(()) + } + + /// Area-weighted smooth normals over the index buffer. + pub fn compute_normals(&mut self) { + let mut n = vec![Vec3::ZERO; self.positions.len()]; + for t in self.indices.chunks_exact(3) { + let (a, b, c) = (t[0] as usize, t[1] as usize, t[2] as usize); + let (Some(pa), Some(pb), Some(pc)) = (self.positions.get(a), self.positions.get(b), self.positions.get(c)) else { continue }; + let face = (*pb - *pa).cross(*pc - *pa); + for v in [a, b, c] { + if let Some(x) = n.get_mut(v) { + *x += face; + } + } + } + self.normals = n.into_iter().map(|v| v.try_normalize().unwrap_or(Vec3::Y)).collect(); + } + + /// MikkTSpace tangents (falls back to per-triangle tangents if generation fails). + pub fn compute_tangents(&mut self) { + self.tangents = vec![Vec4::new(1.0, 0.0, 0.0, 1.0); self.positions.len()]; + if self.normals.len() != self.positions.len() { + self.compute_normals(); + } + if self.uvs.len() != self.positions.len() { + self.uvs = vec![Vec2::ZERO; self.positions.len()]; + } + // mikktspace crashes (out-of-bounds) when every triangle is degenerate; skip it then. + let usable = !crate::unwrap::all_triangles_degenerate(self); + let mut g = Mikk { mesh: self }; + if !usable || !mikktspace::generate_tangents(&mut g) { + self.simple_tangents(); + } + for (t, n) in self.tangents.iter_mut().zip(&self.normals) { + // Orthonormalize against the normal; repair degenerate results. + let v = t.truncate(); + let o = (v - *n * n.dot(v)).try_normalize().unwrap_or_else(|| n.any_orthonormal_vector()); + *t = o.extend(if t.w < 0.0 { -1.0 } else { 1.0 }); + } + } + + fn simple_tangents(&mut self) { + let mut acc = vec![Vec3::ZERO; self.positions.len()]; + for t in self.indices.chunks_exact(3) { + let (a, b, c) = (t[0] as usize, t[1] as usize, t[2] as usize); + let (Some(pa), Some(pb), Some(pc)) = (self.positions.get(a), self.positions.get(b), self.positions.get(c)) else { continue }; + let (Some(ua), Some(ub), Some(uc)) = (self.uvs.get(a), self.uvs.get(b), self.uvs.get(c)) else { continue }; + let (e1, e2) = (*pb - *pa, *pc - *pa); + let (d1, d2) = (*ub - *ua, *uc - *ua); + let r = d1.x * d2.y - d2.x * d1.y; + if r.abs() < 1e-12 { + continue; + } + let tan = (e1 * d2.y - e2 * d1.y) / r; + for v in [a, b, c] { + if let Some(x) = acc.get_mut(v) { + *x += tan; + } + } + } + self.tangents = acc.into_iter().map(|t| t.try_normalize().unwrap_or(Vec3::X).extend(1.0)).collect(); + } + + /// Append `other`, offsetting indices; its submeshes keep their material indices shifted by + /// this mesh's material count. + pub fn append(&mut self, other: &Mesh) { + let base = self.positions.len() as u32; + let ibase = self.indices.len() as u32; + let mbase = self.materials.len(); + self.positions.extend_from_slice(&other.positions); + self.normals.extend_from_slice(&other.normals); + self.tangents.extend_from_slice(&other.tangents); + self.uvs.extend_from_slice(&other.uvs); + if !(self.colors.is_empty() && other.colors.is_empty()) { + // Vertices of the side without colors become white. + self.colors.resize(base as usize, Vec4::ONE); + self.colors.extend(other.colors.iter().copied().chain(std::iter::repeat(Vec4::ONE)).take(other.positions.len())); + } + self.indices.extend(other.indices.iter().map(|i| i + base)); + self.materials.extend(other.materials.iter().cloned()); + self.submeshes.extend(other.submeshes.iter().map(|s| SubMesh { + name: s.name.clone(), + material: s.material + mbase, + start: s.start + ibase, + count: s.count, + })); + } + + /// Uniformly scale and translate so the mesh is centered with the given bounding radius. + pub fn normalize_to(&mut self, radius: f32) { + let (lo, hi) = self.bounds(); + let c = (lo + hi) * 0.5; + let s = radius / self.radius(); + for p in &mut self.positions { + *p = (*p - c) * s; + } + } + + /// A single-material mesh from raw attributes; computes missing normals and tangents. + pub fn from_parts(positions: Vec, normals: Option>, uvs: Vec, indices: Vec, material: &str) -> Result { + let count = indices.len() as u32; + let mut m = Mesh { + normals: normals.unwrap_or_default(), + uvs, + positions, + indices, + tangents: Vec::new(), + colors: Vec::new(), + submeshes: vec![SubMesh { name: material.into(), material: 0, start: 0, count }], + materials: vec![material.into()], + }; + if m.normals.len() != m.positions.len() { + m.compute_normals(); + } + m.compute_tangents(); + m.validate()?; + Ok(m) + } +} + +struct Mikk<'a> { + mesh: &'a mut Mesh, +} + +impl Mikk<'_> { + fn vi(&self, face: usize, vert: usize) -> usize { + self.mesh.indices.get(face * 3 + vert).copied().unwrap_or(0) as usize + } +} + +impl mikktspace::Geometry for Mikk<'_> { + fn num_faces(&self) -> usize { + self.mesh.indices.len() / 3 + } + fn num_vertices_of_face(&self, _face: usize) -> usize { + 3 + } + fn position(&self, face: usize, vert: usize) -> [f32; 3] { + self.mesh.positions.get(self.vi(face, vert)).copied().unwrap_or_default().to_array() + } + fn normal(&self, face: usize, vert: usize) -> [f32; 3] { + self.mesh.normals.get(self.vi(face, vert)).copied().unwrap_or(Vec3::Y).to_array() + } + fn tex_coord(&self, face: usize, vert: usize) -> [f32; 2] { + // MikkTSpace expects OpenGL texture coordinates (v up); our UVs are glTF (v down). With + // v flipped, the bitangent `cross(N, T) * w` points up the image, as glTF requires. + let uv = self.mesh.uvs.get(self.vi(face, vert)).copied().unwrap_or_default(); + [uv.x, 1.0 - uv.y] + } + fn set_tangent_encoded(&mut self, tangent: [f32; 4], face: usize, vert: usize) { + let i = self.vi(face, vert); + if let Some(t) = self.mesh.tangents.get_mut(i) { + *t = Vec4::from_array(tangent); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn quad() -> Mesh { + Mesh::from_parts( + vec![Vec3::new(0.0, 0.0, 0.0), Vec3::new(1.0, 0.0, 0.0), Vec3::new(1.0, 1.0, 0.0), Vec3::new(0.0, 1.0, 0.0)], + 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() + } + + #[test] + fn normals_and_tangents_of_a_quad() { + let m = quad(); + for n in &m.normals { + assert!((*n - Vec3::Z).length() < 1e-5); + } + for t in &m.tangents { + assert!((t.truncate() - Vec3::X).length() < 1e-4, "{t:?}"); + } + assert_eq!(m.material_of(1), Some(0)); + assert_eq!(m.triangles_of(0).collect::>(), vec![0, 1]); + } + + #[test] + fn validate_catches_bad_indices() { + let mut m = quad(); + m.indices[0] = 99; + assert!(m.validate().is_err()); + let mut m = quad(); + m.uvs.pop(); + assert!(m.validate().is_err()); + } + + #[test] + fn append_offsets_indices_and_materials() { + let mut a = quad(); + let b = quad(); + a.append(&b); + assert_eq!(a.indices[6], 4); + assert_eq!(a.submeshes[1].material, 1); + assert_eq!(a.submeshes[1].start, 6); + a.validate().unwrap(); + // Colors on one side only: the other side's vertices become white. + let mut red = quad(); + red.colors = vec![Vec4::new(1.0, 0.0, 0.0, 1.0); 4]; + a.append(&red); + assert_eq!(a.colors.len(), 12); + assert_eq!((a.colors[0], a.colors[8]), (Vec4::ONE, Vec4::new(1.0, 0.0, 0.0, 1.0))); + a.append(&quad()); + assert_eq!(a.colors[15], Vec4::ONE); + a.validate().unwrap(); + a.colors.pop(); + assert!(a.validate().is_err()); + } +} diff --git a/crates/geom/src/primitives.rs b/crates/geom/src/primitives.rs new file mode 100644 index 00000000..39e52a4d --- /dev/null +++ b/crates/geom/src/primitives.rs @@ -0,0 +1,373 @@ +//! Built-in meshes with clean, non-overlapping UVs: material ball, sphere, (rounded) cube, +//! cylinder, torus and plane. Y is up; triangles are counter-clockwise seen from outside. + +use std::f32::consts::{PI, TAU}; + +use glam::{Vec2, Vec3}; + +use crate::mesh::{Mesh, SubMesh}; + +/// A UV-space rectangle: origin and size. +#[derive(Clone, Copy, Debug)] +struct UvRect { + o: Vec2, + s: Vec2, +} + +impl UvRect { + const FULL: UvRect = UvRect { o: Vec2::ZERO, s: Vec2::ONE }; + fn map(&self, u: f32, v: f32) -> Vec2 { + self.o + self.s * Vec2::new(u, v) + } + fn inset(&self, m: f32) -> UvRect { + UvRect { o: self.o + Vec2::splat(m), s: self.s - Vec2::splat(2.0 * m) } + } +} + +#[derive(Default)] +struct Builder { + p: Vec, + n: Vec, + uv: Vec, + idx: Vec, +} + +impl Builder { + fn vert(&mut self, p: Vec3, n: Vec3, uv: Vec2) -> u32 { + self.p.push(p); + self.n.push(n.try_normalize().unwrap_or(Vec3::Y)); + self.uv.push(uv); + (self.p.len() - 1) as u32 + } + + /// A grid of (rows+1)×(cols+1) vertices from `f(row, col)`, rows going "down" the texture. + fn grid(&mut self, rows: usize, cols: usize, mut f: impl FnMut(usize, usize) -> (Vec3, Vec3, Vec2)) { + let base = self.p.len() as u32; + for i in 0..=rows { + for j in 0..=cols { + let (p, n, uv) = f(i, j); + self.vert(p, n, uv); + } + } + let w = (cols + 1) as u32; + for i in 0..rows as u32 { + for j in 0..cols as u32 { + let a = base + i * w + j; + let (b, c, d) = (a + 1, a + w, a + w + 1); + self.idx.extend_from_slice(&[a, c, d, a, d, b]); + } + } + } + + fn finish(self, material: &str) -> Mesh { + let count = self.idx.len() as u32; + let mut m = Mesh { + positions: self.p, + normals: self.n, + uvs: self.uv, + indices: self.idx, + tangents: Vec::new(), + colors: Vec::new(), + submeshes: vec![SubMesh { name: material.into(), material: 0, start: 0, count }], + materials: vec![material.into()], + }; + m.compute_tangents(); + m + } +} + +/// UV sphere of `radius` centred at `center`. +pub fn sphere(radius: f32, segments: usize, rings: usize) -> Mesh { + let mut b = Builder::default(); + sphere_into(&mut b, Vec3::ZERO, radius, segments.max(3), rings.max(2), UvRect::FULL); + b.finish("Sphere") +} + +fn sphere_into(b: &mut Builder, center: Vec3, radius: f32, segs: usize, rings: usize, r: UvRect) { + b.grid(rings, segs, |i, j| { + let (u, v) = (j as f32 / segs as f32, i as f32 / rings as f32); + let (theta, phi) = (v * PI, u * TAU + PI * 1.5); + let n = Vec3::new(theta.sin() * phi.cos(), theta.cos(), -theta.sin() * phi.sin()); + (center + n * radius, n, r.map(u, v)) + }); +} + +/// A box with rounded edges (`bevel` = edge radius; 0 gives a plain cube). Faces are laid out +/// in a 3×2 UV atlas. +pub fn rounded_box(half: Vec3, bevel: f32) -> Mesh { + let mut b = Builder::default(); + rounded_box_into(&mut b, Vec3::ZERO, half, bevel, UvRect::FULL); + b.finish("Box") +} + +/// The cube primitive (sharp edges). +pub fn cube(size: f32) -> Mesh { + let mut m = rounded_box(Vec3::splat(size * 0.5), 0.0); + m.materials = vec!["Cube".into()]; + if let Some(s) = m.submeshes.first_mut() { + s.name = "Cube".into(); + } + m +} + +fn rounded_box_into(b: &mut Builder, center: Vec3, half: Vec3, bevel: f32, r: UvRect) { + let bevel = bevel.clamp(0.0, half.min_element() * 0.95); + // (normal, U = right, V = down) per face, seen from outside. + let faces = [ + (Vec3::Z, Vec3::X, -Vec3::Y), + (Vec3::X, -Vec3::Z, -Vec3::Y), + (-Vec3::Z, -Vec3::X, -Vec3::Y), + (-Vec3::X, Vec3::Z, -Vec3::Y), + (Vec3::Y, Vec3::X, Vec3::Z), + (-Vec3::Y, Vec3::X, -Vec3::Z), + ]; + let (kb, kc) = if bevel > 0.0 { (6usize, 10usize) } else { (0, 12) }; + // Coordinates along one face axis from -h to h, dense in the bevels. + let coords = |h: f32| -> Vec { + let inner = h - bevel; + let mut v = Vec::new(); + for i in 0..kb { + v.push(-h + bevel * i as f32 / kb as f32); + } + for i in 0..=kc { + v.push(-inner + 2.0 * inner * i as f32 / kc as f32); + } + for i in 1..=kb { + v.push(inner + bevel * i as f32 / kb as f32); + } + v + }; + let inner = half - Vec3::splat(bevel); + for (f, (n, u, v)) in faces.iter().enumerate() { + let cell = UvRect { o: r.map((f % 3) as f32 / 3.0, (f / 3) as f32 / 2.0), s: r.s * Vec2::new(1.0 / 3.0, 0.5) }.inset(0.01 * r.s.x); + let (hu, hv, hn) = ((*u * half).abs().max_element(), (*v * half).abs().max_element(), (*n * half).abs().max_element()); + let (cu, cv) = (coords(hu), coords(hv)); + let (cols, rows) = (cu.len() - 1, cv.len() - 1); + b.grid(rows, cols, |i, j| { + let (a, c) = (cu[j], cv[i]); + let q = *n * hn + *u * a + *v * c; + let clamped = q.clamp(-inner, inner); + let dir = (q - clamped).try_normalize().unwrap_or(*n); + let p = if bevel > 0.0 { clamped + dir * bevel } else { q }; + let nn = if bevel > 0.0 { dir } else { *n }; + (center + p, nn, cell.map((a + hu) / (2.0 * hu), (c + hv) / (2.0 * hv))) + }); + } +} + +/// Surface of revolution of a profile `(radius, y)` listed top → bottom, mapped into `r` +/// (u around, v down the profile by arc length). +fn lathe_into(b: &mut Builder, center: Vec3, profile: &[(f32, f32)], segs: usize, r: UvRect) { + if profile.len() < 2 { + return; + } + let mut len = vec![0.0f32]; + for w in profile.windows(2) { + let d = Vec2::new(w[1].0 - w[0].0, w[1].1 - w[0].1).length(); + len.push(len.last().copied().unwrap_or(0.0) + d); + } + let total = len.last().copied().unwrap_or(1.0).max(1e-6); + let rows = profile.len() - 1; + b.grid(rows, segs, |i, j| { + let (rad, y) = profile[i]; + // Profile tangent (downwards) → outward normal in the (r, y) plane. + let prev = profile[i.saturating_sub(1)]; + let next = profile[(i + 1).min(rows)]; + let t = Vec2::new(next.0 - prev.0, next.1 - prev.1).try_normalize().unwrap_or(Vec2::NEG_Y); + let n2 = Vec2::new(-t.y, t.x); + let u = j as f32 / segs as f32; + let phi = u * TAU + PI * 1.5; + let dir = Vec3::new(phi.cos(), 0.0, -phi.sin()); + let p = center + dir * rad + Vec3::Y * y; + let n = dir * n2.x + Vec3::Y * n2.y; + (p, n, r.map(u, len[i] / total)) + }); +} + +/// A flat disk facing up (+Y) or down, with planar UVs inside `r`. +fn disk_into(b: &mut Builder, center: Vec3, radius: f32, up: bool, segs: usize, r: UvRect) { + let n = if up { Vec3::Y } else { Vec3::NEG_Y }; + let c = b.vert(center, n, r.map(0.5, 0.5)); + let first = b.p.len() as u32; + for j in 0..segs { + let a = j as f32 / segs as f32 * TAU; + let (x, z) = (a.cos(), a.sin()); + // Top view: +X right, +Z down the texture; bottom view mirrors X. + let uv = if up { r.map(0.5 + x * 0.5, 0.5 + z * 0.5) } else { r.map(0.5 + x * 0.5, 0.5 - z * 0.5) }; + b.vert(center + Vec3::new(x, 0.0, z) * radius, n, uv); + } + for j in 0..segs as u32 { + let (a, bb) = (first + j, first + (j + 1) % segs as u32); + if up { + b.idx.extend_from_slice(&[c, bb, a]); + } else { + b.idx.extend_from_slice(&[c, a, bb]); + } + } +} + +/// A capped cylinder along Y. +pub fn cylinder(radius: f32, height: f32, segs: usize) -> Mesh { + let mut b = Builder::default(); + let h = height * 0.5; + lathe_into(&mut b, Vec3::ZERO, &[(radius, h), (radius, -h)], segs.max(3), UvRect { o: Vec2::ZERO, s: Vec2::new(1.0, 0.5) }.inset(0.01)); + disk_into(&mut b, Vec3::Y * h, radius, true, segs.max(3), UvRect { o: Vec2::new(0.0, 0.5), s: Vec2::splat(0.5) }.inset(0.01)); + disk_into(&mut b, -Vec3::Y * h, radius, false, segs.max(3), UvRect { o: Vec2::new(0.5, 0.5), s: Vec2::splat(0.5) }.inset(0.01)); + b.finish("Cylinder") +} + +/// A torus around Y. +pub fn torus(major: f32, minor: f32, segs: usize, sides: usize) -> Mesh { + let mut b = Builder::default(); + let (segs, sides) = (segs.max(3), sides.max(3)); + b.grid(sides, segs, |i, j| { + let (u, v) = (j as f32 / segs as f32, i as f32 / sides as f32); + let phi = u * TAU + PI * 1.5; + let theta = v * TAU; + let dir = Vec3::new(phi.cos(), 0.0, -phi.sin()); + let n = dir * theta.cos() - Vec3::Y * theta.sin(); + (dir * major + n * minor, n, Vec2::new(u, v)) + }); + b.finish("Torus") +} + +/// A subdivided plane facing +Y. +pub fn plane(size: f32, subdivisions: usize) -> Mesh { + let mut b = Builder::default(); + let s = subdivisions.max(1); + b.grid(s, s, |i, j| { + let (u, v) = (j as f32 / s as f32, i as f32 / s as f32); + (Vec3::new((u - 0.5) * size, 0.0, (v - 0.5) * size), Vec3::Y, Vec2::new(u, v)) + }); + b.finish("Plane") +} + +/// The default project mesh: a ball on a beveled pedestal, two materials ("Ball", "Base"). +pub fn material_ball() -> Mesh { + let mut ball = Builder::default(); + sphere_into(&mut ball, Vec3::new(0.0, 1.32, 0.0), 1.0, 128, 64, UvRect::FULL); + let mut ball = ball.finish("Ball"); + + let mut base = Builder::default(); + let (r, top, bot, bev) = (1.05f32, 0.32f32, 0.0f32, 0.07f32); + let mut profile = vec![(0.42, top + 0.06), (0.62, top + 0.06)]; + // Collar bevel and the ring the ball sits on, then the beveled drum. + for k in 1..=6 { + let a = k as f32 / 6.0 * PI * 0.5; + profile.push((0.62 + 0.06 * a.sin(), top + 0.06 * a.cos())); + } + profile.push((r - bev, top)); + for k in 1..=6 { + let a = k as f32 / 6.0 * PI * 0.5; + profile.push((r - bev + bev * a.sin(), top - bev + bev * a.cos())); + } + profile.push((r, bot + bev)); + for k in 1..=6 { + let a = k as f32 / 6.0 * PI * 0.5; + profile.push((r - bev + bev * a.cos(), bot + bev - bev * a.sin())); + } + lathe_into(&mut base, Vec3::ZERO, &profile, 128, UvRect { o: Vec2::ZERO, s: Vec2::new(1.0, 0.5) }.inset(0.01)); + disk_into(&mut base, Vec3::new(0.0, top + 0.06, 0.0), 0.42, true, 128, UvRect { o: Vec2::new(0.0, 0.5), s: Vec2::splat(0.5) }.inset(0.01)); + disk_into(&mut base, Vec3::new(0.0, bot, 0.0), r - bev, false, 128, UvRect { o: Vec2::new(0.5, 0.5), s: Vec2::splat(0.5) }.inset(0.01)); + let base = base.finish("Base"); + ball.append(&base); + ball +} + +/// A small showcase scene: a beveled crate with a rounded lid and a cylinder handle, in three +/// materials (used for demo projects and tests of multiple texture sets). +pub fn crate_scene() -> Mesh { + let mut body = Builder::default(); + rounded_box_into(&mut body, Vec3::new(0.0, 0.8, 0.0), Vec3::new(1.2, 0.8, 0.8), 0.12, UvRect::FULL); + let mut m = body.finish("Crate"); + let mut lid = Builder::default(); + rounded_box_into(&mut lid, Vec3::new(0.0, 1.72, 0.0), Vec3::new(1.28, 0.12, 0.88), 0.08, UvRect::FULL); + m.append(&lid.finish("Lid")); + let mut handle = torus(0.32, 0.07, 64, 24); + for p in &mut handle.positions { + *p = Vec3::new(p.x, p.z, -p.y) + Vec3::new(0.0, 1.84, 0.0); + } + for n in &mut handle.normals { + *n = Vec3::new(n.x, n.z, -n.y); + } + handle.materials = vec!["Handle".into()]; + if let Some(s) = handle.submeshes.first_mut() { + s.name = "Handle".into(); + } + handle.compute_tangents(); + m.append(&handle); + m +} + +/// Built-in mesh by id (`builtin:` in project files). +pub fn builtin(id: &str) -> Option { + Some(match id { + "material_ball" | "materialball" | "ball" => material_ball(), + "sphere" => sphere(1.0, 128, 64), + "cube" => cube(2.0), + "rounded_cube" | "roundedcube" | "box" => rounded_box(Vec3::ONE, 0.15), + "cylinder" => cylinder(1.0, 2.0, 96), + "torus" => torus(1.0, 0.35, 128, 48), + "plane" => plane(2.0, 16), + "crate" => crate_scene(), + _ => return None, + }) +} + +/// Ids accepted by [`builtin`], in menu order. +pub const BUILTINS: [(&str, &str); 8] = [ + ("material_ball", "Material ball"), + ("sphere", "Sphere"), + ("cube", "Cube"), + ("rounded_cube", "Rounded cube"), + ("cylinder", "Cylinder"), + ("torus", "Torus"), + ("plane", "Plane"), + ("crate", "Crate"), +]; + +#[cfg(test)] +mod tests { + use super::*; + + /// Every triangle's winding agrees with its vertex normals, UVs stay in 0..1, and the UV + /// mapping is not mirrored (tangent handedness +1). + fn check(m: &Mesh) { + m.validate().unwrap(); + let mut bad = 0; + for t in 0..m.triangle_count() { + let [a, b, c] = m.tri(t).unwrap(); + let face = (m.positions[b] - m.positions[a]).cross(m.positions[c] - m.positions[a]); + if face.length() < 1e-9 { + continue; + } + let n = m.normals[a] + m.normals[b] + m.normals[c]; + if face.dot(n) <= 0.0 { + bad += 1; + } + } + assert_eq!(bad, 0, "{} triangles wound against their normals", bad); + for uv in &m.uvs { + assert!((0.0..=1.0).contains(&uv.x) && (0.0..=1.0).contains(&uv.y), "{uv:?}"); + } + let mirrored = m.tangents.iter().filter(|t| t.w < 0.0).count(); + assert!(mirrored * 50 < m.tangents.len().max(1), "{mirrored} mirrored tangents"); + } + + #[test] + fn primitives_are_well_formed() { + for (id, _) in BUILTINS { + let m = builtin(id).unwrap(); + assert!(m.triangle_count() > 0, "{id}"); + check(&m); + } + assert!(builtin("nope").is_none()); + } + + #[test] + fn material_ball_has_two_materials() { + let m = material_ball(); + assert_eq!(m.materials, vec!["Ball".to_string(), "Base".to_string()]); + assert!(m.triangles_of(0).count() > 0 && m.triangles_of(1).count() > 0); + } +} diff --git a/crates/geom/src/unwrap.rs b/crates/geom/src/unwrap.rs new file mode 100644 index 00000000..2f2eafc0 --- /dev/null +++ b/crates/geom/src/unwrap.rs @@ -0,0 +1,384 @@ +//! Automatic UV unwrapping for meshes imported without texture coordinates. +//! +//! Triangles are grouped into charts by flood fill across shared (welded) edges while each face +//! normal stays within 60° of the chart's seed face, so every chart is a height field over its +//! seed plane and projects onto it without flipping. Charts are rotated to their principal axes +//! and shelf-packed per material into 0..1 with one uniform scale, so texel density is the same +//! everywhere within a material. + +use std::collections::{HashMap, VecDeque}; + +use glam::{Vec2, Vec3}; + +use crate::{GeomError, Mesh, Result}; + +/// Faces of a chart stay within this angle of the seed face normal (cos 60°). +const CHART_COS: f32 = 0.5; +/// Packing never goes beyond this virtual resolution (texels per side). +const MAX_PACK_RES: f32 = 65_536.0; + +/// Welded vertex id per vertex: vertices at the same position (snapped to a grid of 1e-6 of +/// the bounding diagonal) share an id. Ids are dense, in order of first appearance. +pub fn weld_ids(positions: &[Vec3]) -> Vec { + let (lo, hi) = positions.iter().fold((Vec3::splat(f32::MAX), Vec3::splat(f32::MIN)), |(lo, hi), p| (lo.min(*p), hi.max(*p))); + let cell = ((hi - lo).length() * 1e-6).max(1e-12); + let mut map: HashMap<[i64; 3], u32> = HashMap::with_capacity(positions.len()); + positions + .iter() + .map(|p| { + let q = ((*p - lo) / cell).round(); + let next = map.len() as u32; + *map.entry([q.x as i64, q.y as i64, q.z as i64]).or_insert(next) + }) + .collect() +} + +/// True when the mesh has triangles and every one of them has two corners at the same position. +/// MikkTSpace (and so [`Mesh::compute_tangents`]) crashes on such meshes, so callers reject them. +pub fn all_triangles_degenerate(mesh: &Mesh) -> bool { + let p = |i: &u32| mesh.positions.get(*i as usize); + !mesh.indices.is_empty() + && mesh.indices.chunks(3).all(|t| match t { + [a, b, c] => p(a) == p(b) || p(a) == p(c) || p(b) == p(c), + _ => true, + }) +} + +struct Chart { + material: usize, + /// Projection basis (right-handed with the seed normal). + t: Vec3, + b: Vec3, + /// Rotation to the chart's principal axes (cos, sin). + rot: Vec2, + lo: Vec2, + hi: Vec2, + /// Texel offset of the chart's packed rectangle and the material's scale and resolution. + offset: Vec2, +} + +impl Chart { + fn local(&self, p: Vec3) -> Vec2 { + let q = Vec2::new(p.dot(self.t), p.dot(self.b)); + Vec2::new(q.x * self.rot.x - q.y * self.rot.y, q.x * self.rot.y + q.y * self.rot.x) + } + fn size(&self) -> Vec2 { + (self.hi - self.lo).max(Vec2::ZERO) + } +} + +/// Replace the UVs of `mesh` with an automatic non-overlapping layout: each material gets its +/// own 0..1 space with charts at least `padding_texels` (minimum 1) away from each other and +/// the borders at `resolution`. Vertices are split at chart borders; tangents are recomputed. +/// Returns the number of charts. +pub fn auto_unwrap(mesh: &mut Mesh, padding_texels: f32, resolution: u32) -> Result { + mesh.validate_indices()?; + if all_triangles_degenerate(mesh) { + return Err(GeomError::Invalid("every triangle of the mesh is degenerate".into())); + } + if mesh.normals.len() != mesh.positions.len() { + mesh.compute_normals(); + } + let n = mesh.triangle_count(); + let weld = weld_ids(&mesh.positions); + let pos = |v: usize| mesh.positions.get(v).copied().unwrap_or_default(); + let wid = |v: usize| weld.get(v).copied().unwrap_or(u32::MAX); + + // Face normals (None for degenerate faces) and materials. + let mut normal: Vec> = Vec::with_capacity(n); + let mut area = Vec::with_capacity(n); + for t in 0..n { + let [a, b, c] = mesh.tri(t).unwrap_or([0; 3]); + let cr = (pos(b) - pos(a)).cross(pos(c) - pos(a)); + area.push(cr.length() * 0.5); + normal.push(cr.try_normalize()); + } + let mut material = vec![0usize; n]; + for s in &mesh.submeshes { + for t in (s.start / 3) as usize..((s.start as usize + s.count as usize) / 3) { + if let Some(m) = material.get_mut(t) { + *m = s.material; + } + } + } + + // Manifold edge adjacency over welded vertices: edge → (face, face, count). + let mut edges: HashMap<(u32, u32), (u32, u32, u8)> = HashMap::with_capacity(n * 2); + let tri_edges = |t: usize| -> [(u32, u32); 3] { + let [a, b, c] = mesh.tri(t).unwrap_or([0; 3]); + let key = |x: usize, y: usize| { + let (p, q) = (wid(x), wid(y)); + (p.min(q), p.max(q)) + }; + [key(a, b), key(b, c), key(c, a)] + }; + for t in 0..n { + for e in tri_edges(t) { + if e.0 == e.1 { + continue; + } + let slot = edges.entry(e).or_insert((t as u32, u32::MAX, 0)); + if slot.2 == 1 { + slot.1 = t as u32; + } + slot.2 = slot.2.saturating_add(1); + } + } + + // Grow charts from the largest unassigned faces. + let mut order: Vec = (0..n).collect(); + order.sort_by(|a, b| area[*b].total_cmp(&area[*a])); + let mut chart_of = vec![u32::MAX; n]; + let mut charts: Vec = Vec::new(); + let mut queue = VecDeque::new(); + for &seed in &order { + if chart_of[seed] != u32::MAX { + continue; + } + let id = charts.len() as u32; + let sn = normal[seed].unwrap_or(Vec3::Z); + let t = sn.any_orthonormal_vector(); + charts.push(Chart { material: material[seed], t, b: sn.cross(t), rot: Vec2::X, lo: Vec2::ZERO, hi: Vec2::ZERO, offset: Vec2::ZERO }); + chart_of[seed] = id; + queue.push_back(seed); + while let Some(f) = queue.pop_front() { + for e in tri_edges(f) { + let Some(&(f0, f1, 2)) = edges.get(&e) else { continue }; + let g = if f0 as usize == f { f1 } else { f0 } as usize; + let ok = normal.get(g).is_some_and(|nn| nn.is_none_or(|nn| nn.dot(sn) >= CHART_COS)); + if ok && chart_of.get(g) == Some(&u32::MAX) && material.get(g) == Some(&material[seed]) { + chart_of[g] = id; + queue.push_back(g); + } + } + } + } + + // Principal axes and bounds of every chart. + let mut cov = vec![(Vec2::ZERO, [0.0f64; 3], 0usize); charts.len()]; + for (t, &c) in chart_of.iter().enumerate() { + let c = c as usize; + for v in mesh.tri(t).unwrap_or([0; 3]) { + if let (Some(ch), Some(acc)) = (charts.get(c), cov.get_mut(c)) { + let q = ch.local(pos(v)); + acc.0 += q; + acc.1[0] += (q.x * q.x) as f64; + acc.1[1] += (q.y * q.y) as f64; + acc.1[2] += (q.x * q.y) as f64; + acc.2 += 1; + } + } + } + for (ch, (sum, m, k)) in charts.iter_mut().zip(&cov) { + let k = (*k).max(1) as f64; + let (mx, my) = (sum.x as f64 / k, sum.y as f64 / k); + let (cxx, cyy, cxy) = (m[0] / k - mx * mx, m[1] / k - my * my, m[2] / k - mx * my); + let angle = 0.5 * (2.0 * cxy).atan2(cxx - cyy); + // Rotate so the principal axis lies along x. + ch.rot = Vec2::new((-angle).cos() as f32, (-angle).sin() as f32); + ch.lo = Vec2::splat(f32::MAX); + ch.hi = Vec2::splat(f32::MIN); + } + for (t, &c) in chart_of.iter().enumerate() { + let c = c as usize; + for v in mesh.tri(t).unwrap_or([0; 3]) { + if let Some(ch) = charts.get_mut(c) { + let q = ch.local(pos(v)); + ch.lo = ch.lo.min(q); + ch.hi = ch.hi.max(q); + } + } + } + for ch in &mut charts { + if ch.size().y > ch.size().x { + // Quarter turn (x, y) → (y, −x) so shelves stay low. + ch.rot = Vec2::new(ch.rot.y, -ch.rot.x); + let (lo, hi) = (ch.lo, ch.hi); + ch.lo = Vec2::new(lo.y, -hi.x); + ch.hi = Vec2::new(hi.y, -lo.x); + } + } + + // Pack each material's charts into its own 0..1 square. + let pad = padding_texels.max(1.0); + let mut scale = HashMap::new(); + let mats: std::collections::BTreeSet = charts.iter().map(|c| c.material).collect(); + for m in mats { + let ids: Vec = (0..charts.len()).filter(|i| charts[*i].material == m).collect(); + let sizes: Vec = ids.iter().map(|i| charts[*i].size()).collect(); + let (s, res, offsets) = pack(&sizes, resolution.max(16) as f32, pad)?; + for (i, o) in ids.iter().zip(offsets) { + charts[*i].offset = o; + } + scale.insert(m, (s, res)); + } + + // Rebuild vertices: one per (vertex, chart). + let mut remap: HashMap<(u32, u32), u32> = HashMap::with_capacity(mesh.positions.len() * 2); + let (mut positions, mut normals, mut uvs, mut colors) = (Vec::new(), Vec::new(), Vec::new(), Vec::new()); + let mut indices = Vec::with_capacity(mesh.indices.len()); + for (t, &c) in chart_of.iter().enumerate() { + let Some(ch) = charts.get(c as usize) else { continue }; + let (s, res) = scale.get(&ch.material).copied().unwrap_or((0.0, 1.0)); + for v in mesh.tri(t).unwrap_or([0; 3]) { + let idx = *remap.entry((v as u32, c)).or_insert_with(|| { + let q = ch.local(pos(v)); + // Chart y points up; UV v points down the image. + let texel = ch.offset + Vec2::splat(pad) + Vec2::new(q.x - ch.lo.x, ch.hi.y - q.y) * s; + positions.push(pos(v)); + normals.push(mesh.normals.get(v).copied().unwrap_or(Vec3::Y)); + if let Some(c) = mesh.colors.get(v) { + colors.push(*c); + } + uvs.push((texel / res).clamp(Vec2::ZERO, Vec2::ONE)); + (positions.len() - 1) as u32 + }); + indices.push(idx); + } + } + mesh.positions = positions; + mesh.normals = normals; + mesh.uvs = uvs; + mesh.colors = colors; + mesh.indices = indices; + mesh.compute_tangents(); + mesh.validate()?; + Ok(charts.len()) +} + +/// Shelf-pack rectangles of world `sizes` (each grown by `pad` texels per side) into a square of +/// `res` texels with the largest uniform scale that fits. Returns (texels per world unit, +/// resolution used, texel offset per rectangle). The resolution doubles when the padding alone +/// cannot fit (very many charts), so the result always fits in 0..1. +fn pack(sizes: &[Vec2], mut res: f32, pad: f32) -> Result<(f32, f32, Vec)> { + let mut order: Vec = (0..sizes.len()).collect(); + order.sort_by(|a, b| sizes[*b].y.total_cmp(&sizes[*a].y)); + let place = |s: f32, res: f32| -> Option> { + let mut out = vec![Vec2::ZERO; sizes.len()]; + let (mut x, mut y, mut shelf) = (0.0f32, 0.0f32, 0.0f32); + for &i in &order { + let r = sizes[i] * s + Vec2::splat(2.0 * pad); + if r.x > res { + return None; + } + if x + r.x > res { + y += shelf; + x = 0.0; + shelf = 0.0; + } + if y + r.y > res { + return None; + } + out[i] = Vec2::new(x, y); + x += r.x; + shelf = shelf.max(r.y); + } + Some(out) + }; + while place(0.0, res).is_none() { + res *= 2.0; + if res > MAX_PACK_RES { + return Err(GeomError::Invalid("too many UV charts to pack".into())); + } + } + let largest = sizes.iter().map(|s| s.max_element()).fold(0.0f32, f32::max); + if largest <= 0.0 { + return Ok((0.0, res, place(0.0, res).unwrap_or_default())); + } + let (mut lo, mut hi) = (0.0f32, (res - 2.0 * pad).max(0.0) / largest); + if place(hi, res).is_some() { + lo = hi; + } + for _ in 0..40 { + if hi - lo <= lo * 1e-4 { + break; + } + let mid = 0.5 * (lo + hi); + if place(mid, res).is_some() { lo = mid } else { hi = mid } + } + Ok((lo, res, place(lo, res).unwrap_or_default())) +} + +impl Mesh { + /// Index-range checks only (attribute arrays may be incomplete before unwrapping). + fn validate_indices(&self) -> Result<()> { + let n = self.positions.len(); + if !self.indices.len().is_multiple_of(3) || self.indices.iter().any(|i| *i as usize >= n) { + return Err(GeomError::Invalid("mesh indices are out of range".into())); + } + if self.triangle_count() > crate::MAX_TRIANGLES { + return Err(GeomError::Invalid(format!("mesh has more than {} triangles", crate::MAX_TRIANGLES))); + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{UvGBuffer, primitives}; + + fn check(mut m: Mesh, min_coverage: f32) { + m.uvs.clear(); + let charts = auto_unwrap(&mut m, 4.0, 512).unwrap(); + assert!(charts >= 1); + m.validate().unwrap(); + assert!(m.uvs.iter().all(|uv| (0.0..=1.0).contains(&uv.x) && (0.0..=1.0).contains(&uv.y))); + let g = UvGBuffer::new(&m, 0, 512, 512); + let covered = g.covered_count() as f32; + let coverage = covered / (512.0 * 512.0); + assert!(coverage > min_coverage, "coverage {coverage} with {charts} charts"); + assert!((g.overlaps as f32) < covered * 0.005, "overlaps {} of {covered}", g.overlaps); + } + + #[test] + fn sphere_unwraps_without_overlaps() { + check(primitives::sphere(1.0, 48, 24), 0.3); + } + + #[test] + fn cube_unwraps_without_overlaps() { + check(primitives::cube(2.0), 0.3); + } + + #[test] + fn torus_and_material_ball_unwrap() { + check(primitives::torus(1.0, 0.3, 48, 24), 0.2); + let mut ball = primitives::material_ball(); + ball.uvs.clear(); + auto_unwrap(&mut ball, 2.0, 1024).unwrap(); + for m in 0..ball.materials.len() { + let g = UvGBuffer::new(&ball, m, 512, 512); + assert!((g.overlaps as f32) < g.covered_count() as f32 * 0.005 + 1.0, "material {m}: {} overlaps", g.overlaps); + } + } + + #[test] + fn many_tiny_charts_still_fit() { + // 2000 disconnected triangles: the packer grows its virtual resolution. + let mut p = Vec::new(); + let mut idx = Vec::new(); + for i in 0..2000u32 { + let o = Vec3::new(i as f32 * 3.0, 0.0, 0.0); + p.extend([o, o + Vec3::X, o + Vec3::Y]); + idx.extend([i * 3, i * 3 + 1, i * 3 + 2]); + } + let mut m = Mesh::from_parts(p, None, Vec::new(), idx, "m").unwrap(); + auto_unwrap(&mut m, 4.0, 128).unwrap(); + assert!(m.uvs.iter().all(|uv| (0.0..=1.0).contains(&uv.x) && (0.0..=1.0).contains(&uv.y))); + } + + #[test] + fn degenerate_and_empty_meshes_do_not_fail() { + let mut empty = Mesh::default(); + assert_eq!(auto_unwrap(&mut empty, 4.0, 256).unwrap(), 0); + // A sliver (zero-area) triangle next to a real one. + let p = vec![Vec3::ZERO, Vec3::X, Vec3::Y, Vec3::X * 2.0]; + let mut m = Mesh::from_parts(p, None, Vec::new(), vec![0, 1, 2, 0, 1, 3], "m").unwrap(); + auto_unwrap(&mut m, 4.0, 256).unwrap(); + m.validate().unwrap(); + let mut point = Mesh { positions: vec![Vec3::ONE; 3], indices: vec![0, 1, 2], ..Mesh::default() }; + assert!(all_triangles_degenerate(&point)); + assert!(auto_unwrap(&mut point, 4.0, 256).is_err()); + } +} diff --git a/crates/paint/Cargo.toml b/crates/paint/Cargo.toml new file mode 100644 index 00000000..6cc99089 --- /dev/null +++ b/crates/paint/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "texturecraft-paint" +description = "TextureCraft brush engine: strokes, stamps, visibility, texture-space rasterization, commit" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lints] +workspace = true + +[dependencies] +texturecraft-core = { workspace = true } +texturecraft-texel = { workspace = true } +texturecraft-geom = { workspace = true } +texturecraft-compose = { workspace = true } +rayon = { workspace = true } +serde = { workspace = true } diff --git a/crates/paint/src/brush.rs b/crates/paint/src/brush.rs new file mode 100644 index 00000000..6649c1b1 --- /dev/null +++ b/crates/paint/src/brush.rs @@ -0,0 +1,289 @@ +//! Brush settings and alpha shapes. + +use serde::{Deserialize, Serialize}; +use texturecraft_compose::ImageRes; +use texturecraft_texel::noise; + +/// How the stamp plane is oriented. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum Alignment { + /// Along the surface (tangent to the hit normal). + #[default] + Tangent, + /// Facing the camera (stable on noisy geometry). + Camera, +} + +/// The stamp shape (multiplied by the hardness falloff). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum BrushAlpha { + #[default] + Round, + Square, + /// Speckled spray. + Spray, + /// Organic grunge blotches. + Grunge, + /// Soft cloud. + Cloud, + /// Five-point star. + Star, + /// Horizontal streaks (dry brush). + Streaks, +} + +impl BrushAlpha { + pub const ALL: [BrushAlpha; 7] = + [BrushAlpha::Round, BrushAlpha::Square, BrushAlpha::Spray, BrushAlpha::Grunge, BrushAlpha::Cloud, BrushAlpha::Star, BrushAlpha::Streaks]; + + pub fn label(self) -> &'static str { + match self { + BrushAlpha::Round => "Round", + BrushAlpha::Square => "Square", + BrushAlpha::Spray => "Spray", + BrushAlpha::Grunge => "Grunge", + BrushAlpha::Cloud => "Cloud", + BrushAlpha::Star => "Star", + BrushAlpha::Streaks => "Dry streaks", + } + } + + pub fn parse(s: &str) -> Option { + let n = |x: &str| x.to_ascii_lowercase().replace([' ', '_', '-'], ""); + BrushAlpha::ALL.into_iter().find(|a| n(a.label()) == n(s) || n(&format!("{a:?}")) == n(s)) + } + + /// Shape value at local stamp coordinates `l` (unit disc/square, −1..1), seeded per stamp. + pub fn sample(self, lx: f32, ly: f32, hardness: f32, seed: u32) -> f32 { + let r = (lx * lx + ly * ly).sqrt(); + let fall = |d: f32| 1.0 - smoothstep(hardness.min(0.999), 1.0, d); + match self { + BrushAlpha::Round => fall(r), + BrushAlpha::Square => fall(lx.abs().max(ly.abs())), + BrushAlpha::Spray => { + let n = noise::value([lx * 14.0, ly * 14.0, seed as f32 * 0.37], seed); + fall(r) * if n > 0.72 { 1.0 } else { 0.0 } + } + BrushAlpha::Grunge => { + let n = noise::fbm([lx * 2.5 + seed as f32 * 0.13, ly * 2.5, 0.5], 5, seed, noise::perlin); + fall(r) * ((n - 0.42) * 5.0).clamp(0.0, 1.0) + } + BrushAlpha::Cloud => fall(r) * noise::fbm([lx * 1.8, ly * 1.8, seed as f32 * 0.21], 4, seed, noise::value), + BrushAlpha::Star => { + let a = ly.atan2(lx); + let k = (a * 5.0 / 2.0).cos().abs(); + let edge = 0.45 + 0.55 * k * k; + fall(r / edge) + } + BrushAlpha::Streaks => { + let n = noise::value([lx * 0.8, ly * 18.0, seed as f32 * 0.7], seed); + fall(r) * ((n - 0.35) * 3.0).clamp(0.0, 1.0) + } + } + } +} + +/// A stamp shape ready to sample: the procedural alpha, or an image alpha when one is set. +#[derive(Clone, Copy)] +pub struct Shape<'a> { + pub alpha: BrushAlpha, + pub image: Option<&'a ImageRes>, +} + +impl Shape<'_> { + /// Shape value at local stamp coordinates (see [`BrushAlpha::sample`]). + pub fn sample(&self, lx: f32, ly: f32, hardness: f32, seed: u32) -> f32 { + match self.image { + Some(img) => image_alpha(img, lx, ly), + None => self.alpha.sample(lx, ly, hardness, seed), + } + } +} + +/// An image as a brush alpha: luminance × alpha over the stamp square (−1..1, y up), 0 outside. +pub fn image_alpha(img: &ImageRes, lx: f32, ly: f32) -> f32 { + if !(lx.abs() <= 1.0 && ly.abs() <= 1.0) { + return 0.0; + } + let [r, g, b, a] = img.sample(lx * 0.5 + 0.5, 0.5 - ly * 0.5); + ((0.2126 * r + 0.7152 * g + 0.0722 * b) * a).clamp(0.0, 1.0) +} + +fn smoothstep(e0: f32, e1: f32, x: f32) -> f32 { + let t = ((x - e0) / (e1 - e0).max(1e-6)).clamp(0.0, 1.0); + t * t * (3.0 - 2.0 * t) +} + +/// Mirror symmetry through the mesh center. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub struct Symmetry { + pub x: bool, + pub y: bool, + pub z: bool, +} + +impl Symmetry { + pub fn any(&self) -> bool { + self.x || self.y || self.z + } +} + +/// Particle brush: every stamp emits particles that slide down the surface under gravity +/// (world −Y), painting small dabs along their tracks instead of the stamp itself. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +pub struct Particles { + pub enabled: bool, + /// Particles per stamp. + pub count: u32, + /// Simulation steps per particle (one dab each). + pub lifetime: u32, + /// Distance slid per step, relative to the particle size (0 = particles stay put). + pub gravity: f32, +} + +impl Default for Particles { + fn default() -> Self { + Particles { enabled: false, count: 4, lifetime: 24, gravity: 1.0 } + } +} + +/// Brush parameters (the Painter "Brush" properties section plus tool-bar values). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct BrushSettings { + /// Radius as a fraction of the mesh bounding-box diagonal (the UI shows 0–100). + pub size: f32, + /// Pen pressure may shrink the size down to this fraction. + pub min_size: f32, + /// Per-stamp alpha 0..1. + pub flow: f32, + /// Caps a whole stroke 0..1. + pub opacity: f32, + /// Distance between stamps as a fraction of the diameter. + pub spacing: f32, + /// Start of the edge falloff (0 soft … 1 hard). + pub hardness: f32, + /// Stamp rotation, degrees. + pub angle: f32, + pub follow_path: bool, + pub alignment: Alignment, + /// Surfaces facing away more than these angles (degrees) receive less paint. + pub angle_fade: (f32, f32), + /// Lazy mouse distance in screen pixels (0 = off). + pub lazy: f32, + pub jitter_size: f32, + pub jitter_angle: f32, + pub jitter_flow: f32, + /// Position jitter as a fraction of the radius. + pub jitter_position: f32, + pub alpha: BrushAlpha, + /// Image resource used as the stamp shape instead of `alpha` (grayscale, rotated with the stamp). + #[serde(default)] + pub alpha_image: Option, + /// Only paint surfaces facing the camera. + pub backface_culling: bool, + /// Half-depth of the stamp volume relative to the radius (thinner = less bleed into folds). + pub depth: f32, + pub symmetry: Symmetry, + /// Size in screen space (constant on screen) instead of world space. + pub screen_space: bool, + #[serde(default)] + pub particles: Particles, +} + +impl Default for BrushSettings { + fn default() -> Self { + BrushSettings { + size: 0.02, + min_size: 0.05, + flow: 1.0, + opacity: 1.0, + spacing: 0.1, + hardness: 0.5, + angle: 0.0, + follow_path: false, + alignment: Alignment::Tangent, + angle_fade: (70.0, 85.0), + lazy: 0.0, + jitter_size: 0.0, + jitter_angle: 0.0, + jitter_flow: 0.0, + jitter_position: 0.0, + alpha: BrushAlpha::Round, + alpha_image: None, + backface_culling: true, + depth: 1.0, + symmetry: Symmetry::default(), + screen_space: false, + particles: Particles::default(), + } + } +} + +impl BrushSettings { + /// Clamp every value into its valid range (input from UI, CLI or agents). + pub fn sanitized(&self) -> BrushSettings { + let f = |v: f32, lo: f32, hi: f32, d: f32| if v.is_finite() { v.clamp(lo, hi) } else { d }; + BrushSettings { + size: f(self.size, 0.0005, 0.5, 0.02), + min_size: f(self.min_size, 0.0, 1.0, 0.05), + flow: f(self.flow, 0.0, 1.0, 1.0), + opacity: f(self.opacity, 0.0, 1.0, 1.0), + spacing: f(self.spacing, 0.01, 10.0, 0.1), + hardness: f(self.hardness, 0.0, 0.999, 0.5), + angle: f(self.angle, -360.0, 360.0, 0.0), + angle_fade: (f(self.angle_fade.0, 0.0, 90.0, 70.0), f(self.angle_fade.1, 0.0, 90.0, 85.0)), + lazy: f(self.lazy, 0.0, 500.0, 0.0), + jitter_size: f(self.jitter_size, 0.0, 1.0, 0.0), + jitter_angle: f(self.jitter_angle, 0.0, 1.0, 0.0), + jitter_flow: f(self.jitter_flow, 0.0, 1.0, 0.0), + jitter_position: f(self.jitter_position, 0.0, 4.0, 0.0), + depth: f(self.depth, 0.05, 4.0, 1.0), + particles: Particles { + enabled: self.particles.enabled, + count: self.particles.count.clamp(1, 16), + lifetime: self.particles.lifetime.clamp(1, 128), + gravity: f(self.particles.gravity, 0.0, 2.0, 1.0), + }, + ..self.clone() + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn alphas_are_bounded_and_zero_outside() { + for a in BrushAlpha::ALL { + for i in 0..40 { + let t = i as f32 / 20.0 - 1.0; + let v = a.sample(t, t * 0.5, 0.5, 3); + assert!((0.0..=1.0).contains(&v), "{a:?} {v}"); + } + assert_eq!(a.sample(1.5, 1.5, 0.5, 1), 0.0, "{a:?}"); + assert_eq!(BrushAlpha::parse(a.label()), Some(a)); + } + assert!(BrushAlpha::Round.sample(0.0, 0.0, 0.5, 0) > 0.99); + } + + #[test] + fn image_alpha_follows_the_image_and_is_zero_outside() { + // Left half white, right half black (2×1 image, sampled at texel centers). + let img = ImageRes { width: 2, height: 1, rgba: vec![[1.0; 4], [0.0, 0.0, 0.0, 1.0]], srgb: true }; + let shape = Shape { alpha: BrushAlpha::Round, image: Some(&img) }; + assert!(shape.sample(-0.5, 0.0, 0.5, 0) > 0.99); + assert!(shape.sample(0.5, 0.0, 0.5, 0) < 0.01); + assert_eq!(shape.sample(1.5, 0.0, 0.5, 0), 0.0); + assert_eq!(shape.sample(f32::NAN, 0.0, 0.5, 0), 0.0); + } + + #[test] + fn sanitize_fixes_hostile_values() { + let b = BrushSettings { size: f32::NAN, flow: 9.0, hardness: 1.0, spacing: -1.0, ..BrushSettings::default() }.sanitized(); + assert_eq!(b.size, 0.02); + assert_eq!(b.flow, 1.0); + assert!(b.hardness < 1.0); + assert!(b.spacing > 0.0); + } +} diff --git a/crates/paint/src/commit.rs b/crates/paint/src/commit.rs new file mode 100644 index 00000000..7f1bd630 --- /dev/null +++ b/crates/paint/src/commit.rs @@ -0,0 +1,192 @@ +//! Committing a stroke: premultiplied "over" (paint) or scale-down (erase) of the stroke alpha +//! into a layer's channels or a mask's paint effect. Only touched tiles are un-shared, so the +//! previous state stays intact in undo snapshots. + +use std::collections::{BTreeMap, BTreeSet}; + +use texturecraft_compose::PixelStore; +use texturecraft_core::{Channel, ChannelKind, LayerId}; +use texturecraft_texel::{TILE, TILE_AREA, TileImage, from_u16, from_u16_signed, to_u16, to_u16_signed}; + +pub(crate) fn dec(kind: ChannelKind, v: u16) -> f32 { + if kind == ChannelKind::Signed { from_u16_signed(v) } else { from_u16(v) } +} + +pub(crate) fn enc(kind: ChannelKind, v: f32) -> u16 { + if kind == ChannelKind::Signed { to_u16_signed(v) } else { to_u16(v) } +} + +/// Blend `alpha × opacity` into one image of `n` value components plus coverage. With a +/// per-texel source, its alpha factor scales the stroke and, when `stencil_color`, its color +/// replaces `value`; `carried` (clone, smudge) replaces `value` per texel. +fn over(img: &mut TileImage, kind: ChannelKind, value: &[f32], src: &CommitSource, stencil_color: bool, carried: Option<&TileImage>) { + let comps = img.comps(); + let n = comps.saturating_sub(1); + let opacity = src.opacity.clamp(0.0, 1.0); + for &(tx, ty) in src.tiles { + let Some(a) = src.alpha.tile(tx, ty) else { continue }; + if src.erase && !img.is_allocated(tx, ty) { + continue; + } + let carried = match carried.map(|c| c.tile(tx, ty)) { + Some(None) => continue, + Some(Some(t)) => Some(t), + None => None, + }; + // New tiles start empty: zero coverage and a zero value (32768 in signed encoding). + let mut fill = vec![enc(kind, 0.0); n]; + fill.push(0); + let Some(dst) = img.tile_mut(tx, ty, &fill) else { continue }; + for i in 0..TILE_AREA { + let mut m = from_u16(a.get(i).copied().unwrap_or(0)) * opacity; + if m <= 0.0 { + continue; + } + let mut color = None; + if let Some(f) = src.per_texel { + let (x, y) = (tx * TILE + i as u32 % TILE, ty * TILE + i as u32 / TILE); + match f(x, y) { + Some((c, af)) => { + m *= af; + color = Some(c); + } + None => continue, + } + if m <= 0.0 { + continue; + } + } + let Some(px) = dst.get_mut(i * comps..(i + 1) * comps) else { continue }; + let cov = from_u16(px[n]); + for k in 0..n { + let p = dec(kind, px[k]); + let target = match (carried, color) { + (Some(t), _) => dec(kind, t.get(i * n + k).copied().unwrap_or(0)), + (None, Some(c)) if stencil_color => c.get(k).copied().unwrap_or(0.0), + _ => value.get(k).copied().unwrap_or(0.0), + }; + let v = if src.erase { p * (1.0 - m) } else { target * m + p * (1.0 - m) }; + px[k] = enc(kind, v); + } + px[n] = to_u16(if src.erase { cov * (1.0 - m) } else { m + cov * (1.0 - m) }); + } + } +} + +/// Per-texel override of a stroke: `(x, y)` → (base color, alpha factor), None = no paint. +pub type TexelFn<'a> = &'a (dyn Fn(u32, u32) -> Option<([f32; 3], f32)> + Sync); + +/// The stroke being committed: its alpha, touched tiles, opacity cap and mode. +#[derive(Clone, Copy)] +pub struct CommitSource<'a> { + pub alpha: &'a TileImage, + pub tiles: &'a BTreeSet<(u32, u32)>, + pub opacity: f32, + pub erase: bool, + /// Projection tool: base color and alpha per texel from a stencil. + pub per_texel: Option>, + /// Clone and smudge: straight values per channel carried by the stroke (replace `values`). + pub carried: Option<&'a BTreeMap>, +} + +/// Commit a stroke into paint layer `layer` (channels in `values`; erase clears every painted +/// channel of the layer, or only those in `values` when it is not empty). +pub fn commit_layer(pixels: &mut PixelStore, layer: LayerId, resolution: u32, values: &BTreeMap, src: CommitSource) { + let opacity = src.opacity.clamp(0.0, 1.0); + let channels: Vec = if src.erase { + let painted: BTreeSet = pixels.layers.keys().filter(|(id, _)| *id == layer).map(|(_, c)| *c).collect(); + painted.into_iter().filter(|c| values.is_empty() || values.contains_key(c)).collect() + } else { + values.keys().copied().collect() + }; + for c in channels { + let v = values.get(&c).copied().unwrap_or([0.0; 3]); + let img = pixels.layer_mut(layer, c, resolution); + let carried = src.carried.and_then(|m| m.get(&c)); + over(img, c.kind(), &v[..c.components()], &CommitSource { opacity, ..src }, c == Channel::BaseColor, carried); + } +} + +/// Commit a stroke into the paint effect `effect` of a mask, painting gray `value`. +pub fn commit_mask(pixels: &mut PixelStore, effect: LayerId, resolution: u32, value: f32, src: CommitSource) { + let img = pixels.mask_mut(effect, resolution); + over(img, ChannelKind::Scalar, &[value.clamp(0.0, 1.0)], &src, false, None); +} + +/// Commit a stroke into a coverage-only image (1 component), e.g. the quick mask. +pub fn commit_coverage(img: &mut TileImage, src: CommitSource) { + over(img, ChannelKind::Scalar, &[], &src, false, None); +} + +#[cfg(test)] +mod tests { + use super::*; + + fn src<'a>(alpha: &'a TileImage, tiles: &'a BTreeSet<(u32, u32)>, opacity: f32, erase: bool) -> CommitSource<'a> { + CommitSource { alpha, tiles, opacity, erase, per_texel: None, carried: None } + } + + fn alpha(v: f32) -> (TileImage, BTreeSet<(u32, u32)>) { + let mut a = TileImage::new(64, 64, 1); + a.set(5, 5, &[to_u16(v)]); + (a, [(0, 0)].into_iter().collect()) + } + + #[test] + fn paint_then_paint_then_erase_is_premultiplied_over() { + let mut px = PixelStore::default(); + let id = LayerId(1); + let vals: BTreeMap = [(Channel::Roughness, [0.8; 3])].into_iter().collect(); + let (a, tiles) = alpha(0.5); + commit_layer(&mut px, id, 64, &vals, src(&a, &tiles, 1.0, false)); + let t = px.layer(id, Channel::Roughness).unwrap().get(5, 5); + assert!((from_u16(t[0]) - 0.4).abs() < 1e-3, "premul v·a"); + assert!((from_u16(t[1]) - 0.5).abs() < 1e-3, "coverage a"); + commit_layer(&mut px, id, 64, &vals, src(&a, &tiles, 1.0, false)); + let t = px.layer(id, Channel::Roughness).unwrap().get(5, 5); + assert!((from_u16(t[1]) - 0.75).abs() < 1e-3); + assert!((from_u16(t[0]) - 0.6).abs() < 1e-3); + commit_layer(&mut px, id, 64, &BTreeMap::new(), src(&a, &tiles, 1.0, true)); + let t = px.layer(id, Channel::Roughness).unwrap().get(5, 5); + assert!((from_u16(t[1]) - 0.375).abs() < 1e-3); + assert!((from_u16(t[0]) - 0.3).abs() < 1e-3); + } + + #[test] + fn signed_height_and_stroke_opacity() { + let mut px = PixelStore::default(); + let id = LayerId(2); + let vals: BTreeMap = [(Channel::Height, [-0.5; 3])].into_iter().collect(); + let (a, tiles) = alpha(1.0); + commit_layer(&mut px, id, 64, &vals, src(&a, &tiles, 0.5, false)); + let t = px.layer(id, Channel::Height).unwrap().get(5, 5); + assert!((from_u16_signed(t[0]) + 0.25).abs() < 1e-3); + assert!((from_u16(t[1]) - 0.5).abs() < 1e-3); + } + + #[test] + fn mask_commit_and_untouched_tiles_stay_shared() { + let mut px = PixelStore::default(); + let (a, tiles) = alpha(1.0); + commit_mask(&mut px, LayerId(9), 64, 1.0, src(&a, &tiles, 1.0, false)); + let before = px.clone(); + commit_mask(&mut px, LayerId(9), 64, 0.0, src(&a, &tiles, 1.0, true)); + assert_eq!(from_u16(before.masks[&LayerId(9)].get(5, 5)[1]), 1.0); + assert_eq!(px.masks[&LayerId(9)].get(5, 5)[1], 0); + } + + #[test] + fn carried_values_replace_the_tool_value_and_coverage_images_work() { + let mut px = PixelStore::default(); + let (a, tiles) = alpha(1.0); + let mut carried = TileImage::new(64, 64, 1); + carried.set(5, 5, &[to_u16(0.25)]); + let carried: BTreeMap = [(Channel::Roughness, carried)].into_iter().collect(); + let vals: BTreeMap = [(Channel::Roughness, [0.8; 3])].into_iter().collect(); + commit_layer(&mut px, LayerId(3), 64, &vals, CommitSource { carried: Some(&carried), ..src(&a, &tiles, 1.0, false) }); + assert!((from_u16(px.layer(LayerId(3), Channel::Roughness).unwrap().get(5, 5)[0]) - 0.25).abs() < 1e-3); + let mut cov = TileImage::new(64, 64, 1); + commit_coverage(&mut cov, src(&a, &tiles, 0.5, false)); + assert!((from_u16(cov.get(5, 5)[0]) - 0.5).abs() < 1e-3); + } +} diff --git a/crates/paint/src/lib.rs b/crates/paint/src/lib.rs new file mode 100644 index 00000000..c6806432 --- /dev/null +++ b/crates/paint/src/lib.rs @@ -0,0 +1,28 @@ +//! # texturecraft-paint +//! +//! The brush engine. A [`Stroke`] turns pointer samples into world-space [`Stamp`]s (picking, +//! lazy mouse, spacing along the surface, jitter, symmetry); [`raster::rasterize`] accumulates +//! them into a texture-space stroke alpha over the UV G-buffer, hiding surfaces the camera can't +//! see ([`EyeDepth`]); [`commit`] blends the finished stroke into layer pixels. Clone and smudge +//! strokes carry channel values ([`pickup`]) instead of painting the tool's material. +//! [`path`] samples the Path tool's curves and lasso coverage; [`particles`] runs the particle +//! brush. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod brush; +pub mod commit; +pub mod particles; +pub mod path; +pub mod pickup; +pub mod raster; +pub mod stroke; +pub mod visibility; + +pub use brush::{Alignment, BrushAlpha, BrushSettings, Particles, Shape, Symmetry, image_alpha}; +pub use commit::{CommitSource, TexelFn, commit_coverage, commit_layer, commit_mask}; +pub use path::{Lasso, spline}; +pub use pickup::{Pickup, PickupMode}; +pub use raster::{RasterCtx, StrokeCanvas, UvStamp, fill_coverage, fill_triangles, rasterize, rasterize_uv}; +pub use stroke::{PickCtx, Stamp, Stroke, SurfaceHit, pick}; +pub use visibility::EyeDepth; diff --git a/crates/paint/src/particles.rs b/crates/paint/src/particles.rs new file mode 100644 index 00000000..556b1c22 --- /dev/null +++ b/crates/paint/src/particles.rs @@ -0,0 +1,101 @@ +//! Particle brush: each stamp emits a few particles that slide down the surface under gravity +//! (world −Y) and fall off overhangs, painting a small dab per simulation step. Deterministic: +//! particles are seeded by the stamp seed. + +use texturecraft_geom::glam::Vec3; +use texturecraft_geom::{Surface, surface_at}; +use texturecraft_texel::noise::pcg3d; + +use crate::brush::Particles; +use crate::stroke::{PickCtx, Stamp}; + +/// Particle dab radius relative to the brush stamp. +const PARTICLE_SIZE: f32 = 0.35; + +/// The surface hit by a ray, if any within `tmax`. +fn cast(ctx: &PickCtx, o: Vec3, d: Vec3, tmax: f32) -> Option { + let hit = ctx.bvh.closest(o, d, tmax)?; + surface_at(ctx.mesh, hit.tri, hit.u, hit.v) +} + +/// Project `p` onto the surface along the normal `n` (within `reach`). +fn snap(ctx: &PickCtx, p: Vec3, n: Vec3, reach: f32) -> Option { + cast(ctx, p + n * reach, -n, reach * 2.0) +} + +/// Particle dabs for one brush stamp. +pub fn emit(ctx: &PickCtx, p: &Particles, s: &Stamp) -> Vec { + let size = s.radius * PARTICLE_SIZE; + let step = size * p.gravity; + let fall = s.radius * 20.0; + let unit = |v: u32| v as f32 / u32::MAX as f32; + let mut out = Vec::new(); + for k in 0..p.count { + let r = pcg3d(s.seed, k, 0xD057); + let (a, d) = (unit(r[0]) * std::f32::consts::TAU, unit(r[1]).sqrt()); + let start = s.center + (s.tangent * a.cos() + s.bitangent * a.sin()) * d * s.radius; + let Some(mut at) = snap(ctx, start, s.normal, s.radius) else { continue }; + for life in 0..p.lifetime { + let tangent = at.nrm.any_orthonormal_vector(); + out.push(Stamp { + center: at.pos, + normal: at.nrm, + tangent, + bitangent: at.nrm.cross(tangent), + radius: size, + flow: s.flow * (1.0 - life as f32 / p.lifetime as f32), + seed: s.seed.wrapping_mul(17).wrapping_add(k), + ..*s + }); + if step <= 0.0 { + break; + } + let slide = Vec3::NEG_Y - at.nrm * at.nrm.dot(Vec3::NEG_Y); + let next = if slide.length() > 0.05 { + // Slide downhill; past an edge the particle drops onto what lies below. + let to = at.pos + slide.normalize() * step; + snap(ctx, to, at.nrm, step).or_else(|| cast(ctx, to + at.nrm * step, Vec3::NEG_Y, fall)) + } else if at.nrm.y < 0.0 { + // Hanging under a ceiling: fall straight down. + cast(ctx, at.pos + at.nrm * step, Vec3::NEG_Y, fall) + } else { + None // resting on flat ground + }; + match next { + Some(h) => at = h, + None => break, + } + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + use texturecraft_geom::glam::Vec2; + use texturecraft_geom::{Bvh, OrbitCamera, primitives}; + + #[test] + fn particles_slide_down_a_wall_deterministically() { + // A vertical wall: the plane rotated to face +Z. + let mut m = primitives::plane(2.0, 8); + for p in &mut m.positions { + *p = Vec3::new(p.x, -p.z, 0.0); + } + for n in &mut m.normals { + *n = Vec3::Z; + } + let bvh = Bvh::new(&m); + let cam = OrbitCamera::default(); + let ctx = PickCtx { mesh: &m, bvh: &bvh, camera: &cam, viewport: Vec2::new(100.0, 100.0) }; + let s = Stamp { center: Vec3::ZERO, normal: Vec3::Z, tangent: Vec3::X, bitangent: Vec3::Y, radius: 0.1, flow: 1.0, hardness: 0.5, depth: 1.0, seed: 3 }; + let p = Particles { enabled: true, count: 4, lifetime: 20, gravity: 1.0 }; + let a = emit(&ctx, &p, &s); + assert_eq!(a, emit(&ctx, &p, &s)); + assert_eq!(a.len(), 80); + let lowest = a.iter().map(|d| d.center.y).fold(f32::MAX, f32::min); + assert!(lowest < -0.4, "{lowest}"); + assert!(a.iter().all(|d| d.center.z.abs() < 1e-3)); + } +} diff --git a/crates/paint/src/path.rs b/crates/paint/src/path.rs new file mode 100644 index 00000000..fe483ee0 --- /dev/null +++ b/crates/paint/src/path.rs @@ -0,0 +1,97 @@ +//! Path tool geometry: a smooth curve through control points and the antialiased coverage of +//! a closed curve (lasso fill). Works in any 2D space (screen pixels or texels). + +use texturecraft_geom::glam::Vec2; + +/// Most samples per curve segment. +const MAX_SEGMENT_SAMPLES: f32 = 1024.0; + +/// A Catmull-Rom spline through `points`, sampled about every `step` units. Closed curves wrap +/// back to the first point; the result starts at the first point. +pub fn spline(points: &[Vec2], closed: bool, step: f32) -> Vec { + let n = points.len(); + if n < 2 { + return points.to_vec(); + } + let at = |i: isize| { + let i = if closed { i.rem_euclid(n as isize) } else { i.clamp(0, n as isize - 1) }; + points.get(i as usize).copied().unwrap_or_default() + }; + let step = if step.is_finite() && step > 1e-6 { step } else { 1.0 }; + let segments = if closed { n } else { n - 1 } as isize; + let mut out = vec![at(0)]; + for s in 0..segments { + let (p0, p1, p2, p3) = (at(s - 1), at(s), at(s + 1), at(s + 2)); + let k = ((p2 - p1).length() / step).ceil().clamp(1.0, MAX_SEGMENT_SAMPLES) as usize; + for j in 1..=k { + let t = j as f32 / k as f32; + let (t2, t3) = (t * t, t * t * t); + out.push(0.5 * (2.0 * p1 + (p2 - p0) * t + (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2 + (3.0 * p1 - p0 - 3.0 * p2 + p3) * t3)); + } + } + out +} + +/// A closed polygon's coverage: 1 inside, 0 outside, a one-unit ramp across the edge. +#[derive(Clone, Debug)] +pub struct Lasso { + poly: Vec, + lo: Vec2, + hi: Vec2, +} + +impl Lasso { + /// Fewer than three finite points cover nothing. + pub fn new(poly: Vec) -> Lasso { + let poly: Vec = if poly.len() >= 3 && poly.iter().all(|p| p.is_finite()) { poly } else { Vec::new() }; + let lo = poly.iter().fold(Vec2::splat(f32::MAX), |a, p| a.min(*p)) - 1.0; + let hi = poly.iter().fold(Vec2::splat(f32::MIN), |a, p| a.max(*p)) + 1.0; + Lasso { poly, lo, hi } + } + + pub fn coverage(&self, p: Vec2) -> f32 { + if !(p.x >= self.lo.x && p.y >= self.lo.y && p.x <= self.hi.x && p.y <= self.hi.y) { + return 0.0; + } + let mut inside = false; + let mut d2 = f32::MAX; + for (a, b) in self.poly.iter().zip(self.poly.iter().cycle().skip(1)) { + if (a.y > p.y) != (b.y > p.y) && p.x < (b.x - a.x) * (p.y - a.y) / (b.y - a.y) + a.x { + inside = !inside; + } + let e = *b - *a; + let t = ((p - *a).dot(e) / e.length_squared().max(1e-12)).clamp(0.0, 1.0); + d2 = d2.min((*a + e * t).distance_squared(p)); + } + let d = d2.sqrt(); + (if inside { 0.5 + d } else { 0.5 - d }).clamp(0.0, 1.0) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn spline_passes_through_the_points() { + let pts = [Vec2::new(0.0, 0.0), Vec2::new(10.0, 5.0), Vec2::new(20.0, 0.0)]; + let c = spline(&pts, false, 1.0); + for p in pts { + assert!(c.iter().any(|q| q.distance(p) < 1e-4), "{p}"); + } + assert_eq!(c.last().copied(), Some(pts[2])); + let closed = spline(&pts, true, 1.0); + assert!(closed.last().is_some_and(|q| q.distance(pts[0]) < 1e-4)); + assert!(closed.len() > c.len()); + assert_eq!(spline(&pts[..1], false, f32::NAN), vec![pts[0]]); + } + + #[test] + fn lasso_covers_the_inside_with_a_soft_edge() { + let sq = Lasso::new(vec![Vec2::new(0.0, 0.0), Vec2::new(10.0, 0.0), Vec2::new(10.0, 10.0), Vec2::new(0.0, 10.0)]); + assert_eq!(sq.coverage(Vec2::new(5.0, 5.0)), 1.0); + assert_eq!(sq.coverage(Vec2::new(12.0, 5.0)), 0.0); + assert!((sq.coverage(Vec2::new(10.0, 5.0)) - 0.5).abs() < 1e-4); + assert_eq!(Lasso::new(vec![Vec2::ZERO, Vec2::ONE]).coverage(Vec2::ZERO), 0.0); + } +} diff --git a/crates/paint/src/pickup.rs b/crates/paint/src/pickup.rs new file mode 100644 index 00000000..40ff8f88 --- /dev/null +++ b/crates/paint/src/pickup.rs @@ -0,0 +1,193 @@ +//! Clone and smudge: strokes that carry channel values picked up from the texture set instead of +//! painting the tool's material. The carried values are kept straight (not premultiplied) per +//! channel next to the stroke alpha, so the live preview and the commit read them directly. + +use std::collections::BTreeMap; +use std::sync::Arc; + +use texturecraft_compose::Composite; +use texturecraft_core::Channel; +use texturecraft_geom::{NO_TRI, UvGBuffer}; +use texturecraft_texel::{TileImage, from_u16, to_u16}; + +use crate::commit::{dec, enc}; + +/// How a pickup stroke finds the values it deposits. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum PickupMode { + /// Copy the values `offset` texels away (source = destination + offset). + Clone { offset: [i64; 2] }, + /// Drag values along the stroke: each stamp deposits what lay under the previous stamp, + /// blended by `strength` (0..1). + Smudge { strength: f32 }, +} + +/// The values a clone or smudge stroke carries. +#[derive(Clone, Debug)] +pub struct Pickup { + pub mode: PickupMode, + /// The composite when the stroke began (what the stroke reads). + source: Composite, + /// Texels outside the texture set's UV islands are never read. + gbuf: Arc, + /// Straight values per painted channel; the stroke alpha lives in the stroke canvas. + pub values: BTreeMap, + /// Previous smudge stamp center (texels). + prev: Option<[f32; 2]>, + /// Per-stamp scratch: texels that receive values, and the values read for them. + reads: Vec<(u32, u32, f32)>, + read_values: Vec<[f32; 3]>, +} + +fn get(img: &TileImage, c: Channel, x: u32, y: u32) -> [f32; 3] { + let raw = img.get(x, y).map(|v| dec(c.kind(), v)); + if c.components() == 3 { [raw[0], raw[1], raw[2]] } else { [raw[0]; 3] } +} + +fn put(img: &mut TileImage, c: Channel, x: u32, y: u32, v: [f32; 3]) { + img.set(x, y, &v.map(|x| enc(c.kind(), x))[..c.components().min(3)]); +} + +impl Pickup { + /// A pickup reading `source` (whose resolution is the stroke's) for `channels`. + pub fn new(mode: PickupMode, source: Composite, gbuf: Arc, channels: impl IntoIterator) -> Pickup { + let res = source.resolution; + let values = channels.into_iter().map(|c| (c, TileImage::new(res, res, c.components()))).collect(); + Pickup { mode, source, gbuf, values, prev: None, reads: Vec::new(), read_values: Vec::new() } + } + + fn covered(&self, x: u32, y: u32) -> bool { + let i = y as usize * self.gbuf.width as usize + x as usize; + x < self.gbuf.width && self.gbuf.tri.get(i).is_some_and(|t| *t != NO_TRI) + } + + /// Blend one stamp's texels `(x, y, alpha)` (stamp center `center` in texels) into the + /// stroke `alpha` and the carried values. False when nothing was deposited: the first smudge + /// stamp only picks up. + pub fn stamp(&mut self, alpha: &mut TileImage, texels: &[(u32, u32, f32)], center: [f32; 2]) -> bool { + if texels.is_empty() { + return false; + } + let (shift, strength, smudge) = match self.mode { + PickupMode::Clone { offset } => (offset, 1.0, false), + PickupMode::Smudge { strength } => { + let Some(prev) = self.prev.replace(center) else { return false }; + let d = [center[0] - prev[0], center[1] - prev[1]]; + // A jump (UV seam, lifted cursor) has nothing to drag along. + if d[0].hypot(d[1]) > extent(texels) { + return false; + } + // shortcut: nearest-texel pickup; bilinear would smooth sub-texel stamp spacing. + ([-(d[0].round() as i64), -(d[1].round() as i64)], strength.clamp(0.0, 1.0), true) + } + }; + let res = i64::from(self.source.resolution); + self.reads.clear(); + self.read_values.clear(); + for &(x, y, a) in texels { + let a = a * strength; + let (sx, sy) = (i64::from(x) + shift[0], i64::from(y) + shift[1]); + if !(a > 0.0 && (0..res).contains(&sx) && (0..res).contains(&sy) && self.covered(sx as u32, sy as u32)) { + continue; + } + let (sx, sy) = (sx as u32, sy as u32); + // Smudge reads what the stroke shows so far: carried values over the source. + let m = if smudge { from_u16(alpha.get(sx, sy)[0]) } else { 0.0 }; + for (c, img) in &self.values { + let base = self.source.value(*c, sx, sy); + let v = if m > 0.0 { + let carried = get(img, *c, sx, sy); + std::array::from_fn(|k| carried[k] * m + base[k] * (1.0 - m)) + } else { + base + }; + self.read_values.push(v); + } + self.reads.push((x, y, a)); + } + let n = self.values.len(); + for (i, &(x, y, a)) in self.reads.iter().enumerate() { + let m = from_u16(alpha.get(x, y)[0]); + let m2 = a + m * (1.0 - a); + for (k, (c, img)) in self.values.iter_mut().enumerate() { + let Some(src) = self.read_values.get(i * n + k) else { continue }; + let old = get(img, *c, x, y); + put(img, *c, x, y, std::array::from_fn(|j| (src[j] * a + old[j] * m * (1.0 - a)) / m2.max(1e-6))); + } + alpha.set(x, y, &[to_u16(m2)]); + } + !self.reads.is_empty() + } +} + +/// Largest side of a stamp's texel bounding box. +fn extent(texels: &[(u32, u32, f32)]) -> f32 { + let (mut lo, mut hi) = ([u32::MAX; 2], [0u32; 2]); + for &(x, y, _) in texels { + lo = [lo[0].min(x), lo[1].min(y)]; + hi = [hi[0].max(x), hi[1].max(y)]; + } + hi[0].saturating_sub(lo[0]).max(hi[1].saturating_sub(lo[1])) as f32 + 1.0 +} + +#[cfg(test)] +mod tests { + use super::*; + use texturecraft_geom::primitives; + use texturecraft_texel::{TILE, to_u16}; + + /// A 64² plane (fully covered) whose roughness is `f(x)`. + fn setup(f: impl Fn(u32) -> f32) -> (Composite, Arc) { + let m = primitives::plane(2.0, 4); + let g = Arc::new(UvGBuffer::new(&m, 0, TILE, TILE)); + let mut c = Composite::new(&[Channel::Roughness], TILE); + if let Some(img) = c.channels.get_mut(&Channel::Roughness) { + for y in 0..TILE { + for x in 0..TILE { + img.set(x, y, &[to_u16(f(x))]); + } + } + } + (c, g) + } + + fn disc(cx: u32, cy: u32, r: u32) -> Vec<(u32, u32, f32)> { + let mut v = Vec::new(); + for y in cy - r..=cy + r { + for x in cx - r..=cx + r { + if (x.abs_diff(cx).pow(2) + y.abs_diff(cy).pow(2)) <= r * r { + v.push((x, y, 1.0)); + } + } + } + v + } + + #[test] + fn clone_copies_values_from_the_offset() { + let (c, g) = setup(|x| if x < 32 { 0.9 } else { 0.1 }); + let mut p = Pickup::new(PickupMode::Clone { offset: [-30, 0] }, c, g, [Channel::Roughness]); + let mut alpha = TileImage::new(TILE, TILE, 1); + assert!(p.stamp(&mut alpha, &disc(45, 32, 4), [45.0, 32.0])); + let v = get(&p.values[&Channel::Roughness], Channel::Roughness, 45, 32); + assert!((v[0] - 0.9).abs() < 1e-3, "{v:?}"); + assert!(from_u16(alpha.get(45, 32)[0]) > 0.99); + // Sources outside the image are skipped. + assert!(!p.stamp(&mut alpha, &disc(5, 5, 2), [5.0, 5.0])); + } + + #[test] + fn smudge_drags_values_along_the_stroke() { + let (c, g) = setup(|x| if x < 20 { 1.0 } else { 0.0 }); + let mut p = Pickup::new(PickupMode::Smudge { strength: 1.0 }, c, g, [Channel::Roughness]); + let mut alpha = TileImage::new(TILE, TILE, 1); + assert!(!p.stamp(&mut alpha, &disc(18, 32, 5), [18.0, 32.0]), "the first stamp only picks up"); + for cx in 20..30 { + p.stamp(&mut alpha, &disc(cx, 32, 5), [cx as f32, 32.0]); + } + let v = get(&p.values[&Channel::Roughness], Channel::Roughness, 30, 32)[0]; + assert!(v > 0.5 && from_u16(alpha.get(30, 32)[0]) > 0.99, "white dragged to the right: {v}"); + // A jump larger than the stamp deposits nothing. + assert!(!p.stamp(&mut alpha, &disc(50, 10, 3), [50.0, 10.0])); + } +} diff --git a/crates/paint/src/raster.rs b/crates/paint/src/raster.rs new file mode 100644 index 00000000..a5fbce82 --- /dev/null +++ b/crates/paint/src/raster.rs @@ -0,0 +1,291 @@ +//! Stamps → stroke alpha in texture space. Every texel whose surface point lies inside a +//! stamp's oriented volume receives paint, so UV seams are seamless by construction. + +use std::collections::BTreeSet; + +use rayon::prelude::*; + +use texturecraft_geom::glam::Vec3; +use texturecraft_geom::{Bvh, Mesh, NO_TRI, UvGBuffer}; +use texturecraft_texel::{TILE, TileImage, from_u16, to_u16}; + +use crate::brush::Shape; +use crate::pickup::Pickup; +use crate::stroke::Stamp; +use crate::visibility::EyeDepth; + +/// The stroke alpha being accumulated for one texture set. +#[derive(Clone, Debug)] +pub struct StrokeCanvas { + pub alpha: TileImage, + /// Every tile the stroke touched. + pub dirty: BTreeSet<(u32, u32)>, + /// Tiles touched since the caller last cleared this set (incremental preview updates). + pub recent: BTreeSet<(u32, u32)>, + /// Quick mask: paint only where this coverage image (1 component) is set; inverted when + /// the flag is true. + pub limit: Option<(TileImage, bool)>, + /// Clone and smudge: the channel values the stroke carries. + pub pickup: Option, + /// The stamp being added: `(x, y, alpha)` per texel (reused between stamps). + texels: Vec<(u32, u32, f32)>, +} + +impl StrokeCanvas { + pub fn new(resolution: u32) -> StrokeCanvas { + StrokeCanvas { + alpha: TileImage::new(resolution, resolution, 1), + dirty: BTreeSet::new(), + recent: BTreeSet::new(), + limit: None, + pickup: None, + texels: Vec::new(), + } + } + + fn touch(&mut self, x: u32, y: u32) { + let t = (x / TILE, y / TILE); + self.dirty.insert(t); + self.recent.insert(t); + } + + /// Blend the gathered stamp texels (stamp center `center` in texels) into the stroke: the + /// quick mask scales them, then the pickup deposits its values or the alpha accumulates + /// ("over": `m = a + m·(1 − a)`). + fn add_stamp(&mut self, center: [f32; 2]) { + let mut texels = std::mem::take(&mut self.texels); + if let Some((mask, invert)) = &self.limit { + for t in &mut texels { + let m = from_u16(mask.get(t.0, t.1)[0]); + t.2 *= if *invert { 1.0 - m } else { m }; + } + } + let painted = match &mut self.pickup { + Some(p) => p.stamp(&mut self.alpha, &texels, center), + None => { + for &(x, y, a) in &texels { + let m = from_u16(self.alpha.get(x, y)[0]); + self.alpha.set(x, y, &[to_u16(a + m * (1.0 - a))]); + } + true + } + }; + if painted { + for &(x, y, a) in &texels { + if a > 0.0 { + self.touch(x, y); + } + } + } + texels.clear(); + self.texels = texels; + } +} + +/// What the rasterizer reads. +pub struct RasterCtx<'a> { + pub mesh: &'a Mesh, + pub bvh: &'a Bvh, + pub gbuf: &'a UvGBuffer, + pub material: usize, + /// Camera visibility (None paints hidden surfaces too, e.g. for scripted strokes). + pub depth: Option<&'a EyeDepth>, + pub eye: Vec3, + pub backface_culling: bool, + /// Angle fade from/to in degrees. + pub angle_fade: (f32, f32), + pub shape: Shape<'a>, +} + +fn smoothstep(e0: f32, e1: f32, x: f32) -> f32 { + let t = ((x - e0) / (e1 - e0).max(1e-6)).clamp(0.0, 1.0); + t * t * (3.0 - 2.0 * t) +} + +/// Accumulate `stamps` into `canvas` ("over": `m = a + m·(1 − a)`). +pub fn rasterize(canvas: &mut StrokeCanvas, ctx: &RasterCtx, stamps: &[Stamp]) { + let (w, h) = (ctx.gbuf.width, ctx.gbuf.height); + if canvas.alpha.width() != w { + *canvas = StrokeCanvas::new(w); + } + let fade_lo = ctx.angle_fade.1.to_radians().cos(); + let fade_hi = ctx.angle_fade.0.to_radians().cos(); + let mut tris = Vec::new(); + for s in stamps { + if s.radius.is_nan() || s.radius <= 0.0 || !s.center.is_finite() { + continue; + } + let ext = Vec3::splat(s.radius * s.depth.max(1.0) * 1.42); + // The stamp center in texels: the footprint texel nearest to it on the surface. + let mut center = ([0.0f32; 2], f32::MAX); + tris.clear(); + ctx.bvh.query_box(s.center - ext, s.center + ext, |t| tris.push(t)); + for &t in &tris { + if ctx.mesh.material_of(t as usize) != Some(ctx.material) { + continue; + } + let Some([a, b, c]) = ctx.mesh.tri(t as usize) else { continue }; + let (Some(ua), Some(ub), Some(uc)) = (ctx.mesh.uvs.get(a), ctx.mesh.uvs.get(b), ctx.mesh.uvs.get(c)) else { continue }; + let (Some(pa), Some(pb), Some(pc)) = (ctx.mesh.positions.get(a), ctx.mesh.positions.get(b), ctx.mesh.positions.get(c)) else { continue }; + let (Some(na), Some(nb), Some(nc)) = (ctx.mesh.normals.get(a), ctx.mesh.normals.get(b), ctx.mesh.normals.get(c)) else { continue }; + let x0 = (ua.x.min(ub.x).min(uc.x) * w as f32).floor().max(0.0) as u32; + let x1 = ((ua.x.max(ub.x).max(uc.x) * w as f32).ceil().max(0.0) as u32).min(w); + let y0 = (ua.y.min(ub.y).min(uc.y) * h as f32).floor().max(0.0) as u32; + let y1 = ((ua.y.max(ub.y).max(uc.y) * h as f32).ceil().max(0.0) as u32).min(h); + for y in y0..y1 { + for x in x0..x1 { + let i = (y * w + x) as usize; + if ctx.gbuf.tri.get(i).copied().unwrap_or(NO_TRI) != t { + continue; + } + let Some([b1, b2]) = ctx.gbuf.bary.get(i).copied() else { continue }; + let b0 = 1.0 - b1 - b2; + let p = *pa * b0 + *pb * b1 + *pc * b2; + let d = p - s.center; + let lx = d.dot(s.tangent) / s.radius; + let ly = d.dot(s.bitangent) / s.radius; + if lx * lx + ly * ly > 2.0 { + continue; + } + if d.dot(s.normal).abs() > s.radius * s.depth { + continue; + } + let dist = d.length_squared(); + if dist < center.1 { + center = ([x as f32 + 0.5, y as f32 + 0.5], dist); + } + let n = (*na * b0 + *nb * b1 + *nc * b2).try_normalize().unwrap_or(s.normal); + let to_eye = (ctx.eye - p).try_normalize().unwrap_or(n); + let facing = n.dot(to_eye); + if ctx.backface_culling && facing <= 0.0 { + continue; + } + if n.dot(s.normal) <= 0.0 { + continue; + } + if let Some(depth) = ctx.depth + && !depth.visible(p) + { + continue; + } + let fade = if ctx.backface_culling { smoothstep(fade_lo, fade_hi, facing) } else { 1.0 }; + let a = ctx.shape.sample(lx, ly, s.hardness, s.seed) * s.flow * fade; + if a > 0.0 { + canvas.texels.push((x, y, a)); + } + } + } + } + canvas.add_stamp(center.0); + } +} + +/// Stroke alpha covering whole triangles (polygon fill): every texel of `tris` gets alpha 1. +pub fn fill_triangles(canvas: &mut StrokeCanvas, gbuf: &UvGBuffer, tris: &BTreeSet) { + fill_coverage(canvas, gbuf, |_, t| if tris.contains(&t) { 1.0 } else { 0.0 }); +} + +/// One-shot fill of the texture set's texels: `coverage(texel index, triangle)` gives each +/// covered texel's alpha (lasso and polygon fills). +pub fn fill_coverage(canvas: &mut StrokeCanvas, gbuf: &UvGBuffer, coverage: impl Fn(usize, u32) -> f32 + Sync) { + let w = gbuf.width.max(1); + if canvas.alpha.width() != gbuf.width { + *canvas = StrokeCanvas::new(gbuf.width); + } + canvas.texels = gbuf + .tri + .par_iter() + .enumerate() + .filter(|(_, t)| **t != NO_TRI) + .filter_map(|(i, t)| { + let a = coverage(i, *t).min(1.0); + (a > 0.0).then_some(((i as u32) % w, (i as u32) / w, a)) + }) + .collect(); + canvas.add_stamp([0.0; 2]); +} + +#[cfg(test)] +mod tests { + use super::*; + use texturecraft_geom::primitives; + + #[test] + fn one_stamp_on_a_plane_paints_a_disc() { + let m = primitives::plane(2.0, 8); + let bvh = Bvh::new(&m); + let g = UvGBuffer::new(&m, 0, 128, 128); + let mut canvas = StrokeCanvas::new(128); + let ctx = RasterCtx { + mesh: &m, + bvh: &bvh, + gbuf: &g, + material: 0, + depth: None, + eye: Vec3::new(0.0, 5.0, 0.0), + backface_culling: true, + angle_fade: (70.0, 85.0), + shape: Shape { alpha: crate::BrushAlpha::Round, image: None }, + }; + let st = + Stamp { center: Vec3::ZERO, normal: Vec3::Y, tangent: Vec3::X, bitangent: Vec3::Z, radius: 0.25, flow: 1.0, hardness: 0.9, depth: 1.0, seed: 0 }; + rasterize(&mut canvas, &ctx, &[st]); + // Plane is 2 units over 128 texels: radius 0.25 = 16 texels around the center (64, 64). + let at = |x: u32, y: u32| from_u16(canvas.alpha.get(x, y)[0]); + assert!(at(64, 64) > 0.99); + assert!(at(64 + 12, 64) > 0.9); + assert!(at(64 + 20, 64) < 0.01); + assert!(!canvas.dirty.is_empty()); + + // From below (backface), nothing is painted. + let mut c2 = StrokeCanvas::new(128); + let below = RasterCtx { eye: Vec3::new(0.0, -5.0, 0.0), ..ctx }; + rasterize(&mut c2, &below, &[st]); + assert!(c2.dirty.is_empty()); + } +} + +/// A dab in texture space (2D view painting): center and radius in UV units. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct UvStamp { + pub center: texturecraft_geom::glam::Vec2, + pub radius: f32, + pub angle: f32, + pub flow: f32, + pub hardness: f32, + pub seed: u32, +} + +/// Accumulate UV-space stamps into `canvas` (only texels covered by the texture set). +pub fn rasterize_uv(canvas: &mut StrokeCanvas, gbuf: &UvGBuffer, shape: Shape, stamps: &[UvStamp]) { + let (w, h) = (gbuf.width, gbuf.height); + if canvas.alpha.width() != w { + *canvas = StrokeCanvas::new(w); + } + for s in stamps { + if s.radius.is_nan() || s.radius <= 0.0 || !s.center.is_finite() { + continue; + } + let (cx, cy, r) = (s.center.x * w as f32, s.center.y * h as f32, s.radius * w as f32); + let x0 = (cx - r * 1.42).floor().max(0.0) as u32; + let x1 = ((cx + r * 1.42).ceil().max(0.0) as u32).min(w); + let y0 = (cy - r * 1.42).floor().max(0.0) as u32; + let y1 = ((cy + r * 1.42).ceil().max(0.0) as u32).min(h); + let (sn, cs) = s.angle.to_radians().sin_cos(); + for y in y0..y1 { + for x in x0..x1 { + if gbuf.tri.get((y * w + x) as usize).copied().unwrap_or(NO_TRI) == NO_TRI { + continue; + } + // Local frame with y up on screen (texel rows grow downward), as in the 3D view. + let (dx, dy) = ((x as f32 + 0.5 - cx) / r, (cy - y as f32 - 0.5) / r); + let (lx, ly) = (dx * cs + dy * sn, -dx * sn + dy * cs); + let a = shape.sample(lx, ly, s.hardness, s.seed) * s.flow; + if a > 0.0 { + canvas.texels.push((x, y, a)); + } + } + } + canvas.add_stamp([cx, cy]); + } +} diff --git a/crates/paint/src/stroke.rs b/crates/paint/src/stroke.rs new file mode 100644 index 00000000..471d43e2 --- /dev/null +++ b/crates/paint/src/stroke.rs @@ -0,0 +1,277 @@ +//! From pointer samples to stamps: picking, lazy mouse, spacing along the surface, stamp +//! frames, jitter and symmetry. + +use texturecraft_geom::glam::{Quat, Vec2, Vec3}; +use texturecraft_geom::{Bvh, Mesh, OrbitCamera, surface_at}; +use texturecraft_texel::noise::pcg3d; + +use crate::brush::{Alignment, BrushSettings}; + +/// Scene access for picking. +pub struct PickCtx<'a> { + pub mesh: &'a Mesh, + pub bvh: &'a Bvh, + pub camera: &'a OrbitCamera, + /// Viewport size in pixels. + pub viewport: Vec2, +} + +/// A point on the mesh under the cursor. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct SurfaceHit { + pub pos: Vec3, + pub nrm: Vec3, + pub tri: u32, + pub uv: Vec2, + pub material: usize, + pub screen: Vec2, +} + +/// Pick the mesh at a viewport pixel (origin top-left). +pub fn pick(ctx: &PickCtx, screen: Vec2) -> Option { + let vp = ctx.viewport.max(Vec2::ONE); + let ndc = Vec2::new(screen.x / vp.x * 2.0 - 1.0, 1.0 - screen.y / vp.y * 2.0); + let ray = ctx.camera.ray(ndc, vp.x / vp.y); + let hit = ctx.bvh.closest(ray.origin, ray.dir, f32::MAX)?; + let s = surface_at(ctx.mesh, hit.tri, hit.u, hit.v)?; + Some(SurfaceHit { pos: s.pos, nrm: s.nrm, tri: hit.tri, uv: s.uv, material: ctx.mesh.material_of(hit.tri as usize).unwrap_or(0), screen }) +} + +/// One brush dab in world space. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Stamp { + pub center: Vec3, + pub normal: Vec3, + pub tangent: Vec3, + pub bitangent: Vec3, + pub radius: f32, + pub flow: f32, + pub hardness: f32, + /// Half-depth of the stamp volume relative to the radius. + pub depth: f32, + /// Varies procedural alphas per stamp. + pub seed: u32, +} + +/// An in-progress stroke. +#[derive(Clone, Debug)] +pub struct Stroke { + pub settings: BrushSettings, + /// Brush radius in world units at full pressure. + pub radius: f32, + center: Vec3, + last: Option, + dist: f32, + lazy: Option, + count: u32, + last_dir: Option, + /// Every stamp emitted so far (for replay, inspect and tests). + pub stamps: Vec, +} + +fn rand01(n: u32, k: u32) -> f32 { + pcg3d(n, k, 0x5EED)[0] as f32 / u32::MAX as f32 +} + +impl Stroke { + pub fn new(settings: &BrushSettings, mesh: &Mesh) -> Stroke { + let s = settings.sanitized(); + let (lo, hi) = mesh.bounds(); + let diag = (hi - lo).length().max(1e-4); + Stroke { radius: s.size * diag, settings: s, center: (lo + hi) * 0.5, last: None, dist: 0.0, lazy: None, count: 0, last_dir: None, stamps: Vec::new() } + } + + /// Feed a pointer position (pixels) with pen pressure (1 for a mouse). Returns new stamps. + pub fn add(&mut self, ctx: &PickCtx, screen: Vec2, pressure: f32) -> Vec { + let mut p = screen; + if self.settings.lazy > 0.0 { + let l = *self.lazy.get_or_insert(screen); + let d = screen - l; + let len = d.length(); + let nl = if len > self.settings.lazy { l + d * (1.0 - self.settings.lazy / len) } else { l }; + self.lazy = Some(nl); + if self.last.is_some() && nl == l { + return Vec::new(); + } + p = nl; + } + let mut out = Vec::new(); + self.feed(ctx, p, pressure, 0, &mut out); + if self.settings.particles.enabled { + out = out.iter().flat_map(|s| crate::particles::emit(ctx, &self.settings.particles, s)).collect(); + } + let mut all = Vec::with_capacity(out.len() * 2); + for s in out { + all.push(s); + all.extend(self.mirrors(&s)); + } + self.stamps.extend_from_slice(&all); + all + } + + fn radius_at(&self, ctx: &PickCtx, pos: Vec3, pressure: f32) -> f32 { + let p = pressure.clamp(0.0, 1.0); + let scale = self.settings.min_size + (1.0 - self.settings.min_size) * p; + let r = self.radius * if pressure < 1.0 { scale } else { 1.0 }; + if self.settings.screen_space { r * (pos.distance(ctx.camera.eye()) / ctx.camera.distance.max(1e-4)) } else { r } + } + + fn feed(&mut self, ctx: &PickCtx, screen: Vec2, pressure: f32, depth: u32, out: &mut Vec) { + let Some(hit) = pick(ctx, screen) else { + self.last = None; + return; + }; + let radius = self.radius_at(ctx, hit.pos, pressure); + let Some(last) = self.last else { + out.push(self.stamp(ctx, &hit, radius)); + self.last = Some(hit); + self.dist = 0.0; + return; + }; + let seg = hit.pos - last.pos; + let len = seg.length(); + if len < 1e-9 { + return; + } + let step = (self.settings.spacing * 2.0 * radius).max(1e-6); + if len > 4.0 * radius && depth < 4 { + // Jumped across a silhouette or gap: resample in screen space, never through air. + let n = ((len / radius).ceil() as u32).clamp(2, 64); + let a = last.screen; + for k in 1..=n { + let s = a + (screen - a) * (k as f32 / n as f32); + self.feed(ctx, s, pressure, depth + 1, out); + } + return; + } + self.last_dir = Some(seg / len); + let mut t = step - self.dist; + let mut emitted = 0; + while t <= len && emitted < 4096 { + let p = last.pos + seg * (t / len); + // Snap to the surface by re-picking through the projected point. + let aspect = ctx.viewport.x.max(1.0) / ctx.viewport.y.max(1.0); + let snapped = ctx + .camera + .project(p, aspect) + .map(|ndc| Vec2::new((ndc.x * 0.5 + 0.5) * ctx.viewport.x, (0.5 - ndc.y * 0.5) * ctx.viewport.y)) + .and_then(|s| pick(ctx, s)) + .filter(|h| h.pos.distance(p) < radius * 2.0); + let h = snapped.unwrap_or(SurfaceHit { pos: p, ..hit }); + out.push(self.stamp(ctx, &h, radius)); + emitted += 1; + t += step; + } + self.dist = len - (t - step); + self.last = Some(hit); + } + + fn stamp(&mut self, ctx: &PickCtx, hit: &SurfaceHit, radius: f32) -> Stamp { + let s = &self.settings; + let n = self.count; + self.count = self.count.wrapping_add(1); + let jitter = |k: u32, amount: f32| if amount > 0.0 { (rand01(n, k) * 2.0 - 1.0) * amount } else { 0.0 }; + let normal = match s.alignment { + Alignment::Tangent => hit.nrm, + Alignment::Camera => -ctx.camera.forward(), + }; + let cam_up = { + let f = ctx.camera.forward(); + let right = f.cross(Vec3::Y).try_normalize().unwrap_or(Vec3::X); + right.cross(f) + }; + let up_src = if s.follow_path { self.last_dir.unwrap_or(cam_up) } else { cam_up }; + let up = (up_src - normal * normal.dot(up_src)).try_normalize().unwrap_or_else(|| normal.any_orthonormal_vector()); + let tangent0 = up.cross(normal).try_normalize().unwrap_or_else(|| normal.any_orthonormal_vector()); + let angle = (s.angle + jitter(1, s.jitter_angle) * 180.0).to_radians(); + let q = Quat::from_axis_angle(normal, angle); + let tangent = q * tangent0; + let bitangent = normal.cross(tangent); + let r = radius * (1.0 + jitter(2, s.jitter_size)).max(0.05); + let offset = (tangent * jitter(3, 1.0) + bitangent * jitter(4, 1.0)) * s.jitter_position * radius; + let flow = (s.flow * (1.0 - jitter(5, s.jitter_flow).abs())).clamp(0.0, 1.0); + Stamp { center: hit.pos + offset, normal, tangent, bitangent, radius: r, flow, hardness: s.hardness, depth: s.depth, seed: n } + } + + /// Mirror copies of a stamp for the enabled symmetry axes. + fn mirrors(&self, s: &Stamp) -> Vec { + let sym = self.settings.symmetry; + if !sym.any() { + return Vec::new(); + } + let mut out = Vec::new(); + for mask in 1u8..8 { + let (mx, my, mz) = (mask & 1 != 0, mask & 2 != 0, mask & 4 != 0); + if (mx && !sym.x) || (my && !sym.y) || (mz && !sym.z) { + continue; + } + let f = Vec3::new(if mx { -1.0 } else { 1.0 }, if my { -1.0 } else { 1.0 }, if mz { -1.0 } else { 1.0 }); + let m = |v: Vec3| v * f; + let normal = m(s.normal); + let tangent = m(s.tangent); + out.push(Stamp { center: self.center + m(s.center - self.center), normal, tangent, bitangent: normal.cross(tangent), ..*s }); + } + out + } + + pub fn last_hit(&self) -> Option { + self.last + } +} + +#[cfg(test)] +mod tests { + use super::*; + use texturecraft_geom::primitives; + + fn ctx_parts() -> (Mesh, Bvh, OrbitCamera) { + let m = primitives::sphere(1.0, 64, 32); + let b = Bvh::new(&m); + let mut c = OrbitCamera::default(); + c.pitch = 0.0; + c.frame(Vec3::splat(-1.0), Vec3::splat(1.0)); + (m, b, c) + } + + #[test] + fn spacing_controls_stamp_count() { + let (m, b, c) = ctx_parts(); + let ctx = PickCtx { mesh: &m, bvh: &b, camera: &c, viewport: Vec2::new(400.0, 400.0) }; + let count = |spacing: f32| { + let mut s = Stroke::new(&BrushSettings { spacing, ..BrushSettings::default() }, &m); + let mut n = 0; + for i in 0..=40 { + n += s.add(&ctx, Vec2::new(150.0 + i as f32 * 2.5, 200.0), 1.0).len(); + } + n + }; + let dense = count(0.05); + let sparse = count(0.5); + assert!(dense > sparse * 5, "{dense} vs {sparse}"); + assert!(sparse >= 2); + } + + #[test] + fn misses_produce_nothing_and_stamps_lie_on_the_surface() { + let (m, b, c) = ctx_parts(); + let ctx = PickCtx { mesh: &m, bvh: &b, camera: &c, viewport: Vec2::new(400.0, 400.0) }; + let mut s = Stroke::new(&BrushSettings::default(), &m); + assert!(s.add(&ctx, Vec2::new(2.0, 2.0), 1.0).is_empty()); + let st = s.add(&ctx, Vec2::new(200.0, 200.0), 1.0); + assert_eq!(st.len(), 1); + assert!((st[0].center.length() - 1.0).abs() < 0.01); + assert!((st[0].tangent.dot(st[0].normal)).abs() < 1e-4); + } + + #[test] + fn symmetry_mirrors_stamps() { + let (m, b, c) = ctx_parts(); + let ctx = PickCtx { mesh: &m, bvh: &b, camera: &c, viewport: Vec2::new(400.0, 400.0) }; + let mut st = BrushSettings::default(); + st.symmetry.x = true; + let mut s = Stroke::new(&st, &m); + let out = s.add(&ctx, Vec2::new(250.0, 200.0), 1.0); + assert_eq!(out.len(), 2); + assert!((out[0].center.x + out[1].center.x).abs() < 1e-4); + } +} diff --git a/crates/paint/src/visibility.rs b/crates/paint/src/visibility.rs new file mode 100644 index 00000000..01ce84f8 --- /dev/null +++ b/crates/paint/src/visibility.rs @@ -0,0 +1,135 @@ +//! Which surface points the paint camera can see: a software-rasterized eye-distance buffer, +//! rendered once at the start of a stroke. + +use rayon::prelude::*; +use texturecraft_geom::glam::{Mat4, Vec3, Vec4}; +use texturecraft_geom::{Mesh, OrbitCamera}; + +const BAND: usize = 16; + +/// Distance from the eye to the nearest surface per screen pixel (f32::MAX where empty). +#[derive(Clone, Debug)] +pub struct EyeDepth { + pub width: u32, + pub height: u32, + pub view_proj: Mat4, + pub eye: Vec3, + pub dist: Vec, + /// Tolerance in world units (≈ 0.4% of the scene radius). + pub bias: f32, +} + +impl EyeDepth { + /// Rasterize `mesh` seen by `camera` into a `width × height` buffer (sizes capped at 4096). + pub fn render(mesh: &Mesh, camera: &OrbitCamera, width: u32, height: u32) -> EyeDepth { + let (w, h) = (width.clamp(1, 4096), height.clamp(1, 4096)); + let aspect = w as f32 / h as f32; + let vp = camera.view_proj(aspect); + let eye = camera.eye(); + // Project every vertex once. + let clip: Vec = mesh.positions.iter().map(|p| vp * p.extend(1.0)).collect(); + let screen: Vec> = + clip.iter().map(|c| (c.w > 1e-5).then(|| ((c.x / c.w * 0.5 + 0.5) * w as f32, (0.5 - c.y / c.w * 0.5) * h as f32))).collect(); + let dist_v: Vec = mesh.positions.iter().map(|p| p.distance(eye)).collect(); + let bands = (h as usize).div_ceil(BAND); + let mut bins: Vec> = vec![Vec::new(); bands]; + for t in 0..mesh.triangle_count() { + let Some([a, b, c]) = mesh.tri(t) else { continue }; + let (Some(Some(pa)), Some(Some(pb)), Some(Some(pc))) = (screen.get(a), screen.get(b), screen.get(c)) else { continue }; + let y0 = pa.1.min(pb.1).min(pc.1).floor().max(0.0); + let y1 = pa.1.max(pb.1).max(pc.1).ceil().min(h as f32); + if y1 <= y0 || !y0.is_finite() || !y1.is_finite() { + continue; + } + let (b0, b1) = (y0 as usize / BAND, ((y1 as usize).saturating_sub(1)) / BAND); + for bin in bins.iter_mut().take(b1 + 1).skip(b0) { + bin.push(t as u32); + } + } + let mut dist = vec![f32::MAX; (w * h) as usize]; + dist.par_chunks_mut(BAND * w as usize).enumerate().for_each(|(bi, rows)| { + let row0 = bi * BAND; + let nrows = rows.len() / w as usize; + 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(Some(pa)), Some(Some(pb)), Some(Some(pc))) = (screen.get(a), screen.get(b), screen.get(c)) else { continue }; + let (da, db, dc) = (dist_v[a], dist_v[b], dist_v[c]); + let area = (pb.0 - pa.0) * (pc.1 - pa.1) - (pb.1 - pa.1) * (pc.0 - pa.0); + if area.abs() < 1e-9 { + continue; + } + let x0 = pa.0.min(pb.0).min(pc.0).floor().max(0.0) as usize; + let x1 = (pa.0.max(pb.0).max(pc.0).ceil().min(w as f32)) as usize; + let y0 = (pa.1.min(pb.1).min(pc.1).floor().max(row0 as f32)) as usize; + let y1 = (pa.1.max(pb.1).max(pc.1).ceil().min((row0 + nrows) as f32)) as usize; + for y in y0.max(row0)..y1 { + for x in x0..x1.min(w as usize) { + let (px, py) = (x as f32 + 0.5, y as f32 + 0.5); + let w0 = ((pb.0 - px) * (pc.1 - py) - (pb.1 - py) * (pc.0 - px)) / area; + let w1 = ((pc.0 - px) * (pa.1 - py) - (pc.1 - py) * (pa.0 - px)) / area; + let w2 = 1.0 - w0 - w1; + if w0 < -1e-4 || w1 < -1e-4 || w2 < -1e-4 { + continue; + } + let d = w0 * da + w1 * db + w2 * dc; + if let Some(slot) = rows.get_mut((y - row0) * w as usize + x) + && d < *slot + { + *slot = d; + } + } + } + } + }); + EyeDepth { width: w, height: h, view_proj: vp, eye, dist, bias: mesh.radius() * 0.004 } + } + + /// Whether world point `p` is visible (not hidden behind other geometry, inside the view). + pub fn visible(&self, p: Vec3) -> bool { + let c = self.view_proj * p.extend(1.0); + if c.w <= 1e-6 { + return false; + } + let (nx, ny) = (c.x / c.w, c.y / c.w); + if nx.abs() > 1.0 || ny.abs() > 1.0 { + return false; + } + let x = (((nx * 0.5 + 0.5) * self.width as f32) as u32).min(self.width - 1); + let y = (((0.5 - ny * 0.5) * self.height as f32) as u32).min(self.height - 1); + // Conservative: the nearest surface among the pixel and its neighbours. + let mut near = f32::MAX; + for dy in -1i32..=1 { + for dx in -1i32..=1 { + let (sx, sy) = (x as i32 + dx, y as i32 + dy); + if sx < 0 || sy < 0 || sx >= self.width as i32 || sy >= self.height as i32 { + continue; + } + if let Some(d) = self.dist.get((sy as u32 * self.width + sx as u32) as usize) { + near = near.min(*d); + } + } + } + // Allow a bias that grows with distance (depth buffer resolution). + p.distance(self.eye) <= near + self.bias + near * 0.002 + } +} + +#[cfg(test)] +mod tests { + use super::*; + use texturecraft_geom::primitives; + + #[test] + fn front_of_sphere_is_visible_back_is_not() { + let m = primitives::sphere(1.0, 64, 32); + let mut cam = OrbitCamera::default(); + cam.yaw = 0.0; + cam.pitch = 0.0; + cam.frame(Vec3::splat(-1.0), Vec3::splat(1.0)); + let d = EyeDepth::render(&m, &cam, 256, 256); + let toward = (cam.eye() - cam.target).normalize(); + assert!(d.visible(toward * 1.0)); + assert!(!d.visible(-toward * 1.0)); + assert!(!d.visible(Vec3::new(100.0, 0.0, 0.0))); + } +} diff --git a/crates/render/Cargo.toml b/crates/render/Cargo.toml new file mode 100644 index 00000000..0138c994 --- /dev/null +++ b/crates/render/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "texturecraft-render" +description = "TextureCraft wgpu viewport renderer: PBR, image-based lighting, debug views, UV view" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lints] +workspace = true + +[dependencies] +texturecraft-core = { workspace = true } +texturecraft-texel = { workspace = true } +texturecraft-geom = { workspace = true } +texturecraft-compose = { workspace = true } +wgpu = { workspace = true } +bytemuck = { workspace = true } +half = { workspace = true } +rayon = { workspace = true } + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +pollster = "0.4" + +[dev-dependencies] +image = { workspace = true } diff --git a/crates/render/src/headless.rs b/crates/render/src/headless.rs new file mode 100644 index 00000000..f22dd1d4 --- /dev/null +++ b/crates/render/src/headless.rs @@ -0,0 +1,30 @@ +//! A windowless wgpu device for CLI renders, screenshots and GPU tests. + +use crate::Renderer; + +pub struct Headless { + pub device: wgpu::Device, + pub queue: wgpu::Queue, + pub renderer: Renderer, + pub adapter_name: String, +} + +impl Headless { + /// Create a device on the default adapter. Errors when no GPU (or software) adapter exists. + pub fn new() -> Result { + let instance = wgpu::Instance::default(); + let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())).map_err(|e| format!("no GPU adapter: {e}"))?; + let adapter_name = adapter.get_info().name; + let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).map_err(|e| format!("no GPU device: {e}"))?; + let renderer = Renderer::new(&device, &queue, wgpu::TextureFormat::Rgba8Unorm); + Ok(Headless { device, queue, renderer, adapter_name }) + } + + /// Render a frame and read it back as RGBA8 (sRGB-encoded). + pub fn render(&mut self, frame: &crate::Frame) -> Option<(u32, u32, Vec)> { + let mut enc = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("tc_headless") }); + self.renderer.prepare(&self.device, &self.queue, &mut enc, frame); + self.queue.submit([enc.finish()]); + self.renderer.read_view(&self.device, &self.queue, frame.view_id) + } +} diff --git a/crates/render/src/ibl.rs b/crates/render/src/ibl.rs new file mode 100644 index 00000000..d4c9fc8b --- /dev/null +++ b/crates/render/src/ibl.rs @@ -0,0 +1,354 @@ +//! Image-based lighting, computed on the CPU: procedural HDR environments (no image assets), +//! a GGX-prefiltered mip chain for specular, and 9 spherical-harmonics coefficients for +//! diffuse. +//! +//! Equirect convention: `u = φ / 2π`, `v = θ / π`, direction +//! `(sin θ cos φ, cos θ, sin θ sin φ)`; row 0 is straight up. + +use std::f32::consts::{PI, TAU}; + +use rayon::prelude::*; +use texturecraft_geom::glam::{Vec2, Vec3}; + +/// An equirect HDR image (linear RGB). +#[derive(Clone, Debug)] +pub struct EnvImage { + pub width: u32, + pub height: u32, + pub data: Vec<[f32; 3]>, +} + +/// Built-in environments, in menu order. +pub const ENVIRONMENTS: [&str; 6] = ["Studio Soft", "Studio Contrast", "Overcast", "Sunset", "Warehouse", "Night"]; + +pub fn dir_of(u: f32, v: f32) -> Vec3 { + let (theta, phi) = (v * PI, u * TAU); + Vec3::new(theta.sin() * phi.cos(), theta.cos(), theta.sin() * phi.sin()) +} + +pub fn uv_of(d: Vec3) -> Vec2 { + let phi = d.z.atan2(d.x); + Vec2::new((phi / TAU).rem_euclid(1.0), d.y.clamp(-1.0, 1.0).acos() / PI) +} + +/// A soft rectangular light: azimuth/elevation center (degrees), half sizes (degrees). +struct SoftBox { + az: f32, + el: f32, + hw: f32, + hh: f32, + color: Vec3, +} + +impl SoftBox { + fn eval(&self, d: Vec3) -> Vec3 { + let el = d.y.clamp(-1.0, 1.0).asin().to_degrees(); + let az = d.z.atan2(d.x).to_degrees(); + let daz = ((az - self.az + 540.0).rem_euclid(360.0)) - 180.0; + let (x, y) = (daz.abs() / self.hw, (el - self.el).abs() / self.hh); + let e = |t: f32| 1.0 - smooth((t - 0.8) / 0.4); + self.color * e(x) * e(y) + } +} + +fn smooth(t: f32) -> f32 { + let t = t.clamp(0.0, 1.0); + t * t * (3.0 - 2.0 * t) +} + +fn sky(d: Vec3, zenith: Vec3, horizon: Vec3, ground: Vec3) -> Vec3 { + if d.y >= 0.0 { horizon.lerp(zenith, d.y.powf(0.6)) } else { horizon.lerp(ground, (-d.y).powf(0.25)) } +} + +fn radiance(name: &str, d: Vec3) -> Vec3 { + match name { + "Studio Contrast" => { + let base = sky(d, Vec3::splat(0.04), Vec3::splat(0.03), Vec3::splat(0.01)); + base + SoftBox { az: 35.0, el: 30.0, hw: 22.0, hh: 18.0, color: Vec3::new(14.0, 13.2, 12.0) }.eval(d) + + SoftBox { az: -150.0, el: 40.0, hw: 6.0, hh: 30.0, color: Vec3::new(4.0, 4.4, 5.0) }.eval(d) + } + "Overcast" => { + let base = sky(d, Vec3::new(1.2, 1.25, 1.35), Vec3::new(0.95, 0.95, 0.95), Vec3::new(0.18, 0.2, 0.15)); + base + SoftBox { az: 60.0, el: 55.0, hw: 40.0, hh: 25.0, color: Vec3::splat(0.6) }.eval(d) + } + "Sunset" => { + let base = sky(d, Vec3::new(0.15, 0.25, 0.55), Vec3::new(1.4, 0.65, 0.3), Vec3::new(0.08, 0.06, 0.05)); + let sun_dir = dir_of(0.12, 0.47); + let sun = smooth((d.dot(sun_dir) - 0.9985) / 0.0012) * 120.0; + base + Vec3::new(1.0, 0.6, 0.3) * sun + } + "Warehouse" => { + let mut c = sky(d, Vec3::splat(0.25), Vec3::splat(0.18), Vec3::splat(0.06)); + for k in 0..6 { + c += SoftBox { az: -150.0 + k as f32 * 60.0, el: 50.0, hw: 6.0, hh: 3.0, color: Vec3::new(9.0, 8.5, 7.5) }.eval(d); + } + c + SoftBox { az: 90.0, el: 5.0, hw: 30.0, hh: 12.0, color: Vec3::new(1.6, 1.8, 2.2) }.eval(d) + } + "Night" => { + let base = sky(d, Vec3::new(0.01, 0.015, 0.04), Vec3::new(0.03, 0.03, 0.06), Vec3::splat(0.005)); + base + SoftBox { az: 20.0, el: 15.0, hw: 10.0, hh: 10.0, color: Vec3::new(0.4, 1.6, 4.0) }.eval(d) + + SoftBox { az: -110.0, el: 10.0, hw: 12.0, hh: 8.0, color: Vec3::new(4.0, 0.8, 1.6) }.eval(d) + } + // "Studio Soft" (default) + _ => { + let base = sky(d, Vec3::new(0.5, 0.52, 0.56), Vec3::new(0.32, 0.32, 0.33), Vec3::new(0.08, 0.075, 0.07)); + base + SoftBox { az: 40.0, el: 35.0, hw: 28.0, hh: 20.0, color: Vec3::new(5.5, 5.2, 4.8) }.eval(d) + + SoftBox { az: -125.0, el: 20.0, hw: 35.0, hh: 22.0, color: Vec3::new(1.1, 1.25, 1.5) }.eval(d) + + SoftBox { az: 175.0, el: 55.0, hw: 8.0, hh: 25.0, color: Vec3::new(3.0, 3.0, 3.0) }.eval(d) + } + } +} + +/// Render a built-in environment (unknown names give the default studio). +pub fn environment(name: &str, width: u32) -> EnvImage { + let w = width.clamp(16, 4096); + let h = w / 2; + let mut data = vec![[0.0f32; 3]; (w * h) as usize]; + data.par_chunks_mut(w as usize).enumerate().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + let d = dir_of((x as f32 + 0.5) / w as f32, (y as f32 + 0.5) / h as f32); + *px = radiance(name, d).to_array(); + } + }); + EnvImage { width: w, height: h, data } +} + +impl EnvImage { + fn get(&self, x: i64, y: i64) -> Vec3 { + let xx = x.rem_euclid(self.width as i64) as u32; + let yy = y.clamp(0, self.height as i64 - 1) as u32; + self.data.get((yy * self.width + xx) as usize).map(|v| Vec3::from(*v)).unwrap_or(Vec3::ZERO) + } + + /// Bilinear lookup by direction. + pub fn sample(&self, d: Vec3) -> Vec3 { + let uv = uv_of(d); + let (fx, fy) = (uv.x * self.width as f32 - 0.5, uv.y * self.height as f32 - 0.5); + let (x0, y0) = (fx.floor() as i64, fy.floor() as i64); + let (tx, ty) = (fx - x0 as f32, fy - y0 as f32); + let a = self.get(x0, y0).lerp(self.get(x0 + 1, y0), tx); + let b = self.get(x0, y0 + 1).lerp(self.get(x0 + 1, y0 + 1), tx); + a.lerp(b, ty) + } + + /// Box-downsample by 2. + pub fn half(&self) -> EnvImage { + let (w, h) = ((self.width / 2).max(1), (self.height / 2).max(1)); + let mut data = vec![[0.0f32; 3]; (w * h) as usize]; + data.par_chunks_mut(w as usize).enumerate().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + let (sx, sy) = (x as i64 * 2, y as i64 * 2); + let s = (self.get(sx, sy) + self.get(sx + 1, sy) + self.get(sx, sy + 1) + self.get(sx + 1, sy + 1)) * 0.25; + *px = s.to_array(); + } + }); + EnvImage { width: w, height: h, data } + } + + /// An imported equirect image (`rgba` row-major, top row first, sRGB-encoded when `srgb`) as + /// linear radiance: non-finite and negative values become 0, values are capped to the f16 + /// range, and images larger than [`MAX_IMPORT_WIDTH`] are box-downsampled. None when the + /// pixel count does not match the size. + pub fn from_rgba(width: u32, height: u32, rgba: &[[f32; 4]], srgb: bool) -> Option { + if width == 0 || height == 0 || rgba.len() as u64 != width as u64 * height as u64 { + return None; + } + let lin = |v: f32| { + let v = if v.is_finite() { v.clamp(0.0, 65_000.0) } else { 0.0 }; + if srgb { texturecraft_texel::srgb_to_linear(v.min(1.0)) } else { v } + }; + let data = rgba.par_iter().map(|p| [lin(p[0]), lin(p[1]), lin(p[2])]).collect(); + let mut img = EnvImage { width, height, data }; + while img.width > MAX_IMPORT_WIDTH || img.height > MAX_IMPORT_WIDTH { + img = img.half(); + } + // The mip chain needs at least 4×2 texels at its last level. + if img.width < 128 || img.height < 64 { + let (w, h) = (img.width.max(128), img.height.max(64)); + let data = (0..w * h).map(|i| img.sample(dir_of(((i % w) as f32 + 0.5) / w as f32, ((i / w) as f32 + 0.5) / h as f32)).to_array()).collect(); + img = EnvImage { width: w, height: h, data }; + } + Some(img) + } +} + +/// Widest imported environment kept (2K equirect: about 5.6 texels per degree). +pub const MAX_IMPORT_WIDTH: u32 = 2048; + +fn hammersley(i: u32, n: u32) -> Vec2 { + Vec2::new(i as f32 / n as f32, i.reverse_bits() as f32 * 2.328_306_4e-10) +} + +/// GGX-prefiltered mip chain: level `l` of `levels` is filtered for roughness `l / (levels-1)`. +pub fn prefilter(env: &EnvImage, levels: u32) -> Vec { + let levels = levels.clamp(1, 8); + // Clamp fireflies, then build a box pyramid to sample from (filtered importance sampling). + let clamped = EnvImage { data: env.data.iter().map(|c| c.map(|v| v.min(64.0))).collect(), ..env.clone() }; + let mut pyramid = vec![clamped]; + while pyramid.len() < 10 && pyramid.last().is_some_and(|p| p.width > 8) { + if let Some(next) = pyramid.last().map(EnvImage::half) { + pyramid.push(next); + } + } + let mut out = vec![env.clone()]; + let mut size = (env.width, env.height); + for l in 1..levels { + size = ((size.0 / 2).max(4), (size.1 / 2).max(2)); + let rough = l as f32 / (levels - 1) as f32; + let a = (rough * rough).max(1e-3); + let samples = 128u32; + let (w, h) = size; + let sa_texel = 4.0 * PI / (env.width as f32 * env.height as f32); + // With n = v, every sample's half vector, n·l and source level depend only on its index: + // tabulate them once per level as (tangent-space half vector, n·l, pyramid level). + let table: Vec<(Vec3, f32, &EnvImage)> = (0..samples) + .filter_map(|i| { + let xi = hammersley(i, samples); + let phi = TAU * xi.x; + let cos_t = ((1.0 - xi.y) / (1.0 + (a * a - 1.0) * xi.y)).max(0.0).sqrt(); + let sin_t = (1.0 - cos_t * cos_t).max(0.0).sqrt(); + let ndl = 2.0 * cos_t * cos_t - 1.0; + if ndl <= 0.0 { + return None; + } + let dd = a * a / (PI * ((cos_t * cos_t) * (a * a - 1.0) + 1.0).powi(2)).max(1e-8); + let sa_sample = 1.0 / (samples as f32 * dd / 4.0 + 1e-6); + let lod = (0.5 * (sa_sample / sa_texel).log2() + 1.0).clamp(0.0, (pyramid.len() - 1) as f32); + let src = pyramid.get(lod.round() as usize).unwrap_or(&pyramid[0]); + Some((Vec3::new(sin_t * phi.cos(), sin_t * phi.sin(), cos_t), ndl, src)) + }) + .collect(); + let wsum: f32 = table.iter().map(|t| t.1).sum(); + let mut data = vec![[0.0f32; 3]; (w * h) as usize]; + data.par_chunks_mut(w as usize).enumerate().for_each(|(y, row)| { + for (x, px) in row.iter_mut().enumerate() { + let n = dir_of((x as f32 + 0.5) / w as f32, (y as f32 + 0.5) / h as f32); + let up = if n.y.abs() < 0.999 { Vec3::Y } else { Vec3::X }; + let tx = up.cross(n).normalize(); + let ty = n.cross(tx); + let mut acc = Vec3::ZERO; + for (hv, ndl, src) in &table { + let hw = tx * hv.x + ty * hv.y + n * hv.z; + acc += src.sample(hw * (2.0 * hv.z) - n) * *ndl; + } + *px = (acc / wsum.max(1e-6)).to_array(); + } + }); + out.push(EnvImage { width: w, height: h, data }); + } + out +} + +/// Diffuse irradiance as 9 SH coefficients (already convolved with the cosine lobe and divided +/// by π, so `Σ c·Y(n)` times albedo is the diffuse radiance). +pub fn sh9(env: &EnvImage) -> [[f32; 3]; 9] { + let mut src = env.clone(); + while src.width > 128 { + src = src.half(); + } + let (w, h) = (src.width, src.height); + let mut c = [Vec3::ZERO; 9]; + for y in 0..h { + let theta = PI * (y as f32 + 0.5) / h as f32; + let dw = (TAU / w as f32) * (PI / h as f32) * theta.sin(); + for x in 0..w { + let d = dir_of((x as f32 + 0.5) / w as f32, (y as f32 + 0.5) / h as f32); + // No firefly clamp: the projection integrates, so a bright sun lights diffuse fully. + let l = src.get(x as i64, y as i64) * dw; + for (k, ylm) in sh_basis(d).iter().enumerate() { + c[k] += l * *ylm; + } + } + } + let a = [PI, 2.0 * PI / 3.0, 2.0 * PI / 3.0, 2.0 * PI / 3.0, PI / 4.0, PI / 4.0, PI / 4.0, PI / 4.0, PI / 4.0]; + let mut out = [[0.0; 3]; 9]; + for k in 0..9 { + out[k] = (c[k] * a[k] / PI).to_array(); + } + out +} + +pub fn sh_basis(d: Vec3) -> [f32; 9] { + let (x, y, z) = (d.x, d.y, d.z); + [ + 0.282_095, + 0.488_603 * y, + 0.488_603 * z, + 0.488_603 * x, + 1.092_548 * x * y, + 1.092_548 * y * z, + 0.315_392 * (3.0 * z * z - 1.0), + 1.092_548 * x * z, + 0.546_274 * (x * x - y * y), + ] +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn uv_and_direction_round_trip() { + for i in 1..20 { + for j in 1..10 { + let (u, v) = (i as f32 / 20.0, j as f32 / 10.0); + let back = uv_of(dir_of(u, v)); + assert!((back.x - u).abs() < 1e-4 && (back.y - v).abs() < 1e-4); + } + } + } + + #[test] + fn sh_of_a_uniform_environment_is_constant_irradiance() { + let env = EnvImage { width: 64, height: 32, data: vec![[1.0, 1.0, 1.0]; 64 * 32] }; + let c = sh9(&env); + for d in [Vec3::Y, Vec3::X, Vec3::NEG_Z, Vec3::new(0.3, -0.8, 0.5).normalize()] { + let b = sh_basis(d); + let e: f32 = (0..9).map(|k| c[k][0] * b[k]).sum(); + assert!((e - 1.0).abs() < 0.03, "{e}"); + } + } + + #[test] + fn prefilter_levels_shrink_and_stay_finite() { + let env = environment("Studio Soft", 128); + let mips = prefilter(&env, 5); + assert_eq!(mips.len(), 5); + assert!(mips[4].width < mips[1].width); + for m in &mips { + assert!(m.data.iter().all(|c| c.iter().all(|v| v.is_finite() && *v >= 0.0))); + } + for name in ENVIRONMENTS { + let e = environment(name, 64); + assert!(e.data.iter().any(|c| c[0] > 0.5), "{name} has no light"); + } + } + + #[test] + fn imported_images_become_safe_linear_environments() { + assert!(EnvImage::from_rgba(4, 2, &[[0.0; 4]; 7], false).is_none(), "pixel count must match"); + // HDR values stay above 1; NaN, infinity and negatives become 0; tiny images grow to 128×64. + let px = [[50.0, f32::NAN, -1.0, 1.0], [f32::INFINITY, 0.5, 0.25, 1.0]]; + let e = EnvImage::from_rgba(2, 1, &px, false).unwrap(); + assert_eq!((e.width, e.height), (128, 64)); + assert!(e.data.iter().all(|c| c.iter().all(|v| v.is_finite() && *v >= 0.0))); + assert!(e.data.iter().any(|c| c[0] > 40.0), "HDR range is kept"); + // 8-bit images are sRGB-decoded; big ones are halved down to the import limit. + let e = EnvImage::from_rgba(4096, 2048, &vec![[0.5, 0.5, 0.5, 1.0]; 4096 * 2048], true).unwrap(); + assert_eq!((e.width, e.height), (MAX_IMPORT_WIDTH, MAX_IMPORT_WIDTH / 2)); + assert!((e.data[0][0] - 0.214).abs() < 0.01, "{}", e.data[0][0]); + let mips = prefilter(&e, 6); + assert!(mips.iter().all(|m| m.data.iter().all(|c| c.iter().all(|v| v.is_finite())))); + } + + /// CPU cost of a 2K import: `cargo test -p texturecraft-render --release -- --ignored --nocapture`. + #[test] + #[ignore] + fn bench_prefilter_2k() { + let src = environment("Sunset", 2048); + let t = std::time::Instant::now(); + let mips = prefilter(&src, 6); + let sh = sh9(&src); + println!("2K environment: prefilter + SH {:.0} ms ({} levels, sh0 {:?})", t.elapsed().as_secs_f64() * 1000.0, mips.len(), sh[0]); + } +} diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs new file mode 100644 index 00000000..095d77a8 --- /dev/null +++ b/crates/render/src/lib.rs @@ -0,0 +1,23 @@ +//! # texturecraft-render +//! +//! The wgpu viewport renderer: metal/roughness or specular/glossiness PBR with image-based +//! lighting (procedural environments or imported HDR images, prefiltered on the CPU in [`ibl`]), +//! height-to-normal, optional parallax occlusion, 4× MSAA plus idle temporal accumulation, channel / mesh-map / UV +//! checker debug views, the 2D UV view, a linear HDR scene with a post pass (exposure, Linear / +//! ACES tone mapping, bloom, sharpen, vignette, grain), key-light shadow maps, a 3D wireframe +//! overlay, and a [`headless`] device for renders +//! without a window. No UI toolkit dependency; the egui shell drives it through +//! [`Renderer::prepare`] and [`Renderer::blit`]. Fragment passes only, so it runs on WebGPU and +//! WebGL2 too. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod mesh_maps; +#[cfg(not(target_arch = "wasm32"))] +pub mod headless; +pub mod ibl; +pub mod renderer; +pub mod trace; + +pub use renderer::{COLOR_FORMAT, Frame, MSAA, Renderer, Shade, ViewKind}; +pub use wgpu; diff --git a/crates/render/src/mesh_maps.rs b/crates/render/src/mesh_maps.rs new file mode 100644 index 00000000..a9323c5e --- /dev/null +++ b/crates/render/src/mesh_maps.rs @@ -0,0 +1,26 @@ +//! Debug textures for the mesh-map view. + +use texturecraft_compose::BakedMaps; +use texturecraft_core::BakedMap; +use texturecraft_texel::linear_to_srgb; + +/// RGBA8 of baked map `m` resampled to `size × size` (mid gray where not baked). Scalar maps are +/// shown as grayscale; ID maps as their colors. Values are display (sRGB) encoded. +pub fn mesh_map_rgba(baked: Option<&BakedMaps>, m: BakedMap, size: u32) -> Vec { + let size = size.max(1); + let mut out = vec![128u8; (size * size * 4) as usize]; + let Some(b) = baked.filter(|b| b.maps.contains_key(&m)) else { return out }; + for y in 0..size { + for x in 0..size { + let v = b.sample(m, x, y, size).unwrap_or([0.5; 3]); + let i = ((y * size + x) * 4) as usize; + for k in 0..3 { + // Data maps are shown as stored (like Painter), i.e. treated as display values. + out[i + k] = (v[k].clamp(0.0, 1.0) * 255.0 + 0.5) as u8; + } + out[i + 3] = 255; + } + } + let _ = linear_to_srgb; + out +} diff --git a/crates/render/src/renderer.rs b/crates/render/src/renderer.rs new file mode 100644 index 00000000..dd61db33 --- /dev/null +++ b/crates/render/src/renderer.rs @@ -0,0 +1,1277 @@ +//! The wgpu viewport renderer. Independent of any UI toolkit: the shell calls [`Renderer::prepare`] +//! with an encoder (offscreen render into per-view targets) and [`Renderer::blit`] inside its +//! own render pass. +//! +//! Per view and frame: the scene pass (MSAA, linear HDR premultiplied by scene coverage, resolved) +//! → the bloom pyramid (only when bloom is on) → the post pass (flat studio backdrop, exposure, +//! tone mapping, effects, sRGB) into the view's RGBA8 target that [`Renderer::blit`] and +//! [`Renderer::read_view`] use. The key-light shadow map is re-rendered only when the mesh, the +//! light direction or the visible texture sets change. +//! +//! Temporal anti-aliasing (idle accumulation): while a 3D view's frame and the scene content stay +//! the same, each new frame renders with a sub-pixel jitter and is averaged into the view's +//! accumulation target, up to [`TAA_SAMPLES`] frames (4× MSAA each); then the view stops +//! rendering until something changes. [`Renderer::accumulating`] tells the shell to keep redrawing. + +use std::collections::HashMap; + +use bytemuck::{Pod, Zeroable}; +use texturecraft_compose::{DisplayKind, ImageRes}; +use texturecraft_core::{DisplaySettings, ToneMapping}; +use texturecraft_geom::glam::{Mat4, Vec3}; +use texturecraft_geom::{Mesh, OrbitCamera}; +use texturecraft_texel::TILE; + +use crate::ibl; + +pub const MSAA: u32 = 4; +pub const COLOR_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm; +pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float; +/// Scene (pre tone mapping) format where float targets are renderable. +pub const HDR_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float; +/// Prefiltered environment levels (roughness 0..1). +pub const ENV_LEVELS: u32 = 6; +const SHADOW_SIZE: u32 = 2048; +/// Bloom pyramid levels (1/2 .. 1/32 resolution). +const BLOOM_LEVELS: u32 = 5; +/// Bright-pass threshold on the exposed scene value, and the glow gain at strength 1. +const BLOOM_THRESHOLD: f32 = 1.0; +const BLOOM_GAIN: f32 = 0.15; +/// Frames averaged by idle accumulation (temporal anti-aliasing). +pub const TAA_SAMPLES: u32 = 16; + +#[repr(C)] +#[derive(Clone, Copy, Pod, Zeroable)] +struct GlobalsRaw { + view_proj: [[f32; 4]; 4], + inv_view_proj: [[f32; 4]; 4], + eye: [f32; 4], + env: [f32; 4], + sh: [[f32; 4]; 9], + mode: [f32; 4], + bg: [f32; 4], + uv_xform: [f32; 4], + light: [f32; 4], + light_vp: [[f32; 4]; 4], + shadow: [f32; 4], + material: [f32; 4], +} + +/// The post pass uniform (see `Post` in post.wgsl). +#[repr(C)] +#[derive(Clone, Copy, Pod, Zeroable)] +struct PostRaw { + a: [f32; 4], + b: [f32; 4], +} + +#[repr(C)] +#[derive(Clone, Copy, Pod, Zeroable)] +struct Vertex { + pos: [f32; 3], + nrm: [f32; 3], + tan: [f32; 4], + uv: [f32; 2], +} + +#[repr(C)] +#[derive(Clone, Copy, Pod, Zeroable)] +struct BlitRaw { + rect: [f32; 4], + dst: [f32; 4], +} + +/// How the material is shaded (from the project's display settings and the view mode). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Shade { + /// Exposure in EV, applied in the post pass (not to data views). + pub exposure_ev: f32, + /// ACES filmic tone mapping (else linear, clipped). + pub aces: bool, + pub env_rotation_deg: f32, + /// 0..1 → background blur level. + pub env_blur: f32, + pub env_opacity: f32, + pub ao_intensity: f32, + pub height_strength: f32, + /// -1 for DirectX normal maps in the normal channel, 1 for OpenGL. + pub normal_y: f32, + /// 0 material, 1 channel, 2 mesh map (aux texture), 3 UV checker. + pub view_mode: u32, + /// Channel index for view mode 1 (Painter channel order, see the shader). + pub channel: u32, + /// Direction to an optional key light and its intensity (0 = off). The direction is in + /// environment space: it turns with `env_rotation_deg` like the lights painted into the + /// environment, so rotating the environment sweeps the key light and its shadows too. + pub light: [f32; 4], + /// Draw the UV wireframe in the 2D view. + pub wireframe: bool, + /// Key-light shadow map (needs `light[3] > 0`; 3D material view only). Shadows dim the key + /// light, not the image-based lighting. + pub shadows: bool, + /// Master switch of the effects below (Painter's "Post effects"); off costs no extra pass. + /// Effects never apply to data views (channels, mesh maps, UV checker, 2D view). + pub post_effects: bool, + /// Effect strengths, 0..1. + pub bloom: f32, + pub vignette: f32, + pub sharpen: f32, + pub grain: f32, + /// Draw the mesh edges over the shaded 3D view (depth tested). + pub wireframe_3d: bool, + /// Specular/glossiness shading: base color is the diffuse color and the Specular channel the + /// F0 color (roughness arrives as 1 - glossiness from the display conversion). + pub spec_gloss: bool, + /// Parallax occlusion mapping from the height channel (3D view). + pub parallax: bool, + /// Idle accumulation of jittered frames in the 3D view. + pub taa: bool, +} + +impl Shade { + /// The project-level shading (display and shader settings); the caller adds the view mode, + /// channel, height strength and wireframe toggles. + pub fn from_display(d: &DisplaySettings) -> Shade { + Shade { + exposure_ev: d.env_exposure, + aces: d.tone_mapping == ToneMapping::AcesFilmic, + env_rotation_deg: d.env_rotation, + env_blur: d.env_blur, + env_opacity: d.env_opacity, + ao_intensity: d.ao_intensity, + // The key light (on with shadows), in environment space. + light: if d.shadows { [0.5, 0.8, 0.4, 2.5] } else { [0.0; 4] }, + shadows: d.shadows, + post_effects: d.post_effects, + bloom: d.bloom, + vignette: d.vignette, + sharpen: d.sharpen, + grain: d.grain, + spec_gloss: d.spec_gloss, + parallax: d.parallax, + taa: d.taa, + ..Shade::default() + } + } +} + +impl Default for Shade { + fn default() -> Self { + Shade { + exposure_ev: 0.0, + aces: true, + env_rotation_deg: 0.0, + env_blur: 0.35, + env_opacity: 0.0, + ao_intensity: 0.75, + height_strength: 1.0, + normal_y: 1.0, + view_mode: 0, + channel: 0, + light: [0.0; 4], + wireframe: false, + shadows: false, + post_effects: false, + bloom: 0.0, + vignette: 0.0, + sharpen: 0.0, + grain: 0.0, + wireframe_3d: false, + spec_gloss: false, + parallax: false, + taa: false, + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum ViewKind { + /// The 3D viewport. + Scene, + /// The 2D UV view of texture set `ts`: zoom (1 = fit) and pan (clip-space offset). + Uv { ts: usize, zoom: f32, pan: [f32; 2] }, +} + +/// One view to render this frame. +#[derive(Clone, Debug, PartialEq)] +pub struct Frame { + pub view_id: u64, + /// Target size in physical pixels. + pub size: [u32; 2], + pub kind: ViewKind, + pub camera: OrbitCamera, + pub shade: Shade, + /// Visibility per texture set. + pub visible: Vec, +} + +struct Tex { + texture: wgpu::Texture, + view: wgpu::TextureView, + format: wgpu::TextureFormat, + mips: u32, +} + +fn mip_count(size: u32) -> u32 { + 32 - size.max(1).leading_zeros() +} + +impl Tex { + fn new(device: &wgpu::Device, label: &str, w: u32, h: u32, format: wgpu::TextureFormat, mips: u32) -> Tex { + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + mip_level_count: mips, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], + }); + let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); + Tex { texture, view, format, mips } + } +} + +struct SetGpu { + res: u32, + /// The five display textures in [`DisplayKind::ALL`] order, then the aux (mesh map) texture. + tex: Vec, + bind_group: wgpu::BindGroup, + mips_dirty: Vec, +} + +struct MeshGpu { + id: u64, + vb: wgpu::Buffer, + ib: wgpu::Buffer, + /// (first index, count, material) + draws: Vec<(u32, u32, usize)>, + /// Unique UV edges as a line list, per material: (first index, count, material). + lines: wgpu::Buffer, + line_draws: Vec<(u32, u32, usize)>, + /// Bounding sphere (center, radius) for fitting the shadow map. + center: Vec3, + radius: f32, +} + +/// The bloom pyramid of a view, created the first time bloom is used. +struct Bloom { + /// 1/2, 1/4, ... resolution targets. + levels: Vec, + /// Per level: reads the level above (the HDR scene for level 0). + down: Vec, + /// Per level but the last: reads the level below, added onto this one. + up: Vec, +} + +struct ViewTargets { + size: [u32; 2], + msaa: wgpu::TextureView, + depth: wgpu::TextureView, + /// The resolved HDR scene of a jittered accumulation frame. + hdr: wgpu::TextureView, + /// The HDR scene the post pass reads: resolved directly, or the running average of jittered + /// frames (see [`TAA_SAMPLES`]). + accum: wgpu::TextureView, + /// Reads `hdr` for the accumulation blend. + accum_bg: wgpu::BindGroup, + /// What `accum` holds: the frame and content revision, and the sample count. + history: Option<(Frame, u64)>, + samples: u32, + /// The display image (RGBA8, sRGB-encoded): post pass output, blit and read-back source. + resolve: wgpu::Texture, + resolve_view: wgpu::TextureView, + globals: wgpu::Buffer, + globals_bg: Option, + post_buf: wgpu::Buffer, + post_bg: wgpu::BindGroup, + bloom: Option, + blit_buf: wgpu::Buffer, + blit_bg: wgpu::BindGroup, + /// Set by [`Renderer::prepare`]: the view has content to blit. + ready: bool, +} + +/// Key-light shadow map, shared by all views (1×1 until shadows are first used). +struct ShadowMap { + view: wgpu::TextureView, + size: u32, + /// What the map was rendered for: mesh id, light direction bits, visible sets. + key: Option<(u64, [u32; 3])>, + visible: Vec, + vp: Mat4, + buf: wgpu::Buffer, + bg: wgpu::BindGroup, +} + +pub struct Renderer { + target_format: wgpu::TextureFormat, + /// [`HDR_FORMAT`], or [`COLOR_FORMAT`] where float targets are not renderable (WebGL2 + /// without float color buffers). + hdr_format: wgpu::TextureFormat, + globals_bgl: wgpu::BindGroupLayout, + set_bgl: wgpu::BindGroupLayout, + blit_bgl: wgpu::BindGroupLayout, + mip_bgl: wgpu::BindGroupLayout, + fx_bgl: wgpu::BindGroupLayout, + mesh_pipeline: wgpu::RenderPipeline, + uv_pipeline: wgpu::RenderPipeline, + wire_pipeline: wgpu::RenderPipeline, + wire3d_pipeline: wgpu::RenderPipeline, + bg_pipeline: wgpu::RenderPipeline, + blit_pipeline: wgpu::RenderPipeline, + post_pipeline: wgpu::RenderPipeline, + /// Bright pass + first downsample, downsample, additive upsample. + bloom_pipelines: [wgpu::RenderPipeline; 3], + shadow_pipeline: wgpu::RenderPipeline, + mip_pipelines: HashMap, + util_module: wgpu::ShaderModule, + sampler: wgpu::Sampler, + env_sampler: wgpu::Sampler, + shadow_sampler: wgpu::Sampler, + shadow: ShadowMap, + /// 1×1 black texture for unused effect inputs. + black: wgpu::TextureView, + accum_pipeline: wgpu::RenderPipeline, + env: Tex, + env_name: String, + sh: [[f32; 4]; 9], + /// Bumped by every change of what the views show (uploads, mesh, environment): restarts + /// accumulation. + content_rev: u64, + mesh: Option, + sets: Vec, + /// Texture set index per mesh material. + material_sets: Vec>, + views: HashMap, +} + +fn fragment_entry(binding: u32, ty: wgpu::BindingType) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { binding, visibility: wgpu::ShaderStages::FRAGMENT, ty, count: None } +} + +fn tex_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { + fragment_entry( + binding, + wgpu::BindingType::Texture { + multisampled: false, + sample_type: wgpu::TextureSampleType::Float { filterable: true }, + view_dimension: wgpu::TextureViewDimension::D2, + }, + ) +} + +fn sampler_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { + fragment_entry(binding, wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering)) +} + +fn uniform_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Uniform, has_dynamic_offset: false, min_binding_size: None }, + count: None, + } +} + +fn display_format(k: DisplayKind) -> wgpu::TextureFormat { + match k { + DisplayKind::BaseColor | DisplayKind::Emissive => wgpu::TextureFormat::Rgba8UnormSrgb, + DisplayKind::Orm | DisplayKind::Normal => wgpu::TextureFormat::Rgba8Unorm, + DisplayKind::Height => wgpu::TextureFormat::Rgba16Float, + } +} + +/// Default (neutral) texel of a display texture before any upload. +fn default_texel(k: DisplayKind) -> Vec { + match k { + DisplayKind::BaseColor => vec![188, 188, 188, 255], + DisplayKind::Orm => vec![255, 128, 0, 128], + DisplayKind::Normal => vec![128, 128, 255, 255], + DisplayKind::Height => [0.0, 0.04, 0.04, 0.04].iter().flat_map(|v| half::f16::from_f32(*v).to_le_bytes()).collect(), + DisplayKind::Emissive => vec![0, 0, 0, 255], + } +} + +/// The color output of a pipeline: fragment entry point, format, blending. `fs_wire*` entry +/// points draw line lists. +type ColorOut<'a> = (&'a str, wgpu::TextureFormat, Option); + +const ADD: wgpu::BlendComponent = wgpu::BlendComponent { src_factor: wgpu::BlendFactor::One, dst_factor: wgpu::BlendFactor::One, operation: wgpu::BlendOperation::Add }; +const ADDITIVE: wgpu::BlendState = wgpu::BlendState { color: ADD, alpha: ADD }; + +#[allow(clippy::too_many_arguments)] +fn pipeline( + device: &wgpu::Device, + label: &str, + layout: &wgpu::PipelineLayout, + module: &wgpu::ShaderModule, + vs: &str, + buffers: &[Option], + color: Option>, + samples: u32, + depth: Option, +) -> wgpu::RenderPipeline { + let lines = color.is_some_and(|(fs, ..)| fs.starts_with("fs_wire")); + let topology = if lines { wgpu::PrimitiveTopology::LineList } else { wgpu::PrimitiveTopology::TriangleList }; + let targets = color.map(|(_, format, blend)| [Some(wgpu::ColorTargetState { format, blend, write_mask: wgpu::ColorWrites::ALL })]); + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(label), + layout: Some(layout), + vertex: wgpu::VertexState { module, entry_point: Some(vs), buffers, compilation_options: Default::default() }, + primitive: wgpu::PrimitiveState { topology, cull_mode: None, ..Default::default() }, + depth_stencil: depth, + multisample: wgpu::MultisampleState { count: samples, mask: !0, alpha_to_coverage_enabled: false }, + fragment: color.zip(targets.as_ref()).map(|((fs, ..), targets)| wgpu::FragmentState { + module, + entry_point: Some(fs), + targets, + compilation_options: Default::default(), + }), + multiview_mask: None, + cache: None, + }) +} + +fn depth_state(write: bool, compare: wgpu::CompareFunction) -> wgpu::DepthStencilState { + wgpu::DepthStencilState { + format: DEPTH_FORMAT, + depth_write_enabled: Some(write), + depth_compare: Some(compare), + stencil: wgpu::StencilState::default(), + bias: wgpu::DepthBiasState::default(), + } +} + +/// Whether [`HDR_FORMAT`] works as an MSAA render target. Guaranteed on WebGPU and the native +/// backends; WebGL2/GLES need float color buffer extensions, so probe there (wgpu-core reports the +/// validation error synchronously, so the future is ready at once). +fn hdr_renderable(device: &wgpu::Device) -> bool { + if device.adapter_info().backend != wgpu::Backend::Gl { + return true; + } + let scope = device.push_error_scope(wgpu::ErrorFilter::Validation); + let _probe = texture(device, "tc_probe", [1, 1], HDR_FORMAT, MSAA, wgpu::TextureUsages::RENDER_ATTACHMENT); + let mut error = std::pin::pin!(scope.pop()); + matches!(error.as_mut().poll(&mut std::task::Context::from_waker(std::task::Waker::noop())), std::task::Poll::Ready(None)) +} + +fn texture(device: &wgpu::Device, label: &str, size: [u32; 2], format: wgpu::TextureFormat, samples: u32, usage: wgpu::TextureUsages) -> wgpu::Texture { + device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { width: size[0], height: size[1], depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: samples, + dimension: wgpu::TextureDimension::D2, + format, + usage, + view_formats: &[], + }) +} + +fn target_view(device: &wgpu::Device, label: &str, size: [u32; 2], format: wgpu::TextureFormat, samples: u32) -> wgpu::TextureView { + let usage = if samples > 1 || format == DEPTH_FORMAT { + wgpu::TextureUsages::RENDER_ATTACHMENT + } else { + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING + }; + texture(device, label, size, format, samples, usage).create_view(&Default::default()) +} + +fn shadow_view(device: &wgpu::Device, size: u32) -> wgpu::TextureView { + texture(device, "tc_shadow", [size, size], DEPTH_FORMAT, 1, wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING).create_view(&Default::default()) +} + +/// One fullscreen-triangle pass into `target`. +fn fullscreen(encoder: &mut wgpu::CommandEncoder, label: &str, target: &wgpu::TextureView, load: wgpu::LoadOp, pipe: &wgpu::RenderPipeline, bg: &wgpu::BindGroup) { + fullscreen_blend(encoder, label, target, load, pipe, bg, 1.0); +} + +/// [`fullscreen`] with a blend constant (for pipelines that blend by it). +fn fullscreen_blend(encoder: &mut wgpu::CommandEncoder, label: &str, target: &wgpu::TextureView, load: wgpu::LoadOp, pipe: &wgpu::RenderPipeline, bg: &wgpu::BindGroup, constant: f64) { + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some(label), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: target, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { load, store: wgpu::StoreOp::Store }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + pass.set_pipeline(pipe); + pass.set_bind_group(0, bg, &[]); + pass.set_blend_constant(wgpu::Color { r: constant, g: constant, b: constant, a: constant }); + pass.draw(0..3, 0..1); +} + +/// Halton low-discrepancy value of `i` in `base` (0..1). +fn halton(mut i: u32, base: u32) -> f32 { + let (mut f, mut r) = (1.0, 0.0); + while i > 0 { + f /= base as f32; + r += f * (i % base) as f32; + i /= base; + } + r +} + +const CLEAR: wgpu::LoadOp = wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT); + +impl Renderer { + /// `target_format`: the format of the pass [`Renderer::blit`] draws into. + pub fn new(device: &wgpu::Device, queue: &wgpu::Queue, target_format: wgpu::TextureFormat) -> Renderer { + let shader = |label: &str, source: &str| device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some(label), source: wgpu::ShaderSource::Wgsl(source.into()) }); + let module = shader("tc_mesh", include_str!("shaders/mesh.wgsl")); + let util_module = shader("tc_util", include_str!("shaders/util.wgsl")); + let post_module = shader("tc_post", concat!(include_str!("shaders/util.wgsl"), include_str!("shaders/post.wgsl"))); + let shadow_module = shader("tc_shadow", include_str!("shaders/shadow.wgsl")); + let bgl = |label: &str, entries: &[wgpu::BindGroupLayoutEntry]| device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some(label), entries }); + let globals_bgl = bgl( + "tc_globals", + &[ + uniform_entry(0), + tex_entry(1), + sampler_entry(2), + fragment_entry(3, wgpu::BindingType::Texture { multisampled: false, sample_type: wgpu::TextureSampleType::Depth, view_dimension: wgpu::TextureViewDimension::D2 }), + fragment_entry(4, wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Comparison)), + ], + ); + let set_bgl = bgl("tc_set", &[tex_entry(0), tex_entry(1), tex_entry(2), tex_entry(3), tex_entry(4), tex_entry(5), sampler_entry(6)]); + let blit_bgl = bgl("tc_blit", &[tex_entry(0), sampler_entry(1), uniform_entry(2)]); + let mip_bgl = bgl("tc_mip", &[tex_entry(0), sampler_entry(1)]); + let fx_bgl = bgl("tc_fx", &[tex_entry(0), sampler_entry(1), uniform_entry(3), tex_entry(4)]); + let shadow_bgl = bgl("tc_shadow", &[uniform_entry(0)]); + let pl = |label: &str, groups: &[Option<&wgpu::BindGroupLayout>]| { + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some(label), bind_group_layouts: groups, immediate_size: 0 }) + }; + let layout = pl("tc_mesh", &[Some(&globals_bgl), Some(&set_bgl)]); + let bg_layout = pl("tc_bg", &[Some(&globals_bgl)]); + let blit_layout = pl("tc_blit", &[Some(&blit_bgl)]); + let fx_layout = pl("tc_fx", &[Some(&fx_bgl)]); + let shadow_layout = pl("tc_shadow", &[Some(&shadow_bgl)]); + let vb = [Some(wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as u64, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3, 2 => Float32x4, 3 => Float32x2], + })]; + // shortcut: the RGBA8 fallback clips highlights above 1 and bands in the darks; encode + // (e.g. RGBM) if WebGL2 without float color buffers turns out to matter. + let hdr_format = if hdr_renderable(device) { HDR_FORMAT } else { COLOR_FORMAT }; + let hdr = |fs, blend| Some((fs, hdr_format, blend)); + let always = || Some(depth_state(false, wgpu::CompareFunction::Always)); + let mesh_pipeline = pipeline(device, "tc_mesh", &layout, &module, "vs_mesh", &vb, hdr("fs_mesh", None), MSAA, Some(depth_state(true, wgpu::CompareFunction::Less))); + let uv_pipeline = pipeline(device, "tc_uv", &layout, &module, "vs_uv", &vb, hdr("fs_uv", None), MSAA, always()); + let blend = Some(wgpu::BlendState::ALPHA_BLENDING); + let wire_pipeline = pipeline(device, "tc_wire", &layout, &module, "vs_uv", &vb, hdr("fs_wire", blend), MSAA, always()); + let wire3d_depth = Some(depth_state(false, wgpu::CompareFunction::LessEqual)); + let wire3d_pipeline = pipeline(device, "tc_wire3d", &layout, &module, "vs_wire3d", &vb, hdr("fs_wire3d", blend), MSAA, wire3d_depth); + let bg_pipeline = pipeline(device, "tc_bg", &bg_layout, &module, "vs_bg", &[], hdr("fs_bg", None), MSAA, always()); + let blit_pipeline = pipeline(device, "tc_blit", &blit_layout, &util_module, "vs_blit", &[], Some(("fs_blit", target_format, None)), 1, None); + let post_pipeline = pipeline(device, "tc_post", &fx_layout, &post_module, "vs_full", &[], Some(("fs_post", COLOR_FORMAT, None)), 1, None); + let bloom_pipelines = [("fs_bloom_pre", None), ("fs_bloom_down", None), ("fs_bloom_up", Some(ADDITIVE))] + .map(|(fs, blend)| pipeline(device, "tc_bloom", &fx_layout, &post_module, "vs_full", &[], hdr(fs, blend), 1, None)); + let shadow_depth = wgpu::DepthStencilState { + bias: wgpu::DepthBiasState { constant: 0, slope_scale: 1.5, clamp: 0.0 }, + ..depth_state(true, wgpu::CompareFunction::Less) + }; + let shadow_pipeline = pipeline(device, "tc_shadow", &shadow_layout, &shadow_module, "vs_shadow", &vb, None, 1, Some(shadow_depth)); + // accum = hdr * k + accum * (1 - k) with k = 1 / (sample + 1): a running average. + let average = wgpu::BlendComponent { src_factor: wgpu::BlendFactor::Constant, dst_factor: wgpu::BlendFactor::OneMinusConstant, operation: wgpu::BlendOperation::Add }; + let accum_layout = pl("tc_accum", &[Some(&mip_bgl)]); + let accum_blend = Some(wgpu::BlendState { color: average, alpha: average }); + let accum_pipeline = pipeline(device, "tc_accum", &accum_layout, &util_module, "vs_full", &[], hdr("fs_mip", accum_blend), 1, None); + let sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("tc_tex"), + address_mode_u: wgpu::AddressMode::ClampToEdge, + address_mode_v: wgpu::AddressMode::ClampToEdge, + mag_filter: wgpu::FilterMode::Linear, + min_filter: wgpu::FilterMode::Linear, + mipmap_filter: wgpu::MipmapFilterMode::Linear, + anisotropy_clamp: 8, + ..Default::default() + }); + let env_sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("tc_env"), + address_mode_u: wgpu::AddressMode::Repeat, + address_mode_v: wgpu::AddressMode::ClampToEdge, + mag_filter: wgpu::FilterMode::Linear, + min_filter: wgpu::FilterMode::Linear, + mipmap_filter: wgpu::MipmapFilterMode::Linear, + ..Default::default() + }); + let shadow_sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("tc_shadow"), + mag_filter: wgpu::FilterMode::Linear, + min_filter: wgpu::FilterMode::Linear, + compare: Some(wgpu::CompareFunction::LessEqual), + ..Default::default() + }); + let shadow_buf = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("tc_shadow"), + size: std::mem::size_of::<[[f32; 4]; 4]>() as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let shadow = ShadowMap { + view: shadow_view(device, 1), + size: 1, + key: None, + visible: Vec::new(), + vp: Mat4::IDENTITY, + bg: device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("tc_shadow"), + layout: &shadow_bgl, + entries: &[wgpu::BindGroupEntry { binding: 0, resource: shadow_buf.as_entire_binding() }], + }), + buf: shadow_buf, + }; + let black = Tex::new(device, "tc_black", 1, 1, COLOR_FORMAT, 1).view; + let env = Tex::new(device, "tc_env", 512, 256, wgpu::TextureFormat::Rgba16Float, ENV_LEVELS); + let mut r = Renderer { + target_format, + hdr_format, + globals_bgl, + set_bgl, + blit_bgl, + mip_bgl, + fx_bgl, + mesh_pipeline, + uv_pipeline, + wire_pipeline, + wire3d_pipeline, + bg_pipeline, + blit_pipeline, + post_pipeline, + bloom_pipelines, + shadow_pipeline, + accum_pipeline, + mip_pipelines: HashMap::new(), + util_module, + sampler, + env_sampler, + shadow_sampler, + shadow, + black, + env, + env_name: String::new(), + sh: [[0.0; 4]; 9], + content_rev: 0, + mesh: None, + sets: Vec::new(), + material_sets: Vec::new(), + views: HashMap::new(), + }; + r.set_environment(device, queue, "Studio Soft", None); + r + } + + pub fn environment_name(&self) -> &str { + &self.env_name + } + + /// Use environment `name`: with `image`, that equirect image (an imported `.hdr`/`.exr`, or + /// any image, linearized), prefiltered on the CPU (rayon); otherwise the built-in of that + /// name (no-op when already current; unknown names give the default studio). + pub fn set_environment(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, name: &str, image: Option<&ImageRes>) { + let img = match image.and_then(|i| ibl::EnvImage::from_rgba(i.width, i.height, &i.rgba, i.srgb)) { + Some(img) => img, + None if self.env_name == name => return, + None => ibl::environment(name, 512), + }; + let levels = ibl::prefilter(&img, ENV_LEVELS); + if self.env.texture.width() != img.width || self.env.texture.height() != img.height { + self.env = Tex::new(device, "tc_env", img.width, img.height, wgpu::TextureFormat::Rgba16Float, ENV_LEVELS); + } + for (l, lv) in levels.iter().enumerate() { + let bytes: Vec = lv.data.iter().flat_map(|c| [c[0], c[1], c[2], 1.0]).flat_map(|v| half::f16::from_f32(v).to_le_bytes()).collect(); + queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &self.env.texture, mip_level: l as u32, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + &bytes, + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(lv.width * 8), rows_per_image: Some(lv.height) }, + wgpu::Extent3d { width: lv.width, height: lv.height, depth_or_array_layers: 1 }, + ); + } + let sh = ibl::sh9(&img); + self.sh = sh.map(|c| [c[0], c[1], c[2], 0.0]); + self.env_name = name.to_string(); + self.content_rev += 1; + for v in self.views.values_mut() { + v.globals_bg = None; + } + } + + /// Upload the mesh when `id` changed. + pub fn set_mesh(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, mesh: &Mesh, id: u64) { + if self.mesh.as_ref().is_some_and(|m| m.id == id) { + return; + } + let verts: Vec = (0..mesh.positions.len()) + .map(|i| Vertex { + pos: mesh.positions[i].to_array(), + nrm: mesh.normals.get(i).map(|n| n.to_array()).unwrap_or([0.0, 1.0, 0.0]), + tan: mesh.tangents.get(i).map(|t| t.to_array()).unwrap_or([1.0, 0.0, 0.0, 1.0]), + uv: mesh.uvs.get(i).map(|u| u.to_array()).unwrap_or([0.0, 0.0]), + }) + .collect(); + let mk = |label: &str, data: &[u8], usage: wgpu::BufferUsages| { + let buf = device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size: (data.len() as u64).max(4).next_multiple_of(4), + usage: usage | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + queue.write_buffer(&buf, 0, data); + buf + }; + let mut idx_bytes = bytemuck::cast_slice::(&mesh.indices).to_vec(); + idx_bytes.resize(idx_bytes.len().next_multiple_of(4), 0); + let vb = mk("tc_vb", bytemuck::cast_slice(&verts), wgpu::BufferUsages::VERTEX); + let ib = mk("tc_ib", &idx_bytes, wgpu::BufferUsages::INDEX); + let draws = mesh.submeshes.iter().map(|s| (s.start, s.count, s.material)).collect(); + // UV edges (each shared edge once) per material, for the 2D view wireframe. + let mut line_idx: Vec = Vec::new(); + let mut line_draws = Vec::new(); + for m in 0..mesh.materials.len().max(1) { + let start = line_idx.len() as u32; + let mut seen = std::collections::HashSet::new(); + for t in mesh.triangles_of(m) { + let Some(v) = mesh.tri(t) else { continue }; + for (a, b) in [(v[0], v[1]), (v[1], v[2]), (v[2], v[0])] { + let key = (a.min(b), a.max(b)); + if seen.insert(key) { + line_idx.extend_from_slice(&[key.0 as u32, key.1 as u32]); + } + } + } + line_draws.push((start, line_idx.len() as u32 - start, m)); + } + let lines = mk("tc_lines", bytemuck::cast_slice(&line_idx), wgpu::BufferUsages::INDEX); + let (lo, hi) = mesh.bounds(); + let (center, radius) = ((lo + hi) * 0.5, ((hi - lo).length() * 0.5).max(1e-3)); + self.mesh = Some(MeshGpu { id, vb, ib, draws, lines, line_draws, center, radius }); + self.content_rev += 1; + } + + /// Make the per-texture-set textures match `sets` (resolution per set) and map mesh + /// materials to sets. Re-created textures start neutral; returns the sets that were reset + /// (the caller must upload all their tiles). + pub fn set_texture_sets(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, sets: &[(u32, usize)], material_count: usize) -> Vec { + let mut reset = Vec::new(); + self.content_rev += 1; + self.sets.truncate(sets.len()); + for (i, &(res, _)) in sets.iter().enumerate() { + if self.sets.get(i).is_some_and(|s| s.res == res) { + continue; + } + let mips = mip_count(res); + let mut tex: Vec = DisplayKind::ALL.iter().map(|k| Tex::new(device, "tc_set", res, res, display_format(*k), mips)).collect(); + tex.push(Tex::new(device, "tc_aux", res.min(2048), res.min(2048), wgpu::TextureFormat::Rgba8Unorm, mip_count(res.min(2048)))); + for (t, k) in tex.iter().zip(DisplayKind::ALL.iter().map(|k| Some(*k)).chain(std::iter::once(None))) { + let texel = k.map(default_texel).unwrap_or_else(|| vec![128, 128, 128, 255]); + let (w, h) = (t.texture.width(), t.texture.height()); + let row: Vec = texel.iter().copied().cycle().take(texel.len() * w as usize).collect(); + let data: Vec = row.iter().copied().cycle().take(row.len() * h as usize).collect(); + queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &t.texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + &data, + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(w * texel.len() as u32), rows_per_image: Some(h) }, + wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + ); + } + let bind_group = self.set_bind_group(device, &tex); + let n = tex.len(); + let gpu = SetGpu { res, tex, bind_group, mips_dirty: vec![true; n] }; + if i < self.sets.len() { + self.sets[i] = gpu; + } else { + self.sets.push(gpu); + } + reset.push(i); + } + self.material_sets = vec![None; material_count]; + for (i, &(_, m)) in sets.iter().enumerate() { + if let Some(slot) = self.material_sets.get_mut(m) { + *slot = Some(i); + } + } + reset + } + + fn set_bind_group(&self, device: &wgpu::Device, tex: &[Tex]) -> wgpu::BindGroup { + let mut entries: Vec = + tex.iter().enumerate().map(|(i, t)| wgpu::BindGroupEntry { binding: i as u32, resource: wgpu::BindingResource::TextureView(&t.view) }).collect(); + entries.push(wgpu::BindGroupEntry { binding: 6, resource: wgpu::BindingResource::Sampler(&self.sampler) }); + device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("tc_set"), layout: &self.set_bgl, entries: &entries }) + } + + /// Upload one 64×64 tile of a display texture (bytes from `compose::tile_bytes`). + pub fn upload_tile(&mut self, queue: &wgpu::Queue, ts: usize, kind: DisplayKind, tx: u32, ty: u32, bytes: &[u8]) { + let Some(set) = self.sets.get_mut(ts) else { return }; + let Some(ki) = DisplayKind::ALL.iter().position(|k| *k == kind) else { return }; + let Some(t) = set.tex.get(ki) else { return }; + let bpt = kind.bytes_per_texel() as u32; + let (x, y) = (tx * TILE, ty * TILE); + if x >= set.res || y >= set.res || bytes.len() < (TILE * TILE * bpt) as usize { + return; + } + let (w, h) = (TILE.min(set.res - x), TILE.min(set.res - y)); + queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &t.texture, mip_level: 0, origin: wgpu::Origin3d { x, y, z: 0 }, aspect: wgpu::TextureAspect::All }, + bytes, + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(TILE * bpt), rows_per_image: Some(TILE) }, + wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + ); + self.content_rev += 1; + if let Some(d) = set.mips_dirty.get_mut(ki) { + *d = true; + } + } + + /// Upload the aux (mesh map / debug) texture of a set: RGBA8, `size × size`. + pub fn upload_aux(&mut self, queue: &wgpu::Queue, ts: usize, size: u32, rgba: &[u8]) { + let Some(set) = self.sets.get_mut(ts) else { return }; + let Some(t) = set.tex.last() else { return }; + let (w, h) = (t.texture.width(), t.texture.height()); + if size != w || rgba.len() < (w * h * 4) as usize { + return; + } + queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &t.texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + rgba, + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(w * 4), rows_per_image: Some(h) }, + wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + ); + self.content_rev += 1; + if let Some(d) = set.mips_dirty.last_mut() { + *d = true; + } + } + + /// Size of the aux texture of a set. + pub fn aux_size(&self, ts: usize) -> Option { + self.sets.get(ts).and_then(|s| s.tex.last()).map(|t| t.texture.width()) + } + + fn generate_mips(&mut self, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder) { + for si in 0..self.sets.len() { + for ti in 0..self.sets[si].tex.len() { + if !self.sets[si].mips_dirty.get(ti).copied().unwrap_or(false) { + continue; + } + let format = self.sets[si].tex[ti].format; + if !self.mip_pipelines.contains_key(&format) { + let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("tc_mip"), + bind_group_layouts: &[Some(&self.mip_bgl)], + immediate_size: 0, + }); + let p = pipeline(device, "tc_mip", &layout, &self.util_module, "vs_full", &[], Some(("fs_mip", format, None)), 1, None); + self.mip_pipelines.insert(format, p); + } + let Some(pipe) = self.mip_pipelines.get(&format) else { continue }; + let t = &self.sets[si].tex[ti]; + for level in 1..t.mips { + let src = t.texture.create_view(&wgpu::TextureViewDescriptor { base_mip_level: level - 1, mip_level_count: Some(1), ..Default::default() }); + let dst = t.texture.create_view(&wgpu::TextureViewDescriptor { base_mip_level: level, mip_level_count: Some(1), ..Default::default() }); + let bg = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("tc_mip"), + layout: &self.mip_bgl, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&src) }, + wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&self.sampler) }, + ], + }); + fullscreen(encoder, "tc_mip", &dst, CLEAR, pipe, &bg); + } + if let Some(d) = self.sets[si].mips_dirty.get_mut(ti) { + *d = false; + } + } + } + } + + /// Effect bind group (`fx_bgl`): source texture, the post uniform, an extra texture. + fn fx_bind_group(&self, device: &wgpu::Device, src: &wgpu::TextureView, aux: &wgpu::TextureView, post: &wgpu::Buffer) -> wgpu::BindGroup { + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("tc_fx"), + layout: &self.fx_bgl, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(src) }, + wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&self.sampler) }, + wgpu::BindGroupEntry { binding: 3, resource: post.as_entire_binding() }, + wgpu::BindGroupEntry { binding: 4, resource: wgpu::BindingResource::TextureView(aux) }, + ], + }) + } + + fn create_targets(&self, device: &wgpu::Device, size: [u32; 2]) -> ViewTargets { + let hdr = target_view(device, "tc_hdr", size, self.hdr_format, 1); + let accum = target_view(device, "tc_accum", size, self.hdr_format, 1); + let accum_bg = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("tc_accum"), + layout: &self.mip_bgl, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&hdr) }, + wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&self.sampler) }, + ], + }); + let usage = wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_SRC; + let resolve = texture(device, "tc_resolve", size, COLOR_FORMAT, 1, usage); + let resolve_view = resolve.create_view(&Default::default()); + let uniform = |label: &str, size: usize| { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size: size as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }) + }; + let post_buf = uniform("tc_post", std::mem::size_of::()); + let blit_buf = uniform("tc_blit", std::mem::size_of::()); + let blit_bg = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("tc_blit"), + layout: &self.blit_bgl, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&resolve_view) }, + wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&self.sampler) }, + wgpu::BindGroupEntry { binding: 2, resource: blit_buf.as_entire_binding() }, + ], + }); + ViewTargets { + size, + msaa: target_view(device, "tc_msaa", size, self.hdr_format, MSAA), + depth: target_view(device, "tc_depth", size, DEPTH_FORMAT, MSAA), + post_bg: self.fx_bind_group(device, &accum, &self.black, &post_buf), + hdr, + accum, + accum_bg, + history: None, + samples: 0, + resolve, + resolve_view, + globals: uniform("tc_globals", std::mem::size_of::()), + globals_bg: None, + post_buf, + bloom: None, + blit_buf, + blit_bg, + ready: false, + } + } + + /// The bloom pyramid of `v`, and the post bind group that reads it. + fn create_bloom(&self, device: &wgpu::Device, v: &ViewTargets) -> (Bloom, wgpu::BindGroup) { + let levels: Vec = + (1..=BLOOM_LEVELS).map(|i| target_view(device, "tc_bloom", [(v.size[0] >> i).max(1), (v.size[1] >> i).max(1)], self.hdr_format, 1)).collect(); + let fx = |src: &wgpu::TextureView| self.fx_bind_group(device, src, &self.black, &v.post_buf); + let down = std::iter::once(&v.accum).chain(&levels).take(levels.len()).map(fx).collect(); + let up = levels.iter().skip(1).map(fx).collect(); + let post_bg = self.fx_bind_group(device, &v.accum, levels.first().unwrap_or(&self.black), &v.post_buf); + (Bloom { levels, down, up }, post_bg) + } + + fn targets(&mut self, device: &wgpu::Device, id: u64, size: [u32; 2], bloom: bool) -> Option<&mut ViewTargets> { + let size = [size[0].clamp(1, 8192), size[1].clamp(1, 8192)]; + if self.views.get(&id).is_none_or(|v| v.size != size) { + let v = self.create_targets(device, size); + self.views.insert(id, v); + } + if bloom + && let Some(v) = self.views.get(&id) + && v.bloom.is_none() + { + let (b, post_bg) = self.create_bloom(device, v); + let v = self.views.get_mut(&id)?; + v.bloom = Some(b); + v.post_bg = post_bg; + } + let (env_view, shadow_view) = (&self.env.view, &self.shadow.view); + let (bgl, env_sampler, shadow_sampler) = (&self.globals_bgl, &self.env_sampler, &self.shadow_sampler); + let v = self.views.get_mut(&id)?; + if v.globals_bg.is_none() { + v.globals_bg = Some(device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("tc_globals"), + layout: bgl, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: v.globals.as_entire_binding() }, + wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(env_view) }, + wgpu::BindGroupEntry { binding: 2, resource: wgpu::BindingResource::Sampler(env_sampler) }, + wgpu::BindGroupEntry { binding: 3, resource: wgpu::BindingResource::TextureView(shadow_view) }, + wgpu::BindGroupEntry { binding: 4, resource: wgpu::BindingResource::Sampler(shadow_sampler) }, + ], + })); + } + Some(v) + } + + /// Index ranges of `ranges` (first, count, material) whose texture set is visible (and is + /// `only`, when given), with that set. + fn drawable<'a>(&'a self, ranges: &'a [(u32, u32, usize)], visible: &'a [bool], only: Option) -> impl Iterator, &'a SetGpu)> + 'a { + ranges.iter().filter_map(move |&(start, count, material)| { + let ts = self.material_sets.get(material).copied().flatten()?; + if only.is_some_and(|o| o != ts) || !visible.get(ts).copied().unwrap_or(true) { + return None; + } + Some((start..start + count, self.sets.get(ts)?)) + }) + } + + /// Keep the key-light shadow map current; returns its view-projection. `light` points to + /// the light (world space). Re-renders only when the mesh, light or visible sets changed. + /// shortcut: casters are fully opaque (the dithered opacity cut-out is ignored) and the map is + /// one shared buffer, so two 3D views with different lights in one frame would share the last + /// light; upgrade to a per-view map if split 3D views get their own lights. + fn update_shadow(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, light: Vec3, visible: &[bool]) -> Option { + let l = light.try_normalize()?; + let (id, center, radius) = self.mesh.as_ref().map(|m| (m.id, m.center, m.radius))?; + if self.shadow.size != SHADOW_SIZE { + self.shadow.view = shadow_view(device, SHADOW_SIZE); + self.shadow.size = SHADOW_SIZE; + self.shadow.key = None; + for v in self.views.values_mut() { + v.globals_bg = None; + } + } + let key = (id, l.to_array().map(f32::to_bits)); + if self.shadow.key == Some(key) && self.shadow.visible == visible { + return Some(self.shadow.vp); + } + // An orthographic light frustum around the mesh's bounding sphere. + let up = if l.y.abs() > 0.99 { Vec3::Z } else { Vec3::Y }; + let vp = Mat4::orthographic_rh(-radius, radius, -radius, radius, radius * 0.5, radius * 3.5) * Mat4::look_at_rh(center + l * radius * 2.0, center, up); + queue.write_buffer(&self.shadow.buf, 0, bytemuck::bytes_of(&vp.to_cols_array_2d())); + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("tc_shadow"), + color_attachments: &[], + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &self.shadow.view, + depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(1.0), store: wgpu::StoreOp::Store }), + stencil_ops: None, + }), + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + pass.set_pipeline(&self.shadow_pipeline); + pass.set_bind_group(0, &self.shadow.bg, &[]); + if let Some(m) = &self.mesh { + pass.set_vertex_buffer(0, m.vb.slice(..)); + pass.set_index_buffer(m.ib.slice(..), wgpu::IndexFormat::Uint32); + for (range, _) in self.drawable(&m.draws, visible, None) { + pass.draw_indexed(range, 0, 0..1); + } + } + drop(pass); + self.shadow.key = Some(key); + self.shadow.visible = visible.to_vec(); + self.shadow.vp = vp; + Some(vp) + } + + /// Whether view `id` is still accumulating anti-aliasing samples: the shell should draw it + /// again even though nothing changed. + pub fn accumulating(&self, id: u64) -> bool { + self.views.get(&id).is_some_and(|v| v.history.as_ref().is_some_and(|(f, _)| f.shade.taa && f.kind == ViewKind::Scene) && v.samples + 1 < TAA_SAMPLES) + } + + /// Render `frame` into its offscreen target (and regenerate dirty mips). A 3D frame with + /// `shade.taa` that repeats the previous one adds a jittered sample to the average instead, + /// and renders nothing once [`TAA_SAMPLES`] are in. + pub fn prepare(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, frame: &Frame) { + self.generate_mips(device, encoder); + let rev = self.content_rev; + let sample = match self.views.get(&frame.view_id) { + Some(v) if frame.shade.taa && frame.kind == ViewKind::Scene && v.history.as_ref().is_some_and(|(f, r)| *r == rev && f == frame) => v.samples + 1, + _ => 0, + }; + if sample >= TAA_SAMPLES { + return; + } + let aspect = frame.size[0].max(1) as f32 / frame.size[1].max(1) as f32; + let mut vp = frame.camera.view_proj(aspect); + if sample > 0 { + // Sub-pixel jitter in clip space: a box filter over the pixel once averaged. + let (jx, jy) = (halton(sample, 2) - 0.5, halton(sample, 3) - 0.5); + vp = Mat4::from_translation(Vec3::new(2.0 * jx / frame.size[0].max(1) as f32, 2.0 * jy / frame.size[1].max(1) as f32, 0.0)) * vp; + } + let s = &frame.shade; + let (uv_view, uv_xform) = match frame.kind { + ViewKind::Scene => (0.0, [1.0, 1.0, 0.0, 0.0]), + ViewKind::Uv { zoom, pan, .. } => { + // Fit the 0..1 square into the view, keeping texels square. + let z = zoom.clamp(0.05, 64.0) * 0.92; + let (sx, sy) = if aspect >= 1.0 { (z / aspect, z) } else { (z, z * aspect) }; + (1.0, [sx, sy, pan[0], pan[1]]) + } + }; + // Data views (channels, mesh maps, UV checker, the 2D view) show values as they are: no + // exposure, tone mapping, effects or shadows. + let data = uv_view > 0.5 || s.view_mode != 0; + let fx = |v: f32| if s.post_effects && !data { v.clamp(0.0, 1.0) } else { 0.0 }; + let bloom = fx(s.bloom) > 0.0; + // The key light lives in the environment: turn it with it (the inverse of env_rotate in + // mesh.wgsl maps environment directions to world directions). + let (sin, cos) = s.env_rotation_deg.to_radians().sin_cos(); + let [lx, ly, lz, lw] = s.light; + let light = Vec3::new(cos * lx + sin * lz, ly, cos * lz - sin * lx); + let shadow_vp = if !data && s.shadows && lw > 0.0 { self.update_shadow(device, queue, encoder, light, &frame.visible) } else { None }; + let radius = self.mesh.as_ref().map_or(1.0, |m| m.radius); + // Background rays come from a perspective camera even in orthographic views (an ortho + // ray grid would show a single flat environment color). + let bg_vp = OrbitCamera { ortho: false, ..frame.camera }.view_proj(aspect); + let e = frame.camera.eye(); + let raw = GlobalsRaw { + view_proj: vp.to_cols_array_2d(), + inv_view_proj: bg_vp.inverse().to_cols_array_2d(), + eye: [e.x, e.y, e.z, 0.0], + env: [s.env_rotation_deg.to_radians(), (ENV_LEVELS - 1) as f32, s.ao_intensity, 0.0], + sh: self.sh, + mode: [s.view_mode as f32, s.channel as f32, s.height_strength, s.normal_y], + bg: [s.env_opacity, s.env_blur * (ENV_LEVELS - 1) as f32, uv_view, 0.0], + uv_xform, + light: [light.x, light.y, light.z, lw], + light_vp: shadow_vp.unwrap_or(Mat4::IDENTITY).to_cols_array_2d(), + // Normal offset: 1.5 shadow texels of the light frustum (2 × radius wide). + shadow: [if shadow_vp.is_some() { 1.0 } else { 0.0 }, 1.0 / SHADOW_SIZE as f32, radius * 3.0 / SHADOW_SIZE as f32, 0.0], + material: [if s.spec_gloss { 1.0 } else { 0.0 }, if s.parallax { 1.0 } else { 0.0 }, sample as f32, 0.0], + }; + let flag = |b: bool| if b { 1.0 } else { 0.0 }; + let post = PostRaw { + a: [2f32.powf(s.exposure_ev), flag(s.aces), flag(data), fx(s.bloom) * BLOOM_GAIN], + b: [fx(s.vignette), fx(s.sharpen), fx(s.grain), BLOOM_THRESHOLD], + }; + let Some(v) = self.targets(device, frame.view_id, frame.size, bloom) else { return }; + queue.write_buffer(&v.globals, 0, bytemuck::bytes_of(&raw)); + queue.write_buffer(&v.post_buf, 0, bytemuck::bytes_of(&post)); + v.ready = true; + v.history = Some((frame.clone(), rev)); + v.samples = sample; + let Some(v) = self.views.get(&frame.view_id) else { return }; + let Some(globals_bg) = v.globals_bg.as_ref() else { return }; + if sample == 0 { + self.scene_pass(encoder, v, &v.accum, globals_bg, frame); + } else { + self.scene_pass(encoder, v, &v.hdr, globals_bg, frame); + fullscreen_blend(encoder, "tc_accum", &v.accum, wgpu::LoadOp::Load, &self.accum_pipeline, &v.accum_bg, 1.0 / f64::from(sample + 1)); + } + if bloom && let Some(b) = &v.bloom { + let [pre, down, up] = &self.bloom_pipelines; + for (i, (level, bg)) in b.levels.iter().zip(&b.down).enumerate() { + fullscreen(encoder, "tc_bloom_down", level, CLEAR, if i == 0 { pre } else { down }, bg); + } + for (level, bg) in b.levels.iter().zip(&b.up).rev() { + fullscreen(encoder, "tc_bloom_up", level, wgpu::LoadOp::Load, up, bg); + } + } + fullscreen(encoder, "tc_post", &v.resolve_view, CLEAR, &self.post_pipeline, &v.post_bg); + } + + /// Background, mesh and wireframe into the view's MSAA HDR target, resolved to `resolve`. + fn scene_pass(&self, encoder: &mut wgpu::CommandEncoder, v: &ViewTargets, resolve: &wgpu::TextureView, globals_bg: &wgpu::BindGroup, frame: &Frame) { + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("tc_view"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &v.msaa, + resolve_target: Some(resolve), + depth_slice: None, + ops: wgpu::Operations { load: CLEAR, store: wgpu::StoreOp::Discard }, + })], + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &v.depth, + depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(1.0), store: wgpu::StoreOp::Discard }), + stencil_ops: None, + }), + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + pass.set_bind_group(0, globals_bg, &[]); + pass.set_pipeline(&self.bg_pipeline); + pass.draw(0..3, 0..1); + let Some(m) = &self.mesh else { return }; + let uv_ts = match frame.kind { + ViewKind::Scene => None, + ViewKind::Uv { ts, .. } => Some(ts), + }; + let (pipe, wire) = match uv_ts { + None => (&self.mesh_pipeline, frame.shade.wireframe_3d.then_some(&self.wire3d_pipeline)), + Some(_) => (&self.uv_pipeline, frame.shade.wireframe.then_some(&self.wire_pipeline)), + }; + pass.set_vertex_buffer(0, m.vb.slice(..)); + for (pipe, ib, ranges) in [(Some(pipe), &m.ib, &m.draws), (wire, &m.lines, &m.line_draws)] { + let Some(pipe) = pipe else { continue }; + pass.set_pipeline(pipe); + pass.set_index_buffer(ib.slice(..), wgpu::IndexFormat::Uint32); + for (range, set) in self.drawable(ranges, &frame.visible, uv_ts) { + pass.set_bind_group(1, &set.bind_group, &[]); + pass.draw_indexed(range, 0, 0..1); + } + } + } + + /// Draw view `id` into the current pass at `rect` (target pixels: x, y, w, h). + pub fn blit(&self, queue: &wgpu::Queue, pass: &mut wgpu::RenderPass<'_>, id: u64, rect: [f32; 4], target_size: [u32; 2]) { + let Some(v) = self.views.get(&id).filter(|v| v.ready) else { return }; + let raw = BlitRaw { rect, dst: [target_size[0] as f32, target_size[1] as f32, if self.target_format.is_srgb() { 1.0 } else { 0.0 }, 0.0] }; + queue.write_buffer(&v.blit_buf, 0, bytemuck::bytes_of(&raw)); + pass.set_pipeline(&self.blit_pipeline); + pass.set_bind_group(0, &v.blit_bg, &[]); + pass.draw(0..6, 0..1); + } + + /// Write the blit rectangle for view `id` (call from `prepare` when the pass can't write + /// buffers itself). + pub fn set_blit_rect(&self, queue: &wgpu::Queue, id: u64, rect: [f32; 4], target_size: [u32; 2]) { + if let Some(v) = self.views.get(&id) { + let raw = BlitRaw { rect, dst: [target_size[0] as f32, target_size[1] as f32, if self.target_format.is_srgb() { 1.0 } else { 0.0 }, 0.0] }; + queue.write_buffer(&v.blit_buf, 0, bytemuck::bytes_of(&raw)); + } + } + + /// Draw view `id` with the rectangle set by [`Renderer::set_blit_rect`]. + pub fn blit_prepared(&self, pass: &mut wgpu::RenderPass<'_>, id: u64) { + let Some(v) = self.views.get(&id).filter(|v| v.ready) else { return }; + pass.set_pipeline(&self.blit_pipeline); + pass.set_bind_group(0, &v.blit_bg, &[]); + pass.draw(0..6, 0..1); + } + + /// Read back the rendered view as tightly packed RGBA8 (sRGB-encoded), blocking. + pub fn read_view(&self, device: &wgpu::Device, queue: &wgpu::Queue, id: u64) -> Option<(u32, u32, Vec)> { + let v = self.views.get(&id)?; + let (w, h) = (v.size[0], v.size[1]); + let row = (w * 4).next_multiple_of(256); + let buf = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("tc_read"), + size: (row * h) as u64, + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, + mapped_at_creation: false, + }); + let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("tc_read") }); + enc.copy_texture_to_buffer( + wgpu::TexelCopyTextureInfo { texture: &v.resolve, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + wgpu::TexelCopyBufferInfo { buffer: &buf, layout: wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(row), rows_per_image: Some(h) } }, + wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + ); + queue.submit([enc.finish()]); + buf.slice(..).map_async(wgpu::MapMode::Read, |_| {}); + let _ = device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); + let data = buf.slice(..).get_mapped_range().ok()?; + let mut out = Vec::with_capacity((w * h * 4) as usize); + for y in 0..h as usize { + let start = y * row as usize; + out.extend_from_slice(data.get(start..start + (w * 4) as usize)?); + } + Some((w, h, out)) + } + + /// Forget a view's targets (e.g. a closed panel). + pub fn drop_view(&mut self, id: u64) { + self.views.remove(&id); + } +} diff --git a/crates/render/src/shaders/mesh.wgsl b/crates/render/src/shaders/mesh.wgsl new file mode 100644 index 00000000..eb7bf82a --- /dev/null +++ b/crates/render/src/shaders/mesh.wgsl @@ -0,0 +1,393 @@ +// TextureCraft viewport shaders: metal/roughness (or specular/glossiness) PBR with image-based +// lighting and optional parallax occlusion mapping, the 2D UV view, +// the background, debug views and the 3D wireframe. Output is scene-linear HDR, premultiplied by +// alpha = scene coverage over the flat studio backdrop; exposure, tone mapping and sRGB encoding +// happen in the post pass (post.wgsl). WebGPU/WebGL2-safe (fragment passes only). + +struct Globals { + view_proj: mat4x4, + inv_view_proj: mat4x4, // background rays: a perspective camera even when the view is orthographic + eye: vec4, // xyz eye position, w - + env: vec4, // x rotation (radians), y max lod, z AO intensity, w - + sh: array, 9>, + mode: vec4, // x view mode (0 material, 1 channel, 2 mesh map, 3 UV checker), y channel index, z height strength, w normal Y sign + bg: vec4, // x environment opacity, y background blur lod, z UV view (1) or 3D (0), w wireframe opacity + uv_xform: vec4, // UV view: scale xy, offset zw (clip space) + light: vec4, // xyz world direction to the key light (rotated with the environment), w intensity (0 = off) + light_vp: mat4x4, // world -> shadow map clip space + shadow: vec4, // x shadows on (1), y shadow map texel (uv), z normal offset (world units), w - + material: vec4, // x specular/glossiness shading (1), y parallax occlusion (1), z accumulation sample index, w - +}; + +@group(0) @binding(0) var g: Globals; +@group(0) @binding(1) var env_tex: texture_2d; +@group(0) @binding(2) var env_samp: sampler; +@group(0) @binding(3) var shadow_map: texture_depth_2d; +@group(0) @binding(4) var shadow_samp: sampler_comparison; + +@group(1) @binding(0) var t_base: texture_2d; +@group(1) @binding(1) var t_orm: texture_2d; +@group(1) @binding(2) var t_nrm: texture_2d; +@group(1) @binding(3) var t_height: texture_2d; +@group(1) @binding(4) var t_emis: texture_2d; +@group(1) @binding(5) var t_aux: texture_2d; +@group(1) @binding(6) var samp: sampler; + +struct VIn { + @location(0) pos: vec3, + @location(1) nrm: vec3, + @location(2) tan: vec4, + @location(3) uv: vec2, +}; + +struct VOut { + @builtin(position) clip: vec4, + @location(0) wpos: vec3, + @location(1) nrm: vec3, + @location(2) tan: vec4, + @location(3) uv: vec2, +}; + +@vertex +fn vs_mesh(v: VIn) -> VOut { + var o: VOut; + o.clip = g.view_proj * vec4(v.pos, 1.0); + o.wpos = v.pos; + o.nrm = v.nrm; + o.tan = v.tan; + o.uv = v.uv; + return o; +} + +@vertex +fn vs_uv(v: VIn) -> VOut { + var o: VOut; + let p = vec2(v.uv.x * 2.0 - 1.0, 1.0 - v.uv.y * 2.0) * g.uv_xform.xy + g.uv_xform.zw; + o.clip = vec4(p, 0.5, 1.0); + o.wpos = v.pos; + o.nrm = v.nrm; + o.tan = v.tan; + o.uv = v.uv; + return o; +} + +const PI: f32 = 3.14159265; + +fn env_uv(d: vec3) -> vec2 { + let c = cos(g.env.x); + let s = sin(g.env.x); + let r = vec3(c * d.x - s * d.z, d.y, s * d.x + c * d.z); + let phi = atan2(r.z, r.x); + return vec2(fract(phi / (2.0 * PI) + 1.0), acos(clamp(r.y, -1.0, 1.0)) / PI); +} + +fn env_rotate(d: vec3) -> vec3 { + let c = cos(g.env.x); + let s = sin(g.env.x); + return vec3(c * d.x - s * d.z, d.y, s * d.x + c * d.z); +} + +fn sh_irradiance(n0: vec3) -> vec3 { + let n = env_rotate(n0); + let x = n.x; + let y = n.y; + let z = n.z; + var r = g.sh[0].xyz * 0.282095; + r += g.sh[1].xyz * (0.488603 * y); + r += g.sh[2].xyz * (0.488603 * z); + r += g.sh[3].xyz * (0.488603 * x); + r += g.sh[4].xyz * (1.092548 * x * y); + r += g.sh[5].xyz * (1.092548 * y * z); + r += g.sh[6].xyz * (0.315392 * (3.0 * z * z - 1.0)); + r += g.sh[7].xyz * (1.092548 * x * z); + r += g.sh[8].xyz * (0.546274 * (x * x - y * y)); + return max(r, vec3(0.0)); +} + +// Analytic split-sum environment BRDF (Karis, "Physically Based Shading on Mobile"). +fn env_brdf(f0: vec3, rough: f32, nov: f32) -> vec3 { + let c0 = vec4(-1.0, -0.0275, -0.572, 0.022); + let c1 = vec4(1.0, 0.0425, 1.04, -0.04); + let r = rough * c0 + c1; + let a004 = min(r.x * r.x, exp2(-9.28 * nov)) * r.x + r.y; + let ab = vec2(-1.04, 1.04) * a004 + r.zw; + return f0 * ab.x + ab.y; +} + +fn to_linear(c: vec3) -> vec3 { + let lo = c / 12.92; + let hi = pow((c + 0.055) / 1.055, vec3(2.4)); + return select(hi, lo, c <= vec3(0.04045)); +} + +// Data views bypass exposure and tone mapping: linear color, clamped. +fn data_out(c: vec3) -> vec4 { + return vec4(clamp(c, vec3(0.0), vec3(1.0)), 1.0); +} + +// A value shown as-is on screen (normals, height, masks): undo the post pass's sRGB encoding. +fn raw_out(v: vec3) -> vec4 { + return vec4(to_linear(clamp(v, vec3(0.0), vec3(1.0))), 1.0); +} + +// Key-light visibility from the shadow map: 3x3 hardware-filtered PCF taps. +fn shadow_factor(wpos: vec3, n_geo: vec3) -> f32 { + let p = g.light_vp * vec4(wpos + n_geo * g.shadow.z, 1.0); + let ndc = p.xyz / p.w; + let uv = vec2(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5); + if (any(uv < vec2(0.0)) || any(uv > vec2(1.0)) || ndc.z >= 1.0) { + return 1.0; + } + let depth = ndc.z - 0.0005; + var s = 0.0; + for (var y = -1; y <= 1; y++) { + for (var x = -1; x <= 1; x++) { + s += textureSampleCompareLevel(shadow_map, shadow_samp, uv + vec2(f32(x), f32(y)) * g.shadow.y * 1.5, depth); + } + } + return s / 9.0; +} + +// Reoriented normal mapping of a detail normal onto a base normal (tangent space, unpacked). +fn rnm(base: vec3, detail: vec3) -> vec3 { + let t = base + vec3(0.0, 0.0, 1.0); + let u = detail * vec3(-1.0, -1.0, 1.0); + return normalize(t * dot(t, u) - u * t.z); +} + +fn hash12(p: vec2) -> f32 { + var p3 = fract(vec3(p.xyx) * 0.1031); + p3 += dot(p3, p3.yzx + 33.33); + return fract((p3.x + p3.y) * p3.z); +} + +const PARALLAX_STEPS: i32 = 16; + +// Parallax occlusion mapping: march the view ray down through the signed height field (the mesh +// surface at height 0; height 1 lies strength/16 UV units above it, the same scale as +// height-to-normal) and return the UV where it first goes below the heights. `v_ts`: tangent-space +// direction to the eye (+y = up the image, so v grows along -y). A fixed step count keeps the +// loop free of data-dependent exits (derivatives stay valid after it). +fn parallax_uv(uv: vec2, v_ts: vec3) -> vec2 { + let dx = dpdx(uv); + let dy = dpdy(uv); + // UV shift per unit of height along the ray (grazing angles capped). + let shift = vec2(v_ts.x, -v_ts.y) / max(v_ts.z, 0.15) * (g.mode.z / 16.0); + var prev_z = 1.0; + var prev_d = 1.0 - textureSampleGrad(t_height, samp, uv + shift, dx, dy).r; + // Heights at the top of the range already hide the ray at entry. + var found = prev_d <= 0.0; + var hit = select(-1.0, 1.0, found); + for (var k = 1; k <= PARALLAX_STEPS; k++) { + let z = 1.0 - 2.0 * f32(k) / f32(PARALLAX_STEPS); + let d = z - textureSampleGrad(t_height, samp, uv + shift * z, dx, dy).r; + if (!found && d <= 0.0) { + // Linear refinement between the last step above and the first below the surface. + hit = mix(prev_z, z, prev_d / max(prev_d - d, 1e-5)); + found = true; + } + prev_z = z; + prev_d = d; + } + return uv + shift * hit; +} + +@fragment +fn fs_mesh(i: VOut, @builtin(front_facing) front: bool) -> @location(0) vec4 { + var n_geo = normalize(i.nrm); + if (!front) { + n_geo = -n_geo; + } + let t = normalize(i.tan.xyz - n_geo * dot(n_geo, i.tan.xyz)); + let b = cross(n_geo, t) * i.tan.w; + let v = normalize(g.eye.xyz - i.wpos); + var uv = i.uv; + if (g.material.y > 0.5) { + uv = parallax_uv(uv, vec3(dot(v, t), dot(v, b), dot(v, n_geo))); + } + let base_s = textureSample(t_base, samp, uv); + let orm = textureSample(t_orm, samp, uv); + let nrm_s = textureSample(t_nrm, samp, uv).xyz; + let emis = textureSample(t_emis, samp, uv).rgb; + let aux = textureSample(t_aux, samp, uv); + let dims = vec2(textureDimensions(t_height)); + let ts = 1.0 / dims; + let hgt = textureSample(t_height, samp, uv); + let hc = hgt.r; + let hl = textureSample(t_height, samp, uv - vec2(ts.x, 0.0)).r; + let hr = textureSample(t_height, samp, uv + vec2(ts.x, 0.0)).r; + let hu = textureSample(t_height, samp, uv - vec2(0.0, ts.y)).r; + let hd = textureSample(t_height, samp, uv + vec2(0.0, ts.y)).r; + + // Tangent-space normal: height gradient (+Y = up the image) combined with the normal channel. + // Same slope rule as texture export: strength 1 tilts 45° for a height change of 1 over 1/16 of the width. + let k = g.mode.z * dims.x / 16.0; + let n_height = normalize(vec3(-(hr - hl) * 0.5 * k, (hd - hu) * 0.5 * k, 1.0)); + var n_detail = nrm_s * 2.0 - 1.0; + n_detail.y *= g.mode.w; + let n_ts = rnm(n_height, normalize(n_detail)); + let n = normalize(t * n_ts.x + b * n_ts.y + n_geo * n_ts.z); + + let mode = i32(g.mode.x + 0.5); + if (mode == 1) { + let ch = i32(g.mode.y + 0.5); + switch ch { + case 0: { return data_out(base_s.rgb); } + case 1: { return data_out(vec3(orm.b)); } + case 2: { return data_out(vec3(orm.g)); } + case 3: { return raw_out(n_ts * 0.5 + 0.5); } + case 4: { return raw_out(vec3(hc * 0.5 + 0.5)); } + case 5: { return data_out(emis); } + case 6: { return raw_out(vec3(base_s.a)); } + case 7: { return raw_out(vec3(orm.r)); } + case 9: { return data_out(hgt.gba); } + case 10: { return data_out(vec3(1.0 - orm.g)); } + default: { return raw_out(vec3(orm.a)); } + } + } + if (mode == 2) { + return raw_out(aux.rgb); + } + if (mode == 3) { + let c = floor(uv * 32.0); + let chk = (c.x + c.y) - 2.0 * floor((c.x + c.y) * 0.5); + let fine = floor(uv * 256.0); + let f2 = (fine.x + fine.y) - 2.0 * floor((fine.x + fine.y) * 0.5); + let col = mix(vec3(0.85, 0.85, 0.85), vec3(0.18, 0.2, 0.24), chk) * (0.92 + 0.08 * f2); + let tint = vec3(fract(uv.x), fract(uv.y), 0.5) * 0.25; + let diff = max(dot(n, normalize(vec3(0.4, 0.8, 0.5))), 0.0) * 0.6 + 0.4; + return data_out((col + tint) * diff); + } + + // Opacity: dithered alpha test (a new pattern per accumulated frame, so it averages out). + if (base_s.a < hash12(i.clip.xy + g.material.z * vec2(17.0, 59.0))) { + discard; + } + + let albedo = base_s.rgb; + let ao = mix(1.0, orm.r, g.env.z); + let rough = clamp(orm.g, 0.045, 1.0); + let metal = orm.b; + let nov = clamp(abs(dot(n, v)), 1e-4, 1.0); + var f0 = mix(vec3(0.08 * orm.a), albedo, metal); + var diffuse_col = albedo * (1.0 - metal); + if (g.material.x > 0.5) { + // Specular/glossiness: the Specular color is F0 and base color the diffuse color, with + // the energy the specular takes removed (roughness already is 1 - glossiness). + f0 = hgt.gba; + diffuse_col = albedo * (1.0 - max(f0.r, max(f0.g, f0.b))); + } + + let diffuse = sh_irradiance(n) * diffuse_col * ao; + let r = reflect(-v, n); + let pre = textureSampleLevel(env_tex, env_samp, env_uv(r), rough * g.env.y).rgb; + // Horizon fading: reflections that point below the geometric surface are dimmed. + let horizon = clamp(1.0 + 1.3 * dot(r, n_geo), 0.0, 1.0); + let spec = pre * env_brdf(f0, rough, nov) * ao * horizon * horizon; + + var direct = vec3(0.0); + if (g.light.w > 0.0) { + let l = normalize(g.light.xyz); + let h = normalize(l + v); + let nol = max(dot(n, l), 0.0); + let noh = max(dot(n, h), 0.0); + let a = rough * rough; + let a2 = a * a; + let d = a2 / (PI * pow(noh * noh * (a2 - 1.0) + 1.0, 2.0)); + let vis = 0.5 / max(nol * sqrt(nov * nov * (1.0 - a2) + a2) + nov * sqrt(nol * nol * (1.0 - a2) + a2), 1e-5); + let f = f0 + (vec3(1.0) - f0) * pow(1.0 - max(dot(v, h), 0.0), 5.0); + direct = ((vec3(1.0) - f) * diffuse_col / PI + d * vis * f) * nol * g.light.w; + if (g.shadow.x > 0.5 && nol > 0.0) { + direct *= shadow_factor(i.wpos, n_geo); + } + } + + return vec4(diffuse + spec + direct + emis, 1.0); +} + +// The 2D UV view: unlit material color (or the selected debug channel) over the UV layout. +@fragment +fn fs_uv(i: VOut) -> @location(0) vec4 { + let base_s = textureSample(t_base, samp, i.uv); + let orm = textureSample(t_orm, samp, i.uv); + let mode = i32(g.mode.x + 0.5); + if (mode == 1) { + let ch = i32(g.mode.y + 0.5); + switch ch { + case 1: { return data_out(vec3(orm.b)); } + case 2: { return data_out(vec3(orm.g)); } + case 3: { return raw_out(textureSample(t_nrm, samp, i.uv).rgb); } + case 4: { return raw_out(vec3(textureSample(t_height, samp, i.uv).r * 0.5 + 0.5)); } + case 5: { return data_out(textureSample(t_emis, samp, i.uv).rgb); } + case 6: { return raw_out(vec3(base_s.a)); } + case 7: { return raw_out(vec3(orm.r)); } + case 8: { return raw_out(vec3(orm.a)); } + case 9: { return data_out(textureSample(t_height, samp, i.uv).gba); } + case 10: { return data_out(vec3(1.0 - orm.g)); } + default: { return data_out(base_s.rgb); } + } + } + if (mode == 2) { + return raw_out(textureSample(t_aux, samp, i.uv).rgb); + } + if (mode == 3) { + let c = floor(i.uv * 32.0); + let chk = (c.x + c.y) - 2.0 * floor((c.x + c.y) * 0.5); + return data_out(mix(vec3(0.85), vec3(0.18, 0.2, 0.24), chk)); + } + // Lightly shade by the AO so the flat 2D material view still reads as a surface. + return data_out(base_s.rgb * mix(1.0, orm.r, 0.5)); +} + +// Background: the blurred environment, premultiplied by its opacity (the post pass puts the flat +// studio backdrop behind it). +struct BgOut { + @builtin(position) pos: vec4, + @location(0) ndc: vec2, +}; + +@vertex +fn vs_bg(@builtin(vertex_index) vi: u32) -> BgOut { + var p = array, 3>(vec2(-1.0, -3.0), vec2(-1.0, 1.0), vec2(3.0, 1.0)); + var o: BgOut; + o.pos = vec4(p[vi], 1.0, 1.0); + o.ndc = p[vi]; + return o; +} + +@fragment +fn fs_bg(i: BgOut) -> @location(0) vec4 { + let o = g.bg.x; + if (g.bg.z > 0.5 || o <= 0.001) { + return vec4(0.0); + } + let a = g.inv_view_proj * vec4(i.ndc, 0.0, 1.0); + let b = g.inv_view_proj * vec4(i.ndc, 1.0, 1.0); + let d = normalize(b.xyz / b.w - a.xyz / a.w); + let env = textureSampleLevel(env_tex, env_samp, env_uv(d), g.bg.y).rgb; + return vec4(env * o, o); +} + +// UV wireframe in the 2D view. +@fragment +fn fs_wire(i: VOut) -> @location(0) vec4 { + return vec4(0.6, 0.6, 0.6, 0.16); +} + +// 3D wireframe over the shaded mesh: depth-tested, pulled slightly toward the camera so the +// lines win against their own triangles. +@vertex +fn vs_wire3d(v: VIn) -> VOut { + var o: VOut; + o.wpos = v.pos; + o.nrm = v.nrm; + o.tan = v.tan; + o.uv = v.uv; + o.clip = g.view_proj * vec4(v.pos + (g.eye.xyz - v.pos) * 0.002, 1.0); + return o; +} + +@fragment +fn fs_wire3d(i: VOut) -> @location(0) vec4 { + return vec4(0.35, 0.35, 0.35, 0.45); +} diff --git a/crates/render/src/shaders/post.wgsl b/crates/render/src/shaders/post.wgsl new file mode 100644 index 00000000..195d8244 --- /dev/null +++ b/crates/render/src/shaders/post.wgsl @@ -0,0 +1,133 @@ +// Viewport post processing, appended to util.wgsl (uses `src`, `src_samp`, `vs_full`): the +// resolved scene (linear HDR, premultiplied by scene coverage) → flat studio backdrop, exposure, +// tone mapping, optional bloom / sharpen / vignette / grain, sRGB encoding. + +struct Post { + a: vec4, // x exposure multiplier (2^EV), y tone mapping (0 linear, 1 ACES), z data view (1: no exposure, tone mapping or effects), w bloom strength + b: vec4, // x vignette, y sharpen, z grain, w bloom threshold +}; + +@group(0) @binding(3) var post: Post; +@group(0) @binding(4) var bloom_tex: texture_2d; + +// Narkowicz's fitted ACES filmic curve (pre-scaled so mid gray stays put). +fn aces(x: vec3) -> vec3 { + let v = x * 0.6; + return clamp((v * (2.51 * v + 0.03)) / (v * (2.43 * v + 0.59) + 0.14), vec3(0.0), vec3(1.0)); +} + +fn to_srgb(c: vec3) -> vec3 { + let lo = c * 12.92; + let hi = 1.055 * pow(max(c, vec3(0.0)), vec3(1.0 / 2.4)) - 0.055; + return select(hi, lo, c <= vec3(0.0031308)); +} + +fn tonemap(hdr: vec3) -> vec3 { + let x = max(hdr, vec3(0.0)) * post.a.x; + if (post.a.y > 0.5) { + return aces(x); + } + return min(x, vec3(1.0)); +} + +// The flat studio gray behind the scene, with a gentle falloff so the stage doesn't look like a hole. +fn backdrop(uv: vec2) -> vec3 { + let r = length(uv * 2.0 - 1.0) * 0.35; + return vec3(0.0232 * (1.06 - r * r)); +} + +// Display-linear color of one pixel (scene over backdrop), without bloom. +fn shade_px(uv: vec2) -> vec3 { + let c = textureSampleLevel(src, src_samp, uv, 0.0); + let a = clamp(c.a, 0.0, 1.0); + let s = c.rgb / max(c.a, 1e-4); + var m = tonemap(s); + if (post.a.z > 0.5) { + m = clamp(s, vec3(0.0), vec3(1.0)); + } + return mix(backdrop(uv), m, a); +} + +fn hash12(p: vec2) -> f32 { + var p3 = fract(vec3(p.xyx) * 0.1031); + p3 += dot(p3, p3.yzx + 33.33); + return fract((p3.x + p3.y) * p3.z); +} + +@fragment +fn fs_post(i: FsOut) -> @location(0) vec4 { + var col: vec3; + if (post.a.w > 0.0) { + // Bloom adds light in scene space, so it is tone mapped with the pixel; over the backdrop + // it glows on its own. + let c = textureSampleLevel(src, src_samp, i.uv, 0.0); + let glow = textureSampleLevel(bloom_tex, src_samp, i.uv, 0.0).rgb * post.a.w; + col = mix(backdrop(i.uv) + tonemap(glow), tonemap(c.rgb / max(c.a, 1e-4) + glow), clamp(c.a, 0.0, 1.0)); + } else { + col = shade_px(i.uv); + } + if (post.b.y > 0.0) { + // Unsharp mask on the display image (no HDR halos around highlights). + let t = 1.0 / vec2(textureDimensions(src)); + let blur = (shade_px(i.uv + vec2(t.x, 0.0)) + shade_px(i.uv - vec2(t.x, 0.0)) + shade_px(i.uv + vec2(0.0, t.y)) + shade_px(i.uv - vec2(0.0, t.y))) * 0.25; + col = max(col + (shade_px(i.uv) - blur) * post.b.y * 2.0, vec3(0.0)); + } + if (post.b.x > 0.0) { + let q = length(i.uv * 2.0 - 1.0) * 0.70710678; + col *= 1.0 - post.b.x * q * q * q; + } + var out = to_srgb(col); + if (post.b.z > 0.0) { + // Stable film grain, strongest in the mid tones. + let l = dot(out, vec3(0.2126, 0.7152, 0.0722)); + out += (hash12(i.pos.xy) - 0.5) * post.b.z * 0.12 * (1.0 - abs(l * 2.0 - 1.0) * 0.6); + } + return vec4(out, 1.0); +} + +// Bloom: a dual-filter blur pyramid (Bjørge, "Bandwidth-Efficient Rendering", SIGGRAPH 2015) over a +// soft-knee bright pass. +fn bright(c: vec3) -> vec3 { + let t = post.b.w; + let knee = t * 0.5; + let br = max(c.r, max(c.g, c.b)) * post.a.x; + var soft = clamp(br - t + knee, 0.0, 2.0 * knee); + soft = soft * soft / (4.0 * knee + 1e-4); + return c * (max(soft, br - t) / max(br, 1e-4)); +} + +fn down(uv: vec2) -> vec3 { + let h = 1.0 / vec2(textureDimensions(src)); + var s = textureSampleLevel(src, src_samp, uv, 0.0).rgb * 4.0; + s += textureSampleLevel(src, src_samp, uv - h, 0.0).rgb; + s += textureSampleLevel(src, src_samp, uv + h, 0.0).rgb; + s += textureSampleLevel(src, src_samp, uv + vec2(h.x, -h.y), 0.0).rgb; + s += textureSampleLevel(src, src_samp, uv - vec2(h.x, -h.y), 0.0).rgb; + return s / 8.0; +} + +@fragment +fn fs_bloom_pre(i: FsOut) -> @location(0) vec4 { + return vec4(bright(down(i.uv)), 1.0); +} + +@fragment +fn fs_bloom_down(i: FsOut) -> @location(0) vec4 { + return vec4(down(i.uv), 1.0); +} + +// Upsample the smaller level (tent of 8 taps) and add it onto the larger one (additive blend). +@fragment +fn fs_bloom_up(i: FsOut) -> @location(0) vec4 { + let h = 1.0 / vec2(textureDimensions(src)); + let uv = i.uv; + var s = textureSampleLevel(src, src_samp, uv + vec2(-2.0 * h.x, 0.0), 0.0).rgb; + s += textureSampleLevel(src, src_samp, uv + vec2(2.0 * h.x, 0.0), 0.0).rgb; + s += textureSampleLevel(src, src_samp, uv + vec2(0.0, -2.0 * h.y), 0.0).rgb; + s += textureSampleLevel(src, src_samp, uv + vec2(0.0, 2.0 * h.y), 0.0).rgb; + s += textureSampleLevel(src, src_samp, uv + h, 0.0).rgb * 2.0; + s += textureSampleLevel(src, src_samp, uv - h, 0.0).rgb * 2.0; + s += textureSampleLevel(src, src_samp, uv + vec2(h.x, -h.y), 0.0).rgb * 2.0; + s += textureSampleLevel(src, src_samp, uv - vec2(h.x, -h.y), 0.0).rgb * 2.0; + return vec4(s / 12.0, 1.0); +} diff --git a/crates/render/src/shaders/shadow.wgsl b/crates/render/src/shaders/shadow.wgsl new file mode 100644 index 00000000..7f17495b --- /dev/null +++ b/crates/render/src/shaders/shadow.wgsl @@ -0,0 +1,8 @@ +// Depth-only key-light shadow map pass. + +@group(0) @binding(0) var light_vp: mat4x4; + +@vertex +fn vs_shadow(@location(0) pos: vec3) -> @builtin(position) vec4 { + return light_vp * vec4(pos, 1.0); +} diff --git a/crates/render/src/shaders/util.wgsl b/crates/render/src/shaders/util.wgsl new file mode 100644 index 00000000..658a476d --- /dev/null +++ b/crates/render/src/shaders/util.wgsl @@ -0,0 +1,61 @@ +// Fullscreen helpers: mip generation and blitting the viewport into the UI pass. + +struct FsOut { + @builtin(position) pos: vec4, + @location(0) uv: vec2, +}; + +@vertex +fn vs_full(@builtin(vertex_index) vi: u32) -> FsOut { + var p = array, 3>(vec2(-1.0, -3.0), vec2(-1.0, 1.0), vec2(3.0, 1.0)); + var o: FsOut; + o.pos = vec4(p[vi], 0.0, 1.0); + o.uv = vec2(p[vi].x * 0.5 + 0.5, 0.5 - p[vi].y * 0.5); + return o; +} + +@group(0) @binding(0) var src: texture_2d; +@group(0) @binding(1) var src_samp: sampler; + +@fragment +fn fs_mip(i: FsOut) -> @location(0) vec4 { + return textureSample(src, src_samp, i.uv); +} + +struct Blit { + rect: vec4, // destination x, y, w, h in target pixels + dst: vec4, // target w, h, decode sRGB (1 when the target format is sRGB), - +}; + +@group(0) @binding(2) var blit: Blit; + +struct BlitOut { + @builtin(position) pos: vec4, + @location(0) uv: vec2, +}; + +@vertex +fn vs_blit(@builtin(vertex_index) vi: u32) -> BlitOut { + var c = array, 6>(vec2(0.0, 0.0), vec2(1.0, 0.0), vec2(0.0, 1.0), vec2(0.0, 1.0), vec2(1.0, 0.0), vec2(1.0, 1.0)); + let t = c[vi]; + let px = blit.rect.xy + t * blit.rect.zw; + var o: BlitOut; + o.pos = vec4(px.x / blit.dst.x * 2.0 - 1.0, 1.0 - px.y / blit.dst.y * 2.0, 0.0, 1.0); + o.uv = t; + return o; +} + +fn to_linear(c: vec3) -> vec3 { + let lo = c / 12.92; + let hi = pow((c + 0.055) / 1.055, vec3(2.4)); + return select(hi, lo, c <= vec3(0.04045)); +} + +@fragment +fn fs_blit(i: BlitOut) -> @location(0) vec4 { + let c = textureSample(src, src_samp, i.uv); + if (blit.dst.z > 0.5) { + return vec4(to_linear(c.rgb), 1.0); + } + return vec4(c.rgb, 1.0); +} diff --git a/crates/render/src/trace.rs b/crates/render/src/trace.rs new file mode 100644 index 00000000..45fbb62d --- /dev/null +++ b/crates/render/src/trace.rs @@ -0,0 +1,799 @@ +//! Progressive CPU path tracing for Rendering mode: the textured mesh under the environment, +//! with the raster viewport's material conventions (metallic/roughness or specular/glossiness +//! GGX, specular level, +//! height-to-normal slope rule, normal channel blended with RNM, emissive, opacity) but real +//! multi-bounce light transport. Each [`Tracer::pass`] adds one sample per pixel. + +use std::f32::consts::{PI, TAU}; +use std::sync::Arc; + +use rayon::prelude::*; +use texturecraft_compose::{Composite, ImageRes}; +use texturecraft_core::project::{DisplaySettings, ToneMapping}; +use texturecraft_core::Channel; +use texturecraft_geom::glam::{Vec2, Vec3}; +use texturecraft_geom::{Bvh, Mesh, OrbitCamera}; + +use crate::ibl::EnvImage; + +/// One texture set's composited channels. +#[derive(Clone)] +pub struct TraceSet { + /// Mesh material the set covers. + pub material: usize, + pub composite: Composite, + pub height_strength: f32, +} + +/// Everything a pass reads (cheap to clone: shared or copy-on-write data). +#[derive(Clone)] +pub struct TraceScene { + pub mesh: Arc, + pub bvh: Arc, + pub sets: Vec, + pub env: Arc, + /// Environment rotation around +Y (radians), as in the viewport. + pub env_rotation: f32, + /// Texture set per triangle (`usize::MAX`: none, default material). + tri_set: Arc>, + /// Importance sampling of the environment (None for an empty image). + light: Option>, + /// Specular/glossiness shading (Specular color is F0, base color is diffuse). + pub spec_gloss: bool, + /// Ray offset off surfaces, scaled to the mesh. + eps: f32, +} + +impl TraceScene { + pub fn new(mesh: Arc, bvh: Arc, sets: Vec, env: Arc, env_rotation: f32) -> TraceScene { + let mut tri_set = vec![usize::MAX; mesh.triangle_count()]; + let set_of = |material: usize| sets.iter().position(|s| s.material == material).unwrap_or(usize::MAX); + if mesh.submeshes.is_empty() { + tri_set.fill(set_of(0)); + } + for sm in &mesh.submeshes { + let set = set_of(sm.material); + let first = sm.start as usize / 3; + for t in tri_set.iter_mut().skip(first).take(sm.count as usize / 3) { + *t = set; + } + } + let (lo, hi) = mesh.bounds(); + let eps = ((hi - lo).length() * 2e-5).max(1e-6); + let light = EnvSampler::new(&env).map(Arc::new); + TraceScene { mesh, bvh, sets, env, env_rotation, tri_set: Arc::new(tri_set), light, spec_gloss: false, eps } + } + + /// The scene as `display` shows it: the imported environment `env_image` (when the display + /// names a resource and it decodes) or the built-in one, its rotation and the shading mode. + pub fn for_display(mesh: Arc, bvh: Arc, sets: Vec, display: &DisplaySettings, env_image: Option<&ImageRes>) -> TraceScene { + let imported = env_image.and_then(|i| EnvImage::from_rgba(i.width, i.height, &i.rgba, i.srgb)); + let env = imported.unwrap_or_else(|| crate::ibl::environment(&display.environment, 1024)); + let mut scene = TraceScene::new(mesh, bvh, sets, Arc::new(env), display.env_rotation.to_radians()); + scene.spec_gloss = display.spec_gloss; + scene + } + + /// World direction to environment space. + fn to_env(&self, d: Vec3) -> Vec3 { + let (s, c) = self.env_rotation.sin_cos(); + Vec3::new(c * d.x - s * d.z, d.y, s * d.x + c * d.z) + } + + fn env(&self, d: Vec3) -> Vec3 { + self.env.sample(self.to_env(d)) + } + + /// Solid-angle density of [`Self::sample_env`] picking world direction `d`. + fn env_pdf(&self, d: Vec3) -> f32 { + self.light.as_ref().map_or(0.0, |l| l.pdf(self.to_env(d))) + } + + /// A world direction toward the environment, chosen by brightness, with its density. + fn sample_env(&self, rng: &mut Rng) -> Option<(Vec3, f32)> { + let (e, pdf) = self.light.as_ref()?.sample(rng.next(), rng.next(), rng.next(), rng.next())?; + let (s, c) = self.env_rotation.sin_cos(); + Some((Vec3::new(c * e.x + s * e.z, e.y, -s * e.x + c * e.z), pdf)) + } +} + +/// Piecewise-constant importance sampling of an equirect environment by luminance and sin(theta). +struct EnvSampler { + width: usize, + height: usize, + /// Cumulative row probabilities (last = 1). + rows: Vec, + /// Cumulative probabilities within each row (last of each row = 1). + cols: Vec, + /// Probability of each pixel. + prob: Vec, +} + +impl EnvSampler { + fn new(env: &EnvImage) -> Option { + let (w, h) = (env.width as usize, env.height as usize); + if w == 0 || h == 0 || env.data.len() < w * h { + return None; + } + let mut weight: Vec = env.data.iter().take(w * h).map(|c| luminance(Vec3::from(*c)).max(0.0)).collect(); + for (y, row) in weight.chunks_mut(w).enumerate() { + let sin = (PI * (y as f32 + 0.5) / h as f32).sin(); + row.iter_mut().for_each(|v| *v = (*v + 1e-4) * sin); + } + let total: f32 = weight.iter().sum(); + if !total.is_finite() || total <= 0.0 { + return None; + } + let prob: Vec = weight.iter().map(|v| v / total).collect(); + let mut rows = Vec::with_capacity(h); + let mut cols = Vec::with_capacity(w * h); + let mut acc_rows = 0.0; + for row in prob.chunks(w) { + let sum: f32 = row.iter().sum(); + acc_rows += sum; + rows.push(acc_rows); + let mut acc = 0.0; + for p in row { + acc += if sum > 0.0 { p / sum } else { 1.0 / w as f32 }; + cols.push(acc); + } + } + Some(EnvSampler { width: w, height: h, rows, cols, prob }) + } + + /// Environment-space direction and its solid-angle density. + fn sample(&self, u0: f32, u1: f32, u2: f32, u3: f32) -> Option<(Vec3, f32)> { + let y = self.rows.partition_point(|c| *c < u0 * self.rows.last().copied().unwrap_or(1.0)).min(self.height - 1); + let row = self.cols.get(y * self.width..(y + 1) * self.width)?; + let x = row.partition_point(|c| *c < u1 * row.last().copied().unwrap_or(1.0)).min(self.width - 1); + let d = crate::ibl::dir_of((x as f32 + u2) / self.width as f32, (y as f32 + u3) / self.height as f32); + let pdf = self.pdf(d); + (pdf > 0.0).then_some((d, pdf)) + } + + fn pdf(&self, d: Vec3) -> f32 { + let uv = crate::ibl::uv_of(d); + let x = ((uv.x * self.width as f32) as usize).min(self.width - 1); + let y = ((uv.y * self.height as f32) as usize).min(self.height - 1); + let sin = (PI * (y as f32 + 0.5) / self.height as f32).sin(); + let p = self.prob.get(y * self.width + x).copied().unwrap_or(0.0); + if sin <= 1e-6 { 0.0 } else { p * (self.width * self.height) as f32 / (2.0 * PI * PI * sin) } + } +} + +/// How the accumulated radiance becomes display pixels. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct TraceDisplay { + /// Exposure multiplier (2^EV). + pub exposure: f32, + pub aces: bool, + /// How much of the environment shows behind the mesh (0: studio backdrop). + pub env_opacity: f32, + /// Filter the remaining noise (edge-aware; keeps texture detail). + pub denoise: bool, +} + +impl TraceDisplay { + /// Exposure, tone mapping and background as the viewport shows them. + pub fn from_display(d: &DisplaySettings) -> TraceDisplay { + TraceDisplay { exposure: d.env_exposure.exp2(), aces: d.tone_mapping == ToneMapping::AcesFilmic, env_opacity: d.env_opacity, denoise: false } + } +} + +/// Light transport settings. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct TraceSettings { + /// Diffuse/specular bounces after the camera hit. + pub bounces: u32, +} + +impl Default for TraceSettings { + fn default() -> Self { + TraceSettings { bounces: 6 } + } +} + +#[derive(Clone, Copy, Default)] +struct Px { + /// Radiance summed over samples that hit the mesh. + hit: Vec3, + /// Sum of the squared luminance of those samples (for the variance). + hit_lum2: f32, + /// First-hit reflectance, shading normal and distance, summed (denoiser guides). + albedo: Vec3, + normal: Vec3, + depth: f32, + /// Environment radiance summed over samples that missed it. + env: Vec3, + hits: f32, +} + +/// What one camera sample saw. +struct Sample { + radiance: Vec3, + albedo: Vec3, + normal: Vec3, + depth: f32, +} + +/// The accumulation buffer of one view. +pub struct Tracer { + pub width: u32, + pub height: u32, + pub samples: u32, + /// First row of the pass in progress (see [`Tracer::pass_rows`]). + row: u32, + acc: Vec, +} + +impl Tracer { + pub fn new(width: u32, height: u32) -> Tracer { + let (w, h) = (width.clamp(1, 8192), height.clamp(1, 8192)); + Tracer { width: w, height: h, samples: 0, row: 0, acc: vec![Px::default(); (w * h) as usize] } + } + + /// Add one sample per pixel. + pub fn pass(&mut self, scene: &TraceScene, cam: &OrbitCamera, settings: &TraceSettings) { + while !self.pass_rows(scene, cam, settings, self.height) {} + } + + /// Sample up to `rows` more rows of the current pass; true when that finished the pass (the + /// new sample then counts in [`Tracer::samples`]). Lets a single-threaded caller spread a + /// pass over several frames. + pub fn pass_rows(&mut self, scene: &TraceScene, cam: &OrbitCamera, settings: &TraceSettings, rows: u32) -> bool { + let (w, h, pass) = (self.width, self.height, self.samples); + let aspect = w as f32 / h as f32; + let (y0, y1) = (self.row, self.row.saturating_add(rows.max(1)).min(h)); + let span = self.acc.get_mut(y0 as usize * w as usize..y1 as usize * w as usize).unwrap_or_default(); + span.par_chunks_mut(w as usize).enumerate().for_each(|(dy, row)| { + let y = y0 as usize + dy; + for (x, px) in row.iter_mut().enumerate() { + let mut rng = Rng::new(x as u32, y as u32, pass); + let ndc = Vec2::new((x as f32 + rng.next()) / w as f32 * 2.0 - 1.0, 1.0 - (y as f32 + rng.next()) / h as f32 * 2.0); + let ray = cam.ray(ndc, aspect); + match trace(scene, ray.origin, ray.dir, settings, &mut rng) { + Some(s) => { + px.hit += s.radiance; + px.hit_lum2 += luminance(s.radiance).powi(2); + px.albedo += s.albedo; + px.normal += s.normal; + px.depth += s.depth; + px.hits += 1.0; + } + None => px.env += scene.env(ray.dir), + } + } + }); + if y1 >= h { + self.row = 0; + self.samples += 1; + true + } else { + self.row = y1; + false + } + } + + /// Tone-mapped sRGB pixels, row-major from the top. + pub fn rgba8(&self, d: &TraceDisplay) -> Vec { + let n = self.samples.max(1) as f32; + let (w, h) = (self.width as f32, self.height as f32); + let denoised = if d.denoise { self.denoise() } else { Vec::new() }; + let mut out = vec![0u8; self.acc.len() * 4]; + out.par_chunks_mut(4).zip(self.acc.par_iter()).enumerate().for_each(|(i, (o, px))| { + let uv = Vec2::new(((i as u32 % self.width) as f32 + 0.5) / w, ((i as u32 / self.width) as f32 + 0.5) / h); + let a = px.hits / n; + let misses = n - px.hits; + let back = if misses > 0.0 { backdrop(uv).lerp(tonemap(px.env / misses, d), d.env_opacity.clamp(0.0, 1.0)) } else { backdrop(uv) }; + let mean = denoised.get(i).copied().unwrap_or(px.hit / px.hits.max(1.0)); + let scene = if px.hits > 0.0 { tonemap(mean, d) } else { Vec3::ZERO }; + let c = back.lerp(scene, a); + for (k, v) in c.to_array().into_iter().enumerate() { + if let Some(o) = o.get_mut(k) { + *o = (texturecraft_texel::linear_to_srgb(v.clamp(0.0, 1.0)) * 255.0 + 0.5) as u8; + } + } + if let Some(o) = o.get_mut(3) { + *o = 255; + } + }); + out + } +} + +impl Tracer { + /// Edge-avoiding à-trous filtering of the mesh radiance: lighting is divided by the first-hit + /// reflectance (so texture detail stays sharp), filtered five times with growing steps, its + /// weights falling off across normal, depth and luminance edges (scaled by the estimated + /// noise), then multiplied back. Returns the mean radiance per pixel. + fn denoise(&self) -> Vec { + let (w, h) = (self.width as usize, self.height as usize); + struct G { + illum: Vec3, + albedo: Vec3, + normal: Vec3, + depth: f32, + var: f32, + hit: bool, + } + let guides: Vec = self + .acc + .par_iter() + .map(|px| { + let n = px.hits.max(1.0); + let albedo = (px.albedo / n).max(Vec3::splat(0.02)); + let mean = px.hit / n; + let var = (px.hit_lum2 / n - luminance(mean).powi(2)).max(0.0) / n; + let a = luminance(albedo).max(0.02); + G { illum: mean / albedo, albedo, normal: px.normal.normalize_or(Vec3::Z), depth: px.depth / n, var: var / (a * a), hit: px.hits > 0.0 } + }) + .collect(); + const H: [f32; 5] = [1.0 / 16.0, 1.0 / 4.0, 3.0 / 8.0, 1.0 / 4.0, 1.0 / 16.0]; + let mut cur: Vec<(Vec3, f32)> = guides.iter().map(|g| (g.illum, g.var)).collect(); + for step in [1usize, 2, 4, 8, 16] { + let prev = cur; + cur = (0..w * h) + .into_par_iter() + .map(|i| { + let (Some(p), Some(&(ip, vp))) = (guides.get(i), prev.get(i)) else { return (Vec3::ZERO, 0.0) }; + if !p.hit { + return (ip, vp); + } + let (x, y) = ((i % w) as i64, (i / w) as i64); + let sigma_l = 4.0 * vp.sqrt() + 1e-3; + let (mut sum, mut wsum, mut vsum) = (Vec3::ZERO, 0.0f32, 0.0f32); + for (j, hy) in H.iter().enumerate() { + for (k, hx) in H.iter().enumerate() { + let qx = x + (k as i64 - 2) * step as i64; + let qy = y + (j as i64 - 2) * step as i64; + if qx < 0 || qy < 0 || qx >= w as i64 || qy >= h as i64 { + continue; + } + let qi = qy as usize * w + qx as usize; + let (Some(q), Some(&(iq, vq))) = (guides.get(qi), prev.get(qi)) else { continue }; + if !q.hit { + continue; + } + let wn = p.normal.dot(q.normal).max(0.0).powi(64); + let wz = (-(p.depth - q.depth).abs() / (0.01 * p.depth.max(1e-3) * step as f32)).exp(); + let wl = (-(luminance(ip) - luminance(iq)).abs() / sigma_l).exp(); + let wt = hx * hy * wn * wz * wl; + sum += iq * wt; + wsum += wt; + vsum += wt * wt * vq; + } + } + if wsum > 1e-8 { (sum / wsum, vsum / (wsum * wsum)) } else { (ip, vp) } + }) + .collect(); + } + cur.iter().zip(&guides).map(|((illum, _), g)| *illum * g.albedo).collect() + } +} + +/// The viewport's flat studio gray with a gentle falloff (matches the post pass). +fn backdrop(uv: Vec2) -> Vec3 { + let r = (uv * 2.0 - Vec2::ONE).length() * 0.35; + Vec3::splat(0.0232 * (1.06 - r * r)) +} + +fn tonemap(hdr: Vec3, d: &TraceDisplay) -> Vec3 { + let x = hdr.max(Vec3::ZERO) * d.exposure; + if !d.aces { + return x.min(Vec3::ONE); + } + let v = x * 0.6; + ((v * (v * 2.51 + 0.03)) / (v * (v * 2.43 + 0.59) + 0.14)).clamp(Vec3::ZERO, Vec3::ONE) +} + +/// Small, fast per-sample generator (PCG hash seeded by pixel and pass). +struct Rng(u32); + +impl Rng { + fn new(x: u32, y: u32, pass: u32) -> Rng { + Rng(texturecraft_texel::noise::pcg3d(x, y, pass.wrapping_mul(7919).wrapping_add(17))[0] | 1) + } + + fn next(&mut self) -> f32 { + // xorshift32 + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 17; + self.0 ^= self.0 << 5; + (self.0 >> 8) as f32 / (1u32 << 24) as f32 + } +} + +/// Shading data at a hit. +struct Surface { + pos: Vec3, + /// Geometric normal facing the incoming ray. + geo: Vec3, + n: Vec3, + /// Diffuse albedo and specular reflectance at normal incidence. + diffuse: Vec3, + f0: Vec3, + roughness: f32, + emissive: Vec3, + opacity: f32, +} + +fn lerp3(a: Vec3, b: Vec3, c: Vec3, u: f32, v: f32) -> Vec3 { + a * (1.0 - u - v) + b * u + c * v +} + +/// Tangent-space normal blending (reoriented normal mapping), as in the viewport shader. +fn rnm(base: Vec3, detail: Vec3) -> Vec3 { + let t = base + Vec3::Z; + let u = detail * Vec3::new(-1.0, -1.0, 1.0); + (t * t.dot(u) - u * t.z).normalize_or(Vec3::Z) +} + +fn surface(scene: &TraceScene, tri: u32, u: f32, v: f32, o: Vec3, d: Vec3, t: f32) -> Option { + let m = &scene.mesh; + let i = tri as usize * 3; + let idx = [*m.indices.get(i)? as usize, *m.indices.get(i + 1)? as usize, *m.indices.get(i + 2)? as usize]; + let p = idx.map(|k| m.positions.get(k).copied().unwrap_or(Vec3::ZERO)); + let mut geo = (p[1] - p[0]).cross(p[2] - p[0]).normalize_or(Vec3::Y); + let nrm = idx.map(|k| m.normals.get(k).copied().unwrap_or(geo)); + let mut n_geo = lerp3(nrm[0], nrm[1], nrm[2], u, v).normalize_or(geo); + if geo.dot(d) > 0.0 { + geo = -geo; + } + if n_geo.dot(geo) < 0.0 { + n_geo = -n_geo; + } + let uvs = idx.map(|k| m.uvs.get(k).copied().unwrap_or(Vec2::ZERO)); + let uv = uvs[0] * (1.0 - u - v) + uvs[1] * u + uvs[2] * v; + let tan = idx.map(|k| m.tangents.get(k).copied().unwrap_or(texturecraft_geom::glam::Vec4::X)); + let tw = lerp3(tan[0].truncate(), tan[1].truncate(), tan[2].truncate(), u, v); + let sign = if tan[0].w < 0.0 { -1.0 } else { 1.0 }; + + let set = scene.tri_set.get(tri as usize).and_then(|s| scene.sets.get(*s)); + let ch = |c: Channel, dx: i32, dy: i32| -> [f32; 3] { + let Some(s) = set else { return c.default_value() }; + let res = s.composite.resolution.max(1) as i32; + let x = ((uv.x * res as f32).floor() as i32 + dx).rem_euclid(res) as u32; + let y = ((uv.y * res as f32).floor() as i32 + dy).rem_euclid(res) as u32; + s.composite.value(c, x, y) + }; + let base = Vec3::from(ch(Channel::BaseColor, 0, 0)); + let k = set.map_or(0.0, |s| s.height_strength * s.composite.resolution as f32 / 16.0); + let n_height = if k != 0.0 { + let h = |dx, dy| ch(Channel::Height, dx, dy)[0]; + Vec3::new(-(h(1, 0) - h(-1, 0)) * 0.5 * k, (h(0, 1) - h(0, -1)) * 0.5 * k, 1.0).normalize_or(Vec3::Z) + } else { + Vec3::Z + }; + let detail = (Vec3::from(ch(Channel::Normal, 0, 0)) * 2.0 - Vec3::ONE).normalize_or(Vec3::Z); + let n_ts = rnm(n_height, detail); + let tt = (tw - n_geo * n_geo.dot(tw)).normalize_or(n_geo.any_orthonormal_vector()); + let b = n_geo.cross(tt) * sign; + let mut n = (tt * n_ts.x + b * n_ts.y + n_geo * n_ts.z).normalize_or(n_geo); + if n.dot(-d) <= 0.0 { + n = n_geo; + } + // Glossiness replaces roughness wherever a set has it (as in the viewport). + let roughness = match set { + Some(s) if s.composite.channels.contains_key(&Channel::Glossiness) => 1.0 - ch(Channel::Glossiness, 0, 0)[0], + _ => ch(Channel::Roughness, 0, 0)[0], + }; + let (diffuse, f0) = if scene.spec_gloss { + let f0 = Vec3::from(ch(Channel::Specular, 0, 0)).clamp(Vec3::ZERO, Vec3::ONE); + (base * (1.0 - f0.max_element()), f0) + } else { + let metallic = ch(Channel::Metallic, 0, 0)[0].clamp(0.0, 1.0); + let level = ch(Channel::SpecularLevel, 0, 0)[0].clamp(0.0, 1.0); + (base * (1.0 - metallic), Vec3::splat(0.08 * level).lerp(base, metallic)) + }; + Some(Surface { + pos: o + d * t, + geo, + n, + diffuse, + f0, + roughness: roughness.clamp(0.045, 1.0), + emissive: Vec3::from(ch(Channel::Emissive, 0, 0)), + opacity: ch(Channel::Opacity, 0, 0)[0].clamp(0.0, 1.0), + }) +} + +fn luminance(c: Vec3) -> f32 { + c.dot(Vec3::new(0.2126, 0.7152, 0.0722)) +} + +fn schlick(f0: Vec3, cos: f32) -> Vec3 { + f0 + (Vec3::ONE - f0) * (1.0 - cos.clamp(0.0, 1.0)).powi(5) +} + +/// Smith GGX masking for one direction (local frame, z = normal). +fn smith_g1(w: Vec3, a: f32) -> f32 { + let z = w.z.max(1e-6); + 2.0 * z / (z + (a * a * (1.0 - z * z) + z * z).sqrt()) +} + +/// GGX visible-normal sampling (local frame, z = normal). +fn sample_vndf(ve: Vec3, a: f32, u1: f32, u2: f32) -> Vec3 { + let vh = Vec3::new(a * ve.x, a * ve.y, ve.z).normalize_or(Vec3::Z); + let len2 = vh.x * vh.x + vh.y * vh.y; + let t1 = if len2 > 0.0 { Vec3::new(-vh.y, vh.x, 0.0) / len2.sqrt() } else { Vec3::X }; + let t2 = vh.cross(t1); + let r = u1.sqrt(); + let phi = TAU * u2; + let p1 = r * phi.cos(); + let s = 0.5 * (1.0 + vh.z); + let p2 = (1.0 - s) * (1.0 - p1 * p1).max(0.0).sqrt() + s * r * phi.sin(); + let nh = t1 * p1 + t2 * p2 + vh * (1.0 - p1 * p1 - p2 * p2).max(0.0).sqrt(); + Vec3::new(a * nh.x, a * nh.y, nh.z.max(1e-6)).normalize_or(Vec3::Z) +} + +/// The surface's BRDF at one hit, in a local frame (z = shading normal). +struct Bsdf { + t: Vec3, + b: Vec3, + n: Vec3, + /// View direction, local. + v: Vec3, + f0: Vec3, + diffuse: Vec3, + /// GGX alpha. + a: f32, + /// Fresnel at the view angle. + fv: Vec3, + /// Probability of sampling the specular lobe. + p_spec: f32, +} + +impl Bsdf { + fn new(s: &Surface, v: Vec3) -> Bsdf { + let n = s.n; + let t = n.any_orthonormal_vector(); + let b = n.cross(t); + let vl = Vec3::new(v.dot(t), v.dot(b), v.dot(n).max(1e-4)); + let (f0, diffuse) = (s.f0, s.diffuse); + let fv = schlick(f0, vl.z); + let ld = luminance(diffuse * (Vec3::ONE - fv)); + let p_spec = if ld < 1e-5 { 1.0 } else { (luminance(fv) / (luminance(fv) + ld)).clamp(0.1, 0.9) }; + Bsdf { t, b, n, v: vl, f0, diffuse, a: (s.roughness * s.roughness).max(1e-3), fv, p_spec } + } + + fn local(&self, w: Vec3) -> Vec3 { + Vec3::new(w.dot(self.t), w.dot(self.b), w.dot(self.n)) + } + + fn ggx_d(&self, h: Vec3) -> f32 { + let a2 = self.a * self.a; + let d = h.z * h.z * (a2 - 1.0) + 1.0; + a2 / (PI * d * d).max(1e-12) + } + + /// BRDF times cos(theta) toward world direction `w`, and the density of [`Self::sample`] + /// picking it. + fn eval(&self, w: Vec3) -> (Vec3, f32) { + let l = self.local(w); + if l.z <= 0.0 { + return (Vec3::ZERO, 0.0); + } + let h = (l + self.v).normalize_or(Vec3::Z); + let d = self.ggx_d(h); + let g1v = smith_g1(self.v, self.a); + let spec = schlick(self.f0, l.dot(h)) * d * g1v * smith_g1(l, self.a) / (4.0 * self.v.z); + let diff = self.diffuse * (Vec3::ONE - self.fv) * (l.z / PI); + let pdf_spec = g1v * d / (4.0 * self.v.z); + let pdf_diff = l.z / PI; + (spec + diff, self.p_spec * pdf_spec + (1.0 - self.p_spec) * pdf_diff) + } + + /// A world direction from the BRDF lobes (specular by visible normals, diffuse by cosine). + fn sample(&self, rng: &mut Rng) -> Option { + let (u0, u1, u2) = (rng.next(), rng.next(), rng.next()); + let l = if u0 < self.p_spec { + let h = sample_vndf(self.v, self.a, u1, u2); + h * 2.0 * self.v.dot(h) - self.v + } else { + let r = u1.sqrt(); + let phi = TAU * u2; + Vec3::new(r * phi.cos(), r * phi.sin(), (1.0 - u1).max(0.0).sqrt()) + }; + (l.z > 0.0).then(|| self.t * l.x + self.b * l.y + self.n * l.z) + } +} + +/// Power-heuristic MIS weight of a strategy with density `a` against one with density `b`. +fn mis(a: f32, b: f32) -> f32 { + let (a2, b2) = (a * a, b * b); + if a2 + b2 > 0.0 { a2 / (a2 + b2) } else { 0.0 } +} + +/// Clamp rare, very bright indirect contributions (fireflies) to keep progressive renders calm. +fn clamp_firefly(c: Vec3) -> Vec3 { + let lum = luminance(c); + if lum > 32.0 { c * (32.0 / lum) } else { c } +} + +/// Radiance along a camera ray; None when it misses the mesh. Environment light is gathered +/// both by sampling it directly (next-event estimation) and by BRDF sampling, combined with MIS. +fn trace(scene: &TraceScene, mut o: Vec3, mut d: Vec3, settings: &TraceSettings, rng: &mut Rng) -> Option { + let origin = o; + let mut first: Option<(Vec3, Vec3, f32)> = None; + let mut thr = Vec3::ONE; + let mut l = Vec3::ZERO; + let mut bounce = 0u32; + // Density of the BRDF sample that produced `d` (None for the camera ray). + let mut brdf_pdf: Option = None; + // Cutout surfaces don't count as bounces; cap them so a stack of cards can't loop forever. + for _ in 0..settings.bounces + 24 { + let Some(hit) = scene.bvh.closest(o, d, f32::MAX) else { + let pdf = brdf_pdf?; + let w = if scene.light.is_some() { mis(pdf, scene.env_pdf(d)) } else { 1.0 }; + l += clamp_firefly(thr * scene.env(d) * w); + break; + }; + let Some(s) = surface(scene, hit.tri, hit.u, hit.v, o, d, hit.t) else { break }; + if rng.next() > s.opacity { + o = s.pos - s.geo * scene.eps; + continue; + } + if first.is_none() { + first = Some(((s.diffuse + s.f0).min(Vec3::ONE), s.n, (s.pos - origin).length())); + } + l += thr * s.emissive; + if bounce >= settings.bounces { + break; + } + let bsdf = Bsdf::new(&s, -d); + // Next-event estimation toward the environment. + if let Some((wl, pdf_env)) = scene.sample_env(rng) + && wl.dot(s.geo) > 0.0 + { + let (f, pdf_brdf) = bsdf.eval(wl); + if f.max_element() > 0.0 && !scene.bvh.occluded(s.pos + s.geo * scene.eps, wl, f32::MAX) { + l += clamp_firefly(thr * f * scene.env(wl) * (mis(pdf_env, pdf_brdf) / pdf_env)); + } + } + let Some(wi) = bsdf.sample(rng) else { break }; + let (f, pdf) = bsdf.eval(wi); + if pdf <= 0.0 { + break; + } + thr *= f / pdf; + brdf_pdf = Some(pdf); + if bounce >= 2 { + let p = thr.max_element().clamp(0.05, 0.95); + if rng.next() > p { + break; + } + thr /= p; + } + let side = if wi.dot(s.geo) >= 0.0 { 1.0 } else { -1.0 }; + o = s.pos + s.geo * scene.eps * side; + d = wi; + bounce += 1; + } + let (albedo, normal, depth) = first?; + Some(Sample { radiance: if l.is_finite() { l } else { Vec3::ZERO }, albedo, normal, depth }) +} + +#[cfg(test)] +mod tests { + use super::*; + use texturecraft_geom::primitives; + + fn scene(env: &str) -> TraceScene { + let mesh = Arc::new(primitives::sphere(1.0, 32, 16)); + let bvh = Arc::new(Bvh::new(&mesh)); + let env = Arc::new(crate::ibl::environment(env, 128)); + TraceScene::new(mesh, bvh, Vec::new(), env, 0.0) + } + + fn camera() -> OrbitCamera { + let mut cam = OrbitCamera::default(); + cam.target = Vec3::ZERO; + cam.distance = 4.0; + cam + } + + #[test] + fn renders_the_mesh_over_the_backdrop_and_converges() { + let s = scene("Studio Soft"); + let mut tr = Tracer::new(48, 32); + for _ in 0..8 { + tr.pass(&s, &camera(), &TraceSettings::default()); + } + assert_eq!(tr.samples, 8); + let d = TraceDisplay { exposure: 1.0, aces: true, env_opacity: 0.0, denoise: false }; + let img = tr.rgba8(&d); + assert_eq!(img.len(), 48 * 32 * 4); + let px = |x: usize, y: usize| &img[(y * 48 + x) * 4..(y * 48 + x) * 4 + 3]; + // The sphere (default material, lit by the studio) is brighter than the dark backdrop corner. + let center: u32 = px(24, 16).iter().map(|v| *v as u32).sum(); + let corner: u32 = px(0, 0).iter().map(|v| *v as u32).sum(); + assert!(center > corner + 60, "center {center} corner {corner}"); + assert!(tr.acc.iter().all(|p| p.hit.is_finite() && p.env.is_finite())); + } + + #[test] + fn environment_shows_behind_the_mesh_with_opacity() { + let s = scene("Overcast"); + let mut tr = Tracer::new(16, 8); + tr.pass(&s, &camera(), &TraceSettings::default()); + let corner = |opacity: f32| tr.rgba8(&TraceDisplay { exposure: 1.0, aces: true, env_opacity: opacity, denoise: false })[0]; + assert!(corner(1.0) > corner(0.0) + 40); + } + + #[test] + fn light_sampling_matches_brdf_sampling_on_average() { + let lit = scene("Studio Soft"); + let unlit = TraceScene { light: None, ..lit.clone() }; + let mean = |s: &TraceScene| { + let mut rng = Rng::new(9, 9, 9); + let n = 40_000; + let mut sum = Vec3::ZERO; + for _ in 0..n { + sum += trace(s, Vec3::new(0.3, 0.4, 4.0), Vec3::new(-0.07, -0.1, -1.0).normalize(), &TraceSettings { bounces: 3 }, &mut rng).map_or(Vec3::ZERO, |s| s.radiance); + } + sum / n as f32 + }; + let (a, b) = (mean(&lit), mean(&unlit)); + assert!((a - b).length() < 0.05 * b.length().max(0.05), "with light sampling {a}, without {b}"); + } + + #[test] + fn passes_split_into_row_slices_match_whole_passes() { + let s = scene("Studio Soft"); + let (mut whole, mut sliced) = (Tracer::new(40, 30), Tracer::new(40, 30)); + let d = TraceDisplay { exposure: 1.0, aces: true, env_opacity: 0.0, denoise: false }; + for _ in 0..2 { + whole.pass(&s, &camera(), &TraceSettings::default()); + let mut done = false; + while !done { + done = sliced.pass_rows(&s, &camera(), &TraceSettings::default(), 7); + } + } + assert_eq!(sliced.samples, 2); + assert_eq!(whole.rgba8(&d), sliced.rgba8(&d)); + } + + #[test] + fn denoising_lowers_noise_and_keeps_the_mean() { + let s = scene("Studio Contrast"); + let mut tr = Tracer::new(64, 48); + for _ in 0..4 { + tr.pass(&s, &camera(), &TraceSettings::default()); + } + let raw = tr.rgba8(&TraceDisplay { exposure: 1.0, aces: true, env_opacity: 0.0, denoise: false }); + let clean = tr.rgba8(&TraceDisplay { exposure: 1.0, aces: true, env_opacity: 0.0, denoise: true }); + // Neighbour differences inside the ball (rows 18..30, columns 26..38) drop; the mean stays. + let stats = |img: &[u8]| { + let px = |x: usize, y: usize| img[(y * 64 + x) * 4] as f32; + let (mut diff, mut sum, mut n) = (0.0, 0.0, 0.0); + for y in 18..30 { + for x in 26..38 { + diff += (px(x, y) - px(x + 1, y)).abs(); + sum += px(x, y); + n += 1.0; + } + } + (diff / n, sum / n) + }; + let ((d_raw, m_raw), (d_clean, m_clean)) = (stats(&raw), stats(&clean)); + assert!(d_clean < d_raw * 0.6, "noise {d_raw} -> {d_clean}"); + assert!((m_raw - m_clean).abs() < 0.08 * m_raw.max(1.0), "mean {m_raw} -> {m_clean}"); + } + + #[test] + fn white_furnace_keeps_energy_bounded() { + // A uniform white environment: a white diffuse sphere must not exceed the environment. + let mesh = Arc::new(primitives::sphere(1.0, 24, 12)); + let bvh = Arc::new(Bvh::new(&mesh)); + let env = Arc::new(EnvImage { width: 8, height: 4, data: vec![[1.0; 3]; 32] }); + let s = TraceScene::new(mesh, bvh, Vec::new(), env, 0.0); + let mut rng = Rng::new(1, 2, 3); + let mut sum = Vec3::ZERO; + let n = 4000; + for _ in 0..n { + sum += trace(&s, Vec3::new(0.0, 0.0, 4.0), Vec3::NEG_Z, &TraceSettings { bounces: 8 }, &mut rng).map_or(Vec3::ZERO, |s| s.radiance); + } + let avg = sum / n as f32; + assert!(avg.max_element() <= 1.05 && avg.min_element() > 0.3, "{avg}"); + } +} diff --git a/crates/render/tests/headless.rs b/crates/render/tests/headless.rs new file mode 100644 index 00000000..2c5250df --- /dev/null +++ b/crates/render/tests/headless.rs @@ -0,0 +1,352 @@ +use texturecraft_compose::{DisplayKind, ImageRes}; +use texturecraft_geom::{OrbitCamera, primitives}; +use texturecraft_render::headless::Headless; +use texturecraft_render::renderer::TAA_SAMPLES; +use texturecraft_render::{Frame, Shade, ViewKind}; + +/// The UI's key light (`viewport::shade`). +const LIGHT: [f32; 4] = [0.5, 0.8, 0.4, 2.5]; + +struct Img { + w: u32, + px: Vec, +} + +impl Img { + fn at(&self, x: u32, y: u32) -> [u8; 3] { + let i = ((y * self.w + x) * 4) as usize; + [self.px[i], self.px[i + 1], self.px[i + 2]] + } + fn luma(&self, x: u32, y: u32) -> f32 { + let [r, g, b] = self.at(x, y); + 0.2126 * r as f32 + 0.7152 * g as f32 + 0.0722 * b as f32 + } + /// Mean luma over a rectangle. + fn mean(&self, x0: u32, y0: u32, x1: u32, y1: u32) -> f32 { + let mut sum = 0.0; + for y in y0..y1 { + for x in x0..x1 { + sum += self.luma(x, y); + } + } + sum / ((x1 - x0) * (y1 - y0)) as f32 + } +} + +/// A headless renderer with the material ball framed in a 480×360 view (None without a GPU). +fn setup() -> Option<(Headless, Frame)> { + let Ok(mut h) = Headless::new() else { + eprintln!("skipping: no GPU adapter"); + return None; + }; + let mesh = primitives::material_ball(); + h.renderer.set_mesh(&h.device, &h.queue, &mesh, 1); + h.renderer.set_texture_sets(&h.device, &h.queue, &[(256, 0), (256, 1)], 2); + let mut cam = OrbitCamera { yaw: 0.6, pitch: 0.25, ..OrbitCamera::default() }; + let (lo, hi) = mesh.bounds(); + cam.frame(lo, hi); + let frame = Frame { view_id: 1, size: [480, 360], kind: ViewKind::Scene, camera: cam, shade: Shade::default(), visible: vec![true, true] }; + Some((h, frame)) +} + +fn render(h: &mut Headless, frame: &Frame, name: &str) -> Img { + let (w, hgt, px) = h.render(frame).expect("readback"); + assert_eq!((w, hgt), (frame.size[0], frame.size[1])); + if let Ok(dir) = std::env::var("TC_SNAPSHOT_DIR") { + image::save_buffer(format!("{dir}/{name}.png"), &px, w, hgt, image::ColorType::Rgba8).expect("save snapshot"); + } + Img { w, px } +} + +fn shaded(frame: &Frame, f: impl FnOnce(&mut Shade)) -> Frame { + let mut out = frame.clone(); + f(&mut out.shade); + out +} + +#[test] +fn renders_the_material_ball() { + let Some((mut h, frame)) = setup() else { return }; + let img = render(&mut h, &frame, "headless_ball"); + assert_ne!(img.at(240, 140), img.at(5, 5), "mesh should differ from background"); + // UV view of set 0. + let uv = Frame { view_id: 2, kind: ViewKind::Uv { ts: 0, zoom: 1.0, pan: [0.0, 0.0] }, ..frame.clone() }; + let uv_img = render(&mut h, &uv, "headless_uv"); + assert_eq!(uv_img.px.len(), img.px.len()); + let wire = render(&mut h, &shaded(&uv, |s| s.wireframe = true), "headless_uv_wire"); + assert_ne!(wire.px, uv_img.px, "the UV wireframe draws"); +} + +#[test] +fn exposure_brightens_the_material_but_not_data_views() { + let Some((mut h, frame)) = setup() else { return }; + let base = render(&mut h, &frame, "exposure_0"); + let bright = render(&mut h, &shaded(&frame, |s| s.exposure_ev = 1.0), "exposure_plus1"); + let (b0, b1) = (base.mean(200, 100, 280, 180), bright.mean(200, 100, 280, 180)); + assert!(b1 > b0 + 10.0, "exposure +1 brightens the ball ({b0} → {b1})"); + assert_eq!(base.at(5, 5), bright.at(5, 5), "the flat backdrop ignores exposure"); + let ch = shaded(&frame, |s| { + s.view_mode = 1; + s.channel = 0; + }); + let a = render(&mut h, &ch, "channel_base_color"); + let b = render(&mut h, &shaded(&ch, |s| s.exposure_ev = 2.0), "channel_base_color_ev2"); + assert_eq!(a.px, b.px, "data views ignore exposure"); + // Default base color 188 (sRGB) shows unchanged in the channel view. + let [r, ..] = a.at(240, 140); + assert!(r.abs_diff(188) <= 1, "base color channel shows the raw value, got {r}"); +} + +#[test] +fn post_effects() { + let Some((mut h, frame)) = setup() else { return }; + let base = render(&mut h, &frame, "post_off"); + // Strengths without the master switch change nothing. + let gated = render( + &mut h, + &shaded(&frame, |s| { + s.vignette = 1.0; + s.bloom = 1.0; + s.grain = 1.0; + s.sharpen = 1.0; + }), + "post_gated", + ); + assert_eq!(base.px, gated.px, "effects are off without post_effects"); + let on = |f: fn(&mut Shade)| { + shaded(&frame, |s| { + s.post_effects = true; + f(s); + }) + }; + let vignette = render(&mut h, &on(|s| s.vignette = 1.0), "post_vignette"); + assert!(vignette.mean(0, 0, 20, 20) < base.mean(0, 0, 20, 20) - 5.0, "vignette darkens the corners"); + assert!((vignette.luma(240, 180) - base.luma(240, 180)).abs() < 1.5, "vignette keeps the center"); + // A bright environment and +2 EV so highlights pass the bloom threshold. + let hot = |s: &mut Shade| { + s.exposure_ev = 2.0; + s.env_opacity = 1.0; + }; + let hot_base = render(&mut h, &shaded(&frame, hot), "post_hot"); + let bloom = render( + &mut h, + &on(|s| { + s.exposure_ev = 2.0; + s.env_opacity = 1.0; + s.bloom = 1.0; + }), + "post_bloom", + ); + assert!(bloom.mean(0, 0, 480, 360) > hot_base.mean(0, 0, 480, 360) + 0.5, "bloom adds light"); + let sharpen = render(&mut h, &on(|s| s.sharpen = 1.0), "post_sharpen"); + assert_ne!(sharpen.px, base.px, "sharpen changes edges"); + let grain = render(&mut h, &on(|s| s.grain = 1.0), "post_grain"); + assert_ne!(grain.px, base.px, "grain adds noise"); + let all = render( + &mut h, + &on(|s| { + s.bloom = 0.6; + s.vignette = 0.5; + s.sharpen = 0.4; + s.grain = 0.3; + }), + "post_all", + ); + assert_eq!(all.px.len(), base.px.len()); +} + +#[test] +fn shadows_darken_the_key_light() { + let Some((mut h, frame)) = setup() else { return }; + let lit = shaded(&frame, |s| s.light = LIGHT); + let a = render(&mut h, &lit, "shadows_off"); + let b = render(&mut h, &shaded(&lit, |s| s.shadows = true), "shadows_on"); + let (mut darker, mut brighter) = (0, 0); + for y in 0..360 { + for x in 0..480 { + let d = b.luma(x, y) - a.luma(x, y); + darker += (d < -8.0) as u32; + brighter += (d > 2.0) as u32; + } + } + assert!(darker > 1000, "the ball shadows its pedestal ({darker} darker pixels)"); + assert_eq!(brighter, 0, "shadows never brighten"); + // From above, where the pedestal top shows the ball's shadow. + let mut top = shaded(&lit, |s| s.shadows = true); + top.camera.pitch = 0.9; + render(&mut h, &top, "shadows_on_top"); + // Rotating the environment turns the key light and its shadow. + let turned = render(&mut h, &shaded(&lit, |s| { + s.shadows = true; + s.env_rotation_deg = 120.0; + }), "shadows_env_rotated"); + assert_ne!(turned.px, b.px); +} + +#[test] +fn wireframe_3d_overlays_the_mesh() { + let Some((mut h, frame)) = setup() else { return }; + let a = render(&mut h, &frame, "wire3d_off"); + let b = render(&mut h, &shaded(&frame, |s| s.wireframe_3d = true), "wire3d_on"); + assert_ne!(a.mean(200, 100, 280, 180), b.mean(200, 100, 280, 180), "edges draw over the ball"); + assert_eq!(a.at(5, 5), b.at(5, 5), "the background stays clean"); +} + +#[test] +fn ortho_background_matches_perspective() { + let Some((mut h, frame)) = setup() else { return }; + let env = shaded(&frame, |s| s.env_opacity = 1.0); + let persp = render(&mut h, &env, "env_perspective"); + let ortho = render(&mut h, &Frame { camera: OrbitCamera { ortho: true, ..env.camera }, ..env.clone() }, "env_ortho"); + for (x, y) in [(5, 5), (474, 5), (5, 354), (474, 354), (240, 5)] { + assert_eq!(persp.at(x, y), ortho.at(x, y), "background at {x},{y}"); + } + assert_ne!(ortho.at(5, 5), ortho.at(240, 5), "the environment varies across the view"); +} + +/// Frame cost at 1920×1080: `cargo test -p texturecraft-render --release -- --ignored --nocapture`. +#[test] +#[ignore] +fn bench_1080p() { + let Some((mut h, frame)) = setup() else { return }; + h.renderer.set_texture_sets(&h.device, &h.queue, &[(2048, 0), (2048, 1)], 2); + let frame = Frame { size: [1920, 1080], ..frame }; + let run = |h: &mut Headless, frame: &Frame| { + for _ in 0..5 { + h.render(frame); + } + let t = std::time::Instant::now(); + for _ in 0..100 { + let mut enc = h.device.create_command_encoder(&Default::default()); + h.renderer.prepare(&h.device, &h.queue, &mut enc, frame); + h.queue.submit([enc.finish()]); + let _ = h.device.poll(texturecraft_render::wgpu::PollType::Wait { submission_index: None, timeout: None }); + } + t.elapsed().as_secs_f64() * 1000.0 / 100.0 + }; + println!("adapter: {}", h.adapter_name); + println!("default: {:.3} ms/frame", run(&mut h, &frame)); + let effects = shaded(&frame, |s| { + s.post_effects = true; + s.bloom = 0.5; + s.vignette = 0.5; + s.sharpen = 0.5; + s.grain = 0.5; + }); + println!("post effects: {:.3} ms/frame", run(&mut h, &effects)); + let shadows = shaded(&frame, |s| { + s.light = LIGHT; + s.shadows = true; + }); + println!("shadows (cached map): {:.3} ms/frame", run(&mut h, &shadows)); + let wire = shaded(&frame, |s| s.wireframe_3d = true); + println!("3D wireframe: {:.3} ms/frame", run(&mut h, &wire)); + let all = shaded(&effects, |s| { + s.light = LIGHT; + s.shadows = true; + s.wireframe_3d = true; + }); + println!("everything: {:.3} ms/frame", run(&mut h, &all)); + println!("parallax occlusion: {:.3} ms/frame", run(&mut h, &shaded(&frame, |s| s.parallax = true))); + let taa = shaded(&frame, |s| s.taa = true); + h.render(&taa); + let t = std::time::Instant::now(); + for _ in 1..TAA_SAMPLES { + let mut enc = h.device.create_command_encoder(&Default::default()); + h.renderer.prepare(&h.device, &h.queue, &mut enc, &taa); + h.queue.submit([enc.finish()]); + let _ = h.device.poll(texturecraft_render::wgpu::PollType::Wait { submission_index: None, timeout: None }); + } + println!("TAA accumulation: {:.3} ms/sample", t.elapsed().as_secs_f64() * 1000.0 / f64::from(TAA_SAMPLES - 1)); + // Shadow map re-rendered every frame (light moving). + let t = std::time::Instant::now(); + for i in 0..100 { + let f = shaded(&shadows, |s| s.env_rotation_deg = i as f32); + let mut enc = h.device.create_command_encoder(&Default::default()); + h.renderer.prepare(&h.device, &h.queue, &mut enc, &f); + h.queue.submit([enc.finish()]); + let _ = h.device.poll(texturecraft_render::wgpu::PollType::Wait { submission_index: None, timeout: None }); + } + println!("shadows, light moving: {:.3} ms/frame", t.elapsed().as_secs_f64() * 1000.0 / 100.0); +} + +/// Fill the height texture of both 256² sets: `f(x, y)` → (height, F0 r, g, b). +fn upload_height(h: &mut Headless, f: impl Fn(u32, u32) -> [f32; 4]) { + for ts in 0..2 { + for (tx, ty) in (0..16).map(|i| (i % 4, i / 4)) { + let bytes: Vec = (0..64 * 64).flat_map(|i| f(tx * 64 + i % 64, ty * 64 + i / 64)).flat_map(|v| half::f16::from_f32(v).to_le_bytes()).collect(); + h.renderer.upload_tile(&h.queue, ts, DisplayKind::Height, tx, ty, &bytes); + } + } +} + +/// Mean of red minus blue over the ball. +fn warmth(img: &Img) -> f32 { + let mut sum = 0.0; + for y in 100..180 { + for x in 200..280 { + let [r, _, b] = img.at(x, y); + sum += r as f32 - b as f32; + } + } + sum / 6400.0 +} + +#[test] +fn imported_hdr_environment_lights_the_ball() { + let Some((mut h, frame)) = setup() else { return }; + let base = render(&mut h, &frame, "env_default"); + // A procedural HDR: a hot orange sky over a dim blue ground (values far above 1). + let (w, hh) = (256u32, 128u32); + let rgba = (0..w * hh).map(|i| if i / w < hh / 2 { [12.0, 4.0, 1.0, 1.0] } else { [0.02, 0.05, 0.2, 1.0] }).collect(); + let sky = ImageRes { width: w, height: hh, rgba, srgb: false }; + h.renderer.set_environment(&h.device, &h.queue, "resource:sky.hdr", Some(&sky)); + let lit = render(&mut h, &frame, "env_hdr_resource"); + assert_ne!(lit.px, base.px, "the imported environment changes the lighting"); + assert!(warmth(&lit) > warmth(&base) + 10.0, "the orange sky tints the ball"); + assert!(lit.mean(200, 100, 280, 180) > 20.0, "lit, finite shading (NaNs would render black)"); + h.renderer.set_environment(&h.device, &h.queue, "Studio Soft", None); + assert_eq!(render(&mut h, &frame, "env_default_again").px, base.px, "built-ins come back"); +} + +#[test] +fn specular_glossiness_takes_f0_from_the_specular_color() { + let Some((mut h, frame)) = setup() else { return }; + upload_height(&mut h, |_, _| [0.0, 1.0, 0.6, 0.1]); + let mr = render(&mut h, &frame, "shader_metal_rough"); + let sg = render(&mut h, &shaded(&frame, |s| s.spec_gloss = true), "shader_spec_gloss_gold"); + assert!(warmth(&sg) > warmth(&mr) + 10.0, "a gold F0 tints the reflections ({} vs {})", warmth(&sg), warmth(&mr)); + assert!(sg.mean(200, 100, 280, 180) > 20.0); +} + +#[test] +fn parallax_follows_the_height_channel() { + let Some((mut h, frame)) = setup() else { return }; + let pom = shaded(&frame, |s| s.parallax = true); + // A flat height field: parallax changes nothing. + let (off, on) = (render(&mut h, &frame, "parallax_flat_off"), render(&mut h, &pom, "parallax_flat_on")); + assert!(off.px.iter().zip(&on.px).all(|(a, b)| a.abs_diff(*b) <= 2), "flat heights keep the UVs"); + upload_height(&mut h, |x, y| [if (x / 16 + y / 16) % 2 == 0 { 0.8 } else { -0.8 }, 0.04, 0.04, 0.04]); + let (off, on) = (render(&mut h, &frame, "parallax_off"), render(&mut h, &pom, "parallax_on")); + assert_ne!(off.px, on.px, "a checker height field shifts the textures"); + assert!(on.mean(200, 100, 280, 180) > 20.0); +} + +#[test] +fn idle_accumulation_converges_then_stops() { + let Some((mut h, frame)) = setup() else { return }; + let taa = shaded(&frame, |s| s.taa = true); + let first = render(&mut h, &taa, "taa_first"); + assert_eq!(first.px, render(&mut h, &frame, "taa_off").px, "the first sample is the plain frame"); + render(&mut h, &taa, "taa_first_again"); + for _ in 1..TAA_SAMPLES { + assert!(h.renderer.accumulating(1)); + render(&mut h, &taa, "taa_sample"); + } + assert!(!h.renderer.accumulating(1), "converged after {TAA_SAMPLES} samples"); + let last = render(&mut h, &taa, "taa_converged"); + assert_ne!(last.px, first.px, "jittered samples smooth the edges"); + assert_eq!(render(&mut h, &taa, "taa_idle").px, last.px, "a converged view keeps its image"); + render(&mut h, &shaded(&taa, |s| s.env_rotation_deg = 30.0), "taa_changed"); + assert!(h.renderer.accumulating(1), "a change restarts accumulation"); +} diff --git a/crates/texel/Cargo.toml b/crates/texel/Cargo.toml new file mode 100644 index 00000000..dc61a011 --- /dev/null +++ b/crates/texel/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "texturecraft-texel" +description = "TextureCraft texel storage: sparse copy-on-write tiles, UV padding, noise" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lints] +workspace = true + +[dependencies] diff --git a/crates/texel/src/dilate.rs b/crates/texel/src/dilate.rs new file mode 100644 index 00000000..0b3d90a6 --- /dev/null +++ b/crates/texel/src/dilate.rs @@ -0,0 +1,118 @@ +//! UV padding: fill uncovered texels from their nearest covered texel. + +use std::collections::VecDeque; + +/// No source texel. +pub const NONE: u32 = u32::MAX; + +/// For every texel, the index of the covered texel whose value it takes (itself when covered, +/// [`NONE`] when beyond the padding distance). Computed once per UV layout and reused for every +/// dilation of that texture set. +#[derive(Clone, Debug, PartialEq)] +pub struct DilationMap { + pub width: u32, + pub height: u32, + pub src: Vec, +} + +impl DilationMap { + /// `covered.len()` must be `width × height` (shorter input is treated as uncovered). + /// `max_dist` limits padding in texels (Chebyshev steps); `None` fills everything reachable. + pub fn new(covered: &[bool], width: u32, height: u32, max_dist: Option) -> DilationMap { + let n = (width as usize).saturating_mul(height as usize); + let mut src = vec![NONE; n]; + let mut dist = vec![u32::MAX; n]; + let mut queue = VecDeque::new(); + for i in 0..n { + if covered.get(i).copied().unwrap_or(false) { + src[i] = i as u32; + dist[i] = 0; + queue.push_back(i); + } + } + let limit = max_dist.unwrap_or(u32::MAX); + let (w, h) = (width as i64, height as i64); + while let Some(i) = queue.pop_front() { + let d = dist[i]; + if d >= limit { + continue; + } + let (x, y) = ((i as i64) % w, (i as i64) / w); + for (dx, dy) in [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (1, -1), (-1, 1), (1, 1)] { + let (nx, ny) = (x + dx, y + dy); + if nx < 0 || ny < 0 || nx >= w || ny >= h { + continue; + } + let j = (ny * w + nx) as usize; + if dist[j] == u32::MAX { + dist[j] = d + 1; + src[j] = src[i]; + queue.push_back(j); + } + } + } + DilationMap { width, height, src } + } + + /// Copy padded values in place: `data` holds `comps` components per texel. Texels with no + /// source keep their value. + pub fn apply(&self, data: &mut [T], comps: usize) { + for (i, &s) in self.src.iter().enumerate() { + let s = s as usize; + if s == i || s == NONE as usize { + continue; + } + for c in 0..comps { + if let Some(v) = data.get(s * comps + c).copied() + && let Some(d) = data.get_mut(i * comps + c) + { + *d = v; + } + } + } + } + + /// The source texel of (x, y), if any. + pub fn source(&self, x: u32, y: u32) -> Option<(u32, u32)> { + if x >= self.width || y >= self.height { + return None; + } + let s = *self.src.get((y * self.width + x) as usize)?; + (s != NONE).then(|| (s % self.width, s / self.width)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn uncovered_texels_take_the_nearest_covered_value() { + // 5×1 strip, only texel 1 covered. + let covered = [false, true, false, false, false]; + let m = DilationMap::new(&covered, 5, 1, Some(2)); + assert_eq!(m.src, vec![1, 1, 1, 1, NONE]); + let mut data = [0u8, 9, 0, 0, 0]; + m.apply(&mut data, 1); + assert_eq!(data, [9, 9, 9, 9, 0]); + } + + #[test] + fn infinite_padding_fills_everything_and_keeps_covered_values() { + let covered = [true, false, false, false, false, false, false, false, true]; + let m = DilationMap::new(&covered, 3, 3, None); + assert!(m.src.iter().all(|s| *s != NONE)); + assert_eq!(m.src[0], 0); + assert_eq!(m.src[8], 8); + assert_eq!(m.source(2, 2), Some((2, 2))); + assert_eq!(m.source(9, 9), None); + } + + #[test] + fn nothing_covered_is_a_no_op() { + let m = DilationMap::new(&[], 4, 4, None); + let mut data = [3u16; 16]; + m.apply(&mut data, 1); + assert_eq!(data, [3u16; 16]); + } +} diff --git a/crates/texel/src/lib.rs b/crates/texel/src/lib.rs new file mode 100644 index 00000000..329ffaa4 --- /dev/null +++ b/crates/texel/src/lib.rs @@ -0,0 +1,57 @@ +//! # texturecraft-texel +//! +//! Texel storage and texel-space helpers: sparse copy-on-write [`TileImage`]s (64×64 tiles of +//! `u16` components), UV padding ([`DilationMap`]), deterministic [`noise`], and sRGB transfer +//! functions. No workspace dependencies. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod dilate; +pub mod noise; +pub mod tile; + +pub use dilate::DilationMap; +pub use tile::{TILE, TILE_AREA, TileData, TileImage, from_u16, from_u16_signed, to_u16, to_u16_signed}; + +/// Linear → sRGB transfer (0..1). +#[inline] +pub fn linear_to_srgb(v: f32) -> f32 { + let v = v.clamp(0.0, 1.0); + if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 } +} + +/// sRGB → linear transfer (0..1). +#[inline] +pub fn srgb_to_linear(v: f32) -> f32 { + let v = v.clamp(0.0, 1.0); + if v <= 0.040_45 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } +} + +/// Lookup table from the top 12 bits of a linear u16 to an sRGB u8 (fast display upload). +pub fn srgb_lut() -> &'static [u8; 4096] { + static LUT: std::sync::OnceLock<[u8; 4096]> = std::sync::OnceLock::new(); + LUT.get_or_init(|| { + let mut t = [0u8; 4096]; + for (i, v) in t.iter_mut().enumerate() { + *v = (linear_to_srgb((i as f32 + 0.5) / 4096.0) * 255.0 + 0.5) as u8; + } + t + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn srgb_round_trips_and_lut_matches() { + for i in 0..=20 { + let v = i as f32 / 20.0; + assert!((srgb_to_linear(linear_to_srgb(v)) - v).abs() < 1e-5); + } + let lut = srgb_lut(); + assert_eq!(lut[0], 0); + assert_eq!(lut[4095], 255); + assert!((lut[2048] as i32 - (linear_to_srgb(0.5) * 255.0).round() as i32).abs() <= 1); + } +} diff --git a/crates/texel/src/noise.rs b/crates/texel/src/noise.rs new file mode 100644 index 00000000..62411fb7 --- /dev/null +++ b/crates/texel/src/noise.rs @@ -0,0 +1,166 @@ +//! Deterministic 3D noise (integer hashing, so results are identical on every platform). + +/// PCG3D hash (Jarzynski & Olano 2020). +#[inline] +pub fn pcg3d(x: u32, y: u32, z: u32) -> [u32; 3] { + let mut v = + [x.wrapping_mul(1664525).wrapping_add(1013904223), y.wrapping_mul(1664525).wrapping_add(1013904223), z.wrapping_mul(1664525).wrapping_add(1013904223)]; + v[0] = v[0].wrapping_add(v[1].wrapping_mul(v[2])); + v[1] = v[1].wrapping_add(v[2].wrapping_mul(v[0])); + v[2] = v[2].wrapping_add(v[0].wrapping_mul(v[1])); + v = v.map(|a| a ^ (a >> 16)); + v[0] = v[0].wrapping_add(v[1].wrapping_mul(v[2])); + v[1] = v[1].wrapping_add(v[2].wrapping_mul(v[0])); + v[2] = v[2].wrapping_add(v[0].wrapping_mul(v[1])); + v +} + +#[inline] +fn cell(p: f32) -> (i32, f32) { + let f = p.floor(); + (f as i32, p - f) +} + +#[inline] +fn hash01(x: i32, y: i32, z: i32, seed: u32) -> f32 { + pcg3d(x as u32 ^ seed.wrapping_mul(0x9E37_79B9), y as u32, z as u32)[0] as f32 / u32::MAX as f32 +} + +#[inline] +fn smooth(t: f32) -> f32 { + t * t * t * (t * (t * 6.0 - 15.0) + 10.0) +} + +#[inline] +fn lerp(a: f32, b: f32, t: f32) -> f32 { + a + (b - a) * t +} + +/// Value noise in 0..1. +pub fn value(p: [f32; 3], seed: u32) -> f32 { + let (ix, fx) = cell(p[0]); + let (iy, fy) = cell(p[1]); + let (iz, fz) = cell(p[2]); + let (sx, sy, sz) = (smooth(fx), smooth(fy), smooth(fz)); + let h = |dx: i32, dy: i32, dz: i32| hash01(ix + dx, iy + dy, iz + dz, seed); + lerp( + lerp(lerp(h(0, 0, 0), h(1, 0, 0), sx), lerp(h(0, 1, 0), h(1, 1, 0), sx), sy), + lerp(lerp(h(0, 0, 1), h(1, 0, 1), sx), lerp(h(0, 1, 1), h(1, 1, 1), sx), sy), + sz, + ) +} + +fn grad(x: i32, y: i32, z: i32, seed: u32, f: [f32; 3]) -> f32 { + let h = pcg3d(x as u32 ^ seed.wrapping_mul(0x85EB_CA6B), y as u32, z as u32)[1] % 12; + let (a, b) = match h { + 0 => (f[0], f[1]), + 1 => (-f[0], f[1]), + 2 => (f[0], -f[1]), + 3 => (-f[0], -f[1]), + 4 => (f[0], f[2]), + 5 => (-f[0], f[2]), + 6 => (f[0], -f[2]), + 7 => (-f[0], -f[2]), + 8 => (f[1], f[2]), + 9 => (-f[1], f[2]), + 10 => (f[1], -f[2]), + _ => (-f[1], -f[2]), + }; + a + b +} + +/// Gradient (Perlin) noise in about 0..1. +pub fn perlin(p: [f32; 3], seed: u32) -> f32 { + let (ix, fx) = cell(p[0]); + let (iy, fy) = cell(p[1]); + let (iz, fz) = cell(p[2]); + let (sx, sy, sz) = (smooth(fx), smooth(fy), smooth(fz)); + let g = |dx: i32, dy: i32, dz: i32| grad(ix + dx, iy + dy, iz + dz, seed, [fx - dx as f32, fy - dy as f32, fz - dz as f32]); + let v = lerp( + lerp(lerp(g(0, 0, 0), g(1, 0, 0), sx), lerp(g(0, 1, 0), g(1, 1, 0), sx), sy), + lerp(lerp(g(0, 0, 1), g(1, 0, 1), sx), lerp(g(0, 1, 1), g(1, 1, 1), sx), sy), + sz, + ); + (v * 0.5 + 0.5).clamp(0.0, 1.0) +} + +/// Worley (cellular) noise: distance to the nearest feature point, about 0..1. +pub fn worley(p: [f32; 3], seed: u32) -> f32 { + cellular(p, seed).0 +} + +/// Cellular noise: distances to the nearest and second-nearest feature points (each about +/// 0..1) and a random 0..1 id of the nearest point's cell (for per-cell variation). +pub fn cellular(p: [f32; 3], seed: u32) -> (f32, f32, f32) { + let (ix, fx) = cell(p[0]); + let (iy, fy) = cell(p[1]); + let (iz, fz) = cell(p[2]); + let (mut f1, mut f2, mut id) = (f32::MAX, f32::MAX, 0u32); + for dz in -1..=1 { + for dy in -1..=1 { + for dx in -1..=1 { + let h = pcg3d((ix + dx) as u32 ^ seed.wrapping_mul(0xC2B2_AE35), (iy + dy) as u32, (iz + dz) as u32); + let o = h.map(|v| v as f32 / u32::MAX as f32); + let d = [dx as f32 + o[0] - fx, dy as f32 + o[1] - fy, dz as f32 + o[2] - fz]; + let d2 = d[0] * d[0] + d[1] * d[1] + d[2] * d[2]; + if d2 < f1 { + (f2, f1, id) = (f1, d2, h[0] ^ h[1]); + } else if d2 < f2 { + f2 = d2; + } + } + } + } + (f1.sqrt().min(1.0), f2.sqrt().min(1.0), id as f32 / u32::MAX as f32) +} + +/// Random 0..1 value of an integer lattice cell (per-brick or per-tile variation). +pub fn cell_hash(x: i32, y: i32, z: i32, seed: u32) -> f32 { + hash01(x, y, z, seed) +} + +/// Fractal sum of `octaves` layers of `base` noise, normalized to 0..1. +pub fn fbm(p: [f32; 3], octaves: u32, seed: u32, base: fn([f32; 3], u32) -> f32) -> f32 { + let (mut sum, mut amp, mut norm, mut q) = (0.0, 0.5, 0.0, p); + for o in 0..octaves.clamp(1, 12) { + sum += amp * base(q, seed.wrapping_add(o * 31)); + norm += amp; + q = q.map(|v| v * 2.0 + 17.13); + amp *= 0.5; + } + sum / norm +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn noises_are_deterministic_and_in_range() { + for i in 0..500 { + let p = [i as f32 * 0.37, i as f32 * 0.11 - 3.0, (i % 17) as f32 * 0.53]; + for f in [value, perlin, worley] { + let a = f(p, 3); + assert!((0.0..=1.0).contains(&a), "{a}"); + assert_eq!(a, f(p, 3)); + } + let a = fbm(p, 5, 1, value); + assert!((0.0..=1.0).contains(&a)); + let (f1, f2, id) = cellular(p, 3); + assert!(f1 <= f2 && (0.0..=1.0).contains(&f2) && (0.0..=1.0).contains(&id), "{f1} {f2} {id}"); + } + } + + #[test] + fn seeds_change_the_pattern() { + let p = [1.3, 2.7, 0.4]; + assert_ne!(value(p, 1), value(p, 2)); + } + + #[test] + fn value_noise_is_continuous() { + let a = value([0.5, 0.5, 0.5], 0); + let b = value([0.5001, 0.5, 0.5], 0); + assert!((a - b).abs() < 1e-2); + } +} diff --git a/crates/texel/src/tile.rs b/crates/texel/src/tile.rs new file mode 100644 index 00000000..7a68bf0a --- /dev/null +++ b/crates/texel/src/tile.rs @@ -0,0 +1,315 @@ +//! Sparse, copy-on-write tiled images of `u16` components. + +use std::sync::Arc; + +/// Tile edge in texels. +pub const TILE: u32 = 64; +/// Texels per tile. +pub const TILE_AREA: usize = (TILE * TILE) as usize; + +/// A tile's texel data: `TILE_AREA × comps` interleaved components. +pub type TileData = Arc>; + +/// An image split into 64×64 tiles; unallocated tiles read as zero. Cloning shares every tile, +/// so snapshots for undo cost one `Arc` clone per allocated tile. +#[derive(Clone, Debug, PartialEq)] +pub struct TileImage { + width: u32, + height: u32, + comps: usize, + tiles_x: u32, + tiles_y: u32, + tiles: Vec>, +} + +impl TileImage { + /// `comps` is clamped to 1..=4; sizes to at least 1. + pub fn new(width: u32, height: u32, comps: usize) -> TileImage { + let (width, height) = (width.max(1), height.max(1)); + let tiles_x = width.div_ceil(TILE); + let tiles_y = height.div_ceil(TILE); + TileImage { width, height, comps: comps.clamp(1, 4), tiles_x, tiles_y, tiles: vec![None; (tiles_x * tiles_y) as usize] } + } + + pub fn width(&self) -> u32 { + self.width + } + pub fn height(&self) -> u32 { + self.height + } + pub fn comps(&self) -> usize { + self.comps + } + pub fn tiles_x(&self) -> u32 { + self.tiles_x + } + pub fn tiles_y(&self) -> u32 { + self.tiles_y + } + + fn tile_index(&self, tx: u32, ty: u32) -> Option { + (tx < self.tiles_x && ty < self.tiles_y).then(|| (ty * self.tiles_x + tx) as usize) + } + + pub fn tile(&self, tx: u32, ty: u32) -> Option<&[u16]> { + self.tile_index(tx, ty).and_then(|i| self.tiles.get(i)).and_then(|t| t.as_deref().map(Vec::as_slice)) + } + + /// The shared tile handle (for undo snapshots and change fingerprints). + pub fn tile_handle(&self, tx: u32, ty: u32) -> Option<&TileData> { + self.tile_index(tx, ty).and_then(|i| self.tiles.get(i)).and_then(Option::as_ref) + } + + /// Replace a tile wholesale (None frees it). + pub fn set_tile(&mut self, tx: u32, ty: u32, data: Option) { + let len = TILE_AREA * self.comps; + let data = data.filter(|d| d.len() == len); + if let Some(i) = self.tile_index(tx, ty) + && let Some(slot) = self.tiles.get_mut(i) + { + *slot = data; + } + } + + /// Mutable texels of a tile, allocating it filled with `fill` (one value per component) + /// when absent and un-sharing it when shared. + pub fn tile_mut(&mut self, tx: u32, ty: u32, fill: &[u16]) -> Option<&mut [u16]> { + let comps = self.comps; + let i = self.tile_index(tx, ty)?; + let slot = self.tiles.get_mut(i)?; + let tile = slot.get_or_insert_with(|| { + let mut v = vec![0u16; TILE_AREA * comps]; + if fill.iter().any(|f| *f != 0) { + for px in v.chunks_exact_mut(comps) { + for (d, s) in px.iter_mut().zip(fill) { + *d = *s; + } + } + } + Arc::new(v) + }); + Some(Arc::make_mut(tile).as_mut_slice()) + } + + pub fn is_allocated(&self, tx: u32, ty: u32) -> bool { + self.tile_handle(tx, ty).is_some() + } + + /// Allocated tile coordinates. + pub fn allocated(&self) -> impl Iterator + '_ { + self.tiles.iter().enumerate().filter(|(_, t)| t.is_some()).map(|(i, _)| (i as u32 % self.tiles_x, i as u32 / self.tiles_x)) + } + + pub fn allocated_count(&self) -> usize { + self.tiles.iter().filter(|t| t.is_some()).count() + } + + /// Components of the texel at (x, y); zeros when out of range or unallocated. + pub fn get(&self, x: u32, y: u32) -> [u16; 4] { + let mut out = [0u16; 4]; + if x >= self.width || y >= self.height { + return out; + } + if let Some(t) = self.tile(x / TILE, y / TILE) { + let o = (((y % TILE) * TILE + x % TILE) as usize) * self.comps; + for (c, v) in out.iter_mut().take(self.comps).enumerate() { + *v = t.get(o + c).copied().unwrap_or(0); + } + } + out + } + + /// Set one texel (allocating its tile with zeros). + pub fn set(&mut self, x: u32, y: u32, v: &[u16]) { + if x >= self.width || y >= self.height { + return; + } + let comps = self.comps; + if let Some(t) = self.tile_mut(x / TILE, y / TILE, &[]) { + let o = (((y % TILE) * TILE + x % TILE) as usize) * comps; + for (c, s) in v.iter().take(comps).enumerate() { + if let Some(d) = t.get_mut(o + c) { + *d = *s; + } + } + } + } + + /// Bytes held by allocated tiles (memory estimate for the status bar). + pub fn bytes(&self) -> usize { + self.allocated_count() * TILE_AREA * self.comps * 2 + } + + /// Drop tiles whose every texel is zero. + pub fn prune_empty(&mut self) { + for t in &mut self.tiles { + if t.as_ref().is_some_and(|d| d.iter().all(|v| *v == 0)) { + *t = None; + } + } + } +} + +/// 0..1 → u16. +#[inline] +pub fn to_u16(v: f32) -> u16 { + (v.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16 +} + +/// u16 → 0..1. +#[inline] +pub fn from_u16(v: u16) -> f32 { + v as f32 * (1.0 / 65535.0) +} + +/// -1..1 → u16. +#[inline] +pub fn to_u16_signed(v: f32) -> u16 { + to_u16(v * 0.5 + 0.5) +} + +/// u16 → -1..1. +#[inline] +pub fn from_u16_signed(v: u16) -> f32 { + from_u16(v) * 2.0 - 1.0 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn unallocated_tiles_read_zero_and_writes_allocate() { + let mut img = TileImage::new(200, 130, 2); + assert_eq!(img.tiles_x(), 4); + assert_eq!(img.tiles_y(), 3); + assert_eq!(img.get(10, 10), [0; 4]); + img.set(199, 129, &[7, 9]); + assert_eq!(img.get(199, 129), [7, 9, 0, 0]); + assert_eq!(img.allocated_count(), 1); + assert_eq!(img.allocated().collect::>(), vec![(3, 2)]); + // Out of range is ignored, not a panic. + img.set(500, 500, &[1, 1]); + assert_eq!(img.get(500, 500), [0; 4]); + } + + #[test] + fn clones_share_tiles_until_written() { + let mut a = TileImage::new(64, 64, 1); + a.set(0, 0, &[5]); + let b = a.clone(); + assert!(Arc::ptr_eq(a.tile_handle(0, 0).unwrap(), b.tile_handle(0, 0).unwrap())); + a.set(1, 0, &[6]); + assert!(!Arc::ptr_eq(a.tile_handle(0, 0).unwrap(), b.tile_handle(0, 0).unwrap())); + assert_eq!(b.get(1, 0)[0], 0); + assert_eq!(a.get(1, 0)[0], 6); + } + + #[test] + fn tile_mut_fills_new_tiles() { + let mut img = TileImage::new(64, 64, 2); + let t = img.tile_mut(0, 0, &[100, 200]).unwrap(); + assert_eq!(&t[..4], &[100, 200, 100, 200]); + img.prune_empty(); + assert_eq!(img.allocated_count(), 1); + img.set_tile(0, 0, Some(Arc::new(vec![0; TILE_AREA * 2]))); + img.prune_empty(); + assert_eq!(img.allocated_count(), 0); + } + + #[test] + fn unorm_round_trips() { + for v in [0.0, 0.25, 0.5, 1.0] { + assert!((from_u16(to_u16(v)) - v).abs() < 1e-4); + assert!((from_u16_signed(to_u16_signed(v * 2.0 - 1.0)) - (v * 2.0 - 1.0)).abs() < 1e-4); + } + assert_eq!(to_u16(f32::NAN), 0); + } +} + +impl TileImage { + /// A bilinear resample to `width × height` (same component count). Unallocated source + /// texels read as `empty` (one value per component); destination tiles whose source + /// footprint is entirely unallocated stay unallocated. + pub fn resampled(&self, width: u32, height: u32, empty: &[u16]) -> TileImage { + let mut out = TileImage::new(width, height, self.comps); + if self.allocated_count() == 0 { + return out; + } + let (sw, sh) = (self.width as f32, self.height as f32); + let (dw, dh) = (out.width, out.height); + let comps = self.comps; + for ty in 0..out.tiles_y { + for tx in 0..out.tiles_x { + // Skip destination tiles whose source footprint has no allocated tile. + let (x0, y0) = ((tx * TILE) as f32 * sw / dw as f32, (ty * TILE) as f32 * sh / dh as f32); + let (x1, y1) = (((tx + 1) * TILE) as f32 * sw / dw as f32, ((ty + 1) * TILE) as f32 * sh / dh as f32); + let any = ((y0 as u32).saturating_sub(1) / TILE..=((y1 as u32 + 1) / TILE).min(self.tiles_y.saturating_sub(1))) + .any(|sy| ((x0 as u32).saturating_sub(1) / TILE..=((x1 as u32 + 1) / TILE).min(self.tiles_x.saturating_sub(1))).any(|sx| self.is_allocated(sx, sy))); + if !any { + continue; + } + let mut data = vec![0u16; TILE_AREA * comps]; + for ly in 0..TILE { + let y = ty * TILE + ly; + if y >= dh { + break; + } + let fy = ((y as f32 + 0.5) * sh / dh as f32 - 0.5).max(0.0); + let (iy, wy) = (fy.floor() as u32, fy.fract()); + for lx in 0..TILE { + let x = tx * TILE + lx; + if x >= dw { + break; + } + let fx = ((x as f32 + 0.5) * sw / dw as f32 - 0.5).max(0.0); + let (ix, wx) = (fx.floor() as u32, fx.fract()); + let get = |x: u32, y: u32| { + let (x, y) = (x.min(self.width - 1), y.min(self.height - 1)); + if self.is_allocated(x / TILE, y / TILE) { + self.get(x, y) + } else { + let mut e = [0u16; 4]; + for (d, s) in e.iter_mut().zip(empty) { + *d = *s; + } + e + } + }; + let (a, b, c, d) = (get(ix, iy), get(ix + 1, iy), get(ix, iy + 1), get(ix + 1, iy + 1)); + let o = ((ly * TILE + lx) as usize) * comps; + for k in 0..comps { + let top = a[k] as f32 * (1.0 - wx) + b[k] as f32 * wx; + let bot = c[k] as f32 * (1.0 - wx) + d[k] as f32 * wx; + if let Some(slot) = data.get_mut(o + k) { + *slot = (top * (1.0 - wy) + bot * wy + 0.5) as u16; + } + } + } + } + out.set_tile(tx, ty, Some(Arc::new(data))); + } + } + out + } +} + +#[cfg(test)] +mod resample_tests { + use super::*; + + #[test] + fn resample_up_and_down_keeps_values_and_sparsity() { + let mut img = TileImage::new(128, 128, 2); + for y in 0..64 { + for x in 0..64 { + img.set(x, y, &[30000, 65535]); + } + } + let up = img.resampled(256, 256, &[]); + assert_eq!(up.get(40, 40)[..2], [30000, 65535]); + assert!(!up.is_allocated(3, 3), "empty regions stay sparse"); + let down = img.resampled(64, 64, &[]); + assert_eq!(down.get(10, 10)[..2], [30000, 65535]); + } +} diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml new file mode 100644 index 00000000..5eb58e95 --- /dev/null +++ b/crates/ui-egui/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "texturecraft-ui-egui" +description = "TextureCraft's egui shell: Painter-style docks, toolbars, viewports and windows" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lints] +workspace = true + +[dependencies] +texturecraft-engine = { workspace = true } +texturecraft-render = { workspace = true } +eframe = { workspace = true, features = ["default_fonts", "wgpu", "persistence"] } +egui = { workspace = true } +egui-phosphor = { version = "0.14", features = ["regular", "fill"] } +# Dockable, tabbed, floating panels (MIT). +egui_dock = { version = "0.21", features = ["serde"] } +serde = { workspace = true } +serde_json = { workspace = true } +rayon = { workspace = true } +log = { workspace = true } +base64 = "0.22" +image = { workspace = true } +rfd = { workspace = true } +zip = { workspace = true } + +# Browser file I/O (web_files.rs): async pickers and Blob downloads. +[target.'cfg(target_arch = "wasm32")'.dependencies] +wasm-bindgen = "0.2" +wasm-bindgen-futures = "0.4" +js-sys = "0.3" +web-sys = { version = "0.3", features = ["Window", "Document", "HtmlElement", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "Url", "CssStyleDeclaration"] } diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs new file mode 100644 index 00000000..15ac31d7 --- /dev/null +++ b/crates/ui-egui/src/control.rs @@ -0,0 +1,156 @@ +//! The JSON control channel: agents drive the running app with the same commands as the menus, +//! inspect session and UI state, invoke menu items, paint strokes and take screenshots. +//! +//! Methods (params as JSON objects): +//! - `command.run {id, params?}`: run an engine command (programmatic: never opens a dialog). +//! - `command.list {filter?}`: every command with params docs and enabled state. +//! - `session.inspect`: project, texture sets, layer stack, tool, camera, display, history. +//! - `ui.inspect`: the UI state (panels, windows, mode, layout, viewport rect). +//! - `ui.menu.list`: the menu catalog with ids, shortcuts and whether each is live. +//! - `ui.menu.invoke {id}`: behave like a menu click (may open a window). +//! - `ui.set {...}`: merge fields into the UI state (e.g. `{"layout":"Only3D"}`). +//! - `viewport.screenshot {view?: "3d"|"2d"}`: PNG of a viewport, base64. +//! - `ui.screenshot`: PNG of the whole window (replies after the next frame). + +use std::sync::mpsc::{Receiver, Sender, channel}; + +use base64::Engine as _; +use serde_json::{Value, json}; + +use crate::TextureCraftApp; + +pub struct ControlRequest { + pub method: String, + pub params: Value, + pub reply: Sender, +} + +impl ControlRequest { + pub fn new(method: String, params: Value) -> (ControlRequest, Receiver) { + let (tx, rx) = channel(); + (ControlRequest { method, params, reply: tx }, rx) + } +} + +fn ok(v: Value) -> Value { + json!({ "ok": true, "result": v }) +} + +fn err(e: impl std::fmt::Display) -> Value { + json!({ "ok": false, "error": e.to_string() }) +} + +pub fn poll(app: &mut TextureCraftApp, ctx: &egui::Context) { + // Finished window screenshots. + let shots: Vec> = + ctx.input(|i| i.raw.events.iter().filter_map(|e| if let egui::Event::Screenshot { image, .. } = e { Some(image.clone()) } else { None }).collect()); + for img in shots { + let reply = encode_png(img.size[0] as u32, img.size[1] as u32, img.pixels.iter().flat_map(|c| c.to_array()).collect()) + .map(|(w, h, png)| ok(json!({ "width": w, "height": h, "png": base64::engine::general_purpose::STANDARD.encode(png) }))) + .unwrap_or_else(|| err("could not encode the screenshot")); + for w in app.screenshot_waiters.drain(..) { + let _ = w.send(reply.clone()); + } + } + if !app.screenshot_waiters.is_empty() { + ctx.request_repaint(); + } + let Some(rx) = app.control.as_ref() else { return }; + let reqs: Vec = rx.try_iter().collect(); + for r in reqs { + if r.method == "ui.screenshot" { + // Replies when the next frame has been captured. + app.screenshot_waiters.push(r.reply); + ctx.send_viewport_cmd(egui::ViewportCommand::Screenshot(egui::UserData::default())); + ctx.request_repaint(); + continue; + } + let reply = handle(app, ctx, &r.method, &r.params); + let _ = r.reply.send(reply); + ctx.request_repaint(); + } +} + +pub(crate) fn encode_png(w: u32, h: u32, rgba: Vec) -> Option<(u32, u32, Vec)> { + let img = image::RgbaImage::from_raw(w, h, rgba)?; + let mut png = Vec::new(); + img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).ok()?; + Some((w, h, png)) +} + +pub fn handle(app: &mut TextureCraftApp, ctx: &egui::Context, method: &str, p: &Value) -> Value { + match method { + "ping" => ok(json!("pong")), + "command.run" => { + let Some(id) = p.get("id").and_then(Value::as_str) else { return err("`id` is required") }; + let params = p.get("params").cloned().unwrap_or(Value::Null); + match texturecraft_engine::execute(&mut app.session, id, ¶ms) { + Ok(v) => ok(v), + Err(e) => err(e), + } + } + "command.list" => ok(texturecraft_engine::commands::list(&app.session, p.get("filter").and_then(Value::as_str))), + "session.inspect" => ok(texturecraft_engine::inspect::session(&app.session)), + "ui.inspect" => { + let r = app.viewport_rect; + ok(json!({ + "state": app.ui, + "viewport": [r.min.x, r.min.y, r.width(), r.height()], + "fps": app.fps, + "uiMs": app.ui_ms, + "baking": app.bake_progress(), + "toasts": app.toasts.iter().map(|t| json!({"text": t.text, "error": t.error})).collect::>(), + })) + } + "ui.menu.list" => ok(json!( + crate::menus::CATALOG + .iter() + .filter(|i| i.label != "-") + .map(|i| json!({ + "menu": i.menu, "submenu": i.sub, "label": i.label, "id": i.id, "shortcut": crate::keymap::shortcut_of(&app.ui.keymap, i.id), + "implemented": crate::menus::implemented(i.id), "enabled": crate::menus::enabled(app, i.id), + })) + .collect::>() + )), + "ui.menu.invoke" => { + let Some(id) = p.get("id").and_then(Value::as_str) else { return err("`id` is required") }; + if !crate::menus::implemented(id) { + return err(format!("menu item `{id}` is not implemented")); + } + crate::menus::invoke(app, ctx, id); + ok(Value::Null) + } + "ui.set" => { + let mut cur = serde_json::to_value(&app.ui).unwrap_or(Value::Null); + if let (Some(obj), Some(patch)) = (cur.as_object_mut(), p.as_object()) { + for (k, v) in patch { + obj.insert(k.clone(), v.clone()); + } + } + match serde_json::from_value(cur) { + Ok(ui) => { + app.ui = ui; + ok(Value::Null) + } + Err(e) => err(format!("invalid UI state: {e}")), + } + } + "viewport.screenshot" => { + let view = if p.get("view").and_then(Value::as_str) == Some("2d") { crate::viewport::VIEW_2D } else { crate::viewport::VIEW_3D }; + match screenshot(app, view) { + Some((w, h, png)) => ok(json!({ "width": w, "height": h, "png": base64::engine::general_purpose::STANDARD.encode(png) })), + None => err("no rendered view to capture yet"), + } + } + other => err(format!("unknown method `{other}`")), + } +} + +/// Encode a rendered view as PNG. +pub fn screenshot(app: &TextureCraftApp, view: u64) -> Option<(u32, u32, Vec)> { + let rs = app.render_state.as_ref()?; + let renderer = rs.renderer.read(); + let gpu = renderer.callback_resources.get::()?; + let (w, h, rgba) = gpu.renderer.read_view(&rs.device, &rs.queue, view)?; + encode_png(w, h, rgba) +} diff --git a/crates/ui-egui/src/dock.rs b/crates/ui-egui/src/dock.rs new file mode 100644 index 00000000..5e2600dc --- /dev/null +++ b/crates/ui-egui/src/dock.rs @@ -0,0 +1,1321 @@ +//! Panel bodies of the right column: Texture Set List, Layers, Texture Set Settings, Properties +//! (hosted as dock tabs by `docking`). + +use egui::{Color32, Rect, RichText, Sense, Stroke, Ui, pos2, vec2}; +use egui_phosphor::regular as ic; +use serde_json::{Value, json}; +use texturecraft_engine::core::{BakedMap, BlendMode, Channel, EffectKind, Fill, Filter, GeneratorKind, Layer, LayerId, LayerKind, ProceduralKind, Source}; +use texturecraft_engine::paint::BrushAlpha; +use texturecraft_engine::{StackTarget, Tool}; + +use crate::TextureCraftApp; +use crate::state::PropsTab; +use crate::theme::{TOKENS, caption}; +use crate::widgets::{chip, color_swatch, elided, icon_button, section, slider}; + +const TAG_COLORS: [Color32; 8] = [ + Color32::from_rgb(0xc0, 0x40, 0x40), + Color32::from_rgb(0xd0, 0x80, 0x30), + Color32::from_rgb(0xc8, 0xb0, 0x30), + Color32::from_rgb(0x58, 0xa0, 0x40), + Color32::from_rgb(0x30, 0xa0, 0x90), + Color32::from_rgb(0x40, 0x78, 0xd0), + Color32::from_rgb(0x80, 0x58, 0xc8), + Color32::from_rgb(0xb0, 0x50, 0xa0), +]; + +pub(crate) fn texture_set_list(app: &mut TextureCraftApp, ui: &mut Ui) { + let sets: Vec<(String, u32, bool)> = app.session.doc.project.texture_sets.iter().map(|t| (t.name.clone(), t.resolution, t.visible)).collect(); + let active = app.session.ts(); + egui::ScrollArea::vertical().id_salt("ts_scroll").show(ui, |ui| { + for (i, (name, res, visible)) in sets.into_iter().enumerate() { + let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 22.0), Sense::click()); + let p = ui.painter(); + let bg = if i == active { + TOKENS.row_selected + } else if resp.hovered() { + TOKENS.row_hover + } else { + TOKENS.panel + }; + p.rect_filled(rect, 2.0, bg); + if i == active { + p.rect_stroke(rect, 2.0, Stroke::new(1.0, TOKENS.accent), egui::StrokeKind::Inside); + } + let eye = Rect::from_min_size(rect.min + vec2(4.0, 3.0), vec2(16.0, 16.0)); + let eye_resp = ui.interact(eye, ui.id().with(("ts_eye", i)), Sense::click()).on_hover_text("Show or hide the texture set"); + p.text(eye.center(), egui::Align2::CENTER_CENTER, if visible { ic::EYE } else { ic::EYE_SLASH }, egui::FontId::proportional(13.0), TOKENS.text); + // Narrow panels drop the shader label, then the resolution, before cutting the name. + let cy = rect.center().y; + let mut right = rect.right() - 6.0; + if rect.width() >= 260.0 { + let g = p.layout_no_wrap("Main shader".into(), egui::FontId::proportional(11.0), TOKENS.text_dim); + right -= g.size().x; + p.galley(pos2(right, cy - g.size().y * 0.5), g, TOKENS.text_dim); + right -= 10.0; + } + if rect.width() >= 170.0 { + let g = p.layout_no_wrap(format!("{res}x{res}"), egui::FontId::proportional(11.0), TOKENS.text_dim); + let x = (rect.left() + rect.width() * 0.48).min(right - g.size().x); + p.galley(pos2(x, cy - g.size().y * 0.5), g, TOKENS.text_dim); + right = x - 8.0; + } + let color = if i == active { TOKENS.text_strong } else { TOKENS.text }; + let g = elided(p, name, 12.0, color, right - rect.left() - 26.0); + p.galley(pos2(rect.left() + 26.0, cy - g.size().y * 0.5), g, color); + if eye_resp.clicked() { + app.run("textureSet.visible", json!({"ts": i})); + } else if resp.clicked() { + app.run("textureSet.select", json!({"ts": i})); + } + } + }); +} + +// --------------------------------------------------------------------------------------------- +// Layers + +pub(crate) fn layers(app: &mut TextureCraftApp, ui: &mut Ui) { + // Wraps in a narrow column instead of widening the rows below past the panel. + ui.horizontal_wrapped(|ui| { + let chans = app.session.doc.project.texture_set(app.session.ts()).map(|t| t.channel_list()).unwrap_or_default(); + egui::ComboBox::from_id_salt("layer_channel").width(86.0).selected_text(app.ui.layer_channel.label()).show_ui(ui, |ui| { + for c in chans { + ui.selectable_value(&mut app.ui.layer_channel, c, c.label()); + } + }); + let has = app.session.selected_layer().is_some(); + let selected = app.session.selected_layer().and_then(|id| app.session.doc.project.layers.get(&id)).cloned(); + ui.menu_button(RichText::new(ic::MAGIC_WAND).size(14.0), |ui| match &selected { + Some(l) => effect_menu(app, ui, l), + None => { + ui.label(RichText::new("Select a layer").color(TOKENS.text_dim)); + } + }) + .response + .on_hover_text("Add effect"); + ui.menu_button(RichText::new(ic::SELECTION_BACKGROUND).size(14.0), |ui| { + for (label, id) in + [("Add white mask", "mask.add.white"), ("Add black mask", "mask.add.black"), ("Add mask with color selection", "mask.add.colorSelection")] + { + if ui.add_enabled(has, egui::Button::new(label)).clicked() { + app.run(id, Value::Null); + ui.close(); + } + } + }) + .response + .on_hover_text("Add mask"); + if icon_button(ui, ic::PAINT_BRUSH, "Add paint layer", false, 22.0).clicked() { + app.run("layer.add.paint", Value::Null); + } + if icon_button(ui, ic::PAINT_BUCKET, "Add fill layer", false, 22.0).clicked() { + app.run("layer.add.fill", Value::Null); + } + if icon_button(ui, ic::FOLDER_SIMPLE, "Add folder", false, 22.0).clicked() { + app.run("layer.add.group", Value::Null); + } + if icon_button(ui, ic::TRASH, &crate::keymap::tip(&app.ui.keymap, "Remove layer", "layer.remove"), false, 22.0).clicked() { + app.run("layer.remove", Value::Null); + } + }); + ui.add_space(2.0); + let ts = app.session.ts(); + let stack: Vec = app.session.doc.project.texture_sets.get(ts).map(|t| t.stack.iter().rev().copied().collect()).unwrap_or_default(); + egui::ScrollArea::vertical().id_salt("layers_scroll").auto_shrink([false, false]).show(ui, |ui| { + for id in stack { + layer_tree(app, ui, id, 0); + } + // Click on empty space deselects nothing; leave room to drop at the bottom. + let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 24.0), Sense::hover()); + if let Some(dragged) = resp.dnd_release_payload::() { + let bottom = app.session.doc.project.texture_sets.get(ts).and_then(|t| t.stack.first().copied()); + if bottom.map(|b| b.0) != Some(*dragged) { + app.run("layer.move", json!({"layer": *dragged, "index": 0})); + } + } + if resp.dnd_hover_payload::().is_some() { + ui.painter().line_segment([rect.left_top(), rect.right_top()], Stroke::new(2.0, TOKENS.accent)); + } + }); +} + +/// Whether the selection targets the layer content (the content or one of its effects) rather +/// than the mask. +fn targets_content(app: &TextureCraftApp) -> bool { + let p = &app.session.doc.project; + match app.session.target { + StackTarget::Content => true, + StackTarget::Mask => false, + StackTarget::Effect(e) => p.effect(e).and_then(|(owner, _)| p.layers.get(&owner)).is_some_and(|l| l.content_effects.iter().any(|x| x.id == e)), + } +} + +/// "Add effect" items for `layer` (toolbar and context menu). Effects that fit both stacks go +/// to the content when it is the selected target; generators and compare always go to the +/// mask, anchor points to the content. +fn effect_menu(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer) { + let content = !layer.is_group() && app.session.selected_layer() == Some(layer.id) && targets_content(app); + let t = if content { "content" } else { "mask" }; + ui.label(caption(if content { "Add to the layer content" } else { "Add to the mask" })); + let items = [ + ("Add generator", "effect.add.generator", json!({})), + ("Add paint", "effect.add.paint", json!({"target": t})), + ("Add fill", "effect.add.fill", json!({"target": t})), + ("Add levels", "effect.add.levels", json!({"target": t})), + ("Add filter: blur", "effect.add.filter", json!({"target": t, "filter": "blur", "radius": 8})), + ("Add filter: sharpen", "effect.add.filter", json!({"target": t, "filter": "sharpen"})), + ("Add filter: invert", "effect.add.filter", json!({"target": t, "filter": "invert"})), + ("Add filter: threshold", "effect.add.filter", json!({"target": t, "filter": "threshold"})), + ("Add filter: HSL", "effect.add.filter", json!({"target": t, "filter": "hsl"})), + ("Add compare mask", "effect.add.compare", json!({})), + ("Add anchor point", "effect.add.anchor", json!({})), + ]; + for (label, id, p) in items { + let enabled = id != "effect.add.anchor" || !layer.is_group(); + if ui.add_enabled(enabled, egui::Button::new(label)).clicked() { + if app.session.selected_layer() != Some(layer.id) { + app.run("layer.select", json!({"layer": layer.id.0})); + } + app.run(id, with(&p, json!({"layer": layer.id.0}))); + ui.close(); + } + } +} + +fn layer_tree(app: &mut TextureCraftApp, ui: &mut Ui, id: LayerId, depth: usize) { + let Some(layer) = app.session.doc.project.layers.get(&id).cloned() else { return }; + layer_row(app, ui, &layer, depth); + for e in layer.content_effects.iter().rev() { + effect_row(app, ui, &layer, e.id, &e.name, e.visible, depth + 1, ic::SPHERE); + } + if let Some(m) = &layer.mask { + for e in m.effects.iter().rev() { + effect_row(app, ui, &layer, e.id, &e.name, e.visible, depth + 1, ic::MAGIC_WAND); + } + } + if let LayerKind::Group { children, collapsed } = &layer.kind + && !collapsed + && depth < 12 + { + for c in children.iter().rev() { + layer_tree(app, ui, *c, depth + 1); + } + } +} + +fn layer_row(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer, depth: usize) { + let selected = app.session.selected_layer() == Some(layer.id); + let h = 38.0; + let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), h), Sense::click_and_drag()); + let p = ui.painter().clone(); + let bg = if selected && app.session.target == StackTarget::Content { + TOKENS.row_selected + } else if resp.hovered() { + TOKENS.row_hover + } else { + TOKENS.row + }; + p.rect_filled(rect.shrink2(vec2(0.0, 1.0)), 2.0, bg); + if selected { + p.rect_stroke(rect.shrink2(vec2(0.0, 1.0)), 2.0, Stroke::new(1.0, TOKENS.accent), egui::StrokeKind::Inside); + } + if let Some(tag) = layer.color_tag.and_then(|t| TAG_COLORS.get(t as usize)) { + p.rect_filled(Rect::from_min_size(rect.min + vec2(0.0, 1.0), vec2(3.0, h - 2.0)), 0.0, *tag); + } + let x0 = rect.left() + 6.0 + depth as f32 * 14.0; + // Eye. + let eye = Rect::from_center_size(pos2(x0 + 8.0, rect.center().y), vec2(18.0, 18.0)); + let eye_resp = ui.interact(eye, ui.id().with(("eye", layer.id.0)), Sense::click()).on_hover_text("Show or hide the layer"); + p.text( + eye.center(), + egui::Align2::CENTER_CENTER, + if layer.visible { ic::EYE } else { ic::EYE_SLASH }, + egui::FontId::proportional(13.0), + if layer.visible { TOKENS.text } else { TOKENS.text_dim }, + ); + if eye_resp.clicked() { + app.run("layer.visible", json!({"layer": layer.id.0})); + } + // Group caret. + let mut x = x0 + 20.0; + if let LayerKind::Group { collapsed, .. } = layer.kind { + let caret = Rect::from_center_size(pos2(x + 6.0, rect.center().y), vec2(14.0, 14.0)); + let cr = ui.interact(caret, ui.id().with(("caret", layer.id.0)), Sense::click()).on_hover_text("Expand or collapse the folder"); + p.text( + caret.center(), + egui::Align2::CENTER_CENTER, + if collapsed { ic::CARET_RIGHT } else { ic::CARET_DOWN }, + egui::FontId::proportional(11.0), + TOKENS.text_dim, + ); + if cr.clicked() + && let Some(LayerKind::Group { collapsed, .. }) = app.session.doc.project.layers.get_mut(&layer.id).map(|l| &mut l.kind) + { + *collapsed = !*collapsed; + } + x += 14.0; + } + // Thumbnail. + let thumb = Rect::from_min_size(pos2(x, rect.top() + 5.0), vec2(28.0, 28.0)); + p.rect_filled(thumb, 2.0, Color32::from_rgb(0x1a, 0x1a, 0x1a)); + match &layer.kind { + LayerKind::Fill(f) => { + let c = match f.source(Channel::BaseColor) { + Source::Uniform(v) => v, + Source::Procedural { low, high, .. } => [(low[0] + high[0]) * 0.5, (low[1] + high[1]) * 0.5, (low[2] + high[2]) * 0.5], + Source::Image { .. } | Source::Anchor { .. } => [0.5; 3], + }; + crate::thumbs::paint_sphere(&p, thumb.center(), 12.0, c); + } + LayerKind::Paint => { + p.text(thumb.center(), egui::Align2::CENTER_CENTER, ic::PAINT_BRUSH, egui::FontId::proportional(15.0), TOKENS.text); + } + LayerKind::Group { .. } => { + p.text(thumb.center(), egui::Align2::CENTER_CENTER, ic::FOLDER_SIMPLE, egui::FontId::proportional(16.0), TOKENS.text); + } + LayerKind::Instance { .. } => { + p.text(thumb.center(), egui::Align2::CENTER_CENTER, ic::LINK, egui::FontId::proportional(16.0), TOKENS.accent); + } + } + x = thumb.right() + 8.0; + // An instance draws its source's content, blending and (unless it has its own) mask. + let project = &app.session.doc.project; + let source = layer.instance_source().map(|src| { + let set = project.locate(src).and_then(|(ts, _, _)| project.texture_sets.get(ts)).map(|t| t.name.as_str()).unwrap_or("?"); + project.layers.get(&src).map_or_else(|| "missing source".to_string(), |l| format!("{} · {set}", l.name)) + }); + let mask = project.layers.get(&project.mask_owner(layer.id)).and_then(|l| l.mask.clone()); + let blend = project.content_layer(layer.id).and_then(|l| l.blend(app.ui.layer_channel)); + // Mask thumbnail. + let right_w = 46.0; + let mask_rect = Rect::from_min_size(pos2(rect.right() - right_w - 30.0, rect.top() + 7.0), vec2(24.0, 24.0)); + if let Some(m) = &mask { + let mask_sel = selected && !targets_content(app); + p.rect_filled( + mask_rect, + 1.0, + if m.background == texturecraft_engine::core::MaskBackground::White { Color32::from_gray(225) } else { Color32::from_gray(18) }, + ); + p.rect_stroke( + mask_rect, + 1.0, + Stroke::new(if mask_sel { 1.5 } else { 1.0 }, if mask_sel { TOKENS.accent } else { Color32::from_gray(80) }), + egui::StrokeKind::Outside, + ); + if !m.effects.is_empty() { + p.text(mask_rect.center(), egui::Align2::CENTER_CENTER, ic::MAGIC_WAND, egui::FontId::proportional(11.0), Color32::from_rgb(0x80, 0x80, 0x80)); + } + if !m.enabled { + p.line_segment([mask_rect.left_top(), mask_rect.right_bottom()], Stroke::new(1.5, TOKENS.danger)); + } + let mr = + ui.interact(mask_rect, ui.id().with(("mask", layer.id.0)), Sense::click()).on_hover_text("Select the mask (Alt+click: show it in the viewport)"); + if mr.clicked() { + app.run("layer.select", json!({"layer": layer.id.0, "target": "mask"})); + if ui.input(|i| i.modifiers.alt) { + let showing = app.session.view == texturecraft_engine::ViewMode::Mask(layer.id); + let p = if showing { json!({"mode": "material"}) } else { json!({"mode": "mask", "layer": layer.id.0}) }; + app.run("viewport.view", p); + } + } + if app.session.view == texturecraft_engine::ViewMode::Mask(layer.id) { + p.rect_stroke(mask_rect.expand(2.0), 1.0, Stroke::new(1.5, Color32::from_rgb(0xe0, 0xb0, 0x40)), egui::StrokeKind::Outside); + } + } else { + p.rect_stroke(mask_rect, 1.0, Stroke::new(1.0, Color32::from_gray(52)), egui::StrokeKind::Inside); + } + // Name (or rename editor). + let name_rect = Rect::from_min_max(pos2(x, rect.top() + 4.0), pos2(mask_rect.left() - 4.0, rect.bottom() - 4.0)); + let renaming = app.ui.rename.as_ref().is_some_and(|(r, _)| *r == layer.id.0); + if renaming { + let mut child = ui.new_child(egui::UiBuilder::new().max_rect(name_rect).layout(egui::Layout::left_to_right(egui::Align::Center))); + if let Some((_, buf)) = app.ui.rename.as_mut() { + let r = child.add(egui::TextEdit::singleline(buf).desired_width(name_rect.width())); + r.request_focus(); + if r.lost_focus() { + let name = buf.clone(); + app.ui.rename = None; + app.run("layer.rename", json!({"layer": layer.id.0, "name": name})); + } + } + } else { + let galley = elided(&p, layer.name.clone(), 12.0, if layer.visible { TOKENS.text_strong } else { TOKENS.text_dim }, name_rect.width()); + let y = if source.is_some() { rect.center().y - 7.0 } else { rect.center().y }; + p.galley(pos2(name_rect.left(), y - galley.size().y * 0.5), galley, TOKENS.text); + if let Some(src) = source { + let src = elided(&p, format!("{} {src}", ic::LINK_SIMPLE), 10.0, TOKENS.text_dim, name_rect.width()); + p.galley(pos2(name_rect.left(), rect.center().y + 7.0 - src.size().y * 0.5), src, TOKENS.text_dim); + } + } + // Blend mode and opacity of the displayed channel. + let ch = app.ui.layer_channel; + let blend_rect = Rect::from_min_size(pos2(rect.right() - right_w - 2.0, rect.top() + 3.0), vec2(right_w, 16.0)); + let op_rect = Rect::from_min_size(pos2(rect.right() - right_w - 2.0, rect.top() + 19.0), vec2(right_w, 16.0)); + if let Some(b) = blend { + let br = ui.interact(blend_rect, ui.id().with(("blend", layer.id.0)), Sense::click()).on_hover_text("Blending mode of the displayed channel"); + p.text(blend_rect.right_center() - vec2(10.0, 0.0), egui::Align2::RIGHT_CENTER, b.mode.short(), egui::FontId::proportional(11.0), TOKENS.text); + p.text(blend_rect.right_center(), egui::Align2::RIGHT_CENTER, ic::CARET_DOWN, egui::FontId::proportional(8.0), TOKENS.text_dim); + egui::Popup::menu(&br).show(|ui| { + ui.set_min_width(190.0); + for m in BlendMode::MENU { + match m { + Some(m) => { + if ui.selectable_label(m == b.mode, m.label()).clicked() { + app.run("layer.blend", json!({"layer": layer.id.0, "channel": ch.id(), "mode": format!("{m:?}")})); + ui.close(); + } + } + None => { + ui.separator(); + } + } + } + }); + let mut op = (b.opacity * 100.0).round(); + let mut child = ui.new_child(egui::UiBuilder::new().max_rect(op_rect).layout(egui::Layout::right_to_left(egui::Align::Center))); + let r = child.add(egui::DragValue::new(&mut op).range(0.0..=100.0).speed(0.5).max_decimals(0)); + if r.changed() { + app.run("layer.blend", json!({"layer": layer.id.0, "channel": ch.id(), "opacity": op / 100.0})); + } + } else { + p.text(blend_rect.right_center(), egui::Align2::RIGHT_CENTER, "off", egui::FontId::proportional(11.0), TOKENS.text_dim); + } + // Interaction: select, rename, drag & drop, context menu. + if resp.clicked() && !eye_resp.clicked() { + app.run("layer.select", json!({"layer": layer.id.0})); + } + if resp.double_clicked() { + app.ui.rename = Some((layer.id.0, layer.name.clone())); + } + if resp.drag_started() { + resp.dnd_set_drag_payload(layer.id.0); + } + if let Some(src) = resp.dnd_hover_payload::() + && *src != layer.id.0 + { + let inside = layer.is_group() && ui.input(|i| i.pointer.hover_pos()).is_some_and(|p| (p.y - rect.center().y).abs() < h * 0.25); + if inside { + p.rect_stroke(rect, 2.0, Stroke::new(2.0, TOKENS.accent), egui::StrokeKind::Inside); + } else { + p.line_segment([rect.left_top(), rect.right_top()], Stroke::new(2.0, TOKENS.accent)); + } + } + if let Some(src) = resp.dnd_release_payload::() + && *src != layer.id.0 + { + let inside = layer.is_group() && ui.input(|i| i.pointer.hover_pos()).is_some_and(|p| (p.y - rect.center().y).abs() < h * 0.25); + app.run("layer.move", json!({"layer": *src, "above": layer.id.0, "inside": inside})); + } + resp.context_menu(|ui| layer_context_menu(app, ui, layer)); +} + +/// One effect of a layer's content (`glyph` sphere) or mask (`glyph` wand) stack. +#[allow(clippy::too_many_arguments)] +fn effect_row(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer, eid: LayerId, name: &str, visible: bool, depth: usize, glyph: &str) { + let selected = app.session.selected_layer() == Some(layer.id) && app.session.target == StackTarget::Effect(eid); + let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 22.0), Sense::click()); + let p = ui.painter().clone(); + p.rect_filled( + rect.shrink2(vec2(0.0, 1.0)), + 2.0, + if selected { + TOKENS.row_selected + } else if resp.hovered() { + TOKENS.row_hover + } else { + TOKENS.panel + }, + ); + if selected { + p.rect_stroke(rect.shrink2(vec2(0.0, 1.0)), 2.0, Stroke::new(1.0, TOKENS.accent), egui::StrokeKind::Inside); + } + let x0 = rect.left() + 6.0 + depth as f32 * 14.0; + let eye = Rect::from_center_size(pos2(x0 + 8.0, rect.center().y), vec2(16.0, 16.0)); + let er = ui.interact(eye, ui.id().with(("eff_eye", eid.0)), Sense::click()).on_hover_text("Enable or disable the effect"); + p.text(eye.center(), egui::Align2::CENTER_CENTER, if visible { ic::EYE } else { ic::EYE_SLASH }, egui::FontId::proportional(12.0), TOKENS.text_dim); + p.text(pos2(x0 + 22.0, rect.center().y), egui::Align2::LEFT_CENTER, glyph, egui::FontId::proportional(11.0), TOKENS.text_dim); + p.text(pos2(x0 + 38.0, rect.center().y), egui::Align2::LEFT_CENTER, name, egui::FontId::proportional(11.5), TOKENS.text); + if er.clicked() { + app.run("effect.set", json!({"effect": eid.0, "visible": !visible})); + } else if resp.clicked() { + app.run("layer.select", json!({"layer": layer.id.0, "target": eid.0})); + } + resp.context_menu(|ui| { + if ui.button("Remove effect").clicked() { + app.run("effect.remove", json!({"effect": eid.0})); + ui.close(); + } + }); +} + +fn layer_context_menu(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer) { + ui.set_min_width(230.0); + let lid = layer.id.0; + let run = |app: &mut TextureCraftApp, ui: &mut Ui, label: &str, id: &str, p: Value, enabled: bool| { + if ui.add_enabled(enabled, egui::Button::new(label)).clicked() { + app.run("layer.select", json!({"layer": lid})); + app.run(id, p); + ui.close(); + } + }; + // An instance without a mask of its own edits its source's; adding one overrides it. + let project = &app.session.doc.project; + let has_mask = project.layers.get(&project.mask_owner(layer.id)).is_some_and(|l| l.mask.is_some()); + run(app, ui, "Add white mask", "mask.add.white", json!({"layer": lid}), layer.mask.is_none()); + run(app, ui, "Add black mask", "mask.add.black", json!({"layer": lid}), layer.mask.is_none()); + run(app, ui, "Add mask with color selection", "mask.add.colorSelection", json!({"layer": lid}), true); + ui.separator(); + run(app, ui, "Toggle mask", "mask.toggle", json!({"layer": lid}), has_mask); + run(app, ui, "Invert mask background", "mask.invertBackground", json!({"layer": lid}), has_mask); + run(app, ui, "Invert mask", "mask.invert", json!({"layer": lid}), has_mask); + run(app, ui, "Clear mask", "mask.clear", json!({"layer": lid}), has_mask); + run(app, ui, "Remove mask", "mask.remove", json!({"layer": lid}), has_mask); + ui.separator(); + run(app, ui, "Flatten layer Ctrl+M", "layer.flatten", json!({"layer": lid}), !layer.is_group()); + #[cfg(not(target_arch = "wasm32"))] + if ui.button("Create smart material").clicked() { + let file = crate::library_dir().join(format!("{}.tcmat", texturecraft_engine::formats::sanitize_resource_name(&layer.name))); + if let Some(r) = app.run("material.save", json!({"layer": lid, "path": file.display().to_string(), "name": layer.name})) { + let p = r.get("path").and_then(|p| p.as_str()).unwrap_or_default().to_string(); + app.toast(format!("Smart material saved to {p}"), false); + app.library = None; + } + ui.close(); + } + ui.separator(); + run(app, ui, "Cut layer(s) Ctrl+X", "layer.cut", json!({"layer": lid}), true); + run(app, ui, "Copy layer(s) Ctrl+C", "layer.copy", json!({"layer": lid}), true); + run(app, ui, "Paste layer(s) Ctrl+V", "layer.paste", json!({}), app.session.clipboard.is_some()); + let paste_instance = crate::menus::enabled(app, "layer.pasteInstance"); + run(app, ui, "Paste as instance", "layer.pasteInstance", json!({}), paste_instance); + run(app, ui, "Duplicate layer(s) Ctrl+D", "layer.duplicate", json!({"layer": lid}), true); + run(app, ui, "Instantiate across texture sets", "layer.instantiateAll", json!({"layer": lid}), app.session.set_count() > 1); + run(app, ui, "Copy to all texture sets", "layer.instantiateAll", json!({"layer": lid, "copy": true}), app.session.set_count() > 1); + run(app, ui, "Remove layer(s) Del", "layer.remove", json!({"layer": lid}), true); + run(app, ui, "Group layer(s) Ctrl+G", "layer.group", json!({"layer": lid}), true); + ui.separator(); + ui.horizontal(|ui| { + let (r, resp) = ui.allocate_exact_size(vec2(16.0, 16.0), Sense::click()); + ui.painter().rect_stroke(r, 2.0, Stroke::new(1.0, TOKENS.text_dim), egui::StrokeKind::Inside); + ui.painter().line_segment([r.left_bottom(), r.right_top()], Stroke::new(1.0, TOKENS.danger)); + if resp.clicked() { + app.run("layer.colorTag", json!({"layer": lid})); + } + for (i, c) in TAG_COLORS.iter().enumerate() { + let (r, resp) = ui.allocate_exact_size(vec2(16.0, 16.0), Sense::click()); + ui.painter().rect_filled(r, 2.0, *c); + if resp.clicked() { + app.run("layer.colorTag", json!({"layer": lid, "tag": i})); + } + } + }); + ui.separator(); + effect_menu(app, ui, layer); +} + +// --------------------------------------------------------------------------------------------- +// Texture set settings + +pub(crate) fn texture_set_settings(app: &mut TextureCraftApp, ui: &mut Ui) { + let ts = app.session.ts(); + let Some(set) = app.session.doc.project.texture_sets.get(ts).cloned() else { return }; + egui::ScrollArea::vertical().id_salt("tss").show(ui, |ui| { + ui.label(caption("General properties")); + egui::Grid::new("tss_grid").num_columns(2).spacing(vec2(8.0, 6.0)).show(ui, |ui| { + ui.label("Name"); + ui.label(RichText::new(&set.name).color(TOKENS.text_strong)); + ui.end_row(); + ui.label("Size"); + egui::ComboBox::from_id_salt("ts_size").selected_text(format!("{}", set.resolution)).show_ui(ui, |ui| { + for r in [256u32, 512, 1024, 2048, 4096, 8192] { + if ui.selectable_label(r == set.resolution, format!("{r}")).clicked() { + app.run("textureSet.resolution", json!({"ts": ts, "resolution": r})); + } + } + }); + ui.end_row(); + ui.label("Shader instance"); + ui.label("Main shader"); + ui.end_row(); + }); + ui.add_space(8.0); + ui.horizontal(|ui| { + ui.label(caption("Channels")); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + ui.menu_button("Add channel", |ui| { + for c in Channel::ALL { + if !set.has_channel(c) && ui.button(c.label()).clicked() { + app.run("textureSet.channel.add", json!({"ts": ts, "channel": c.id()})); + ui.close(); + } + } + }); + }); + }); + for (c, f) in &set.channels { + ui.horizontal(|ui| { + ui.label(RichText::new(c.label()).strong()); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if *c != Channel::BaseColor && icon_button(ui, ic::X, "Remove channel", false, 18.0).clicked() { + app.run("textureSet.channel.remove", json!({"ts": ts, "channel": c.id()})); + } + ui.label(RichText::new(f.label()).color(TOKENS.text_dim)); + }); + }); + } + ui.add_space(6.0); + let mut hs = set.height_strength; + if slider(ui, "Height to normal strength", &mut hs, 0.0..=10.0, 2).changed() { + app.run("textureSet.settings", json!({"ts": ts, "heightStrength": hs})); + } + ui.add_space(8.0); + ui.horizontal(|ui| { + ui.label(caption("Mesh maps")); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if ui.button("Bake Mesh Maps").clicked() { + app.ui.mode = crate::state::Mode::Baking; + } + }); + }); + let baked = app.session.doc.baked.get(&ts); + for m in BakedMap::ALL { + let present = baked.is_some_and(|b| b.maps.contains_key(&m)); + ui.horizontal(|ui| { + let (r, _) = ui.allocate_exact_size(vec2(26.0, 26.0), Sense::hover()); + ui.painter().rect_filled(r, 2.0, if present { mesh_map_color(m) } else { Color32::from_gray(40) }); + ui.vertical(|ui| { + ui.label(RichText::new(m.label()).strong()); + ui.label( + RichText::new(if present { format!("{}_{}", set.name, m.id()) } else { "Missing (bake to create)".into() }) + .size(10.5) + .color(TOKENS.text_dim), + ); + }); + }); + } + }); +} + +fn mesh_map_color(m: BakedMap) -> Color32 { + match m { + BakedMap::Normal => Color32::from_rgb(128, 128, 255), + BakedMap::WorldNormal => Color32::from_rgb(120, 200, 140), + BakedMap::Id => Color32::from_rgb(220, 60, 60), + BakedMap::AmbientOcclusion => Color32::from_gray(200), + BakedMap::Curvature => Color32::from_gray(128), + BakedMap::Position => Color32::from_rgb(120, 180, 90), + BakedMap::Thickness => Color32::from_gray(160), + BakedMap::Height => Color32::from_gray(128), + BakedMap::BentNormal => Color32::from_rgb(140, 170, 230), + BakedMap::Opacity => Color32::from_gray(230), + BakedMap::ColorMap => Color32::from_rgb(200, 150, 90), + BakedMap::WorldDirection => Color32::from_gray(180), + } +} + +// --------------------------------------------------------------------------------------------- +// Properties + +/// The Properties tab title: what the panel edits. +pub(crate) fn properties_title(app: &TextureCraftApp) -> String { + let p = &app.session.doc.project; + let what = match (app.session.selected_layer().and_then(|id| p.layers.get(&id)), app.session.target) { + (Some(_), StackTarget::Effect(e)) => p.effect(e).map(|(_, x)| x.name.clone()).unwrap_or_default(), + (Some(l), StackTarget::Content) if matches!(l.kind, LayerKind::Fill(_)) => "Fill".to_string(), + (Some(l), StackTarget::Content) if l.is_group() => "Folder".to_string(), + (Some(l), StackTarget::Content) if l.instance_source().is_some() => "Instance".to_string(), + _ => app.session.tool.tool.label().to_string(), + }; + format!("{} - {what}", crate::i18n::tr("Properties")) +} + +pub(crate) fn properties(app: &mut TextureCraftApp, ui: &mut Ui) { + let sel = app.session.selected_layer().and_then(|id| app.session.doc.project.layers.get(&id).cloned()); + let target = app.session.target; + egui::ScrollArea::vertical().id_salt("props").auto_shrink([false, false]).show(ui, |ui| match (&sel, target) { + (Some(l), StackTarget::Effect(e)) => effect_properties(app, ui, l, e), + (Some(l), StackTarget::Content) if matches!(l.kind, LayerKind::Fill(_)) => fill_properties(app, ui, l), + (Some(l), StackTarget::Content) if l.is_group() => { + ui.label(RichText::new("A folder blends its children (pass-through). Set per-channel opacity in the layer rows.").color(TOKENS.text_dim)); + } + (Some(l), StackTarget::Content) if l.instance_source().is_some() => instance_properties(app, ui, l), + (Some(l), StackTarget::Mask) => { + height_mask_section(app, ui, l); + ui.separator(); + tool_properties(app, ui); + } + _ => tool_properties(app, ui), + }); +} + +/// An instance draws its source: point there for edits. +fn instance_properties(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer) { + let Some(src) = layer.instance_source() else { return }; + let p = &app.session.doc.project; + let name = p.layers.get(&src).map_or_else(|| "a missing layer".to_string(), |l| format!("\"{}\"", l.name)); + let set = p.locate(src).and_then(|(ts, _, _)| p.texture_sets.get(ts)).map(|t| t.name.clone()).unwrap_or_default(); + ui.label(caption("Instance")); + ui.label(RichText::new(format!("{} Draws {name} of texture set \"{set}\". Edits to the source show in every instance.", ic::LINK)).color(TOKENS.text_dim)); + if ui.button("Select source").clicked() { + app.run("layer.select", json!({"layer": src.0})); + } +} + +/// Mask with height: the layer's height shapes its mask transitions. +fn height_mask_section(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer) { + let id = layer.id.0; + let hm = app.session.doc.project.content_layer(layer.id).map(|l| l.height_mask).unwrap_or_default(); + section(ui, "sec_height_mask", "Mask with height", true, |ui| { + let mut on = hm.enabled; + if ui + .checkbox(&mut on, "Combine with the layer's height") + .on_hover_text("Partial mask values follow the layer's height: high parts show first") + .changed() + { + app.run("layer.set", json!({"layer": id, "heightMask": {"enabled": on}})); + } + ui.add_enabled_ui(hm.enabled, |ui| { + let (mut contrast, mut offset) = (hm.contrast, hm.offset); + if slider(ui, "Contrast", &mut contrast, 0.0..=16.0, 2).drag_stopped() { + app.run("layer.set", json!({"layer": id, "heightMask": {"contrast": contrast}})); + } + if slider(ui, "Height offset", &mut offset, -1.0..=1.0, 2).drag_stopped() { + app.run("layer.set", json!({"layer": id, "heightMask": {"offset": offset}})); + } + }); + }); +} + +fn tool_properties(app: &mut TextureCraftApp, ui: &mut Ui) { + // Brush preview: a stroke and a sphere with the tool's material. + let (rect, _) = ui.allocate_exact_size(vec2(ui.available_width(), 88.0), Sense::hover()); + let p = ui.painter(); + p.rect_filled(rect, 2.0, Color32::from_rgb(0x1e, 0x1e, 0x1e)); + let half = rect.width() * 0.5; + let b = &app.session.tool.brush; + let mut pts = Vec::new(); + for k in 0..=40 { + let t = k as f32 / 40.0; + pts.push(pos2(rect.left() + 14.0 + t * (half - 28.0), rect.center().y + (t * 9.0).sin() * 18.0)); + } + let w = (b.size / 0.25 * 60.0).clamp(2.0, 30.0); + let a = (b.flow * b.opacity * 255.0) as u8; + p.add(egui::Shape::line(pts, Stroke::new(w, Color32::from_white_alpha(a.max(30))))); + let base = app.session.tool.values.get(&Channel::BaseColor).copied().unwrap_or([0.8; 3]); + crate::thumbs::paint_sphere(p, pos2(rect.left() + half * 1.5, rect.center().y), 36.0, base); + ui.add_space(4.0); + ui.horizontal(|ui| { + for (tab, icon, tip) in + [(PropsTab::Brush, ic::PAINT_BRUSH, "Brush"), (PropsTab::Alpha, ic::CIRCLE_HALF, "Alpha"), (PropsTab::Material, ic::SPHERE, "Material")] + { + if icon_button(ui, icon, tip, app.ui.props_tab == tab, 24.0).clicked() { + app.ui.props_tab = tab; + } + } + }); + ui.separator(); + if app.session.quick_mask.editing { + ui.label(RichText::new("Editing the quick mask: strokes paint where painting is allowed. Press U to finish.").color(TOKENS.text_dim)); + ui.separator(); + } + let tool_section: Option = match app.session.tool.tool { + Tool::Projection => Some(stencil_section), + Tool::Clone => Some(clone_section), + Tool::Smudge => Some(smudge_section), + Tool::Path => Some(path_section), + _ => None, + }; + if let Some(section) = tool_section { + section(app, ui); + ui.separator(); + } + match app.ui.props_tab { + PropsTab::Brush => brush_section(app, ui), + PropsTab::Alpha => alpha_section(app, ui), + PropsTab::Material => material_section(app, ui), + } +} + +fn stencil_section(app: &mut TextureCraftApp, ui: &mut Ui) { + ui.label(caption("Stencil")); + let names: Vec = app.session.doc.resources.keys().cloned().collect(); + let current = app.session.tool.stencil.as_ref().map(|s| s.image.clone()).unwrap_or_else(|| "None".into()); + ui.horizontal(|ui| { + egui::ComboBox::from_id_salt("stencil_img").width(170.0).selected_text(current.clone()).show_ui(ui, |ui| { + if ui.selectable_label(app.session.tool.stencil.is_none(), "None").clicked() { + app.run("tool.stencil", json!({"clear": true})); + } + for n in &names { + if ui.selectable_label(*n == current, n).clicked() { + app.run("tool.stencil", json!({"image": n})); + } + } + }); + import_button(app, ui); + }); + if names.is_empty() { + ui.label(RichText::new("Import an image (File > Import resources) to project it onto the mesh.").color(TOKENS.text_dim)); + return; + } + if let Some(st) = app.session.tool.stencil.as_mut() { + slider(ui, "Scale", &mut st.scale, 0.05..=4.0, 2); + slider(ui, "Rotation", &mut st.rotation, 0.0..=360.0, 0); + slider(ui, "Overlay opacity", &mut st.opacity, 0.0..=1.0, 2); + ui.checkbox(&mut st.tile, "Tile"); + ui.label(RichText::new("Hold S: left-drag rotates, right-drag scales, middle-drag moves the stencil.").size(10.5).color(TOKENS.text_dim)); + } +} + +fn import_button(app: &mut TextureCraftApp, ui: &mut Ui) { + if ui.small_button("Import...").clicked() { + let ctx = ui.ctx().clone(); + crate::menus::invoke(app, &ctx, "resource.import"); + } +} + +fn clone_section(app: &mut TextureCraftApp, ui: &mut Ui) { + ui.label(caption("Clone")); + let Some(src) = app.session.tool.clone_source else { + ui.label(RichText::new("Hold V and click the mesh to set the source, then paint elsewhere to copy it.").color(TOKENS.text_dim)); + return; + }; + ui.horizontal(|ui| { + ui.label(format!("Source U {:.3} V {:.3}", src.uv[0], src.uv[1])); + if ui.small_button("Clear").clicked() { + app.run("tool.cloneSource", json!({"clear": true})); + } + }); + let hint = if src.offset.is_some() { + "Aligned: the offset set by the first stroke is kept. V + click sets a new source." + } else { + "The first stroke sets the offset from the source." + }; + ui.label(RichText::new(hint).size(10.5).color(TOKENS.text_dim)); +} + +fn smudge_section(app: &mut TextureCraftApp, ui: &mut Ui) { + ui.label(caption("Smudge")); + let mut v = app.session.tool.smudge_strength * 100.0; + if slider(ui, "Strength", &mut v, 0.0..=100.0, 0).changed() { + app.session.tool.smudge_strength = (v / 100.0).clamp(0.0, 1.0); + } + ui.label(RichText::new("Drags the layer stack's values along the stroke into the selected layer.").size(10.5).color(TOKENS.text_dim)); +} + +fn path_section(app: &mut TextureCraftApp, ui: &mut Ui) { + ui.label(caption("Path")); + ui.horizontal(|ui| crate::toolbar::path_controls(app, ui)); + let n = app.session.tool.path.points.len(); + let hint = format!( + "{n} control points. Click the mesh (or the 2D view) to add points and drag them to adjust; click the first point to close the path. Enter paints it with the brush (a closed, filled path fills its inside), Esc cancels." + ); + ui.label(RichText::new(hint).size(10.5).color(TOKENS.text_dim)); +} + +fn brush_section(app: &mut TextureCraftApp, ui: &mut Ui) { + let b = &mut app.session.tool.brush; + ui.label(caption("Brush")); + section(ui, "sec_size", "Size", true, |ui| { + let mut size = b.size / 0.25 * 100.0; + if slider(ui, "Size", &mut size, 0.05..=100.0, 1).changed() { + b.size = (size / 100.0 * 0.25).clamp(0.0005, 0.25); + } + let mut ms = b.min_size * 100.0; + if slider(ui, "Minimum size (%)", &mut ms, 0.0..=100.0, 0).changed() { + b.min_size = ms / 100.0; + } + ui.checkbox(&mut b.screen_space, "Size in screen space"); + }); + section(ui, "sec_flow", "Flow and opacity", true, |ui| { + let mut f = b.flow * 100.0; + if slider(ui, "Flow", &mut f, 0.0..=100.0, 0).changed() { + b.flow = f / 100.0; + } + let mut o = b.opacity * 100.0; + if slider(ui, "Stroke opacity", &mut o, 0.0..=100.0, 0).changed() { + b.opacity = o / 100.0; + } + let mut s = b.spacing * 100.0; + if slider(ui, "Spacing", &mut s, 1.0..=1000.0, 0).changed() { + b.spacing = s / 100.0; + } + let mut h = b.hardness * 100.0; + if slider(ui, "Hardness", &mut h, 0.0..=99.9, 0).changed() { + b.hardness = h / 100.0; + } + }); + section(ui, "sec_angle", "Angle", false, |ui| { + slider(ui, "Angle", &mut b.angle, -180.0..=180.0, 0); + ui.checkbox(&mut b.follow_path, "Follow path"); + }); + section(ui, "sec_jitter", "Jitters", false, |ui| { + for (label, v) in [("Size jitter", &mut b.jitter_size), ("Angle jitter", &mut b.jitter_angle), ("Flow jitter", &mut b.jitter_flow)] { + let mut x = *v * 100.0; + if slider(ui, label, &mut x, 0.0..=100.0, 0).changed() { + *v = x / 100.0; + } + } + let mut pj = b.jitter_position * 100.0; + if slider(ui, "Position jitter", &mut pj, 0.0..=400.0, 0).changed() { + b.jitter_position = pj / 100.0; + } + }); + section(ui, "sec_align", "Alignment and culling", false, |ui| { + egui::ComboBox::from_label("Alignment").selected_text(format!("{:?}", b.alignment)).show_ui(ui, |ui| { + ui.selectable_value(&mut b.alignment, texturecraft_engine::paint::Alignment::Tangent, "Tangent | Wrap"); + ui.selectable_value(&mut b.alignment, texturecraft_engine::paint::Alignment::Camera, "Camera"); + }); + ui.checkbox(&mut b.backface_culling, "Backface culling"); + slider(ui, "Angle fade from", &mut b.angle_fade.0, 0.0..=90.0, 0); + slider(ui, "Angle fade to", &mut b.angle_fade.1, 0.0..=90.0, 0); + slider(ui, "Stamp depth", &mut b.depth, 0.05..=4.0, 2); + }); + section(ui, "sec_particles", "Particles", false, |ui| { + let pt = &mut b.particles; + ui.checkbox(&mut pt.enabled, "Particle brush (slides down the surface)"); + let mut count = pt.count as f32; + if slider(ui, "Count per stamp", &mut count, 1.0..=16.0, 0).changed() { + pt.count = count as u32; + } + let mut life = pt.lifetime as f32; + if slider(ui, "Lifetime (steps)", &mut life, 1.0..=128.0, 0).changed() { + pt.lifetime = life as u32; + } + slider(ui, "Gravity", &mut pt.gravity, 0.0..=2.0, 2); + }); + section(ui, "sec_lazy", "Lazy mouse and symmetry", false, |ui| { + slider(ui, "Lazy mouse distance", &mut b.lazy, 0.0..=200.0, 0); + ui.horizontal(|ui| { + ui.checkbox(&mut b.symmetry.x, "Mirror X"); + ui.checkbox(&mut b.symmetry.y, "Mirror Y"); + ui.checkbox(&mut b.symmetry.z, "Mirror Z"); + }); + }); + *b = b.sanitized(); +} + +/// One alpha in the picker: a 52×64 tile with the shape drawn by `paint` and a label. +fn alpha_tile(ui: &mut Ui, on: bool, label: &str, paint: impl FnOnce(&egui::Painter, Rect)) -> egui::Response { + let (rect, resp) = ui.allocate_exact_size(vec2(52.0, 64.0), Sense::click()); + let p = ui.painter(); + p.rect_filled(rect, 3.0, if on { Color32::from_rgb(0x34, 0x3c, 0x48) } else { Color32::from_rgb(0x1e, 0x1e, 0x1e) }); + if on { + p.rect_stroke(rect, 3.0, Stroke::new(1.0, TOKENS.accent), egui::StrokeKind::Inside); + } + paint(p, Rect::from_center_size(rect.center() - vec2(0.0, 7.0), vec2(40.0, 40.0))); + p.text(rect.center_bottom() - vec2(0.0, 6.0), egui::Align2::CENTER_CENTER, label, egui::FontId::proportional(9.5), TOKENS.text); + resp.on_hover_text(label) +} + +fn alpha_section(app: &mut TextureCraftApp, ui: &mut Ui) { + ui.horizontal(|ui| { + ui.label(caption("Alpha")); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| import_button(app, ui)); + }); + let brush = &app.session.tool.brush; + let (current, image, hardness) = (brush.alpha, brush.alpha_image.clone(), brush.hardness); + let mut pick = None; + ui.horizontal_wrapped(|ui| { + for a in BrushAlpha::ALL { + if alpha_tile(ui, image.is_none() && current == a, a.label(), |p, r| crate::thumbs::paint_alpha(p, r, a, hardness)).clicked() { + pick = Some(json!(a.label())); + } + } + // Imported images, sampled as grayscale. + for name in app.session.doc.resources.keys() { + let Some(img) = app.session.images.get(name) else { continue }; + let tex = app.thumbs.image(ui.ctx(), name, img); + let on = image.as_deref() == Some(name.as_str()); + if alpha_tile(ui, on, name, |p, r| { + p.image(tex.id(), r, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE); + }) + .clicked() + { + pick = Some(json!({ "image": name })); + } + } + }); + if let Some(alpha) = pick { + app.run("tool.brush", json!({ "alpha": alpha })); + } +} + +fn material_section(app: &mut TextureCraftApp, ui: &mut Ui) { + ui.label(caption("Material")); + let chans = app.session.doc.project.texture_set(app.session.ts()).map(|t| t.channel_list()).unwrap_or_default(); + ui.horizontal_wrapped(|ui| { + for c in &chans { + let on = app.session.tool.enabled.contains(c); + if chip(ui, c.short(), on).clicked() { + app.run("tool.material", json!({"channel": c.id(), "enabled": !on})); + } + } + }); + ui.add_space(4.0); + let mask_mode = !targets_content(app); + if mask_mode { + let mut v = app.session.tool.mask_value; + if slider(ui, "Mask value (gray)", &mut v, 0.0..=1.0, 2).changed() { + app.session.tool.mask_value = v; + } + return; + } + for c in chans { + if !app.session.tool.enabled.contains(&c) { + continue; + } + let mut v = app.session.tool.values.get(&c).copied().unwrap_or(c.default_value()); + match c.kind() { + texturecraft_engine::core::ChannelKind::Color => { + ui.horizontal(|ui| { + ui.label(c.label()); + if color_swatch(ui, &format!("tool_{}", c.id()), &mut v, ui.available_width() - 4.0) { + app.session.tool.values.insert(c, v); + } + }); + } + texturecraft_engine::core::ChannelKind::NormalMap => { + ui.label(RichText::new("Normal: painting uses the flat normal").color(TOKENS.text_dim)); + } + texturecraft_engine::core::ChannelKind::Signed => { + if slider(ui, c.label(), &mut v[0], -1.0..=1.0, 2).changed() { + app.session.tool.values.insert(c, [v[0]; 3]); + } + } + texturecraft_engine::core::ChannelKind::Scalar => { + if slider(ui, c.label(), &mut v[0], 0.0..=1.0, 2).changed() { + app.session.tool.values.insert(c, [v[0]; 3]); + } + } + } + } + if app.session.tool.tool == Tool::Eraser { + ui.label(RichText::new("The eraser removes paint from the enabled channels.").color(TOKENS.text_dim)); + } +} + +fn fill_properties(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer) { + let LayerKind::Fill(fill) = &layer.kind else { return }; + let on: Vec = layer.channels.keys().copied().collect(); + ui.label(caption("Fill")); + fill_editor(app, ui, fill, &json!({"layer": layer.id.0}), &on, layer.id); +} + +/// `base` with the fields of `extra` added. +fn with(base: &Value, extra: Value) -> Value { + let mut v = base.clone(); + if let (Some(o), Value::Object(e)) = (v.as_object_mut(), extra) { + o.extend(e); + } + v +} + +/// Anchor points below layer `reader` in its texture set: (effect id, "layer / anchor" label). +fn anchors_below(app: &TextureCraftApp, reader: LayerId) -> Vec<(LayerId, String)> { + let p = &app.session.doc.project; + let Some((ts, _, _)) = p.locate(reader) else { return Vec::new() }; + p.walk(ts) + .into_iter() + .map(|(id, _)| id) + .take_while(|id| *id != reader) + .filter_map(|id| p.layers.get(&id)) + .flat_map(|l| l.content_effects.iter().filter(|e| matches!(e.kind, EffectKind::Anchor)).map(move |e| (e.id, format!("{} / {}", l.name, e.name)))) + .collect() +} + +fn anchor_label(app: &TextureCraftApp, anchor: LayerId, channel: Channel) -> String { + let name = app.session.doc.project.effect(anchor).map(|(_, e)| e.name.clone()).unwrap_or_else(|| "missing".into()); + format!("Anchor: {name} ({})", channel.label()) +} + +/// Projection and per-channel sources of a fill: a fill layer's (`base` = {"layer"}, `on` = +/// its enabled channels) or a content fill effect's (`base` = {"effect"}, `on` = the channels +/// it has a source for). `reader` is the layer owning the fill (for anchor sources). +fn fill_editor(app: &mut TextureCraftApp, ui: &mut Ui, fill: &Fill, base: &Value, on: &[Channel], reader: LayerId) { + use texturecraft_engine::core::{ChannelKind, Projection}; + let salt = base.to_string(); + let proj_label = fill.projection.label(); + egui::ComboBox::from_label("Projection").selected_text(proj_label).show_ui(ui, |ui| { + for (id, label) in [ + ("uv", "UV projection"), + ("triplanar", "Tri-planar projection"), + ("planar", "Planar projection"), + ("spherical", "Spherical projection"), + ("cylindrical", "Cylindrical projection"), + ] { + if ui.selectable_label(proj_label == label, label).clicked() { + app.run("layer.fill.projection", with(base, json!({"projection": id}))); + } + } + }); + let (mut scale, proj_id) = match &fill.projection { + Projection::Uv { scale, .. } => (scale[0], "uv"), + Projection::Triplanar { scale, .. } => (*scale, "triplanar"), + Projection::Planar { scale, .. } => (*scale, "planar"), + Projection::Spherical { scale } => (*scale, "spherical"), + Projection::Cylindrical { scale, .. } => (*scale, "cylindrical"), + }; + if slider(ui, "Scale / tiling", &mut scale, 0.01..=20.0, 2).drag_stopped() { + app.run("layer.fill.projection", with(base, json!({"projection": proj_id, "scale": scale}))); + } + ui.add_space(6.0); + ui.label(caption("Material")); + let chans = app.session.doc.project.texture_set(app.session.ts()).map(|t| t.channel_list()).unwrap_or_default(); + ui.horizontal_wrapped(|ui| { + for c in &chans { + let enabled = on.contains(c); + if chip(ui, c.short(), enabled).clicked() { + app.run("layer.fill.set", with(base, json!({"channel": c.id(), "enabled": !enabled}))); + } + } + }); + ui.add_space(4.0); + let anchors = anchors_below(app, reader); + for c in chans { + if !on.contains(&c) { + continue; + } + let set = |extra: Value| with(base, with(&json!({"channel": c.id()}), extra)); + ui.group(|ui| { + ui.horizontal(|ui| { + ui.label(RichText::new(c.label()).strong()); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + ui.menu_button(RichText::new(ic::DOTS_THREE).size(13.0), |ui| { + if ui.button("Uniform value").clicked() { + app.run("layer.fill.set", set(json!({"value": c.default_value()}))); + ui.close(); + } + ui.separator(); + for k in ProceduralKind::ALL { + if ui.button(k.label()).clicked() { + let (lo, hi) = match c { + Channel::BaseColor => ([0.15, 0.15, 0.15], [0.85, 0.85, 0.85]), + Channel::Height => ([-0.05; 3], [0.05; 3]), + _ => ([0.2; 3], [0.8; 3]), + }; + app.run("layer.fill.set", set(json!({"procedural": k.label(), "low": lo, "high": hi}))); + ui.close(); + } + } + if !anchors.is_empty() { + ui.separator(); + } + for (a, label) in &anchors { + if ui.button(format!("Anchor: {label}")).clicked() { + app.run("layer.fill.set", set(json!({"anchor": a.0}))); + ui.close(); + } + } + }) + .response + .on_hover_text("Channel source: uniform value, procedural or anchor"); + }); + }); + match fill.source(c) { + Source::Uniform(mut v) => { + if c.kind() == ChannelKind::Color { + if color_swatch(ui, &format!("fill_{salt}_{}", c.id()), &mut v, ui.available_width() - 4.0) { + app.run("layer.fill.set", set(json!({"color": v}))); + } + } else { + let range = if c.kind() == ChannelKind::Signed { -1.0..=1.0 } else { 0.0..=1.0 }; + let r = slider(ui, "Value", &mut v[0], range, 2); + if r.changed() { + app.run("layer.fill.set", set(json!({"value": v[0]}))); + } + } + } + Source::Procedural { pattern, low, high } => { + ui.label(RichText::new(format!("{} (scale {:.1}, seed {})", pattern.kind.label(), pattern.scale, pattern.seed)).color(TOKENS.text_dim)); + let mut s = pattern.scale; + let mut con = pattern.contrast; + let r1 = slider(ui, "Pattern scale", &mut s, 0.1..=100.0, 1); + let r2 = slider(ui, "Contrast", &mut con, 0.0..=8.0, 2); + let mut seed = pattern.seed as f32; + let r3 = slider(ui, "Seed", &mut seed, 0.0..=999.0, 0); + if r1.drag_stopped() || r2.drag_stopped() || r3.drag_stopped() { + app.run("layer.fill.set", set(json!({"procedural": pattern.kind.label(), "scale": s, "contrast": con, "seed": seed as u64, "low": low, "high": high}))); + } + if c.kind() == ChannelKind::Color { + let (mut lo, mut hi) = (low, high); + let mut changed = false; + ui.horizontal(|ui| { + ui.label("Low"); + changed |= color_swatch(ui, &format!("lo_{salt}_{}", c.id()), &mut lo, 60.0); + ui.label("High"); + changed |= color_swatch(ui, &format!("hi_{salt}_{}", c.id()), &mut hi, 60.0); + }); + if changed { + app.run("layer.fill.set", set(json!({"procedural": pattern.kind.label(), "scale": s, "contrast": con, "seed": pattern.seed, "low": lo, "high": hi}))); + } + } + } + Source::Image { name } => { + ui.label(format!("Image: {name}")); + } + Source::Anchor { anchor, channel } => { + ui.label(RichText::new(anchor_label(app, anchor, channel)).color(TOKENS.text_dim)); + } + } + }); + } +} + +fn effect_properties(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer, eid: LayerId) { + let Some((_, e)) = app.session.doc.project.effect(eid).map(|(l, e)| (l, e.clone())) else { return }; + let id = eid.0; + ui.horizontal(|ui| { + ui.label("Blending"); + egui::ComboBox::from_id_salt("eff_blend").selected_text(e.blend.label()).show_ui(ui, |ui| { + for m in BlendMode::all() { + if ui.selectable_label(m == e.blend, m.label()).clicked() { + app.run("effect.set", json!({"effect": id, "blend": format!("{m:?}")})); + } + } + }); + }); + let mut op = e.opacity; + if slider(ui, "Opacity", &mut op, 0.0..=1.0, 2).drag_stopped() { + app.run("effect.set", json!({"effect": id, "opacity": op})); + } + ui.separator(); + match &e.kind { + EffectKind::Generator(g) => { + ui.label(caption("Generator")); + egui::ComboBox::from_id_salt("gen_kind").selected_text(g.kind.label()).show_ui(ui, |ui| { + for k in GeneratorKind::ALL { + if ui.selectable_label(k == g.kind, k.label()).clicked() { + // Replace the generator kind: remove and add keeps ordering simple. + app.run("effect.remove", json!({"effect": id})); + app.run("effect.add.generator", json!({"generator": format!("{k:?}")})); + } + } + }); + let (mut level, mut soft, mut noise, mut ns) = (g.level, g.softness, g.noise_amount, g.noise_scale); + let r1 = slider(ui, "Level", &mut level, 0.0..=1.0, 2); + let r2 = slider(ui, "Softness", &mut soft, 0.0..=1.0, 2); + let r3 = slider(ui, "Noise amount", &mut noise, 0.0..=2.0, 2); + let r4 = slider(ui, "Noise scale", &mut ns, 0.1..=64.0, 1); + let mut inv = g.invert; + let r5 = ui.checkbox(&mut inv, "Invert"); + if r1.drag_stopped() || r2.drag_stopped() || r3.drag_stopped() || r4.drag_stopped() || r5.changed() { + app.run("effect.set", json!({"effect": id, "level": level, "softness": soft, "noise": noise, "noiseScale": ns, "invert": inv})); + } + if !app.session.doc.baked.contains_key(&app.session.ts()) { + ui.label( + RichText::new(format!("{} Bake mesh maps (F8) for curvature, AO and thickness based generators.", ic::WARNING)) + .color(Color32::from_rgb(0xe0, 0xb0, 0x40)), + ); + } + } + EffectKind::Levels(l) => { + ui.label(caption("Levels")); + let mut v = [l.in_low, l.in_high, l.gamma, l.out_low, l.out_high]; + let labels = ["Input low", "Input high", "Gamma", "Output low", "Output high"]; + let mut done = false; + for (i, label) in labels.iter().enumerate() { + let range = if i == 2 { 0.1..=5.0 } else { 0.0..=1.0 }; + done |= slider(ui, label, &mut v[i], range, 2).drag_stopped(); + } + if done { + app.run("effect.set", json!({"effect": id, "inLow": v[0], "inHigh": v[1], "gamma": v[2], "outLow": v[3], "outHigh": v[4]})); + } + } + EffectKind::Fill { source, .. } => { + ui.label(caption("Fill")); + let anchors = anchors_below(app, layer.id); + let current = match source { + Source::Anchor { anchor, channel } => anchor_label(app, *anchor, *channel), + Source::Uniform(_) => "Uniform value".to_string(), + Source::Procedural { pattern, .. } => pattern.kind.label().to_string(), + Source::Image { name } => format!("Image: {name}"), + }; + egui::ComboBox::from_label("Source").selected_text(current).show_ui(ui, |ui| { + if ui.selectable_label(matches!(source, Source::Uniform(_)), "Uniform value").clicked() { + app.run("effect.set", json!({"effect": id, "value": 1.0})); + } + for (a, label) in &anchors { + if ui.selectable_label(source.anchor() == Some(*a), format!("Anchor: {label}")).clicked() { + app.run("effect.set", json!({"effect": id, "anchor": a.0})); + } + } + }); + match source { + Source::Uniform(v) => { + let mut x = v[0]; + if slider(ui, "Value", &mut x, 0.0..=1.0, 2).drag_stopped() { + app.run("effect.set", json!({"effect": id, "value": x})); + } + } + Source::Anchor { channel, .. } => { + let chans = app.session.doc.project.texture_set(app.session.ts()).map(|t| t.channel_list()).unwrap_or_default(); + egui::ComboBox::from_label("Channel").selected_text(channel.label()).show_ui(ui, |ui| { + for c in chans { + if ui.selectable_label(c == *channel, c.label()).clicked() { + app.run("effect.set", json!({"effect": id, "anchorChannel": c.id()})); + } + } + }); + ui.label(RichText::new("Colors read as gray. Add a levels or blur effect above to shape the result.").color(TOKENS.text_dim)); + } + _ => {} + } + if anchors.is_empty() { + ui.label(RichText::new("Add an anchor point to a layer below to use its stack as a mask.").color(TOKENS.text_dim)); + } + } + EffectKind::Material(f) => { + ui.label(caption("Fill")); + let on: Vec = f.sources.keys().copied().collect(); + fill_editor(app, ui, f, &json!({"effect": id}), &on, layer.id); + } + EffectKind::Anchor => { + ui.label(caption("Anchor point")); + ui.label( + RichText::new("Records the stack up to this layer. Fill effects in masks (and fill layers) above can read it as a source.") + .color(TOKENS.text_dim), + ); + } + EffectKind::Paint => { + let what = if layer.content_effects.iter().any(|x| x.id == eid) { "the layer content" } else { "this mask. Use the Material tab's mask value for gray" }; + ui.label(RichText::new(format!("Paint on the mesh to edit {what}.")).color(TOKENS.text_dim)); + tool_properties(app, ui); + } + EffectKind::Filter(Filter::Hsl { hue, saturation, lightness }) => { + ui.label(caption("HSL")); + let (mut h, mut s, mut l) = (hue * 360.0, *saturation, *lightness); + let r1 = slider(ui, "Hue shift (degrees)", &mut h, -180.0..=180.0, 0); + let r2 = slider(ui, "Saturation", &mut s, -1.0..=1.0, 2); + let r3 = slider(ui, "Lightness", &mut l, -1.0..=1.0, 2); + if r1.drag_stopped() || r2.drag_stopped() || r3.drag_stopped() { + app.run("effect.set", json!({"effect": id, "hue": h / 360.0, "saturation": s, "lightness": l})); + } + } + EffectKind::Filter(Filter::Blur { radius }) => { + ui.label(caption("Blur")); + let mut r = *radius; + if slider(ui, "Intensity (texels at 2K)", &mut r, 0.0..=128.0, 1).drag_stopped() { + app.run("effect.set", json!({"effect": id, "radius": r})); + } + } + EffectKind::Filter(Filter::Sharpen { amount }) => { + ui.label(caption("Sharpen")); + let mut a = *amount; + if slider(ui, "Amount", &mut a, 0.0..=5.0, 2).drag_stopped() { + app.run("effect.set", json!({"effect": id, "amount": a})); + } + } + EffectKind::Filter(Filter::Threshold { level }) => { + ui.label(caption("Threshold")); + let mut l = *level; + if slider(ui, "Level", &mut l, 0.0..=1.0, 2).drag_stopped() { + app.run("effect.set", json!({"effect": id, "level": l})); + } + } + other => { + ui.label(RichText::new(other.name()).color(TOKENS.text_dim)); + } + } +} + diff --git a/crates/ui-egui/src/docking.rs b/crates/ui-egui/src/docking.rs new file mode 100644 index 00000000..c3c033c2 --- /dev/null +++ b/crates/ui-egui/src/docking.rs @@ -0,0 +1,612 @@ +//! The dock host: every side panel (assets, texture set list, layers, texture set settings, +//! properties, display/shader settings, history, log) is a tab that can be docked, tabbed, +//! resized, floated and closed around the viewport (`egui_dock`). The panel bodies live in +//! `dock.rs`, `shelf.rs` and `windows.rs`; this module only hosts them. +//! +//! The layout persists with `UiState` as a small validated tree (`Layout`), not as the dock +//! crate's internal arrays, so a hand-edited or stale layout can never break the dock. + +use egui::{CornerRadius, Id, Margin, Rect, RichText, Stroke, Ui, WidgetText, pos2, vec2}; +use egui_dock::{DockArea, DockState, LeafNode, NodeIndex, Split, Style, TabIndex, TabStyle, TabViewer, Tree}; +use serde::{Deserialize, Serialize}; + +use crate::TextureCraftApp; +use crate::i18n::tr; +use crate::theme::TOKENS; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum Panel { + Viewport, + Assets, + TextureSetList, + Layers, + TextureSetSettings, + Properties, + Display, + Shader, + History, + Log, + /// A panel from a newer layout file; dropped on load. + #[serde(other)] + Unknown, +} + +/// Window ▸ Views items and the panels they show or hide. +pub const VIEWS: [(&str, Panel); 9] = [ + ("ui.view.assets", Panel::Assets), + ("ui.view.display", Panel::Display), + ("ui.view.history", Panel::History), + ("ui.view.layers", Panel::Layers), + ("ui.view.log", Panel::Log), + ("ui.view.properties", Panel::Properties), + ("ui.view.shader", Panel::Shader), + ("ui.view.textureSetList", Panel::TextureSetList), + ("ui.view.textureSetSettings", Panel::TextureSetSettings), +]; + +/// The panel a Window ▸ Views item toggles. +pub fn view_panel(id: &str) -> Option { + VIEWS.iter().find(|(v, _)| *v == id).map(|(_, p)| *p) +} + +/// Where a closed panel comes back. +enum Home { + /// Left of the viewport. + Left, + /// The right column, top (0) to bottom (2). + Column(u8), + /// A floating window, cascaded by index. + Float(u8), + Fixed, +} + +impl Panel { + const COUNT: usize = Panel::Unknown as usize + 1; + + pub fn name(self) -> &'static str { + match self { + Panel::Viewport => "Viewport", + Panel::Assets => "Assets", + Panel::TextureSetList => "Texture Set List", + Panel::Layers => "Layers", + Panel::TextureSetSettings => "Texture Set Settings", + Panel::Properties => "Properties", + Panel::Display => "Display Settings", + Panel::Shader => "Shader Settings", + Panel::History => "History", + Panel::Log => "Log", + Panel::Unknown => "", + } + } + + fn home(self) -> Home { + match self { + Panel::Assets => Home::Left, + Panel::TextureSetList => Home::Column(0), + Panel::Layers | Panel::TextureSetSettings => Home::Column(1), + Panel::Properties => Home::Column(2), + Panel::Display => Home::Float(0), + Panel::Shader => Home::Float(1), + Panel::History => Home::Float(2), + Panel::Log => Home::Float(3), + Panel::Viewport | Panel::Unknown => Home::Fixed, + } + } +} + +// --------------------------------------------------------------------------------------------- +// Persisted layout + +/// A dock tree: a group of tabs, or two subtrees side by side (`vertical`: one above the other). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Node { + Tabs { + tabs: Vec, + #[serde(default)] + active: usize, + }, + Split { + vertical: bool, + /// Share of the first (left or top) subtree, 0..1. + fraction: f32, + first: Box, + second: Box, + }, +} + +/// A floating window: screen rect `[x, y, width, height]` and its tree. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Floating { + #[serde(default)] + pub rect: Option<[f32; 4]>, + pub root: Node, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Layout { + pub main: Node, + #[serde(default)] + pub windows: Vec, +} + +fn tabs(tabs: &[Panel]) -> Node { + Node::Tabs { tabs: tabs.to_vec(), active: 0 } +} + +fn split(vertical: bool, fraction: f32, first: Node, second: Node) -> Node { + Node::Split { vertical, fraction, first: Box::new(first), second: Box::new(second) } +} + +impl Default for Layout { + /// Painter's default: assets left of the viewport; texture set list, layers (tabbed with the + /// texture set settings) and properties down the right. Fractions match the 1600x960 window. + fn default() -> Self { + use Panel::*; + let column = split(true, 0.125, tabs(&[TextureSetList]), split(true, 0.57, tabs(&[Layers, TextureSetSettings]), tabs(&[Properties]))); + Layout { main: split(false, 0.115, tabs(&[Assets]), split(false, 0.78, tabs(&[Viewport]), column)), windows: Vec::new() } + } +} + +/// Deeper trees are flattened into one tab group (the dock's node array doubles per level). +const MAX_DEPTH: usize = 8; + +impl Node { + fn collect(self, out: &mut Vec) { + match self { + Node::Tabs { tabs, .. } => out.extend(tabs), + Node::Split { first, second, .. } => { + first.collect(out); + second.collect(out); + } + } + } + + /// Drop unknown and repeated panels (and the viewport outside the main area), empty groups + /// and one-sided splits; clamp fractions; flatten too-deep trees. + fn sanitize(self, seen: &mut [bool; Panel::COUNT], depth: usize, main: bool) -> Option { + match self { + Node::Split { .. } if depth >= MAX_DEPTH => { + let mut all = Vec::new(); + self.collect(&mut all); + Node::Tabs { tabs: all, active: 0 }.sanitize(seen, depth, main) + } + Node::Tabs { tabs, active } => { + let tabs: Vec = tabs + .into_iter() + .filter(|p| *p != Panel::Unknown && (main || *p != Panel::Viewport) && !std::mem::replace(&mut seen[*p as usize], true)) + .collect(); + let last = tabs.len().checked_sub(1)?; + Some(Node::Tabs { tabs, active: active.min(last) }) + } + Node::Split { vertical, fraction, first, second } => { + let first = first.sanitize(seen, depth + 1, main); + let second = second.sanitize(seen, depth + 1, main); + match (first, second) { + (Some(a), Some(b)) => Some(split(vertical, if fraction.is_finite() { fraction.clamp(0.05, 0.95) } else { 0.5 }, a, b)), + (a, b) => a.or(b), + } + } + } + } + + /// Write this (sanitized) tree into `tree` at `at`, which must hold a leaf. + fn place(self, tree: &mut Tree, at: NodeIndex) { + match self { + Node::Tabs { tabs, active } => { + let mut leaf = LeafNode::new(tabs); + leaf.active = TabIndex(active); + tree[at] = egui_dock::Node::Leaf(leaf); + } + Node::Split { vertical, fraction, first, second } => { + // The placeholder tab is overwritten by `second`. + let [a, b] = tree.split(at, if vertical { Split::Below } else { Split::Right }, fraction, egui_dock::Node::leaf(Panel::Unknown)); + first.place(tree, a); + second.place(tree, b); + } + } + } + + fn read(nodes: &[&egui_dock::Node], i: usize) -> Option { + let (vertical, s) = match nodes.get(i)? { + egui_dock::Node::Empty => return None, + egui_dock::Node::Leaf(l) => return (!l.tabs.is_empty()).then(|| Node::Tabs { tabs: l.tabs.clone(), active: l.active.0 }), + egui_dock::Node::Vertical(s) => (true, s), + egui_dock::Node::Horizontal(s) => (false, s), + }; + let first = Node::read(nodes, i.saturating_mul(2).saturating_add(1)); + let second = Node::read(nodes, i.saturating_mul(2).saturating_add(2)); + match (first, second) { + (Some(a), Some(b)) => Some(split(vertical, if s.fraction.is_finite() { s.fraction } else { 0.5 }, a, b)), + (a, b) => a.or(b), + } + } + + fn of(tree: &Tree) -> Option { + Node::read(&tree.iter().collect::>(), 0) + } +} + +impl Layout { + /// The live dock for this layout, or `None` when it has no viewport. + fn build(self) -> Option> { + let mut seen = [false; Panel::COUNT]; + let main = self.main.sanitize(&mut seen, 0, true)?; + if !seen[Panel::Viewport as usize] { + return None; + } + let mut state = DockState::new(Vec::new()); + main.place(state.main_surface_mut(), NodeIndex::root()); + for w in self.windows { + let Some(root) = w.root.sanitize(&mut seen, 0, false) else { continue }; + let s = state.add_window(vec![Panel::Unknown]); + if let Some(tree) = state.get_surface_mut(s).and_then(|x| x.node_tree_mut()) { + root.place(tree, NodeIndex::root()); + } + if let (Some([x, y, width, height]), Some(ws)) = (w.rect, state.get_window_state_mut(s)) + && [x, y, width, height].iter().all(|v| v.is_finite()) + && width > 0.0 + && height > 0.0 + { + ws.set_position(pos2(x, y)).set_size(vec2(width, height)); + } + } + Some(state) + } + + fn of(state: &DockState) -> Layout { + let Some(main) = Node::of(state.main_surface()) else { return Layout::default() }; + let windows = state + .iter_surfaces() + .filter_map(|s| match s { + egui_dock::Surface::Window(tree, ws) => { + let r = ws.rect(); + let rect = (r.is_finite() && r.is_positive()).then(|| [r.min.x, r.min.y, r.width(), r.height()]); + Node::of(tree).map(|root| Floating { rect, root }) + } + _ => None, + }) + .collect(); + Layout { main, windows } + } +} + +// --------------------------------------------------------------------------------------------- +// The live dock + +/// The dock layout (part of `UiState`; serialized as a `Layout`). Empty only while the dock is +/// being drawn. +#[derive(Clone, Debug)] +pub struct Dock(Option>); + +impl Default for Dock { + fn default() -> Self { + Dock(Layout::default().build()) + } +} + +impl PartialEq for Dock { + fn eq(&self, other: &Self) -> bool { + self.layout() == other.layout() + } +} + +impl Serialize for Dock { + fn serialize(&self, s: S) -> Result { + self.layout().serialize(s) + } +} + +impl<'de> Deserialize<'de> for Dock { + fn deserialize>(d: D) -> Result { + Ok(Layout::deserialize(d)?.build().map_or_else(Dock::default, |s| Dock(Some(s)))) + } +} + +impl Dock { + pub fn layout(&self) -> Layout { + self.0.as_ref().map(Layout::of).unwrap_or_default() + } + + pub fn is_open(&self, p: Panel) -> bool { + self.0.as_ref().is_some_and(|s| s.find_tab(&p).is_some()) + } + + /// Open and the front tab of its group. + pub fn is_visible(&self, p: Panel) -> bool { + let Some(s) = &self.0 else { return false }; + s.find_tab(&p).is_some_and(|path| s.leaf(path.node_path()).is_ok_and(|l| l.active == path.tab)) + } + + /// Hide a visible panel, otherwise bring it up (`screen` places floating windows). + pub fn toggle(&mut self, p: Panel, screen: Rect) { + if self.is_visible(p) { self.close(p) } else { self.focus(p, screen) } + } + + pub fn close(&mut self, p: Panel) { + if p == Panel::Viewport { + return; + } + if let Some(s) = &mut self.0 + && let Some(path) = s.find_tab(&p) + { + s.remove_tab(path); + } + } + + /// Open `p` where it lives by default (if closed) and bring it to the front. + pub fn focus(&mut self, p: Panel, screen: Rect) { + let Some(s) = &mut self.0 else { return }; + if s.find_tab(&p).is_none() { + insert(s, p, screen); + } + if let Some(path) = s.find_tab(&p) { + let _ = s.set_active_tab(path); + s.set_focused_node_and_surface(path.node_path()); + } + } +} + +fn insert(state: &mut DockState, p: Panel, screen: Rect) { + match p.home() { + Home::Fixed => {} + Home::Float(k) => { + let s = state.add_window(vec![p]); + if let Some(w) = state.get_window_state_mut(s) { + w.set_position(screen.right_top() + vec2(-640.0, 90.0 + 50.0 * k as f32)).set_size(vec2(300.0, 380.0)); + } + } + Home::Left => beside_viewport(state.main_surface_mut(), Split::Left, 0.13, p), + Home::Column(order) => { + let tree = state.main_surface_mut(); + // The right-column group nearest to the panel's slot (its own slot first). + let mut best: Option<(NodeIndex, u8)> = None; + for (i, n) in tree.iter().enumerate() { + let slot = n.get_leaf().and_then(|l| { + l.tabs.iter().find_map(|t| match t.home() { + Home::Column(o) => Some(o), + _ => None, + }) + }); + if let Some(o) = slot + && best.is_none_or(|(_, b)| o.abs_diff(order) < b.abs_diff(order)) + { + best = Some((NodeIndex(i), o)); + } + } + match best { + Some((node, o)) if o == order => { + if let Ok(leaf) = tree.leaf_mut(node) { + leaf.append_tab(p); + } + } + Some((node, o)) if o > order => { + tree.split_above(node, 0.3, vec![p]); + } + Some((node, _)) => { + tree.split_below(node, 0.6, vec![p]); + } + None => beside_viewport(tree, Split::Right, 0.78, p), + } + } + } +} + +/// Split the viewport's group; `fraction` is the left (or top) share. +fn beside_viewport(tree: &mut Tree, side: Split, fraction: f32, p: Panel) { + match tree.find_tab(&Panel::Viewport) { + Some((node, _)) => { + tree.split(node, side, fraction, egui_dock::Node::leaf(p)); + } + None => tree.push_to_first_leaf(p), + } +} + +// --------------------------------------------------------------------------------------------- +// Drawing + +struct Host<'a> { + app: &'a mut TextureCraftApp, +} + +impl TabViewer for Host<'_> { + type Tab = Panel; + + fn id(&mut self, tab: &mut Panel) -> Id { + Id::new(("panel", *tab)) + } + + fn title(&mut self, tab: &mut Panel) -> WidgetText { + let text = match tab { + Panel::Properties => crate::dock::properties_title(self.app), + // Bake mode shows its settings where the shelf lives, as Painter does. + Panel::Assets if self.app.ui.mode == crate::state::Mode::Baking => tr("Bake Mesh Maps").to_string(), + p => tr(p.name()).to_string(), + }; + RichText::new(text.to_uppercase()).size(10.5).strong().into() + } + + fn ui(&mut self, ui: &mut Ui, tab: &mut Panel) { + let app = &mut *self.app; + match tab { + Panel::Viewport => crate::viewport::central(app, ui), + Panel::Assets if app.ui.mode == crate::state::Mode::Baking => crate::windows::bake_body(app, ui), + Panel::Assets => crate::shelf::body(app, ui), + Panel::TextureSetList => crate::dock::texture_set_list(app, ui), + Panel::Layers => crate::dock::layers(app, ui), + Panel::TextureSetSettings => crate::dock::texture_set_settings(app, ui), + Panel::Properties => crate::dock::properties(app, ui), + Panel::Display => crate::windows::display(app, ui), + Panel::Shader => crate::windows::shader(app, ui), + Panel::History => crate::windows::history(app, ui), + Panel::Log => crate::windows::log(app, ui), + Panel::Unknown => {} + } + } + + fn is_closeable(&self, tab: &Panel) -> bool { + *tab != Panel::Viewport + } + + fn allowed_in_windows(&self, tab: &mut Panel) -> bool { + *tab != Panel::Viewport + } + + fn scroll_bars(&self, tab: &Panel) -> [bool; 2] { + // The other panels scroll their own lists. + [false, matches!(tab, Panel::Display | Panel::Shader)] + } + + fn tab_style_override(&self, tab: &Panel, global: &TabStyle) -> Option { + (*tab == Panel::Viewport).then(|| { + let mut s = global.clone(); + s.tab_body.inner_margin = Margin::ZERO; + s.tab_body.bg_fill = TOKENS.viewport; + s + }) + } +} + +/// The dock look: flat dark tab bars, Painter-style uppercase captions, thin separators. +fn style(egui_style: &egui::Style) -> Style { + let t = TOKENS; + let mut s = Style::from_egui(egui_style); + s.separator.width = 2.0; + // Smallest panel extent a separator drag leaves (the crate's 175 px would grow the list). + s.separator.extra = 60.0; + s.separator.color_idle = t.separator; + s.separator.color_hovered = t.accent_dim; + s.separator.color_dragged = t.accent; + s.tab_bar.bg_fill = t.chrome; + s.tab_bar.height = 24.0; + s.tab_bar.hline_color = t.separator; + s.tab_bar.corner_radius = CornerRadius::ZERO; + for (tab, text) in [ + (&mut s.tab.active, t.text_strong), + (&mut s.tab.focused, t.text_strong), + (&mut s.tab.active_with_kb_focus, t.text_strong), + (&mut s.tab.focused_with_kb_focus, t.text_strong), + (&mut s.tab.hovered, t.text), + (&mut s.tab.inactive, t.text_dim), + (&mut s.tab.inactive_with_kb_focus, t.text_dim), + ] { + tab.text_color = text; + tab.corner_radius = CornerRadius::ZERO; + tab.bg_fill = if text == t.text_strong { t.panel } else { t.chrome }; + tab.outline_color = tab.bg_fill; + } + let body = &mut s.tab.tab_body; + body.bg_fill = t.panel; + body.inner_margin = Margin::symmetric(6, 4); + body.stroke = Stroke::NONE; + body.corner_radius = CornerRadius::ZERO; + // The viewport's group hides its tab bar entirely: no drag strip, no "show tab bar" button. + body.hidden_tab_bar_drag_height = Some(0.0); + s.buttons.show_tab_bar_size = 0.0; + s.buttons.show_tab_bar_hover_expand = 0.0; + s.buttons.show_tab_bar_color = egui::Color32::TRANSPARENT; + s.buttons.show_tab_bar_active_color = egui::Color32::TRANSPARENT; + s.overlay.selection_color = t.accent.gamma_multiply(0.35); + s +} + +/// Draw the dock (viewport and panels) into `ui`. +pub fn show(app: &mut TextureCraftApp, ui: &mut Ui) { + let Some(mut state) = app.ui.dock.0.take() else { + app.ui.dock = Dock::default(); + return; + }; + // Only the viewport alone in its group hides the tab bar. + for (_, leaf) in state.iter_leaves_mut() { + leaf.tab_bar_hidden = leaf.tabs == [Panel::Viewport]; + } + DockArea::new(&mut state) + .id(Id::new("dock_area")) + .style(style(ui.style())) + .hidable_tab_bars(true) + .tab_context_menus(false) + .show_leaf_close_all_buttons(false) + .show_leaf_collapse_buttons(false) + .show_secondary_button_hint(false) + .show_inside(ui, &mut Host { app }); + // A layout adopted while drawing (Window ▸ Load UI Layout) wins over the drawn one. + if app.ui.dock.0.is_none() { + app.ui.dock.0 = Some(state); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn default_layout_round_trips() { + let dock = Dock::default(); + assert_eq!(dock.layout(), Layout::default()); + let json = serde_json::to_string(&dock).unwrap(); + let back: Dock = serde_json::from_str(&json).unwrap(); + assert_eq!(back.layout(), Layout::default()); + } + + #[test] + fn hostile_layouts_are_repaired() { + // Duplicates, unknown panels, a viewport in a window, bad fractions and rects, empty groups. + let json = r#"{"main":{"split":{"vertical":false,"fraction":0.3,"first":{"tabs":{"tabs":["Layers","Layers","Bogus"],"active":9}}, + "second":{"split":{"vertical":true,"fraction":7,"first":{"tabs":{"tabs":["Viewport"]}},"second":{"tabs":{"tabs":[]}}}}}}, + "windows":[{"rect":[0,0,-5,10],"root":{"tabs":{"tabs":["Viewport","Log","Layers"]}}}]}"#; + let dock: Dock = serde_json::from_str(json).unwrap(); + let l = dock.layout(); + assert_eq!(l.main, split(false, 0.3, Node::Tabs { tabs: vec![Panel::Layers], active: 0 }, tabs(&[Panel::Viewport]))); + assert_eq!(l.windows.len(), 1); + assert_eq!(l.windows[0].root, tabs(&[Panel::Log])); + // No viewport at all: the default layout. + let none: Dock = serde_json::from_str(r#"{"main":{"tabs":{"tabs":["Layers"]}}}"#).unwrap(); + assert_eq!(none.layout(), Layout::default()); + } + + #[test] + fn deep_trees_are_flattened() { + let mut node = tabs(&[Panel::Viewport]); + for p in [ + Panel::Assets, + Panel::Layers, + Panel::Log, + Panel::History, + Panel::Shader, + Panel::Display, + Panel::Properties, + Panel::TextureSetList, + Panel::TextureSetSettings, + ] { + node = split(true, 0.5, tabs(&[p]), node); + } + let dock = Dock(Layout { main: node, windows: Vec::new() }.build()); + let mut all = Vec::new(); + dock.layout().main.collect(&mut all); + assert_eq!(all.len(), 10); + } + + #[test] + fn closed_panels_come_back_home() { + let screen = Rect::from_min_size(pos2(0.0, 0.0), vec2(1600.0, 960.0)); + let mut dock = Dock::default(); + assert!(dock.is_visible(Panel::Layers) && !dock.is_visible(Panel::TextureSetSettings)); + dock.toggle(Panel::TextureSetSettings, screen); + assert!(dock.is_visible(Panel::TextureSetSettings) && !dock.is_visible(Panel::Layers)); + for p in [Panel::Assets, Panel::TextureSetList, Panel::Layers, Panel::TextureSetSettings, Panel::Properties] { + dock.close(p); + assert!(!dock.is_open(p)); + } + for p in [Panel::Properties, Panel::Layers, Panel::TextureSetList, Panel::Assets, Panel::Display] { + dock.focus(p, screen); + assert!(dock.is_visible(p), "{p:?}"); + } + dock.close(Panel::Viewport); + assert!(dock.is_open(Panel::Viewport)); + let l = dock.layout(); + assert_eq!(l.windows.len(), 1); + let mut order = Vec::new(); + l.main.collect(&mut order); + assert_eq!(order, [Panel::Assets, Panel::Viewport, Panel::TextureSetList, Panel::Layers, Panel::Properties]); + } +} diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rs new file mode 100644 index 00000000..55d56ee7 --- /dev/null +++ b/crates/ui-egui/src/gpu.rs @@ -0,0 +1,251 @@ +//! Bridge between the session and the wgpu renderer: per-frame scene sync (mesh, texture set +//! layout, dirty tiles, environment) and the egui paint callback that renders and blits a view. + +use std::collections::BTreeMap; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex, PoisonError}; + +use eframe::egui_wgpu::{self, CallbackResources, CallbackTrait, ScreenDescriptor}; +use rayon::prelude::*; +use texturecraft_engine::Session; +use texturecraft_engine::compose::{DisplayKind, ImageRes, tile_bytes}; +use texturecraft_engine::core::BakedMap; +use texturecraft_engine::geom::Mesh; +use texturecraft_render::{Frame, Renderer, wgpu}; + +/// Content shown through the aux texture. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum AuxView { + MeshMap(BakedMap), + Mask(texturecraft_engine::core::LayerId), + /// The quick mask being edited, over the base color. + QuickMask, +} + +/// What the renderer must catch up with before drawing. +#[derive(Default)] +pub struct SceneSync { + pub mesh: Option<(Arc, u64)>, + /// (resolution, material) per texture set, and the mesh material count. + pub sets: Option<(Vec<(u32, usize)>, usize)>, + /// The environment name and, for `resource:`, its decoded image. + pub env: Option<(String, Option>)>, + pub tiles: Vec<(usize, DisplayKind, u32, u32, Vec)>, + pub aux: Vec<(usize, u32, Vec)>, +} + +/// Lives in egui's callback resources. +pub struct GpuState { + pub renderer: Renderer, + /// Raised when texture-set textures were recreated: the UI must upload every tile again. + pub reset: Arc, +} + +/// UI-side bookkeeping of what the GPU already has. +pub struct GpuLink { + pub reset: Arc, + /// Raised when a view callback was dropped before the GPU applied its scene sync (the + /// frame was never painted, e.g. a WebGPU surface not ready yet): send everything again. + pub lost: Arc, + mesh_id: u64, + sets: Vec<(u32, usize)>, + /// The environment the GPU has: name and image identity (a re-imported resource differs). + env: (String, usize), + /// What each set's aux texture holds: (mesh map or mask key, revision). + aux: BTreeMap, + pub adapter: String, + /// Most tiles waiting to be composited since compositing last caught up (progress). + composite_total: usize, +} + +/// Compositing time per frame: a full recomposite of a large texture set spreads over frames. +const COMPOSITE_BUDGET_MS: f64 = 30.0; + +pub fn init(rs: &egui_wgpu::RenderState) -> GpuLink { + let reset = Arc::new(AtomicBool::new(false)); + let renderer = Renderer::new(&rs.device, &rs.queue, rs.target_format); + rs.renderer.write().callback_resources.insert(GpuState { renderer, reset: reset.clone() }); + let info = rs.adapter.get_info(); + GpuLink { + reset, + lost: Arc::new(AtomicBool::new(false)), + mesh_id: 0, + sets: Vec::new(), + env: Default::default(), + aux: BTreeMap::new(), + adapter: format!("{} ({:?})", info.name, info.backend), + composite_total: 0, + } +} + +impl GpuLink { + /// Collect everything the GPU needs this frame (runs the compositor first). + /// Compositing progress (0..1) while tiles wait to be composited. + pub fn composite_progress(&self, s: &Session) -> Option { + let pending = s.pending_tiles(); + (pending > 0 && self.composite_total > 0).then(|| 1.0 - pending as f32 / self.composite_total as f32) + } + + pub fn sync(&mut self, s: &mut Session, aux_map: Option) -> SceneSync { + let pending = s.pending_tiles(); + self.composite_total = if pending == 0 { 0 } else { self.composite_total.max(pending) }; + s.update_for(COMPOSITE_BUDGET_MS, crate::now_ms); + let mut out = SceneSync::default(); + if self.lost.swap(false, Ordering::AcqRel) { + self.mesh_id = 0; + self.sets.clear(); + self.env = Default::default(); + self.reset.store(true, Ordering::Release); + } + if self.mesh_id != s.mesh_id { + out.mesh = Some((s.mesh.clone(), s.mesh_id)); + self.mesh_id = s.mesh_id; + self.aux.clear(); + } + let sets: Vec<(u32, usize)> = s.doc.project.texture_sets.iter().map(|t| (t.resolution, t.material)).collect(); + if sets != self.sets || out.mesh.is_some() { + out.sets = Some((sets.clone(), s.mesh.materials.len().max(1))); + self.sets = sets; + } + let d = &s.doc.project.display; + let image = d.environment_resource().and_then(|n| s.images.get(n)); + let ptr = image.map_or(0, |i| Arc::as_ptr(i) as usize); + if self.env.0 != d.environment || self.env.1 != ptr { + self.env = (d.environment.clone(), ptr); + out.env = Some((d.environment.clone(), image.cloned())); + } + if self.reset.swap(false, Ordering::AcqRel) { + for c in &mut s.caches { + c.upload.extend(texturecraft_engine::compose::all_tiles(c.resolution)); + } + self.aux.clear(); + } + for ts in 0..s.caches.len() { + let tiles = s.take_uploads(ts); + if tiles.is_empty() { + continue; + } + let Some(cache) = s.caches.get(ts) else { continue }; + let empty = Default::default(); + let baked = s.doc.baked.get(&ts).unwrap_or(&empty); + let comp = &cache.composite; + let bytes: Vec<_> = tiles + .par_iter() + .flat_map_iter(|&(tx, ty)| DisplayKind::ALL.into_iter().map(move |k| (k, tx, ty))) + .map(|(k, tx, ty)| (ts, k, tx, ty, tile_bytes(comp, baked, k, tx, ty))) + .collect(); + out.tiles.extend(bytes); + } + match aux_map { + Some(AuxView::MeshMap(m)) => { + for ts in 0..s.caches.len() { + let rev = s.doc.baked.get(&ts).and_then(|b| b.maps.get(&m)).map(|d| Arc::as_ptr(d) as u64).unwrap_or(0); + let key = (format!("{m:?}"), rev); + if self.aux.get(&ts) == Some(&key) { + continue; + } + let res = s.caches.get(ts).map(|c| c.resolution).unwrap_or(1024).min(2048); + out.aux.push((ts, res, texturecraft_render::mesh_maps::mesh_map_rgba(s.doc.baked.get(&ts), m, res))); + self.aux.insert(ts, key); + } + } + Some(AuxView::Mask(id)) => { + if let Some((ts, _, _)) = s.doc.project.locate(id) { + let key = (format!("mask{}", id.0), s.revision); + if self.aux.get(&ts) != Some(&key) && !s.stroke_active() { + let res = s.caches.get(ts).map(|c| c.resolution).unwrap_or(1024).min(2048); + if let Some(rgba) = s.mask_view_rgba(id, res) { + out.aux.push((ts, res, rgba)); + } + self.aux.insert(ts, key); + } + } + } + Some(AuxView::QuickMask) => { + for ts in 0..s.caches.len() { + let Some(cache) = s.caches.get(ts) else { continue }; + let key = (format!("quickmask{}", s.quick_mask.revision), cache.revision); + if self.aux.get(&ts) == Some(&key) { + continue; + } + let res = cache.resolution.min(2048); + if let Some(rgba) = s.quick_mask_rgba(ts, res) { + out.aux.push((ts, res, rgba)); + } + self.aux.insert(ts, key); + } + } + None => {} + } + out + } +} + +/// Paint callback for one view. +pub struct ViewCallback { + pub frame: Frame, + /// Scene changes, applied by the first view drawn this frame. + pub sync: Mutex>, + pub rect: egui::Rect, + /// [`GpuLink::lost`]. + pub lost: Arc, +} + +impl Drop for ViewCallback { + fn drop(&mut self) { + if self.sync.get_mut().unwrap_or_else(PoisonError::into_inner).is_some() { + self.lost.store(true, Ordering::Release); + } + } +} + +impl CallbackTrait for ViewCallback { + fn prepare( + &self, + device: &wgpu::Device, + queue: &wgpu::Queue, + screen: &ScreenDescriptor, + encoder: &mut wgpu::CommandEncoder, + resources: &mut CallbackResources, + ) -> Vec { + let Some(gpu) = resources.get_mut::() else { return Vec::new() }; + let sync = self.sync.lock().unwrap_or_else(PoisonError::into_inner).take(); + if let Some(sync) = sync { + if let Some((env, image)) = &sync.env { + gpu.renderer.set_environment(device, queue, env, image.as_deref()); + } + if let Some((mesh, id)) = &sync.mesh { + gpu.renderer.set_mesh(device, queue, mesh, *id); + } + if let Some((sets, mats)) = &sync.sets { + let reset = gpu.renderer.set_texture_sets(device, queue, sets, *mats); + if !reset.is_empty() { + gpu.reset.store(true, Ordering::Release); + } + } + for (ts, k, tx, ty, bytes) in &sync.tiles { + gpu.renderer.upload_tile(queue, *ts, *k, *tx, *ty, bytes); + } + for (ts, size, bytes) in &sync.aux { + gpu.renderer.upload_aux(queue, *ts, *size, bytes); + } + } + gpu.renderer.prepare(device, queue, encoder, &self.frame); + let ppp = screen.pixels_per_point; + let r = self.rect; + gpu.renderer.set_blit_rect(queue, self.frame.view_id, [r.min.x * ppp, r.min.y * ppp, r.width() * ppp, r.height() * ppp], screen.size_in_pixels); + Vec::new() + } + + fn paint(&self, info: egui::PaintCallbackInfo, pass: &mut wgpu::RenderPass<'static>, resources: &CallbackResources) { + let Some(gpu) = resources.get::() else { return }; + let [sw, sh] = info.screen_size_px; + let clip = info.clip_rect_in_pixels(); + if clip.width_px <= 0 || clip.height_px <= 0 || sw == 0 || sh == 0 { + return; + } + pass.set_viewport(0.0, 0.0, sw as f32, sh as f32, 0.0, 1.0); + pass.set_scissor_rect(clip.left_px.max(0) as u32, clip.top_px.max(0) as u32, clip.width_px as u32, clip.height_px as u32); + gpu.renderer.blit_prepared(pass, self.frame.view_id); + } +} diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs new file mode 100644 index 00000000..5197b154 --- /dev/null +++ b/crates/ui-egui/src/i18n.rs @@ -0,0 +1,43 @@ +//! UI text lookup. Keys are the English strings, so English needs no table and a missing +//! translation falls back to English. A language is one `Language` entry whose table lists +//! `(English, translation)` pairs sorted by the English key; `Prefs::language` picks it. + +use std::sync::atomic::{AtomicUsize, Ordering}; + +pub struct Language { + /// BCP 47 code ("en", "de", "ja"). + pub code: &'static str, + /// The language's own name, for a picker. + pub name: &'static str, + /// `(English, translation)`, sorted by the English key. + pub table: &'static [(&'static str, &'static str)], +} + +pub const LANGUAGES: &[Language] = &[Language { code: "en", name: "English", table: &[] }]; + +static CURRENT: AtomicUsize = AtomicUsize::new(0); + +/// Switch the UI language (unknown codes fall back to English). +pub fn set_language(code: &str) { + CURRENT.store(LANGUAGES.iter().position(|l| l.code == code).unwrap_or(0), Ordering::Relaxed); +} + +/// `text` in the current language. +pub fn tr(text: &'static str) -> &'static str { + let Some(lang) = LANGUAGES.get(CURRENT.load(Ordering::Relaxed)) else { return text }; + lang.table.binary_search_by_key(&text, |(en, _)| en).ok().and_then(|i| lang.table.get(i)).map_or(text, |(_, t)| t) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn tables_are_sorted_and_english_is_identity() { + for l in LANGUAGES { + assert!(l.table.windows(2).all(|w| w[0].0 < w[1].0), "{} table must be sorted by key", l.code); + } + set_language("xx"); + assert_eq!(tr("Layers"), "Layers"); + } +} diff --git a/crates/ui-egui/src/keymap.rs b/crates/ui-egui/src/keymap.rs new file mode 100644 index 00000000..e010734d --- /dev/null +++ b/crates/ui-egui/src/keymap.rs @@ -0,0 +1,295 @@ +//! Keyboard shortcuts: every menu item and every command with a default shortcut, the user's +//! overrides (`UiState::keymap`, id → "Ctrl+Shift+E" or "" for unbound), conflict detection and +//! the shortcut editor window (Edit ▸ Settings ▸ Keyboard, Help ▸ Shortcuts list). + +use std::collections::BTreeMap; + +use egui::{Event, Id, Key, KeyboardShortcut, Modifiers, RichText, vec2}; +use egui_phosphor::regular as ic; + +use crate::TextureCraftApp; +use crate::i18n::tr; +use crate::menus::{CATALOG, parse_shortcut}; +use crate::theme::TOKENS; +use crate::widgets::{icon_button, section}; + +pub type Keymap = BTreeMap; + +/// Something a shortcut can trigger. +pub struct Binding { + pub id: &'static str, + /// Menu (or command group) it belongs to. + pub group: &'static str, + pub label: &'static str, + pub default: Option<&'static str>, +} + +/// Every bindable item: the menu catalog, then commands with a default shortcut. +pub fn bindings() -> &'static [Binding] { + static ALL: std::sync::OnceLock> = std::sync::OnceLock::new(); + ALL.get_or_init(|| { + let mut v: Vec = Vec::new(); + for i in CATALOG.iter().filter(|i| i.label != "-") { + if !v.iter().any(|b| b.id == i.id) { + v.push(Binding { id: i.id, group: i.menu, label: i.label, default: i.shortcut }); + } + } + for c in texturecraft_engine::command_specs().iter().filter(|c| c.shortcut.is_some()) { + if !v.iter().any(|b| b.id == c.id) { + v.push(Binding { id: c.id, group: c.menu.first().copied().unwrap_or("Commands"), label: c.label, default: c.shortcut }); + } + } + v + }) +} + +/// The shortcut `b` currently has. +pub fn shortcut<'a>(keymap: &'a Keymap, b: &'a Binding) -> Option<&'a str> { + match keymap.get(b.id) { + Some(s) => (!s.is_empty()).then_some(s.as_str()), + None => b.default, + } +} + +/// The shortcut of item `id` (for menu labels and tooltips). +pub fn shortcut_of<'a>(keymap: &'a Keymap, id: &str) -> Option<&'a str> { + bindings().iter().find(|b| b.id == id).and_then(|b| shortcut(keymap, b)) +} + +/// "Label (Shortcut)" for an icon button's tooltip. +pub fn tip(keymap: &Keymap, label: &str, id: &str) -> String { + match shortcut_of(keymap, id) { + Some(s) => format!("{label} ({s})"), + None => label.to_owned(), + } +} + +/// "Ctrl+Shift+E" notation (the inverse of `menus::parse_shortcut`). +pub fn format_shortcut(sc: &KeyboardShortcut) -> String { + let m = sc.modifiers; + let mut s = String::new(); + if m.command || m.ctrl || m.mac_cmd { + s.push_str("Ctrl+"); + } + if m.shift { + s.push_str("Shift+"); + } + if m.alt { + s.push_str("Alt+"); + } + s.push_str(sc.logical_key.name()); + s +} + +fn modifier_count(m: Modifiers) -> u8 { + u8::from(m.command || m.ctrl || m.mac_cmd) + u8::from(m.shift) + u8::from(m.alt) +} + +/// Every bound shortcut with its item id, most modifiers first: egui ignores extra Shift and +/// Alt when matching, so Shift+C has to be tried before C. +pub fn active(keymap: &Keymap) -> Vec<(&'static str, KeyboardShortcut)> { + let mut v: Vec<(&'static str, KeyboardShortcut)> = bindings().iter().filter_map(|b| Some((b.id, parse_shortcut(shortcut(keymap, b)?)?))).collect(); + v.sort_by_key(|(_, sc)| std::cmp::Reverse(modifier_count(sc.modifiers))); + v +} + +/// Each binding's parsed shortcut, in `bindings()` order. +pub fn resolve(keymap: &Keymap) -> Vec> { + bindings().iter().map(|b| shortcut(keymap, b).and_then(parse_shortcut)).collect() +} + +/// Another binding with the same shortcut as binding `i`. +pub fn conflict(resolved: &[Option], i: usize) -> Option { + let sc = resolved.get(i).copied().flatten()?; + resolved.iter().enumerate().position(|(j, o)| j != i && *o == Some(sc)) +} + +/// Bind `b` to `value` ("" = no shortcut); the default is stored as no override. +pub fn rebind(keymap: &mut Keymap, b: &Binding, value: &str) { + if b.default.unwrap_or("") == value { + keymap.remove(b.id); + } else { + keymap.insert(b.id.to_owned(), value.to_owned()); + } +} + +const CAPTURE: &str = "keymap_capture"; + +/// Whether the editor is waiting for a key combination (global shortcuts pause meanwhile). +pub fn capturing(ctx: &egui::Context) -> bool { + ctx.data(|d| d.get_temp::(Id::new(CAPTURE))).is_some() +} + +/// The first key press this frame as a shortcut. +fn pressed(ctx: &egui::Context) -> Option { + ctx.input(|i| { + i.events.iter().find_map(|e| match e { + Event::Key { key, pressed: true, modifiers, .. } => Some(KeyboardShortcut::new(*modifiers, *key)), + _ => None, + }) + }) +} + +/// The shortcut editor window. +pub fn window(app: &mut TextureCraftApp, ctx: &egui::Context) { + if !app.ui.windows.shortcuts { + return; + } + let capture_id = Id::new(CAPTURE); + let filter_id = Id::new("keymap_filter"); + let mut capture: Option = ctx.data(|d| d.get_temp(capture_id)); + if let (Some(i), Some(sc)) = (capture, pressed(ctx)) { + if (sc.logical_key != Key::Escape || !sc.modifiers.is_none()) + && let Some(b) = bindings().get(i) + { + rebind(&mut app.ui.keymap, b, &format_shortcut(&sc)); + } + capture = None; + } + let mut filter: String = ctx.data(|d| d.get_temp(filter_id)).unwrap_or_default(); + let mut open = true; + crate::windows::window("Keyboard shortcuts", &mut open).default_width(480.0).show(ctx, |ui| { + let resolved = resolve(&app.ui.keymap); + ui.horizontal(|ui| { + ui.label(RichText::new(ic::MAGNIFYING_GLASS).color(TOKENS.text_dim)); + ui.add(egui::TextEdit::singleline(&mut filter).hint_text("Search commands or keys").desired_width(300.0)); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if ui.add_enabled(!app.ui.keymap.is_empty(), egui::Button::new("Reset all")).on_hover_text("Restore every default shortcut").clicked() { + app.ui.keymap.clear(); + } + }); + }); + let clashes = (0..resolved.len()).filter(|i| conflict(&resolved, *i).is_some()).count(); + if clashes > 0 { + ui.label(RichText::new(format!("{} {clashes} items share a shortcut; the first one listed wins.", ic::WARNING)).color(TOKENS.danger)); + } + ui.add_space(4.0); + let needle = filter.to_lowercase(); + egui::ScrollArea::vertical().max_height(420.0).auto_shrink([false, true]).show(ui, |ui| { + egui::Grid::new("keymap_grid").num_columns(3).striped(true).spacing(vec2(10.0, 4.0)).show(ui, |ui| { + for (i, b) in bindings().iter().enumerate() { + let current = shortcut(&app.ui.keymap, b); + if !needle.is_empty() && ![b.label, b.group, b.id, current.unwrap_or("")].iter().any(|t| t.to_lowercase().contains(&needle)) { + continue; + } + ui.horizontal(|ui| { + ui.label(tr(b.label)); + ui.label(RichText::new(tr(b.group)).size(10.5).color(TOKENS.text_dim)); + }); + let waiting = capture == Some(i); + let text = if waiting { "Press a key..." } else { current.unwrap_or("-") }; + let key = egui::Button::new(RichText::new(text).monospace()).selected(waiting).min_size(vec2(130.0, 0.0)); + if ui.add(key).on_hover_text("Click, then press the new shortcut (Esc cancels)").clicked() { + capture = if waiting { None } else { Some(i) }; + } + let bound = current.is_some(); + ui.horizontal(|ui| { + if let Some(other) = conflict(&resolved, i).and_then(|j| bindings().get(j)) { + ui.label(RichText::new(ic::WARNING).color(TOKENS.danger)).on_hover_text(format!( + "Also bound to {} ({})", + tr(other.label), + tr(other.group) + )); + } + if bound && icon_button(ui, ic::X, "Remove the shortcut", false, 18.0).clicked() { + rebind(&mut app.ui.keymap, b, ""); + } + if app.ui.keymap.contains_key(b.id) + && icon_button(ui, ic::ARROW_COUNTER_CLOCKWISE, &format!("Reset to {}", b.default.unwrap_or("none")), false, 18.0).clicked() + { + app.ui.keymap.remove(b.id); + } + }); + ui.end_row(); + } + }); + }); + ui.add_space(4.0); + section(ui, "keymap_nav", "Viewport and brush (fixed)", false, |ui| { + egui::Grid::new("keymap_fixed").striped(true).show(ui, |ui| { + for (k, v) in [ + ("Alt + LMB drag", "Orbit"), + ("MMB drag / Alt + MMB", "Pan"), + ("Wheel / Alt + RMB drag", "Zoom"), + ("LMB", "Paint with the current tool"), + ("[ / ]", "Brush size down / up"), + ("Shift + wheel", "Brush size"), + ("Esc", "Cancel the stroke"), + ] { + ui.label(RichText::new(k).strong()); + ui.label(v); + ui.end_row(); + } + }); + }); + }); + app.ui.windows.shortcuts = open; + if !open { + capture = None; + } + ctx.data_mut(|d| { + d.insert_temp(filter_id, filter); + match capture { + Some(i) => { + d.insert_temp(capture_id, i); + } + None => { + d.remove::(capture_id); + } + } + }); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn shortcuts_round_trip_through_parse_and_format() { + for b in bindings() { + if let Some(s) = b.default { + let sc = parse_shortcut(s).unwrap_or_else(|| panic!("{} has an unparsable shortcut {s}", b.id)); + assert_eq!(format_shortcut(&sc), s); + } + } + for s in ["Ctrl+Shift+Alt+F12", "Alt+OpenBracket", "Shift+Delete", "Ctrl+9"] { + assert_eq!(parse_shortcut(s).map(|sc| format_shortcut(&sc)).as_deref(), Some(s)); + } + assert!(parse_shortcut("Ctrl+").is_none() && parse_shortcut("Ctrl+NoSuchKey").is_none()); + } + + #[test] + fn default_shortcuts_do_not_conflict() { + let resolved = resolve(&Keymap::new()); + for (i, b) in bindings().iter().enumerate() { + assert!(conflict(&resolved, i).is_none(), "{} shares its default shortcut", b.id); + } + } + + #[test] + fn rebinding_detects_conflicts_and_resets() { + let all = bindings(); + let save = all.iter().position(|b| b.id == "file.save").unwrap(); + let undo = all.iter().position(|b| b.id == "edit.undo").unwrap(); + let mut keymap = Keymap::new(); + rebind(&mut keymap, &all[save], "Ctrl+Z"); + let resolved = resolve(&keymap); + assert_eq!(conflict(&resolved, save), Some(undo)); + assert_eq!(conflict(&resolved, undo), Some(save)); + assert_eq!(shortcut_of(&keymap, "file.save"), Some("Ctrl+Z")); + // Unbinding clears the conflict; binding back to the default drops the override. + rebind(&mut keymap, &all[save], ""); + assert_eq!(shortcut_of(&keymap, "file.save"), None); + assert!(conflict(&resolve(&keymap), undo).is_none()); + rebind(&mut keymap, &all[save], "Ctrl+S"); + assert!(keymap.is_empty()); + } + + #[test] + fn specific_shortcuts_are_tried_first() { + let active = active(&Keymap::new()); + let pos = |id: &str| active.iter().position(|(x, _)| *x == id).unwrap(); + assert!(pos("viewport.previousChannel") < pos("viewport.nextChannel")); + assert!(pos("file.saveAs") < pos("file.save")); + } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs new file mode 100644 index 00000000..2709f23d --- /dev/null +++ b/crates/ui-egui/src/lib.rs @@ -0,0 +1,349 @@ +//! # texturecraft-ui-egui +//! +//! The TextureCraft desktop and web shell: a dense dark professional layout (menu bar, +//! tool-settings bar, tool shelf, asset shelf, 3D + 2D viewports, texture set list, layer stack, +//! properties, display/shader/history windows, bake mode). It is a thin layer: every action goes +//! through `texturecraft-engine` commands, so the CLI, control channel and MCP can do the same. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod control; +pub mod dock; +pub mod docking; +pub mod gpu; +pub mod i18n; +pub mod keymap; +pub mod menus; +pub mod parity; +pub mod render_mode; +pub mod shelf; +pub mod state; +pub mod theme; +pub mod thumbs; +pub mod toolbar; +pub mod viewport; +pub mod web_files; +pub mod widgets; +pub mod windows; + +use std::sync::Arc; +use std::sync::atomic::Ordering; + +use serde_json::Value; +use texturecraft_engine::bake::Progress; +use texturecraft_engine::{BakeTask, Session}; + +pub use control::ControlRequest; +pub use state::UiState; + +/// A background bake of one or more texture sets. +pub struct BakeJob { + pub queue: Vec, + pub current: Option<(usize, Arc, std::sync::mpsc::Receiver>)>, + pub total: usize, +} + +pub struct Toast { + pub text: String, + pub error: bool, + pub until: f64, +} + +pub struct TextureCraftApp { + pub session: Session, + pub ui: UiState, + pub gpu: Option, + pub control: Option>, + pub toasts: Vec, + pub bake: Option, + pub thumbs: thumbs::Thumbs, + /// Last frame's viewport rect (logical points), for scripted input. + pub viewport_rect: egui::Rect, + /// Pending screenshot requests from the control channel. + pub screenshot_waiters: Vec>, + pub fps: f32, + /// User smart materials found in the library folder (refreshed on demand). + pub library: Option>, + /// Rendering mode's path tracer. + pub render: render_mode::RenderView, + /// Smoothed CPU time spent building the UI per frame (ms). + pub ui_ms: f32, + /// Where the last 3D stroke ended (viewport pixels), for Shift+click straight lines. + pub last_paint_point: Option, + /// The wgpu state (screenshots read the renderer's targets). + pub render_state: Option, + last_time: f64, + /// When the next autosave is due (seconds of app time; desktop only). + #[cfg_attr(target_arch = "wasm32", allow(dead_code))] + next_autosave: f64, + styled: bool, +} + +impl TextureCraftApp { + pub fn new(cc: &eframe::CreationContext<'_>, session: Session) -> TextureCraftApp { + theme::install(&cc.egui_ctx); + let gpu = cc.wgpu_render_state.as_ref().map(gpu::init); + let ui = cc.storage.and_then(|s| eframe::get_value::(s, "ui")).unwrap_or_default(); + let next_autosave = ui.prefs.autosave_minutes as f64 * 60.0; + let mut app = TextureCraftApp { + session, + ui, + gpu, + control: None, + toasts: Vec::new(), + bake: None, + thumbs: thumbs::Thumbs::default(), + viewport_rect: egui::Rect::NOTHING, + screenshot_waiters: Vec::new(), + fps: 0.0, + ui_ms: 0.0, + library: None, + render: render_mode::RenderView::default(), + last_paint_point: None, + render_state: cc.wgpu_render_state.clone(), + last_time: 0.0, + next_autosave, + styled: true, + }; + app.ui.rename = None; + app.ui.windows.new_project = false; + app + } + + /// Run an engine command, reporting errors as toasts. Returns the result on success. + pub fn run(&mut self, id: &str, params: Value) -> Option { + match texturecraft_engine::execute(&mut self.session, id, ¶ms) { + Ok(v) => Some(v), + Err(e) => { + self.toast(e.to_string(), true); + None + } + } + } + + pub fn toast(&mut self, text: impl Into, error: bool) { + self.toasts.push(Toast { text: text.into(), error, until: self.last_time + if error { 6.0 } else { 3.0 } }); + } + + /// Start baking texture sets in the background (one after another). + pub fn start_bake(&mut self, sets: Vec) { + if self.bake.is_some() || sets.is_empty() { + return; + } + let total = sets.len(); + self.bake = Some(BakeJob { queue: sets, current: None, total }); + } + + fn poll_bake(&mut self, ctx: &egui::Context) { + let Some(job) = self.bake.as_mut() else { return }; + ctx.request_repaint_after(std::time::Duration::from_millis(50)); + if let Some((ts, _, rx)) = &job.current { + match rx.try_recv() { + Ok(Ok(out)) => { + let ts = *ts; + job.current = None; + if let Err(e) = self.session.apply_bake(ts, out) { + self.toast(e.to_string(), true); + } + } + Ok(Err(e)) => { + job.current = None; + let msg = e.to_string(); + self.bake = None; + self.toast(msg, true); + return; + } + Err(std::sync::mpsc::TryRecvError::Empty) => return, + Err(std::sync::mpsc::TryRecvError::Disconnected) => { + self.bake = None; + self.toast("bake worker stopped", true); + return; + } + } + } + let Some(job) = self.bake.as_mut() else { return }; + if job.current.is_none() { + if job.queue.is_empty() { + self.bake = None; + self.toast("Mesh maps baked", false); + return; + } + let ts = job.queue.remove(0); + match self.session.bake_task(ts) { + Ok(task) => { + let progress = Arc::new(Progress::new()); + let (tx, rx) = std::sync::mpsc::channel(); + spawn_bake(task, progress.clone(), tx); + if let Some(job) = self.bake.as_mut() { + job.current = Some((ts, progress, rx)); + } + } + Err(e) => self.toast(e.to_string(), true), + } + } + } + + pub fn cancel_bake(&mut self) { + if let Some(job) = self.bake.take() + && let Some((_, p, _)) = job.current + { + p.cancel.store(true, Ordering::Relaxed); + } + } + + /// Bake progress 0..1 across all queued sets. + pub fn bake_progress(&self) -> Option { + let job = self.bake.as_ref()?; + let done = job.total - job.queue.len() - usize::from(job.current.is_some()); + let cur = job.current.as_ref().map(|(_, p, _)| p.fraction()).unwrap_or(0.0); + Some(((done as f32 + cur) / job.total.max(1) as f32).clamp(0.0, 1.0)) + } +} + +#[cfg(not(target_arch = "wasm32"))] +fn spawn_bake(task: BakeTask, progress: Arc, tx: std::sync::mpsc::Sender>) { + std::thread::spawn(move || { + let _ = tx.send(task.run(&progress)); + }); +} + +#[cfg(target_arch = "wasm32")] +fn spawn_bake(task: BakeTask, progress: Arc, tx: std::sync::mpsc::Sender>) { + // shortcut: no threads on the web; the bake runs on the UI thread (the window pauses). + let _ = tx.send(task.run(&progress)); +} + +impl TextureCraftApp { + /// Every `prefs.autosave_minutes`, write a modified project to the autosave folder (encoded + /// and written on a worker thread; the project data is shared copy-on-write). + #[cfg(not(target_arch = "wasm32"))] + fn autosave(&mut self, now: f64) { + let period = self.ui.prefs.autosave_minutes as f64 * 60.0; + // The clock starts at the first unsaved change. + if period <= 0.0 || !self.session.modified { + self.next_autosave = now + period; + return; + } + if now < self.next_autosave { + return; + } + self.next_autosave = now + period; + let dir = user_dir("autosave"); + let path = dir.join(format!("{}.{}", texturecraft_engine::formats::export::sanitize_file_stem(&self.session.doc.project.name), texturecraft_engine::core::PROJECT_EXT)); + let pf = self.session.project_file(); + let spawned = std::thread::Builder::new().name("texturecraft-autosave".into()).spawn(move || { + let result = std::fs::create_dir_all(&dir).map_err(|e| e.to_string()).and_then(|_| texturecraft_engine::formats::save_project(&path, &pf).map_err(|e| e.to_string())); + match result { + Ok(()) => log::info!("autosaved {}", path.display()), + Err(e) => log::warn!("autosave to {} failed: {e}", path.display()), + } + }); + if let Err(e) = spawned { + log::warn!("could not start autosave: {e}"); + } + } +} + +impl eframe::App for TextureCraftApp { + fn save(&mut self, storage: &mut dyn eframe::Storage) { + eframe::set_value(storage, "ui", &self.ui); + } + + fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + let now = ctx.input(|i| i.time); + let dt = (now - self.last_time) as f32; + if dt > 0.0 { + self.fps = self.fps * 0.9 + (1.0 / dt) * 0.1; + } + self.last_time = now; + if !self.styled { + theme::install(ctx); + self.styled = true; + } + control::poll(self, ctx); + web_files::poll(self); + let dropped = ctx.input(|i| i.raw.dropped_files.clone()); + for f in &dropped { + web_files::dropped(self, ctx, f); + } + self.poll_bake(ctx); + let scale = self.ui.prefs.ui_scale.clamp(0.5, 3.0); + if (ctx.zoom_factor() - scale).abs() > 1e-3 { + ctx.set_zoom_factor(scale); + } + #[cfg(not(target_arch = "wasm32"))] + self.autosave(now); + i18n::set_language(&self.ui.prefs.language); + menus::shortcuts(self, ctx); + self.toasts.retain(|t| t.until > now); + } + + fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) { + let ctx = ui.ctx().clone(); + let t0 = now_ms(); + if !self.ui.hide_ui { + menus::menu_bar(self, ui); + toolbar::top_bar(self, ui); + toolbar::status_bar(self, ui); + toolbar::tool_shelf(self, ui); + // Bake settings take the Assets slot; a side panel only when Assets is closed. + if self.ui.mode == state::Mode::Baking && !self.ui.dock.is_open(docking::Panel::Assets) { + windows::bake_left(self, ui); + } + toolbar::rail(self, ui); + } + let hide = self.ui.hide_ui; + egui::CentralPanel::default().frame(egui::Frame::NONE.fill(if hide { theme::TOKENS.viewport } else { theme::TOKENS.separator })).show(ui, |ui| { + if hide { viewport::central(self, ui) } else { docking::show(self, ui) } + }); + windows::show(self, &ctx); + toasts(self, &ctx); + let dt = (now_ms() - t0) as f32; + self.ui_ms = if self.ui_ms == 0.0 { dt } else { self.ui_ms * 0.9 + dt * 0.1 }; + } +} + +/// `Documents/TextureCraft/` in the user's home folder. +#[cfg(not(target_arch = "wasm32"))] +pub fn user_dir(sub: &str) -> std::path::PathBuf { + let home = std::env::var("USERPROFILE").or_else(|_| std::env::var("HOME")).unwrap_or_else(|_| ".".into()); + std::path::Path::new(&home).join("Documents").join("TextureCraft").join(sub) +} + +/// Folder holding the user's smart materials. +#[cfg(not(target_arch = "wasm32"))] +pub fn library_dir() -> std::path::PathBuf { + user_dir("library") +} + +/// Milliseconds since the first call (0 on the web, where `std::time` is unavailable). +pub fn now_ms() -> f64 { + #[cfg(not(target_arch = "wasm32"))] + { + static START: std::sync::OnceLock = std::sync::OnceLock::new(); + START.get_or_init(std::time::Instant::now).elapsed().as_secs_f64() * 1000.0 + } + #[cfg(target_arch = "wasm32")] + 0.0 +} + +fn toasts(app: &mut TextureCraftApp, ctx: &egui::Context) { + if app.toasts.is_empty() { + return; + } + ctx.request_repaint_after(std::time::Duration::from_millis(250)); + egui::Area::new(egui::Id::new("toasts")).anchor(egui::Align2::RIGHT_BOTTOM, egui::vec2(-16.0, -36.0)).show(ctx, |ui| { + for t in &app.toasts { + egui::Frame::NONE + .fill(egui::Color32::from_rgb(0x30, 0x30, 0x30)) + .stroke(egui::Stroke::new(1.0, if t.error { theme::TOKENS.danger } else { theme::TOKENS.accent })) + .corner_radius(4) + .inner_margin(egui::Margin::symmetric(10, 6)) + .show(ui, |ui| { + ui.set_max_width(360.0); + ui.label(egui::RichText::new(&t.text).color(theme::TOKENS.text_strong)); + }); + ui.add_space(4.0); + } + }); +} diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs new file mode 100644 index 00000000..1184bf55 --- /dev/null +++ b/crates/ui-egui/src/menus.rs @@ -0,0 +1,286 @@ +//! The menu bar and global shortcuts. The menu catalog follows the menu layout of a standard +//! texture-painting workflow, with stable ids; an item is live when the engine (or the shell, for `ui.*` ids) implements +//! its id. The same catalog drives `docs/parity.md`. + +use egui::{Key, KeyboardShortcut, Modifiers}; +use serde_json::{Value, json}; + +use crate::TextureCraftApp; +use crate::i18n::tr; +use crate::state::{Mode, ViewLayout}; +use crate::theme::TOKENS; + +/// One menu entry. `label == "-"` is a separator; `sub` names a submenu group. +#[derive(Clone, Copy, Debug)] +pub struct Item { + pub menu: &'static str, + pub sub: Option<&'static str>, + pub label: &'static str, + pub id: &'static str, + pub shortcut: Option<&'static str>, +} + +const fn it(menu: &'static str, label: &'static str, id: &'static str, shortcut: Option<&'static str>) -> Item { + Item { menu, sub: None, label, id, shortcut } +} +const fn sub(menu: &'static str, sub: &'static str, label: &'static str, id: &'static str, shortcut: Option<&'static str>) -> Item { + Item { menu, sub: Some(sub), label, id, shortcut } +} +const fn sep(menu: &'static str) -> Item { + Item { menu, sub: None, label: "-", id: "", shortcut: None } +} + +pub const TOP: [&str; 6] = ["File", "Edit", "Mode", "Window", "Viewport", "Help"]; + +/// The menu catalog (Painter's menu tree with our ids). +pub const CATALOG: &[Item] = &[ + it("File", "New project...", "ui.newProject", Some("Ctrl+N")), + it("File", "Open...", "file.open", Some("Ctrl+O")), + it("File", "Open sample project...", "project.sample", None), + it("File", "Close", "file.close", Some("Ctrl+F4")), + sep("File"), + it("File", "Save", "file.save", Some("Ctrl+S")), + it("File", "Save as...", "file.saveAs", Some("Ctrl+Shift+S")), + it("File", "Save a copy...", "file.saveCopy", None), + it("File", "Remove unused resources...", "file.removeUnused", None), + sep("File"), + it("File", "Import resources...", "resource.import", None), + it("File", "Export Textures...", "ui.export", Some("Ctrl+Shift+E")), + it("File", "Export Mesh...", "file.exportMesh", None), + sep("File"), + it("File", "Quit", "ui.quit", Some("Ctrl+Q")), + it("Edit", "Undo", "edit.undo", Some("Ctrl+Z")), + it("Edit", "Redo", "edit.redo", Some("Ctrl+Y")), + sep("Edit"), + it("Edit", "Project Configuration...", "ui.projectConfig", None), + it("Edit", "Reimport mesh", "file.reimportMesh", Some("Ctrl+Shift+R")), + it("Edit", "Settings...", "ui.settings", None), + it("Mode", "Bake Mesh Maps", "ui.mode.bake", Some("F8")), + it("Mode", "Painting", "ui.mode.paint", Some("F9")), + it("Mode", "Rendering (path traced)", "ui.mode.render", Some("F10")), + sub("Window", "Views", "Assets", "ui.view.assets", None), + sub("Window", "Views", "Display Settings", "ui.view.display", None), + sub("Window", "Views", "History", "ui.view.history", None), + sub("Window", "Views", "Layers", "ui.view.layers", None), + sub("Window", "Views", "Log", "ui.view.log", None), + sub("Window", "Views", "Properties", "ui.view.properties", None), + sub("Window", "Views", "Shader Settings", "ui.view.shader", None), + sub("Window", "Views", "Texture Set List", "ui.view.textureSetList", None), + sub("Window", "Views", "Texture Set Settings", "ui.view.textureSetSettings", None), + sub("Window", "Save UI Layout", "Save current layout...", "ui.layout.save", None), + it("Window", "Load UI Layout", "ui.layout.load", None), + it("Window", "Hide UI", "ui.hideUi", Some("Tab")), + it("Window", "Reset UI", "ui.resetUi", None), + it("Viewport", "Display material", "viewport.displayMaterial", Some("M")), + it("Viewport", "Display next channel", "viewport.nextChannel", Some("C")), + it("Viewport", "Display previous channel", "viewport.previousChannel", Some("Shift+C")), + it("Viewport", "Display next mesh map", "viewport.nextMeshMap", Some("B")), + it("Viewport", "Display previous mesh map", "viewport.previousMeshMap", Some("Shift+B")), + it("Viewport", "UV checker", "ui.uvChecker", None), + sep("Viewport"), + it("Viewport", "Show entire mesh", "viewport.frame", Some("F")), + sep("Viewport"), + it("Viewport", "3D/2D views", "ui.layout.split", Some("F1")), + it("Viewport", "3D view only", "ui.layout.3d", Some("F2")), + it("Viewport", "2D view only", "ui.layout.2d", Some("F3")), + sep("Viewport"), + it("Viewport", "Enable quick mask", "viewport.quickMask", Some("Y")), + it("Viewport", "Edit quick mask", "viewport.quickMask.edit", Some("U")), + it("Viewport", "Invert quick mask", "viewport.quickMask.invert", Some("Ctrl+Shift+I")), + it("Help", "Documentation...", "ui.help.docs", None), + it("Help", "Shortcuts list...", "ui.help.shortcuts", None), + it("Help", "Join the community (Discord)...", "ui.help.discord", None), + it("Help", "ArtCraft website...", "ui.help.website", None), + it("Help", "Report a bug...", "ui.help.bug", None), + sep("Help"), + it("Help", "Show log...", "ui.view.log", None), + it("Help", "About TextureCraft...", "ui.about", None), +]; + +/// Ids the shell handles itself. +fn is_ui(id: &str) -> bool { + id.starts_with("ui.") +} + +/// Whether an item can run now. +pub fn enabled(app: &TextureCraftApp, id: &str) -> bool { + if is_ui(id) { + return true; + } + texturecraft_engine::find(id).is_some_and(|c| (c.enabled)(&app.session).is_ok()) +} + +/// Whether an id is implemented at all (parity report). +pub fn implemented(id: &str) -> bool { + is_ui(id) || texturecraft_engine::find(id).is_some() +} + +/// Invoke a menu item (menus, shortcuts, palette, control channel `ui.menu.invoke`). +pub fn invoke(app: &mut TextureCraftApp, ctx: &egui::Context, id: &str) { + if let Some(panel) = crate::docking::view_panel(id) { + app.ui.dock.toggle(panel, ctx.content_rect()); + return; + } + match id { + "ui.newProject" => app.ui.windows.new_project = true, + "ui.export" => app.ui.windows.export = true, + "ui.quit" => ctx.send_viewport_cmd(egui::ViewportCommand::Close), + "ui.projectConfig" => app.ui.dock.focus(crate::docking::Panel::Display, ctx.content_rect()), + "ui.settings" => app.ui.windows.settings = true, + "ui.mode.bake" => app.ui.mode = Mode::Baking, + "ui.mode.paint" => app.ui.mode = Mode::Painting, + "ui.mode.render" => app.ui.mode = Mode::Rendering, + "ui.hideUi" => app.ui.hide_ui = !app.ui.hide_ui, + "ui.resetUi" => app.ui.adopt_layout(crate::UiState::default()), + "ui.layout.save" => crate::windows::layout_file(app, true), + "ui.layout.load" => crate::windows::layout_file(app, false), + "ui.uvChecker" => { + app.run("viewport.view", json!({"mode": "uvChecker"})); + } + "ui.layout.split" => app.ui.layout = ViewLayout::Split, + "ui.layout.3d" => app.ui.layout = ViewLayout::Only3D, + "ui.layout.2d" => app.ui.layout = ViewLayout::Only2D, + "ui.help.shortcuts" => app.ui.windows.shortcuts = true, + "ui.about" => app.ui.windows.about = true, + "ui.help.discord" => ctx.open_url(egui::OpenUrl::new_tab("https://discord.gg/artcraft")), + "ui.help.website" => ctx.open_url(egui::OpenUrl::new_tab("https://getartcraft.com/apps/texturecraft")), + "ui.help.docs" => ctx.open_url(egui::OpenUrl::new_tab("https://github.com/storytold/texturecraft#readme")), + "ui.help.bug" => ctx.open_url(egui::OpenUrl::new_tab("https://github.com/storytold/texturecraft/issues")), + _ => { + if crate::windows::file_action(app, ctx, id) { + return; + } + app.run(id, Value::Null); + } + } +} + +/// Parse "Ctrl+Shift+E" style shortcuts. +pub fn parse_shortcut(s: &str) -> Option { + let mut mods = Modifiers::NONE; + let mut key = None; + for part in s.split('+') { + match part { + "Ctrl" | "Cmd" => mods |= Modifiers::COMMAND, + "Shift" => mods |= Modifiers::SHIFT, + "Alt" => mods |= Modifiers::ALT, + k => key = Key::from_name(k), + } + } + key.map(|k| KeyboardShortcut::new(mods, k)) +} + +pub fn menu_bar(app: &mut TextureCraftApp, ui: &mut egui::Ui) { + egui::Panel::top("menu_bar").exact_size(24.0).frame(egui::Frame::NONE.fill(TOKENS.chrome).inner_margin(egui::Margin::symmetric(6, 0))).show(ui, |ui| { + egui::MenuBar::new().ui(ui, |ui| { + let ctx = ui.ctx().clone(); + for top in TOP { + ui.menu_button(tr(top), |ui| { + ui.set_min_width(220.0); + let mut current_sub: Option<&str> = None; + let items: Vec = CATALOG.iter().filter(|i| i.menu == top).copied().collect(); + for item in &items { + if let Some(s) = item.sub { + if current_sub == Some(s) { + continue; + } + current_sub = Some(s); + ui.menu_button(tr(s), |ui| { + for x in items.iter().filter(|x| x.sub == Some(s)) { + menu_item(app, &ctx, ui, x); + } + }); + continue; + } + if item.label == "-" { + ui.separator(); + continue; + } + menu_item(app, &ctx, ui, item); + } + }); + } + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + let mode = match app.ui.mode { + Mode::Painting => "Painting", + Mode::Baking => "Baking", + Mode::Rendering => "Rendering", + }; + ui.label(egui::RichText::new(mode).size(11.0).color(TOKENS.text_dim)); + }); + }); + }); +} + +fn menu_item(app: &mut TextureCraftApp, ctx: &egui::Context, ui: &mut egui::Ui, item: &Item) { + let on = enabled(app, item.id); + let mut b = egui::Button::new(tr(item.label)); + if let Some(sc) = crate::keymap::shortcut_of(&app.ui.keymap, item.id) { + b = b.shortcut_text(sc); + } + let checked = match item.id { + "ui.mode.paint" => Some(app.ui.mode == Mode::Painting), + "ui.mode.bake" => Some(app.ui.mode == Mode::Baking), + "ui.mode.render" => Some(app.ui.mode == Mode::Rendering), + id => crate::docking::view_panel(id).map(|p| app.ui.dock.is_open(p)), + }; + if let Some(c) = checked { + b = b.selected(c); + } + if ui.add_enabled(on, b).clicked() { + invoke(app, ctx, item.id); + ui.close(); + } + #[cfg(not(target_arch = "wasm32"))] + if item.id == "file.open" { + ui.add_enabled_ui(!app.ui.recent.is_empty(), |ui| { + ui.menu_button("Recent projects", |ui| { + for path in app.ui.recent.clone() { + if ui.button(&path).clicked() { + crate::windows::open_path(app, &path); + ui.close(); + } + } + ui.separator(); + if ui.button("Clear recent projects").clicked() { + app.ui.recent.clear(); + ui.close(); + } + }); + }); + } +} + +/// Global shortcuts: the catalog's and every engine command's (defaults or the user's keymap), +/// plus the fixed brush keys. +pub fn shortcuts(app: &mut TextureCraftApp, ctx: &egui::Context) { + if crate::keymap::capturing(ctx) || (ctx.memory(|m| m.focused().is_some()) && ctx.egui_wants_keyboard_input()) { + return; + } + if !ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { pressed: true, .. }))) { + return; + } + let active = crate::keymap::active(&app.ui.keymap); + let mut fire: Vec<&str> = Vec::new(); + ctx.input_mut(|i| { + for (id, sc) in &active { + if i.consume_shortcut(sc) { + fire.push(*id); + } + } + // Brush size and hardness: [ and ]. + let size_down = i.consume_key(Modifiers::NONE, Key::OpenBracket); + let size_up = i.consume_key(Modifiers::NONE, Key::CloseBracket); + if size_down || size_up { + let f = if size_up { 1.15 } else { 1.0 / 1.15 }; + app.session.tool.brush.size = (app.session.tool.brush.size * f).clamp(0.0005, 0.25); + } + if i.consume_key(Modifiers::NONE, Key::Escape) { + app.session.cancel_stroke(); + app.session.tool.path.points.clear(); + } + }); + for id in fire { + invoke(app, ctx, id); + } +} diff --git a/crates/ui-egui/src/parity.rs b/crates/ui-egui/src/parity.rs new file mode 100644 index 00000000..4bfc1b6c --- /dev/null +++ b/crates/ui-egui/src/parity.rs @@ -0,0 +1,52 @@ +//! Menu parity with Painter: which catalog items are live. `cargo test -p texturecraft-ui-egui +//! parity` checks the floor; with `TC_UPDATE_PARITY=1` it rewrites `docs/parity.md`. + +use crate::menus::{CATALOG, implemented}; + +/// Live menu items must never drop below this (raise it when parity rises). +pub const FLOOR: usize = 51; + +/// (live, total) over the catalog, separators excluded. +pub fn counts() -> (usize, usize) { + let items: Vec<_> = CATALOG.iter().filter(|i| i.label != "-").collect(); + (items.iter().filter(|i| implemented(i.id)).count(), items.len()) +} + +/// The Markdown report. +pub fn report() -> String { + let (live, total) = counts(); + let mut out = String::new(); + out.push_str("# Menu parity\n\nGenerated by `TC_UPDATE_PARITY=1 cargo test -p texturecraft-ui-egui parity`. "); + out.push_str("Each row is a menu item of the target menu layout with TextureCraft's command id; "); + out.push_str("**live** means the id is implemented. Feature parity beyond menus is tracked in [`ROADMAP.md`](../ROADMAP.md).\n\n"); + out.push_str(&format!("**{live} of {total} menu items live ({:.0}%).**\n\n", live as f32 * 100.0 / total.max(1) as f32)); + for top in crate::menus::TOP { + out.push_str(&format!("## {top}\n\n| Item | Id | Shortcut | Status |\n|---|---|---|---|\n")); + for i in CATALOG.iter().filter(|i| i.menu == top && i.label != "-") { + let label = match i.sub { + Some(s) => format!("{s} ▸ {}", i.label), + None => i.label.to_string(), + }; + let status = if implemented(i.id) { "live" } else { "missing" }; + out.push_str(&format!("| {label} | `{}` | {} | {status} |\n", i.id, i.shortcut.unwrap_or(""))); + } + out.push('\n'); + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parity_floor_and_report() { + let (live, total) = counts(); + assert!(live >= FLOOR, "menu parity dropped: {live} < {FLOOR}"); + assert!(total >= live); + if std::env::var("TC_UPDATE_PARITY").is_ok() { + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/parity.md"); + std::fs::write(path, report()).unwrap(); + } + } +} diff --git a/crates/ui-egui/src/render_mode.rs b/crates/ui-egui/src/render_mode.rs new file mode 100644 index 00000000..e4122aea --- /dev/null +++ b/crates/ui-egui/src/render_mode.rs @@ -0,0 +1,274 @@ +//! Rendering mode (F10): a progressive path-traced view of the current camera. The tracer runs on +//! a worker thread (one pass per frame on the single-threaded web) and restarts whenever the +//! scene, camera, view size or settings change. + +use std::sync::Arc; +#[cfg(not(target_arch = "wasm32"))] +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Mutex, PoisonError}; + +use egui::{Color32, ColorImage, Rect, RichText, TextureHandle, TextureOptions, Ui, vec2}; +use texturecraft_engine::geom::OrbitCamera; +use texturecraft_render::trace::{TraceDisplay, TraceScene, TraceSet, TraceSettings, Tracer}; + +use crate::TextureCraftApp; +use crate::theme::TOKENS; + +/// A finished pass, ready to show. +struct Frame { + width: u32, + height: u32, + rgba: Vec, + samples: u32, +} + +/// The tracer and everything it needs. +struct Work { + scene: TraceScene, + camera: OrbitCamera, + settings: TraceSettings, + display: TraceDisplay, + max_samples: u32, + tracer: Tracer, +} + +impl Work { + fn frame(&self) -> Frame { + Frame { width: self.tracer.width, height: self.tracer.height, rgba: self.tracer.rgba8(&self.display), samples: self.tracer.samples } + } + + /// One more sample per pixel, or None when done. + #[cfg(not(target_arch = "wasm32"))] + fn step(&mut self) -> Option { + if self.tracer.samples >= self.max_samples { + return None; + } + self.tracer.pass(&self.scene, &self.camera, &self.settings); + Some(self.frame()) + } + + /// Trace for about `budget_ms` of this frame (the web has one thread: the UI must keep + /// running); a frame to show when a pass finished. + #[cfg(target_arch = "wasm32")] + fn step_for(&mut self, budget_ms: f64) -> Option { + let start = js_sys::Date::now(); + let mut rows = 4; + while self.tracer.samples < self.max_samples { + let t = js_sys::Date::now(); + if self.tracer.pass_rows(&self.scene, &self.camera, &self.settings, rows) { + return Some(self.frame()); + } + let now = js_sys::Date::now(); + if now - start >= budget_ms { + break; + } + // Grow the slice while rows are cheap, staying inside the budget. + if now - t < budget_ms / 8.0 { + rows *= 2; + } + } + None + } +} + +/// The running render of one scene state. +struct Job { + key: String, + latest: Arc>>, + started: f64, + #[cfg(not(target_arch = "wasm32"))] + cancel: Arc, + #[cfg(target_arch = "wasm32")] + work: Work, +} + +#[cfg(not(target_arch = "wasm32"))] +impl Drop for Job { + fn drop(&mut self) { + self.cancel.store(true, Ordering::Release); + } +} + +/// Rendering mode state kept by the app. +#[derive(Default)] +pub struct RenderView { + job: Option, + texture: Option, + shown: Option<(u32, u32, Vec)>, + samples: u32, + elapsed: f64, +} + +/// Viewport resolution of the render (the web renders at half size: it has one thread). +fn render_size(rect: Rect) -> (u32, u32) { + let scale = if cfg!(target_arch = "wasm32") { 0.5 } else { 1.0 }; + ((rect.width() * scale).max(16.0) as u32, (rect.height() * scale).max(16.0) as u32) +} + +fn work(app: &TextureCraftApp, size: (u32, u32)) -> Work { + let s = &app.session; + let d = &s.doc.project.display; + let sets = s + .doc + .project + .texture_sets + .iter() + .zip(&s.caches) + .map(|(ts, c)| TraceSet { material: ts.material, composite: c.composite.clone(), height_strength: ts.height_strength }) + .collect(); + let env_image = d.environment_resource().and_then(|n| s.images.get(n)); + Work { + scene: TraceScene::for_display(s.mesh.clone(), s.bvh.clone(), sets, d, env_image.map(|i| i.as_ref())), + camera: s.camera, + settings: TraceSettings { bounces: app.ui.render.bounces }, + display: TraceDisplay { denoise: app.ui.render.denoise, ..TraceDisplay::from_display(d) }, + max_samples: app.ui.render.max_samples.max(1), + tracer: Tracer::new(size.0, size.1), + } +} + +/// What the render depends on; a change restarts it. +fn key(app: &TextureCraftApp, size: (u32, u32)) -> String { + let s = &app.session; + let revisions: Vec = s.caches.iter().map(|c| c.revision).collect(); + format!("{}|{revisions:?}|{:?}|{size:?}|{:?}|{:?}", s.mesh_id, s.camera, s.doc.project.display, app.ui.render) +} + +fn start(app: &TextureCraftApp, ctx: &egui::Context, key: String, size: (u32, u32)) -> Job { + let latest = Arc::new(Mutex::new(None)); + let started = ctx.input(|i| i.time); + #[cfg(not(target_arch = "wasm32"))] + { + let cancel = Arc::new(AtomicBool::new(false)); + let mut w = work(app, size); + let (out, stop, ctx) = (latest.clone(), cancel.clone(), ctx.clone()); + let spawned = std::thread::Builder::new().name("texturecraft-render".into()).spawn(move || { + while !stop.load(Ordering::Acquire) { + let Some(frame) = w.step() else { break }; + if stop.load(Ordering::Acquire) { + break; + } + *out.lock().unwrap_or_else(PoisonError::into_inner) = Some(frame); + ctx.request_repaint(); + } + }); + if let Err(e) = spawned { + log::error!("could not start the render thread: {e}"); + } + Job { key, latest, started, cancel } + } + #[cfg(target_arch = "wasm32")] + { + let _ = ctx; + Job { key, latest, started, work: work(app, size) } + } +} + +/// Draw Rendering mode over the 3D view in `rect` (navigation is handled by the 3D view). +pub fn show(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect) { + let ctx = ui.ctx().clone(); + let size = render_size(rect); + let k = key(app, size); + if app.render.job.as_ref().is_none_or(|j| j.key != k) { + app.render.job = Some(start(app, &ctx, k, size)); + app.render.samples = 0; + } + let now = ctx.input(|i| i.time); + let mut running = false; + if let Some(job) = app.render.job.as_mut() { + #[cfg(target_arch = "wasm32")] + { + if let Some(f) = job.work.step_for(30.0) { + *job.latest.lock().unwrap_or_else(PoisonError::into_inner) = Some(f); + } + if job.work.tracer.samples < job.work.max_samples { + ctx.request_repaint(); + } + } + if let Some(f) = job.latest.lock().unwrap_or_else(PoisonError::into_inner).take() { + let img = ColorImage::from_rgba_unmultiplied([f.width as usize, f.height as usize], &f.rgba); + match app.render.texture.as_mut() { + Some(t) => t.set(img, TextureOptions::LINEAR), + None => app.render.texture = Some(ctx.load_texture("render-view", img, TextureOptions::LINEAR)), + } + app.render.samples = f.samples; + app.render.elapsed = now - job.started; + app.render.shown = Some((f.width, f.height, f.rgba)); + } + running = app.render.samples < app.ui.render.max_samples; + } + let p = ui.painter_at(rect); + match &app.render.texture { + Some(t) if app.render.samples > 0 => { + p.image(t.id(), rect, Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)), Color32::WHITE); + } + _ => { + p.rect_filled(rect, 0.0, TOKENS.viewport); + p.text(rect.center(), egui::Align2::CENTER_CENTER, "Starting render...", egui::FontId::proportional(14.0), TOKENS.text_dim); + } + } + overlay(app, ui, rect, running); +} + +/// The render status and settings card in the top-left corner. +fn overlay(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, running: bool) { + let card = Rect::from_min_size(rect.min + vec2(12.0, 12.0), vec2(250.0, 176.0)); + let mut child = ui.new_child(egui::UiBuilder::new().max_rect(card).layout(egui::Layout::top_down(egui::Align::Min))); + egui::Frame::popup(child.style()).show(&mut child, |ui| { + ui.set_width(226.0); + let max = app.ui.render.max_samples.max(1); + ui.label(RichText::new("RENDERING").size(11.0).strong()); + let status = if running { "Rendering" } else { "Done" }; + ui.label(RichText::new(format!("{status}: {} / {max} samples, {:.1} s", app.render.samples, app.render.elapsed)).size(11.0).color(TOKENS.text_dim)); + ui.add(egui::ProgressBar::new(app.render.samples as f32 / max as f32).desired_height(4.0)); + ui.horizontal(|ui| { + ui.label("Samples"); + ui.add(egui::DragValue::new(&mut app.ui.render.max_samples).range(1..=16384)); + ui.label("Bounces"); + ui.add(egui::DragValue::new(&mut app.ui.render.bounces).range(1..=32)); + }); + ui.checkbox(&mut app.ui.render.denoise, "Denoise").on_hover_text("Filter the remaining noise while keeping edges and texture detail"); + ui.horizontal(|ui| { + if ui.add_enabled(app.render.shown.is_some(), egui::Button::new("Save render...")).clicked() { + save(app); + } + if ui.button("Back to painting").clicked() { + app.ui.mode = crate::state::Mode::Painting; + } + }); + }); +} + +/// Save the shown render as PNG. +fn save(app: &mut TextureCraftApp) { + let Some((w, h, rgba)) = app.render.shown.clone() else { return }; + let Some(png) = crate::control::encode_png(w, h, rgba).map(|(_, _, png)| png) else { + app.toast("Couldn't encode the render", true); + return; + }; + let name = format!("{}_render.png", texturecraft_engine::formats::export::sanitize_file_stem(&app.session.doc.project.name)); + #[cfg(not(target_arch = "wasm32"))] + { + let Some(path) = rfd::FileDialog::new().add_filter("PNG image", &["png"]).set_file_name(name).save_file() else { return }; + match std::fs::write(&path, png) { + Ok(()) => app.toast(format!("Saved {}", path.display()), false), + Err(e) => app.toast(format!("Couldn't save the render: {e}"), true), + } + } + #[cfg(target_arch = "wasm32")] + match crate::web_files::download(&name, &png) { + Ok(()) => app.toast(format!("Downloaded {name}"), false), + Err(e) => app.toast(format!("Couldn't save the render: {e}"), true), + } +} + +impl RenderView { + pub fn is_active(&self) -> bool { + self.job.is_some() || self.texture.is_some() + } +} + +/// Stop rendering (leaving Rendering mode frees the worker and the image). +pub fn stop(app: &mut TextureCraftApp) { + app.render = RenderView::default(); +} diff --git a/crates/ui-egui/src/shelf.rs b/crates/ui-egui/src/shelf.rs new file mode 100644 index 00000000..650e462f --- /dev/null +++ b/crates/ui-egui/src/shelf.rs @@ -0,0 +1,266 @@ +//! The asset shelf (the Assets dock tab): Painter-style tabs (materials, smart materials, smart +//! masks, procedurals, environments, textures), all built in and procedural. Search filters +//! across every tab. + +use egui::{Color32, RichText, Sense, Ui, vec2}; +use egui_phosphor::regular as ic; +use serde_json::json; +use texturecraft_engine::core::ProceduralKind; +use texturecraft_engine::materials::MATERIALS; +use texturecraft_engine::smart_masks::SMART_MASKS; +use texturecraft_render::ibl::ENVIRONMENTS; + +use crate::TextureCraftApp; +use crate::theme::TOKENS; +use crate::widgets::icon_button; + +#[derive(Clone, Copy, PartialEq, Eq)] +enum Tab { + Materials, + Smart, + Masks, + Procedurals, + Environments, + Textures, +} + +const TABS: [(Tab, &str, &str); 6] = [ + (Tab::Materials, ic::SPHERE, "Materials"), + (Tab::Smart, ic::STACK, "Smart materials"), + (Tab::Masks, ic::CIRCLE_HALF, "Smart masks"), + (Tab::Procedurals, ic::CHECKERBOARD, "Procedurals"), + (Tab::Environments, ic::SUN_HORIZON, "Environments"), + (Tab::Textures, ic::IMAGE, "Textures"), +]; + +/// The Assets panel body (a dock tab). +pub fn body(app: &mut TextureCraftApp, ui: &mut Ui) { + let ctx = ui.ctx().clone(); + let tab_id = egui::Id::new("shelf_tab"); + let mut tab = ctx.data_mut(|d| *d.get_persisted_mut_or(tab_id, 0usize)).min(TABS.len() - 1); + let searching = !app.ui.shelf_filter.is_empty(); + ui.horizontal_wrapped(|ui| { + ui.spacing_mut().item_spacing = vec2(2.0, 2.0); + for (i, (_, icon, label)) in TABS.iter().enumerate() { + if icon_button(ui, icon, label, i == tab && !searching, 24.0).clicked() { + tab = i; + app.ui.shelf_filter.clear(); + } + } + }); + ctx.data_mut(|d| d.insert_persisted(tab_id, tab)); + ui.horizontal(|ui| { + ui.label(RichText::new(ic::MAGNIFYING_GLASS).color(TOKENS.text_dim)); + ui.add(egui::TextEdit::singleline(&mut app.ui.shelf_filter).hint_text("Search all assets").desired_width(f32::INFINITY)); + }); + ui.add_space(4.0); + let filter = app.ui.shelf_filter.to_lowercase(); + egui::ScrollArea::vertical().id_salt("shelf_scroll").auto_shrink([false, false]).show(ui, |ui| { + // Searching shows matches from every tab, each under its tab's name. + for (i, (t, _, title)) in TABS.iter().enumerate() { + if filter.is_empty() && i != tab { + continue; + } + match t { + Tab::Materials => materials(app, ui, &ctx, &filter, false, title), + Tab::Smart => { + materials(app, ui, &ctx, &filter, true, title); + library(app, ui, &filter); + } + Tab::Masks => smart_masks(app, ui, &ctx, &filter, title), + Tab::Procedurals => procedurals(app, ui, &ctx, &filter, title), + Tab::Environments => environments(app, ui, &filter, title), + Tab::Textures => textures(app, ui, &ctx, &filter, title), + } + } + }); +} + +fn matches(filter: &str, texts: &[&str]) -> bool { + filter.is_empty() || texts.iter().any(|t| t.to_lowercase().contains(filter)) +} + +fn title(ui: &mut Ui, text: &str) { + ui.label(RichText::new(text).strong().color(TOKENS.text_strong)); +} + +fn header(ui: &mut Ui, text: &str) { + ui.add_space(2.0); + ui.label(RichText::new(text).size(11.0).color(TOKENS.text_dim)); +} + +fn tiles(ui: &mut Ui, body: impl FnOnce(&mut Ui)) { + ui.horizontal_wrapped(|ui| { + ui.spacing_mut().item_spacing = vec2(4.0, 4.0); + body(ui); + }); + ui.add_space(6.0); +} + +/// Built-in materials (or smart materials), grouped by category. +fn materials(app: &mut TextureCraftApp, ui: &mut Ui, ctx: &egui::Context, filter: &str, smart: bool, name: &str) { + let shown: Vec<_> = MATERIALS.iter().filter(|m| m.smart == smart && matches(filter, &[m.label, m.category, m.id])).collect(); + if shown.is_empty() { + return; + } + title(ui, name); + let mut categories: Vec<&str> = Vec::new(); + for m in &shown { + if !categories.contains(&m.category) { + categories.push(m.category); + } + } + for cat in categories { + header(ui, cat); + tiles(ui, |ui| { + for m in shown.iter().filter(|m| m.category == cat) { + let tex = app.thumbs.material(ctx, m.id); + let hint = if m.smart { + "Add the smart material folder above the selected layer" + } else { + "Add a fill layer with this material above the selected layer" + }; + if tile(ui, tex.as_ref(), m.label, hint).clicked() { + app.run("material.apply", json!({"material": m.id})); + } + } + }); + } +} + +/// The user's saved smart materials (desktop library folder). +#[cfg(not(target_arch = "wasm32"))] +fn library(app: &mut TextureCraftApp, ui: &mut Ui, filter: &str) { + if app.library.is_none() { + let mut files: Vec = std::fs::read_dir(crate::library_dir()) + .map(|d| d.flatten().map(|e| e.path()).filter(|p| p.extension().is_some_and(|x| x == "tcmat")).collect()) + .unwrap_or_default(); + files.sort(); + app.library = Some(files); + } + let files = app.library.clone().unwrap_or_default(); + let name = |f: &std::path::PathBuf| f.file_stem().and_then(|s| s.to_str()).unwrap_or("material").to_string(); + let shown: Vec<_> = files.iter().filter(|f| matches(filter, &[&name(f)])).collect(); + if !filter.is_empty() && shown.is_empty() { + return; + } + ui.horizontal(|ui| { + header(ui, "My smart materials"); + if ui.small_button(ic::ARROW_CLOCKWISE).on_hover_text("Rescan the library folder").clicked() { + app.library = None; + } + }); + if files.is_empty() { + ui.label(RichText::new("Right-click a layer > Create smart material").size(10.0).color(TOKENS.text_dim)); + } + for f in shown { + if ui.selectable_label(false, format!("{} {}", ic::SPHERE, name(f))).on_hover_text(f.display().to_string()).clicked() { + app.run("material.load", json!({"path": f.display().to_string()})); + } + } + ui.add_space(6.0); +} + +#[cfg(target_arch = "wasm32")] +fn library(_: &mut TextureCraftApp, _: &mut Ui, _: &str) {} + +fn smart_masks(app: &mut TextureCraftApp, ui: &mut Ui, ctx: &egui::Context, filter: &str, name: &str) { + let shown: Vec<_> = SMART_MASKS.iter().filter(|m| matches(filter, &[m.label, m.id])).collect(); + if shown.is_empty() { + return; + } + title(ui, name); + tiles(ui, |ui| { + for m in shown { + let tex = app.thumbs.smart_mask(ctx, m.id); + if tile(ui, tex.as_ref(), m.label, "Replace the selected layer's mask with this smart mask").clicked() { + app.run("mask.smart.apply", json!({"name": m.id})); + } + } + }); +} + +fn procedurals(app: &mut TextureCraftApp, ui: &mut Ui, ctx: &egui::Context, filter: &str, name: &str) { + let shown: Vec = ProceduralKind::ALL.into_iter().filter(|k| matches(filter, &[k.label()])).collect(); + if shown.is_empty() { + return; + } + title(ui, name); + tiles(ui, |ui| { + for k in shown { + let tex = app.thumbs.procedural(ctx, k); + if tile(ui, tex.as_ref(), k.label(), "Add a fill layer using this pattern").clicked() { + app.run("layer.add.fill", json!({"name": k.label()})); + app.run("layer.fill.set", json!({"channel": "baseColor", "procedural": k.label(), "low": [0.1, 0.1, 0.1], "high": [0.9, 0.9, 0.9]})); + } + } + }); +} + +fn environments(app: &mut TextureCraftApp, ui: &mut Ui, filter: &str, name: &str) { + let shown: Vec<&&str> = ENVIRONMENTS.iter().filter(|e| matches(filter, &[e])).collect(); + if shown.is_empty() { + return; + } + title(ui, name); + for env in shown { + let on = app.session.doc.project.display.environment == **env; + if ui.selectable_label(on, format!("{} {env}", ic::SUN_HORIZON)).clicked() { + app.run("display.set", json!({"environment": env})); + } + } + ui.add_space(6.0); +} + +fn textures(app: &mut TextureCraftApp, ui: &mut Ui, ctx: &egui::Context, filter: &str, name: &str) { + let names: Vec = app.session.doc.resources.keys().filter(|n| matches(filter, &[n])).cloned().collect(); + if !filter.is_empty() && names.is_empty() { + return; + } + ui.horizontal(|ui| { + title(ui, name); + if ui.small_button("Import...").clicked() { + crate::menus::invoke(app, ctx, "resource.import"); + } + }); + if names.is_empty() { + ui.label(RichText::new("Import images to use them in fills and masks").size(10.0).color(TOKENS.text_dim)); + } + tiles(ui, |ui| { + for name in names { + let Some(img) = app.session.images.get(&name).cloned() else { continue }; + let tex = app.thumbs.image(ctx, &name, &img); + let r = tile(ui, Some(&tex), &name, "Add a fill layer using this image"); + if r.clicked() { + app.run("layer.add.image", json!({"name": name})); + } + r.context_menu(|ui| { + if ui.button("Remove resource").clicked() { + app.run("resource.remove", json!({"name": name})); + ui.close(); + } + }); + } + }); +} + +/// A shelf tile: thumbnail (a placeholder disc while it renders) and a wrapped label. +fn tile(ui: &mut Ui, tex: Option<&egui::TextureHandle>, label: &str, hint: &str) -> egui::Response { + let (rect, resp) = ui.allocate_exact_size(vec2(72.0, 86.0), Sense::click()); + let p = ui.painter(); + if resp.hovered() { + p.rect_filled(rect, 3.0, TOKENS.row_hover); + } + let img = egui::Rect::from_center_size(rect.center_top() + vec2(0.0, 32.0), vec2(56.0, 56.0)); + match tex { + Some(t) => { + p.image(t.id(), img, egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)), Color32::WHITE); + } + None => { + p.circle_filled(img.center(), img.width() * 0.45, TOKENS.row_hover); + } + } + let galley = p.layout(label.to_string(), egui::FontId::proportional(9.5), TOKENS.text, rect.width() - 4.0); + p.galley(egui::pos2(rect.center().x - galley.size().x * 0.5, img.bottom() + 3.0), galley, TOKENS.text); + resp.on_hover_text(format!("{label}: {hint}")) +} diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs new file mode 100644 index 00000000..2d67c00b --- /dev/null +++ b/crates/ui-egui/src/state.rs @@ -0,0 +1,250 @@ +//! UI state (serde, so the control channel can read and drive it). + +use serde::{Deserialize, Serialize}; +use texturecraft_engine::core::Channel; + +use crate::docking::Dock; +use crate::keymap::Keymap; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum Mode { + #[default] + Painting, + Baking, + /// Path-traced view (F10). + Rendering, +} + +/// Rendering mode settings. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default)] +pub struct RenderUi { + pub max_samples: u32, + pub bounces: u32, + pub denoise: bool, +} + +impl Default for RenderUi { + fn default() -> Self { + // The web traces on one thread: fewer bounces keep it interactive. + RenderUi { max_samples: 256, bounces: if cfg!(target_arch = "wasm32") { 3 } else { 6 }, denoise: true } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum ViewLayout { + /// 3D and 2D side by side. + #[default] + Split, + Only3D, + Only2D, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum PropsTab { + #[default] + Brush, + Alpha, + Material, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)] +pub struct Windows { + pub export: bool, + pub new_project: bool, + pub about: bool, + pub shortcuts: bool, + #[serde(default)] + pub settings: bool, +} + +/// Edit › Settings. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default)] +pub struct Prefs { + /// Interface zoom (1 = 100%). + pub ui_scale: f32, + /// Minutes between autosaves of a modified project (0: off; desktop). + pub autosave_minutes: u32, + /// UI language code (`i18n::LANGUAGES`). + pub language: String, +} + +impl Default for Prefs { + fn default() -> Self { + Prefs { ui_scale: 1.0, autosave_minutes: 10, language: "en".into() } + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct NewProject { + pub mesh: String, + pub resolution: u32, + pub name: String, + /// A mesh file chosen from disk (desktop only). + pub file: Option, +} + +impl Default for NewProject { + fn default() -> Self { + NewProject { mesh: "material_ball".into(), resolution: 2048, name: "Untitled".into(), file: None } + } +} + +/// The export dialog's settings (remembered between sessions). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct ExportUi { + pub dir: String, + pub preset: String, + /// File extension override ("" = the preset's formats). + pub format: String, + /// Bit depth override (0 = the preset's). + pub bit_depth: u8, + /// none | dilation | dilationDefault | infinite + pub padding: String, + pub dilation: u32, + /// Output size override (0 = each texture set's size). + pub size: u32, + /// "" (project), openGl or directX. + pub normal_format: String, + /// Texture sets excluded from export. + pub skip: Vec, + pub tab: u8, +} + +impl Default for ExportUi { + fn default() -> Self { + ExportUi { + dir: default_export_dir(), + preset: "Document channels + Normal + AO".into(), + format: String::new(), + bit_depth: 0, + padding: "infinite".into(), + dilation: 16, + size: 0, + normal_format: String::new(), + skip: Vec::new(), + tab: 0, + } + } +} + +fn default_export_dir() -> String { + #[cfg(not(target_arch = "wasm32"))] + { + crate::user_dir("export").display().to_string() + } + #[cfg(target_arch = "wasm32")] + { + String::new() + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct UiState { + pub mode: Mode, + pub layout: ViewLayout, + /// The docked, tabbed and floating panels around the viewport. + #[serde(default)] + pub dock: Dock, + /// Window ▸ Hide UI (Tab): only the viewport. + pub hide_ui: bool, + pub windows: Windows, + pub uv_zoom: f32, + pub uv_pan: [f32; 2], + /// Channel whose blending the layer rows show. + pub layer_channel: Channel, + pub shelf_filter: String, + pub props_tab: PropsTab, + /// Layer being renamed and the edit buffer. + pub rename: Option<(u64, String)>, + pub new_project: NewProject, + /// Show the UV wireframe in the 2D view. + pub uv_wireframe: bool, + /// Show the mesh wireframe over the 3D view. + #[serde(default)] + pub wireframe_3d: bool, + #[serde(default)] + pub render: RenderUi, + #[serde(default)] + pub prefs: Prefs, + #[serde(default)] + pub export: ExportUi, + /// Recently opened or saved projects (desktop). + #[serde(default)] + pub recent: Vec, + /// Custom keyboard shortcuts (item id → "Ctrl+Shift+E", "" = none) over the defaults. + #[serde(default)] + pub keymap: Keymap, +} + +impl UiState { + /// Take `layout`'s panels, windows and view settings, keeping this state's dialogs, export + /// settings and recent files. + pub fn adopt_layout(&mut self, layout: UiState) { + let keep = std::mem::replace(self, layout); + self.new_project = keep.new_project; + self.export = keep.export; + self.recent = keep.recent; + self.shelf_filter = keep.shelf_filter; + self.prefs = keep.prefs; + self.keymap = keep.keymap; + self.rename = None; + } +} + +impl Default for UiState { + fn default() -> Self { + UiState { + mode: Mode::Painting, + layout: ViewLayout::Split, + dock: Dock::default(), + hide_ui: false, + windows: Windows::default(), + uv_zoom: 1.0, + uv_pan: [0.0, 0.0], + layer_channel: Channel::BaseColor, + shelf_filter: String::new(), + props_tab: PropsTab::Brush, + rename: None, + new_project: NewProject::default(), + uv_wireframe: true, + wireframe_3d: false, + render: RenderUi::default(), + prefs: Prefs::default(), + export: ExportUi::default(), + recent: Vec::new(), + keymap: Keymap::new(), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn adopting_a_layout_keeps_recent_files_export_settings_and_shortcuts() { + let mut ui = UiState { recent: vec!["a.tcraft".into()], ..UiState::default() }; + ui.export.preset = "Mine".into(); + ui.keymap.insert("file.save".into(), "F5".into()); + let mut layout = UiState::default(); + layout.dock.close(crate::docking::Panel::Assets); + ui.adopt_layout(layout); + assert!(!ui.dock.is_open(crate::docking::Panel::Assets)); + assert_eq!(ui.recent, vec!["a.tcraft".to_string()]); + assert_eq!(ui.export.preset, "Mine"); + assert_eq!(ui.keymap.get("file.save").map(String::as_str), Some("F5")); + } + + #[test] + fn older_saved_state_without_a_dock_loads_the_default_layout() { + let mut v = serde_json::to_value(UiState::default()).unwrap(); + let obj = v.as_object_mut().unwrap(); + obj.remove("dock"); + obj.remove("keymap"); + obj.insert("show_shelf".into(), serde_json::json!(false)); + let ui: UiState = serde_json::from_value(v).unwrap(); + assert_eq!(ui.dock, Dock::default()); + } +} diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs new file mode 100644 index 00000000..3ea09fe0 --- /dev/null +++ b/crates/ui-egui/src/theme.rs @@ -0,0 +1,113 @@ +//! Design tokens and egui styling: a dense, very dark professional theme (our own palette and +//! widgets; no third-party assets). + +use egui::{Color32, CornerRadius, FontFamily, FontId, Stroke, TextStyle, Visuals}; + +/// Colors and metrics used across the UI. Never hard-code colors elsewhere. +#[derive(Clone, Copy, Debug)] +pub struct Tokens { + /// Menu bar, title strip, tool shelves. + pub chrome: Color32, + /// Dock panel backgrounds. + pub panel: Color32, + /// Panel title rows ("LAYERS", "PROPERTIES"). + pub header: Color32, + /// List rows (layers, texture sets). + pub row: Color32, + pub row_hover: Color32, + pub row_selected: Color32, + /// Separators between panels. + pub separator: Color32, + pub text: Color32, + pub text_dim: Color32, + pub text_strong: Color32, + /// Selection and focus. + pub accent: Color32, + pub accent_dim: Color32, + pub field: Color32, + pub danger: Color32, + pub viewport: Color32, + pub radius: u8, +} + +pub const TOKENS: Tokens = Tokens { + chrome: Color32::from_rgb(0x1c, 0x1c, 0x1c), + panel: Color32::from_rgb(0x24, 0x24, 0x24), + header: Color32::from_rgb(0x24, 0x24, 0x24), + row: Color32::from_rgb(0x2a, 0x2a, 0x2a), + row_hover: Color32::from_rgb(0x33, 0x33, 0x33), + row_selected: Color32::from_rgb(0x2c, 0x34, 0x40), + separator: Color32::from_rgb(0x14, 0x14, 0x14), + text: Color32::from_rgb(0xd4, 0xd4, 0xd4), + text_dim: Color32::from_rgb(0x96, 0x96, 0x96), + text_strong: Color32::from_rgb(0xf2, 0xf2, 0xf2), + accent: Color32::from_rgb(0x2f, 0x86, 0xeb), + accent_dim: Color32::from_rgb(0x1f, 0x4f, 0x8a), + field: Color32::from_rgb(0x1a, 0x1a, 0x1a), + danger: Color32::from_rgb(0xe0, 0x55, 0x4a), + viewport: Color32::from_rgb(0x2a, 0x2a, 0x2a), + radius: 3, +}; + +/// Install fonts (egui defaults + the Phosphor icon font) and the dark style. +pub fn install(ctx: &egui::Context) { + let mut fonts = egui::FontDefinitions::default(); + egui_phosphor::add_to_fonts(&mut fonts, egui_phosphor::Variant::Regular); + ctx.set_fonts(fonts); + let t = TOKENS; + ctx.all_styles_mut(|s| { + s.text_styles = [ + (TextStyle::Small, FontId::new(10.0, FontFamily::Proportional)), + (TextStyle::Body, FontId::new(12.0, FontFamily::Proportional)), + (TextStyle::Button, FontId::new(12.0, FontFamily::Proportional)), + (TextStyle::Heading, FontId::new(14.0, FontFamily::Proportional)), + (TextStyle::Monospace, FontId::new(11.5, FontFamily::Monospace)), + ] + .into(); + s.spacing.item_spacing = egui::vec2(6.0, 4.0); + s.spacing.button_padding = egui::vec2(6.0, 2.0); + s.spacing.interact_size = egui::vec2(36.0, 20.0); + s.spacing.slider_width = 120.0; + s.spacing.menu_margin = egui::Margin::same(4); + s.spacing.window_margin = egui::Margin::same(8); + let mut v = Visuals::dark(); + v.panel_fill = t.panel; + v.window_fill = t.panel; + v.extreme_bg_color = t.field; + v.faint_bg_color = t.row; + v.code_bg_color = t.field; + v.window_stroke = Stroke::new(1.0, Color32::from_rgb(0x3a, 0x3a, 0x3a)); + v.window_corner_radius = CornerRadius::same(4); + v.menu_corner_radius = CornerRadius::same(3); + v.selection.bg_fill = t.accent_dim; + v.selection.stroke = Stroke::new(1.0, t.accent); + v.hyperlink_color = t.accent; + let r = CornerRadius::same(t.radius); + v.widgets.noninteractive.bg_fill = t.panel; + v.widgets.noninteractive.fg_stroke = Stroke::new(1.0, t.text); + v.widgets.noninteractive.bg_stroke = Stroke::new(1.0, t.separator); + v.widgets.inactive.bg_fill = Color32::from_rgb(0x33, 0x33, 0x33); + v.widgets.inactive.weak_bg_fill = Color32::from_rgb(0x30, 0x30, 0x30); + v.widgets.inactive.fg_stroke = Stroke::new(1.0, t.text); + v.widgets.inactive.corner_radius = r; + v.widgets.hovered.bg_fill = Color32::from_rgb(0x40, 0x40, 0x40); + v.widgets.hovered.weak_bg_fill = Color32::from_rgb(0x3b, 0x3b, 0x3b); + v.widgets.hovered.fg_stroke = Stroke::new(1.0, t.text_strong); + v.widgets.hovered.bg_stroke = Stroke::new(1.0, Color32::from_rgb(0x55, 0x55, 0x55)); + v.widgets.hovered.corner_radius = r; + v.widgets.active.bg_fill = Color32::from_rgb(0x4a, 0x4a, 0x4a); + v.widgets.active.weak_bg_fill = Color32::from_rgb(0x45, 0x45, 0x45); + v.widgets.active.fg_stroke = Stroke::new(1.0, Color32::WHITE); + v.widgets.active.corner_radius = r; + v.widgets.open.bg_fill = Color32::from_rgb(0x3a, 0x3a, 0x3a); + v.widgets.open.corner_radius = r; + v.slider_trailing_fill = true; + v.striped = false; + s.visuals = v; + }); +} + +/// Painter-style uppercase panel caption. +pub fn caption(text: &str) -> egui::RichText { + egui::RichText::new(text.to_uppercase()).size(10.5).strong().color(TOKENS.text_strong) +} diff --git a/crates/ui-egui/src/thumbs.rs b/crates/ui-egui/src/thumbs.rs new file mode 100644 index 00000000..70ff7fd6 --- /dev/null +++ b/crates/ui-egui/src/thumbs.rs @@ -0,0 +1,236 @@ +//! Thumbnails: shaded material spheres, smart mask spheres, procedural swatches and brush alpha +//! previews, rendered on the CPU into cached egui textures (no image assets). New shelf +//! thumbnails are rendered lazily, a few per frame, so opening the shelf never stalls a frame. + +use std::collections::HashMap; + +use egui::{Color32, ColorImage, Painter, Pos2, Rect, TextureHandle, TextureOptions}; +use texturecraft_engine::core::ProceduralKind; +use texturecraft_engine::paint::BrushAlpha; +use texturecraft_engine::preview::{self, Sample}; +use texturecraft_engine::texel::linear_to_srgb; + +/// Shelf thumbnails rendered per frame at most (the rest follow on the next frames). +const PER_FRAME: usize = 2; +/// Shelf sphere thumbnail size in pixels. +const SPHERE_PX: usize = 72; + +#[derive(Default)] +pub struct Thumbs { + /// Shelf thumbnails by key (`mat:`, `mask:`, `proc:`). + shelf: HashMap, + images: HashMap, + /// (frame, thumbnails rendered in it). + budget: (u64, usize), +} + +fn shade(n: [f32; 3], base: [f32; 3], metal: f32, rough: f32) -> [f32; 3] { + let l = [0.45f32, 0.7, 0.55]; + let len = (l[0] * l[0] + l[1] * l[1] + l[2] * l[2]).sqrt(); + let l = l.map(|v| v / len); + let ndl = (n[0] * l[0] + n[1] * l[1] + n[2] * l[2]).max(0.0); + let ndv = n[2].max(0.0); + // View along +Z: half vector between light and view. + let h = [l[0], l[1], l[2] + 1.0]; + let hl = (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]).sqrt(); + let ndh = ((n[0] * h[0] + n[1] * h[1] + n[2] * h[2]) / hl).max(0.0); + let shin = 2.0 / (rough * rough * rough * rough).max(1e-3) - 2.0; + let spec_amt = ndh.powf(shin.clamp(1.0, 4096.0)) * (shin + 2.0) / 8.0; + // A studio environment seen in the mirror direction: dark floor, bright sky, a horizon that + // blurs (and the whole environment averages out) as roughness grows. + let ry = 2.0 * ndv * n[1]; + let w = 0.06 + rough * 0.9; + let t = ((ry + w) / (2.0 * w)).clamp(0.0, 1.0); + let sharp = 0.04 + 0.03 * (ry + 1.0) + (0.2 + 0.55 * ry.max(0.0)) * t * t * (3.0 - 2.0 * t); + let env = sharp + (0.28 - sharp) * (rough * rough); + let fres = (1.0 - ndv).powi(5); + let sky = 0.35 + 0.25 * n[1]; + let mut out = [0.0; 3]; + for k in 0..3 { + let f0 = 0.04 + (base[k] - 0.04) * metal; + let f = f0 + ((1.0 - rough).max(f0) - f0) * fres; + let diffuse = base[k] * (1.0 - metal) * (ndl * 0.85 + sky * 0.5); + out[k] = diffuse * (1.0 - f) + f * env + f0 * spec_amt * ndl * 1.5; + } + out +} + +fn aces(x: f32) -> f32 { + let v = x * 0.6; + ((v * (2.51 * v + 0.03)) / (v * (2.43 * v + 0.59) + 0.14)).clamp(0.0, 1.0) +} + +/// A shaded sphere image (sRGB, transparent outside) from per-pixel preview samples (row-major +/// `size²`, see [`preview::sphere_normals`]); height bumps the normals. +pub fn sphere_image(size: usize, samples: &[Option]) -> ColorImage { + let mut img = ColorImage::filled([size, size], Color32::TRANSPARENT); + let r = (size as f32 * 0.5 - 1.0).max(0.5); + let h = |x: Option, y: Option| { + let (x, y) = (x.filter(|x| *x < size)?, y?); + samples.get(y.checked_mul(size)?.checked_add(x)?).copied().flatten().map(|s| s.height) + }; + for (i, n) in preview::sphere_normals(size).enumerate() { + let (Some(n), Some(Some(s))) = (n, samples.get(i)) else { continue }; + let (x, y) = (i % size, i / size); + let c = s.height; + // Central differences (image y points down). + let dx = h(Some(x + 1), Some(y)).unwrap_or(c) - h(x.checked_sub(1), Some(y)).unwrap_or(c); + let dy = h(Some(x), y.checked_sub(1)).unwrap_or(c) - h(Some(x), Some(y + 1)).unwrap_or(c); + let k = r * 0.5 * 1.5; + let b = [n.x - dx * k, n.y - dy * k, n.z]; + let len = (b[0] * b[0] + b[1] * b[1] + b[2] * b[2]).sqrt().max(1e-6); + let col = shade(b.map(|v| v / len), s.base, s.metallic, s.roughness).map(|v| (linear_to_srgb(aces(v * 1.4)) * 255.0 + 0.5) as u8); + let edge = ((1.0 - (n.x * n.x + n.y * n.y).sqrt()) * r).clamp(0.0, 1.0); + img.pixels[i] = Color32::from_rgba_unmultiplied(col[0], col[1], col[2], (edge * 255.0) as u8); + } + img +} + +impl Thumbs { + /// A larger cached texture of an image resource for the stencil overlay (up to 1024 px). + pub fn stencil(&mut self, ctx: &egui::Context, name: &str, img: &texturecraft_engine::compose::ImageRes) -> TextureHandle { + let key = format!("stencil:{name}"); + if let Some(t) = self.images.get(&key) { + return t.clone(); + } + let scale = (1024.0 / img.width.max(img.height).max(1) as f32).min(1.0); + let (w, h) = (((img.width as f32 * scale) as usize).max(1), ((img.height as f32 * scale) as usize).max(1)); + let mut ci = ColorImage::filled([w, h], Color32::TRANSPARENT); + for y in 0..h { + for x in 0..w { + let s = img.sample((x as f32 + 0.5) / w as f32, (y as f32 + 0.5) / h as f32); + let enc = |v: f32| if img.srgb { (v.clamp(0.0, 1.0) * 255.0) as u8 } else { (linear_to_srgb(v) * 255.0) as u8 }; + ci.pixels[y * w + x] = Color32::from_rgba_unmultiplied(enc(s[0]), enc(s[1]), enc(s[2]), (s[3].clamp(0.0, 1.0) * 255.0) as u8); + } + } + let t = ctx.load_texture(key.clone(), ci, TextureOptions::LINEAR); + self.images.insert(key, t.clone()); + t + } + + /// Cached preview of an image resource (64 px, nearest downsample). + pub fn image(&mut self, ctx: &egui::Context, name: &str, img: &texturecraft_engine::compose::ImageRes) -> TextureHandle { + if let Some(t) = self.images.get(name) { + return t.clone(); + } + let n = 64usize; + let mut ci = ColorImage::filled([n, n], Color32::BLACK); + for y in 0..n { + for x in 0..n { + let s = img.sample((x as f32 + 0.5) / n as f32, (y as f32 + 0.5) / n as f32); + let enc = |v: f32| if img.srgb { (v.clamp(0.0, 1.0) * 255.0) as u8 } else { (linear_to_srgb(v) * 255.0) as u8 }; + ci.pixels[y * n + x] = Color32::from_rgb(enc(s[0]), enc(s[1]), enc(s[2])); + } + } + let t = ctx.load_texture(format!("res_{name}"), ci, TextureOptions::LINEAR); + self.images.insert(name.to_string(), t.clone()); + t + } + + /// A cached shelf thumbnail, rendering it with `render` if this frame's budget allows (None + /// until then; a repaint is requested so it follows shortly). + fn shelf(&mut self, ctx: &egui::Context, key: String, render: impl FnOnce() -> ColorImage) -> Option { + if let Some(t) = self.shelf.get(&key) { + return Some(t.clone()); + } + let frame = ctx.cumulative_pass_nr(); + if self.budget.0 != frame { + self.budget = (frame, 0); + } + if self.budget.1 >= PER_FRAME { + ctx.request_repaint(); + return None; + } + self.budget.1 += 1; + let t = ctx.load_texture(format!("thumb_{key}"), render(), TextureOptions::LINEAR); + self.shelf.insert(key, t.clone()); + Some(t) + } + + /// Sphere thumbnail of built-in material `id`. + pub fn material(&mut self, ctx: &egui::Context, id: &str) -> Option { + self.shelf(ctx, format!("mat:{id}"), || sphere_image(SPHERE_PX, &preview::material(id, SPHERE_PX).unwrap_or_default())) + } + + /// Sphere thumbnail of smart mask `id`: white where the mask applies, over dark gray. + pub fn smart_mask(&mut self, ctx: &egui::Context, id: &str) -> Option { + self.shelf(ctx, format!("mask:{id}"), || { + let px = preview::smart_mask(id, SPHERE_PX).unwrap_or_default(); + let samples: Vec> = + px.iter().map(|m| m.map(|m| Sample { base: [0.04 + 0.76 * m; 3], metallic: 0.0, roughness: 0.6, height: 0.0 })).collect(); + sphere_image(SPHERE_PX, &samples) + }) + } + + /// Grayscale swatch of procedural `k`. + pub fn procedural(&mut self, ctx: &egui::Context, k: ProceduralKind) -> Option { + use texturecraft_engine::compose::pattern::{Coords, procedural}; + use texturecraft_engine::core::{Procedural, Projection}; + use texturecraft_engine::geom::glam::{Vec2, Vec3}; + self.shelf(ctx, format!("proc:{k:?}"), || { + let n = 56usize; + let pr = Procedural { scale: 3.0, ..Procedural::new(k) }; + let mut img = ColorImage::filled([n, n], Color32::BLACK); + for (i, px) in img.pixels.iter_mut().enumerate() { + let uv = Vec2::new(((i % n) as f32 + 0.5) / n as f32, ((i / n) as f32 + 0.5) / n as f32); + let v = procedural(&pr, &Projection::default(), &Coords { uv, pos: Vec3::ZERO, nrm: Vec3::Y }); + *px = Color32::from_gray((v.clamp(0.0, 1.0) * 255.0) as u8); + } + img + }) + } +} + +/// A small shaded sphere drawn with a vertex-colored mesh (layer thumbnails, previews). +pub fn paint_sphere(p: &Painter, c: Pos2, r: f32, base: [f32; 3]) { + let mut mesh = egui::Mesh::default(); + let rings = 10; + let segs = 28; + let color = |dx: f32, dy: f32| { + let d2 = (dx * dx + dy * dy).min(1.0); + let n = [dx, dy, (1.0 - d2).sqrt()]; + let s = shade(n, base, 0.0, 0.5).map(|v| (linear_to_srgb(aces(v * 1.4)) * 255.0) as u8); + Color32::from_rgb(s[0], s[1], s[2]) + }; + mesh.colored_vertex(c, color(0.0, 0.0)); + for i in 1..=rings { + let rr = i as f32 / rings as f32; + for j in 0..segs { + let a = j as f32 / segs as f32 * std::f32::consts::TAU; + let (dx, dy) = (a.cos() * rr, a.sin() * rr); + mesh.colored_vertex(Pos2::new(c.x + dx * r, c.y - dy * r), color(dx, dy)); + } + } + for j in 0..segs { + let (a, b) = (1 + j, 1 + (j + 1) % segs); + mesh.add_triangle(0, a as u32, b as u32); + } + for i in 1..rings { + let r0 = 1 + (i - 1) * segs; + let r1 = 1 + i * segs; + for j in 0..segs { + let (a, b) = (r0 + j, r0 + (j + 1) % segs); + let (c2, d) = (r1 + j, r1 + (j + 1) % segs); + mesh.add_triangle(a as u32, c2 as u32, d as u32); + mesh.add_triangle(a as u32, d as u32, b as u32); + } + } + p.add(egui::Shape::mesh(mesh)); +} + +/// Brush alpha preview drawn as small cells (cheap, no texture). +pub fn paint_alpha(p: &Painter, rect: Rect, a: BrushAlpha, hardness: f32) { + let n = 20; + let cell = rect.width() / n as f32; + for y in 0..n { + for x in 0..n { + let lx = (x as f32 + 0.5) / n as f32 * 2.0 - 1.0; + let ly = (y as f32 + 0.5) / n as f32 * 2.0 - 1.0; + let v = a.sample(lx, ly, hardness, 7); + if v > 0.02 { + let r = Rect::from_min_size(rect.min + egui::vec2(x as f32 * cell, y as f32 * cell), egui::vec2(cell + 0.5, cell + 0.5)); + p.rect_filled(r, 0.0, Color32::from_white_alpha((v * 255.0) as u8)); + } + } + } +} diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs new file mode 100644 index 00000000..10dd7acd --- /dev/null +++ b/crates/ui-egui/src/toolbar.rs @@ -0,0 +1,260 @@ +//! The context tool bar (brush size, flow, stroke opacity, spacing, lazy mouse, symmetry, view +//! toggles), the vertical tool shelf, the right icon rail and the status bar. + +use egui::{RichText, vec2}; +use egui_phosphor::regular as ic; +use serde_json::json; +use texturecraft_engine::Tool; + +use crate::TextureCraftApp; +use crate::docking::Panel; +use crate::i18n::tr; +use crate::keymap::tip; +use crate::state::{Mode, ViewLayout}; +use crate::theme::TOKENS; +use crate::widgets::{bar_slider, icon_button}; + +pub fn tool_icon(t: Tool) -> &'static str { + match t { + Tool::Paint => ic::PAINT_BRUSH, + Tool::Eraser => ic::ERASER, + Tool::Projection => ic::PROJECTOR_SCREEN, + Tool::PolygonFill => ic::POLYGON, + Tool::Smudge => ic::FINGERPRINT, + Tool::Clone => ic::STAMP, + Tool::ColorPicker => ic::EYEDROPPER, + Tool::Path => ic::PATH, + } +} + +pub fn top_bar(app: &mut TextureCraftApp, ui: &mut egui::Ui) { + egui::Panel::top("tool_bar").exact_size(34.0).frame(egui::Frame::NONE.fill(TOKENS.chrome).inner_margin(egui::Margin::symmetric(8, 2))).show(ui, |ui| { + ui.horizontal_centered(|ui| { + ui.spacing_mut().item_spacing.x = 10.0; + if app.ui.mode == Mode::Baking { + ui.label(RichText::new("Bake Mesh Maps").color(TOKENS.text_strong)); + return; + } + let tool = app.session.tool.tool; + ui.label(RichText::new(tool_icon(tool)).size(15.0).color(TOKENS.text_strong)).on_hover_text(tool.label()); + let b = &mut app.session.tool.brush; + match tool { + Tool::Paint | Tool::Eraser | Tool::Projection | Tool::Smudge | Tool::Clone | Tool::Path => { + let mut size = b.size / 0.25 * 100.0; + if bar_slider(ui, "Size", &mut size, 0.05..=100.0, 110.0).changed() { + b.size = (size / 100.0 * 0.25).clamp(0.0005, 0.25); + } + let mut flow = b.flow * 100.0; + if bar_slider(ui, "Flow", &mut flow, 0.0..=100.0, 100.0).changed() { + b.flow = flow / 100.0; + } + let mut op = b.opacity * 100.0; + if bar_slider(ui, "Stroke opacity", &mut op, 0.0..=100.0, 110.0).changed() { + b.opacity = op / 100.0; + } + let mut sp = b.spacing * 100.0; + if bar_slider(ui, "Spacing", &mut sp, 1.0..=200.0, 100.0).changed() { + b.spacing = sp / 100.0; + } + ui.separator(); + let lazy_on = b.lazy > 0.0; + if icon_button(ui, ic::SCRIBBLE, "Lazy mouse", lazy_on, 24.0).clicked() { + b.lazy = if lazy_on { 0.0 } else { 20.0 }; + } + if lazy_on { + let mut d = b.lazy; + if bar_slider(ui, "Distance", &mut d, 1.0..=200.0, 90.0).changed() { + b.lazy = d; + } + } + ui.separator(); + if icon_button(ui, ic::FLIP_HORIZONTAL, "Symmetry (X)", b.symmetry.x, 24.0).clicked() { + b.symmetry.x = !b.symmetry.x; + } + if icon_button(ui, ic::FLIP_VERTICAL, "Symmetry (Y)", b.symmetry.y, 24.0).clicked() { + b.symmetry.y = !b.symmetry.y; + } + if icon_button(ui, ic::CLOUD_RAIN, "Particle brush: particles slide down the surface", b.particles.enabled, 24.0).clicked() { + b.particles.enabled = !b.particles.enabled; + } + if tool == Tool::Smudge { + ui.separator(); + let mut st = app.session.tool.smudge_strength * 100.0; + if bar_slider(ui, "Strength", &mut st, 0.0..=100.0, 100.0).changed() { + app.session.tool.smudge_strength = (st / 100.0).clamp(0.0, 1.0); + } + } + if tool == Tool::Clone && app.session.tool.clone_source.is_none() { + ui.separator(); + ui.label(RichText::new("Hold V and click the mesh to set the clone source").color(TOKENS.text_dim)); + } + if tool == Tool::Path { + ui.separator(); + path_controls(app, ui); + } + } + Tool::PolygonFill => { + use texturecraft_engine::FillMode; + for (m, icon, tip) in [ + (FillMode::Triangle, ic::POLYGON, "Triangle fill"), + (FillMode::UvIsland, ic::SHAPES, "UV island fill"), + (FillMode::Mesh, ic::CUBE, "Mesh fill"), + ] { + if icon_button(ui, icon, tip, app.session.tool.fill_mode == m, 24.0).clicked() { + app.session.tool.fill_mode = m; + } + } + } + Tool::ColorPicker => { + ui.label(RichText::new("Click the mesh to pick its material into the brush").color(TOKENS.text_dim)); + } + } + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if icon_button(ui, ic::CAMERA, "Screenshot of the viewport", false, 24.0).clicked() { + app.toast("Use the control channel or `texturecraft-cli render` for screenshots", false); + } + ui.separator(); + for (l, icon, label, id) in [ + (ViewLayout::Only2D, ic::SQUARE, "2D view only", "ui.layout.2d"), + (ViewLayout::Only3D, ic::CUBE, "3D view only", "ui.layout.3d"), + (ViewLayout::Split, ic::COLUMNS, "3D/2D views", "ui.layout.split"), + ] { + if icon_button(ui, icon, &tip(&app.ui.keymap, label, id), app.ui.layout == l, 24.0).clicked() { + app.ui.layout = l; + } + } + ui.separator(); + let checker = app.session.view == texturecraft_engine::ViewMode::UvChecker; + if icon_button(ui, ic::CHECKERBOARD, &tip(&app.ui.keymap, "UV checker", "ui.uvChecker"), checker, 24.0).clicked() { + let mode = if checker { "material" } else { "uvChecker" }; + app.run("viewport.view", json!({"mode": mode})); + } + }); + }); + }); +} + +/// Path tool options and actions (top bar and properties). +pub fn path_controls(app: &mut TextureCraftApp, ui: &mut egui::Ui) { + let path = &mut app.session.tool.path; + if icon_button(ui, ic::POLYGON, "Closed path", path.closed, 24.0).clicked() { + path.closed = !path.closed; + } + let fill_tip = if path.closed { "Fill the closed path (lasso)" } else { "Fill the path (close it first)" }; + if icon_button(ui, ic::PAINT_BUCKET, fill_tip, path.fill, 24.0).clicked() { + path.fill = !path.fill; + } + let has_points = !path.points.is_empty(); + if ui.add_enabled(has_points, egui::Button::new(format!("{} Apply", ic::CHECK))).on_hover_text("Paint the path with the brush (Enter)").clicked() { + app.run("tool.path", json!({"commit": true})); + } + if ui.add_enabled(has_points, egui::Button::new(format!("{} Cancel", ic::X))).on_hover_text("Remove the path (Esc)").clicked() { + app.run("tool.path", json!({"clear": true})); + } +} + +pub fn tool_shelf(app: &mut TextureCraftApp, ui: &mut egui::Ui) { + egui::Panel::left("tool_shelf") + .resizable(false) + .exact_size(34.0) + .frame(egui::Frame::NONE.fill(TOKENS.chrome).inner_margin(egui::Margin::symmetric(3, 6))) + .show(ui, |ui| { + ui.spacing_mut().item_spacing.y = 3.0; + let painting = app.ui.mode == Mode::Painting; + for t in Tool::ALL { + let selected = painting && app.session.tool.tool == t; + let hint = tip(&app.ui.keymap, t.label(), &format!("tool.{}", t.id())); + if icon_button(ui, tool_icon(t), &hint, selected, 28.0).clicked() { + app.ui.mode = Mode::Painting; + app.run("tool.select", json!({"tool": t.id()})); + } + } + ui.add_space(6.0); + ui.separator(); + ui.add_space(6.0); + let q = &app.session.quick_mask; + for (id, icon, label, on) in [ + ("viewport.quickMask", ic::SELECTION_BACKGROUND, "Quick mask", q.enabled), + ("viewport.quickMask.edit", ic::SELECTION, "Edit quick mask", q.editing), + ("viewport.quickMask.invert", ic::SELECTION_INVERSE, "Invert quick mask", q.inverted), + ] { + if icon_button(ui, icon, &tip(&app.ui.keymap, label, id), on, 28.0).clicked() { + app.run(id, json!({})); + } + } + ui.add_space(6.0); + ui.separator(); + ui.add_space(6.0); + if icon_button(ui, ic::ATOM, &tip(&app.ui.keymap, "Bake mesh maps", "ui.mode.bake"), app.ui.mode == Mode::Baking, 28.0).clicked() { + app.ui.mode = if app.ui.mode == Mode::Baking { Mode::Painting } else { Mode::Baking }; + } + if icon_button(ui, ic::APERTURE, &tip(&app.ui.keymap, "Rendering, path traced", "ui.mode.render"), app.ui.mode == Mode::Rendering, 28.0).clicked() { + app.ui.mode = if app.ui.mode == Mode::Rendering { Mode::Painting } else { Mode::Rendering }; + } + if icon_button(ui, ic::SPHERE, "Add smart material from the shelf", false, 28.0).clicked() { + app.ui.dock.focus(Panel::Assets, ui.ctx().content_rect()); + } + }); +} + +pub fn rail(app: &mut TextureCraftApp, ui: &mut egui::Ui) { + egui::Panel::right("rail").resizable(false).exact_size(32.0).frame(egui::Frame::NONE.fill(TOKENS.chrome).inner_margin(egui::Margin::symmetric(3, 6))).show( + ui, + |ui| { + ui.spacing_mut().item_spacing.y = 4.0; + if icon_button(ui, ic::EXPORT, &tip(&app.ui.keymap, "Export textures", "ui.export"), app.ui.windows.export, 26.0).clicked() { + app.ui.windows.export = !app.ui.windows.export; + } + let screen = ui.ctx().content_rect(); + for (panel, icon, id) in [ + (Panel::TextureSetSettings, ic::STACK, "ui.view.textureSetSettings"), + (Panel::Display, ic::MONITOR, "ui.view.display"), + (Panel::Shader, ic::SLIDERS_HORIZONTAL, "ui.view.shader"), + (Panel::History, ic::CLOCK_COUNTER_CLOCKWISE, "ui.view.history"), + ] { + if icon_button(ui, icon, &tip(&app.ui.keymap, tr(panel.name()), id), app.ui.dock.is_visible(panel), 26.0).clicked() { + app.ui.dock.toggle(panel, screen); + } + } + }, + ); +} + +pub fn status_bar(app: &mut TextureCraftApp, ui: &mut egui::Ui) { + egui::Panel::bottom("status_bar").exact_size(22.0).frame(egui::Frame::NONE.fill(TOKENS.chrome).inner_margin(egui::Margin::symmetric(10, 0))).show( + ui, + |ui| { + ui.horizontal_centered(|ui| { + if let Some(p) = app.bake_progress() { + ui.add(egui::ProgressBar::new(p).desired_width(160.0).desired_height(10.0).text(RichText::new("Baking").size(10.0))); + if ui.small_button("Cancel").clicked() { + app.cancel_bake(); + } + } else if let Some(p) = app.gpu.as_ref().and_then(|g| g.composite_progress(&app.session)) { + ui.add(egui::ProgressBar::new(p).desired_width(160.0).desired_height(10.0).text(RichText::new("Compositing").size(10.0))); + ui.ctx().request_repaint(); + } else if let Some(msg) = app.session.log.last() { + ui.label(RichText::new(msg).size(11.0).color(TOKENS.text_dim)); + } + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + let gpu = app.gpu.as_ref().map(|g| g.adapter.as_str()).unwrap_or("no GPU"); + ui.label(RichText::new(format!("Version: {} ({gpu})", texturecraft_engine::VERSION)).size(11.0).color(TOKENS.text_dim)); + ui.separator(); + let m = app.session.memory(); + let mb = |b: usize| b as f64 / (1024.0 * 1024.0); + let total = mb(m.total()); + let decimals = if total < 10.0 { 1 } else { 0 }; + ui.label(RichText::new(format!("Layer memory: {total:.decimals$} MB")).size(11.0).color(TOKENS.text_dim)).on_hover_text(format!( + "Painted layer pixels {:.1} MB, composited channels {:.1} MB, compositing cache {:.1} MB", + mb(m.pixels), + mb(m.composites), + mb(m.cache) + )); + ui.separator(); + ui.label(RichText::new(format!("UI {:.1} ms", app.ui_ms)).size(11.0).color(TOKENS.text_dim)); + ui.add_space(vec2(0.0, 0.0).x); + }); + }); + }, + ); +} diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs new file mode 100644 index 00000000..1adf5980 --- /dev/null +++ b/crates/ui-egui/src/viewport.rs @@ -0,0 +1,597 @@ +//! The 3D viewport and the 2D UV view: navigation, painting input, brush cursor and the GPU +//! callbacks that render them. + +use std::sync::Mutex; + +use egui::{Color32, PointerButton, Pos2, Rect, RichText, Sense, Stroke, Ui, pos2, vec2}; +use serde_json::json; +use texturecraft_engine::core::{BakedMap, Channel}; +use texturecraft_engine::geom::glam::{Vec2, Vec3}; +use texturecraft_engine::{Tool, ViewMode}; +use texturecraft_render::{Frame, Shade, ViewKind}; + +use crate::TextureCraftApp; +use crate::gpu::{SceneSync, ViewCallback}; +use crate::state::ViewLayout; +use crate::theme::TOKENS; + +pub const VIEW_3D: u64 = 1; +pub const VIEW_2D: u64 = 2; + +pub fn central(app: &mut TextureCraftApp, ui: &mut Ui) { + let aux = match app.session.view { + _ if app.session.quick_mask.editing => Some(crate::gpu::AuxView::QuickMask), + ViewMode::MeshMap(m) => Some(crate::gpu::AuxView::MeshMap(m)), + ViewMode::Mask(id) => Some(crate::gpu::AuxView::Mask(id)), + _ => None, + }; + let mut sync = app.gpu.as_mut().map(|g| g.sync(&mut app.session, aux)); + let full = ui.max_rect(); + let mode = app.ui.mode; + path_keys(app, ui); + let layout = if mode == crate::state::Mode::Painting { app.ui.layout } else { ViewLayout::Only3D }; + match layout { + ViewLayout::Only3D => view_3d(app, ui, full, &mut sync), + ViewLayout::Only2D => view_2d(app, ui, full, &mut sync), + ViewLayout::Split => { + let mid = full.center().x; + let left = Rect::from_min_max(full.min, pos2(mid - 1.0, full.max.y)); + let right = Rect::from_min_max(pos2(mid + 1.0, full.min.y), full.max); + view_3d(app, ui, left, &mut sync); + view_2d(app, ui, right, &mut sync); + ui.painter().line_segment([pos2(mid, full.top()), pos2(mid, full.bottom())], Stroke::new(2.0, TOKENS.separator)); + } + } + if mode == crate::state::Mode::Rendering { + crate::render_mode::show(app, ui, full); + } else if app.render.is_active() { + crate::render_mode::stop(app); + } + // Keep drawing while the 3D view accumulates anti-aliasing samples (it stops on its own). + let accumulating = layout != ViewLayout::Only2D + && app.render_state.as_ref().is_some_and(|rs| rs.renderer.read().callback_resources.get::().is_some_and(|g| g.renderer.accumulating(VIEW_3D))); + if app.session.stroke_active() || accumulating { + ui.ctx().request_repaint(); + } +} + +fn shade(app: &TextureCraftApp) -> Shade { + let (view_mode, channel) = match app.session.view { + _ if app.session.quick_mask.editing => (2, 0), + ViewMode::Material => (0, 0), + ViewMode::Channel(c) => (1, c.index() as u32), + ViewMode::MeshMap(_) | ViewMode::Mask(_) => (2, 0), + ViewMode::UvChecker => (3, 0), + }; + let ts = app.session.ts(); + let hs = app.session.doc.project.texture_sets.get(ts).map(|t| t.height_strength).unwrap_or(1.0); + Shade { + height_strength: hs, + view_mode, + channel, + wireframe: app.ui.uv_wireframe, + wireframe_3d: app.ui.wireframe_3d, + ..Shade::from_display(&app.session.doc.project.display) + } +} + +fn add_callback(app: &TextureCraftApp, ui: &Ui, rect: Rect, frame: Frame, sync: &mut Option) { + let Some(gpu) = &app.gpu else { + ui.painter().text(rect.center(), egui::Align2::CENTER_CENTER, "No GPU renderer available", egui::FontId::proportional(14.0), TOKENS.text_dim); + return; + }; + let cb = ViewCallback { frame, sync: Mutex::new(sync.take()), rect, lost: gpu.lost.clone() }; + ui.painter().add(eframe::egui_wgpu::Callback::new_paint_callback(rect, cb)); +} + +/// The view-mode dropdown shown at the top of each view. +fn view_header(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, id: &str) { + let header = Rect::from_min_size(rect.min + vec2(rect.width() * 0.5 - 150.0, 8.0), vec2(300.0, 22.0)); + let mut child = ui.new_child(egui::UiBuilder::new().max_rect(header).layout(egui::Layout::left_to_right(egui::Align::Center))); + egui::ComboBox::from_id_salt(format!("{id}_cam")).width(130.0).selected_text("Default camera").show_ui(&mut child, |ui| { + if ui.selectable_label(true, "Default camera").clicked() { + app.run("viewport.frame", json!({})); + } + }); + let label = app.session.view.label(); + let channels = app.session.doc.project.texture_set(app.session.ts()).map(|t| t.channel_list()).unwrap_or_default(); + egui::ComboBox::from_id_salt(format!("{id}_mode")).width(130.0).selected_text(label).show_ui(&mut child, |ui| { + if ui.selectable_label(app.session.view == ViewMode::Material, "Material").clicked() { + app.run("viewport.view", json!({"mode": "material"})); + } + ui.separator(); + for c in channels { + if ui.selectable_label(app.session.view == ViewMode::Channel(c), c.label()).clicked() { + app.run("viewport.view", json!({"mode": c.id()})); + } + } + ui.separator(); + for m in BakedMap::ALL { + if ui.selectable_label(app.session.view == ViewMode::MeshMap(m), format!("Mesh map: {}", m.label())).clicked() { + app.run("viewport.view", json!({"mode": format!("map:{}", m.id())})); + } + } + ui.separator(); + if ui.selectable_label(app.session.view == ViewMode::UvChecker, "UV checker").clicked() { + app.run("viewport.view", json!({"mode": "uvChecker"})); + } + }); +} + +fn axis_gizmo(ui: &Ui, rect: Rect, app: &TextureCraftApp, uv: bool) { + let o = pos2(rect.left() + 28.0, rect.bottom() - 28.0); + let p = ui.painter(); + if uv { + p.line_segment([o, o + vec2(16.0, 0.0)], Stroke::new(1.5, Color32::from_rgb(0xd8, 0x4a, 0x3a))); + p.line_segment([o, o - vec2(0.0, 16.0)], Stroke::new(1.5, Color32::from_rgb(0x5c, 0xc0, 0x4a))); + p.text(o + vec2(20.0, 0.0), egui::Align2::LEFT_CENTER, "U", egui::FontId::proportional(9.0), TOKENS.text_dim); + p.text(o - vec2(0.0, 22.0), egui::Align2::CENTER_CENTER, "V", egui::FontId::proportional(9.0), TOKENS.text_dim); + return; + } + let view = app.session.camera.view(); + for (axis, color, name) in [ + (Vec3::X, Color32::from_rgb(0xd8, 0x4a, 0x3a), "X"), + (Vec3::Y, Color32::from_rgb(0x5c, 0xc0, 0x4a), "Y"), + (Vec3::Z, Color32::from_rgb(0x4a, 0x80, 0xe0), "Z"), + ] { + let d = view.transform_vector3(axis); + let end = o + vec2(d.x, -d.y) * 16.0; + p.line_segment([o, end], Stroke::new(1.5, color)); + p.text(end + vec2(d.x, -d.y) * 6.0, egui::Align2::CENTER_CENTER, name, egui::FontId::proportional(9.0), TOKENS.text_dim); + } +} + +fn view_3d(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, sync: &mut Option) { + let resp = ui.interact(rect, ui.id().with("view3d"), Sense::click_and_drag()); + app.viewport_rect = rect; + let size = rect.size(); + app.session.viewport = Vec2::new(size.x.max(16.0), size.y.max(16.0)); + let (alt, shift) = ui.input(|i| (i.modifiers.alt, i.modifiers.shift)); + let local = |p: egui::Pos2| Vec2::new(p.x - rect.left(), p.y - rect.top()); + let navigating = alt || ui.input(|i| i.pointer.middle_down() || i.pointer.secondary_down()); + + // Navigation. + let delta = ui.input(|i| i.pointer.delta()); + if resp.dragged_by(PointerButton::Middle) || (alt && resp.dragged_by(PointerButton::Middle)) { + app.session.camera.pan(delta.x / size.y, delta.y / size.y); + } else if alt && resp.dragged_by(PointerButton::Primary) { + app.session.camera.orbit(delta.x * 0.008, delta.y * 0.008); + } else if resp.dragged_by(PointerButton::Secondary) { + if alt { + app.session.camera.zoom(-delta.y * 0.01); + } else { + app.session.camera.orbit(delta.x * 0.008, delta.y * 0.008); + } + } + if resp.hovered() { + let scroll = ui.input(|i| i.smooth_scroll_delta.y); + if scroll != 0.0 { + if shift { + let b = &mut app.session.tool.brush; + b.size = (b.size * (1.0 + scroll * 0.002)).clamp(0.0005, 0.25); + } else { + app.session.camera.zoom(scroll * 0.0025); + } + } + } + + // Stencil: S + left drag rotates, S + right drag scales, S + middle drag moves. + let s_held = ui.input(|i| i.key_down(egui::Key::S)) && !ui.ctx().egui_wants_keyboard_input(); + if s_held + && let Some(st) = app.session.tool.stencil.as_mut() + { + if resp.dragged_by(PointerButton::Primary) { + st.rotation = (st.rotation + delta.x * 0.4).rem_euclid(360.0); + } else if resp.dragged_by(PointerButton::Secondary) { + st.scale = (st.scale * (1.0 + delta.x * 0.005)).clamp(0.05, 10.0); + } else if resp.dragged_by(PointerButton::Middle) { + st.center[0] += delta.x / size.x * 2.0; + st.center[1] -= delta.y / size.y * 2.0; + } + } + // Clone source: V + click. + let v_held = clone_source_key(app, ui); + if v_held + && !alt + && (resp.clicked_by(PointerButton::Primary) || resp.drag_started_by(PointerButton::Primary)) + && let Some(p) = resp.interact_pointer_pos() + { + let (q, vp) = (local(p), app.session.viewport); + app.run("tool.cloneSource", json!({"x": q.x, "y": q.y, "viewport": [vp.x, vp.y]})); + } + // Painting. + let painting = app.ui.mode == crate::state::Mode::Painting && !alt && !s_held && !v_held; + let path_tool = app.session.tool.tool == Tool::Path; + let vp = app.session.viewport; + let show = |app: &TextureCraftApp, p: Vec3| project(app, rect, p); + if painting && path_tool { + path_input(app, ui, &resp, false, |app, p| app.session.pick(local(p), vp).map(|h| h.pos), show); + } else if painting { + // Pen pressure arrives as touch force (Windows Ink, Wayland, web); mice report none. + // shortcut: egui 0.36 / winit 0.30 report no pen tilt, so tilt can't drive the brush angle yet. + let pressure = ui.input(|i| { + i.raw.events.iter().rev().find_map(|e| match e { + egui::Event::Touch { force: Some(f), .. } if f.is_finite() => Some(f.clamp(0.0, 1.0)), + _ => None, + }) + }); + let pressure = pressure.unwrap_or(1.0); + if resp.drag_started_by(PointerButton::Primary) || resp.clicked_by(PointerButton::Primary) { + // Shift+click paints a straight line from where the last stroke ended. + let line_from = if shift { app.last_paint_point } else { None }; + if let Some(p) = resp.interact_pointer_pos() { + match app.session.begin_stroke(line_from.unwrap_or(local(p)), app.session.viewport, pressure) { + Ok(_) => { + if line_from.is_some() { + app.session.continue_stroke(local(p), pressure); + } + app.last_paint_point = Some(local(p)); + } + Err(e) => app.toast(e.to_string(), true), + } + } + if resp.clicked_by(PointerButton::Primary) + && let Err(e) = app.session.end_stroke() + { + app.toast(e.to_string(), true); + } + } else if resp.dragged_by(PointerButton::Primary) + && app.session.stroke_active() + && let Some(p) = resp.interact_pointer_pos() + { + app.session.continue_stroke(local(p), pressure); + app.last_paint_point = Some(local(p)); + } + if resp.drag_stopped_by(PointerButton::Primary) + && let Err(e) = app.session.end_stroke() + { + app.toast(e.to_string(), true); + } + } + + let frame = Frame { + view_id: crate::viewport::VIEW_3D, + size: [(size.x * ui.ctx().pixels_per_point()).max(1.0) as u32, (size.y * ui.ctx().pixels_per_point()).max(1.0) as u32], + kind: ViewKind::Scene, + camera: app.session.camera, + shade: shade(app), + visible: app.session.doc.project.texture_sets.iter().map(|t| t.visible).collect(), + }; + add_callback(app, ui, rect, frame, sync); + + // Brush cursor. + if app.ui.mode == crate::state::Mode::Painting + && !navigating + && resp.hovered() + && matches!(app.session.tool.tool, Tool::Paint | Tool::Eraser | Tool::Projection | Tool::Smudge | Tool::Clone | Tool::Path) + && let Some(hp) = resp.hover_pos() + && let Some(hit) = app.session.pick(local(hp), app.session.viewport) + { + ui.ctx().set_cursor_icon(egui::CursorIcon::None); + brush_cursor(app, ui, rect, hit.pos, hit.nrm); + if app.session.tool.tool == Tool::Clone + && let Some(src) = app.session.clone_source_point(Some(hit.uv)) + { + clone_marker(app, ui, rect, src); + } + // Shift: preview the straight line Shift+click paints from the last stroke. + if shift + && !path_tool + && let Some(from) = app.last_paint_point + { + rubber_band(ui, rect, pos2(rect.left() + from.x, rect.top() + from.y), hp); + } + } + if app.ui.mode == crate::state::Mode::Painting && path_tool { + path_overlay(app, ui, rect, &resp, false, show); + } + stencil_overlay(app, ui, rect); + if app.session.quick_mask.editing { + ui.painter_at(rect).text( + rect.left_top() + vec2(12.0, 40.0), + egui::Align2::LEFT_TOP, + "Editing quick mask: paint where painting is allowed (U to finish)", + egui::FontId::proportional(12.0), + Color32::from_rgb(0xe8, 0x6a, 0x6a), + ); + } + view_header(app, ui, rect, "v3d"); + axis_gizmo(ui, rect, app, false); +} + +/// Draw the projection stencil over the 3D view. +fn stencil_overlay(app: &mut TextureCraftApp, ui: &Ui, rect: Rect) { + if app.session.tool.tool != Tool::Projection { + return; + } + let Some(st) = app.session.tool.stencil.clone() else { return }; + let Some(img) = app.session.images.get(&st.image).cloned() else { return }; + let tex = app.thumbs.stencil(ui.ctx(), &st.image, &img); + let aspect = rect.width() / rect.height().max(1.0); + let (center, half, rot) = st.placement(img.width as f32 / img.height.max(1) as f32, aspect); + let to_screen = |nx: f32, ny: f32| pos2(rect.left() + (nx * 0.5 + 0.5) * rect.width(), rect.top() + (0.5 - ny * 0.5) * rect.height()); + let (sn, cs) = rot.sin_cos(); + let corner = |lx: f32, ly: f32| { + let (x, y) = (lx * half[0], ly * half[1]); + to_screen(center[0] + x * cs - y * sn, center[1] + x * sn + y * cs) + }; + let tint = Color32::from_white_alpha((st.opacity * 255.0) as u8); + let mut mesh = egui::Mesh::with_texture(tex.id()); + for (lx, ly, u, v) in [(-1.0, 1.0, 0.0, 0.0), (1.0, 1.0, 1.0, 0.0), (1.0, -1.0, 1.0, 1.0), (-1.0, -1.0, 0.0, 1.0)] { + mesh.vertices.push(egui::epaint::Vertex { pos: corner(lx, ly), uv: pos2(u, v), color: tint }); + } + mesh.indices.extend_from_slice(&[0, 1, 2, 0, 2, 3]); + let p = ui.painter_at(rect); + p.add(egui::Shape::mesh(mesh)); + let outline = vec![corner(-1.0, 1.0), corner(1.0, 1.0), corner(1.0, -1.0), corner(-1.0, -1.0), corner(-1.0, 1.0)]; + p.add(egui::Shape::dashed_line(&outline, Stroke::new(1.0, Color32::from_white_alpha(120)), 5.0, 4.0)); +} + +/// A world point on screen in the 3D view `rect` (None behind the camera). +fn project(app: &TextureCraftApp, rect: Rect, p: Vec3) -> Option { + let n = app.session.camera.project(p, rect.width() / rect.height().max(1.0))?; + Some(pos2(rect.left() + (n.x * 0.5 + 0.5) * rect.width(), rect.top() + (0.5 - n.y * 0.5) * rect.height())) +} + +/// Control point grab radius (points). +const HANDLE_GRAB: f32 = 8.0; + +/// Path tool keys: Enter applies the path, Backspace removes its last point (Esc, handled with +/// the global shortcuts, cancels it). +fn path_keys(app: &mut TextureCraftApp, ui: &Ui) { + if app.ui.mode != crate::state::Mode::Painting + || app.session.tool.tool != Tool::Path + || app.session.tool.path.points.is_empty() + || ui.ctx().egui_wants_keyboard_input() + { + return; + } + let (enter, back) = ui.input_mut(|i| (i.consume_key(egui::Modifiers::NONE, egui::Key::Enter), i.consume_key(egui::Modifiers::NONE, egui::Key::Backspace))); + if enter { + app.run("tool.path", json!({"commit": true})); + } + if back { + app.session.tool.path.points.pop(); + } +} + +/// Path tool input in a view (`uv`: the 2D view). Pressing on a control point drags it, +/// elsewhere adds a point (and drags it); clicking the first point of an open path closes it. +/// `place` maps a pointer position to a path point (None off the surface), `show` a path point +/// to the screen. +fn path_input( + app: &mut TextureCraftApp, + ui: &Ui, + resp: &egui::Response, + uv: bool, + place: impl Fn(&TextureCraftApp, Pos2) -> Option, + show: impl Fn(&TextureCraftApp, Vec3) -> Option, +) { + let drag_id = resp.id.with("path_drag"); + let clicked = resp.clicked_by(PointerButton::Primary); + if (clicked || resp.drag_started_by(PointerButton::Primary)) + && let Some(at) = ui.input(|i| i.pointer.press_origin()).or(resp.interact_pointer_pos()) + { + let path = &app.session.tool.path; + let near = if path.uv == uv { path.points.iter().position(|p| show(app, *p).is_some_and(|s| s.distance(at) < HANDLE_GRAB)) } else { None }; + match near { + Some(0) if clicked && !path.closed && path.points.len() >= 3 => app.session.tool.path.closed = true, + Some(i) => ui.data_mut(|d| { + d.insert_temp(drag_id, i); + }), + None => match place(app, at) { + Some(p) => { + app.session.tool.path.push(p, uv); + let last = app.session.tool.path.points.len().saturating_sub(1); + ui.data_mut(|d| { + d.insert_temp(drag_id, last); + }); + } + None => app.toast("Place path points on the mesh", false), + }, + } + } + if resp.dragged_by(PointerButton::Primary) + && let Some(i) = ui.data(|d| d.get_temp::(drag_id)) + && let Some(p) = resp.interact_pointer_pos().and_then(|pos| place(app, pos)) + && let Some(q) = app.session.tool.path.points.get_mut(i) + { + *q = p; + } + if !resp.is_pointer_button_down_on() { + ui.data_mut(|d| d.remove::(drag_id)); + } +} + +/// The path being edited: its curve, control points (the first one in the accent color) and, +/// while open, a dashed preview of the next segment. +fn path_overlay(app: &TextureCraftApp, ui: &Ui, rect: Rect, resp: &egui::Response, uv: bool, show: impl Fn(&TextureCraftApp, Vec3) -> Option) { + let p = ui.painter_at(rect); + let path = &app.session.tool.path; + let hint = "Path: click adds a point, drag moves it, Enter applies, Esc cancels"; + p.text(rect.left_bottom() + vec2(56.0, -12.0), egui::Align2::LEFT_BOTTOM, hint, egui::FontId::proportional(12.0), TOKENS.text_dim); + if path.uv != uv { + return; + } + let handles: Vec = path.points.iter().filter_map(|q| show(app, *q)).collect(); + let pts: Vec = handles.iter().map(|h| Vec2::new(h.x, h.y)).collect(); + let curve: Vec = texturecraft_engine::paint::spline(&pts, path.closed, 3.0).into_iter().map(|v| pos2(v.x, v.y)).collect(); + let color = if path.closed && path.fill { TOKENS.accent } else { Color32::from_white_alpha(230) }; + p.add(egui::Shape::line(curve.clone(), Stroke::new(3.0, Color32::from_black_alpha(140)))); + p.add(egui::Shape::line(curve, Stroke::new(1.4, color))); + let hover = resp.hover_pos(); + if !path.closed + && let (Some(last), Some(to)) = (handles.last(), hover) + { + rubber_band(ui, rect, *last, to); + } + for (i, h) in handles.iter().enumerate() { + let r = if hover.is_some_and(|m| m.distance(*h) < HANDLE_GRAB) { 5.5 } else { 4.0 }; + p.circle(*h, r, if i == 0 { TOKENS.accent } else { Color32::WHITE }, Stroke::new(1.5, Color32::from_black_alpha(180))); + } +} + +/// Dashed preview of a straight segment. +fn rubber_band(ui: &Ui, rect: Rect, from: Pos2, to: Pos2) { + let p = ui.painter_at(rect); + p.add(egui::Shape::dashed_line(&[from, to], Stroke::new(2.5, Color32::from_black_alpha(120)), 6.0, 4.0)); + p.add(egui::Shape::dashed_line(&[from, to], Stroke::new(1.0, Color32::from_white_alpha(200)), 6.0, 4.0)); +} + +/// V held with the Clone tool in painting mode: clicks set the clone source. +fn clone_source_key(app: &TextureCraftApp, ui: &Ui) -> bool { + app.ui.mode == crate::state::Mode::Painting + && app.session.tool.tool == Tool::Clone + && ui.input(|i| i.key_down(egui::Key::V)) + && !ui.ctx().egui_wants_keyboard_input() +} + +/// Crosshair where the Clone tool reads from. +fn clone_marker(app: &TextureCraftApp, ui: &Ui, rect: Rect, pos: Vec3) { + let Some(c) = project(app, rect, pos) else { return }; + let p = ui.painter_at(rect); + for (w, color) in [(3.0, Color32::from_black_alpha(140)), (1.2, TOKENS.accent)] { + p.circle_stroke(c, 7.0, Stroke::new(w, color)); + p.line_segment([c - vec2(11.0, 0.0), c + vec2(11.0, 0.0)], Stroke::new(w, color)); + p.line_segment([c - vec2(0.0, 11.0), c + vec2(0.0, 11.0)], Stroke::new(w, color)); + } +} + +fn brush_cursor(app: &TextureCraftApp, ui: &Ui, rect: Rect, pos: Vec3, nrm: Vec3) { + let (lo, hi) = app.session.mesh.bounds(); + let r = app.session.tool.brush.size * (hi - lo).length(); + let t = nrm.any_orthonormal_vector(); + let b = nrm.cross(t); + let to_screen = |p: Vec3| project(app, rect, p); + let mut pts = Vec::with_capacity(49); + for k in 0..=48 { + let a = k as f32 / 48.0 * std::f32::consts::TAU; + if let Some(s) = to_screen(pos + (t * a.cos() + b * a.sin()) * r) { + pts.push(s); + } + } + let p = ui.painter_at(rect); + p.add(egui::Shape::line(pts.clone(), Stroke::new(2.5, Color32::from_black_alpha(140)))); + p.add(egui::Shape::line(pts, Stroke::new(1.2, Color32::from_white_alpha(230)))); + let h = app.session.tool.brush.hardness; + if h > 0.05 { + let mut inner = Vec::new(); + for k in 0..=48 { + let a = k as f32 / 48.0 * std::f32::consts::TAU; + if let Some(s) = to_screen(pos + (t * a.cos() + b * a.sin()) * r * h) { + inner.push(s); + } + } + p.add(egui::Shape::dashed_line(&inner, Stroke::new(1.0, Color32::from_white_alpha(120)), 3.0, 3.0)); + } + if let Some(c) = to_screen(pos) { + p.circle_filled(c, 1.5, Color32::WHITE); + } +} + +fn view_2d(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, sync: &mut Option) { + let resp = ui.interact(rect, ui.id().with("view2d"), Sense::click_and_drag()); + let size = rect.size(); + let alt = ui.input(|i| i.modifiers.alt); + let delta = ui.input(|i| i.pointer.delta()); + if resp.dragged_by(PointerButton::Middle) || (alt && resp.dragged_by(PointerButton::Primary)) { + app.ui.uv_pan[0] += delta.x / size.x * 2.0; + app.ui.uv_pan[1] -= delta.y / size.y * 2.0; + } + if resp.hovered() { + let scroll = ui.input(|i| i.smooth_scroll_delta.y); + if scroll != 0.0 { + let old = app.ui.uv_zoom; + let new = (old * (1.0 + scroll * 0.002)).clamp(0.1, 40.0); + // Zoom about the cursor. + if let Some(hp) = resp.hover_pos() { + let c = [(hp.x - rect.left()) / size.x * 2.0 - 1.0, 1.0 - (hp.y - rect.top()) / size.y * 2.0]; + for k in 0..2 { + app.ui.uv_pan[k] = c[k] - (c[k] - app.ui.uv_pan[k]) * new / old; + } + } + app.ui.uv_zoom = new; + } + } + let aspect = size.x / size.y.max(1.0); + let z = app.ui.uv_zoom * 0.92; + let (sx, sy) = if aspect >= 1.0 { (z / aspect, z) } else { (z, z * aspect) }; + let pan = app.ui.uv_pan; + let to_uv = |p: egui::Pos2| { + let xc = (p.x - rect.left()) / size.x * 2.0 - 1.0; + let yc = 1.0 - (p.y - rect.top()) / size.y * 2.0; + Vec2::new(((xc - pan[0]) / sx + 1.0) * 0.5, (1.0 - (yc - pan[1]) / sy) * 0.5) + }; + let to = |u: f32, v: f32| { + let x = (u * 2.0 - 1.0) * sx + pan[0]; + let y = (1.0 - v * 2.0) * sy + pan[1]; + pos2(rect.left() + (x * 0.5 + 0.5) * size.x, rect.top() + (0.5 - y * 0.5) * size.y) + }; + let show = |_: &TextureCraftApp, p: Vec3| Some(to(p.x, p.y)); + let path_tool = app.session.tool.tool == Tool::Path; + // Clone source (V + click) and painting in UV space. + let v_held = clone_source_key(app, ui); + if v_held + && (resp.clicked_by(PointerButton::Primary) || resp.drag_started_by(PointerButton::Primary)) + && let Some(p) = resp.interact_pointer_pos() + { + let uv = to_uv(p); + app.run("tool.cloneSource", json!({"uv": [uv.x, uv.y]})); + } + let painting = app.ui.mode == crate::state::Mode::Painting && !alt && !v_held; + if painting && path_tool { + path_input(app, ui, &resp, true, |_, p| Some(to_uv(p).extend(0.0)), show); + } else if painting { + if resp.drag_started_by(PointerButton::Primary) || resp.clicked_by(PointerButton::Primary) { + if let Some(p) = resp.interact_pointer_pos() + && let Err(e) = app.session.begin_stroke_uv(to_uv(p), 1.0) + { + app.toast(e.to_string(), true); + } + if resp.clicked_by(PointerButton::Primary) + && let Err(e) = app.session.end_stroke() + { + app.toast(e.to_string(), true); + } + } else if resp.dragged_by(PointerButton::Primary) + && app.session.stroke_active() + && let Some(p) = resp.interact_pointer_pos() + { + app.session.continue_stroke_uv(to_uv(p), 1.0); + } + if resp.drag_stopped_by(PointerButton::Primary) + && let Err(e) = app.session.end_stroke() + { + app.toast(e.to_string(), true); + } + } + if resp.double_clicked() && !(painting && path_tool) { + app.ui.uv_zoom = 1.0; + app.ui.uv_pan = [0.0, 0.0]; + } + let ts = app.session.ts(); + let frame = Frame { + view_id: VIEW_2D, + size: [(size.x * ui.ctx().pixels_per_point()).max(1.0) as u32, (size.y * ui.ctx().pixels_per_point()).max(1.0) as u32], + kind: ViewKind::Uv { ts, zoom: app.ui.uv_zoom, pan: app.ui.uv_pan }, + camera: app.session.camera, + shade: shade(app), + visible: app.session.doc.project.texture_sets.iter().map(|t| t.visible).collect(), + }; + add_callback(app, ui, rect, frame, sync); + // The 0..1 tile outline and the path being edited. + ui.painter_at(rect).rect_stroke( + Rect::from_two_pos(to(0.0, 0.0), to(1.0, 1.0)), + 0.0, + Stroke::new(1.0, Color32::from_white_alpha(40)), + egui::StrokeKind::Outside, + ); + if app.ui.mode == crate::state::Mode::Painting && path_tool { + path_overlay(app, ui, rect, &resp, true, show); + } + view_header(app, ui, rect, "v2d"); + axis_gizmo(ui, rect, app, true); + let name = app.session.doc.project.texture_sets.get(ts).map(|t| t.name.clone()).unwrap_or_default(); + ui.painter().text(rect.right_bottom() + vec2(-10.0, -10.0), egui::Align2::RIGHT_BOTTOM, name, egui::FontId::proportional(11.0), TOKENS.text_dim); + let _ = (RichText::new(""), Channel::BaseColor); +} diff --git a/crates/ui-egui/src/web_files.rs b/crates/ui-egui/src/web_files.rs new file mode 100644 index 00000000..274d59a3 --- /dev/null +++ b/crates/ui-egui/src/web_files.rs @@ -0,0 +1,314 @@ +//! File I/O that works without paths. In the browser, File › Open is the browser's file picker and +//! Save / Export are downloads; on the desktop, the same calls open native dialogs and run the +//! path-based engine commands (`file.open`, `file.saveAs`, `file.exportTextures`). +//! +//! A browser pick is asynchronous: its bytes wait in an inbox until [`poll`] (called every frame) +//! turns them into the session. + +use std::sync::{Mutex, PoisonError}; + +use texturecraft_engine::Session; +use texturecraft_engine::core::PROJECT_EXT; +use texturecraft_engine::formats::ExportedFile; +use texturecraft_engine::formats::export::sanitize_file_stem; +use texturecraft_engine::formats::mesh_import::MESH_EXTENSIONS; + +use crate::TextureCraftApp; + +/// What File › Open accepts: projects and meshes. +pub const OPEN_EXTS: &[&str] = &[PROJECT_EXT, "glb", "gltf", "fbx", "dae", "obj", "ply", "stl"]; + +/// Images File › Import resources accepts. +pub const IMAGE_EXTS: &[&str] = &["png", "jpg", "jpeg", "tga", "tif", "tiff", "bmp", "webp", "exr", "hdr"]; + +/// Equirect HDR images Display settings › Import HDR accepts. +pub const ENV_EXTS: &[&str] = &["hdr", "exr"]; + +/// Texture size of a project made from an opened mesh (smaller on the single-threaded web). +const MESH_RESOLUTION: u32 = if cfg!(target_arch = "wasm32") { 1024 } else { 2048 }; + +/// What a picked file is for. +#[derive(Clone, Copy, PartialEq)] +#[cfg_attr(not(target_arch = "wasm32"), allow(dead_code))] +enum Pick { + /// File > Open / a dropped file: a project, a mesh or an image resource. + Open, + /// Display settings > Import HDR. + Environment, + /// Bake mode: a high-definition mesh. + HighPoly, + /// Bake mode: the cage mesh. + Cage, +} + +/// Files picked in the browser, waiting for the next frame: (file name, bytes, purpose). +static INBOX: Mutex, Pick)>> = Mutex::new(Vec::new()); + +/// Load files that arrived since the last frame. Call once per frame. +pub fn poll(app: &mut TextureCraftApp) { + let picked = std::mem::take(&mut *INBOX.lock().unwrap_or_else(PoisonError::into_inner)); + for (name, bytes, pick) in picked { + let done = match pick { + Pick::Open => { + load(app, &name, &bytes); + continue; + } + Pick::Environment => app.session.import_resource_bytes(&name, bytes).map(|res| use_environment(app, &res)), + Pick::HighPoly => app.session.add_high_poly_bytes(&name, bytes).map(|n| app.toast(format!("High poly: {n}"), false)), + Pick::Cage => app.session.set_cage_bytes(&name, bytes).map(|n| app.toast(format!("Cage: {n}"), false)), + }; + if let Err(e) = done { + app.toast(format!("Couldn't import {name}: {e}"), true); + } + } +} + +/// Bake mode: add high-definition meshes (or load the cage) with the native file dialog; in +/// the browser they arrive on a later frame. +#[cfg(not(target_arch = "wasm32"))] +pub fn import_bake_mesh(app: &mut TextureCraftApp, _ctx: &egui::Context, cage: bool) { + let dialog = rfd::FileDialog::new().add_filter("Meshes", &MESH_EXTENSIONS); + let paths = if cage { dialog.pick_file().into_iter().collect() } else { dialog.pick_files().unwrap_or_default() }; + for p in paths { + let cmd = if cage { "bake.cage.set" } else { "bake.highPoly.add" }; + app.run(cmd, serde_json::json!({ "path": p.display().to_string() })); + } +} + +/// Display settings › Import HDR: pick an equirect `.hdr`/`.exr`, import it as a resource and +/// light the scene with it (in the browser the file arrives on a later frame). +#[cfg(not(target_arch = "wasm32"))] +pub fn import_environment(app: &mut TextureCraftApp, _ctx: &egui::Context) { + let Some(p) = rfd::FileDialog::new().add_filter("HDR environments", ENV_EXTS).pick_file() else { return }; + if let Some(r) = app.run("resource.import", serde_json::json!({ "path": p.display().to_string() })) + && let Some(name) = r.get("name").and_then(|n| n.as_str()) + { + use_environment(app, name); + } +} + +/// Make image resource `name` the environment. +fn use_environment(app: &mut TextureCraftApp, name: &str) { + let env = format!("{}{name}", texturecraft_engine::core::project::ENV_RESOURCE_PREFIX); + if app.run("display.set", serde_json::json!({ "environment": env })).is_some() { + app.toast(format!("Environment: {name}"), false); + } +} + +/// Import an image as a project resource, or replace the session with a `.tcraft` project or a +/// mesh file's bytes (errors become toasts). +pub fn load(app: &mut TextureCraftApp, name: &str, bytes: &[u8]) { + let path = std::path::Path::new(name); + let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase(); + if IMAGE_EXTS.contains(&ext.as_str()) { + match app.session.import_resource_bytes(name, bytes.to_vec()) { + Ok(res) => { + app.ui.dock.focus(crate::docking::Panel::Assets, egui::Rect::ZERO); + app.toast(format!("Imported {res}"), false); + } + Err(e) => app.toast(format!("Couldn't import {name}: {e}"), true), + } + return; + } + let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("Untitled"); + let next = if ext == PROJECT_EXT { Session::from_project_bytes(bytes) } else { Session::from_mesh_bytes(name, bytes, MESH_RESOLUTION, stem) }; + match next { + Ok(mut next) => { + next.viewport = app.session.viewport; + app.session = next; + app.toast(format!("Opened {name}"), false); + } + Err(e) => app.toast(format!("Couldn't open {name}: {e}"), true), + } +} + +/// A file dropped on the window: images import as resources, anything else opens. +pub fn dropped(app: &mut TextureCraftApp, ctx: &egui::Context, file: &egui::DroppedFileHandle) { + #[cfg(not(target_arch = "wasm32"))] + { + let _ = ctx; + let path = file.path(); + let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase(); + let path = path.display().to_string(); + if IMAGE_EXTS.contains(&ext.as_str()) { + if let Some(r) = app.run("resource.import", serde_json::json!({ "path": path })) { + let name = r.get("name").and_then(|n| n.as_str()).unwrap_or_default().to_string(); + app.ui.dock.focus(crate::docking::Panel::Assets, egui::Rect::ZERO); + app.toast(format!("Imported {name}"), false); + } + } else { + crate::windows::open_path(app, &path); + } + } + #[cfg(target_arch = "wasm32")] + { + let _ = app; + let (file, ctx) = (file.clone(), ctx.clone()); + wasm_bindgen_futures::spawn_local(async move { + let name = file.path().display().to_string(); + match file.bytes_async().await { + Ok(bytes) => INBOX.lock().unwrap_or_else(PoisonError::into_inner).push((name, bytes, Pick::Open)), + Err(e) => log::warn!("couldn't read dropped file {name}: {e}"), + } + ctx.request_repaint(); + }); + } +} + +/// The project's download / suggested file name. +pub fn project_file_name(app: &TextureCraftApp) -> String { + format!("{}.{PROJECT_EXT}", sanitize_file_stem(&app.session.doc.project.name)) +} + +/// Pack exported textures into one zip archive. +#[cfg_attr(not(target_arch = "wasm32"), allow(dead_code))] +fn zip_files(files: &[ExportedFile]) -> Result, String> { + use std::io::Write as _; + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated).large_file(true); + for f in files { + zip.start_file(f.name.as_str(), opts).map_err(|e| e.to_string())?; + zip.write_all(&f.bytes).map_err(|e| e.to_string())?; + } + Ok(zip.finish().map_err(|e| e.to_string())?.into_inner()) +} + +#[cfg(target_arch = "wasm32")] +pub use web::{download, export_mesh, export_textures, import_bake_mesh, import_environment, import_images, open, save_project}; + +#[cfg(target_arch = "wasm32")] +mod web { + use super::*; + use serde_json::Value; + use wasm_bindgen::JsCast as _; + + /// File › Open: the browser's file picker; the file loads on a later frame (see [`poll`]). + pub fn open(ctx: &egui::Context) { + pick(ctx, "Projects and meshes", OPEN_EXTS, false, Pick::Open); + } + + /// File › Import resources: pick images; they import on a later frame. + pub fn import_images(ctx: &egui::Context) { + pick(ctx, "Images", IMAGE_EXTS, true, Pick::Open); + } + + /// Display settings › Import HDR: pick an equirect image; it becomes the environment on a + /// later frame. + pub fn import_environment(_app: &mut TextureCraftApp, ctx: &egui::Context) { + pick(ctx, "HDR environments", ENV_EXTS, false, Pick::Environment); + } + + /// Bake mode: pick high-definition meshes (or the cage); they load on a later frame. + pub fn import_bake_mesh(_app: &mut TextureCraftApp, ctx: &egui::Context, cage: bool) { + pick(ctx, "Meshes", &MESH_EXTENSIONS, !cage, if cage { Pick::Cage } else { Pick::HighPoly }); + } + + fn pick(ctx: &egui::Context, filter: &'static str, exts: &'static [&'static str], multiple: bool, kind: Pick) { + let ctx = ctx.clone(); + wasm_bindgen_futures::spawn_local(async move { + let dialog = rfd::AsyncFileDialog::new().add_filter(filter, exts); + let files = if multiple { dialog.pick_files().await.unwrap_or_default() } else { dialog.pick_file().await.into_iter().collect() }; + for file in files { + let bytes = file.read().await; + INBOX.lock().unwrap_or_else(PoisonError::into_inner).push((file.file_name(), bytes, kind)); + } + ctx.request_repaint(); + }); + } + + /// File › Save: download the project as `.tcraft`. + pub fn save_project(app: &mut TextureCraftApp) { + let name = project_file_name(app); + match app.session.to_project_bytes().map_err(|e| e.to_string()).and_then(|b| download(&name, &b)) { + Ok(()) => { + app.session.modified = false; + app.session.log.push(format!("Saved {name}")); + app.toast(format!("Downloaded {name}"), false); + } + Err(e) => app.toast(format!("Couldn't save {name}: {e}"), true), + } + } + + /// File › Export Mesh: download the working mesh as GLB with its textures embedded (the + /// browser has no save dialog to pick a format; the desktop also offers OBJ). + pub fn export_mesh(app: &mut TextureCraftApp) { + let name = format!("{}.glb", sanitize_file_stem(&app.session.doc.project.name)); + match app.session.mesh_glb_bytes().map_err(|e| e.to_string()).and_then(|b| download(&name, &b)) { + Ok(()) => app.toast(format!("Downloaded {name}"), false), + Err(e) => app.toast(format!("Couldn't export the mesh: {e}"), true), + } + } + + /// Export texture sets `sets` with `file.exportTextures` params (minus `dir` and `ts`): one + /// file downloads as is, several as `_textures.zip`. + pub fn export_textures(app: &mut TextureCraftApp, params: &Value, sets: &[usize]) { + match export(app, params, sets) { + Ok(name) => app.toast(format!("Downloaded {name}"), false), + Err(e) => app.toast(format!("Couldn't export textures: {e}"), true), + } + } + + fn export(app: &mut TextureCraftApp, params: &Value, sets: &[usize]) -> Result { + let opts = texturecraft_engine::file_cmds::export_options(params, "file.exportTextures").map_err(|e| e.to_string())?; + let files = app.session.export_files(sets, &opts).map_err(|e| e.to_string())?; + app.session.log.extend(files.iter().flat_map(|f| f.warnings.iter().cloned())); + let name = match files.as_slice() { + [] => return Err("the preset produced no files".into()), + [f] => { + download(&f.name, &f.bytes)?; + f.name.clone() + } + _ => { + let name = format!("{}_textures.zip", sanitize_file_stem(&app.session.doc.project.name)); + download(&name, &zip_files(&files)?)?; + name + } + }; + app.session.log.push(format!("Exported {} textures ({name})", files.len())); + Ok(name) + } + + /// Start a browser download of `bytes` named `name` (Blob + a temporary ``). + pub fn download(name: &str, bytes: &[u8]) -> Result<(), String> { + let js = |e: wasm_bindgen::JsValue| format!("{e:?}"); + let window = web_sys::window().ok_or("no window")?; + let document = window.document().ok_or("no document")?; + let parts = js_sys::Array::of1(&js_sys::Uint8Array::from(bytes)); + let opts = web_sys::BlobPropertyBag::new(); + opts.set_type(if name.ends_with(".zip") { "application/zip" } else { "application/octet-stream" }); + let blob = web_sys::Blob::new_with_u8_array_sequence_and_options(&parts, &opts).map_err(js)?; + let url = web_sys::Url::create_object_url_with_blob(&blob).map_err(js)?; + let a: web_sys::HtmlAnchorElement = document.create_element("a").map_err(js)?.dyn_into().map_err(|_| "not an anchor")?; + a.set_href(&url); + a.set_download(name); + a.style().set_property("display", "none").map_err(js)?; + document.body().ok_or("no body")?.append_child(&a).map_err(js)?; + a.click(); + a.remove(); + // Revoke once the click has been dispatched; the download keeps its own reference. + let revoke = wasm_bindgen::closure::Closure::once_into_js(move || { + web_sys::Url::revoke_object_url(&url).ok(); + }); + window.set_timeout_with_callback_and_timeout_and_arguments_0(revoke.unchecked_ref(), 10_000).map_err(js)?; + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn zip_round_trips_every_export_file() { + let file = |name: &str, bytes: &[u8]| ExportedFile { name: name.into(), bytes: bytes.to_vec(), width: 1, height: 1, warnings: Vec::new() }; + let files = [file("a_BaseColor.png", b"one"), file("a_Normal.png", &[7u8; 4096])]; + let bytes = zip_files(&files).unwrap(); + let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + assert_eq!(zip.len(), 2); + for f in &files { + let mut out = Vec::new(); + std::io::Read::read_to_end(&mut zip.by_name(&f.name).unwrap(), &mut out).unwrap(); + assert_eq!(out, f.bytes); + } + } +} diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs new file mode 100644 index 00000000..42574f1d --- /dev/null +++ b/crates/ui-egui/src/widgets.rs @@ -0,0 +1,172 @@ +//! Compact widgets matching the app's look: labelled slim sliders, icon buttons, section +//! headers, channel toggle chips and color swatches. + +use egui::{Color32, Response, RichText, Sense, Stroke, Ui, vec2}; + +use crate::theme::TOKENS; + +/// A labelled slider in two rows: "Label ........ value" over a slim track with a round knob. +/// Returns the response of the track (`.changed()` when dragged, `.drag_stopped()` at the end). +pub fn slider(ui: &mut Ui, label: &str, value: &mut f32, range: std::ops::RangeInclusive, decimals: usize) -> Response { + let (lo, hi) = (*range.start(), *range.end()); + let w = ui.available_width(); + ui.horizontal(|ui| { + ui.label(RichText::new(label).color(TOKENS.text)); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + let mut text = format!("{:.*}", decimals, value); + let r = ui.add(egui::TextEdit::singleline(&mut text).desired_width(44.0).horizontal_align(egui::Align::RIGHT).frame(egui::Frame::NONE)); + if r.lost_focus() + && let Ok(v) = text.trim().parse::() + && v.is_finite() + { + *value = v.clamp(lo, hi); + } + }); + }); + let (rect, mut resp) = ui.allocate_exact_size(vec2(w, 14.0), Sense::click_and_drag()); + if (resp.dragged() || resp.clicked()) + && let Some(p) = resp.interact_pointer_pos() + { + let t = ((p.x - rect.left() - 5.0) / (rect.width() - 10.0).max(1.0)).clamp(0.0, 1.0); + let v = lo + (hi - lo) * t; + if (v - *value).abs() > f32::EPSILON { + *value = v; + resp.mark_changed(); + } + } + let p = ui.painter(); + let y = rect.center().y; + let (x0, x1) = (rect.left() + 5.0, rect.right() - 5.0); + let t = if hi > lo { ((*value - lo) / (hi - lo)).clamp(0.0, 1.0) } else { 0.0 }; + let xk = x0 + (x1 - x0) * t; + p.line_segment([egui::pos2(x0, y), egui::pos2(x1, y)], Stroke::new(2.0, Color32::from_rgb(0x4a, 0x4a, 0x4a))); + p.line_segment([egui::pos2(x0, y), egui::pos2(xk, y)], Stroke::new(2.0, Color32::from_rgb(0xb8, 0xb8, 0xb8))); + let knob = if resp.hovered() || resp.dragged() { TOKENS.text_strong } else { Color32::from_rgb(0xc8, 0xc8, 0xc8) }; + p.circle(egui::pos2(xk, y), 4.5, Color32::from_rgb(0x26, 0x26, 0x26), Stroke::new(1.5, knob)); + resp +} + +/// A small inline slider for the top tool bar: "Label value" above a short track. +pub fn bar_slider(ui: &mut Ui, label: &str, value: &mut f32, range: std::ops::RangeInclusive, width: f32) -> Response { + let (lo, hi) = (*range.start(), *range.end()); + let (rect, mut resp) = ui.allocate_exact_size(vec2(width, 28.0), Sense::click_and_drag()); + if (resp.dragged() || resp.clicked()) + && let Some(p) = resp.interact_pointer_pos() + { + let t = ((p.x - rect.left() - 4.0) / (rect.width() - 8.0).max(1.0)).clamp(0.0, 1.0); + let v = lo + (hi - lo) * t; + if (v - *value).abs() > f32::EPSILON { + *value = v; + resp.mark_changed(); + } + } + let p = ui.painter(); + let font = egui::FontId::proportional(11.0); + p.text(rect.left_top() + vec2(2.0, 2.0), egui::Align2::LEFT_TOP, label, font.clone(), TOKENS.text); + p.text(rect.right_top() + vec2(-2.0, 2.0), egui::Align2::RIGHT_TOP, format!("{:.0}", value), font, TOKENS.text_strong); + let y = rect.bottom() - 7.0; + let (x0, x1) = (rect.left() + 4.0, rect.right() - 4.0); + let t = if hi > lo { ((*value - lo) / (hi - lo)).clamp(0.0, 1.0) } else { 0.0 }; + let xk = x0 + (x1 - x0) * t; + p.line_segment([egui::pos2(x0, y), egui::pos2(x1, y)], Stroke::new(1.5, Color32::from_rgb(0x4a, 0x4a, 0x4a))); + p.line_segment([egui::pos2(x0, y), egui::pos2(xk, y)], Stroke::new(1.5, Color32::from_rgb(0xc0, 0xc0, 0xc0))); + p.circle(egui::pos2(xk, y), 4.0, Color32::from_rgb(0x22, 0x22, 0x22), Stroke::new(1.5, Color32::from_rgb(0xd0, 0xd0, 0xd0))); + resp +} + +/// One line of `text` cut with "…" to fit `max_width`, for list rows that shrink with their panel. +pub fn elided(painter: &egui::Painter, text: impl Into, size: f32, color: Color32, max_width: f32) -> std::sync::Arc { + let mut job = egui::text::LayoutJob::simple_singleline(text.into(), egui::FontId::proportional(size), color); + job.wrap = egui::text::TextWrapping::truncate_at_width(max_width.max(0.0)); + painter.layout_job(job) +} + +/// A square icon button (Phosphor glyph). `selected` draws the accent background. +pub fn icon_button(ui: &mut Ui, icon: &str, tooltip: &str, selected: bool, size: f32) -> Response { + let (rect, resp) = ui.allocate_exact_size(vec2(size, size), Sense::click()); + let p = ui.painter(); + let bg = if selected { + Color32::from_rgb(0x3d, 0x3d, 0x3d) + } else if resp.hovered() { + Color32::from_rgb(0x33, 0x33, 0x33) + } else { + Color32::TRANSPARENT + }; + p.rect_filled(rect, 3.0, bg); + if selected { + p.rect_stroke(rect, 3.0, Stroke::new(1.0, Color32::from_rgb(0x55, 0x55, 0x55)), egui::StrokeKind::Inside); + } + let color = if selected || resp.hovered() { TOKENS.text_strong } else { TOKENS.text }; + p.text(rect.center(), egui::Align2::CENTER_CENTER, icon, egui::FontId::proportional(size * 0.62), color); + resp.on_hover_text(tooltip) +} + +/// A collapsible section header with a chevron ("> Size"). +pub fn section(ui: &mut Ui, id: &str, title: &str, default_open: bool, body: impl FnOnce(&mut Ui)) { + let id = ui.make_persistent_id(id); + let mut state = egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, default_open); + let open = state.is_open(); + let resp = ui + .horizontal(|ui| { + let chev = if open { egui_phosphor::regular::CARET_DOWN } else { egui_phosphor::regular::CARET_RIGHT }; + ui.label(RichText::new(chev).size(10.0).color(TOKENS.text_dim)); + ui.label(RichText::new(title).strong().color(TOKENS.text_strong)); + }) + .response + .interact(Sense::click()); + if resp.clicked() { + state.toggle(ui); + } + state.show_body_unindented(ui, |ui| { + ui.indent(id.with("in"), body); + }); +} + +/// A Painter-style channel chip ("color", "metal", "rough"…): toggled = accent outline. +pub fn chip(ui: &mut Ui, text: &str, on: bool) -> Response { + let galley = ui.painter().layout_no_wrap(text.into(), egui::FontId::proportional(11.0), TOKENS.text); + let size = vec2(galley.size().x + 14.0, 20.0); + let (rect, resp) = ui.allocate_exact_size(size, Sense::click()); + let p = ui.painter(); + p.rect_filled(rect, 3.0, if on { Color32::from_rgb(0x3a, 0x3a, 0x3a) } else { Color32::from_rgb(0x2a, 0x2a, 0x2a) }); + p.rect_stroke( + rect, + 3.0, + Stroke::new(1.0, if on { Color32::from_rgb(0x9a, 0x9a, 0x9a) } else { Color32::from_rgb(0x3a, 0x3a, 0x3a) }), + egui::StrokeKind::Inside, + ); + p.text(rect.center(), egui::Align2::CENTER_CENTER, text, egui::FontId::proportional(11.0), if on { TOKENS.text_strong } else { TOKENS.text_dim }); + resp +} + +/// A color swatch button for a linear RGB value; opens a picker on click. Returns true when +/// the color changed. +pub fn color_swatch(ui: &mut Ui, id: &str, linear: &mut [f32; 3], width: f32) -> bool { + let srgb = linear.map(|v| (texturecraft_engine::texel::linear_to_srgb(v) * 255.0 + 0.5) as u8); + let mut c = Color32::from_rgb(srgb[0], srgb[1], srgb[2]); + let (rect, resp) = ui.allocate_exact_size(vec2(width, 18.0), Sense::click()); + ui.painter().rect_filled(rect, 2.0, c); + ui.painter().rect_stroke(rect, 2.0, Stroke::new(1.0, Color32::from_rgb(0x55, 0x55, 0x55)), egui::StrokeKind::Inside); + let popup = ui.make_persistent_id(id); + if resp.clicked() { + egui::Popup::toggle_id(ui.ctx(), popup); + } + let mut changed = false; + egui::Popup::from_response(&resp).id(popup).close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside).show(|ui| { + if egui::color_picker::color_picker_color32(ui, &mut c, egui::color_picker::Alpha::Opaque) { + let lin = [c.r(), c.g(), c.b()].map(|v| texturecraft_engine::texel::srgb_to_linear(v as f32 / 255.0)); + *linear = lin; + changed = true; + } + }); + changed +} + +/// Panel title row with optional trailing content. +pub fn panel_header(ui: &mut Ui, title: &str, trailing: impl FnOnce(&mut Ui)) { + ui.horizontal(|ui| { + ui.set_min_height(22.0); + ui.label(crate::theme::caption(title)); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), trailing); + }); +} diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs new file mode 100644 index 00000000..438ec4e7 --- /dev/null +++ b/crates/ui-egui/src/windows.rs @@ -0,0 +1,874 @@ +//! Floating windows, the dockable panel bodies (display, shader, history, log) and the bake mode +//! panels. + +use egui::{Color32, RichText, Ui, vec2}; +use egui_phosphor::regular as ic; +use serde_json::json; +use texturecraft_engine::bake_cmds; +use texturecraft_engine::core::project::ENV_RESOURCE_PREFIX; +use texturecraft_engine::core::{BakeMatch, BakeSettings, BakedMap, ToneMapping}; +use texturecraft_engine::geom::primitives::BUILTINS; +use texturecraft_render::ibl::ENVIRONMENTS; + +use crate::TextureCraftApp; +use crate::state::Mode; +use crate::theme::{TOKENS, caption}; +use crate::widgets::{icon_button, slider}; + +pub fn show(app: &mut TextureCraftApp, ctx: &egui::Context) { + new_project(app, ctx); + about(app, ctx); + settings(app, ctx); + crate::keymap::window(app, ctx); + crate::windows::export(app, ctx); + if app.ui.mode == Mode::Baking { + bake_bar(app, ctx); + } +} + +pub(crate) fn window<'a>(title: &'a str, open: &'a mut bool) -> egui::Window<'a> { + egui::Window::new(RichText::new(title.to_uppercase()).size(10.5).strong()).open(open).resizable(true).collapsible(false).default_width(300.0) +} + +/// The Display Settings panel body (a dock tab). +pub(crate) fn display(app: &mut TextureCraftApp, ui: &mut Ui) { + let ctx = ui.ctx().clone(); + let mut d = app.session.doc.project.display.clone(); + let before = d.clone(); + ui.label(caption("Environment settings")); + let mut hdrs: Vec<&String> = app.session.images.iter().filter(|(_, i)| !i.srgb).map(|(k, _)| k).collect(); + hdrs.sort(); + let mut import_hdr = false; + let shown = d.environment_resource().unwrap_or(&d.environment).to_string(); + egui::ComboBox::from_label("Environment map").selected_text(shown).show_ui(ui, |ui| { + for e in ENVIRONMENTS { + ui.selectable_value(&mut d.environment, e.to_string(), e); + } + ui.separator(); + for name in hdrs { + ui.selectable_value(&mut d.environment, format!("{ENV_RESOURCE_PREFIX}{name}"), name.as_str()); + } + import_hdr = ui + .selectable_label(false, format!("{} Import HDR...", ic::FILE_ARROW_UP)) + .on_hover_text("Use an equirectangular .hdr or .exr image as the environment") + .clicked(); + }); + slider(ui, "Environment opacity", &mut d.env_opacity, 0.0..=1.0, 2); + slider(ui, "Environment exposure (EV)", &mut d.env_exposure, -5.0..=5.0, 2); + slider(ui, "Environment blur", &mut d.env_blur, 0.0..=1.0, 2); + slider(ui, "Environment rotation", &mut d.env_rotation, 0.0..=360.0, 0); + ui.checkbox(&mut d.shadows, "Key light (shadow-casting light, direct only)"); + ui.add_space(6.0); + ui.label(caption("Camera settings")); + slider(ui, "Field of view (deg)", &mut d.fov, 5.0..=120.0, 0); + ui.horizontal(|ui| { + ui.label("Tone mapping"); + ui.selectable_value(&mut d.tone_mapping, ToneMapping::Linear, "Linear"); + ui.selectable_value(&mut d.tone_mapping, ToneMapping::AcesFilmic, "ACES filmic"); + }); + ui.add_space(6.0); + ui.label(caption("Viewer settings")); + slider(ui, "Ambient occlusion intensity", &mut d.ao_intensity, 0.0..=1.0, 2); + ui.checkbox(&mut app.ui.wireframe_3d, "Show wireframe in the 3D view"); + ui.checkbox(&mut app.ui.uv_wireframe, "Show UV wireframe in the 2D view"); + ui.checkbox(&mut d.taa, "Temporal anti-aliasing (refines the 3D view while it is still)"); + if d != before { + app.session.camera.fov = d.fov; + app.session.doc.project.display = d; + } + if import_hdr { + crate::web_files::import_environment(app, &ctx); + } +} + +/// The Shader Settings panel body (a dock tab). +pub(crate) fn shader(app: &mut TextureCraftApp, ui: &mut Ui) { + let mut d = app.session.doc.project.display.clone(); + let before = d.clone(); + let model = |sg: bool| if sg { "TextureCraft PBR - Specular Glossiness" } else { "TextureCraft PBR - Metallic Roughness" }; + egui::ComboBox::from_id_salt("shader_model").width(260.0).selected_text(model(d.spec_gloss)).show_ui(ui, |ui| { + for sg in [false, true] { + ui.selectable_value(&mut d.spec_gloss, sg, model(sg)); + } + }); + let channels = if d.spec_gloss { + "Base color is the diffuse color, Specular the F0 color and Glossiness 1 - roughness (add these channels in the texture set settings)." + } else { + "Base color, Metallic, Roughness and Specular level." + }; + ui.label(RichText::new(format!("GGX specular with height-correlated Smith visibility, Lambert diffuse, split-sum image-based lighting with spherical-harmonics irradiance and horizon fading. {channels}")).color(TOKENS.text_dim)); + ui.add_space(6.0); + ui.checkbox(&mut d.parallax, "Parallax occlusion (height channel)") + .on_hover_text("Offsets the textures by the height channel, at the height to normal strength"); + if d != before { + app.session.doc.project.display = d; + } + let ts = app.session.ts(); + if let Some(set) = app.session.doc.project.texture_sets.get(ts) { + let mut hs = set.height_strength; + if slider(ui, "Height to normal strength", &mut hs, 0.0..=10.0, 2).drag_stopped() { + app.run("textureSet.settings", json!({"ts": ts, "heightStrength": hs})); + } + } + let mut ao = app.session.doc.project.display.ao_intensity; + if slider(ui, "AO intensity", &mut ao, 0.0..=1.0, 2).changed() { + app.session.doc.project.display.ao_intensity = ao; + } +} + +/// The History panel body (a dock tab). +pub(crate) fn history(app: &mut TextureCraftApp, ui: &mut Ui) { + let labels = app.session.history_labels(); + egui::ScrollArea::vertical().max_height(320.0).show(ui, |ui| { + ui.label(RichText::new("Project opened").color(TOKENS.text_dim)); + for l in &labels { + ui.label(l); + } + if let Some(r) = app.session.redo_label() { + ui.label(RichText::new(format!("{r} (undone)")).color(TOKENS.text_dim)); + } + }); + ui.horizontal(|ui| { + if ui.add_enabled(app.session.can_undo(), egui::Button::new(format!("{} Undo", ic::ARROW_COUNTER_CLOCKWISE))).clicked() { + app.run("edit.undo", json!({})); + } + if ui.add_enabled(app.session.can_redo(), egui::Button::new(format!("{} Redo", ic::ARROW_CLOCKWISE))).clicked() { + app.run("edit.redo", json!({})); + } + }); +} + +fn new_project(app: &mut TextureCraftApp, ctx: &egui::Context) { + if !app.ui.windows.new_project { + return; + } + let mut open = true; + let mut create = false; + egui::Window::new("New project").open(&mut open).collapsible(false).resizable(false).anchor(egui::Align2::CENTER_CENTER, vec2(0.0, 0.0)).show(ctx, |ui| { + ui.label(caption("Basic settings")); + egui::Grid::new("np").num_columns(2).spacing(vec2(12.0, 8.0)).show(ui, |ui| { + ui.label("3D mesh"); + ui.horizontal(|ui| { + let label = app.ui.new_project.file.clone().unwrap_or_else(|| { + BUILTINS.iter().find(|(id, _)| *id == app.ui.new_project.mesh).map(|(_, l)| format!("Built-in: {l}")).unwrap_or_default() + }); + egui::ComboBox::from_id_salt("np_mesh").width(200.0).selected_text(label).show_ui(ui, |ui| { + for (id, l) in BUILTINS { + if ui.selectable_label(app.ui.new_project.mesh == id && app.ui.new_project.file.is_none(), format!("Built-in: {l}")).clicked() { + app.ui.new_project.mesh = id.to_string(); + app.ui.new_project.file = None; + } + } + }); + if file_dialogs_available() && ui.button("Select...").clicked() + && let Some(p) = pick_mesh_file() { + app.ui.new_project.file = Some(p); + } + }); + ui.end_row(); + ui.label("Project template"); + ui.label("PBR - Metallic Roughness (TextureCraft)"); + ui.end_row(); + ui.label("Document resolution"); + egui::ComboBox::from_id_salt("np_res").selected_text(format!("{0} x {0}", app.ui.new_project.resolution)).show_ui(ui, |ui| { + for r in [512u32, 1024, 2048, 4096] { + ui.selectable_value(&mut app.ui.new_project.resolution, r, format!("{r} x {r}")); + } + }); + ui.end_row(); + ui.label("Name"); + ui.text_edit_singleline(&mut app.ui.new_project.name); + ui.end_row(); + }); + ui.add_space(8.0); + ui.horizontal(|ui| { + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if ui.add(egui::Button::new(RichText::new("Create").color(Color32::WHITE)).fill(TOKENS.accent)).clicked() { + create = true; + } + if ui.button("Cancel").clicked() { + app.ui.windows.new_project = false; + } + }); + }); + }); + if create { + let np = app.ui.new_project.clone(); + let r = match &np.file { + Some(path) => file_new_from_mesh(app, path, np.resolution, &np.name), + None => app.run("project.new", json!({"mesh": np.mesh, "resolution": np.resolution, "name": np.name})).map(|_| ()), + }; + if r.is_some() { + app.ui.windows.new_project = false; + app.ui.mode = Mode::Painting; + } + } + if !open { + app.ui.windows.new_project = false; + } +} + +fn settings(app: &mut TextureCraftApp, ctx: &egui::Context) { + let mut open = app.ui.windows.settings; + window("Settings", &mut open).default_width(360.0).show(ctx, |ui| { + let p = &mut app.ui.prefs; + ui.label(caption("Interface")); + slider(ui, "Interface scale", &mut p.ui_scale, 0.75..=2.0, 2); + ui.add_space(6.0); + #[cfg(not(target_arch = "wasm32"))] + { + ui.label(caption("Project")); + ui.horizontal(|ui| { + ui.label("Autosave every"); + ui.add(egui::DragValue::new(&mut p.autosave_minutes).range(0..=120).suffix(" min")); + }); + ui.label(RichText::new(format!("0 turns autosave off. Copies go to {}", crate::user_dir("autosave").display())).size(10.5).color(TOKENS.text_dim)); + ui.add_space(6.0); + ui.label(caption("Folders")); + for (label, dir) in [("Smart materials", crate::library_dir()), ("UI layouts", crate::user_dir("layouts"))] { + ui.horizontal(|ui| { + ui.label(label); + ui.label(RichText::new(dir.display().to_string()).size(10.5).color(TOKENS.text_dim)); + }); + } + ui.add_space(6.0); + } + ui.label(caption("Keyboard")); + if ui.button("Edit keyboard shortcuts...").on_hover_text("Rebind any menu item, tool or command").clicked() { + app.ui.windows.shortcuts = true; + } + }); + app.ui.windows.settings = open; +} + +fn about(app: &mut TextureCraftApp, ctx: &egui::Context) { + let mut open = app.ui.windows.about; + egui::Window::new("About TextureCraft").open(&mut open).collapsible(false).resizable(false).anchor(egui::Align2::CENTER_CENTER, vec2(0.0, 0.0)).show( + ctx, + |ui| { + ui.vertical_centered(|ui| { + ui.label(RichText::new("TextureCraft").size(22.0).strong().color(TOKENS.text_strong)); + ui.label(format!("Version {}", texturecraft_engine::VERSION)); + ui.add_space(6.0); + ui.label("3D texture painting: layers, masks, smart materials, mesh map baking and export."); + ui.label("Free and open source (MIT OR Apache-2.0), written in Rust."); + ui.add_space(6.0); + ui.hyperlink_to("Join the ArtCraft community on Discord", "https://discord.gg/artcraft"); + ui.hyperlink_to("getartcraft.com/apps/texturecraft", "https://getartcraft.com/apps/texturecraft"); + ui.add_space(6.0); + ui.label(RichText::new("Made by the ArtCraft team and community.").color(TOKENS.text_dim)); + }); + }, + ); + app.ui.windows.about = open; +} + +/// The Log panel body (a dock tab). +pub(crate) fn log(app: &mut TextureCraftApp, ui: &mut Ui) { + egui::ScrollArea::vertical().max_height(300.0).stick_to_bottom(true).show(ui, |ui| { + for l in &app.session.log { + ui.label(RichText::new(l).monospace()); + } + }); +} + +// --------------------------------------------------------------------------------------------- +// Bake mode (Painter's "Bake Mesh Maps": texture sets, bakers, common and per-map parameters) + +pub fn bake_left(app: &mut TextureCraftApp, ui: &mut Ui) { + egui::Panel::left("bake_left") + .resizable(true) + .default_size(320.0) + .frame(egui::Frame::NONE.fill(TOKENS.panel).inner_margin(egui::Margin::symmetric(8, 6))) + .show(ui, |ui| bake_body(app, ui)); +} + +/// The bake settings, scrollable (the Assets dock slot shows them in bake mode). +pub fn bake_body(app: &mut TextureCraftApp, ui: &mut Ui) { + egui::ScrollArea::vertical().id_salt("bake_body").auto_shrink([false; 2]).show(ui, |ui| bake_panel(app, ui)); +} + +fn bake_panel(app: &mut TextureCraftApp, ui: &mut Ui) { + ui.label(caption("Texture set list")); + for i in 0..app.session.set_count() { + let Some(t) = app.session.doc.project.texture_sets.get(i) else { continue }; + let (name, res, mut on) = (t.name.clone(), t.bake.resolution, t.bake.enabled); + ui.horizontal(|ui| { + if ui.checkbox(&mut on, "").on_hover_text("Bake with \"Bake selected textures\"").changed() { + app.run("bake.settings", json!({ "ts": i, "enabled": on })); + } + if ui.selectable_label(i == app.session.ts(), format!("{name} ({res}x{res})")).clicked() { + app.run("textureSet.select", json!({ "ts": i })); + } + }); + } + ui.separator(); + let ts = app.session.ts(); + let Some(before) = app.session.doc.project.texture_sets.get(ts).map(|t| t.bake.clone()) else { return }; + let mut b = before.clone(); + // The selected baker entry: None = common parameters. + let sel_id = egui::Id::new("bake_selected_baker"); + let mut selected: Option = ui.data(|d| d.get_temp(sel_id)).flatten(); + ui.label(caption("Mesh map bakers")); + if ui.selectable_label(selected.is_none(), format!("{} Common parameters", ic::GEAR)).clicked() { + selected = None; + } + for m in BakedMap::ALL { + ui.horizontal(|ui| { + let mut on = b.maps.contains(&m); + if ui.checkbox(&mut on, "").changed() { + if on { + b.maps.insert(m); + } else { + b.maps.remove(&m); + } + } + if ui.selectable_label(selected == Some(m), m.label()).clicked() { + selected = Some(m); + } + }); + } + ui.data_mut(|d| d.insert_temp(sel_id, selected)); + ui.separator(); + match selected { + None => common_params(app, ui, &mut b), + Some(m) => map_params(ui, m, &mut b), + } + if b.direction == [0.0; 3] { + b.direction = before.direction; + } + if b != before { + let mut p = bake_cmds::settings_params(&b); + p["ts"] = ts.into(); + app.run("bake.settings", p); + } +} + +fn hint(ui: &mut Ui, text: &str) { + ui.label(RichText::new(text).color(TOKENS.text_dim).size(11.0)); +} + +fn common_params(app: &mut TextureCraftApp, ui: &mut Ui, b: &mut BakeSettings) { + ui.label(caption("Common parameters")); + egui::ComboBox::from_label("Output size").selected_text(format!("{}", b.resolution)).show_ui(ui, |ui| { + for r in [256u32, 512, 1024, 2048, 4096, 8192] { + ui.selectable_value(&mut b.resolution, r, format!("{r}")); + } + }); + egui::ComboBox::from_label("Antialiasing") + .selected_text(match b.antialiasing { + 2 => "Subsampling 2x2", + 4 => "Subsampling 4x4", + 8 => "Subsampling 8x8", + _ => "None", + }) + .show_ui(ui, |ui| { + for (v, l) in [(1u32, "None"), (2, "Subsampling 2x2"), (4, "Subsampling 4x4"), (8, "Subsampling 8x8")] { + ui.selectable_value(&mut b.antialiasing, v, l); + } + }); + let mut dil = b.dilation as f32; + if slider(ui, "Dilation width", &mut dil, 0.0..=64.0, 0).changed() { + b.dilation = dil as u32; + } + + ui.add_space(6.0); + ui.label(RichText::new("High definition meshes").strong().color(TOKENS.text_strong)); + let ctx = ui.ctx().clone(); + let highs: Vec<(String, usize)> = app.session.bake_meshes.high.iter().map(|h| (h.name.clone(), h.mesh.triangle_count())).collect(); + let mut remove = None; + for (i, (name, tris)) in highs.iter().enumerate() { + ui.horizontal(|ui| { + ui.label(format!("{name} ({tris} triangles)")); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if icon_button(ui, ic::TRASH, "Remove", false, 18.0).clicked() { + remove = Some(i); + } + }); + }); + } + if highs.is_empty() { + hint(ui, "None: the low poly is its own high poly."); + } + if let Some(i) = remove { + app.run("bake.highPoly.remove", json!({ "index": i })); + } + if ui.button(format!("{} Add high definition meshes", ic::PLUS)).clicked() { + crate::web_files::import_bake_mesh(app, &ctx, false); + } + ui.checkbox(&mut b.use_low_as_high, "Use low poly mesh as high poly mesh"); + egui::ComboBox::from_label("Match") + .selected_text(match b.matching { + BakeMatch::Always => "Always", + BakeMatch::ByMeshName => "By mesh name", + }) + .show_ui(ui, |ui| { + ui.selectable_value(&mut b.matching, BakeMatch::Always, "Always"); + ui.selectable_value(&mut b.matching, BakeMatch::ByMeshName, "By mesh name"); + }); + if b.matching == BakeMatch::ByMeshName { + for (label, suffix) in [("Low poly mesh suffix", &mut b.low_suffix), ("High poly mesh suffix", &mut b.high_suffix)] { + ui.horizontal(|ui| { + ui.label(label); + ui.add(egui::TextEdit::singleline(suffix).desired_width(80.0)); + }); + } + } + slider(ui, "Max frontal distance", &mut b.max_frontal, 0.0..=0.2, 3); + slider(ui, "Max rear distance", &mut b.max_rear, 0.0..=0.2, 3); + hint(ui, "Relative to the bounding box diagonal."); + ui.checkbox(&mut b.average_normals, "Average normals"); + + ui.add_space(6.0); + let cage = app.session.bake_meshes.cage.as_ref().map(|c| c.name.clone()); + ui.horizontal(|ui| { + ui.label(format!("Cage: {}", cage.as_deref().unwrap_or("none"))); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if cage.is_some() && icon_button(ui, ic::TRASH, "Remove the cage", false, 18.0).clicked() { + app.run("bake.cage.clear", json!({})); + } + if icon_button(ui, ic::FOLDER_OPEN, "Load a cage (the low poly, inflated)", false, 18.0).clicked() { + crate::web_files::import_bake_mesh(app, &ctx, true); + } + }); + }); + + ui.add_space(6.0); + if ui.button("Apply to all texture sets").on_hover_text("Copy these bake settings to every texture set").clicked() { + let mut p = bake_cmds::settings_params(b); + p["ts"] = "all".into(); + if let Some(o) = p.as_object_mut() { + o.remove("enabled"); + } + app.run("bake.settings", p); + } +} + +fn map_params(ui: &mut Ui, m: BakedMap, b: &mut BakeSettings) { + ui.label(caption(m.label())); + match m { + BakedMap::AmbientOcclusion | BakedMap::Thickness | BakedMap::BentNormal => { + let mut rays = b.rays as f32; + if slider(ui, "Secondary rays", &mut rays, 4.0..=256.0, 0).changed() { + b.rays = rays as u32; + } + slider(ui, "Max occluder distance", &mut b.max_distance, 0.01..=1.0, 2); + hint(ui, "Shared by ambient occlusion, thickness and bent normals."); + } + BakedMap::Curvature => { + slider(ui, "Strength", &mut b.curvature_strength, 0.1..=4.0, 2); + ui.checkbox(&mut b.curvature_from_normal, "From normal map"); + hint(ui, "From normal map: curvature of the baked normal map's detail instead of the mesh."); + } + BakedMap::WorldDirection => { + for (axis, v) in ["X", "Y", "Z"].iter().zip(b.direction.iter_mut()) { + slider(ui, &format!("Direction {axis}"), v, -1.0..=1.0, 2); + } + hint(ui, "White where the surface faces the direction, black where it faces away."); + } + BakedMap::Normal => hint(ui, "Tangent-space normals of the high definition meshes."), + BakedMap::WorldNormal => hint(ui, "World-space normals of the high poly (or the low poly)."), + BakedMap::Id => hint(ui, "One color per mesh part."), + BakedMap::Position => hint(ui, "Position in the low poly's bounding box."), + BakedMap::Height => hint(ui, "High poly height between the max rear (black) and max frontal (white) distances."), + BakedMap::Opacity => hint(ui, "White where the high poly covers the low poly, times its vertex alpha."), + BakedMap::ColorMap => hint(ui, "Vertex colors of the high definition meshes."), + } +} + +fn bake_bar(app: &mut TextureCraftApp, ctx: &egui::Context) { + egui::Area::new(egui::Id::new("bake_bar")).anchor(egui::Align2::CENTER_BOTTOM, vec2(0.0, -40.0)).show(ctx, |ui| { + ui.horizontal(|ui| { + if let Some(p) = app.bake_progress() { + ui.add(egui::ProgressBar::new(p).desired_width(220.0).show_percentage()); + if ui.button("Cancel").clicked() { + app.cancel_bake(); + } + } else { + let sets: Vec = app.session.doc.project.texture_sets.iter().enumerate().filter(|(_, t)| t.bake.enabled).map(|(i, _)| i).collect(); + let label = RichText::new(format!("Bake selected textures ({})", sets.len())).color(Color32::WHITE); + if ui.add_enabled(!sets.is_empty(), egui::Button::new(label).fill(TOKENS.accent)).clicked() { + app.start_bake(sets); + } + } + if ui.button("Return to painting mode").clicked() { + app.ui.mode = Mode::Painting; + } + }); + }); +} + +// --------------------------------------------------------------------------------------------- +// Files (wired to texturecraft-formats once it lands) + +pub fn file_dialogs_available() -> bool { + cfg!(not(target_arch = "wasm32")) +} + +#[cfg(not(target_arch = "wasm32"))] +fn pick_mesh_file() -> Option { + rfd::FileDialog::new().add_filter("3D meshes", &texturecraft_engine::formats::mesh_import::MESH_EXTENSIONS).pick_file().map(|p| p.display().to_string()) +} + +#[cfg(target_arch = "wasm32")] +fn pick_mesh_file() -> Option { + None +} + +fn file_new_from_mesh(app: &mut TextureCraftApp, path: &str, resolution: u32, name: &str) -> Option<()> { + app.run("file.new", json!({"mesh": path, "resolution": resolution, "name": name})).map(|_| ()) +} + +/// Remember a project path in File ▸ Recent projects. +pub fn remember(app: &mut TextureCraftApp, path: &str) { + app.ui.recent.retain(|p| p != path); + app.ui.recent.insert(0, path.to_string()); + app.ui.recent.truncate(10); +} + +/// Window › Save / Load UI layout: panels, windows and view settings as a JSON file. +#[cfg(not(target_arch = "wasm32"))] +pub fn layout_file(app: &mut TextureCraftApp, save: bool) { + let dir = crate::user_dir("layouts"); + let dialog = rfd::FileDialog::new().add_filter("UI layout", &["json"]).set_directory(&dir); + let result = if save { + let Some(path) = dialog.set_file_name("layout.json").save_file() else { return }; + let layout = crate::UiState { recent: Vec::new(), ..app.ui.clone() }; + std::fs::create_dir_all(&dir) + .map_err(|e| e.to_string()) + .and_then(|_| serde_json::to_vec_pretty(&layout).map_err(|e| e.to_string())) + .and_then(|bytes| std::fs::write(&path, bytes).map_err(|e| e.to_string())) + .map(|_| format!("Saved UI layout {}", path.display())) + } else { + let Some(path) = dialog.pick_file() else { return }; + std::fs::read(&path) + .map_err(|e| e.to_string()) + .and_then(|bytes| serde_json::from_slice::(&bytes).map_err(|e| e.to_string())) + .map(|layout| { + app.ui.adopt_layout(layout); + format!("Loaded UI layout {}", path.display()) + }) + }; + match result { + Ok(msg) => app.toast(msg, false), + Err(e) => app.toast(format!("UI layout: {e}"), true), + } +} + +/// The browser has no file paths; layouts persist with the rest of the UI state. +#[cfg(target_arch = "wasm32")] +pub fn layout_file(app: &mut TextureCraftApp, _save: bool) { + app.toast("Saving UI layouts to files is available in the desktop app", false); +} + +/// File menu actions that need dialogs. Returns true when handled. +#[cfg(not(target_arch = "wasm32"))] +pub fn file_action(app: &mut TextureCraftApp, _ctx: &egui::Context, id: &str) -> bool { + match id { + "file.open" => { + let pick = rfd::FileDialog::new().add_filter("TextureCraft project or mesh", crate::web_files::OPEN_EXTS).pick_file(); + if let Some(p) = pick { + open_path(app, &p.display().to_string()); + } + true + } + "file.save" if app.session.path.is_some() => { + if let Some(r) = app.run("file.save", json!({})) { + let p = r.get("path").and_then(|p| p.as_str()).unwrap_or_default().to_string(); + app.toast(format!("Saved {p}"), false); + } + true + } + "file.save" | "file.saveAs" | "file.saveCopy" => { + let name = crate::web_files::project_file_name(app); + if let Some(p) = rfd::FileDialog::new().add_filter("TextureCraft project", &[texturecraft_engine::core::PROJECT_EXT]).set_file_name(name).save_file() { + let path = p.display().to_string(); + let cmd = if id == "file.saveCopy" { "file.saveCopy" } else { "file.saveAs" }; + if app.run(cmd, json!({"path": path})).is_some() { + remember(app, &path); + app.toast(format!("Saved {path}"), false); + } + } + true + } + "file.importResources" | "resource.import" => { + let picks = rfd::FileDialog::new().add_filter("Images", crate::web_files::IMAGE_EXTS).pick_files(); + for p in picks.unwrap_or_default() { + if let Some(r) = app.run("resource.import", json!({"path": p.display().to_string()})) { + let name = r.get("name").and_then(|n| n.as_str()).unwrap_or_default().to_string(); + app.toast(format!("Imported {name}"), false); + } + } + app.ui.dock.focus(crate::docking::Panel::Assets, _ctx.content_rect()); + true + } + "file.exportMesh" => { + let name = format!("{}.obj", texturecraft_engine::formats::export::sanitize_file_stem(&app.session.doc.project.name)); + if let Some(p) = + rfd::FileDialog::new().add_filter("Wavefront OBJ", &["obj"]).add_filter("glTF binary with textures", &["glb"]).set_file_name(name).save_file() + && let Some(r) = app.run("file.exportMesh", json!({"path": p.display().to_string()})) + { + let p = r.get("path").and_then(|p| p.as_str()).unwrap_or_default().to_string(); + app.toast(format!("Exported mesh to {p}"), false); + } + true + } + "file.reimportMesh" if app.session.mesh_source.is_none() => { + if let Some(p) = rfd::FileDialog::new().add_filter("3D meshes", &texturecraft_engine::formats::mesh_import::MESH_EXTENSIONS).pick_file() { + app.run("file.reimportMesh", json!({"path": p.display().to_string()})); + } + true + } + _ => false, + } +} + +/// Open a project or mesh by path (menus, Recent projects, drag and drop). +#[cfg(not(target_arch = "wasm32"))] +pub fn open_path(app: &mut TextureCraftApp, path: &str) { + if app.run("file.open", json!({"path": path})).is_some() { + remember(app, path); + app.ui.mode = Mode::Painting; + } +} + +/// In the browser, Open and Import use the file picker and Save downloads the project. +#[cfg(target_arch = "wasm32")] +pub fn file_action(app: &mut TextureCraftApp, ctx: &egui::Context, id: &str) -> bool { + match id { + "file.open" => crate::web_files::open(ctx), + "file.importResources" | "resource.import" => crate::web_files::import_images(ctx), + "file.save" | "file.saveAs" | "file.saveCopy" => crate::web_files::save_project(app), + "file.exportMesh" => crate::web_files::export_mesh(app), + _ => return false, + } + true +} + +/// Texture sets ticked in the export dialog. +fn export_selection(app: &TextureCraftApp) -> Vec { + (0..app.session.set_count()).filter(|i| !app.ui.export.skip.contains(i)).collect() +} + +/// Export the selected texture sets; false when a command failed (its error is already shown). +#[cfg(not(target_arch = "wasm32"))] +fn export_sets(app: &mut TextureCraftApp) -> bool { + let mut written = 0usize; + for ts in export_selection(app) { + let mut p = export_params(app); + p["dir"] = json!(app.ui.export.dir); + p["ts"] = json!(ts); + match app.run("file.exportTextures", p) { + Some(r) => written += r.get("files").and_then(|f| f.as_array()).map(Vec::len).unwrap_or(0), + None => return false, + } + } + app.toast(format!("Exported {written} textures to {}", app.ui.export.dir), false); + true +} + +/// In the browser the textures download (one zip for several files). +#[cfg(target_arch = "wasm32")] +fn export_sets(app: &mut TextureCraftApp) -> bool { + let sets = export_selection(app); + crate::web_files::export_textures(app, &export_params(app), &sets); + true +} + +fn export_params(app: &TextureCraftApp) -> serde_json::Value { + let e = &app.ui.export; + let mut p = json!({"preset": e.preset, "padding": e.padding, "dilation": e.dilation}); + if !e.format.is_empty() { + p["format"] = json!(e.format); + } + if e.bit_depth > 0 { + p["bitDepth"] = json!(e.bit_depth); + } + if e.size > 0 { + p["size"] = json!(e.size); + } + if !e.normal_format.is_empty() { + p["normalFormat"] = json!(e.normal_format); + } + p +} + +fn export_settings_tab(app: &mut TextureCraftApp, ui: &mut Ui, presets: &[texturecraft_engine::formats::ExportPreset]) { + ui.label(caption("General export parameters")); + egui::Grid::new("exp_grid").num_columns(2).spacing(vec2(12.0, 8.0)).show(ui, |ui| { + // The browser downloads the files instead. + #[cfg(not(target_arch = "wasm32"))] + { + ui.label("Output directory"); + ui.horizontal(|ui| { + ui.add(egui::TextEdit::singleline(&mut app.ui.export.dir).desired_width(360.0)); + if ui.button("...").clicked() + && let Some(d) = rfd::FileDialog::new().set_directory(&app.ui.export.dir).pick_folder() + { + app.ui.export.dir = d.display().to_string(); + } + }); + ui.end_row(); + } + ui.label("Output template"); + egui::ComboBox::from_id_salt("exp_preset").width(360.0).selected_text(app.ui.export.preset.clone()).show_ui(ui, |ui| { + for p in presets { + ui.selectable_value(&mut app.ui.export.preset, p.name.clone(), &p.name); + } + }); + ui.end_row(); + ui.label("File type"); + ui.horizontal(|ui| { + let label = if app.ui.export.format.is_empty() { "From template".to_string() } else { app.ui.export.format.clone() }; + egui::ComboBox::from_id_salt("exp_fmt").selected_text(label).show_ui(ui, |ui| { + ui.selectable_value(&mut app.ui.export.format, String::new(), "From template"); + for f in ["png", "jpeg", "tga", "tiff", "bmp", "webp", "exr", "hdr"] { + ui.selectable_value(&mut app.ui.export.format, f.to_string(), f); + } + }); + let bd = if app.ui.export.bit_depth == 0 { "Default".to_string() } else { format!("{} bits", app.ui.export.bit_depth) }; + egui::ComboBox::from_id_salt("exp_bits").selected_text(bd).show_ui(ui, |ui| { + for (v, l) in [(0u8, "Default"), (8, "8 bits"), (16, "16 bits"), (32, "32 bits (float)")] { + ui.selectable_value(&mut app.ui.export.bit_depth, v, l); + } + }); + }); + ui.end_row(); + ui.label("Size"); + let sz = if app.ui.export.size == 0 { "Based on each texture set's size".to_string() } else { format!("{0} x {0}", app.ui.export.size) }; + egui::ComboBox::from_id_salt("exp_size").width(360.0).selected_text(sz).show_ui(ui, |ui| { + ui.selectable_value(&mut app.ui.export.size, 0, "Based on each texture set's size"); + for r in [256u32, 512, 1024, 2048, 4096, 8192] { + ui.selectable_value(&mut app.ui.export.size, r, format!("{r} x {r}")); + } + }); + ui.end_row(); + ui.label("Padding"); + ui.horizontal(|ui| { + let label = match app.ui.export.padding.as_str() { + "none" => "No padding (passthrough)", + "dilation" => "Dilation + transparent", + "dilationDefault" => "Dilation + default background color", + _ => "Dilation infinite", + }; + egui::ComboBox::from_id_salt("exp_pad").width(260.0).selected_text(label).show_ui(ui, |ui| { + for (v, l) in [("infinite", "Dilation infinite"), ("dilation", "Dilation + transparent"), ("dilationDefault", "Dilation + default background color"), ("none", "No padding (passthrough)")] { + ui.selectable_value(&mut app.ui.export.padding, v.to_string(), l); + } + }); + if app.ui.export.padding.starts_with("dilation") { + ui.add(egui::DragValue::new(&mut app.ui.export.dilation).range(0..=256).suffix(" px")); + } + }); + ui.end_row(); + ui.label("Normal map format"); + let nf = match app.ui.export.normal_format.as_str() { + "openGl" => "OpenGL", + "directX" => "DirectX", + _ => "From template / project", + }; + egui::ComboBox::from_id_salt("exp_nf").selected_text(nf).show_ui(ui, |ui| { + for (v, l) in [("", "From template / project"), ("openGl", "OpenGL"), ("directX", "DirectX")] { + ui.selectable_value(&mut app.ui.export.normal_format, v.to_string(), l); + } + }); + ui.end_row(); + }); +} + +pub fn export(app: &mut TextureCraftApp, ctx: &egui::Context) { + if !app.ui.windows.export { + return; + } + let mut open = true; + let mut do_export = false; + let presets = texturecraft_engine::formats::builtin_presets(); + egui::Window::new("Export textures").open(&mut open).collapsible(false).resizable(false).fixed_size(vec2(820.0, 430.0)).anchor(egui::Align2::CENTER_CENTER, vec2(0.0, 0.0)).show(ctx, |ui| { + ui.horizontal(|ui| { + for (i, t) in ["Settings", "Output templates", "List of exports"].iter().enumerate() { + if ui.selectable_label(app.ui.export.tab == i as u8, caption(t)).clicked() { + app.ui.export.tab = i as u8; + } + } + }); + ui.separator(); + ui.allocate_ui_with_layout(vec2(ui.available_width(), 360.0), egui::Layout::left_to_right(egui::Align::Min), |ui| { + ui.set_height(360.0); + ui.vertical(|ui| { + ui.set_width(180.0); + ui.label(RichText::new("Texture sets").strong()); + for i in 0..app.session.set_count() { + let name = app.session.doc.project.texture_sets.get(i).map(|t| t.name.clone()).unwrap_or_default(); + let mut on = !app.ui.export.skip.contains(&i); + if ui.checkbox(&mut on, name).changed() { + if on { + app.ui.export.skip.retain(|x| *x != i); + } else { + app.ui.export.skip.push(i); + } + } + } + }); + ui.separator(); + ui.vertical(|ui| match app.ui.export.tab { + 0 => export_settings_tab(app, ui, &presets), + 1 => { + if let Some(p) = presets.iter().find(|p| p.name == app.ui.export.preset) { + ui.label(RichText::new(&p.name).strong()); + egui::ScrollArea::vertical().max_height(340.0).show(ui, |ui| { + for m in &p.maps { + ui.horizontal(|ui| { + ui.label(RichText::new(&m.file).monospace()); + ui.label(RichText::new(format!("{} {}-bit", m.format.extension(), m.bit_depth)).color(TOKENS.text_dim)); + }); + for (dest, src) in &m.channels { + ui.label(RichText::new(format!(" {dest:?} <- {src:?}")).size(11.0).color(TOKENS.text_dim)); + } + } + }); + } + } + _ => { + let mut params = export_params(app); + params["ts"] = json!("all"); + match texturecraft_engine::execute(&mut app.session, "export.plan", ¶ms) { + Ok(v) => { + egui::ScrollArea::vertical().max_height(340.0).show(ui, |ui| { + for f in v.as_array().into_iter().flatten() { + let ts = f.get("ts").and_then(|t| t.as_u64()).unwrap_or(0) as usize; + if !app.ui.export.skip.contains(&ts) { + ui.label(RichText::new(f.get("file").and_then(|x| x.as_str()).unwrap_or_default()).monospace()); + } + } + }); + } + Err(e) => { + ui.label(RichText::new(e.to_string()).color(TOKENS.danger)); + } + } + } + }); + }); + ui.add_space(8.0); + ui.separator(); + ui.horizontal(|ui| { + if ui.button("Reset all").clicked() { + app.ui.export = crate::state::ExportUi::default(); + } + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if ui.add(egui::Button::new(RichText::new("Export").color(Color32::WHITE)).fill(TOKENS.accent)).clicked() { + do_export = true; + } + if ui.button("Cancel").clicked() { + app.ui.windows.export = false; + } + }); + }); + }); + if do_export && export_sets(app) { + app.ui.windows.export = false; + } + if !open { + app.ui.windows.export = false; + } +} diff --git a/docs/architecture.md b/docs/architecture.md new file mode 100644 index 00000000..781b6e5a --- /dev/null +++ b/docs/architecture.md @@ -0,0 +1,92 @@ +# Architecture + +TextureCraft is a 3D texture painter: import a mesh, paint PBR material channels on its surface +in layers with masks, bake mesh maps that drive procedural masks, and export game-ready textures. +This document explains how the pieces fit together. + +## Crates and layers + +```text +L0 core document model: Project, TextureSet, Layer (paint / fill / group), Mask, Effect, + Channel, 32 BlendModes with the CPU reference math (blend1/blend3) +L0 texel TileImage (sparse copy-on-write 64x64 tiles of u16), DilationMap (UV padding), + deterministic noise, sRGB transfer +L0 geom Mesh (+ MikkTSpace tangents), primitives, SAH BVH, UvGBuffer, OrbitCamera, auto-unwrap +L1 compose the layer-stack compositor (CPU, per tile, rayon), patterns, display conversion +L1 bake AO, curvature, thickness, world normal, position, ID, normal and height bakers +L2 paint strokes -> stamps, camera visibility, texture-space rasterization, commit +L2 formats mesh import (glTF/GLB, OBJ, PLY, STL), texture export presets, .tcraft projects, + image resources +L3 engine Session (document + caches + history + tools), the command registry +L3 render wgpu viewport renderer (PBR, IBL, post effects, UV view), headless renders +L4 automation MCP server, JSON-lines RPC, bridge to the running app +L5 ui-egui the egui shell (swappable) + apps texturecraft (desktop), texturecraft-cli, texturecraft-web +``` + +`cargo xtask layers` enforces that a crate depends only on lower layers and that nothing below +`ui-egui` touches egui, eframe, winit or rfd. + +## The UV-space G-buffer: one source of truth + +For every texture set (one per mesh material) `geom::UvGBuffer` rasterizes the mesh's UV layout +once: each texel knows which triangle covers its center and where on it (barycentrics). From that +every texel has a world position, a shading normal and a tangent frame. Painting, compositing, +procedural masks, baking and UV padding all read the same buffer, so they agree exactly on which +texel is which surface point. Texels whose centers fall outside UV islands are filled by padding +(`texel::DilationMap`, a multi-source BFS computed once per layout). + +## Painting + +1. A `paint::Stroke` turns pointer samples into world-space stamps: picking through the BVH, + lazy mouse, spacing measured along the surface (re-projected and re-picked so stamps stay on + the mesh, resampled in screen space across silhouettes), jitter and mirror symmetry. +2. `paint::rasterize` gathers the triangles within each stamp's volume (BVH box query) and + accumulates the brush alpha into a texture-space stroke mask for every covered texel inside the + stamp's oriented box. Because stamps live in 3D, strokes cross UV seams without gaps. A CPU + eye-distance buffer (`EyeDepth`, rendered once per stroke) keeps paint off surfaces the camera + can't see; angle fade and backface culling soften grazing surfaces. +3. While dragging, the compositor reads the stroke mask as a live preview. On release, + `paint::commit` blends it into the layer's tiles with premultiplied "over" (or scales them down + when erasing); only touched tiles are un-shared. Strokes in the 2D view use UV-space stamps. + +## Compositing + +`compose::composite_tiles` evaluates a texture set's stack per 64x64 tile in parallel. For each +covered texel it walks the stack bottom to top: paint layers are unpremultiplied from their +(value, coverage) tiles, fill layers evaluate uniform values, procedurals (through UV, tri-planar, +planar, spherical or cylindrical projections) or image resources; masks evaluate their effect +stack (painted bitmaps, fills, mesh-map generators, levels, filters, ID color selection, +compare); each enabled channel blends with its mode and opacity. Only dirty tiles are recomposited +(a stroke dirties the tiles it touches), then padded, converted to display formats and uploaded to +the GPU. + +The CPU compositor is the reference for everything users see and export. Export +(`formats::export`) reads the same composite, adds height-to-normal (Sobel) combined with the +normal channel and baked normals by reoriented normal mapping, and packs channels per preset. + +## Undo + +`Session::edit` snapshots the `Document` (project + pixels + baked maps + resources) before a +change and restores it if the change fails. Pixel data is copy-on-write (`Arc` tiles), so a +snapshot costs one reference-count bump per allocated tile, and an undo step only keeps the tiles +that actually changed. + +## Rendering + +`render::Renderer` draws the mesh with metal/roughness PBR: GGX specular with height-correlated +Smith visibility, Lambert diffuse, image-based lighting from procedural studio environments +(prefiltered on the CPU into a mip chain for specular, nine spherical-harmonic coefficients for +diffuse), the analytic split-sum environment BRDF, horizon fading, baked AO, height-to-normal and +the painted normal channel. Debug views show any channel, any baked mesh map or a UV checker. +The 2D view draws the UV layout with the material and a wireframe. Everything is fragment passes, +so it runs on Vulkan, Metal, DX12, WebGPU and WebGL2. `render::headless` renders without a window +(CLI, MCP, tests). + +## Commands and automation + +Every action is a `CommandSpec` in `engine::commands` (id, label, menu path, shortcut, params doc, +enabled predicate, run). The menus, shortcuts, the desktop app's control channel +(`--control `), the CLI and the MCP server all execute commands by id, so an agent can do +anything a user can and verify it through `project.inspect`. See +[`control-protocol.md`](control-protocol.md). diff --git a/docs/brand/LICENSE-brand.txt b/docs/brand/LICENSE-brand.txt new file mode 100644 index 00000000..adf1ad77 --- /dev/null +++ b/docs/brand/LICENSE-brand.txt @@ -0,0 +1,42 @@ +ArtCraft brand assets: license + +The ArtCraft name, wordmark and mark (the "ArtCraft Marks"), including every file in this +directory in every color, size and format, are owned by the ArtCraft Team. They are trademarks +and are NOT open source. They are not covered by TextureCraft's MIT OR Apache-2.0 license, and nothing in +that license, in NOTICE or in any other file of this repository grants any right to them. + +Permission + +The ArtCraft Team grants a limited, non-exclusive, non-transferable, revocable, royalty-free +permission to use the ArtCraft Marks only: + + 1. as part of copies of this repository; and + 2. within TextureCraft itself, its documentation and its official builds, to identify TextureCraft as made + by the ArtCraft team and community and to link to getartcraft.com and its community. + +Conditions + +The permission holds only while every one of these is true: + + - The marks are used exactly as supplied in this directory. Do not redraw, recolor, crop, + stretch, rotate, animate, outline or add effects to them, extract parts of them, or combine + them with any other name, mark or artwork. + - They are used only in the context of TextureCraft, and only to say that the ArtCraft team makes it. + They must never suggest that the ArtCraft Team made, sponsors, endorses, certifies or supports + any other product, service, fork, modified version, plug-in, organization or person. + - Neither the marks nor the ArtCraft name appear in the name, logo, icon, domain, social media + handle, app-store listing, advertising or marketing of anything other than TextureCraft. + +Forks and modified versions + +If you distribute or publish a modified version of TextureCraft, or any work derived from it, you must +remove the ArtCraft Marks from it (or replace them with your own) and must not call it ArtCraft or +present it as an ArtCraft product. You may say, in plain text, that your work is based on TextureCraft by +the ArtCraft team. A fork kept only to propose changes back to this repository may keep the marks +unchanged. + +Everything else + +Any use not expressly permitted here needs prior written permission from the ArtCraft Team +(https://getartcraft.com/). The permission ends automatically for anyone who breaks these terms. +All other rights are reserved. diff --git a/docs/brand/artcraft-logo-white.png b/docs/brand/artcraft-logo-white.png new file mode 100644 index 00000000..8cfb31d9 Binary files /dev/null and b/docs/brand/artcraft-logo-white.png differ diff --git a/docs/brand/artcraft-logo-white.svg b/docs/brand/artcraft-logo-white.svg new file mode 100644 index 00000000..4af80f75 --- /dev/null +++ b/docs/brand/artcraft-logo-white.svg @@ -0,0 +1,7 @@ + + ArtCraft + + + + + diff --git a/docs/brand/artcraft-logo.png b/docs/brand/artcraft-logo.png new file mode 100644 index 00000000..a4d1544c Binary files /dev/null and b/docs/brand/artcraft-logo.png differ diff --git a/docs/brand/artcraft-logo.svg b/docs/brand/artcraft-logo.svg new file mode 100644 index 00000000..d63f3eae --- /dev/null +++ b/docs/brand/artcraft-logo.svg @@ -0,0 +1,7 @@ + + ArtCraft + + + + + diff --git a/docs/brand/artcraft-mark-black.png b/docs/brand/artcraft-mark-black.png new file mode 100644 index 00000000..27664bc1 Binary files /dev/null and b/docs/brand/artcraft-mark-black.png differ diff --git a/docs/brand/artcraft-mark-black.svg b/docs/brand/artcraft-mark-black.svg new file mode 100644 index 00000000..63943537 --- /dev/null +++ b/docs/brand/artcraft-mark-black.svg @@ -0,0 +1,4 @@ + + ArtCraft + + diff --git a/docs/brand/artcraft-mark.png b/docs/brand/artcraft-mark.png new file mode 100644 index 00000000..dbb3dc08 Binary files /dev/null and b/docs/brand/artcraft-mark.png differ diff --git a/docs/brand/artcraft-mark.svg b/docs/brand/artcraft-mark.svg new file mode 100644 index 00000000..72e191f8 --- /dev/null +++ b/docs/brand/artcraft-mark.svg @@ -0,0 +1,4 @@ + + ArtCraft + + diff --git a/docs/control-protocol.md b/docs/control-protocol.md new file mode 100644 index 00000000..1dbba734 --- /dev/null +++ b/docs/control-protocol.md @@ -0,0 +1,225 @@ +# Control protocol, MCP and CLI + +Everything TextureCraft does is an engine command with a stable id (`layer.add.fill`, +`bake.run`, `edit.undo`, …). Agents and scripts reach the same commands four ways: + +| Surface | Start it with | Use it for | +|---|---|---| +| Control channel | `texturecraft --control ` | Drive the running desktop app: commands, inspect, menus, screenshots | +| MCP server | `texturecraft-cli mcp` | AI agents (Claude Code, Claude Desktop, any MCP client), headless or bridged to the app | +| JSON-lines server | `texturecraft-cli serve [--port ]` | Scripts that keep one headless session open | +| CLI subcommands | `texturecraft-cli info/run/batch/bake/export/render/new` | One-shot jobs in shells and pipelines | + +Programmatic calls never open dialogs: a command runs with its defaults. Only `ui.menu.invoke` +behaves like a menu click. `session.inspect` shows every layer, mask, effect, fill source and +painted tile count, so an agent can verify its work without a screenshot. + +## Control channel (desktop app) + +```sh +texturecraft --control 7878 +# stderr: texturecraft: control token written to /texturecraft-control-7878.token +``` + +Loopback TCP (`127.0.0.1` only), one JSON object per line each way. Requests are +`{"id", "method", "params"}`; replies are `{"id", "ok": true, "result"}` or +`{"id", "ok": false, "error"}`. Requests are limited to 8 MiB. + +**Authentication.** The first request on every connection must be + +```json +{"id": 0, "method": "auth", "params": {"token": ""}} +``` + +The token is `TEXTURECRAFT_CONTROL_TOKEN` when that is set (16 characters or more), otherwise a +fresh random token the app writes to `/texturecraft-control-.token` (`` is +`%TEMP%` on Windows, `$TMPDIR` or `/tmp` elsewhere). Any other first request is refused. + +| Method | Params | Result | +|---|---|---| +| `ping` | | `"pong"` | +| `command.run` | `{id, params?}` | the command's JSON result | +| `command.list` | `{filter?}` | `[{id, label, menu, shortcut, params, enabled}]` | +| `session.inspect` | | project, mesh, texture sets, layer stack, tool, camera, display, history | +| `ui.inspect` | | UI state, viewport rect, fps, bake progress, toasts | +| `ui.menu.list` | | the menu catalog: `{menu, submenu, label, id, shortcut, implemented, enabled}` | +| `ui.menu.invoke` | `{id}` | behaves like a menu click (may open a window) | +| `ui.set` | fields of the UI state, e.g. `{"layout": "Only3D"}`; `dock` is the panel layout (`{main, windows}`: `{"tabs": {"tabs": [...], "active": 0}}` or `{"split": {"vertical", "fraction", "first", "second"}}` nodes over `Viewport`, `Assets`, `TextureSetList`, `Layers`, `TextureSetSettings`, `Properties`, `Display`, `Shader`, `History`, `Log`), `keymap` the custom shortcuts (`{"file.save": "F5"}`, `""` unbinds) | `null` | +| `viewport.screenshot` | `{view?: "3d" \| "2d"}` | `{width, height, png}` (PNG, base64) | +| `ui.screenshot` | | `{width, height, png}` of the whole window, after the next frame | + +Example session: + +```text +> {"id":0,"method":"auth","params":{"token":"3f9c…"}} +< {"id":0,"ok":true} +> {"id":1,"method":"command.run","params":{"id":"layer.add.fill","params":{"color":"#336699","roughness":0.2}}} +< {"id":1,"ok":true,"result":{"layer":7}} +> {"id":2,"method":"command.run","params":{"id":"mask.add.black"}} +< {"id":2,"ok":true,"result":null} +> {"id":3,"method":"viewport.screenshot","params":{"view":"3d"}} +< {"id":3,"ok":true,"result":{"width":1210,"height":788,"png":"iVBORw0KGgo…"}} +``` + +A minimal client (Python, for illustration): + +```python +import json, os, socket, tempfile +port = 7878 +token = os.environ.get("TEXTURECRAFT_CONTROL_TOKEN") or open( + os.path.join(tempfile.gettempdir(), f"texturecraft-control-{port}.token")).read().strip() +s = socket.create_connection(("127.0.0.1", port)); f = s.makefile("rw") +def call(method, params=None, _id=[0]): + _id[0] += 1 + f.write(json.dumps({"id": _id[0], "method": method, "params": params or {}}) + "\n"); f.flush() + reply = json.loads(f.readline()) + if not reply["ok"]: raise RuntimeError(reply["error"]) + return reply.get("result") +call("auth", {"token": token}) +print(call("command.run", {"id": "layer.add.fill", "params": {"color": [0.8, 0.1, 0.1]}})) +``` + +## MCP server + +`texturecraft-cli mcp` speaks MCP on stdin/stdout (stdout carries only protocol messages). + +- **Headless** (default): an in-process session that starts on the material ball (1024). + Files are reachable only below the roots given at launch, as forward-slash paths relative to + them: `--read-root ` for `project_open` / `mesh_import`, `--write-root ` for + `project_save` / `material_save` / `textures_export`. Without roots there is no file access. `..`, absolute + paths, drive letters, device names and symlink or junction escapes are refused. +- **Bridge**: `--bridge ` forwards to a running `texturecraft --control ` (token from + `--token`, `TEXTURECRAFT_CONTROL_TOKEN` or the app's token file). The app reads and writes its + own files, so the path tools are unavailable there; `project_save` without a path invokes + File › Save. + +Through `command_run`, every `file.*` command but `file.close` (and `file.reimportMesh` without +a `path`) is refused, and so is any command given a `path` or `dir` (`resource.import`, +`material.save`, `material.load`, …), in both modes: use the workspace tools below. + +| Tool | Params | Notes | +|---|---|---| +| `session_inspect` | | The whole session as JSON | +| `command_list` | `{filter?}` | Command ids, labels, params docs, enabled now | +| `command_run` | `{id, params?}` | Any command by id | +| `command_batch` | `{steps: [{id, params?}]}` | Stops at the first error: `{completed, results, failed: {index, id, error}}` (a tool error) | +| `project_new` | `{mesh?, resolution?, name?}` | Built-in mesh: material_ball, sphere, cube, rounded_cube, cylinder, torus, plane, crate | +| `project_open` | `{path}` | `.tcraft` or a mesh file, relative to the read root | +| `project_save` | `{path?}` | `.tcraft` (appended if missing), relative to the write root | +| `mesh_import` | `{path, resolution?}` | New project from .glb/.gltf/.fbx/.dae/.obj/.ply/.stl | +| `resource_import` | `{path}` | Image resource (png, jpg, tga, tif, bmp, webp, exr, hdr) → its name, for `layer.add.image` / `tool.stencil` | +| `material_save` | `{path, layer?, name?}` | A layer or folder as a `.tcmat` smart material below the write root | +| `material_load` | `{path}` | Add a `.tcmat` smart material above the selection | +| `textures_export` | `{dir, preset?, ts?, format?, padding?, size?}` | Writes files below the write root | +| `bake` | `{ts?, resolution?, maps?}` | Mesh maps for generators and smart materials | +| `paint_stroke` | `{points, viewport?, color?, roughness?, metallic?, height?, size?, hardness?}` | Paint tool + brush material + `tool.stroke`; points in viewport pixels | +| `viewport_render` | `{view?: "3d" \| "2d", width?, height?, yaw?, pitch?, samples?, bounces?}` | Headless: PNG image (≤ 2048 px a side); yaw/pitch in degrees for this render only; `samples` (1–4096) path traces the 3D view on the CPU instead (no GPU needed) | +| `viewport_screenshot` | `{view?}` | Bridge: the app's 3D or 2D view | +| `ui_screenshot` | | Bridge: the whole window (downscaled to 2048 px) | +| `ui_inspect` | | Bridge: UI state | +| `ui_menu_invoke` | `{id}` | Bridge: a menu click | + +Replies are capped: 8 MiB of JSON, 5 MiB per PNG, 256 batch steps. Stroke points and render +sizes share one space: the default render size is the session viewport (1280×800), so +`paint_stroke` points can be read straight off a `viewport_render` image. + +A typical agent run: `project_new {"mesh": "sphere"}` → `command_run layer.add.fill +{"color": "#336699", "roughness": 0.2}` → `bake {"resolution": 512}` → `command_run +mask.add.black` → `command_run effect.add.generator {"generator": "edgeWear"}` → +`session_inspect` → `viewport_render` → `project_save {"path": "ball.tcraft"}` → +`textures_export {"dir": "textures", "preset": "glTF PBR Metal Roughness"}`. + +### Claude Code + +```sh +claude mcp add texturecraft -- texturecraft-cli mcp --read-root . --write-root . +# bridged to the running app: +claude mcp add texturecraft-app -- texturecraft-cli mcp --bridge 7878 +``` + +or a project `.mcp.json`: + +```json +{ + "mcpServers": { + "texturecraft": { "command": "texturecraft-cli", "args": ["mcp", "--read-root", ".", "--write-root", "."] } + } +} +``` + +### Claude Desktop + +In `claude_desktop_config.json` (Settings › Developer › Edit Config), with absolute paths: + +```json +{ + "mcpServers": { + "texturecraft": { + "command": "C:\\Program Files\\TextureCraft\\texturecraft-cli.exe", + "args": ["mcp", "--read-root", "C:\\Users\\me\\Texturing", "--write-root", "C:\\Users\\me\\Texturing"] + } + } +} +``` + +## Headless JSON-lines server + +`texturecraft-cli serve` keeps one headless session open and answers the control channel's +envelope on stdin/stdout (no authentication: the caller started the process). With +`--port ` it listens on `127.0.0.1:` instead, with the same `auth` handshake and +token rules as the app (a generated token goes to `/texturecraft-control-.token`). +File access follows `--read-root` / `--write-root` as for MCP. + +| Method | Params | +|---|---| +| `ping`, `methods`, `session.inspect` | | +| `command.run` | `{id, params?}` | +| `command.list` | `{filter?}` | +| `command.batch` | `{steps: [{id, params?}]}` | +| `project.open` | `{path}` | +| `project.save` | `{path?}` | +| `mesh.import` | `{path, resolution?, name?}` | +| `resource.import` | `{path}` | +| `material.save` | `{path, layer?, name?}` | +| `material.load` | `{path}` | +| `textures.export` | `{dir, preset?, ts?, format?, padding?, size?, bitDepth?, dilation?, normalFormat?}` | +| `render` | `{view?, width?, height?, yaw?, pitch?}` → `{width, height, png}` | + +```sh +printf '%s\n' '{"id":1,"method":"command.run","params":{"id":"project.new","params":{"mesh":"torus"}}}' \ + '{"id":2,"method":"render","params":{"width":512,"height":512}}' | texturecraft-cli serve +``` + +## CLI + +```sh +texturecraft-cli --version # TextureCraft 0.1.0 (abc1234 2026-10-14) +texturecraft-cli info model.glb # triangles, materials, UV report +texturecraft-cli info project.tcraft # texture sets, layers, baked maps +texturecraft-cli commands --filter mask +texturecraft-cli new builtin:material_ball ball.tcraft --resolution 1024 --sample +texturecraft-cli new model.glb model.tcraft --resolution 2048 +texturecraft-cli run ball.tcraft layer.add.fill '{"color":"#c08040","metallic":1}' -o ball.tcraft +texturecraft-cli batch script.json +texturecraft-cli bake model.tcraft --resolution 2048 --maps ambientOcclusion,curvature,thickness +texturecraft-cli export model.tcraft out/ --preset "Unreal Engine (Packed)" --format png --size 2048 +texturecraft-cli render ball.tcraft ball.png --size 1280x800 --view 3d --yaw 30 --pitch 15 +``` + +`` is a `.tcraft` project, a mesh file or `builtin:`. A batch script is a list of +steps or an object with an optional input and output (paths relative to the current directory): + +```json +{ + "open": "builtin:sphere", + "steps": [ + {"id": "layer.add.fill", "params": {"color": [0.6, 0.6, 0.62], "metallic": 1, "roughness": 0.3}}, + {"id": "bake.run", "params": {"resolution": 512}}, + {"id": "material.apply", "params": {"material": "iron_rusted"}} + ], + "save": "rusty.tcraft" +} +``` + +Errors print `texturecraft-cli : error: …` and exit 1; usage errors exit 2. The CLI is a +console program and never opens a window; `render` needs a GPU or software adapter. diff --git a/docs/images/texturecraft-workspace.png b/docs/images/texturecraft-workspace.png new file mode 100644 index 00000000..009e6a8c Binary files /dev/null and b/docs/images/texturecraft-workspace.png differ diff --git a/docs/parity.md b/docs/parity.md new file mode 100644 index 00000000..86866434 --- /dev/null +++ b/docs/parity.md @@ -0,0 +1,89 @@ +# Menu parity + +Generated by `TC_UPDATE_PARITY=1 cargo test -p texturecraft-ui-egui parity`. Each row is a menu item of the target menu layout with TextureCraft's command id; **live** means the id is implemented. Feature parity beyond menus is tracked in [`ROADMAP.md`](../ROADMAP.md). + +**53 of 53 menu items live (100%).** + +## File + +| Item | Id | Shortcut | Status | +|---|---|---|---| +| New project... | `ui.newProject` | Ctrl+N | live | +| Open... | `file.open` | Ctrl+O | live | +| Open sample project... | `project.sample` | | live | +| Close | `file.close` | Ctrl+F4 | live | +| Save | `file.save` | Ctrl+S | live | +| Save as... | `file.saveAs` | Ctrl+Shift+S | live | +| Save a copy... | `file.saveCopy` | | live | +| Remove unused resources... | `file.removeUnused` | | live | +| Import resources... | `resource.import` | | live | +| Export Textures... | `ui.export` | Ctrl+Shift+E | live | +| Export Mesh... | `file.exportMesh` | | live | +| Quit | `ui.quit` | Ctrl+Q | live | + +## Edit + +| Item | Id | Shortcut | Status | +|---|---|---|---| +| Undo | `edit.undo` | Ctrl+Z | live | +| Redo | `edit.redo` | Ctrl+Y | live | +| Project Configuration... | `ui.projectConfig` | | live | +| Reimport mesh | `file.reimportMesh` | Ctrl+Shift+R | live | +| Settings... | `ui.settings` | | live | + +## Mode + +| Item | Id | Shortcut | Status | +|---|---|---|---| +| Bake Mesh Maps | `ui.mode.bake` | F8 | live | +| Painting | `ui.mode.paint` | F9 | live | +| Rendering (path traced) | `ui.mode.render` | F10 | live | + +## Window + +| Item | Id | Shortcut | Status | +|---|---|---|---| +| Views ▸ Assets | `ui.view.assets` | | live | +| Views ▸ Display Settings | `ui.view.display` | | live | +| Views ▸ History | `ui.view.history` | | live | +| Views ▸ Layers | `ui.view.layers` | | live | +| Views ▸ Log | `ui.view.log` | | live | +| Views ▸ Properties | `ui.view.properties` | | live | +| Views ▸ Shader Settings | `ui.view.shader` | | live | +| Views ▸ Texture Set List | `ui.view.textureSetList` | | live | +| Views ▸ Texture Set Settings | `ui.view.textureSetSettings` | | live | +| Save UI Layout ▸ Save current layout... | `ui.layout.save` | | live | +| Load UI Layout | `ui.layout.load` | | live | +| Hide UI | `ui.hideUi` | Tab | live | +| Reset UI | `ui.resetUi` | | live | + +## Viewport + +| Item | Id | Shortcut | Status | +|---|---|---|---| +| Display material | `viewport.displayMaterial` | M | live | +| Display next channel | `viewport.nextChannel` | C | live | +| Display previous channel | `viewport.previousChannel` | Shift+C | live | +| Display next mesh map | `viewport.nextMeshMap` | B | live | +| Display previous mesh map | `viewport.previousMeshMap` | Shift+B | live | +| UV checker | `ui.uvChecker` | | live | +| Show entire mesh | `viewport.frame` | F | live | +| 3D/2D views | `ui.layout.split` | F1 | live | +| 3D view only | `ui.layout.3d` | F2 | live | +| 2D view only | `ui.layout.2d` | F3 | live | +| Enable quick mask | `viewport.quickMask` | Y | live | +| Edit quick mask | `viewport.quickMask.edit` | U | live | +| Invert quick mask | `viewport.quickMask.invert` | Ctrl+Shift+I | live | + +## Help + +| Item | Id | Shortcut | Status | +|---|---|---|---| +| Documentation... | `ui.help.docs` | | live | +| Shortcuts list... | `ui.help.shortcuts` | | live | +| Join the community (Discord)... | `ui.help.discord` | | live | +| ArtCraft website... | `ui.help.website` | | live | +| Report a bug... | `ui.help.bug` | | live | +| Show log... | `ui.view.log` | | live | +| About TextureCraft... | `ui.about` | | live | + diff --git a/docs/release-playbook.md b/docs/release-playbook.md new file mode 100644 index 00000000..49f109e7 --- /dev/null +++ b/docs/release-playbook.md @@ -0,0 +1,8 @@ +# Release playbook + +The release playbook shared by every crafting app lives in craftrules: +[`../craftrules/release/playbook.md`](../../craftrules/release/playbook.md) +(`storytold/craftrules`). It is the canonical copy; don't duplicate it here. + +PhotoCraft is its reference implementation. TextureCraft's copy (`.github/workflows/release.yml`, +`packaging/`, `cargo xtask version`) and its app-specific details are in [releasing.md](releasing.md). diff --git a/docs/releasing.md b/docs/releasing.md new file mode 100644 index 00000000..ccca327a --- /dev/null +++ b/docs/releasing.md @@ -0,0 +1,104 @@ +# Releasing TextureCraft + +The recipe is craftrules' [release playbook](release-playbook.md); this page covers what is +specific to TextureCraft. The pipeline is [`.github/workflows/release.yml`](../.github/workflows/release.yml). + +## Cutting a release + +1. On `main`, set the version: `cargo xtask version set 1.2.0` (rewrites `[workspace.package] + version` in the root `Cargo.toml`, the single source of truth, and refreshes `Cargo.lock`). + `cargo xtask version` prints it. A version with a hyphen (`1.2.0-rc.1`) becomes a pre-release. +2. Merge `main` into `release` (fast-forward) and push. Only org admins and the `release-managers` + team can push `release`. +3. The run builds every platform and creates or updates the **draft** GitHub Release + `TextureCraft v`, tag `v`, with `SHA256SUMS.txt`. Review it, then publish. + A draft is updated in place (assets replaced); a published release is never overwritten, so + bump the version before building again. + +Test builds: dispatch the workflow on `release` with a `version` such as `1.2.0-rc.1`; the input +overrides `Cargo.toml` for that run (`cargo xtask version set` runs in every build job). A +dispatch on any other branch is a dry run: Linux, Flatpak, FreeBSD and web build for real, the +signing jobs are refused by the `release` environment (0 steps), and no draft is created. + +Before the first release, an org admin runs craftrules `release/signing-setup.md` steps 1-2 for +`storytold/texturecraft` (the `release` environment, its deployment-branch rule, the ruleset and +the twelve secrets) and adds the repo to the org's Default runner group (`release/cut-a-release.md` +§3), or the macOS job waits forever. + +## What gets built + +All file names are `texturecraft---.`, written to `dist/release/`. + +| Platform | Artifacts | Script | +|---|---|---| +| macOS (`macos-15-xlarge`) | `-macos-universal.dmg` (`TextureCraft.app`), `texturecraft-cli--macos-universal.zip` | `packaging/macos/package.sh`, `verify.sh` | +| Windows x64, x86, arm64 | `-windows-.msi`, `-windows--portable.zip` | `packaging/windows/package.ps1` | +| Linux x86_64, aarch64 (Ubuntu 22.04, glibc 2.35) | `.AppImage` + `.AppImage.zsync`, `.deb`, `.rpm`, `.tar.gz` | `packaging/linux/package.sh` | +| Flatpak x86_64, aarch64 | `-linux-.flatpak`, repackaged from the Linux tarball | `packaging/linux/flatpak-bundle.sh` | +| FreeBSD x86_64 (14.3 VM) | `-freebsd-x86_64.tar.gz` | `packaging/freebsd/package.sh` | +| Web | `texturecraft-web-.zip` (trunk, `wasm-release` profile; hosting notes in `packaging/web/README.md`) | `packaging/web/package.sh` | + +Every desktop package ships both binaries, `texturecraft` and `texturecraft-cli`. The packaging +scripts smoke-test `texturecraft-cli --version`, and the Linux job also runs the AppImage with +`--version`, so both binaries must print their version and exit without opening a window. + +## TextureCraft-specific values + +| What | Value | +|---|---| +| Bundle / app id | `ai.storyteller.texturecraft` | +| Project format | `.tcraft`, MIME `application/x-texturecraft`, UTI `ai.storyteller.texturecraft.project` (owned) | +| Also opens | `.glb .gltf .obj .ply .stl` ("Open with" only, never the default app) | +| WiX UpgradeCode | `7E670E60-BB22-4E6B-8749-DD3FD4CA8979`, never change it | +| Flathub manifest | `packaging/linux/flatpak/ai.storyteller.TextureCraft.yml` (release bundle: `…TextureCraft.bundle.yml`) | +| macOS minimum | 11.0 (`LSMinimumSystemVersion`, `MACOSX_DEPLOYMENT_TARGET`) | + +## Version and build info in the binaries + +`apps/texturecraft/build.rs` (also `include!`d by `apps/texturecraft-cli/build.rs`) sets, for +`env!`: + +- `TEXTURECRAFT_BUILD_SHA`: the full commit. CI sets it to `github.sha`; `packaging/env.sh` sets + it from git; otherwise build.rs asks git itself; empty when unknown. +- `TEXTURECRAFT_BUILD_DATE`: `YYYY-MM-DD` (UTC). CI sets it once per run; otherwise today's date, + or `SOURCE_DATE_EPOCH`'s for reproducible builds. + +On Windows targets it also embeds `assets/app-icon/texturecraft.ico` and VERSIONINFO +(ProductName TextureCraft); `package.ps1` sets `TEXTURECRAFT_REQUIRE_WINRES=1` so a release can't +ship an exe without them. The version itself is `CARGO_PKG_VERSION`. + +Other environment variables the scripts read: `TEXTURECRAFT_VERSION` (override the version), +`DIST`, `CARGO_TARGET_DIR`, `TEXTURECRAFT_MAINTAINER` (deb/rpm maintainer), +`TEXTURECRAFT_WASM_MAX_BYTES` (web size gate, default 24 MiB), and at run time +`TEXTURECRAFT_NO_DESKTOP_INTEGRATION=1` (the AppImage's AppRun skips installing its `.desktop` +entry and icons). + +## Signing + +Secrets live only in the `release` environment and are all optional: a missing one gives an +unsigned artifact and a `::warning::`, never a failed build. + +- macOS: `APPLE_CERTIFICATE`, `APPLE_CERTIFICATE_PASSWORD`, `KEYCHAIN_PASSWORD` (import into a + temporary keychain, `packaging/macos/import-cert.sh`), then `APPLE_ID`, `APPLE_PASSWORD`, + `APPLE_TEAM_ID` for `notarytool` and stapling. Without them the app is ad-hoc signed. +- Windows: `packaging/windows/sign.ps1` uses a PFX (`WINDOWS_CERTIFICATE`, + `WINDOWS_CERTIFICATE_PASSWORD`) if set, otherwise Azure Trusted Signing (all six `AZURE_*`). + It signs both exes before WiX and the MSI after. + +## Icons + +`assets/app-icon/icon.py` draws the octopus (original artwork, three colours, craftrules +`standards/icon-design.md`) into `icon.svg`. `packaging/icons.sh` renders everything else with +resvg and `cargo xtask ico|icns`: the hicolor PNGs (16-512 px) and scalable SVG, `icon-1024.png`, +`texturecraft.ico` and `texturecraft.icns`. The outputs are committed; rerun both after changing +the drawing and keep `ATTRIBUTION.md` in step. + +## Local checks + +- `cargo xtask ci`: fmt, clippy, tests, `cargo xtask layers`, `cargo xtask wasm`. +- `bash packaging/linux/apprun-test.sh` and `bash packaging/freebsd/package.sh --dry-run` run + anywhere with bash. +- `packaging/macos/package.sh --arch aarch64` on a Mac builds an ad-hoc signed DMG. +- `packaging/windows/package.ps1 -Arch x64` on Windows (needs WiX 5.0.2) builds an unsigned MSI. +- `.github/workflows/packaging-lint.yml` runs actionlint, shellcheck, the PowerShell parser and + the desktop/AppStream/Flatpak validators on every change to `packaging/` or the workflows. diff --git a/packaging/.shellcheckrc b/packaging/.shellcheckrc new file mode 100644 index 00000000..36c19d06 --- /dev/null +++ b/packaging/.shellcheckrc @@ -0,0 +1,3 @@ +# shellcheck config for the packaging scripts (CI: .github/workflows/packaging-lint.yml). +source-path=SCRIPTDIR +external-sources=true diff --git a/packaging/env.sh b/packaging/env.sh new file mode 100644 index 00000000..211b541f --- /dev/null +++ b/packaging/env.sh @@ -0,0 +1,53 @@ +# shellcheck shell=bash +# Shared setup for the packaging scripts. Source it: `. "$(dirname "$0")/../env.sh"`. +# +# Exports: +# ROOT workspace root +# VERSION [workspace.package] version from Cargo.toml (override: TEXTURECRAFT_VERSION) +# DIST output directory for release artifacts (default: $ROOT/dist/release) +# TEXTURECRAFT_BUILD_SHA git commit baked into the binaries (apps/texturecraft/build.rs) +# TEXTURECRAFT_BUILD_DATE UTC build date, YYYY-MM-DD +# CARGO_TARGET_DIR cargo's target dir (default: $ROOT/target) + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +export ROOT + +# The version lives in exactly one place: `[workspace.package] version` in the root Cargo.toml. +# (`cargo xtask version` prints the same thing; awk avoids compiling xtask here.) +workspace_version() { + awk ' + /^\[/ { in_pkg = ($0 == "[workspace.package]") ; next } + in_pkg && $1 == "version" { gsub(/[" ]/, "", $3); print $3; exit } + ' "$ROOT/Cargo.toml" +} + +VERSION="${TEXTURECRAFT_VERSION:-$(workspace_version)}" +if [ -z "$VERSION" ]; then + echo "error: could not read [workspace.package] version from $ROOT/Cargo.toml" >&2 + exit 1 +fi +export VERSION + +DIST="${DIST:-$ROOT/dist/release}" +mkdir -p "$DIST" +export DIST + +if [ -z "${TEXTURECRAFT_BUILD_SHA:-}" ]; then + TEXTURECRAFT_BUILD_SHA="$(git -C "$ROOT" rev-parse HEAD 2>/dev/null || true)" +fi +export TEXTURECRAFT_BUILD_SHA +export TEXTURECRAFT_BUILD_DATE="${TEXTURECRAFT_BUILD_DATE:-$(date -u +%Y-%m-%d)}" +export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$ROOT/target}" + +# Emit a GitHub Actions warning (plain stderr outside Actions). +warn() { + if [ -n "${GITHUB_ACTIONS:-}" ]; then echo "::warning::$*"; else echo "warning: $*" >&2; fi +} + +# Copy licence, notice and readme files that exist into a package directory. +copy_docs() { + local dest="$1" f + for f in README.md LICENSE-MIT LICENSE-APACHE NOTICE ATTRIBUTION.md; do + if [ -f "$ROOT/$f" ]; then cp "$ROOT/$f" "$dest/"; fi + done +} diff --git a/packaging/freebsd/package.sh b/packaging/freebsd/package.sh new file mode 100644 index 00000000..463ca9ba --- /dev/null +++ b/packaging/freebsd/package.sh @@ -0,0 +1,100 @@ +#!/usr/bin/env bash +# Build and package TextureCraft for FreeBSD: +# +# $DIST/texturecraft--freebsd-x86_64.tar.gz a /usr/local-style tree: +# texturecraft--freebsd-x86_64/{bin, share/applications, share/icons, share/mime, +# share/metainfo, share/doc/texturecraft} +# +# Install by copying the tree's contents into /usr/local: +# tar -xzf texturecraft--freebsd-x86_64.tar.gz --strip-components 1 -C /usr/local +# +# Usage: packaging/freebsd/package.sh [--skip-build] [--dry-run] +# --dry-run stage the tree from stub binaries and list it, without building (works on any OS) +# +# Needs: bash, cargo, and the packages installed in .github/workflows/freebsd.yml. +set -euo pipefail +# shellcheck source=../env.sh +. "$(dirname "${BASH_SOURCE[0]}")/../env.sh" +LINUX="$ROOT/packaging/linux" +APP_ID=ai.storyteller.texturecraft + +SKIP_BUILD=0 +DRY_RUN=0 +while [ $# -gt 0 ]; do + case "$1" in + --skip-build) SKIP_BUILD=1; shift ;; + --dry-run) DRY_RUN=1; SKIP_BUILD=1; shift ;; + -h | --help) sed -n '2,15p' "$0"; exit 0 ;; + *) echo "unknown argument: $1" >&2; exit 2 ;; + esac +done + +# FreeBSD's uname says amd64; release file names use x86_64 like the Linux and Windows ones. +MACHINE="$(uname -m)" +case "$MACHINE" in + amd64 | x86_64) ARCH=x86_64 ;; + arm64 | aarch64) ARCH=aarch64 ;; + *) echo "unsupported architecture $MACHINE" >&2; exit 2 ;; +esac +if [ "$DRY_RUN" = 0 ] && [ "$(uname -s)" != FreeBSD ]; then + echo "error: build on FreeBSD (or pass --dry-run to check the tree layout)" >&2 + exit 2 +fi +BASENAME="texturecraft-$VERSION-freebsd-$ARCH" + +echo "==> TextureCraft $VERSION for FreeBSD $ARCH" + +# The release VM has 12 GB; full parallelism on the biggest crates runs it out of memory. +export CARGO_BUILD_JOBS="${CARGO_BUILD_JOBS:-4}" +if [ "$SKIP_BUILD" = 0 ]; then + (cd "$ROOT" && cargo build --release --locked -p texturecraft -p texturecraft-cli) +fi +WORK="$CARGO_TARGET_DIR/freebsd-package" +STAGE="$WORK/$BASENAME" +rm -rf "$WORK" +mkdir -p "$WORK" + +BIN="$CARGO_TARGET_DIR/release" +OUT_DIR="$DIST" +if [ "$DRY_RUN" = 1 ]; then + # Stub binaries, and the tarball stays in the work dir so dist/ only ever holds real builds. + BIN="$WORK/stub-bin" + OUT_DIR="$WORK" + mkdir -p "$BIN" + for b in texturecraft texturecraft-cli; do + printf '#!/bin/sh\necho "%s %s (dry-run stub)"\n' "$b" "$VERSION" >"$BIN/$b" + chmod 755 "$BIN/$b" + done +fi + +# ---- stage a /usr/local-style tree -------------------------------------------------------------- +# FreeBSD's install(1) has no -D: create the directories first. +mkdir -p "$STAGE/bin" "$STAGE/share/applications" "$STAGE/share/mime/packages" \ + "$STAGE/share/metainfo" "$STAGE/share/icons" "$STAGE/share/doc/texturecraft" +install -m 755 "$BIN/texturecraft" "$BIN/texturecraft-cli" "$STAGE/bin/" +strip "$STAGE/bin/texturecraft" "$STAGE/bin/texturecraft-cli" 2>/dev/null || true +# The desktop entry, MIME type, metainfo and icons are the freedesktop files Linux ships. +install -m 644 "$LINUX/$APP_ID.desktop" "$STAGE/share/applications/$APP_ID.desktop" +install -m 644 "$LINUX/$APP_ID.mime.xml" "$STAGE/share/mime/packages/$APP_ID.xml" +sed -e "s/@VERSION@/$VERSION/g" -e "s/@DATE@/$TEXTURECRAFT_BUILD_DATE/g" \ + "$LINUX/$APP_ID.metainfo.xml.in" >"$STAGE/share/metainfo/$APP_ID.metainfo.xml" +cp -R "$ROOT/assets/app-icon/hicolor" "$STAGE/share/icons/" +copy_docs "$STAGE/share/doc/texturecraft" + +for f in bin/texturecraft bin/texturecraft-cli "share/applications/$APP_ID.desktop" \ + "share/icons/hicolor/256x256/apps/$APP_ID.png" "share/icons/hicolor/scalable/apps/$APP_ID.svg" \ + share/doc/texturecraft/LICENSE-MIT share/doc/texturecraft/LICENSE-APACHE; do + if [ ! -e "$STAGE/$f" ]; then echo "error: $f is missing from the package" >&2; exit 1; fi +done + +# ---- .tar.gz ------------------------------------------------------------------------------------ +mkdir -p "$OUT_DIR" +tar -C "$WORK" -czf "$OUT_DIR/$BASENAME.tar.gz" "$BASENAME" +echo "wrote $OUT_DIR/$BASENAME.tar.gz" + +"$STAGE/bin/texturecraft-cli" --version +if [ "$DRY_RUN" = 1 ]; then + echo "==> dry run: tarball contents" + tar -tzvf "$OUT_DIR/$BASENAME.tar.gz" +fi +echo "==> done" diff --git a/packaging/icons.sh b/packaging/icons.sh new file mode 100644 index 00000000..06996126 --- /dev/null +++ b/packaging/icons.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash +# Regenerate every app icon from assets/app-icon/icon.svg (written by assets/app-icon/icon.py). +# +# Needs: resvg (cargo install resvg / brew install resvg) and cargo (for `cargo xtask ico|icns`). +# The outputs are committed, so packaging never needs these tools. +# +# python3 assets/app-icon/icon.py && packaging/icons.sh +set -euo pipefail +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +DIR="$ROOT/assets/app-icon" +SVG="$DIR/icon.svg" +ID=ai.storyteller.texturecraft +TMP="$(mktemp -d)" +trap 'rm -rf "$TMP"' EXIT + +command -v resvg >/dev/null || { echo "error: resvg not found (cargo install resvg)" >&2; exit 1; } + +# The artwork is a full-bleed 512-unit tile (rx=112). macOS icons pad it to Apple's 824/1024 body +# grid (transparent margin). Windows and Linux icons crop 22 units off each side (into the +# rounded corners) so the portrait reads at 16-48 px. +grep -q 'viewBox="0 0 512 512"' "$SVG" || { echo "error: expected viewBox=\"0 0 512 512\" in $SVG" >&2; exit 1; } +MAC="$TMP/mac.svg" +sed 's/viewBox="0 0 512 512"/viewBox="-62 -62 636 636"/' "$SVG" >"$MAC" +TIGHT="$TMP/tight.svg" +sed 's/viewBox="0 0 512 512"/viewBox="22 22 468 468"/' "$SVG" >"$TIGHT" + +render() { resvg -w "$2" -h "$2" "$1" "$3" /dev/null || return 0 + tmp="$apps/.$DESK_ID.tmp.$$" + esc="$(printf '%s' "$appimage" | sed 's/[\\&|]/\\&/g')" + if sed -e "s|^Exec=.*|Exec=\"$esc\" %F|" -e '/^TryExec=/d' "$src_desktop" 2>/dev/null >"$tmp"; then + if cmp -s "$tmp" "$apps/$DESK_ID.desktop"; then + rm -f "$tmp" + else + if mv -f "$tmp" "$apps/$DESK_ID.desktop" 2>/dev/null; then + command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$apps" >/dev/null 2>&1 + else + rm -f "$tmp" + fi + fi + else + rm -f "$tmp" + return 0 + fi + + # Icons: per-file compare so an unchanged install costs a handful of cmp calls. + src_icons="$APPDIR/usr/share/icons/hicolor" + [ -d "$src_icons" ] || return 0 + icons="$data/icons/hicolor" + (cd "$src_icons" && find . -type f) | while IFS= read -r rel; do + mkdir -p "$icons/$(dirname "$rel")" 2>/dev/null || continue + if ! cmp -s "$src_icons/$rel" "$icons/$rel"; then + cp "$src_icons/$rel" "$icons/$rel" 2>/dev/null || true + fi + done +} + +integrate +exec "$APPDIR/usr/bin/texturecraft" "$@" diff --git a/packaging/linux/ai.storyteller.texturecraft.desktop b/packaging/linux/ai.storyteller.texturecraft.desktop new file mode 100644 index 00000000..5f0f43f6 --- /dev/null +++ b/packaging/linux/ai.storyteller.texturecraft.desktop @@ -0,0 +1,14 @@ +[Desktop Entry] +Type=Application +Name=TextureCraft +GenericName=3D Texture Painter +Comment=Paint PBR textures directly on 3D meshes +Exec=texturecraft %F +TryExec=texturecraft +Icon=ai.storyteller.texturecraft +Terminal=false +StartupNotify=true +StartupWMClass=texturecraft +Categories=Graphics;3DGraphics; +Keywords=texture;paint;pbr;material;3d;mesh;bake;gltf; +MimeType=application/x-texturecraft;model/gltf-binary;model/gltf+json;model/obj;model/x-ply;model/stl; diff --git a/packaging/linux/ai.storyteller.texturecraft.metainfo.xml.in b/packaging/linux/ai.storyteller.texturecraft.metainfo.xml.in new file mode 100644 index 00000000..a1f37874 --- /dev/null +++ b/packaging/linux/ai.storyteller.texturecraft.metainfo.xml.in @@ -0,0 +1,68 @@ + + + + ai.storyteller.texturecraft + CC0-1.0 + MIT OR Apache-2.0 + TextureCraft + Paint PBR textures directly on 3D meshes + + Learning Machines LLC + + +

+ TextureCraft is an open-source, native 3D texture painter that works the way Substance 3D + Painter users expect: familiar menus, shortcuts, panels and tools, with texture sets, + layers, masks, fills and effects painted straight onto the model. +

+
    +
  • Open glTF/GLB, OBJ, PLY and STL meshes and save projects as .tcraft
  • +
  • Paint base colour, roughness, metallic, normal and height together with PBR brushes
  • +
  • Bake mesh maps and export texture sets as PNG, EXR, TGA, JPEG or TIFF
  • +
  • Every action is a command, scriptable from a CLI and a JSON control channel
  • +
+
+ ai.storyteller.texturecraft.desktop + ai.storyteller.texturecraft + + https://getartcraft.com/apps/texturecraft + https://github.com/storytold/texturecraft/issues + https://github.com/storytold/texturecraft + https://discord.gg/artcraft + + Graphics + 3DGraphics + + + texture painting + pbr + 3d + mesh + + + #f0c39c + #8f4416 + + + 760 + + + keyboard + pointing + tablet + + + texturecraft + application/x-texturecraft + model/gltf-binary + model/gltf+json + model/obj + model/x-ply + model/stl + + + + + +
diff --git a/packaging/linux/ai.storyteller.texturecraft.mime.xml b/packaging/linux/ai.storyteller.texturecraft.mime.xml new file mode 100644 index 00000000..9ec32b9e --- /dev/null +++ b/packaging/linux/ai.storyteller.texturecraft.mime.xml @@ -0,0 +1,41 @@ + + + + + TextureCraft project + + + + + + glTF binary model + + + + + + + glTF model + + + + + Wavefront OBJ model + + + + + PLY model + + + + + + + + STL model + + + diff --git a/packaging/linux/apprun-test.sh b/packaging/linux/apprun-test.sh new file mode 100644 index 00000000..0668cc80 --- /dev/null +++ b/packaging/linux/apprun-test.sh @@ -0,0 +1,98 @@ +#!/usr/bin/env bash +# Smoke test for packaging/linux/AppRun: the AppImage's desktop integration writes +# and refreshes the host .desktop entry and icons, is idempotent, honours the opt-out +# and an unwritable HOME, and always launches the app. +# +# Runs in seconds with no build (CI: .github/workflows/packaging-lint.yml). +set -euo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ROOT="$(cd "$HERE/../.." && pwd)" +APP_ID=ai.storyteller.texturecraft + +TMP="$(mktemp -d)" +trap 'rm -rf "$TMP"' EXIT + +# Fake AppDir with the real AppRun, .desktop template, icons, and a stub binary. +APPDIR="$TMP/TextureCraft.AppDir" +mkdir -p "$APPDIR/usr/bin" +install -Dm755 "$HERE/AppRun" "$APPDIR/AppRun" +install -Dm644 "$HERE/$APP_ID.desktop" "$APPDIR/usr/share/applications/$APP_ID.desktop" +mkdir -p "$APPDIR/usr/share/icons" +cp -R "$ROOT/assets/app-icon/hicolor" "$APPDIR/usr/share/icons/" +printf '#!/bin/sh\necho "stub: $*"\n' >"$APPDIR/usr/bin/texturecraft" +chmod +x "$APPDIR/usr/bin/texturecraft" + +APPIMAGE="$TMP/texturecraft test.AppImage" # spaces on purpose: Exec must stay quoted +: >"$APPIMAGE" +export APPIMAGE +export HOME="$TMP/home" +export XDG_DATA_HOME="$HOME/.local/share" +mkdir -p "$HOME" + +fail() { + echo "FAIL: $*" >&2 + exit 1 +} + +# 1. Launch works and passes arguments through. +out="$("$APPDIR/AppRun" open model.glb)" || fail "AppRun exited non-zero" +[ "$out" = "stub: open model.glb" ] || fail "arguments not forwarded: $out" + +DESKTOP="$XDG_DATA_HOME/applications/$APP_ID.desktop" +[ -f "$DESKTOP" ] || fail "desktop entry not installed" + +# 2. Exec rewritten to the AppImage (quoted, spaces intact), TryExec dropped, +# everything else preserved from the packaged entry. +expected_exec="Exec=\"$(readlink -f "$APPIMAGE")\" %F" +grep -Fxq "$expected_exec" "$DESKTOP" || fail "Exec not rewritten: $(grep '^Exec=' "$DESKTOP")" +grep -q '^TryExec=' "$DESKTOP" && fail "TryExec kept, the entry would be hidden" +grep -Fxq "Icon=$APP_ID" "$DESKTOP" || fail "Icon line lost" +grep -Fxq 'Categories=Graphics;3DGraphics;' "$DESKTOP" || fail "metadata lines lost" +if command -v desktop-file-validate >/dev/null; then + desktop-file-validate "$DESKTOP" || fail "generated desktop entry is invalid" +fi + +# 3. Icons landed under the name the .desktop entry references. +for f in "16x16/apps/$APP_ID.png" "256x256/apps/$APP_ID.png" "scalable/apps/$APP_ID.svg"; do + [ -f "$XDG_DATA_HOME/icons/hicolor/$f" ] || fail "icon missing: $f" +done + +# 4. Idempotent: a second launch must not rewrite anything (inode unchanged). +before="$(stat -c %i "$DESKTOP")" +out="$("$APPDIR/AppRun")" || fail "second launch exited non-zero" +[ "$out" = "stub: " ] || fail "second launch output: $out" +after="$(stat -c %i "$DESKTOP")" +[ "$before" = "$after" ] || fail "desktop entry rewritten although unchanged" + +# 5. Moving the AppImage refreshes Exec. +mv "$APPIMAGE" "$TMP/moved.AppImage" +export APPIMAGE="$TMP/moved.AppImage" +"$APPDIR/AppRun" >/dev/null || fail "launch after move exited non-zero" +grep -Fxq "Exec=\"$(readlink -f "$APPIMAGE")\" %F" "$DESKTOP" || fail "Exec not refreshed after move" + +# 6. Opt-out installs nothing. +rm -rf "$XDG_DATA_HOME" +TEXTURECRAFT_NO_DESKTOP_INTEGRATION=1 "$APPDIR/AppRun" >/dev/null || fail "opt-out launch exited non-zero" +[ ! -e "$XDG_DATA_HOME" ] || fail "opt-out still integrated" + +# 7. An unwritable data dir never blocks the launch. +mkdir -p "$TMP/ro" && chmod a-w "$TMP/ro" +out="$(XDG_DATA_HOME="$TMP/ro/data" "$APPDIR/AppRun" out.txt)" || fail "launch blocked by unwritable data dir" +[ "$out" = "stub: out.txt" ] || fail "stub not run against unwritable data dir: $out" +chmod u+w "$TMP/ro" + +# 8. No $APPIMAGE (extracted AppDir): launch only, nothing installed. +rm -rf "$XDG_DATA_HOME" +env -u APPIMAGE "$APPDIR/AppRun" >/dev/null || fail "extracted-AppDir launch exited non-zero" +[ ! -e "$XDG_DATA_HOME" ] || fail "integrated without $APPIMAGE" + +# 9. A path that would need Exec escaping (here a `$`): launch only, nothing installed. +rm -rf "$XDG_DATA_HOME" +odd="$TMP/odd\$(id).AppImage" +: >"$odd" +out="$(APPIMAGE="$odd" "$APPDIR/AppRun" x)" || fail "launch with an odd path exited non-zero" +[ "$out" = "stub: x" ] || fail "odd path: $out" +[ ! -e "$XDG_DATA_HOME" ] || fail "integrated an AppImage path that needs escaping" + +echo "AppRun desktop integration ok" diff --git a/packaging/linux/flatpak-bundle.sh b/packaging/linux/flatpak-bundle.sh new file mode 100644 index 00000000..3f9e4e07 --- /dev/null +++ b/packaging/linux/flatpak-bundle.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash +# Repackage a TextureCraft Linux tarball as a single-file Flatpak bundle: +# +# $DIST/texturecraft--linux-.flatpak +# +# Usage: packaging/linux/flatpak-bundle.sh [--no-test] [TARBALL] +# +# TARBALL defaults to $DIST/texturecraft--linux-.tar.gz from package.sh. The bundle +# is built for the host architecture (x86_64 or aarch64), which must match the tarball's. +# Needs flatpak, flatpak-builder and the SVG pixbuf loader (librsvg2-common) for the host's +# `appstreamcli compose`; the freedesktop runtime and SDK named in the manifest are +# installed per-user from Flathub. Unless --no-test, the bundle is then installed per-user and +# `texturecraft-cli --version` is run inside the sandbox as a smoke test. +# Manifest: packaging/linux/flatpak/ai.storyteller.TextureCraft.bundle.yml. +set -euo pipefail +# shellcheck source=../env.sh +. "$(dirname "${BASH_SOURCE[0]}")/../env.sh" +HERE="$ROOT/packaging/linux" +APP_ID=ai.storyteller.texturecraft +FLATHUB=https://dl.flathub.org/repo/flathub.flatpakrepo + +TEST=1 +TARBALL="" +while [ $# -gt 0 ]; do + case "$1" in + --no-test) TEST=0; shift ;; + -h | --help) sed -n '2,14p' "$0"; exit 0 ;; + -*) echo "unknown argument: $1" >&2; exit 2 ;; + *) TARBALL="$1"; shift ;; + esac +done + +ARCH="$(uname -m)" +case "$ARCH" in + x86_64) ;; + aarch64 | arm64) ARCH=aarch64 ;; + *) echo "unsupported architecture $ARCH" >&2; exit 2 ;; +esac +BASENAME="texturecraft-$VERSION-linux-$ARCH" +TARBALL="${TARBALL:-$DIST/$BASENAME.tar.gz}" +[ -f "$TARBALL" ] || { echo "error: $TARBALL not found (run packaging/linux/package.sh --formats tar first)" >&2; exit 1; } +for tool in flatpak flatpak-builder; do + command -v "$tool" >/dev/null || { echo "error: $tool not found (apt install flatpak flatpak-builder librsvg2-common)" >&2; exit 1; } +done + +echo "==> TextureCraft $VERSION Flatpak bundle for $ARCH from $(basename "$TARBALL")" + +WORK="$CARGO_TARGET_DIR/flatpak-bundle" +rm -rf "$WORK" +mkdir -p "$WORK/stage" +tar -xzf "$TARBALL" -C "$WORK/stage" --strip-components=1 +[ -x "$WORK/stage/bin/texturecraft" ] || { echo "error: $TARBALL has no bin/texturecraft" >&2; exit 1; } +cp "$HERE/flatpak/ai.storyteller.TextureCraft.bundle.yml" "$WORK/$APP_ID.yml" + +flatpak remote-add --user --if-not-exists flathub "$FLATHUB" +# --disable-rofiles-fuse: no FUSE in containers and CI runners. +flatpak-builder --user --install-deps-from=flathub --disable-rofiles-fuse --force-clean \ + --default-branch=stable --repo="$WORK/repo" --state-dir="$WORK/state" "$WORK/build" "$WORK/$APP_ID.yml" + +OUT="$DIST/$BASENAME.flatpak" +# --runtime-repo lets `flatpak install` fetch the freedesktop runtime from Flathub if missing. +flatpak build-bundle --runtime-repo="$FLATHUB" "$WORK/repo" "$OUT" "$APP_ID" stable +echo "wrote $OUT" + +if [ "$TEST" = 1 ]; then + flatpak install --user -y --noninteractive --reinstall "$OUT" + flatpak info --user "$APP_ID" + flatpak run --command=texturecraft-cli "$APP_ID" --version + flatpak run --command=sh "$APP_ID" -c 'ls /app/share/applications /app/share/metainfo /app/share/mime/packages /app/share/icons/hicolor/scalable/apps' +fi +echo "==> done" +ls -lh "$OUT" diff --git a/packaging/linux/flatpak/ai.storyteller.TextureCraft.bundle.yml b/packaging/linux/flatpak/ai.storyteller.TextureCraft.bundle.yml new file mode 100644 index 00000000..259e2396 --- /dev/null +++ b/packaging/linux/flatpak/ai.storyteller.TextureCraft.bundle.yml @@ -0,0 +1,50 @@ +# Flatpak manifest for the single-file bundle attached to every GitHub release +# (texturecraft--linux-.flatpak). Build it with packaging/linux/flatpak-bundle.sh. +# +# It repackages the release's own Linux tarball (texturecraft--linux-.tar.gz, +# built on Ubuntu 22.04 by package.sh) instead of compiling Rust inside flatpak-builder: no +# network and no vendored crate sources during the build, a few seconds instead of 20+ minutes, +# and byte-for-byte the same binaries as the AppImage/deb/rpm. The script unpacks the tarball +# into `stage/` next to a copy of this file. The binaries need glibc >= 2.35 and load X11, +# Wayland, xkbcommon and Vulkan at runtime; the freedesktop runtime has all of them, and the +# GPU driver comes from its org.freedesktop.Platform.GL extension. +# +# ai.storyteller.TextureCraft.yml (same directory) is the from-source manifest for Flathub. Keep +# runtime-version and finish-args identical in both (packaging-lint checks this). +id: ai.storyteller.texturecraft +runtime: org.freedesktop.Platform +runtime-version: '26.08' +sdk: org.freedesktop.Sdk +command: texturecraft +finish-args: + # Display: Wayland first; X11 only when no Wayland compositor is running. + - --socket=wayland + - --socket=fallback-x11 + # X11 shared memory (MIT-SHM). Without it the X11 fallback is slow or fails. + - --share=ipc + # GPU access for the wgpu viewport (Vulkan, or GL as a fallback). + - --device=dri + # Open/Save dialogs go through the file-chooser portal (rfd's xdg-portal backend) and + # drag-and-drop through the document portal, so picked files work anywhere without broad + # filesystem access. Pictures and Documents are granted so files passed by path (the command + # line, Open Recent, "Save" next to the original) work in the usual places. + - --filesystem=xdg-pictures + - --filesystem=xdg-documents + # Deliberately not granted: network (the optional --control server listens on 127.0.0.1 in + # the sandbox; `flatpak override --user --share=network ai.storyteller.texturecraft` exposes it to + # host agents) and home/host filesystem access. +modules: + - name: texturecraft + buildsystem: simple + build-commands: + - install -Dm755 bin/texturecraft /app/bin/texturecraft + - install -Dm755 bin/texturecraft-cli /app/bin/texturecraft-cli + - install -Dm644 share/applications/ai.storyteller.texturecraft.desktop /app/share/applications/ai.storyteller.texturecraft.desktop + # .tcraft, plus mesh types older shared-mime-info databases lack. + - install -Dm644 share/mime/packages/ai.storyteller.texturecraft.xml /app/share/mime/packages/ai.storyteller.texturecraft.xml + # Already has the release's version and date substituted by package.sh. + - install -Dm644 share/metainfo/ai.storyteller.texturecraft.metainfo.xml /app/share/metainfo/ai.storyteller.texturecraft.metainfo.xml + - mkdir -p /app/share/icons /app/share/doc && cp -R share/icons/hicolor /app/share/icons/ && cp -R share/doc/texturecraft /app/share/doc/ + sources: + - type: dir + path: stage diff --git a/packaging/linux/flatpak/ai.storyteller.TextureCraft.yml b/packaging/linux/flatpak/ai.storyteller.TextureCraft.yml new file mode 100644 index 00000000..3b680178 --- /dev/null +++ b/packaging/linux/flatpak/ai.storyteller.TextureCraft.yml @@ -0,0 +1,66 @@ +# Flatpak manifest for TextureCraft, ready for a Flathub submission. +# +# Flatpak builds run offline, so the crate sources must be vendored first. From the repo root: +# +# curl -fsSLO https://raw.githubusercontent.com/flatpak/flatpak-builder-tools/master/cargo/flatpak-cargo-generator.py +# python3 flatpak-cargo-generator.py Cargo.lock -o packaging/linux/flatpak/cargo-sources.json +# flatpak-builder --user --install --force-clean build-flatpak packaging/linux/flatpak/ai.storyteller.TextureCraft.yml +# flatpak run ai.storyteller.texturecraft +# +# For Flathub, replace the `dir` source with a `git` source pinned to a release tag and commit. +# The single-file .flatpak attached to each release comes from ai.storyteller.TextureCraft.bundle.yml, +# which repackages the prebuilt release binaries. Keep runtime-version and finish-args identical. +id: ai.storyteller.texturecraft +runtime: org.freedesktop.Platform +runtime-version: '26.08' +sdk: org.freedesktop.Sdk +sdk-extensions: + - org.freedesktop.Sdk.Extension.rust-stable +command: texturecraft +finish-args: + # Display: Wayland first; X11 only when no Wayland compositor is running. + - --socket=wayland + - --socket=fallback-x11 + # X11 shared memory (MIT-SHM). Without it the X11 fallback is slow or fails. + - --share=ipc + # GPU access for the wgpu viewport (Vulkan, or GL as a fallback). + - --device=dri + # Open/Save dialogs go through the file-chooser portal (rfd's xdg-portal backend) and + # drag-and-drop through the document portal, so picked files work anywhere without broad + # filesystem access. Pictures and Documents are granted so files passed by path (the command + # line, Open Recent, "Save" next to the original) work in the usual places. + - --filesystem=xdg-pictures + - --filesystem=xdg-documents + # Deliberately not granted: network (the optional --control server listens on 127.0.0.1 in + # the sandbox; `flatpak override --user --share=network ai.storyteller.texturecraft` exposes it to + # host agents) and home/host filesystem access. +build-options: + append-path: /usr/lib/sdk/rust-stable/bin +modules: + - name: texturecraft + buildsystem: simple + build-options: + env: + CARGO_HOME: /run/build/texturecraft/cargo + CARGO_NET_OFFLINE: 'true' + build-commands: + - cargo --offline build --release --locked -p texturecraft -p texturecraft-cli + - install -Dm755 target/release/texturecraft /app/bin/texturecraft + - install -Dm755 target/release/texturecraft-cli /app/bin/texturecraft-cli + - install -Dm644 packaging/linux/ai.storyteller.texturecraft.desktop /app/share/applications/ai.storyteller.texturecraft.desktop + - install -Dm644 packaging/linux/ai.storyteller.texturecraft.mime.xml /app/share/mime/packages/ai.storyteller.texturecraft.xml + - mkdir -p /app/share/icons && cp -R assets/app-icon/hicolor /app/share/icons/ + - >- + version=$(awk '/^\[/{p=($0=="[workspace.package]");next} p&&$1=="version"{gsub(/[" ]/,"",$3);print $3;exit}' Cargo.toml); + install -d /app/share/metainfo; + sed -e "s/@VERSION@/$version/" -e "s/@DATE@/$(date -u +%Y-%m-%d)/" + packaging/linux/ai.storyteller.texturecraft.metainfo.xml.in > /app/share/metainfo/ai.storyteller.texturecraft.metainfo.xml + sources: + - type: dir + path: ../../.. + skip: + - target + - dist + - build-flatpak + - .flatpak-builder + - cargo-sources.json diff --git a/packaging/linux/nfpm.yaml b/packaging/linux/nfpm.yaml new file mode 100644 index 00000000..30aca20a --- /dev/null +++ b/packaging/linux/nfpm.yaml @@ -0,0 +1,81 @@ +# .deb and .rpm packages (nfpm: https://nfpm.goreleaser.com). Run by package.sh, which stages +# the file tree, substitutes its path for ${STAGE} (nfpm doesn't expand env vars in `src`) and +# exports VERSION and NFPM_ARCH. +# +# nfpm package -f packaging/linux/nfpm.yaml -p deb -t dist/release/ +name: texturecraft +arch: ${NFPM_ARCH} +platform: linux +version: ${VERSION} +# semver: 0.2.0-rc.1 becomes 0.2.0~rc.1 in deb/rpm, so pre-releases sort before the release. +version_schema: semver +release: 1 +section: graphics +priority: optional +maintainer: ${TEXTURECRAFT_MAINTAINER} +vendor: Learning Machines LLC +homepage: https://getartcraft.com/apps/texturecraft +license: MIT OR Apache-2.0 +description: |- + Open-source, native 3D texture painter that works the way Substance 3D + Painter users expect. Paints PBR texture sets directly on glTF, OBJ, PLY and + STL meshes with layers, masks, fills and smart materials; bakes mesh maps; + exports texture sets for game engines. GPU viewport (wgpu). + Includes texturecraft-cli, a headless converter and command runner. + +# The binaries link only glibc and libgcc_s directly. Windowing (X11/Wayland, xkbcommon) and the +# GPU (Vulkan, EGL/GL) are loaded at runtime with dlopen, so they aren't ELF NEEDED entries and +# the package tools can't find them: list them here. winit 0.30 loads, for X11: libX11, +# libX11-xcb, libxcb, libXcursor, libXi, libxkbcommon and libxkbcommon-x11 (missing +# libxkbcommon-x11 panics); for Wayland: libwayland-client and libxkbcommon. wgpu needs libvulkan +# or libEGL. RandR goes through x11rb's protocol code and fonts are found by scanning directories, +# so libXrandr and fontconfig aren't loaded. +overrides: + deb: + depends: + - libc6 (>= 2.35) + - libgcc-s1 + - libxkbcommon0 + - libxkbcommon-x11-0 + - libwayland-client0 + - libx11-6 + - libx11-xcb1 + - libxcb1 + - libxcursor1 + - libxi6 + - libvulkan1 | libegl1 + recommends: + - mesa-vulkan-drivers + - libegl-mesa0 + - xdg-desktop-portal + rpm: + # Soname provides rather than package names, so the same requirements resolve on Fedora/RHEL + # (libxkbcommon-x11, libX11-xcb, ...) and openSUSE (libxkbcommon-x11-0, libX11-xcb1, ...). + depends: + - libxkbcommon.so.0()(64bit) + - libxkbcommon-x11.so.0()(64bit) + - libwayland-client.so.0()(64bit) + - libX11.so.6()(64bit) + - libX11-xcb.so.1()(64bit) + - libxcb.so.1()(64bit) + - libXcursor.so.1()(64bit) + - libXi.so.6()(64bit) + - (libvulkan.so.1()(64bit) or libEGL.so.1()(64bit)) + recommends: + - mesa-vulkan-drivers + - xdg-desktop-portal + +contents: + - src: ${STAGE}/usr/bin/texturecraft + dst: /usr/bin/texturecraft + file_info: { mode: 0755 } + - src: ${STAGE}/usr/bin/texturecraft-cli + dst: /usr/bin/texturecraft-cli + file_info: { mode: 0755 } + - src: ${STAGE}/usr/share/ + dst: /usr/share/ + type: tree + +scripts: + postinstall: packaging/linux/postinst.sh + postremove: packaging/linux/postinst.sh diff --git a/packaging/linux/package.sh b/packaging/linux/package.sh new file mode 100644 index 00000000..81f8884d --- /dev/null +++ b/packaging/linux/package.sh @@ -0,0 +1,144 @@ +#!/usr/bin/env bash +# Build and package TextureCraft for Linux ( is x86_64 or aarch64): +# +# $DIST/texturecraft--linux-.AppImage any distro with glibc >= the build host's +# $DIST/texturecraft--linux-.AppImage.zsync delta updates (needs zsyncmake) +# $DIST/texturecraft--linux-.deb Debian, Ubuntu, Mint, Pop!_OS, ... +# $DIST/texturecraft--linux-.rpm Fedora, openSUSE, RHEL, ... +# $DIST/texturecraft--linux-.tar.gz plain FHS-style tree (bin/, share/) +# +# Usage: packaging/linux/package.sh [--skip-build] [--formats "appimage deb rpm tar"] +# +# Needs: cargo; nfpm for deb/rpm (https://nfpm.goreleaser.com); appimagetool for the AppImage +# (downloaded into $CARGO_TARGET_DIR if missing). Build on an old distro (CI: Ubuntu 22.04, +# glibc 2.35) so the binaries run on newer ones. Optional: desktop-file-validate, appstreamcli, +# zsyncmake (the zsync package) for the AppImage's .zsync. +set -euo pipefail +# shellcheck source=../env.sh +. "$(dirname "${BASH_SOURCE[0]}")/../env.sh" +HERE="$ROOT/packaging/linux" +APP_ID=ai.storyteller.texturecraft + +SKIP_BUILD=0 +FORMATS="appimage deb rpm tar" +while [ $# -gt 0 ]; do + case "$1" in + --skip-build) SKIP_BUILD=1; shift ;; + --formats) FORMATS="$2"; shift 2 ;; + -h | --help) sed -n '2,15p' "$0"; exit 0 ;; + *) echo "unknown argument: $1" >&2; exit 2 ;; + esac +done + +ARCH="$(uname -m)" +case "$ARCH" in + x86_64) DEB_ARCH=amd64 ;; + aarch64 | arm64) ARCH=aarch64; DEB_ARCH=arm64 ;; + *) echo "unsupported architecture $ARCH" >&2; exit 2 ;; +esac +export TEXTURECRAFT_MAINTAINER="${TEXTURECRAFT_MAINTAINER:-TextureCraft maintainers }" +BASENAME="texturecraft-$VERSION-linux-$ARCH" + +echo "==> TextureCraft $VERSION for Linux $ARCH ($FORMATS)" + +if [ "$SKIP_BUILD" = 0 ]; then + (cd "$ROOT" && cargo build --release --locked -p texturecraft -p texturecraft-cli) +fi +BIN="$CARGO_TARGET_DIR/release" +WORK="$CARGO_TARGET_DIR/linux-package" +STAGE="$WORK/root" +rm -rf "$WORK" + +# ---- stage an FHS tree (shared by every format) ------------------------------------------------- +install -Dm755 "$BIN/texturecraft" "$STAGE/usr/bin/texturecraft" +install -Dm755 "$BIN/texturecraft-cli" "$STAGE/usr/bin/texturecraft-cli" +strip "$STAGE/usr/bin/texturecraft" "$STAGE/usr/bin/texturecraft-cli" 2>/dev/null || true +install -Dm644 "$HERE/$APP_ID.desktop" "$STAGE/usr/share/applications/$APP_ID.desktop" +install -Dm644 "$HERE/$APP_ID.mime.xml" "$STAGE/usr/share/mime/packages/$APP_ID.xml" +mkdir -p "$STAGE/usr/share/metainfo" +sed -e "s/@VERSION@/$VERSION/g" -e "s/@DATE@/$TEXTURECRAFT_BUILD_DATE/g" \ + "$HERE/$APP_ID.metainfo.xml.in" >"$STAGE/usr/share/metainfo/$APP_ID.metainfo.xml" +mkdir -p "$STAGE/usr/share/icons" +cp -R "$ROOT/assets/app-icon/hicolor" "$STAGE/usr/share/icons/" +mkdir -p "$STAGE/usr/share/doc/texturecraft" +copy_docs "$STAGE/usr/share/doc/texturecraft" + +if command -v desktop-file-validate >/dev/null; then + desktop-file-validate "$STAGE/usr/share/applications/$APP_ID.desktop" +fi +if command -v appstreamcli >/dev/null; then + appstreamcli validate --no-net --explain "$STAGE/usr/share/metainfo/$APP_ID.metainfo.xml" +fi + +has() { case " $FORMATS " in *" $1 "*) return 0 ;; *) return 1 ;; esac; } + +# ---- .tar.gz ------------------------------------------------------------------------------------ +if has tar; then + mkdir -p "$WORK/tar" + cp -R "$STAGE/usr" "$WORK/tar/$BASENAME" + tar -C "$WORK/tar" -czf "$DIST/$BASENAME.tar.gz" "$BASENAME" + echo "wrote $DIST/$BASENAME.tar.gz" +fi + +# ---- .deb / .rpm -------------------------------------------------------------------------------- +if has deb || has rpm; then + command -v nfpm >/dev/null || { echo "error: nfpm not found (https://nfpm.goreleaser.com/install/)" >&2; exit 1; } + export VERSION + export NFPM_ARCH="$DEB_ARCH" + # nfpm expands env vars in fields like `version` and `arch`, but not in `contents[].src`. + sed "s|\${STAGE}|$STAGE|g" "$HERE/nfpm.yaml" >"$WORK/nfpm.yaml" + for fmt in deb rpm; do + if has "$fmt"; then (cd "$ROOT" && nfpm package -f "$WORK/nfpm.yaml" -p "$fmt" -t "$DIST/$BASENAME.$fmt"); fi + done +fi + +# ---- AppImage ----------------------------------------------------------------------------------- +if has appimage; then + APPDIR="$WORK/TextureCraft.AppDir" + cp -R "$STAGE" "$APPDIR" + mv "$APPDIR/usr/share/doc" "$WORK/doc-unused" + # AppRun is a script, not a symlink: it installs the .desktop entry and icons into + # $XDG_DATA_HOME before launching, so Wayland compositors can resolve the window's + # app_id to our icon instead of the generic Wayland logo (opt-out env in the + # script header). Logic is covered by packaging/linux/apprun-test.sh. + install -Dm755 "$HERE/AppRun" "$APPDIR/AppRun" + cp "$HERE/$APP_ID.desktop" "$APPDIR/$APP_ID.desktop" + cp "$ROOT/assets/app-icon/hicolor/256x256/apps/$APP_ID.png" "$APPDIR/$APP_ID.png" + ln -s "$APP_ID.png" "$APPDIR/.DirIcon" + + TOOL="${APPIMAGETOOL:-$(command -v appimagetool || true)}" + if [ -z "$TOOL" ]; then + TOOL="$CARGO_TARGET_DIR/appimagetool-$ARCH.AppImage" + if [ ! -x "$TOOL" ]; then + curl -fsSL -o "$TOOL" "https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-$ARCH.AppImage" + chmod +x "$TOOL" + fi + fi + # Absolute, because appimagetool runs in $DIST below (CARGO_TARGET_DIR or APPIMAGETOOL may be + # relative, e.g. target/agent-). + OUT="$(cd "$DIST" && pwd)/$BASENAME.AppImage" + APPDIR="$(cd "$APPDIR" && pwd)" + TOOL="$(cd "$(dirname "$TOOL")" && pwd)/$(basename "$TOOL")" + # A .zsync left by an earlier run would hide a missing zsyncmake and describe another file. + rm -f "$OUT.zsync" + # Update information: AppImageUpdate, AppImageLauncher and the like read it from the + # file and fetch only the blocks that changed in a newer release, through the .zsync published + # next to each AppImage on GitHub Releases. `latest` is the newest published release that is + # not a pre-release. A fork's builds point at its own releases through GITHUB_REPOSITORY. + REPO="${GITHUB_REPOSITORY:-storytold/texturecraft}" + UPDATE_INFO="gh-releases-zsync|${REPO%%/*}|${REPO#*/}|latest|texturecraft-*-linux-$ARCH.AppImage.zsync" + # Extract-and-run: works without FUSE (containers, CI). The output embeds the static runtime, + # so users don't need libfuse2 either. With zsyncmake on the host (CI installs the zsync + # package) appimagetool also writes the .zsync, into its working directory, hence the cd. + (cd "$DIST" && ARCH="$ARCH" APPIMAGE_EXTRACT_AND_RUN=1 "$TOOL" --no-appstream -u "$UPDATE_INFO" "$APPDIR" "$OUT") + echo "wrote $OUT" + if [ -s "$OUT.zsync" ]; then + echo "wrote $OUT.zsync" + else + warn "zsyncmake not found, so $OUT.zsync was not written; AppImage delta updates need it" + fi +fi + +"$STAGE/usr/bin/texturecraft-cli" --version +echo "==> done" +ls -lh "$DIST" diff --git a/packaging/linux/postinst.sh b/packaging/linux/postinst.sh new file mode 100644 index 00000000..775e4df9 --- /dev/null +++ b/packaging/linux/postinst.sh @@ -0,0 +1,13 @@ +#!/bin/sh +# deb/rpm post-install and post-remove: refresh the desktop, MIME and icon caches. +# Every tool is optional; missing ones are skipped. +if command -v update-desktop-database >/dev/null 2>&1; then + update-desktop-database -q /usr/share/applications || true +fi +if command -v update-mime-database >/dev/null 2>&1; then + update-mime-database /usr/share/mime || true +fi +if command -v gtk-update-icon-cache >/dev/null 2>&1; then + gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor || true +fi +exit 0 diff --git a/packaging/macos/Info.plist.in b/packaging/macos/Info.plist.in new file mode 100644 index 00000000..29391ec0 --- /dev/null +++ b/packaging/macos/Info.plist.in @@ -0,0 +1,183 @@ + + + + + + CFBundleDevelopmentRegion + en + CFBundleDisplayName + TextureCraft + CFBundleName + TextureCraft + CFBundleExecutable + TextureCraft + CFBundleIdentifier + ai.storyteller.texturecraft + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + APPL + CFBundleSignature + ???? + CFBundleShortVersionString + @SHORT_VERSION@ + CFBundleVersion + @SHORT_VERSION@ + TextureCraftVersion + @VERSION@ + TextureCraftBuildCommit + @BUILD_SHA@ + CFBundleIconFile + TextureCraft + LSApplicationCategoryType + public.app-category.graphics-design + + LSMinimumSystemVersion + 11.0 + NSHighResolutionCapable + + NSSupportsAutomaticGraphicsSwitching + + NSRequiresAquaSystemAppearance + + NSHumanReadableCopyright + Copyright © the TextureCraft contributors. MIT OR Apache-2.0. + + + UTExportedTypeDeclarations + + + UTTypeIdentifier + ai.storyteller.texturecraft.project + UTTypeDescription + TextureCraft Project + UTTypeConformsTo + + public.data + public.content + + UTTypeIconFile + TextureCraft + UTTypeTagSpecification + + public.filename-extension + tcraft + public.mime-type + application/x-texturecraft + + + + + UTImportedTypeDeclarations + + + UTTypeIdentifier + ai.storyteller.texturecraft.gltf-binary + UTTypeDescription + glTF Binary Model + UTTypeConformsTo + public.datapublic.3d-content + UTTypeTagSpecification + + public.filename-extension + glb + public.mime-type + model/gltf-binary + + + + UTTypeIdentifier + ai.storyteller.texturecraft.gltf + UTTypeDescription + glTF Model + UTTypeConformsTo + public.datapublic.3d-content + UTTypeTagSpecification + + public.filename-extension + gltf + public.mime-type + model/gltf+json + + + + UTTypeIdentifier + public.geometry-definition-format + UTTypeDescription + Wavefront OBJ Model + UTTypeConformsTo + public.datapublic.3d-content + UTTypeTagSpecification + + public.filename-extension + obj + public.mime-type + model/obj + + + + UTTypeIdentifier + public.polygon-file-format + UTTypeDescription + PLY Model + UTTypeConformsTo + public.datapublic.3d-content + UTTypeTagSpecification + + public.filename-extension + ply + + + + UTTypeIdentifier + public.standard-tesselated-geometry-format + UTTypeDescription + STL Model + UTTypeConformsTo + public.datapublic.3d-content + UTTypeTagSpecification + + public.filename-extension + stl + public.mime-type + model/stl + + + + + CFBundleDocumentTypes + + + CFBundleTypeName + TextureCraft Project + CFBundleTypeRole + Editor + LSHandlerRank + Owner + CFBundleTypeIconFile + TextureCraft + LSItemContentTypes + ai.storyteller.texturecraft.project + + + + CFBundleTypeName + 3D Model + CFBundleTypeRole + Viewer + LSHandlerRank + Alternate + LSItemContentTypes + + ai.storyteller.texturecraft.gltf-binary + ai.storyteller.texturecraft.gltf + public.geometry-definition-format + public.polygon-file-format + public.standard-tesselated-geometry-format + + + + + diff --git a/packaging/macos/entitlements.plist b/packaging/macos/entitlements.plist new file mode 100644 index 00000000..295c5d95 --- /dev/null +++ b/packaging/macos/entitlements.plist @@ -0,0 +1,9 @@ + + + + + + + diff --git a/packaging/macos/import-cert.sh b/packaging/macos/import-cert.sh new file mode 100644 index 00000000..249dfed1 --- /dev/null +++ b/packaging/macos/import-cert.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# CI only: import the Developer ID certificate into a temporary keychain so codesign can use it. +# +# Inputs (env): APPLE_CERTIFICATE (base64 .p12), APPLE_CERTIFICATE_PASSWORD, KEYCHAIN_PASSWORD +# (optional; random if unset), RUNNER_TEMP (or TMPDIR). +# Outputs: MACOS_SIGN_IDENTITY (SHA-1 of the identity) and MACOS_KEYCHAIN, appended to +# $GITHUB_ENV when running in Actions, otherwise printed. +# +# Missing certificate: prints a warning and exits 0, so package.sh falls back to ad-hoc signing. +set -euo pipefail +umask 077 +# shellcheck source=../env.sh +. "$(dirname "${BASH_SOURCE[0]}")/../env.sh" + +if [ -z "${APPLE_CERTIFICATE:-}" ]; then + warn "APPLE_CERTIFICATE is not set: macOS builds will be ad-hoc signed and not notarized" + exit 0 +fi + +TMP="${RUNNER_TEMP:-${TMPDIR:-/tmp}}" +KEYCHAIN="$TMP/texturecraft-signing.keychain-db" +CERT="$TMP/texturecraft-signing.p12" +KC_PASS="${KEYCHAIN_PASSWORD:-$(openssl rand -hex 24)}" + +trap 'rm -f "$CERT"' EXIT +printf '%s' "$APPLE_CERTIFICATE" | base64 --decode >"$CERT" +security create-keychain -p "$KC_PASS" "$KEYCHAIN" +security set-keychain-settings -lut 21600 "$KEYCHAIN" +security unlock-keychain -p "$KC_PASS" "$KEYCHAIN" +security import "$CERT" -P "${APPLE_CERTIFICATE_PASSWORD:-}" -T /usr/bin/codesign -T /usr/bin/security -t cert -f pkcs12 -k "$KEYCHAIN" +rm -f "$CERT" +# Let codesign use the key without a UI prompt. +security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$KC_PASS" "$KEYCHAIN" >/dev/null +# Put it on the search list (keeping the login keychain) so codesign and notarytool find it. +# shellcheck disable=SC2046 +security list-keychains -d user -s "$KEYCHAIN" $(security list-keychains -d user | tr -d '"') + +IDENTITY="$(security find-identity -v -p codesigning "$KEYCHAIN" | awk '/Developer ID Application/ { print $2; exit }')" +if [ -z "$IDENTITY" ]; then + security find-identity -v -p codesigning "$KEYCHAIN" >&2 + echo "error: no 'Developer ID Application' identity in APPLE_CERTIFICATE" >&2 + exit 1 +fi +echo "Imported signing identity $IDENTITY" +if [ -n "${GITHUB_ENV:-}" ]; then + { echo "MACOS_SIGN_IDENTITY=$IDENTITY"; echo "MACOS_KEYCHAIN=$KEYCHAIN"; } >>"$GITHUB_ENV" +else + echo "MACOS_SIGN_IDENTITY=$IDENTITY" + echo "MACOS_KEYCHAIN=$KEYCHAIN" +fi diff --git a/packaging/macos/package.sh b/packaging/macos/package.sh new file mode 100644 index 00000000..43a3c218 --- /dev/null +++ b/packaging/macos/package.sh @@ -0,0 +1,165 @@ +#!/usr/bin/env bash +# Build, sign and (optionally) notarize the macOS release artifacts: +# +# $DIST/texturecraft--macos-.dmg TextureCraft.app on a drag-to-Applications DMG +# $DIST/texturecraft-cli--macos-.zip the headless CLI +# +# Usage: packaging/macos/package.sh [--arch universal|aarch64|x86_64] [--skip-build] +# +# Signing (env): +# MACOS_SIGN_IDENTITY codesign identity (name or SHA-1). Default "-" = ad-hoc (local testing; +# Gatekeeper will reject the result on other Macs). In CI, import-cert.sh sets it. +# MACOS_KEYCHAIN keychain holding the identity (optional) +# Notarization (env; all three needed, and a real identity): +# APPLE_ID, APPLE_PASSWORD (app-specific password), APPLE_TEAM_ID +set -euo pipefail +# shellcheck source=../env.sh +. "$(dirname "${BASH_SOURCE[0]}")/../env.sh" +HERE="$ROOT/packaging/macos" + +ARCH=universal +SKIP_BUILD=0 +while [ $# -gt 0 ]; do + case "$1" in + --arch) ARCH="$2"; shift 2 ;; + --skip-build) SKIP_BUILD=1; shift ;; + -h | --help) sed -n '2,16p' "$0"; exit 0 ;; + *) echo "unknown argument: $1" >&2; exit 2 ;; + esac +done +case "$ARCH" in + universal) TARGETS=(aarch64-apple-darwin x86_64-apple-darwin) ;; + aarch64) TARGETS=(aarch64-apple-darwin) ;; + x86_64) TARGETS=(x86_64-apple-darwin) ;; + *) echo "unknown --arch $ARCH" >&2; exit 2 ;; +esac + +# Keep in sync with LSMinimumSystemVersion in Info.plist.in. +export MACOSX_DEPLOYMENT_TARGET=11.0 +IDENTITY="${MACOS_SIGN_IDENTITY:--}" +SHORT_VERSION="${VERSION%%-*}" +WORK="$CARGO_TARGET_DIR/macos-package" +APP="$WORK/TextureCraft.app" +DMG="$DIST/texturecraft-$VERSION-macos-$ARCH.dmg" +CLI_ZIP="$DIST/texturecraft-cli-$VERSION-macos-$ARCH.zip" + +NOTARIZE=0 +if [ "$IDENTITY" = "-" ]; then + warn "macOS: ad-hoc signing (no MACOS_SIGN_IDENTITY); artifacts are not notarized" +elif [ -n "${APPLE_ID:-}" ] && [ -n "${APPLE_PASSWORD:-}" ] && [ -n "${APPLE_TEAM_ID:-}" ]; then + NOTARIZE=1 +else + warn "macOS: APPLE_ID / APPLE_PASSWORD / APPLE_TEAM_ID incomplete; signed but not notarized" +fi + +echo "==> TextureCraft $VERSION for macOS ($ARCH), identity: $IDENTITY, notarize: $NOTARIZE" + +# ---- build ------------------------------------------------------------------------------------- +if [ "$SKIP_BUILD" = 0 ]; then + args=() + for t in "${TARGETS[@]}"; do args+=(--target "$t"); done + (cd "$ROOT" && cargo build --release --locked -p texturecraft -p texturecraft-cli "${args[@]}") +fi + +rm -rf "$WORK" +mkdir -p "$WORK/bin" +for bin in texturecraft texturecraft-cli; do + inputs=() + for t in "${TARGETS[@]}"; do inputs+=("$CARGO_TARGET_DIR/$t/release/$bin"); done + lipo -create -output "$WORK/bin/$bin" "${inputs[@]}" + lipo -info "$WORK/bin/$bin" +done + +# ---- signing helpers --------------------------------------------------------------------------- +sign() { + # Hardened runtime + secure timestamp for a real identity; ad-hoc can't be timestamped. + local ts=(--timestamp) + [ "$IDENTITY" = "-" ] && ts=(--timestamp=none) + local kc=() + [ -n "${MACOS_KEYCHAIN:-}" ] && kc=(--keychain "$MACOS_KEYCHAIN") + codesign --force --sign "$IDENTITY" ${kc[@]+"${kc[@]}"} "${ts[@]}" "$@" +} + +notarize() { + local file="$1" out id status + echo "==> notarizing $(basename "$file") (this can take a few minutes)" + out="$(xcrun notarytool submit "$file" --apple-id "$APPLE_ID" --password "$APPLE_PASSWORD" \ + --team-id "$APPLE_TEAM_ID" --wait --timeout 1h --output-format json)" || true + echo "$out" + id="$(printf '%s' "$out" | plutil -extract id raw -o - - 2>/dev/null || true)" + status="$(printf '%s' "$out" | plutil -extract status raw -o - - 2>/dev/null || true)" + if [ "$status" != "Accepted" ]; then + if [ -n "$id" ]; then + xcrun notarytool log "$id" --apple-id "$APPLE_ID" --password "$APPLE_PASSWORD" --team-id "$APPLE_TEAM_ID" || true + fi + echo "error: notarization of $(basename "$file") failed (status: ${status:-unknown})" >&2 + exit 1 + fi +} + +# ---- TextureCraft.app ---------------------------------------------------------------------------- +echo "==> assembling $APP" +mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources" +# Executable and icon carry the display name (CFBundleExecutable / CFBundleIconFile). +cp "$WORK/bin/texturecraft" "$APP/Contents/MacOS/TextureCraft" +cp "$ROOT/assets/app-icon/texturecraft.icns" "$APP/Contents/Resources/TextureCraft.icns" +sed -e "s/@VERSION@/$VERSION/g" -e "s/@SHORT_VERSION@/$SHORT_VERSION/g" \ + -e "s/@BUILD_SHA@/${TEXTURECRAFT_BUILD_SHA:-unknown}/g" \ + "$HERE/Info.plist.in" >"$APP/Contents/Info.plist" +plutil -lint "$APP/Contents/Info.plist" +printf 'APPL????' >"$APP/Contents/PkgInfo" + +# Sign inside-out: nested code first, then the bundle itself (no --deep on the final signature). +# Today the only nested code is the main executable; frameworks/helpers would be signed here too. +sign --options runtime --entitlements "$HERE/entitlements.plist" "$APP/Contents/MacOS/TextureCraft" +sign --options runtime --entitlements "$HERE/entitlements.plist" "$APP" +codesign --verify --strict --deep --verbose=2 "$APP" + +if [ "$NOTARIZE" = 1 ]; then + ditto -c -k --keepParent "$APP" "$WORK/TextureCraft-notarize.zip" + notarize "$WORK/TextureCraft-notarize.zip" + xcrun stapler staple "$APP" + xcrun stapler validate "$APP" + spctl --assess --type execute -vvv "$APP" +fi + +# ---- DMG --------------------------------------------------------------------------------------- +echo "==> building $DMG" +STAGE="$WORK/dmg" +mkdir -p "$STAGE" +ditto "$APP" "$STAGE/TextureCraft.app" +ln -s /Applications "$STAGE/Applications" +rm -f "$DMG" "$WORK/raw.dmg" +# makehybrid + convert builds the image without attaching a device, unlike `create -srcfolder`, +# which is flaky on CI runners ("Resource busy") and hangs in sandboxed sessions. +hdiutil makehybrid -hfs -hfs-volume-name "TextureCraft $VERSION" -hfs-openfolder "$STAGE" -o "$WORK/raw.dmg" "$STAGE" +hdiutil convert "$WORK/raw.dmg" -format UDZO -imagekey zlib-level=9 -o "$DMG" +rm -f "$WORK/raw.dmg" +sign "$DMG" +codesign --verify --strict --verbose=2 "$DMG" +if [ "$NOTARIZE" = 1 ]; then + notarize "$DMG" + xcrun stapler staple "$DMG" + xcrun stapler validate "$DMG" + spctl --assess --type open --context context:primary-signature -vvv "$DMG" +fi + +# ---- CLI --------------------------------------------------------------------------------------- +echo "==> building $CLI_ZIP" +CLI_DIR="$WORK/texturecraft-cli-$VERSION-macos-$ARCH" +mkdir -p "$CLI_DIR" +cp "$WORK/bin/texturecraft-cli" "$CLI_DIR/" +copy_docs "$CLI_DIR" +# Same Developer ID and hardened runtime as the app, with an explicit reverse-DNS identifier +# (codesign would otherwise use the bare file name). +sign --options runtime --identifier ai.storyteller.texturecraft-cli "$CLI_DIR/texturecraft-cli" +codesign --verify --strict --verbose=2 "$CLI_DIR/texturecraft-cli" +rm -f "$CLI_ZIP" +ditto -c -k --keepParent "$CLI_DIR" "$CLI_ZIP" +# notarytool accepts a zip of the bare binary. A bare Mach-O can't carry a stapled ticket, so +# Gatekeeper looks the notarization up online when a quarantined copy first runs. +if [ "$NOTARIZE" = 1 ]; then notarize "$CLI_ZIP"; fi + +"$WORK/bin/texturecraft-cli" --version +echo "==> done (check the artifacts as shipped with packaging/macos/verify.sh --arch $ARCH)" +ls -lh "$DMG" "$CLI_ZIP" diff --git a/packaging/macos/verify.sh b/packaging/macos/verify.sh new file mode 100644 index 00000000..30dc1458 --- /dev/null +++ b/packaging/macos/verify.sh @@ -0,0 +1,149 @@ +#!/usr/bin/env bash +# Check the macOS release artifacts exactly as users download them: +# +# $DIST/texturecraft--macos-.dmg signed, notarized, ticket stapled +# $DIST/texturecraft-cli--macos-.zip unpacked here; the binary inside must be +# Developer ID signed with the hardened runtime, +# timestamped and notarized +# +# Usage: packaging/macos/verify.sh [--arch universal|aarch64|x86_64] +# +# With a real signing identity and notarization credentials in the environment (the same +# MACOS_SIGN_IDENTITY / APPLE_ID / APPLE_PASSWORD / APPLE_TEAM_ID that package.sh uses), every +# check is strict and a failure exits non-zero. Without them (local ad-hoc builds, or a release run +# whose secrets are missing) only the signature's integrity is checked and the rest is a warning, +# matching package.sh. Nothing here reads or prints a secret's value; it only tests whether one is set. +# +# A bare Mach-O can't carry a stapled notarization ticket (only .app, .dmg and .pkg can), so for the +# CLI Gatekeeper looks the ticket up online the first time a quarantined copy runs. `spctl --assess +# --type install` does that same online lookup here, which is how this script proves the CLI was +# notarized. +set -euo pipefail +# shellcheck source=../env.sh +. "$(dirname "${BASH_SOURCE[0]}")/../env.sh" + +ARCH=universal +while [ $# -gt 0 ]; do + case "$1" in + --arch) ARCH="$2"; shift 2 ;; + -h | --help) sed -n '2,20p' "$0"; exit 0 ;; + *) echo "unknown argument: $1" >&2; exit 2 ;; + esac +done +case "$ARCH" in + universal) WANT_ARCHS="x86_64 arm64" ;; + aarch64) WANT_ARCHS="arm64" ;; + x86_64) WANT_ARCHS="x86_64" ;; + *) echo "unknown --arch $ARCH" >&2; exit 2 ;; +esac + +DMG="$DIST/texturecraft-$VERSION-macos-$ARCH.dmg" +CLI_ZIP="$DIST/texturecraft-cli-$VERSION-macos-$ARCH.zip" +IDENTITY="${MACOS_SIGN_IDENTITY:--}" +STRICT=0 +if [ "$IDENTITY" != "-" ] && [ -n "${APPLE_ID:-}" ] && [ -n "${APPLE_PASSWORD:-}" ] && [ -n "${APPLE_TEAM_ID:-}" ]; then + STRICT=1 +fi + +FAILED=0 +fail() { + if [ "$STRICT" = 1 ]; then + echo "error: $*" >&2 + FAILED=1 + else + warn "$* (expected without signing secrets)" + fi +} + +# spctl's online ticket lookup can lag a few seconds behind notarytool's "Accepted". +assess() { + local out attempt tries=1 + [ "$STRICT" = 1 ] && tries=5 + for attempt in $(seq "$tries"); do + if out="$(spctl "$@" 2>&1)"; then + printf '%s\n' "$out" + return 0 + fi + [ "$attempt" = "$tries" ] || sleep 10 + done + printf '%s\n' "$out" + return 1 +} + +for f in "$DMG" "$CLI_ZIP"; do + if [ ! -f "$f" ]; then + echo "error: missing $f" >&2 + exit 1 + fi +done + +WORK="$(mktemp -d "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/texturecraft-verify.XXXXXX")" +trap 'rm -rf "$WORK"' EXIT + +# ---- CLI zip ----------------------------------------------------------------------------------- +echo "==> $(basename "$CLI_ZIP")" +ditto -x -k "$CLI_ZIP" "$WORK/cli" +CLI="$WORK/cli/texturecraft-cli-$VERSION-macos-$ARCH/texturecraft-cli" +if [ ! -x "$CLI" ]; then + echo "error: $(basename "$CLI_ZIP") has no executable texturecraft-cli-$VERSION-macos-$ARCH/texturecraft-cli" >&2 + exit 1 +fi + +archs="$(lipo -archs "$CLI")" +for a in $WANT_ARCHS; do + case " $archs " in + *" $a "*) ;; + *) echo "error: texturecraft-cli lacks $a (has: $archs)" >&2; exit 1 ;; + esac +done + +# Integrity first: this must hold even for ad-hoc builds. +codesign --verify --strict --verbose=2 "$CLI" +details="$(codesign -d --verbose=2 "$CLI" 2>&1)" +printf '%s\n' "$details" | grep -E '^(Identifier|Format|CodeDirectory|Authority|Timestamp|TeamIdentifier)[= ]' || true + +printf '%s\n' "$details" | grep -Eq '^CodeDirectory .*flags=0x[0-9a-f]+\([^)]*runtime' \ + || fail "texturecraft-cli is not signed with the hardened runtime" +printf '%s\n' "$details" | grep -q '^Authority=Developer ID Application:' \ + || fail "texturecraft-cli is not signed with a Developer ID Application identity" +printf '%s\n' "$details" | grep -q '^Timestamp=' \ + || fail "texturecraft-cli signature has no secure timestamp" +if [ -n "${APPLE_TEAM_ID:-}" ]; then + printf '%s\n' "$details" | grep -qx "TeamIdentifier=$APPLE_TEAM_ID" \ + || fail "texturecraft-cli is signed by a different team" +fi +if gk="$(assess --assess --type install -vv "$CLI")"; then + printf '%s\n' "$gk" + printf '%s\n' "$gk" | grep -q 'source=Notarized Developer ID' \ + || fail "Gatekeeper accepts texturecraft-cli but not as notarized" +else + printf '%s\n' "$gk" + fail "Gatekeeper rejects texturecraft-cli (not notarized?)" +fi +"$CLI" --version + +# ---- DMG --------------------------------------------------------------------------------------- +echo "==> $(basename "$DMG")" +codesign --verify --strict --verbose=2 "$DMG" +if [ "$STRICT" = 1 ]; then + xcrun stapler validate "$DMG" || fail "no notarization ticket stapled to the DMG" + assess --assess --type open --context context:primary-signature -vv "$DMG" \ + || fail "Gatekeeper rejects the DMG" +fi + +if [ "$FAILED" != 0 ]; then + echo "error: macOS artifacts failed verification" >&2 + exit 1 +fi + +if [ "$STRICT" = 1 ]; then + result="signed (Developer ID, hardened runtime), notarized; DMG stapled" +else + result="signature intact; Developer ID and notarization not required (no signing secrets)" +fi +echo "==> verified: $result" +if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then + { + echo "- macOS \`$(basename "$DMG")\`, \`$(basename "$CLI_ZIP")\`: $result" + } >>"$GITHUB_STEP_SUMMARY" +fi diff --git a/packaging/web/.htaccess b/packaging/web/.htaccess new file mode 100644 index 00000000..7b7e6e18 --- /dev/null +++ b/packaging/web/.htaccess @@ -0,0 +1,16 @@ +# Apache (see HOSTING.md). Needs mod_mime; mod_deflate and mod_headers are optional. +AddType application/wasm .wasm +AddType text/javascript .js + + + AddOutputFilterByType DEFLATE application/wasm text/javascript text/html + + + + + Header set Cache-Control "public, max-age=31536000, immutable" + + + Header set Cache-Control "no-cache" + + diff --git a/packaging/web/README.md b/packaging/web/README.md new file mode 100644 index 00000000..914ce49c --- /dev/null +++ b/packaging/web/README.md @@ -0,0 +1,88 @@ +# Hosting TextureCraft for the web + +`texturecraft-web-.zip` (from the GitHub release, or `packaging/web/package.sh`) holds a +static site in `texturecraft-web-/`: + +| File | What it is | +|---|---| +| `index.html` | The page. It loads everything through relative URLs. | +| `texturecraft-web-.js` | wasm-bindgen glue (generated by trunk, ES module) | +| `texturecraft-web-_bg.wasm` | The app (built with the `wasm-release` Cargo profile) | +| `_headers`, `.htaccess` | Sample header rules for Netlify/Cloudflare Pages and Apache | + +There is no server-side code and no hand-written JavaScript: trunk generates the glue from the +Rust crate `apps/texturecraft-web`. Upload the folder's contents anywhere that serves static files. + +`packaging/web/package.sh` fails the build when a `.wasm` grows past 24 MiB, because Cloudflare +Pages and Workers reject any file over 25 MiB. + +## Any path works + +All URLs in `index.html` are relative (`public_url = "./"` in `apps/texturecraft-web/Trunk.toml`), +so the site works at a domain root (`https://example.com/`), under a prefix +(`https://example.com/tools/texturecraft/`) and from a CDN bucket. The asset names carry a content +hash, so they can be cached forever. Only `index.html` needs revalidation. + +## Required server settings + +- **MIME type:** serve `.wasm` as `application/wasm`. Browsers refuse to stream-compile it under + any other type, and the app then loads slowly or not at all. Serve `.js` as `text/javascript`. + For nginx, check that `mime.types` has `application/wasm wasm;`. +- **Compression:** turn on gzip or Brotli for `.wasm`, `.js` and `.html` (a wasm file typically + shrinks to well under half). You can also precompress (`brotli -k *.wasm`) and let the server + send `Content-Encoding: br`. +- **Caching:** `Cache-Control: public, max-age=31536000, immutable` on the hashed `.wasm` and + `.js` files, and `no-cache` on `index.html`. +- **HTTPS:** WebGPU (and the clipboard) only work in a secure context, which means `https://` + or `http://localhost`. Over plain HTTP elsewhere, the app falls back to WebGL2. +- **No special isolation headers:** the app doesn't use `SharedArrayBuffer`, so it doesn't need + `Cross-Origin-Opener-Policy` or `Cross-Origin-Embedder-Policy`. If your site already sends COEP + `require-corp`, also send `Cross-Origin-Resource-Policy: same-origin` (or `cross-origin` when + the files live on a CDN) on the app's files. + +nginx example: + +```nginx +location /texturecraft/ { + types { application/wasm wasm; text/javascript js; text/html html; } + gzip on; + gzip_types application/wasm text/javascript text/html; + location ~* \.(wasm|js)$ { add_header Cache-Control "public, max-age=31536000, immutable"; } + location ~* index\.html$ { add_header Cache-Control "no-cache"; } +} +``` + +Local test: `python3 -m http.server 8765` inside the folder, then open http://localhost:8765/. + +## Embedding in a page (iframe) + +```html + +``` + +- The app fills the iframe and follows its size, so size the iframe and not the app. +- Keyboard shortcuts go to the iframe after the user clicks into it, as with any embedded app. +- **Sandboxed iframes** need at least + `sandbox="allow-scripts allow-same-origin allow-downloads allow-popups"`. Without + `allow-same-origin` there's no storage. Without `allow-downloads`, Save and Export (browser + downloads) are blocked. +- Don't send `X-Frame-Options: DENY` or a `frame-ancestors` CSP that excludes the embedding page. + +## Renderer selection and fallback flags + +TextureCraft renders with wgpu. It uses **WebGPU** when the browser has it and falls back to +**WebGL2** on its own. URL query flags override this, and they work on the iframe `src` too: + +| Flag | Effect | +|---|---| +| *(none)* | WebGPU if available, otherwise WebGL2 | +| `?webgl` | Force the WebGL2 backend (useful when a WebGPU driver misbehaves, and for headless smoke tests) | + +The viewport needs a GPU backend, so there is no CPU-only mode. A browser with neither WebGPU nor +WebGL2 gets a message in place of the app. diff --git a/packaging/web/_headers b/packaging/web/_headers new file mode 100644 index 00000000..fac0fbe1 --- /dev/null +++ b/packaging/web/_headers @@ -0,0 +1,13 @@ +# Netlify / Cloudflare Pages header rules (see HOSTING.md). Other hosts: set the same headers. +/* + X-Content-Type-Options: nosniff + +/*.wasm + Content-Type: application/wasm + Cache-Control: public, max-age=31536000, immutable + +/*.js + Cache-Control: public, max-age=31536000, immutable + +/index.html + Cache-Control: no-cache diff --git a/packaging/web/package.sh b/packaging/web/package.sh new file mode 100644 index 00000000..baecbc0d --- /dev/null +++ b/packaging/web/package.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# Build the browser version and zip it: $DIST/texturecraft-web-.zip +# +# Usage: packaging/web/package.sh [--skip-build] +# +# Needs: trunk (brew install trunk / cargo install trunk --locked) and the wasm32-unknown-unknown +# target. The zip holds a self-contained static site in texturecraft-web-/ that works +# from any URL path and inside an