Skip to content

Commit

Permalink
Use correct coordinates for sorting chunk sections (#2924)
Browse files Browse the repository at this point in the history
Fix section and region sorting by using the correct section coordinate instead of the integer part of the camera transform, which is incorrect near the origin.

Closes #2918
  • Loading branch information
douira authored Dec 7, 2024
1 parent 5929b38 commit ca35ecb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import net.caffeinemc.mods.sodium.client.render.chunk.RenderSection;
import net.caffeinemc.mods.sodium.client.render.chunk.RenderSectionFlags;
import net.caffeinemc.mods.sodium.client.render.chunk.region.RenderRegion;
import net.caffeinemc.mods.sodium.client.render.viewport.CameraTransform;
import net.caffeinemc.mods.sodium.client.util.iterator.ByteArrayIterator;
import net.caffeinemc.mods.sodium.client.util.iterator.ByteIterator;
import net.caffeinemc.mods.sodium.client.util.iterator.ReversibleByteArrayIterator;
import net.minecraft.core.SectionPos;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -43,10 +43,10 @@ public void reset(int frame) {
// clamping the relative camera position to the region bounds means there can only be very few different distances
private static final int SORTING_HISTOGRAM_SIZE = RenderRegion.REGION_WIDTH + RenderRegion.REGION_HEIGHT + RenderRegion.REGION_LENGTH - 2;

public void sortSections(CameraTransform transform, int[] sortItems) {
var cameraX = Mth.clamp((transform.intX >> 4) - this.region.getChunkX(), 0, RenderRegion.REGION_WIDTH - 1);
var cameraY = Mth.clamp((transform.intY >> 4) - this.region.getChunkY(), 0, RenderRegion.REGION_HEIGHT - 1);
var cameraZ = Mth.clamp((transform.intZ >> 4) - this.region.getChunkZ(), 0, RenderRegion.REGION_LENGTH - 1);
public void sortSections(SectionPos cameraPos, int[] sortItems) {
var cameraX = Mth.clamp(cameraPos.getX() - this.region.getChunkX(), 0, RenderRegion.REGION_WIDTH - 1);
var cameraY = Mth.clamp(cameraPos.getY() - this.region.getChunkY(), 0, RenderRegion.REGION_HEIGHT - 1);
var cameraZ = Mth.clamp(cameraPos.getZ() - this.region.getChunkZ(), 0, RenderRegion.REGION_LENGTH - 1);

int[] histogram = new int[SORTING_HISTOGRAM_SIZE];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import net.caffeinemc.mods.sodium.client.render.chunk.region.RenderRegion;
import net.caffeinemc.mods.sodium.client.render.viewport.Viewport;

import java.util.*;
import java.util.ArrayDeque;
import java.util.EnumMap;
import java.util.Map;
import java.util.Queue;

/**
* The visible chunk collector is passed to the occlusion graph search culler to
Expand Down Expand Up @@ -67,10 +70,10 @@ private void addToRebuildLists(RenderSection section) {

public SortedRenderLists createRenderLists(Viewport viewport) {
// sort the regions by distance to fix rare region ordering bugs
var transform = viewport.getTransform();
var cameraX = transform.intX >> (4 + RenderRegion.REGION_WIDTH_SH);
var cameraY = transform.intY >> (4 + RenderRegion.REGION_HEIGHT_SH);
var cameraZ = transform.intZ >> (4 + RenderRegion.REGION_LENGTH_SH);
var sectionPos = viewport.getChunkCoord();
var cameraX = sectionPos.getX() >> RenderRegion.REGION_WIDTH_SH;
var cameraY = sectionPos.getY() >> RenderRegion.REGION_HEIGHT_SH;
var cameraZ = sectionPos.getZ() >> RenderRegion.REGION_LENGTH_SH;
var size = this.sortedRenderLists.size();

if (sortItems.length < size) {
Expand All @@ -95,7 +98,7 @@ public SortedRenderLists createRenderLists(Viewport viewport) {
}

for (var list : sorted) {
list.sortSections(transform, sortItems);
list.sortSections(sectionPos, sortItems);
}

return new SortedRenderLists(sorted);
Expand Down

0 comments on commit ca35ecb

Please # to comment.