You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.7 KiB
82 lines
2.7 KiB
2 years ago
|
using System;
|
||
|
using gaemstone.ECS;
|
||
|
using Silk.NET.Input;
|
||
|
using Silk.NET.Maths;
|
||
|
using static gaemstone.Client.Input;
|
||
|
|
||
|
namespace gaemstone.Client;
|
||
|
|
||
|
[Module]
|
||
|
[DependsOn(typeof(Input))]
|
||
|
public class CameraModule
|
||
|
{
|
||
|
[Component]
|
||
|
public struct Camera
|
||
|
{
|
||
|
public static readonly Camera Default2D = Create2D();
|
||
|
public static readonly Camera Default3D = Create3D(80.0F);
|
||
|
|
||
|
public static Camera Create2D(float nearPlane = -100.0F, float farPlane = 100.0F)
|
||
|
=> new() { NearPlane = nearPlane, FarPlane = farPlane };
|
||
|
public static Camera Create3D(float fieldOfView, float nearPlane = 0.1F, float farPlane = 200.0F)
|
||
|
=> new() { FieldOfView = fieldOfView, NearPlane = nearPlane, FarPlane = farPlane };
|
||
|
|
||
|
public float FieldOfView { get; set; }
|
||
|
public float NearPlane { get; set; }
|
||
|
public float FarPlane { get; set; }
|
||
|
|
||
|
public bool IsOrthographic => (FieldOfView == 0.0F);
|
||
|
}
|
||
|
|
||
|
[Component]
|
||
|
public struct CameraViewport
|
||
|
{
|
||
|
public Vector4D<byte> ClearColor { get; set; }
|
||
|
public Rectangle<int> Viewport { get; set; }
|
||
|
}
|
||
|
|
||
|
[Component]
|
||
|
public struct CameraController
|
||
|
{
|
||
|
public float MouseSensitivity { get; set; }
|
||
|
public Vector2D<float>? MouseGrabbedAt { get; set; }
|
||
|
}
|
||
|
|
||
|
[System]
|
||
|
public static void UpdateCamera(TimeSpan delta, in Camera camera,
|
||
|
ref GlobalTransform transform, ref CameraController controller,
|
||
|
[Source(typeof(Game))] RawInput input)
|
||
|
{
|
||
|
var isMouseDown = input.IsDown(MouseButton.Right);
|
||
|
var isMouseGrabbed = controller.MouseGrabbedAt != null;
|
||
|
if (isMouseDown != isMouseGrabbed) {
|
||
|
if (isMouseDown) controller.MouseGrabbedAt = input.MousePosition;
|
||
|
else controller.MouseGrabbedAt = null;
|
||
|
}
|
||
|
|
||
|
if (controller.MouseGrabbedAt is not Vector2D<float> pos) return;
|
||
|
|
||
|
var mouseMoved = input.MousePosition - pos;
|
||
|
input.Context!.Mice[0].Position = pos.ToSystem();
|
||
|
|
||
|
var dt = (float)delta.TotalSeconds;
|
||
|
var xMovement = mouseMoved.X * dt * controller.MouseSensitivity;
|
||
|
var yMovement = mouseMoved.Y * dt * controller.MouseSensitivity;
|
||
|
|
||
|
if (camera.IsOrthographic) {
|
||
|
transform *= Matrix4X4.CreateTranslation(-xMovement, -yMovement, 0);
|
||
|
} else {
|
||
|
var speed = dt * (input.IsDown(Key.ShiftLeft) ? 12 : 4);
|
||
|
var forwardMovement = ((input.IsDown(Key.W) ? -1 : 0) + (input.IsDown(Key.S) ? 1 : 0)) * speed;
|
||
|
var sideMovement = ((input.IsDown(Key.A) ? -1 : 0) + (input.IsDown(Key.D) ? 1 : 0)) * speed;
|
||
|
|
||
|
var curTranslation = new Vector3D<float>(transform.Value.M41, transform.Value.M42, transform.Value.M43);
|
||
|
var yawRotation = Matrix4X4.CreateRotationY(-xMovement / 100, curTranslation);
|
||
|
var pitchRotation = Matrix4X4.CreateRotationX(-yMovement / 100);
|
||
|
var translation = Matrix4X4.CreateTranslation(sideMovement, 0, forwardMovement);
|
||
|
|
||
|
transform = translation * pitchRotation * transform * yawRotation;
|
||
|
}
|
||
|
}
|
||
|
}
|