Fix prefab preview to better match model bounds inside it

This commit is contained in:
2026-04-20 12:48:52 +02:00
parent 7014f40965
commit e90dde491d
+34 -6
View File
@@ -127,16 +127,34 @@ namespace FlaxEditor.Content
return asset.IsLoaded;
}
private void Prepare(Actor actor)
private void Prepare(Actor actor, ref BoundingBox bounds)
{
if (!actor.IsActive)
return;
if (actor is TextRender textRender)
{
textRender.UpdateLayout();
}
// Use bounds from visual-only elements
var actorBounds = BoundingBox.Empty;
if (actor is ModelInstanceActor ||
actor is SpriteRender ||
actor is TextRender)
{
actorBounds = actor.EditorBox;
}
if (actorBounds != BoundingBox.Empty)
{
if (bounds == BoundingBox.Empty)
bounds = actorBounds;
else
BoundingBox.Merge(ref actorBounds, ref bounds, out bounds);
}
for (int i = 0; i < actor.ChildrenCount; i++)
{
Prepare(actor.GetChild(i));
Prepare(actor.GetChild(i), ref bounds);
}
}
@@ -164,15 +182,25 @@ namespace FlaxEditor.Content
else
{
// Update some actors data (some actor types update bounds/data later but its required to be done before rendering)
Prepare(_preview.Instance);
var bounds = BoundingBox.Empty;
Prepare(_preview.Instance, ref bounds);
//bounds = _preview.Instance.EditorBoxChildren;
// Auto fit actor to camera
float targetSize = 30.0f;
var bounds = _preview.Instance.EditorBoxChildren;
if (bounds != BoundingBox.Empty)
{
float targetSize = 38.0f;
var maxSize = Math.Max(0.001f, (float)bounds.Size.MaxValue);
_preview.Instance.Scale = new Float3(targetSize / maxSize);
float scale = targetSize / maxSize;
_preview.Instance.Scale = new Float3(scale);
_preview.Instance.Position = -bounds.Center * scale;
}
else
{
_preview.Instance.Scale = Float3.One;
_preview.Instance.Position = Vector3.Zero;
}
}
_preview.Task.OnDraw();
}