Skip to content

Commit

Permalink
add Ibl strength for sky component
Browse files Browse the repository at this point in the history
  • Loading branch information
T-rvw authored Mar 12, 2024
2 parents 717d715 + 304498d commit e15bba1
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Engine/BuiltInShaders/common/Envirnoment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ SAMPLERCUBE(s_texCubeIrr, IBL_IRRADIANCE_SLOT);
SAMPLERCUBE(s_texCubeRad, IBL_RADIANCE_SLOT);
SAMPLER2D(s_texLUT, BRDF_LUT_SLOT);

uniform vec4 u_iblStrength;

vec3 SampleEnvIrradiance(vec3 normal, float mip) {
// We use the HDR texture which in linear space.
vec3 cubeNormalDir = normalize(fixCubeLookup(normal, mip, 256.0));
Expand Down Expand Up @@ -74,6 +76,8 @@ vec3 GetIBL(Material material, vec3 vertexNormal, vec3 viewDir) {

// Specular
envColor += (envSpecularBRDF * envRadiance * finalSpecularOcclusion);

envColor *= vec3_splat(u_iblStrength.x);
#endif

return envColor;
Expand Down
4 changes: 3 additions & 1 deletion Engine/BuiltInShaders/shaders/fs_skybox.sc
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ $input v_worldPos

SAMPLERCUBE(s_texSkybox, 0);

uniform vec4 u_skyboxStrength;

void main()
{
vec3 dir = normalize(v_worldPos.xyz);

// Use radiance texture mip0.
// We use the HDR texture which in linear space.
vec3 color = textureCubeLod(s_texSkybox, dir, 0.0).xyz * vec3_splat(0.6);
vec3 color = textureCubeLod(s_texSkybox, dir, 0.0).xyz * vec3_splat(u_skyboxStrength.x);

gl_FragColor = vec4(color, 1.0);
}
25 changes: 21 additions & 4 deletions Engine/Source/Editor/UILayers/Inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,25 @@ void UpdateComponentWidget<engine::MaterialComponent>(engine::SceneWorld* pScene
ImGui::PopStyleVar();
}

// Shaders
// Ambient
{
bool isOpen = ImGui::CollapsingHeader("Ambient", ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_DefaultOpen);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2));
ImGui::Separator();
if (isOpen)
{
bool isOpen = ImGui::CollapsingHeader("Shader", ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_DefaultOpen);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2));
ImGui::Separator();
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);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2));
ImGui::Separator();

if (isOpen)
{
Expand Down Expand Up @@ -617,6 +631,9 @@ void UpdateComponentWidget<engine::SkyComponent>(engine::SceneWorld* pSceneWorld
}
}

ImGui::Separator();
ImGuiUtils::ImGuiFloatProperty("Skybox Strength", pSkyComponent->GetSkyboxStrength(), cd::Unit::None, 0.01f, 10.0f, false, 0.02f);

if (pSkyComponent->GetAtmophericScatteringEnable())
{
ImGui::Separator();
Expand Down
8 changes: 7 additions & 1 deletion Engine/Source/Runtime/ECWorld/MaterialComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ class MaterialComponent final
void SetToonParameters(ToonParameters toonParameters) { m_toonParameters = toonParameters; }
ToonParameters& GetToonParameters() { return m_toonParameters; }
ToonParameters GetToonParameters() const { return m_toonParameters; }

void SetIblStrengeth(float strength) { m_iblStrength = strength; }
float& GetIblStrengeth() { return m_iblStrength; }
float GetIblStrengeth() const { return m_iblStrength; }

private:
// Input
const cd::Material* m_pMaterialData = nullptr;
Expand Down Expand Up @@ -205,8 +210,9 @@ class MaterialComponent final
std::vector<TextureBlob> m_cacheTextureBlobs;

// Output
std::map<cd::MaterialTextureType, PropertyGroup> m_propertyGroups;
float m_iblStrength = 0.5f;
ToonParameters m_toonParameters;
std::map<cd::MaterialTextureType, PropertyGroup> m_propertyGroups;
};

}
5 changes: 5 additions & 0 deletions Engine/Source/Runtime/ECWorld/SkyComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class SkyComponent final
SkyType& GetSkyType() { return m_type; }
const SkyType& GetSkyType() const { return m_type; }

void SetSkyboxStrength(float strength) { m_skyboxStrength = strength; }
float& GetSkyboxStrength() { return m_skyboxStrength; }
float GetSkyboxStrength() const { return m_skyboxStrength; }

