Skip to content

Preserve RGBA transparency in add_mesh_trimesh GLB export - #783

Open
lu123bing wants to merge 1 commit into
viser-project:mainfrom
lu123bing:fix/trimesh-rgba-transparency-525
Open

lu123bing wants to merge 1 commit into
viser-project:mainfrom
lu123bing:fix/trimesh-rgba-transparency-525

Conversation

@lu123bing

Copy link
Copy Markdown

Fixes #525.

Motivation

SceneApi.add_mesh_trimesh() silently drops transparency: a trimesh mesh with RGBA vertex/face colors (alpha < 255) renders fully opaque in the viewer. Maintainer diagnosis on the issue (root cause: trimesh's GLB exporter emits no materials entry for ColorVisuals, so the three.js loader falls back to an opaque standard material; upstream mikedh/trimesh#2436 still open) plus a multi-step manual workaround. This PR applies that workaround automatically, inside viser, so the repro from #525 just works.

Changes

  • src/viser/_scene_api.py: new _trimesh_with_transparency() helper, wired into both add_mesh_trimesh() and add_batched_meshes_trimesh() (same export path, same bug). When semi-transparent colors are present it exports a copy converted to texture visuals with a PBR BLEND material; the caller's mesh is never mutated. Fully opaque meshes return as-is, keeping the export byte-identical. Graceful fallbacks (legacy opaque path) when the trimesh version lacks the PBR API or Pillow is missing for texture baking (with a warning pointing at Pillow).
  • tests/test_trimesh_compatibility.py: 3 regression tests — wire-level test asserting the queued GLB payload carries alphaMode: BLEND plus caller non-mutation; opaque mesh keeps the material-free export; unit test for the vertex-color path and opaque identity.

Verification

  • Reproduced at GLB level pre-fix (no materials in export); post-fix the texture PNG carries the exact alpha (76) and material is BLEND.
  • tests/test_trimesh_compatibility.py + tests/test_strict_copy_on_add.py: 6 passed; test_docs_coverage.py + test_scene_tree.py: 10 passed.
  • ruff check, ruff format --check, pyright: all clean (0 errors).

ColorVisuals meshes export without a glTF material, so three.js renders
them fully opaque. Convert semi-transparent meshes to texture visuals
with a PBR BLEND material on a copy; opaque meshes keep the legacy path.
Fixes viser-project#525.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add_mesh_trimesh ignores transparency from trimesh RGBA colors

1 participant