@@ -347,6 +347,10 @@ local initiated = false
347347local featuresDefsWithAlpha = {} -- featureDefIDs (ie trees) that should be drawn with alpha.
348348local unitDefsUseSkinning = {} -- unitDefIDs that use the bones and stretchy skin system
349349
350+ local ENEMY_CLOAK_FRAMES = false -- 4 -- How many extra frames to show enemy units for.
351+ local cloakDeferFrame = {}
352+ local keepDrawFlagChecking = false -- a list of units that need draw flag checking
353+
350354local spGetUnitIsCloaked = Spring .GetUnitIsCloaked
351355
352356---- ----------------------------------------------------------------
@@ -1421,47 +1425,65 @@ local function RemoveObject(objectID, reason) -- we get pos/neg objectID here
14211425 end
14221426end
14231427
1424- local function ProcessUnits (units , drawFlags , reason )
1425- for i = 1 , # units do
1426- local unitID = units [i ]
1427- local drawFlag = drawFlags [i ]
1428- if debugmode then Spring .Echo (" ProcessUnits" , unitID , drawFlag , reason ) end
1428+ local function ProcessCusUnit (unitID , drawFlag , gameFrame , reason )
1429+ if debugmode then Spring .Echo (" ProcessUnits" , unitID , drawFlag , reason ) end
14291430
1430- if math_bit_and (drawFlag , 34 ) > 0 then -- has alpha (2) or alphashadow(32) flag
1431- -- cloaked units get mapped to pure forward + deferred, no refl/refr either
1431+ if cloakDeferFrame [unitID ] and ((not drawFlag ) or drawFlag == 0 ) then
1432+ if cloakDeferFrame [unitID ] < gameFrame then
1433+ cloakDeferFrame [unitID ] = nil
1434+ end
1435+ if not Spring .ValidUnitID (unitID ) then
1436+ return
1437+ end
1438+ if cloakDeferFrame [unitID ] then
14321439 drawFlag = 1
1440+ keepDrawFlagChecking = keepDrawFlagChecking or {}
1441+ keepDrawFlagChecking [# keepDrawFlagChecking + 1 ] = unitID
14331442 end
1434-
1435- if drawFlag % 4 > 1 then -- check if its at least in opaque or alpha pass
1436- if not unitsInViewport [unitID ] then
1437- -- CALL the UnitViewportAPI
1438- numUnitsInViewport = numUnitsInViewport + 1
1439- end
1440- unitsInViewport [unitID ] = drawFlag
1441- else
1442- if unitsInViewport [unitID ] then
1443- -- CALL the UnitViewportAPI
1444- numUnitsInViewport = numUnitsInViewport - 1
1445- end
1446- unitsInViewport [unitID ] = nil
1443+ end
1444+ if not drawFlag then
1445+ drawFlag = Spring .GetUnitDrawFlag (unitID )
1446+ end
1447+
1448+ if math_bit_and (drawFlag , 34 ) > 0 then -- has alpha (2) or alphashadow(32) flag
1449+ -- cloaked units get mapped to pure forward + deferred, no refl/refr either
1450+ drawFlag = 1
1451+ end
1452+
1453+ if drawFlag % 4 > 1 then -- check if its at least in opaque or alpha pass
1454+ if not unitsInViewport [unitID ] then
1455+ -- CALL the UnitViewportAPI
1456+ numUnitsInViewport = numUnitsInViewport + 1
14471457 end
1448-
1449- if (drawFlag == 0 ) or (drawFlag >= 128 ) then
1450- RemoveObject (unitID , reason )
1451- else
1452- if not cusUnitIDtoDrawFlag [unitID ] then -- object was not seen
1453- if Spring .ValidUnitID (unitID ) and (not spGetUnitIsCloaked (unitID )) then
1454- uniformCache [1 ] = 0
1455- gl .SetUnitBufferUniforms (unitID , uniformCache , 12 ) -- cloak
1456- end
1457- AddObject (unitID , drawFlag , reason )
1458- elseif cusUnitIDtoDrawFlag [unitID ] ~= drawFlag then -- flags have changed
1459- UpdateObject (unitID , drawFlag , reason )
1460- end
1458+ unitsInViewport [unitID ] = drawFlag
1459+ else
1460+ if unitsInViewport [unitID ] then
1461+ -- CALL the UnitViewportAPI
1462+ numUnitsInViewport = numUnitsInViewport - 1
1463+ end
1464+ unitsInViewport [unitID ] = nil
1465+ end
1466+
1467+ if (drawFlag == 0 ) or (drawFlag >= 128 ) then
1468+ RemoveObject (unitID , reason )
1469+ else
1470+ if not cusUnitIDtoDrawFlag [unitID ] then -- object was not seen
1471+ -- if Spring.ValidUnitID(unitID) and (not spGetUnitIsCloaked(unitID)) then
1472+ -- uniformCache[1] = 0
1473+ -- gl.SetUnitBufferUniforms(unitID, uniformCache, 12) -- cloak
1474+ -- end
1475+ AddObject (unitID , drawFlag , reason )
1476+ elseif cusUnitIDtoDrawFlag [unitID ] ~= drawFlag then -- flags have changed
1477+ UpdateObject (unitID , drawFlag , reason )
14611478 end
14621479 end
14631480end
14641481
1482+ local function ProcessUnits (units , drawFlags , gameFrame , reason )
1483+ for i = 1 , # units do
1484+ ProcessCusUnit (units [i ], drawFlags and drawFlags [i ], gameFrame , reason )
1485+ end
1486+ end
14651487
14661488local function ProcessFeatures (features , drawFlags , reason )
14671489 for i = 1 , # features do
@@ -2041,19 +2063,35 @@ function gadget:UnitGiven(unitID)
20412063end
20422064
20432065function gadget :UnitCloaked (unitID )
2044- uniformCache [1 ] = Spring .GetGameFrame ()
2045- gl .SetUnitBufferUniforms (unitID , uniformCache , 12 )
2046- wantTranparent [unitID ] = true
2066+ local frame = Spring .GetGameFrame ()
2067+ if ENEMY_CLOAK_FRAMES then
2068+ local _ , fullview = Spring .GetSpectatingState ()
2069+ local allyUnit = fullview or Spring .IsUnitAllied (unitID )
2070+ uniformCache [1 ] = frame + (allyUnit and 0 or 1000000 )
2071+ gl .SetUnitBufferUniforms (unitID , uniformCache , 12 )
2072+ wantTranparent [unitID ] = true
2073+ if not allyUnit then
2074+ keepDrawFlagChecking = keepDrawFlagChecking or {}
2075+ keepDrawFlagChecking [# keepDrawFlagChecking + 1 ] = unitID
2076+ cloakDeferFrame [unitID ] = frame + ENEMY_CLOAK_FRAMES
2077+ end
2078+ else
2079+ uniformCache [1 ] = frame
2080+ gl .SetUnitBufferUniforms (unitID , uniformCache , 12 )
2081+ wantTranparent [unitID ] = true
2082+ end
20472083 UpdateUnit (unitID , Spring .GetUnitDrawFlag (unitID ))
20482084 if debugmode then
20492085 Spring .Echo (" UnitCloaked" , unitID , Spring .GetUnitDrawFlag (unitID ))
20502086 end
20512087end
20522088
20532089function gadget :UnitDecloaked (unitID )
2090+ local _ , fullview = Spring .GetSpectatingState ()
2091+ local allyUnit = fullview or Spring .IsUnitAllied (unitID )
20542092 wantTranparent [unitID ] = false
20552093 UpdateUnit (unitID , Spring .GetUnitDrawFlag (unitID ))
2056- uniformCache [1 ] = - 1 * Spring .GetGameFrame ()
2094+ uniformCache [1 ] = - 1 * Spring .GetGameFrame () - ( allyUnit and 0 or 1000000 )
20572095 gl .SetUnitBufferUniforms (unitID , uniformCache , 12 )
20582096 if debugmode then
20592097 Spring .Echo (" UnitDecloaked" , unitID , Spring .GetUnitDrawFlag (unitID ))
@@ -2074,6 +2112,7 @@ function gadget:DrawWorldPreUnit()
20742112 end
20752113
20762114 updateframe = (updateframe + 1 ) % updaterate
2115+ local gameFrame = Spring .GetGameFrame ()
20772116 if updateframe == 0 then
20782117 local t0 = Spring .GetTimerMicros ()
20792118 local units , drawFlagsUnits , features , drawFlagsFeatures
@@ -2106,12 +2145,15 @@ function gadget:DrawWorldPreUnit()
21062145 -- Spring.Echo(printDrawPassStats())
21072146 -- end
21082147 local totalobjects = # units + # features + numdestroyedUnits + numdestroyedFeatures
2148+ if keepDrawFlagChecking then
2149+ totalobjects = totalobjects + # keepDrawFlagChecking
2150+ end
21092151
21102152 -- Why do we also do this processing round if #units > 0?
21112153 if debugmode and (# destroyedUnitIDs > 0 or # units > 0 ) then Spring .Echo (" Processing destroyedUnitIDs" , # units , # destroyedUnitIDs ) end
21122154
21132155 if numdestroyedUnits > 0 then
2114- ProcessUnits (destroyedUnitIDs , destroyedUnitDrawFlags , " destroyed" )
2156+ ProcessUnits (destroyedUnitIDs , destroyedUnitDrawFlags , gameFrame , " destroyed" )
21152157 for i = numdestroyedUnits , 1 , - 1 do
21162158 destroyedUnitIDs [i ] = nil
21172159 destroyedUnitDrawFlags [i ] = nil
@@ -2147,11 +2189,16 @@ function gadget:DrawWorldPreUnit()
21472189 for i , unitID in ipairs (firstunits ) do
21482190 firstdrawFlagsUnits [i ] = 1 + 4 + 16
21492191 end
2150- ProcessUnits (firstunits , firstdrawFlagsUnits , " firstDraw" )
2192+ ProcessUnits (firstunits , firstdrawFlagsUnits , gameFrame , " firstDraw" )
21512193 firstDraw = false
21522194 end
21532195
2154- ProcessUnits (units , drawFlagsUnits , " changed" )
2196+ ProcessUnits (units , drawFlagsUnits , gameFrame , " changed" )
2197+ if keepDrawFlagChecking then
2198+ local checkUnits = keepDrawFlagChecking
2199+ keepDrawFlagChecking = false
2200+ ProcessUnits (checkUnits , false , gameFrame , " changed" )
2201+ end
21552202 ProcessFeatures (features , drawFlagsFeatures , " changed" )
21562203
21572204 local deltat = Spring .DiffTimers (Spring .GetTimerMicros (), t0 , nil ) -- in ms
0 commit comments