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!;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ using Engine.Core;
|
||||
|
||||
namespace Engine.Integration.MonoGame;
|
||||
|
||||
public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, IPreDraw
|
||||
public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFrameUpdate, IPreDraw
|
||||
{
|
||||
public Event<MonoGameCamera3D, ViewChangedArguments> OnViewChanged { get; } = new();
|
||||
public Event<MonoGameCamera3D, ProjectionChangedArguments> OnProjectionChanged { get; } = new();
|
||||
@@ -83,7 +83,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, IPreDra
|
||||
}
|
||||
}
|
||||
|
||||
public Engine.Core.Quaternion Rotation
|
||||
public Core.Quaternion Rotation
|
||||
{
|
||||
get => Transform.Rotation;
|
||||
set => Transform.Rotation = value;
|
||||
@@ -104,10 +104,12 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, IPreDra
|
||||
|
||||
public Vector2D WorldToScreenPosition(Vector3D worldPosition) => Viewport.Project(worldPosition.ToVector3(), _projection, _view, Matrix.Identity).ToVector3D();
|
||||
|
||||
public void LastActiveFrame() => Transform = null!;
|
||||
public void FirstActiveFrame()
|
||||
{
|
||||
Graphics = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.Graphics;
|
||||
Viewport = Graphics.GraphicsDevice.Viewport;
|
||||
Transform = BehaviourController.GetRequiredBehaviour<ITransform3D>();
|
||||
}
|
||||
|
||||
public void PreDraw()
|
||||
@@ -121,9 +123,6 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, IPreDra
|
||||
Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Viewport.AspectRatio, 0.1f, 100f);
|
||||
}
|
||||
|
||||
protected sealed override void InitializeInternal() => Transform = BehaviourController.GetRequiredBehaviour<ITransform3D>();
|
||||
protected sealed override void FinalizeInternal() => Transform = null!;
|
||||
|
||||
public readonly record struct ViewChangedArguments(Matrix PreviousView);
|
||||
public readonly record struct ProjectionChangedArguments(Matrix PreviousProjection);
|
||||
public readonly record struct ViewportChangedArguments(Viewport PreviousViewport);
|
||||
|
||||
Reference in New Issue
Block a user