feat: added a basic Viewport field to ICamera

This commit is contained in:
2026-03-28 22:35:01 +03:00
parent 734649955e
commit 0707665481
3 changed files with 23 additions and 19 deletions

View File

@@ -5,6 +5,11 @@ namespace Engine.Core;
/// </summary> /// </summary>
public interface ICamera public interface ICamera
{ {
/// <summary>
/// The viewport of the <see cref="ICamera"/>.
/// </summary>
Vector2D Viewport { get; }
/// <summary> /// <summary>
/// View <see cref="Matrix4x4"/> of the <see cref="ICamera"/>. /// View <see cref="Matrix4x4"/> of the <see cref="ICamera"/>.
/// </summary> /// </summary>

View File

@@ -1,5 +1,4 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Engine.Core; using Engine.Core;
@@ -41,7 +40,7 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
} }
} = Matrix4x4.Identity; } = Matrix4x4.Identity;
public Viewport Viewport public Vector2D Viewport
{ {
get; get;
set set
@@ -72,8 +71,8 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
// TODO This causes delay since OnPreDraw calls assuming this is called in in Update // TODO This causes delay since OnPreDraw calls assuming this is called in in Update
public Vector2D ScreenToWorldPosition(Vector2D screenPosition) public Vector2D ScreenToWorldPosition(Vector2D screenPosition)
{ {
float x = 2f * screenPosition.X / Viewport.Width - 1f; float x = 2f * screenPosition.X / Viewport.X - 1f;
float y = 1f - 2f * screenPosition.Y / Viewport.Height; float y = 1f - 2f * screenPosition.Y / Viewport.Y;
Vector4D normalizedCoordinates = new(x, y, 0f, 1f); Vector4D normalizedCoordinates = new(x, y, 0f, 1f);
Matrix4x4 invertedViewProjectionMatrix = (ProjectionMatrix * ViewMatrix).Inverse; Matrix4x4 invertedViewProjectionMatrix = (ProjectionMatrix * ViewMatrix).Inverse;
@@ -96,8 +95,8 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
if (clip.W != 0f) if (clip.W != 0f)
clip /= clip.W; clip /= clip.W;
float screenX = (clip.X + 1f) * .5f * Viewport.Width; float screenX = (clip.X + 1f) * .5f * Viewport.X;
float screenY = (1f - clip.Y) * .5f * Viewport.Height; float screenY = (1f - clip.Y) * .5f * Viewport.Y;
return new(screenX, screenY); return new(screenX, screenY);
} }
@@ -106,17 +105,17 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
public void FirstActiveFrame() public void FirstActiveFrame()
{ {
Graphics = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.Graphics; Graphics = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.Graphics;
Viewport = Graphics.GraphicsDevice.Viewport; Viewport = new(Graphics.GraphicsDevice.Viewport.Width, Graphics.GraphicsDevice.Viewport.Height);
Transform = BehaviourController.GetRequiredBehaviour<ITransform2D>(); Transform = BehaviourController.GetRequiredBehaviour<ITransform2D>();
} }
public void PreDraw() public void PreDraw()
{ {
ProjectionMatrix = Matrix4x4.CreateOrthographicViewCentered(Viewport.Width, Viewport.Height); ProjectionMatrix = Matrix4x4.CreateOrthographicViewCentered(Viewport.X, Viewport.Y);
ViewMatrix = Matrix4x4.Identity ViewMatrix = Matrix4x4.Identity
.ApplyTranslation(new Vector3D(-Transform.Position.X, -Transform.Position.Y, 0f))
.ApplyRotationZ(Transform.Rotation * Math.DegreeToRadian)
.ApplyScale(Transform.Scale.X.Max(Transform.Scale.Y)) .ApplyScale(Transform.Scale.X.Max(Transform.Scale.Y))
.ApplyScale(Zoom) .ApplyScale(Zoom);
.ApplyRotationZ(-Transform.Rotation * Math.DegreeToRadian)
.ApplyTranslation(new Vector3D(-Transform.Position.X, -Transform.Position.Y, 0f));
} }
} }

View File

