Skip to content

Commit

Permalink
keep bones in rest pos if there's no track for them
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Mar 4, 2023
1 parent 3f6e46e commit a5cbc5b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 38 deletions.
2 changes: 1 addition & 1 deletion FModel/FModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
<PackageReference Include="NVorbis" Version="0.10.5" />
<PackageReference Include="Oodle.NET" Version="1.0.1" />
<PackageReference Include="Ookii.Dialogs.Wpf" Version="5.0.1" />
<PackageReference Include="OpenTK" Version="4.7.5" />
<PackageReference Include="OpenTK" Version="4.7.7" />
<PackageReference Include="RestSharp" Version="108.0.3" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
Expand Down
13 changes: 9 additions & 4 deletions FModel/ViewModels/CUE4ParseViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,24 @@ public Snooper SnooperViewer
{
get
{
if (_snooper != null) return _snooper;

return Application.Current.Dispatcher.Invoke(delegate
{
return _snooper ??= new Snooper(
new GameWindowSettings { RenderFrequency = Snooper.GetMaxRefreshFrequency() },
var scale = ImGuiController.GetDpiScale();
var htz = Snooper.GetMaxRefreshFrequency();
return _snooper = new Snooper(
new GameWindowSettings { RenderFrequency = htz, UpdateFrequency = htz },
new NativeWindowSettings
{
Size = new OpenTK.Mathematics.Vector2i(
Convert.ToInt32(SystemParameters.MaximizedPrimaryScreenWidth * .75 * ImGuiController.GetDpiScale()),
Convert.ToInt32(SystemParameters.MaximizedPrimaryScreenHeight * .85 * ImGuiController.GetDpiScale())),
Convert.ToInt32(SystemParameters.MaximizedPrimaryScreenWidth * .75 * scale),
Convert.ToInt32(SystemParameters.MaximizedPrimaryScreenHeight * .85 * scale)),
NumberOfSamples = Constants.SAMPLES_COUNT,
WindowBorder = WindowBorder.Resizable,
Flags = ContextFlags.ForwardCompatible,
Profile = ContextProfile.Core,
Vsync = VSyncMode.Adaptive,
APIVersion = new Version(4, 6),
StartVisible = false,
StartFocused = false,
Expand Down
4 changes: 0 additions & 4 deletions FModel/Views/Snooper/Animations/Animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ private void Popup(Snooper s, Save saver, int i)
}
}
ImGui.EndMenu();
}
if (ImGui.MenuItem("Additive", false))
{
}
if (ImGui.MenuItem("Save"))
{
Expand Down
13 changes: 9 additions & 4 deletions FModel/Views/Snooper/Animations/BoneIndice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ public class BoneIndice
public string LoweredParentBoneName;
public bool IsRoot => BoneIndex == 0 && ParentBoneIndex == -1 && string.IsNullOrEmpty(LoweredParentBoneName);

public int TrackIndex = -1;
public int ParentTrackIndex = -1; // bone index of the first tracked parent bone
public bool HasTrack => TrackIndex > -1;
public bool HasParentTrack => ParentTrackIndex > -1;
public int TrackedBoneIndex = -1;
public int TrackedParentBoneIndex = -1; // bone index of the first tracked parent bone
public bool IsTracked => TrackedBoneIndex > -1;
public bool IsParentTracked => TrackedParentBoneIndex > -1;

public bool IsNative => BoneIndex == TrackedBoneIndex;
public bool IsParentNative => ParentBoneIndex == TrackedParentBoneIndex; // always true?

public override string ToString() => $"Mesh Ref '{BoneIndex}' is Skel Ref '{TrackedBoneIndex}' ({IsNative}, {IsParentNative})";
}
46 changes: 23 additions & 23 deletions FModel/Views/Snooper/Animations/Skeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,36 @@ public void Animate(CAnimSet anim, bool rotationOnly)
var originalTransform = BonesTransformByIndex[boneIndices.BoneIndex];
_animatedBonesTransform[s][boneIndices.BoneIndex] = new Transform[sequence.NumFrames];

if (!boneIndices.HasTrack)
var trackedBoneIndex = boneIndices.TrackedBoneIndex;
if (sequence.OriginalSequence.FindTrackForBoneIndex(trackedBoneIndex) < 0)
{
for (int frame = 0; frame < _animatedBonesTransform[s][boneIndices.BoneIndex].Length; frame++)
{
_animatedBonesTransform[s][boneIndices.BoneIndex][frame] = new Transform
{
Relation = boneIndices.HasParentTrack ?
originalTransform.LocalMatrix * _animatedBonesTransform[s][boneIndices.ParentTrackIndex][frame].Matrix :
Relation = boneIndices.IsParentTracked ?
originalTransform.LocalMatrix * _animatedBonesTransform[s][boneIndices.TrackedParentBoneIndex][frame].Matrix :
originalTransform.Relation
};
}
}
else
{
var trackIndex = boneIndices.TrackIndex;
for (int frame = 0; frame < _animatedBonesTransform[s][boneIndices.BoneIndex].Length; frame++)
{
var boneOrientation = originalTransform.Rotation;
var bonePosition = originalTransform.Position;
var boneScale = originalTransform.Scale;

sequence.Tracks[trackIndex].GetBonePosition(frame, sequence.NumFrames, false, ref bonePosition, ref boneOrientation);
if (frame < sequence.Tracks[trackIndex].KeyScale.Length)
boneScale = sequence.Tracks[trackIndex].KeyScale[frame];
sequence.Tracks[trackedBoneIndex].GetBonePosition(frame, sequence.NumFrames, false, ref bonePosition, ref boneOrientation);
if (frame < sequence.Tracks[trackedBoneIndex].KeyScale.Length)
boneScale = sequence.Tracks[trackedBoneIndex].KeyScale[frame];

switch (anim.BoneModes[trackIndex])
switch (anim.BoneModes[trackedBoneIndex])
{
case EBoneTranslationRetargetingMode.Skeleton when !rotationOnly:
{
var targetTransform = sequence.RetargetBasePose?[trackIndex] ?? anim.BonePositions[trackIndex];
var targetTransform = sequence.RetargetBasePose?[trackedBoneIndex] ?? anim.BonePositions[trackedBoneIndex];
bonePosition = targetTransform.Translation;
break;
}
Expand All @@ -126,7 +126,7 @@ public void Animate(CAnimSet anim, bool rotationOnly)
var sourceTranslationLength = (originalTransform.Position / Constants.SCALE_DOWN_RATIO).Size();
if (sourceTranslationLength > UnrealMath.KindaSmallNumber)
{
var targetTranslationLength = sequence.RetargetBasePose?[trackIndex].Translation.Size() ?? anim.BonePositions[trackIndex].Translation.Size();
var targetTranslationLength = sequence.RetargetBasePose?[trackedBoneIndex].Translation.Size() ?? anim.BonePositions[trackedBoneIndex].Translation.Size();
bonePosition.Scale(targetTranslationLength / sourceTranslationLength);
}
break;
Expand All @@ -135,7 +135,7 @@ public void Animate(CAnimSet anim, bool rotationOnly)
{
// can't tell if it's working or not
var sourceSkelTrans = originalTransform.Position / Constants.SCALE_DOWN_RATIO;
var refPoseTransform = sequence.RetargetBasePose?[trackIndex] ?? anim.BonePositions[trackIndex];
var refPoseTransform = sequence.RetargetBasePose?[trackedBoneIndex] ?? anim.BonePositions[trackedBoneIndex];

boneOrientation = boneOrientation * FQuat.Conjugate(originalTransform.Rotation) * refPoseTransform.Rotation;
bonePosition += refPoseTransform.Translation - sourceSkelTrans;
Expand All @@ -146,7 +146,7 @@ public void Animate(CAnimSet anim, bool rotationOnly)
case EBoneTranslationRetargetingMode.OrientAndScale when !rotationOnly:
{
var sourceSkelTrans = originalTransform.Position / Constants.SCALE_DOWN_RATIO;
var targetSkelTrans = sequence.RetargetBasePose?[trackIndex].Translation ?? anim.BonePositions[trackIndex].Translation;
var targetSkelTrans = sequence.RetargetBasePose?[trackedBoneIndex].Translation ?? anim.BonePositions[trackedBoneIndex].Translation;

if (!sourceSkelTrans.Equals(targetSkelTrans))
{
Expand All @@ -167,12 +167,12 @@ public void Animate(CAnimSet anim, bool rotationOnly)
}

// revert FixRotationKeys
if (trackIndex > 0) boneOrientation.Conjugate();
if (trackedBoneIndex > 0) boneOrientation.Conjugate();
bonePosition *= Constants.SCALE_DOWN_RATIO;

_animatedBonesTransform[s][boneIndices.BoneIndex][frame] = new Transform
{
Relation = boneIndices.HasParentTrack ? _animatedBonesTransform[s][boneIndices.ParentTrackIndex][frame].Matrix : originalTransform.Relation,
Relation = boneIndices.IsParentTracked ? _animatedBonesTransform[s][boneIndices.TrackedParentBoneIndex][frame].Matrix : originalTransform.Relation,
Rotation = boneOrientation,
Position = rotationOnly ? originalTransform.Position : bonePosition,
Scale = sequence.bAdditive ? FVector.OneVector : boneScale
Expand All @@ -194,24 +194,24 @@ private void TrackSkeleton(CAnimSet anim)
if (!BonesIndicesByLoweredName.TryGetValue(info.Name.Text.ToLower(), out var boneIndices))
continue;

boneIndices.TrackIndex = trackIndex;
boneIndices.TrackedBoneIndex = trackIndex;
var parentTrackIndex = info.ParentIndex;

do
{
if (parentTrackIndex < 0) break;
info = anim.TrackBonesInfo[parentTrackIndex];
if (boneIndices.LoweredParentBoneName.Equals(info.Name.Text, StringComparison.OrdinalIgnoreCase) && // same parent (name based)
BonesIndicesByLoweredName.TryGetValue(info.Name.Text.ToLower(), out var parentBoneIndices) && parentBoneIndices.HasTrack)
boneIndices.ParentTrackIndex = parentBoneIndices.BoneIndex;
BonesIndicesByLoweredName.TryGetValue(info.Name.Text.ToLower(), out var parentBoneIndices) && parentBoneIndices.IsTracked)
boneIndices.TrackedParentBoneIndex = parentBoneIndices.BoneIndex;
else parentTrackIndex = info.ParentIndex;
} while (!boneIndices.HasParentTrack);
} while (!boneIndices.IsParentTracked);
}

// fix parent of untracked bones
foreach ((var boneName, var boneIndices) in BonesIndicesByLoweredName)
{
if (boneIndices.IsRoot || boneIndices.HasTrack && boneIndices.HasParentTrack) // assuming root bone always has a track
if (boneIndices.IsRoot || boneIndices.IsTracked && boneIndices.IsParentTracked) // assuming root bone always has a track
continue;

#if DEBUG
Expand All @@ -222,18 +222,18 @@ private void TrackSkeleton(CAnimSet anim)
do
{
var parentBoneIndices = BonesIndicesByLoweredName[loweredParentBoneName];
if (parentBoneIndices.HasParentTrack || parentBoneIndices.IsRoot) boneIndices.ParentTrackIndex = parentBoneIndices.BoneIndex;
if (parentBoneIndices.IsParentTracked || parentBoneIndices.IsRoot) boneIndices.TrackedParentBoneIndex = parentBoneIndices.BoneIndex;
else loweredParentBoneName = parentBoneIndices.LoweredParentBoneName;
} while (!boneIndices.HasParentTrack);
} while (!boneIndices.IsParentTracked);
}
}

public void ResetAnimatedData(bool full = false)
{
foreach (var boneIndices in BonesIndicesByLoweredName.Values)
{
boneIndices.TrackIndex = -1;
boneIndices.ParentTrackIndex = -1;
boneIndices.TrackedBoneIndex = -1;
boneIndices.TrackedParentBoneIndex = -1;
}

if (!full) return;
Expand Down
5 changes: 3 additions & 2 deletions FModel/Views/Snooper/Snooper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ protected override void OnUpdateFrame(FrameEventArgs e)
if (!IsVisible || ImGui.GetIO().WantTextInput)
return;

Renderer.CameraOp.Modify(KeyboardState, (float) e.Time);
var delta = (float) e.Time;
Renderer.CameraOp.Modify(KeyboardState, delta);

if (KeyboardState.IsKeyPressed(Keys.Space))
Renderer.Options.Tracker.IsPaused = !Renderer.Options.Tracker.IsPaused;
Expand Down Expand Up @@ -233,7 +234,7 @@ private struct DEVMODE
public static int GetMaxRefreshFrequency()
{
var rf = 60;
DEVMODE vDevMode = new DEVMODE();
var vDevMode = new DEVMODE();
var i = 0;
while (EnumDisplaySettings(null, i, ref vDevMode))
{
Expand Down

0 comments on commit a5cbc5b

Please sign in to comment.