Many ecs, component and editor changes

This commit is contained in:
Simon Lübeß
2022-03-02 01:59:51 +01:00
parent afe36b19df
commit cbee9f59d8
10 changed files with 243 additions and 31 deletions
+92
View File
@@ -1,4 +1,6 @@
using GlitchyEngine.Math;
using GlitchyEngine.Renderer;
using System;
namespace GlitchyEngine.World
{
@@ -31,4 +33,94 @@ namespace GlitchyEngine.World
Color = color;
}
}
struct SceneCamera : Camera
{
public enum ProjectionType
{
case Perspective(float FovY, float NearPlane, float FarPlane);
case InfinitePerspective(float FovY, float NearPlane);
case Orthographic(float Height, float NearPlane, float FarPlane);
}
private float _aspectRatio = 16.0f / 9.0f;
private ProjectionType _projectionType = .InfinitePerspective(MathHelper.ToRadians(75f), 0.1f);
public ProjectionType ProjectionType
{
get => _projectionType;
set mut
{
_projectionType = value;
CalculateProjection();
}
}
private const Matrix mat = Matrix.InfinitePerspectiveProjection(MathHelper.ToRadians(75f), 1.0f, 0.1f);
public this()
{
CalculateProjection();
}
public void SetOrthographic(float height, float nearPlane, float farPlane) mut
{
_projectionType = .Orthographic(height, nearPlane, farPlane);
CalculateProjection();
}
public void SetPerspective(float fovY, float nearPlane, float farPlane) mut
{
_projectionType = .Perspective(fovY, nearPlane, farPlane);
CalculateProjection();
}
public void SetInfinitePerspective(float fovY, float nearPlane) mut
{
_projectionType = .InfinitePerspective(fovY, nearPlane);
CalculateProjection();
}
public void SetViewportSize(uint32 width, uint32 height) mut
{
_aspectRatio = (float)width / (float)height;
CalculateProjection();
}
private void CalculateProjection() mut
{
if (_projectionType case .Orthographic(let nearPlane, let farPlane, let projectionHeight))
{
float halfHeight = projectionHeight / 2.0f;
float halfWidth = halfHeight / _aspectRatio;
_projection = Matrix.OrthographicProjectionOffCenter(-halfWidth, halfWidth, halfHeight, -halfHeight, nearPlane, farPlane);
}
else if (_projectionType case .Perspective(let nearPlane, let farPlane, let fovY))
{
_projection = Matrix.PerspectiveProjection(fovY, _aspectRatio, nearPlane, farPlane);
}
else if (_projectionType case .InfinitePerspective(let nearPlane, let fovY))
{
_projection = Matrix.InfinitePerspectiveProjection(fovY, _aspectRatio, nearPlane);
}
}
}
struct CameraComponent
{
public SceneCamera Camera;
public bool Primary = true; // Todo: probably move into scene
public bool FixedAspectRatio = false;
public this()
{
Camera = .();
}
}
}
+40 -4
View File
@@ -22,11 +22,30 @@ namespace GlitchyEngine.World
{
//TransformSystem.Update(_ecsWorld);
for (var (entity, transform, sprite) in _ecsWorld.Enumerate<SimpleTransformComponent, SpriterRendererComponent>())
{
Renderer2D.DrawQuad(transform.Transform, sprite.Color);
}
Camera* primaryCamera = null;
Matrix* primaryCameraTransform = null;
for (var (entity, transform, camera) in _ecsWorld.Enumerate<SimpleTransformComponent, CameraComponent>())
{
if (camera.Primary)
{
primaryCamera = &camera.Camera;
primaryCameraTransform = &transform.Transform;
}
}
// Sprite renderer
if (primaryCamera != null)
{
Renderer2D.BeginScene(*primaryCamera, *primaryCameraTransform);
for (var (entity, transform, sprite) in _ecsWorld.Enumerate<SimpleTransformComponent, SpriterRendererComponent>())
{
Renderer2D.DrawQuad(transform.Transform, sprite.Color);
}
Renderer2D.EndScene();
}
}
public Entity CreateEntity(String name = "")
@@ -44,5 +63,22 @@ namespace GlitchyEngine.World
{
_ecsWorld.RemoveEntity(entity.Handle);
}
private uint32 ViewportWidth, ViewportHeight;
/// Sets the size of the viewport into which the scene will be rendered.
public void OnViewportResize(uint32 width, uint32 height)
{
ViewportWidth = width;
ViewportHeight = height;
for (var (entity, cameraComponent) in _ecsWorld.Enumerate<CameraComponent>())
{
if (!cameraComponent.FixedAspectRatio)
{
cameraComponent.Camera.SetViewportSize(width, height);
}
}
}
}
}