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

View File

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