Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Feb 17, 2023
1 parent 0db81b8 commit 4797a4b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 38 deletions.
2 changes: 1 addition & 1 deletion FModel/Views/Snooper/Animations/Skeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ private void TrackSkeleton(CAnimSet anim)

boneIndices.TrackIndex = trackIndex;
var parentTrackIndex = info.ParentIndex;
if (parentTrackIndex < 0) continue;

do
{
if (parentTrackIndex < 0) break;
info = anim.TrackBonesInfo[parentTrackIndex];
if (BonesIndicesByLoweredName.TryGetValue(info.Name.Text.ToLower(), out var parentBoneIndices) && parentBoneIndices.HasTrack)
boneIndices.ParentTrackIndex = parentBoneIndices.BoneIndex;
Expand Down
9 changes: 3 additions & 6 deletions FModel/Views/Snooper/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public Model(UStaticMesh export, FGuid guid, CStaticMesh staticMesh, Transform t
}

public Model(USkeletalMesh export, FGuid guid, CSkeletalMesh skeletalMesh) : this(export, guid, skeletalMesh, Transform.Identity) {}
private Model(USkeletalMesh export, FGuid guid, CSkeletalMesh skeletalMesh, Transform transform) : this(export, guid, export.Materials, skeletalMesh.LODs, transform)
public Model(USkeletalMesh export, FGuid guid, CSkeletalMesh skeletalMesh, Transform transform) : this(export, guid, export.Materials, skeletalMesh.LODs, transform)
{
Box = skeletalMesh.BoundingBox * Constants.SCALE_DOWN_RATIO;
Skeleton = new Skeleton(export.ReferenceSkeleton);
Expand Down Expand Up @@ -275,17 +275,14 @@ public void UpdateMatrices(Options options)
}
private Matrix4x4 UpdateMatrices()
{
_matrixVbo.Bind();
for (int instance = 0; instance < TransformsCount; instance++)
{
var matrix = Transforms[instance].Matrix;
if (matrix == _previousMatrix) return matrix;

_matrixVbo.Bind();
_matrixVbo.Update(instance, matrix);
_matrixVbo.Unbind();

_previousMatrix = matrix;
}
_matrixVbo.Unbind();
return _previousMatrix;
}

Expand Down
4 changes: 3 additions & 1 deletion FModel/Views/Snooper/Models/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Socket : IDisposable
public readonly string Name;
public readonly FName BoneName;
public readonly Transform Transform;
public readonly bool IsVirtual;

public readonly List<SocketAttachementInfo> AttachedModels;

Expand All @@ -27,11 +28,12 @@ private Socket()
AttachedModels = new List<SocketAttachementInfo>();
}

public Socket(string name, FName boneName, Transform transform) : this()
public Socket(string name, FName boneName, Transform transform, bool isVirtual) : this()
{
Name = name;
BoneName = boneName;
Transform = transform;
IsVirtual = isVirtual;
}

public Socket(UStaticMeshSocket socket) : this()
Expand Down
12 changes: 9 additions & 3 deletions FModel/Views/Snooper/Options.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using CUE4Parse_Conversion.Textures;
using CUE4Parse.UE4.Assets.Exports.Texture;
using CUE4Parse.UE4.Objects.Core.Misc;
Expand Down Expand Up @@ -105,7 +106,7 @@ public void RemoveModel(FGuid guid)

private void DetachAndRemoveModels(Model model)
{
foreach (var socket in model.Sockets)
foreach (var socket in model.Sockets.ToList())
{
foreach (var info in socket.AttachedModels)
{
Expand All @@ -114,7 +115,12 @@ private void DetachAndRemoveModels(Model model)
attachedModel.SafeDetachModel(model);
RemoveModel(info.Guid);
}
socket.Dispose();

if (socket.IsVirtual)
{
socket.Dispose();
model.Sockets.Remove(socket);
}
}
}

Expand Down
68 changes: 42 additions & 26 deletions FModel/Views/Snooper/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,54 @@ private void Animate(UObject anim, Model model)
!notifyClass.TryGetValue(out FPackageIndex meshProp, "SkeletalMeshProp", "StaticMeshProp", "Mesh") ||
!meshProp.TryLoad(out UObject export)) continue;

FGuid guid = export switch
var t = Transform.Identity;
if (notifyClass.TryGetValue(out FTransform offset, "Offset"))
{
UStaticMesh st => LoadStaticMesh(st, false),
USkeletalMesh sk => LoadSkeletalMesh(sk, false),
_ => throw new ArgumentException()
};
t.Rotation = offset.Rotation;
t.Position = offset.Translation * Constants.SCALE_DOWN_RATIO;
t.Scale = offset.Scale3D;
}

