Gizmo improvements

This commit is contained in:
Simon Lübeß
2022-05-15 14:36:07 +02:00
parent 3956080520
commit 62bb5baac9
8 changed files with 108 additions and 38 deletions
@@ -41,12 +41,14 @@ namespace GlitchyEditor.EditWindows
ImGui.PushStyleVar(.WindowPadding, ImGui.Vec2(1, 1)); ImGui.PushStyleVar(.WindowPadding, ImGui.Vec2(1, 1));
defer ImGui.PopStyleVar(); defer ImGui.PopStyleVar();
if(!ImGui.Begin(s_WindowTitle, &_open, .NoScrollbar)) if(!ImGui.Begin(s_WindowTitle, &_open, .NoScrollbar | .MenuBar))
{ {
ImGui.End(); ImGui.End();
return; return;
} }
ShowMenuBar();
if(ImGui.IsWindowHovered() && Input.IsMouseButtonPressing(.RightButton)) if(ImGui.IsWindowHovered() && Input.IsMouseButtonPressing(.RightButton))
{ {
var currentWindow = ImGui.GetCurrentWindow(); var currentWindow = ImGui.GetCurrentWindow();
@@ -74,8 +76,60 @@ namespace GlitchyEditor.EditWindows
} }
} }
private ImGuizmo.OPERATION _gizmoType = .TRANSLATE;
private ImGuizmo.MODE _gizmoMode = .LOCAL;
private void ShowMenuBar()
{
if(ImGui.BeginMenuBar())
{
if (ImGui.RadioButton("Position", _gizmoType.HasFlag(.TRANSLATE)))
{
if (Input.IsKeyPressed(Key.Control))
_gizmoType ^= .TRANSLATE;
else
_gizmoType = .TRANSLATE;
}
if (ImGui.RadioButton("Rotation", _gizmoType.HasFlag(.ROTATE)))
{
if (Input.IsKeyPressed(Key.Control))
_gizmoType ^= .ROTATE;
else
_gizmoType = .ROTATE;
}
if (ImGui.RadioButton("Scale", _gizmoType.HasFlag(.SCALE)))
{
if (Input.IsKeyPressed(Key.Control))
_gizmoType ^= .SCALE;
else
_gizmoType = .SCALE;
}
if (ImGui.RadioButton("All", _gizmoType == .TRANSLATE | .ROTATE | .SCALE))
_gizmoType = .TRANSLATE | .ROTATE | .SCALE;
// If we scale, the mode must be local otherwise we could skew the matrix.
if (_gizmoType.HasFlag(.SCALE))
_gizmoMode = .LOCAL;
if (ImGui.MenuItem(_gizmoMode == .WORLD ? "Global" : "Local", null, true, !_gizmoType.HasFlag(.SCALE)))
{
if (_gizmoMode == .WORLD)
_gizmoMode = .LOCAL;
else
_gizmoMode = .WORLD;
}
ImGui.EndMenuBar();
}
}
private void DrawImGuizmo(ImGui.Vec2 viewportSize) private void DrawImGuizmo(ImGui.Vec2 viewportSize)
{ {
ImGuizmo.SetOrthographic(false);
ImGuizmo.SetDrawlist(); ImGuizmo.SetDrawlist();
var topLeft = ImGui.GetWindowPos(); var topLeft = ImGui.GetWindowPos();
@@ -85,28 +139,34 @@ namespace GlitchyEditor.EditWindows
topLeft.y += cntMin.y; topLeft.y += cntMin.y;
ImGuizmo.SetRect(topLeft.x, topLeft.y, viewportSize.x, viewportSize.y); ImGuizmo.SetRect(topLeft.x, topLeft.y, viewportSize.x, viewportSize.y);
var cameraTransformCmp = _editor.CurrentCamera.GetComponent<TransformComponent>(); var cameraTransformCmp = _editor.CurrentScene.ActiveCamera.GetComponent<TransformComponent>();
var view = cameraTransformCmp.WorldTransform.Invert(); var view = cameraTransformCmp.WorldTransform.Invert();
var cameraCmp = _editor.CurrentCamera.GetComponent<CameraComponent>(); var cameraCmp = _editor.CurrentCamera.GetComponent<CameraComponent>();
var projection = cameraCmp.Camera.Projection; var projection = cameraCmp.Camera.Projection;
Matrix mat = .Identity; if(_editor.EntityHierarchyWindow.SelectedEntities.Count == 0)
ImGuizmo.DrawGrid((.)&view, (.)&projection, (.)&mat, 10); return;
if(_editor.EntityHierarchyWindow.SelectedEntities.Count > 0) var entity = _editor.EntityHierarchyWindow.SelectedEntities.Back;
{
var entity = _editor.EntityHierarchyWindow.SelectedEntities.Front;
var transformCmp = entity.GetComponent<TransformComponent>(); var transformCmp = entity.GetComponent<TransformComponent>();
var transform = transformCmp.LocalTransform; var worldTransform = transformCmp.WorldTransform;
ImGuizmo.SetRect(topLeft.x, topLeft.y, viewportSize.x, viewportSize.y); Matrix parentView = .Identity;
if (transformCmp.Parent != .InvalidEntity)
{
var parentTransformCmp = Entity(transformCmp.Parent, entity.Scene).GetComponent<TransformComponent>();
parentView = parentTransformCmp.WorldTransform.Invert();
}
ImGuizmo.Manipulate((.)&view, (.)&projection, .TRANSLATE, .LOCAL, (.)&transform); if (ImGuizmo.Manipulate((.)&view, (.)&projection, _gizmoType, _gizmoMode, (.)&worldTransform))
{
transformCmp.LocalTransform = transform; // TODO: Fix when parent is scaled
// Seems to work fine for parent rotation and translation but scaled parent ruins everything
// (probably because scaling a rotated matrix results in a skewed matrix, but unity can do it, so should we)
transformCmp.LocalTransform = parentView * worldTransform;
} }
} }
} }
+5 -12
View File
@@ -28,7 +28,6 @@ namespace GlitchyEditor
{ {
_scene = value; _scene = value;
_entityHierarchyWindow.SetContext(_scene); _entityHierarchyWindow.SetContext(_scene);
FindCurrentEditorCamera();
} }
} }
@@ -45,26 +44,20 @@ namespace GlitchyEditor
public void Update() public void Update()
{ {
var scriptComponent = CurrentCamera.GetComponent<NativeScriptComponent>(); _currentCamera = _scene.ActiveCamera;
if (_currentCamera.IsValid)
{
var scriptComponent = _currentCamera.GetComponent<NativeScriptComponent>();
if (var camController = scriptComponent?.Instance as EditorCameraController) if (var camController = scriptComponent?.Instance as EditorCameraController)
{ {
camController.IsEnabled = (SceneViewportWindow.HasFocus && Input.IsMouseButtonPressed(.RightButton)); camController.IsEnabled = (SceneViewportWindow.HasFocus && Input.IsMouseButtonPressed(.RightButton));
} }
}
_entityHierarchyWindow.Show(); _entityHierarchyWindow.Show();
_componentEditWindow.Show(); _componentEditWindow.Show();
_sceneViewportWindow.Show(); _sceneViewportWindow.Show();
} }
private void FindCurrentEditorCamera()
{
_currentCamera = .(.InvalidEntity, _scene);
// TODO: a bit sketchy
for (var (entity, editComp, cam) in _scene.[Friend]_ecsWorld.Enumerate<EditorComponent, CameraComponent>())
{
_currentCamera = .(entity, CurrentScene);
}
}
} }
} }
+4 -4
View File
@@ -226,14 +226,14 @@ namespace GlitchyEditor
var fxLib = Application.Get().EffectLibrary; var fxLib = Application.Get().EffectLibrary;
using (Effect myEffect = fxLib.Load("content/Shaders/myEffect.hlsl")) using (Effect myEffect = fxLib.Load("content/Shaders/myEffect.hlsl"))
using (Texture2D albedo = new Texture2D("Textures/White.png", true)) /*using (Texture2D albedo = new Texture2D("Textures/White.png", true))
using (Texture2D normal = new Texture2D("Textures/White.png")) using (Texture2D normal = new Texture2D("Textures/White.png"))
using (Texture2D rough = new Texture2D("Textures/White.png")) using (Texture2D rough = new Texture2D("Textures/White.png"))
using (Texture2D metal = new Texture2D("Textures/White.png")) using (Texture2D metal = new Texture2D("Textures/White.png"))*/
/*using (Texture2D albedo = new Texture2D("Textures/TestMat/rustediron2_albedo.png", true)) using (Texture2D albedo = new Texture2D("Textures/TestMat/rustediron2_albedo.png", true))
using (Texture2D normal = new Texture2D("Textures/TestMat/rustediron2_normal.png")) using (Texture2D normal = new Texture2D("Textures/TestMat/rustediron2_normal.png"))
using (Texture2D rough = new Texture2D("Textures/TestMat/rustediron2_roughness.png")) using (Texture2D rough = new Texture2D("Textures/TestMat/rustediron2_roughness.png"))
using (Texture2D metal = new Texture2D("Textures/TestMat/rustediron2_metallic.png"))*/ using (Texture2D metal = new Texture2D("Textures/TestMat/rustediron2_metallic.png"))
{ {
albedo.SamplerState = SamplerStateManager.AnisotropicWrap; albedo.SamplerState = SamplerStateManager.AnisotropicWrap;
normal.SamplerState = SamplerStateManager.AnisotropicWrap; normal.SamplerState = SamplerStateManager.AnisotropicWrap;
+1 -1
View File
@@ -27,7 +27,7 @@ namespace GlitchyEngine.World
public ChildEnumerator EnumerateChildren => .(this); public ChildEnumerator EnumerateChildren => .(this);
public bool IsValid => _entity.IsValid; public bool IsValid => _entity.IsValid && _scene != null;
public Entity? Parent public Entity? Parent
{ {
+14
View File
@@ -13,6 +13,20 @@ 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 _;
public Entity ActiveCamera => {
Entity cameraEntity = .();
for (var (entity, camera) in _ecsWorld.Enumerate<CameraComponent>())
{
if (camera.Primary && camera.RenderTarget != null)
{
cameraEntity = .(entity, this);
}
}
cameraEntity
};
public this() public this()
{ {
Entity entity = CreateEntity("Green Quad"); Entity entity = CreateEntity("Green Quad");
@@ -43,7 +43,10 @@ namespace GlitchyEngine.World
_localTransform = value; _localTransform = value;
Matrix.Decompose(_localTransform, out _position, out _rotation, out _scale); Matrix.Decompose(_localTransform, out _position, let rotation, out _scale);
Rotation = rotation;
IsDirty = true; IsDirty = true;
} }
} }