-
-
Save wandergis/450bc0615ac1802cc61e217924ac93f8 to your computer and use it in GitHub Desktop.
A quick hack to clone a Three.js GLTF scene without re-loading or re-parsing the source.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cloneGltf = (gltf) => { | |
const clone = { | |
animations: gltf.animations, | |
scene: gltf.scene.clone(true) | |
}; | |
const skinnedMeshes = {}; | |
gltf.scene.traverse(node => { | |
if (node.isSkinnedMesh) { | |
skinnedMeshes[node.name] = node; | |
} | |
}); | |
const cloneBones = {}; | |
const cloneSkinnedMeshes = {}; | |
clone.scene.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 Skeleton(orderedCloneBones, skeleton.boneInverses), | |
cloneSkinnedMesh.matrixWorld); | |
} | |
return clone; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment