From 9b6b95f97b5dae169aa3e2993c6f24bfb2a92492 Mon Sep 17 00:00:00 2001 From: rdh Date: Thu, 20 Aug 2026 12:40:34 -0400 Subject: [PATCH 1/3] implement fast frustum clamping based on ryan hitchman's article: https://cod.ifies.com/voxel-visibility --- .../chunk/occlusion/FastFrustumClamping.java | 96 +++++++++++++ .../chunk/occlusion/OcclusionCuller.java | 132 ++++++++++++------ 2 files changed, 188 insertions(+), 40 deletions(-) create mode 100644 common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/FastFrustumClamping.java diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/FastFrustumClamping.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/FastFrustumClamping.java new file mode 100644 index 000000000..1aa3f03a9 --- /dev/null +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/FastFrustumClamping.java @@ -0,0 +1,96 @@ +package org.embeddedt.embeddium.impl.render.chunk.occlusion; + +// A packed aperture stores xy, xz, and yz lower/upper ranks in six 10-bit lanes, low to high. +final class FastFrustumClamping { + private static final int TABLE_SIZE = 64; + private static final int TARGET_LIMIT = TABLE_SIZE - 2; + private static final int RANK_BITS = 10; + private static final int RANK_MASK = (1 << RANK_BITS) - 1; + private static final double HALF_PI = Math.PI / 2.0; + private static final long[] BOUNDS = new long[TABLE_SIZE * TABLE_SIZE]; + + static final long EMPTY = -1L; + static final long FULL = pack(0, RANK_MASK, 0, RANK_MASK, 0, RANK_MASK); + + static { + for (int x = 0; x < TABLE_SIZE; x++) { + for (int y = 0; y < TABLE_SIZE; y++) { + BOUNDS[index(x, y)] = pack(rankDown(angle(x + 1, y - 1)), rankUp(angle(x - 1, y + 1)), 0, 0, 0, 0); + } + } + } + + // Intersects each projected interval with the target cell's conservative angular bounds. + static long clip(long aperture, int x, int y, int z) { + while (Math.max(x, Math.max(y, z)) > TARGET_LIMIT) { + x >>>= 1; + y >>>= 1; + z >>>= 1; + } + + long xy = BOUNDS[index(x, y)]; + long xz = BOUNDS[index(x, z)]; + long yz = BOUNDS[index(y, z)]; + long result = pack( + Math.max(lower(aperture, 0), lower(xy, 0)), Math.min(upper(aperture, 0), upper(xy, 0)), + Math.max(lower(aperture, 1), lower(xz, 0)), Math.min(upper(aperture, 1), upper(xz, 0)), + Math.max(lower(aperture, 2), lower(yz, 0)), Math.min(upper(aperture, 2), upper(yz, 0))); + + return isEmpty(result) ? EMPTY : result; + } + + // Forms the per-plane hull used when multiple portal paths reach the same cell. + static long hull(long left, long right) { + return pack( + Math.min(lower(left, 0), lower(right, 0)), Math.max(upper(left, 0), upper(right, 0)), + Math.min(lower(left, 1), lower(right, 1)), Math.max(upper(left, 1), upper(right, 1)), + Math.min(lower(left, 2), lower(right, 2)), Math.max(upper(left, 2), upper(right, 2)) + ); + } + + // Rejects a path as soon as one of its three projected intervals has no overlap. + private static boolean isEmpty(long aperture) { + return lower(aperture, 0) > upper(aperture, 0) + || lower(aperture, 1) > upper(aperture, 1) + || lower(aperture, 2) > upper(aperture, 2); + } + + // Maps a projected cell corner to the first-quadrant angle used by the rank table. + private static double angle(int run, int rise) { + return Math.max(0.0, Math.min(HALF_PI, Math.atan2(rise, run))); + } + + // Rounds a lower angular bound down so rank quantization cannot hide a visible path. + private static int rankDown(double angle) { + return Math.max(0, Math.min(RANK_MASK, (int) Math.floor(angle * RANK_MASK / HALF_PI))); + } + + // Rounds an upper angular bound up so rank quantization cannot hide a visible path. + private static int rankUp(double angle) { + return Math.max(0, Math.min(RANK_MASK, (int) Math.ceil(angle * RANK_MASK / HALF_PI))); + } + + private static int index(int x, int y) { + return x * TABLE_SIZE + y; + } + + // Reads one plane's lower endpoint from the packed aperture. + private static int lower(long aperture, int plane) { + return (int) (aperture >>> (plane * 2 * RANK_BITS) & RANK_MASK); + } + + // Reads one plane's upper endpoint from the packed aperture. + private static int upper(long aperture, int plane) { + return (int) (aperture >>> ((plane * 2 + 1) * RANK_BITS) & RANK_MASK); + } + + // Packs all three projected intervals for hot-path min/max operations. + private static long pack(int xyLower, int xyUpper, int xzLower, int xzUpper, int yzLower, int yzUpper) { + return (long) xyLower + | ((long) xyUpper << RANK_BITS) + | ((long) xzLower << (2 * RANK_BITS)) + | ((long) xzUpper << (3 * RANK_BITS)) + | ((long) yzLower << (4 * RANK_BITS)) + | ((long) yzUpper << (5 * RANK_BITS)); + } +} diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java index 9cd81db42..90966a7b8 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java @@ -65,6 +65,7 @@ public class OcclusionCuller { */ private long[] queue = new long[256]; private int tail; + private long[] apertures = new long[0]; @@ -72,6 +73,7 @@ public class OcclusionCuller { private final RegionCullCache regionCullCache = new RegionCullCache(); private boolean isCameraInUnloadedSection; + private boolean useFastFrustumClamping; public OcclusionCuller(SectionLattice lattice, int minSectionY, int maxSectionY) { this.lattice = lattice; @@ -109,12 +111,13 @@ public class OcclusionCuller { this.regionCullCache.begin(viewport, searchDistance, numRegions); this.isCameraInUnloadedSection = false; + this.useFastFrustumClamping = false; this.init(visitor, viewport, searchDistance, useOcclusionCulling, frame); if (this.isCameraInUnloadedSection) { useOcclusionCulling = false; } - this.process(visitor, viewport, searchDistance, useOcclusionCulling, frame); + this.process(visitor, viewport, searchDistance, useOcclusionCulling, frame, this.useFastFrustumClamping); } /** @@ -129,13 +132,15 @@ public class OcclusionCuller { Viewport viewport, float searchDistance, boolean useOcclusionCulling, - int frame) + int frame, + boolean useFastFrustumClamping) { final long[] visitState = this.lattice.visitState; final long[] sectionMeta = this.lattice.sectionMeta; final int[] regionOfCell = this.lattice.regionOfCell; final int[] delta = this.lattice.delta; final long[] queue = this.queue; + final long[] apertures = this.apertures; final int baseX = this.lattice.baseX, baseY = this.lattice.baseY, baseZ = this.lattice.baseZ; final RegionCullCache cache = this.regionCullCache; @@ -205,43 +210,52 @@ public class OcclusionCuller { // We can only traverse outwards from the centre of the search. connections &= getOutwardDirections(chunkX, chunkY, chunkZ, camX, camY, camZ); - // Unrolled variant of visitNeighbors, with delta, step, incoming values inlined into each branch. - // The unrolling measurably improves performance here. - if ((connections & (1 << GraphDirection.DOWN)) != 0) { - final int n = idx - 1; - long ns = visitState[n]; - if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz - yStep) << 32) | (n & 0xFFFFFFFFL); } - visitState[n] = ns | (1 << GraphDirection.UP); - } - if ((connections & (1 << GraphDirection.UP)) != 0) { - final int n = idx + 1; - long ns = visitState[n]; - if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz + yStep) << 32) | (n & 0xFFFFFFFFL); } - visitState[n] = ns | (1 << GraphDirection.DOWN); - } - if ((connections & (1 << GraphDirection.NORTH)) != 0) { - final int n = idx - strideZ; - long ns = visitState[n]; - if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz - 1) << 32) | (n & 0xFFFFFFFFL); } - visitState[n] = ns | (1 << GraphDirection.SOUTH); - } - if ((connections & (1 << GraphDirection.SOUTH)) != 0) { - final int n = idx + strideZ; - long ns = visitState[n]; - if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz + 1) << 32) | (n & 0xFFFFFFFFL); } - visitState[n] = ns | (1 << GraphDirection.NORTH); - } - if ((connections & (1 << GraphDirection.WEST)) != 0) { - final int n = idx - strideX; - long ns = visitState[n]; - if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz - xStep) << 32) | (n & 0xFFFFFFFFL); } - visitState[n] = ns | (1 << GraphDirection.EAST); - } - if ((connections & (1 << GraphDirection.EAST)) != 0) { - final int n = idx + strideX; - long ns = visitState[n]; - if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz + xStep) << 32) | (n & 0xFFFFFFFFL); } - visitState[n] = ns | (1 << GraphDirection.WEST); + if (useFastFrustumClamping) { + if ((connections & (1 << GraphDirection.DOWN)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, apertures[idx], idx - 1, xyz - yStep, 1 << GraphDirection.UP, frameStamp, chunkX, chunkY - 1, chunkZ, camX, camY, camZ, tail); + if ((connections & (1 << GraphDirection.UP)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, apertures[idx], idx + 1, xyz + yStep, 1 << GraphDirection.DOWN, frameStamp, chunkX, chunkY + 1, chunkZ, camX, camY, camZ, tail); + if ((connections & (1 << GraphDirection.NORTH)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, apertures[idx], idx - strideZ, xyz - 1, 1 << GraphDirection.SOUTH, frameStamp, chunkX, chunkY, chunkZ - 1, camX, camY, camZ, tail); + if ((connections & (1 << GraphDirection.SOUTH)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, apertures[idx], idx + strideZ, xyz + 1, 1 << GraphDirection.NORTH, frameStamp, chunkX, chunkY, chunkZ + 1, camX, camY, camZ, tail); + if ((connections & (1 << GraphDirection.WEST)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, apertures[idx], idx - strideX, xyz - xStep, 1 << GraphDirection.EAST, frameStamp, chunkX - 1, chunkY, chunkZ, camX, camY, camZ, tail); + if ((connections & (1 << GraphDirection.EAST)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, apertures[idx], idx + strideX, xyz + xStep, 1 << GraphDirection.WEST, frameStamp, chunkX + 1, chunkY, chunkZ, camX, camY, camZ, tail); + } else { + // Unrolled variant of visitNeighbors, with delta, step, incoming values inlined into each branch. + // The unrolling measurably improves performance here. + if ((connections & (1 << GraphDirection.DOWN)) != 0) { + final int n = idx - 1; + long ns = visitState[n]; + if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz - yStep) << 32) | (n & 0xFFFFFFFFL); } + visitState[n] = ns | (1 << GraphDirection.UP); + } + if ((connections & (1 << GraphDirection.UP)) != 0) { + final int n = idx + 1; + long ns = visitState[n]; + if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz + yStep) << 32) | (n & 0xFFFFFFFFL); } + visitState[n] = ns | (1 << GraphDirection.DOWN); + } + if ((connections & (1 << GraphDirection.NORTH)) != 0) { + final int n = idx - strideZ; + long ns = visitState[n]; + if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz - 1) << 32) | (n & 0xFFFFFFFFL); } + visitState[n] = ns | (1 << GraphDirection.SOUTH); + } + if ((connections & (1 << GraphDirection.SOUTH)) != 0) { + final int n = idx + strideZ; + long ns = visitState[n]; + if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz + 1) << 32) | (n & 0xFFFFFFFFL); } + visitState[n] = ns | (1 << GraphDirection.NORTH); + } + if ((connections & (1 << GraphDirection.WEST)) != 0) { + final int n = idx - strideX; + long ns = visitState[n]; + if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz - xStep) << 32) | (n & 0xFFFFFFFFL); } + visitState[n] = ns | (1 << GraphDirection.EAST); + } + if ((connections & (1 << GraphDirection.EAST)) != 0) { + final int n = idx + strideX; + long ns = visitState[n]; + if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz + xStep) << 32) | (n & 0xFFFFFFFFL); } + visitState[n] = ns | (1 << GraphDirection.WEST); + } } } @@ -277,6 +291,25 @@ public class OcclusionCuller { } } + private int visitFfcNode(long[] visitState, long[] apertures, long[] queue, long parentAperture, int idx, int xyz, int incoming, long frameStamp, + int chunkX, int chunkY, int chunkZ, int camX, int camY, int camZ, int tail) { + long aperture = FastFrustumClamping.clip(parentAperture, Math.abs(chunkX - camX), Math.abs(chunkY - camY), Math.abs(chunkZ - camZ)); + if (aperture == FastFrustumClamping.EMPTY) { + return tail; + } + + long state = visitState[idx]; + if (state < frameStamp) { + apertures[idx] = aperture; + visitState[idx] = frameStamp | incoming; + queue[tail++] = ((long) xyz << 32) | (idx & 0xFFFFFFFFL); + } else if (state != SectionLattice.SENTINEL) { + apertures[idx] = FastFrustumClamping.hull(apertures[idx], aperture); + visitState[idx] = state | incoming; + } + return tail; + } + // Allow movement only away from the camera on each axis. A coordinate // equal to the camera coordinate permits both directions on that axis. private static int getOutwardDirections(int chunkX, int chunkY, int chunkZ, int camX, int camY, int camZ) { @@ -386,6 +419,14 @@ public class OcclusionCuller { long frameStamp = SectionLattice.frameStamp(frame); visitState[idx] = frameStamp; + this.useFastFrustumClamping = useOcclusionCulling; + if (this.useFastFrustumClamping) { + if (this.apertures.length < visitState.length) { + this.apertures = new long[visitState.length]; + } + this.apertures[idx] = FastFrustumClamping.FULL; + } + // Visit the origin immediately; it is processed inline rather than // enqueued so the BFS starts with its neighbours. int sectionIndex = LocalSectionIndex.pack(origin.x(), origin.y(), origin.z()); @@ -402,7 +443,18 @@ public class OcclusionCuller { } int xyz = this.lattice.packXyz(origin.x(), origin.y(), origin.z()); - this.visitNeighbors(visitState, queue, delta, idx, xyz, outgoing, frameStamp); + if (this.useFastFrustumClamping) { + while (outgoing != 0) { + int dir = Integer.numberOfTrailingZeros(outgoing); + outgoing &= outgoing - 1; + this.tail = this.visitFfcNode(visitState, this.apertures, queue, FastFrustumClamping.FULL, + idx + delta[dir], xyz + XYZ_STEP[dir], INCOMING[dir], frameStamp, + origin.x() + GraphDirection.x(dir), origin.y() + GraphDirection.y(dir), origin.z() + GraphDirection.z(dir), + origin.x(), origin.y(), origin.z(), this.tail); + } + } else { + this.visitNeighbors(visitState, queue, delta, idx, xyz, outgoing, frameStamp); + } } /** -- 2.54.1 From 9882ee5add36902a064ea7e3f48bdbe684735230 Mon Sep 17 00:00:00 2001 From: rdh Date: Thu, 20 Aug 2026 16:23:07 -0400 Subject: [PATCH 2/3] minor optimizations --- .../chunk/occlusion/FastFrustumClamping.java | 6 ++++- .../chunk/occlusion/OcclusionCuller.java | 23 +++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/FastFrustumClamping.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/FastFrustumClamping.java index 1aa3f03a9..126c0f62c 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/FastFrustumClamping.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/FastFrustumClamping.java @@ -15,7 +15,11 @@ final class FastFrustumClamping { static { for (int x = 0; x < TABLE_SIZE; x++) { for (int y = 0; y < TABLE_SIZE; y++) { - BOUNDS[index(x, y)] = pack(rankDown(angle(x + 1, y - 1)), rankUp(angle(x - 1, y + 1)), 0, 0, 0, 0); + BOUNDS[index(x, y)] = pack( + rankDown(angle(x + 1, y - 1)), + rankUp(angle(x - 1, y + 1)), + 0, 0, 0, 0 + ); } } } diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java index 90966a7b8..c0786983e 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java @@ -211,12 +211,16 @@ public class OcclusionCuller { connections &= getOutwardDirections(chunkX, chunkY, chunkZ, camX, camY, camZ); if (useFastFrustumClamping) { - if ((connections & (1 << GraphDirection.DOWN)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, apertures[idx], idx - 1, xyz - yStep, 1 << GraphDirection.UP, frameStamp, chunkX, chunkY - 1, chunkZ, camX, camY, camZ, tail); - if ((connections & (1 << GraphDirection.UP)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, apertures[idx], idx + 1, xyz + yStep, 1 << GraphDirection.DOWN, frameStamp, chunkX, chunkY + 1, chunkZ, camX, camY, camZ, tail); - if ((connections & (1 << GraphDirection.NORTH)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, apertures[idx], idx - strideZ, xyz - 1, 1 << GraphDirection.SOUTH, frameStamp, chunkX, chunkY, chunkZ - 1, camX, camY, camZ, tail); - if ((connections & (1 << GraphDirection.SOUTH)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, apertures[idx], idx + strideZ, xyz + 1, 1 << GraphDirection.NORTH, frameStamp, chunkX, chunkY, chunkZ + 1, camX, camY, camZ, tail); - if ((connections & (1 << GraphDirection.WEST)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, apertures[idx], idx - strideX, xyz - xStep, 1 << GraphDirection.EAST, frameStamp, chunkX - 1, chunkY, chunkZ, camX, camY, camZ, tail); - if ((connections & (1 << GraphDirection.EAST)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, apertures[idx], idx + strideX, xyz + xStep, 1 << GraphDirection.WEST, frameStamp, chunkX + 1, chunkY, chunkZ, camX, camY, camZ, tail); + long parentAperture = apertures[idx]; + int relativeX = Math.abs(chunkX - camX); + int relativeY = Math.abs(chunkY - camY); + int relativeZ = Math.abs(chunkZ - camZ); + if ((connections & (1 << GraphDirection.DOWN)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, parentAperture, idx - 1, xyz - yStep, 1 << GraphDirection.UP, frameStamp, relativeX, relativeY + 1, relativeZ, tail); + if ((connections & (1 << GraphDirection.UP)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, parentAperture, idx + 1, xyz + yStep, 1 << GraphDirection.DOWN, frameStamp, relativeX, relativeY + 1, relativeZ, tail); + if ((connections & (1 << GraphDirection.NORTH)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, parentAperture, idx - strideZ, xyz - 1, 1 << GraphDirection.SOUTH, frameStamp, relativeX, relativeY, relativeZ + 1, tail); + if ((connections & (1 << GraphDirection.SOUTH)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, parentAperture, idx + strideZ, xyz + 1, 1 << GraphDirection.NORTH, frameStamp, relativeX, relativeY, relativeZ + 1, tail); + if ((connections & (1 << GraphDirection.WEST)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, parentAperture, idx - strideX, xyz - xStep, 1 << GraphDirection.EAST, frameStamp, relativeX + 1, relativeY, relativeZ, tail); + if ((connections & (1 << GraphDirection.EAST)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, parentAperture, idx + strideX, xyz + xStep, 1 << GraphDirection.WEST, frameStamp, relativeX + 1, relativeY, relativeZ, tail); } else { // Unrolled variant of visitNeighbors, with delta, step, incoming values inlined into each branch. // The unrolling measurably improves performance here. @@ -292,8 +296,8 @@ public class OcclusionCuller { } private int visitFfcNode(long[] visitState, long[] apertures, long[] queue, long parentAperture, int idx, int xyz, int incoming, long frameStamp, - int chunkX, int chunkY, int chunkZ, int camX, int camY, int camZ, int tail) { - long aperture = FastFrustumClamping.clip(parentAperture, Math.abs(chunkX - camX), Math.abs(chunkY - camY), Math.abs(chunkZ - camZ)); + int relativeX, int relativeY, int relativeZ, int tail) { + long aperture = FastFrustumClamping.clip(parentAperture, relativeX, relativeY, relativeZ); if (aperture == FastFrustumClamping.EMPTY) { return tail; } @@ -449,8 +453,7 @@ public class OcclusionCuller { outgoing &= outgoing - 1; this.tail = this.visitFfcNode(visitState, this.apertures, queue, FastFrustumClamping.FULL, idx + delta[dir], xyz + XYZ_STEP[dir], INCOMING[dir], frameStamp, - origin.x() + GraphDirection.x(dir), origin.y() + GraphDirection.y(dir), origin.z() + GraphDirection.z(dir), - origin.x(), origin.y(), origin.z(), this.tail); + Math.abs(GraphDirection.x(dir)), Math.abs(GraphDirection.y(dir)), Math.abs(GraphDirection.z(dir)), this.tail); } } else { this.visitNeighbors(visitState, queue, delta, idx, xyz, outgoing, frameStamp); -- 2.54.1 From 53a475aa38fb76e5e5ef585ac02076c44c6e5c2c Mon Sep 17 00:00:00 2001 From: rdh Date: Thu, 20 Aug 2026 20:13:11 -0400 Subject: [PATCH 3/3] refine portal visibility with camera-to-cell angles --- .../chunk/occlusion/OcclusionCuller.java | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java index c0786983e..d356eb329 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java @@ -48,6 +48,12 @@ public class OcclusionCuller { // When stepping from a cell in dir, the neighbour is entered from the opposite face. private static final int[] INCOMING = new int[GraphDirection.COUNT]; + private static final long X_OPPOSITE_FACE_PAIRS = (1L << VisibilityEncoding.bit(GraphDirection.WEST, GraphDirection.EAST)) + | (1L << VisibilityEncoding.bit(GraphDirection.EAST, GraphDirection.WEST)); + private static final long Y_OPPOSITE_FACE_PAIRS = (1L << VisibilityEncoding.bit(GraphDirection.DOWN, GraphDirection.UP)) + | (1L << VisibilityEncoding.bit(GraphDirection.UP, GraphDirection.DOWN)); + private static final long Z_OPPOSITE_FACE_PAIRS = (1L << VisibilityEncoding.bit(GraphDirection.NORTH, GraphDirection.SOUTH)) + | (1L << VisibilityEncoding.bit(GraphDirection.SOUTH, GraphDirection.NORTH)); static { for (int dir = 0; dir < GraphDirection.COUNT; dir++) { @@ -198,18 +204,16 @@ public class OcclusionCuller { int connections; if (useOcclusionCulling) { - // Merge the outgoing paths reachable from every incoming path - // accumulated in this cell. int incoming = (int) (visitState[idx] & DIR_MASK); - connections = VisibilityEncoding.getConnections( - sm & PackedSectionMetadata.VISIBILITY_MASK, incoming); + connections = getAngleRefinedConnections(sm & PackedSectionMetadata.VISIBILITY_MASK, incoming, + getOutwardDirections(chunkX, chunkY, chunkZ, camX, camY, camZ), + transform, chunkX, chunkY, chunkZ); } else { connections = GraphDirectionSet.ALL; + // We can only traverse outwards from the centre of the search. + connections &= getOutwardDirections(chunkX, chunkY, chunkZ, camX, camY, camZ); } - // We can only traverse outwards from the centre of the search. - connections &= getOutwardDirections(chunkX, chunkY, chunkZ, camX, camY, camZ); - if (useFastFrustumClamping) { long parentAperture = apertures[idx]; int relativeX = Math.abs(chunkX - camX); @@ -331,6 +335,26 @@ public class OcclusionCuller { return planes; } + static int getAngleRefinedConnections(long visibilityData, int incoming, int outward, CameraTransform camera, int chunkX, int chunkY, int chunkZ) { + return VisibilityEncoding.getConnections(visibilityData & getAngleRefinementMask(camera, chunkX, chunkY, chunkZ), incoming) & outward; + } + + static long getAngleRefinementMask(CameraTransform camera, int chunkX, int chunkY, int chunkZ) { + double centreX = chunkX * 16.0 + 8.0; + double centreY = chunkY * 16.0 + 8.0; + double centreZ = chunkZ * 16.0 + 8.0; + double dx = Math.abs(camera.x - centreX); + double dy = Math.abs(camera.y - centreY); + double dz = Math.abs(camera.z - centreZ); + long mask = VisibilityEncoding.EVERYTHING; + + if (dy > dx || dz > dx) mask &= ~X_OPPOSITE_FACE_PAIRS; + if (dx > dy || dz > dy) mask &= ~Y_OPPOSITE_FACE_PAIRS; + if (dx > dz || dy > dz) mask &= ~Z_OPPOSITE_FACE_PAIRS; + + return mask; + } + // Convert a section coordinate to the origin of its containing render // region, in block coordinates. private static int regionOrigin(int chunkCoord, int shift, int blockSize) { -- 2.54.1