mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Mouse picking
This commit is contained in:
@@ -23,8 +23,9 @@ namespace GlitchyEditor.EditWindows
|
|||||||
|
|
||||||
public List<Entity> SelectedEntities => _selectedEntities;
|
public List<Entity> SelectedEntities => _selectedEntities;
|
||||||
|
|
||||||
public this(Scene scene)
|
public this(Editor editor, Scene scene)
|
||||||
{
|
{
|
||||||
|
_editor = editor;
|
||||||
SetContext(scene);
|
SetContext(scene);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,6 +62,23 @@ namespace GlitchyEditor.EditWindows
|
|||||||
ImGui.EndPopup();
|
ImGui.EndPopup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_editor.SceneViewportWindow.SelectionChanged)
|
||||||
|
{
|
||||||
|
if (Input.IsKeyReleased(.Control))
|
||||||
|
{
|
||||||
|
_selectedEntities.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
var handle = _scene.[Friend]_ecsWorld.GetCurrentVersion(EcsEntity.[Friend]CreateEntityID(_editor.SceneViewportWindow.SelectedEntityId, 0));
|
||||||
|
|
||||||
|
if (handle case .Ok(let h))
|
||||||
|
{
|
||||||
|
Entity e = .(h, _scene);
|
||||||
|
|
||||||
|
_selectedEntities.Add(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ShowEntityHierarchy();
|
ShowEntityHierarchy();
|
||||||
|
|
||||||
if ((ImGui.IsMouseDown(.Left) || ImGui.IsMouseDown(.Right)) && !ImGui.IsAnyItemHovered() && !ImGui.GetIO().KeyCtrl && ImGui.IsWindowHovered(.AllowWhenBlockedByPopup))
|
if ((ImGui.IsMouseDown(.Left) || ImGui.IsMouseDown(.Right)) && !ImGui.IsAnyItemHovered() && !ImGui.GetIO().KeyCtrl && ImGui.IsWindowHovered(.AllowWhenBlockedByPopup))
|
||||||
@@ -113,6 +131,8 @@ namespace GlitchyEditor.EditWindows
|
|||||||
{
|
{
|
||||||
_scene.DestroyEntity(entity, true);
|
_scene.DestroyEntity(entity, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_selectedEntities.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ShowEntityHierarchyMenuBar()
|
private void ShowEntityHierarchyMenuBar()
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using GlitchyEngine.Math;
|
|||||||
using GlitchyEngine;
|
using GlitchyEngine;
|
||||||
using GlitchyEngine.World;
|
using GlitchyEngine.World;
|
||||||
using ImGuizmo;
|
using ImGuizmo;
|
||||||
|
using GlitchyEngine.Events;
|
||||||
|
|
||||||
namespace GlitchyEditor.EditWindows
|
namespace GlitchyEditor.EditWindows
|
||||||
{
|
{
|
||||||
@@ -15,6 +16,7 @@ namespace GlitchyEditor.EditWindows
|
|||||||
private RenderTargetGroup _renderTarget ~ _?.ReleaseRef();
|
private RenderTargetGroup _renderTarget ~ _?.ReleaseRef();
|
||||||
|
|
||||||
public Event<EventHandler<Vector2>> ViewportSizeChangedEvent ~ _.Dispose();
|
public Event<EventHandler<Vector2>> ViewportSizeChangedEvent ~ _.Dispose();
|
||||||
|
public Event<EventHandler<uint32>> EntityClickedEvent ~ _.Dispose();
|
||||||
|
|
||||||
public RenderTargetGroup RenderTarget
|
public RenderTargetGroup RenderTarget
|
||||||
{
|
{
|
||||||
@@ -36,6 +38,9 @@ namespace GlitchyEditor.EditWindows
|
|||||||
private ImGui.Vec2 oldViewportSize;
|
private ImGui.Vec2 oldViewportSize;
|
||||||
private bool viewPortChanged;
|
private bool viewPortChanged;
|
||||||
|
|
||||||
|
public uint32 SelectedEntityId;
|
||||||
|
public bool SelectionChanged;
|
||||||
|
|
||||||
protected override void InternalShow()
|
protected override void InternalShow()
|
||||||
{
|
{
|
||||||
ImGui.PushStyleVar(.WindowPadding, ImGui.Vec2(1, 1));
|
ImGui.PushStyleVar(.WindowPadding, ImGui.Vec2(1, 1));
|
||||||
@@ -94,35 +99,35 @@ namespace GlitchyEditor.EditWindows
|
|||||||
if(_renderTarget != null)
|
if(_renderTarget != null)
|
||||||
{
|
{
|
||||||
//ImGui.Image(_renderTarget.GetViewBinding(0), viewportSize);
|
//ImGui.Image(_renderTarget.GetViewBinding(0), viewportSize);
|
||||||
ImGui.Image(_editor.CurrentCamera.RenderTarget.GetViewBinding(0), viewportSize);
|
//ImGui.Image(_editor.CurrentCamera.RenderTarget.GetViewBinding(0), viewportSize);
|
||||||
|
ImGui.Image(_editor.CurrentScene.[Friend]_compositeTarget.GetViewBinding(0), viewportSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool gizmoUsed = DrawImGuizmo(viewportSize);
|
||||||
|
|
||||||
Vector2 relativeMouse = (Vector2)ImGui.GetMousePos() - (Vector2)ImGui.GetItemRectMin();
|
Vector2 relativeMouse = (Vector2)ImGui.GetMousePos() - (Vector2)ImGui.GetItemRectMin();
|
||||||
|
|
||||||
int x = Renderer.[Friend]_gBuffer.Target.Width;
|
int x = Renderer.[Friend]_gBuffer.Target.Width;
|
||||||
int y = Renderer.[Friend]_gBuffer.Target.Height;
|
int y = Renderer.[Friend]_gBuffer.Target.Height;
|
||||||
|
|
||||||
if (Input.IsMouseButtonPressing(.LeftButton) && relativeMouse.X >= 0 && relativeMouse.Y >= 0 &&
|
if (!gizmoUsed && !_editor.CurrentCamera.[Friend]BindMouse && Input.IsMouseButtonPressing(.LeftButton) && relativeMouse.X >= 0 && relativeMouse.Y >= 0 &&
|
||||||
relativeMouse.X < viewportSize.x && relativeMouse.Y < viewportSize.y &&
|
relativeMouse.X < viewportSize.x && relativeMouse.Y < viewportSize.y &&
|
||||||
relativeMouse.X < x && relativeMouse.Y < y)
|
relativeMouse.X < x && relativeMouse.Y < y)
|
||||||
{
|
{
|
||||||
uint32 id = uint32.MaxValue;
|
uint32 id = uint32.MaxValue;
|
||||||
|
|
||||||
_editor.CurrentCamera.RenderTarget.GetData<uint32>(&id, 1, (.)relativeMouse.X, (.)relativeMouse.Y, 1, 1);
|
_editor.CurrentScene.[Friend]_compositeTarget.GetData<uint32>(&id, 1, (.)relativeMouse.X, (.)relativeMouse.Y, 1, 1);
|
||||||
|
|
||||||
//_renderTarget.GetData<uint32>(&pixel, 0, );
|
SelectionChanged = true;
|
||||||
|
SelectedEntityId = id;
|
||||||
Log.EngineLogger.Info($"{relativeMouse} || ID = {id}");
|
|
||||||
|
|
||||||
if (id != uint32.MaxValue)
|
|
||||||
{
|
|
||||||
//_editor.CurrentScene.[Friend]_ecsWorld.GetCurrentVersion()
|
|
||||||
}
|
|
||||||
//_editor.EntityHierarchyWindow.SelectEntityWithId(id, true);
|
|
||||||
|
|
||||||
|
EntityClickedEvent(this, id);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SelectionChanged = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
DrawImGuizmo(viewportSize);
|
|
||||||
|
|
||||||
ImGui.End();
|
ImGui.End();
|
||||||
|
|
||||||
@@ -203,7 +208,7 @@ namespace GlitchyEditor.EditWindows
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawImGuizmo(ImGui.Vec2 viewportSize)
|
private bool DrawImGuizmo(ImGui.Vec2 viewportSize)
|
||||||
{
|
{
|
||||||
ImGuizmo.SetOrthographic(false);
|
ImGuizmo.SetOrthographic(false);
|
||||||
ImGuizmo.SetDrawlist();
|
ImGuizmo.SetDrawlist();
|
||||||
@@ -219,7 +224,7 @@ namespace GlitchyEditor.EditWindows
|
|||||||
var projection = _editor.CurrentCamera.Projection;
|
var projection = _editor.CurrentCamera.Projection;
|
||||||
|
|
||||||
if(_editor.EntityHierarchyWindow.SelectedEntities.Count == 0)
|
if(_editor.EntityHierarchyWindow.SelectedEntities.Count == 0)
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
var entity = _editor.EntityHierarchyWindow.SelectedEntities.Back;
|
var entity = _editor.EntityHierarchyWindow.SelectedEntities.Back;
|
||||||
|
|
||||||
@@ -245,6 +250,8 @@ namespace GlitchyEditor.EditWindows
|
|||||||
// (probably because scaling a rotated matrix results in a skewed matrix, but unity can do it, so should we)
|
// (probably because scaling a rotated matrix results in a skewed matrix, but unity can do it, so should we)
|
||||||
transformCmp.LocalTransform = parentView * worldTransform;
|
transformCmp.LocalTransform = parentView * worldTransform;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ImGuizmo.IsUsing();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ namespace GlitchyEditor
|
|||||||
/// Creates a new editor for the given world
|
/// Creates a new editor for the given world
|
||||||
public this(Scene scene)
|
public this(Scene scene)
|
||||||
{
|
{
|
||||||
_entityHierarchyWindow = new EntityHierarchyWindow(_scene);
|
_entityHierarchyWindow = new EntityHierarchyWindow(this, _scene);
|
||||||
CurrentScene = scene;
|
CurrentScene = scene;
|
||||||
|
|
||||||
_componentEditWindow = new ComponentEditWindow(_entityHierarchyWindow);
|
_componentEditWindow = new ComponentEditWindow(_entityHierarchyWindow);
|
||||||
|
|||||||
@@ -341,11 +341,12 @@ namespace GlitchyEngine.Renderer
|
|||||||
|
|
||||||
_lights.Clear();
|
_lights.Clear();
|
||||||
|
|
||||||
// Copy EntityIDs to camera target
|
// Copy EntityIDs to compositionTarget
|
||||||
_gBuffer.Target.CopyTo(_sceneConstants.CameraTarget, 1, Int2.Zero, Int2(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height), Int2.Zero, 5);
|
_gBuffer.Target.CopyTo(_sceneConstants.CompositionTarget, 1, Int2.Zero, Int2(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height), Int2.Zero, 5);
|
||||||
|
|
||||||
RenderCommand.SetBlendState(_gBufferBlend);
|
RenderCommand.SetBlendState(_gBufferBlend);
|
||||||
|
|
||||||
|
RenderCommand.UnbindRenderTargets();
|
||||||
RenderCommand.SetRenderTargetGroup(_sceneConstants.CompositionTarget, true);
|
RenderCommand.SetRenderTargetGroup(_sceneConstants.CompositionTarget, true);
|
||||||
RenderCommand.BindRenderTargets();
|
RenderCommand.BindRenderTargets();
|
||||||
|
|
||||||
|
|||||||
@@ -161,10 +161,10 @@ namespace GlitchyEngine.World
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the entity with the same ID and the current version. Or null, if no such entity exists.
|
/// Returns the entity with the same ID and the current version. Or null, if no such entity exists.
|
||||||
public EcsEntity? GetCurrentVersion(EcsEntity entity)
|
public Result<EcsEntity> GetCurrentVersion(EcsEntity entity)
|
||||||
{
|
{
|
||||||
if(entity.Index > _entities.Count)
|
if(entity.Index > _entities.Count)
|
||||||
return null;
|
return .Err;
|
||||||
|
|
||||||
var listEntity = ref _entities[entity.Index];
|
var listEntity = ref _entities[entity.Index];
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ namespace GlitchyEngine.World
|
|||||||
|
|
||||||
private Dictionary<Type, function void(Entity entity, Type componentType, void* component)> _onComponentAddedHandlers = new .() ~ delete _;
|
private Dictionary<Type, function void(Entity entity, Type componentType, void* component)> _onComponentAddedHandlers = new .() ~ delete _;
|
||||||
|
|
||||||
|
private RenderTargetGroup _compositeTarget ~ _.ReleaseRef();
|
||||||
|
|
||||||
public Entity ActiveCamera => {
|
public Entity ActiveCamera => {
|
||||||
Entity cameraEntity = .();
|
Entity cameraEntity = .();
|
||||||
|
|
||||||
@@ -42,6 +44,13 @@ namespace GlitchyEngine.World
|
|||||||
|
|
||||||
cameraComponent.Camera.SetViewportSize(e.Scene.ViewportWidth, e.Scene.ViewportHeight);
|
cameraComponent.Camera.SetViewportSize(e.Scene.ViewportWidth, e.Scene.ViewportHeight);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
RenderTargetGroupDescription desc = .(100, 100,
|
||||||
|
TargetDescription[](
|
||||||
|
RenderTargetFormat.R16G16B16A16_Float,
|
||||||
|
.(RenderTargetFormat.R32_UInt) {ClearColor = .UInt(uint32.MaxValue)}),
|
||||||
|
RenderTargetFormat.D24_UNorm_S8_UInt);
|
||||||
|
_compositeTarget = new RenderTargetGroup(desc);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ~this()
|
public ~this()
|
||||||
@@ -50,6 +59,9 @@ namespace GlitchyEngine.World
|
|||||||
|
|
||||||
public void UpdateRuntime(GameTime gameTime, RenderTargetGroup finalTarget)
|
public void UpdateRuntime(GameTime gameTime, RenderTargetGroup finalTarget)
|
||||||
{
|
{
|
||||||
|
// TODO: adapt changes from UpdateEditor!
|
||||||
|
Runtime.NotImplemented();
|
||||||
|
/*
|
||||||
Debug.Profiler.ProfileRendererFunction!();
|
Debug.Profiler.ProfileRendererFunction!();
|
||||||
|
|
||||||
TransformSystem.Update(_ecsWorld);
|
TransformSystem.Update(_ecsWorld);
|
||||||
@@ -109,6 +121,7 @@ namespace GlitchyEngine.World
|
|||||||
|
|
||||||
renderTarget?.ReleaseRef();
|
renderTarget?.ReleaseRef();
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateEditor(GameTime gameTime, EditorCamera camera, RenderTargetGroup viewportTarget, delegate void() DebugDraw3D, delegate void() DrawDebug2D)
|
public void UpdateEditor(GameTime gameTime, EditorCamera camera, RenderTargetGroup viewportTarget, delegate void() DebugDraw3D, delegate void() DrawDebug2D)
|
||||||
@@ -120,7 +133,7 @@ namespace GlitchyEngine.World
|
|||||||
TransformSystem.Update(_ecsWorld);
|
TransformSystem.Update(_ecsWorld);
|
||||||
|
|
||||||
// 3D render
|
// 3D render
|
||||||
Renderer.BeginScene(camera, viewportTarget);
|
Renderer.BeginScene(camera, _compositeTarget);
|
||||||
|
|
||||||
for (var (entity, transform, mesh, meshRenderer) in _ecsWorld.Enumerate<TransformComponent, MeshComponent, MeshRendererComponent>())
|
for (var (entity, transform, mesh, meshRenderer) in _ecsWorld.Enumerate<TransformComponent, MeshComponent, MeshRendererComponent>())
|
||||||
{
|
{
|
||||||
@@ -136,9 +149,10 @@ namespace GlitchyEngine.World
|
|||||||
|
|
||||||
Renderer.EndScene();
|
Renderer.EndScene();
|
||||||
|
|
||||||
// TODO: alphablending
|
// TODO: alphablending (handle in Renderer2D)
|
||||||
|
// TODO: 2D-Postprocessing requires rendering into separate target instead of directly into compositeTarget
|
||||||
|
|
||||||
RenderCommand.SetRenderTargetGroup(camera.RenderTarget);
|
RenderCommand.SetRenderTargetGroup(_compositeTarget);
|
||||||
RenderCommand.BindRenderTargets();
|
RenderCommand.BindRenderTargets();
|
||||||
|
|
||||||
// Sprite renderer
|
// Sprite renderer
|
||||||
@@ -157,7 +171,7 @@ namespace GlitchyEngine.World
|
|||||||
|
|
||||||
Renderer2D.EndScene();
|
Renderer2D.EndScene();
|
||||||
|
|
||||||
// TODO: cameraTarget into viewportTarget (gamma correction!)
|
// TODO: compositeTarget into viewportTarget (gamma correction!)
|
||||||
|
|
||||||
viewportTarget.ReleaseRef();
|
viewportTarget.ReleaseRef();
|
||||||
}
|
}
|
||||||
@@ -206,6 +220,8 @@ namespace GlitchyEngine.World
|
|||||||
cameraComponent.Camera.SetViewportSize(width, height);
|
cameraComponent.Camera.SetViewportSize(width, height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_compositeTarget.Resize(ViewportWidth, ViewportHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnComponentAdded(Entity entity, Type componentType, void* component)
|
private void OnComponentAdded(Entity entity, Type componentType, void* component)
|
||||||
|
|||||||
Reference in New Issue
Block a user