Skip to content

Commit

Permalink
Fix blocks overflowing over the top of octree bounds
Browse files Browse the repository at this point in the history
This was due to an incorrect maxDimension calculation
  • Loading branch information
NotStirred committed Sep 19, 2024
1 parent 16b409c commit 595ef76
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -1443,18 +1443,23 @@ private int calculateOctreeOrigin(Collection<ChunkPosition> chunksToLoad, boolea
zmin *= 16;
zmax *= 16;

int maxDimension = Math.max(yMax - yMin, Math.max(xmax - xmin, zmax - zmin));
int requiredDepth = QuickMath.log2(QuickMath.nextPow2(maxDimension));

int requiredDepth;
if(centerOctree) {
int maxDimension = Math.max(yMax - yMin, Math.max(xmax - xmin, zmax - zmin));
requiredDepth = QuickMath.log2(QuickMath.nextPow2(maxDimension));

int xroom = (1 << requiredDepth) - (xmax - xmin);
int yroom = (1 << requiredDepth) - (yMax - yMin);
int zroom = (1 << requiredDepth) - (zmax - zmin);

origin.set(xmin - xroom / 2, -yroom / 2, zmin - zroom / 2);
} else {
// Note: Math.floorDiv rather than integer division for round toward -infinity
origin.set(xmin, Math.floorDiv(yMin, 16) * 16, zmin);
int yMin16 = yMin & ~0xf; // closest multiple of 16 below yMin

int maxDimension = Math.max(yMax - yMin16, Math.max(xmax - xmin, zmax - zmin));
requiredDepth = QuickMath.log2(QuickMath.nextPow2(maxDimension));

origin.set(xmin, yMin16, zmin);
}
return requiredDepth;
}
Expand Down

0 comments on commit 595ef76

Please sign in to comment.