mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Many ecs, component and editor changes
This commit is contained in:
@@ -10,7 +10,7 @@ namespace GlitchyEditor.EditWindows
|
||||
{
|
||||
class SceneViewportWindow : EditorWindow
|
||||
{
|
||||
public Camera _camera;
|
||||
public OldCamera _camera;
|
||||
|
||||
public const String s_WindowTitle = "Scene";
|
||||
|
||||
|
||||
@@ -28,6 +28,9 @@ namespace GlitchyEditor
|
||||
|
||||
RenderTarget2D _viewportTarget ~ _?.ReleaseRef();
|
||||
|
||||
Entity _cameraEntity;
|
||||
Entity _otherCameraEntity;
|
||||
|
||||
public this() : base("Example")
|
||||
{
|
||||
Application.Get().Window.IsVSync = false;
|
||||
@@ -35,6 +38,26 @@ namespace GlitchyEditor
|
||||
InitGraphics();
|
||||
//InitEcs();
|
||||
InitEditor();
|
||||
|
||||
{
|
||||
_cameraEntity = _scene.CreateEntity("Camera Entity");
|
||||
let camera = _cameraEntity.AddComponent<CameraComponent>();
|
||||
camera.Camera.SetPerspective(0.1f, 1000f, MathHelper.ToRadians(75));
|
||||
camera.Primary = true;
|
||||
camera.FixedAspectRatio = false;
|
||||
let transform = _cameraEntity.GetComponent<SimpleTransformComponent>();
|
||||
transform.Transform = Matrix.Translation(0, 0, -5);
|
||||
}
|
||||
|
||||
{
|
||||
_otherCameraEntity = _scene.CreateEntity("Other Camera Entity");
|
||||
let camera = _otherCameraEntity.AddComponent<CameraComponent>();
|
||||
camera.Camera.SetPerspective(0.1f, 1000f, MathHelper.ToRadians(45));
|
||||
camera.Primary = false;
|
||||
camera.FixedAspectRatio = false;
|
||||
let transform = _otherCameraEntity.GetComponent<SimpleTransformComponent>();
|
||||
transform.Transform = Matrix.Translation(0, 0, -5);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitGraphics()
|
||||
@@ -56,18 +79,6 @@ namespace GlitchyEditor
|
||||
_viewportTarget.SamplerState = SamplerStateManager.LinearClamp;
|
||||
}
|
||||
|
||||
/*private void InitEcs()
|
||||
{
|
||||
_world.Register<DebugNameComponent>();
|
||||
_world.Register<TransformComponent>();
|
||||
_world.Register<ParentComponent>();
|
||||
_world.Register<MeshComponent>();
|
||||
_world.Register<MeshRendererComponent>();
|
||||
_world.Register<SkinnedMeshRendererComponent>();
|
||||
_world.Register<CameraComponent>();
|
||||
_world.Register<AnimationComponent>();
|
||||
}*/
|
||||
|
||||
private void InitEditor()
|
||||
{
|
||||
_editor = new Editor(_scene);
|
||||
@@ -104,13 +115,8 @@ namespace GlitchyEditor
|
||||
|
||||
//Renderer.EndScene();
|
||||
|
||||
Renderer2D.BeginScene(_cameraController.Camera);
|
||||
|
||||
_scene.Update(gameTime);
|
||||
|
||||
Renderer2D.EndScene();
|
||||
|
||||
|
||||
RenderCommand.Clear(null, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0);
|
||||
|
||||
RenderCommand.SetRenderTarget(null);
|
||||
@@ -133,6 +139,18 @@ namespace GlitchyEditor
|
||||
|
||||
private bool OnImGuiRender(ImGuiRenderEvent event)
|
||||
{
|
||||
ImGui.Begin("Test");
|
||||
|
||||
static bool cameraA = true;
|
||||
|
||||
if (ImGui.Checkbox("Camera A", &cameraA))
|
||||
{
|
||||
_cameraEntity.GetComponent<CameraComponent>().Primary = cameraA;
|
||||
_otherCameraEntity.GetComponent<CameraComponent>().Primary = !cameraA;
|
||||
}
|
||||
|
||||
ImGui.End();
|
||||
|
||||
ImGui.Viewport* viewport = ImGui.GetMainViewport();
|
||||
ImGui.DockSpaceOverViewport(viewport);
|
||||
|
||||
@@ -194,6 +212,8 @@ namespace GlitchyEditor
|
||||
_viewportTarget.Resize(sizeX, sizeY);
|
||||
|
||||
_cameraController.AspectRatio = (float)sizeX / (float)sizeY;
|
||||
|
||||
_scene.OnViewportResize(sizeX, sizeY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
@@ -33,6 +33,7 @@ namespace GlitchyEngine.Renderer
|
||||
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);
|
||||
|
||||
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