aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling
diff options
context:
space:
mode:
authorKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2023-07-22 14:43:00 +0800
committerKevinthegreat <92656833+kevinthegreat1@users.noreply.github.com>2023-08-18 18:05:10 +0800
commitfc65ff5b469fb384d2df422a5a6d8437012a819b (patch)
tree0b967fa17e1f791b9efc9c630d54546fcc14a615 /src/main/java/me/xmrvizzy/skyblocker/utils/render/culling
parent6069d3cf7d2e96ca7ef1975a3dd04e2121a6e3c9 (diff)
downloadSkyblocker-fc65ff5b469fb384d2df422a5a6d8437012a819b.tar.gz
Skyblocker-fc65ff5b469fb384d2df422a5a6d8437012a819b.tar.bz2
Skyblocker-fc65ff5b469fb384d2df422a5a6d8437012a819b.zip
Refactor utils package
Diffstat (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils/render/culling')
-rw-r--r--src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/OcclusionCulling.java47
-rw-r--r--src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/WorldProvider.java28
-rw-r--r--src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/package-info.java4
3 files changed, 79 insertions, 0 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/OcclusionCulling.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/OcclusionCulling.java
new file mode 100644
index 00000000..5c7f69ad
--- /dev/null
+++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/OcclusionCulling.java
@@ -0,0 +1,47 @@
+package me.xmrvizzy.skyblocker.utils.render.culling;
+
+import com.logisticscraft.occlusionculling.OcclusionCullingInstance;
+import com.logisticscraft.occlusionculling.cache.ArrayOcclusionCache;
+import com.logisticscraft.occlusionculling.util.Vec3d;
+import me.xmrvizzy.skyblocker.utils.render.FrustumUtils;
+import net.minecraft.client.MinecraftClient;
+
+public class OcclusionCulling {
+ private static final int TRACING_DISTANCE = 128;
+ private static final MinecraftClient CLIENT = MinecraftClient.getInstance();
+ private static OcclusionCullingInstance instance = null;
+
+ // Reused objects to reduce allocation overhead
+ private static final Vec3d cameraPos = new Vec3d(0, 0, 0);
+ private static final Vec3d min = new Vec3d(0, 0, 0);
+ private static final Vec3d max = new Vec3d(0, 0, 0);
+
+ /**
+ * Initializes the occlusion culling instance
+ */
+ public static void init() {
+ instance = new OcclusionCullingInstance(TRACING_DISTANCE, new WorldProvider(), new ArrayOcclusionCache(TRACING_DISTANCE), 2);
+ }
+
+ private static void updateCameraPos() {
+ var camera = CLIENT.gameRenderer.getCamera().getPos();
+ cameraPos.set(camera.x, camera.y, camera.z);
+ }
+
+ /**
+ * This first checks checks if the bounding box is within the camera's FOV, if
+ * it is then it checks for whether it's occluded or not.
+ *
+ * @return A boolean representing whether the bounding box is fully visible or
+ * not.
+ */
+ public static boolean isVisible(double x1, double y1, double z1, double x2, double y2, double z2) {
+ if (!FrustumUtils.isVisible(x1, y1, z1, x2, y2, z2)) return false;
+
+ updateCameraPos();
+ min.set(x1, y1, z1);
+ max.set(x2, y2, z2);
+
+ return instance.isAABBVisible(min, max, cameraPos);
+ }
+}
diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/WorldProvider.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/WorldProvider.java
new file mode 100644
index 00000000..09a7bc79
--- /dev/null
+++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/WorldProvider.java
@@ -0,0 +1,28 @@
+package me.xmrvizzy.skyblocker.utils.render.culling;
+
+import com.logisticscraft.occlusionculling.DataProvider;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.client.world.ClientWorld;
+import net.minecraft.util.math.BlockPos;
+
+public class WorldProvider implements DataProvider {
+ private final static MinecraftClient CLIENT = MinecraftClient.getInstance();
+ private ClientWorld world = null;
+
+ @Override
+ public boolean prepareChunk(int chunkX, int chunkZ) {
+ this.world = CLIENT.world;
+ return this.world != null;
+ }
+
+ @Override
+ public boolean isOpaqueFullCube(int x, int y, int z) {
+ BlockPos pos = new BlockPos(x, y, z);
+ return this.world.getBlockState(pos).isOpaqueFullCube(this.world, pos);
+ }
+
+ @Override
+ public void cleanup() {
+ this.world = null;
+ }
+}
diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/package-info.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/package-info.java
new file mode 100644
index 00000000..97ebac86
--- /dev/null
+++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/package-info.java
@@ -0,0 +1,4 @@
+/**
+ * Package dedicated to occlusion culling utilities
+ */
+package me.xmrvizzy.skyblocker.utils.render.culling; \ No newline at end of file