From 08a832f721902279a123c979911d99fc36637021 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 13 Mar 2023 23:58:35 +0800 Subject: [PATCH 1/2] fix: glTF SkinnedMeshRenderer default local bounds --- packages/core/src/mesh/SkinnedMeshRenderer.ts | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/packages/core/src/mesh/SkinnedMeshRenderer.ts b/packages/core/src/mesh/SkinnedMeshRenderer.ts index 4a9d175959..3457107747 100644 --- a/packages/core/src/mesh/SkinnedMeshRenderer.ts +++ b/packages/core/src/mesh/SkinnedMeshRenderer.ts @@ -276,17 +276,14 @@ export class SkinnedMeshRenderer extends MeshRenderer { } else { // Root bone is not in joints list, we can only compute approximate inverse bind matrix // Average all root bone's children inverse bind matrix - let subRootBoneCount = 0; const approximateBindMatrix = new Matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - const inverseBindMatrices = skin.inverseBindMatrices; - const rootBoneChildren = rootBone.children; - for (let i = 0; i < jointCount; i++) { - const index = jointEntities.indexOf(rootBoneChildren[i]); - if (index !== -1) { - Matrix.add(approximateBindMatrix, inverseBindMatrices[index], approximateBindMatrix); - subRootBoneCount++; - } - } + let subRootBoneCount = this._computeApproximateBindMatrix( + jointEntities, + skin.inverseBindMatrices, + rootBone, + approximateBindMatrix + ); + if (subRootBoneCount !== 0) { Matrix.multiplyScalar(approximateBindMatrix, 1.0 / subRootBoneCount, approximateBindMatrix); BoundingBox.transform(this._mesh.bounds, approximateBindMatrix, this._localBounds); @@ -304,6 +301,33 @@ export class SkinnedMeshRenderer extends MeshRenderer { } } + private _computeApproximateBindMatrix( + jointEntities: Entity[], + inverseBindMatrices: Matrix[], + rootEntity: Entity, + approximateBindMatrix: Matrix + ): number { + let subRootBoneCount = 0; + const children = rootEntity.children; + for (let i = 0, n = children.length; i < n; i++) { + const child = children[i]; + const index = jointEntities.indexOf(child); + if (index !== -1) { + Matrix.add(approximateBindMatrix, inverseBindMatrices[index], approximateBindMatrix); + subRootBoneCount++; + } else { + subRootBoneCount += this._computeApproximateBindMatrix( + jointEntities, + inverseBindMatrices, + child, + approximateBindMatrix + ); + } + } + + return subRootBoneCount; + } + private _findByEntityName(rootEntity: Entity, name: string): Entity { if (!rootEntity) { return null; From ea8f68d6e54f6200483b6fc16a292078dbd1b7f2 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Tue, 14 Mar 2023 00:01:16 +0800 Subject: [PATCH 2/2] refactor: opt code --- packages/core/src/mesh/SkinnedMeshRenderer.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/mesh/SkinnedMeshRenderer.ts b/packages/core/src/mesh/SkinnedMeshRenderer.ts index 3457107747..bffd7598ce 100644 --- a/packages/core/src/mesh/SkinnedMeshRenderer.ts +++ b/packages/core/src/mesh/SkinnedMeshRenderer.ts @@ -310,8 +310,8 @@ export class SkinnedMeshRenderer extends MeshRenderer { let subRootBoneCount = 0; const children = rootEntity.children; for (let i = 0, n = children.length; i < n; i++) { - const child = children[i]; - const index = jointEntities.indexOf(child); + const rootChild = children[i]; + const index = jointEntities.indexOf(rootChild); if (index !== -1) { Matrix.add(approximateBindMatrix, inverseBindMatrices[index], approximateBindMatrix); subRootBoneCount++; @@ -319,7 +319,7 @@ export class SkinnedMeshRenderer extends MeshRenderer { subRootBoneCount += this._computeApproximateBindMatrix( jointEntities, inverseBindMatrices, - child, + rootChild, approximateBindMatrix ); }