From 43695a729b7f12768a91ef1c1eb4df7f43e9d018 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Thu, 30 Oct 2025 23:14:16 +0300 Subject: [PATCH] refactor: monogame camera transform caching --- .../Behaviours/MonoGameCamera2D.cs | 11 +++++------ .../Behaviours/MonoGameCamera3D.cs | 9 ++++----- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/MonoGameCamera2D.cs b/Engine.Integration/Engine.Integration.MonoGame/Behaviours/MonoGameCamera2D.cs index 911a7af..e8827be 100644 --- a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/MonoGameCamera2D.cs +++ b/Engine.Integration/Engine.Integration.MonoGame/Behaviours/MonoGameCamera2D.cs @@ -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 OnMatrixTransformChanged { get; } = new(); public Event 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().Window.Graphics; Viewport = Graphics.GraphicsDevice.Viewport; + Transform = BehaviourController.GetRequiredBehaviour(); } 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(); - protected sealed override void FinalizeInternal() => Transform = null!; } diff --git a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/MonoGameCamera3D.cs b/Engine.Integration/Engine.Integration.MonoGame/Behaviours/MonoGameCamera3D.cs index 591ed3b..1142814 100644 --- a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/MonoGameCamera3D.cs +++ b/Engine.Integration/Engine.Integration.MonoGame/Behaviours/MonoGameCamera3D.cs @@ -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 OnViewChanged { get; } = new(); public Event 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().Window.Graphics; Viewport = Graphics.GraphicsDevice.Viewport; + Transform = BehaviourController.GetRequiredBehaviour(); } 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(); - 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);