Skip to content

Commit

Permalink
Merge pull request #1495 from actnwit/feat/animation
Browse files Browse the repository at this point in the history
feat: animation
  • Loading branch information
emadurandal authored Dec 5, 2024
2 parents 47e8219 + 98dfa55 commit 70f5a40
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 93 deletions.
27 changes: 11 additions & 16 deletions src/foundation/components/Skeletal/SkeletalComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/foundation/importer/Gltf2Importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
74 changes: 0 additions & 74 deletions src/foundation/importer/ModelConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions src/types/RnM2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export type RnM2Node = {
camera?: number;
childrenObjects?: RnM2Node[];
children?: number[];
parent?: number;
parentObject?: RnM2Node;
skinObject?: RnM2Skin;
skin?: number;
skinName?: string;
Expand Down

0 comments on commit 70f5a40

Please sign in to comment.