From 46b3542b848bc34845d2eafd24167b94f82b710f Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 16 Dec 2020 15:43:10 -0800 Subject: [PATCH 01/12] testing spotlight shadows Signed-off-by: Ian Chen --- gazebo/rendering/Light.cc | 34 ++++- gazebo/rendering/LightPrivate.hh | 4 +- gazebo/rendering/RTShaderSystem.cc | 35 +++-- media/materials/programs/CMakeLists.txt | 2 + .../programs/spotlight_shadow_test_fp.glsl | 125 +++++++++++++++ .../programs/spotlight_shadow_test_vp.glsl | 36 +++++ media/materials/scripts/CMakeLists.txt | 1 + .../scripts/spotlight_shadow_test.material | 71 +++++++++ worlds/spotlight_shadows.test.world | 144 ++++++++++++++++++ 9 files changed, 432 insertions(+), 20 deletions(-) create mode 100644 media/materials/programs/spotlight_shadow_test_fp.glsl create mode 100644 media/materials/programs/spotlight_shadow_test_vp.glsl create mode 100644 media/materials/scripts/spotlight_shadow_test.material create mode 100644 worlds/spotlight_shadows.test.world diff --git a/gazebo/rendering/Light.cc b/gazebo/rendering/Light.cc index f04f66b04a..7fd6dac147 100644 --- a/gazebo/rendering/Light.cc +++ b/gazebo/rendering/Light.cc @@ -466,6 +466,13 @@ void Light::SetLightType(const std::string &_type) this->CreateVisual(); } +// ////////////////////////////////////////////////// +// void Light::CreateShadowCameraSetup() +// { +// Ogre::LiSPSMShadowCameraSetup setup = new Ogre::LiSPSMShadowCameraSetup(); +// this->dataPtr->shadowCameraSetup = Ogre::ShadowCameraSetupPtr(setup); +// } + ////////////////////////////////////////////////// std::string Light::LightType() const { @@ -584,13 +591,28 @@ void Light::SetRange(const double _range) ////////////////////////////////////////////////// void Light::SetCastShadows(const bool _cast) { - if (this->dataPtr->light->getType() == Ogre::Light::LT_DIRECTIONAL) - { - this->dataPtr->light->setCastShadows(_cast); - } - else + this->dataPtr->light->setCastShadows(_cast); + +// if (this->dataPtr->light->getType() == Ogre::Light::LT_DIRECTIONAL) +// { +// this->dataPtr->light->setCastShadows(_cast); +// } +// else +// { +// this->dataPtr->light->setCastShadows(false); +// } + + + if (_cast && this->dataPtr->light->getType() != + Ogre::Light::LT_DIRECTIONAL && this->dataPtr->shadowCameraSetup.isNull()) { - this->dataPtr->light->setCastShadows(false); + // this->CreateShadowCameraSetup(); + auto *setup = new Ogre::FocusedShadowCameraSetup(); + // auto *setup = new Ogre::DefaultShadowCameraSetup(); + this->dataPtr->shadowCameraSetup = Ogre::ShadowCameraSetupPtr(setup); + this->dataPtr->light->setCustomShadowCameraSetup( + this->dataPtr->shadowCameraSetup); + std::cerr << " using custom shadow camera setup " << std::endl; } } diff --git a/gazebo/rendering/LightPrivate.hh b/gazebo/rendering/LightPrivate.hh index 15253379e5..e19f8cdd41 100644 --- a/gazebo/rendering/LightPrivate.hh +++ b/gazebo/rendering/LightPrivate.hh @@ -38,7 +38,7 @@ namespace gazebo class LightPrivate { /// \brief The ogre light source - public: Ogre::Light *light; + public: Ogre::Light *light = nullptr; /// \brief The visual used to visualize the light. public: VisualPtr visual; @@ -63,6 +63,8 @@ namespace gazebo /// \brief Counter used to generate unique light names. public: static unsigned int lightCounter; + + public: Ogre::ShadowCameraSetupPtr shadowCameraSetup; }; } } diff --git a/gazebo/rendering/RTShaderSystem.cc b/gazebo/rendering/RTShaderSystem.cc index 0ccd59dc7a..7b1e7dc3dc 100644 --- a/gazebo/rendering/RTShaderSystem.cc +++ b/gazebo/rendering/RTShaderSystem.cc @@ -516,9 +516,9 @@ void RTShaderSystem::ApplyShadows(ScenePtr _scene) // 3 textures per directional light sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_DIRECTIONAL, 3); - sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_POINT, 0); - sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_SPOTLIGHT, 0); - sceneMgr->setShadowTextureCount(3); + sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_POINT, 1); + sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_SPOTLIGHT, 1); + sceneMgr->setShadowTextureCount(5); unsigned int texSize = this->dataPtr->shadowTextureSize; #if defined(__APPLE__) @@ -532,22 +532,29 @@ void RTShaderSystem::ApplyShadows(ScenePtr _scene) sceneMgr->setShadowTextureConfig(1, texSize, texSize, Ogre::PF_FLOAT32_R); sceneMgr->setShadowTextureConfig(2, texSize, texSize, Ogre::PF_FLOAT32_R); + // spot and point + sceneMgr->setShadowTextureConfig(3, texSize, texSize, Ogre::PF_FLOAT32_R); + sceneMgr->setShadowTextureConfig(4, texSize, texSize, Ogre::PF_FLOAT32_R); + #if defined(HAVE_OPENGL) // Enable shadow map comparison, so shader can use // float texture(sampler2DShadow, vec3, [float]) instead of // vec4 texture(sampler2D, vec2, [float]). // NVidia, AMD, and Intel all take this as a cue to provide "hardware PCF", // a driver hack that softens shadow edges with 4-sample interpolation. - for (size_t i = 0; i < sceneMgr->getShadowTextureCount(); ++i) - { - const Ogre::TexturePtr tex = sceneMgr->getShadowTexture(i); - // This will fail if not using OpenGL as the rendering backend. - GLuint texId; - tex->getCustomAttribute("GLID", &texId); - glBindTexture(GL_TEXTURE_2D, texId); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, - GL_COMPARE_R_TO_TEXTURE); - } + // for (size_t i = 0; i < sceneMgr->getShadowTextureCount(); ++i) + // TODO set this only for the 3 PSSM shadow textures for directional light + +// for (size_t i = 0u; i < 3u; ++i) +// { +// const Ogre::TexturePtr tex = sceneMgr->getShadowTexture(i); +// // This will fail if not using OpenGL as the rendering backend. +// GLuint texId; +// tex->getCustomAttribute("GLID", &texId); +// glBindTexture(GL_TEXTURE_2D, texId); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, +// GL_COMPARE_R_TO_TEXTURE); +// } #endif sceneMgr->setShadowTextureSelfShadow(false); @@ -559,6 +566,8 @@ void RTShaderSystem::ApplyShadows(ScenePtr _scene) // sceneMgr->setShadowTextureCasterMaterial("PSSM/shadow_caster"); sceneMgr->setShadowTextureCasterMaterial("Gazebo/shadow_caster"); + return; + // Disable fog on the caster pass. // Ogre::MaterialPtr passCaterMaterial = // Ogre::MaterialManager::getSingleton().getByName("PSSM/shadow_caster"); diff --git a/media/materials/programs/CMakeLists.txt b/media/materials/programs/CMakeLists.txt index f63e65c6d8..d0a1cd4fc7 100644 --- a/media/materials/programs/CMakeLists.txt +++ b/media/materials/programs/CMakeLists.txt @@ -1,6 +1,8 @@ add_subdirectory(deferred_rendering) set (files +spotlight_shadow_test_vp.glsl +spotlight_shadow_test_fp.glsl ambient_one_texture_vp.glsl blur.glsl camera_distortion_map_fs.glsl diff --git a/media/materials/programs/spotlight_shadow_test_fp.glsl b/media/materials/programs/spotlight_shadow_test_fp.glsl new file mode 100644 index 0000000000..41a7914ba2 --- /dev/null +++ b/media/materials/programs/spotlight_shadow_test_fp.glsl @@ -0,0 +1,125 @@ +#version 130 + +uniform sampler2D shadowMap0; +//uniform sampler2D shadowMap1; +//uniform sampler2D shadowMap2; +//uniform sampler2D shadowMap3; + +uniform float inverseShadowmapSize0; +//uniform float inverseShadowmapSize1; +//uniform float inverseShadowmapSize2; +//uniform float inverseShadowmapSize3; + +in vec4 lightSpacePos0; +//in vec4 lightSpacePos1; +//in vec4 lightSpacePos2; +//in vec4 lightSpacePos3; + +in vec4 worldPos; +in vec4 worldViewPos; + +out vec4 outputCol; + + +//------------------------------------------------------------------------------ +// A Simple blur function +vec4 Blur(sampler2D map, vec2 uv, const in vec2 offset, float steps, float adjust) +{ + float stepSize = offset.x; + uv.xy -= vec2(stepSize * steps); + + vec4 total = vec4(0.0, 0.0, 0.0, 0.0); + for (float x = 0.0; x < steps; x+=1.0) + for (float y = 0.0; y < steps; y+=1.0) + total += + texture2D(map, vec2(uv.xy + vec2(x * stepSize, y * stepSize))) + adjust; + + return total / (steps * steps); +} + +//------------------------------------------------------------------------------ +// Calculate the shadow factor +float ShadowPCF(in sampler2D shadow_map, in vec4 shadow_map_pos, + const in vec2 offset) +{ + // Old depth calc, using linear distance + //float depth = (vertex_light_pos.z - shadow_depth_range.x) * + // shadow_depth_range.w; + + // Get the shadow map position + shadow_map_pos = shadow_map_pos / shadow_map_pos.w; + vec2 uv = shadow_map_pos.xy; + + float depth_adjust = 0.000001; + vec2 c = Blur(shadow_map, uv, offset, 2.0, depth_adjust).xy; + + // standard variance shadow mapping code + float variance = min(max( c.y - (c.x * c.x), 0.0), 1.0); + float m_d = c.x - shadow_map_pos.z; + float p = variance / (variance + m_d * m_d); + + return smoothstep(0.4, 1.0, shadow_map_pos.z <= c.x ? 1.0 : p); +} + + + +//------------------------------------------------------------------------------ +float ShadowSimple(in sampler2D shadowMap, in vec4 shadowMapPos, + const in vec2 offset) +{ + // perform perspective divide + vec3 shadowMapUV = shadowMapPos.xyz / shadowMapPos.w; + + if (shadowMapUV.z < 0.0 || shadowMapUV.z > 1.0) + return 0.0; + + // get closest depth value from light's perspective + float closestDepth = texture2D(shadowMap, shadowMapUV.xy).r; + + // get depth of current fragment from light's perspective + float currentDepth = shadowMapUV.z; + + // check whether current frag pos is in shadow + float shadow = currentDepth > closestDepth ? 1.0 : 0.0; + + return shadow; +} + + + + +void main() +{ + + vec4 color = vec4(1.0, 0.0, 0.0, 1.0); + // float shadowFactor = ShadowPCF(shadowMap3, lightSpacePos3, vec2(inverseShadowmapSize3)); + + + // float shadowFactor = ShadowSimple(shadowMap3, lightSpacePos3, vec2(inverseShadowmapSize3)); + float shadowFactor = ShadowSimple(shadowMap0, lightSpacePos0, vec2(inverseShadowmapSize0)); + + outputCol = color * (1.0-shadowFactor); + + +// vec4 color = ambient * gl_LightSource[0].ambient; +// +// // normalize both input vectors +// vec3 n = normalize(normal); +// vec3 e = normalize(-position); +// +// vec3 lightDir = normalize(vec3(gl_LightSource[0].position)); +// float NdotL = max(dot(normal, lightDir), 0.0); +// +// // if the vertex is lit compute the specular color +// if (NdotL> 0.0) { +// color += gl_LightSource[0].diffuse * diffuse * NdotL; +// // compute the half vector +// // vec3 halfVector = normalize(lightDir + e); +// vec3 halfVector = normalize(gl_LightSource[0].halfVector.xyz); +// // add specular +// float NdotH = max(dot(n, halfVector), 0.0); +// float shininess = 1.0; +// color += gl_LightSource[0].specular * specular * pow(NdotH, shininess); +// } + +} diff --git a/media/materials/programs/spotlight_shadow_test_vp.glsl b/media/materials/programs/spotlight_shadow_test_vp.glsl new file mode 100644 index 0000000000..677057e5bc --- /dev/null +++ b/media/materials/programs/spotlight_shadow_test_vp.glsl @@ -0,0 +1,36 @@ +#version 130 + + +in vec4 position; + +uniform mat4 worldMatrix; +uniform mat4 worldViewMatrix; +uniform mat4 viewProjMatrix; + +uniform mat4 texViewProjMatrix0; +//uniform mat4 texViewProjMatrix1; +//uniform mat4 texViewProjMatrix2; +//uniform mat4 texViewProjMatrix3; + +out vec4 lightSpacePos0; +//out vec4 lightSpacePos1; +//out vec4 lightSpacePos2; +//out vec4 lightSpacePos3; + +out vec4 worldPos; +out vec4 worldViewPos; + + +void main() +{ + worldPos = worldMatrix * position; + gl_Position = viewProjMatrix * worldPos; + + worldViewPos = worldViewMatrix * position; + + lightSpacePos0 = texViewProjMatrix0 * worldPos; +// lightSpacePos1 = texViewProjMatrix1 * worldPos; +// lightSpacePos2 = texViewProjMatrix2 * worldPos; +// lightSpacePos3 = texViewProjMatrix3 * worldPos; +} + diff --git a/media/materials/scripts/CMakeLists.txt b/media/materials/scripts/CMakeLists.txt index 9e4200dec6..0a37837891 100644 --- a/media/materials/scripts/CMakeLists.txt +++ b/media/materials/scripts/CMakeLists.txt @@ -1,4 +1,5 @@ set (files +spotlight_shadow_test.material blur.compositor blur.material CreaseShading.compositor diff --git a/media/materials/scripts/spotlight_shadow_test.material b/media/materials/scripts/spotlight_shadow_test.material new file mode 100644 index 0000000000..0971d64c45 --- /dev/null +++ b/media/materials/scripts/spotlight_shadow_test.material @@ -0,0 +1,71 @@ +vertex_program Gazebo/SpotLightShadowVS glsl +{ + source spotlight_shadow_test_vp.glsl + + default_params + { + param_named_auto worldViewMatrix worldview_matrix + param_named_auto viewProjMatrix viewproj_matrix + param_named_auto worldMatrix world_matrix + + param_named_auto texViewProjMatrix0 texture_viewproj_matrix 0 +// param_named_auto texViewProjMatrix1 texture_viewproj_matrix 1 +// param_named_auto texViewProjMatrix2 texture_viewproj_matrix 2 +// param_named_auto texViewProjMatrix3 texture_viewproj_matrix 3 + } +} + +fragment_program Gazebo/SpotLightShadowFS glsl +{ + source spotlight_shadow_test_fp.glsl + + default_params + { + param_named shadowMap0 int 0 +// param_named shadowMap1 int 1 +// param_named shadowMap2 int 2 +// param_named shadowMap3 int 3 + + param_named_auto inverseShadowmapSize0 inverse_texture_size 0 +// param_named_auto inverseShadowmapSize1 inverse_texture_size 1 +// param_named_auto inverseShadowmapSize2 inverse_texture_size 2 +// param_named_auto inverseShadowmapSize3 inverse_texture_size 3 + } +} + +material Gazebo/SpotLightShadowTest +{ + technique + { + pass + { + vertex_program_ref Gazebo/SpotLightShadowVS + { + } + + fragment_program_ref Gazebo/SpotLightShadowFS + { + } + texture_unit shadowMap0 + { + content_type shadow + tex_address_mode clamp + } +// texture_unit shadowMap1 +// { +// content_type shadow +// tex_address_mode clamp +// } +// texture_unit shadowMap2 +// { +// content_type shadow +// tex_address_mode clamp +// } +// texture_unit shadowMap3 +// { +// content_type shadow +// tex_address_mode clamp +// } + } + } +} diff --git a/worlds/spotlight_shadows.test.world b/worlds/spotlight_shadows.test.world new file mode 100644 index 0000000000..00bf5de22a --- /dev/null +++ b/worlds/spotlight_shadows.test.world @@ -0,0 +1,144 @@ + + + + + + model://sun + + + + + true + -3 0 2 0 0 0 + 0.8 0.8 0.8 1 + 0.2 0.2 0.2 1 + + 100 + 0.5 + 0.01 + 0.001 + + 0.5 0. -0.3 + + + + + true + + + + + 0 0 1 + 100 100 + + + + + 0xffff + + + + 100 + 50 + + + + + + false + + + 0 0 1 + 100 100 + + + + + + + + + + + + + 0 0 0.5 0 0 0 + + + + + 1 1 1 + + + + + + + 1 1 1 + + + + + + + + 0 1.5 0.5 0 0 0 + + + + + 0.5 + + + + + + + 0.5 + + + + + + + + + + 0 -1.5 0.5 0 1.5707 0 + + + + + 0.5 + 1.0 + + + + + + + 0.5 + 1.0 + + + + + + + + + + From 40b6d4955bc75f95ac13089980ebf437a9ed2493 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 16 Dec 2020 16:05:32 -0800 Subject: [PATCH 02/12] spotlight shadows working with pssm shadows Signed-off-by: Ian Chen --- gazebo/rendering/RTShaderSystem.cc | 27 ++++++----- .../programs/spotlight_shadow_test_fp.glsl | 22 ++++----- .../programs/spotlight_shadow_test_vp.glsl | 18 +++---- .../scripts/spotlight_shadow_test.material | 48 +++++++++---------- worlds/spotlight_shadows.test.world | 16 +++---- 5 files changed, 66 insertions(+), 65 deletions(-) diff --git a/gazebo/rendering/RTShaderSystem.cc b/gazebo/rendering/RTShaderSystem.cc index 7b1e7dc3dc..8597b5807f 100644 --- a/gazebo/rendering/RTShaderSystem.cc +++ b/gazebo/rendering/RTShaderSystem.cc @@ -520,6 +520,11 @@ void RTShaderSystem::ApplyShadows(ScenePtr _scene) sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_SPOTLIGHT, 1); sceneMgr->setShadowTextureCount(5); +// sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_POINT, 0); +// sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_SPOTLIGHT, 0); +// sceneMgr->setShadowTextureCount(3); + + unsigned int texSize = this->dataPtr->shadowTextureSize; #if defined(__APPLE__) // workaround a weird but on OSX if texture size at 2 and 3 splits are not @@ -545,16 +550,16 @@ void RTShaderSystem::ApplyShadows(ScenePtr _scene) // for (size_t i = 0; i < sceneMgr->getShadowTextureCount(); ++i) // TODO set this only for the 3 PSSM shadow textures for directional light -// for (size_t i = 0u; i < 3u; ++i) -// { -// const Ogre::TexturePtr tex = sceneMgr->getShadowTexture(i); -// // This will fail if not using OpenGL as the rendering backend. -// GLuint texId; -// tex->getCustomAttribute("GLID", &texId); -// glBindTexture(GL_TEXTURE_2D, texId); -// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, -// GL_COMPARE_R_TO_TEXTURE); -// } + for (size_t i = 0u; i < 3u; ++i) + { + const Ogre::TexturePtr tex = sceneMgr->getShadowTexture(i); + // This will fail if not using OpenGL as the rendering backend. + GLuint texId; + tex->getCustomAttribute("GLID", &texId); + glBindTexture(GL_TEXTURE_2D, texId); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, + GL_COMPARE_R_TO_TEXTURE); + } #endif sceneMgr->setShadowTextureSelfShadow(false); @@ -566,8 +571,6 @@ void RTShaderSystem::ApplyShadows(ScenePtr _scene) // sceneMgr->setShadowTextureCasterMaterial("PSSM/shadow_caster"); sceneMgr->setShadowTextureCasterMaterial("Gazebo/shadow_caster"); - return; - // Disable fog on the caster pass. // Ogre::MaterialPtr passCaterMaterial = // Ogre::MaterialManager::getSingleton().getByName("PSSM/shadow_caster"); diff --git a/media/materials/programs/spotlight_shadow_test_fp.glsl b/media/materials/programs/spotlight_shadow_test_fp.glsl index 41a7914ba2..429e676017 100644 --- a/media/materials/programs/spotlight_shadow_test_fp.glsl +++ b/media/materials/programs/spotlight_shadow_test_fp.glsl @@ -1,19 +1,19 @@ #version 130 uniform sampler2D shadowMap0; -//uniform sampler2D shadowMap1; -//uniform sampler2D shadowMap2; -//uniform sampler2D shadowMap3; +uniform sampler2D shadowMap1; +uniform sampler2D shadowMap2; +uniform sampler2D shadowMap3; uniform float inverseShadowmapSize0; -//uniform float inverseShadowmapSize1; -//uniform float inverseShadowmapSize2; -//uniform float inverseShadowmapSize3; +uniform float inverseShadowmapSize1; +uniform float inverseShadowmapSize2; +uniform float inverseShadowmapSize3; in vec4 lightSpacePos0; -//in vec4 lightSpacePos1; -//in vec4 lightSpacePos2; -//in vec4 lightSpacePos3; +in vec4 lightSpacePos1; +in vec4 lightSpacePos2; +in vec4 lightSpacePos3; in vec4 worldPos; in vec4 worldViewPos; @@ -95,8 +95,8 @@ void main() // float shadowFactor = ShadowPCF(shadowMap3, lightSpacePos3, vec2(inverseShadowmapSize3)); - // float shadowFactor = ShadowSimple(shadowMap3, lightSpacePos3, vec2(inverseShadowmapSize3)); - float shadowFactor = ShadowSimple(shadowMap0, lightSpacePos0, vec2(inverseShadowmapSize0)); + float shadowFactor = ShadowSimple(shadowMap3, lightSpacePos3, vec2(inverseShadowmapSize3)); + // float shadowFactor = ShadowSimple(shadowMap0, lightSpacePos0, vec2(inverseShadowmapSize0)); outputCol = color * (1.0-shadowFactor); diff --git a/media/materials/programs/spotlight_shadow_test_vp.glsl b/media/materials/programs/spotlight_shadow_test_vp.glsl index 677057e5bc..322bb506d2 100644 --- a/media/materials/programs/spotlight_shadow_test_vp.glsl +++ b/media/materials/programs/spotlight_shadow_test_vp.glsl @@ -8,14 +8,14 @@ uniform mat4 worldViewMatrix; uniform mat4 viewProjMatrix; uniform mat4 texViewProjMatrix0; -//uniform mat4 texViewProjMatrix1; -//uniform mat4 texViewProjMatrix2; -//uniform mat4 texViewProjMatrix3; +uniform mat4 texViewProjMatrix1; +uniform mat4 texViewProjMatrix2; +uniform mat4 texViewProjMatrix3; out vec4 lightSpacePos0; -//out vec4 lightSpacePos1; -//out vec4 lightSpacePos2; -//out vec4 lightSpacePos3; +out vec4 lightSpacePos1; +out vec4 lightSpacePos2; +out vec4 lightSpacePos3; out vec4 worldPos; out vec4 worldViewPos; @@ -29,8 +29,8 @@ void main() worldViewPos = worldViewMatrix * position; lightSpacePos0 = texViewProjMatrix0 * worldPos; -// lightSpacePos1 = texViewProjMatrix1 * worldPos; -// lightSpacePos2 = texViewProjMatrix2 * worldPos; -// lightSpacePos3 = texViewProjMatrix3 * worldPos; + lightSpacePos1 = texViewProjMatrix1 * worldPos; + lightSpacePos2 = texViewProjMatrix2 * worldPos; + lightSpacePos3 = texViewProjMatrix3 * worldPos; } diff --git a/media/materials/scripts/spotlight_shadow_test.material b/media/materials/scripts/spotlight_shadow_test.material index 0971d64c45..7369ec954c 100644 --- a/media/materials/scripts/spotlight_shadow_test.material +++ b/media/materials/scripts/spotlight_shadow_test.material @@ -9,9 +9,9 @@ vertex_program Gazebo/SpotLightShadowVS glsl param_named_auto worldMatrix world_matrix param_named_auto texViewProjMatrix0 texture_viewproj_matrix 0 -// param_named_auto texViewProjMatrix1 texture_viewproj_matrix 1 -// param_named_auto texViewProjMatrix2 texture_viewproj_matrix 2 -// param_named_auto texViewProjMatrix3 texture_viewproj_matrix 3 + param_named_auto texViewProjMatrix1 texture_viewproj_matrix 1 + param_named_auto texViewProjMatrix2 texture_viewproj_matrix 2 + param_named_auto texViewProjMatrix3 texture_viewproj_matrix 3 } } @@ -22,14 +22,14 @@ fragment_program Gazebo/SpotLightShadowFS glsl default_params { param_named shadowMap0 int 0 -// param_named shadowMap1 int 1 -// param_named shadowMap2 int 2 -// param_named shadowMap3 int 3 + param_named shadowMap1 int 1 + param_named shadowMap2 int 2 + param_named shadowMap3 int 3 param_named_auto inverseShadowmapSize0 inverse_texture_size 0 -// param_named_auto inverseShadowmapSize1 inverse_texture_size 1 -// param_named_auto inverseShadowmapSize2 inverse_texture_size 2 -// param_named_auto inverseShadowmapSize3 inverse_texture_size 3 + param_named_auto inverseShadowmapSize1 inverse_texture_size 1 + param_named_auto inverseShadowmapSize2 inverse_texture_size 2 + param_named_auto inverseShadowmapSize3 inverse_texture_size 3 } } @@ -51,21 +51,21 @@ material Gazebo/SpotLightShadowTest content_type shadow tex_address_mode clamp } -// texture_unit shadowMap1 -// { -// content_type shadow -// tex_address_mode clamp -// } -// texture_unit shadowMap2 -// { -// content_type shadow -// tex_address_mode clamp -// } -// texture_unit shadowMap3 -// { -// content_type shadow -// tex_address_mode clamp -// } + texture_unit shadowMap1 + { + content_type shadow + tex_address_mode clamp + } + texture_unit shadowMap2 + { + content_type shadow + tex_address_mode clamp + } + texture_unit shadowMap3 + { + content_type shadow + tex_address_mode clamp + } } } } diff --git a/worlds/spotlight_shadows.test.world b/worlds/spotlight_shadows.test.world index 00bf5de22a..2575ba42b5 100644 --- a/worlds/spotlight_shadows.test.world +++ b/worlds/spotlight_shadows.test.world @@ -1,16 +1,15 @@ - + model://sun - true -3 0 2 0 0 0 @@ -22,18 +21,19 @@ 0.01 0.001 - 0.5 0. -0.3 + 0.5 0.0 -0.3 - + + 0 0 0.1 0 0 0 true 0 0 1 - 100 100 + 10 10 @@ -53,14 +53,13 @@ 0 0 1 - 100 100 + 10 10 @@ -68,7 +67,6 @@ - 0 0 0.5 0 0 0 From 649d4c11176149e65ce35633efbcccfb31e9dcd7 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 23 Dec 2020 17:35:12 -0800 Subject: [PATCH 03/12] multiple spot light shadows working Signed-off-by: Ian Chen --- gazebo/rendering/Light.cc | 33 +-- gazebo/rendering/Light.hh | 1 + gazebo/rendering/RTShaderSystem.cc | 193 ++++++++++++------ gazebo/rendering/RTShaderSystem.hh | 7 + gazebo/rendering/RTShaderSystemPrivate.hh | 3 + media/materials/programs/CMakeLists.txt | 4 + .../multiple_spotlight_shadow_test_fp.glsl | 95 +++++++++ ...multiple_spotlight_shadow_test_fp.glsl.erb | 75 +++++++ .../multiple_spotlight_shadow_test_vp.glsl | 57 ++++++ ...multiple_spotlight_shadow_test_vp.glsl.erb | 53 +++++ media/materials/scripts/CMakeLists.txt | 1 + .../multiple_spotlight_shadow_test.material | 117 +++++++++++ ...ultiple_spotlight_shadow_test.material.erb | 99 +++++++++ 13 files changed, 650 insertions(+), 88 deletions(-) create mode 100644 media/materials/programs/multiple_spotlight_shadow_test_fp.glsl create mode 100644 media/materials/programs/multiple_spotlight_shadow_test_fp.glsl.erb create mode 100644 media/materials/programs/multiple_spotlight_shadow_test_vp.glsl create mode 100644 media/materials/programs/multiple_spotlight_shadow_test_vp.glsl.erb create mode 100644 media/materials/scripts/multiple_spotlight_shadow_test.material create mode 100644 media/materials/scripts/multiple_spotlight_shadow_test.material.erb diff --git a/gazebo/rendering/Light.cc b/gazebo/rendering/Light.cc index 7fd6dac147..649603a75e 100644 --- a/gazebo/rendering/Light.cc +++ b/gazebo/rendering/Light.cc @@ -466,13 +466,6 @@ void Light::SetLightType(const std::string &_type) this->CreateVisual(); } -// ////////////////////////////////////////////////// -// void Light::CreateShadowCameraSetup() -// { -// Ogre::LiSPSMShadowCameraSetup setup = new Ogre::LiSPSMShadowCameraSetup(); -// this->dataPtr->shadowCameraSetup = Ogre::ShadowCameraSetupPtr(setup); -// } - ////////////////////////////////////////////////// std::string Light::LightType() const { @@ -593,25 +586,17 @@ void Light::SetCastShadows(const bool _cast) { this->dataPtr->light->setCastShadows(_cast); -// if (this->dataPtr->light->getType() == Ogre::Light::LT_DIRECTIONAL) -// { -// this->dataPtr->light->setCastShadows(_cast); -// } -// else -// { -// this->dataPtr->light->setCastShadows(false); -// } - - if (_cast && this->dataPtr->light->getType() != - Ogre::Light::LT_DIRECTIONAL && this->dataPtr->shadowCameraSetup.isNull()) + Ogre::Light::LT_DIRECTIONAL) { - // this->CreateShadowCameraSetup(); - auto *setup = new Ogre::FocusedShadowCameraSetup(); - // auto *setup = new Ogre::DefaultShadowCameraSetup(); - this->dataPtr->shadowCameraSetup = Ogre::ShadowCameraSetupPtr(setup); - this->dataPtr->light->setCustomShadowCameraSetup( - this->dataPtr->shadowCameraSetup); + if (this->dataPtr->shadowCameraSetup.isNull()) + { + auto *setup = new Ogre::FocusedShadowCameraSetup(); + this->dataPtr->shadowCameraSetup = Ogre::ShadowCameraSetupPtr(setup); + this->dataPtr->light->setCustomShadowCameraSetup( + this->dataPtr->shadowCameraSetup); + } + RTShaderSystem::Instance()->UpdateShadows(); std::cerr << " using custom shadow camera setup " << std::endl; } } diff --git a/gazebo/rendering/Light.hh b/gazebo/rendering/Light.hh index 1c4b52c4c1..56b3c98c5d 100644 --- a/gazebo/rendering/Light.hh +++ b/gazebo/rendering/Light.hh @@ -26,6 +26,7 @@ #include "gazebo/msgs/msgs.hh" #include "gazebo/rendering/RenderTypes.hh" +#include "gazebo/rendering/RTShaderSystem.hh" #include "gazebo/common/Color.hh" #include "gazebo/util/system.hh" diff --git a/gazebo/rendering/RTShaderSystem.cc b/gazebo/rendering/RTShaderSystem.cc index 8597b5807f..d3da214bd8 100644 --- a/gazebo/rendering/RTShaderSystem.cc +++ b/gazebo/rendering/RTShaderSystem.cc @@ -40,6 +40,7 @@ #include "gazebo/common/Profiler.hh" #include "gazebo/rendering/ogre_gazebo.h" #include "gazebo/rendering/CustomPSSMShadowCameraSetup.hh" +#include "gazebo/rendering/Light.hh" #include "gazebo/rendering/RenderEngine.hh" #include "gazebo/rendering/Scene.hh" #include "gazebo/rendering/Visual.hh" @@ -512,64 +513,7 @@ void RTShaderSystem::ApplyShadows(ScenePtr _scene) this->dataPtr->shaderGenerator->getRenderState(_scene->Name() + Ogre::RTShader::ShaderGenerator::DEFAULT_SCHEME_NAME); - sceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_TEXTURE_ADDITIVE_INTEGRATED); - - // 3 textures per directional light - sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_DIRECTIONAL, 3); - sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_POINT, 1); - sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_SPOTLIGHT, 1); - sceneMgr->setShadowTextureCount(5); - -// sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_POINT, 0); -// sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_SPOTLIGHT, 0); -// sceneMgr->setShadowTextureCount(3); - - - unsigned int texSize = this->dataPtr->shadowTextureSize; -#if defined(__APPLE__) - // workaround a weird but on OSX if texture size at 2 and 3 splits are not - // halved - texSize = this->dataPtr->shadowTextureSize/2; -#endif - sceneMgr->setShadowTextureConfig(0, - this->dataPtr->shadowTextureSize, this->dataPtr->shadowTextureSize, - Ogre::PF_FLOAT32_R); - sceneMgr->setShadowTextureConfig(1, texSize, texSize, Ogre::PF_FLOAT32_R); - sceneMgr->setShadowTextureConfig(2, texSize, texSize, Ogre::PF_FLOAT32_R); - - // spot and point - sceneMgr->setShadowTextureConfig(3, texSize, texSize, Ogre::PF_FLOAT32_R); - sceneMgr->setShadowTextureConfig(4, texSize, texSize, Ogre::PF_FLOAT32_R); - -#if defined(HAVE_OPENGL) - // Enable shadow map comparison, so shader can use - // float texture(sampler2DShadow, vec3, [float]) instead of - // vec4 texture(sampler2D, vec2, [float]). - // NVidia, AMD, and Intel all take this as a cue to provide "hardware PCF", - // a driver hack that softens shadow edges with 4-sample interpolation. - // for (size_t i = 0; i < sceneMgr->getShadowTextureCount(); ++i) - // TODO set this only for the 3 PSSM shadow textures for directional light - - for (size_t i = 0u; i < 3u; ++i) - { - const Ogre::TexturePtr tex = sceneMgr->getShadowTexture(i); - // This will fail if not using OpenGL as the rendering backend. - GLuint texId; - tex->getCustomAttribute("GLID", &texId); - glBindTexture(GL_TEXTURE_2D, texId); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, - GL_COMPARE_R_TO_TEXTURE); - } -#endif - - sceneMgr->setShadowTextureSelfShadow(false); - sceneMgr->setShadowCasterRenderBackFaces(true); - - // TODO: We have two different shadow caster materials, both taken from - // OGRE samples. They should be compared and tested. - // Set up caster material - this is just a standard depth/shadow map caster - // sceneMgr->setShadowTextureCasterMaterial("PSSM/shadow_caster"); - sceneMgr->setShadowTextureCasterMaterial("Gazebo/shadow_caster"); + this->UpdateShadows(_scene); // Disable fog on the caster pass. // Ogre::MaterialPtr passCaterMaterial = @@ -648,20 +592,32 @@ Ogre::PSSMShadowCameraSetup *RTShaderSystem::GetPSSMShadowCameraSetup() const void RTShaderSystem::Update() { GZ_PROFILE("rendering::RTShaderSystem::Update"); - if (!this->dataPtr->initialized || !this->dataPtr->updateShaders) + if (!this->dataPtr->initialized) { return; } - for (const auto &scene : this->dataPtr->scenes) + if (this->dataPtr->updateShadows) { - VisualPtr vis = scene->WorldVisual(); - if (vis) + for (const auto &scene : this->dataPtr->scenes) { - this->UpdateShaders(vis); + this->UpdateShadows(scene); } + this->dataPtr->updateShadows = false; + } + + if (this->dataPtr->updateShaders) + { + for (const auto &scene : this->dataPtr->scenes) + { + VisualPtr vis = scene->WorldVisual(); + if (vis) + { + this->UpdateShaders(vis); + } + } + this->dataPtr->updateShaders = false; } - this->dataPtr->updateShaders = false; } ///////////////////////////////////////////////// @@ -729,3 +685,112 @@ double RTShaderSystem::ShadowSplitPadding() const { return this->dataPtr->shadowSplitPadding; } + +///////////////////////////////////////////////// +void RTShaderSystem::UpdateShadows() +{ + this->dataPtr->updateShadows = true; +} + +///////////////////////////////////////////////// +void RTShaderSystem::UpdateShadows(ScenePtr _scene) +{ + if (!this->dataPtr->initialized) + return; + + Ogre::SceneManager *sceneMgr = _scene->OgreSceneManager(); + + sceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_TEXTURE_ADDITIVE_INTEGRATED); + + // directional: assume there can only be one dir light and we always create + // the shadow map for the dir light + // spot: update number of shadow textures based on number of shadow casting + // spot lights + // point: not working yet + unsigned int dirLightCount = 1u; + unsigned int spotLightCount = 0u; + std::cerr << " light count " << _scene->LightCount() << std::endl; + for (unsigned int i = 0; i < _scene->LightCount(); ++i) + { + LightPtr light = _scene->LightByIndex(i); + + if (!light->CastShadows()) + continue; + + if (light->Type() == "spot") + spotLightCount++; + } + + // 3 textures per directional light + sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_DIRECTIONAL, 3); + + // spot light shadow count + sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_SPOTLIGHT, 1); + std::cerr << "setting spot light shadows: " << spotLightCount << std::endl; + + // \todo(anyone) make point light shadows work + sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_POINT, 0); + + // \todo(anyone) include point light shadows when it is working + unsigned int dirShadowCount = 3 * dirLightCount; + unsigned int spotShadowCount = spotLightCount; + sceneMgr->setShadowTextureCount(dirShadowCount + spotShadowCount); + + unsigned int texSize = this->dataPtr->shadowTextureSize; +#if defined(__APPLE__) + // workaround a weird but on OSX if texture size at 2 and 3 splits are not + // halved + texSize = this->dataPtr->shadowTextureSize/2; +#endif + sceneMgr->setShadowTextureConfig(0, + this->dataPtr->shadowTextureSize, this->dataPtr->shadowTextureSize, + Ogre::PF_FLOAT32_R, 1); + sceneMgr->setShadowTextureConfig(1, texSize, texSize, Ogre::PF_FLOAT32_R, 2); + sceneMgr->setShadowTextureConfig(2, texSize, texSize, Ogre::PF_FLOAT32_R, 3); + +#if defined(HAVE_OPENGL) + // Enable shadow map comparison, so shader can use + // float texture(sampler2DShadow, vec3, [float]) instead of + // vec4 texture(sampler2D, vec2, [float]). + // NVidia, AMD, and Intel all take this as a cue to provide "hardware PCF", + // a driver hack that softens shadow edges with 4-sample interpolation. + // for (size_t i = 0; i < sceneMgr->getShadowTextureCount(); ++i) + // TODO set this only for the 3 PSSM shadow textures for directional light + + for (size_t i = 0u; i < dirShadowCount; ++i) + { + const Ogre::TexturePtr tex = sceneMgr->getShadowTexture(i); + // This will fail if not using OpenGL as the rendering backend. + GLuint texId; + tex->getCustomAttribute("GLID", &texId); + glBindTexture(GL_TEXTURE_2D, texId); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, + GL_COMPARE_R_TO_TEXTURE); + } + GLint maxTexUnits = 0u; + GLint maxCombinedTexUnits = 0u; + glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTexUnits); + glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxCombinedTexUnits); + + unsigned int totalShadowCount = spotShadowCount + dirShadowCount; + if ((totalShadowCount) > static_cast(maxTexUnits)) + { + gzwarn << "The number of shadow textures exceed the max texture unit limit " + << "defined by GL_MAX_TEXTURE_IMAGE_UNITS: " + << totalShadowCount << " shadows vs " + << maxTexUnits << " texture units. Shadows may not work properly" + << std::endl; + } + std::cerr << "max units " << maxTexUnits << " " << maxCombinedTexUnits << std::endl; + +#endif + + sceneMgr->setShadowTextureSelfShadow(false); + sceneMgr->setShadowCasterRenderBackFaces(true); + + // TODO: We have two different shadow caster materials, both taken from + // OGRE samples. They should be compared and tested. + // Set up caster material - this is just a standard depth/shadow map caster + // sceneMgr->setShadowTextureCasterMaterial("PSSM/shadow_caster"); + sceneMgr->setShadowTextureCasterMaterial("Gazebo/shadow_caster"); +} diff --git a/gazebo/rendering/RTShaderSystem.hh b/gazebo/rendering/RTShaderSystem.hh index ffd292ca43..61da4d25ee 100644 --- a/gazebo/rendering/RTShaderSystem.hh +++ b/gazebo/rendering/RTShaderSystem.hh @@ -91,6 +91,9 @@ namespace gazebo /// \brief Queue a call to update the shaders. public: void UpdateShaders(); + /// \brief Queue a call to update the shadows. + public: void UpdateShadows(); + /// \brief Set a viewport to use shaders. /// \param[in] _viewport The viewport to add. /// \param[in] _scene The scene that the viewport uses. @@ -177,6 +180,10 @@ namespace gazebo /// \param[in] _vis Pointer to the visual to update. private: void UpdateShaders(VisualPtr _vis); + /// \brief Update the shadows for a scene + /// \param[in] _scene Pointer to the scene to update + private: void UpdateShadows(ScenePtr _scene); + /// \brief Re-apply shadows. Call this if a shadow paramenter is changed. private: void ReapplyShadows(); diff --git a/gazebo/rendering/RTShaderSystemPrivate.hh b/gazebo/rendering/RTShaderSystemPrivate.hh index 7899d444a3..06b7e63c2a 100644 --- a/gazebo/rendering/RTShaderSystemPrivate.hh +++ b/gazebo/rendering/RTShaderSystemPrivate.hh @@ -56,6 +56,9 @@ namespace gazebo /// \brief Flag to indicate that shaders need to be updated. public: bool updateShaders; + /// \brief Flag to indicate that shaders need to be updated. + public: bool updateShadows = false; + /// \brief Size of the Parallel Split Shadow Map (PSSM) shadow texture /// at closest layer. public: unsigned int shadowTextureSize = 2048u; diff --git a/media/materials/programs/CMakeLists.txt b/media/materials/programs/CMakeLists.txt index d0a1cd4fc7..beaf6dc888 100644 --- a/media/materials/programs/CMakeLists.txt +++ b/media/materials/programs/CMakeLists.txt @@ -3,6 +3,10 @@ add_subdirectory(deferred_rendering) set (files spotlight_shadow_test_vp.glsl spotlight_shadow_test_fp.glsl + +multiple_spotlight_shadow_test_vp.glsl +multiple_spotlight_shadow_test_fp.glsl + ambient_one_texture_vp.glsl blur.glsl camera_distortion_map_fs.glsl diff --git a/media/materials/programs/multiple_spotlight_shadow_test_fp.glsl b/media/materials/programs/multiple_spotlight_shadow_test_fp.glsl new file mode 100644 index 0000000000..44e2384479 --- /dev/null +++ b/media/materials/programs/multiple_spotlight_shadow_test_fp.glsl @@ -0,0 +1,95 @@ +#version 130 + +uniform sampler2D shadowMap0; +uniform sampler2D shadowMap1; +uniform sampler2D shadowMap2; + +uniform float inverseShadowmapSize0; +uniform float inverseShadowmapSize1; +uniform float inverseShadowmapSize2; + +in vec4 lightSpacePos0; +in vec4 lightSpacePos1; +in vec4 lightSpacePos2; + +uniform sampler2D shadowMap3; +uniform float inverseShadowmapSize3; +in vec4 lightSpacePos3; + +uniform sampler2D shadowMap4; +uniform float inverseShadowmapSize4; +in vec4 lightSpacePos4; + +uniform sampler2D shadowMap5; +uniform float inverseShadowmapSize5; +in vec4 lightSpacePos5; + +uniform sampler2D shadowMap6; +uniform float inverseShadowmapSize6; +in vec4 lightSpacePos6; + +uniform sampler2D shadowMap7; +uniform float inverseShadowmapSize7; +in vec4 lightSpacePos7; + +uniform sampler2D shadowMap8; +uniform float inverseShadowmapSize8; +in vec4 lightSpacePos8; + + +in vec4 worldPos; +in vec4 worldViewPos; + +out vec4 outputCol; + + +//------------------------------------------------------------------------------ +float ShadowSimple(in sampler2D shadowMap, in vec4 shadowMapPos, + const in vec2 offset) +{ + // perform perspective divide + vec3 shadowMapUV = shadowMapPos.xyz / shadowMapPos.w; + + if (shadowMapUV.z < 0.0 || shadowMapUV.z > 1.0) + return 0.0; + + // get closest depth value from light's perspective + float closestDepth = texture2D(shadowMap, shadowMapUV.xy).r; + + // get depth of current fragment from light's perspective + float currentDepth = shadowMapUV.z; + + // check whether current frag pos is in shadow + float shadow = currentDepth > closestDepth ? 1.0 : 0.0; + + return shadow; +} + + +void main() +{ + // plane color - red + vec4 color = vec4(1.0, 0.0, 0.0, 1.0); + + float f = 0.0f; +// f += ShadowSimple(shadowMap3, lightSpacePos3, vec2(inverseShadowmapSize3)); +// f = clamp(f, 0.0f, 1.0f); +// f += ShadowSimple(shadowMap4, lightSpacePos4, vec2(inverseShadowmapSize4)); +// f = clamp(f, 0.0f, 1.0f); + f += ShadowSimple(shadowMap5, lightSpacePos5, vec2(inverseShadowmapSize5)); +// f = clamp(f, 0.0f, 1.0f); +// f += ShadowSimple(shadowMap6, lightSpacePos6, vec2(inverseShadowmapSize6)); +// f = clamp(f, 0.0f, 1.0f); +// f += ShadowSimple(shadowMap7, lightSpacePos7, vec2(inverseShadowmapSize7)); +// f = clamp(f, 0.0f, 1.0f); +// f += ShadowSimple(shadowMap8, lightSpacePos8, vec2(inverseShadowmapSize8)); +// f = clamp(f, 0.0f, 1.0f); + + float shadowFactor = f; +// outputCol = color * (1.0-shadowFactor); + + if (abs(length(lightSpacePos3 - lightSpacePos7)) < 0.0001) + outputCol = vec4(1.0, 0.0, 0.0, 1.0); + else + outputCol = vec4(0.0, 1.0, 0.0, 1.0); +} diff --git a/media/materials/programs/multiple_spotlight_shadow_test_fp.glsl.erb b/media/materials/programs/multiple_spotlight_shadow_test_fp.glsl.erb new file mode 100644 index 0000000000..e9c5317b34 --- /dev/null +++ b/media/materials/programs/multiple_spotlight_shadow_test_fp.glsl.erb @@ -0,0 +1,75 @@ +#version 130 + +uniform sampler2D shadowMap0; +uniform sampler2D shadowMap1; +uniform sampler2D shadowMap2; + +uniform float inverseShadowmapSize0; +uniform float inverseShadowmapSize1; +uniform float inverseShadowmapSize2; + +in vec4 lightSpacePos0; +in vec4 lightSpacePos1; +in vec4 lightSpacePos2; + +<% + # number of shadows + $n = 6 + for l in (3..$n+2) +%> +uniform sampler2D shadowMap<%= l.to_s %>; +uniform float inverseShadowmapSize<%= l.to_s %>; +in vec4 lightSpacePos<%= l.to_s %>; + +<% + end +%> + +in vec4 worldPos; +in vec4 worldViewPos; + +out vec4 outputCol; + + +//------------------------------------------------------------------------------ +float ShadowSimple(in sampler2D shadowMap, in vec4 shadowMapPos, + const in vec2 offset) +{ + // perform perspective divide + vec3 shadowMapUV = shadowMapPos.xyz / shadowMapPos.w; + + if (shadowMapUV.z < 0.0 || shadowMapUV.z > 1.0) + return 0.0; + + // get closest depth value from light's perspective + float closestDepth = texture2D(shadowMap, shadowMapUV.xy).r; + + // get depth of current fragment from light's perspective + float currentDepth = shadowMapUV.z; + + // check whether current frag pos is in shadow + float shadow = currentDepth > closestDepth ? 1.0 : 0.0; + + return shadow; +} + + +void main() +{ + // plane color - red + vec4 color = vec4(1.0, 0.0, 0.0, 1.0); + + float f = 0.0f; +<% + # number of shadows + for l in (3..$n+2) +%> + f += ShadowSimple(shadowMap<%= l.to_s %>, lightSpacePos<%= l.to_s %>, vec2(inverseShadowmapSize<%= l.to_s %>)); + f = clamp(f, 0.0f, 1.0f); +<% + end +%> + + float shadowFactor = f; + outputCol = color * (1.0-shadowFactor); +} diff --git a/media/materials/programs/multiple_spotlight_shadow_test_vp.glsl b/media/materials/programs/multiple_spotlight_shadow_test_vp.glsl new file mode 100644 index 0000000000..9f30321160 --- /dev/null +++ b/media/materials/programs/multiple_spotlight_shadow_test_vp.glsl @@ -0,0 +1,57 @@ +#version 130 + +in vec4 position; + +uniform mat4 worldMatrix; +uniform mat4 worldViewMatrix; +uniform mat4 viewProjMatrix; + +uniform mat4 texViewProjMatrix0; +uniform mat4 texViewProjMatrix1; +uniform mat4 texViewProjMatrix2; + +out vec4 lightSpacePos0; +out vec4 lightSpacePos1; +out vec4 lightSpacePos2; + +uniform mat4 texViewProjMatrix3; +out vec4 lightSpacePos3; + +uniform mat4 texViewProjMatrix4; +out vec4 lightSpacePos4; + +uniform mat4 texViewProjMatrix5; +out vec4 lightSpacePos5; + +uniform mat4 texViewProjMatrix6; +out vec4 lightSpacePos6; + +uniform mat4 texViewProjMatrix7; +out vec4 lightSpacePos7; + +uniform mat4 texViewProjMatrix8; +out vec4 lightSpacePos8; + + +out vec4 worldPos; +out vec4 worldViewPos; + +void main() +{ + worldPos = worldMatrix * position; + gl_Position = viewProjMatrix * worldPos; + + worldViewPos = worldViewMatrix * position; + + lightSpacePos0 = texViewProjMatrix0 * worldPos; + lightSpacePos1 = texViewProjMatrix1 * worldPos; + lightSpacePos2 = texViewProjMatrix2 * worldPos; + + lightSpacePos3 = texViewProjMatrix3 * worldPos; + lightSpacePos4 = texViewProjMatrix4 * worldPos; + lightSpacePos5 = texViewProjMatrix5 * worldPos; + lightSpacePos6 = texViewProjMatrix6 * worldPos; + lightSpacePos7 = texViewProjMatrix7 * worldPos; + lightSpacePos8 = texViewProjMatrix8 * worldPos; +} + diff --git a/media/materials/programs/multiple_spotlight_shadow_test_vp.glsl.erb b/media/materials/programs/multiple_spotlight_shadow_test_vp.glsl.erb new file mode 100644 index 0000000000..f5fd97a959 --- /dev/null +++ b/media/materials/programs/multiple_spotlight_shadow_test_vp.glsl.erb @@ -0,0 +1,53 @@ +#version 130 + + +in vec4 position; + +uniform mat4 worldMatrix; +uniform mat4 worldViewMatrix; +uniform mat4 viewProjMatrix; + +uniform mat4 texViewProjMatrix0; +uniform mat4 texViewProjMatrix1; +uniform mat4 texViewProjMatrix2; + +out vec4 lightSpacePos0; +out vec4 lightSpacePos1; +out vec4 lightSpacePos2; + +<% + # number of shadows + $n = 6 + for l in (3..$n+2) +%> +uniform mat4 texViewProjMatrix<%= l.to_s %>; +out vec4 lightSpacePos<%= l.to_s %>; + +<% + end +%> + +out vec4 worldPos; +out vec4 worldViewPos; + +void main() +{ + worldPos = worldMatrix * position; + gl_Position = viewProjMatrix * worldPos; + + worldViewPos = worldViewMatrix * position; + + lightSpacePos0 = texViewProjMatrix0 * worldPos; + lightSpacePos1 = texViewProjMatrix1 * worldPos; + lightSpacePos2 = texViewProjMatrix2 * worldPos; + +<% + # number of shadows + for l in (3..$n+2) +%> + lightSpacePos<%= l.to_s %> = texViewProjMatrix<%= l.to_s %> * worldPos; +<% + end +%> +} + diff --git a/media/materials/scripts/CMakeLists.txt b/media/materials/scripts/CMakeLists.txt index 0a37837891..e79a2ab886 100644 --- a/media/materials/scripts/CMakeLists.txt +++ b/media/materials/scripts/CMakeLists.txt @@ -1,5 +1,6 @@ set (files spotlight_shadow_test.material +multiple_spotlight_shadow_test.material blur.compositor blur.material CreaseShading.compositor diff --git a/media/materials/scripts/multiple_spotlight_shadow_test.material b/media/materials/scripts/multiple_spotlight_shadow_test.material new file mode 100644 index 0000000000..852c6fb2a7 --- /dev/null +++ b/media/materials/scripts/multiple_spotlight_shadow_test.material @@ -0,0 +1,117 @@ +vertex_program Gazebo/MultipleSpotLightShadowVS glsl +{ + source multiple_spotlight_shadow_test_vp.glsl + + default_params + { + param_named_auto worldViewMatrix worldview_matrix + param_named_auto viewProjMatrix viewproj_matrix + param_named_auto worldMatrix world_matrix + + param_named_auto texViewProjMatrix0 texture_viewproj_matrix 0 + param_named_auto texViewProjMatrix1 texture_viewproj_matrix 1 + param_named_auto texViewProjMatrix2 texture_viewproj_matrix 2 + + param_named_auto texViewProjMatrix3 texture_viewproj_matrix 3 + param_named_auto texViewProjMatrix4 texture_viewproj_matrix 4 + param_named_auto texViewProjMatrix5 texture_viewproj_matrix 5 + param_named_auto texViewProjMatrix6 texture_viewproj_matrix 6 + param_named_auto texViewProjMatrix7 texture_viewproj_matrix 7 + param_named_auto texViewProjMatrix8 texture_viewproj_matrix 8 + } +} + +fragment_program Gazebo/MultipleSpotLightShadowFS glsl +{ + source multiple_spotlight_shadow_test_fp.glsl + + default_params + { + param_named shadowMap0 int 0 + param_named shadowMap1 int 1 + param_named shadowMap2 int 2 + + param_named_auto inverseShadowmapSize0 inverse_texture_size 0 + param_named_auto inverseShadowmapSize1 inverse_texture_size 1 + param_named_auto inverseShadowmapSize2 inverse_texture_size 2 + + param_named shadowMap3 int 3 + param_named_auto inverseShadowmapSize3 inverse_texture_size 3 + param_named shadowMap4 int 4 + param_named_auto inverseShadowmapSize4 inverse_texture_size 4 + param_named shadowMap5 int 5 + param_named_auto inverseShadowmapSize5 inverse_texture_size 5 + param_named shadowMap6 int 6 + param_named_auto inverseShadowmapSize6 inverse_texture_size 6 + param_named shadowMap7 int 7 + param_named_auto inverseShadowmapSize7 inverse_texture_size 7 + param_named shadowMap8 int 8 + param_named_auto inverseShadowmapSize8 inverse_texture_size 8 + } +} + +material Gazebo/MultipleSpotLightShadowTest +{ + technique + { + pass + { + // iteration once_per_light + // scene_blend add + // start_light 6 + + vertex_program_ref Gazebo/MultipleSpotLightShadowVS + { + } + + fragment_program_ref Gazebo/MultipleSpotLightShadowFS + { + } + texture_unit shadowMap0 + { + content_type shadow + tex_address_mode clamp + } + texture_unit shadowMap1 + { + content_type shadow + tex_address_mode clamp + } + texture_unit shadowMap2 + { + content_type shadow + tex_address_mode clamp + } + texture_unit shadowMap3 + { + content_type shadow + tex_address_mode clamp + } + texture_unit shadowMap4 + { + content_type shadow + tex_address_mode clamp + } + texture_unit shadowMap5 + { + content_type shadow + tex_address_mode clamp + } + texture_unit shadowMap6 + { + content_type shadow + tex_address_mode clamp + } + texture_unit shadowMap7 + { + content_type shadow + tex_address_mode clamp + } + texture_unit shadowMap8 + { + content_type shadow + tex_address_mode clamp + } + } + } +} diff --git a/media/materials/scripts/multiple_spotlight_shadow_test.material.erb b/media/materials/scripts/multiple_spotlight_shadow_test.material.erb new file mode 100644 index 0000000000..489510d626 --- /dev/null +++ b/media/materials/scripts/multiple_spotlight_shadow_test.material.erb @@ -0,0 +1,99 @@ +vertex_program Gazebo/MultipleSpotLightShadowVS glsl +{ + source multiple_spotlight_shadow_test_vp.glsl + + default_params + { + param_named_auto worldViewMatrix worldview_matrix + param_named_auto viewProjMatrix viewproj_matrix + param_named_auto worldMatrix world_matrix + + param_named_auto texViewProjMatrix0 texture_viewproj_matrix 0 + param_named_auto texViewProjMatrix1 texture_viewproj_matrix 1 + param_named_auto texViewProjMatrix2 texture_viewproj_matrix 2 + +<% + # number of shadows + $n = 6 + for l in (3..$n+2) +%> + param_named_auto texViewProjMatrix<%= l.to_s %> texture_viewproj_matrix <%= l.to_s %> + +<% + end +%> + } +} + +fragment_program Gazebo/MultipleSpotLightShadowFS glsl +{ + source multiple_spotlight_shadow_test_fp.glsl + + default_params + { + param_named shadowMap0 int 0 + param_named shadowMap1 int 1 + param_named shadowMap2 int 2 + + param_named_auto inverseShadowmapSize0 inverse_texture_size 0 + param_named_auto inverseShadowmapSize1 inverse_texture_size 1 + param_named_auto inverseShadowmapSize2 inverse_texture_size 2 + +<% + # number of shadows + for l in (3..$n+2) +%> + param_named shadowMap<%= l.to_s %> int <%= l.to_s %> + + param_named_auto inverseShadowmapSize<%= l.to_s %> inverse_texture_size <%= l.to_s %> + +<% + end +%> + } +} + +material Gazebo/MultipleSpotLightShadowTest +{ + technique + { + pass + { + vertex_program_ref Gazebo/MultipleSpotLightShadowVS + { + } + + fragment_program_ref Gazebo/MultipleSpotLightShadowFS + { + } + texture_unit shadowMap0 + { + content_type shadow + tex_address_mode clamp + } + texture_unit shadowMap1 + { + content_type shadow + tex_address_mode clamp + } + texture_unit shadowMap2 + { + content_type shadow + tex_address_mode clamp + } +<% + # number of shadows + for l in (3..$n+2) +%> + texture_unit shadowMap<%= l.to_s %> + + { + content_type shadow + tex_address_mode clamp + } +<% + end +%> + } + } +} From 0e17c16473a5b6e552e81bc9204f9873c0fde7bd Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 23 Dec 2020 18:28:52 -0800 Subject: [PATCH 04/12] cleanup Signed-off-by: Ian Chen --- gazebo/rendering/LightPrivate.hh | 1 + gazebo/rendering/RTShaderSystem.cc | 18 --- media/materials/programs/CMakeLists.txt | 6 - .../multiple_spotlight_shadow_test_fp.glsl | 95 ------------- ...multiple_spotlight_shadow_test_fp.glsl.erb | 75 ----------- .../multiple_spotlight_shadow_test_vp.glsl | 57 -------- ...multiple_spotlight_shadow_test_vp.glsl.erb | 53 -------- .../programs/spotlight_shadow_test_fp.glsl | 125 ------------------ .../programs/spotlight_shadow_test_vp.glsl | 36 ----- media/materials/scripts/CMakeLists.txt | 2 - 10 files changed, 1 insertion(+), 467 deletions(-) delete mode 100644 media/materials/programs/multiple_spotlight_shadow_test_fp.glsl delete mode 100644 media/materials/programs/multiple_spotlight_shadow_test_fp.glsl.erb delete mode 100644 media/materials/programs/multiple_spotlight_shadow_test_vp.glsl delete mode 100644 media/materials/programs/multiple_spotlight_shadow_test_vp.glsl.erb delete mode 100644 media/materials/programs/spotlight_shadow_test_fp.glsl delete mode 100644 media/materials/programs/spotlight_shadow_test_vp.glsl diff --git a/gazebo/rendering/LightPrivate.hh b/gazebo/rendering/LightPrivate.hh index e19f8cdd41..0162a8c000 100644 --- a/gazebo/rendering/LightPrivate.hh +++ b/gazebo/rendering/LightPrivate.hh @@ -64,6 +64,7 @@ namespace gazebo /// \brief Counter used to generate unique light names. public: static unsigned int lightCounter; + /// \brief Custom shadow camera setup for non-directional lights public: Ogre::ShadowCameraSetupPtr shadowCameraSetup; }; } diff --git a/gazebo/rendering/RTShaderSystem.cc b/gazebo/rendering/RTShaderSystem.cc index d3da214bd8..c28613a15c 100644 --- a/gazebo/rendering/RTShaderSystem.cc +++ b/gazebo/rendering/RTShaderSystem.cc @@ -709,7 +709,6 @@ void RTShaderSystem::UpdateShadows(ScenePtr _scene) // point: not working yet unsigned int dirLightCount = 1u; unsigned int spotLightCount = 0u; - std::cerr << " light count " << _scene->LightCount() << std::endl; for (unsigned int i = 0; i < _scene->LightCount(); ++i) { LightPtr light = _scene->LightByIndex(i); @@ -726,7 +725,6 @@ void RTShaderSystem::UpdateShadows(ScenePtr _scene) // spot light shadow count sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_SPOTLIGHT, 1); - std::cerr << "setting spot light shadows: " << spotLightCount << std::endl; // \todo(anyone) make point light shadows work sceneMgr->setShadowTextureCountPerLightType(Ogre::Light::LT_POINT, 0); @@ -767,22 +765,6 @@ void RTShaderSystem::UpdateShadows(ScenePtr _scene) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); } - GLint maxTexUnits = 0u; - GLint maxCombinedTexUnits = 0u; - glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTexUnits); - glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxCombinedTexUnits); - - unsigned int totalShadowCount = spotShadowCount + dirShadowCount; - if ((totalShadowCount) > static_cast(maxTexUnits)) - { - gzwarn << "The number of shadow textures exceed the max texture unit limit " - << "defined by GL_MAX_TEXTURE_IMAGE_UNITS: " - << totalShadowCount << " shadows vs " - << maxTexUnits << " texture units. Shadows may not work properly" - << std::endl; - } - std::cerr << "max units " << maxTexUnits << " " << maxCombinedTexUnits << std::endl; - #endif sceneMgr->setShadowTextureSelfShadow(false); diff --git a/media/materials/programs/CMakeLists.txt b/media/materials/programs/CMakeLists.txt index beaf6dc888..f63e65c6d8 100644 --- a/media/materials/programs/CMakeLists.txt +++ b/media/materials/programs/CMakeLists.txt @@ -1,12 +1,6 @@ add_subdirectory(deferred_rendering) set (files -spotlight_shadow_test_vp.glsl -spotlight_shadow_test_fp.glsl - -multiple_spotlight_shadow_test_vp.glsl -multiple_spotlight_shadow_test_fp.glsl - ambient_one_texture_vp.glsl blur.glsl camera_distortion_map_fs.glsl diff --git a/media/materials/programs/multiple_spotlight_shadow_test_fp.glsl b/media/materials/programs/multiple_spotlight_shadow_test_fp.glsl deleted file mode 100644 index 44e2384479..0000000000 --- a/media/materials/programs/multiple_spotlight_shadow_test_fp.glsl +++ /dev/null @@ -1,95 +0,0 @@ -#version 130 - -uniform sampler2D shadowMap0; -uniform sampler2D shadowMap1; -uniform sampler2D shadowMap2; - -uniform float inverseShadowmapSize0; -uniform float inverseShadowmapSize1; -uniform float inverseShadowmapSize2; - -in vec4 lightSpacePos0; -in vec4 lightSpacePos1; -in vec4 lightSpacePos2; - -uniform sampler2D shadowMap3; -uniform float inverseShadowmapSize3; -in vec4 lightSpacePos3; - -uniform sampler2D shadowMap4; -uniform float inverseShadowmapSize4; -in vec4 lightSpacePos4; - -uniform sampler2D shadowMap5; -uniform float inverseShadowmapSize5; -in vec4 lightSpacePos5; - -uniform sampler2D shadowMap6; -uniform float inverseShadowmapSize6; -in vec4 lightSpacePos6; - -uniform sampler2D shadowMap7; -uniform float inverseShadowmapSize7; -in vec4 lightSpacePos7; - -uniform sampler2D shadowMap8; -uniform float inverseShadowmapSize8; -in vec4 lightSpacePos8; - - -in vec4 worldPos; -in vec4 worldViewPos; - -out vec4 outputCol; - - -//------------------------------------------------------------------------------ -float ShadowSimple(in sampler2D shadowMap, in vec4 shadowMapPos, - const in vec2 offset) -{ - // perform perspective divide - vec3 shadowMapUV = shadowMapPos.xyz / shadowMapPos.w; - - if (shadowMapUV.z < 0.0 || shadowMapUV.z > 1.0) - return 0.0; - - // get closest depth value from light's perspective - float closestDepth = texture2D(shadowMap, shadowMapUV.xy).r; - - // get depth of current fragment from light's perspective - float currentDepth = shadowMapUV.z; - - // check whether current frag pos is in shadow - float shadow = currentDepth > closestDepth ? 1.0 : 0.0; - - return shadow; -} - - -void main() -{ - // plane color - red - vec4 color = vec4(1.0, 0.0, 0.0, 1.0); - - float f = 0.0f; -// f += ShadowSimple(shadowMap3, lightSpacePos3, vec2(inverseShadowmapSize3)); -// f = clamp(f, 0.0f, 1.0f); -// f += ShadowSimple(shadowMap4, lightSpacePos4, vec2(inverseShadowmapSize4)); -// f = clamp(f, 0.0f, 1.0f); - f += ShadowSimple(shadowMap5, lightSpacePos5, vec2(inverseShadowmapSize5)); -// f = clamp(f, 0.0f, 1.0f); -// f += ShadowSimple(shadowMap6, lightSpacePos6, vec2(inverseShadowmapSize6)); -// f = clamp(f, 0.0f, 1.0f); -// f += ShadowSimple(shadowMap7, lightSpacePos7, vec2(inverseShadowmapSize7)); -// f = clamp(f, 0.0f, 1.0f); -// f += ShadowSimple(shadowMap8, lightSpacePos8, vec2(inverseShadowmapSize8)); -// f = clamp(f, 0.0f, 1.0f); - - float shadowFactor = f; -// outputCol = color * (1.0-shadowFactor); - - if (abs(length(lightSpacePos3 - lightSpacePos7)) < 0.0001) - outputCol = vec4(1.0, 0.0, 0.0, 1.0); - else - outputCol = vec4(0.0, 1.0, 0.0, 1.0); -} diff --git a/media/materials/programs/multiple_spotlight_shadow_test_fp.glsl.erb b/media/materials/programs/multiple_spotlight_shadow_test_fp.glsl.erb deleted file mode 100644 index e9c5317b34..0000000000 --- a/media/materials/programs/multiple_spotlight_shadow_test_fp.glsl.erb +++ /dev/null @@ -1,75 +0,0 @@ -#version 130 - -uniform sampler2D shadowMap0; -uniform sampler2D shadowMap1; -uniform sampler2D shadowMap2; - -uniform float inverseShadowmapSize0; -uniform float inverseShadowmapSize1; -uniform float inverseShadowmapSize2; - -in vec4 lightSpacePos0; -in vec4 lightSpacePos1; -in vec4 lightSpacePos2; - -<% - # number of shadows - $n = 6 - for l in (3..$n+2) -%> -uniform sampler2D shadowMap<%= l.to_s %>; -uniform float inverseShadowmapSize<%= l.to_s %>; -in vec4 lightSpacePos<%= l.to_s %>; - -<% - end -%> - -in vec4 worldPos; -in vec4 worldViewPos; - -out vec4 outputCol; - - -//------------------------------------------------------------------------------ -float ShadowSimple(in sampler2D shadowMap, in vec4 shadowMapPos, - const in vec2 offset) -{ - // perform perspective divide - vec3 shadowMapUV = shadowMapPos.xyz / shadowMapPos.w; - - if (shadowMapUV.z < 0.0 || shadowMapUV.z > 1.0) - return 0.0; - - // get closest depth value from light's perspective - float closestDepth = texture2D(shadowMap, shadowMapUV.xy).r; - - // get depth of current fragment from light's perspective - float currentDepth = shadowMapUV.z; - - // check whether current frag pos is in shadow - float shadow = currentDepth > closestDepth ? 1.0 : 0.0; - - return shadow; -} - - -void main() -{ - // plane color - red - vec4 color = vec4(1.0, 0.0, 0.0, 1.0); - - float f = 0.0f; -<% - # number of shadows - for l in (3..$n+2) -%> - f += ShadowSimple(shadowMap<%= l.to_s %>, lightSpacePos<%= l.to_s %>, vec2(inverseShadowmapSize<%= l.to_s %>)); - f = clamp(f, 0.0f, 1.0f); -<% - end -%> - - float shadowFactor = f; - outputCol = color * (1.0-shadowFactor); -} diff --git a/media/materials/programs/multiple_spotlight_shadow_test_vp.glsl b/media/materials/programs/multiple_spotlight_shadow_test_vp.glsl deleted file mode 100644 index 9f30321160..0000000000 --- a/media/materials/programs/multiple_spotlight_shadow_test_vp.glsl +++ /dev/null @@ -1,57 +0,0 @@ -#version 130 - -in vec4 position; - -uniform mat4 worldMatrix; -uniform mat4 worldViewMatrix; -uniform mat4 viewProjMatrix; - -uniform mat4 texViewProjMatrix0; -uniform mat4 texViewProjMatrix1; -uniform mat4 texViewProjMatrix2; - -out vec4 lightSpacePos0; -out vec4 lightSpacePos1; -out vec4 lightSpacePos2; - -uniform mat4 texViewProjMatrix3; -out vec4 lightSpacePos3; - -uniform mat4 texViewProjMatrix4; -out vec4 lightSpacePos4; - -uniform mat4 texViewProjMatrix5; -out vec4 lightSpacePos5; - -uniform mat4 texViewProjMatrix6; -out vec4 lightSpacePos6; - -uniform mat4 texViewProjMatrix7; -out vec4 lightSpacePos7; - -uniform mat4 texViewProjMatrix8; -out vec4 lightSpacePos8; - - -out vec4 worldPos; -out vec4 worldViewPos; - -void main() -{ - worldPos = worldMatrix * position; - gl_Position = viewProjMatrix * worldPos; - - worldViewPos = worldViewMatrix * position; - - lightSpacePos0 = texViewProjMatrix0 * worldPos; - lightSpacePos1 = texViewProjMatrix1 * worldPos; - lightSpacePos2 = texViewProjMatrix2 * worldPos; - - lightSpacePos3 = texViewProjMatrix3 * worldPos; - lightSpacePos4 = texViewProjMatrix4 * worldPos; - lightSpacePos5 = texViewProjMatrix5 * worldPos; - lightSpacePos6 = texViewProjMatrix6 * worldPos; - lightSpacePos7 = texViewProjMatrix7 * worldPos; - lightSpacePos8 = texViewProjMatrix8 * worldPos; -} - diff --git a/media/materials/programs/multiple_spotlight_shadow_test_vp.glsl.erb b/media/materials/programs/multiple_spotlight_shadow_test_vp.glsl.erb deleted file mode 100644 index f5fd97a959..0000000000 --- a/media/materials/programs/multiple_spotlight_shadow_test_vp.glsl.erb +++ /dev/null @@ -1,53 +0,0 @@ -#version 130 - - -in vec4 position; - -uniform mat4 worldMatrix; -uniform mat4 worldViewMatrix; -uniform mat4 viewProjMatrix; - -uniform mat4 texViewProjMatrix0; -uniform mat4 texViewProjMatrix1; -uniform mat4 texViewProjMatrix2; - -out vec4 lightSpacePos0; -out vec4 lightSpacePos1; -out vec4 lightSpacePos2; - -<% - # number of shadows - $n = 6 - for l in (3..$n+2) -%> -uniform mat4 texViewProjMatrix<%= l.to_s %>; -out vec4 lightSpacePos<%= l.to_s %>; - -<% - end -%> - -out vec4 worldPos; -out vec4 worldViewPos; - -void main() -{ - worldPos = worldMatrix * position; - gl_Position = viewProjMatrix * worldPos; - - worldViewPos = worldViewMatrix * position; - - lightSpacePos0 = texViewProjMatrix0 * worldPos; - lightSpacePos1 = texViewProjMatrix1 * worldPos; - lightSpacePos2 = texViewProjMatrix2 * worldPos; - -<% - # number of shadows - for l in (3..$n+2) -%> - lightSpacePos<%= l.to_s %> = texViewProjMatrix<%= l.to_s %> * worldPos; -<% - end -%> -} - diff --git a/media/materials/programs/spotlight_shadow_test_fp.glsl b/media/materials/programs/spotlight_shadow_test_fp.glsl deleted file mode 100644 index 429e676017..0000000000 --- a/media/materials/programs/spotlight_shadow_test_fp.glsl +++ /dev/null @@ -1,125 +0,0 @@ -#version 130 - -uniform sampler2D shadowMap0; -uniform sampler2D shadowMap1; -uniform sampler2D shadowMap2; -uniform sampler2D shadowMap3; - -uniform float inverseShadowmapSize0; -uniform float inverseShadowmapSize1; -uniform float inverseShadowmapSize2; -uniform float inverseShadowmapSize3; - -in vec4 lightSpacePos0; -in vec4 lightSpacePos1; -in vec4 lightSpacePos2; -in vec4 lightSpacePos3; - -in vec4 worldPos; -in vec4 worldViewPos; - -out vec4 outputCol; - - -//------------------------------------------------------------------------------ -// A Simple blur function -vec4 Blur(sampler2D map, vec2 uv, const in vec2 offset, float steps, float adjust) -{ - float stepSize = offset.x; - uv.xy -= vec2(stepSize * steps); - - vec4 total = vec4(0.0, 0.0, 0.0, 0.0); - for (float x = 0.0; x < steps; x+=1.0) - for (float y = 0.0; y < steps; y+=1.0) - total += - texture2D(map, vec2(uv.xy + vec2(x * stepSize, y * stepSize))) + adjust; - - return total / (steps * steps); -} - -//------------------------------------------------------------------------------ -// Calculate the shadow factor -float ShadowPCF(in sampler2D shadow_map, in vec4 shadow_map_pos, - const in vec2 offset) -{ - // Old depth calc, using linear distance - //float depth = (vertex_light_pos.z - shadow_depth_range.x) * - // shadow_depth_range.w; - - // Get the shadow map position - shadow_map_pos = shadow_map_pos / shadow_map_pos.w; - vec2 uv = shadow_map_pos.xy; - - float depth_adjust = 0.000001; - vec2 c = Blur(shadow_map, uv, offset, 2.0, depth_adjust).xy; - - // standard variance shadow mapping code - float variance = min(max( c.y - (c.x * c.x), 0.0), 1.0); - float m_d = c.x - shadow_map_pos.z; - float p = variance / (variance + m_d * m_d); - - return smoothstep(0.4, 1.0, shadow_map_pos.z <= c.x ? 1.0 : p); -} - - - -//------------------------------------------------------------------------------ -float ShadowSimple(in sampler2D shadowMap, in vec4 shadowMapPos, - const in vec2 offset) -{ - // perform perspective divide - vec3 shadowMapUV = shadowMapPos.xyz / shadowMapPos.w; - - if (shadowMapUV.z < 0.0 || shadowMapUV.z > 1.0) - return 0.0; - - // get closest depth value from light's perspective - float closestDepth = texture2D(shadowMap, shadowMapUV.xy).r; - - // get depth of current fragment from light's perspective - float currentDepth = shadowMapUV.z; - - // check whether current frag pos is in shadow - float shadow = currentDepth > closestDepth ? 1.0 : 0.0; - - return shadow; -} - - - - -void main() -{ - - vec4 color = vec4(1.0, 0.0, 0.0, 1.0); - // float shadowFactor = ShadowPCF(shadowMap3, lightSpacePos3, vec2(inverseShadowmapSize3)); - - - float shadowFactor = ShadowSimple(shadowMap3, lightSpacePos3, vec2(inverseShadowmapSize3)); - // float shadowFactor = ShadowSimple(shadowMap0, lightSpacePos0, vec2(inverseShadowmapSize0)); - - outputCol = color * (1.0-shadowFactor); - - -// vec4 color = ambient * gl_LightSource[0].ambient; -// -// // normalize both input vectors -// vec3 n = normalize(normal); -// vec3 e = normalize(-position); -// -// vec3 lightDir = normalize(vec3(gl_LightSource[0].position)); -// float NdotL = max(dot(normal, lightDir), 0.0); -// -// // if the vertex is lit compute the specular color -// if (NdotL> 0.0) { -// color += gl_LightSource[0].diffuse * diffuse * NdotL; -// // compute the half vector -// // vec3 halfVector = normalize(lightDir + e); -// vec3 halfVector = normalize(gl_LightSource[0].halfVector.xyz); -// // add specular -// float NdotH = max(dot(n, halfVector), 0.0); -// float shininess = 1.0; -// color += gl_LightSource[0].specular * specular * pow(NdotH, shininess); -// } - -} diff --git a/media/materials/programs/spotlight_shadow_test_vp.glsl b/media/materials/programs/spotlight_shadow_test_vp.glsl deleted file mode 100644 index 322bb506d2..0000000000 --- a/media/materials/programs/spotlight_shadow_test_vp.glsl +++ /dev/null @@ -1,36 +0,0 @@ -#version 130 - - -in vec4 position; - -uniform mat4 worldMatrix; -uniform mat4 worldViewMatrix; -uniform mat4 viewProjMatrix; - -uniform mat4 texViewProjMatrix0; -uniform mat4 texViewProjMatrix1; -uniform mat4 texViewProjMatrix2; -uniform mat4 texViewProjMatrix3; - -out vec4 lightSpacePos0; -out vec4 lightSpacePos1; -out vec4 lightSpacePos2; -out vec4 lightSpacePos3; - -out vec4 worldPos; -out vec4 worldViewPos; - - -void main() -{ - worldPos = worldMatrix * position; - gl_Position = viewProjMatrix * worldPos; - - worldViewPos = worldViewMatrix * position; - - lightSpacePos0 = texViewProjMatrix0 * worldPos; - lightSpacePos1 = texViewProjMatrix1 * worldPos; - lightSpacePos2 = texViewProjMatrix2 * worldPos; - lightSpacePos3 = texViewProjMatrix3 * worldPos; -} - diff --git a/media/materials/scripts/CMakeLists.txt b/media/materials/scripts/CMakeLists.txt index e79a2ab886..9e4200dec6 100644 --- a/media/materials/scripts/CMakeLists.txt +++ b/media/materials/scripts/CMakeLists.txt @@ -1,6 +1,4 @@ set (files -spotlight_shadow_test.material -multiple_spotlight_shadow_test.material blur.compositor blur.material CreaseShading.compositor From 1ba06e38e8ca8066bdfe15af7d9b7109306a582c Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Mon, 11 Jan 2021 22:46:42 -0800 Subject: [PATCH 05/12] make custom material work without named constants Signed-off-by: Ian Chen --- gazebo/rendering/Heightmap.cc | 49 ++++++++++++++++++++--------------- gazebo/rendering/Light.cc | 1 - 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/gazebo/rendering/Heightmap.cc b/gazebo/rendering/Heightmap.cc index 3ffc7e797b..a361bb37e7 100644 --- a/gazebo/rendering/Heightmap.cc +++ b/gazebo/rendering/Heightmap.cc @@ -3314,27 +3314,34 @@ Ogre::MaterialPtr TerrainMaterial::Profile::generate( // set up shadow split points in a way that is consistent with the // default ogre terrain material generator - Ogre::PSSMShadowCameraSetup* pssm = - RTShaderSystem::Instance()->GetPSSMShadowCameraSetup(); - unsigned int numTextures = - static_cast(pssm->getSplitCount()); - Ogre::Vector4 splitPoints; - const Ogre::PSSMShadowCameraSetup::SplitPointList& splitPointList = - pssm->getSplitPoints(); - // populate from split point 1 not 0, and include shadowFarDistance - for (unsigned int t = 0u; t < numTextures; ++t) - splitPoints[t] = splitPointList[t+1]; - params->setNamedConstant("pssmSplitPoints", splitPoints); - - // set up uv transform - double xTrans = static_cast(gridCount / gridWidth) * factor; - double yTrans = (gridWidth - 1 - (gridCount % gridWidth)) * factor; - // explicitly set all matrix elements to avoid uninitialized values - Ogre::Matrix4 uvTransform(factor, 0.0, 0.0, xTrans, - 0.0, factor, 0.0, yTrans, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0); - params->setNamedConstant("uvTransform", uvTransform); + + if (params->_findNamedConstantDefinition("pssmSplitPoints")) + { + Ogre::PSSMShadowCameraSetup* pssm = + RTShaderSystem::Instance()->GetPSSMShadowCameraSetup(); + unsigned int numTextures = + static_cast(pssm->getSplitCount()); + Ogre::Vector4 splitPoints; + const Ogre::PSSMShadowCameraSetup::SplitPointList& splitPointList = + pssm->getSplitPoints(); + // populate from split point 1 not 0, and include shadowFarDistance + for (unsigned int t = 0u; t < numTextures; ++t) + splitPoints[t] = splitPointList[t+1]; + params->setNamedConstant("pssmSplitPoints", splitPoints); + } + + if (params->_findNamedConstantDefinition("uvTransform")) + { + // set up uv transform + double xTrans = static_cast(gridCount / gridWidth) * factor; + double yTrans = (gridWidth - 1 - (gridCount % gridWidth)) * factor; + // explicitly set all matrix elements to avoid uninitialized values + Ogre::Matrix4 uvTransform(factor, 0.0, 0.0, xTrans, + 0.0, factor, 0.0, yTrans, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0); + params->setNamedConstant("uvTransform", uvTransform); + } } } gridCount++; diff --git a/gazebo/rendering/Light.cc b/gazebo/rendering/Light.cc index 649603a75e..4484d0fdd0 100644 --- a/gazebo/rendering/Light.cc +++ b/gazebo/rendering/Light.cc @@ -597,7 +597,6 @@ void Light::SetCastShadows(const bool _cast) this->dataPtr->shadowCameraSetup); } RTShaderSystem::Instance()->UpdateShadows(); - std::cerr << " using custom shadow camera setup " << std::endl; } } From 7f5d187333f3ef2e80b0e5838e9d645ba76fe67a Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Tue, 12 Jan 2021 10:43:44 -0800 Subject: [PATCH 06/12] update material and shaders Signed-off-by: Ian Chen --- media/materials/programs/CMakeLists.txt | 2 + .../programs/spotlight_shadow_demo_fp.glsl | 44 ++++++ .../programs/spotlight_shadow_demo_vp.glsl | 24 +++ media/materials/scripts/CMakeLists.txt | 1 + .../multiple_spotlight_shadow_test.material | 117 --------------- ...ultiple_spotlight_shadow_test.material.erb | 99 ------------ .../scripts/spotlight_shadow_demo.material | 48 ++++++ .../scripts/spotlight_shadow_test.material | 71 --------- worlds/spotlight_shadows.test.world | 142 ------------------ worlds/spotlight_shadows_demo.world | 112 ++++++++++++++ 10 files changed, 231 insertions(+), 429 deletions(-) create mode 100644 media/materials/programs/spotlight_shadow_demo_fp.glsl create mode 100644 media/materials/programs/spotlight_shadow_demo_vp.glsl delete mode 100644 media/materials/scripts/multiple_spotlight_shadow_test.material delete mode 100644 media/materials/scripts/multiple_spotlight_shadow_test.material.erb create mode 100644 media/materials/scripts/spotlight_shadow_demo.material delete mode 100644 media/materials/scripts/spotlight_shadow_test.material delete mode 100644 worlds/spotlight_shadows.test.world create mode 100644 worlds/spotlight_shadows_demo.world diff --git a/media/materials/programs/CMakeLists.txt b/media/materials/programs/CMakeLists.txt index f63e65c6d8..5374739d76 100644 --- a/media/materials/programs/CMakeLists.txt +++ b/media/materials/programs/CMakeLists.txt @@ -45,6 +45,8 @@ projector.vert shadow_caster_fp.glsl shadow_caster_vp.glsl StdQuad_vp.glsl +spotlight_shadow_demo_fp.glsl +spotlight_shadow_demo_vp.glsl spot_shadow_receiver_fp.glsl spot_shadow_receiver_vp.glsl wide_lens_map_fp.glsl diff --git a/media/materials/programs/spotlight_shadow_demo_fp.glsl b/media/materials/programs/spotlight_shadow_demo_fp.glsl new file mode 100644 index 0000000000..01731cc2da --- /dev/null +++ b/media/materials/programs/spotlight_shadow_demo_fp.glsl @@ -0,0 +1,44 @@ +#version 130 + +uniform sampler2D shadowMap0; +in vec4 lightSpacePos0; + +in vec4 worldPos; +in vec4 worldViewPos; + +out vec4 outputCol; + +//------------------------------------------------------------------------------ +float ShadowSimple(in sampler2D shadowMap, in vec4 shadowMapPos) +{ + // perform perspective divide + vec3 shadowMapUV = shadowMapPos.xyz / shadowMapPos.w; + + if (shadowMapUV.z < 0.0 || shadowMapUV.z > 1.0) + return 0.0; + + // get closest depth value from light's perspective + float closestDepth = texture2D(shadowMap, shadowMapUV.xy).r; + + // get depth of current fragment from light's perspective + float currentDepth = shadowMapUV.z; + + // check whether current frag pos is in shadow + float shadow = currentDepth > closestDepth ? 1.0 : 0.0; + + return shadow; +} + +void main() +{ + float f = 0.0f; + + // flat red color - no lighting + outputCol = vec4(1.0, 0.0, 0.0, 1.0); + + // grey shadows + f += ShadowSimple(shadowMap0, lightSpacePos0); + f = clamp(f, 0.0f, 1.0f); + if (f > 0.0f) + outputCol = vec4(0.2, 0.2, 0.2, 1.0); +} diff --git a/media/materials/programs/spotlight_shadow_demo_vp.glsl b/media/materials/programs/spotlight_shadow_demo_vp.glsl new file mode 100644 index 0000000000..66dd8231fa --- /dev/null +++ b/media/materials/programs/spotlight_shadow_demo_vp.glsl @@ -0,0 +1,24 @@ +#version 130 + +in vec4 position; + +uniform mat4 worldMatrix; +uniform mat4 worldViewMatrix; +uniform mat4 viewProjMatrix; + +uniform mat4 texViewProjMatrix0; +out vec4 lightSpacePos0; + +out vec4 worldPos; +out vec4 worldViewPos; + +void main() +{ + worldPos = worldMatrix * position; + gl_Position = viewProjMatrix * worldPos; + + worldViewPos = worldViewMatrix * position; + + lightSpacePos0 = texViewProjMatrix0 * worldPos; +} + diff --git a/media/materials/scripts/CMakeLists.txt b/media/materials/scripts/CMakeLists.txt index 9e4200dec6..f72e9f718e 100644 --- a/media/materials/scripts/CMakeLists.txt +++ b/media/materials/scripts/CMakeLists.txt @@ -21,6 +21,7 @@ picker.material ShadowCaster.material shadow_caster.program ShadowCaster.program +spotlight_shadow_demo.material ssao.compositor ssao.material SSAOPost.compositor diff --git a/media/materials/scripts/multiple_spotlight_shadow_test.material b/media/materials/scripts/multiple_spotlight_shadow_test.material deleted file mode 100644 index 852c6fb2a7..0000000000 --- a/media/materials/scripts/multiple_spotlight_shadow_test.material +++ /dev/null @@ -1,117 +0,0 @@ -vertex_program Gazebo/MultipleSpotLightShadowVS glsl -{ - source multiple_spotlight_shadow_test_vp.glsl - - default_params - { - param_named_auto worldViewMatrix worldview_matrix - param_named_auto viewProjMatrix viewproj_matrix - param_named_auto worldMatrix world_matrix - - param_named_auto texViewProjMatrix0 texture_viewproj_matrix 0 - param_named_auto texViewProjMatrix1 texture_viewproj_matrix 1 - param_named_auto texViewProjMatrix2 texture_viewproj_matrix 2 - - param_named_auto texViewProjMatrix3 texture_viewproj_matrix 3 - param_named_auto texViewProjMatrix4 texture_viewproj_matrix 4 - param_named_auto texViewProjMatrix5 texture_viewproj_matrix 5 - param_named_auto texViewProjMatrix6 texture_viewproj_matrix 6 - param_named_auto texViewProjMatrix7 texture_viewproj_matrix 7 - param_named_auto texViewProjMatrix8 texture_viewproj_matrix 8 - } -} - -fragment_program Gazebo/MultipleSpotLightShadowFS glsl -{ - source multiple_spotlight_shadow_test_fp.glsl - - default_params - { - param_named shadowMap0 int 0 - param_named shadowMap1 int 1 - param_named shadowMap2 int 2 - - param_named_auto inverseShadowmapSize0 inverse_texture_size 0 - param_named_auto inverseShadowmapSize1 inverse_texture_size 1 - param_named_auto inverseShadowmapSize2 inverse_texture_size 2 - - param_named shadowMap3 int 3 - param_named_auto inverseShadowmapSize3 inverse_texture_size 3 - param_named shadowMap4 int 4 - param_named_auto inverseShadowmapSize4 inverse_texture_size 4 - param_named shadowMap5 int 5 - param_named_auto inverseShadowmapSize5 inverse_texture_size 5 - param_named shadowMap6 int 6 - param_named_auto inverseShadowmapSize6 inverse_texture_size 6 - param_named shadowMap7 int 7 - param_named_auto inverseShadowmapSize7 inverse_texture_size 7 - param_named shadowMap8 int 8 - param_named_auto inverseShadowmapSize8 inverse_texture_size 8 - } -} - -material Gazebo/MultipleSpotLightShadowTest -{ - technique - { - pass - { - // iteration once_per_light - // scene_blend add - // start_light 6 - - vertex_program_ref Gazebo/MultipleSpotLightShadowVS - { - } - - fragment_program_ref Gazebo/MultipleSpotLightShadowFS - { - } - texture_unit shadowMap0 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap1 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap2 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap3 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap4 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap5 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap6 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap7 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap8 - { - content_type shadow - tex_address_mode clamp - } - } - } -} diff --git a/media/materials/scripts/multiple_spotlight_shadow_test.material.erb b/media/materials/scripts/multiple_spotlight_shadow_test.material.erb deleted file mode 100644 index 489510d626..0000000000 --- a/media/materials/scripts/multiple_spotlight_shadow_test.material.erb +++ /dev/null @@ -1,99 +0,0 @@ -vertex_program Gazebo/MultipleSpotLightShadowVS glsl -{ - source multiple_spotlight_shadow_test_vp.glsl - - default_params - { - param_named_auto worldViewMatrix worldview_matrix - param_named_auto viewProjMatrix viewproj_matrix - param_named_auto worldMatrix world_matrix - - param_named_auto texViewProjMatrix0 texture_viewproj_matrix 0 - param_named_auto texViewProjMatrix1 texture_viewproj_matrix 1 - param_named_auto texViewProjMatrix2 texture_viewproj_matrix 2 - -<% - # number of shadows - $n = 6 - for l in (3..$n+2) -%> - param_named_auto texViewProjMatrix<%= l.to_s %> texture_viewproj_matrix <%= l.to_s %> - -<% - end -%> - } -} - -fragment_program Gazebo/MultipleSpotLightShadowFS glsl -{ - source multiple_spotlight_shadow_test_fp.glsl - - default_params - { - param_named shadowMap0 int 0 - param_named shadowMap1 int 1 - param_named shadowMap2 int 2 - - param_named_auto inverseShadowmapSize0 inverse_texture_size 0 - param_named_auto inverseShadowmapSize1 inverse_texture_size 1 - param_named_auto inverseShadowmapSize2 inverse_texture_size 2 - -<% - # number of shadows - for l in (3..$n+2) -%> - param_named shadowMap<%= l.to_s %> int <%= l.to_s %> - - param_named_auto inverseShadowmapSize<%= l.to_s %> inverse_texture_size <%= l.to_s %> - -<% - end -%> - } -} - -material Gazebo/MultipleSpotLightShadowTest -{ - technique - { - pass - { - vertex_program_ref Gazebo/MultipleSpotLightShadowVS - { - } - - fragment_program_ref Gazebo/MultipleSpotLightShadowFS - { - } - texture_unit shadowMap0 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap1 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap2 - { - content_type shadow - tex_address_mode clamp - } -<% - # number of shadows - for l in (3..$n+2) -%> - texture_unit shadowMap<%= l.to_s %> - - { - content_type shadow - tex_address_mode clamp - } -<% - end -%> - } - } -} diff --git a/media/materials/scripts/spotlight_shadow_demo.material b/media/materials/scripts/spotlight_shadow_demo.material new file mode 100644 index 0000000000..061f1f1c64 --- /dev/null +++ b/media/materials/scripts/spotlight_shadow_demo.material @@ -0,0 +1,48 @@ +vertex_program Gazebo/SpotLightShadowDemoVS glsl +{ + source spotlight_shadow_demo_vp.glsl + + default_params + { + param_named_auto worldViewMatrix worldview_matrix + param_named_auto viewProjMatrix viewproj_matrix + param_named_auto worldMatrix world_matrix + + param_named_auto texViewProjMatrix0 texture_viewproj_matrix 0 + } +} + +fragment_program Gazebo/SpotLightShadowDemoFS glsl +{ + source spotlight_shadow_demo_fp.glsl + + default_params + { + param_named shadowMap0 int 0 + } +} + +material Gazebo/SpotLightShadowDemo +{ + technique + { + pass + { + max_lights 32 + iteration 1 per_n_lights 1 spot + + vertex_program_ref Gazebo/SpotLightShadowDemoVS + { + } + + fragment_program_ref Gazebo/SpotLightShadowDemoFS + { + } + texture_unit shadowMap0 + { + content_type shadow + tex_address_mode clamp + } + } + } +} diff --git a/media/materials/scripts/spotlight_shadow_test.material b/media/materials/scripts/spotlight_shadow_test.material deleted file mode 100644 index 7369ec954c..0000000000 --- a/media/materials/scripts/spotlight_shadow_test.material +++ /dev/null @@ -1,71 +0,0 @@ -vertex_program Gazebo/SpotLightShadowVS glsl -{ - source spotlight_shadow_test_vp.glsl - - default_params - { - param_named_auto worldViewMatrix worldview_matrix - param_named_auto viewProjMatrix viewproj_matrix - param_named_auto worldMatrix world_matrix - - param_named_auto texViewProjMatrix0 texture_viewproj_matrix 0 - param_named_auto texViewProjMatrix1 texture_viewproj_matrix 1 - param_named_auto texViewProjMatrix2 texture_viewproj_matrix 2 - param_named_auto texViewProjMatrix3 texture_viewproj_matrix 3 - } -} - -fragment_program Gazebo/SpotLightShadowFS glsl -{ - source spotlight_shadow_test_fp.glsl - - default_params - { - param_named shadowMap0 int 0 - param_named shadowMap1 int 1 - param_named shadowMap2 int 2 - param_named shadowMap3 int 3 - - param_named_auto inverseShadowmapSize0 inverse_texture_size 0 - param_named_auto inverseShadowmapSize1 inverse_texture_size 1 - param_named_auto inverseShadowmapSize2 inverse_texture_size 2 - param_named_auto inverseShadowmapSize3 inverse_texture_size 3 - } -} - -material Gazebo/SpotLightShadowTest -{ - technique - { - pass - { - vertex_program_ref Gazebo/SpotLightShadowVS - { - } - - fragment_program_ref Gazebo/SpotLightShadowFS - { - } - texture_unit shadowMap0 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap1 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap2 - { - content_type shadow - tex_address_mode clamp - } - texture_unit shadowMap3 - { - content_type shadow - tex_address_mode clamp - } - } - } -} diff --git a/worlds/spotlight_shadows.test.world b/worlds/spotlight_shadows.test.world deleted file mode 100644 index 2575ba42b5..0000000000 --- a/worlds/spotlight_shadows.test.world +++ /dev/null @@ -1,142 +0,0 @@ - - - - - - model://ground_plane - - - - model://sun - - - - true - -3 0 2 0 0 0 - 0.8 0.8 0.8 1 - 0.2 0.2 0.2 1 - - 100 - 0.5 - 0.01 - 0.001 - - 0.5 0.0 -0.3 - - - - - 0 0 0.1 0 0 0 - true - - - - - 0 0 1 - 10 10 - - - - - 0xffff - - - - 100 - 50 - - - - - - false - - - 0 0 1 - 10 10 - - - - - - - - - - - - 0 0 0.5 0 0 0 - - - - - 1 1 1 - - - - - - - 1 1 1 - - - - - - - - 0 1.5 0.5 0 0 0 - - - - - 0.5 - - - - - - - 0.5 - - - - - - - - - - 0 -1.5 0.5 0 1.5707 0 - - - - - 0.5 - 1.0 - - - - - - - 0.5 - 1.0 - - - - - - - - - - diff --git a/worlds/spotlight_shadows_demo.world b/worlds/spotlight_shadows_demo.world new file mode 100644 index 0000000000..ba3da631d5 --- /dev/null +++ b/worlds/spotlight_shadows_demo.world @@ -0,0 +1,112 @@ + + + + + + + + + + model://ground_plane + + + + model://sun + + + + + true + -3.0 0.0 3.0 0 0 0 + 0.8 0.8 0.8 1 + 0.2 0.2 0.2 1 + + 10 + 0.05 + 0.01 + 0.001 + + + 0.1 + 0.5 + 1.2 + + 1 0 -1 + + + + 0 0 0.1 0 0 0 + true + + + + + 0 0 1 + 10 10 + + + + + 0xffff + + + + 100 + 50 + + + + + + false + + + 0 0 1 + 10 10 + + + + + + + + + + + + 0 0 0.5 0 0 0 + + + + + 1 1 1 + + + + + + + 1 1 1 + + + + + + + + + + + From 4326472ad0b0df14d07ffa32ddc117810c3c1f65 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Tue, 12 Jan 2021 10:45:54 -0800 Subject: [PATCH 07/12] remove unused code: Signed-off-by: Ian Chen --- gazebo/rendering/RTShaderSystem.cc | 3 --- 1 file changed, 3 deletions(-) diff --git a/gazebo/rendering/RTShaderSystem.cc b/gazebo/rendering/RTShaderSystem.cc index c28613a15c..76c9db14c3 100644 --- a/gazebo/rendering/RTShaderSystem.cc +++ b/gazebo/rendering/RTShaderSystem.cc @@ -752,9 +752,6 @@ void RTShaderSystem::UpdateShadows(ScenePtr _scene) // vec4 texture(sampler2D, vec2, [float]). // NVidia, AMD, and Intel all take this as a cue to provide "hardware PCF", // a driver hack that softens shadow edges with 4-sample interpolation. - // for (size_t i = 0; i < sceneMgr->getShadowTextureCount(); ++i) - // TODO set this only for the 3 PSSM shadow textures for directional light - for (size_t i = 0u; i < dirShadowCount; ++i) { const Ogre::TexturePtr tex = sceneMgr->getShadowTexture(i); From d6b6f13f7864fb482999f0573b21cc52e1e79dd1 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Tue, 12 Jan 2021 13:18:53 -0800 Subject: [PATCH 08/12] remove include Signed-off-by: Ian Chen --- gazebo/rendering/Light.hh | 1 - 1 file changed, 1 deletion(-) diff --git a/gazebo/rendering/Light.hh b/gazebo/rendering/Light.hh index 56b3c98c5d..1c4b52c4c1 100644 --- a/gazebo/rendering/Light.hh +++ b/gazebo/rendering/Light.hh @@ -26,7 +26,6 @@ #include "gazebo/msgs/msgs.hh" #include "gazebo/rendering/RenderTypes.hh" -#include "gazebo/rendering/RTShaderSystem.hh" #include "gazebo/common/Color.hh" #include "gazebo/util/system.hh" From 0eba2249b5d5e2ceac619cf6f53f9ce04cd0737d Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Tue, 12 Jan 2021 13:20:02 -0800 Subject: [PATCH 09/12] move include Signed-off-by: Ian Chen --- gazebo/rendering/Light.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/gazebo/rendering/Light.cc b/gazebo/rendering/Light.cc index 4484d0fdd0..0ba0640b4e 100644 --- a/gazebo/rendering/Light.cc +++ b/gazebo/rendering/Light.cc @@ -29,6 +29,7 @@ #include "gazebo/rendering/Visual.hh" #include "gazebo/rendering/Light.hh" #include "gazebo/rendering/LightPrivate.hh" +#include "gazebo/rendering/RTShaderSystem.hh" using namespace gazebo; using namespace rendering; From 7aa1d3521b6b5da5f411011b2960656d0ecd2f30 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 13 Jan 2021 17:19:01 -0800 Subject: [PATCH 10/12] disable point light shadows, fix test, mutex Signed-off-by: Ian Chen --- gazebo/rendering/Light.cc | 25 ++++++++++++++++------- gazebo/rendering/RTShaderSystem.cc | 9 +++++--- gazebo/rendering/RTShaderSystemPrivate.hh | 6 +++++- gazebo/rendering/RenderingLight_TEST.cc | 5 +++-- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/gazebo/rendering/Light.cc b/gazebo/rendering/Light.cc index 0ba0640b4e..9752bf9a00 100644 --- a/gazebo/rendering/Light.cc +++ b/gazebo/rendering/Light.cc @@ -585,19 +585,30 @@ void Light::SetRange(const double _range) ////////////////////////////////////////////////// void Light::SetCastShadows(const bool _cast) { - this->dataPtr->light->setCastShadows(_cast); - if (_cast && this->dataPtr->light->getType() != - Ogre::Light::LT_DIRECTIONAL) + if (this->dataPtr->light->getType() == Ogre::Light::LT_DIRECTIONAL) { - if (this->dataPtr->shadowCameraSetup.isNull()) + // directional light uses PSSM shadow camera and should already be + // configured in RTShaderSystem + this->dataPtr->light->setCastShadows(_cast); + } + else if (this->dataPtr->light->getType() == Ogre::Light::LT_SPOTLIGHT) + { + // use different shadow camera for spot light + this->dataPtr->light->setCastShadows(_cast); + if (_cast && this->dataPtr->shadowCameraSetup.isNull()) { - auto *setup = new Ogre::FocusedShadowCameraSetup(); - this->dataPtr->shadowCameraSetup = Ogre::ShadowCameraSetupPtr(setup); + this->dataPtr->shadowCameraSetup = + Ogre::ShadowCameraSetupPtr(new Ogre::FocusedShadowCameraSetup()); this->dataPtr->light->setCustomShadowCameraSetup( this->dataPtr->shadowCameraSetup); + RTShaderSystem::Instance()->UpdateShadows(); } - RTShaderSystem::Instance()->UpdateShadows(); + } + else + { + // todo(anyone) make point light casts shadows + this->dataPtr->light->setCastShadows(false); } } diff --git a/gazebo/rendering/RTShaderSystem.cc b/gazebo/rendering/RTShaderSystem.cc index 76c9db14c3..00b7502c4e 100644 --- a/gazebo/rendering/RTShaderSystem.cc +++ b/gazebo/rendering/RTShaderSystem.cc @@ -242,6 +242,7 @@ void RTShaderSystem::DetachViewport(Ogre::Viewport *_viewport, ScenePtr _scene) void RTShaderSystem::UpdateShaders() { // shaders will be updated in the Update call on pre-render event. + std::lock_guard lock(this->dataPtr->updateMutex); this->dataPtr->updateShaders = true; } @@ -597,6 +598,7 @@ void RTShaderSystem::Update() return; } + std::lock_guard lock(this->dataPtr->updateMutex); if (this->dataPtr->updateShadows) { for (const auto &scene : this->dataPtr->scenes) @@ -689,6 +691,7 @@ double RTShaderSystem::ShadowSplitPadding() const ///////////////////////////////////////////////// void RTShaderSystem::UpdateShadows() { + std::lock_guard lock(this->dataPtr->updateMutex); this->dataPtr->updateShadows = true; } @@ -742,9 +745,9 @@ void RTShaderSystem::UpdateShadows(ScenePtr _scene) #endif sceneMgr->setShadowTextureConfig(0, this->dataPtr->shadowTextureSize, this->dataPtr->shadowTextureSize, - Ogre::PF_FLOAT32_R, 1); - sceneMgr->setShadowTextureConfig(1, texSize, texSize, Ogre::PF_FLOAT32_R, 2); - sceneMgr->setShadowTextureConfig(2, texSize, texSize, Ogre::PF_FLOAT32_R, 3); + Ogre::PF_FLOAT32_R); + sceneMgr->setShadowTextureConfig(1, texSize, texSize, Ogre::PF_FLOAT32_R); + sceneMgr->setShadowTextureConfig(2, texSize, texSize, Ogre::PF_FLOAT32_R); #if defined(HAVE_OPENGL) // Enable shadow map comparison, so shader can use diff --git a/gazebo/rendering/RTShaderSystemPrivate.hh b/gazebo/rendering/RTShaderSystemPrivate.hh index 06b7e63c2a..15d1b277fa 100644 --- a/gazebo/rendering/RTShaderSystemPrivate.hh +++ b/gazebo/rendering/RTShaderSystemPrivate.hh @@ -18,6 +18,7 @@ #ifndef _GAZEBO_RTSHADERSYSTEM_PRIVATE_HH_ #define _GAZEBO_RTSHADERSYSTEM_PRIVATE_HH_ +#include #include #include @@ -56,7 +57,7 @@ namespace gazebo /// \brief Flag to indicate that shaders need to be updated. public: bool updateShaders; - /// \brief Flag to indicate that shaders need to be updated. + /// \brief Flag to indicate that shadows need to be updated. public: bool updateShadows = false; /// \brief Size of the Parallel Split Shadow Map (PSSM) shadow texture @@ -82,6 +83,9 @@ namespace gazebo /// \brief Flag to indicate if normal map should be enabled public: bool enableNormalMap = true; + + /// \brief Mutex to protect shaders and shadows update + public: std::mutex updateMutex; }; } } diff --git a/gazebo/rendering/RenderingLight_TEST.cc b/gazebo/rendering/RenderingLight_TEST.cc index 7b82fa37a8..d7f2bbaf77 100644 --- a/gazebo/rendering/RenderingLight_TEST.cc +++ b/gazebo/rendering/RenderingLight_TEST.cc @@ -115,8 +115,9 @@ TEST_F(Light_TEST, CastShadows) msg.set_cast_shadows(true); spotLight->LoadFromMsg(msg); EXPECT_EQ(spotLight->LightType(), "spot"); - // issue #2083: spot light does not cast shadows - EXPECT_FALSE(spotLight->CastShadows()); + // issue #2083: spot light generates shadow maps but they are not currently + // being rendered + EXPECT_TRUE(spotLight->CastShadows()); scene->RemoveLight(spotLight); spotLight.reset(); From 5f426b2af82f444af6e701b4bffa3d554e3e35f8 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 15 Jan 2021 16:49:14 -0800 Subject: [PATCH 11/12] scope lock Signed-off-by: Ian Chen --- gazebo/rendering/RTShaderSystem.cc | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/gazebo/rendering/RTShaderSystem.cc b/gazebo/rendering/RTShaderSystem.cc index 00b7502c4e..6d119fcdb5 100644 --- a/gazebo/rendering/RTShaderSystem.cc +++ b/gazebo/rendering/RTShaderSystem.cc @@ -598,17 +598,24 @@ void RTShaderSystem::Update() return; } - std::lock_guard lock(this->dataPtr->updateMutex); - if (this->dataPtr->updateShadows) + bool updateShaders, updateShadows = false; + { + std::lock_guard lock(this->dataPtr->updateMutex); + updateShaders = this->dataPtr->updateShaders; + updateShadows = this->dataPtr->updateShadows; + this->dataPtr->updateShaders = false; + this->dataPtr->updateShadows = false; + } + + if (updateShadows) { for (const auto &scene : this->dataPtr->scenes) { this->UpdateShadows(scene); } - this->dataPtr->updateShadows = false; } - if (this->dataPtr->updateShaders) + if (updateShaders) { for (const auto &scene : this->dataPtr->scenes) { @@ -618,7 +625,6 @@ void RTShaderSystem::Update() this->UpdateShaders(vis); } } - this->dataPtr->updateShaders = false; } } From cf404b11fd7b5cc9f3bedfc209c5ce3976438694 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 15 Jan 2021 16:53:33 -0800 Subject: [PATCH 12/12] use glsl 120 Signed-off-by: Ian Chen --- .../programs/spotlight_shadow_demo_fp.glsl | 14 +++++++------- .../programs/spotlight_shadow_demo_vp.glsl | 14 ++++++-------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/media/materials/programs/spotlight_shadow_demo_fp.glsl b/media/materials/programs/spotlight_shadow_demo_fp.glsl index 01731cc2da..1508127845 100644 --- a/media/materials/programs/spotlight_shadow_demo_fp.glsl +++ b/media/materials/programs/spotlight_shadow_demo_fp.glsl @@ -1,12 +1,10 @@ -#version 130 +#version 120 uniform sampler2D shadowMap0; -in vec4 lightSpacePos0; +varying vec4 lightSpacePos0; -in vec4 worldPos; -in vec4 worldViewPos; - -out vec4 outputCol; +varying vec4 worldPos; +varying vec4 worldViewPos; //------------------------------------------------------------------------------ float ShadowSimple(in sampler2D shadowMap, in vec4 shadowMapPos) @@ -34,11 +32,13 @@ void main() float f = 0.0f; // flat red color - no lighting - outputCol = vec4(1.0, 0.0, 0.0, 1.0); + vec4 outputCol = vec4(1.0, 0.0, 0.0, 1.0); // grey shadows f += ShadowSimple(shadowMap0, lightSpacePos0); f = clamp(f, 0.0f, 1.0f); if (f > 0.0f) outputCol = vec4(0.2, 0.2, 0.2, 1.0); + + gl_FragColor = outputCol; } diff --git a/media/materials/programs/spotlight_shadow_demo_vp.glsl b/media/materials/programs/spotlight_shadow_demo_vp.glsl index 66dd8231fa..71dc3e6e18 100644 --- a/media/materials/programs/spotlight_shadow_demo_vp.glsl +++ b/media/materials/programs/spotlight_shadow_demo_vp.glsl @@ -1,23 +1,21 @@ -#version 130 - -in vec4 position; +#version 120 uniform mat4 worldMatrix; uniform mat4 worldViewMatrix; uniform mat4 viewProjMatrix; uniform mat4 texViewProjMatrix0; -out vec4 lightSpacePos0; +varying vec4 lightSpacePos0; -out vec4 worldPos; -out vec4 worldViewPos; +varying vec4 worldPos; +varying vec4 worldViewPos; void main() { - worldPos = worldMatrix * position; + worldPos = worldMatrix * gl_Vertex; gl_Position = viewProjMatrix * worldPos; - worldViewPos = worldViewMatrix * position; + worldViewPos = worldViewMatrix * gl_Vertex; lightSpacePos0 = texViewProjMatrix0 * worldPos; }