From 756f3ead55777b294a032a1188e294a6d01b6147 Mon Sep 17 00:00:00 2001 From: austin Date: Wed, 30 Apr 2025 15:44:07 -0400 Subject: [PATCH] octree cache busting when sync teleport dist --- docs/src/progress/renderertodo.md | 1 + .../terrain/cells/ClientDrawCellManager.java | 16 ++++++++++++++ .../terrain/foliage/FoliageCellManager.java | 15 ++++++++++++- .../client/ui/menu/debug/ImGuiDrawCell.java | 7 +++++- .../physicssync/ClientPhysicsSyncTree.java | 22 ++++++++++++++++--- .../net/client/protocol/EntityProtocol.java | 4 ++++ .../net/client/protocol/PlayerProtocol.java | 4 ++-- 7 files changed, 62 insertions(+), 7 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 09c5806b..f2e93879 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1619,6 +1619,7 @@ Craftable wooden floor fab Using up charges destroys the item (including toolbar instances) Crafting can consume charges Products from crafting can add charges to existing items +Cache busting when physics sync pulls player entity TELEPORT distances diff --git a/src/main/java/electrosphere/client/terrain/cells/ClientDrawCellManager.java b/src/main/java/electrosphere/client/terrain/cells/ClientDrawCellManager.java index 669be126..d61bc593 100644 --- a/src/main/java/electrosphere/client/terrain/cells/ClientDrawCellManager.java +++ b/src/main/java/electrosphere/client/terrain/cells/ClientDrawCellManager.java @@ -147,6 +147,11 @@ public class ClientDrawCellManager { */ boolean initialized = false; + /** + * Used to bust the distance cache from external calls + */ + boolean bustDistCache = false; + /** * Constructor * @param voxelTextureAtlas The voxel texture atlas @@ -171,6 +176,10 @@ public class ClientDrawCellManager { Vector3d playerPos = EntityUtils.getPosition(Globals.playerEntity); Vector3i playerWorldPos = Globals.clientWorldData.convertRealToWorldSpace(playerPos); int distCache = this.getDistCache(this.lastPlayerPos, playerWorldPos); + if(this.bustDistCache){ + this.bustDistCache = false; + distCache = this.chunkTree.getMaxLevel(); + } this.lastPlayerPos.set(playerWorldPos); //the sets to iterate through updatedLastFrame = true; @@ -1084,6 +1093,13 @@ public class ClientDrawCellManager { return false; } + /** + * Busts the distance cache + */ + public void bustDistanceCache(){ + this.bustDistCache = true; + } + } diff --git a/src/main/java/electrosphere/client/terrain/foliage/FoliageCellManager.java b/src/main/java/electrosphere/client/terrain/foliage/FoliageCellManager.java index 45129cc5..bd9f154e 100644 --- a/src/main/java/electrosphere/client/terrain/foliage/FoliageCellManager.java +++ b/src/main/java/electrosphere/client/terrain/foliage/FoliageCellManager.java @@ -158,6 +158,11 @@ public class FoliageCellManager { */ List breakPoints = new LinkedList(); + /** + * Used to bust the distance cache from external calls + */ + boolean bustDistCache = false; + /** * Constructor * @param worldDim The size of the world in chunks @@ -193,8 +198,9 @@ public class FoliageCellManager { Vector3d playerPos = EntityUtils.getPosition(Globals.playerEntity); Vector3i absVoxelPos = Globals.clientWorldData.convertRealToAbsoluteVoxelSpace(playerPos); int distCache = this.getDistCache(this.lastPlayerPos, absVoxelPos); - if(absVoxelPos.distance(this.lastPlayerPos) > TELEPORT_DISTANCE){ + if(bustDistCache || absVoxelPos.distance(this.lastPlayerPos) > TELEPORT_DISTANCE){ distCache = BUST_META_CELLS; + bustDistCache = false; } this.lastPlayerPos.set(absVoxelPos); //the sets to iterate through @@ -926,6 +932,13 @@ public class FoliageCellManager { public void addBreakPoint(Vector3i absVoxelPos){ this.breakPoints.add(absVoxelPos); } + + /** + * Busts the distance cache + */ + public void bustDistanceCache(){ + this.bustDistCache = true; + } } diff --git a/src/main/java/electrosphere/client/ui/menu/debug/ImGuiDrawCell.java b/src/main/java/electrosphere/client/ui/menu/debug/ImGuiDrawCell.java index 62655f9d..6c33fc6f 100644 --- a/src/main/java/electrosphere/client/ui/menu/debug/ImGuiDrawCell.java +++ b/src/main/java/electrosphere/client/ui/menu/debug/ImGuiDrawCell.java @@ -63,11 +63,16 @@ public class ImGuiDrawCell { LoggerInterface.loggerEngine.WARNING("Chunk not in cache! " + cameraWorldPos); } } - if(ImGui.button("Debug FoliageCell at camera position")){ + if(ImGui.button("Print debug info for FoliageCell at camera position")){ Vector3i cameraWorldPos = Globals.clientWorldData.convertRealToWorldSpace(CameraEntityUtils.getCameraCenter(Globals.playerCamera)); FoliageCell cell = Globals.foliageCellManager.getFoliageCell(cameraWorldPos.x, cameraWorldPos.y, cameraWorldPos.z); LoggerInterface.loggerEngine.WARNING("" + cell); } + if(ImGui.button("Debug FoliageCell evaluation at camera position")){ + Vector3i cameraWorldPos = Globals.clientWorldData.convertRealToWorldSpace(CameraEntityUtils.getCameraCenter(Globals.playerCamera)); + FoliageCell cell = Globals.foliageCellManager.getFoliageCell(cameraWorldPos.x, cameraWorldPos.y, cameraWorldPos.z); + cell.setTripDebug(true); + } if(ImGui.button("Request terrain at camera position")){ Vector3i cameraWorldPos = Globals.clientWorldData.convertRealToWorldSpace(CameraEntityUtils.getCameraCenter(Globals.playerCamera)); Globals.clientTerrainManager.requestChunk(cameraWorldPos.x, cameraWorldPos.y, cameraWorldPos.z, 1); diff --git a/src/main/java/electrosphere/entity/state/physicssync/ClientPhysicsSyncTree.java b/src/main/java/electrosphere/entity/state/physicssync/ClientPhysicsSyncTree.java index 2577d919..f3b328c2 100644 --- a/src/main/java/electrosphere/entity/state/physicssync/ClientPhysicsSyncTree.java +++ b/src/main/java/electrosphere/entity/state/physicssync/ClientPhysicsSyncTree.java @@ -5,6 +5,7 @@ import org.joml.Vector3d; import org.ode4j.ode.DBody; import electrosphere.client.entity.camera.CameraEntityUtils; +import electrosphere.client.terrain.foliage.FoliageCellManager; import electrosphere.collision.PhysicsEntityUtils; import electrosphere.collision.PhysicsUtils; import electrosphere.engine.Globals; @@ -20,13 +21,19 @@ import electrosphere.net.parser.net.message.EntityMessage; */ public class ClientPhysicsSyncTree implements BehaviorTree { - //The parent entity for the tree + /** + * The parent entity for the tree + */ Entity parent; - //The most recent message received from the client + /** + * The most recent message received from the client + */ EntityMessage latestMessage = null; - //checks if the message has been pushed to the physics engine or not + /** + * checks if the message has been pushed to the physics engine or not + */ boolean hasPushesMessage = true; /** @@ -52,6 +59,15 @@ public class ClientPhysicsSyncTree implements BehaviorTree { Vector3d angularForce = new Vector3d(); DBody body = PhysicsEntityUtils.getDBody(parent); + // + //bust distance caches if this is the player's entity and we've traveled a long distance suddenly + if(parent == Globals.playerEntity){ + if(position.distance(EntityUtils.getPosition(parent)) > FoliageCellManager.TELEPORT_DISTANCE){ + Globals.clientDrawCellManager.bustDistanceCache(); + Globals.foliageCellManager.bustDistanceCache(); + } + } + // //Synchronize data EntityUtils.getPosition(parent).set(position); diff --git a/src/main/java/electrosphere/net/client/protocol/EntityProtocol.java b/src/main/java/electrosphere/net/client/protocol/EntityProtocol.java index 6a4a8762..b6c7c81c 100644 --- a/src/main/java/electrosphere/net/client/protocol/EntityProtocol.java +++ b/src/main/java/electrosphere/net/client/protocol/EntityProtocol.java @@ -87,6 +87,9 @@ public class EntityProtocol implements ClientProtocolTemplate { case COMMON: { EntityProtocol.spawnCommon(message); } break; + case ENGINE: { + throw new Error("Unsupported entity type!"); + } } Globals.clientConnection.release(message); } break; @@ -159,6 +162,7 @@ public class EntityProtocol implements ClientProtocolTemplate { // TODO // // + case INTERACT: case UPDATEENTITYVIEWDIR: case KILL: //to be implemented diff --git a/src/main/java/electrosphere/net/client/protocol/PlayerProtocol.java b/src/main/java/electrosphere/net/client/protocol/PlayerProtocol.java index 1ebe1fad..f2bdef52 100644 --- a/src/main/java/electrosphere/net/client/protocol/PlayerProtocol.java +++ b/src/main/java/electrosphere/net/client/protocol/PlayerProtocol.java @@ -26,9 +26,9 @@ public class PlayerProtocol implements ClientProtocolTemplate { Globals.clientPlayer = new Player(message.getplayerID(), Player.CLIENT_DB_ID); LoggerInterface.loggerNetworking.DEBUG("[CLIENT] Player ID is " + Globals.clientPlayer.getId()); break; - case SETINITIALDISCRETEPOSITION: + case SETINITIALDISCRETEPOSITION: { Globals.clientPlayerData.setWorldPos(new Vector3i(message.getinitialDiscretePositionX(), message.getinitialDiscretePositionY(), message.getinitialDiscretePositionZ())); - break; + } break; } Globals.profiler.endCpuSample(); }