improved bone export

This commit is contained in:
Perfare 2019-07-29 13:41:42 +08:00
parent e62b6c3d77
commit de54257eef
2 changed files with 94 additions and 60 deletions

View File

@ -333,34 +333,32 @@ namespace AssetStudio
hasBones = false; hasBones = false;
} }
FbxArray<FbxNode*>* pBoneNodeList = nullptr;
FbxArray<FbxCluster*>* pClusterArray = nullptr; FbxArray<FbxCluster*>* pClusterArray = nullptr;
try try
{ {
if (hasBones) if (hasBones)
{ {
pBoneNodeList = new FbxArray<FbxNode*>(); pClusterArray = new FbxArray<FbxCluster*>(boneList->Count);
pBoneNodeList->Reserve(boneList->Count);
for (int i = 0; i < boneList->Count; i++) for (int i = 0; i < boneList->Count; i++)
{ {
auto bone = boneList[i]; auto bone = boneList[i];
auto frame = imported->RootFrame->FindFrameByPath(bone->Path); if (bone->Path != nullptr)
auto frameNode = (FbxNode*)frameToNode[frame];
pBoneNodeList->Add(frameNode);
}
pClusterArray = new FbxArray<FbxCluster*>();
pClusterArray->Reserve(boneList->Count);
for (int i = 0; i < boneList->Count; i++)
{ {
FbxNode* pNode = pBoneNodeList->GetAt(i); auto frame = imported->RootFrame->FindFrameByPath(bone->Path);
FbxString lClusterName = pNode->GetNameOnly() + FbxString("Cluster"); auto boneNode = (FbxNode*)frameToNode[frame];
FbxString lClusterName = boneNode->GetNameOnly() + FbxString("Cluster");
FbxCluster* pCluster = FbxCluster::Create(pScene, lClusterName.Buffer()); FbxCluster* pCluster = FbxCluster::Create(pScene, lClusterName.Buffer());
pCluster->SetLink(pNode); pCluster->SetLink(boneNode);
pCluster->SetLinkMode(FbxCluster::eTotalOne); pCluster->SetLinkMode(FbxCluster::eTotalOne);
pClusterArray->Add(pCluster); pClusterArray->Add(pCluster);
} }
else
{
pClusterArray->Add(NULL);
}
}
} }
FbxMesh* pMesh = FbxMesh::Create(pScene, pFrameNode->GetName()); FbxMesh* pMesh = FbxMesh::Create(pScene, pFrameNode->GetName());
@ -532,11 +530,14 @@ namespace AssetStudio
if (boneIndices[k] < boneList->Count && weights4[k] > 0) if (boneIndices[k] < boneList->Count && weights4[k] > 0)
{ {
FbxCluster* pCluster = pClusterArray->GetAt(boneIndices[k]); FbxCluster* pCluster = pClusterArray->GetAt(boneIndices[k]);
if (pCluster)
{
pCluster->AddControlPointIndex(j + firstVertex, weights4[k]); pCluster->AddControlPointIndex(j + firstVertex, weights4[k]);
} }
} }
} }
} }
}
for (int j = 0; j < faceList->Count; j++) for (int j = 0; j < faceList->Count; j++)
{ {
@ -558,6 +559,8 @@ namespace AssetStudio
for (int j = 0; j < boneList->Count; j++) for (int j = 0; j < boneList->Count; j++)
{ {
FbxCluster* pCluster = pClusterArray->GetAt(j); FbxCluster* pCluster = pClusterArray->GetAt(j);
if (pCluster)
{
auto boneMatrix = boneList[j]->Matrix; auto boneMatrix = boneList[j]->Matrix;
FbxAMatrix lBoneMatrix; FbxAMatrix lBoneMatrix;
for (int m = 0; m < 4; m++) for (int m = 0; m < 4; m++)
@ -573,6 +576,7 @@ namespace AssetStudio
pSkin->AddCluster(pCluster); pSkin->AddCluster(pCluster);
} }
}
if (pSkin->GetClusterCount() > 0) if (pSkin->GetClusterCount() > 0)
{ {
@ -582,10 +586,6 @@ namespace AssetStudio
} }
finally finally
{ {
if (pBoneNodeList != NULL)
{
delete pBoneNodeList;
}
if (pClusterArray != NULL) if (pClusterArray != NULL)
{ {
delete pClusterArray; delete pClusterArray;

View File

@ -392,46 +392,80 @@ namespace AssetStudio
if (meshR is SkinnedMeshRenderer sMesh) if (meshR is SkinnedMeshRenderer sMesh)
{ {
//Bone //Bone
/*
* 0 - None
* 1 - m_Bones
* 2 - m_BoneNameHashes
*/
var boneType = 0;
if (sMesh.m_Bones.Length > 0) if (sMesh.m_Bones.Length > 0)
{ {
var boneMax = Math.Min(sMesh.m_Bones.Length, mesh.m_BindPose.Length); if (sMesh.m_Bones.Length == mesh.m_BindPose.Length)
iMesh.BoneList = new List<ImportedBone>(boneMax); {
for (int i = 0; i < boneMax; i++) var verifiedBoneCount = sMesh.m_Bones.Count(x => x.TryGet(out _));
if (verifiedBoneCount > 0)
{
boneType = 1;
}
if (verifiedBoneCount != sMesh.m_Bones.Length)
{
//尝试使用m_BoneNameHashes 4.3 and up
if (mesh.m_BindPose.Length > 0 && (mesh.m_BindPose.Length == mesh.m_BoneNameHashes?.Length))
{
//有效bone数量是否大于SkinnedMeshRenderer
var verifiedBoneCount2 = mesh.m_BoneNameHashes.Count(x => FixBonePath(GetPathFromHash(x)) != null);
if (verifiedBoneCount2 > verifiedBoneCount)
{
boneType = 2;
}
}
}
}
else
{
//Logger.Error("");
}
}
else
{
//尝试使用m_BoneNameHashes 4.3 and up
if (mesh.m_BindPose.Length > 0 && (mesh.m_BindPose.Length == mesh.m_BoneNameHashes?.Length))
{
boneType = 2;
}
}
if (boneType == 1)
{
var boneCount = sMesh.m_Bones.Length;
iMesh.BoneList = new List<ImportedBone>(boneCount);
for (int i = 0; i < boneCount; i++)
{ {
var bone = new ImportedBone(); var bone = new ImportedBone();
if (sMesh.m_Bones[i].TryGet(out var m_Transform)) if (sMesh.m_Bones[i].TryGet(out var m_Transform))
{ {
bone.Path = GetTransformPath(m_Transform); bone.Path = GetTransformPath(m_Transform);
} }
if (!string.IsNullOrEmpty(bone.Path))
{
var convert = Matrix4x4.Scale(new Vector3(-1, 1, 1)); var convert = Matrix4x4.Scale(new Vector3(-1, 1, 1));
bone.Matrix = convert * mesh.m_BindPose[i] * convert; bone.Matrix = convert * mesh.m_BindPose[i] * convert;
iMesh.BoneList.Add(bone); iMesh.BoneList.Add(bone);
} }
} }
} else if (boneType == 2)
if (iMesh.BoneList == null || iMesh.BoneList.Count == 0)
{ {
if (mesh.m_BindPose.Length > 0 && mesh.m_BoneNameHashes?.Length > 0) var boneCount = mesh.m_BindPose.Length;
{ iMesh.BoneList = new List<ImportedBone>(boneCount);
var boneMax = Math.Min(mesh.m_BindPose.Length, mesh.m_BoneNameHashes.Length); for (int i = 0; i < boneCount; i++)
iMesh.BoneList = new List<ImportedBone>(boneMax);
for (int i = 0; i < boneMax; i++)
{ {
var bone = new ImportedBone(); var bone = new ImportedBone();
var boneHash = mesh.m_BoneNameHashes[i]; var boneHash = mesh.m_BoneNameHashes[i];
var path = GetPathFromHash(boneHash); var path = GetPathFromHash(boneHash);
bone.Path = FixBonePath(path); bone.Path = FixBonePath(path);
if (!string.IsNullOrEmpty(bone.Path))
{
var convert = Matrix4x4.Scale(new Vector3(-1, 1, 1)); var convert = Matrix4x4.Scale(new Vector3(-1, 1, 1));
bone.Matrix = convert * mesh.m_BindPose[i] * convert; bone.Matrix = convert * mesh.m_BindPose[i] * convert;
iMesh.BoneList.Add(bone); iMesh.BoneList.Add(bone);
} }
} }
}
}
//Morphs //Morphs
if (mesh.m_Shapes?.channels?.Length > 0) if (mesh.m_Shapes?.channels?.Length > 0)
@ -446,7 +480,7 @@ namespace AssetStudio
morph.Channels.Add(channel); morph.Channels.Add(channel);
var shapeChannel = mesh.m_Shapes.channels[i]; var shapeChannel = mesh.m_Shapes.channels[i];
morphChannelNames.Add(shapeChannel.nameHash, shapeChannel.name); morphChannelNames[shapeChannel.nameHash] = shapeChannel.name;
channel.Name = shapeChannel.name; channel.Name = shapeChannel.name;
channel.KeyframeList = new List<ImportedMorphKeyframe>(shapeChannel.frameCount); channel.KeyframeList = new List<ImportedMorphKeyframe>(shapeChannel.frameCount);