Simple MapEntity visibility culling (structures outside the camera view or items with nothing to render aren't drawn)
Could be optimized further, but already gives a pretty nice performance boost
This commit is contained in:
@@ -319,6 +319,11 @@ namespace Barotrauma
|
||||
mapEntityList.Insert(i, this);
|
||||
}
|
||||
|
||||
public virtual bool IsVisible(Rectangle worldView)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual void Draw(SpriteBatch spriteBatch, bool editing, bool back=true) {}
|
||||
|
||||
public virtual void DrawDamage(SpriteBatch spriteBatch, Effect damageEffect) {}
|
||||
|
||||
@@ -475,6 +475,15 @@ namespace Barotrauma
|
||||
if (convexHulls != null) convexHulls.ForEach(x => x.Remove());
|
||||
}
|
||||
|
||||
public override bool IsVisible(Rectangle WorldView)
|
||||
{
|
||||
Rectangle worldRect = WorldRect;
|
||||
|
||||
if (worldRect.X > WorldView.Right || worldRect.Right < WorldView.X) return false;
|
||||
if (worldRect.Y < WorldView.Y - WorldView.Height || worldRect.Y - worldRect.Height > WorldView.Y) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch spriteBatch, bool editing, bool back = true)
|
||||
{
|
||||
@@ -514,7 +523,7 @@ namespace Barotrauma
|
||||
spriteBatch,
|
||||
new Vector2(rect.X + drawOffset.X, -(rect.Y + drawOffset.Y)),
|
||||
new Vector2(rect.Width, rect.Height),
|
||||
Vector2.Zero, color, Point.Zero);
|
||||
color, Point.Zero);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -553,7 +562,7 @@ namespace Barotrauma
|
||||
spriteBatch,
|
||||
new Vector2(sections[i].rect.X + drawOffset.X, -(sections[i].rect.Y + drawOffset.Y)),
|
||||
new Vector2(sections[i].rect.Width, sections[i].rect.Height),
|
||||
Vector2.Zero, color,
|
||||
color,
|
||||
textureOffset, depth);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -289,10 +289,10 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
//drawing ----------------------------------------------------
|
||||
|
||||
|
||||
public static void CullEntities(Camera cam)
|
||||
{
|
||||
List<Submarine> visibleSubs = new List<Submarine>();
|
||||
HashSet<Submarine> visibleSubs = new HashSet<Submarine>();
|
||||
foreach (Submarine sub in Submarine.Loaded)
|
||||
{
|
||||
Rectangle worldBorders = new Rectangle(
|
||||
@@ -301,19 +301,20 @@ namespace Barotrauma
|
||||
sub.Borders.Width + 1000,
|
||||
sub.Borders.Height + 1000);
|
||||
|
||||
|
||||
if (Submarine.RectsOverlap(worldBorders, cam.WorldView))
|
||||
{
|
||||
visibleSubs.Add(sub);
|
||||
}
|
||||
}
|
||||
|
||||
Rectangle worldView = cam.WorldView;
|
||||
|
||||
visibleEntities = new List<MapEntity>();
|
||||
foreach (MapEntity me in MapEntity.mapEntityList)
|
||||
{
|
||||
if (me.Submarine == null || visibleSubs.Contains(me.Submarine))
|
||||
{
|
||||
visibleEntities.Add(me);
|
||||
if (me.IsVisible(worldView)) visibleEntities.Add(me);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user