feat: added a basic Viewport field to ICamera
This commit is contained in:
@@ -5,6 +5,11 @@ namespace Engine.Core;
|
||||
/// </summary>
|
||||
public interface ICamera
|
||||
{
|
||||
/// <summary>
|
||||
/// The viewport of the <see cref="ICamera"/>.
|
||||
/// </summary>
|
||||
Vector2D Viewport { get; }
|
||||
|
||||
/// <summary>
|
||||
/// View <see cref="Matrix4x4"/> of the <see cref="ICamera"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
using Engine.Core;
|
||||
|
||||
@@ -41,7 +40,7 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
|
||||
}
|
||||
} = Matrix4x4.Identity;
|
||||
|
||||
public Viewport Viewport
|
||||
public Vector2D Viewport
|
||||
{
|
||||
get;
|
||||
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
|
||||
public Vector2D ScreenToWorldPosition(Vector2D screenPosition)
|
||||
{
|
||||
float x = 2f * screenPosition.X / Viewport.Width - 1f;
|
||||
float y = 1f - 2f * screenPosition.Y / Viewport.Height;
|
||||
float x = 2f * screenPosition.X / Viewport.X - 1f;
|
||||
float y = 1f - 2f * screenPosition.Y / Viewport.Y;
|
||||
Vector4D normalizedCoordinates = new(x, y, 0f, 1f);
|
||||
|
||||
Matrix4x4 invertedViewProjectionMatrix = (ProjectionMatrix * ViewMatrix).Inverse;
|
||||
@@ -96,8 +95,8 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
|
||||
if (clip.W != 0f)
|
||||
clip /= clip.W;
|
||||
|
||||
float screenX = (clip.X + 1f) * .5f * Viewport.Width;
|
||||
float screenY = (1f - clip.Y) * .5f * Viewport.Height;
|
||||
float screenX = (clip.X + 1f) * .5f * Viewport.X;
|
||||
float screenY = (1f - clip.Y) * .5f * Viewport.Y;
|
||||
|
||||
return new(screenX, screenY);
|
||||
}
|
||||
@@ -106,17 +105,17 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
|
||||
public void FirstActiveFrame()
|
||||
{
|
||||
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>();
|
||||
}
|
||||
|
||||
public void PreDraw()
|
||||
{
|
||||
ProjectionMatrix = Matrix4x4.CreateOrthographicViewCentered(Viewport.Width, Viewport.Height);
|
||||
ProjectionMatrix = Matrix4x4.CreateOrthographicViewCentered(Viewport.X, Viewport.Y);
|
||||
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(Zoom)
|
||||
.ApplyRotationZ(-Transform.Rotation * Math.DegreeToRadian)
|
||||
.ApplyTranslation(new Vector3D(-Transform.Position.X, -Transform.Position.Y, 0f));
|
||||
.ApplyScale(Zoom);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
||||
}
|
||||
} = Matrix4x4.Identity;
|
||||
|
||||
public Viewport Viewport
|
||||
public Vector2D Viewport
|
||||
{
|
||||
get;
|
||||
set
|
||||
@@ -57,7 +57,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
||||
if (field.Equals(value))
|
||||
return;
|
||||
|
||||
Viewport previousViewport = field;
|
||||
Vector2D previousViewport = field;
|
||||
field = value;
|
||||
SetForRecalculation();
|
||||
OnViewportChanged.Invoke(this, new(previousViewport));
|
||||
@@ -114,21 +114,21 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
||||
Matrix projection = ProjectionMatrix.ToXnaMatrix();
|
||||
Matrix view = ViewMatrix.ToXnaMatrix();
|
||||
|
||||
Vector3 worldNear = Viewport.Unproject(nearPoint, projection, view, Matrix.Identity);
|
||||
Vector3 worldFar = Viewport.Unproject(farPoint, projection, view, Matrix.Identity);
|
||||
Vector3 worldNear = Graphics.GraphicsDevice.Viewport.Unproject(nearPoint, projection, view, Matrix.Identity);
|
||||
Vector3 worldFar = Graphics.GraphicsDevice.Viewport.Unproject(farPoint, projection, view, Matrix.Identity);
|
||||
|
||||
Vector3 direction = Vector3.Normalize(worldFar - worldNear);
|
||||
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 FirstActiveFrame()
|
||||
{
|
||||
Transform = BehaviourController.GetRequiredBehaviour<ITransform3D>();
|
||||
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);
|
||||
}
|
||||
@@ -167,7 +167,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
||||
private void CalculateProjection()
|
||||
{
|
||||
float yScale = 1f / (float)Math.Tan(fieldOfView / 2f);
|
||||
float xScale = yScale / Viewport.AspectRatio;
|
||||
float xScale = yScale / (Viewport.X / Viewport.Y);
|
||||
|
||||
ProjectionMatrix = new Matrix4x4(
|
||||
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 ProjectionChangedArguments(Matrix4x4 PreviousProjection);
|
||||
public readonly record struct ViewportChangedArguments(Viewport PreviousViewport);
|
||||
public readonly record struct ViewportChangedArguments(Vector2D PreviousViewport);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user