1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 21:59:55 +00:00

Don't exclusively rely on a shadow map if its bounds have been expanded since the cull traversal.

This commit is contained in:
AnyOldName3 2018-12-08 20:39:41 +00:00
parent 244bd289cb
commit 2d5da1a6fa
4 changed files with 62 additions and 8 deletions

View file

@ -1315,6 +1315,29 @@ void MWShadowTechnique::cull(osgUtil::CullVisitor& cv)
if (!orthographicViewFrustum && settings->getShadowMapProjectionHint()==ShadowSettings::PERSPECTIVE_SHADOW_MAP) if (!orthographicViewFrustum && settings->getShadowMapProjectionHint()==ShadowSettings::PERSPECTIVE_SHADOW_MAP)
{ {
{
osg::Matrix validRegionMatrix = cv.getCurrentCamera()->getInverseViewMatrix() * camera->getViewMatrix() * camera->getProjectionMatrix();
std::string validRegionUniformName = "validRegionMatrix" + std::to_string(sm_i);
osg::ref_ptr<osg::Uniform> validRegionUniform;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_accessUniformsAndProgramMutex);
for (auto uniform : _uniforms)
{
if (uniform->getName() == validRegionUniformName)
validRegionUniform = uniform;
}
if (!validRegionUniform)
{
validRegionUniform = new osg::Uniform(osg::Uniform::FLOAT_MAT4, validRegionUniformName);
_uniforms.push_back(validRegionUniform);
}
validRegionUniform->set(validRegionMatrix);
}
if (settings->getMultipleShadowMapHint() == ShadowSettings::CASCADED) if (settings->getMultipleShadowMapHint() == ShadowSettings::CASCADED)
adjustPerspectiveShadowMapCameraSettings(vdsmCallback->getRenderStage(), frustum, pl, camera.get(), cascaseNear, cascadeFar); adjustPerspectiveShadowMapCameraSettings(vdsmCallback->getRenderStage(), frustum, pl, camera.get(), cascaseNear, cascadeFar);
else else

View file

@ -119,6 +119,12 @@ namespace SceneUtil
else else
definesWithShadows["useShadowDebugOverlay"] = "0"; definesWithShadows["useShadowDebugOverlay"] = "0";
// switch this to reading settings if it's ever exposed to the user
if (mShadowSettings->getShadowMapProjectionHint() == ShadowSettings::PERSPECTIVE_SHADOW_MAP)
definesWithShadows["perspectiveShadowMaps"] = "1";
else
definesWithShadows["perspectiveShadowMaps"] = "0";
return definesWithShadows; return definesWithShadows;
} }
@ -132,6 +138,8 @@ namespace SceneUtil
definesWithShadows["useShadowDebugOverlay"] = "0"; definesWithShadows["useShadowDebugOverlay"] = "0";
definesWithShadows["perspectiveShadowMaps"] = "0";
return definesWithShadows; return definesWithShadows;
} }
void ShadowManager::enableIndoorMode() void ShadowManager::enableIndoorMode()

View file

