From 0079b80b1c88e0a0e0869fe8418fc99932d5cf14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Thu, 16 Sep 2021 13:19:25 +0200 Subject: [PATCH] Created editor project and moved Editor there --- BeefSpace.toml | 4 +- GlitchyEditor/BeefProj.toml | 10 +++ GlitchyEditor/content/Shaders/lineShader.hlsl | 31 +++++++ .../content/Shaders/render2dShaderInst.hlsl | 37 ++++++++ GlitchyEditor/content/Shaders/testShader.hlsl | 84 +++++++++++++++++ .../src/EditWindows}/ComponentEditWindow.bf | 2 +- .../src/EditWindows}/EntityHierarchyWindow.bf | 5 +- .../Editor => GlitchyEditor/src}/Editor.bf | 3 +- GlitchyEditor/src/EditorApp.bf | 19 ++++ GlitchyEditor/src/EditorLayer.bf | 90 +++++++++++++++++++ Sandbox/src/SandboxApp.bf | 4 - 11 files changed, 279 insertions(+), 10 deletions(-) create mode 100644 GlitchyEditor/BeefProj.toml create mode 100644 GlitchyEditor/content/Shaders/lineShader.hlsl create mode 100644 GlitchyEditor/content/Shaders/render2dShaderInst.hlsl create mode 100644 GlitchyEditor/content/Shaders/testShader.hlsl rename {GlitchyEngine/src/Editor => GlitchyEditor/src/EditWindows}/ComponentEditWindow.bf (98%) rename {GlitchyEngine/src/Editor => GlitchyEditor/src/EditWindows}/EntityHierarchyWindow.bf (98%) rename {GlitchyEngine/src/Editor => GlitchyEditor/src}/Editor.bf (97%) create mode 100644 GlitchyEditor/src/EditorApp.bf create mode 100644 GlitchyEditor/src/EditorLayer.bf diff --git a/BeefSpace.toml b/BeefSpace.toml index 573f407..4b74bad 100644 --- a/BeefSpace.toml +++ b/BeefSpace.toml @@ -1,8 +1,8 @@ FileVersion = 1 -Projects = {Sandbox = {Path = "Sandbox"}, GlitchyEngine = {Path = "GlitchyEngine"}, GlitchLog = {Path = "GlitchLog"}, DirectX = {Path = "GlitchyEngine/vendor/directx/DirectX"}, ImGui = {Path = "GlitchyEngine/vendor/imgui-beef/ImGui"}, ImGuiImplWin32 = {Path = "GlitchyEngine/vendor/imgui-beef/ImGuiImplWin32"}, ImGuiImplDX11 = {Path = "GlitchyEngine/vendor/imgui-beef/ImGuiImplDX11"}, DirectXTK = {Path = "GlitchyEngine/vendor/DirectXTK/DirectXTK-beef"}, LodePng = {Path = "vendor/lodepng-beef/lodepng-beef"}, FreeType = {Path = "GlitchyEngine/vendor/freetype"}, cgltf-beef = {Path = "GlitchyEngine/vendor/gltf/cgltf-beef"}} +Projects = {Sandbox = {Path = "Sandbox"}, GlitchyEngine = {Path = "GlitchyEngine"}, GlitchLog = {Path = "GlitchLog"}, DirectX = {Path = "GlitchyEngine/vendor/directx/DirectX"}, ImGui = {Path = "GlitchyEngine/vendor/imgui-beef/ImGui"}, ImGuiImplWin32 = {Path = "GlitchyEngine/vendor/imgui-beef/ImGuiImplWin32"}, ImGuiImplDX11 = {Path = "GlitchyEngine/vendor/imgui-beef/ImGuiImplDX11"}, DirectXTK = {Path = "GlitchyEngine/vendor/DirectXTK/DirectXTK-beef"}, LodePng = {Path = "vendor/lodepng-beef/lodepng-beef"}, FreeType = {Path = "GlitchyEngine/vendor/freetype"}, cgltf-beef = {Path = "GlitchyEngine/vendor/gltf/cgltf-beef"}, GlitchyEditor = {Path = "GlitchyEditor"}} [Workspace] -StartupProject = "Sandbox" +StartupProject = "GlitchyEditor" [Configs.Debug.Win64] AllocType = "CRT" diff --git a/GlitchyEditor/BeefProj.toml b/GlitchyEditor/BeefProj.toml new file mode 100644 index 0000000..becc4c5 --- /dev/null +++ b/GlitchyEditor/BeefProj.toml @@ -0,0 +1,10 @@ +FileVersion = 1 +Dependencies = {corlib = "*", GlitchLog = "*", GlitchyEngine = "*"} + +[Project] +Name = "GlitchyEditor" +TargetType = "BeefGUIApplication" +StartupObject = "GlitchyEngine.Program" + +[Configs.Debug.Win64] +CLibType = "DynamicDebug" diff --git a/GlitchyEditor/content/Shaders/lineShader.hlsl b/GlitchyEditor/content/Shaders/lineShader.hlsl new file mode 100644 index 0000000..1353f92 --- /dev/null +++ b/GlitchyEditor/content/Shaders/lineShader.hlsl @@ -0,0 +1,31 @@ +cbuffer Constants : register(b0) +{ + float4x4 ViewProjection; + float4 Color; +}; + +struct VS_Input +{ + float4 Position : POSITION; +}; + +struct PS_Input +{ + float4 Position : SV_Position; +}; + +PS_Input VS(VS_Input input) +{ + PS_Input output; + + output.Position = mul(ViewProjection, input.Position); + + return output; +} + +float4 PS(PS_Input input) : SV_Target0 +{ + return Color; +} + +#effect[VS=VS, PS=PS] diff --git a/GlitchyEditor/content/Shaders/render2dShaderInst.hlsl b/GlitchyEditor/content/Shaders/render2dShaderInst.hlsl new file mode 100644 index 0000000..31b234b --- /dev/null +++ b/GlitchyEditor/content/Shaders/render2dShaderInst.hlsl @@ -0,0 +1,37 @@ +Texture2D Texture : register(t0); +SamplerState Sampler : register(s0); + +struct VS_Input +{ + float2 Position : POSITION; + float4x4 Tranform : TRANSFORM; + float4 Color : COLOR; + float4 UVTransform : TEXCOORD; +}; + +struct PS_Input +{ + float4 Position : SV_Position; + float2 TexCoord : TEXCOORD0; + float4 Color : COLOR; +}; + +PS_Input VS(VS_Input input) +{ + PS_Input output; + + output.Position = mul(float4(input.Position, 0.0f, 1.0f), input.Tranform); + output.TexCoord = input.UVTransform.xy + input.UVTransform.zw * input.Position; + output.Color = input.Color; + + return output; +} + +float4 PS(PS_Input input) : SV_Target0 +{ + float4 color = input.Color * Texture.Sample(Sampler, input.TexCoord); + + return color; +} + +#effect[VS=VS, PS=PS] \ No newline at end of file diff --git a/GlitchyEditor/content/Shaders/testShader.hlsl b/GlitchyEditor/content/Shaders/testShader.hlsl new file mode 100644 index 0000000..cf953ea --- /dev/null +++ b/GlitchyEditor/content/Shaders/testShader.hlsl @@ -0,0 +1,84 @@ +cbuffer SceneConstants +{ + float4x4 ViewProjection = float4x4(1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1); +} + +cbuffer ObjectConstants +{ + float4x4 Transform; +} + +cbuffer Constants +{ + float4 BaseColor; + float3 LightDir; +} + +cbuffer SkinningMatrices +{ + matrix SkinningMatrices[255]; + float3x3 InvTransSkinningMatrices[255]; +} + +struct VS_IN +{ + float3 Position : POSITION; + float3 Normal : NORMAL; + uint4 JointIndices : JOINTS_0; + float4 JointWeights : WEIGHTS_0; +}; + +struct PS_IN +{ + float4 Position : SV_POSITION; + float3 Normal : NORMAL; +}; + +PS_IN VS(VS_IN input) +{ + PS_IN output; + + // Unanimated position in modelspace + float4 positionRaw = float4(input.Position, 1); + + // Calculate animated position in modelspace + + float4 position = + mul(SkinningMatrices[input.JointIndices.x], positionRaw) * input.JointWeights.x + + mul(SkinningMatrices[input.JointIndices.y], positionRaw) * input.JointWeights.y + + mul(SkinningMatrices[input.JointIndices.z], positionRaw) * input.JointWeights.z + + mul(SkinningMatrices[input.JointIndices.w], positionRaw) * input.JointWeights.w; + + float3 normal = + mul(InvTransSkinningMatrices[input.JointIndices.x], input.Normal) * input.JointWeights.x + + mul(InvTransSkinningMatrices[input.JointIndices.y], input.Normal) * input.JointWeights.y + + mul(InvTransSkinningMatrices[input.JointIndices.z], input.Normal) * input.JointWeights.z + + mul(InvTransSkinningMatrices[input.JointIndices.w], input.Normal) * input.JointWeights.w; + + //position = saturate(position - 1000) + positionRaw; + + //float4 position = mul(SkinningMatrices[input.JointIndices.x], positionRaw); + + float4 worldPosition = mul(Transform, position); + + output.Position = mul(ViewProjection, worldPosition); + output.Normal = mul((float3x3)Transform, normal); + + return output; +} + +float4 PS(PS_IN input) : SV_TARGET +{ + input.Normal = normalize(input.Normal); + + float shading = dot(LightDir, input.Normal); + //float shading = dot(LightDir, LightDir) + 1; + shading = clamp(shading, 0.0f, 1.0f); + + return BaseColor * shading; +} + +#effect[VS=VS,PS=PS] diff --git a/GlitchyEngine/src/Editor/ComponentEditWindow.bf b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf similarity index 98% rename from GlitchyEngine/src/Editor/ComponentEditWindow.bf rename to GlitchyEditor/src/EditWindows/ComponentEditWindow.bf index 9a20fe1..62769b3 100644 --- a/GlitchyEngine/src/Editor/ComponentEditWindow.bf +++ b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf @@ -3,7 +3,7 @@ using GlitchyEngine.World; using System; using GlitchyEngine.Math; -namespace GlitchyEngine.Editor +namespace GlitchyEditor.EditWindows { class ComponentEditWindow { diff --git a/GlitchyEngine/src/Editor/EntityHierarchyWindow.bf b/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf similarity index 98% rename from GlitchyEngine/src/Editor/EntityHierarchyWindow.bf rename to GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf index b3ac576..dc25580 100644 --- a/GlitchyEngine/src/Editor/EntityHierarchyWindow.bf +++ b/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf @@ -3,10 +3,11 @@ using GlitchyEngine.World; using ImGui; using System; using System.Collections; +using GlitchyEngine; -namespace GlitchyEngine.Editor +namespace GlitchyEditor.EditWindows { - using internal GlitchyEngine.Editor; + using internal GlitchyEditor; /// A window for viewing and editing the scene hierarchy class EntityHierarchyWindow diff --git a/GlitchyEngine/src/Editor/Editor.bf b/GlitchyEditor/src/Editor.bf similarity index 97% rename from GlitchyEngine/src/Editor/Editor.bf rename to GlitchyEditor/src/Editor.bf index 55276c4..6e8b9f9 100644 --- a/GlitchyEngine/src/Editor/Editor.bf +++ b/GlitchyEditor/src/Editor.bf @@ -3,8 +3,9 @@ using ImGui; using System; using System.Collections; using GlitchyEngine.Collections; +using GlitchyEditor.EditWindows; -namespace GlitchyEngine.Editor +namespace GlitchyEditor { class Editor { diff --git a/GlitchyEditor/src/EditorApp.bf b/GlitchyEditor/src/EditorApp.bf new file mode 100644 index 0000000..fd66144 --- /dev/null +++ b/GlitchyEditor/src/EditorApp.bf @@ -0,0 +1,19 @@ +using System; +using GlitchyEngine; + +namespace GlitchyEditor +{ + class SandboxApp : Application + { + public this() + { + PushLayer(new EditorLayer()); + } + + [Export, LinkName("CreateApplication")] + public static Application CreateApplication() + { + return new SandboxApp(); + } + } +} diff --git a/GlitchyEditor/src/EditorLayer.bf b/GlitchyEditor/src/EditorLayer.bf new file mode 100644 index 0000000..8fa24f5 --- /dev/null +++ b/GlitchyEditor/src/EditorLayer.bf @@ -0,0 +1,90 @@ +using GlitchyEditor.EditWindows; +using GlitchyEngine; +using GlitchyEngine.Events; +using GlitchyEngine.ImGui; +using GlitchyEngine.Renderer; +using GlitchyEngine.World; +using ImGui; + +namespace GlitchyEditor +{ + class EditorLayer : Layer + { + RasterizerState _rasterizerState ~ _?.ReleaseRef(); + RasterizerState _rasterizerStateClockWise ~ _?.ReleaseRef(); + + GraphicsContext _context ~ _?.ReleaseRef(); + DepthStencilTarget _depthTarget ~ _?.ReleaseRef(); + + BlendState _alphaBlendState ~ _?.ReleaseRef(); + BlendState _opaqueBlendState ~ _?.ReleaseRef(); + + EcsWorld _world = new EcsWorld() ~ delete _; + + Editor _editor = new Editor(_world) ~ delete _; + + public this() : base("Example") + { + Application.Get().Window.IsVSync = false; + + InitGraphics(); + InitEcs(); + } + + private void InitGraphics() + { + _context = Application.Get().Window.Context..AddRef(); + + _depthTarget = new DepthStencilTarget(_context, _context.SwapChain.Width, _context.SwapChain.Height); + + RasterizerStateDescription rsDesc = .(.Solid, .Back, true); + _rasterizerState = new RasterizerState(_context, rsDesc); + + rsDesc.FrontCounterClockwise = false; + _rasterizerStateClockWise = new RasterizerState(_context, rsDesc); + + BlendStateDescription blendDesc = .(); + blendDesc.RenderTarget[0] = .(true, .SourceAlpha, .InvertedSourceAlpha, .Add, .SourceAlpha, .InvertedSourceAlpha, .Add, .All); + _alphaBlendState = new BlendState(_context, blendDesc); + _opaqueBlendState = new BlendState(_context, .Default); + } + + private void InitEcs() + { + _world.Register(); + _world.Register(); + _world.Register(); + _world.Register(); + _world.Register(); + _world.Register(); + _world.Register(); + _world.Register(); + } + + public override void Update(GameTime gameTime) + { + RenderCommand.Clear(null, .(0.2f, 0.2f, 0.2f)); + _depthTarget.Clear(1.0f, 0, .Depth); + + _context.SetRenderTarget(null); + _depthTarget.Bind(); + _context.BindRenderTargets(); + + _context.SetViewport(_context.SwapChain.BackbufferViewport); + } + + public override void OnEvent(Event event) + { + EventDispatcher dispatcher = EventDispatcher(event); + + dispatcher.Dispatch(scope (e) => OnImGuiRender(e)); + } + + private bool OnImGuiRender(ImGuiRenderEvent event) + { + _editor.Update(); + + return false; + } + } +} diff --git a/Sandbox/src/SandboxApp.bf b/Sandbox/src/SandboxApp.bf index d79bc36..eb36a42 100644 --- a/Sandbox/src/SandboxApp.bf +++ b/Sandbox/src/SandboxApp.bf @@ -11,7 +11,6 @@ using GlitchyEngine.World; using System.Collections; using GlitchyEngine.Content; using GlitchyEngine.Renderer.Animation; -using GlitchyEngine.Editor; namespace Sandbox { @@ -74,8 +73,6 @@ namespace Sandbox EcsWorld _world = new EcsWorld() ~ delete _; - Editor _editor = new Editor(_world) ~ delete _; - // OrthographicCameraController _cameraController ~ delete _; PerspectiveCameraController _cameraController ~ delete _; @@ -544,7 +541,6 @@ namespace Sandbox private bool OnImGuiRender(ImGuiRenderEvent e) { - _editor.Update(); /* ImGui.Begin("Test");