Skip to content

Commit

Permalink
Fix issue with glTF skin node/mesh metadata (#12684)
Browse files Browse the repository at this point in the history
  • Loading branch information
bghgary authored Jun 28, 2022
1 parent 710c922 commit 09baca3
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion packages/dev/loaders/src/glTF/2.0/glTFLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ export class ArrayItem {
}
}

// https://stackoverflow.com/a/48218209
function mergeDeep(...objects: any[]): any {
const isObject = (obj: any) => obj && typeof obj === "object";

return objects.reduce((prev, obj) => {
Object.keys(obj).forEach((key) => {
const pVal = prev[key];
const oVal = obj[key];

if (Array.isArray(pVal) && Array.isArray(oVal)) {
prev[key] = pVal.concat(...oVal);
} else if (isObject(pVal) && isObject(oVal)) {
prev[key] = mergeDeep(pVal, oVal);
} else {
prev[key] = oVal;
}
});

return prev;
}, {});
}

/**
* The glTF 2.0 loader
*/
Expand Down Expand Up @@ -798,6 +820,9 @@ export class GLTFLoader implements IGLTFLoader {
this._loadMeshAsync(`/meshes/${mesh.index}`, node, mesh, (babylonTransformNode) => {
const babylonTransformNodeForSkin = node._babylonTransformNodeForSkin!;

// Merge the metadata from the skin node to the skinned mesh in case a loader extension added metadata.
babylonTransformNode.metadata = mergeDeep(babylonTransformNodeForSkin.metadata, babylonTransformNode.metadata);

const skin = ArrayItem.Get(`${context}/skin`, this._gltf.skins, node.skin);
promises.push(
this._loadSkinAsync(`/skins/${skin.index}`, node, skin, (babylonSkeleton) => {
Expand All @@ -812,7 +837,7 @@ export class GLTFLoader implements IGLTFLoader {
// Handle special case when the parent of the skeleton root is the skinned mesh.
const parentNode = ArrayItem.Get(`/skins/${skin.index}/skeleton`, this._gltf.nodes, skin.skeleton).parent!;
if (node.index === parentNode.index) {
babylonTransformNode.parent = node._babylonTransformNodeForSkin!.parent;
babylonTransformNode.parent = babylonTransformNodeForSkin.parent;
} else {
babylonTransformNode.parent = parentNode._babylonTransformNode!;
}
Expand Down

0 comments on commit 09baca3

Please sign in to comment.