Textures: reconstruct tangent-space Z on decoded BC5 normal maps #69

Manually merged
github merged 1 commit from gh-pr/69/head into gh-pr/69/base 2026-11-26 20:07:23 +00:00
Owner

@⁠Ahmet-Dedeler on GitHub · 2026-11-23 18:52 UTC · original
Ahmet-Dedeler:fix/bc5-normal-z → master · +51 −3 · 3 files · 1 commits · merged 2026-11-23 19:11 UTC

texture2ddecoder::decode_bc5 outputs (R, G, 0, 255) for two-channel BC5 textures. Across packs.rpf (3,326 / 3,329 BC5 texture bindings), BC5 textures are tangent-space bumptex normal maps.

Leaving B = 0 caused three issues:

  1. Exported glTF normal maps unpacked to z = -1 (normals inverted into the surface) in standard glTF 2.0 viewers such as Blender.
  2. ttpf::stats() never set normal_like (m[2] > 200.0) on BC5 normal maps.
  3. The Metal 4 viewer shader (render3d_metal4.rs) read normal_texture.sample(...).xyz * 2.0 - 1.0 without reconstructing z (unlike shader.wgsl, which already computes sqrt(max(1 - x² - y², 0))).

Changes

  • sixng-core/src/texture.rs: Reconstruct B = sqrt(max(0, 1 - x² - y²)) after decode_bc5 in to_rgba. R and G (DirectX green) are left untouched so shader.wgsl and render3d_metal4.rs keep working consistently, and DDS output is unchanged. Added unit test bc5_reconstructs_normal_z.
  • sixng-core/src/ttpf.rs: Invalidate cached BC5 PNGs whose stored mean_rgba[2] < 1.0 so previously exported caches pick up the reconstructed Z channel and normal_like flag.
  • sixng-gui/src/render3d_metal4.rs: Reconstruct sampled.z = sqrt(max(1.0 - sampled.x * sampled.x - sampled.y * sampled.y, 0.0)) to match shader.wgsl.

Verification

  • cargo test -p sixng-core --no-default-features: 139 passed, 0 failed.
  • cargo check --no-default-features: clean across all workspace crates (sixng-core, sixng-cli, sixng-gui).
**@⁠Ahmet-Dedeler** on GitHub · 2026-11-23 18:52 UTC · [original](https://github.com/holdmysocks/SixNG/pull/69) `Ahmet-Dedeler:fix/bc5-normal-z` → `master` · +51 −3 · 3 files · 1 commits · merged 2026-11-23 19:11 UTC `texture2ddecoder::decode_bc5` outputs `(R, G, 0, 255)` for two-channel BC5 textures. Across `packs.rpf` (3,326 / 3,329 BC5 texture bindings), BC5 textures are tangent-space `bumptex` normal maps. Leaving `B = 0` caused three issues: 1. Exported glTF normal maps unpacked to `z = -1` (normals inverted into the surface) in standard glTF 2.0 viewers such as Blender. 2. `ttpf::stats()` never set `normal_like` (`m[2] > 200.0`) on BC5 normal maps. 3. The Metal 4 viewer shader (`render3d_metal4.rs`) read `normal_texture.sample(...).xyz * 2.0 - 1.0` without reconstructing `z` (unlike `shader.wgsl`, which already computes `sqrt(max(1 - x² - y², 0))`). ### Changes - **`sixng-core/src/texture.rs`**: Reconstruct `B = sqrt(max(0, 1 - x² - y²))` after `decode_bc5` in `to_rgba`. R and G (DirectX green) are left untouched so `shader.wgsl` and `render3d_metal4.rs` keep working consistently, and DDS output is unchanged. Added unit test `bc5_reconstructs_normal_z`. - **`sixng-core/src/ttpf.rs`**: Invalidate cached BC5 PNGs whose stored `mean_rgba[2] < 1.0` so previously exported caches pick up the reconstructed Z channel and `normal_like` flag. - **`sixng-gui/src/render3d_metal4.rs`**: Reconstruct `sampled.z = sqrt(max(1.0 - sampled.x * sampled.x - sampled.y * sampled.y, 0.0))` to match `shader.wgsl`. ### Verification - `cargo test -p sixng-core --no-default-features`: 139 passed, 0 failed. - `cargo check --no-default-features`: clean across all workspace crates (`sixng-core`, `sixng-cli`, `sixng-gui`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 20:07:22 +00:00
github manually merged commit 8bcac40b60 into gh-pr/69/base 2026-11-26 20:07:23 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.