refactor: monogame camera transform caching
This commit is contained in:
@@ -5,7 +5,7 @@ using Engine.Core;
|
||||
|
||||
namespace Engine.Integration.MonoGame;
|
||||
|
||||
public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, IPreDraw
|
||||
public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFrameUpdate, IPreDraw
|
||||
{
|
||||
public Event<MonoGameCamera2D> OnMatrixTransformChanged { get; } = new();
|
||||
public Event<MonoGameCamera2D> OnViewportChanged { get; } = new();
|
||||
@@ -56,7 +56,7 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, IPreDra
|
||||
get => _zoom;
|
||||
set
|
||||
{
|
||||
float newValue = Engine.Core.Math.Max(0.1f, value);
|
||||
float newValue = Math.Max(0.1f, value);
|
||||
|
||||
if (_zoom == newValue)
|
||||
return;
|
||||
@@ -84,22 +84,21 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, IPreDra
|
||||
return screenPosition.Scale(EngineConverterExtensions.screenScale);
|
||||
}
|
||||
|
||||
public void LastActiveFrame() => Transform = null!;
|
||||
public void FirstActiveFrame()
|
||||
{
|
||||
Graphics = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.Graphics;
|
||||
Viewport = Graphics.GraphicsDevice.Viewport;
|
||||
Transform = BehaviourController.GetRequiredBehaviour<ITransform2D>();
|
||||
}
|
||||
|
||||
public void PreDraw()
|
||||
{
|
||||
MatrixTransform =
|
||||
Matrix.CreateTranslation(new Vector3(-Position.X, Position.Y, 0f)) *
|
||||
Matrix.CreateRotationZ(Rotation * Engine.Core.Math.DegreeToRadian) *
|
||||
Matrix.CreateRotationZ(Rotation * Math.DegreeToRadian) *
|
||||
Matrix.CreateScale(Transform.Scale.X.Max(Transform.Scale.Y)) *
|
||||
Matrix.CreateScale(Zoom) *
|
||||
Matrix.CreateTranslation(new Vector3(_viewport.Width * .5f, _viewport.Height * .5f, 0f));
|
||||
}
|
||||
|
||||
protected sealed override void InitializeInternal() => Transform = BehaviourController.GetRequiredBehaviour<ITransform2D>();
|
||||
protected sealed override void FinalizeInternal() => Transform = null!;
|
||||
}
|
||||
|
||||
@@ -53,12 +53,6 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3D Position
|
||||
{
|
||||
get => Transform.Position;
|
||||
set => Transform.Position = value;
|
||||
}
|
||||
|
||||
public Viewport Viewport
|
||||
{
|
||||
get => _viewport;
|
||||
@@ -115,12 +109,6 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
||||
}
|
||||
}
|
||||
|
||||
public Core.Quaternion Rotation
|
||||
{
|
||||
get => Transform.Rotation;
|
||||
set => Transform.Rotation = value;
|
||||
}
|
||||
|
||||
// TODO This causes delay since OnPreDraw calls assuming this is called in in Update
|
||||
public Ray3D ScreenToWorldRay(Vector2D screenPosition)
|
||||
{
|
||||
@@ -164,7 +152,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
||||
Vector3 forward = Vector3.Normalize(Transform.Forward.ToVector3());
|
||||
Vector3 up = Vector3.Normalize(Transform.Up.ToVector3());
|
||||
Vector3 right = Vector3.Normalize(Transform.Right.ToVector3());
|
||||
Vector3 position = Position.ToVector3();
|
||||
Vector3 position = Transform.Position.ToVector3();
|
||||
|
||||
View = new Matrix(
|
||||
right.X, up.X, forward.X, 0,
|
||||
|
||||
Reference in New Issue
Block a user