Merge branch 'center-import' of https://github.com/Tryibion/FlaxEngine into Tryibion-center-import
This commit is contained in:
@@ -648,6 +648,60 @@ bool ImportMesh(int32 i, ImportedModelData& result, AssimpImporterData& data, St
|
||||
result.LODs.Resize(lodIndex + 1);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -836,6 +836,20 @@ bool ProcessMesh(ImportedModelData& result, OpenFbxImporterData& data, const ofb
|
||||
mesh.TransformBuffer(geometryTransform);
|
||||
}*/
|
||||
|
||||
// Get local transform for origin shifting translation
|
||||
auto translation = ToMatrix(aMesh->getGlobalTransform()).GetTranslation();
|
||||
auto scale = data.GlobalSettings.UnitScaleFactor;
|
||||
if (data.GlobalSettings.CoordAxis == ofbx::CoordSystem_RightHanded)
|
||||
mesh.OriginTranslation = scale * Vector3(translation.X, translation.Y, -translation.Z);
|
||||
else
|
||||
mesh.OriginTranslation = scale * Vector3(translation.X, translation.Y, translation.Z);
|
||||
|
||||
auto rot = aMesh->getLocalRotation();
|
||||
auto quat = Quaternion::Euler(-(float)rot.x, -(float)rot.y, -(float)rot.z);
|
||||
mesh.OriginOrientation = quat;
|
||||
|
||||
auto scaling = aMesh->getLocalScaling();
|
||||
mesh.Scaling = Vector3(scale * (float)scaling.x, scale * (float)scaling.y, scale * (float)scaling.z);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -373,6 +373,7 @@ void ModelTool::Options::Serialize(SerializeStream& stream, const void* otherObj
|
||||
SERIALIZE(Scale);
|
||||
SERIALIZE(Rotation);
|
||||
SERIALIZE(Translation);
|
||||
SERIALIZE(UseLocalOrigin);
|
||||
SERIALIZE(CenterGeometry);
|
||||
SERIALIZE(Duration);
|
||||
SERIALIZE(FramesRange);
|
||||
@@ -420,6 +421,7 @@ void ModelTool::Options::Deserialize(DeserializeStream& stream, ISerializeModifi
|
||||
DESERIALIZE(Scale);
|
||||
DESERIALIZE(Rotation);
|
||||
DESERIALIZE(Translation);
|
||||
DESERIALIZE(UseLocalOrigin);
|
||||
DESERIALIZE(CenterGeometry);
|
||||
DESERIALIZE(Duration);
|
||||
DESERIALIZE(FramesRange);
|
||||
@@ -1091,11 +1093,16 @@ bool ModelTool::ImportModel(const String& path, ModelData& meshData, Options& op
|
||||
|
||||
// Prepare import transformation
|
||||
Transform importTransform(options.Translation, options.Rotation, Float3(options.Scale));
|
||||
if (options.UseLocalOrigin && data.LODs.HasItems() && data.LODs[0].Meshes.HasItems())
|
||||
{
|
||||
importTransform.Translation -= importTransform.Orientation * data.LODs[0].Meshes[0]->OriginTranslation * importTransform.Scale;
|
||||
}
|
||||
if (options.CenterGeometry && data.LODs.HasItems() && data.LODs[0].Meshes.HasItems())
|
||||
{
|
||||
// Calculate the bounding box (use LOD0 as a reference)
|
||||
BoundingBox box = data.LODs[0].GetBox();
|
||||
importTransform.Translation -= box.GetCenter();
|
||||
auto center = data.LODs[0].Meshes[0]->OriginOrientation * importTransform.Orientation * box.GetCenter() * importTransform.Scale * data.LODs[0].Meshes[0]->Scaling;
|
||||
importTransform.Translation -= center;
|
||||
}
|
||||
const bool applyImportTransform = !importTransform.IsIdentity();
|
||||
|
||||
|
||||
@@ -282,8 +282,11 @@ public:
|
||||
// Custom import geometry offset.
|
||||
API_FIELD(Attributes="EditorOrder(520), EditorDisplay(\"Transform\")")
|
||||
Float3 Translation = Float3::Zero;
|
||||
// If checked, the imported geometry will be shifted to the center of mass.
|
||||
// If checked, the imported geometry will be shifted to its local transform origin.
|
||||
API_FIELD(Attributes="EditorOrder(530), EditorDisplay(\"Transform\")")
|
||||
bool UseLocalOrigin = false;
|
||||
// If checked, the imported geometry will be shifted to the center of mass.
|
||||
API_FIELD(Attributes="EditorOrder(540), EditorDisplay(\"Transform\")")
|
||||
bool CenterGeometry = false;
|
||||
|
||||
public: // Animation
|
||||
|
||||
Reference in New Issue
Block a user