Skip to content

Commit

Permalink
trying to merge 2 skeletons, doesn't fix anything
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Aug 13, 2023
1 parent 51516cd commit 7d698ea
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 125 deletions.
8 changes: 7 additions & 1 deletion FModel/Views/Snooper/Animations/Bone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@ public class Bone
public readonly int Index;
public readonly int ParentIndex;
public readonly Transform Rest;
public readonly bool IsVirtual;

public string LoweredParentName;
public List<string> LoweredChildNames;

public int SkeletonIndex = -1;
public readonly List<int> AnimatedBySequences;

public Bone(int i, int p, Transform t)
public Bone(int i, int p, Transform t, bool isVirtual = false)
{
Index = i;
ParentIndex = p;
Rest = t;
IsVirtual = isVirtual;

LoweredChildNames = new List<string>();
AnimatedBySequences = new List<int>();
}

public bool IsRoot => Index == 0 && ParentIndex == -1 && string.IsNullOrEmpty(LoweredParentName);
public bool IsDaron => LoweredChildNames.Count > 0;
public bool IsMapped => SkeletonIndex > -1;
public bool IsAnimated => AnimatedBySequences.Count > 0;
public bool IsNative => Index == SkeletonIndex;
Expand Down
137 changes: 97 additions & 40 deletions FModel/Views/Snooper/Animations/Skeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CUE4Parse.UE4.Assets.Exports.Animation;
using FModel.Views.Snooper.Buffers;
using FModel.Views.Snooper.Shading;
using ImGuiNET;
using OpenTK.Graphics.OpenGL4;
using Serilog;

Expand All @@ -16,13 +17,18 @@ public class Skeleton : IDisposable
private int _handle;
private BufferObject<Matrix4x4> _rest;
private BufferObject<Matrix4x4> _ssbo;
private Matrix4x4[] _boneMatriceAtFrame;

public string Name;
public bool IsAnimated { get; private set; }
public readonly string RootBoneName;
public readonly Dictionary<string, Bone> BonesByLoweredName;

public readonly int BoneCount;
public readonly Dictionary<string, Bone> BonesByLoweredName;
private Matrix4x4[] _boneMatriceAtFrame;
public int AdditionalBoneCount;
public int TotalBoneCount => BoneCount + AdditionalBoneCount;

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

public Skeleton()
{
Expand All @@ -35,23 +41,75 @@ public Skeleton(FReferenceSkeleton referenceSkeleton) : this()
for (int boneIndex = 0; boneIndex < BoneCount; boneIndex++)
{
var info = referenceSkeleton.FinalRefBoneInfo[boneIndex];
var boneTransform = new Transform
var boneName = info.Name.Text.ToLower();
var bone = new Bone(boneIndex, info.ParentIndex, new Transform
{
Rotation = referenceSkeleton.FinalRefBonePose[boneIndex].Rotation,
Position = referenceSkeleton.FinalRefBonePose[boneIndex].Translation * Constants.SCALE_DOWN_RATIO,
Scale = referenceSkeleton.FinalRefBonePose[boneIndex].Scale3D
};
});

var bone = new Bone(boneIndex, info.ParentIndex, boneTransform);
if (!bone.IsRoot)
{
bone.LoweredParentName =
referenceSkeleton.FinalRefBoneInfo[bone.ParentIndex].Name.Text.ToLower();
bone.Rest.Relation = BonesByLoweredName[bone.LoweredParentName].Rest.Matrix;
bone.LoweredParentName = referenceSkeleton.FinalRefBoneInfo[bone.ParentIndex].Name.Text.ToLower();
var parentBone = BonesByLoweredName[bone.LoweredParentName];

bone.Rest.Relation = parentBone.Rest.Matrix;
parentBone.LoweredChildNames.Add(boneName);
}

BonesByLoweredName[info.Name.Text.ToLower()] = bone;
if (boneIndex == 0) RootBoneName = boneName;
BonesByLoweredName[boneName] = bone;
}
_boneMatriceAtFrame = new Matrix4x4[BoneCount];
}

