Terrible postprocessing and start of Editor GUI for lights

This commit is contained in:
Simon Lübeß
2022-05-11 22:49:36 +02:00
parent 56e92a7d67
commit b1a1268319
7 changed files with 166 additions and 56 deletions
@@ -0,0 +1,37 @@
Texture2D CameraTarget : register(t0);
SamplerState CameraTargetSampler : register(s0);
struct VS_IN
{
float2 Position : POSITION;
float2 TexCoord : TEXCOORD0;
};
struct PS_IN
{
float4 Position : SV_POSITION;
float2 TexCoord : TEXCOORD;
};
PS_IN VS(VS_IN input)
{
PS_IN output;
output.Position = float4(input.Position, 0, 1);
output.TexCoord = input.TexCoord;
return output;
}
float4 PS(PS_IN input) : SV_TARGET
{
float4 rawColor = CameraTarget.Sample(CameraTargetSampler, input.TexCoord);
float3 color = rawColor.rgb / (rawColor.rgb + 1.0f);
color = pow(color, 1.0f / 2.2f);
return float4(color, 1);
}
#effect[VS = VS, PS = PS]
@@ -108,15 +108,7 @@ float4 PS(PS_IN input) : SV_TARGET
float3 luminanceColor = LightColor * Illuminance; float3 luminanceColor = LightColor * Illuminance;
float3 cook = (kd * diffuse + specular) * luminanceColor * n_dot_l; float3 final = (kd * diffuse + specular) * luminanceColor * n_dot_l;
float3 final = cook;
// Tone mapping // TODO: do in postprocessing
final = final / (final + 1.0f);
// Gamma correction // TODO: do in postprocessing/hardware
final = pow(final, 1.0f / 2.2f);
#if OUTPUT == Inspect_NormalDistribution #if OUTPUT == Inspect_NormalDistribution
final = max(final - 10000000, nrmDist.xxx); final = max(final - 10000000, nrmDist.xxx);
@@ -61,6 +61,7 @@ namespace GlitchyEditor.EditWindows
ShowComponentEditor<CameraComponent>("Camera", entity, => ShowCameraComponentEditor, => ShowComponentContextMenu<CameraComponent>); ShowComponentEditor<CameraComponent>("Camera", entity, => ShowCameraComponentEditor, => ShowComponentContextMenu<CameraComponent>);
ShowComponentEditor<SpriterRendererComponent>("Sprite Renderer", entity, => ShowSpriteRendererComponentEditor, => ShowComponentContextMenu<SpriterRendererComponent>); ShowComponentEditor<SpriterRendererComponent>("Sprite Renderer", entity, => ShowSpriteRendererComponentEditor, => ShowComponentContextMenu<SpriterRendererComponent>);
ShowComponentEditor<MeshRendererComponent>("Mesh Renderer", entity, => ShowMeshRendererComponentEditor, => ShowComponentContextMenu<MeshRendererComponent>); ShowComponentEditor<MeshRendererComponent>("Mesh Renderer", entity, => ShowMeshRendererComponentEditor, => ShowComponentContextMenu<MeshRendererComponent>);
ShowComponentEditor<LightComponent>("Mesh Renderer", entity, => ShowLightComponentEditor, => ShowComponentContextMenu<LightComponent>);
ShowAddComponentButton(entity); ShowAddComponentButton(entity);
} }
@@ -349,6 +350,58 @@ namespace GlitchyEditor.EditWindows
} }
} }
private static void LabelColumn(StringView label)
{
ImGui.TextUnformatted(label);
ImGui.NextColumn();
}
private static void ShowLightComponentEditor(Entity entity, LightComponent* lightComponent)
{
ImGui.Columns(2);
defer ImGui.Columns(1);
const String[?] strings = String[]("Directional", "Point", "Spot");
var light = ref lightComponent.SceneLight;
String typeName = strings[light.LightType.Underlying];
LabelColumn("Type");
if (ImGui.BeginCombo("##Type", typeName.CStr()))
{
for (int i = 0; i < 3; i++)
{
bool isSelected = (typeName == strings[i]);
if (ImGui.Selectable(strings[i], isSelected))
{
light.LightType = (.)i;
}
if (isSelected)
ImGui.SetItemDefaultFocus();
}
ImGui.EndCombo();
}
ImGui.NextColumn();
LabelColumn("Color");
ColorRGB color = light.Color;
if (ImGui.ColorEdit3("##Color", ref color))
light.Color = color;
ImGui.NextColumn();
LabelColumn("Illuminance");
float illuminance = light.Illuminance;
if (ImGui.DragFloat("##Illuminance", &illuminance, 0.1f, 0.0f, float.MaxValue))
light.Illuminance = illuminance;
}
private static void ShowAddComponentButton(Entity entity) private static void ShowAddComponentButton(Entity entity)
{ {
static char8[128] searchBuffer = .(); static char8[128] searchBuffer = .();
+35 -15
View File
@@ -28,8 +28,8 @@ namespace GlitchyEditor
Editor _editor ~ delete _; Editor _editor ~ delete _;
RenderTarget2D _intermediateTarget ~ _?.ReleaseRef(); RenderTarget2D _cameraTarget ~ _.ReleaseRef();
RenderTarget2D _viewportTarget ~ _?.ReleaseRef(); RenderTarget2D _viewportTarget ~ _.ReleaseRef();
SettingsWindow _settingsWindow = new .() ~ delete _; SettingsWindow _settingsWindow = new .() ~ delete _;
@@ -87,16 +87,18 @@ namespace GlitchyEditor
camera.Camera.SetPerspective(MathHelper.ToRadians(75), 0.1f, 10000.0f); camera.Camera.SetPerspective(MathHelper.ToRadians(75), 0.1f, 10000.0f);
camera.Primary = true; camera.Primary = true;
camera.FixedAspectRatio = false; camera.FixedAspectRatio = false;
camera.RenderTarget = _viewportTarget; camera.RenderTarget = _cameraTarget;
let transform = _cameraEntity.GetComponent<TransformComponent>(); let transform = _cameraEntity.GetComponent<TransformComponent>();
transform.Position = .(-1.5f, 1.5f, -2.5f); //transform.Position = .(-1.5f, 1.5f, -2.5f);
transform.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(35), 0); 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);
_cameraEntity.AddComponent<NativeScriptComponent>().Bind<EditorCameraController>(); _cameraEntity.AddComponent<NativeScriptComponent>().Bind<EditorCameraController>();
_cameraEntity.AddComponent<EditorComponent>(); _cameraEntity.AddComponent<EditorComponent>();
} }
{ /*{
_otherCameraEntity = _scene.CreateEntity("Other Camera Entity"); _otherCameraEntity = _scene.CreateEntity("Other Camera Entity");
let camera = _otherCameraEntity.AddComponent<CameraComponent>(); let camera = _otherCameraEntity.AddComponent<CameraComponent>();
camera.Camera.SetPerspective(MathHelper.ToRadians(45), 0.1f, 1000.0f); camera.Camera.SetPerspective(MathHelper.ToRadians(45), 0.1f, 1000.0f);
@@ -108,13 +110,13 @@ namespace GlitchyEditor
_otherCameraEntity.AddComponent<NativeScriptComponent>().Bind<EditorCameraController>(); _otherCameraEntity.AddComponent<NativeScriptComponent>().Bind<EditorCameraController>();
_otherCameraEntity.AddComponent<EditorComponent>(); _otherCameraEntity.AddComponent<EditorComponent>();
} }*/
{ {
var lightNtt = _scene.CreateEntity("My Sexy Sun"); var lightNtt = _scene.CreateEntity("My Sexy Sun");
let transform = lightNtt.GetComponent<TransformComponent>(); let transform = lightNtt.GetComponent<TransformComponent>();
transform.Position = .(0, 0, 0); transform.Position = .(0, 0, 0);
transform.RotationEuler = .(MathHelper.ToRadians(45), MathHelper.ToRadians(-45), 0); transform.RotationEuler = .(MathHelper.ToRadians(45), MathHelper.ToRadians(-100), 0);
let light = lightNtt.AddComponent<LightComponent>(); let light = lightNtt.AddComponent<LightComponent>();
light.SceneLight.Illuminance = 10.0f; light.SceneLight.Illuminance = 10.0f;
@@ -132,6 +134,17 @@ namespace GlitchyEditor
light.SceneLight.Color = .(1.0f, 0.95f, 0.8f); light.SceneLight.Color = .(1.0f, 0.95f, 0.8f);
} }
{
var lightNtt = _scene.CreateEntity("My Sexy Sun 3");
let transform = lightNtt.GetComponent<TransformComponent>();
transform.Position = .(0, 0, 0);
transform.RotationEuler = .(MathHelper.ToRadians(20), MathHelper.ToRadians(-55), 0);
let light = lightNtt.AddComponent<LightComponent>();
light.SceneLight.Illuminance = 10.0f;
light.SceneLight.Color = .(1.0f, 0.95f, 0.8f);
}
InitEditor(); InitEditor();
TestEntitiesWithModels(); TestEntitiesWithModels();
@@ -160,8 +173,11 @@ namespace GlitchyEditor
mat.SetTexture("NormalTexture", normal); mat.SetTexture("NormalTexture", normal);
mat.SetTexture("MetallicTexture", metal); mat.SetTexture("MetallicTexture", metal);
mat.SetTexture("RoughnessTexture", rough); mat.SetTexture("RoughnessTexture", rough);
//mat.SetVariable("BaseColor", Vector4(1, 0, 1, 1));
//mat.SetVariable("LightDir", Vector3(1, 1, 0).Normalized()); mat.SetVariable("AlbedoColor", ColorRGBA.White);
mat.SetVariable("NormalScaling", Vector2.One);
mat.SetVariable("MetallicFactor", 1.0f);
mat.SetVariable("RoughnessFactor", 1.0f);
EcsEntity e = ModelLoader.LoadModel("content/Models/sphere.glb", myEffect, mat, _scene.[Friend]_ecsWorld, clips, "Sphere 1"); EcsEntity e = ModelLoader.LoadModel("content/Models/sphere.glb", myEffect, mat, _scene.[Friend]_ecsWorld, clips, "Sphere 1");
@@ -240,7 +256,10 @@ namespace GlitchyEditor
DepthStencilStateDescription dsDesc = .(); DepthStencilStateDescription dsDesc = .();
_depthStencilState = new DepthStencilState(dsDesc); _depthStencilState = new DepthStencilState(dsDesc);
_viewportTarget = new RenderTarget2D(RenderTarget2DDescription(.R8G8B8A8_UNorm, 100, 100) {DepthStencilFormat = .D32_Float}); _cameraTarget = new RenderTarget2D(RenderTarget2DDescription(.R16G16B16A16_Float, 100, 100) {DepthStencilFormat = .D32_Float});
_cameraTarget.SamplerState = SamplerStateManager.LinearClamp;
_viewportTarget = new RenderTarget2D(RenderTarget2DDescription(.R8G8B8A8_UNorm, 100, 100));
_viewportTarget.SamplerState = SamplerStateManager.LinearClamp; _viewportTarget.SamplerState = SamplerStateManager.LinearClamp;
} }
@@ -281,7 +300,7 @@ namespace GlitchyEditor
//Renderer.EndScene(); //Renderer.EndScene();
_scene.Update(gameTime); _scene.Update(gameTime, _viewportTarget);
RenderCommand.Clear(null, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0); RenderCommand.Clear(null, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0);
@@ -305,9 +324,9 @@ namespace GlitchyEditor
private bool OnImGuiRender(ImGuiRenderEvent event) private bool OnImGuiRender(ImGuiRenderEvent event)
{ {
//viewer.ViewTexture(Renderer.[Friend]_gBuffer.Normal); viewer.ViewTexture(_cameraTarget);
ImGui.Begin("Test"); /*ImGui.Begin("Test");
static bool cameraA = true; static bool cameraA = true;
@@ -317,7 +336,7 @@ namespace GlitchyEditor
_otherCameraEntity.GetComponent<CameraComponent>().Primary = !cameraA; _otherCameraEntity.GetComponent<CameraComponent>().Primary = !cameraA;
} }
ImGui.End(); ImGui.End();*/
ImGui.Viewport* viewport = ImGui.GetMainViewport(); ImGui.Viewport* viewport = ImGui.GetMainViewport();
ImGui.DockSpaceOverViewport(viewport); ImGui.DockSpaceOverViewport(viewport);
@@ -383,6 +402,7 @@ namespace GlitchyEditor
return; return;
_viewportTarget.Resize(sizeX, sizeY); _viewportTarget.Resize(sizeX, sizeY);
_cameraTarget.Resize(sizeX, sizeY);
_scene.OnViewportResize(sizeX, sizeY); _scene.OnViewportResize(sizeX, sizeY);
} }
+28 -24
View File
@@ -12,6 +12,7 @@ namespace GlitchyEngine.Renderer
public Matrix ViewProjection; public Matrix ViewProjection;
public Vector3 CameraPosition; public Vector3 CameraPosition;
public RenderTarget2D CameraTarget; public RenderTarget2D CameraTarget;
public RenderTarget2D CompositionTarget;
} }
struct ObjectConstants struct ObjectConstants
@@ -26,6 +27,11 @@ namespace GlitchyEngine.Renderer
public DepthStencilTarget DepthStencil; public DepthStencilTarget DepthStencil;
public uint32 Width => _width;
public uint32 Height => _height;
public Int2 Size => .(_width, _height);
// RGB: Albedo.rgb A: ? // RGB: Albedo.rgb A: ?
public RenderTarget2D Albedo ~ _?.ReleaseRef(); public RenderTarget2D Albedo ~ _?.ReleaseRef();
// RG: TextureNormal.xy BA: GeometryNormal.xy // RG: TextureNormal.xy BA: GeometryNormal.xy
@@ -114,6 +120,7 @@ namespace GlitchyEngine.Renderer
static GBuffer _gBuffer; static GBuffer _gBuffer;
static Effect TestFullscreenEffect; static Effect TestFullscreenEffect;
static Effect s_tonemappingEffect;
static BlendState _gBufferBlend; static BlendState _gBufferBlend;
static BlendState _lightBlend; static BlendState _lightBlend;
@@ -188,6 +195,7 @@ namespace GlitchyEngine.Renderer
static void InitDeferredRenderer(EffectLibrary effectLibrary) static void InitDeferredRenderer(EffectLibrary effectLibrary)
{ {
TestFullscreenEffect = effectLibrary.Load("content\\Shaders\\simpleLight.hlsl"); TestFullscreenEffect = effectLibrary.Load("content\\Shaders\\simpleLight.hlsl");
s_tonemappingEffect = effectLibrary.Load("content\\Shaders\\SimpleTonemapping.hlsl");
s_fullscreenQuadGeometry = new GeometryBinding(); s_fullscreenQuadGeometry = new GeometryBinding();
s_fullscreenQuadGeometry.SetPrimitiveTopology(.TriangleList); s_fullscreenQuadGeometry.SetPrimitiveTopology(.TriangleList);
@@ -256,6 +264,7 @@ namespace GlitchyEngine.Renderer
delete _gBuffer; delete _gBuffer;
s_fullscreenQuadGeometry.ReleaseRef(); s_fullscreenQuadGeometry.ReleaseRef();
s_tonemappingEffect.ReleaseRef();
TestFullscreenEffect.ReleaseRef(); TestFullscreenEffect.ReleaseRef();
} }
@@ -268,7 +277,7 @@ namespace GlitchyEngine.Renderer
//_sceneConstants.Update(); //_sceneConstants.Update();
} }
public static void BeginScene(Camera camera, Matrix transform, RenderTarget2D renderTarget) public static void BeginScene(Camera camera, Matrix transform, RenderTarget2D renderTarget, RenderTarget2D finalTarget)
{ {
Debug.Profiler.ProfileRendererFunction!(); Debug.Profiler.ProfileRendererFunction!();
@@ -276,6 +285,7 @@ namespace GlitchyEngine.Renderer
_sceneConstants.ViewProjection = viewProjection; _sceneConstants.ViewProjection = viewProjection;
_sceneConstants.CameraPosition = transform.Translation; _sceneConstants.CameraPosition = transform.Translation;
_sceneConstants.CameraTarget = renderTarget; _sceneConstants.CameraTarget = renderTarget;
_sceneConstants.CompositionTarget = finalTarget;
} }
public static int SortMeshes(SubmittedMesh left, SubmittedMesh right) public static int SortMeshes(SubmittedMesh left, SubmittedMesh right)
@@ -361,12 +371,19 @@ namespace GlitchyEngine.Renderer
RenderCommand.SetRenderTarget(_sceneConstants.CameraTarget, 0, true); RenderCommand.SetRenderTarget(_sceneConstants.CameraTarget, 0, true);
RenderCommand.BindRenderTargets(); RenderCommand.BindRenderTargets();
RenderCommand.Clear(_sceneConstants.CameraTarget, .Black);
RenderCommand.SetBlendState(_lightBlend); RenderCommand.SetBlendState(_lightBlend);
RenderCommand.SetDepthStencilState(_fullscreenDepthState); RenderCommand.SetDepthStencilState(_fullscreenDepthState);
// Scaling to make sure that only the part of the gbuffer that we actually used gets rendered into the viewport.
Vector2 scaling = Vector2(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height) / (Vector2)_gBuffer.Size;
for (SubmittedLight light in _lights) for (SubmittedLight light in _lights)
{ {
Vector3 lightDir = light.Transform.Forward; Debug.Profiler.ProfileRendererScope!("Draw Light");
Vector3 lightDir = -light.Transform.Forward;
_gBuffer.Albedo.SamplerState = SamplerStateManager.PointClamp; _gBuffer.Albedo.SamplerState = SamplerStateManager.PointClamp;
@@ -378,11 +395,11 @@ namespace GlitchyEngine.Renderer
TestFullscreenEffect.Variables["LightColor"].SetData(light.Light.Color); TestFullscreenEffect.Variables["LightColor"].SetData(light.Light.Color);
TestFullscreenEffect.Variables["Illuminance"].SetData(light.Light.Illuminance); TestFullscreenEffect.Variables["Illuminance"].SetData(light.Light.Illuminance);
TestFullscreenEffect.Variables["LightDir"].SetData(-lightDir); TestFullscreenEffect.Variables["LightDir"].SetData(lightDir);
TestFullscreenEffect.Variables["CameraPos"].SetData(_sceneConstants.CameraPosition); TestFullscreenEffect.Variables["CameraPos"].SetData(_sceneConstants.CameraPosition);
TestFullscreenEffect.Variables["Scaling"].SetData(Vector2(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height) / Vector2(_gBuffer.Albedo.Width, _gBuffer.Albedo.Height)); TestFullscreenEffect.Variables["Scaling"].SetData(scaling);
TestFullscreenEffect.Bind(_context); TestFullscreenEffect.Bind(_context);
@@ -390,30 +407,17 @@ namespace GlitchyEngine.Renderer
RenderCommand.DrawIndexed(s_fullscreenQuadGeometry); RenderCommand.DrawIndexed(s_fullscreenQuadGeometry);
} }
RenderCommand.SetBlendState(_gBufferBlend);
_lights.Clear(); _lights.Clear();
/* RenderCommand.SetBlendState(_gBufferBlend);
_gBuffer.Albedo.SamplerState = SamplerStateManager.PointClamp;
TestFullscreenEffect.SetTexture("GBuffer_Albedo", _gBuffer.Albedo); RenderCommand.SetRenderTarget(_sceneConstants.CompositionTarget, 0, false);
TestFullscreenEffect.SetTexture("GBuffer_Normal", _gBuffer.Normal); RenderCommand.BindRenderTargets();
TestFullscreenEffect.SetTexture("GBuffer_Tangent", _gBuffer.Tangent);
TestFullscreenEffect.SetTexture("GBuffer_Position", _gBuffer.Position);
TestFullscreenEffect.SetTexture("GBuffer_Material", _gBuffer.Material);
TestFullscreenEffect.Variables["LightColor"].SetData(Vector3(1, 1, 1)); // TODO: Postprocessing effects
TestFullscreenEffect.Variables["Illuminance"].SetData(5.0f); s_tonemappingEffect.SetTexture("CameraTarget", _sceneConstants.CameraTarget);
TestFullscreenEffect.Variables["LightDir"].SetData(Vector3(0, 1, 0)); s_tonemappingEffect.Bind(_context);
TestFullscreenEffect.Variables["CameraPos"].SetData(_sceneConstants.CameraPosition); RenderCommand.DrawIndexed(s_fullscreenQuadGeometry);
TestFullscreenEffect.Variables["Scaling"].SetData(Vector2(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height) / Vector2(_gBuffer.Albedo.Width, _gBuffer.Albedo.Height));
TestFullscreenEffect.Bind(_context);
s_quadGeometry.Bind();
RenderCommand.DrawIndexed(s_quadGeometry);
*/
RenderCommand.UnbindTextures(); RenderCommand.UnbindTextures();
} }
+8 -4
View File
@@ -308,9 +308,9 @@ namespace GlitchyEngine.World
{ {
public enum LightType public enum LightType
{ {
Directional, Directional = 0,
Spot, Spot = 1,
Point Point = 2
} }
private LightType _type = .Directional; private LightType _type = .Directional;
@@ -319,7 +319,11 @@ namespace GlitchyEngine.World
private ColorRGB _color = .(1, 1, 1); private ColorRGB _color = .(1, 1, 1);
public LightType LightType => _type; public LightType LightType
{
get => _type;
set mut => _type = value;
}
public ColorRGB Color public ColorRGB Color
{ {
+2 -2
View File
@@ -34,7 +34,7 @@ namespace GlitchyEngine.World
{ {
} }
public void Update(GameTime gameTime) public void Update(GameTime gameTime, RenderTarget2D finalTarget)
{ {
TransformSystem.Update(_ecsWorld); TransformSystem.Update(_ecsWorld);
@@ -67,7 +67,7 @@ namespace GlitchyEngine.World
if (primaryCamera != null) if (primaryCamera != null)
{ {
// 3D render // 3D render
Renderer.BeginScene(*primaryCamera, primaryCameraTransform, renderTarget); Renderer.BeginScene(*primaryCamera, primaryCameraTransform, renderTarget, finalTarget);
for (var (entity, transform, mesh, meshRenderer) in _ecsWorld.Enumerate<TransformComponent, MeshComponent, MeshRendererComponent>()) for (var (entity, transform, mesh, meshRenderer) in _ecsWorld.Enumerate<TransformComponent, MeshComponent, MeshRendererComponent>())
{ {