Skip to content

Commit

Permalink
mock-up bone hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Aug 16, 2023
1 parent 7d698ea commit 1aa45b1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
1 change: 1 addition & 0 deletions FModel/Views/Snooper/Animations/Bone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public Bone(int i, int p, Transform t, bool isVirtual = false)
public bool IsMapped => SkeletonIndex > -1;
public bool IsAnimated => AnimatedBySequences.Count > 0;
public bool IsNative => Index == SkeletonIndex;
public uint Color => !IsMapped || !IsAnimated ? 0xFFA0A0A0 : 0xFF48B048;

public override string ToString() => $"Mesh Ref '{Index}' is Skel Ref '{SkeletonIndex}'";
}
58 changes: 47 additions & 11 deletions FModel/Views/Snooper/Animations/Skeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ public class Skeleton : IDisposable
private BufferObject<Matrix4x4> _rest;
private BufferObject<Matrix4x4> _ssbo;
private Matrix4x4[] _boneMatriceAtFrame;
private readonly List<string> _breadcrumb;

public string Name;
public readonly string RootBoneName;
public readonly Dictionary<string, Bone> BonesByLoweredName;

public readonly int BoneCount;
public int AdditionalBoneCount;
public int TotalBoneCount => BoneCount + AdditionalBoneCount;
private int _additionalBoneCount;
private int TotalBoneCount => BoneCount + _additionalBoneCount;

public bool IsAnimated { get; private set; }
public string SelectedBone;

public Skeleton()
{
BonesByLoweredName = new Dictionary<string, Bone>();
_breadcrumb = new List<string>();
}

public Skeleton(FReferenceSkeleton referenceSkeleton) : this()
Expand All @@ -58,7 +59,7 @@ public Skeleton(FReferenceSkeleton referenceSkeleton) : this()
parentBone.LoweredChildNames.Add(boneName);
}

if (boneIndex == 0) RootBoneName = boneName;
if (boneIndex == 0) SelectedBone = boneName;
BonesByLoweredName[boneName] = bone;
}
_boneMatriceAtFrame = new Matrix4x4[BoneCount];
Expand All @@ -73,7 +74,7 @@ public void Merge(FReferenceSkeleton referenceSkeleton)

if (!BonesByLoweredName.TryGetValue(boneName, out var bone))
{
bone = new Bone(BoneCount + AdditionalBoneCount, info.ParentIndex, new Transform
bone = new Bone(BoneCount + _additionalBoneCount, info.ParentIndex, new Transform
{
Rotation = referenceSkeleton.FinalRefBonePose[boneIndex].Rotation,
Position = referenceSkeleton.FinalRefBonePose[boneIndex].Translation * Constants.SCALE_DOWN_RATIO,
Expand All @@ -90,10 +91,10 @@ public void Merge(FReferenceSkeleton referenceSkeleton)
}

BonesByLoweredName[boneName] = bone;
AdditionalBoneCount++;
_additionalBoneCount++;
}
}
_boneMatriceAtFrame = new Matrix4x4[BoneCount + AdditionalBoneCount];
_boneMatriceAtFrame = new Matrix4x4[TotalBoneCount];
}

public void Setup()
Expand Down Expand Up @@ -135,7 +136,7 @@ public void Animate(CAnimSet animation)
{
bone.AnimatedBySequences.Add(s);
}
sequence.Retarget(animation.Skeleton);
sequence.RetargetTracks(animation.Skeleton);
}

#if DEBUG
Expand Down Expand Up @@ -238,22 +239,56 @@ public void Render(Shader shader)
_rest.BindBufferBase(2);
}

public void ImGuiBoneBreadcrumb()
{
for (int i = _breadcrumb.Count - 1; i >= 0; i--)
{
var boneName = _breadcrumb[i];
ImGui.SameLine();
var clicked = ImGui.SmallButton(boneName);
ImGui.SameLine();
ImGui.Text(">");

if (clicked)
{
SelectedBone = boneName;
_breadcrumb.RemoveRange(0, i + 1);
break;
}
}
}