@ -4,6 +4,10 @@
@foreach shadow_texture_unit_index @shadow_texture_unit_list @foreach shadow_texture_unit_index @shadow_texture_unit_list
uniform sampler2DShadow shadowTexture@shadow_texture_unit_index; uniform sampler2DShadow shadowTexture@shadow_texture_unit_index;
varying vec4 shadowSpaceCoords@shadow_texture_unit_index; varying vec4 shadowSpaceCoords@shadow_texture_unit_index;
#if @perspectiveShadowMaps
varying vec4 shadowRegionCoords@shadow_texture_unit_index;
#endif
@endforeach @endforeach
#endif // SHADOWS #endif // SHADOWS
@ -17,13 +21,18 @@ float unshadowedLightRatio()
if (!doneShadows) if (!doneShadows)
{ {
vec3 shadowXYZ = shadowSpaceCoords@shadow_texture_unit_index.xyz / shadowSpaceCoords@shadow_texture_unit_index.w; vec3 shadowXYZ = shadowSpaceCoords@shadow_texture_unit_index.xyz / shadowSpaceCoords@shadow_texture_unit_index.w;
vec2 shadowXY = shadowXYZ.xy; #if @perspectiveShadowMaps
if (all(lessThan(shadowXY, vec2(1.0, 1.0))) && all(greaterThan(shadowXY, vec2(0.0, 0.0)))) vec3 shadowRegionXYZ = shadowRegionCoords@shadow_texture_unit_index.xyz / shadowRegionCoords@shadow_texture_unit_index.w;
#endif
if (all(lessThan(shadowXYZ.xy, vec2(1.0, 1.0))) && all(greaterThan(shadowXYZ.xy, vec2(0.0, 0.0))))
{ {
shadowing = min(shadow2DProj(shadowTexture@shadow_texture_unit_index, shadowSpaceCoords@shadow_texture_unit_index).r, shadowing); shadowing = min(shadow2DProj(shadowTexture@shadow_texture_unit_index, shadowSpaceCoords@shadow_texture_unit_index).r, shadowing);
if (all(lessThan(shadowXYZ, vec3(0.95, 0.95, 1.0))) && all(greaterThan(shadowXYZ, vec3(0.05, 0.05, 0.0))))
doneShadows = true; doneShadows = all(lessThan(shadowXYZ, vec3(0.95, 0.95, 1.0))) && all(greaterThan(shadowXYZ.xy, vec2(0.05, 0.05)));
#if @perspectiveShadowMaps
doneShadows = doneShadows && all(lessThan(shadowRegionXYZ, vec3(1.0, 1.0, 1.0))) && all(greaterThan(shadowRegionXYZ.xy, vec2(-1.0, -1.0)));
#endif
} }
} }
@endforeach @endforeach
@ -44,8 +53,11 @@ void applyShadowDebugOverlay()
@foreach shadow_texture_unit_index @shadow_texture_unit_list @foreach shadow_texture_unit_index @shadow_texture_unit_list
if (!doneOverlay) if (!doneOverlay)
{ {
vec2 shadowXY = shadowSpaceCoords@shadow_texture_unit_index.xy / shadowSpaceCoords@shadow_texture_unit_index.w; vec3 shadowXYZ = shadowSpaceCoords@shadow_texture_unit_index.xyz / shadowSpaceCoords@shadow_texture_unit_index.w;
if (all(lessThan(shadowXY, vec2(1.0, 1.0))) && all(greaterThan(shadowXY, vec2(0.0, 0.0)))) #if @perspectiveShadowMaps
vec3 shadowRegionXYZ = shadowRegionCoords@shadow_texture_unit_index.xyz / shadowRegionCoords@shadow_texture_unit_index.w;
#endif
if (all(lessThan(shadowXYZ.xy, vec2(1.0, 1.0))) && all(greaterThan(shadowXYZ.xy, vec2(0.0, 0.0))))
{ {
colourIndex = mod(@shadow_texture_unit_index.0, 3.0); colourIndex = mod(@shadow_texture_unit_index.0, 3.0);
if (colourIndex < 1.0) if (colourIndex < 1.0)
@ -55,8 +67,10 @@ void applyShadowDebugOverlay()
else else
gl_FragData[0].z += 0.1; gl_FragData[0].z += 0.1;
if (all(lessThan(shadowXY, vec2(0.95, 0.95))) && all(greaterThan(shadowXY, vec2(0.05, 0.05)))) doneOverlay = all(lessThan(shadowXYZ, vec3(0.95, 0.95, 1.0))) && all(greaterThan(shadowXYZ.xy, vec2(0.05, 0.05)));
doneOverlay = true; #if @perspectiveShadowMaps
doneOverlay = doneOverlay && all(lessThan(shadowRegionXYZ.xyz, vec3(1.0, 1.0, 1.0))) && all(greaterThan(shadowRegionXYZ.xy, vec2(-1.0, -1.0)));
#endif
} }
} }
@endforeach @endforeach

View file

@ -4,6 +4,11 @@
@foreach shadow_texture_unit_index @shadow_texture_unit_list @foreach shadow_texture_unit_index @shadow_texture_unit_list
uniform int shadowTextureUnit@shadow_texture_unit_index; uniform int shadowTextureUnit@shadow_texture_unit_index;
varying vec4 shadowSpaceCoords@shadow_texture_unit_index; varying vec4 shadowSpaceCoords@shadow_texture_unit_index;
#if @perspectiveShadowMaps
uniform mat4 validRegionMatrix@shadow_texture_unit_index;
varying vec4 shadowRegionCoords@shadow_texture_unit_index;
#endif
@endforeach @endforeach
#endif // SHADOWS #endif // SHADOWS
@ -15,6 +20,10 @@ void setupShadowCoords(vec4 viewPos)
@foreach shadow_texture_unit_index @shadow_texture_unit_list @foreach shadow_texture_unit_index @shadow_texture_unit_list
eyePlaneMat = mat4(gl_EyePlaneS[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneT[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneR[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneQ[shadowTextureUnit@shadow_texture_unit_index]); eyePlaneMat = mat4(gl_EyePlaneS[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneT[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneR[shadowTextureUnit@shadow_texture_unit_index], gl_EyePlaneQ[shadowTextureUnit@shadow_texture_unit_index]);
shadowSpaceCoords@shadow_texture_unit_index = viewPos * eyePlaneMat; shadowSpaceCoords@shadow_texture_unit_index = viewPos * eyePlaneMat;
#if @perspectiveShadowMaps
shadowRegionCoords@shadow_texture_unit_index = validRegionMatrix@shadow_texture_unit_index * viewPos;
#endif
@endforeach @endforeach
#endif // SHADOWS #endif // SHADOWS
} }