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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
feat(gameclient): reach overlay for one defence, smooth edges, new look
Olcay Seygan0d3cbcc9e5a1
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 45179dd99..e504e94e0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,10 +10,10 @@ found and fixed, EA's own, not port damage.**## The ground a defence can hit-- Click one of your defences, or put a new one on the cursor, and the ground it fires on is painted on the terrain in your colour: a quiet wash with a sharp line round its edge, lying over the hills and down the slopes, under your units and buildings. While a building is on the cursor every defence of yours and your allies' shows its ground as well, so the gap between two Patriot batteries is something you see before you pay for a third. The enemy's defences show nothing. This came back after the one-interface change took the old range ring away with the Reforged interface.-- Only ground the defence really hits is painted. Every spot is put to the same questions the game asks before a defence fires on its own: is the target in reach, with the extra reach high ground gives; does a hill or a building stand in the line of the shot; can you see the spot at all; is it too close, the way the patch under a Fire Base's walls is. A spot is painted only if the answer is yes for the lowest raider and the widest tank in the game, and for every gun the defence might pick for it. Where the edge steps round the corner of a building or along the brow of a hill, that is the game's own grid: the shot is decided square by square there, and a smoother line would paint ground the defence cannot hit. The old ring was drawn flat on the screen, over the units, and for weeks it ringed a Patriot at its 450 assist missile when it shoots tanks out to 225.+- Click one of your defences, or put a new one on the cursor, and the ground it fires on is painted on the terrain in your colour: a quiet wash with a clean line round the end of its reach, lying over the hills and down the slopes, under your units and buildings. Only what you clicked or what you are placing is painted: a base of ten defences no longer floods the ground in colour the moment a dozer picks up a building. Where a cliff, a building or the fog cuts into the ground, the wash fades in softly instead of ending on a line, so you can tell at a glance whether a gap is out of range or out of sight. A faint pulse leaves the line every few seconds and runs inward. The enemy's defences show nothing. This came back after the one-interface change took the old range ring away with the Reforged interface.+- Only ground the defence really hits is painted. Every spot is put to the same questions the game asks before a defence fires on its own: is the target in reach, with the extra reach high ground gives; does a hill or a building stand in the line of the shot; can you see the spot at all; is it too close, the way the patch under a Fire Base's walls is. A spot is painted only if the answer is yes for the lowest raider and the widest tank in the game, and for every gun the defence might pick for it. The shot is decided square by square, and the first version drew those squares: the edge stepped round every building and down every brow in hard little stairs. The edge is smoothed now and drawn a hair inside the squares, never outside them, so it still paints no ground the defence cannot hit. The old ring was drawn flat on the screen, over the units, and for weeks it ringed a Patriot at its 450 assist missile when it shoots tanks out to 225.- A Patriot battery shows where it shoots ground targets by itself. The long missile it fires only when another Patriot calls for help is left out, so is its anti-aircraft missile, and so is anything in the air. A defence with no power, or one that has been hacked or hit by an EMP, shows nothing, because it fires at nothing.-- Checked against the game itself. On Twilight Flame's hilly south-west plateau a Patriot, a Fire Base and a Gattling Cannon had their painted ground sampled at 3,490 random spots, with a Combat Cycle, a Rebel or an Overlord standing on each and the game asked whether the defence would fire: it would, on all 3,490. Leave the hill test out of the painting and the same check finds 51 to 338 painted spots per run the Patriot never shoots at.+- Checked against the game itself. On Twilight Flame's hilly south-west plateau a Patriot, a Fire Base and a Gattling Cannon had their painted ground sampled at 3,411 random spots, with a Combat Cycle, a Rebel or an Overlord standing on each and the game asked whether the defence would fire: it would, on all 3,411, and on all 2,596 of the spots painted for the same defences on the placement cursor. Leave the hill test out of the painting and the same check finds 51 to 338 painted spots per run the Patriot never shoots at.## What a rank givesdiff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/ReachOverlay.h b/GeneralsMD/Code/GameEngine/Include/GameClient/ReachOverlay.hindex ca841ec7e..161563af3 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/ReachOverlay.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/ReachOverlay.h@@ -16,10 +16,14 @@ class ThingTemplate;/** One defence's hittable ground. Sample (i, j) sits at world (originX + i * step, originY + j * step).* distance[ j * width + i ] is in world units: positive inside the ground the defence can hit, negative- * outside, nought on the edge, clamped to +-distanceLimit. The edge drawn where the field taken- * bilinearly crosses nought lies only in grid cells whose four corners the logic's own tests passed- * (ReachOverlay.cpp says how), so the renderer must not grow the area: draw nothing where the- * filtered distance is at or under nought. */+ * outside, nought on the edge, clamped to +-distanceLimit, and smoothed so its edge has no grid steps.+ * The edge drawn where the field taken bilinearly crosses nought lies only in grid cells whose four+ * corners the logic's own tests passed (ReachOverlay.cpp says how), so the renderer must not grow the+ * area: draw nothing where the filtered distance is at or under nought.+ *+ * hardness, on the same grid, says what the nearest edge is: 1 where it is the end of the guns' reach+ * (a range ring, drawn as a line), 0 where something blocks it (a cliff, a building, the fog), which+ * fades in softly instead. Taken bilinearly too. */struct ReachOverlayField{Real originX;@@ -29,9 +33,10 @@ struct ReachOverlayFieldInt height;Real distanceLimit;std::vector< Real > distance;+ std::vector< Real > hardness;Color color; ///< the owner's colour, alpha ignoredReal opacity; ///< 0..1, the whole area's strength this frame- UnsignedInt version; ///< changes whenever distance or the grid changes, so a texture can be kept+ UnsignedInt version; ///< changes whenever distance, hardness or the grid changes, so a texture can be kept};/** The fields to draw this frame, in no particular order. Empty when nothing is shown, and always@@ -39,8 +44,8 @@ struct ReachOverlayFieldvoid ReachOverlay_getFields( std::vector< const ReachOverlayField * > &out );/** Work out this frame's fields: the selected defences of the local player, an ally or (to an- * observer) anyone, and while a building is on the cursor every such defence plus the one on the- * cursor. InGameUI::update calls it once a frame. */+ * observer) anyone, or while a defence is on the placement cursor that one alone. Nothing else is+ * ever drawn. InGameUI::update calls it once a frame. */void ReachOverlay_update( void );/** Sample a standing defence's field, and its template's on the cursor at the same spot, againstdiff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/ReachOverlay.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/ReachOverlay.cppindex 0ed89d1be..099029540 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/ReachOverlay.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/ReachOverlay.cpp@@ -61,8 +61,9 @@//// The tests are taken on a grid, a quarter of a pathfind cell apart, and a grid point is lit only if// it and its eight neighbours all pass. The renderer paints where the field, taken bilinearly, is-// above nought, which needs one lit corner, so every painted spot lies in a grid cell whose four-// corners all passed. ReachOverlay_prove measures what is left: points between the grid points.+// above nought, which needs one lit corner (shadeField smooths the field but holds every unlit point+// below nought), so every painted spot lies in a grid cell whose four corners all passed.+// ReachOverlay_prove measures what is left: points between the grid points.//-------------------------------------------------------------------------------------------------static const Real LIVE_STEP = PATHFIND_CELL_SIZE_F * 0.25f; ///< a standing defence's grid@@ -308,24 +309,28 @@ static Real standHeight( Real x, Real y )return z;}+/// what a grid point's tests said: out of the guns' reach (a range edge, drawn as a line), passed, or+/// in reach but blocked (a cliff, a building, the sight line, the fog: an edge that fades in)+enum { REACH_OUT = 0, REACH_PASS = 1, REACH_BLOCKED = 2 };+//-------------------------------------------------------------------------------------------------/** Every test but the fog, for the worst ground target standing at (x, y). *///--------------------------------------------------------------------------------------------------static Bool hitsGroundAt( const ReachShooter &s, const GroundProbe &probe, Real x, Real y )+static UnsignedByte hitsGroundAt( const ReachShooter &s, const GroundProbe &probe, Real x, Real y ){const Real dx = x - s.center.x;const Real dy = y - s.center.y;if( dx * dx + dy * dy > s.reachBound * s.reachBound )- return FALSE;+ return REACH_OUT;Coord3D at;at.set( x, y, standHeight( x, y ) );const PathfindCell *cell = TheAI->pathfinder()->getCell( LAYER_GROUND, &at );if( cell == NULL || cell->getType() == PathfindCell::CELL_OBSTACLE )- return FALSE;+ return REACH_BLOCKED;if( TheTerrainLogic->findBridgeAt( &at ) != NULL )- return FALSE;+ return REACH_BLOCKED;// FROM_BOUNDINGSPHERE_2D from the defence's edge to the target's middleReal distSqr;@@ -341,7 +346,7 @@ static Bool hitsGroundAt( const ReachShooter &s, const GroundProbe &probe, Realconst Real look = s.obj ? Weapon_elevatedRange( s.obj, s.acquireRange, at.z ): s.acquireRange + Weapon_elevationRangeBonus( s.acquireRange, s.center.z - at.z );if( distSqr > look * look )- return FALSE;+ return REACH_OUT;// the widest target's edge is this much nearer than its middle (distCalcProc_BoundaryAndBoundary_2D)const Real edge = max( 0.0f, sqrtf( distSqr ) - probe.widestRadius );@@ -352,7 +357,7 @@ static Bool hitsGroundAt( const ReachShooter &s, const GroundProbe &probe, Realif( gun.live ){if( !gun.live->isWithinAttackRange( s.obj, &at ) )- return FALSE;+ return REACH_OUT;}else{@@ -360,13 +365,13 @@ static Bool hitsGroundAt( const ReachShooter &s, const GroundProbe &probe, Realconst Real reach = range + Weapon_elevationRangeBonus( range, s.center.z - at.z );const Real minimum = gun.tmpl->getMinimumAttackRange();if( distSqr > reach * reach || distSqr < minimum * minimum )- return FALSE;+ return REACH_OUT;}const Real minimum = gun.tmpl->getMinimumAttackRange();if( minimum > 0.0f && edge * edge < minimum * minimum )- return FALSE;+ return REACH_OUT;if( pitchLimited( gun.tmpl ) && fabs( at.z - s.center.z ) >= PITCH_FREE_HEIGHT )- return FALSE;+ return REACH_BLOCKED;}if( s.lineOfSight )@@ -376,21 +381,21 @@ static Bool hitsGroundAt( const ReachShooter &s, const GroundProbe &probe, Realif( s.obj ){if( !ThePartitionManager->isClearLineOfSightTerrain( s.obj, *s.obj->getPosition(), NULL, top ) )- return FALSE;+ return REACH_BLOCKED;if( TheAI->pathfinder()->isAttackViewBlockedByObstacle( s.obj, *s.obj->getPosition(), NULL, at ) )- return FALSE;+ return REACH_BLOCKED;}else{Coord3D eye = s.center;eye.z = s.eyeZ;if( !ThePartitionManager->isClearLineOfSightTerrain( NULL, eye, NULL, top ) )- return FALSE;+ return REACH_BLOCKED;if( TheAI->pathfinder()->isGroundViewBlockedByObstacle( s.center, at, s.standingIn ) )- return FALSE;+ return REACH_BLOCKED;}}- return TRUE;+ return REACH_PASS;}//-------------------------------------------------------------------------------------------------@@ -399,7 +404,7 @@ static Bool hitsGroundAt( const ReachShooter &s, const GroundProbe &probe, Realstruct ReachEntry{ReachOverlayField field;- std::vector< UnsignedByte > geometry; ///< every test but the fog passed at this grid point+ std::vector< UnsignedByte > geometry; ///< REACH_PASS where every test but the fog passed at this grid pointUnsignedInt signature;UnsignedInt fogFrame;UnsignedInt lastWanted;@@ -433,7 +438,7 @@ static void traceGeometry( ReachEntry &entry, const ReachShooter &s, Real step ){const Real y = entry.field.originY + j * step;for( Int i = 0; i < w; ++i )- entry.geometry[ j * w + i ] = hitsGroundAt( s, probe, entry.field.originX + i * step, y ) ? 1 : 0;+ entry.geometry[ j * w + i ] = hitsGroundAt( s, probe, entry.field.originX + i * step, y );}// the bound allows for all the high ground a range can gain; cut the grid down to what passed,@@ -443,7 +448,7 @@ static void traceGeometry( ReachEntry &entry, const ReachShooter &s, Real step ){for( Int i = 0; i < w; ++i ){- if( !entry.geometry[ j * w + i ] )+ if( entry.geometry[ j * w + i ] != REACH_PASS )continue;loI = min( loI, i );hiI = max( hiI, i );@@ -477,10 +482,65 @@ static void traceGeometry( ReachEntry &entry, const ReachShooter &s, Real step )entry.fogFrame = 0;}+/// a separable Gaussian over the grid, sigma in grid steps; past the border the edge sample holds,+/// and the border is two points of nothing (traceGeometry's crop)+static void blurGrid( std::vector< Real > &v, Int w, Int h, Real sigma )+{+ const Int radius = (Int)ceilf( 2.5f * sigma );+ std::vector< Real > weight( radius + 1 );+ Real sum = 0.0f;+ for( Int r = 0; r <= radius; ++r )+ {+ weight[ r ] = expf( -0.5f * (Real)( r * r ) / ( sigma * sigma ) );+ sum += r == 0 ? weight[ r ] : 2.0f * weight[ r ];+ }+ for( Int r = 0; r <= radius; ++r )+ weight[ r ] /= sum;++ // one line of samples, stride apart, through a padded copy so the inner loop has no clamp+ std::vector< Real > line( max( w, h ) + 2 * radius );+ auto pass = [ & ]( Real *first, Int count, Int stride )+ {+ for( Int n = 0; n < count; ++n )+ line[ radius + n ] = first[ n * stride ];+ for( Int r = 0; r < radius; ++r )+ {+ line[ r ] = first[ 0 ];+ line[ radius + count + r ] = first[ ( count - 1 ) * stride ];+ }+ for( Int n = 0; n < count; ++n )+ {+ const Real *at = &line[ radius + n ];+ Real total = weight[ 0 ] * at[ 0 ];+ for( Int r = 1; r <= radius; ++r )+ total += weight[ r ] * ( at[ -r ] + at[ r ] );+ first[ n * stride ] = total;+ }+ };+ for( Int j = 0; j < h; ++j )+ pass( &v[ j * w ], w, 1 );+ for( Int i = 0; i < w; ++i )+ pass( &v[ i ], h, w );+}++static const Real SMOOTH_SIGMA = 4.0f; ///< world units the edge is smoothed over, at least a grid step+static const Real SMOOTH_MARGIN = 0.6f; ///< grid steps the smoothed edge is pulled in, past the steps' corners+static const Real UNLIT_HOLD = 0.05f; ///< grid steps an unlit point is held under nought whatever the blur says+//-------------------------------------------------------------------------------------------------/** The fog on top of the geometry, then the erosion, then the signed distance (a chamfer walk, a- * grid step straight and step root two across; only the sign is load-bearing, the size only shapes- * the border the renderer draws). */+ * grid step straight and step root two across), smoothed. Only the sign is load-bearing; the size+ * shapes the border the renderer draws.+ *+ * The lit points are the grid's own staircase, and so is a field taken straight from them: a range+ * ring drawn in steps of a grid point, a cliff's edge in steps of a pathfind cell. The distance is+ * blurred, which lays the edge along the middle of the steps, and pulled in by a margin so it passes+ * inside their corners. Whatever the blur did, an unlit point is then held below nought, so the+ * edge the renderer draws still lies only in grid cells with a lit corner, whose four corners all+ * passed: the area can shrink by the margin, never grow.+ *+ * The hardness is what the nearest failed point failed on, out of reach (a ring, hard) or blocked+ * (soft), blurred as the distance is so a ring meeting a cliff turns from line to fade smoothly. *///-------------------------------------------------------------------------------------------------static void shadeField( ReachEntry &entry ){@@ -489,19 +549,49 @@ static void shadeField( ReachEntry &entry )const Int h = field.height;const Real step = field.step;+ // hard: what the nearest failed point failed on, 1 out of reach, 0 blocked (a point in reach that+ // the fog hides counts as blocked); gap: how far that point is, in grid steps, a city-block walkstd::vector< UnsignedByte > seen( w * h, 0 );+ std::vector< Real > hard( w * h, 1.0f );+ std::vector< Int > gap( w * h, w + h );for( Int j = 0; j < h; ++j ){for( Int i = 0; i < w; ++i ){const Int k = j * w + i;- if( !entry.geometry[ k ] )- continue;- Coord3D at;- at.set( field.originX + i * step, field.originY + j * step, 0.0f );- seen[ k ] = ThePartitionManager->getShroudStatusForPlayer( entry.playerIndex, &at ) == CELLSHROUD_CLEAR ? 1 : 0;+ if( entry.geometry[ k ] == REACH_PASS )+ {+ Coord3D at;+ at.set( field.originX + i * step, field.originY + j * step, 0.0f );+ seen[ k ] = ThePartitionManager->getShroudStatusForPlayer( entry.playerIndex, &at ) == CELLSHROUD_CLEAR ? 1 : 0;+ }+ if( !seen[ k ] )+ {+ gap[ k ] = 0;+ hard[ k ] = entry.geometry[ k ] == REACH_OUT ? 1.0f : 0.0f;+ }}}+ auto take = [ & ]( Int k, Int from )+ {+ if( gap[ from ] + 1 < gap[ k ] )+ {+ gap[ k ] = gap[ from ] + 1;+ hard[ k ] = hard[ from ];+ }+ };+ for( Int j = 0; j < h; ++j )+ for( Int i = 0; i < w; ++i )+ {+ if( i > 0 ) take( j * w + i, j * w + i - 1 );+ if( j > 0 ) take( j * w + i, ( j - 1 ) * w + i );+ }+ for( Int j = h - 1; j >= 0; --j )+ for( Int i = w - 1; i >= 0; --i )+ {+ if( i < w - 1 ) take( j * w + i, j * w + i + 1 );+ if( j < h - 1 ) take( j * w + i, ( j + 1 ) * w + i );+ }// lit: it and its eight neighbours passedstd::vector< UnsignedByte > lit( w * h, 0 );@@ -561,15 +651,29 @@ static void shadeField( ReachEntry &entry )}}+ for( Int k = 0; k < w * h; ++k )+ {+ const Real magnitude = min( d[ k ] - 0.5f * step, DISTANCE_LIMIT );+ d[ k ] = lit[ k ] ? magnitude : -magnitude;+ }+ const Real sigma = max( SMOOTH_SIGMA / step, 1.0f );+ blurGrid( d, w, h, sigma );+ blurGrid( hard, w, h, sigma );+// a fog refresh that changed nothing keeps the version, so the renderer keeps its texture- Bool changed = (Int)field.distance.size() != w * h;+ Bool changed = (Int)field.distance.size() != w * h || (Int)field.hardness.size() != w * h;field.distance.resize( w * h );+ field.hardness.resize( w * h );for( Int k = 0; k < w * h; ++k ){- const Real magnitude = min( d[ k ] - 0.5f * step, DISTANCE_LIMIT );- const Real value = lit[ k ] ? magnitude : -magnitude;- changed = changed || field.distance[ k ] != value;+ Real value = d[ k ] - SMOOTH_MARGIN * step;+ if( !lit[ k ] )+ value = min( value, -UNLIT_HOLD * step );+ value = max( -DISTANCE_LIMIT, min( value, DISTANCE_LIMIT ) );+ const Real hardness = hard[ k ];+ changed = changed || field.distance[ k ] != value || field.hardness[ k ] != hardness;field.distance[ k ] = value;+ field.hardness[ k ] = hardness;}if( changed || entry.regridded )field.version = ++theVersion;@@ -706,20 +810,11 @@ void ReachOverlay_update( void )theStructures.push_back( mark );}- // which defences are wanted: the selected ones, and while a building is on the cursor every one- // the local player is shown+ // which defences are wanted: the selected ones, or while a building is on the cursor none but+ // that one; every defence at once buries the ground in colourstd::vector< const Object * > wanted;const ThingTemplate *pending = TheInGameUI->getPendingPlaceType();- if( pending != NULL )- {- for( size_t k = 0; k < theStructures.size(); ++k )- {- const Object *obj = TheGameLogic->findObjectByID( theStructures[ k ].id );- if( revealedToLocal( obj ) )- wanted.push_back( obj );- }- }- else+ if( pending == NULL ){const DrawableList *selected = TheInGameUI->getAllSelectedDrawables();for( DrawableListCIt it = selected->begin(); it != selected->end(); ++it )diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DReachOverlay.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DReachOverlay.hindex c45ff9a4a..46b106d53 100644--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DReachOverlay.h+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DReachOverlay.h@@ -15,12 +15,23 @@ struct ReachOverlayField;* through its constants, so tuning one needs no new program in the shader cache. */namespace ReachLook{- const Real BorderWidth = 1.5f; ///< the line on the inside of the edge+ const Real BorderWidth = 1.5f; ///< the line on the inside of a range edgeconst Real BandWidth = 10.0f; ///< the stronger wash just inside the edge fades out over this- const Real FillAlpha = 0.10f; ///< the whole area+ const Real FillAlpha = 0.08f; ///< the whole areaconst Real BandAlpha = 0.14f; ///< added at the edge, nought BandWidth in- const Real LineAlpha = 0.70f;- const Real LineWhite = 0.35f; ///< how far the line goes from the owner's colour toward white (Direct3D 11)+ const Real LineAlpha = 0.75f;+ const Real LineWhite = 0.40f; ///< how far the line goes from the owner's colour toward white+ const Real ShadeWidth = 0.75f; ///< a dark hairline just inside the line, so it reads on pale ground+ const Real ShadeAlpha = 0.30f;+ const Real SoftWidth = 12.0f; ///< a blocked edge (cliff, building, fog) fades in over this, no line+ const Real SoftLine = 0.15f; ///< what is left of the line along a blocked edge+ const Real RippleAlpha = 0.16f; ///< a faint band that leaves the range line and runs inward (Direct3D 11)+ const Real RipplePeriod = 3.0f; ///< seconds between two+ /// world units it runs before it is gone. The field is flat past 13 in (its 16 limit less the+ /// margin), so a ripple that ran further would light the whole area at once; and the hardness is+ /// only meaningful about 20 in from a blocked edge+ const Real RippleSpan = 8.0f;+ const Real RippleWidth = 3.0f;}/** This frame's fields on the ground. Called from the terrain pass once the ground, the roads, thediff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/DX11ReachOverlay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/DX11ReachOverlay.cppindex 6f6f7abf7..e9b13a179 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/DX11ReachOverlay.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/DX11ReachOverlay.cpp@@ -9,6 +9,11 @@//// The fields of one owner are drawn in one pass as one area, the union of their distances (the// largest), so overlaps neither darken nor draw a line through each other.+//+// The look, like the game's own radius decals: a thin wash in the owner's colour, stronger toward the+// edge, and along the end of the guns' reach a light line with a dark hairline under it and a faint+// band that leaves it every few seconds and runs inward. Where a cliff, a building or the fog cuts+// the area there is no line; the wash fades in from the edge instead (the field's hardness).#include "W3DDevice/GameClient/W3DReachOverlay.h"#include "Common/Debug.h"@@ -52,7 +57,9 @@ struct Constantsfloat Viewport[ 4 ]; // pixel origin (half a pixel in, as the backend's viewport sits), sizefloat Colour[ 4 ]; // the owner's colour; w unusedfloat Look0[ 4 ]; // border width, band width, fill alpha, band alpha- float Look1[ 4 ]; // line alpha, line toward white, field count+ float Look1[ 4 ]; // line alpha, line toward white, field count, seconds+ float Look2[ 4 ]; // soft width, soft line, ripple alpha, ripple period+ float Look3[ 4 ]; // ripple span, ripple width, shade width, shade alphafloat Grid[ FieldsPerPass ][ 4 ]; // origin, 1 / step, -distanceLimitfloat Size[ FieldsPerPass ][ 4 ]; // width - 1, height - 1, opacity};@@ -65,11 +72,13 @@ cbuffer Reach : register(b0)float4 Colour;float4 Look0;float4 Look1;+ float4 Look2;+ float4 Look3;float4 Grid[32];float4 Size[32];};Texture2D<float> SceneDepth : register(t0);-Texture2D<float> Fields[32] : register(t1);+Texture2D<float2> Fields[32] : register(t1); // distance, hardnessfloat4 main(float4 position : SV_Position) : SV_Target{@@ -79,9 +88,10 @@ float4 main(float4 position : SV_Position) : SV_Targetfloat w = abs(world.w) < 0.000001 ? 0.000001 : world.w;float2 at = world.xy / w;- // the union of the fields: the largest distance, and the strongest opacity among the fields- // the pixel is inside+ // the union of the fields: the largest distance with its hardness, and the strongest opacity+ // among the fields the pixel is insidefloat d = -1.0e6;+ float hard = 1;float inside[32];uint count = (uint)Look1.z;[unroll] for (uint i = 0; i < 32; ++i)@@ -90,18 +100,24 @@ float4 main(float4 position : SV_Position) : SV_Targetif (i < count){float2 g = (at - Grid[i].xy) * Grid[i].z;+ float2 here = float2(Grid[i].w, 1);if (all(g >= 0) && all(g <= Size[i].xy)){float2 base = min(floor(g), Size[i].xy - 1);float2 f = g - base;int3 p = int3(base, 0);- float a = Fields[i].Load(p);- float b = Fields[i].Load(p + int3(1, 0, 0));- float c = Fields[i].Load(p + int3(0, 1, 0));- float e = Fields[i].Load(p + int3(1, 1, 0));- inside[i] = lerp(lerp(a, b, f.x), lerp(c, e, f.x), f.y);+ float2 a = Fields[i].Load(p);+ float2 b = Fields[i].Load(p + int3(1, 0, 0));+ float2 c = Fields[i].Load(p + int3(0, 1, 0));+ float2 e = Fields[i].Load(p + int3(1, 1, 0));+ here = lerp(lerp(a, b, f.x), lerp(c, e, f.x), f.y);+ inside[i] = here.x;+ }+ if (here.x > d)+ {+ d = here.x;+ hard = here.y;}- d = max(d, inside[i]);}}@@ -112,16 +128,28 @@ float4 main(float4 position : SV_Position) : SV_Target[unroll] for (uint k = 0; k < 32; ++k)if (k < count) opacity = max(opacity, Size[k].z * saturate(inside[k] / pixel));- // nothing at or under nought: the antialiasing ramps inward from the edge, never out of it+ // Nothing at or under nought: every ramp below runs inward from the edge, never out of it. A+ // range edge is a line antialiased over the pixel; a blocked one fades in over the soft width.+ hard = saturate(hard);float edge = saturate(d / pixel);+ float cover = lerp(smoothstep(0, Look2.x, d), edge, hard);float border = max(Look0.x, pixel);- float stroke = edge * saturate((border - d) / pixel + 0.5) * Look1.x;+ float stroke = edge * saturate((border - d) / pixel + 0.5) * Look1.x * lerp(Look2.y, 1, hard);+ float shadeEnd = border + max(Look3.z, pixel);+ float shade = saturate((d - border) / pixel + 0.5) * saturate((shadeEnd - d) / pixel + 0.5) * Look3.w * hard;++ // a faint band leaves the range line every period and runs inward, thinning as it goes+ float phase = frac(Look1.w / Look2.w);+ float r = (d - border - phase * Look3.x) / Look3.y;+ float ripple = exp(-r * r) * (1 - phase) * Look2.z * hard;+float band = saturate(1 - d / Look0.y);- float fill = (Look0.z + Look0.w * band * band) * edge;- float alpha = stroke + fill * (1 - stroke);- float3 colour = lerp(Colour.rgb, float3(1, 1, 1), Look1.y) * stroke + Colour.rgb * fill * (1 - stroke);+ float fill = saturate(Look0.z + Look0.w * band * band + ripple) * cover;+ float4 result = float4(Colour.rgb * fill, fill);+ result = result * (1 - shade) + float4(0, 0, 0, shade);+ result = result * (1 - stroke) + float4(lerp(Colour.rgb, float3(1, 1, 1), Look1.y) * stroke, stroke);if (d <= 0 || depth >= 1) discard;- return float4(colour, alpha) * opacity;+ return result * opacity;})HLSL";@@ -238,13 +266,20 @@ ID3D11ShaderResourceView *viewFor( const ReachOverlayField &field )description.Height = (UINT)field.height;description.MipLevels = 1;description.ArraySize = 1;- description.Format = DXGI_FORMAT_R32_FLOAT;+ description.Format = DXGI_FORMAT_R32G32_FLOAT;description.SampleDesc.Count = 1;description.Usage = D3D11_USAGE_IMMUTABLE;description.BindFlags = D3D11_BIND_SHADER_RESOURCE;+ const size_t samples = (size_t)field.width * field.height;+ std::vector< float > texels( samples * 2 );+ for( size_t k = 0; k < samples; ++k )+ {+ texels[ 2 * k ] = field.distance[ k ];+ texels[ 2 * k + 1 ] = k < field.hardness.size() ? field.hardness[ k ] : 1.0f;+ }D3D11_SUBRESOURCE_DATA data = {};- data.pSysMem = field.distance.data();- data.SysMemPitch = (UINT)( field.width * sizeof( Real ) );+ data.pSysMem = texels.data();+ data.SysMemPitch = (UINT)( field.width * 2 * sizeof( float ) );if( FAILED( shared.device->CreateTexture2D( &description, &data, &made.texture ) )|| FAILED( shared.device->CreateShaderResourceView( made.texture, nullptr, &made.view ) ) ){@@ -438,6 +473,16 @@ Bool DX11ReachOverlay_Draw( CameraClass &camera, const std::vector< const ReachOconstants.Look0[ 3 ] = ReachLook::BandAlpha;constants.Look1[ 0 ] = ReachLook::LineAlpha;constants.Look1[ 1 ] = ReachLook::LineWhite;+ // the render clock, wrapped so a float keeps its milliseconds; the ripple's period divides it+ constants.Look1[ 3 ] = (float)( WW3D::Get_Sync_Time() % 600000u ) * 0.001f;+ constants.Look2[ 0 ] = ReachLook::SoftWidth;+ constants.Look2[ 1 ] = ReachLook::SoftLine;+ constants.Look2[ 2 ] = ReachLook::RippleAlpha;+ constants.Look2[ 3 ] = ReachLook::RipplePeriod;+ constants.Look3[ 0 ] = ReachLook::RippleSpan;+ constants.Look3[ 1 ] = ReachLook::RippleWidth;+ constants.Look3[ 2 ] = ReachLook::ShadeWidth;+ constants.Look3[ 3 ] = ReachLook::ShadeAlpha;for( size_t start = 0; start < order.size(); ){diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DReachOverlay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DReachOverlay.cppindex dea65dc42..fd7628fcb 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DReachOverlay.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DReachOverlay.cpp@@ -60,32 +60,41 @@ Real clamp01( Real value )return value < 0.0f ? 0.0f : ( value > 1.0f ? 1.0f : value );}-// The field taken bilinearly at grid coordinates (gx, gy), both inside the grid.-Real fieldAt( const ReachOverlayField &field, Real gx, Real gy )+// One of the field's grids (distance or hardness) taken bilinearly at grid coordinates (gx, gy), both+// inside the grid.+Real fieldAt( const ReachOverlayField &field, const std::vector< Real > &grid, Real gx, Real gy ){Int i = (Int)floorf( gx ), j = (Int)floorf( gy );if( i > field.width - 2 ) i = field.width - 2;if( j > field.height - 2 ) j = field.height - 2;const Real fx = gx - (Real)i, fy = gy - (Real)j;- const Real *row = &field.distance[ j * field.width + i ];+ const Real *row = &grid[ j * field.width + i ];const Real bottom = row[ 0 ] + ( row[ 1 ] - row[ 0 ] ) * fx;const Real top = row[ field.width ] + ( row[ field.width + 1 ] - row[ field.width ] ) * fx;return bottom + ( top - bottom ) * fy;}-/** A baked texel at distance d inside the edge, as straight colour and alpha for a SRCALPHA blend.- * The lit texels start up to a texel and a half in (the nine-neighbour rule), so the line is laid- * from there, and at least two texels wide: at the border's own 1.5 units a coarse texel held half- * a line, which the filter smeared into the fade and no edge showed. */-UnsignedInt texelAt( Real d, Real texel, const Real owner[ 3 ] )+/** A baked texel at distance d inside the edge, as straight colour and alpha for a SRCALPHA blend,+ * the Direct3D 11 look without the ripple. The lit texels start up to a texel and a half in (the+ * nine-neighbour rule), so the line is laid from there, and at least two texels wide: at the+ * border's own 1.5 units a coarse texel held half a line, which the filter smeared into the fade and+ * no edge showed. hard is the field's hardness: 1 a range edge with its line, 0 a blocked edge that+ * fades in over the soft width. */+UnsignedInt texelAt( Real d, Real hard, Real texel, const Real owner[ 3 ] ){+ hard = clamp01( hard );const Real lineEnd = 1.5f * texel + ( ReachLook::BorderWidth > 2.0f * texel ? ReachLook::BorderWidth : 2.0f * texel );- const Real line = clamp01( ( lineEnd - d ) / texel ) * ReachLook::LineAlpha;+ const Real line = clamp01( ( lineEnd - d ) / texel ) * ReachLook::LineAlpha+ * ( ReachLook::SoftLine + ( 1.0f - ReachLook::SoftLine ) * hard );+ const Real shade = ( d >= lineEnd && d < lineEnd + texel ? ReachLook::ShadeAlpha * hard : 0.0f ) * ( 1.0f - line );+ Real soft = clamp01( d / ReachLook::SoftWidth );+ soft = soft * soft * ( 3.0f - 2.0f * soft );Real band = clamp01( 1.0f - d / ReachLook::BandWidth );band *= band;- const Real fill = ( ReachLook::FillAlpha + ReachLook::BandAlpha * band ) * ( 1.0f - line );- const Real alpha = line + fill;- UnsignedInt result = (UnsignedInt)( alpha * 255.0f + 0.5f ) << 24;+ const Real cover = soft + ( 1.0f - soft ) * hard;+ const Real fill = ( ReachLook::FillAlpha + ReachLook::BandAlpha * band ) * cover * ( 1.0f - line ) * ( 1.0f - shade );+ const Real alpha = line + shade + fill;+ UnsignedInt result = (UnsignedInt)( clamp01( alpha ) * 255.0f + 0.5f ) << 24;for( Int c = 0; c < 3; ++c ){const Real lineColour = owner[ c ] + ( 1.0f - owner[ c ] ) * ReachLook::LineWhite;@@ -113,10 +122,17 @@ Bool bake( const ReachOverlayField &field, Baked &baked )if( across > MaxBakedSide * 2 || down > MaxBakedSide * 2 )return FALSE; // ponytail: a field past 1024 samples a side is left out here; cut the grid if one ever comes- std::vector< Real > d( across * down );+ std::vector< Real > d( across * down ), hard( across * down, 1.0f );+ const Bool hasHardness = field.hardness.size() >= field.distance.size();for( Int y = 0; y < down; ++y )+ {for( Int x = 0; x < across; ++x )- d[ y * across + x ] = fieldAt( field, (Real)x / cuts, (Real)y / cuts );+ {+ d[ y * across + x ] = fieldAt( field, field.distance, (Real)x / cuts, (Real)y / cuts );+ if( hasHardness )+ hard[ y * across + x ] = fieldAt( field, field.hardness, (Real)x / cuts, (Real)y / cuts );+ }+ }baked.texel = field.step / cuts;baked.side[ 0 ] = powerOfTwo( across );@@ -135,7 +151,7 @@ Bool bake( const ReachOverlayField &field, Baked &baked )( field.color & 0xff ) / 255.0f };// what an unlit texel holds: no alpha and the line's colour, so the filter blends no other colour// into the edge it fades out on- const UnsignedInt unlit = texelAt( 0.0f, baked.texel, owner ) & 0x00FFFFFF;+ const UnsignedInt unlit = texelAt( 0.0f, 1.0f, baked.texel, owner ) & 0x00FFFFFF;for( Int y = 0; y < baked.side[ 1 ]; ++y ){UnsignedInt *row = (UnsignedInt *)( bits + y * pitch );@@ -145,7 +161,7 @@ Bool bake( const ReachOverlayField &field, Baked &baked )for( Int dy = -1; lit && dy <= 1; ++dy )for( Int dx = -1; lit && dx <= 1; ++dx )lit = d[ ( y + dy ) * across + x + dx ] > InsideMargin;- row[ x ] = lit ? texelAt( d[ y * across + x ], baked.texel, owner ) : unlit;+ row[ x ] = lit ? texelAt( d[ y * across + x ], hard[ y * across + x ], baked.texel, owner ) : unlit;}}surface->Unlock();