Add skeleton nodes names debug drawing in Editor preview when enabled

This commit is contained in:
Wojtek Figat
2023-04-29 12:10:15 +02:00
parent ba319d8499
commit 896f75b789

View File

@@ -15,8 +15,8 @@ namespace FlaxEditor.Viewport.Previews
/// <seealso cref="AssetPreview" /> /// <seealso cref="AssetPreview" />
public class AnimatedModelPreview : AssetPreview public class AnimatedModelPreview : AssetPreview
{ {
private ContextMenuButton _showNodesButton, _showBoundsButton, _showFloorButton; private ContextMenuButton _showNodesButton, _showBoundsButton, _showFloorButton, _showNodesNamesButton;
private bool _showNodes, _showBounds, _showFloor, _showCurrentLOD; private bool _showNodes, _showBounds, _showFloor, _showCurrentLOD, _showNodesNames;
private AnimatedModel _previewModel; private AnimatedModel _previewModel;
private StaticModel _floorModel; private StaticModel _floorModel;
private ContextMenuButton _showCurrentLODButton; private ContextMenuButton _showCurrentLODButton;
@@ -98,6 +98,24 @@ namespace FlaxEditor.Viewport.Previews
} }
} }
/// <summary>
/// Gets or sets a value indicating whether show animated model skeleton nodes names debug view.
/// </summary>
public bool ShowNodesNames
{
get => _showNodesNames;
set
{
if (_showNodesNames == value)
return;
_showNodesNames = value;
if (value)
ShowDebugDraw = true;
if (_showNodesNamesButton != null)
_showNodesNamesButton.Checked = value;
}
}
/// <summary> /// <summary>
/// Gets or sets a value indicating whether show animated model bounding box debug view. /// Gets or sets a value indicating whether show animated model bounding box debug view.
/// </summary> /// </summary>
@@ -183,6 +201,9 @@ namespace FlaxEditor.Viewport.Previews
// Show Skeleton // Show Skeleton
_showNodesButton = ViewWidgetShowMenu.AddButton("Skeleton", () => ShowNodes = !ShowNodes); _showNodesButton = ViewWidgetShowMenu.AddButton("Skeleton", () => ShowNodes = !ShowNodes);
// Show Skeleton Names
_showNodesNamesButton = ViewWidgetShowMenu.AddButton("Skeleton Names", () => ShowNodesNames = !ShowNodesNames);
// Show Floor // Show Floor
_showFloorButton = ViewWidgetShowMenu.AddButton("Floor", button => ShowFloor = !ShowFloor); _showFloorButton = ViewWidgetShowMenu.AddButton("Floor", button => ShowFloor = !ShowFloor);
_showFloorButton.IndexInParent = 1; _showFloorButton.IndexInParent = 1;
@@ -327,37 +348,51 @@ namespace FlaxEditor.Viewport.Previews
base.OnDebugDraw(context, ref renderContext); base.OnDebugDraw(context, ref renderContext);
// Draw skeleton nodes // Draw skeleton nodes
if (_showNodes) if (_showNodes || _showNodesNames)
{ {
_previewModel.GetCurrentPose(out var pose, true); _previewModel.GetCurrentPose(out var pose, true);
var nodes = _previewModel.SkinnedModel?.Nodes; var nodes = _previewModel.SkinnedModel?.Nodes;
if (pose != null && pose.Length != 0 && nodes != null) if (pose != null && pose.Length != 0 && nodes != null)
{ {
// Draw bounding box at the node locations
var nodesMask = NodesMask != null && NodesMask.Length == nodes.Length ? NodesMask : null; var nodesMask = NodesMask != null && NodesMask.Length == nodes.Length ? NodesMask : null;
var localBox = new OrientedBoundingBox(new Vector3(-1.0f), new Vector3(1.0f)); if (_showNodes)
for (int nodeIndex = 0; nodeIndex < pose.Length; nodeIndex++)
{ {
if (nodesMask != null && !nodesMask[nodeIndex]) // Draw bounding box at the node locations
continue; var localBox = new OrientedBoundingBox(new Vector3(-1.0f), new Vector3(1.0f));
var transform = pose[nodeIndex]; for (int nodeIndex = 0; nodeIndex < pose.Length; nodeIndex++)
transform.Decompose(out var scale, out Matrix _, out _);
transform = Matrix.Invert(Matrix.Scaling(scale)) * transform;
var box = localBox * transform;
DebugDraw.DrawWireBox(box, Color.Green, 0, false);
}
// Nodes connections
for (int nodeIndex = 0; nodeIndex < nodes.Length; nodeIndex++)
{
int parentIndex = nodes[nodeIndex].ParentIndex;
if (parentIndex != -1)
{ {
if (nodesMask != null && (!nodesMask[nodeIndex] || !nodesMask[parentIndex])) if (nodesMask != null && !nodesMask[nodeIndex])
continue; continue;
var parentPos = pose[parentIndex].TranslationVector; var transform = pose[nodeIndex];
var bonePos = pose[nodeIndex].TranslationVector; transform.Decompose(out var scale, out Matrix _, out _);
DebugDraw.DrawLine(parentPos, bonePos, Color.Green, 0, false); transform = Matrix.Invert(Matrix.Scaling(scale)) * transform;
var box = localBox * transform;
DebugDraw.DrawWireBox(box, Color.Green, 0, false);
}
// Nodes connections
for (int nodeIndex = 0; nodeIndex < nodes.Length; nodeIndex++)
{
int parentIndex = nodes[nodeIndex].ParentIndex;
if (parentIndex != -1)
{
if (nodesMask != null && (!nodesMask[nodeIndex] || !nodesMask[parentIndex]))
continue;
var parentPos = pose[parentIndex].TranslationVector;
var bonePos = pose[nodeIndex].TranslationVector;
DebugDraw.DrawLine(parentPos, bonePos, Color.Green, 0, false);
}
}
}
if (_showNodesNames)
{
// Nodes names
for (int nodeIndex = 0; nodeIndex < nodes.Length; nodeIndex++)
{
if (nodesMask != null && !nodesMask[nodeIndex])
continue;
//var t = new Transform(pose[nodeIndex].TranslationVector, Quaternion.Identity, new Float3(0.1f));
DebugDraw.DrawText(nodes[nodeIndex].Name, pose[nodeIndex].TranslationVector, Color.White, 20, 0.0f, 0.1f);
} }
} }
} }
@@ -448,6 +483,7 @@ namespace FlaxEditor.Viewport.Previews
_showBoundsButton = null; _showBoundsButton = null;
_showFloorButton = null; _showFloorButton = null;
_showCurrentLODButton = null; _showCurrentLODButton = null;
_showNodesNamesButton = null;
base.OnDestroy(); base.OnDestroy();
} }