mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Many ecs, component and editor changes
This commit is contained in:
@@ -3,7 +3,25 @@ using GlitchyEngine.Math;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
public abstract class Camera
|
||||
struct Camera
|
||||
{
|
||||
protected Matrix _projection;
|
||||
|
||||
public Matrix Projection => _projection;
|
||||
|
||||
protected this()
|
||||
{
|
||||
_projection = .Identity;
|
||||
}
|
||||
|
||||
public this(Matrix projection)
|
||||
{
|
||||
_projection = projection;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public abstract class OldCamera
|
||||
{
|
||||
protected Matrix _view;
|
||||
protected Matrix _transform;
|
||||
|
||||
@@ -2,7 +2,7 @@ using GlitchyEngine.Math;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
public struct CameraComponent
|
||||
public struct OldCameraComponent
|
||||
{
|
||||
public enum Projection
|
||||
{
|
||||
@@ -29,10 +29,11 @@ namespace GlitchyEngine.Renderer
|
||||
*/
|
||||
Orthographic
|
||||
}
|
||||
|
||||
|
||||
float _nearPlane;
|
||||
float _farPlane;
|
||||
Projection _projectionType;
|
||||
|
||||
float _fovY;
|
||||
float _aspect;
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ using GlitchyEngine.Math;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
public class OrthographicCamera : Camera
|
||||
public class OrthographicCamera : OldCamera
|
||||
{
|
||||
protected float _bottom, _left, _right, _top;
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace GlitchyEngine.Renderer
|
||||
/**
|
||||
* If FarPlane is float.PositiveInfinity the projection matrix will be an infinite projection (that means no far plane)
|
||||
*/
|
||||
public class PerspectiveCamera : Camera
|
||||
public class PerspectiveCamera : OldCamera
|
||||
{
|
||||
public enum ProjectionType
|
||||
{
|
||||
|
||||
@@ -79,7 +79,8 @@ namespace GlitchyEngine.Renderer
|
||||
LineGeometry.SetVertexLayout(layout..ReleaseRefNoDelete());
|
||||
}
|
||||
|
||||
public static void BeginScene(EcsWorld world, EcsEntity cameraEntity)
|
||||
// TODO
|
||||
/*public static void BeginScene(EcsWorld world, EcsEntity cameraEntity)
|
||||
{
|
||||
Debug.Profiler.ProfileRendererFunction!();
|
||||
|
||||
@@ -91,9 +92,9 @@ namespace GlitchyEngine.Renderer
|
||||
var proj = camera.Projection;
|
||||
|
||||
_sceneConstants.ViewProjection = proj * view;
|
||||
}
|
||||
}*/
|
||||
|
||||
public static void BeginScene(Camera camera)
|
||||
public static void BeginScene(OldCamera camera)
|
||||
{
|
||||
Debug.Profiler.ProfileRendererFunction!();
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using GlitchyEngine.Renderer.Text;
|
||||
using GlitchyEngine.World;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
@@ -362,7 +363,8 @@ namespace GlitchyEngine.Renderer
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void BeginScene(Camera camera, DrawOrder drawOrder = .SortByTexture, Effect effect = null, Effect circleEffect = null)
|
||||
// TODO: remove?
|
||||
public static void BeginScene(OldCamera camera, DrawOrder drawOrder = .SortByTexture, Effect effect = null, Effect circleEffect = null)
|
||||
{
|
||||
Debug.Profiler.ProfileRendererFunction!();
|
||||
#if DEBUG
|
||||
@@ -397,6 +399,48 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
s_drawOrder = drawOrder;
|
||||
|
||||
#if DEBUG
|
||||
s_sceneRunning = true;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void BeginScene(Camera camera, Matrix transform, DrawOrder drawOrder = .SortByTexture, Effect effect = null, Effect circleEffect = null)
|
||||
{
|
||||
Debug.Profiler.ProfileRendererFunction!();
|
||||
#if DEBUG
|
||||
Log.EngineLogger.AssertDebug(s_initialized, "Renderer2D was not initialized.");
|
||||
Log.EngineLogger.AssertDebug(!s_sceneRunning, "You have to call EndScene before you can make another call to BeginScene.");
|
||||
#endif
|
||||
|
||||
//s_textureColorEffect.Bind(Renderer._context);
|
||||
|
||||
s_currentEffect?.ReleaseRef();
|
||||
if(effect != null)
|
||||
{
|
||||
s_currentEffect = effect..AddRef();
|
||||
}
|
||||
else
|
||||
{
|
||||
s_currentEffect = s_batchEffect..AddRef();
|
||||
}
|
||||
|
||||
s_currentCircleEffect?.ReleaseRef();
|
||||
if(circleEffect != null)
|
||||
{
|
||||
s_currentCircleEffect = effect..AddRef();
|
||||
}
|
||||
else
|
||||
{
|
||||
s_currentCircleEffect = s_circleBatchEffect..AddRef();
|
||||
}
|
||||
|
||||
Matrix viewProjection = camera.Projection * Matrix.Invert(transform);
|
||||
|
||||
s_currentEffect.Variables["ViewProjection"].SetData(viewProjection);
|
||||
s_currentCircleEffect.Variables["ViewProjection"].SetData(viewProjection);
|
||||
|
||||
s_drawOrder = drawOrder;
|
||||
|
||||
#if DEBUG
|
||||
s_sceneRunning = true;
|
||||
#endif
|
||||
|
||||
@@ -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 = .();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user