@@ -49,7 +49,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
} }
} = Matrix4x4.Identity; } = Matrix4x4.Identity;
public Viewport Viewport public Vector2D Viewport
{ {
get; get;
set set
@@ -57,7 +57,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
if (field.Equals(value)) if (field.Equals(value))
return; return;
Viewport previousViewport = field; Vector2D previousViewport = field;
field = value; field = value;
SetForRecalculation(); SetForRecalculation();
OnViewportChanged.Invoke(this, new(previousViewport)); OnViewportChanged.Invoke(this, new(previousViewport));
@@ -114,21 +114,21 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
Matrix projection = ProjectionMatrix.ToXnaMatrix(); Matrix projection = ProjectionMatrix.ToXnaMatrix();
Matrix view = ViewMatrix.ToXnaMatrix(); Matrix view = ViewMatrix.ToXnaMatrix();
Vector3 worldNear = Viewport.Unproject(nearPoint, projection, view, Matrix.Identity); Vector3 worldNear = Graphics.GraphicsDevice.Viewport.Unproject(nearPoint, projection, view, Matrix.Identity);
Vector3 worldFar = Viewport.Unproject(farPoint, projection, view, Matrix.Identity); Vector3 worldFar = Graphics.GraphicsDevice.Viewport.Unproject(farPoint, projection, view, Matrix.Identity);
Vector3 direction = Vector3.Normalize(worldFar - worldNear); Vector3 direction = Vector3.Normalize(worldFar - worldNear);
return new(worldNear.ToVector3D(), direction.ToVector3D()); return new(worldNear.ToVector3D(), direction.ToVector3D());
} }
public Vector2D WorldToScreenPosition(Vector3D worldPosition) => Viewport.Project(worldPosition.ToVector3(), ProjectionMatrix.ToXnaMatrix(), ViewMatrix.ToXnaMatrix(), Matrix.Identity).ToVector3D(); public Vector2D WorldToScreenPosition(Vector3D worldPosition) => Graphics.GraphicsDevice.Viewport.Project(worldPosition.ToVector3(), ProjectionMatrix.ToXnaMatrix(), ViewMatrix.ToXnaMatrix(), Matrix.Identity).ToVector3D();
public void LastActiveFrame() => Transform.OnTransformUpdated.RemoveListener(SetDirtyOnTransformUpdate); public void LastActiveFrame() => Transform.OnTransformUpdated.RemoveListener(SetDirtyOnTransformUpdate);
public void FirstActiveFrame() public void FirstActiveFrame()
{ {
Transform = BehaviourController.GetRequiredBehaviour<ITransform3D>(); Transform = BehaviourController.GetRequiredBehaviour<ITransform3D>();
Graphics = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.Graphics; Graphics = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.Graphics;
Viewport = Graphics.GraphicsDevice.Viewport; Viewport = new(Graphics.GraphicsDevice.Viewport.Width, Graphics.GraphicsDevice.Viewport.Height);
Transform.OnTransformUpdated.AddListener(SetDirtyOnTransformUpdate); Transform.OnTransformUpdated.AddListener(SetDirtyOnTransformUpdate);
} }
@@ -167,7 +167,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
private void CalculateProjection() private void CalculateProjection()
{ {
float yScale = 1f / (float)Math.Tan(fieldOfView / 2f); float yScale = 1f / (float)Math.Tan(fieldOfView / 2f);
float xScale = yScale / Viewport.AspectRatio; float xScale = yScale / (Viewport.X / Viewport.Y);
ProjectionMatrix = new Matrix4x4( ProjectionMatrix = new Matrix4x4(
xScale, 0, 0, 0, xScale, 0, 0, 0,
@@ -179,5 +179,5 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
public readonly record struct ViewChangedArguments(Matrix4x4 PreviousView); public readonly record struct ViewChangedArguments(Matrix4x4 PreviousView);
public readonly record struct ProjectionChangedArguments(Matrix4x4 PreviousProjection); public readonly record struct ProjectionChangedArguments(Matrix4x4 PreviousProjection);
public readonly record struct ViewportChangedArguments(Viewport PreviousViewport); public readonly record struct ViewportChangedArguments(Vector2D PreviousViewport);
} }