diff --git a/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/RenderSectionManager.java b/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/RenderSectionManager.java index a3380f051b..2bb0585714 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/RenderSectionManager.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/RenderSectionManager.java @@ -122,7 +122,7 @@ private void createTerrainRenderList(Camera camera, Viewport viewport, int frame this.occlusionCuller.findVisible(visitor, viewport, searchDistance, useOcclusionCulling, frame); - this.renderLists = visitor.createRenderLists(); + this.renderLists = visitor.createRenderLists(camera.getPos()); this.rebuildLists = visitor.getRebuildLists(); } diff --git a/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/lists/ChunkRenderList.java b/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/lists/ChunkRenderList.java index 157f03a567..6380864872 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/lists/ChunkRenderList.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/lists/ChunkRenderList.java @@ -24,6 +24,8 @@ public class ChunkRenderList { private int lastVisibleFrame; + private double distanceFromCamera; + public ChunkRenderList(RenderRegion region) { this.region = region; } @@ -101,4 +103,14 @@ public RenderRegion getRegion() { return this.region; } + /** + * Get the squared distance of this region from the camera. + */ + public double getDistanceFromCamera() { + return this.distanceFromCamera; + } + + public void setDistanceFromCamera(double d) { + this.distanceFromCamera = d; + } } diff --git a/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/lists/SortedRenderLists.java b/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/lists/SortedRenderLists.java index 7c28e941ab..fabfbb2575 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/lists/SortedRenderLists.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/lists/SortedRenderLists.java @@ -4,6 +4,9 @@ import me.jellysquid.mods.sodium.client.render.chunk.RenderSection; import me.jellysquid.mods.sodium.client.util.iterator.ReversibleObjectArrayIterator; import me.jellysquid.mods.sodium.client.render.chunk.region.RenderRegion; +import net.minecraft.util.math.Vec3d; + +import java.util.Comparator; public class SortedRenderLists implements ChunkRenderListIterable { private static final SortedRenderLists EMPTY = new SortedRenderLists(ObjectArrayList.of()); @@ -24,6 +27,7 @@ public static SortedRenderLists empty() { } public static class Builder { + private static final Comparator LIST_DISTANCE_COMPARATOR = Comparator.comparingDouble(ChunkRenderList::getDistanceFromCamera); private final ObjectArrayList lists = new ObjectArrayList<>(); private final int frame; @@ -44,7 +48,17 @@ public void add(RenderSection section) { list.add(section); } - public SortedRenderLists build() { + public SortedRenderLists build(Vec3d cameraPos) { + //noinspection ForLoopReplaceableByForEach + for (int i = 0; i < this.lists.size(); i++) { + ChunkRenderList renderList = this.lists.get(i); + RenderRegion region = renderList.getRegion(); + double dx = cameraPos.x - region.getOriginX(); + double dy = cameraPos.y - region.getOriginY(); + double dz = cameraPos.z - region.getOriginZ(); + renderList.setDistanceFromCamera((dx * dx) + (dy * dy) + (dz * dz)); + } + this.lists.sort(LIST_DISTANCE_COMPARATOR); return new SortedRenderLists(this.lists); } } diff --git a/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/lists/VisibleChunkCollector.java b/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/lists/VisibleChunkCollector.java index a294766343..225429aad8 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/lists/VisibleChunkCollector.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/render/chunk/lists/VisibleChunkCollector.java @@ -2,6 +2,7 @@ import me.jellysquid.mods.sodium.client.render.chunk.ChunkUpdateType; import me.jellysquid.mods.sodium.client.render.chunk.RenderSection; +import net.minecraft.util.math.Vec3d; import java.util.ArrayDeque; import java.util.EnumMap; @@ -43,8 +44,8 @@ private void addToRebuildLists(RenderSection section) { } } - public SortedRenderLists createRenderLists() { - return this.sortedRenderLists.build(); + public SortedRenderLists createRenderLists(Vec3d cameraPos) { + return this.sortedRenderLists.build(cameraPos); } public Map> getRebuildLists() {