Skip to content

Commit

Permalink
Sort render regions by distance from camera
Browse files Browse the repository at this point in the history
Fixes some edge cases like #2132
  • Loading branch information
embeddedt committed Oct 21, 2023
1 parent dff676d commit 4349eb8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class ChunkRenderList {

private int lastVisibleFrame;

private double distanceFromCamera;

public ChunkRenderList(RenderRegion region) {
this.region = region;
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -24,6 +27,7 @@ public static SortedRenderLists empty() {
}

public static class Builder {
private static final Comparator<ChunkRenderList> LIST_DISTANCE_COMPARATOR = Comparator.comparingDouble(ChunkRenderList::getDistanceFromCamera);
private final ObjectArrayList<ChunkRenderList> lists = new ObjectArrayList<>();
private final int frame;

Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ChunkUpdateType, ArrayDeque<RenderSection>> getRebuildLists() {
Expand Down

0 comments on commit 4349eb8

Please sign in to comment.