mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Editor camera and Editor/Runtime scene update separation
This commit is contained in:
@@ -59,6 +59,38 @@ namespace GlitchyEditor.EditWindows
|
||||
|
||||
var viewportSize = ImGui.GetContentRegionAvail();
|
||||
|
||||
if (_editor.CurrentCamera.[Friend]BindMouse)
|
||||
{
|
||||
var mousePos = ImGui.GetMousePos();
|
||||
var newMousePos = mousePos;
|
||||
var winPos = ImGui.GetWindowPos();
|
||||
var winSize = ImGui.GetWindowSize();
|
||||
|
||||
if (mousePos.x < winPos.x)
|
||||
{
|
||||
newMousePos.x = winPos.x + winSize.x - 1;
|
||||
}
|
||||
else if (mousePos.x > winPos.x + winSize.x)
|
||||
{
|
||||
newMousePos.x = winPos.x + 1;
|
||||
}
|
||||
|
||||
if (mousePos.y < winPos.y)
|
||||
{
|
||||
newMousePos.y = winPos.y + winSize.y - 1;
|
||||
}
|
||||
else if (mousePos.y > winPos.y + winSize.y)
|
||||
{
|
||||
newMousePos.y = winPos.y + 1;
|
||||
}
|
||||
|
||||
if (newMousePos != mousePos)
|
||||
{
|
||||
Input.SetMousePosition(Point((int32)newMousePos.x, (int32)newMousePos.y));
|
||||
_editor.CurrentCamera.[Friend]MouseCooldown = 2;
|
||||
}
|
||||
}
|
||||
|
||||
if(_renderTarget != null)
|
||||
{
|
||||
ImGui.Image(_renderTarget, viewportSize);
|
||||
@@ -157,11 +189,8 @@ namespace GlitchyEditor.EditWindows
|
||||
topLeft.y += cntMin.y;
|
||||
ImGuizmo.SetRect(topLeft.x, topLeft.y, viewportSize.x, viewportSize.y);
|
||||
|
||||
var cameraTransformCmp = _editor.CurrentScene.ActiveCamera.GetComponent<TransformComponent>();
|
||||
var view = cameraTransformCmp.WorldTransform.Invert();
|
||||
|
||||
var cameraCmp = _editor.CurrentCamera.GetComponent<CameraComponent>();
|
||||
var projection = cameraCmp.Camera.Projection;
|
||||
var view = _editor.CurrentCamera.View;
|
||||
var projection = _editor.CurrentCamera.Projection;
|
||||
|
||||
if(_editor.EntityHierarchyWindow.SelectedEntities.Count == 0)
|
||||
return;
|
||||
|
||||
@@ -11,7 +11,6 @@ namespace GlitchyEditor
|
||||
class Editor
|
||||
{
|
||||
private Scene _scene;
|
||||
private Entity _currentCamera;
|
||||
|
||||
private EntityHierarchyWindow _entityHierarchyWindow ~ delete _;
|
||||
private ComponentEditWindow _componentEditWindow ~ delete _;
|
||||
@@ -31,7 +30,7 @@ namespace GlitchyEditor
|
||||
}
|
||||
}
|
||||
|
||||
public Entity CurrentCamera => _currentCamera;
|
||||
public EditorCamera* CurrentCamera { get; set; }
|
||||
|
||||
/// Creates a new editor for the given world
|
||||
public this(Scene scene)
|
||||
@@ -44,17 +43,6 @@ namespace GlitchyEditor
|
||||
|
||||
public void Update()
|
||||
{
|
||||
_currentCamera = _scene.ActiveCamera;
|
||||
if (_currentCamera.IsValid)
|
||||
{
|
||||
var scriptComponent = _currentCamera.GetComponent<NativeScriptComponent>();
|
||||
|
||||
if (var camController = scriptComponent?.Instance as EditorCameraController)
|
||||
{
|
||||
camController.IsEnabled = (SceneViewportWindow.HasFocus && Input.IsMouseButtonPressed(.RightButton));
|
||||
}
|
||||
}
|
||||
|
||||
_entityHierarchyWindow.Show();
|
||||
_componentEditWindow.Show();
|
||||
_sceneViewportWindow.Show();
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
using GlitchyEngine;
|
||||
using GlitchyEngine.Math;
|
||||
using GlitchyEngine.World;
|
||||
|
||||
namespace GlitchyEditor
|
||||
{
|
||||
class EditorCameraController : ScriptableEntity
|
||||
{
|
||||
private float _cameraTranslationSpeed = 2.0f;
|
||||
private float _cameraRotationSpeedX = 0.001f;
|
||||
private float _cameraRotationSpeedY = 0.001f;
|
||||
|
||||
public bool IsEnabled = false;
|
||||
|
||||
protected override void OnUpdate(GameTime gt)
|
||||
{
|
||||
if (!IsEnabled)
|
||||
return;
|
||||
|
||||
Debug.Profiler.ProfileFunction!();
|
||||
|
||||
Vector3 movement = .();
|
||||
|
||||
if(Input.IsKeyPressed(Key.W))
|
||||
movement.Z += 1;
|
||||
if(Input.IsKeyPressed(Key.S))
|
||||
movement.Z -= 1;
|
||||
|
||||
if(Input.IsKeyPressed(Key.A))
|
||||
movement.X -= 1;
|
||||
if(Input.IsKeyPressed(Key.D))
|
||||
movement.X += 1;
|
||||
|
||||
if(Input.IsKeyPressed(Key.Space))
|
||||
movement.Y += 1;
|
||||
if(Input.IsKeyPressed(Key.Control))
|
||||
movement.Y -= 1;
|
||||
|
||||
var transformComponent = transform;
|
||||
|
||||
if(movement != .Zero)
|
||||
{
|
||||
movement.Normalize();
|
||||
|
||||
movement *= (float)(gt.FrameTime.TotalSeconds) * _cameraTranslationSpeed;
|
||||
|
||||
Matrix view = transformComponent.WorldTransform.Invert();
|
||||
|
||||
Vector4 delta = Vector4(movement, 1.0f) * view;
|
||||
|
||||
transformComponent.Position = transformComponent.Position + delta.XYZ;
|
||||
}
|
||||
|
||||
// Camera rotation
|
||||
var mouseDelta = Input.GetMouseMovement();
|
||||
|
||||
float rotY = mouseDelta.X * _cameraRotationSpeedX;
|
||||
float rotX = mouseDelta.Y * _cameraRotationSpeedY;
|
||||
|
||||
transformComponent.RotationEuler = transformComponent.RotationEuler + .(rotX, rotY, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,8 +47,7 @@ namespace GlitchyEditor
|
||||
|
||||
SettingsWindow _settingsWindow = new .() ~ delete _;
|
||||
|
||||
Entity _cameraEntity;
|
||||
Entity _otherCameraEntity;
|
||||
EditorCamera _camera ~ _.Dispose();
|
||||
|
||||
class CameraController : ScriptableEntity
|
||||
{
|
||||
@@ -95,6 +94,9 @@ namespace GlitchyEditor
|
||||
|
||||
InitGraphics();
|
||||
|
||||
_camera = EditorCamera(Vector3(3.5f, 1.25f, 2.75f), Quaternion.FromEulerAngles(MathHelper.ToRadians(40), MathHelper.ToRadians(25), 0), MathHelper.ToRadians(75), 0.1f, 1);
|
||||
_camera.RenderTarget = _cameraTarget;
|
||||
|
||||
NewScene();
|
||||
|
||||
InitEditor();
|
||||
@@ -129,10 +131,13 @@ namespace GlitchyEditor
|
||||
{
|
||||
_editor = new Editor(_scene);
|
||||
_editor.SceneViewportWindow.ViewportSizeChangedEvent.Add(new (s, e) => ViewportSizeChanged(s, e));
|
||||
_editor.CurrentCamera = &_camera;
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
_camera.Update(gameTime);
|
||||
|
||||
// Clear the swapchain-buffer
|
||||
RenderCommand.Clear(null, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0);
|
||||
|
||||
@@ -146,7 +151,7 @@ namespace GlitchyEditor
|
||||
RenderCommand.SetBlendState(_alphaBlendState);
|
||||
RenderCommand.SetDepthStencilState(_depthStencilState);
|
||||
|
||||
_scene.Update(gameTime, _viewportTarget);
|
||||
_scene.UpdateEditor(gameTime, _camera, _viewportTarget);
|
||||
|
||||
RenderCommand.UnbindRenderTargets();
|
||||
RenderCommand.SetRenderTarget(null, 0, true);
|
||||
@@ -162,6 +167,7 @@ namespace GlitchyEditor
|
||||
dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e));
|
||||
dispatcher.Dispatch<WindowResizeEvent>(scope (e) => OnWindowResize(e));
|
||||
dispatcher.Dispatch<KeyPressedEvent>(scope (e) => OnKeyPressed(e));
|
||||
dispatcher.Dispatch<MouseScrolledEvent>(scope (e) => OnMouseScrolled(e));
|
||||
}
|
||||
|
||||
ImGui.ID _mainDockspaceId;
|
||||
@@ -172,18 +178,6 @@ namespace GlitchyEditor
|
||||
{
|
||||
viewer.ViewTexture(_cameraTarget);
|
||||
|
||||
/*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);
|
||||
|
||||
@@ -313,34 +307,14 @@ namespace GlitchyEditor
|
||||
}
|
||||
}
|
||||
|
||||
private void PrepareSceneForEditor()
|
||||
{
|
||||
// Create the editor camera
|
||||
{
|
||||
_cameraEntity = _scene.CreateEntity("Editor Camera");
|
||||
// Add EditorComponent so that the engine knows that this is not part of the game.
|
||||
_cameraEntity.AddComponent<EditorComponent>();
|
||||
// Script for controlling the camera.
|
||||
_cameraEntity.AddComponent<NativeScriptComponent>().Bind<EditorCameraController>();
|
||||
|
||||
let camera = _cameraEntity.AddComponent<CameraComponent>();
|
||||
camera.Camera.SetPerspective(MathHelper.ToRadians(75), 0.1f, 10000.0f);
|
||||
camera.Primary = true;
|
||||
camera.RenderTarget = _cameraTarget;
|
||||
let transform = _cameraEntity.GetComponent<TransformComponent>();
|
||||
//transform.Position = .(-1.5f, 1.5f, -2.5f);
|
||||
transform.Position = .(3.5f, 1.25f, 2.75f);
|
||||
//transform.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(35), 0);
|
||||
transform.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(40), 0);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new scene.
|
||||
private void NewScene()
|
||||
{
|
||||
SceneFilePath = null;
|
||||
|
||||
_camera.Position = .(-1.5f, 1.5f, -2.5f);
|
||||
_camera.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(35), 0);
|
||||
|
||||
// Create the default light source
|
||||
{
|
||||
var lightNtt = _scene.CreateEntity("Light");
|
||||
@@ -354,8 +328,6 @@ namespace GlitchyEditor
|
||||
}
|
||||
|
||||
TestEntitiesWithModels();
|
||||
|
||||
PrepareSceneForEditor();
|
||||
}
|
||||
|
||||
/// Saves the scene in the file that is was loaded from or saved to last. If there is no such path (i.e. it is a new scene) the save file dialog will open.
|
||||
@@ -402,8 +374,6 @@ namespace GlitchyEditor
|
||||
SceneSerializer serializer = scope .(_scene);
|
||||
serializer.Deserialize(SceneFilePath);
|
||||
|
||||
PrepareSceneForEditor();
|
||||
|
||||
_editor.CurrentScene = _scene;
|
||||
}
|
||||
}
|
||||
@@ -481,6 +451,7 @@ namespace GlitchyEditor
|
||||
_cameraTarget.Resize(sizeX, sizeY);
|
||||
|
||||
_scene.OnViewportResize(sizeX, sizeY);
|
||||
_camera.OnViewportResize(sizeX, sizeY);
|
||||
}
|
||||
|
||||
private bool OnKeyPressed(KeyPressedEvent e)
|
||||
@@ -511,5 +482,13 @@ namespace GlitchyEditor
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool OnMouseScrolled(MouseScrolledEvent e)
|
||||
{
|
||||
if (_camera.OnMouseScrolled(e))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,9 @@ namespace GlitchyEngine
|
||||
public static extern Point GetMousePosition();
|
||||
public static extern int32 GetMouseX();
|
||||
public static extern int32 GetMouseY();
|
||||
|
||||
// TODO: Should Input be able to set mousepos?
|
||||
public static extern void SetMousePosition(Point pos);
|
||||
|
||||
public static extern bool WasMouseButtonPressed(MouseButton button);
|
||||
public static extern bool WasMouseButtonReleased(MouseButton button);
|
||||
|
||||
@@ -5,6 +5,7 @@ using GlitchyEngine.Events;
|
||||
using DirectX.Windows.VirtualKeyCodes;
|
||||
using DirectX.Windows;
|
||||
using GlitchyEngine.Math;
|
||||
using System.Interop;
|
||||
using static System.Windows;
|
||||
|
||||
namespace GlitchyEngine
|
||||
@@ -154,11 +155,15 @@ namespace GlitchyEngine
|
||||
|
||||
public override static Point GetMouseMovement() => CurrentState.CursorPositionDifference;
|
||||
|
||||
public override static void SetMousePosition(Point pos) => SetCursorPos(pos.X, pos.Y);
|
||||
|
||||
//[CLink, CallingConvention(.Stdcall)]
|
||||
//static extern int16 GetKeyState(int32 keycode);
|
||||
[CLink, CallingConvention(.Stdcall)]
|
||||
static extern IntBool GetCursorPos(out Point p);
|
||||
[CLink, CallingConvention(.Stdcall)]
|
||||
static extern IntBool SetCursorPos(c_int x, c_int y);
|
||||
[CLink, CallingConvention(.Stdcall)]
|
||||
static extern IntBool ScreenToClient(HWnd hWnd, ref Point p);
|
||||
|
||||
public override static void NewFrame()
|
||||
|
||||
@@ -288,6 +288,17 @@ namespace GlitchyEngine.Renderer
|
||||
_sceneConstants.CompositionTarget = finalTarget;
|
||||
}
|
||||
|
||||
public static void BeginScene(EditorCamera camera, RenderTarget2D finalTarget)
|
||||
{
|
||||
Debug.Profiler.ProfileRendererFunction!();
|
||||
|
||||
Matrix viewProjection = camera.Projection * camera.View;
|
||||
_sceneConstants.ViewProjection = viewProjection;
|
||||
_sceneConstants.CameraPosition = camera.Position;
|
||||
_sceneConstants.CameraTarget = camera.RenderTarget;
|
||||
_sceneConstants.CompositionTarget = finalTarget;
|
||||
}
|
||||
|
||||
public static int SortMeshes(SubmittedMesh left, SubmittedMesh right)
|
||||
{
|
||||
// TODO: Once Material "inheritance" is ready we could perhaps check how similar materials are (e.g. shared textures/variables/etc...)
|
||||
|
||||
@@ -444,6 +444,48 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
s_drawOrder = drawOrder;
|
||||
|
||||
#if DEBUG
|
||||
s_sceneRunning = true;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void BeginScene(EditorCamera camera, 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 * camera.View;
|
||||
|
||||
s_currentEffect.Variables["ViewProjection"].SetData(viewProjection);
|
||||
s_currentCircleEffect.Variables["ViewProjection"].SetData(viewProjection);
|
||||
|
||||
s_drawOrder = drawOrder;
|
||||
|
||||
#if DEBUG
|
||||
s_sceneRunning = true;
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,328 @@
|
||||
using GlitchyEngine;
|
||||
using GlitchyEngine.Renderer;
|
||||
using GlitchyEngine.Math;
|
||||
using System;
|
||||
using GlitchyEngine.Events;
|
||||
|
||||
namespace GlitchyEngine.World
|
||||
{
|
||||
struct EditorCamera : Camera, IDisposable
|
||||
{
|
||||
private Vector3 _position;
|
||||
private Quaternion _rotation;
|
||||
|
||||
private Vector3 _focalPosition = .Zero;
|
||||
private float _focalDistance = 5.0f;
|
||||
|
||||
private float _cameraTranslationSpeed = 2.0f;
|
||||
private float _cameraRotationSpeedX = 0.001f;
|
||||
private float _cameraRotationSpeedY = 0.001f;
|
||||
private float _cameraFastFactor = 10f;
|
||||
|
||||
private Matrix _view;
|
||||
|
||||
private float _fovY;
|
||||
private float _nearPlane;
|
||||
private float _aspectRatio;
|
||||
|
||||
private RenderTarget2D _renderTarget = null;
|
||||
|
||||
internal bool BindMouse;
|
||||
internal uint8 MouseCooldown;
|
||||
|
||||
private bool _isAltMode = false;
|
||||
|
||||
public Matrix View => _view;
|
||||
|
||||
public RenderTarget2D RenderTarget
|
||||
{
|
||||
get => _renderTarget;
|
||||
set mut
|
||||
{
|
||||
if (_renderTarget == value)
|
||||
return;
|
||||
|
||||
SetReference!(_renderTarget, value);
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 Position
|
||||
{
|
||||
get => _position;
|
||||
set mut
|
||||
{
|
||||
if (_position == value)
|
||||
return;
|
||||
|
||||
_position = value;
|
||||
UpdateView();
|
||||
}
|
||||
}
|
||||
|
||||
public Quaternion Rotation
|
||||
{
|
||||
get => _rotation;
|
||||
set mut
|
||||
{
|
||||
if (_rotation == value)
|
||||
return;
|
||||
|
||||
_rotation = value;
|
||||
UpdateView();
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 RotationEuler
|
||||
{
|
||||
get => Quaternion.ToEulerAngles(_rotation);
|
||||
set mut
|
||||
{
|
||||
Quaternion quat = Quaternion.FromEulerAngles(value.Y, value.X, value.Z);
|
||||
if (_rotation == quat)
|
||||
return;
|
||||
|
||||
_rotation = quat;
|
||||
UpdateView();
|
||||
}
|
||||
}
|
||||
|
||||
public (Vector3 Axis, float Angle) RotationAxisAngle
|
||||
{
|
||||
get => _rotation.ToAxisAngle();
|
||||
set mut
|
||||
{
|
||||
Quaternion quat = Quaternion.FromAxisAngle(value.Axis, value.Angle);
|
||||
if (_rotation == quat)
|
||||
return;
|
||||
|
||||
_rotation = quat;
|
||||
UpdateView();
|
||||
}
|
||||
}
|
||||
|
||||
public float FovY
|
||||
{
|
||||
get => _fovY;
|
||||
set mut
|
||||
{
|
||||
if (_fovY == value)
|
||||
return;
|
||||
|
||||
_fovY = value;
|
||||
UpdateProjection();
|
||||
}
|
||||
}
|
||||
|
||||
public float NearPlane
|
||||
{
|
||||
get => _nearPlane;
|
||||
set mut
|
||||
{
|
||||
if (_nearPlane == value)
|
||||
return;
|
||||
|
||||
_nearPlane = value;
|
||||
UpdateProjection();
|
||||
}
|
||||
}
|
||||
|
||||
public float AspectRatio
|
||||
{
|
||||
get => _aspectRatio;
|
||||
set mut
|
||||
{
|
||||
if (_aspectRatio == value)
|
||||
return;
|
||||
|
||||
_aspectRatio = value;
|
||||
UpdateProjection();
|
||||
}
|
||||
}
|
||||
|
||||
public this(Vector3 position, Quaternion rotation, float fovY, float nearPlane, float aspectRatio)
|
||||
{
|
||||
_position = position;
|
||||
_rotation = rotation;
|
||||
_fovY = fovY;
|
||||
_nearPlane = nearPlane;
|
||||
_aspectRatio = aspectRatio;
|
||||
|
||||
UpdateView();
|
||||
UpdateProjection();
|
||||
}
|
||||
|
||||
public void Update(GameTime gameTime) mut
|
||||
{
|
||||
Debug.Profiler.ProfileFunction!();
|
||||
|
||||
BindMouse = false;
|
||||
|
||||
if (Input.IsKeyPressed(.Alt))
|
||||
{
|
||||
AltController(gameTime);
|
||||
_isAltMode = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
FirstPersonController(gameTime);
|
||||
_isAltMode = false;
|
||||
}
|
||||
|
||||
if (MouseCooldown != 0)
|
||||
MouseCooldown--;
|
||||
}
|
||||
|
||||
void FirstPersonController(GameTime gameTime) mut
|
||||
{
|
||||
if (!Input.IsMouseButtonPressed(.RightButton))
|
||||
return;
|
||||
|
||||
BindMouse = true;
|
||||
|
||||
bool transformChanged = false;
|
||||
|
||||
Vector3 movement = .();
|
||||
|
||||
if(Input.IsKeyPressed(Key.W))
|
||||
movement.Z += 1;
|
||||
if(Input.IsKeyPressed(Key.S))
|
||||
movement.Z -= 1;
|
||||
|
||||
if(Input.IsKeyPressed(Key.A))
|
||||
movement.X -= 1;
|
||||
if(Input.IsKeyPressed(Key.D))
|
||||
movement.X += 1;
|
||||
|
||||
if(Input.IsKeyPressed(Key.Space))
|
||||
movement.Y += 1;
|
||||
if(Input.IsKeyPressed(Key.Control))
|
||||
movement.Y -= 1;
|
||||
|
||||
if(movement != .Zero)
|
||||
{
|
||||
movement.Normalize();
|
||||
|
||||
if(Input.IsKeyPressed(Key.Shift))
|
||||
movement *= _cameraFastFactor;
|
||||
|
||||
movement *= (float)(gameTime.DeltaTime) * _cameraTranslationSpeed;
|
||||
|
||||
Vector4 delta = Vector4(movement, 1.0f) * _view;
|
||||
|
||||
_position += delta.XYZ;
|
||||
|
||||
transformChanged = true;
|
||||
}
|
||||
|
||||
// Camera rotation
|
||||
var mouseDelta = Input.GetMouseMovement();
|
||||
|
||||
float rotY = mouseDelta.X * _cameraRotationSpeedX;
|
||||
float rotX = mouseDelta.Y * _cameraRotationSpeedY;
|
||||
|
||||
if (MouseCooldown == 0 && rotY != 0 && rotX != 0)
|
||||
{
|
||||
Vector3 rotationEuler = RotationEuler + Vector3(rotX, rotY, 0);
|
||||
_rotation = Quaternion.FromEulerAngles(rotationEuler.Y, rotationEuler.X, rotationEuler.Z);
|
||||
|
||||
transformChanged = true;
|
||||
}
|
||||
|
||||
if (transformChanged)
|
||||
UpdateView();
|
||||
}
|
||||
|
||||
private float GetZoomSpeed()
|
||||
{
|
||||
float dist = _focalDistance * 0.2f;
|
||||
dist = Math.Max(dist, 0.0f);
|
||||
|
||||
float speed = Math.Pow(dist, 1.5f);
|
||||
speed = Math.Min(speed, 100.0f);
|
||||
|
||||
return speed;
|
||||
}
|
||||
|
||||
void AltController(GameTime gameTime) mut
|
||||
{
|
||||
bool transformChanged = false;
|
||||
|
||||
BindMouse = Input.IsMouseButtonPressed(.LeftButton) || Input.IsMouseButtonPressed(.RightButton);
|
||||
|
||||
var mouseDelta = Input.GetMouseMovement();
|
||||
|
||||
if (MouseCooldown == 0 && Input.IsMouseButtonPressed(.LeftButton) && mouseDelta != .())
|
||||
{
|
||||
Vector2 movement = .(
|
||||
-mouseDelta.X,
|
||||
mouseDelta.Y);
|
||||
|
||||
movement *= (float)(gameTime.DeltaTime) * _cameraTranslationSpeed * GetZoomSpeed();
|
||||
|
||||
Vector4 delta = Vector4(movement, 0.0f, 1.0f) * _view;
|
||||
|
||||
_focalPosition += delta.XYZ;
|
||||
|
||||
transformChanged = true;
|
||||
}
|
||||
|
||||
if (MouseCooldown == 0 && Input.IsMouseButtonPressed(.RightButton) && mouseDelta != .())
|
||||
{
|
||||
float rotY = mouseDelta.X * _cameraRotationSpeedX;
|
||||
float rotX = mouseDelta.Y * _cameraRotationSpeedY;
|
||||
|
||||
Vector3 rotationEuler = RotationEuler + Vector3(rotX, rotY, 0);
|
||||
_rotation = Quaternion.FromEulerAngles(rotationEuler.Y, rotationEuler.X, rotationEuler.Z);
|
||||
|
||||
transformChanged = true;
|
||||
}
|
||||
|
||||
if (transformChanged)
|
||||
UpdateView();
|
||||
}
|
||||
|
||||
private void UpdateView() mut
|
||||
{
|
||||
Matrix viewRotation = Matrix.RotationQuaternion(Quaternion.Inverse(_rotation));
|
||||
|
||||
Vector4 offset = Vector4(0, 0, -_focalDistance, 1.0f) * viewRotation;
|
||||
if (_isAltMode)
|
||||
_position = _focalPosition + offset.XYZ;
|
||||
else
|
||||
_focalPosition = _position - offset.XYZ;
|
||||
|
||||
_view = viewRotation * Matrix.Translation(-_position);
|
||||
|
||||
//_view = (Matrix.Translation(_position) * Matrix.RotationQuaternion(_rotation)).Invert();
|
||||
}
|
||||
|
||||
private void UpdateProjection() mut
|
||||
{
|
||||
_projection = Matrix.InfinitePerspectiveProjection(_fovY, _aspectRatio, _nearPlane);
|
||||
}
|
||||
|
||||
public void OnViewportResize(uint32 sizeX, uint32 sizeY) mut
|
||||
{
|
||||
_aspectRatio = (float)sizeX / sizeY;
|
||||
UpdateProjection();
|
||||
}
|
||||
|
||||
public bool OnMouseScrolled(MouseScrolledEvent event) mut
|
||||
{
|
||||
if (_isAltMode)
|
||||
{
|
||||
_focalDistance = Math.Max(_focalDistance - GetZoomSpeed() * event.YOffset, 0.01f);
|
||||
UpdateView();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_renderTarget?.ReleaseRef();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,8 +48,10 @@ namespace GlitchyEngine.World
|
||||
{
|
||||
}
|
||||
|
||||
public void Update(GameTime gameTime, RenderTarget2D finalTarget)
|
||||
public void UpdateRuntime(GameTime gameTime, RenderTarget2D finalTarget)
|
||||
{
|
||||
Debug.Profiler.ProfileRendererFunction!();
|
||||
|
||||
TransformSystem.Update(_ecsWorld);
|
||||
|
||||
for (var (entity, script) in _ecsWorld.Enumerate<NativeScriptComponent>())
|
||||
@@ -88,9 +90,9 @@ namespace GlitchyEngine.World
|
||||
Renderer.Submit(mesh.Mesh, meshRenderer.Material, transform.WorldTransform);
|
||||
}
|
||||
|
||||
for (var (entity, transform, camera) in _ecsWorld.Enumerate<TransformComponent, LightComponent>())
|
||||
for (var (entity, transform, light) in _ecsWorld.Enumerate<TransformComponent, LightComponent>())
|
||||
{
|
||||
Renderer.Submit(camera.SceneLight, transform.WorldTransform);
|
||||
Renderer.Submit(light.SceneLight, transform.WorldTransform);
|
||||
}
|
||||
|
||||
Renderer.EndScene();
|
||||
@@ -109,6 +111,42 @@ namespace GlitchyEngine.World
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateEditor(GameTime gameTime, EditorCamera camera, RenderTarget2D viewportTarget)
|
||||
{
|
||||
Debug.Profiler.ProfileRendererFunction!();
|
||||
|
||||
viewportTarget.AddRef();
|
||||
|
||||
TransformSystem.Update(_ecsWorld);
|
||||
|
||||
// 3D render
|
||||
Renderer.BeginScene(camera, viewportTarget);
|
||||
|
||||
for (var (entity, transform, mesh, meshRenderer) in _ecsWorld.Enumerate<TransformComponent, MeshComponent, MeshRendererComponent>())
|
||||
{
|
||||
Renderer.Submit(mesh.Mesh, meshRenderer.Material, transform.WorldTransform);
|
||||
}
|
||||
|
||||
for (var (entity, transform, light) in _ecsWorld.Enumerate<TransformComponent, LightComponent>())
|
||||
{
|
||||
Renderer.Submit(light.SceneLight, transform.WorldTransform);
|
||||
}
|
||||
|
||||
Renderer.EndScene();
|
||||
|
||||
// Sprite renderer
|
||||
Renderer2D.BeginScene(camera);
|
||||
|
||||
for (var (entity, transform, sprite) in _ecsWorld.Enumerate<TransformComponent, SpriterRendererComponent>())
|
||||
{
|
||||
Renderer2D.DrawQuad(transform.WorldTransform, sprite.Sprite, sprite.Color);
|
||||
}
|
||||
|
||||
Renderer2D.EndScene();
|
||||
|
||||
viewportTarget.ReleaseRef();
|
||||
}
|
||||
|
||||
/// Creates a new Entity with the given name.
|
||||
public Entity CreateEntity(String name = "")
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user