refactor: ICamera interface added with view and projection matrices fields
This commit is contained in:
17
Engine.Core/Abstract/ICamera.cs
Normal file
17
Engine.Core/Abstract/ICamera.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
namespace Engine.Core;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a camera with view and projections matrices in the engine.
|
||||||
|
/// </summary>
|
||||||
|
public interface ICamera
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// View <see cref="Matrix4x4"/> of the <see cref="ICamera"/>.
|
||||||
|
/// </summary>
|
||||||
|
Matrix4x4 ViewMatrix { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Projection <see cref="Matrix4x4"/> of the <see cref="ICamera"/>.
|
||||||
|
/// </summary>
|
||||||
|
Matrix4x4 ProjectionMatrix { get; }
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ namespace Engine.Core;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a 2D camera in the engine.
|
/// Represents a 2D camera in the engine.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface ICamera2D : IBehaviour2D
|
public interface ICamera2D : ICamera, IBehaviour2D
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The zoom level of the camera.
|
/// The zoom level of the camera.
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ namespace Engine.Core;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a 3D camera in the engine.
|
/// Represents a 3D camera in the engine.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface ICamera3D : IBehaviour3D
|
public interface ICamera3D : ICamera, IBehaviour3D
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event triggered when the near plane of the <see cref="ICamera3D"/> changes.
|
/// Event triggered when the near plane of the <see cref="ICamera3D"/> changes.
|
||||||
|
|||||||
@@ -7,31 +7,39 @@ namespace Engine.Integration.MonoGame;
|
|||||||
|
|
||||||
public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFrameUpdate, IPreDraw
|
public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFrameUpdate, IPreDraw
|
||||||
{
|
{
|
||||||
public Event<MonoGameCamera2D> OnMatrixTransformChanged { get; } = new();
|
public Event<MonoGameCamera2D> OnViewMatrixChanged { get; } = new();
|
||||||
|
public Event<MonoGameCamera2D> OnProjectionMatrixChanged { get; } = new();
|
||||||
public Event<MonoGameCamera2D> OnViewportChanged { get; } = new();
|
public Event<MonoGameCamera2D> OnViewportChanged { get; } = new();
|
||||||
public Event<MonoGameCamera2D> OnZoomChanged { get; } = new();
|
public Event<MonoGameCamera2D> OnZoomChanged { get; } = new();
|
||||||
|
|
||||||
public GraphicsDeviceManager Graphics { get; private set; } = null!;
|
public GraphicsDeviceManager Graphics { get; private set; } = null!;
|
||||||
public ITransform2D Transform { get; private set; } = null!;
|
public ITransform2D Transform { get; private set; } = null!;
|
||||||
|
|
||||||
public Matrix MatrixTransform
|
public Matrix4x4 ProjectionMatrix
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
set
|
private set
|
||||||
{
|
{
|
||||||
if (field == value)
|
if (field == value)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
field = value;
|
field = value;
|
||||||
OnMatrixTransformChanged.Invoke(this);
|
OnProjectionMatrixChanged.Invoke(this);
|
||||||
}
|
}
|
||||||
} = Matrix.Identity;
|
} = Matrix4x4.Identity;
|
||||||
|
|
||||||
public Vector2D Position
|
public Matrix4x4 ViewMatrix
|
||||||
{
|
{
|
||||||
get => Transform.Position;
|
get;
|
||||||
set => Transform.Position = value;
|
private set
|
||||||
}
|
{
|
||||||
|
if (field == value)
|
||||||
|
return;
|
||||||
|
|
||||||
|
field = value;
|
||||||
|
OnViewMatrixChanged.Invoke(this);
|
||||||
|
}
|
||||||
|
} = Matrix4x4.Identity;
|
||||||
|
|
||||||
public Viewport Viewport
|
public Viewport Viewport
|
||||||
{
|
{
|
||||||
@@ -61,21 +69,15 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
|
|||||||
}
|
}
|
||||||
} = 1f;
|
} = 1f;
|
||||||
|
|
||||||
public float Rotation
|
|
||||||
{
|
|
||||||
get => Transform.Rotation;
|
|
||||||
set => Transform.Rotation = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
Vector2D worldPosition = Vector2.Transform(screenPosition.ToVector2(), Matrix.Invert(MatrixTransform)).ToVector2D();
|
Vector2D worldPosition = Vector2.Transform(screenPosition.ToVector2(), ViewMatrix.Inverse.ToXnaMatrix()).ToVector2D();
|
||||||
return worldPosition.Scale(EngineConverterExtensions.screenScale);
|
return worldPosition.Scale(EngineConverterExtensions.screenScale);
|
||||||
}
|
}
|
||||||
public Vector2D WorldToScreenPosition(Vector2D worldPosition)
|
public Vector2D WorldToScreenPosition(Vector2D worldPosition)
|
||||||
{
|
{
|
||||||
Vector2D screenPosition = Vector2.Transform(worldPosition.ToVector2(), MatrixTransform).ToVector2D();
|
Vector2D screenPosition = Vector2.Transform(worldPosition.ToVector2(), ViewMatrix.ToXnaMatrix()).ToVector2D();
|
||||||
return screenPosition.Scale(EngineConverterExtensions.screenScale);
|
return screenPosition.Scale(EngineConverterExtensions.screenScale);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,11 +91,12 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
|
|||||||
|
|
||||||
public void PreDraw()
|
public void PreDraw()
|
||||||
{
|
{
|
||||||
MatrixTransform =
|
ProjectionMatrix = Matrix.CreateOrthographicOffCenter(Viewport.X, Viewport.Width, Viewport.Height, Viewport.Y, 0, 1).FromXnaMatrix();
|
||||||
Matrix.CreateTranslation(new Vector3(-Position.X, Position.Y, 0f)) *
|
ViewMatrix =
|
||||||
Matrix.CreateRotationZ(Rotation * Math.DegreeToRadian) *
|
Matrix4x4.CreateTranslation(new Vector3D(-Transform.Position.X, Transform.Position.Y, 0f)) *
|
||||||
Matrix.CreateScale(Transform.Scale.X.Max(Transform.Scale.Y)) *
|
Matrix4x4.CreateRotationZ(Transform.Rotation * Math.DegreeToRadian) *
|
||||||
Matrix.CreateScale(Zoom) *
|
Matrix4x4.CreateScale(Transform.Scale.X.Max(Transform.Scale.Y)) *
|
||||||
Matrix.CreateTranslation(new Vector3(Viewport.Width * .5f, Viewport.Height * .5f, 0f));
|
Matrix4x4.CreateScale(Zoom) *
|
||||||
|
Matrix4x4.CreateTranslation(new Vector3D(Viewport.Width * .5f, Viewport.Height * .5f, 0f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
|||||||
public GraphicsDeviceManager Graphics { get; private set; } = null!;
|
public GraphicsDeviceManager Graphics { get; private set; } = null!;
|
||||||
public ITransform3D Transform { get; private set; } = null!;
|
public ITransform3D Transform { get; private set; } = null!;
|
||||||
|
|
||||||
public Matrix View
|
public Matrix4x4 ViewMatrix
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
set
|
set
|
||||||
@@ -29,13 +29,13 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
|||||||
if (field == value)
|
if (field == value)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Matrix previousView = field;
|
Matrix4x4 previousView = field;
|
||||||
field = value;
|
field = value;
|
||||||
OnViewChanged.Invoke(this, new(previousView));
|
OnViewChanged.Invoke(this, new(previousView));
|
||||||
}
|
}
|
||||||
} = Matrix.Identity;
|
} = Matrix4x4.Identity;
|
||||||
|
|
||||||
public Matrix Projection
|
public Matrix4x4 ProjectionMatrix
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
set
|
set
|
||||||
@@ -43,11 +43,11 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
|||||||
if (field == value)
|
if (field == value)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Matrix previousProjection = field;
|
Matrix4x4 previousProjection = field;
|
||||||
field = value;
|
field = value;
|
||||||
OnProjectionChanged.Invoke(this, new(previousProjection));
|
OnProjectionChanged.Invoke(this, new(previousProjection));
|
||||||
}
|
}
|
||||||
} = Matrix.Identity;
|
} = Matrix4x4.Identity;
|
||||||
|
|
||||||
public Viewport Viewport
|
public Viewport Viewport
|
||||||
{
|
{
|
||||||
@@ -111,14 +111,17 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
|||||||
Vector3 nearPoint = new(screenPosition.X, screenPosition.Y, 0f);
|
Vector3 nearPoint = new(screenPosition.X, screenPosition.Y, 0f);
|
||||||
Vector3 farPoint = new(screenPosition.X, screenPosition.Y, 1f);
|
Vector3 farPoint = new(screenPosition.X, screenPosition.Y, 1f);
|
||||||
|
|
||||||
Vector3 worldNear = Viewport.Unproject(nearPoint, Projection, View, Matrix.Identity);
|
Matrix projection = ProjectionMatrix.ToXnaMatrix();
|
||||||
Vector3 worldFar = Viewport.Unproject(farPoint, Projection, View, Matrix.Identity);
|
Matrix view = ViewMatrix.ToXnaMatrix();
|
||||||
|
|
||||||
|
Vector3 worldNear = Viewport.Unproject(nearPoint, projection, view, Matrix.Identity);
|
||||||
|
Vector3 worldFar = 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(), Projection, View, Matrix.Identity).ToVector3D();
|
public Vector2D WorldToScreenPosition(Vector3D worldPosition) => 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()
|
||||||
@@ -150,7 +153,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
|||||||
Vector3 right = Vector3.Normalize(Transform.Right.ToVector3());
|
Vector3 right = Vector3.Normalize(Transform.Right.ToVector3());
|
||||||
Vector3 position = Transform.Position.ToVector3();
|
Vector3 position = Transform.Position.ToVector3();
|
||||||
|
|
||||||
View = new Matrix(
|
ViewMatrix = new Matrix4x4(
|
||||||
right.X, up.X, forward.X, 0,
|
right.X, up.X, forward.X, 0,
|
||||||
right.Y, up.Y, forward.Y, 0,
|
right.Y, up.Y, forward.Y, 0,
|
||||||
right.Z, up.Z, forward.Z, 0,
|
right.Z, up.Z, forward.Z, 0,
|
||||||
@@ -166,7 +169,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
|||||||
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.AspectRatio;
|
||||||
|
|
||||||
Projection = new Matrix(
|
ProjectionMatrix = new Matrix4x4(
|
||||||
xScale, 0, 0, 0,
|
xScale, 0, 0, 0,
|
||||||
0, yScale, 0, 0,
|
0, yScale, 0, 0,
|
||||||
0, 0, FarPlane / (FarPlane - NearPlane), 1,
|
0, 0, FarPlane / (FarPlane - NearPlane), 1,
|
||||||
@@ -174,7 +177,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly record struct ViewChangedArguments(Matrix PreviousView);
|
public readonly record struct ViewChangedArguments(Matrix4x4 PreviousView);
|
||||||
public readonly record struct ProjectionChangedArguments(Matrix PreviousProjection);
|
public readonly record struct ProjectionChangedArguments(Matrix4x4 PreviousProjection);
|
||||||
public readonly record struct ViewportChangedArguments(Viewport PreviousViewport);
|
public readonly record struct ViewportChangedArguments(Viewport PreviousViewport);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,14 @@ public static class EngineConverterExtensions
|
|||||||
m.M41, m.M42, m.M43, m.M44
|
m.M41, m.M42, m.M43, m.M44
|
||||||
);
|
);
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Matrix4x4 FromXnaMatrix(this Matrix m) => new(
|
||||||
|
m.M11, m.M12, m.M13, m.M14,
|
||||||
|
m.M21, m.M22, m.M23, m.M24,
|
||||||
|
m.M31, m.M32, m.M33, m.M34,
|
||||||
|
m.M41, m.M42, m.M43, m.M44
|
||||||
|
);
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Microsoft.Xna.Framework.Quaternion ToXnaQuaternion(this Core.Quaternion quaternion) => new(quaternion.X, quaternion.Y, quaternion.Z, quaternion.W);
|
public static Microsoft.Xna.Framework.Quaternion ToXnaQuaternion(this Core.Quaternion quaternion) => new(quaternion.X, quaternion.Y, quaternion.Z, quaternion.W);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user