public void Merge(FReferenceSkeleton referenceSkeleton)
{
for (int boneIndex = 0; boneIndex < referenceSkeleton.FinalRefBoneInfo.Length; boneIndex++)
{
var info = referenceSkeleton.FinalRefBoneInfo[boneIndex];
var boneName = info.Name.Text.ToLower();

if (!BonesByLoweredName.TryGetValue(boneName, out var bone))
{
bone = new Bone(BoneCount + AdditionalBoneCount, info.ParentIndex, new Transform
{
Rotation = referenceSkeleton.FinalRefBonePose[boneIndex].Rotation,
Position = referenceSkeleton.FinalRefBonePose[boneIndex].Translation * Constants.SCALE_DOWN_RATIO,
Scale = referenceSkeleton.FinalRefBonePose[boneIndex].Scale3D
}, true);

if (!bone.IsRoot)
{
bone.LoweredParentName = referenceSkeleton.FinalRefBoneInfo[bone.ParentIndex].Name.Text.ToLower();
var parentBone = BonesByLoweredName[bone.LoweredParentName];

bone.Rest.Relation = parentBone.Rest.Matrix;
parentBone.LoweredChildNames.Add(boneName);
}

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

public void Setup()
{
_handle = GL.CreateProgram();

_rest = new BufferObject<Matrix4x4>(BoneCount, BufferTarget.ShaderStorageBuffer);
foreach (var bone in BonesByLoweredName.Values)
{
if (bone.IsVirtual) break;
_rest.Update(bone.Index, bone.Rest.Matrix);
}
_rest.Unbind();

_ssbo = new BufferObject<Matrix4x4>(TotalBoneCount, BufferTarget.ShaderStorageBuffer);
_ssbo.UpdateRange(Matrix4x4.Identity);
}

public void Animate(CAnimSet animation)
Expand Down Expand Up @@ -101,28 +159,7 @@ public void ResetAnimatedData(bool full = false)

if (!full) return;
IsAnimated = false;
_ssbo.UpdateRange(BoneCount, Matrix4x4.Identity);
}

public void Setup()
{
_handle = GL.CreateProgram();

_rest = new BufferObject<Matrix4x4>(BoneCount, BufferTarget.ShaderStorageBuffer);
foreach (var bone in BonesByLoweredName.Values)
{
_rest.Update(bone.Index, bone.Rest.Matrix);
}
_rest.Unbind();

_ssbo = new BufferObject<Matrix4x4>(BoneCount, BufferTarget.ShaderStorageBuffer);
_ssbo.UpdateRange(BoneCount, Matrix4x4.Identity);

_boneMatriceAtFrame = new Matrix4x4[BoneCount];
for (int boneIndex = 0; boneIndex < _boneMatriceAtFrame.Length; boneIndex++)
{
_boneMatriceAtFrame[boneIndex] = Matrix4x4.Identity;
}
_ssbo.UpdateRange(Matrix4x4.Identity);
}

public void UpdateAnimationMatrices(Animation animation, bool rotationOnly)
Expand Down Expand Up @@ -191,14 +228,7 @@ void Get(Bone b)
return (s, f);
}

public Matrix4x4 GetBoneMatrix(Bone bone)
{
_ssbo.Bind();
var anim = _ssbo.Get(bone.Index);
_ssbo.Unbind();

return IsAnimated ? anim : bone.Rest.Matrix;
}
public Matrix4x4 GetBoneMatrix(Bone bone) => IsAnimated ? _boneMatriceAtFrame[bone.Index] : bone.Rest.Matrix;

public void Render(Shader shader)
{
Expand All @@ -208,6 +238,33 @@ public void Render(Shader shader)
_rest.BindBufferBase(2);
}

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

private void DrawBoneTree(string boneName, Bone bone)
{
var flags = ImGuiTreeNodeFlags.OpenOnArrow | ImGuiTreeNodeFlags.OpenOnDoubleClick | ImGuiTreeNodeFlags.SpanAvailWidth;
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);
var open = ImGui.TreeNodeEx(boneName, flags);
if (ImGui.IsItemClicked() && !ImGui.IsItemToggledOpen())
SelectedBone = boneName;

if (open)
{
foreach (var name in bone.LoweredChildNames)
{
DrawBoneTree(name, BonesByLoweredName[name]);
}
ImGui.TreePop();
}
}

