Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(w3ddevice): stop the shadows flickering as the camera moves

Olcay Seyganf5c273dc3ab3
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
index 0f6ff4d60..cdd3bcaca 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
@@ -3845,6 +3845,20 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
Matrix3D transform;
transform.Look_At( focus + toSun * SHADOW_MAP_SUN_DISTANCE, focus, 0.0f );
+
+ /* The box has to sit on whole texels of its own map, or every scroll of the camera slides the
+ grid under the world by a fraction of a texel and every shadow edge crawls and sparkles. The
+ look point is taken into the sun's own frame, rounded to the texel it lands in, and taken back
+ out: the box then moves in texel steps and a shadow that did not move does not shimmer. */
+ const Real texelWidth = (2.0f * SHADOW_MAP_HALF_WIDTH) / (Real)SHADOW_MAP_TEXELS;
+ Vector3 focusInSun;
+ Matrix3D::Inverse_Transform_Vector( transform, focus, &focusInSun );
+ focusInSun.X = WWMath::Floor( focusInSun.X / texelWidth + 0.5f ) * texelWidth;
+ focusInSun.Y = WWMath::Floor( focusInSun.Y / texelWidth + 0.5f ) * texelWidth;
+ Vector3 snapped;
+ Matrix3D::Transform_Vector( transform, focusInSun, &snapped );
+ transform.Look_At( snapped + toSun * SHADOW_MAP_SUN_DISTANCE, snapped, 0.0f );
+
sun.Set_Transform( transform );
Matrix4x4 projection;
@@ -3872,8 +3886,17 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
for (W3DVolumetricShadow *shadow = m_shadowList; shadow; shadow = shadow->m_next)
{
RenderObjClass *robj = shadow->getRenderObject();
- if (robj == NULL || !robj->Is_Really_Visible() || !shadow->isRenderEnabled()
- || shadow->isInvisibleEnabled())
+ if (robj == NULL || !shadow->isRenderEnabled() || shadow->isInvisibleEnabled())
+ continue;
+
+ /* What goes into the map is what stands in the sun's box, not what the tactical camera can
+ see. Culling by the camera meant a tank just off the left of the screen stopped casting,
+ so its shadow blinked out while the shadow of the tank beside it stayed - and scrolling
+ turned that into a row of shadows flickering along the edge of the frame. */
+ Vector3 inSun;
+ Matrix3D::Inverse_Transform_Vector( transform, robj->Get_Position(), &inSun );
+ const Real reach = SHADOW_MAP_HALF_WIDTH + robj->Get_Bounding_Sphere().Radius;
+ if (inSun.X < -reach || inSun.X > reach || inSun.Y < -reach || inSun.Y > reach)
continue;
robj->Render( sunInfo );
@@ -3922,9 +3945,12 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
Direct3D11_Set_Shadow_Parameters( SHADOW_MAP_DEPTH_BIAS, strength, widest,
SHADOW_MAP_NARROWEST_TEXELS, penumbra / worldPerTexel, unitsPerUnitOfDepth, skyFill );
- // Said last, and only on the way out: everything above can bail, and the volumes have to know
- // whether this frame's shadows are in the map or still theirs to draw.
- theShadowMapHoldsTheFrame = (casters > 0);
+ /* Said last, and only on the way out: everything above can bail, and the volumes have to know
+ whether this frame's shadows are in the map or still theirs to draw. The count of casters is
+ not part of that answer. It was, and a frame that happened to hold none - a camera over open
+ ground - handed the frame back to the volumes for that one frame, so the whole scene's
+ shadows changed style and back again as the camera moved. */
+ theShadowMapHoldsTheFrame = TRUE;
// The report costs a full stall of the pipeline, so it is one line a second rather than one a
// frame: what it answers is whether the pass draws the world at all, and that does not change
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.h
index 19bcc5cb2..bc7e8ae68 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.h
@@ -159,10 +159,11 @@ const unsigned NORMAL_MAPPED_LIGHTS = 4;
"\n" \
" // Five by five rather than three by three: opened up to nine texels, nine taps stand so\n" \
" // far apart that a body as narrow as a helicopter's falls between them and casts nothing.\n" \
- " // The grid is turned by an angle taken from the pixel's own place on the screen, which\n" \
- " // trades the steps a fixed grid leaves across a wide penumbra for noise the eye reads as\n" \
- " // a gradient.\n" \
- " float turn = frac(sin(dot(position.xy, float2(12.9898, 78.233))) * 43758.5453) * 6.2831853;\n" \
+ " // The grid is turned by an angle, which trades the steps a fixed grid leaves across a wide\n" \
+ " // penumbra for noise the eye reads as a gradient. The angle comes from where the pixel is\n" \
+ " // in the sun's map and not from where it is on the screen: on the screen it swims as soon\n" \
+ " // as the camera moves, and a shadow that stands still shimmers.\n" \
+ " float turn = frac(sin(dot(map * 4096.0, float2(12.9898, 78.233))) * 43758.5453) * 6.2831853;\n" \
" float2 turn_cos_sin = float2(cos(turn), sin(turn));\n" \
" float blocked = 0.0;\n" \
" for (int y = -2; y <= 2; ++y) {\n" \