Skip to content

Commit

Permalink
Introduce VirtualCam
Browse files Browse the repository at this point in the history
  • Loading branch information
AsgardXIV committed Dec 19, 2022
1 parent 62628e0 commit 7c9015b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace XAT.Plugin.Cutscene;

public record class CameraState(Vector3 Position, Quaternion Rotation, float FoV);
public class CameraSettings
{
public Vector3 Scale = Vector3.One;
Expand Down
11 changes: 7 additions & 4 deletions Dalamud/XAT.Plugin/Cutscene/CutsceneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public class CutsceneManager : IDisposable
{
private const double FRAME_STEP = 33.33333333333333;

private XATPlugin Plugin { get; }

public XATCameraPathFile? CameraPath { get; set; }

public CameraSettings CameraSettings { get; } = new();

public CameraState? CameraState { get; private set; }

private XATPlugin Plugin { get; }
public VirtualCamera VirtualCamera => Plugin.VirtualCamera;

public bool IsRunning => Stopwatch.IsRunning;
private Stopwatch Stopwatch = new();
Expand Down Expand Up @@ -56,13 +56,16 @@ public void StartPlayback()

Stopwatch.Reset();
Stopwatch.Start();
this.VirtualCamera.IsActive = true;

}

public void StopPlayback()
{
if (IsRunning)
{
Stopwatch.Reset();
this.VirtualCamera.IsActive = false;
}

}
Expand Down Expand Up @@ -120,7 +123,7 @@ public void Update()
Quaternion finalRotation = BaseRotation * rawRotation;


CameraState = new CameraState
this.VirtualCamera.State = new VirtualCamera.CameraState
(
Position: finalPosition,
Rotation: finalRotation,
Expand Down
13 changes: 13 additions & 0 deletions Dalamud/XAT.Plugin/Cutscene/VirtualCamera.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Numerics;

namespace XAT.Plugin.Cutscene;

public class VirtualCamera
{
public record class CameraState(Vector3 Position, Quaternion Rotation, float? FoV);

public bool IsActive { get; set; } = false;

public CameraState State { get; set; } = new CameraState(Vector3.Zero, Quaternion.Identity, null);
}
35 changes: 10 additions & 25 deletions Dalamud/XAT.Plugin/Game/Hooks/CameraHooks.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using Dalamud.Hooking;
using System;
using System.Numerics;
using XAT.Plugin.Cutscene;
using XAT.Plugin.Utils;

namespace XAT.Plugin.Game.Hooks;

public unsafe class CameraHooks : IDisposable
{
private XATPlugin Plugin { get; }
private VirtualCamera VirtualCamera => Plugin.VirtualCamera;

private delegate Matrix4x4* MakeProjectionMatrix2(IntPtr ptr, float fov, float aspect, float nearPlane, float farPlane, float a6, float a7);
private static Hook<MakeProjectionMatrix2> ProjectionHook = null!;
Expand All @@ -31,20 +33,10 @@ public CameraHooks(XATPlugin plugin)

private unsafe Matrix4x4* ProjectionDetour(IntPtr ptr, float fov, float aspect, float nearPlane, float farPlane, float a6, float a7)
{
var exec = ProjectionHook.Original(ptr, fov, aspect, nearPlane, farPlane, a6, a7);

if (!Plugin.CutsceneManager.IsRunning)
{
return exec;
}

var cameraState = Plugin.CutsceneManager.CameraState;
if (cameraState == null)
{
return exec;
}
if (VirtualCamera.IsActive)
fov = VirtualCamera.State.FoV ?? fov;

exec = ProjectionHook.Original(ptr, cameraState.FoV, aspect, nearPlane, farPlane, a6, a7);
var exec = ProjectionHook.Original(ptr, fov, aspect, nearPlane, farPlane, a6, a7);

return exec;
}
Expand All @@ -53,23 +45,16 @@ public CameraHooks(XATPlugin plugin)
{
var exec = ViewHook.Original(a1);

if (!Plugin.CutsceneManager.IsRunning)
{
return exec;
}

var cameraState = Plugin.CutsceneManager.CameraState;
if (cameraState == null)
{
if (!VirtualCamera.IsActive)
return exec;
}

var cameraState = VirtualCamera.State;
var rotMat = Matrix4x4.CreateFromQuaternion(cameraState.Rotation);
Matrix4x4.Invert(rotMat, out Matrix4x4 invRotMat);
var tranMat = Matrix4x4.CreateTranslation(-cameraState.Position);
var finalMat = tranMat * invRotMat;
var transMat = Matrix4x4.CreateTranslation(-cameraState.Position);
var finalMat = transMat * invRotMat;

* exec = finalMat;
*exec = finalMat;

return exec;
}
Expand Down
3 changes: 3 additions & 0 deletions Dalamud/XAT.Plugin/XATPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class XATPlugin : IDalamudPlugin

public CameraHooks CameraHooks { get; }

public VirtualCamera VirtualCamera { get; }

public XATWindow Window { get; }

public WindowSystem WindowSystem { get; }
Expand All @@ -50,6 +52,7 @@ public XATPlugin(DalamudPluginInterface pluginInterface)
this.PluginInterface = pluginInterface;
this.PluginInterface.Inject(this);
this.GPoseService= new(this);
this.VirtualCamera = new();
this.CameraHooks = new(this);

this.CutsceneManager= new(this);
Expand Down

0 comments on commit 7c9015b

Please sign in to comment.