|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Normalize Mixamo's Xbot FBX into the single-skeleton GLB used at runtime. |
| 4 | + * |
| 5 | + * Mixamo exports Beta_Surface and Beta_Joints with separate copies of the same |
| 6 | + * skeleton. Posecode needs one unambiguous bone tree, so the runtime character |
| 7 | + * uses the complete Beta_Surface mesh/skeleton. The decorative joint shell is |
| 8 | + * deliberately omitted; it can return later as a rigid overlay after the core |
| 9 | + * retarget path is stable. |
| 10 | + * |
| 11 | + * Usage: node scripts/prepare-xbot.mjs <input.fbx> [output.glb] |
| 12 | + */ |
| 13 | +import fs from "node:fs"; |
| 14 | +import path from "node:path"; |
| 15 | +import * as THREE from "three"; |
| 16 | +import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader.js"; |
| 17 | +import { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter.js"; |
| 18 | +import { mergeVertices } from "three/examples/jsm/utils/BufferGeometryUtils.js"; |
| 19 | + |
| 20 | +globalThis.ProgressEvent ??= class ProgressEvent {}; |
| 21 | +globalThis.FileReader ??= class FileReader { |
| 22 | + readAsArrayBuffer(blob) { |
| 23 | + blob.arrayBuffer().then((result) => { |
| 24 | + this.result = result; |
| 25 | + this.onloadend?.(); |
| 26 | + }, (error) => this.onerror?.(error)); |
| 27 | + } |
| 28 | + |
| 29 | + readAsDataURL(blob) { |
| 30 | + blob.arrayBuffer().then((result) => { |
| 31 | + this.result = `data:${blob.type};base64,${Buffer.from(result).toString("base64")}`; |
| 32 | + this.onloadend?.(); |
| 33 | + }, (error) => this.onerror?.(error)); |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +const input = process.argv[2]; |
| 38 | +const output = process.argv[3] ?? "playground/public/models/xbot.glb"; |
| 39 | +if (!input) throw new Error("usage: node scripts/prepare-xbot.mjs <input.fbx> [output.glb]"); |
| 40 | + |
| 41 | +const bytes = fs.readFileSync(input); |
| 42 | +const scene = new FBXLoader().parse( |
| 43 | + bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength), |
| 44 | + `${path.dirname(input)}/`, |
| 45 | +); |
| 46 | + |
| 47 | +let surface = null; |
| 48 | +let joints = null; |
| 49 | +const remove = []; |
| 50 | +scene.traverse((node) => { |
| 51 | + if (node.isSkinnedMesh && node.name === "Beta_Surface") surface = node; |
| 52 | + if (node.isSkinnedMesh && node.name === "Beta_Joints") joints = node; |
| 53 | + // The second skeleton is nested under the primary Hips in Mixamo's Xbot. |
| 54 | + if (node.isBone && /Hips$/.test(node.name) && node.parent?.isBone) remove.push(node); |
| 55 | +}); |
| 56 | +if (!surface) throw new Error("Xbot normalization: Beta_Surface was not found"); |
| 57 | +if (!joints) throw new Error("Xbot normalization: Beta_Joints was not found"); |
| 58 | +for (const node of remove) node.removeFromParent(); |
| 59 | + |
| 60 | +const boneNames = new Set(surface.skeleton.bones.map((bone) => bone.name)); |
| 61 | +if (boneNames.size !== surface.skeleton.bones.length) { |
| 62 | + throw new Error("Xbot normalization: primary skeleton still has duplicate bone names"); |
| 63 | +} |
| 64 | + |
| 65 | +// Re-index the decorative joint shell onto the primary skeleton. Both meshes |
| 66 | +// use the same Mixamo names but export separate bone objects (and Beta_Joints |
| 67 | +// omits one terminal bone), so indices must be translated by name first. |
| 68 | +const primaryIndex = new Map(surface.skeleton.bones.map((bone, index) => [bone.name, index])); |
| 69 | +const jointToPrimary = joints.skeleton.bones.map((bone) => { |
| 70 | + const index = primaryIndex.get(bone.name); |
| 71 | + if (index === undefined) throw new Error(`Xbot normalization: unmapped joint bone ${bone.name}`); |
| 72 | + return index; |
| 73 | +}); |
| 74 | +const jointSkinIndex = joints.geometry.getAttribute("skinIndex"); |
| 75 | +for (let vertex = 0; vertex < jointSkinIndex.count; vertex++) { |
| 76 | + for (let lane = 0; lane < jointSkinIndex.itemSize; lane++) { |
| 77 | + const source = jointSkinIndex.getComponent(vertex, lane); |
| 78 | + jointSkinIndex.setComponent(vertex, lane, jointToPrimary[source]); |
| 79 | + } |
| 80 | +} |
| 81 | +jointSkinIndex.needsUpdate = true; |
| 82 | + |
| 83 | +// Bake Mixamo centimeters into Posecode meters. Scaling the wrapper around a |
| 84 | +// live skin makes Xbot's bone matrices participate in that scale as well; bake |
| 85 | +// it into vertices + joint translations and rebuild inverse binds instead. |
| 86 | +scene.updateMatrixWorld(true); |
| 87 | +const bounds = new THREE.Box3().setFromObject(surface); |
| 88 | +const unitScale = 1.75 / bounds.getSize(new THREE.Vector3()).y; |
| 89 | +surface.geometry.scale(unitScale, unitScale, unitScale); |
| 90 | +joints.geometry.scale(unitScale, unitScale, unitScale); |
| 91 | +surface.geometry = mergeVertices(surface.geometry, 1e-4); |
| 92 | +joints.geometry = mergeVertices(joints.geometry, 1e-4); |
| 93 | +for (const bone of surface.skeleton.bones) bone.position.multiplyScalar(unitScale); |
| 94 | +scene.updateMatrixWorld(true); |
| 95 | +surface.bind(surface.skeleton); |
| 96 | +surface.normalizeSkinWeights(); |
| 97 | +joints.bind(surface.skeleton); |
| 98 | +joints.normalizeSkinWeights(); |
| 99 | + |
| 100 | +// FBX legacy materials import almost black on Posecode's dark stage. Produce a |
| 101 | +// stable runtime PBR material rather than depending on converter heuristics. |
| 102 | +const oldMaterials = Array.isArray(surface.material) ? surface.material : [surface.material]; |
| 103 | +for (const material of oldMaterials) material?.dispose(); |
| 104 | +surface.material = new THREE.MeshStandardMaterial({ |
| 105 | + color: 0xb9c0cc, |
| 106 | + metalness: 0.08, |
| 107 | + roughness: 0.68, |
| 108 | +}); |
| 109 | +const oldJointMaterials = Array.isArray(joints.material) ? joints.material : [joints.material]; |
| 110 | +for (const material of oldJointMaterials) material?.dispose(); |
| 111 | +joints.material = new THREE.MeshStandardMaterial({ |
| 112 | + color: 0x48515f, |
| 113 | + metalness: 0.12, |
| 114 | + roughness: 0.62, |
| 115 | +}); |
| 116 | + |
| 117 | +const glb = await new GLTFExporter().parseAsync(scene, { |
| 118 | + animations: [], |
| 119 | + binary: true, |
| 120 | + onlyVisible: true, |
| 121 | +}); |
| 122 | +fs.mkdirSync(path.dirname(output), { recursive: true }); |
| 123 | +fs.writeFileSync(output, Buffer.from(glb)); |
| 124 | +console.log(`Prepared ${output}: ${surface.skeleton.bones.length} bones, ${surface.geometry.attributes.position.count + joints.geometry.attributes.position.count} vertices, scale ${unitScale.toFixed(6)}`); |
0 commit comments