Skip to content

Commit

Permalink
PBR reflectance parameter (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas authored Apr 6, 2024
1 parent 74230b1 commit 2320dfa
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 59 deletions.
9 changes: 5 additions & 4 deletions Engine/BuiltInShaders/common/Material.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
29 changes: 15 additions & 14 deletions Engine/Source/Editor/UILayers/Inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,21 @@ void UpdateComponentWidget<engine::MaterialComponent>(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);
Expand Down Expand Up @@ -309,20 +324,6 @@ void UpdateComponentWidget<engine::MaterialComponent>(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);
Expand Down
5 changes: 5 additions & 0 deletions Engine/Source/Runtime/ECWorld/MaterialComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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<cd::MaterialTextureType, PropertyGroup> m_propertyGroups;
};
Expand Down
13 changes: 7 additions & 6 deletions Engine/Source/Runtime/Rendering/TerrainRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -200,12 +200,13 @@ void TerrainRenderer::Render(float deltaTime)
constexpr StringCrc albedoColorCrc(albedoColor);
GetRenderContext()->FillUniform(albedoColorCrc, pMaterialComponent->GetFactor<cd::Vec3f>(cd::MaterialPropertyGroup::BaseColor), 1);

cd::Vec4f metallicRoughnessFactorData(
cd::Vec4f u_metallicRoughnessRefectanceFactorData(
*(pMaterialComponent->GetFactor<float>(cd::MaterialPropertyGroup::Metallic)),
*(pMaterialComponent->GetFactor<float>(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::Vec4f>(cd::MaterialPropertyGroup::Emissive), 1);
Expand Down
71 changes: 36 additions & 35 deletions Engine/Source/Runtime/Rendering/WorldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -310,12 +310,13 @@ void WorldRenderer::Render(float deltaTime)
constexpr StringCrc albedoColorCrc(albedoColor);
GetRenderContext()->FillUniform(albedoColorCrc, pMaterialComponent->GetFactor<cd::Vec3f>(cd::MaterialPropertyGroup::BaseColor), 1);

cd::Vec4f metallicRoughnessFactorData(
cd::Vec4f metallicRoughnessRefectanceFactorData(
*(pMaterialComponent->GetFactor<float>(cd::MaterialPropertyGroup::Metallic)),
*(pMaterialComponent->GetFactor<float>(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::Vec4f>(cd::MaterialPropertyGroup::Emissive), 1);
Expand Down

0 comments on commit 2320dfa

Please sign in to comment.