refactor: ICamera interface added with view and projection matrices fields
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user