public void Dispose()
{
BonesByLoweredName.Clear();
Expand Down
9 changes: 7 additions & 2 deletions FModel/Views/Snooper/Buffers/BufferObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class BufferObject<TDataType> : IDisposable where TDataType : unmanaged
private readonly int _sizeOf;
private readonly BufferTarget _bufferTarget;

public readonly int Size;

private unsafe BufferObject(BufferTarget bufferTarget)
{
_bufferTarget = bufferTarget;
Expand All @@ -20,14 +22,17 @@ private unsafe BufferObject(BufferTarget bufferTarget)

public BufferObject(TDataType[] data, BufferTarget bufferTarget) : this(bufferTarget)
{
GL.BufferData(bufferTarget, data.Length * _sizeOf, data, BufferUsageHint.StaticDraw);
Size = data.Length;
GL.BufferData(bufferTarget, Size * _sizeOf, data, BufferUsageHint.StaticDraw);
}

public BufferObject(int length, BufferTarget bufferTarget) : this(bufferTarget)
{
GL.BufferData(bufferTarget, length * _sizeOf, IntPtr.Zero, BufferUsageHint.DynamicDraw);
Size = length;
GL.BufferData(bufferTarget, Size * _sizeOf, IntPtr.Zero, BufferUsageHint.DynamicDraw);
}

public void UpdateRange(TDataType data) => UpdateRange(Size, data);
public void UpdateRange(int count, TDataType data)
{
Bind();
Expand Down
4 changes: 4 additions & 0 deletions FModel/Views/Snooper/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public Model(USkeletalMesh export, CSkeletalMesh skeletalMesh, Transform transfo
if (HasSkeleton && export.Skeleton.TryLoad(out USkeleton skeleton))
{
Skeleton.Name = skeleton.Name;
// Skeleton.Merge(skeleton.ReferenceSkeleton);
sockets.AddRange(skeleton.Sockets);
}

Expand Down Expand Up @@ -255,6 +256,8 @@ public void UpdateMatrices(Options options)
var worldMatrix = UpdateMatrices();
foreach (var socket in Sockets)
{
if (!socket.IsDaron) continue;

var boneMatrix = Matrix4x4.Identity;
if (HasSkeleton && Skeleton.BonesByLoweredName.TryGetValue(socket.BoneName.Text.ToLower(), out var bone))
boneMatrix = Skeleton.GetBoneMatrix(bone);
Expand Down Expand Up @@ -415,6 +418,7 @@ public void Render(Shader shader, bool outline = false)

GL.DrawElementsInstanced(PrimitiveType.Triangles, section.FacesCount, DrawElementsType.UnsignedInt, section.FirstFaceIndexPtr, TransformsCount);
}
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
_vao.Unbind();

if (IsSelected)
Expand Down
1 change: 1 addition & 0 deletions FModel/Views/Snooper/Models/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Socket : IDisposable
public readonly bool IsVirtual;

public readonly List<SocketAttachementInfo> AttachedModels;
public bool IsDaron => AttachedModels.Count > 0;

private Socket()
{
Expand Down
2 changes: 1 addition & 1 deletion FModel/Views/Snooper/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private void Animate(UObject anim, FGuid guid)
if (notifyClass.TryGetValue(out FVector scale, "Scale"))
t.Scale = scale;

var s = new Socket($"TL_{addedModel.Name}", socketName, t, true);
var s = new Socket($"ANIM_{addedModel.Name}", socketName, t, true);
model.Sockets.Add(s);
addedModel.AttachModel(model, s, new SocketAttachementInfo { Guid = guid, Instance = addedModel.SelectedInstance });
}
Expand Down
Loading

0 comments on commit 7d698ea

Please sign in to comment.