From 98f0e6658c7e8dabe1912ad178c632467019179e Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Mon, 29 May 2023 00:11:53 +0800 Subject: [PATCH 01/26] generate repeating blobs mirroring bright spots --- src/Core/Components/Camera/CameraEffects.cpp | 103 +++++++++++++++++- src/Core/Components/Camera/CameraEffects.h | 22 ++++ src/Core/Rendering/RenderAdaptor.cpp | 10 ++ src/Core/Rendering/RenderController.cpp | 35 ++++++ src/Core/Rendering/RenderController.h | 1 + src/Platform/OpenGL/Shaders/lens_flare.glsl | 29 +++++ .../OpenGL/Shaders/lens_flare_prefilter.glsl | 17 +++ 7 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 src/Platform/OpenGL/Shaders/lens_flare.glsl create mode 100644 src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl diff --git a/src/Core/Components/Camera/CameraEffects.cpp b/src/Core/Components/Camera/CameraEffects.cpp index 7e446643..76116da6 100644 --- a/src/Core/Components/Camera/CameraEffects.cpp +++ b/src/Core/Components/Camera/CameraEffects.cpp @@ -166,6 +166,69 @@ namespace MxEngine this->bloomIterations = (uint8_t)Min(100, iterations); } + + float CameraEffects::GetLensFlareScale()const + { + return this->lensFlareScale; + } + + float CameraEffects::GetLensFlareBias()const + { + return this->lensFlareBias; + } + + int CameraEffects::GetLensFlareNumOfGhosts()const + { + return this->lensFlareNumOfGhosts; + } + + float CameraEffects::GetLensFlareGhostDispersal()const + { + return this->lensFlareGhostDispersal; + } + + float CameraEffects::GetLensFalreHaloWidth()const + { + return this->lensFalreHaloWidth; + } + + bool CameraEffects::GetLensFlareEnable()const + { + return this->lensFlareEnable; + } + + void CameraEffects::SetLensFlareScale(float v) + { + this->lensFlareScale=v; + } + + void CameraEffects::SetLensFlareBias(float v) + { + this->lensFlareBias=v; + } + + void CameraEffects::SetLensFlareNumOfGhosts(int v) + { + this->lensFlareNumOfGhosts=v; + } + + void CameraEffects::SetLensFlareGhostDispersal(float v) + { + this->lensFlareGhostDispersal=v; + } + + void CameraEffects::SetLensFalreHaloWidth(float v) + { + this->lensFalreHaloWidth=v; + } + + void CameraEffects::SetLensFlareEnable(bool v) + { + this->lensFlareEnable=v; + } + + + MXENGINE_REFLECT_TYPE { rttr::registration::class_("CameraEffects") @@ -185,13 +248,13 @@ namespace MxEngine .property("fog density", &CameraEffects::GetFogDensity, &CameraEffects::SetFogDensity) ( rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range{ 0.0f, 10000.0f }), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 10000.0f }), rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) ) .property("fog distance", &CameraEffects::GetFogDistance, &CameraEffects::SetFogDistance) ( rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range{ 0.0f, 1.0f }), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 1.0f }), rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) ) .property("bloom weight", &CameraEffects::GetBloomWeight, &CameraEffects::SetBloomWeight) @@ -247,6 +310,42 @@ namespace MxEngine rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), rttr::metadata(EditorInfo::EDIT_RANGE, Range { 5.0f, 10.f }), rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) + ) + .property("lensFlareScale", &CameraEffects::GetLensFlareScale, &CameraEffects::SetLensFlareScale) + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), + rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) + ) + .property("lensFlareBias", &CameraEffects::GetLensFlareBias, &CameraEffects::SetLensFlareBias) + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { -200.0f, 0.f }), + rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) + ) + .property("lensFlareNumOfGhosts", &CameraEffects::GetLensFlareNumOfGhosts, &CameraEffects::SetLensFlareNumOfGhosts) + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), + rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) + ) + .property("lensFlareGhostDispersal", &CameraEffects::GetLensFlareGhostDispersal, &CameraEffects::SetLensFlareGhostDispersal) + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), + rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) + ) + .property("lensFalreHaloWidth", &CameraEffects::GetLensFalreHaloWidth, &CameraEffects::SetLensFalreHaloWidth) + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), + rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) + ) + .property("lensFlareEnable", &CameraEffects::GetLensFlareEnable, &CameraEffects::SetLensFlareEnable) + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), + rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) ); } } \ No newline at end of file diff --git a/src/Core/Components/Camera/CameraEffects.h b/src/Core/Components/Camera/CameraEffects.h index 361a9acc..24f6c193 100644 --- a/src/Core/Components/Camera/CameraEffects.h +++ b/src/Core/Components/Camera/CameraEffects.h @@ -56,6 +56,13 @@ namespace MxEngine float bokehRadius = 10.f; float focusRange = 5.f; float focusDistance = 0.f; + + float lensFlareScale = 1.f; + float lensFlareBias = -50.f; + int lensFlareNumOfGhosts = 3; + float lensFlareGhostDispersal = 0.3f; + float lensFalreHaloWidth = 0.5f; + bool lensFlareEnable = true; public: CameraEffects() = default; @@ -98,5 +105,20 @@ namespace MxEngine void ToggleFXAA(bool value); + + float GetLensFlareScale()const; + float GetLensFlareBias()const; + int GetLensFlareNumOfGhosts()const; + float GetLensFlareGhostDispersal()const; + float GetLensFalreHaloWidth()const; + bool GetLensFlareEnable()const; + + void SetLensFlareScale(float); + void SetLensFlareBias(float); + void SetLensFlareNumOfGhosts(int); + void SetLensFlareGhostDispersal(float); + void SetLensFalreHaloWidth(float); + void SetLensFlareEnable(bool); + }; } \ No newline at end of file diff --git a/src/Core/Rendering/RenderAdaptor.cpp b/src/Core/Rendering/RenderAdaptor.cpp index 03354242..6fb764d0 100644 --- a/src/Core/Rendering/RenderAdaptor.cpp +++ b/src/Core/Rendering/RenderAdaptor.cpp @@ -329,6 +329,16 @@ namespace MxEngine shaderFolder / "dof_combine.glsl" ); + environment.Shaders["LensFlare"_id] = AssetManager::LoadShader( + shaderFolder / "rect_vertex.glsl", + shaderFolder / "lens_flare.glsl" + ); + + environment.Shaders["LensFlarePrefilter"_id] = AssetManager::LoadShader( + shaderFolder / "rect_vertex.glsl", + shaderFolder / "lens_flare_prefilter.glsl" + ); + // compute shaders environment.ComputeShaders["Particle"_id] = AssetManager::LoadComputeShader( shaderFolder / "particle_compute.glsl" diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index 7cbb3845..bf7102ec 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -396,6 +396,8 @@ namespace MxEngine this->ComputeBloomEffect(camera, camera.HDRTexture); this->ApplySSGI(camera, camera.HDRTexture, camera.SwapTexture1, camera.SwapTexture2); + + this->ApplyLensFlare(camera, camera.HDRTexture, camera.SwapTexture1, camera.SwapTexture2); this->ApplyGodRayEffect(camera, camera.HDRTexture, camera.SwapTexture1); this->ApplyChromaticAbberation(camera, camera.HDRTexture, camera.SwapTexture1); @@ -714,8 +716,10 @@ namespace MxEngine std::swap(input, output); } + void RenderController::ApplyDepthOfFieldEffect(CameraUnit& camera, TextureHandle& inputOutput, TextureHandle& temporary0, TextureHandle& temporary1) { + return; if (camera.Effects->GetFocusDistance() == 0.f) return; MAKE_SCOPE_PROFILER("RenderController::ApplyDepthOfFieldEffect()"); @@ -754,6 +758,37 @@ namespace MxEngine std::swap(inputOutput, temporary0); } + + void RenderController::ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporary, TextureHandle& output) + { + float scale = camera.Effects->GetLensFlareScale(); + float bias = camera.Effects->GetLensFlareBias(); + int numOfGhosts = camera.Effects->GetLensFlareNumOfGhosts(); + float dispersal = camera.Effects->GetLensFlareGhostDispersal(); + float haloWidth = camera.Effects->GetLensFalreHaloWidth(); + + MAKE_SCOPE_PROFILER("RenderController::ApplyLensFlare()"); + + input->GenerateMipmaps(); + temporary->GenerateMipmaps(); + auto& prefilter = this->Pipeline.Environment.Shaders["LensFlarePrefilter"_id]; + prefilter->Bind(); + prefilter->SetUniform("uScale", scale); + prefilter->SetUniform("uBias", bias); + input->Bind(0); + this->RenderToTextureNoClear(temporary, prefilter); + + auto& lensFlare = this->Pipeline.Environment.Shaders["LensFlare"_id]; + lensFlare->Bind(); + lensFlare->SetUniform("ghosts", numOfGhosts); + lensFlare->SetUniform("ghostDispersal", dispersal); + temporary->Bind(0); + input->Bind(1); + this->RenderToTextureNoClear(output, lensFlare); + + std::swap(input, output); + } + void RenderController::ApplyHDRToLDRConversion(CameraUnit& camera, TextureHandle& input, TextureHandle& output) { if (camera.ToneMapping == nullptr) return; diff --git a/src/Core/Rendering/RenderController.h b/src/Core/Rendering/RenderController.h index eae63413..76ff5822 100644 --- a/src/Core/Rendering/RenderController.h +++ b/src/Core/Rendering/RenderController.h @@ -76,6 +76,7 @@ namespace MxEngine void ApplyVignette(CameraUnit& camera, TextureHandle& input, TextureHandle& output); void ApplyColorGrading(CameraUnit& camera, TextureHandle& input, TextureHandle& output); void ApplyDepthOfFieldEffect(CameraUnit& camera, TextureHandle& input, TextureHandle& temporary, TextureHandle& output); + void ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporary, TextureHandle& output); void DrawIBL(CameraUnit& camera, TextureHandle& output); void DrawDirectionalLights(CameraUnit& camera, TextureHandle& output); void DrawShadowedPointLights(CameraUnit& camera, TextureHandle& output); diff --git a/src/Platform/OpenGL/Shaders/lens_flare.glsl b/src/Platform/OpenGL/Shaders/lens_flare.glsl new file mode 100644 index 00000000..bddae638 --- /dev/null +++ b/src/Platform/OpenGL/Shaders/lens_flare.glsl @@ -0,0 +1,29 @@ +layout(binding = 0) uniform sampler2D lensFlareColor; +layout(binding = 1) uniform sampler2D inputColor; + +uniform int ghosts; // number of ghost samples +uniform float ghostDispersal; // dispersion factor +//uniform float uHaloWidth; + +in vec2 TexCoord; +out vec4 outColor; + +void main() +{ + vec2 texcoord = TexCoord; + texcoord = -texcoord + vec2(1.0); + // ghost vector to image centre: + vec2 ghostVec = (vec2(0.5) - texcoord) * ghostDispersal; + + // sample ghosts + vec4 result = vec4(0.0); + for (int i = 0; i < ghosts; ++i) { + vec2 offset = fract(texcoord + ghostVec * float(i)); + float weight = length(vec2(0.5) - offset) / length(vec2(0.5)); + weight = pow(1.0 - weight, 10.0); + + result += texture(lensFlareColor, offset)*weight; + } + + outColor = result + texture(inputColor,TexCoord); +} \ No newline at end of file diff --git a/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl b/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl new file mode 100644 index 00000000..992fd638 --- /dev/null +++ b/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl @@ -0,0 +1,17 @@ +layout(binding = 0) uniform sampler2D inputColor; + +uniform float uScale; +uniform float uBias; + +in vec2 TexCoord; + +out vec4 outputColor; + +void main() +{ + vec4 col = texture(inputColor, TexCoord); + if(length(max(vec4(0.0),col+vec4(uBias)))<=0.001f) + outputColor=vec4(0.0); + else + outputColor=col*vec4(uScale); +} \ No newline at end of file From 2db950a68c28da1dc933a4a9d7e78e65c30b7ccb Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Sat, 3 Jun 2023 22:09:07 +0800 Subject: [PATCH 02/26] use lower resolution for ghost generation --- .../Components/Camera/CameraController.cpp | 21 +++++++++++++++++++ src/Core/Components/Camera/CameraController.h | 4 ++++ src/Core/Rendering/RenderController.cpp | 21 ++++++++++++------- src/Core/Rendering/RenderController.h | 2 +- src/Core/Rendering/RenderPipeline.h | 2 ++ src/Platform/OpenGL/Shaders/lens_flare.glsl | 13 ++++++++++-- 6 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/Core/Components/Camera/CameraController.cpp b/src/Core/Components/Camera/CameraController.cpp index 21bcc750..fcc163a2 100644 --- a/src/Core/Components/Camera/CameraController.cpp +++ b/src/Core/Components/Camera/CameraController.cpp @@ -445,6 +445,16 @@ namespace MxEngine return this->renderBuffers->SwapHDR2; } + TextureHandle CameraController::GetSwapHalf1() const + { + return this->renderBuffers->SwapHalf1; + } + + TextureHandle CameraController::GetSwapHalf2() const + { + return this->renderBuffers->SwapHalf2; + } + TextureHandle CameraController::GetSwapHDRTexture1() const { return this->renderBuffers->SwapHDR1; @@ -461,6 +471,8 @@ namespace MxEngine this->HDR = Factory::Create(); this->SwapHDR1 = Factory::Create(); this->SwapHDR2 = Factory::Create(); + this->SwapHalf1 = Factory::Create(); + this->SwapHalf2 = Factory::Create(); this->Resize(width, height); @@ -512,6 +524,14 @@ namespace MxEngine this->SwapHDR2->Load(nullptr, width, height, 3, false, TextureFormat::RGBA16F); this->SwapHDR2->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap hdr 2")); this->SwapHDR2->SetWrapType(TextureWrap::CLAMP_TO_EDGE); + + this->SwapHalf1->Load(nullptr, width / 4, height / 4, 3, false, TextureFormat::RGBA16F); + this->SwapHalf1->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap half 1")); + this->SwapHalf1->SetWrapType(TextureWrap::CLAMP_TO_EDGE); + + this->SwapHalf2->Load(nullptr, width / 4, height / 4, 3, false, TextureFormat::RGBA16F); + this->SwapHalf2->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap half 2")); + this->SwapHalf2->SetWrapType(TextureWrap::CLAMP_TO_EDGE); } void CameraRender::DeInit() @@ -524,6 +544,7 @@ namespace MxEngine Factory::Destroy(this->HDR); Factory::Destroy(this->SwapHDR1); Factory::Destroy(this->SwapHDR2); + Factory::Destroy(this->SwapHalf1); } MXENGINE_REFLECT_TYPE diff --git a/src/Core/Components/Camera/CameraController.h b/src/Core/Components/Camera/CameraController.h index 73b885e3..d4976bad 100644 --- a/src/Core/Components/Camera/CameraController.h +++ b/src/Core/Components/Camera/CameraController.h @@ -53,6 +53,8 @@ namespace MxEngine TextureHandle HDR; TextureHandle SwapHDR1; TextureHandle SwapHDR2; + TextureHandle SwapHalf1; + TextureHandle SwapHalf2; void Init(int width, int height); void Resize(int width, int height); @@ -135,5 +137,7 @@ namespace MxEngine TextureHandle GetHDRTexture() const; TextureHandle GetSwapHDRTexture1() const; TextureHandle GetSwapHDRTexture2() const; + TextureHandle GetSwapHalf1() const; + TextureHandle GetSwapHalf2() const; }; } \ No newline at end of file diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index bf7102ec..34af7f2d 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -397,7 +397,7 @@ namespace MxEngine this->ComputeBloomEffect(camera, camera.HDRTexture); this->ApplySSGI(camera, camera.HDRTexture, camera.SwapTexture1, camera.SwapTexture2); - this->ApplyLensFlare(camera, camera.HDRTexture, camera.SwapTexture1, camera.SwapTexture2); + this->ApplyLensFlare(camera, camera.HDRTexture, camera.SwapHalfTexture1, camera.SwapHalfTexture2, camera.SwapTexture1); this->ApplyGodRayEffect(camera, camera.HDRTexture, camera.SwapTexture1); this->ApplyChromaticAbberation(camera, camera.HDRTexture, camera.SwapTexture1); @@ -759,7 +759,7 @@ namespace MxEngine std::swap(inputOutput, temporary0); } - void RenderController::ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporary, TextureHandle& output) + void RenderController::ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporaryHalf0, TextureHandle& temporaryHalf1, TextureHandle& temporary1) { float scale = camera.Effects->GetLensFlareScale(); float bias = camera.Effects->GetLensFlareBias(); @@ -770,23 +770,26 @@ namespace MxEngine MAKE_SCOPE_PROFILER("RenderController::ApplyLensFlare()"); input->GenerateMipmaps(); - temporary->GenerateMipmaps(); + temporaryHalf0->GenerateMipmaps(); auto& prefilter = this->Pipeline.Environment.Shaders["LensFlarePrefilter"_id]; prefilter->Bind(); - prefilter->SetUniform("uScale", scale); + prefilter->SetUniform("uScale", scale); prefilter->SetUniform("uBias", bias); input->Bind(0); - this->RenderToTextureNoClear(temporary, prefilter); + this->RenderToTextureNoClear(temporaryHalf0, prefilter); + + this->ApplyGaussianBlur(temporaryHalf0, temporaryHalf1,3); auto& lensFlare = this->Pipeline.Environment.Shaders["LensFlare"_id]; lensFlare->Bind(); lensFlare->SetUniform("ghosts", numOfGhosts); lensFlare->SetUniform("ghostDispersal", dispersal); - temporary->Bind(0); + lensFlare->SetUniform("uHaloWidth", haloWidth); + temporaryHalf1->Bind(0); input->Bind(1); - this->RenderToTextureNoClear(output, lensFlare); + this->RenderToTextureNoClear(temporary1, lensFlare); - std::swap(input, output); + std::swap(input, temporary1); } void RenderController::ApplyHDRToLDRConversion(CameraUnit& camera, TextureHandle& input, TextureHandle& output) @@ -1490,6 +1493,8 @@ namespace MxEngine camera.HDRTexture = controller.GetHDRTexture(); camera.SwapTexture1 = controller.GetSwapHDRTexture1(); camera.SwapTexture2 = controller.GetSwapHDRTexture2(); + camera.SwapHalfTexture1 = controller.GetSwapHalf1(); + camera.SwapHalfTexture2 = controller.GetSwapHalf2(); camera.OutputTexture = controller.GetRenderTexture(); camera.RenderToTexture = controller.IsRendering(); camera.SkyboxTexture = (skybox != nullptr && skybox->CubeMap.IsValid()) ? skybox->CubeMap : this->Pipeline.Environment.DefaultSkybox; diff --git a/src/Core/Rendering/RenderController.h b/src/Core/Rendering/RenderController.h index 76ff5822..c53bd991 100644 --- a/src/Core/Rendering/RenderController.h +++ b/src/Core/Rendering/RenderController.h @@ -76,7 +76,7 @@ namespace MxEngine void ApplyVignette(CameraUnit& camera, TextureHandle& input, TextureHandle& output); void ApplyColorGrading(CameraUnit& camera, TextureHandle& input, TextureHandle& output); void ApplyDepthOfFieldEffect(CameraUnit& camera, TextureHandle& input, TextureHandle& temporary, TextureHandle& output); - void ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporary, TextureHandle& output); + void ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporaryHalf0, TextureHandle& temporaryHalf1, TextureHandle& temporary1); void DrawIBL(CameraUnit& camera, TextureHandle& output); void DrawDirectionalLights(CameraUnit& camera, TextureHandle& output); void DrawShadowedPointLights(CameraUnit& camera, TextureHandle& output); diff --git a/src/Core/Rendering/RenderPipeline.h b/src/Core/Rendering/RenderPipeline.h index d79cf4a9..8b5b4fd5 100644 --- a/src/Core/Rendering/RenderPipeline.h +++ b/src/Core/Rendering/RenderPipeline.h @@ -66,6 +66,8 @@ namespace MxEngine TextureHandle HDRTexture; TextureHandle SwapTexture1; TextureHandle SwapTexture2; + TextureHandle SwapHalfTexture1; + TextureHandle SwapHalfTexture2; FrustrumCuller Culler; Matrix4x4 InverseViewProjMatrix; diff --git a/src/Platform/OpenGL/Shaders/lens_flare.glsl b/src/Platform/OpenGL/Shaders/lens_flare.glsl index bddae638..6cf8283d 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare.glsl @@ -3,7 +3,7 @@ layout(binding = 1) uniform sampler2D inputColor; uniform int ghosts; // number of ghost samples uniform float ghostDispersal; // dispersion factor -//uniform float uHaloWidth; +uniform float uHaloWidth; in vec2 TexCoord; out vec4 outColor; @@ -20,10 +20,19 @@ void main() for (int i = 0; i < ghosts; ++i) { vec2 offset = fract(texcoord + ghostVec * float(i)); float weight = length(vec2(0.5) - offset) / length(vec2(0.5)); - weight = pow(1.0 - weight, 10.0); + weight = pow(1.0 - weight, 80.0); result += texture(lensFlareColor, offset)*weight; } + // vec2 haloVec = normalize(ghostVec) * uHaloWidth; + // float weight = length(vec2(0.5) - fract(texcoord + haloVec)) / length(vec2(0.5)); + // weight = pow(1.0 - weight, 5.0); + // result += texture(inputColor, texcoord + haloVec) * weight; + outColor = result + texture(inputColor,TexCoord); + //float weight = length(vec2(0.5) - TexCoord)/length(vec2(0.5)); + //weight = pow(weight, 20.0); + //outColor=vec4(weight); + //weight = pow(1.0 - weight, 20.0); } \ No newline at end of file From 735318506d90ef56e79cf35f61fc5976f0aa8dc6 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Sat, 3 Jun 2023 22:34:32 +0800 Subject: [PATCH 03/26] change default value --- src/Core/Components/Camera/CameraEffects.h | 6 +++--- src/Platform/OpenGL/Shaders/lens_flare.glsl | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Core/Components/Camera/CameraEffects.h b/src/Core/Components/Camera/CameraEffects.h index 24f6c193..81df23f5 100644 --- a/src/Core/Components/Camera/CameraEffects.h +++ b/src/Core/Components/Camera/CameraEffects.h @@ -58,9 +58,9 @@ namespace MxEngine float focusDistance = 0.f; float lensFlareScale = 1.f; - float lensFlareBias = -50.f; - int lensFlareNumOfGhosts = 3; - float lensFlareGhostDispersal = 0.3f; + float lensFlareBias = -42; + int lensFlareNumOfGhosts = 10; + float lensFlareGhostDispersal = 0.13f; float lensFalreHaloWidth = 0.5f; bool lensFlareEnable = true; public: diff --git a/src/Platform/OpenGL/Shaders/lens_flare.glsl b/src/Platform/OpenGL/Shaders/lens_flare.glsl index 6cf8283d..d6520a35 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare.glsl @@ -25,10 +25,10 @@ void main() result += texture(lensFlareColor, offset)*weight; } - // vec2 haloVec = normalize(ghostVec) * uHaloWidth; - // float weight = length(vec2(0.5) - fract(texcoord + haloVec)) / length(vec2(0.5)); - // weight = pow(1.0 - weight, 5.0); - // result += texture(inputColor, texcoord + haloVec) * weight; + vec2 haloVec = normalize(ghostVec) * uHaloWidth; + float weight = length(vec2(0.5) - fract(texcoord + haloVec)) / length(vec2(0.5)); + weight = pow(1.0 - weight, 5.0); + result += texture(inputColor, texcoord + haloVec) * weight; outColor = result + texture(inputColor,TexCoord); //float weight = length(vec2(0.5) - TexCoord)/length(vec2(0.5)); From 5f385b9acff8dc33fdbfdca4c3569843265c19c5 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Sat, 3 Jun 2023 23:48:09 +0800 Subject: [PATCH 04/26] adjust halo --- src/Platform/OpenGL/Shaders/lens_flare.glsl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Platform/OpenGL/Shaders/lens_flare.glsl b/src/Platform/OpenGL/Shaders/lens_flare.glsl index d6520a35..04a70b3e 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare.glsl @@ -27,12 +27,11 @@ void main() vec2 haloVec = normalize(ghostVec) * uHaloWidth; float weight = length(vec2(0.5) - fract(texcoord + haloVec)) / length(vec2(0.5)); - weight = pow(1.0 - weight, 5.0); + weight = pow(1.0 - weight, 75.0); result += texture(inputColor, texcoord + haloVec) * weight; outColor = result + texture(inputColor,TexCoord); //float weight = length(vec2(0.5) - TexCoord)/length(vec2(0.5)); //weight = pow(weight, 20.0); - //outColor=vec4(weight); //weight = pow(1.0 - weight, 20.0); } \ No newline at end of file From e8c96dd0ad4fcb3b43c8b8b0c8a79defffdca935 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Sun, 4 Jun 2023 00:25:26 +0800 Subject: [PATCH 05/26] add component --- src/CMakeLists.txt | 1 + src/Core/Components/Camera/CameraEffects.cpp | 99 ------------------- src/Core/Components/Camera/CameraEffects.h | 21 ---- .../Components/Camera/CameraLensFlare.cpp | 95 ++++++++++++++++++ src/Core/Components/Camera/CameraLensFlare.h | 32 ++++++ src/Core/Components/Components.cpp | 2 + src/Core/Components/Components.h | 1 + src/Core/Rendering/RenderAdaptor.cpp | 5 +- src/Core/Rendering/RenderController.cpp | 21 ++-- src/Core/Rendering/RenderController.h | 2 +- src/Core/Rendering/RenderPipeline.h | 2 + 11 files changed, 152 insertions(+), 129 deletions(-) create mode 100644 src/Core/Components/Camera/CameraLensFlare.cpp create mode 100644 src/Core/Components/Camera/CameraLensFlare.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f8823135..b3eec9de 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,6 +12,7 @@ set(MXENGINE_SOURCES "Core/Components/Camera/CameraController.cpp" "Core/Components/Camera/CameraEffects.cpp" "Core/Components/Camera/CameraGodRay.cpp" +"Core/Components/Camera/CameraLensFlare.cpp" "Core/Components/Camera/FrustrumCamera.cpp" "Core/Components/Camera/InputController.cpp" "Core/Components/Camera/OrthographicCamera.cpp" diff --git a/src/Core/Components/Camera/CameraEffects.cpp b/src/Core/Components/Camera/CameraEffects.cpp index 76116da6..a5b03545 100644 --- a/src/Core/Components/Camera/CameraEffects.cpp +++ b/src/Core/Components/Camera/CameraEffects.cpp @@ -166,69 +166,6 @@ namespace MxEngine this->bloomIterations = (uint8_t)Min(100, iterations); } - - float CameraEffects::GetLensFlareScale()const - { - return this->lensFlareScale; - } - - float CameraEffects::GetLensFlareBias()const - { - return this->lensFlareBias; - } - - int CameraEffects::GetLensFlareNumOfGhosts()const - { - return this->lensFlareNumOfGhosts; - } - - float CameraEffects::GetLensFlareGhostDispersal()const - { - return this->lensFlareGhostDispersal; - } - - float CameraEffects::GetLensFalreHaloWidth()const - { - return this->lensFalreHaloWidth; - } - - bool CameraEffects::GetLensFlareEnable()const - { - return this->lensFlareEnable; - } - - void CameraEffects::SetLensFlareScale(float v) - { - this->lensFlareScale=v; - } - - void CameraEffects::SetLensFlareBias(float v) - { - this->lensFlareBias=v; - } - - void CameraEffects::SetLensFlareNumOfGhosts(int v) - { - this->lensFlareNumOfGhosts=v; - } - - void CameraEffects::SetLensFlareGhostDispersal(float v) - { - this->lensFlareGhostDispersal=v; - } - - void CameraEffects::SetLensFalreHaloWidth(float v) - { - this->lensFalreHaloWidth=v; - } - - void CameraEffects::SetLensFlareEnable(bool v) - { - this->lensFlareEnable=v; - } - - - MXENGINE_REFLECT_TYPE { rttr::registration::class_("CameraEffects") @@ -310,42 +247,6 @@ namespace MxEngine rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), rttr::metadata(EditorInfo::EDIT_RANGE, Range { 5.0f, 10.f }), rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) - ) - .property("lensFlareScale", &CameraEffects::GetLensFlareScale, &CameraEffects::SetLensFlareScale) - ( - rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), - rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) - ) - .property("lensFlareBias", &CameraEffects::GetLensFlareBias, &CameraEffects::SetLensFlareBias) - ( - rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range { -200.0f, 0.f }), - rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) - ) - .property("lensFlareNumOfGhosts", &CameraEffects::GetLensFlareNumOfGhosts, &CameraEffects::SetLensFlareNumOfGhosts) - ( - rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), - rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) - ) - .property("lensFlareGhostDispersal", &CameraEffects::GetLensFlareGhostDispersal, &CameraEffects::SetLensFlareGhostDispersal) - ( - rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), - rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) - ) - .property("lensFalreHaloWidth", &CameraEffects::GetLensFalreHaloWidth, &CameraEffects::SetLensFalreHaloWidth) - ( - rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), - rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) - ) - .property("lensFlareEnable", &CameraEffects::GetLensFlareEnable, &CameraEffects::SetLensFlareEnable) - ( - rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), - rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) ); } } \ No newline at end of file diff --git a/src/Core/Components/Camera/CameraEffects.h b/src/Core/Components/Camera/CameraEffects.h index 81df23f5..cdfa8b7e 100644 --- a/src/Core/Components/Camera/CameraEffects.h +++ b/src/Core/Components/Camera/CameraEffects.h @@ -57,12 +57,6 @@ namespace MxEngine float focusRange = 5.f; float focusDistance = 0.f; - float lensFlareScale = 1.f; - float lensFlareBias = -42; - int lensFlareNumOfGhosts = 10; - float lensFlareGhostDispersal = 0.13f; - float lensFalreHaloWidth = 0.5f; - bool lensFlareEnable = true; public: CameraEffects() = default; @@ -105,20 +99,5 @@ namespace MxEngine void ToggleFXAA(bool value); - - float GetLensFlareScale()const; - float GetLensFlareBias()const; - int GetLensFlareNumOfGhosts()const; - float GetLensFlareGhostDispersal()const; - float GetLensFalreHaloWidth()const; - bool GetLensFlareEnable()const; - - void SetLensFlareScale(float); - void SetLensFlareBias(float); - void SetLensFlareNumOfGhosts(int); - void SetLensFlareGhostDispersal(float); - void SetLensFalreHaloWidth(float); - void SetLensFlareEnable(bool); - }; } \ No newline at end of file diff --git a/src/Core/Components/Camera/CameraLensFlare.cpp b/src/Core/Components/Camera/CameraLensFlare.cpp new file mode 100644 index 00000000..b93f1a28 --- /dev/null +++ b/src/Core/Components/Camera/CameraLensFlare.cpp @@ -0,0 +1,95 @@ +#include "CameraLensFlare.h" +#include "Utilities/Math/Math.h" +#include "Core/Runtime/Reflection.h" + +namespace MxEngine +{ + float CameraLensFlare::GetLensFlareScale()const + { + return this->lensFlareScale; + } + + float CameraLensFlare::GetLensFlareBias()const + { + return this->lensFlareBias; + } + + int CameraLensFlare::GetLensFlareNumOfGhosts()const + { + return this->lensFlareNumOfGhosts; + } + + float CameraLensFlare::GetLensFlareGhostDispersal()const + { + return this->lensFlareGhostDispersal; + } + + float CameraLensFlare::GetLensFlareHaloWidth()const + { + return this->lensFalreHaloWidth; + } + + void CameraLensFlare::SetLensFlareScale(float v) + { + this->lensFlareScale = v; + } + + void CameraLensFlare::SetLensFlareBias(float v) + { + this->lensFlareBias = v; + } + + void CameraLensFlare::SetLensFlareNumOfGhosts(int v) + { + this->lensFlareNumOfGhosts = v; + } + + void CameraLensFlare::SetLensFlareGhostDispersal(float v) + { + this->lensFlareGhostDispersal = v; + } + + void CameraLensFlare::SetLensFlareHaloWidth(float v) + { + this->lensFalreHaloWidth = v; + } + + MXENGINE_REFLECT_TYPE + { + rttr::registration::class_("CameraGodRay") + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::CLONE_COPY | MetaInfo::CLONE_INSTANCE) + ) + .constructor<>() + .property("lens flare scale", &CameraLensFlare::GetLensFlareScale, &CameraLensFlare::SetLensFlareScale) + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), + rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) + ) + .property("lens flare bias", &CameraLensFlare::GetLensFlareBias, &CameraLensFlare::SetLensFlareBias) + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { -200.0f, 0.f }), + rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) + ) + .property("lens flare num of ghosts", &CameraLensFlare::GetLensFlareNumOfGhosts, &CameraLensFlare::SetLensFlareNumOfGhosts) + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), + rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) + ) + .property("lens flare ghost dispersal", &CameraLensFlare::GetLensFlareGhostDispersal, &CameraLensFlare::SetLensFlareGhostDispersal) + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), + rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) + ) + .property("lens falre halo width", &CameraLensFlare::GetLensFlareHaloWidth, &CameraLensFlare::SetLensFlareHaloWidth) + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), + rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) + ); + } +} \ No newline at end of file diff --git a/src/Core/Components/Camera/CameraLensFlare.h b/src/Core/Components/Camera/CameraLensFlare.h new file mode 100644 index 00000000..f4b86bb7 --- /dev/null +++ b/src/Core/Components/Camera/CameraLensFlare.h @@ -0,0 +1,32 @@ +#pragma once + +#include "Utilities/ECS/Component.h" + + +namespace MxEngine +{ + class CameraLensFlare + { + MAKE_COMPONENT(CameraLensFlare); + + float lensFlareScale = 1.f; + float lensFlareBias = -42; + int lensFlareNumOfGhosts = 10; + float lensFlareGhostDispersal = 0.13f; + float lensFalreHaloWidth = 0.5f; + bool lensFlareEnable = true; + + public: + float GetLensFlareScale()const; + float GetLensFlareBias()const; + int GetLensFlareNumOfGhosts()const; + float GetLensFlareGhostDispersal()const; + float GetLensFlareHaloWidth()const; + + void SetLensFlareScale(float); + void SetLensFlareBias(float); + void SetLensFlareNumOfGhosts(int); + void SetLensFlareGhostDispersal(float); + void SetLensFlareHaloWidth(float); + }; +} \ No newline at end of file diff --git a/src/Core/Components/Components.cpp b/src/Core/Components/Components.cpp index 2b1ef9d6..a5f632ee 100644 --- a/src/Core/Components/Components.cpp +++ b/src/Core/Components/Components.cpp @@ -86,6 +86,7 @@ namespace MxEngine TEMPLATE_INSTANCIATE_COMPONENT(CameraSSGI ); TEMPLATE_INSTANCIATE_COMPONENT(CameraSSAO ); TEMPLATE_INSTANCIATE_COMPONENT(CameraGodRay ); + TEMPLATE_INSTANCIATE_COMPONENT(CameraLensFlare ); TEMPLATE_INSTANCIATE_COMPONENT(CameraToneMapping ); TEMPLATE_INSTANCIATE_COMPONENT(VRCameraController ); TEMPLATE_INSTANCIATE_COMPONENT(AudioSource ); @@ -124,6 +125,7 @@ namespace MxEngine RegisterComponent(); RegisterComponent(); RegisterComponent(); + RegisterComponent(); RegisterComponent(); RegisterComponent(); RegisterComponent(); diff --git a/src/Core/Components/Components.h b/src/Core/Components/Components.h index f052c005..f73cace6 100644 --- a/src/Core/Components/Components.h +++ b/src/Core/Components/Components.h @@ -45,6 +45,7 @@ #include "Camera/CameraSSGI.h" #include "Camera/CameraSSAO.h" #include "Camera/CameraGodRay.h" +#include "Camera/CameraLensFlare.h" #include "Camera/InputController.h" #include "Camera/VRCameraController.h" #include "Audio/AudioListener.h" diff --git a/src/Core/Rendering/RenderAdaptor.cpp b/src/Core/Rendering/RenderAdaptor.cpp index 6fb764d0..cb8c7a8f 100644 --- a/src/Core/Rendering/RenderAdaptor.cpp +++ b/src/Core/Rendering/RenderAdaptor.cpp @@ -43,6 +43,7 @@ #include "Core/Components/Camera/CameraSSGI.h" #include "Core/Components/Camera/CameraSSAO.h" #include "Core/Components/Camera/CameraGodRay.h" +#include "Core/Components/Camera/CameraLensFlare.h" #include "Core/Components/Camera/CameraToneMapping.h" #include "Core/Components/Lighting/DirectionalLight.h" #include "Core/Components/Lighting/PointLight.h" @@ -401,6 +402,7 @@ namespace MxEngine auto ssgiComponent = object.GetComponent(); auto ssaoComponent = object.GetComponent(); auto godRayComponent = object.GetComponent(); + auto lensFlareComponent= object.GetComponent(); Skybox* skybox = skyboxComponent.IsValid() ? skyboxComponent.GetUnchecked() : nullptr; CameraEffects* effects = effectsComponent.IsValid() ? effectsComponent.GetUnchecked() : nullptr; CameraToneMapping* toneMapping = toneMappingComponent.IsValid() ? toneMappingComponent.GetUnchecked() : nullptr; @@ -408,8 +410,9 @@ namespace MxEngine CameraSSGI* ssgi = ssgiComponent.IsValid() ? ssgiComponent.GetUnchecked() : nullptr; CameraSSAO* ssao = ssaoComponent.IsValid() ? ssaoComponent.GetUnchecked() : nullptr; CameraGodRay* godRay = godRayComponent.IsValid() ? godRayComponent.GetUnchecked() : nullptr; + CameraLensFlare* lensFlare = lensFlareComponent.IsValid() ? lensFlareComponent.GetUnchecked() : nullptr; - this->Renderer.SubmitCamera(camera, transform, skybox, effects, toneMapping, ssr, ssgi, ssao, godRay); + this->Renderer.SubmitCamera(camera, transform, skybox, effects, toneMapping, ssr, ssgi, ssao, godRay, lensFlare); TrackMainCameraIndex(camera); } } diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index 34af7f2d..d9b14af8 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -37,6 +37,7 @@ #include "Core/Components/Camera/CameraSSGI.h" #include "Core/Components/Camera/CameraSSAO.h" #include "Core/Components/Camera/CameraGodRay.h" +#include "Core/Components/Camera/CameraLensFlare.h" #include "Core/Components/Lighting/DirectionalLight.h" #include "Core/Components/Lighting/SpotLight.h" #include "Core/Components/Lighting/PointLight.h" @@ -761,13 +762,17 @@ namespace MxEngine void RenderController::ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporaryHalf0, TextureHandle& temporaryHalf1, TextureHandle& temporary1) { - float scale = camera.Effects->GetLensFlareScale(); - float bias = camera.Effects->GetLensFlareBias(); - int numOfGhosts = camera.Effects->GetLensFlareNumOfGhosts(); - float dispersal = camera.Effects->GetLensFlareGhostDispersal(); - float haloWidth = camera.Effects->GetLensFalreHaloWidth(); - MAKE_SCOPE_PROFILER("RenderController::ApplyLensFlare()"); + if (camera.LensFlare == nullptr) + return; + + //Todo: support chromatic aberration + //Todo: add starbust effect + float scale = camera.LensFlare->GetLensFlareScale(); + float bias = camera.LensFlare->GetLensFlareBias(); + int numOfGhosts = camera.LensFlare->GetLensFlareNumOfGhosts(); + float dispersal = camera.LensFlare->GetLensFlareGhostDispersal(); + float haloWidth = camera.LensFlare->GetLensFlareHaloWidth(); input->GenerateMipmaps(); temporaryHalf0->GenerateMipmaps(); @@ -1473,7 +1478,8 @@ namespace MxEngine const CameraSSR* ssr, const CameraSSGI* ssgi, const CameraSSAO* ssao, - const CameraGodRay* godRay) + const CameraGodRay* godRay, + const CameraLensFlare* lensFlare) { auto& camera = this->Pipeline.Cameras.emplace_back(); @@ -1508,6 +1514,7 @@ namespace MxEngine camera.SSGI = ssgi; camera.SSAO = ssao; camera.GodRay = godRay; + camera.LensFlare = lensFlare; } size_t RenderController::SubmitRenderGroup(const Mesh& mesh, size_t instanceOffset, size_t instanceCount) diff --git a/src/Core/Rendering/RenderController.h b/src/Core/Rendering/RenderController.h index c53bd991..ed8a620f 100644 --- a/src/Core/Rendering/RenderController.h +++ b/src/Core/Rendering/RenderController.h @@ -131,7 +131,7 @@ namespace MxEngine void SubmitLightSource(const SpotLight& light, const Transform& parentTransform); void SubmitCamera(const CameraController& controller, const Transform& parentTransform, const Skybox* skybox, const CameraEffects* effects, const CameraToneMapping* toneMapping, - const CameraSSR* ssr, const CameraSSGI* ssgi, const CameraSSAO* ssao,const CameraGodRay* godRay); + const CameraSSR* ssr, const CameraSSGI* ssgi, const CameraSSAO* ssao,const CameraGodRay* godRay, const CameraLensFlare* lensFlare); size_t SubmitRenderGroup(const Mesh& mesh, size_t instanceOffset, size_t instanceCount); void SubmitRenderUnit(size_t renderGroupIndex, const SubMesh& object, const Material& material, const Transform& parentTransform, bool castsShadow, const char* debugName = nullptr); void SubmitImage(const TextureHandle& texture); diff --git a/src/Core/Rendering/RenderPipeline.h b/src/Core/Rendering/RenderPipeline.h index 8b5b4fd5..22cba881 100644 --- a/src/Core/Rendering/RenderPipeline.h +++ b/src/Core/Rendering/RenderPipeline.h @@ -48,6 +48,7 @@ namespace MxEngine class CameraSSGI; class CameraSSAO; class CameraGodRay; + class CameraLensFlare; struct DebugBufferUnit { @@ -94,6 +95,7 @@ namespace MxEngine const CameraSSGI* SSGI; const CameraSSAO* SSAO; const CameraGodRay* GodRay; + const CameraLensFlare* LensFlare; }; struct EnvironmentUnit From 7ee898255d315c6908f5c1b69c839115e569e1e4 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Sun, 4 Jun 2023 00:42:00 +0800 Subject: [PATCH 06/26] fix compile --- src/Core/Components/Camera/CameraGodRay.h | 2 +- .../Components/Camera/CameraLensFlare.cpp | 2 +- src/Core/Components/Camera/CameraLensFlare.h | 5 +-- src/Platform/OpenGL/Shaders/lens_flare.glsl | 35 +++++++++---------- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/Core/Components/Camera/CameraGodRay.h b/src/Core/Components/Camera/CameraGodRay.h index 5c48859d..a5d218ee 100644 --- a/src/Core/Components/Camera/CameraGodRay.h +++ b/src/Core/Components/Camera/CameraGodRay.h @@ -22,4 +22,4 @@ namespace MxEngine void SetGodRaySampleStep(float value); void SetGodRayAsymmetry(float value); }; -} \ No newline at end of file +} diff --git a/src/Core/Components/Camera/CameraLensFlare.cpp b/src/Core/Components/Camera/CameraLensFlare.cpp index b93f1a28..3c9875f2 100644 --- a/src/Core/Components/Camera/CameraLensFlare.cpp +++ b/src/Core/Components/Camera/CameraLensFlare.cpp @@ -56,7 +56,7 @@ namespace MxEngine MXENGINE_REFLECT_TYPE { - rttr::registration::class_("CameraGodRay") + rttr::registration::class_("CameraLensFlare") ( rttr::metadata(MetaInfo::FLAGS, MetaInfo::CLONE_COPY | MetaInfo::CLONE_INSTANCE) ) diff --git a/src/Core/Components/Camera/CameraLensFlare.h b/src/Core/Components/Camera/CameraLensFlare.h index f4b86bb7..c043b13e 100644 --- a/src/Core/Components/Camera/CameraLensFlare.h +++ b/src/Core/Components/Camera/CameraLensFlare.h @@ -7,8 +7,8 @@ namespace MxEngine { class CameraLensFlare { - MAKE_COMPONENT(CameraLensFlare); - + MAKE_COMPONENT(CameraLensFlare); + private: float lensFlareScale = 1.f; float lensFlareBias = -42; int lensFlareNumOfGhosts = 10; @@ -17,6 +17,7 @@ namespace MxEngine bool lensFlareEnable = true; public: + CameraLensFlare() = default; float GetLensFlareScale()const; float GetLensFlareBias()const; int GetLensFlareNumOfGhosts()const; diff --git a/src/Platform/OpenGL/Shaders/lens_flare.glsl b/src/Platform/OpenGL/Shaders/lens_flare.glsl index 04a70b3e..0165d80c 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare.glsl @@ -8,30 +8,27 @@ uniform float uHaloWidth; in vec2 TexCoord; out vec4 outColor; -void main() +void main() { vec2 texcoord = TexCoord; texcoord = -texcoord + vec2(1.0); - // ghost vector to image centre: - vec2 ghostVec = (vec2(0.5) - texcoord) * ghostDispersal; + // ghost vector to image centre: + vec2 ghostVec = (vec2(0.5) - texcoord) * ghostDispersal; - // sample ghosts - vec4 result = vec4(0.0); - for (int i = 0; i < ghosts; ++i) { - vec2 offset = fract(texcoord + ghostVec * float(i)); - float weight = length(vec2(0.5) - offset) / length(vec2(0.5)); - weight = pow(1.0 - weight, 80.0); + // sample ghosts + vec4 result = vec4(0.0); + for (int i = 0; i < ghosts; ++i) { + vec2 offset = fract(texcoord + ghostVec * float(i)); + float weight = length(vec2(0.5) - offset) / length(vec2(0.5)); + weight = pow(1.0 - weight, 80.0); - result += texture(lensFlareColor, offset)*weight; - } + result += texture(lensFlareColor, offset) * weight; + } - vec2 haloVec = normalize(ghostVec) * uHaloWidth; - float weight = length(vec2(0.5) - fract(texcoord + haloVec)) / length(vec2(0.5)); - weight = pow(1.0 - weight, 75.0); - result += texture(inputColor, texcoord + haloVec) * weight; + vec2 haloVec = normalize(ghostVec) * uHaloWidth; + float weight = length(vec2(0.5) - fract(texcoord + haloVec)) / length(vec2(0.5)); + weight = pow(1.0 - weight, 75.0); + result += texture(inputColor, texcoord + haloVec) * weight; - outColor = result + texture(inputColor,TexCoord); - //float weight = length(vec2(0.5) - TexCoord)/length(vec2(0.5)); - //weight = pow(weight, 20.0); - //weight = pow(1.0 - weight, 20.0); + outColor = result + texture(inputColor, TexCoord); } \ No newline at end of file From 994b27a40447a12e17dd00efb9047dc7c962a583 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Mon, 5 Jun 2023 22:04:47 +0800 Subject: [PATCH 07/26] improve writing --- .../Components/Camera/CameraController.cpp | 27 +++--- src/Core/Components/Camera/CameraController.h | 8 +- .../Components/Camera/CameraLensFlare.cpp | 22 ++--- src/Core/Components/Camera/CameraLensFlare.h | 5 +- src/Core/Rendering/RenderAdaptor.cpp | 3 +- src/Core/Rendering/RenderController.cpp | 86 ++++++++----------- src/Core/Rendering/RenderController.h | 20 ++++- src/Core/Rendering/RenderPipeline.h | 4 +- src/Platform/OpenGL/Shaders/lens_flare.glsl | 3 +- 9 files changed, 90 insertions(+), 88 deletions(-) diff --git a/src/Core/Components/Camera/CameraController.cpp b/src/Core/Components/Camera/CameraController.cpp index fcc163a2..bd1077c1 100644 --- a/src/Core/Components/Camera/CameraController.cpp +++ b/src/Core/Components/Camera/CameraController.cpp @@ -445,14 +445,14 @@ namespace MxEngine return this->renderBuffers->SwapHDR2; } - TextureHandle CameraController::GetSwapHalf1() const + TextureHandle CameraController::GetSwapQuater1() const { - return this->renderBuffers->SwapHalf1; + return this->renderBuffers->SwapQuater1; } - TextureHandle CameraController::GetSwapHalf2() const + TextureHandle CameraController::GetSwapQuater2() const { - return this->renderBuffers->SwapHalf2; + return this->renderBuffers->SwapQuater2; } TextureHandle CameraController::GetSwapHDRTexture1() const @@ -471,8 +471,8 @@ namespace MxEngine this->HDR = Factory::Create(); this->SwapHDR1 = Factory::Create(); this->SwapHDR2 = Factory::Create(); - this->SwapHalf1 = Factory::Create(); - this->SwapHalf2 = Factory::Create(); + this->SwapQuater1 = Factory::Create(); + this->SwapQuater2 = Factory::Create(); this->Resize(width, height); @@ -525,13 +525,13 @@ namespace MxEngine this->SwapHDR2->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap hdr 2")); this->SwapHDR2->SetWrapType(TextureWrap::CLAMP_TO_EDGE); - this->SwapHalf1->Load(nullptr, width / 4, height / 4, 3, false, TextureFormat::RGBA16F); - this->SwapHalf1->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap half 1")); - this->SwapHalf1->SetWrapType(TextureWrap::CLAMP_TO_EDGE); + this->SwapQuater1->Load(nullptr, width / 4, height / 4, 3, false, TextureFormat::RGBA16F); + this->SwapQuater1->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap half 1")); + this->SwapQuater1->SetWrapType(TextureWrap::CLAMP_TO_EDGE); - this->SwapHalf2->Load(nullptr, width / 4, height / 4, 3, false, TextureFormat::RGBA16F); - this->SwapHalf2->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap half 2")); - this->SwapHalf2->SetWrapType(TextureWrap::CLAMP_TO_EDGE); + this->SwapQuater2->Load(nullptr, width / 4, height / 4, 3, false, TextureFormat::RGBA16F); + this->SwapQuater2->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap half 2")); + this->SwapQuater2->SetWrapType(TextureWrap::CLAMP_TO_EDGE); } void CameraRender::DeInit() @@ -544,7 +544,8 @@ namespace MxEngine Factory::Destroy(this->HDR); Factory::Destroy(this->SwapHDR1); Factory::Destroy(this->SwapHDR2); - Factory::Destroy(this->SwapHalf1); + Factory::Destroy(this->SwapQuater1); + Factory::Destroy(this->SwapQuater2); } MXENGINE_REFLECT_TYPE diff --git a/src/Core/Components/Camera/CameraController.h b/src/Core/Components/Camera/CameraController.h index d4976bad..b148cfc9 100644 --- a/src/Core/Components/Camera/CameraController.h +++ b/src/Core/Components/Camera/CameraController.h @@ -53,8 +53,8 @@ namespace MxEngine TextureHandle HDR; TextureHandle SwapHDR1; TextureHandle SwapHDR2; - TextureHandle SwapHalf1; - TextureHandle SwapHalf2; + TextureHandle SwapQuater1; + TextureHandle SwapQuater2; void Init(int width, int height); void Resize(int width, int height); @@ -137,7 +137,7 @@ namespace MxEngine TextureHandle GetHDRTexture() const; TextureHandle GetSwapHDRTexture1() const; TextureHandle GetSwapHDRTexture2() const; - TextureHandle GetSwapHalf1() const; - TextureHandle GetSwapHalf2() const; + TextureHandle GetSwapQuater1() const; + TextureHandle GetSwapQuater2() const; }; } \ No newline at end of file diff --git a/src/Core/Components/Camera/CameraLensFlare.cpp b/src/Core/Components/Camera/CameraLensFlare.cpp index 3c9875f2..be70c4d0 100644 --- a/src/Core/Components/Camera/CameraLensFlare.cpp +++ b/src/Core/Components/Camera/CameraLensFlare.cpp @@ -29,29 +29,29 @@ namespace MxEngine return this->lensFalreHaloWidth; } - void CameraLensFlare::SetLensFlareScale(float v) + void CameraLensFlare::SetLensFlareScale(float scale) { - this->lensFlareScale = v; + this->lensFlareScale = scale; } - void CameraLensFlare::SetLensFlareBias(float v) + void CameraLensFlare::SetLensFlareBias(float bias) { - this->lensFlareBias = v; + this->lensFlareBias = bias; } - void CameraLensFlare::SetLensFlareNumOfGhosts(int v) + void CameraLensFlare::SetLensFlareNumOfGhosts(int num) { - this->lensFlareNumOfGhosts = v; + this->lensFlareNumOfGhosts = Max(num,0); } - void CameraLensFlare::SetLensFlareGhostDispersal(float v) + void CameraLensFlare::SetLensFlareGhostDispersal(float dispersal) { - this->lensFlareGhostDispersal = v; + this->lensFlareGhostDispersal = dispersal; } - void CameraLensFlare::SetLensFlareHaloWidth(float v) + void CameraLensFlare::SetLensFlareHaloWidth(float width) { - this->lensFalreHaloWidth = v; + this->lensFalreHaloWidth = width; } MXENGINE_REFLECT_TYPE @@ -82,7 +82,7 @@ namespace MxEngine .property("lens flare ghost dispersal", &CameraLensFlare::GetLensFlareGhostDispersal, &CameraLensFlare::SetLensFlareGhostDispersal) ( rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.001f, 1.f }), rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) ) .property("lens falre halo width", &CameraLensFlare::GetLensFlareHaloWidth, &CameraLensFlare::SetLensFlareHaloWidth) diff --git a/src/Core/Components/Camera/CameraLensFlare.h b/src/Core/Components/Camera/CameraLensFlare.h index c043b13e..278e4062 100644 --- a/src/Core/Components/Camera/CameraLensFlare.h +++ b/src/Core/Components/Camera/CameraLensFlare.h @@ -9,12 +9,11 @@ namespace MxEngine { MAKE_COMPONENT(CameraLensFlare); private: - float lensFlareScale = 1.f; - float lensFlareBias = -42; + float lensFlareScale = 1.0f; + float lensFlareBias = -42.0f; int lensFlareNumOfGhosts = 10; float lensFlareGhostDispersal = 0.13f; float lensFalreHaloWidth = 0.5f; - bool lensFlareEnable = true; public: CameraLensFlare() = default; diff --git a/src/Core/Rendering/RenderAdaptor.cpp b/src/Core/Rendering/RenderAdaptor.cpp index cb8c7a8f..673da902 100644 --- a/src/Core/Rendering/RenderAdaptor.cpp +++ b/src/Core/Rendering/RenderAdaptor.cpp @@ -412,7 +412,8 @@ namespace MxEngine CameraGodRay* godRay = godRayComponent.IsValid() ? godRayComponent.GetUnchecked() : nullptr; CameraLensFlare* lensFlare = lensFlareComponent.IsValid() ? lensFlareComponent.GetUnchecked() : nullptr; - this->Renderer.SubmitCamera(camera, transform, skybox, effects, toneMapping, ssr, ssgi, ssao, godRay, lensFlare); + CameraInfo camInfo{ camera, transform, skybox, effects, toneMapping, ssr, ssgi, ssao, godRay, lensFlare }; + this->Renderer.SubmitCamera(std::move(camInfo)); TrackMainCameraIndex(camera); } } diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index d9b14af8..8292ff42 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -398,7 +398,7 @@ namespace MxEngine this->ComputeBloomEffect(camera, camera.HDRTexture); this->ApplySSGI(camera, camera.HDRTexture, camera.SwapTexture1, camera.SwapTexture2); - this->ApplyLensFlare(camera, camera.HDRTexture, camera.SwapHalfTexture1, camera.SwapHalfTexture2, camera.SwapTexture1); + this->ApplyLensFlare(camera, camera.HDRTexture, camera.SwapQuaterTexture1, camera.SwapQuaterTexture2, camera.SwapTexture1); this->ApplyGodRayEffect(camera, camera.HDRTexture, camera.SwapTexture1); this->ApplyChromaticAbberation(camera, camera.HDRTexture, camera.SwapTexture1); @@ -760,7 +760,7 @@ namespace MxEngine std::swap(inputOutput, temporary0); } - void RenderController::ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporaryHalf0, TextureHandle& temporaryHalf1, TextureHandle& temporary1) + void RenderController::ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporaryQuater0, TextureHandle& temporaryQuater1, TextureHandle& temporary1) { MAKE_SCOPE_PROFILER("RenderController::ApplyLensFlare()"); if (camera.LensFlare == nullptr) @@ -775,22 +775,22 @@ namespace MxEngine float haloWidth = camera.LensFlare->GetLensFlareHaloWidth(); input->GenerateMipmaps(); - temporaryHalf0->GenerateMipmaps(); + temporaryQuater0->GenerateMipmaps(); auto& prefilter = this->Pipeline.Environment.Shaders["LensFlarePrefilter"_id]; prefilter->Bind(); prefilter->SetUniform("uScale", scale); prefilter->SetUniform("uBias", bias); input->Bind(0); - this->RenderToTextureNoClear(temporaryHalf0, prefilter); + this->RenderToTextureNoClear(temporaryQuater0, prefilter); - this->ApplyGaussianBlur(temporaryHalf0, temporaryHalf1,3); + this->ApplyGaussianBlur(temporaryQuater0, temporaryQuater1,3); auto& lensFlare = this->Pipeline.Environment.Shaders["LensFlare"_id]; lensFlare->Bind(); lensFlare->SetUniform("ghosts", numOfGhosts); lensFlare->SetUniform("ghostDispersal", dispersal); lensFlare->SetUniform("uHaloWidth", haloWidth); - temporaryHalf1->Bind(0); + temporaryQuater1->Bind(0); input->Bind(1); this->RenderToTextureNoClear(temporary1, lensFlare); @@ -1469,52 +1469,42 @@ namespace MxEngine baseLightData->Direction = light.GetMaxDistance() * Normalize(light.Direction); } - void RenderController::SubmitCamera( - const CameraController& controller, - const Transform& parentTransform, - const Skybox* skybox, - const CameraEffects* effects, - const CameraToneMapping* toneMapping, - const CameraSSR* ssr, - const CameraSSGI* ssgi, - const CameraSSAO* ssao, - const CameraGodRay* godRay, - const CameraLensFlare* lensFlare) + void RenderController::SubmitCamera(CameraInfo&& info) { auto& camera = this->Pipeline.Cameras.emplace_back(); - camera.ViewportPosition = parentTransform.GetPosition(); - camera.AspectRatio = controller.Camera.GetAspectRatio(); - camera.StaticViewProjectionMatrix = controller.GetMatrix(MakeVector3(0.0f)); - camera.ViewProjectionMatrix = controller.GetMatrix(parentTransform.GetPosition()); + camera.ViewportPosition = info.parentTransform.GetPosition(); + camera.AspectRatio = info.controller.Camera.GetAspectRatio(); + camera.StaticViewProjectionMatrix = info.controller.GetMatrix(MakeVector3(0.0f)); + camera.ViewProjectionMatrix = info.controller.GetMatrix(info.parentTransform.GetPosition()); camera.InverseViewProjMatrix = Inverse(camera.ViewProjectionMatrix); - camera.Culler = controller.GetFrustrumCuller(); - camera.IsPerspective = controller.GetCameraType() == CameraType::PERSPECTIVE; - camera.GBuffer = controller.GetGBuffer(); - camera.AlbedoTexture = controller.GetAlbedoTexture(); - camera.NormalTexture = controller.GetNormalTexture(); - camera.MaterialTexture = controller.GetMaterialTexture(); - camera.DepthTexture = controller.GetDepthTexture(); - camera.AverageWhiteTexture = controller.GetAverageWhiteTexture(); - camera.HDRTexture = controller.GetHDRTexture(); - camera.SwapTexture1 = controller.GetSwapHDRTexture1(); - camera.SwapTexture2 = controller.GetSwapHDRTexture2(); - camera.SwapHalfTexture1 = controller.GetSwapHalf1(); - camera.SwapHalfTexture2 = controller.GetSwapHalf2(); - camera.OutputTexture = controller.GetRenderTexture(); - camera.RenderToTexture = controller.IsRendering(); - camera.SkyboxTexture = (skybox != nullptr && skybox->CubeMap.IsValid()) ? skybox->CubeMap : this->Pipeline.Environment.DefaultSkybox; - camera.IrradianceTexture = (skybox != nullptr && skybox->Irradiance.IsValid()) ? skybox->Irradiance : camera.SkyboxTexture; - camera.SkyboxIntensity = (skybox != nullptr) ? skybox->GetIntensity() : Skybox::DefaultIntensity; - camera.InversedSkyboxRotation = (skybox != nullptr) ? Transpose(MakeRotationMatrix(RadiansVec(skybox->GetRotation()))) : Matrix3x3(1.0f); - camera.Gamma = (toneMapping != nullptr) ? toneMapping->GetGamma() : CameraToneMapping::DefaultGamma; - camera.Effects = effects; - camera.ToneMapping = toneMapping; - camera.SSR = ssr; - camera.SSGI = ssgi; - camera.SSAO = ssao; - camera.GodRay = godRay; - camera.LensFlare = lensFlare; + camera.Culler = info.controller.GetFrustrumCuller(); + camera.IsPerspective = info.controller.GetCameraType() == CameraType::PERSPECTIVE; + camera.GBuffer = info.controller.GetGBuffer(); + camera.AlbedoTexture = info.controller.GetAlbedoTexture(); + camera.NormalTexture = info.controller.GetNormalTexture(); + camera.MaterialTexture = info.controller.GetMaterialTexture(); + camera.DepthTexture = info.controller.GetDepthTexture(); + camera.AverageWhiteTexture = info.controller.GetAverageWhiteTexture(); + camera.HDRTexture = info.controller.GetHDRTexture(); + camera.SwapTexture1 = info.controller.GetSwapHDRTexture1(); + camera.SwapTexture2 = info.controller.GetSwapHDRTexture2(); + camera.SwapQuaterTexture1 = info.controller.GetSwapQuater1(); + camera.SwapQuaterTexture2 = info.controller.GetSwapQuater2(); + camera.OutputTexture = info.controller.GetRenderTexture(); + camera.RenderToTexture = info.controller.IsRendering(); + camera.SkyboxTexture = (info.skybox != nullptr && info.skybox->CubeMap.IsValid()) ? info.skybox->CubeMap : this->Pipeline.Environment.DefaultSkybox; + camera.IrradianceTexture = (info.skybox != nullptr && info.skybox->Irradiance.IsValid()) ? info.skybox->Irradiance : camera.SkyboxTexture; + camera.SkyboxIntensity = (info.skybox != nullptr) ? info.skybox->GetIntensity() : Skybox::DefaultIntensity; + camera.InversedSkyboxRotation = (info.skybox != nullptr) ? Transpose(MakeRotationMatrix(RadiansVec(info.skybox->GetRotation()))) : Matrix3x3(1.0f); + camera.Gamma = (info.toneMapping != nullptr) ? info.toneMapping->GetGamma() : CameraToneMapping::DefaultGamma; + camera.Effects = info.effects; + camera.ToneMapping = info.toneMapping; + camera.SSR = info.ssr; + camera.SSGI = info.ssgi; + camera.SSAO = info.ssao; + camera.GodRay = info.godRay; + camera.LensFlare = info.lensFlare; } size_t RenderController::SubmitRenderGroup(const Mesh& mesh, size_t instanceOffset, size_t instanceCount) diff --git a/src/Core/Rendering/RenderController.h b/src/Core/Rendering/RenderController.h index ed8a620f..1e6bff76 100644 --- a/src/Core/Rendering/RenderController.h +++ b/src/Core/Rendering/RenderController.h @@ -47,6 +47,20 @@ namespace MxEngine class ParticleSystem; class Transform; + struct CameraInfo + { + const CameraController& controller; + const Transform& parentTransform; + const Skybox* skybox; + const CameraEffects* effects; + const CameraToneMapping* toneMapping; + const CameraSSR* ssr; + const CameraSSGI* ssgi; + const CameraSSAO* ssao; + const CameraGodRay* godRay; + const CameraLensFlare* lensFlare; + }; + class RenderController { Renderer renderer; @@ -76,7 +90,7 @@ namespace MxEngine void ApplyVignette(CameraUnit& camera, TextureHandle& input, TextureHandle& output); void ApplyColorGrading(CameraUnit& camera, TextureHandle& input, TextureHandle& output); void ApplyDepthOfFieldEffect(CameraUnit& camera, TextureHandle& input, TextureHandle& temporary, TextureHandle& output); - void ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporaryHalf0, TextureHandle& temporaryHalf1, TextureHandle& temporary1); + void ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporaryQuater0, TextureHandle& temporaryQuater1, TextureHandle& temporary1); void DrawIBL(CameraUnit& camera, TextureHandle& output); void DrawDirectionalLights(CameraUnit& camera, TextureHandle& output); void DrawShadowedPointLights(CameraUnit& camera, TextureHandle& output); @@ -129,9 +143,7 @@ namespace MxEngine void SubmitLightSource(const DirectionalLight& light, const Transform& parentTransform); void SubmitLightSource(const PointLight& light, const Transform& parentTransform); void SubmitLightSource(const SpotLight& light, const Transform& parentTransform); - void SubmitCamera(const CameraController& controller, const Transform& parentTransform, - const Skybox* skybox, const CameraEffects* effects, const CameraToneMapping* toneMapping, - const CameraSSR* ssr, const CameraSSGI* ssgi, const CameraSSAO* ssao,const CameraGodRay* godRay, const CameraLensFlare* lensFlare); + void SubmitCamera(CameraInfo&& info); size_t SubmitRenderGroup(const Mesh& mesh, size_t instanceOffset, size_t instanceCount); void SubmitRenderUnit(size_t renderGroupIndex, const SubMesh& object, const Material& material, const Transform& parentTransform, bool castsShadow, const char* debugName = nullptr); void SubmitImage(const TextureHandle& texture); diff --git a/src/Core/Rendering/RenderPipeline.h b/src/Core/Rendering/RenderPipeline.h index 22cba881..a7627ef3 100644 --- a/src/Core/Rendering/RenderPipeline.h +++ b/src/Core/Rendering/RenderPipeline.h @@ -67,8 +67,8 @@ namespace MxEngine TextureHandle HDRTexture; TextureHandle SwapTexture1; TextureHandle SwapTexture2; - TextureHandle SwapHalfTexture1; - TextureHandle SwapHalfTexture2; + TextureHandle SwapQuaterTexture1; + TextureHandle SwapQuaterTexture2; FrustrumCuller Culler; Matrix4x4 InverseViewProjMatrix; diff --git a/src/Platform/OpenGL/Shaders/lens_flare.glsl b/src/Platform/OpenGL/Shaders/lens_flare.glsl index 0165d80c..e6193dda 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare.glsl @@ -10,8 +10,7 @@ out vec4 outColor; void main() { - vec2 texcoord = TexCoord; - texcoord = -texcoord + vec2(1.0); + texcoord = vec2(1.0) - TexCoord; // ghost vector to image centre: vec2 ghostVec = (vec2(0.5) - texcoord) * ghostDispersal; From d17ea1d485a146d475f93c5da3b3e19b9e3dca9f Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Mon, 5 Jun 2023 22:56:13 +0800 Subject: [PATCH 08/26] fix compile --- src/Platform/OpenGL/Shaders/lens_flare.glsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Platform/OpenGL/Shaders/lens_flare.glsl b/src/Platform/OpenGL/Shaders/lens_flare.glsl index e6193dda..2d3a80fd 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare.glsl @@ -10,7 +10,7 @@ out vec4 outColor; void main() { - texcoord = vec2(1.0) - TexCoord; + vec2 texcoord = vec2(1.0) - TexCoord; // ghost vector to image centre: vec2 ghostVec = (vec2(0.5) - texcoord) * ghostDispersal; From a6b21824b8d280ccbb419344cbace4e711a38528 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Tue, 6 Jun 2023 00:12:09 +0800 Subject: [PATCH 09/26] fix wrongly return --- src/Core/Rendering/RenderController.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index 8292ff42..126cd1fc 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -720,7 +720,6 @@ namespace MxEngine void RenderController::ApplyDepthOfFieldEffect(CameraUnit& camera, TextureHandle& inputOutput, TextureHandle& temporary0, TextureHandle& temporary1) { - return; if (camera.Effects->GetFocusDistance() == 0.f) return; MAKE_SCOPE_PROFILER("RenderController::ApplyDepthOfFieldEffect()"); @@ -775,6 +774,7 @@ namespace MxEngine float haloWidth = camera.LensFlare->GetLensFlareHaloWidth(); input->GenerateMipmaps(); + temporaryQuater0->GenerateMipmaps(); auto& prefilter = this->Pipeline.Environment.Shaders["LensFlarePrefilter"_id]; prefilter->Bind(); From c22d8f9937c66430add1e3c0dac39f271288456b Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Tue, 6 Jun 2023 01:04:38 +0800 Subject: [PATCH 10/26] using weighted sum to calc lum --- src/Core/Rendering/RenderController.cpp | 5 +++-- src/Platform/OpenGL/Shaders/Library/shader_utils.glsl | 7 +++++++ .../OpenGL/Shaders/average_white_fragment.glsl | 6 +++--- src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl | 11 ++++++----- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index 126cd1fc..b5d54f94 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -761,11 +761,12 @@ namespace MxEngine void RenderController::ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporaryQuater0, TextureHandle& temporaryQuater1, TextureHandle& temporary1) { - MAKE_SCOPE_PROFILER("RenderController::ApplyLensFlare()"); if (camera.LensFlare == nullptr) return; - //Todo: support chromatic aberration + MAKE_SCOPE_PROFILER("RenderController::ApplyLensFlare()"); + + //Todo: support chromatic aberration //Todo: add starbust effect float scale = camera.LensFlare->GetLensFlareScale(); float bias = camera.LensFlare->GetLensFlareBias(); diff --git a/src/Platform/OpenGL/Shaders/Library/shader_utils.glsl b/src/Platform/OpenGL/Shaders/Library/shader_utils.glsl index d8b00775..77477d9b 100644 --- a/src/Platform/OpenGL/Shaders/Library/shader_utils.glsl +++ b/src/Platform/OpenGL/Shaders/Library/shader_utils.glsl @@ -148,4 +148,11 @@ FragmentInfo getFragmentInfo(vec2 texCoord, sampler2D albedoTexture, sampler2D n float noise(vec2 p) { return fract(sin(dot(p, vec2(123.45, 875.43))) * 5432.3); +} + +float calcLuminance(vec3 color) +{ + vec3 weight = vec3(0.2125f, 0.7154f, 0.0721f); + float luminance = dot(weight, color);//Weighted sum + return luminance; } \ No newline at end of file diff --git a/src/Platform/OpenGL/Shaders/average_white_fragment.glsl b/src/Platform/OpenGL/Shaders/average_white_fragment.glsl index e0031729..0d7324c1 100644 --- a/src/Platform/OpenGL/Shaders/average_white_fragment.glsl +++ b/src/Platform/OpenGL/Shaders/average_white_fragment.glsl @@ -1,3 +1,5 @@ +#include "Library/shader_utils.glsl" + in vec2 TexCoord; out vec4 OutColor; @@ -6,13 +8,11 @@ uniform sampler2D prevFrameWhite; uniform float adaptSpeed; uniform float adaptThreshold; -vec3 luminance = vec3(0.2125f, 0.7154f, 0.0721f); - void main() { vec3 color = texture(curFrameHDR, TexCoord).rgb; float oldWhite = texture(prevFrameWhite, vec2(0.0f)).r; - float curWhite = dot(luminance, color); + float curWhite = calcLuminance(color); float diff = abs(curWhite - oldWhite) < adaptThreshold ? 0.0f : curWhite - oldWhite; diff --git a/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl b/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl index 992fd638..a09a30f7 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl @@ -1,3 +1,5 @@ +#include "Library/shader_utils.glsl" + layout(binding = 0) uniform sampler2D inputColor; uniform float uScale; @@ -9,9 +11,8 @@ out vec4 outputColor; void main() { - vec4 col = texture(inputColor, TexCoord); - if(length(max(vec4(0.0),col+vec4(uBias)))<=0.001f) - outputColor=vec4(0.0); - else - outputColor=col*vec4(uScale); + vec3 col = texture(inputColor, TexCoord).rgb; + float lum = calcLuminance(col) + uBias; + col = max(lum,0.0) * col * uScale; + outputColor=vec4(col,1.0f); } \ No newline at end of file From ee9f78be2ab8e6eec11ae8bf549b7304f9d04873 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Sun, 18 Jun 2023 01:01:52 +0800 Subject: [PATCH 11/26] fix halo visibility --- src/Platform/OpenGL/Shaders/lens_flare.glsl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Platform/OpenGL/Shaders/lens_flare.glsl b/src/Platform/OpenGL/Shaders/lens_flare.glsl index 2d3a80fd..c94b64ea 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare.glsl @@ -1,3 +1,4 @@ +#include "Library/shader_utils.glsl" layout(binding = 0) uniform sampler2D lensFlareColor; layout(binding = 1) uniform sampler2D inputColor; @@ -8,6 +9,8 @@ uniform float uHaloWidth; in vec2 TexCoord; out vec4 outColor; +const float gWeight = 80.0f;//relative to pixel pos + void main() { vec2 texcoord = vec2(1.0) - TexCoord; @@ -19,15 +22,16 @@ void main() for (int i = 0; i < ghosts; ++i) { vec2 offset = fract(texcoord + ghostVec * float(i)); float weight = length(vec2(0.5) - offset) / length(vec2(0.5)); - weight = pow(1.0 - weight, 80.0); + weight = pow(1.0 - weight, gWeight); result += texture(lensFlareColor, offset) * weight; } vec2 haloVec = normalize(ghostVec) * uHaloWidth; float weight = length(vec2(0.5) - fract(texcoord + haloVec)) / length(vec2(0.5)); - weight = pow(1.0 - weight, 75.0); - result += texture(inputColor, texcoord + haloVec) * weight; - + weight = pow(1.0 - weight, gWeight); + vec4 haloColor = texture(inputColor, texcoord + haloVec) * weight; + haloColor = max(calcLuminance(haloColor.rgb)-0.7f,0.0) * haloColor; + result+=haloColor; outColor = result + texture(inputColor, TexCoord); } \ No newline at end of file From 0f8bf0c0329ce2b1204ac1bf8ddce2dbd8c6ce3c Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Mon, 19 Jun 2023 01:09:10 +0800 Subject: [PATCH 12/26] using average white texture --- src/Core/Rendering/RenderController.cpp | 10 +++++++++- .../OpenGL/Shaders/lens_flare_prefilter.glsl | 15 ++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index b5d54f94..0b1cb94e 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -766,6 +766,13 @@ namespace MxEngine MAKE_SCOPE_PROFILER("RenderController::ApplyLensFlare()"); + float averageLum = 0.f; + if (camera.ToneMapping != nullptr) + { + Image averageTexData = camera.AverageWhiteTexture->GetRawTextureData(); + averageLum = *reinterpret_cast(averageTexData.GetRawData()); + } + //Todo: support chromatic aberration //Todo: add starbust effect float scale = camera.LensFlare->GetLensFlareScale(); @@ -781,6 +788,7 @@ namespace MxEngine prefilter->Bind(); prefilter->SetUniform("uScale", scale); prefilter->SetUniform("uBias", bias); + prefilter->SetUniform("uAverage", fmax(averageLum,0.f)); input->Bind(0); this->RenderToTextureNoClear(temporaryQuater0, prefilter); @@ -790,7 +798,7 @@ namespace MxEngine lensFlare->Bind(); lensFlare->SetUniform("ghosts", numOfGhosts); lensFlare->SetUniform("ghostDispersal", dispersal); - lensFlare->SetUniform("uHaloWidth", haloWidth); + lensFlare->SetUniform("uHaloWidth", haloWidth); temporaryQuater1->Bind(0); input->Bind(1); this->RenderToTextureNoClear(temporary1, lensFlare); diff --git a/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl b/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl index a09a30f7..bd0f0812 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl @@ -4,6 +4,7 @@ layout(binding = 0) uniform sampler2D inputColor; uniform float uScale; uniform float uBias; +uniform float uAverage; in vec2 TexCoord; @@ -12,7 +13,15 @@ out vec4 outputColor; void main() { vec3 col = texture(inputColor, TexCoord).rgb; - float lum = calcLuminance(col) + uBias; - col = max(lum,0.0) * col * uScale; - outputColor=vec4(col,1.0f); + + float lum = calcLuminance(col) - uAverage; + if (lum <= 0) + { + outputColor = vec4(vec3(0.0f), 1.0f); + return; + } + + lum += uBias; + col = max(lum, 0.0f) * col * uScale; + outputColor = vec4(col, 1.0f); } \ No newline at end of file From 49b702ed1fd91637b141cf8f3713d4a2183a753e Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Mon, 19 Jun 2023 11:56:51 +0800 Subject: [PATCH 13/26] remove gpu -> cpu sync --- src/Core/Rendering/RenderController.cpp | 9 +-------- src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl | 4 ++-- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index 0b1cb94e..02b4e346 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -765,13 +765,6 @@ namespace MxEngine return; MAKE_SCOPE_PROFILER("RenderController::ApplyLensFlare()"); - - float averageLum = 0.f; - if (camera.ToneMapping != nullptr) - { - Image averageTexData = camera.AverageWhiteTexture->GetRawTextureData(); - averageLum = *reinterpret_cast(averageTexData.GetRawData()); - } //Todo: support chromatic aberration //Todo: add starbust effect @@ -788,8 +781,8 @@ namespace MxEngine prefilter->Bind(); prefilter->SetUniform("uScale", scale); prefilter->SetUniform("uBias", bias); - prefilter->SetUniform("uAverage", fmax(averageLum,0.f)); input->Bind(0); + camera.AverageWhiteTexture->Bind(1); this->RenderToTextureNoClear(temporaryQuater0, prefilter); this->ApplyGaussianBlur(temporaryQuater0, temporaryQuater1,3); diff --git a/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl b/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl index bd0f0812..dd12bf4f 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl @@ -1,10 +1,10 @@ #include "Library/shader_utils.glsl" layout(binding = 0) uniform sampler2D inputColor; +layout(binding = 1) uniform sampler2D averageWhiteTex; uniform float uScale; uniform float uBias; -uniform float uAverage; in vec2 TexCoord; @@ -14,7 +14,7 @@ void main() { vec3 col = texture(inputColor, TexCoord).rgb; - float lum = calcLuminance(col) - uAverage; + float lum = calcLuminance(col) - max(texture(averageWhiteTex, vec2(0.0f)).r,0.0f); if (lum <= 0) { outputColor = vec4(vec3(0.0f), 1.0f); From 8fd56c5f17773aa6841b2b40139bef0571037352 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Fri, 23 Jun 2023 00:11:29 +0800 Subject: [PATCH 14/26] fix halo --- .../Components/Camera/CameraController.cpp | 15 +++++- src/Core/Components/Camera/CameraController.h | 2 + src/Core/Rendering/RenderAdaptor.cpp | 11 ++-- src/Core/Rendering/RenderController.cpp | 46 +++++++++++------ src/Core/Rendering/RenderController.h | 2 +- src/Core/Rendering/RenderPipeline.h | 1 + src/Platform/OpenGL/Shaders/lens_flare.glsl | 50 +++++++++---------- .../OpenGL/Shaders/lens_flare_ghosts.glsl | 27 ++++++++++ .../OpenGL/Shaders/lens_flare_halo.glsl | 29 +++++++++++ .../OpenGL/Shaders/lens_flare_prefilter.glsl | 27 ---------- 10 files changed, 135 insertions(+), 75 deletions(-) create mode 100644 src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl create mode 100644 src/Platform/OpenGL/Shaders/lens_flare_halo.glsl delete mode 100644 src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl diff --git a/src/Core/Components/Camera/CameraController.cpp b/src/Core/Components/Camera/CameraController.cpp index bd1077c1..1078d928 100644 --- a/src/Core/Components/Camera/CameraController.cpp +++ b/src/Core/Components/Camera/CameraController.cpp @@ -455,6 +455,11 @@ namespace MxEngine return this->renderBuffers->SwapQuater2; } + TextureHandle CameraController::GetSwapQuater3() const + { + return this->renderBuffers->SwapQuater3; + } + TextureHandle CameraController::GetSwapHDRTexture1() const { return this->renderBuffers->SwapHDR1; @@ -473,6 +478,7 @@ namespace MxEngine this->SwapHDR2 = Factory::Create(); this->SwapQuater1 = Factory::Create(); this->SwapQuater2 = Factory::Create(); + this->SwapQuater3 = Factory::Create(); this->Resize(width, height); @@ -526,12 +532,16 @@ namespace MxEngine this->SwapHDR2->SetWrapType(TextureWrap::CLAMP_TO_EDGE); this->SwapQuater1->Load(nullptr, width / 4, height / 4, 3, false, TextureFormat::RGBA16F); - this->SwapQuater1->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap half 1")); + this->SwapQuater1->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap quater 1")); this->SwapQuater1->SetWrapType(TextureWrap::CLAMP_TO_EDGE); this->SwapQuater2->Load(nullptr, width / 4, height / 4, 3, false, TextureFormat::RGBA16F); - this->SwapQuater2->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap half 2")); + this->SwapQuater2->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap quater 2")); this->SwapQuater2->SetWrapType(TextureWrap::CLAMP_TO_EDGE); + + this->SwapQuater3->Load(nullptr, width / 4, height / 4, 3, false, TextureFormat::RGBA16F); + this->SwapQuater3->SetInternalEngineTag(MXENGINE_MAKE_INTERNAL_TAG("camera swap quater 3")); + this->SwapQuater3->SetWrapType(TextureWrap::CLAMP_TO_EDGE); } void CameraRender::DeInit() @@ -546,6 +556,7 @@ namespace MxEngine Factory::Destroy(this->SwapHDR2); Factory::Destroy(this->SwapQuater1); Factory::Destroy(this->SwapQuater2); + Factory::Destroy(this->SwapQuater3); } MXENGINE_REFLECT_TYPE diff --git a/src/Core/Components/Camera/CameraController.h b/src/Core/Components/Camera/CameraController.h index b148cfc9..044e0690 100644 --- a/src/Core/Components/Camera/CameraController.h +++ b/src/Core/Components/Camera/CameraController.h @@ -55,6 +55,7 @@ namespace MxEngine TextureHandle SwapHDR2; TextureHandle SwapQuater1; TextureHandle SwapQuater2; + TextureHandle SwapQuater3; void Init(int width, int height); void Resize(int width, int height); @@ -139,5 +140,6 @@ namespace MxEngine TextureHandle GetSwapHDRTexture2() const; TextureHandle GetSwapQuater1() const; TextureHandle GetSwapQuater2() const; + TextureHandle GetSwapQuater3() const; }; } \ No newline at end of file diff --git a/src/Core/Rendering/RenderAdaptor.cpp b/src/Core/Rendering/RenderAdaptor.cpp index 673da902..8aade78c 100644 --- a/src/Core/Rendering/RenderAdaptor.cpp +++ b/src/Core/Rendering/RenderAdaptor.cpp @@ -335,9 +335,14 @@ namespace MxEngine shaderFolder / "lens_flare.glsl" ); - environment.Shaders["LensFlarePrefilter"_id] = AssetManager::LoadShader( + environment.Shaders["LensFlareGhosts"_id] = AssetManager::LoadShader( shaderFolder / "rect_vertex.glsl", - shaderFolder / "lens_flare_prefilter.glsl" + shaderFolder / "lens_flare_ghosts.glsl" + ); + + environment.Shaders["LensFlareHalo"_id] = AssetManager::LoadShader( + shaderFolder / "rect_vertex.glsl", + shaderFolder / "lens_flare_halo.glsl" ); // compute shaders @@ -451,7 +456,7 @@ namespace MxEngine } size_t renderGroupIndex = this->Renderer.SubmitRenderGroup(*mesh, instanceOffset, instanceCount); - for (const auto& submesh : mesh->GetSubMeshes()) + for (const auto& submesh : mesh->GetSubMeshes()) { auto materialId = submesh.GetMaterialId(); if (materialId >= meshRenderer->Materials.size()) continue; diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index 02b4e346..406fbe0a 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -398,7 +398,7 @@ namespace MxEngine this->ComputeBloomEffect(camera, camera.HDRTexture); this->ApplySSGI(camera, camera.HDRTexture, camera.SwapTexture1, camera.SwapTexture2); - this->ApplyLensFlare(camera, camera.HDRTexture, camera.SwapQuaterTexture1, camera.SwapQuaterTexture2, camera.SwapTexture1); + this->ApplyLensFlare(camera, camera.HDRTexture, camera.SwapQuaterTexture1, camera.SwapQuaterTexture2, camera.SwapQuaterTexture3, camera.SwapTexture1); this->ApplyGodRayEffect(camera, camera.HDRTexture, camera.SwapTexture1); this->ApplyChromaticAbberation(camera, camera.HDRTexture, camera.SwapTexture1); @@ -759,7 +759,7 @@ namespace MxEngine std::swap(inputOutput, temporary0); } - void RenderController::ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporaryQuater0, TextureHandle& temporaryQuater1, TextureHandle& temporary1) + void RenderController::ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporaryQuater0, TextureHandle& temporaryQuater1, TextureHandle& temporaryQuater2, TextureHandle& temporary1) { if (camera.LensFlare == nullptr) return; @@ -776,24 +776,37 @@ namespace MxEngine input->GenerateMipmaps(); - temporaryQuater0->GenerateMipmaps(); - auto& prefilter = this->Pipeline.Environment.Shaders["LensFlarePrefilter"_id]; - prefilter->Bind(); - prefilter->SetUniform("uScale", scale); - prefilter->SetUniform("uBias", bias); - input->Bind(0); - camera.AverageWhiteTexture->Bind(1); - this->RenderToTextureNoClear(temporaryQuater0, prefilter); + { + temporaryQuater0->GenerateMipmaps(); + auto& shaderGhost = this->Pipeline.Environment.Shaders["LensFlareGhosts"_id]; + shaderGhost->Bind(); + shaderGhost->SetUniform("uScale", scale); + shaderGhost->SetUniform("uBias", bias); + input->Bind(0); + camera.AverageWhiteTexture->Bind(1); + this->RenderToTextureNoClear(temporaryQuater0, shaderGhost); + this->ApplyGaussianBlur(temporaryQuater0, temporaryQuater1, 3); + temporaryQuater1->GenerateMipmaps(); + } - this->ApplyGaussianBlur(temporaryQuater0, temporaryQuater1,3); + { + auto& shaderHalo = this->Pipeline.Environment.Shaders["LensFlareHalo"_id]; + shaderHalo->Bind(); + shaderHalo->SetUniform("uGhostDispersal", dispersal); + shaderHalo->SetUniform("uHaloWidth", haloWidth); + input->Bind(0); + temporaryQuater1->Bind(1); + this->RenderToTextureNoClear(temporaryQuater0, shaderHalo); + this->ApplyGaussianBlur(temporaryQuater0, temporaryQuater2, 3); + } auto& lensFlare = this->Pipeline.Environment.Shaders["LensFlare"_id]; lensFlare->Bind(); - lensFlare->SetUniform("ghosts", numOfGhosts); - lensFlare->SetUniform("ghostDispersal", dispersal); - lensFlare->SetUniform("uHaloWidth", haloWidth); - temporaryQuater1->Bind(0); - input->Bind(1); + lensFlare->SetUniform("uGhosts", numOfGhosts); + lensFlare->SetUniform("uGhostDispersal", dispersal); + temporaryQuater1->Bind(0); + temporaryQuater2->Bind(1); + input->Bind(2); this->RenderToTextureNoClear(temporary1, lensFlare); std::swap(input, temporary1); @@ -1493,6 +1506,7 @@ namespace MxEngine camera.SwapTexture2 = info.controller.GetSwapHDRTexture2(); camera.SwapQuaterTexture1 = info.controller.GetSwapQuater1(); camera.SwapQuaterTexture2 = info.controller.GetSwapQuater2(); + camera.SwapQuaterTexture3 = info.controller.GetSwapQuater3(); camera.OutputTexture = info.controller.GetRenderTexture(); camera.RenderToTexture = info.controller.IsRendering(); camera.SkyboxTexture = (info.skybox != nullptr && info.skybox->CubeMap.IsValid()) ? info.skybox->CubeMap : this->Pipeline.Environment.DefaultSkybox; diff --git a/src/Core/Rendering/RenderController.h b/src/Core/Rendering/RenderController.h index 1e6bff76..e3569b83 100644 --- a/src/Core/Rendering/RenderController.h +++ b/src/Core/Rendering/RenderController.h @@ -90,7 +90,7 @@ namespace MxEngine void ApplyVignette(CameraUnit& camera, TextureHandle& input, TextureHandle& output); void ApplyColorGrading(CameraUnit& camera, TextureHandle& input, TextureHandle& output); void ApplyDepthOfFieldEffect(CameraUnit& camera, TextureHandle& input, TextureHandle& temporary, TextureHandle& output); - void ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporaryQuater0, TextureHandle& temporaryQuater1, TextureHandle& temporary1); + void ApplyLensFlare(CameraUnit& camera, TextureHandle& input, TextureHandle& temporaryQuater0, TextureHandle& temporaryQuater1, TextureHandle& temporaryQuater2, TextureHandle& temporary1); void DrawIBL(CameraUnit& camera, TextureHandle& output); void DrawDirectionalLights(CameraUnit& camera, TextureHandle& output); void DrawShadowedPointLights(CameraUnit& camera, TextureHandle& output); diff --git a/src/Core/Rendering/RenderPipeline.h b/src/Core/Rendering/RenderPipeline.h index a7627ef3..5b9a8cf4 100644 --- a/src/Core/Rendering/RenderPipeline.h +++ b/src/Core/Rendering/RenderPipeline.h @@ -69,6 +69,7 @@ namespace MxEngine TextureHandle SwapTexture2; TextureHandle SwapQuaterTexture1; TextureHandle SwapQuaterTexture2; + TextureHandle SwapQuaterTexture3; FrustrumCuller Culler; Matrix4x4 InverseViewProjMatrix; diff --git a/src/Platform/OpenGL/Shaders/lens_flare.glsl b/src/Platform/OpenGL/Shaders/lens_flare.glsl index c94b64ea..55795ef3 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare.glsl @@ -1,10 +1,10 @@ #include "Library/shader_utils.glsl" -layout(binding = 0) uniform sampler2D lensFlareColor; -layout(binding = 1) uniform sampler2D inputColor; +layout(binding = 0) uniform sampler2D inputGhost; +layout(binding = 1) uniform sampler2D inputHalo; +layout(binding = 2) uniform sampler2D inputColor; -uniform int ghosts; // number of ghost samples -uniform float ghostDispersal; // dispersion factor -uniform float uHaloWidth; +uniform int uGhosts; // number of ghost samples +uniform float uGhostDispersal; // dispersion factor in vec2 TexCoord; out vec4 outColor; @@ -13,25 +13,23 @@ const float gWeight = 80.0f;//relative to pixel pos void main() { - vec2 texcoord = vec2(1.0) - TexCoord; - // ghost vector to image centre: - vec2 ghostVec = (vec2(0.5) - texcoord) * ghostDispersal; - - // sample ghosts - vec4 result = vec4(0.0); - for (int i = 0; i < ghosts; ++i) { - vec2 offset = fract(texcoord + ghostVec * float(i)); - float weight = length(vec2(0.5) - offset) / length(vec2(0.5)); - weight = pow(1.0 - weight, gWeight); - - result += texture(lensFlareColor, offset) * weight; - } - - vec2 haloVec = normalize(ghostVec) * uHaloWidth; - float weight = length(vec2(0.5) - fract(texcoord + haloVec)) / length(vec2(0.5)); - weight = pow(1.0 - weight, gWeight); - vec4 haloColor = texture(inputColor, texcoord + haloVec) * weight; - haloColor = max(calcLuminance(haloColor.rgb)-0.7f,0.0) * haloColor; - result+=haloColor; - outColor = result + texture(inputColor, TexCoord); + vec2 texcoord = vec2(1.0) - TexCoord; + // ghost vector to image centre: + vec2 ghostVec = (vec2(0.5) - texcoord) * uGhostDispersal; + + // sample ghosts + vec3 result = vec3(0.0); + for (int i = 0; i < uGhosts; ++i) + { + vec2 offset = fract(texcoord + 0.8 * ghostVec * float(i)); + float weight = length(vec2(0.5) - offset) / length(vec2(0.5)); + weight = pow(1.0 - weight, gWeight); + + result += texture(inputGhost, offset).rgb * weight; + } + + //halo + result += texture(inputHalo, TexCoord).rgb; + + outColor = vec4(result + texture(inputColor, TexCoord).rgb, 1.f); } \ No newline at end of file diff --git a/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl b/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl new file mode 100644 index 00000000..49f69b6d --- /dev/null +++ b/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl @@ -0,0 +1,27 @@ +#include "Library/shader_utils.glsl" + +layout(binding = 0) uniform sampler2D inputColor; +layout(binding = 1) uniform sampler2D averageWhiteTex; + +uniform float uScale; +uniform float uBias; + +in vec2 TexCoord; + +out vec4 outputColor; + +void main() +{ + vec3 col = texture(inputColor, TexCoord).rgb; + + float lum = calcLuminance(col) - max(texture(averageWhiteTex, vec2(0.0f)).r, 0.0f); + if (lum <= 0) + { + outputColor = vec4(vec3(0.0f), 1.0f); + return; + } + + lum += uBias; + col = max(lum, 0.0f) * col * uScale; + outputColor = vec4(col, 1.0f); +} \ No newline at end of file diff --git a/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl b/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl new file mode 100644 index 00000000..e065a63d --- /dev/null +++ b/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl @@ -0,0 +1,29 @@ +#include "Library/shader_utils.glsl" + +layout(binding = 0) uniform sampler2D inputColor; +layout(binding = 1) uniform sampler2D inputGhost; + +uniform float uGhostDispersal; // dispersion factor +uniform float uHaloWidth; + +in vec2 TexCoord; + +out vec4 outputColor; + +void main() +{ + vec3 haloColor = vec3(0.f); + + vec2 ghostTexcoord = vec2(1.0) - TexCoord; + vec2 haloVec = normalize((vec2(0.5) - ghostTexcoord) * uGhostDispersal) * uHaloWidth; + float weight = length(vec2(0.5) - fract(ghostTexcoord + haloVec)) / length(vec2(0.5)); + weight = pow(1.0 - weight, 80.f); + haloColor = texture(inputColor, ghostTexcoord + haloVec).rgb * weight; + + if (calcLuminance(texture(inputGhost, vec2(0.5), 4).rgb) < 5) + { + outputColor = vec4(vec3(0.f), 1.0f); + return; + } + outputColor = vec4(haloColor, 1.0f); +} \ No newline at end of file diff --git a/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl b/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl deleted file mode 100644 index dd12bf4f..00000000 --- a/src/Platform/OpenGL/Shaders/lens_flare_prefilter.glsl +++ /dev/null @@ -1,27 +0,0 @@ -#include "Library/shader_utils.glsl" - -layout(binding = 0) uniform sampler2D inputColor; -layout(binding = 1) uniform sampler2D averageWhiteTex; - -uniform float uScale; -uniform float uBias; - -in vec2 TexCoord; - -out vec4 outputColor; - -void main() -{ - vec3 col = texture(inputColor, TexCoord).rgb; - - float lum = calcLuminance(col) - max(texture(averageWhiteTex, vec2(0.0f)).r,0.0f); - if (lum <= 0) - { - outputColor = vec4(vec3(0.0f), 1.0f); - return; - } - - lum += uBias; - col = max(lum, 0.0f) * col * uScale; - outputColor = vec4(col, 1.0f); -} \ No newline at end of file From fe53b894d7cb6e31cc6400e0c1c9d4ba23a1c4bf Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Fri, 23 Jun 2023 00:14:53 +0800 Subject: [PATCH 15/26] update default setting for ghost num --- src/Core/Components/Camera/CameraLensFlare.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/Components/Camera/CameraLensFlare.h b/src/Core/Components/Camera/CameraLensFlare.h index 278e4062..cf8d585f 100644 --- a/src/Core/Components/Camera/CameraLensFlare.h +++ b/src/Core/Components/Camera/CameraLensFlare.h @@ -11,7 +11,7 @@ namespace MxEngine private: float lensFlareScale = 1.0f; float lensFlareBias = -42.0f; - int lensFlareNumOfGhosts = 10; + int lensFlareNumOfGhosts = 12; float lensFlareGhostDispersal = 0.13f; float lensFalreHaloWidth = 0.5f; From 45686b8ca5a18d00b0f7ed58e2422fd59e468ed8 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Fri, 23 Jun 2023 00:30:10 +0800 Subject: [PATCH 16/26] change gauss option --- src/Core/Rendering/RenderController.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index 406fbe0a..8f284ebf 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -797,7 +797,7 @@ namespace MxEngine input->Bind(0); temporaryQuater1->Bind(1); this->RenderToTextureNoClear(temporaryQuater0, shaderHalo); - this->ApplyGaussianBlur(temporaryQuater0, temporaryQuater2, 3); + this->ApplyGaussianBlur(temporaryQuater0, temporaryQuater2, 1); } auto& lensFlare = this->Pipeline.Environment.Shaders["LensFlare"_id]; From b89bfb423aab2b76ae933b81e0272f4e48d7644e Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Sat, 24 Jun 2023 00:27:19 +0800 Subject: [PATCH 17/26] remove constants --- src/Core/Rendering/RenderController.cpp | 33 ++++++++++++------- .../OpenGL/Shaders/lens_flare_halo.glsl | 20 ++++++----- src/Platform/Window/Window.cpp | 2 +- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index 8f284ebf..2e094343 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -763,11 +763,13 @@ namespace MxEngine { if (camera.LensFlare == nullptr) return; + if (camera.ToneMapping == nullptr)//lens flare requires tone mapping + return; MAKE_SCOPE_PROFILER("RenderController::ApplyLensFlare()"); - //Todo: support chromatic aberration - //Todo: add starbust effect + //TODO(fall2019): support chromatic aberration + //TODO(fall2019): add starbust effect float scale = camera.LensFlare->GetLensFlareScale(); float bias = camera.LensFlare->GetLensFlareBias(); int numOfGhosts = camera.LensFlare->GetLensFlareNumOfGhosts(); @@ -776,6 +778,7 @@ namespace MxEngine input->GenerateMipmaps(); + //calc ghosts { temporaryQuater0->GenerateMipmaps(); auto& shaderGhost = this->Pipeline.Environment.Shaders["LensFlareGhosts"_id]; @@ -789,26 +792,34 @@ namespace MxEngine temporaryQuater1->GenerateMipmaps(); } + //calc halo { auto& shaderHalo = this->Pipeline.Environment.Shaders["LensFlareHalo"_id]; shaderHalo->Bind(); shaderHalo->SetUniform("uGhostDispersal", dispersal); shaderHalo->SetUniform("uHaloWidth", haloWidth); + int mipLevel = ceil(Log2(Max(temporaryQuater1->GetWidth(), temporaryQuater1->GetHeight()))) - 2; + //use max mip level - 2 to get centre average brightness since only centre spot is used in ghosts generation + shaderHalo->SetUniform("uMipLevel", mipLevel); input->Bind(0); temporaryQuater1->Bind(1); + camera.AverageWhiteTexture->Bind(2); this->RenderToTextureNoClear(temporaryQuater0, shaderHalo); this->ApplyGaussianBlur(temporaryQuater0, temporaryQuater2, 1); } - auto& lensFlare = this->Pipeline.Environment.Shaders["LensFlare"_id]; - lensFlare->Bind(); - lensFlare->SetUniform("uGhosts", numOfGhosts); - lensFlare->SetUniform("uGhostDispersal", dispersal); - temporaryQuater1->Bind(0); - temporaryQuater2->Bind(1); - input->Bind(2); - this->RenderToTextureNoClear(temporary1, lensFlare); - + //combine + { + auto& lensFlare = this->Pipeline.Environment.Shaders["LensFlare"_id]; + lensFlare->Bind(); + lensFlare->SetUniform("uGhosts", numOfGhosts); + lensFlare->SetUniform("uGhostDispersal", dispersal); + temporaryQuater1->Bind(0); + temporaryQuater2->Bind(1); + input->Bind(2); + this->RenderToTextureNoClear(temporary1, lensFlare); + } + std::swap(input, temporary1); } diff --git a/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl b/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl index e065a63d..ee50805c 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl @@ -2,28 +2,32 @@ layout(binding = 0) uniform sampler2D inputColor; layout(binding = 1) uniform sampler2D inputGhost; +layout(binding = 2) uniform sampler2D inputSceneAverageWhite; uniform float uGhostDispersal; // dispersion factor uniform float uHaloWidth; +uniform int uMipLevel; in vec2 TexCoord; - out vec4 outputColor; +const float gWeight = 80.0f; + void main() { - vec3 haloColor = vec3(0.f); + float ghostAverageWhite = calcLuminance(texture(inputGhost, vec2(0.5), uMipLevel).rgb); + if (ghostAverageWhite < texture(inputSceneAverageWhite,vec2(0.0)).r / 2.f) + { + outputColor = vec4(vec3(0.f), 1.0f); + return; + } + vec3 haloColor = vec3(0.f); vec2 ghostTexcoord = vec2(1.0) - TexCoord; vec2 haloVec = normalize((vec2(0.5) - ghostTexcoord) * uGhostDispersal) * uHaloWidth; float weight = length(vec2(0.5) - fract(ghostTexcoord + haloVec)) / length(vec2(0.5)); - weight = pow(1.0 - weight, 80.f); + weight = pow(1.0 - weight, gWeight); haloColor = texture(inputColor, ghostTexcoord + haloVec).rgb * weight; - if (calcLuminance(texture(inputGhost, vec2(0.5), 4).rgb) < 5) - { - outputColor = vec4(vec3(0.f), 1.0f); - return; - } outputColor = vec4(haloColor, 1.0f); } \ No newline at end of file diff --git a/src/Platform/Window/Window.cpp b/src/Platform/Window/Window.cpp index 3c774ffd..e9159450 100644 --- a/src/Platform/Window/Window.cpp +++ b/src/Platform/Window/Window.cpp @@ -303,7 +303,7 @@ namespace MxEngine } // window events SwitchContext(); - glfwSwapInterval(0); + glfwSwapInterval(1); glfwSetWindowUserPointer(this->window, this); glfwSetKeyCallback(this->window, [](GLFWwindow* w, int key, int scancode, int action, int mods) { From b7451abaea7f7cd3ec3bcfaddd277ce4bd0cbfef Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Sat, 24 Jun 2023 01:12:50 +0800 Subject: [PATCH 18/26] update average white no matter tone mapping is enabled. --- src/Core/Rendering/RenderController.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index c0b97d65..6917ab05 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -341,13 +341,16 @@ namespace MxEngine TextureHandle RenderController::ComputeAverageWhite(CameraUnit& camera) { MAKE_SCOPE_PROFILER("RenderController::ComputeAverageWhite()"); - MX_ASSERT(camera.ToneMapping != nullptr); camera.HDRTexture->GenerateMipmaps(); float dt = this->Pipeline.Environment.TimeDelta; - float fadingAdaptationSpeed = 1.0f - std::exp(-camera.ToneMapping->GetEyeAdaptationSpeed() * dt); - float adaptationThreshold = camera.ToneMapping->GetEyeAdaptationThreshold(); - + float fadingAdaptationSpeed = 0.0f; + float adaptationThreshold = 0.0f; + if (camera.ToneMapping != nullptr) + { + fadingAdaptationSpeed = 1.0f - std::exp(-camera.ToneMapping->GetEyeAdaptationSpeed() * dt); + adaptationThreshold = camera.ToneMapping->GetEyeAdaptationThreshold(); + } auto& shader = this->Pipeline.Environment.Shaders["AverageWhite"_id]; auto& output = this->Pipeline.Environment.AverageWhiteTexture; shader->Bind(); @@ -763,8 +766,6 @@ namespace MxEngine { if (camera.LensFlare == nullptr) return; - if (camera.ToneMapping == nullptr)//lens flare requires tone mapping - return; MAKE_SCOPE_PROFILER("RenderController::ApplyLensFlare()"); @@ -825,11 +826,12 @@ namespace MxEngine void RenderController::ApplyHDRToLDRConversion(CameraUnit& camera, TextureHandle& input, TextureHandle& output) { + auto& HDRToLDRShader = this->Pipeline.Environment.Shaders["HDRToLDR"_id]; + auto averageWhite = this->ComputeAverageWhite(camera); + if (camera.ToneMapping == nullptr) return; MAKE_SCOPE_PROFILER("RenderController::ApplyHDRToLDRConversion()"); - auto& HDRToLDRShader = this->Pipeline.Environment.Shaders["HDRToLDR"_id]; - auto averageWhite = this->ComputeAverageWhite(camera); auto aces = camera.ToneMapping->GetACESCoefficients(); HDRToLDRShader->Bind(); From 35ae9936c0bb1c89c5cc3810c49675338f9d81de Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Sat, 1 Jul 2023 00:19:55 +0800 Subject: [PATCH 19/26] revert swap interval --- src/Platform/Window/Window.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Platform/Window/Window.cpp b/src/Platform/Window/Window.cpp index e9159450..3c774ffd 100644 --- a/src/Platform/Window/Window.cpp +++ b/src/Platform/Window/Window.cpp @@ -303,7 +303,7 @@ namespace MxEngine } // window events SwitchContext(); - glfwSwapInterval(1); + glfwSwapInterval(0); glfwSetWindowUserPointer(this->window, this); glfwSetKeyCallback(this->window, [](GLFWwindow* w, int key, int scancode, int action, int mods) { From 58facab98d50032e9867cbac8389e22e39c75e56 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Sat, 1 Jul 2023 00:28:52 +0800 Subject: [PATCH 20/26] move average white --- src/Core/Rendering/RenderController.cpp | 8 ++++---- src/Core/Rendering/RenderController.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index 6917ab05..e7c2e47e 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -408,7 +408,9 @@ namespace MxEngine this->ApplyFogEffect(camera, camera.HDRTexture, camera.SwapTexture1); this->ApplyDepthOfFieldEffect(camera, camera.HDRTexture, camera.SwapTexture1, camera.SwapTexture2); - this->ApplyHDRToLDRConversion(camera, camera.HDRTexture, camera.SwapTexture1); + + auto averageWhite = this->ComputeAverageWhite(camera); + this->ApplyHDRToLDRConversion(camera, camera.HDRTexture, camera.SwapTexture1, averageWhite); this->ApplyFXAA(camera, camera.HDRTexture, camera.SwapTexture1); this->ApplyColorGrading(camera, camera.HDRTexture, camera.SwapTexture1); @@ -824,11 +826,9 @@ namespace MxEngine std::swap(input, temporary1); } - void RenderController::ApplyHDRToLDRConversion(CameraUnit& camera, TextureHandle& input, TextureHandle& output) + void RenderController::ApplyHDRToLDRConversion(CameraUnit& camera, TextureHandle& input, TextureHandle& output, TextureHandle& averageWhite) { auto& HDRToLDRShader = this->Pipeline.Environment.Shaders["HDRToLDR"_id]; - auto averageWhite = this->ComputeAverageWhite(camera); - if (camera.ToneMapping == nullptr) return; MAKE_SCOPE_PROFILER("RenderController::ApplyHDRToLDRConversion()"); diff --git a/src/Core/Rendering/RenderController.h b/src/Core/Rendering/RenderController.h index e3569b83..36041812 100644 --- a/src/Core/Rendering/RenderController.h +++ b/src/Core/Rendering/RenderController.h @@ -85,7 +85,7 @@ namespace MxEngine void ApplySSAO(CameraUnit& camera, TextureHandle& input, TextureHandle& temporary, TextureHandle& output); void ApplySSR(CameraUnit& camera, TextureHandle& input, TextureHandle& temporary, TextureHandle& output); void ApplySSGI(CameraUnit& camera, TextureHandle& input, TextureHandle& temporary, TextureHandle& output); - void ApplyHDRToLDRConversion(CameraUnit& camera, TextureHandle& input, TextureHandle& output); + void ApplyHDRToLDRConversion(CameraUnit& camera, TextureHandle& input, TextureHandle& output, TextureHandle& averageWhite); void ApplyFXAA(CameraUnit& camera, TextureHandle& input, TextureHandle& output); void ApplyVignette(CameraUnit& camera, TextureHandle& input, TextureHandle& output); void ApplyColorGrading(CameraUnit& camera, TextureHandle& input, TextureHandle& output); From d591b6d2f6ef76e6e3a0fb83a2f0b81a9c57b066 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Sat, 1 Jul 2023 01:02:34 +0800 Subject: [PATCH 21/26] decompose eye adaptation and average white --- src/Core/Rendering/RenderController.cpp | 25 ++++++++++++++----------- src/Core/Rendering/RenderController.h | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index e7c2e47e..690257fb 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -338,19 +338,11 @@ namespace MxEngine std::swap(input, output); } - TextureHandle RenderController::ComputeAverageWhite(CameraUnit& camera) + TextureHandle RenderController::ComputeAverageWhite(CameraUnit& camera,float fadingAdaptationSpeed,float adaptationThreshold ) { MAKE_SCOPE_PROFILER("RenderController::ComputeAverageWhite()"); camera.HDRTexture->GenerateMipmaps(); - float dt = this->Pipeline.Environment.TimeDelta; - float fadingAdaptationSpeed = 0.0f; - float adaptationThreshold = 0.0f; - if (camera.ToneMapping != nullptr) - { - fadingAdaptationSpeed = 1.0f - std::exp(-camera.ToneMapping->GetEyeAdaptationSpeed() * dt); - adaptationThreshold = camera.ToneMapping->GetEyeAdaptationThreshold(); - } auto& shader = this->Pipeline.Environment.Shaders["AverageWhite"_id]; auto& output = this->Pipeline.Environment.AverageWhiteTexture; shader->Bind(); @@ -409,7 +401,18 @@ namespace MxEngine this->ApplyDepthOfFieldEffect(camera, camera.HDRTexture, camera.SwapTexture1, camera.SwapTexture2); - auto averageWhite = this->ComputeAverageWhite(camera); + TextureHandle averageWhite; + if (camera.ToneMapping == nullptr) + { + averageWhite = this->ComputeAverageWhite(camera); + } + else + { + float dt = this->Pipeline.Environment.TimeDelta; + float fadingAdaptationSpeed = 1.0f - std::exp(-camera.ToneMapping->GetEyeAdaptationSpeed() * dt); + float adaptationThreshold = camera.ToneMapping->GetEyeAdaptationThreshold(); + averageWhite = this->ComputeAverageWhite(camera, fadingAdaptationSpeed, adaptationThreshold); + } this->ApplyHDRToLDRConversion(camera, camera.HDRTexture, camera.SwapTexture1, averageWhite); this->ApplyFXAA(camera, camera.HDRTexture, camera.SwapTexture1); @@ -828,12 +831,12 @@ namespace MxEngine void RenderController::ApplyHDRToLDRConversion(CameraUnit& camera, TextureHandle& input, TextureHandle& output, TextureHandle& averageWhite) { - auto& HDRToLDRShader = this->Pipeline.Environment.Shaders["HDRToLDR"_id]; if (camera.ToneMapping == nullptr) return; MAKE_SCOPE_PROFILER("RenderController::ApplyHDRToLDRConversion()"); auto aces = camera.ToneMapping->GetACESCoefficients(); + auto& HDRToLDRShader = this->Pipeline.Environment.Shaders["HDRToLDR"_id]; HDRToLDRShader->Bind(); input->Bind(0); averageWhite->Bind(1); diff --git a/src/Core/Rendering/RenderController.h b/src/Core/Rendering/RenderController.h index 36041812..c170183a 100644 --- a/src/Core/Rendering/RenderController.h +++ b/src/Core/Rendering/RenderController.h @@ -75,7 +75,7 @@ namespace MxEngine void DrawDebugBuffer(const CameraUnit& camera); void DrawObject(const RenderUnit& unit, size_t instanceCount, size_t baseInstance, const Shader& shader); void ComputeBloomEffect(CameraUnit& camera, const TextureHandle& output); - TextureHandle ComputeAverageWhite(CameraUnit& camera); + TextureHandle ComputeAverageWhite(CameraUnit& camera, float fadingAdaptationSpeed = 0.f, float adaptationThreshold = 0.f); void PerformPostProcessing(CameraUnit& camera); void PerformLightPass(CameraUnit& camera); void DrawTransparentObjects(CameraUnit& camera); From 0f4500ea2884d52df36a1d782b11878a222b7b23 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Mon, 25 Mar 2024 17:33:55 +0800 Subject: [PATCH 22/26] temp --- samples/Sponza/sponza.json | 428 +++++++++--------- src/Core/Rendering/RenderController.cpp | 6 +- src/Platform/OpenGL/Shaders/lens_flare.glsl | 9 +- .../OpenGL/Shaders/lens_flare_ghosts.glsl | 13 +- .../OpenGL/Shaders/lens_flare_halo.glsl | 14 +- 5 files changed, 241 insertions(+), 229 deletions(-) diff --git a/samples/Sponza/sponza.json b/samples/Sponza/sponza.json index 04c90a0c..01a8cf39 100644 --- a/samples/Sponza/sponza.json +++ b/samples/Sponza/sponza.json @@ -12,7 +12,7 @@ ], "Material": [ { - "albedo map": 101, + "albedo map": 102, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -64,7 +64,7 @@ ] }, { - "albedo map": 13, + "albedo map": 14, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -78,11 +78,11 @@ "height map": 18446744073709551615, "id": 2, "metallic factor": 1.0, - "metallic map": 11, + "metallic map": 12, "name": "DefaultMaterial", - "normal map": 23, + "normal map": 24, "roughness factor": 1.0, - "roughness map": 16, + "roughness map": 17, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -90,7 +90,7 @@ ] }, { - "albedo map": 20, + "albedo map": 21, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -106,7 +106,7 @@ "metallic factor": 1.0, "metallic map": 18446744073709551615, "name": "Lion Head", - "normal map": 26, + "normal map": 27, "roughness factor": 0.0, "roughness map": 18446744073709551615, "transparency": 1.0, @@ -116,7 +116,7 @@ ] }, { - "albedo map": 22, + "albedo map": 23, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -130,11 +130,11 @@ "height map": 18446744073709551615, "id": 4, "metallic factor": 1.0, - "metallic map": 17, + "metallic map": 18, "name": "DefaultMaterial", - "normal map": 30, + "normal map": 31, "roughness factor": 1.0, - "roughness map": 12, + "roughness map": 13, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -142,7 +142,7 @@ ] }, { - "albedo map": 25, + "albedo map": 26, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -156,11 +156,11 @@ "height map": 18446744073709551615, "id": 5, "metallic factor": 1.0, - "metallic map": 14, + "metallic map": 15, "name": "DefaultMaterial", - "normal map": 33, + "normal map": 34, "roughness factor": 1.0, - "roughness map": 15, + "roughness map": 16, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -168,7 +168,7 @@ ] }, { - "albedo map": 29, + "albedo map": 30, "alpha mode": "MASKED", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -182,11 +182,11 @@ "height map": 18446744073709551615, "id": 6, "metallic factor": 1.0, - "metallic map": 18, + "metallic map": 19, "name": "DefaultMaterial", - "normal map": 28, + "normal map": 29, "roughness factor": 1.0, - "roughness map": 19, + "roughness map": 20, "transparency": 0.5, "uv multipliers": [ 1.0, @@ -194,7 +194,7 @@ ] }, { - "albedo map": 31, + "albedo map": 32, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -208,11 +208,11 @@ "height map": 18446744073709551615, "id": 7, "metallic factor": 1.0, - "metallic map": 27, + "metallic map": 28, "name": "DefaultMaterial", - "normal map": 24, + "normal map": 25, "roughness factor": 1.0, - "roughness map": 21, + "roughness map": 22, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -220,7 +220,7 @@ ] }, { - "albedo map": 40, + "albedo map": 41, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -234,11 +234,11 @@ "height map": 18446744073709551615, "id": 8, "metallic factor": 1.0, - "metallic map": 35, + "metallic map": 36, "name": "DefaultMaterial", - "normal map": 34, + "normal map": 35, "roughness factor": 1.0, - "roughness map": 37, + "roughness map": 38, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -246,7 +246,7 @@ ] }, { - "albedo map": 43, + "albedo map": 44, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -260,11 +260,11 @@ "height map": 18446744073709551615, "id": 9, "metallic factor": 1.0, - "metallic map": 39, + "metallic map": 40, "name": "DefaultMaterial", - "normal map": 38, + "normal map": 39, "roughness factor": 1.0, - "roughness map": 32, + "roughness map": 33, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -272,7 +272,7 @@ ] }, { - "albedo map": 44, + "albedo map": 45, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -286,11 +286,11 @@ "height map": 18446744073709551615, "id": 10, "metallic factor": 1.0, - "metallic map": 42, + "metallic map": 43, "name": "DefaultMaterial", - "normal map": 41, + "normal map": 42, "roughness factor": 1.0, - "roughness map": 36, + "roughness map": 37, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -298,7 +298,7 @@ ] }, { - "albedo map": 45, + "albedo map": 46, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -312,11 +312,11 @@ "height map": 18446744073709551615, "id": 11, "metallic factor": 1.0, - "metallic map": 47, + "metallic map": 48, "name": "DefaultMaterial", - "normal map": 46, + "normal map": 47, "roughness factor": 1.0, - "roughness map": 48, + "roughness map": 49, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -324,7 +324,7 @@ ] }, { - "albedo map": 49, + "albedo map": 50, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -338,11 +338,11 @@ "height map": 18446744073709551615, "id": 12, "metallic factor": 1.0, - "metallic map": 51, + "metallic map": 52, "name": "DefaultMaterial", - "normal map": 50, + "normal map": 51, "roughness factor": 1.0, - "roughness map": 52, + "roughness map": 53, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -350,7 +350,7 @@ ] }, { - "albedo map": 53, + "albedo map": 54, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -364,11 +364,11 @@ "height map": 18446744073709551615, "id": 13, "metallic factor": 1.0, - "metallic map": 55, + "metallic map": 56, "name": "DefaultMaterial", - "normal map": 54, + "normal map": 55, "roughness factor": 1.0, - "roughness map": 56, + "roughness map": 57, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -376,7 +376,7 @@ ] }, { - "albedo map": 60, + "albedo map": 61, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -402,7 +402,7 @@ ] }, { - "albedo map": 57, + "albedo map": 58, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -416,11 +416,11 @@ "height map": 18446744073709551615, "id": 15, "metallic factor": 1.0, - "metallic map": 69, + "metallic map": 70, "name": "DefaultMaterial", - "normal map": 58, + "normal map": 59, "roughness factor": 1.0, - "roughness map": 59, + "roughness map": 60, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -428,7 +428,7 @@ ] }, { - "albedo map": 62, + "albedo map": 63, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -442,11 +442,11 @@ "height map": 18446744073709551615, "id": 16, "metallic factor": 1.0, - "metallic map": 74, + "metallic map": 75, "name": "DefaultMaterial", - "normal map": 63, + "normal map": 64, "roughness factor": 1.0, - "roughness map": 64, + "roughness map": 65, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -454,7 +454,7 @@ ] }, { - "albedo map": 66, + "albedo map": 67, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -468,11 +468,11 @@ "height map": 18446744073709551615, "id": 17, "metallic factor": 1.0, - "metallic map": 78, + "metallic map": 79, "name": "DefaultMaterial", - "normal map": 67, + "normal map": 68, "roughness factor": 1.0, - "roughness map": 61, + "roughness map": 62, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -480,7 +480,7 @@ ] }, { - "albedo map": 73, + "albedo map": 74, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -496,9 +496,9 @@ "metallic factor": 0.25, "metallic map": 18446744073709551615, "name": "Floor", - "normal map": 81, + "normal map": 82, "roughness factor": 1.0, - "roughness map": 65, + "roughness map": 66, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -506,7 +506,7 @@ ] }, { - "albedo map": 77, + "albedo map": 78, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -520,11 +520,11 @@ "height map": 18446744073709551615, "id": 19, "metallic factor": 1.0, - "metallic map": 70, + "metallic map": 71, "name": "DefaultMaterial", - "normal map": 79, + "normal map": 80, "roughness factor": 1.0, - "roughness map": 71, + "roughness map": 72, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -532,7 +532,7 @@ ] }, { - "albedo map": 82, + "albedo map": 83, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -546,11 +546,11 @@ "height map": 18446744073709551615, "id": 20, "metallic factor": 1.0, - "metallic map": 75, + "metallic map": 76, "name": "DefaultMaterial", - "normal map": 76, + "normal map": 77, "roughness factor": 1.0, - "roughness map": 68, + "roughness map": 69, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -558,7 +558,7 @@ ] }, { - "albedo map": 94, + "albedo map": 95, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -572,11 +572,11 @@ "height map": 18446744073709551615, "id": 21, "metallic factor": 1.0, - "metallic map": 72, + "metallic map": 73, "name": "DefaultMaterial", - "normal map": 86, + "normal map": 87, "roughness factor": 1.0, - "roughness map": 80, + "roughness map": 81, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -584,7 +584,7 @@ ] }, { - "albedo map": 89, + "albedo map": 90, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -598,11 +598,11 @@ "height map": 18446744073709551615, "id": 22, "metallic factor": 1.0, - "metallic map": 84, + "metallic map": 85, "name": "DefaultMaterial", - "normal map": 10, + "normal map": 11, "roughness factor": 1.0, - "roughness map": 85, + "roughness map": 86, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -610,7 +610,7 @@ ] }, { - "albedo map": 93, + "albedo map": 94, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -626,7 +626,7 @@ "metallic factor": 1.0, "metallic map": 18446744073709551615, "name": "Lion Head", - "normal map": 9, + "normal map": 10, "roughness factor": 0.0, "roughness map": 18446744073709551615, "transparency": 1.0, @@ -636,7 +636,7 @@ ] }, { - "albedo map": 97, + "albedo map": 98, "alpha mode": "OPAQUE", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -650,11 +650,11 @@ "height map": 18446744073709551615, "id": 24, "metallic factor": 1.0, - "metallic map": 90, + "metallic map": 91, "name": "DefaultMaterial", - "normal map": 99, + "normal map": 100, "roughness factor": 1.0, - "roughness map": 83, + "roughness map": 84, "transparency": 1.0, "uv multipliers": [ 1.0, @@ -662,7 +662,7 @@ ] }, { - "albedo map": 98, + "albedo map": 99, "alpha mode": "MASKED", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -676,11 +676,11 @@ "height map": 18446744073709551615, "id": 25, "metallic factor": 1.0, - "metallic map": 92, + "metallic map": 93, "name": "DefaultMaterial", - "normal map": 91, + "normal map": 92, "roughness factor": 1.0, - "roughness map": 100, + "roughness map": 101, "transparency": 0.5, "uv multipliers": [ 1.0, @@ -688,7 +688,7 @@ ] }, { - "albedo map": 95, + "albedo map": 96, "alpha mode": "MASKED", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -702,11 +702,11 @@ "height map": 18446744073709551615, "id": 26, "metallic factor": 1.0, - "metallic map": 88, + "metallic map": 89, "name": "DefaultMaterial", - "normal map": 87, + "normal map": 88, "roughness factor": 1.0, - "roughness map": 96, + "roughness map": 97, "transparency": 0.5, "uv multipliers": [ 1.0, @@ -714,7 +714,7 @@ ] }, { - "albedo map": 102, + "albedo map": 103, "alpha mode": "TRANSPARENT", "ambient occlusion map": 18446744073709551615, "base color": [ @@ -904,7 +904,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/13982482287905699490.jpg" }, - "id": 9, + "id": 10, "wrap type": "REPEAT" }, { @@ -918,7 +918,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/16299174074766089871.jpg" }, - "id": 10, + "id": 11, "wrap type": "REPEAT" }, { @@ -932,7 +932,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_24.png" }, - "id": 11, + "id": 12, "wrap type": "REPEAT" }, { @@ -946,7 +946,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_22.png" }, - "id": 12, + "id": 13, "wrap type": "REPEAT" }, { @@ -960,7 +960,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/2969916736137545357.jpg" }, - "id": 13, + "id": 14, "wrap type": "REPEAT" }, { @@ -974,7 +974,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_21.png" }, - "id": 14, + "id": 15, "wrap type": "REPEAT" }, { @@ -988,7 +988,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_21.png" }, - "id": 15, + "id": 16, "wrap type": "REPEAT" }, { @@ -1002,7 +1002,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_24.png" }, - "id": 16, + "id": 17, "wrap type": "REPEAT" }, { @@ -1016,7 +1016,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_22.png" }, - "id": 17, + "id": 18, "wrap type": "REPEAT" }, { @@ -1030,7 +1030,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_20.png" }, - "id": 18, + "id": 19, "wrap type": "REPEAT" }, { @@ -1044,7 +1044,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_20.png" }, - "id": 19, + "id": 20, "wrap type": "REPEAT" }, { @@ -1058,7 +1058,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/6772804448157695701.jpg" }, - "id": 20, + "id": 21, "wrap type": "REPEAT" }, { @@ -1072,7 +1072,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_19.png" }, - "id": 21, + "id": 22, "wrap type": "REPEAT" }, { @@ -1086,7 +1086,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/8481240838833932244.jpg" }, - "id": 22, + "id": 23, "wrap type": "REPEAT" }, { @@ -1100,7 +1100,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/14118779221266351425.jpg" }, - "id": 23, + "id": 24, "wrap type": "REPEAT" }, { @@ -1114,7 +1114,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/4910669866631290573.jpg" }, - "id": 24, + "id": 25, "wrap type": "REPEAT" }, { @@ -1128,7 +1128,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/755318871556304029.jpg" }, - "id": 25, + "id": 26, "wrap type": "REPEAT" }, { @@ -1142,7 +1142,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/759203620573749278.jpg" }, - "id": 26, + "id": 27, "wrap type": "REPEAT" }, { @@ -1156,7 +1156,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_19.png" }, - "id": 27, + "id": 28, "wrap type": "REPEAT" }, { @@ -1170,7 +1170,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/14170708867020035030.jpg" }, - "id": 28, + "id": 29, "wrap type": "REPEAT" }, { @@ -1184,7 +1184,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/16275776544635328252.png" }, - "id": 29, + "id": 30, "wrap type": "REPEAT" }, { @@ -1198,7 +1198,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/10381718147657362067.jpg" }, - "id": 30, + "id": 31, "wrap type": "REPEAT" }, { @@ -1212,7 +1212,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/9288698199695299068.jpg" }, - "id": 31, + "id": 32, "wrap type": "REPEAT" }, { @@ -1226,7 +1226,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_17.png" }, - "id": 32, + "id": 33, "wrap type": "REPEAT" }, { @@ -1240,7 +1240,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/3827035219084910048.jpg" }, - "id": 33, + "id": 34, "wrap type": "REPEAT" }, { @@ -1254,7 +1254,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/4601176305987539675.jpg" }, - "id": 34, + "id": 35, "wrap type": "REPEAT" }, { @@ -1268,7 +1268,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_18.png" }, - "id": 35, + "id": 36, "wrap type": "REPEAT" }, { @@ -1282,7 +1282,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_16.png" }, - "id": 36, + "id": 37, "wrap type": "REPEAT" }, { @@ -1296,7 +1296,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_18.png" }, - "id": 37, + "id": 38, "wrap type": "REPEAT" }, { @@ -1310,7 +1310,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/6593109234861095314.jpg" }, - "id": 38, + "id": 39, "wrap type": "REPEAT" }, { @@ -1324,7 +1324,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_17.png" }, - "id": 39, + "id": 40, "wrap type": "REPEAT" }, { @@ -1338,7 +1338,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/11474523244911310074.jpg" }, - "id": 40, + "id": 41, "wrap type": "REPEAT" }, { @@ -1352,7 +1352,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/332936164838540657.jpg" }, - "id": 41, + "id": 42, "wrap type": "REPEAT" }, { @@ -1366,7 +1366,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_16.png" }, - "id": 42, + "id": 43, "wrap type": "REPEAT" }, { @@ -1380,7 +1380,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/17876391417123941155.jpg" }, - "id": 43, + "id": 44, "wrap type": "REPEAT" }, { @@ -1394,7 +1394,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/2185409758123873465.jpg" }, - "id": 44, + "id": 45, "wrap type": "REPEAT" }, { @@ -1408,7 +1408,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/2775690330959970771.jpg" }, - "id": 45, + "id": 46, "wrap type": "REPEAT" }, { @@ -1422,7 +1422,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/2374361008830720677.jpg" }, - "id": 46, + "id": 47, "wrap type": "REPEAT" }, { @@ -1436,7 +1436,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_15.png" }, - "id": 47, + "id": 48, "wrap type": "REPEAT" }, { @@ -1450,7 +1450,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_15.png" }, - "id": 48, + "id": 49, "wrap type": "REPEAT" }, { @@ -1464,7 +1464,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/4675343432951571524.jpg" }, - "id": 49, + "id": 50, "wrap type": "REPEAT" }, { @@ -1478,7 +1478,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/7056944414013900257.jpg" }, - "id": 50, + "id": 51, "wrap type": "REPEAT" }, { @@ -1492,7 +1492,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_14.png" }, - "id": 51, + "id": 52, "wrap type": "REPEAT" }, { @@ -1506,7 +1506,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_14.png" }, - "id": 52, + "id": 53, "wrap type": "REPEAT" }, { @@ -1520,7 +1520,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/4975155472559461469.jpg" }, - "id": 53, + "id": 54, "wrap type": "REPEAT" }, { @@ -1534,7 +1534,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/2299742237651021498.jpg" }, - "id": 54, + "id": 55, "wrap type": "REPEAT" }, { @@ -1548,7 +1548,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_13.png" }, - "id": 55, + "id": 56, "wrap type": "REPEAT" }, { @@ -1562,7 +1562,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_13.png" }, - "id": 56, + "id": 57, "wrap type": "REPEAT" }, { @@ -1576,7 +1576,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/6151467286084645207.jpg" }, - "id": 57, + "id": 58, "wrap type": "REPEAT" }, { @@ -1590,7 +1590,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/7645212358685992005.jpg" }, - "id": 58, + "id": 59, "wrap type": "REPEAT" }, { @@ -1604,7 +1604,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_11.png" }, - "id": 59, + "id": 60, "wrap type": "REPEAT" }, { @@ -1618,7 +1618,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/white.png" }, - "id": 60, + "id": 61, "wrap type": "REPEAT" }, { @@ -1632,7 +1632,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_9.png" }, - "id": 61, + "id": 62, "wrap type": "REPEAT" }, { @@ -1646,7 +1646,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/11490520546946913238.jpg" }, - "id": 62, + "id": 63, "wrap type": "REPEAT" }, { @@ -1660,7 +1660,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/3628158980083700836.jpg" }, - "id": 63, + "id": 64, "wrap type": "REPEAT" }, { @@ -1674,7 +1674,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_10.png" }, - "id": 64, + "id": 65, "wrap type": "REPEAT" }, { @@ -1688,7 +1688,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_8.png" }, - "id": 65, + "id": 66, "wrap type": "REPEAT" }, { @@ -1702,7 +1702,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/7441062115984513793.jpg" }, - "id": 66, + "id": 67, "wrap type": "REPEAT" }, { @@ -1716,7 +1716,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/6667038893015345571.jpg" }, - "id": 67, + "id": 68, "wrap type": "REPEAT" }, { @@ -1730,7 +1730,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_6.png" }, - "id": 68, + "id": 69, "wrap type": "REPEAT" }, { @@ -1744,7 +1744,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_11.png" }, - "id": 69, + "id": 70, "wrap type": "REPEAT" }, { @@ -1758,7 +1758,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_7.png" }, - "id": 70, + "id": 71, "wrap type": "REPEAT" }, { @@ -1772,7 +1772,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_7.png" }, - "id": 71, + "id": 72, "wrap type": "REPEAT" }, { @@ -1786,7 +1786,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_5.png" }, - "id": 72, + "id": 73, "wrap type": "REPEAT" }, { @@ -1800,7 +1800,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/5823059166183034438.jpg" }, - "id": 73, + "id": 74, "wrap type": "REPEAT" }, { @@ -1814,7 +1814,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_10.png" }, - "id": 74, + "id": 75, "wrap type": "REPEAT" }, { @@ -1828,7 +1828,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_6.png" }, - "id": 75, + "id": 76, "wrap type": "REPEAT" }, { @@ -1842,7 +1842,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/10388182081421875623.jpg" }, - "id": 76, + "id": 77, "wrap type": "REPEAT" }, { @@ -1856,7 +1856,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/6047387724914829168.jpg" }, - "id": 77, + "id": 78, "wrap type": "REPEAT" }, { @@ -1870,7 +1870,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_9.png" }, - "id": 78, + "id": 79, "wrap type": "REPEAT" }, { @@ -1884,7 +1884,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/15722799267630235092.jpg" }, - "id": 79, + "id": 80, "wrap type": "REPEAT" }, { @@ -1898,7 +1898,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_5.png" }, - "id": 80, + "id": 81, "wrap type": "REPEAT" }, { @@ -1912,7 +1912,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/14267839433702832875.jpg" }, - "id": 81, + "id": 82, "wrap type": "REPEAT" }, { @@ -1926,7 +1926,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/15295713303328085182.jpg" }, - "id": 82, + "id": 83, "wrap type": "REPEAT" }, { @@ -1940,7 +1940,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_2.png" }, - "id": 83, + "id": 84, "wrap type": "REPEAT" }, { @@ -1954,7 +1954,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_4.png" }, - "id": 84, + "id": 85, "wrap type": "REPEAT" }, { @@ -1968,7 +1968,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_4.png" }, - "id": 85, + "id": 86, "wrap type": "REPEAT" }, { @@ -1982,7 +1982,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/2051777328469649772.jpg" }, - "id": 86, + "id": 87, "wrap type": "REPEAT" }, { @@ -1996,7 +1996,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/8773302468495022225.jpg" }, - "id": 87, + "id": 88, "wrap type": "REPEAT" }, { @@ -2010,7 +2010,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_0.png" }, - "id": 88, + "id": 89, "wrap type": "REPEAT" }, { @@ -2024,7 +2024,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/5792855332885324923.jpg" }, - "id": 89, + "id": 90, "wrap type": "REPEAT" }, { @@ -2038,7 +2038,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_2.png" }, - "id": 90, + "id": 91, "wrap type": "REPEAT" }, { @@ -2052,7 +2052,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/12501374198249454378.jpg" }, - "id": 91, + "id": 92, "wrap type": "REPEAT" }, { @@ -2066,7 +2066,7 @@ "format": "R", "path": "Resources/Sponza/glTF/metallic_1.png" }, - "id": 92, + "id": 93, "wrap type": "REPEAT" }, { @@ -2080,7 +2080,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/8750083169368950601.jpg" }, - "id": 93, + "id": 94, "wrap type": "REPEAT" }, { @@ -2094,7 +2094,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/14650633544276105767.jpg" }, - "id": 94, + "id": 95, "wrap type": "REPEAT" }, { @@ -2108,7 +2108,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/5061699253647017043.png" }, - "id": 95, + "id": 96, "wrap type": "REPEAT" }, { @@ -2122,7 +2122,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_0.png" }, - "id": 96, + "id": 97, "wrap type": "REPEAT" }, { @@ -2136,7 +2136,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/7268504077753552595.jpg" }, - "id": 97, + "id": 98, "wrap type": "REPEAT" }, { @@ -2150,7 +2150,7 @@ "format": "RGBA", "path": "Resources/Sponza/glTF/8006627369776289000.png" }, - "id": 98, + "id": 99, "wrap type": "REPEAT" }, { @@ -2164,7 +2164,7 @@ "format": "RG", "path": "Resources/Sponza/glTF/4477655471536070370.jpg" }, - "id": 99, + "id": 100, "wrap type": "REPEAT" }, { @@ -2178,7 +2178,7 @@ "format": "R", "path": "Resources/Sponza/glTF/roughness_1.png" }, - "id": 100, + "id": 101, "wrap type": "REPEAT" }, { @@ -2192,7 +2192,7 @@ "format": "RGB", "path": "Resources/moon.jpg" }, - "id": 101, + "id": 102, "wrap type": "REPEAT" }, { @@ -2206,7 +2206,7 @@ "format": "RGBA", "path": "Resources/fire.png" }, - "id": 102, + "id": 103, "wrap type": "REPEAT" } ], @@ -2220,7 +2220,7 @@ "paused": false, "physics-step": 1.0, "time-scale": 1.0, - "total-time": 3214.124267578125, + "total-time": 3242.16943359375, "viewport": 0 }, "mxobjects": [ @@ -2228,14 +2228,14 @@ "CameraController": { "camera type": "PERSPECTIVE", "direction": [ - 0.9956035614013672, - -0.0917634665966034, - 0.01878945901989937 + 0.9829415082931519, + -0.06535527855157852, + 0.17191438376903534 ], "forward vector": [ - 0.9998219609260559, + 0.9850475192070007, 0.0, - 0.01886907033622265 + 0.17228271067142487 ], "id": 0, "is listening window resize event": true, @@ -2243,12 +2243,13 @@ "perspective camera": { "aspect ratio": 1.7777777910232544, "fov": 65.0, + "zfar": 100000.0, "znear": 0.10000000149011612 }, "right vector": [ - -0.01886911503970623, + -0.17228275537490845, 0.0, - 0.9998219609260559 + 0.9850475192070007 ], "up vector": [ 0.0, @@ -2262,6 +2263,9 @@ "chromatic aberration distortion": 0.800000011920929, "chromatic aberration intensity": 0.07999999821186066, "chromatic aberration min distance": 0.800000011920929, + "dof bokeh radius": 10.0, + "dof focus distance": 0.0, + "dof focus range": 5.0, "fog color": [ 0.5, 0.6000000238418579, @@ -2273,6 +2277,14 @@ "id": 0, "vignette radius": 0.10000000149011612 }, + "CameraLensFlare": { + "id": 0, + "lens falre halo width": 0.5, + "lens flare bias": -42.0, + "lens flare ghost dispersal": 0.12999999523162842, + "lens flare num of ghosts": 12, + "lens flare scale": 1.0 + }, "CameraSSAO": { "blur iterations": 1, "blur lod": 2, @@ -2360,13 +2372,14 @@ "down key binding": "LEFT_SHIFT", "forward key binding": "W", "id": 0, - "move speed": 10.0, - "rotate speed": 10.0, "is horizontal rotation bound": true, "is vertical rotation bound": true, "left key binding": "A", + "move speed": 10.0, "right key binding": "D", - "up key binding": "SPACE" + "rotate speed": 10.0, + "up key binding": "SPACE", + "use camera style movement": true }, "RigidBody": { "angular air resistance": 0.0, @@ -2403,9 +2416,9 @@ 1.0 ], "linear velocity": [ - 4.5776359911542386e-05, + 4.897639769296802e-07, 5.7220458984375e-06, - 1.2390954395868903e-07 + -1.4539867265739304e-07 ], "mass": 1.0, "push velocity": [ @@ -2424,6 +2437,9 @@ }, "Script": { "_name": "ShootSpheres", + "database": { + "_database": null + }, "id": 0 }, "Skybox": { @@ -2441,9 +2457,9 @@ "name": "Camera", "transform": { "position": [ - -26.250003814697266, + -19.770307540893555, 2.999999523162842, - 0.007744258269667625 + -0.5053378939628601 ], "rotation": [ 0.0, @@ -2465,6 +2481,7 @@ 0.8377978205680847, 0.75 ], + "depth scale": 20.0, "direction": [ 0.0, 1.0, @@ -2473,7 +2490,6 @@ "id": 0, "intensity": 100.0, "is following viewport": true, - "depth scale": 20.0, "projections": [ 4.0, 15.0, @@ -2484,9 +2500,9 @@ "name": "Global Light", "transform": { "position": [ - -26.250003814697266, + -19.770307540893555, 2.999999523162842, - 0.007744258269667625 + -0.5053378939628601 ], "rotation": [ 0.0, diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index 4956cb83..bc014298 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -359,7 +359,7 @@ namespace MxEngine // generate depth texture mipmaps for post-processing algorithms. Replace later with hierarhical depth map depth->GenerateMipmaps(); } - + TextureHandle RenderController::ComputeAverageWhite(CameraUnit& camera, float fadingAdaptationSpeed, float adaptationThreshold) { MAKE_RENDER_PASS_SCOPE("RenderController::ComputeAverageWhite()"); @@ -810,14 +810,13 @@ namespace MxEngine temporaryQuater1->GenerateMipmaps(); } + int mipLevel = 1 + floor(log2(Max(temporaryQuater1->GetWidth(), temporaryQuater1->GetHeight()))); //calc halo { auto& shaderHalo = this->Pipeline.Environment.Shaders["LensFlareHalo"_id]; shaderHalo->Bind(); shaderHalo->SetUniform("uGhostDispersal", dispersal); shaderHalo->SetUniform("uHaloWidth", haloWidth); - int mipLevel = ceil(Log2(Max(temporaryQuater1->GetWidth(), temporaryQuater1->GetHeight()))) - 2; - //use max mip level - 2 to get centre average brightness since only centre spot is used in ghosts generation shaderHalo->SetUniform("uMipLevel", mipLevel); input->Bind(0); temporaryQuater1->Bind(1); @@ -832,6 +831,7 @@ namespace MxEngine lensFlare->Bind(); lensFlare->SetUniform("uGhosts", numOfGhosts); lensFlare->SetUniform("uGhostDispersal", dispersal); + lensFlare->SetUniform("uMipLevel", mipLevel); temporaryQuater1->Bind(0); temporaryQuater2->Bind(1); input->Bind(2); diff --git a/src/Platform/OpenGL/Shaders/lens_flare.glsl b/src/Platform/OpenGL/Shaders/lens_flare.glsl index bae50fdf..8a406f62 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare.glsl @@ -5,6 +5,8 @@ layout(binding = 2) uniform sampler2D inputColor; uniform int uGhosts; // number of ghost samples uniform float uGhostDispersal; // dispersion factor +uniform int uMipLevel; + in vec2 TexCoord; out vec4 outColor; @@ -18,14 +20,17 @@ void main() vec2 ghostVec = (vec2(0.5) - texcoord) * uGhostDispersal; // sample ghosts + float averageDiff = calcLuminance(texelFetch(inputGhost,ivec2(0,0),uMipLevel).rgb); vec3 result = vec3(0.0); for (int i = 0; i < uGhosts; ++i) { vec2 offset = fract(texcoord + 0.8 * ghostVec * float(i)); float weight = length(vec2(0.5) - offset) / length(vec2(0.5)); weight = pow(1.0 - weight, gWeight); - - result += texture(inputGhost, offset).rgb * weight; + vec3 px = texture(inputGhost, offset).rgb; + float factor = calcLuminance(px) - 50.0;//(averageDiff * 2.0); + factor = max(factor,0.0); + result += factor * weight * px; } //halo diff --git a/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl b/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl index 9a8087dd..bb8fcbda 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl @@ -13,15 +13,10 @@ out vec4 outputColor; void main() { vec3 col = texture(inputColor, TexCoord).rgb; + float sceneLum = calcLuminance(col); + float averageWhite = texture(averageWhiteTex, vec2(0.0f)).r; - float lum = calcLuminance(col) - max(texture(averageWhiteTex, vec2(0.0f)).r, 0.0f); - if (lum <= 0) - { - outputColor = vec4(vec3(0.0f), 1.0f); - return; - } - - lum += uBias; - col = max(lum, 0.0f) * col * uScale; + float lumDiff = sceneLum - averageWhite; + col = max(lumDiff, 0.0f) * col; outputColor = vec4(col, 1.0f); } \ No newline at end of file diff --git a/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl b/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl index a9471a5f..509f70e3 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl @@ -16,18 +16,14 @@ const float gWeight = 80.0f; void main() { float ghostAverageWhite = calcLuminance(texture(inputGhost, vec2(0.5), uMipLevel).rgb); - if (ghostAverageWhite < texture(inputSceneAverageWhite,vec2(0.0)).r / 2.f) - { - outputColor = vec4(vec3(0.f), 1.0f); - return; - } + float sceneAverageWhite = texture(inputSceneAverageWhite,vec2(0.0)).r; + float factor = smoothstep(0.0,1.0,(ghostAverageWhite/sceneAverageWhite)-0.5); - vec3 haloColor = vec3(0.f); vec2 ghostTexcoord = vec2(1.0) - TexCoord; vec2 haloVec = normalize((vec2(0.5) - ghostTexcoord) * uGhostDispersal) * uHaloWidth; - float weight = length(vec2(0.5) - fract(ghostTexcoord + haloVec)) / length(vec2(0.5)); + float weight = length(vec2(0.5) - fract(ghostTexcoord + haloVec)) / length(vec2(0.5)); weight = pow(1.0 - weight, gWeight); - haloColor = texture(inputColor, ghostTexcoord + haloVec).rgb * weight; + vec3 haloColor = texture(inputColor, ghostTexcoord + haloVec).rgb * weight; - outputColor = vec4(haloColor, 1.0f); + outputColor = vec4(haloColor * factor, 1.0f); } \ No newline at end of file From a7d2ef9f113380d402b201ba2d2e6bd11abbb8d0 Mon Sep 17 00:00:00 2001 From: fall2019 <53587145+fall2019@users.noreply.github.com> Date: Mon, 25 Mar 2024 23:19:27 +0800 Subject: [PATCH 23/26] wide range of lum --- src/Platform/OpenGL/Shaders/lens_flare.glsl | 7 ++----- src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl | 10 +++++----- src/Platform/OpenGL/Shaders/lens_flare_halo.glsl | 6 +++--- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Platform/OpenGL/Shaders/lens_flare.glsl b/src/Platform/OpenGL/Shaders/lens_flare.glsl index 8a406f62..361cd9f3 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare.glsl @@ -20,17 +20,14 @@ void main() vec2 ghostVec = (vec2(0.5) - texcoord) * uGhostDispersal; // sample ghosts - float averageDiff = calcLuminance(texelFetch(inputGhost,ivec2(0,0),uMipLevel).rgb); vec3 result = vec3(0.0); for (int i = 0; i < uGhosts; ++i) { vec2 offset = fract(texcoord + 0.8 * ghostVec * float(i)); float weight = length(vec2(0.5) - offset) / length(vec2(0.5)); weight = pow(1.0 - weight, gWeight); - vec3 px = texture(inputGhost, offset).rgb; - float factor = calcLuminance(px) - 50.0;//(averageDiff * 2.0); - factor = max(factor,0.0); - result += factor * weight * px; + vec4 px = texture(inputGhost, offset).rgba; + result += px.rgb * weight; } //halo diff --git a/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl b/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl index bb8fcbda..590ec4c9 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl @@ -13,10 +13,10 @@ out vec4 outputColor; void main() { vec3 col = texture(inputColor, TexCoord).rgb; - float sceneLum = calcLuminance(col); - float averageWhite = texture(averageWhiteTex, vec2(0.0f)).r; + float sceneLum = calcLuminance(col); - float lumDiff = sceneLum - averageWhite; - col = max(lumDiff, 0.0f) * col; - outputColor = vec4(col, 1.0f); + float averageWhite = texture(averageWhiteTex, vec2(0.0f)).r; + float lum = sceneLum / (averageWhite * 10.0); + lum = max(lum - 0.1, 0.0); + outputColor = vec4(lum * col * 0.5, averageWhite); } \ No newline at end of file diff --git a/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl b/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl index 509f70e3..345eb146 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare_halo.glsl @@ -16,12 +16,12 @@ const float gWeight = 80.0f; void main() { float ghostAverageWhite = calcLuminance(texture(inputGhost, vec2(0.5), uMipLevel).rgb); - float sceneAverageWhite = texture(inputSceneAverageWhite,vec2(0.0)).r; - float factor = smoothstep(0.0,1.0,(ghostAverageWhite/sceneAverageWhite)-0.5); + float sceneAverageWhite = texture(inputSceneAverageWhite, vec2(0.0)).r; + float factor = smoothstep(0.0, 1.0, (ghostAverageWhite / sceneAverageWhite * 5.0)); vec2 ghostTexcoord = vec2(1.0) - TexCoord; vec2 haloVec = normalize((vec2(0.5) - ghostTexcoord) * uGhostDispersal) * uHaloWidth; - float weight = length(vec2(0.5) - fract(ghostTexcoord + haloVec)) / length(vec2(0.5)); + float weight = length(vec2(0.5) - fract(ghostTexcoord + haloVec)) / length(vec2(0.5)); weight = pow(1.0 - weight, gWeight); vec3 haloColor = texture(inputColor, ghostTexcoord + haloVec).rgb * weight; From 7daf66e22e16fb4cc3d346e8581738d420b96980 Mon Sep 17 00:00:00 2001 From: MomoDeve Date: Fri, 29 Mar 2024 05:23:52 +0900 Subject: [PATCH 24/26] beautify + remove unused --- .../Components/Camera/CameraLensFlare.cpp | 22 ------------------- src/Core/Components/Camera/CameraLensFlare.h | 2 -- src/Core/Rendering/RenderController.cpp | 8 ------- src/Platform/OpenGL/Shaders/lens_flare.glsl | 5 ++--- 4 files changed, 2 insertions(+), 35 deletions(-) diff --git a/src/Core/Components/Camera/CameraLensFlare.cpp b/src/Core/Components/Camera/CameraLensFlare.cpp index be70c4d0..7dd89940 100644 --- a/src/Core/Components/Camera/CameraLensFlare.cpp +++ b/src/Core/Components/Camera/CameraLensFlare.cpp @@ -4,16 +4,6 @@ namespace MxEngine { - float CameraLensFlare::GetLensFlareScale()const - { - return this->lensFlareScale; - } - - float CameraLensFlare::GetLensFlareBias()const - { - return this->lensFlareBias; - } - int CameraLensFlare::GetLensFlareNumOfGhosts()const { return this->lensFlareNumOfGhosts; @@ -61,18 +51,6 @@ namespace MxEngine rttr::metadata(MetaInfo::FLAGS, MetaInfo::CLONE_COPY | MetaInfo::CLONE_INSTANCE) ) .constructor<>() - .property("lens flare scale", &CameraLensFlare::GetLensFlareScale, &CameraLensFlare::SetLensFlareScale) - ( - rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), - rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) - ) - .property("lens flare bias", &CameraLensFlare::GetLensFlareBias, &CameraLensFlare::SetLensFlareBias) - ( - rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range { -200.0f, 0.f }), - rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) - ) .property("lens flare num of ghosts", &CameraLensFlare::GetLensFlareNumOfGhosts, &CameraLensFlare::SetLensFlareNumOfGhosts) ( rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), diff --git a/src/Core/Components/Camera/CameraLensFlare.h b/src/Core/Components/Camera/CameraLensFlare.h index cf8d585f..857ccd96 100644 --- a/src/Core/Components/Camera/CameraLensFlare.h +++ b/src/Core/Components/Camera/CameraLensFlare.h @@ -17,8 +17,6 @@ namespace MxEngine public: CameraLensFlare() = default; - float GetLensFlareScale()const; - float GetLensFlareBias()const; int GetLensFlareNumOfGhosts()const; float GetLensFlareGhostDispersal()const; float GetLensFlareHaloWidth()const; diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index bc014298..ee15d105 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -53,7 +53,6 @@ namespace MxEngine constexpr size_t MaxDirLightCount = 4; constexpr size_t ParticleComputeGroupSize = 64; - constexpr int DefaultLinearBlurSampleCount = 5; void RenderController::PrepareShadowMaps() { @@ -788,8 +787,6 @@ namespace MxEngine //TODO(fall2019): support chromatic aberration //TODO(fall2019): add starbust effect - float scale = camera.LensFlare->GetLensFlareScale(); - float bias = camera.LensFlare->GetLensFlareBias(); int numOfGhosts = camera.LensFlare->GetLensFlareNumOfGhosts(); float dispersal = camera.LensFlare->GetLensFlareGhostDispersal(); float haloWidth = camera.LensFlare->GetLensFlareHaloWidth(); @@ -801,8 +798,6 @@ namespace MxEngine temporaryQuater0->GenerateMipmaps(); auto& shaderGhost = this->Pipeline.Environment.Shaders["LensFlareGhosts"_id]; shaderGhost->Bind(); - shaderGhost->SetUniform("uScale", scale); - shaderGhost->SetUniform("uBias", bias); input->Bind(0); camera.AverageWhiteTexture->Bind(1); this->RenderToTextureNoClear(temporaryQuater0, shaderGhost); @@ -810,14 +805,12 @@ namespace MxEngine temporaryQuater1->GenerateMipmaps(); } - int mipLevel = 1 + floor(log2(Max(temporaryQuater1->GetWidth(), temporaryQuater1->GetHeight()))); //calc halo { auto& shaderHalo = this->Pipeline.Environment.Shaders["LensFlareHalo"_id]; shaderHalo->Bind(); shaderHalo->SetUniform("uGhostDispersal", dispersal); shaderHalo->SetUniform("uHaloWidth", haloWidth); - shaderHalo->SetUniform("uMipLevel", mipLevel); input->Bind(0); temporaryQuater1->Bind(1); camera.AverageWhiteTexture->Bind(2); @@ -831,7 +824,6 @@ namespace MxEngine lensFlare->Bind(); lensFlare->SetUniform("uGhosts", numOfGhosts); lensFlare->SetUniform("uGhostDispersal", dispersal); - lensFlare->SetUniform("uMipLevel", mipLevel); temporaryQuater1->Bind(0); temporaryQuater2->Bind(1); input->Bind(2); diff --git a/src/Platform/OpenGL/Shaders/lens_flare.glsl b/src/Platform/OpenGL/Shaders/lens_flare.glsl index 361cd9f3..a97747eb 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare.glsl @@ -5,13 +5,12 @@ layout(binding = 2) uniform sampler2D inputColor; uniform int uGhosts; // number of ghost samples uniform float uGhostDispersal; // dispersion factor -uniform int uMipLevel; in vec2 TexCoord; out vec4 outColor; -const float gWeight = 80.0f;//relative to pixel pos +const float gWeight = 80.0; //relative to pixel pos void main() { @@ -33,5 +32,5 @@ void main() //halo result += texture(inputHalo, TexCoord).rgb; - outColor = vec4(result + texture(inputColor, TexCoord).rgb, 1.f); + outColor = vec4(result + texture(inputColor, TexCoord).rgb, 1.0); } \ No newline at end of file From bcc1013ba2cffe6a9e1e95c9069aba61b57a0399 Mon Sep 17 00:00:00 2001 From: MomoDeve Date: Fri, 29 Mar 2024 05:49:24 +0900 Subject: [PATCH 25/26] intensity of flare --- .../Components/Camera/CameraLensFlare.cpp | 50 +++++++++++-------- src/Core/Components/Camera/CameraLensFlare.h | 26 +++++----- src/Core/Rendering/RenderController.cpp | 10 ++-- .../OpenGL/Shaders/lens_flare_ghosts.glsl | 11 ++-- 4 files changed, 52 insertions(+), 45 deletions(-) diff --git a/src/Core/Components/Camera/CameraLensFlare.cpp b/src/Core/Components/Camera/CameraLensFlare.cpp index 7dd89940..32f290e3 100644 --- a/src/Core/Components/Camera/CameraLensFlare.cpp +++ b/src/Core/Components/Camera/CameraLensFlare.cpp @@ -4,44 +4,44 @@ namespace MxEngine { - int CameraLensFlare::GetLensFlareNumOfGhosts()const + float CameraLensFlare::GetIntensity() const { - return this->lensFlareNumOfGhosts; + return this->intensity; } - float CameraLensFlare::GetLensFlareGhostDispersal()const + int CameraLensFlare::GetGhostNumber() const { - return this->lensFlareGhostDispersal; + return this->ghostNumber; } - float CameraLensFlare::GetLensFlareHaloWidth()const + float CameraLensFlare::GetGhostDispersal()const { - return this->lensFalreHaloWidth; + return this->ghostDispersal; } - void CameraLensFlare::SetLensFlareScale(float scale) + float CameraLensFlare::GetHaloWidth()const { - this->lensFlareScale = scale; + return this->haloWidth; } - void CameraLensFlare::SetLensFlareBias(float bias) + void CameraLensFlare::SetIntensity(float scale) { - this->lensFlareBias = bias; + this->intensity = Max(scale, 0.0); } - void CameraLensFlare::SetLensFlareNumOfGhosts(int num) + void CameraLensFlare::SetGhostNumber(int num) { - this->lensFlareNumOfGhosts = Max(num,0); + this->ghostNumber = Max(num,0); } - void CameraLensFlare::SetLensFlareGhostDispersal(float dispersal) + void CameraLensFlare::SetGhostDispersal(float dispersal) { - this->lensFlareGhostDispersal = dispersal; + this->ghostDispersal = dispersal; } - void CameraLensFlare::SetLensFlareHaloWidth(float width) + void CameraLensFlare::SetHaloWidth(float width) { - this->lensFalreHaloWidth = width; + this->haloWidth = width; } MXENGINE_REFLECT_TYPE @@ -51,22 +51,28 @@ namespace MxEngine rttr::metadata(MetaInfo::FLAGS, MetaInfo::CLONE_COPY | MetaInfo::CLONE_INSTANCE) ) .constructor<>() - .property("lens flare num of ghosts", &CameraLensFlare::GetLensFlareNumOfGhosts, &CameraLensFlare::SetLensFlareNumOfGhosts) + .property("intensity", &CameraLensFlare::GetIntensity, &CameraLensFlare::SetIntensity) ( rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 1.0f }), rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) ) - .property("lens flare ghost dispersal", &CameraLensFlare::GetLensFlareGhostDispersal, &CameraLensFlare::SetLensFlareGhostDispersal) + .property("ghost number", &CameraLensFlare::GetGhostNumber, &CameraLensFlare::SetGhostNumber) ( rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.001f, 1.f }), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.0f }), rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) ) - .property("lens falre halo width", &CameraLensFlare::GetLensFlareHaloWidth, &CameraLensFlare::SetLensFlareHaloWidth) + .property("ghost dispersal", &CameraLensFlare::GetGhostDispersal, &CameraLensFlare::SetGhostDispersal) ( rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), - rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.f }), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.001f, 1.0f }), + rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) + ) + .property("halo width", &CameraLensFlare::GetHaloWidth, &CameraLensFlare::SetHaloWidth) + ( + rttr::metadata(MetaInfo::FLAGS, MetaInfo::SERIALIZABLE | MetaInfo::EDITABLE), + rttr::metadata(EditorInfo::EDIT_RANGE, Range { 0.0f, 100.0f }), rttr::metadata(EditorInfo::EDIT_PRECISION, 0.01f) ); } diff --git a/src/Core/Components/Camera/CameraLensFlare.h b/src/Core/Components/Camera/CameraLensFlare.h index 857ccd96..7a34e320 100644 --- a/src/Core/Components/Camera/CameraLensFlare.h +++ b/src/Core/Components/Camera/CameraLensFlare.h @@ -9,22 +9,22 @@ namespace MxEngine { MAKE_COMPONENT(CameraLensFlare); private: - float lensFlareScale = 1.0f; - float lensFlareBias = -42.0f; - int lensFlareNumOfGhosts = 12; - float lensFlareGhostDispersal = 0.13f; - float lensFalreHaloWidth = 0.5f; + float intensity = 0.1; + int ghostNumber = 12; + float ghostDispersal = 0.13f; + float haloWidth = 0.5f; public: CameraLensFlare() = default; - int GetLensFlareNumOfGhosts()const; - float GetLensFlareGhostDispersal()const; - float GetLensFlareHaloWidth()const; - void SetLensFlareScale(float); - void SetLensFlareBias(float); - void SetLensFlareNumOfGhosts(int); - void SetLensFlareGhostDispersal(float); - void SetLensFlareHaloWidth(float); + float GetIntensity() const; + int GetGhostNumber() const; + float GetGhostDispersal() const; + float GetHaloWidth() const; + + void SetIntensity(float); + void SetGhostNumber(int); + void SetGhostDispersal(float); + void SetHaloWidth(float); }; } \ No newline at end of file diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index ee15d105..e7dd5802 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -787,9 +787,10 @@ namespace MxEngine //TODO(fall2019): support chromatic aberration //TODO(fall2019): add starbust effect - int numOfGhosts = camera.LensFlare->GetLensFlareNumOfGhosts(); - float dispersal = camera.LensFlare->GetLensFlareGhostDispersal(); - float haloWidth = camera.LensFlare->GetLensFlareHaloWidth(); + int ghostNumber = camera.LensFlare->GetGhostNumber(); + float dispersal = camera.LensFlare->GetGhostDispersal(); + float haloWidth = camera.LensFlare->GetHaloWidth(); + float intensity = camera.LensFlare->GetIntensity(); input->GenerateMipmaps(); @@ -798,6 +799,7 @@ namespace MxEngine temporaryQuater0->GenerateMipmaps(); auto& shaderGhost = this->Pipeline.Environment.Shaders["LensFlareGhosts"_id]; shaderGhost->Bind(); + shaderGhost->SetUniform("uIntensity", intensity); input->Bind(0); camera.AverageWhiteTexture->Bind(1); this->RenderToTextureNoClear(temporaryQuater0, shaderGhost); @@ -822,7 +824,7 @@ namespace MxEngine { auto& lensFlare = this->Pipeline.Environment.Shaders["LensFlare"_id]; lensFlare->Bind(); - lensFlare->SetUniform("uGhosts", numOfGhosts); + lensFlare->SetUniform("uGhosts", ghostNumber); lensFlare->SetUniform("uGhostDispersal", dispersal); temporaryQuater1->Bind(0); temporaryQuater2->Bind(1); diff --git a/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl b/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl index 590ec4c9..1d151d63 100644 --- a/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl +++ b/src/Platform/OpenGL/Shaders/lens_flare_ghosts.glsl @@ -3,8 +3,7 @@ layout(binding = 0) uniform sampler2D inputColor; layout(binding = 1) uniform sampler2D averageWhiteTex; -uniform float uScale; -uniform float uBias; +uniform float uIntensity; // [0, 1] range in vec2 TexCoord; @@ -15,8 +14,8 @@ void main() vec3 col = texture(inputColor, TexCoord).rgb; float sceneLum = calcLuminance(col); - float averageWhite = texture(averageWhiteTex, vec2(0.0f)).r; - float lum = sceneLum / (averageWhite * 10.0); - lum = max(lum - 0.1, 0.0); - outputColor = vec4(lum * col * 0.5, averageWhite); + float averageWhite = texture(averageWhiteTex, vec2(0.0)).r; + float lumRatio = sceneLum / averageWhite; + float lum = lumRatio < 1.0 ? 0.0 : pow(lumRatio, uIntensity) - 1.0; + outputColor = vec4(lum * col, averageWhite); } \ No newline at end of file From cca33ba771e5da5cf327a0761fbc4c512c31e111 Mon Sep 17 00:00:00 2001 From: MomoDeve Date: Fri, 29 Mar 2024 05:56:24 +0900 Subject: [PATCH 26/26] revert uMipLevel deletion --- src/Core/Rendering/RenderController.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Core/Rendering/RenderController.cpp b/src/Core/Rendering/RenderController.cpp index e7dd5802..fc5b7ae3 100644 --- a/src/Core/Rendering/RenderController.cpp +++ b/src/Core/Rendering/RenderController.cpp @@ -791,6 +791,7 @@ namespace MxEngine float dispersal = camera.LensFlare->GetGhostDispersal(); float haloWidth = camera.LensFlare->GetHaloWidth(); float intensity = camera.LensFlare->GetIntensity(); + int mipLevel = 1 + floor(log2(Max(temporaryQuater1->GetWidth(), temporaryQuater1->GetHeight()))); input->GenerateMipmaps(); @@ -813,6 +814,7 @@ namespace MxEngine shaderHalo->Bind(); shaderHalo->SetUniform("uGhostDispersal", dispersal); shaderHalo->SetUniform("uHaloWidth", haloWidth); + shaderHalo->SetUniform("uMipLevel", mipLevel); input->Bind(0); temporaryQuater1->Bind(1); camera.AverageWhiteTexture->Bind(2);