Skip to content

feat: add VRM export to avatar preview#179

Closed
decentraland-bot wants to merge 10 commits into
masterfrom
feat/vrm-export
Closed

feat: add VRM export to avatar preview#179
decentraland-bot wants to merge 10 commits into
masterfrom
feat/vrm-export

Conversation

@decentraland-bot

Copy link
Copy Markdown
Contributor

Summary

  • Adds src/lib/babylon/export.ts: exports the composited Babylon.js scene as a VRM 0.x file by using @babylonjs/serializers GLTF2Export, then post-processing the GLB binary to inject the VRM extension with the humanoid bone mapping (DCL Avatar_* → VRM humanoid spec)
  • Extends the render() return value with an export controller exposing exportVRM(), accessible both in-page and via the existing postMessage controller API
  • Adds a "↓ VRM" download button to the avatar preview UI (visible only in AVATAR preview type after loading)
  • Adds @babylonjs/serializers ^4.2.2 dependency (matches existing Babylon version)

How it works

  1. GLTF2Export.GLBAsync(scene) exports the composited scene (skipping social-emote duplicates and the centering helper mesh)
  2. The GLB binary is parsed, the VRM extension JSON is injected with humanoid.humanBones entries built by matching GLTF node names against the canonical DCL→VRM bone mapping (sourced from unity-renderer's VRMBonesMapping.asset)
  3. The patched GLB is repacked and served as a Blob download

Test plan

  • Load an avatar preview in AVATAR mode → "↓ VRM" button appears after loading
  • Click button → avatar.vrm downloads
  • Open the .vrm in VRoid Hub, VSeeFace, or UniVRM inspector → humanoid bones are mapped correctly
  • Wearable-only preview (non-AVATAR type) → button does not appear
  • postMessage export.exportVRM() via controller API returns an object URL

Requested by Ignacio Mazzara via Slack

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@vercel

vercel Bot commented May 20, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
wearable-preview Ready Ready Preview, Comment May 20, 2026 7:51pm

Request Review

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When wearables cover body parts, the base meshes are hidden via
setEnabled(false). The Babylon GLTF exporter does not skip disabled
meshes by default, so hidden body parts were being exported alongside
wearable meshes causing visual artifacts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The previous isEnabled() check was too aggressive — it filtered out
TransformNodes (skeleton bones) in addition to meshes, and checked
the full ancestor chain. Now uses instanceof AbstractMesh to only
filter actual mesh geometry, and isEnabled(false) to check only the
node's own state without traversing ancestors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ones

Each wearable container loads its own skeleton hierarchy. When exported
to GLTF, wearable meshes reference their own duplicate bone nodes
instead of the primary skeleton that the VRM humanoid mapping points to.
This causes wearable meshes to appear at wrong positions or be invisible
in VRM viewers.

Remap all GLTF skin joints to reference the first occurrence of each
bone name, unifying all meshes under a single skeleton.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The Babylon GLTF exporter strips __root__ coordinate-conversion nodes
and disables right-handed conversion for their descendants (bones).
But center() reparents meshes under a different parent, so meshes still
get coordinate-converted while bones don't. This puts them in different
coordinate spaces, producing a 180-degree Y rotation on mesh geometry.

Apply a compensating 180-degree Y rotation to the parent node in the
GLTF JSON to re-align meshes with the bone coordinate space.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…rdinate space

The GLTF exporter strips __root__ nodes and disables coordinate conversion
for their descendants. Since center() moves meshes out from under __root__
to a separate parent node, meshes got coordinate-converted but bones did
not — causing the upside-down orientation. Reparenting parent under __root__
before export ensures uniform treatment of all nodes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…tively

The setParent(__root__) approach caused double Z-inversion: __root__ has
scale (1,1,-1) for GLTF→Babylon conversion, and reparenting parent under
it gave parent a compensating (1,1,-1) local scale. Combined with the
serializer's own Z inversion, wearable meshes ended up in the wrong
coordinate space. Fix: don't touch the hierarchy at all — just reset
parent's full transform (including rotation) to identity and let the
serializer handle handedness conversion on its own.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The GLTF exporter strips __root__ nodes (which have the loader's
coordinate conversion scale) but computes IBMs against Babylon world
transforms that include __root__'s Scale(-1,1,1). The exported GLTF
joint transforms don't include this scale (since __root__ is stripped),
so at bind pose skinMatrix = Scale(-1,1,1) instead of identity —
causing inverted/deformed joints.

Fix: after mergeSkeletons remaps joint indices, recompute IBMs as
inverse(worldTransform(joint)) using the GLTF node hierarchy directly.
This ensures skinMatrix = identity at bind pose, matching what
unity-renderer's MeshFreezer.NormalizeSkinnedMesh does.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The Babylon GLTF exporter applies right-handed coordinate conversion
(Z-negation) to vertex data for meshes under 'parent' (not under
__root__), but the vertex data was already in right-handed GLTF space.
This caused the model to appear upside-down and deform during animation
due to vertex/bone coordinate space mismatch.

- undoVertexConversion: negates Z back in POSITION, NORMAL, TANGENT
  accessor data for skinned meshes
- resetSkinnedMeshTransforms: removes compensating Scale(-1,1,1) from
  skinned mesh nodes (ignored per GLTF spec but safest as identity)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

2 participants