public void ImGuiBoneHierarchy()
{
DrawBoneTree(RootBoneName, BonesByLoweredName[RootBoneName]);
DrawBoneTree(SelectedBone, BonesByLoweredName[SelectedBone]);
}

private void DrawBoneTree(string boneName, Bone bone)
{
var flags = ImGuiTreeNodeFlags.OpenOnArrow | ImGuiTreeNodeFlags.OpenOnDoubleClick | ImGuiTreeNodeFlags.SpanAvailWidth;
ImGui.PushID(bone.Index);
ImGui.TableNextRow();
ImGui.TableNextColumn();

var flags = ImGuiTreeNodeFlags.OpenOnArrow | ImGuiTreeNodeFlags.SpanFullWidth;
if (boneName == SelectedBone) flags |= ImGuiTreeNodeFlags.Selected;
if (bone.IsVirtual) flags |= ImGuiTreeNodeFlags.Leaf;
else if (!bone.IsDaron) flags |= ImGuiTreeNodeFlags.Bullet;

ImGui.SetNextItemOpen(bone.LoweredChildNames.Count <= 1, ImGuiCond.Appearing);
ImGui.SetNextItemOpen(bone.LoweredChildNames.Count <= 1 || flags.HasFlag(ImGuiTreeNodeFlags.Selected), ImGuiCond.Appearing);
var open = ImGui.TreeNodeEx(boneName, flags);
if (ImGui.IsItemClicked() && !ImGui.IsItemToggledOpen())
{
SelectedBone = boneName;
_breadcrumb.Clear();
do
{
_breadcrumb.Add(boneName);
boneName = BonesByLoweredName[boneName].LoweredParentName;
} while (boneName != null);
}

ImGui.TableNextColumn();
ImGui.TextColored(ImGui.ColorConvertU32ToFloat4(bone.Color), bone.SkeletonIndex.ToString());

if (open)
{
Expand All @@ -263,6 +298,7 @@ private void DrawBoneTree(string boneName, Bone bone)
}
ImGui.TreePop();
}
ImGui.PopID();
}

public void Dispose()
Expand Down
17 changes: 13 additions & 4 deletions FModel/Views/Snooper/SnimGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ public void Render(Snooper s)
Draw3DViewport(s);
DrawNavbar();

DrawModals(s);

if (_ti_open) DrawTextureInspector(s);
if (_bh_open) DrawBoneHierarchy(s);

DrawModals(s);

Controller.Render();
}

Expand Down Expand Up @@ -722,11 +723,19 @@ private void DrawTextureInspector(Snooper s)

private void DrawBoneHierarchy(Snooper s)
{
ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, Vector2.Zero);
if (ImGui.Begin("Bone Hierarchy", ref _bh_open, ImGuiWindowFlags.NoScrollbar) && s.Renderer.Options.TryGetModel(out var model))
{
model.Skeleton.ImGuiBoneHierarchy();
model.Skeleton.ImGuiBoneBreadcrumb();
if (ImGui.BeginTable("bone_hierarchy", 2, ImGuiTableFlags.NoSavedSettings | ImGuiTableFlags.RowBg, ImGui.GetContentRegionAvail(), ImGui.GetWindowWidth()))
{
ImGui.TableSetupColumn("Bone", ImGuiTableColumnFlags.WidthStretch);
ImGui.TableSetupColumn("", ImGuiTableColumnFlags.NoHeaderWidth | ImGuiTableColumnFlags.WidthFixed, _tableWidth);
model.Skeleton.ImGuiBoneHierarchy();
ImGui.EndTable();
}
}
ImGui.End(); // if window is collapsed
ImGui.PopStyleVar();
}

private void Draw3DViewport(Snooper s)
Expand Down

0 comments on commit 1aa45b1

Please sign in to comment.