From cccd91e0dc2c552922c0083293532530ee56428d Mon Sep 17 00:00:00 2001 From: 06wj <06wj@163.com> Date: Fri, 3 Jul 2020 17:36:51 +0800 Subject: [PATCH] fix: skinnedMesh bone position is wrong under special circumstances --- src/core/Skeleton.js | 2 +- src/loader/GLTFParser.js | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/core/Skeleton.js b/src/core/Skeleton.js index 2c4cb1ae..7c54b8d9 100644 --- a/src/core/Skeleton.js +++ b/src/core/Skeleton.js @@ -37,7 +37,7 @@ const Skeleton = Class.create(/** @lends Skeleton.prototype */ { this.jointNodeList = []; /** * @default [] - * @type {Array} + * @type {String[]} */ this.jointNames = []; /** diff --git a/src/loader/GLTFParser.js b/src/loader/GLTFParser.js index 61f25b89..468d29ec 100644 --- a/src/loader/GLTFParser.js +++ b/src/loader/GLTFParser.js @@ -1161,7 +1161,7 @@ const GLTFParser = Class.create(/** @lends GLTFParser.prototype */{ const nodes = scene.nodes; this.parseSkins(); nodes.forEach(node => this.parseNode(node, this.node)); - this.node.resetSkinedMeshRootNode(); + this.resetSkinInfo(this.node); const model = { node: this.node, @@ -1220,6 +1220,25 @@ const GLTFParser = Class.create(/** @lends GLTFParser.prototype */{ return skeleton; }); } + }, + /** + * 重设 jointName,使其保持唯一性。 + * @private + * @param {Node} rootNode + */ + resetSkinInfo(rootNode) { + const jointNameMap = {}; + rootNode.traverse((node) => { + const newJointName = `${node.id}_${node.name}`; + jointNameMap[node.jointName] = newJointName; + node.jointName = newJointName; + }); + + this.skins.forEach((skin) => { + skin.jointNames = skin.jointNames.map(jointName => jointNameMap[jointName]); + }); + + rootNode.resetSkinedMeshRootNode(); } });