major improvements
This commit is contained in:
@@ -484,6 +484,26 @@ bool ProcessMesh(ModelData& result, AssimpImporterData& data, const aiMesh* aMes
|
||||
}
|
||||
}
|
||||
|
||||
AssimpNode* curNode = &data.Nodes[mesh.NodeIndex];
|
||||
Vector3 translation = Vector3::Zero;
|
||||
Vector3 scale = Vector3::One;
|
||||
Quaternion rotation = Quaternion::Identity;
|
||||
|
||||
while(true)
|
||||
{
|
||||
translation += curNode->LocalTransform.Translation;
|
||||
scale *= curNode->LocalTransform.Scale;
|
||||
rotation *= curNode->LocalTransform.Orientation;
|
||||
|
||||
if (curNode->ParentIndex == -1)
|
||||
break;
|
||||
curNode = &data.Nodes[curNode->ParentIndex];
|
||||
}
|
||||
|
||||
mesh.OriginTranslation = translation;
|
||||
mesh.OriginOrientation = rotation;
|
||||
mesh.Scaling = scale;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -624,6 +644,7 @@ bool ImportMesh(int32 index, ModelData& result, AssimpImporterData& data, String
|
||||
|
||||
// Import mesh data
|
||||
MeshData* meshData = New<MeshData>();
|
||||
meshData->NodeIndex = data.MeshIndexToNodeIndex[index][0];
|
||||
if (ProcessMesh(result, data, aMesh, *meshData, errorMsg))
|
||||
{
|
||||
Delete(meshData);
|
||||
@@ -650,59 +671,6 @@ bool ImportMesh(int32 index, ModelData& result, AssimpImporterData& data, String
|
||||
result.LODs[lodIndex].Meshes.Add(meshData);
|
||||
}
|
||||
|
||||
auto root = data.Scene->mRootNode;
|
||||
Array<Transform> points;
|
||||
if (root->mNumChildren == 0)
|
||||
{
|
||||
aiQuaternion aiQuat;
|
||||
aiVector3D aiPos;
|
||||
aiVector3D aiScale;
|
||||
root->mTransformation.Decompose(aiScale, aiQuat, aiPos);
|
||||
auto quat = ToQuaternion(aiQuat);
|
||||
auto pos = ToFloat3(aiPos);
|
||||
auto scale = ToFloat3(aiScale);
|
||||
Transform trans = Transform(pos, quat, scale);
|
||||
points.Add(trans);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (unsigned int j = 0; j < root->mNumChildren; j++)
|
||||
{
|
||||
aiQuaternion aiQuat;
|
||||
aiVector3D aiPos;
|
||||
aiVector3D aiScale;
|
||||
root->mChildren[j]->mTransformation.Decompose(aiScale, aiQuat, aiPos);
|
||||
auto quat = ToQuaternion(aiQuat);
|
||||
auto pos = ToFloat3(aiPos);
|
||||
auto scale = ToFloat3(aiScale);
|
||||
Transform trans = Transform(pos, quat, scale);
|
||||
points.Add(trans);
|
||||
}
|
||||
}
|
||||
|
||||
Float3 translation = Float3::Zero;
|
||||
Float3 scale = Float3::Zero;
|
||||
Quaternion orientation = Quaternion::Identity;
|
||||
for (auto point : points)
|
||||
{
|
||||
translation += point.Translation;
|
||||
scale += point.Scale;
|
||||
orientation *= point.Orientation;
|
||||
}
|
||||
|
||||
if (points.Count() > 0)
|
||||
{
|
||||
meshData->OriginTranslation = translation / (float)points.Count();
|
||||
meshData->OriginOrientation = Quaternion::Invert(orientation);
|
||||
meshData->Scaling = scale / (float)points.Count();
|
||||
}
|
||||
else
|
||||
{
|
||||
meshData->OriginTranslation = translation;
|
||||
meshData->OriginOrientation = Quaternion::Invert(orientation);
|
||||
meshData->Scaling = Float3(1);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1333,13 +1333,26 @@ bool ModelTool::ImportModel(const String& path, ModelData& data, Options& option
|
||||
auto& mesh = *data.LODs[lodIndex].Meshes[meshIndex];
|
||||
auto& node = data.Nodes[mesh.NodeIndex];
|
||||
|
||||
auto currentNode = &data.Nodes[mesh.NodeIndex];
|
||||
|
||||
Vector3 scale = Vector3::One;
|
||||
Quaternion rotation = Quaternion::Identity;
|
||||
while (true)
|
||||
{
|
||||
scale *= currentNode->LocalTransform.Scale;
|
||||
rotation *= currentNode->LocalTransform.Orientation;
|
||||
|
||||
if (currentNode->ParentIndex == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
currentNode = &data.Nodes[currentNode->ParentIndex];
|
||||
}
|
||||
|
||||
// Transform vertices
|
||||
auto transformationMatrix = Matrix::Identity;
|
||||
transformationMatrix.SetScaleVector(node.LocalTransform.Scale);
|
||||
|
||||
Matrix rotation;
|
||||
node.LocalTransform.GetRotation(rotation);
|
||||
transformationMatrix = transformationMatrix * rotation;
|
||||
transformationMatrix.SetScaleVector(scale);
|
||||
transformationMatrix = transformationMatrix * Matrix::RotationQuaternion(rotation);
|
||||
|
||||
if (!transformationMatrix.IsIdentity())
|
||||
mesh.TransformBuffer(transformationMatrix);
|
||||
|
||||
Reference in New Issue
Block a user