24

A quick hack to clone a Three.js GLTF scene without re-loading or re-parsing the...

 2 years ago
source link: https://gist.github.com/cdata/f2d7a6ccdec071839bc1954c32595e87
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A quick hack to clone a Three.js GLTF scene without re-loading or re-parsing the source. · GitHub

Instantly share code, notes, and snippets.

A quick hack to clone a Three.js GLTF scene without re-loading or re-parsing the source.

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; }


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK