Fix AnimatedModel bounds calculations

This commit is contained in:
Wojtek Figat
2023-03-03 17:44:34 +01:00
parent 987e680908
commit 31411e334b
2 changed files with 14 additions and 28 deletions

View File

@@ -25,7 +25,7 @@ AnimatedModel::AnimatedModel(const SpawnParams& params)
{
_drawCategory = SceneRendering::SceneDrawAsync;
GraphInstance.Object = this;
_box = _boxLocal = BoundingBox(Vector3::Zero);
_box = BoundingBox(Vector3::Zero);
_sphere = BoundingSphere(Vector3::Zero, 0.0f);
SkinnedModel.Changed.Bind<AnimatedModel, &AnimatedModel::OnSkinnedModelChanged>(this);
@@ -511,50 +511,41 @@ void AnimatedModel::OnActiveInTreeChanged()
ModelInstanceActor::OnActiveInTreeChanged();
}
void AnimatedModel::UpdateLocalBounds()
void AnimatedModel::UpdateBounds()
{
BoundingBox box;
if (CustomBounds.GetSize().LengthSquared() > 0.01f)
{
box = CustomBounds;
BoundingBox::Transform(CustomBounds, _transform, _box);
}
else if (SkinnedModel && SkinnedModel->IsLoaded())
{
//box = SkinnedModel->GetBox(GraphInstance.RootTransform.GetWorld());
//box = SkinnedModel->GetBox();
if (GraphInstance.NodesPose.Count() != 0)
{
// Per-bone bounds estimated from positions
auto& skeleton = SkinnedModel->Skeleton;
const int32 bonesCount = skeleton.Bones.Count();
box = BoundingBox(GraphInstance.NodesPose[skeleton.Bones[0].NodeIndex].GetTranslation());
#define GET_NODE_POS(i) _transform.LocalToWorld(GraphInstance.NodesPose[skeleton.Bones[i].NodeIndex].GetTranslation())
BoundingBox box(GET_NODE_POS(0));
for (int32 boneIndex = 1; boneIndex < bonesCount; boneIndex++)
box.Merge(GraphInstance.NodesPose[skeleton.Bones[boneIndex].NodeIndex].GetTranslation());
box.Merge(GET_NODE_POS(boneIndex));
_box = box;
#undef GET_NODE_POS
}
else
{
box = SkinnedModel->GetBox();
_box = SkinnedModel->GetBox(_transform.GetWorld());
}
// Apply margin based on model dimensions
const Vector3 modelBoxSize = SkinnedModel->GetBox().GetSize();
const Vector3 center = box.GetCenter();
const Vector3 sizeHalf = Vector3::Max(box.GetSize() + modelBoxSize * 0.2f, modelBoxSize) * 0.5f;
box = BoundingBox(center - sizeHalf, center + sizeHalf);
const Vector3 center = _box.GetCenter();
const Vector3 sizeHalf = Vector3::Max(_box.GetSize() + modelBoxSize * 0.2f, modelBoxSize) * 0.5f;
_box = BoundingBox(center - sizeHalf, center + sizeHalf);
}
else
{
box = BoundingBox(Vector3::Zero);
_box = BoundingBox(_transform.Translation);
}
_boxLocal = BoundingBox::MakeScaled(box, BoundsScale);
}
void AnimatedModel::UpdateBounds()
{
UpdateLocalBounds();
BoundingBox::Transform(_boxLocal, _transform, _box);
BoundingSphere::FromBox(_box, _sphere);
if (_sceneRenderingKey != -1)
GetSceneRendering()->UpdateActor(this, _sceneRenderingKey);
@@ -963,10 +954,7 @@ void AnimatedModel::OnTransformChanged()
// Base
ModelInstanceActor::OnTransformChanged();
BoundingBox::Transform(_boxLocal, _transform, _box);
BoundingSphere::FromBox(_box, _sphere);
if (_sceneRenderingKey != -1)
GetSceneRendering()->UpdateActor(this, _sceneRenderingKey);
UpdateBounds();
}
void AnimatedModel::WaitForModelLoad()

View File

@@ -55,7 +55,6 @@ public:
};
private:
BoundingBox _boxLocal;
GeometryDrawStateData _drawState;
SkinnedMeshDrawData _skinningData;
AnimationUpdateMode _actualMode;
@@ -349,7 +348,6 @@ private:
void SyncParameters();
void Update();
void UpdateLocalBounds();
void UpdateBounds();
void UpdateSockets();
void OnAnimationUpdated_Async();