Skip to content

Commit

Permalink
Replaced extension methods with System.Numerics equivalents; Updated …
Browse files Browse the repository at this point in the history
…scene loading logic
  • Loading branch information
ergoxiv committed Sep 8, 2024
1 parent 5b270b3 commit 2151ef3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
7 changes: 3 additions & 4 deletions Anamnesis/Files/CameraShotFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void Apply(CameraService camService, ActorMemory actor)
actorEuler.Z = 0;

// First we use the 0 rotation position and rotate it around the actor by it's current rotation in local space
Vector3 rotatedRelativePosition = QuaternionExtensions.Multiply(QuaternionExtensions.FromEuler(actorEuler), this.Position);
Vector3 rotatedRelativePosition = Vector3.Transform(this.Position, QuaternionExtensions.FromEuler(actorEuler));

// Adjust camera position to world space
camService.GPoseCamera.Position = actor.ModelObject.Transform.Position + rotatedRelativePosition;
Expand Down Expand Up @@ -64,9 +64,8 @@ public void WriteToFile(CameraService camService, ActorMemory actor)
Vector3 localRelativePositon = camService.GPoseCamera.Position - actor.ModelObject.Transform.Position;

// Now we calculate what the position would be if the actor had a 0 rotation
Quaternion invertedActorRotation = QuaternionExtensions.FromEuler(actorEuler);
invertedActorRotation.Invert();
Vector3 rotatedRelativePosition = QuaternionExtensions.Multiply(invertedActorRotation, localRelativePositon);
Quaternion invertedActorRotation = Quaternion.Inverse(QuaternionExtensions.FromEuler(actorEuler));
Vector3 rotatedRelativePosition = Vector3.Transform(localRelativePositon, invertedActorRotation);
this.Position = rotatedRelativePosition;

// We save the angle of the camera as an offset from the angle of the actor
Expand Down
29 changes: 14 additions & 15 deletions Anamnesis/Files/SceneFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace Anamnesis.Files;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using XivToolsWpf.Math3D.Extensions;

[Serializable]
public class SceneFile : JsonFileBase
Expand Down Expand Up @@ -110,16 +109,15 @@ public async Task Apply(Mode mode)
}

Vector3 rootPosition = rootActor!.ModelObject!.Transform!.Position;
Quaternion rootRotation = rootActor!.ModelObject!.Transform!.Rotation;
Quaternion invertedRootRotation = rootRotation;
invertedRootRotation.Invert();
Quaternion rootRotation = new Quaternion(0, 0, 0, 1) * rootActorEntry.Rotation;
rootActor.ModelObject!.Transform!.Rotation = rootRotation;

// Adjust for waist
SkeletonVisual3d rootSkeleton = new();
await rootSkeleton.SetActor(rootActor);
Vector3 rootOriginalWaist = rootActorEntry.Pose?.Bones?["n_hara"]?.Position ?? Vector3.Zero;
Vector3 rootCurrentWaist = rootSkeleton.GetBone("n_hara")?.Position ?? Vector3.Zero;
Vector3 rootAdjustedWaist = QuaternionExtensions.Multiply(rootRotation, rootCurrentWaist - rootOriginalWaist);
Vector3 rootAdjustedWaist = Vector3.Transform(rootCurrentWaist - rootOriginalWaist, rootRotation);

