Compare commits
6 Commits
main
...
985f898327
| Author | SHA1 | Date | |
|---|---|---|---|
| 985f898327 | |||
| 7c9973a5e7 | |||
| af6dde84fd | |||
| e2820670c6 | |||
| 5ce5e4eb0b | |||
| ee58e60ef1 |
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>
|
||||
/// Represents a 2D camera in the engine.
|
||||
/// </summary>
|
||||
public interface ICamera2D : IBehaviour2D
|
||||
public interface ICamera2D : ICamera, IBehaviour2D
|
||||
{
|
||||
/// <summary>
|
||||
/// The zoom level of the camera.
|
||||
|
||||
@@ -3,7 +3,7 @@ namespace Engine.Core;
|
||||
/// <summary>
|
||||
/// Represents a 3D camera in the engine.
|
||||
/// </summary>
|
||||
public interface ICamera3D : IBehaviour3D
|
||||
public interface ICamera3D : ICamera, IBehaviour3D
|
||||
{
|
||||
/// <summary>
|
||||
/// Event triggered when the near plane of the <see cref="ICamera3D"/> changes.
|
||||
|
||||
@@ -62,6 +62,11 @@ public readonly struct Matrix4x4(
|
||||
0f, 0f, 0f, 1f
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Represents the inverted version of this <see cref="Matrix4x4"/>.
|
||||
/// </summary>
|
||||
public Matrix4x4 Inverse => Invert(this);
|
||||
|
||||
public static Matrix4x4 operator *(Matrix4x4 a, Matrix4x4 b) => new(
|
||||
a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31 + a.M14 * b.M41,
|
||||
a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32 + a.M14 * b.M42,
|
||||
@@ -129,6 +134,74 @@ public readonly struct Matrix4x4(
|
||||
m.M13 * m.M21 * m.M32 * m.M44 - m.M11 * m.M23 * m.M32 * m.M44 -
|
||||
m.M12 * m.M21 * m.M33 * m.M44 + m.M11 * m.M22 * m.M33 * m.M44;
|
||||
|
||||
/// <summary>
|
||||
/// Inverts the given <see cref="Matrix4x4"/>.
|
||||
/// </summary>
|
||||
/// <param name="m">The <see cref="Matrix4x4"/>.</param>
|
||||
/// <returns>The inverted <see cref="Matrix4x4"/> of the given <see cref="Matrix4x4"/>.</returns>
|
||||
public static Matrix4x4 Invert(Matrix4x4 m)
|
||||
{
|
||||
float m1 = m.M11, m2 = m.M12, m3 = m.M13, m4 = m.M14;
|
||||
float m5 = m.M21, m6 = m.M22, m7 = m.M23, m8 = m.M24;
|
||||
float m9 = m.M31, m10 = m.M32, m11 = m.M33, m12 = m.M34;
|
||||
float m13 = m.M41, m14 = m.M42, m15 = m.M43, m16 = m.M44;
|
||||
|
||||
float num = m11 * m16 - m12 * m15;
|
||||
float num2 = m10 * m16 - m12 * m14;
|
||||
float num3 = m10 * m15 - m11 * m14;
|
||||
float num4 = m9 * m16 - m12 * m13;
|
||||
float num5 = m9 * m15 - m11 * m13;
|
||||
float num6 = m9 * m14 - m10 * m13;
|
||||
|
||||
float num7 = m6 * num - m7 * num2 + m8 * num3;
|
||||
float num8 = -(m5 * num - m7 * num4 + m8 * num5);
|
||||
float num9 = m5 * num2 - m6 * num4 + m8 * num6;
|
||||
float num10 = -(m5 * num3 - m6 * num5 + m7 * num6);
|
||||
|
||||
float invDet = 1f / (m1 * num7 + m2 * num8 + m3 * num9 + m4 * num10);
|
||||
|
||||
float r11 = num7 * invDet;
|
||||
float r21 = num8 * invDet;
|
||||
float r31 = num9 * invDet;
|
||||
float r41 = num10 * invDet;
|
||||
|
||||
float r12 = (-(m2 * num - m3 * num2 + m4 * num3)) * invDet;
|
||||
float r22 = (m1 * num - m3 * num4 + m4 * num5) * invDet;
|
||||
float r32 = (-(m1 * num2 - m2 * num4 + m4 * num6)) * invDet;
|
||||
float r42 = (m1 * num3 - m2 * num5 + m3 * num6) * invDet;
|
||||
|
||||
float num12 = m7 * m16 - m8 * m15;
|
||||
float num13 = m6 * m16 - m8 * m14;
|
||||
float num14 = m6 * m15 - m7 * m14;
|
||||
float num15 = m5 * m16 - m8 * m13;
|
||||
float num16 = m5 * m15 - m7 * m13;
|
||||
float num17 = m5 * m14 - m6 * m13;
|
||||
|
||||
float r13 = (m2 * num12 - m3 * num13 + m4 * num14) * invDet;
|
||||
float r23 = (-(m1 * num12 - m3 * num15 + m4 * num16)) * invDet;
|
||||
float r33 = (m1 * num13 - m2 * num15 + m4 * num17) * invDet;
|
||||
float r43 = (-(m1 * num14 - m2 * num16 + m3 * num17)) * invDet;
|
||||
|
||||
float num18 = m7 * m12 - m8 * m11;
|
||||
float num19 = m6 * m12 - m8 * m10;
|
||||
float num20 = m6 * m11 - m7 * m10;
|
||||
float num21 = m5 * m12 - m8 * m9;
|
||||
float num22 = m5 * m11 - m7 * m9;
|
||||
float num23 = m5 * m10 - m6 * m9;
|
||||
|
||||
float r14 = (-(m2 * num18 - m3 * num19 + m4 * num20)) * invDet;
|
||||
float r24 = (m1 * num18 - m3 * num21 + m4 * num22) * invDet;
|
||||
float r34 = (-(m1 * num19 - m2 * num21 + m4 * num23)) * invDet;
|
||||
float r44 = (m1 * num20 - m2 * num22 + m3 * num23) * invDet;
|
||||
|
||||
return new(
|
||||
r11, r12, r13, r14,
|
||||
r21, r22, r23, r24,
|
||||
r31, r32, r33, r34,
|
||||
r41, r42, r43, r44
|
||||
);
|
||||
}
|
||||
|
||||
public static Matrix4x4 CreateTranslation(Vector3D position) => new(
|
||||
1f, 0f, 0f, 0f,
|
||||
0f, 1f, 0f, 0f,
|
||||
@@ -136,6 +209,13 @@ public readonly struct Matrix4x4(
|
||||
position.X, position.Y, position.Z, 1
|
||||
);
|
||||
|
||||
public static Matrix4x4 CreateScale(float scale) => new(
|
||||
scale, 0f, 0f, 0f,
|
||||
0f, scale, 0f, 0f,
|
||||
0f, 0f, scale, 0f,
|
||||
0f, 0f, 0f, 1f
|
||||
);
|
||||
|
||||
public static Matrix4x4 CreateScale(Vector3D scale) => new(
|
||||
scale.X, 0f, 0f, 0f,
|
||||
0f, scale.Y, 0f, 0f,
|
||||
@@ -261,12 +341,18 @@ public static class Matrix4x4Extensions
|
||||
/// <inheritdoc cref="Matrix4x4.Determinant(Matrix4x4)" />
|
||||
public static float Determinant(this Matrix4x4 matrix) => Matrix4x4.Determinant(matrix);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.Invert(Matrix4x4)" />
|
||||
public static Matrix4x4 Invert(this Matrix4x4 matrix) => Matrix4x4.Invert(matrix);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateTranslation(Vector3D)" />
|
||||
public static Matrix4x4 ApplyTranslation(this Matrix4x4 matrix, Vector3D translation) => matrix * Matrix4x4.CreateTranslation(translation);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateScale(Vector3D)" />
|
||||
public static Matrix4x4 ApplyScale(this Matrix4x4 matrix, Vector3 scale) => matrix * Matrix4x4.CreateScale(scale);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateScale(float)" />
|
||||
public static Matrix4x4 ApplyScale(this Matrix4x4 matrix, float scale) => matrix * Matrix4x4.CreateScale(scale);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateRotationZ(float)" />
|
||||
public static Matrix4x4 ApplyRotationX(this Matrix4x4 matrix, float radians) => matrix * Matrix4x4.CreateRotationX(radians);
|
||||
|
||||
|
||||
@@ -9,65 +9,56 @@ public class UniverseEntranceManager : Internal.BehaviourIndependent
|
||||
private static System.Func<IBehaviour, int> GetPriority() => (b) => b.Priority;
|
||||
|
||||
private readonly ActiveBehaviourCollectorOrdered<int, IEnterUniverse> enterUniverses = new(GetPriority(), SortByAscendingPriority());
|
||||
private readonly ActiveBehaviourCollectorOrdered<int, IExitUniverse> exitUniverses = new(GetPriority(), SortByAscendingPriority());
|
||||
|
||||
private readonly List<IEnterUniverse> toCallEnterUniverses = new(32);
|
||||
private readonly List<IExitUniverse> toCallExitUniverses = new(32);
|
||||
private bool isInitialCollectionDone = false;
|
||||
private readonly FastListOrdered<int, IEnterUniverse> toCallEnterUniverses = new(GetPriority(), SortByAscendingPriority());
|
||||
|
||||
protected override void OnEnteredUniverse(IUniverse universe)
|
||||
{
|
||||
// FIXME: This causes an issue when the UniverseEntranceManager is already attached to a UniverseObject then registered into a Universe,
|
||||
// the enter/exit universe collectors call OnUniverseObjectRegistered internally on Assign, but since the Universe calls the OnUniverseObjectRegistered
|
||||
// event it tries to call OnUniverseObjectRegistered again on the same object, causing a duplicate entry error.
|
||||
Debug.Assert.AssertTrue(BehaviourController.Count == 1, $"{nameof(UniverseEntranceManager)} must be in it's own {nameof(IUniverseObject)} with no other {nameof(IBehaviour)}s attached at the moment. Failing to do so might cause instantiation or serialization issues.");
|
||||
exitUniverses.Assign(universe);
|
||||
|
||||
// FIXME: the isInitialCollectionDone is for the sole reason of some behaviours
|
||||
// adding more behaviours during entrance calls and the internal workings of
|
||||
// behaviour collector not being able to tell which behaviour was already called
|
||||
// (because it just runs a for loop with the behaviour count, and priority ordering doesn't help as well)
|
||||
// so it sometimes double processes or misses behaviours. A more elegant way of
|
||||
// handling this would be nice but for now it works good enough.
|
||||
//
|
||||
// SIDE NOTE: This same issue has the potential to occur on exitUniverses as well, but I've yet to run
|
||||
// into an instance of it actually happening so... I'm not gonna touch it until the edge case happens.
|
||||
isInitialCollectionDone = false;
|
||||
enterUniverses.Assign(universe);
|
||||
isInitialCollectionDone = true;
|
||||
|
||||
foreach (IUniverseObject universeObject in universe)
|
||||
OnUniverseObjectRegistered(universe, new(universeObject));
|
||||
|
||||
universe.OnUniverseObjectRegistered.AddListener(OnUniverseObjectRegistered);
|
||||
universe.OnUniverseObjectUnRegistered.AddListener(OnUniverseObjectUnRegistered);
|
||||
for (int i = toCallEnterUniverses.Count - 1; i >= 0; i--)
|
||||
toCallEnterUniverses[i].EnterUniverse(universe);
|
||||
toCallEnterUniverses.Clear();
|
||||
}
|
||||
|
||||
protected override void OnExitedUniverse(IUniverse universe)
|
||||
{
|
||||
enterUniverses.Unassign();
|
||||
|
||||
foreach (IUniverseObject universeObject in universe)
|
||||
OnUniverseObjectUnRegistered(universe, new(universeObject));
|
||||
|
||||
universe.OnUniverseObjectRegistered.RemoveListener(OnUniverseObjectRegistered);
|
||||
universe.OnUniverseObjectUnRegistered.RemoveListener(OnUniverseObjectUnRegistered);
|
||||
}
|
||||
|
||||
private void OnUniverseObjectUnRegistered(IUniverse sender, IUniverse.UniverseObjectUnRegisteredArguments args)
|
||||
{
|
||||
args.UniverseObjectUnregistered.BehaviourController.GetBehavioursInChildren(toCallExitUniverses);
|
||||
|
||||
for (int i = toCallExitUniverses.Count - 1; i >= 0; i--)
|
||||
{
|
||||
IExitUniverse exitUniverse = toCallExitUniverses[i];
|
||||
toCallExitUniverses.RemoveAt(i);
|
||||
exitUniverse.ExitUniverse(Universe);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnUniverseObjectRegistered(IUniverse sender, IUniverse.UniverseObjectRegisteredArguments args)
|
||||
{
|
||||
for (int i = toCallEnterUniverses.Count - 1; i >= 0; i--)
|
||||
{
|
||||
IEnterUniverse enterUniverse = toCallEnterUniverses[i];
|
||||
toCallEnterUniverses.RemoveAt(i);
|
||||
enterUniverse.EnterUniverse(Universe);
|
||||
}
|
||||
exitUniverses.Unassign();
|
||||
}
|
||||
|
||||
private void OnEnterUniverseCollected(IBehaviourCollector<IEnterUniverse> sender, IBehaviourCollector<IEnterUniverse>.BehaviourCollectedArguments args)
|
||||
{
|
||||
if (!isInitialCollectionDone)
|
||||
{
|
||||
toCallEnterUniverses.Add(args.BehaviourCollected);
|
||||
return;
|
||||
}
|
||||
|
||||
args.BehaviourCollected.EnterUniverse(Universe);
|
||||
}
|
||||
|
||||
private void OnExitUniverseRemoved(IBehaviourCollector<IExitUniverse> sender, IBehaviourCollector<IExitUniverse>.BehaviourRemovedArguments args)
|
||||
=> args.BehaviourRemoved.ExitUniverse(Universe);
|
||||
|
||||
public UniverseEntranceManager()
|
||||
{
|
||||
enterUniverses.OnCollected.AddListener(OnEnterUniverseCollected);
|
||||
exitUniverses.OnRemoved.AddListener(OnExitUniverseRemoved);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
using Engine.Core;
|
||||
|
||||
namespace Engine.Integration.MonoGame;
|
||||
|
||||
public interface ITriangleBatch
|
||||
{
|
||||
void Begin(Matrix? view = null, Matrix? projection = null);
|
||||
void Draw(Triangle triangle, ColorRGBA colorRGBA);
|
||||
void End();
|
||||
}
|
||||
@@ -7,31 +7,39 @@ namespace Engine.Integration.MonoGame;
|
||||
|
||||
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> OnZoomChanged { get; } = new();
|
||||
|
||||
public GraphicsDeviceManager Graphics { get; private set; } = null!;
|
||||
public ITransform2D Transform { get; private set; } = null!;
|
||||
|
||||
public Matrix MatrixTransform
|
||||
public Matrix4x4 ProjectionMatrix
|
||||
{
|
||||
get;
|
||||
set
|
||||
private set
|
||||
{
|
||||
if (field == value)
|
||||
return;
|
||||
|
||||
field = value;
|
||||
OnMatrixTransformChanged.Invoke(this);
|
||||
OnProjectionMatrixChanged.Invoke(this);
|
||||
}
|
||||
} = Matrix.Identity;
|
||||
} = Matrix4x4.Identity;
|
||||
|
||||
public Vector2D Position
|
||||
public Matrix4x4 ViewMatrix
|
||||
{
|
||||
get => Transform.Position;
|
||||
set => Transform.Position = value;
|
||||
get;
|
||||
private set
|
||||
{
|
||||
if (field == value)
|
||||
return;
|
||||
|
||||
field = value;
|
||||
OnViewMatrixChanged.Invoke(this);
|
||||
}
|
||||
} = Matrix4x4.Identity;
|
||||
|
||||
public Viewport Viewport
|
||||
{
|
||||
@@ -61,21 +69,15 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
|
||||
}
|
||||
} = 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
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -89,11 +91,12 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
|
||||
|
||||
public void PreDraw()
|
||||
{
|
||||
MatrixTransform =
|
||||
Matrix.CreateTranslation(new Vector3(-Position.X, Position.Y, 0f)) *
|
||||
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));
|
||||
ProjectionMatrix = Matrix.CreateOrthographicOffCenter(Viewport.X, Viewport.Width, Viewport.Height, Viewport.Y, 0, 1).FromXnaMatrix();
|
||||
ViewMatrix =
|
||||
Matrix4x4.CreateTranslation(new Vector3D(-Transform.Position.X, Transform.Position.Y, 0f)) *
|
||||
Matrix4x4.CreateRotationZ(Transform.Rotation * Math.DegreeToRadian) *
|
||||
Matrix4x4.CreateScale(Transform.Scale.X.Max(Transform.Scale.Y)) *
|
||||
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 ITransform3D Transform { get; private set; } = null!;
|
||||
|
||||
public Matrix View
|
||||
public Matrix4x4 ViewMatrix
|
||||
{
|
||||
get;
|
||||
set
|
||||
@@ -29,13 +29,13 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
||||
if (field == value)
|
||||
return;
|
||||
|
||||
Matrix previousView = field;
|
||||
Matrix4x4 previousView = field;
|
||||
field = value;
|
||||
OnViewChanged.Invoke(this, new(previousView));
|
||||
}
|
||||
} = Matrix.Identity;
|
||||
} = Matrix4x4.Identity;
|
||||
|
||||
public Matrix Projection
|
||||
public Matrix4x4 ProjectionMatrix
|
||||
{
|
||||
get;
|
||||
set
|
||||
@@ -43,11 +43,11 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
||||
if (field == value)
|
||||
return;
|
||||
|
||||
Matrix previousProjection = field;
|
||||
Matrix4x4 previousProjection = field;
|
||||
field = value;
|
||||
OnProjectionChanged.Invoke(this, new(previousProjection));
|
||||
}
|
||||
} = Matrix.Identity;
|
||||
} = Matrix4x4.Identity;
|
||||
|
||||
public Viewport Viewport
|
||||
{
|
||||
@@ -111,14 +111,17 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
||||
Vector3 nearPoint = new(screenPosition.X, screenPosition.Y, 0f);
|
||||
Vector3 farPoint = new(screenPosition.X, screenPosition.Y, 1f);
|
||||
|
||||
Vector3 worldNear = Viewport.Unproject(nearPoint, Projection, View, Matrix.Identity);
|
||||
Vector3 worldFar = Viewport.Unproject(farPoint, Projection, View, Matrix.Identity);
|
||||
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 direction = Vector3.Normalize(worldFar - worldNear);
|
||||
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 FirstActiveFrame()
|
||||
@@ -150,7 +153,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
||||
Vector3 right = Vector3.Normalize(Transform.Right.ToVector3());
|
||||
Vector3 position = Transform.Position.ToVector3();
|
||||
|
||||
View = new Matrix(
|
||||
ViewMatrix = new Matrix4x4(
|
||||
right.X, up.X, forward.X, 0,
|
||||
right.Y, up.Y, forward.Y, 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 xScale = yScale / Viewport.AspectRatio;
|
||||
|
||||
Projection = new Matrix(
|
||||
ProjectionMatrix = new Matrix4x4(
|
||||
xScale, 0, 0, 0,
|
||||
0, yScale, 0, 0,
|
||||
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 ProjectionChangedArguments(Matrix PreviousProjection);
|
||||
public readonly record struct ViewChangedArguments(Matrix4x4 PreviousView);
|
||||
public readonly record struct ProjectionChangedArguments(Matrix4x4 PreviousProjection);
|
||||
public readonly record struct ViewportChangedArguments(Viewport PreviousViewport);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class SpriteBatcher : Behaviour, IFirstFrameUpdate, IDraw
|
||||
|
||||
public void Draw()
|
||||
{
|
||||
spriteBatch.Begin(transformMatrix: camera2D.MatrixTransform);
|
||||
spriteBatch.Begin(transformMatrix: camera2D.ViewMatrix.ToXnaMatrix());
|
||||
for (int i = 0; i < drawableSprites.Count; i++)
|
||||
drawableSprites[i].Draw(spriteBatch);
|
||||
spriteBatch.End();
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
using Engine.Core;
|
||||
|
||||
namespace Engine.Integration.MonoGame;
|
||||
|
||||
public class TriangleBatcher : Behaviour, ITriangleBatch, IFirstFrameUpdate, IDraw
|
||||
{
|
||||
private static Comparer<int> SortByAscendingPriority() => Comparer<int>.Create((x, y) => x.CompareTo(y));
|
||||
private static System.Func<IBehaviour, int> GetPriority() => (b) => b.Priority;
|
||||
|
||||
private TriangleBatch triangleBatch = null!;
|
||||
private MonoGameCamera2D camera2D = null!;
|
||||
|
||||
private readonly ActiveBehaviourCollectorOrdered<int, IDrawableTriangle> drawableShapes = new(GetPriority(), SortByAscendingPriority());
|
||||
|
||||
public void FirstActiveFrame()
|
||||
{
|
||||
MonoGameWindowContainer windowContainer = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>();
|
||||
camera2D = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameCamera2D>();
|
||||
|
||||
triangleBatch = new(windowContainer.Window.GraphicsDevice);
|
||||
drawableShapes.Unassign();
|
||||
drawableShapes.Assign(BehaviourController.UniverseObject.Universe);
|
||||
}
|
||||
|
||||
public void Draw()
|
||||
{
|
||||
triangleBatch.Begin(camera2D.MatrixTransform);
|
||||
for (int i = 0; i < drawableShapes.Count; i++)
|
||||
drawableShapes[i].Draw(triangleBatch);
|
||||
triangleBatch.End();
|
||||
}
|
||||
|
||||
public void Begin(Matrix? view = null, Matrix? projection = null) => triangleBatch.Begin(view, projection);
|
||||
public void Draw(Triangle triangle, ColorRGBA colorRGBA) => triangleBatch.Draw(triangle, colorRGBA);
|
||||
public void End() => triangleBatch.End();
|
||||
}
|
||||
@@ -49,6 +49,14 @@ public static class EngineConverterExtensions
|
||||
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)]
|
||||
public static Microsoft.Xna.Framework.Quaternion ToXnaQuaternion(this Core.Quaternion quaternion) => new(quaternion.X, quaternion.Y, quaternion.Z, quaternion.W);
|
||||
|
||||
|
||||
@@ -2,24 +2,26 @@ using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
using Engine.Core;
|
||||
using Engine.Systems.Graphics;
|
||||
|
||||
namespace Engine.Integration.MonoGame;
|
||||
|
||||
public class TriangleBatch : ITriangleBatch
|
||||
public class MonoGameTriangleBatch : Behaviour, ITriangleBatch, IFirstFrameUpdate
|
||||
{
|
||||
private readonly GraphicsDevice graphicsDevice;
|
||||
private readonly VertexBuffer vertexBuffer = default!;
|
||||
private GraphicsDevice graphicsDevice = null!;
|
||||
private VertexBuffer vertexBuffer = default!;
|
||||
private readonly VertexPositionColor[] vertices = new VertexPositionColor[1024];
|
||||
|
||||
private int verticesIndex = 0;
|
||||
private Matrix view = Matrix.Identity;
|
||||
private Matrix projection = Matrix.Identity;
|
||||
|
||||
private readonly BasicEffect basicEffect = null!;
|
||||
private BasicEffect basicEffect = null!;
|
||||
private readonly RasterizerState rasterizerState = new() { CullMode = CullMode.None };
|
||||
|
||||
public TriangleBatch(GraphicsDevice graphicsDevice)
|
||||
public void FirstActiveFrame()
|
||||
{
|
||||
GraphicsDevice graphicsDevice = Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.GraphicsDevice;
|
||||
this.graphicsDevice = graphicsDevice;
|
||||
basicEffect = new(graphicsDevice);
|
||||
basicEffect.VertexColorEnabled = true;
|
||||
@@ -41,15 +43,15 @@ public class TriangleBatch : ITriangleBatch
|
||||
vertices[verticesIndex++] = new(new(C.X, C.Y, 0f), color);
|
||||
}
|
||||
|
||||
public void Begin(Matrix? view = null, Matrix? projection = null)
|
||||
public void Begin(Matrix4x4? view = null, Matrix4x4? projection = null)
|
||||
{
|
||||
if (view != null)
|
||||
this.view = view.Value;
|
||||
this.view = view.Value.ToXnaMatrix();
|
||||
else
|
||||
this.view = Matrix.Identity;
|
||||
|
||||
if (projection != null)
|
||||
this.projection = projection.Value;
|
||||
this.projection = projection.Value.ToXnaMatrix();
|
||||
else
|
||||
{
|
||||
Viewport viewport = graphicsDevice.Viewport;
|
||||
@@ -2,9 +2,9 @@ using System.Collections.Generic;
|
||||
|
||||
using Engine.Core;
|
||||
|
||||
namespace Engine.Integration.MonoGame;
|
||||
namespace Engine.Systems.Graphics;
|
||||
|
||||
public class DrawableShape : Behaviour2D, IDrawableTriangle, IPreDraw
|
||||
public class DrawableShape2D : Behaviour2D, IDrawableTriangle, IPreDraw
|
||||
{
|
||||
private readonly Shape2D shape = new([]);
|
||||
private readonly List<Triangle> worldTriangles = [];
|
||||
@@ -23,7 +23,7 @@ public class DrawableShape : Behaviour2D, IDrawableTriangle, IPreDraw
|
||||
|
||||
protected void UpdateWorldShape() => shape.Transform(Transform, worldShape);
|
||||
|
||||
public DrawableShape() => shape = Shape2D.Triangle;
|
||||
public DrawableShape(Shape2D shape) => this.shape = shape;
|
||||
public DrawableShape(Shape2D shape, ColorRGB color) { this.shape = shape; this.color = color; }
|
||||
public DrawableShape2D() => shape = Shape2D.Triangle;
|
||||
public DrawableShape2D(Shape2D shape) => this.shape = shape;
|
||||
public DrawableShape2D(Shape2D shape, ColorRGB color) { this.shape = shape; this.color = color; }
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using Engine.Core;
|
||||
|
||||
namespace Engine.Integration.MonoGame;
|
||||
namespace Engine.Systems.Graphics;
|
||||
|
||||
public interface IDrawableTriangle : IBehaviour
|
||||
{
|
||||
10
Engine.Systems/Graphics/ITriangleBatch.cs
Normal file
10
Engine.Systems/Graphics/ITriangleBatch.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Engine.Core;
|
||||
|
||||
namespace Engine.Systems.Graphics;
|
||||
|
||||
public interface ITriangleBatch
|
||||
{
|
||||
void Begin(Matrix4x4? view = null, Matrix4x4? projection = null);
|
||||
void Draw(Triangle triangle, ColorRGBA colorRGBA);
|
||||
void End();
|
||||
}
|
||||
33
Engine.Systems/Graphics/TriangleBatcher.cs
Normal file
33
Engine.Systems/Graphics/TriangleBatcher.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Engine.Core;
|
||||
|
||||
namespace Engine.Systems.Graphics;
|
||||
|
||||
public class TriangleBatcher : Behaviour, IFirstFrameUpdate, IDraw
|
||||
{
|
||||
private static Comparer<int> SortByAscendingPriority() => Comparer<int>.Create((x, y) => x.CompareTo(y));
|
||||
private static System.Func<IBehaviour, int> GetPriority() => (b) => b.Priority;
|
||||
|
||||
private ITriangleBatch triangleBatch = null!;
|
||||
private ICamera camera = null!;
|
||||
|
||||
private readonly ActiveBehaviourCollectorOrdered<int, IDrawableTriangle> drawableShapes = new(GetPriority(), SortByAscendingPriority());
|
||||
|
||||
public void FirstActiveFrame()
|
||||
{
|
||||
camera = Universe.FindRequiredBehaviour<ICamera>();
|
||||
|
||||
triangleBatch = Universe.FindRequiredBehaviour<ITriangleBatch>();
|
||||
drawableShapes.Unassign();
|
||||
drawableShapes.Assign(Universe);
|
||||
}
|
||||
|
||||
public void Draw()
|
||||
{
|
||||
triangleBatch.Begin(camera.ViewMatrix, camera.ProjectionMatrix);
|
||||
for (int i = 0; i < drawableShapes.Count; i++)
|
||||
drawableShapes[i].Draw(triangleBatch);
|
||||
triangleBatch.End();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user