void SetAtmophericScatteringEnable(bool state) { m_isAtmophericScatteringEnable = state; }
bool& GetAtmophericScatteringEnable() { return m_isAtmophericScatteringEnable; }
const bool& GetAtmophericScatteringEnable() const { return m_isAtmophericScatteringEnable; }
Expand Down Expand Up @@ -75,6 +79,7 @@ class SkyComponent final

private:
SkyType m_type = SkyType::SkyBox;
float m_skyboxStrength = 0.5f;
bool m_isAtmophericScatteringEnable = false;

StringCrc m_ATMTransmittanceCrc;
Expand Down
7 changes: 5 additions & 2 deletions Engine/Source/Runtime/Rendering/SkyboxRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace

constexpr const char* skyboxSampler = "s_texSkybox";
constexpr const char* skyboxProgram = "skyboxProgram";
constexpr const char* skyboxStrength = "u_skyboxStrength";

constexpr uint16_t sampleFalg = BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP | BGFX_SAMPLER_W_CLAMP;
constexpr uint64_t renderState = BGFX_STATE_WRITE_MASK | BGFX_STATE_CULL_CCW | BGFX_STATE_MSAA | BGFX_STATE_DEPTH_TEST_LEQUAL;
Expand All @@ -33,6 +34,7 @@ void SkyboxRenderer::Warmup()
SkyComponent* pSkyComponent = m_pCurrentSceneWorld->GetSkyComponent(m_pCurrentSceneWorld->GetSkyEntity());

GetRenderContext()->CreateUniform(skyboxSampler, bgfx::UniformType::Sampler);
GetRenderContext()->CreateUniform(skyboxStrength, bgfx::UniformType::Vec4, 1);
GetRenderContext()->CreateTexture(pSkyComponent->GetRadianceTexturePath().c_str(), sampleFalg);

GetRenderContext()->UploadShaderProgram(skyboxProgram);
Expand Down Expand Up @@ -84,14 +86,15 @@ void SkyboxRenderer::Render(float deltaTime)
SkyComponent* pSkyComponent = m_pCurrentSceneWorld->GetSkyComponent(m_pCurrentSceneWorld->GetSkyEntity());
GetRenderContext()->CreateTexture(pSkyComponent->GetRadianceTexturePath().c_str(), sampleFalg);

constexpr StringCrc samplerCrc(skyboxSampler);
constexpr StringCrc skyboxStrengthCrc{ skyboxStrength };
GetRenderContext()->FillUniform(skyboxStrengthCrc, &(pSkyComponent->GetSkyboxStrength()));

constexpr StringCrc samplerCrc(skyboxSampler);
bgfx::setTexture(0,
GetRenderContext()->GetUniform(samplerCrc),
GetRenderContext()->GetTexture(StringCrc(pSkyComponent->GetRadianceTexturePath())));

bgfx::setState(renderState);

SubmitStaticMeshDrawCall(pMeshComponent, GetViewID(), skyboxProgram);
}

Expand Down
9 changes: 7 additions & 2 deletions Engine/Source/Runtime/Rendering/WorldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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* metallicRoughnessFactor = "u_metallicRoughnessFactor";
Expand Down Expand Up @@ -78,6 +79,7 @@ void WorldRenderer::Warmup()
GetRenderContext()->CreateTexture(pSkyComponent->GetRadianceTexturePath().c_str(), samplerFlags);

GetRenderContext()->CreateUniform(cameraPos, bgfx::UniformType::Vec4, 1);
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);
Expand Down Expand Up @@ -265,8 +267,11 @@ void WorldRenderer::Render(float deltaTime)
GetRenderContext()->GetUniform(radSamplerCrc),
GetRenderContext()->GetTexture(StringCrc(pSkyComponent->GetRadianceTexturePath())));

constexpr StringCrc lutsamplerCrc(lutSampler);
constexpr StringCrc luttextureCrc(lutTexture);
constexpr StringCrc iblStrengthCrc{ iblStrength };
GetRenderContext()->FillUniform(iblStrengthCrc, &(pMaterialComponent->GetIblStrengeth()));

constexpr StringCrc lutsamplerCrc{ lutSampler };
constexpr StringCrc luttextureCrc{ lutTexture };
bgfx::setTexture(BRDF_LUT_SLOT, GetRenderContext()->GetUniform(lutsamplerCrc), GetRenderContext()->GetTexture(luttextureCrc));
}
else if (SkyType::AtmosphericScattering == crtSkyType)
Expand Down
Binary file removed Projects/Test/Textures/skybox/defaultSkybox.hdr
Binary file not shown.
Binary file modified Projects/Test/Textures/skybox/defaultSkybox_irr.dds
Binary file not shown.
Binary file modified Projects/Test/Textures/skybox/defaultSkybox_rad.dds
Binary file not shown.

0 comments on commit e15bba1

Please sign in to comment.