if (mode.HasFlag(Mode.WorldPosition))
{
Expand All @@ -136,16 +134,16 @@ public async Task Apply(Mode mode)

ActorEntry entry = this.ActorEntries[name];

if (name != this.RootActorName && mode.HasFlag(Mode.RelativePosition))
if (actor != rootActor && mode.HasFlag(Mode.RelativePosition))
{
Quaternion rotatedRotation = rootRotation * entry.Rotation;
Vector3 rotatedRelativePosition = QuaternionExtensions.Multiply(rootRotation, entry.Position);

Vector3 originalWaist = entry.Pose?.Bones?["n_hara"]?.Position ?? Vector3.Zero;
Vector3 currentWaist = rootSkeleton.GetBone("n_hara")?.Position ?? Vector3.Zero;
Vector3 adjustedWaist = QuaternionExtensions.Multiply(rotatedRotation, currentWaist - originalWaist);
Vector3 adjustedWaist = Vector3.Transform(currentWaist - originalWaist, rotatedRotation);

actor.ModelObject!.Transform!.Position = (rootPosition + rotatedRelativePosition) - adjustedWaist;
Vector3 rotatedRelativePosition = Vector3.Transform(entry.Position, rootRotation);
actor.ModelObject!.Transform!.Position = rootPosition + rotatedRelativePosition - adjustedWaist;
actor.ModelObject!.Transform!.Rotation = rotatedRotation;

if (!mode.HasFlag(Mode.WorldPosition))
Expand Down Expand Up @@ -178,12 +176,12 @@ public async Task WriteToFile()

List<ActorMemory> actors = TargetService.Instance.PinnedActors.Select(i => i.Memory!).ToList();

if (actors.Count == 0)
throw new Exception("No pinned actors found");

ActorMemory rootActor = actors[0];
this.RootActorName = rootActor.DisplayName;

Vector3 rootPosition = rootActor!.ModelObject!.Transform!.Position;
Quaternion rootRotation = rootActor!.ModelObject!.Transform!.Rotation;

PinnedActor? targetActor = TargetService.GetPlayerTarget();
if (targetActor == null || !targetActor.IsValid || targetActor.Memory == null)
throw new Exception("Targeted actor must be pinned");
Expand All @@ -195,8 +193,9 @@ public async Task WriteToFile()

this.ActorEntries = new();

Quaternion invertedRootRotation = rootRotation;
invertedRootRotation.Invert();
Vector3 rootPosition = rootActor!.ModelObject!.Transform!.Position;
Quaternion rootRotation = rootActor!.ModelObject!.Transform!.Rotation;
Quaternion invertedRootRotation = Quaternion.Inverse(rootRotation);

foreach (ActorMemory actor in actors)
{
Expand All @@ -206,7 +205,7 @@ public async Task WriteToFile()

Vector3 relativePosition = actorPosition - rootPosition;

Vector3 rotatedRelativePosition = QuaternionExtensions.Multiply(invertedRootRotation, relativePosition);
Vector3 rotatedRelativePosition = Vector3.Transform(relativePosition, invertedRootRotation);
Quaternion rotatedRelativeRotation = invertedRootRotation * actorRotation;

CharacterFile characterFile = new();
Expand Down
2 changes: 1 addition & 1 deletion Anamnesis/Serialization/Converters/QuaternionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public override Quaternion Read(ref Utf8JsonReader reader, Type typeToConvert, J

public override void Write(Utf8JsonWriter writer, Quaternion value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
writer.WriteStringValue(value.ToInvariantString());
}
}
2 changes: 1 addition & 1 deletion Anamnesis/Serialization/Converters/VectorConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public override Vector3 Read(ref Utf8JsonReader reader, Type typeToConvert, Json

public override void Write(Utf8JsonWriter writer, Vector3 value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
writer.WriteStringValue(value.ToInvariantString());
}
}
6 changes: 3 additions & 3 deletions Anamnesis/Styles/Controls/QuaternionEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public void Scroll(double delta)
if (this.Active == null)
return;

CmVector angleDelta = VectorExtensions.Multiply(this.Active.Axis, (float)delta);
CmVector angleDelta = CmVector.Multiply(this.Active.Axis, (float)delta);
this.ApplyDelta(angleDelta);
}

Expand Down Expand Up @@ -582,7 +582,7 @@ public CmVector Drag(Point3D mousePosition)
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
speed = 0.5f;

return VectorExtensions.Multiply(this.Axis, (float)(angle * speed));
return CmVector.Multiply(this.Axis, (float)(angle * speed));
}
}
else
Expand Down Expand Up @@ -612,7 +612,7 @@ public CmVector Drag(Point3D mousePosition)
if (Math.Abs(distPos) > Math.Abs(distNeg))
dist = distPos;

return VectorExtensions.Multiply(this.Axis, (float)(-dist * speed));
return CmVector.Multiply(this.Axis, (float)(-dist * speed));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Lib/XivToolsWpf

0 comments on commit 2151ef3

Please sign in to comment.