From b2417957d67278ac3c0951f92edc97ce074e1353 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Mon, 1 Feb 2021 08:38:30 -0600 Subject: [PATCH] Have the stored regions size change based on view distance also fix/improve the profiler --- .../backsun/lod/objects/LodDimension.java | 20 +++++++++++-- .../java/backsun/lod/objects/LodWorld.java | 20 +++++++++---- .../java/backsun/lod/proxy/ClientProxy.java | 30 +++++++++++++++++-- .../backsun/lod/renderer/LodRenderer.java | 14 +++++---- 4 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/main/java/backsun/lod/objects/LodDimension.java b/src/main/java/backsun/lod/objects/LodDimension.java index 97c2c98fe..1fd7dfb8a 100644 --- a/src/main/java/backsun/lod/objects/LodDimension.java +++ b/src/main/java/backsun/lod/objects/LodDimension.java @@ -9,14 +9,14 @@ import net.minecraft.world.DimensionType; * for a given dimension. * * @author James Seibel - * @version 01-30-2021 + * @version 01-31-2021 */ public class LodDimension { public final DimensionType dimension; - private int width; // if this ever changes make sure to update the halfWidth too - private int halfWidth; + private volatile int width; // if this ever changes make sure to update the halfWidth too + private volatile int halfWidth; public LodRegion regions[][]; public boolean isRegionDirty[][]; @@ -295,6 +295,20 @@ public class LodDimension { return width; } + + public void setRegionWidth(int newWidth) + { + width = newWidth; + halfWidth = (int)Math.floor(width / 2); + + regions = new LodRegion[width][width]; + isRegionDirty = new boolean[width][width]; + + // populate isRegionDirty + for(int i = 0; i < width; i++) + for(int j = 0; j < width; j++) + isRegionDirty[i][j] = false; + } } diff --git a/src/main/java/backsun/lod/objects/LodWorld.java b/src/main/java/backsun/lod/objects/LodWorld.java index b2d2013fd..e31af7ed1 100644 --- a/src/main/java/backsun/lod/objects/LodWorld.java +++ b/src/main/java/backsun/lod/objects/LodWorld.java @@ -1,13 +1,14 @@ package backsun.lod.objects; import java.util.Dictionary; +import java.util.Enumeration; import java.util.Hashtable; /** * This stores all LODs for a given world. * * @author James Seibel - * @version 01-30-2021 + * @version 01-31-2021 */ public class LodWorld { @@ -16,24 +17,33 @@ public class LodWorld /** * Key = Dimension id (as an int) */ - private Dictionary LodDimensions; + private Dictionary lodDimensions; public LodWorld(String newWorldName) { worldName = newWorldName; - LodDimensions = new Hashtable(); + lodDimensions = new Hashtable(); } public void addLodDimension(LodDimension newStorage) { - LodDimensions.put(newStorage.dimension.getId(), newStorage); + lodDimensions.put(newStorage.dimension.getId(), newStorage); } public LodDimension getLodDimension(int dimensionId) { - return LodDimensions.get(dimensionId); + return lodDimensions.get(dimensionId); + } + + + public void resizeDimensionRegionWidth(int newWidth) + { + Enumeration keys = lodDimensions.keys(); + + while(keys.hasMoreElements()) + lodDimensions.get(keys.nextElement()).setRegionWidth(newWidth); } } diff --git a/src/main/java/backsun/lod/proxy/ClientProxy.java b/src/main/java/backsun/lod/proxy/ClientProxy.java index 6671a8c4e..24cf13e7f 100644 --- a/src/main/java/backsun/lod/proxy/ClientProxy.java +++ b/src/main/java/backsun/lod/proxy/ClientProxy.java @@ -15,6 +15,7 @@ import net.minecraft.world.DimensionType; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.storage.ExtendedBlockStorage; import net.minecraftforge.client.event.EntityViewRenderEvent; +import net.minecraftforge.client.event.RenderWorldLastEvent; import net.minecraftforge.event.terraingen.PopulateChunkEvent; import net.minecraftforge.event.world.ChunkEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @@ -23,7 +24,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; * This is used by the client. * * @author James_Seibel - * @version 01-30-2021 + * @version 01-31-2021 */ public class ClientProxy extends CommonProxy { @@ -31,7 +32,6 @@ public class ClientProxy extends CommonProxy private LodWorld lodWorld; private ExecutorService lodGenThreadPool = Executors.newFixedThreadPool(1); - // TODO make this change dynamically based on the render distance private int regionWidth = 5; public ClientProxy() @@ -46,9 +46,35 @@ public class ClientProxy extends CommonProxy // render event // //==============// + /** prevent LODs from being rendered multiple times */ + private boolean frameRendered = false; + + @SubscribeEvent + public void onRenderTick(RenderWorldLastEvent event) + { + frameRendered = false; + } + @SubscribeEvent public void onRenderTick(EntityViewRenderEvent.FogDensity event) { + // this event is called 5 times every frame + // but we only need to render the LODs once + if (frameRendered) + return; + frameRendered = true; + +// int newWidth = Math.max(3, (Minecraft.getMinecraft().gameSettings.renderDistanceChunks * LodRenderer.VIEW_DISTANCE_MULTIPLIER) / LodRegion.SIZE); +// if (lodWorld != null && regionWidth != newWidth) +// { +// lodWorld.resizeDimensionRegionWidth(newWidth); +// regionWidth = newWidth; +// +// // skip this frame, hopefully the lodWorld +// // should have everything set up by then +// return; +// } + Minecraft mc = Minecraft.getMinecraft(); int dimId = mc.player.dimension; LodDimension lodDim = lodWorld.getLodDimension(dimId); diff --git a/src/main/java/backsun/lod/renderer/LodRenderer.java b/src/main/java/backsun/lod/renderer/LodRenderer.java index 7713f9f96..f934e708e 100644 --- a/src/main/java/backsun/lod/renderer/LodRenderer.java +++ b/src/main/java/backsun/lod/renderer/LodRenderer.java @@ -81,7 +81,9 @@ public class LodRenderer // used for debugging and viewing how long different processes take - mc.world.profiler.startSection("LOD setup"); + mc.mcProfiler.endSection(); + mc.mcProfiler.startSection("LOD"); + mc.mcProfiler.startSection("LOD setup"); @SuppressWarnings("unused") long startTime = System.nanoTime(); @@ -150,7 +152,7 @@ public class LodRenderer // create the LODs // //=================// - mc.world.profiler.endStartSection("LOD generation"); + mc.mcProfiler.endStartSection("LOD generation"); // TODO multithread this @@ -263,7 +265,7 @@ public class LodRenderer // rendering // //===========// - mc.world.profiler.endStartSection("LOD build buffer"); + mc.mcProfiler.endStartSection("LOD build buffer"); // send the LODs over to the GPU sendToGPUAndDraw(lodArray, colorArray, cameraX, cameraY ,cameraZ); @@ -278,7 +280,7 @@ public class LodRenderer // cleanup // //=========// - mc.world.profiler.endStartSection("LOD cleanup"); + mc.mcProfiler.endStartSection("LOD cleanup"); // this must be done otherwise other parts of the screen may be drawn with a fog effect @@ -299,7 +301,7 @@ public class LodRenderer long endTime = System.nanoTime(); // end of profiler tracking - mc.world.profiler.endSection(); + mc.mcProfiler.endSection(); } @@ -415,7 +417,7 @@ public class LodRenderer colorIndex++; } - mc.world.profiler.endStartSection("LOD draw"); + mc.mcProfiler.endStartSection("LOD draw"); // draw the LODs tessellator.draw();