Skip to content

Commit

Permalink
add pbr and ibl demo and add light shadow switch (#19)
Browse files Browse the repository at this point in the history
* add pbr and ibl demo and add light shadow switch
  • Loading branch information
VTui22 committed May 16, 2024
1 parent 93422a4 commit 7c5babd
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
7 changes: 4 additions & 3 deletions Engine/BuiltInShaders/common/LightSource.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ uniform vec4 u_lightParams[LIGHT_LENGTH];
uniform mat4 u_lightViewProjs[4*3];
uniform vec4 u_clipFrustumDepth;
uniform vec4 u_bias[3]; // [LIGHT_NUM]
uniform vec4 u_isCastShadow;

SAMPLERCUBE(s_texCubeShadowMap_1, SHADOW_MAP_CUBE_FIRST_SLOT);
SAMPLERCUBE(s_texCubeShadowMap_2, SHADOW_MAP_CUBE_SECOND_SLOT);
Expand Down Expand Up @@ -185,7 +186,7 @@ vec3 CalculatePointLight(U_Light light, Material material, vec3 worldPos, vec3 v
vec3 specularBRDF = Fre * NDF * Vis;

vec3 KD = mix(vec3_splat(1.0) - Fre, vec3_splat(0.0), material.metallic);
float shadow = CalculatePointShadow(worldPos, light.position, light.range, lightIndex);
float shadow = CalculatePointShadow(worldPos, light.position, light.range, lightIndex) * u_isCastShadow.x;
return (1 - shadow) * (KD * diffuseBRDF + specularBRDF) * radiance * NdotL;
}

Expand Down Expand Up @@ -237,7 +238,7 @@ vec3 CalculateSpotLight(U_Light light, Material material, vec3 worldPos, vec3 vi
vec3 specularBRDF = Fre * NDF * Vis;

vec3 KD = mix(1.0 - Fre, vec3_splat(0.0), material.metallic);
float shadow = CalculateSpotShadow(worldPos, material.normal, lightDir, light.lightViewProjOffset, lightIndex);
float shadow = CalculatePointShadow(worldPos, light.position, light.range, lightIndex) * u_isCastShadow.x;
return (1.0 - shadow) * (KD * diffuseBRDF + specularBRDF) * radiance * NdotL;
}

Expand Down Expand Up @@ -310,7 +311,7 @@ vec3 CalculateDirectionalLight(U_Light light, Material material, vec3 worldPos,

vec3 KD = mix(1.0 - Fre, vec3_splat(0.0), material.metallic);
vec3 irradiance = light.color * light.intensity;
float shadow = CalculateCascadedDirectionalShadow(worldPos, material.normal, lightDir, csmDepth, light.lightViewProjOffset, lightIndex);
float shadow = CalculateCascadedDirectionalShadow(worldPos, material.normal, lightDir, csmDepth, light.lightViewProjOffset, lightIndex) * u_isCastShadow.x;
return (1.0 - shadow) * (KD * diffuseBRDF + specularBRDF) * irradiance * NdotL;
}

Expand Down
2 changes: 2 additions & 0 deletions Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ void EditorApp::Init(engine::EngineInitArgs initArgs)

InitECWorld();
m_pEditorImGuiContext->SetSceneWorld(m_pSceneWorld.get());
// In order to avoid temporary handling of bugs
m_pSceneWorld->GetSceneDatabase()->GetMeshes().reserve(100);

InitEngineRenderers();

Expand Down
34 changes: 34 additions & 0 deletions Engine/Source/Editor/UILayers/EntityList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,40 @@ void EntityList::AddEntity(engine::SceneWorld* pSceneWorld)

particleForceFieldComponent.Build();
}

else if (ImGui::MenuItem("PBR Demo"))
{
engine::Entity entity = AddNamedEntity("DirectionalLight");
auto& lightComponent = CreateLightComponents(entity, cd::LightType::Directional, 4.0f, cd::Vec3f(1.0f, 1.0f, 1.0f), true);
lightComponent.SetDirection(cd::Direction(0.65f, -0.65f, 0.3f));
lightComponent.SetCascadeNum(4);
lightComponent.SetIsCastShadow(false);
lightComponent.SetFrustumClips(cd::Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
lightComponent.SetShadowMapTexture(BGFX_INVALID_HANDLE);

for (int horizontalIndex = 0 ; horizontalIndex < 7; ++horizontalIndex)
{
for (int verticalIndex = 0; verticalIndex < 7; ++verticalIndex)
{
engine::Entity entity = AddNamedEntity("Sphere");
std::optional<cd::Mesh> optMesh = cd::MeshGenerator::Generate(cd::Sphere(cd::Point(0.0f), 10.0f), 100U, 100U, pPBRMaterialType->GetRequiredVertexFormat());
assert(optMesh.has_value());
optMesh->SetName("Sphere");
CreateShapeComponents(entity, cd::MoveTemp(optMesh.value()), pPBRMaterialType);

auto* pTransformComponent = pSceneWorld->GetTransformComponent(entity);
cd::Transform transform = cd::Transform::Identity();
transform.SetTranslation(cd::Vec3f(26.0f * (horizontalIndex - 4), 26.0f * (verticalIndex - 4), 0.0f));
pTransformComponent->SetTransform(transform);
pTransformComponent->Build();

auto* pMaterialComponent = pSceneWorld->GetMaterialComponent(entity);
pMaterialComponent->SetFactor<float>(cd::MaterialPropertyGroup::Metallic, horizontalIndex * (1.0f / 6.0f));
pMaterialComponent->SetFactor<float>(cd::MaterialPropertyGroup::Roughness, verticalIndex * (1.0f / 6.0f));
}
}

}
}

void EntityList::DrawEntity(engine::SceneWorld* pSceneWorld, engine::Entity entity)
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/ECWorld/MaterialComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class MaterialComponent final
bool m_twoSided;
cd::BlendMode m_blendMode;
float m_alphaCutOff;
float m_iblStrength = 0.25f;
float m_iblStrength = 1.0f;
float m_reflectance = 0.5f;
ToonParameters m_toonParameters;
std::map<cd::MaterialTextureType, PropertyGroup> m_propertyGroups;
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/Rendering/ShadowMapRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void ShadowMapRenderer::Render(float deltaTime)
// Non-shadow-casting lights(include area lights) are excluded
if (!lightComponent->IsCastShadow())
{
continue;
//continue;
}

// Render shadow map
Expand Down
15 changes: 15 additions & 0 deletions Engine/Source/Runtime/Rendering/WorldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ constexpr const char* cameraNearFarPlane = "u_cameraNearFarPlane"
constexpr const char* cameraLookAt = "u_cameraLookAt";
constexpr const char* clipFrustumDepth = "u_clipFrustumDepth";

constexpr const char* IsCastShadow = "u_isCastShadow";
constexpr const char* directionShadowMapTexture = "DirectionShadowMapTexture";
constexpr const char* pointShadowMapTexture = "PointShadowMapTexture";
constexpr const char* spotShadowMapTexture = "SpotShadowMapTexture";
Expand Down Expand Up @@ -95,6 +96,7 @@ void WorldRenderer::Init()

GetRenderContext()->CreateUniform(cameraNearFarPlane, bgfx::UniformType::Vec4, 1);
GetRenderContext()->CreateUniform(clipFrustumDepth, bgfx::UniformType::Vec4, 1);
GetRenderContext()->CreateUniform(IsCastShadow, bgfx::UniformType::Vec4, 1);

bgfx::setViewName(GetViewID(), "WorldRenderer");
}
Expand Down Expand Up @@ -367,6 +369,19 @@ void WorldRenderer::Render(float deltaTime)
{
auto lightComponent = m_pCurrentSceneWorld->GetLightComponent(lightEntities[lightIndex]);
cd::LightType lightType = lightComponent->GetType();

constexpr StringCrc CastShadowIntensityCrc(IsCastShadow);
if (lightComponent->IsCastShadow())
{
cd::Vec4f vec4 = cd::Vec4f::One();
GetRenderContext()->FillUniform(CastShadowIntensityCrc, &vec4);
}
else
{
cd::Vec4f vec4 = cd::Vec4f::Zero();
GetRenderContext()->FillUniform(CastShadowIntensityCrc, &vec4);
}
//GetRenderContext()->FillUniform(CastShadowIntensityCrc, &lightComponent->IsCastShadow());
if (cd::LightType::Directional == lightType)
{
bgfx::TextureHandle blitDstShadowMapTexture = static_cast<bgfx::TextureHandle>(lightComponent->GetShadowMapTexture());
Expand Down

0 comments on commit 7c5babd

Please sign in to comment.