From 2320dfa767aa13c32da1dbab7a087bb50d3cda7a Mon Sep 17 00:00:00 2001 From: Hinageshi <69386319+Hinageshi01@users.noreply.github.com> Date: Sat, 6 Apr 2024 14:29:56 +0800 Subject: [PATCH] PBR reflectance parameter (#10) --- Engine/BuiltInShaders/common/Material.sh | 9 +-- Engine/Source/Editor/UILayers/Inspector.cpp | 29 ++++---- .../Runtime/ECWorld/MaterialComponent.h | 5 ++ .../Runtime/Rendering/TerrainRenderer.cpp | 13 ++-- .../Runtime/Rendering/WorldRenderer.cpp | 71 ++++++++++--------- 5 files changed, 68 insertions(+), 59 deletions(-) diff --git a/Engine/BuiltInShaders/common/Material.sh b/Engine/BuiltInShaders/common/Material.sh index 6b587d1a..21067fa3 100644 --- a/Engine/BuiltInShaders/common/Material.sh +++ b/Engine/BuiltInShaders/common/Material.sh @@ -21,7 +21,7 @@ struct Material { }; uniform vec4 u_albedoColor; -uniform vec4 u_metallicRoughnessFactor; +uniform vec4 u_metallicRoughnessRefectanceFactor; uniform vec4 u_albedoUVOffsetAndScale; uniform vec4 u_alphaCutOff; @@ -110,15 +110,16 @@ Material GetMaterial(vec2 uv, vec3 normal, mat3 TBN) { material.roughness = orm.y; material.metallic = orm.z; #else - material.roughness = u_metallicRoughnessFactor.y; - material.metallic = u_metallicRoughnessFactor.x; + material.roughness = u_metallicRoughnessRefectanceFactor.y; + material.metallic = u_metallicRoughnessRefectanceFactor.x; #endif #if defined(EMISSIVEMAP) material.emissive = SampleEmissiveTexture(uv); #endif - material.F0 = mix(vec3_splat(0.04), material.albedo, material.metallic); + float refectance = u_metallicRoughnessRefectanceFactor.z; + material.F0 = mix(0.16 * refectance * refectance, material.albedo, material.metallic); return material; } diff --git a/Engine/Source/Editor/UILayers/Inspector.cpp b/Engine/Source/Editor/UILayers/Inspector.cpp index 6d2e2955..2210a698 100644 --- a/Engine/Source/Editor/UILayers/Inspector.cpp +++ b/Engine/Source/Editor/UILayers/Inspector.cpp @@ -278,6 +278,21 @@ void UpdateComponentWidget(engine::SceneWorld* pScene ImGui::PopStyleVar(); } + // PBR + { + bool isOpen = ImGui::CollapsingHeader("PBR", ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_DefaultOpen); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2)); + ImGui::Separator(); + if (isOpen) + { + ImGuiUtils::ImGuiFloatProperty("iblStrength", pMaterialComponent->GetIblStrengeth(), cd::Unit::None, 0.01f, 10.0f, false, 0.02f); + ImGuiUtils::ImGuiFloatProperty("Reflectance", pMaterialComponent->GetReflectance(), cd::Unit::None, 0.0f, 1.0f); + } + + ImGui::Separator(); + ImGui::PopStyleVar(); + } + // Cartoon { bool isOpen = ImGui::CollapsingHeader("Cartoon Material", ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_Selected); @@ -309,20 +324,6 @@ void UpdateComponentWidget(engine::SceneWorld* pScene ImGui::PopStyleVar(); } - // Ambient - { - bool isOpen = ImGui::CollapsingHeader("Ambient", ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_DefaultOpen); - ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2)); - ImGui::Separator(); - if (isOpen) - { - ImGuiUtils::ImGuiFloatProperty("iblStrength", pMaterialComponent->GetIblStrengeth(), cd::Unit::None, 0.01f, 10.0f, false, 0.02f); - } - - ImGui::Separator(); - ImGui::PopStyleVar(); - } - // Shaders { bool isOpen = ImGui::CollapsingHeader("Shader", ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_DefaultOpen); diff --git a/Engine/Source/Runtime/ECWorld/MaterialComponent.h b/Engine/Source/Runtime/ECWorld/MaterialComponent.h index 0f7571f6..e08e0e0e 100644 --- a/Engine/Source/Runtime/ECWorld/MaterialComponent.h +++ b/Engine/Source/Runtime/ECWorld/MaterialComponent.h @@ -184,6 +184,10 @@ class MaterialComponent final float& GetIblStrengeth() { return m_iblStrength; } float GetIblStrengeth() const { return m_iblStrength; } + void SetReflectance(float reflectance) { m_reflectance = reflectance; } + float& GetReflectance() { return m_reflectance; } + float GetReflectance() const { return m_reflectance; } + void SetToonParameters(ToonParameters toonParameters) { m_toonParameters = toonParameters; } ToonParameters& GetToonParameters() { return m_toonParameters; } ToonParameters GetToonParameters() const { return m_toonParameters; } @@ -205,6 +209,7 @@ class MaterialComponent final cd::BlendMode m_blendMode; float m_alphaCutOff; float m_iblStrength = 0.5f; + float m_reflectance = 0.5f; ToonParameters m_toonParameters; std::map m_propertyGroups; }; diff --git a/Engine/Source/Runtime/Rendering/TerrainRenderer.cpp b/Engine/Source/Runtime/Rendering/TerrainRenderer.cpp index 0b05e160..a2b2080d 100644 --- a/Engine/Source/Runtime/Rendering/TerrainRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/TerrainRenderer.cpp @@ -43,7 +43,7 @@ constexpr const char* cameraPos = "u_cameraPos"; constexpr const char* cameraNearFarPlane = "u_cameraNearFarPlane"; constexpr const char* albedoColor = "u_albedoColor"; -constexpr const char* metallicRoughnessFactor = "u_metallicRoughnessFactor"; +constexpr const char* metallicRoughnessRefectanceFactor = "u_metallicRoughnessRefectanceFactor"; constexpr const char* albedoUVOffsetAndScale = "u_albedoUVOffsetAndScale"; constexpr const char* alphaCutOff = "u_alphaCutOff"; constexpr const char* emissiveColor = "u_emissiveColor"; @@ -80,7 +80,7 @@ void TerrainRenderer::Init() GetRenderContext()->CreateUniform(albedoColor, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(emissiveColor, bgfx::UniformType::Vec4, 1); - GetRenderContext()->CreateUniform(metallicRoughnessFactor, bgfx::UniformType::Vec4, 1); + GetRenderContext()->CreateUniform(metallicRoughnessRefectanceFactor, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(albedoUVOffsetAndScale, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(alphaCutOff, bgfx::UniformType::Vec4, 1); @@ -200,12 +200,13 @@ void TerrainRenderer::Render(float deltaTime) constexpr StringCrc albedoColorCrc(albedoColor); GetRenderContext()->FillUniform(albedoColorCrc, pMaterialComponent->GetFactor(cd::MaterialPropertyGroup::BaseColor), 1); - cd::Vec4f metallicRoughnessFactorData( + cd::Vec4f u_metallicRoughnessRefectanceFactorData( *(pMaterialComponent->GetFactor(cd::MaterialPropertyGroup::Metallic)), *(pMaterialComponent->GetFactor(cd::MaterialPropertyGroup::Roughness)), - 1.0f, 1.0f); - constexpr StringCrc mrFactorCrc(metallicRoughnessFactor); - GetRenderContext()->FillUniform(mrFactorCrc, metallicRoughnessFactorData.begin(), 1); + pMaterialComponent->GetReflectance(), + 1.0f); + constexpr StringCrc mrrFactorCrc(metallicRoughnessRefectanceFactor); + GetRenderContext()->FillUniform(mrrFactorCrc, u_metallicRoughnessRefectanceFactorData.begin(), 1); constexpr StringCrc emissiveColorCrc(emissiveColor); GetRenderContext()->FillUniform(emissiveColorCrc, pMaterialComponent->GetFactor(cd::MaterialPropertyGroup::Emissive), 1); diff --git a/Engine/Source/Runtime/Rendering/WorldRenderer.cpp b/Engine/Source/Runtime/Rendering/WorldRenderer.cpp index 404bb2e1..69967817 100644 --- a/Engine/Source/Runtime/Rendering/WorldRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/WorldRenderer.cpp @@ -24,37 +24,37 @@ namespace engine namespace { -constexpr const char* lutSampler = "s_texLUT"; -constexpr const char* cubeIrradianceSampler = "s_texCubeIrr"; -constexpr const char* cubeRadianceSampler = "s_texCubeRad"; +constexpr const char* lutSampler = "s_texLUT"; +constexpr const char* cubeIrradianceSampler = "s_texCubeIrr"; +constexpr const char* cubeRadianceSampler = "s_texCubeRad"; + +constexpr const char* lutTexture = "Textures/lut/ibl_brdf_lut.dds"; + +constexpr const char* cameraPos = "u_cameraPos"; +constexpr const char* iblStrength = "u_iblStrength"; +constexpr const char* albedoColor = "u_albedoColor"; +constexpr const char* emissiveColorAndFactor = "u_emissiveColorAndFactor"; +constexpr const char* metallicRoughnessRefectanceFactor = "u_metallicRoughnessRefectanceFactor"; -constexpr const char* lutTexture = "Textures/lut/ibl_brdf_lut.dds"; - -constexpr const char* cameraPos = "u_cameraPos"; -constexpr const char* iblStrength = "u_iblStrength"; -constexpr const char* albedoColor = "u_albedoColor"; -constexpr const char* emissiveColorAndFactor = "u_emissiveColorAndFactor"; -constexpr const char* metallicRoughnessFactor = "u_metallicRoughnessFactor"; - -constexpr const char* albedoUVOffsetAndScale = "u_albedoUVOffsetAndScale"; -constexpr const char* alphaCutOff = "u_alphaCutOff"; - -constexpr const char* lightCountAndStride = "u_lightCountAndStride"; -constexpr const char* lightParams = "u_lightParams"; - -constexpr const char* LightDir = "u_LightDir"; -constexpr const char* HeightOffsetAndshadowLength = "u_HeightOffsetAndshadowLength"; - -constexpr const char* lightViewProjs = "u_lightViewProjs"; -constexpr const char* cubeShadowMapSamplers[3] = { "s_texCubeShadowMap_1", "s_texCubeShadowMap_2" , "s_texCubeShadowMap_3" }; - -constexpr const char* cameraNearFarPlane = "u_cameraNearFarPlane"; -constexpr const char* cameraLookAt = "u_cameraLookAt"; -constexpr const char* clipFrustumDepth = "u_clipFrustumDepth"; - -constexpr const char* directionShadowMapTexture = "DirectionShadowMapTexture"; -constexpr const char* pointShadowMapTexture = "PointShadowMapTexture"; -constexpr const char* spotShadowMapTexture = "SpotShadowMapTexture"; +constexpr const char* albedoUVOffsetAndScale = "u_albedoUVOffsetAndScale"; +constexpr const char* alphaCutOff = "u_alphaCutOff"; + +constexpr const char* lightCountAndStride = "u_lightCountAndStride"; +constexpr const char* lightParams = "u_lightParams"; + +constexpr const char* LightDir = "u_LightDir"; +constexpr const char* HeightOffsetAndshadowLength = "u_HeightOffsetAndshadowLength"; + +constexpr const char* lightViewProjs = "u_lightViewProjs"; +constexpr const char* cubeShadowMapSamplers[3] = { "s_texCubeShadowMap_1", "s_texCubeShadowMap_2" , "s_texCubeShadowMap_3" }; + +constexpr const char* cameraNearFarPlane = "u_cameraNearFarPlane"; +constexpr const char* cameraLookAt = "u_cameraLookAt"; +constexpr const char* clipFrustumDepth = "u_clipFrustumDepth"; + +constexpr const char* directionShadowMapTexture = "DirectionShadowMapTexture"; +constexpr const char* pointShadowMapTexture = "PointShadowMapTexture"; +constexpr const char* spotShadowMapTexture = "SpotShadowMapTexture"; constexpr uint64_t samplerFlags = BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP | BGFX_SAMPLER_W_CLAMP; constexpr uint64_t defaultRenderingState = BGFX_STATE_WRITE_MASK | BGFX_STATE_MSAA | BGFX_STATE_DEPTH_TEST_LESS; @@ -78,7 +78,7 @@ void WorldRenderer::Init() GetRenderContext()->CreateUniform(iblStrength, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(albedoColor, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(emissiveColorAndFactor, bgfx::UniformType::Vec4, 1); - GetRenderContext()->CreateUniform(metallicRoughnessFactor, bgfx::UniformType::Vec4, 1); + GetRenderContext()->CreateUniform(metallicRoughnessRefectanceFactor, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(albedoUVOffsetAndScale, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(alphaCutOff, bgfx::UniformType::Vec4, 1); @@ -310,12 +310,13 @@ void WorldRenderer::Render(float deltaTime) constexpr StringCrc albedoColorCrc(albedoColor); GetRenderContext()->FillUniform(albedoColorCrc, pMaterialComponent->GetFactor(cd::MaterialPropertyGroup::BaseColor), 1); - cd::Vec4f metallicRoughnessFactorData( + cd::Vec4f metallicRoughnessRefectanceFactorData( *(pMaterialComponent->GetFactor(cd::MaterialPropertyGroup::Metallic)), *(pMaterialComponent->GetFactor(cd::MaterialPropertyGroup::Roughness)), - 1.0f, 1.0f); - constexpr StringCrc mrFactorCrc(metallicRoughnessFactor); - GetRenderContext()->FillUniform(mrFactorCrc, metallicRoughnessFactorData.begin(), 1); + pMaterialComponent->GetReflectance(), + 1.0f); + constexpr StringCrc mrrFactorCrc(metallicRoughnessRefectanceFactor); + GetRenderContext()->FillUniform(mrrFactorCrc, metallicRoughnessRefectanceFactorData.begin(), 1); constexpr StringCrc emissiveColorCrc(emissiveColorAndFactor); GetRenderContext()->FillUniform(emissiveColorCrc, pMaterialComponent->GetFactor(cd::MaterialPropertyGroup::Emissive), 1);