diff --git a/Engine/BuiltInShaders/common/LightSource.sh b/Engine/BuiltInShaders/common/LightSource.sh index 0ca00e5d..5faf3ada 100644 --- a/Engine/BuiltInShaders/common/LightSource.sh +++ b/Engine/BuiltInShaders/common/LightSource.sh @@ -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); @@ -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; } @@ -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; } @@ -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; } diff --git a/Engine/Source/Editor/EditorApp.cpp b/Engine/Source/Editor/EditorApp.cpp index 1acadab0..ad40c564 100644 --- a/Engine/Source/Editor/EditorApp.cpp +++ b/Engine/Source/Editor/EditorApp.cpp @@ -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(); diff --git a/Engine/Source/Editor/UILayers/EntityList.cpp b/Engine/Source/Editor/UILayers/EntityList.cpp index b72e301f..8826c860 100644 --- a/Engine/Source/Editor/UILayers/EntityList.cpp +++ b/Engine/Source/Editor/UILayers/EntityList.cpp @@ -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 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(cd::MaterialPropertyGroup::Metallic, horizontalIndex * (1.0f / 6.0f)); + pMaterialComponent->SetFactor(cd::MaterialPropertyGroup::Roughness, verticalIndex * (1.0f / 6.0f)); + } + } + + } } void EntityList::DrawEntity(engine::SceneWorld* pSceneWorld, engine::Entity entity) diff --git a/Engine/Source/Runtime/ECWorld/MaterialComponent.h b/Engine/Source/Runtime/ECWorld/MaterialComponent.h index a1833dc3..efc3a875 100644 --- a/Engine/Source/Runtime/ECWorld/MaterialComponent.h +++ b/Engine/Source/Runtime/ECWorld/MaterialComponent.h @@ -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 m_propertyGroups; diff --git a/Engine/Source/Runtime/Rendering/ShadowMapRenderer.cpp b/Engine/Source/Runtime/Rendering/ShadowMapRenderer.cpp index 705aee72..198e87f4 100644 --- a/Engine/Source/Runtime/Rendering/ShadowMapRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/ShadowMapRenderer.cpp @@ -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 diff --git a/Engine/Source/Runtime/Rendering/WorldRenderer.cpp b/Engine/Source/Runtime/Rendering/WorldRenderer.cpp index 313205a8..93ba41ab 100644 --- a/Engine/Source/Runtime/Rendering/WorldRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/WorldRenderer.cpp @@ -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"; @@ -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"); } @@ -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(lightComponent->GetShadowMapTexture());