Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clone an already-loaded FBX model in three.js and be able to animate it. Adapted from: https://gist.github.com/cdata/f2d7a6ccdec071839bc1954c32595e87
const cloneFbx = (fbx) => {
const clone = fbx.clone(true)
clone.animations = fbx.animations
clone.skeleton = { bones: [] }
const skinnedMeshes = {}
fbx.traverse(node => {
if (node.isSkinnedMesh) {
skinnedMeshes[node.name] = node
}
})
const cloneBones = {}
const cloneSkinnedMeshes = {}
clone.traverse(node => {
if (node.isBone) {
cloneBones[node.name] = node
}
if (node.isSkinnedMesh) {
cloneSkinnedMeshes[node.name] = node
}
})
for (let name in skinnedMeshes) {
const skinnedMesh = skinnedMeshes[name]
const skeleton = skinnedMesh.skeleton
const cloneSkinnedMesh = cloneSkinnedMeshes[name]
const orderedCloneBones = []
for (let i=0; i<skeleton.bones.length; i++) {
const cloneBone = cloneBones[skeleton.bones[i].name]
orderedCloneBones.push(cloneBone)
}
cloneSkinnedMesh.bind(
new THREE.Skeleton(orderedCloneBones, skeleton.boneInverses),
cloneSkinnedMesh.matrixWorld)
// For animation to work correctly:
clone.skeleton.bones.push(cloneSkinnedMesh)
clone.skeleton.bones.push(...orderedCloneBones)
}
return clone
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment