Viewer: normal maps in a tangent frame built from the UV set they are sampled with #120

Manually merged
github merged 2 commits from gh-pr/120/head into gh-pr/120/base 2026-11-26 20:11:19 +00:00
Owner

@⁠holdmysocks on GitHub · 2026-11-24 04:52 UTC · original
holdmysocks:claude/tangent-frame → master · +89 −3 · 5 files · 2 commits · merged 2026-11-24 05:01 UTC

Supersedes #112. Reported in #112 by DarkBlade909.

Cause

The tangents stored in the models follow the second UV set, not the first one the viewer samples normal maps with. Measured per triangle (stored tangent against dP/du, cross(n, t) * w against dP/dv, area weighted):

model agrees with UV0 agrees with UV1
rhino_hi 49 % / 52 % 100 % / 100 %
player_story000_01_pantsjeans_mr1_002_regular_mw_long_full 99.4 % / 93.5 % 99.9 % / 99.9 %

So on models whose two layouts differ the frame was unrelated to the normal map, and the tn.y = -tn.y flip was right on some surfaces and wrong on others. That is why the jeans looked right without the flip and the rhino's bolts looked right with it.

Change

  • glb.rs: the loader builds the frame from UV0 for every mesh with tangents: tangent along +u, sign so that cross(n, t) * w runs along +v (down the image). Mirrored halves get the opposite sign from their UV winding. Vertices no triangle with UV area touches keep the stored frame.
  • shader.wgsl and the Metal shader: the green flip is removed; green is used as stored (DirectX style, green = +v).

Verified

  • rhino_hi at --tod 12: side armour bolts lit from above on both sides of the tank (mirrored halves); on master they were lit from the side.
  • The jeans: folds render as in the reporter's "without the flip" images. The map convention was checked against the garment's own textures: the divergence of the normal map's XY with green = +v correlates +0.42 with the packed map's curvature channel, about 0 with green = -v.
  • New unit test for an upright and a mirrored quad. Workspace tests pass.

Not verified

  • The Metal shader edit (one deleted line) was not run.
  • Only the model viewer was rendered, not the 3D map.
  • Before / after images are with the session that opened this PR (not attached: no image upload from the command line).
**@⁠holdmysocks** on GitHub · 2026-11-24 04:52 UTC · [original](https://github.com/holdmysocks/SixNG/pull/120) `holdmysocks:claude/tangent-frame` → `master` · +89 −3 · 5 files · 2 commits · merged 2026-11-24 05:01 UTC Supersedes #112. Reported in #112 by DarkBlade909. ## Cause The tangents stored in the models follow the **second** UV set, not the first one the viewer samples normal maps with. Measured per triangle (stored tangent against dP/du, `cross(n, t) * w` against dP/dv, area weighted): | model | agrees with UV0 | agrees with UV1 | |---|---|---| | `rhino_hi` | 49 % / 52 % | 100 % / 100 % | | `player_story000_01_pantsjeans_mr1_002_regular_mw_long_full` | 99.4 % / 93.5 % | 99.9 % / 99.9 % | So on models whose two layouts differ the frame was unrelated to the normal map, and the `tn.y = -tn.y` flip was right on some surfaces and wrong on others. That is why the jeans looked right without the flip and the rhino's bolts looked right with it. ## Change - `glb.rs`: the loader builds the frame from UV0 for every mesh with tangents: tangent along +u, sign so that `cross(n, t) * w` runs along +v (down the image). Mirrored halves get the opposite sign from their UV winding. Vertices no triangle with UV area touches keep the stored frame. - `shader.wgsl` and the Metal shader: the green flip is removed; green is used as stored (DirectX style, green = +v). ## Verified - `rhino_hi` at `--tod 12`: side armour bolts lit from above on both sides of the tank (mirrored halves); on master they were lit from the side. - The jeans: folds render as in the reporter's "without the flip" images. The map convention was checked against the garment's own textures: the divergence of the normal map's XY with green = +v correlates +0.42 with the packed map's curvature channel, about 0 with green = -v. - New unit test for an upright and a mirrored quad. Workspace tests pass. ## Not verified - The Metal shader edit (one deleted line) was not run. - Only the model viewer was rendered, not the 3D map. - Before / after images are with the session that opened this PR (not attached: no image upload from the command line).
The tangents stored in the models follow the second UV set (rhino_hi:
100 % agreement with UV1, 49 % with UV0), so normal maps sampled on UV0
were lit from arbitrary sides wherever the two layouts differ, and the
green flip in the shaders was right on some models and wrong on others.
The loader now builds the frame from UV0 (tangent +u, bitangent +v) and
both shaders use the map's green as stored (DirectX style).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 20:11:17 +00:00
github manually merged commit 6329e42beb into gh-pr/120/base 2026-11-26 20:11:19 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.