Skip to content

Commit

Permalink
Expose shadow texture size for directional lighting in SDF (#633)
Browse files Browse the repository at this point in the history
Signed-off-by: Athena Z <athenaz@google.com>
Signed-off-by: Athena Z. <athenaz@google.com>
Co-authored-by: Ian Chen <ichen@openrobotics.org>
  • Loading branch information
athenaz2 and iche033 authored Aug 21, 2024
1 parent 26d8be0 commit 96098d6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/plugins/minimal_scene/MinimalScene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,16 @@ std::string GzRenderer::Initialize(RenderThreadRhi &_rhi)
scene->SetSkyEnabled(true);
}

if (!scene->SetShadowTextureSize(rendering::LightType::DIRECTIONAL,
this->directionalLightTextureSize))
{
gzerr << "Unable to set directional light shadow <texture_size> to '"
<< this->directionalLightTextureSize
<< "'. Using default texture size of "
<< scene->ShadowTextureSize(rendering::LightType::DIRECTIONAL)
<< std::endl;
}

auto root = scene->RootVisual();

// Camera
Expand Down Expand Up @@ -1321,6 +1331,20 @@ void RenderWindowItem::SetSkyEnabled(const bool &_sky)
this->dataPtr->renderThread->gzRenderer.skyEnable = _sky;
}

/////////////////////////////////////////////////
bool RenderWindowItem::SetShadowTextureSize(rendering::LightType _lightType,
unsigned int _textureSize)
{
if (_lightType == rendering::LightType::DIRECTIONAL)
{
this->dataPtr->renderThread->gzRenderer.directionalLightTextureSize =
_textureSize;
return true;
}

return false;
}

/////////////////////////////////////////////////
void RenderWindowItem::SetGraphicsAPI(
const rendering::GraphicsAPI &_graphicsAPI)
Expand Down Expand Up @@ -1464,6 +1488,44 @@ void MinimalScene::LoadConfig(const tinyxml2::XMLElement *_pluginElem)
<< std::endl;
}

elem = _pluginElem->FirstChildElement("shadows");
if (nullptr != elem && !elem->NoChildren())
{
auto textureSizeElem = elem->FirstChildElement("texture_size");
if (nullptr != elem && nullptr != textureSizeElem->GetText())
{
unsigned int texSize;
std::stringstream texSizeStr;
texSizeStr << std::string(textureSizeElem->GetText());
texSizeStr >> texSize;
if (texSizeStr.fail())
{
gzerr << "Unable to set shadow <texture_size> to '"
<< texSizeStr.str()
<< "' using default texture size" << std::endl;
}
else
{
std::string lightType = textureSizeElem->Attribute("light_type");
if (lightType == "directional")
{
if (!renderWindow->SetShadowTextureSize(
rendering::LightType::DIRECTIONAL, texSize))
{
gzerr << "Unable to set shadow <texture_size> to '"
<< texSizeStr.str()
<< "' using default texture size" << std::endl;
}
}
else
{
gzerr << "Setting shadow <texture_size> for light type ["
<< lightType << "] is not supported." << std::endl;
}
}
}
}

const std::string backendApiName = gz::gui::renderEngineBackendApiName();
if (backendApiName == "vulkan")
{
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/minimal_scene/MinimalScene.hh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <gz/math/Vector2.hh>
#include <gz/utils/ImplPtr.hh>
#include <gz/rendering/GraphicsAPI.hh>
#include <gz/rendering/Light.hh>

#include "gz/gui/Plugin.hh"

Expand Down Expand Up @@ -238,6 +239,9 @@ namespace gz::gui::plugins
/// \brief True if sky is enabled;
public: bool skyEnable = false;

/// \brief Shadow texture size for directional light
public: unsigned int directionalLightTextureSize = 2048u;

/// \brief Horizontal FOV of the camera;
public: math::Angle cameraHFOV = math::Angle(M_PI * 0.5);

Expand Down Expand Up @@ -368,6 +372,12 @@ namespace gz::gui::plugins
/// \param[in] _sky True to enable the sky, false otherwise.
public: void SetSkyEnabled(const bool &_sky);

/// \brief Set the shadow texture size for the given light type.
/// \param _lightType Light type that creates the shadow
/// \param _textureSize Shadow texture size
public: bool SetShadowTextureSize(rendering::LightType _lightType,
unsigned int _textureSize);

/// \brief Set the Horizontal FOV of the camera
/// \param[in] _fov FOV of the camera in degree
public: void SetCameraHFOV(const math::Angle &_fov);
Expand Down
8 changes: 8 additions & 0 deletions test/integration/minimal_scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ TEST(MinimalSceneTest, GZ_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Config))
" <near>0.1</near>"
" <far>5000</far>"
"</camera_clip>"
"<shadows>"
" <texture_size light_type=\"directional\">8192</texture_size>"
"</shadows>"
"<horizontal_fov>60</horizontal_fov>"
"<view_controller>ortho</view_controller>"
"</plugin>";
Expand Down Expand Up @@ -131,6 +134,11 @@ TEST(MinimalSceneTest, GZ_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Config))
EXPECT_DOUBLE_EQ(0.1, camera->NearClipPlane());
EXPECT_DOUBLE_EQ(5000.0, camera->FarClipPlane());

EXPECT_EQ(8192u, scene->ShadowTextureSize(
rendering::LightType::DIRECTIONAL));
EXPECT_EQ(2048u, scene->ShadowTextureSize(rendering::LightType::SPOT));
EXPECT_EQ(2048u, scene->ShadowTextureSize(rendering::LightType::POINT));

EXPECT_NEAR(60, camera->HFOV().Degree(), 1e-4);

EXPECT_EQ(rendering::CameraProjectionType::CPT_ORTHOGRAPHIC,
Expand Down

0 comments on commit 96098d6

Please sign in to comment.