From a3f3d3c2e9d1e3d98059a51035d8acfd0262127d Mon Sep 17 00:00:00 2001 From: Yuki Shimada Date: Wed, 4 Dec 2024 23:03:08 +0900 Subject: [PATCH 1/3] fix: change the behavior when joint is invisible --- .../components/Skeletal/SkeletalComponent.ts | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/foundation/components/Skeletal/SkeletalComponent.ts b/src/foundation/components/Skeletal/SkeletalComponent.ts index c3a0eb725..b7830a6a6 100644 --- a/src/foundation/components/Skeletal/SkeletalComponent.ts +++ b/src/foundation/components/Skeletal/SkeletalComponent.ts @@ -189,23 +189,18 @@ export class SkeletalComponent extends Component { for (let i = 0; i < this.__joints.length; i++) { const joint = this.__joints[i]; - let m; - if (joint.isVisible) { - const globalJointTransform = joint.matrixInner; - - MutableMatrix44.multiplyTypedArrayTo( - globalJointTransform, - this.__inverseBindMatricesAccessor!.getTypedArray(), - SkeletalComponent.__tmp_mat4, - i - ); - if (this._bindShapeMatrix) { - SkeletalComponent.__tmp_mat4.multiply(this._bindShapeMatrix); // only for glTF1 - } - m = SkeletalComponent.__tmp_mat4; - } else { - m = SkeletalComponent.__identityMat; + const globalJointTransform = joint.isVisible ? joint.matrixInner : joint.matrixRestInner; + + MutableMatrix44.multiplyTypedArrayTo( + globalJointTransform, + this.__inverseBindMatricesAccessor!.getTypedArray(), + SkeletalComponent.__tmp_mat4, + i + ); + if (this._bindShapeMatrix) { + SkeletalComponent.__tmp_mat4.multiply(this._bindShapeMatrix); // only for glTF1 } + const m = SkeletalComponent.__tmp_mat4; if (i === 0 && joint.entity.tryToGetAnimation() != null) { this.__worldMatrix.copyComponents(m); From 95d8f2ef4e5cfa7e7e69d8414dc2957581eb4f98 Mon Sep 17 00:00:00 2001 From: Yuki Shimada Date: Wed, 4 Dec 2024 23:19:59 +0900 Subject: [PATCH 2/3] remove an unused method --- src/foundation/importer/ModelConverter.ts | 74 ----------------------- 1 file changed, 74 deletions(-) diff --git a/src/foundation/importer/ModelConverter.ts b/src/foundation/importer/ModelConverter.ts index 344ea29c5..8396ba405 100644 --- a/src/foundation/importer/ModelConverter.ts +++ b/src/foundation/importer/ModelConverter.ts @@ -2137,80 +2137,6 @@ export class ModelConverter { byteAlign: 4, }); } - - private static __generateVrmNormalizedSkeleton(gltfModel: RnM2, rnEntities: ISceneGraphEntity[]) { - // Create a Copy of Skeleton - const backupRnJoints: ISceneGraphEntity[] = []; - const createHierarchyRecursively = (rnm2Node: RnM2Node, rnEntity: ISceneGraphEntity) => { - if (Is.exist(rnm2Node.children)) { - for (const childIdx of rnm2Node.children) { - const rnJoint = backupRnJoints[childIdx]; - if (Is.exist(rnJoint)) { - rnEntity.getSceneGraph().addChild(rnJoint.getSceneGraph()); - createHierarchyRecursively(gltfModel.nodes[childIdx], rnJoint); - } - } - } - }; - - for (const node of gltfModel.nodes) { - if (Is.exist(node.skinObject)) { - const joints = node.skinObject.joints; - for (const jointIdx of joints) { - const rnJointEntity = rnEntities[jointIdx]; - const newRnJointEntity = createGroupEntity(); - newRnJointEntity.getTransform().localMatrix = rnJointEntity.getTransform().localMatrix; - backupRnJoints[jointIdx] = newRnJointEntity; - } - } - } - for (const node of gltfModel.nodes) { - if (Is.exist(node.skinObject)) { - const rnJointEntity = backupRnJoints[node.skinObject.joints[0]]; - createHierarchyRecursively(node.skinObject.jointsObjects[0], rnJointEntity); - } - } - - // Normalize Skeleton - for (let i = 0; i < gltfModel.nodes.length; i++) { - const node = gltfModel.nodes[i]; - if (Is.exist(node.skinObject)) { - const joints = node.skinObject.joints; - for (const jointIdx of joints) { - const rnJointEntity = rnEntities[jointIdx]; - rnJointEntity.getTransform().localMatrix = Matrix44.identity(); - } - for (const jointIdx of joints) { - const rnJointEntity = rnEntities[jointIdx]; - let parentInvWorldMatrix = MutableMatrix44.identity(); - if (backupRnJoints[jointIdx].getSceneGraph().parent) { - parentInvWorldMatrix = backupRnJoints[jointIdx].getSceneGraph().parent!.matrix.invert(); - } - rnJointEntity.getTransform().localPosition = parentInvWorldMatrix.multiplyVector3( - backupRnJoints[jointIdx].getSceneGraph().position - ); - } - } - } - - // Update Inverse Bind Matrices from the normalized skeleton - for (let i = 0; i < gltfModel.nodes.length; i++) { - const node = gltfModel.nodes[i]; - if (Is.exist(node.skinObject)) { - const joints = node.skinObject.joints; - const rnSkeletalEntity = rnEntities[i]; - const skeletalComponent = rnSkeletalEntity.tryToGetSkeletal(); - if (Is.exist(skeletalComponent)) { - const accessor = skeletalComponent.getInverseBindMatricesAccessor(); - for (let j = 0; j < joints.length; j++) { - const jointIdx = joints[j]; - const rnJointEntity = rnEntities[jointIdx]; - accessor!.setMat4AsMatrix44(j, rnJointEntity.getSceneGraph().matrix.invert(), {}); - } - } - } - } - } } function setupPbrMetallicRoughness( From 98dfa559432d5a6f48663833e08d57d2915095f7 Mon Sep 17 00:00:00 2001 From: Yuki Shimada Date: Thu, 5 Dec 2024 01:47:15 +0900 Subject: [PATCH 3/3] feat: add parent and parentObject to RnM2Node --- src/foundation/importer/Gltf2Importer.ts | 10 +++++++--- src/types/RnM2.ts | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/foundation/importer/Gltf2Importer.ts b/src/foundation/importer/Gltf2Importer.ts index c1c60062a..ccd3a9cf7 100644 --- a/src/foundation/importer/Gltf2Importer.ts +++ b/src/foundation/importer/Gltf2Importer.ts @@ -269,12 +269,16 @@ export class Gltf2Importer { } static _loadDependenciesOfNodes(gltfJson: RnM2) { - for (const node of gltfJson.nodes) { + for (const node_i in gltfJson.nodes) { + const node = gltfJson.nodes[node_i]; + // Hierarchy node.childrenObjects = node.childrenObjects ?? []; if (node.children) { - for (const i of node.children) { - node.childrenObjects![i] = gltfJson.nodes[i]; + for (const child_i of node.children) { + node.childrenObjects![child_i] = gltfJson.nodes[child_i]; + gltfJson.nodes[child_i].parent = parseInt(node_i); + gltfJson.nodes[child_i].parentObject = node; } } diff --git a/src/types/RnM2.ts b/src/types/RnM2.ts index c460a6384..92562a72f 100644 --- a/src/types/RnM2.ts +++ b/src/types/RnM2.ts @@ -91,6 +91,8 @@ export type RnM2Node = { camera?: number; childrenObjects?: RnM2Node[]; children?: number[]; + parent?: number; + parentObject?: RnM2Node; skinObject?: RnM2Skin; skin?: number; skinName?: string;