Skip to content

Commit

Permalink
normalize quaternions
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Jan 8, 2023
1 parent 134f722 commit 8f9a692
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 24 deletions.
2 changes: 1 addition & 1 deletion FModel/Views/Snooper/Buffers/PickingTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void Render(Matrix4x4 viewMatrix, Matrix4x4 projMatrix, IDictionary<FGuid
_shader.SetUniform("uD", guid.D);

if (!model.Show) continue;
model.SimpleRender(_shader);
model.PickingRender(_shader);
}

Bind(0);
Expand Down
24 changes: 23 additions & 1 deletion FModel/Views/Snooper/Models/Animations/Skeleton.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using CUE4Parse_Conversion.Animations;
using CUE4Parse.UE4.Assets.Exports.Animation;
using CUE4Parse.UE4.Assets.Exports.SkeletalMesh;
using CUE4Parse.UE4.Objects.Core.Math;
using CUE4Parse.UE4.Objects.UObject;
using FModel.Views.Snooper.Shading;
using Serilog;

namespace FModel.Views.Snooper.Models.Animations;

Expand All @@ -19,7 +22,9 @@ public class Skeleton : IDisposable

public Animation Anim;

public Skeleton(FPackageIndex package)
private FVector _previousMatrix;

public Skeleton(FPackageIndex package, Transform transform)
{
UnrealSkeleton = package.Load<USkeleton>();
if (UnrealSkeleton == null) return;
Expand All @@ -38,6 +43,7 @@ public Skeleton(FPackageIndex package)
var bone = UnrealSkeleton.ReferenceSkeleton.FinalRefBonePose[parentBoneIndex];
boneTransform = new Transform
{
Relation = transform.Matrix,
Rotation = bone.Rotation,
Position = bone.Translation * Constants.SCALE_DOWN_RATIO,
Scale = bone.Scale3D
Expand Down Expand Up @@ -81,6 +87,22 @@ public void SetAnimation(CAnimSet anim)
Anim = new Animation(anim, BonesIndexByName, BonesTransformByIndex);
}

public void UpdateSocketsMatrix(Transform t)
{
var m = t.Position;
if (m == _previousMatrix) return;

var delta = _previousMatrix - m;
Log.Logger.Information("Update {0}", delta);

// BonesTransformByIndex[0].Relation.Translation += delta;
foreach (var socket in Sockets)
{
socket.Transform.Relation.Translation += delta;
}
_previousMatrix = m;
}

public void SetUniform(Shader shader)
{
if (!IsLoaded) return;
Expand Down
14 changes: 11 additions & 3 deletions FModel/Views/Snooper/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public Model(USkeletalMesh export, CSkeletalMesh skeletalMesh) : this(export, sk

private Model(UObject export, ResolvedObject[] materials, FPackageIndex skeleton, int numLods, CBaseMeshLod lod, CMeshVertex[] vertices, Transform transform = null) : this(export)
{
var t = transform ?? Transform.Identity;
var hasCustomUvs = lod.ExtraUV.IsValueCreated;
UvCount = hasCustomUvs ? Math.Max(lod.NumTexCoords, numLods) : lod.NumTexCoords;
TwoSided = lod.IsTwoSided;
Expand All @@ -124,7 +125,7 @@ private Model(UObject export, ResolvedObject[] materials, FPackageIndex skeleton

if (skeleton != null)
{
Skeleton = new Skeleton(skeleton);
Skeleton = new Skeleton(skeleton, t);
VertexSize += 8; // + BoneIds + BoneWeights
}

Expand Down Expand Up @@ -186,7 +187,7 @@ private Model(UObject export, ResolvedObject[] materials, FPackageIndex skeleton
if (section.IsValid) Sections[s].SetupMaterial(Materials[section.MaterialIndex]);
}

AddInstance(transform ?? Transform.Identity);
AddInstance(t);
}

public void AddInstance(Transform transform)
Expand All @@ -195,6 +196,13 @@ public void AddInstance(Transform transform)
Transforms.Add(transform);
}

public void UpdateMatrix() => UpdateMatrix(SelectedInstance);
public void UpdateMatrix(Transform transform) => UpdateMatrix(SelectedInstance, transform);
public void UpdateMatrix(int instance, Transform transform)
{
Transforms[instance] = transform;
UpdateMatrix(instance);
}
public void UpdateMatrix(int instance)
{
_matrixVbo.Bind();
Expand Down Expand Up @@ -304,7 +312,7 @@ public void Render(Shader shader, bool outline = false)
if (outline) GL.Enable(EnableCap.DepthTest);
}

public void SimpleRender(Shader shader)
public void PickingRender(Shader shader)
{
if (TwoSided) GL.Disable(EnableCap.CullFace);

Expand Down
3 changes: 3 additions & 0 deletions FModel/Views/Snooper/Models/Socket.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using CUE4Parse.UE4.Assets.Exports.SkeletalMesh;
using CUE4Parse.UE4.Objects.Core.Misc;

namespace FModel.Views.Snooper.Models;

Expand All @@ -9,6 +10,8 @@ public class Socket : IDisposable
public readonly string Bone;
public readonly Transform Transform;

public FGuid AttachedModel;

public Socket(USkeletalMeshSocket socket)
{
Name = socket.SocketName.Text;
Expand Down
20 changes: 13 additions & 7 deletions FModel/Views/Snooper/SnimGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using OpenTK.Windowing.Common;
using System.Numerics;
using System.Text;
using CUE4Parse.UE4.Objects.Core.Math;
using FModel.Settings;
using FModel.Views.Snooper.Models;
using FModel.Views.Snooper.Shading;
Expand Down Expand Up @@ -402,14 +403,18 @@ private void DrawSockets(Snooper s)
foreach (var socket in model.Skeleton.Sockets)
{
ImGui.PushID(i);
ImGui.Text($"{socket.Name} attached to {socket.Bone}");
ImGui.Text($"P: {socket.Transform.Matrix.M41} | {socket.Transform.Matrix.M42} | {socket.Transform.Matrix.M43}");
// ImGui.Text($"R: {socket.Transform.Rotation}");
// ImGui.Text($"S: {socket.Transform.Scale}");
ImGui.Text($"'{socket.Name}' attached to '{socket.Bone}'");
ImGui.Text($"P: {socket.Transform.Matrix.Translation.X} | {socket.Transform.Matrix.Translation.Y} | {socket.Transform.Matrix.Translation.Z}");
if (ImGui.Button("Attach") && s.Renderer.Options.TryGetModel(out var selected))
{
selected.Transforms[selected.SelectedInstance] = socket.Transform;
selected.UpdateMatrix(selected.SelectedInstance);
socket.AttachedModel = s.Renderer.Options.SelectedModel;
selected.UpdateMatrix(new Transform
{
Relation = socket.Transform.Matrix,
Position = FVector.ZeroVector,
Rotation = new FQuat(0),
Scale = FVector.OneVector
});
}
ImGui.PopID();
i++;
Expand Down Expand Up @@ -527,7 +532,8 @@ private void DrawDetails(Snooper s)
ImGui.EndDisabled(); ImGui.PopID();
model.Transforms[model.SelectedInstance].ImGuiTransform(s.Renderer.CameraOp.Speed / 100f);
model.UpdateMatrix(model.SelectedInstance);
model.UpdateMatrix();
ImGui.EndTabItem();
}
Expand Down
18 changes: 6 additions & 12 deletions FModel/Views/Snooper/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ public static Transform Identity

public Matrix4x4 Matrix =>
Matrix4x4.CreateScale(Scale.X, Scale.Z, Scale.Y) *
Matrix4x4.CreateFromQuaternion(new Quaternion(Rotation.X, Rotation.Z, Rotation.Y, -Rotation.W)) *
Matrix4x4.CreateFromQuaternion(Quaternion.Normalize(new Quaternion(Rotation.X, Rotation.Z, Rotation.Y, -Rotation.W))) *
Matrix4x4.CreateTranslation(Position.X, Position.Z, Position.Y)
* Relation;

public void ImGuiTransform(float speed)
{
const int width = 100;
const float width = 100f;

ImGui.SetNextItemOpen(true, ImGuiCond.Appearing);
if (ImGui.TreeNode("Location"))
{
ImGui.PushID(1);
ImGui.SetNextItemWidth(width);
ImGui.DragFloat("X", ref Position.X, speed, 0f, 0f, "%.2f m");

Expand All @@ -39,33 +38,29 @@ public void ImGuiTransform(float speed)
ImGui.SetNextItemWidth(width);
ImGui.DragFloat("Z", ref Position.Y, speed, 0f, 0f, "%.2f m");

ImGui.PopID();
ImGui.TreePop();
}

ImGui.SetNextItemOpen(true, ImGuiCond.Appearing);
if (ImGui.TreeNode("Rotation"))
{
ImGui.PushID(2);
ImGui.SetNextItemWidth(width);
ImGui.DragFloat("X", ref Rotation.X, .5f, 0f, 0f, "%.1f°");
ImGui.DragFloat("W", ref Rotation.W, .005f, 0f, 0f, "%.3f rad");

ImGui.SetNextItemWidth(width);
ImGui.DragFloat("Y", ref Rotation.Z, .5f, 0f, 0f, "%.1f°");
ImGui.DragFloat("X", ref Rotation.X, .005f, 0f, 0f, "%.3f rad");

ImGui.SetNextItemWidth(width);
ImGui.DragFloat("Z", ref Rotation.Y, .5f, 0f, 0f, "%.1f°");
ImGui.DragFloat("Y", ref Rotation.Z, .005f, 0f, 0f, "%.3f rad");

ImGui.SetNextItemWidth(width);
ImGui.DragFloat("W", ref Rotation.W, .5f, 0f, 0f, "%.1f°");
ImGui.DragFloat("Z", ref Rotation.Y, .005f, 0f, 0f, "%.3f rad");

ImGui.PopID();
ImGui.TreePop();
}

if (ImGui.TreeNode("Scale"))
{
ImGui.PushID(3);
ImGui.SetNextItemWidth(width);
ImGui.DragFloat("X", ref Scale.X, speed, 0f, 0f, "%.3f");

Expand All @@ -75,7 +70,6 @@ public void ImGuiTransform(float speed)
ImGui.SetNextItemWidth(width);
ImGui.DragFloat("Z", ref Scale.Y, speed, 0f, 0f, "%.3f");

ImGui.PopID();
ImGui.TreePop();
}
}
Expand Down

0 comments on commit 8f9a692

Please sign in to comment.