From efa2afa822d0c92e609396adec2a4d4655189cfb Mon Sep 17 00:00:00 2001 From: austin Date: Sun, 25 Feb 2024 21:16:29 -0500 Subject: [PATCH] Client entity culling --- .../client/scene/ClientSceneWrapper.java | 19 +++++++++++++++++++ .../client/sim/ClientFunctions.java | 1 + .../client/terrain/cells/DrawCellManager.java | 9 +++++++++ 3 files changed, 29 insertions(+) diff --git a/src/main/java/electrosphere/client/scene/ClientSceneWrapper.java b/src/main/java/electrosphere/client/scene/ClientSceneWrapper.java index af0eb824..e85c7e4e 100644 --- a/src/main/java/electrosphere/client/scene/ClientSceneWrapper.java +++ b/src/main/java/electrosphere/client/scene/ClientSceneWrapper.java @@ -3,10 +3,16 @@ package electrosphere.client.scene; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.joml.Vector3d; + import electrosphere.collision.CollisionEngine; +import electrosphere.engine.Globals; +import electrosphere.entity.ClientEntityUtils; import electrosphere.entity.Entity; +import electrosphere.entity.EntityUtils; import electrosphere.entity.Scene; import electrosphere.logger.LoggerInterface; +import electrosphere.server.terrain.manager.ServerTerrainChunk; /** * Wrapper around the scene object to provide lots of much needed client-specific utility @@ -94,4 +100,17 @@ public class ClientSceneWrapper { return collisionEngine; } + /** + * Destroys all entities outside simulation range + */ + public void destroyEntitiesOutsideSimRange(){ + double cullRadius = Globals.drawCellManager.getDrawRadius() + ServerTerrainChunk.CHUNK_DIMENSION; + for(Entity entity : scene.getEntityList()){ + Vector3d position = EntityUtils.getPosition(entity); + if(Globals.playerEntity != null && EntityUtils.getPosition(Globals.playerEntity).distance(position) > cullRadius){ + EntityUtils.cleanUpEntity(entity); + } + } + } + } diff --git a/src/main/java/electrosphere/client/sim/ClientFunctions.java b/src/main/java/electrosphere/client/sim/ClientFunctions.java index 582e8898..03d8c2cb 100644 --- a/src/main/java/electrosphere/client/sim/ClientFunctions.java +++ b/src/main/java/electrosphere/client/sim/ClientFunctions.java @@ -27,6 +27,7 @@ public class ClientFunctions { public static void runClientFunctions(){ ClientTerrainManager.generateTerrainChunkGeometry(); updateSkyboxPos(); + Globals.clientSceneWrapper.destroyEntitiesOutsideSimRange(); // updateCellManager(); } diff --git a/src/main/java/electrosphere/client/terrain/cells/DrawCellManager.java b/src/main/java/electrosphere/client/terrain/cells/DrawCellManager.java index 69776fd3..4812a01b 100644 --- a/src/main/java/electrosphere/client/terrain/cells/DrawCellManager.java +++ b/src/main/java/electrosphere/client/terrain/cells/DrawCellManager.java @@ -423,6 +423,15 @@ public class DrawCellManager { } + /** + * Gets the draw radius + * @return the draw radius + */ + public double getDrawRadius(){ + return drawRadius; + } + + // public