diff --git a/Engine/BuiltInShaders/common/LightSource.sh b/Engine/BuiltInShaders/common/LightSource.sh index 1a19f7ae..d4abfb97 100644 --- a/Engine/BuiltInShaders/common/LightSource.sh +++ b/Engine/BuiltInShaders/common/LightSource.sh @@ -1,8 +1,8 @@ -//-----------------------------------------------------------------------------------------// -// @brief Calculates the contribution of all light sources. // -// // -// vec3 CalculateLights(Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF); // -//-----------------------------------------------------------------------------------------// +//--------------------------------------------------------------------------------------------------------// +// @brief Calculates the contribution of all light sources. // +// // +// vec3 CalculateLights(Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF, float csmDepth) // +//--------------------------------------------------------------------------------------------------------// #include "../UniformDefines/U_Light.sh" #include "../UniformDefines/U_Shadow.sh" diff --git a/Engine/BuiltInShaders/shaders/fs_PBR.sc b/Engine/BuiltInShaders/shaders/fs_PBR.sc index 63e8c8e8..67f27874 100644 --- a/Engine/BuiltInShaders/shaders/fs_PBR.sc +++ b/Engine/BuiltInShaders/shaders/fs_PBR.sc @@ -30,8 +30,8 @@ void main() vec3 cameraPos = GetCamera().position.xyz; vec3 viewDir = normalize(cameraPos - v_worldPos); - // Directional Light - float csmDepth = (v_color0.z - u_cameraNearFarPlane.x)/(u_cameraNearFarPlane.y - u_cameraNearFarPlane.x); + // Directional Light + float csmDepth = (v_color0.z - u_cameraNearFarPlane.x) / (u_cameraNearFarPlane.y - u_cameraNearFarPlane.x); vec3 dirColor = GetDirectional(material, v_worldPos, viewDir, csmDepth); // Environment Light diff --git a/Engine/BuiltInShaders/shaders/fs_terrain.sc b/Engine/BuiltInShaders/shaders/fs_terrain.sc index 75bd9f21..1dbac96b 100644 --- a/Engine/BuiltInShaders/shaders/fs_terrain.sc +++ b/Engine/BuiltInShaders/shaders/fs_terrain.sc @@ -1,5 +1,5 @@ #define IBL -$input v_worldPos, v_normal, v_texcoord0, v_TBN +$input v_worldPos, v_normal, v_texcoord0, v_TBN, v_color0 #include "../common/common.sh" #include "../common/BRDF.sh" @@ -12,14 +12,15 @@ $input v_worldPos, v_normal, v_texcoord0, v_TBN #include "../UniformDefines/U_Terrain.sh" uniform vec4 u_emissiveColor; +uniform vec4 u_cameraNearFarPlane; SAMPLER2D(s_texSnow, TERRAIN_TOP_ALBEDO_MAP_SLOT); SAMPLER2D(s_texRock, TERRAIN_MEDIUM_ALBEDO_MAP_SLOT); SAMPLER2D(s_texGrass, TERRAIN_BOTTOM_ALBEDO_MAP_SLOT); -vec3 GetDirectional(Material material, vec3 worldPos, vec3 viewDir) { +vec3 GetDirectional(Material material, vec3 worldPos, vec3 viewDir, float csmDepth) { vec3 diffuseBRDF = material.albedo * CD_PI_INV; - return CalculateLights(material, worldPos, viewDir, diffuseBRDF); + return CalculateLights(material, worldPos, viewDir, diffuseBRDF, csmDepth); } vec3 GetEnvironment(Material material, vec3 normal, vec3 viewDir) { @@ -74,7 +75,8 @@ void main() vec3 viewDir = normalize(cameraPos - v_worldPos); // Directional Light - vec3 dirColor = GetDirectional(material, v_worldPos, viewDir); + float csmDepth = (v_color0.z - u_cameraNearFarPlane.x) / (u_cameraNearFarPlane.y - u_cameraNearFarPlane.x); + vec3 dirColor = GetDirectional(material, v_worldPos, viewDir, csmDepth); // Environment Light vec3 envColor = GetEnvironment(material, v_normal, viewDir); diff --git a/Engine/BuiltInShaders/shaders/vs_terrain.sc b/Engine/BuiltInShaders/shaders/vs_terrain.sc index ef4b9671..6a124860 100644 --- a/Engine/BuiltInShaders/shaders/vs_terrain.sc +++ b/Engine/BuiltInShaders/shaders/vs_terrain.sc @@ -1,5 +1,5 @@ $input a_position, a_normal, a_tangent, a_texcoord0 -$output v_worldPos, v_normal, v_texcoord0, v_TBN +$output v_worldPos, v_normal, v_texcoord0, v_TBN, v_color0 #include "../common/common.sh" @@ -32,6 +32,7 @@ void main() gl_Position = mul(u_modelViewProj, vec4(a_position.x, elevation, a_position.z, 1.0)); v_worldPos = mul(u_model[0], vec4(a_position.x, elevation, a_position.z, 1.0)).xyz; + v_color0 = mul(u_modelView, vec4(a_position, 1.0)); v_normal = -normalize(N1 + N2 + N3 +N4); vec3 tangent = normalize(mul(u_modelInvTrans, vec4(a_tangent, 0.0)).xyz); diff --git a/Engine/Source/Runtime/Rendering/TerrainRenderer.cpp b/Engine/Source/Runtime/Rendering/TerrainRenderer.cpp index 5938d595..88d91002 100644 --- a/Engine/Source/Runtime/Rendering/TerrainRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/TerrainRenderer.cpp @@ -37,6 +37,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* cameraNearFarPlane = "u_cameraNearFarPlane"; constexpr const char* albedoColor = "u_albedoColor"; constexpr const char* metallicRoughnessFactor = "u_metallicRoughnessFactor"; @@ -76,6 +77,8 @@ void TerrainRenderer::Warmup() GetRenderContext()->CreateTexture(pSkyComponent->GetRadianceTexturePath().c_str(), samplerFlags); GetRenderContext()->CreateUniform(cameraPos, bgfx::UniformType::Vec4, 1); + GetRenderContext()->CreateUniform(cameraNearFarPlane, bgfx::UniformType::Vec4, 1); + GetRenderContext()->CreateUniform(albedoColor, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(emissiveColor, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(metallicRoughnessFactor, bgfx::UniformType::Vec4, 1); @@ -97,7 +100,10 @@ void TerrainRenderer::UpdateView(const float* pViewMatrix, const float* pProject void TerrainRenderer::Render(float deltaTime) { // TODO : Remove it. If every renderer need to submit camera related uniform, it should be done not inside Renderer class. + const CameraComponent* pMainCameraComponent = m_pCurrentSceneWorld->GetCameraComponent(m_pCurrentSceneWorld->GetMainCameraEntity()); const cd::Transform& cameraTransform = m_pCurrentSceneWorld->GetTransformComponent(m_pCurrentSceneWorld->GetMainCameraEntity())->GetTransform(); + SkyComponent* pSkyComponent = m_pCurrentSceneWorld->GetSkyComponent(m_pCurrentSceneWorld->GetSkyEntity()); + for (Entity entity : m_pCurrentSceneWorld->GetTerrainEntities()) { MaterialComponent* pMaterialComponent = m_pCurrentSceneWorld->GetMaterialComponent(entity); @@ -145,14 +151,9 @@ void TerrainRenderer::Render(float deltaTime) GetRenderContext()->GetTexture(StringCrc(elevationTexture))); // Sky - SkyComponent* pSkyComponent = m_pCurrentSceneWorld->GetSkyComponent(m_pCurrentSceneWorld->GetSkyEntity()); SkyType crtSkyType = pSkyComponent->GetSkyType(); - if (crtSkyType == SkyType::SkyBox) { - // Create a new TextureHandle each frame if the skybox texture path has been updated, - // otherwise RenderContext::CreateTexture will automatically skip it. - constexpr StringCrc irrSamplerCrc(cubeIrradianceSampler); GetRenderContext()->CreateTexture(pSkyComponent->GetIrradianceTexturePath().c_str(), samplerFlags); bgfx::setTexture(IBL_IRRADIANCE_SLOT, @@ -174,6 +175,10 @@ void TerrainRenderer::Render(float deltaTime) constexpr StringCrc cameraPosCrc(cameraPos); GetRenderContext()->FillUniform(cameraPosCrc, &cameraTransform.GetTranslation().x(), 1); + constexpr StringCrc cameraNearFarPlaneCrc(cameraNearFarPlane); + float cameraNearFarPlanedata[2] { pMainCameraComponent->GetNearPlane(), pMainCameraComponent->GetFarPlane() }; + GetRenderContext()->FillUniform(cameraNearFarPlaneCrc, cameraNearFarPlanedata, 1); + // Submit uniform values : material settings constexpr StringCrc albedoColorCrc(albedoColor); GetRenderContext()->FillUniform(albedoColorCrc, pMaterialComponent->GetFactor(cd::MaterialPropertyGroup::BaseColor), 1); diff --git a/Engine/Source/Runtime/Rendering/WorldRenderer.cpp b/Engine/Source/Runtime/Rendering/WorldRenderer.cpp index 264497eb..b4ad19f6 100644 --- a/Engine/Source/Runtime/Rendering/WorldRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/WorldRenderer.cpp @@ -89,7 +89,7 @@ void WorldRenderer::Warmup() GetRenderContext()->CreateUniform(LightDir, bgfx::UniformType::Vec4, 1); GetRenderContext()->CreateUniform(HeightOffsetAndshadowLength, bgfx::UniformType::Vec4, 1); - GetRenderContext()->CreateUniform(lightViewProjs, bgfx::UniformType::Mat4, 12); // + GetRenderContext()->CreateUniform(lightViewProjs, bgfx::UniformType::Mat4, 12); GetRenderContext()->CreateUniform(cubeShadowMapSamplers[0], bgfx::UniformType::Sampler); GetRenderContext()->CreateUniform(cubeShadowMapSamplers[1], bgfx::UniformType::Sampler); GetRenderContext()->CreateUniform(cubeShadowMapSamplers[2], bgfx::UniformType::Sampler); @@ -107,6 +107,7 @@ void WorldRenderer::UpdateView(const float* pViewMatrix, const float* pProjectio void WorldRenderer::Render(float deltaTime) { // TODO : Remove it. If every renderer need to submit camera related uniform, it should be done not inside Renderer class. + const CameraComponent* pMainCameraComponent = m_pCurrentSceneWorld->GetCameraComponent(m_pCurrentSceneWorld->GetMainCameraEntity()); const cd::Transform& cameraTransform = m_pCurrentSceneWorld->GetTransformComponent(m_pCurrentSceneWorld->GetMainCameraEntity())->GetTransform(); SkyComponent* pSkyComponent = m_pCurrentSceneWorld->GetSkyComponent(m_pCurrentSceneWorld->GetSkyEntity()); @@ -231,7 +232,7 @@ void WorldRenderer::Render(float deltaTime) if (SkyType::SkyBox == crtSkyType) { // Create a new TextureHandle each frame if the skybox texture path has been updated, - // otherwise RenderContext::CreateTexture will automatically skip it. + // otherwise RenderContext::CreateTexture will skip it automatically. constexpr StringCrc irrSamplerCrc(cubeIrradianceSampler); GetRenderContext()->CreateTexture(pSkyComponent->GetIrradianceTexturePath().c_str(), samplerFlags); @@ -267,6 +268,10 @@ void WorldRenderer::Render(float deltaTime) constexpr StringCrc cameraPosCrc(cameraPos); GetRenderContext()->FillUniform(cameraPosCrc, &cameraTransform.GetTranslation().x(), 1); + constexpr StringCrc cameraNearFarPlaneCrc(cameraNearFarPlane); + float cameraNearFarPlanedata[2]{ pMainCameraComponent->GetNearPlane(), pMainCameraComponent->GetFarPlane() }; + GetRenderContext()->FillUniform(cameraNearFarPlaneCrc, cameraNearFarPlanedata, 1); + // Submit uniform values : material settings constexpr StringCrc albedoColorCrc(albedoColor); GetRenderContext()->FillUniform(albedoColorCrc, pMaterialComponent->GetFactor(cd::MaterialPropertyGroup::BaseColor), 1); @@ -320,12 +325,6 @@ void WorldRenderer::Render(float deltaTime) constexpr engine::StringCrc lightViewProjsCrc(lightViewProjs); GetRenderContext()->FillUniform(lightViewProjsCrc, lightViewProjsData.data(), totalLightViewProjOffset); - // Submit shared uniform (related to camera) - constexpr StringCrc cameraNearFarPlaneCrc(cameraNearFarPlane); - CameraComponent* pMainCameraComponent = m_pCurrentSceneWorld->GetCameraComponent(m_pCurrentSceneWorld->GetMainCameraEntity()); - float cameraNearFarPlanedata[2] = { pMainCameraComponent->GetNearPlane(), pMainCameraComponent->GetFarPlane() }; - GetRenderContext()->FillUniform(cameraNearFarPlaneCrc, &cameraNearFarPlanedata[0], 1); - // Submit shadow map and settings of each light constexpr StringCrc shadowMapSamplerCrcs[3] = { StringCrc(cubeShadowMapSamplers[0]), StringCrc(cubeShadowMapSamplers[1]), StringCrc(cubeShadowMapSamplers[2]) }; for (int lightIndex = 0; lightIndex < lightEntityCount; lightIndex++)