Skip to content

Commit

Permalink
Fix ship render chunks getting added multiple times, causing fps lag
Browse files Browse the repository at this point in the history
  • Loading branch information
StewStrong committed Mar 10, 2025
1 parent 289933e commit a508722
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.valkyrienskies.core.api.ships.ClientShip;
import org.valkyrienskies.core.util.datastructures.BlockPos2ByteOpenHashMap;
import org.valkyrienskies.mod.common.VSClientGameUtils;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.hooks.VSGameEvents;
Expand Down Expand Up @@ -70,7 +71,7 @@ public abstract class MixinLevelRendererVanilla implements LevelRendererVanillaD
private Minecraft minecraft;

@Unique
private ObjectArrayList<RenderChunkInfo> renderChunksGeneratedByVanilla = new ObjectArrayList<>();
private BlockPos2ByteOpenHashMap vs$visibileShipChunks = new BlockPos2ByteOpenHashMap();

/**
* Fix the distance to render chunks, so that MC doesn't think ship chunks are too far away
Expand Down Expand Up @@ -121,8 +122,6 @@ private void preSetupRender(final Camera camera, final Frustum frustum, final bo

@Override
public void vs$addShipVisibleChunks(final Frustum frustum) {
renderChunksGeneratedByVanilla = new ObjectArrayList<>(renderChunksInFrustum);

final BlockPos.MutableBlockPos tempPos = new BlockPos.MutableBlockPos();
final ViewAreaAccessor chunkStorageAccessor = (ViewAreaAccessor) viewArea;
for (final ClientShip shipObject : VSGameUtilsKt.getShipObjectWorld(level).getLoadedShips()) {
Expand All @@ -134,6 +133,10 @@ private void preSetupRender(final Camera camera, final Frustum frustum, final bo
shipObject.getActiveChunksSet().forEach((x, z) -> {
final LevelChunk levelChunk = level.getChunk(x, z);
for (int y = level.getMinSection(); y < level.getMaxSection(); y++) {
// Don't add ship chunks more than once
if (vs$visibileShipChunks.contains(x, y, z)) {
continue;
}
tempPos.set(x << 4, y << 4, z << 4);
final ChunkRenderDispatcher.RenderChunk renderChunk =
chunkStorageAccessor.callGetRenderChunkAt(tempPos);
Expand Down Expand Up @@ -162,6 +165,7 @@ private void preSetupRender(final Camera camera, final Frustum frustum, final bo
RenderChunkInfoAccessor.vs$new(renderChunk, null, 0);
}
shipRenderChunks.computeIfAbsent(shipObject, k -> new ObjectArrayList<>()).add(newChunkInfo);
vs$visibileShipChunks.put(x, y, z, (byte) 1);
renderChunksInFrustum.add(newChunkInfo);
}
}
Expand All @@ -178,6 +182,7 @@ private void preSetupRender(final Camera camera, final Frustum frustum, final bo
)
private void clearShipChunks(final CallbackInfo ci) {
shipRenderChunks.forEach((ship, chunks) -> chunks.clear());
vs$visibileShipChunks = new BlockPos2ByteOpenHashMap();
}

@WrapOperation(
Expand All @@ -191,10 +196,7 @@ private void redirectRenderChunkLayer(final LevelRenderer receiver,
final RenderType renderType, final PoseStack poseStack, final double camX, final double camY, final double camZ,
final Matrix4f matrix4f, final Operation<Void> renderChunkLayer) {

final var originalRenderChunks = renderChunksInFrustum;
renderChunksInFrustum = renderChunksGeneratedByVanilla;
renderChunkLayer.call(receiver, renderType, poseStack, camX, camY, camZ, matrix4f);
renderChunksInFrustum = originalRenderChunks;

VSGameEvents.INSTANCE.getShipsStartRendering().emit(new VSGameEvents.ShipStartRenderEvent(
receiver, renderType, poseStack, camX, camY, camZ, matrix4f
Expand Down Expand Up @@ -312,5 +314,4 @@ private void renderChunkLayer(final RenderType renderType, final PoseStack poseS
this.minecraft.getProfiler().pop();
renderType.clearRenderState();
}

}

0 comments on commit a508722

Please sign in to comment.