FGuid guid;
switch (export)
{
case UStaticMesh st:
{
guid = st.LightingGuid;
if (Options.TryGetModel(guid, out var instancedModel))
instancedModel.AddInstance(t);
else if (st.TryConvert(out var mesh))
Options.Models[guid] = new Model(st, guid, mesh, t);
break;
}
case USkeletalMesh sk:
{
guid = new FGuid((uint) sk.GetFullName().GetHashCode());
if (!Options.Models.ContainsKey(guid) && sk.TryConvert(out var mesh))
Options.Models[guid] = new Model(sk, guid, mesh, t);
break;
}
default:
throw new ArgumentException();
}

if (!Options.TryGetModel(guid, out var addedModel))
continue;

addedModel.IsAnimatedProp = true;
if (notifyClass.TryGetValue(out UObject skeletalMeshPropAnimation, "SkeletalMeshPropAnimation"))
if (notifyClass.TryGetValue(out UObject skeletalMeshPropAnimation, "SkeletalMeshPropAnimation", "Animation"))
Animate(skeletalMeshPropAnimation, addedModel);
if (notifyClass.TryGetValue(out FName socketName, "SocketName"))
{
var t = Transform.Identity;
t = Transform.Identity;
if (notifyClass.TryGetValue(out FVector location, "LocationOffset", "Location"))
t.Position = location * Constants.SCALE_DOWN_RATIO;
if (notifyClass.TryGetValue(out FRotator rotation, "RotationOffset", "Rotation"))
t.Rotation = rotation.Quaternion();
if (notifyClass.TryGetValue(out FVector scale, "Scale"))
t.Scale = scale;

var s = new Socket($"TL_{addedModel.Name}", socketName, t);
var s = new Socket($"TL_{addedModel.Name}", socketName, t, true);
model.Sockets.Add(s);
addedModel.AttachModel(model, s);
}
Expand Down Expand Up @@ -243,40 +267,32 @@ public void Render(float deltaSeconds)
Picking.Render(viewMatrix, projMatrix, Options.Models);
}

private FGuid LoadStaticMesh(UStaticMesh original, bool select = true)
private void LoadStaticMesh(UStaticMesh original)
{
var guid = original.LightingGuid;
if (Options.TryGetModel(guid, out var model))
{
model.AddInstance(Transform.Identity);
if (select) Application.Current.Dispatcher.Invoke(() => model.SetupInstances());
return guid;
Application.Current.Dispatcher.Invoke(() => model.SetupInstances());
return;
}

if (!original.TryConvert(out var mesh))
return guid;
return;

Options.Models[guid] = new Model(original, guid, mesh);
if (select)
{
Options.SelectModel(guid);
SetupCamera(Options.Models[guid].Box);
}
return guid;
Options.SelectModel(guid);
SetupCamera(Options.Models[guid].Box);
}

private FGuid LoadSkeletalMesh(USkeletalMesh original, bool select = true)
private void LoadSkeletalMesh(USkeletalMesh original)
{
var guid = new FGuid((uint) original.GetFullName().GetHashCode());
if (Options.Models.ContainsKey(guid) || !original.TryConvert(out var mesh)) return guid;
if (Options.Models.ContainsKey(guid) || !original.TryConvert(out var mesh)) return;

Options.Models[guid] = new Model(original, guid, mesh);
if (select)
{
Options.SelectModel(guid);
SetupCamera(Options.Models[guid].Box);
}
return guid;
Options.SelectModel(guid);
SetupCamera(Options.Models[guid].Box);
}

private void LoadMaterialInstance(UMaterialInstance original)
Expand Down
2 changes: 1 addition & 1 deletion FModel/Views/Snooper/Snooper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected override void OnUpdateFrame(FrameEventArgs e)
Renderer.CameraOp.Modify(KeyboardState, (float) e.Time);

if (KeyboardState.IsKeyPressed(Keys.Delete))
Renderer.Options.Models.Remove(Renderer.Options.SelectedModel);
Renderer.Options.RemoveModel(Renderer.Options.SelectedModel);
if (KeyboardState.IsKeyPressed(Keys.H))
WindowShouldClose(true, false);
if (KeyboardState.IsKeyPressed(Keys.Escape))
Expand Down

0 comments on commit 4797a4b

Please sign in to comment.