From 5fc4a74860671041808c4ea76e422995f0cb82a5 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Fri, 26 Jan 2024 16:40:15 -0800 Subject: [PATCH] Get some bounding spheres at home - Compute bounding spheres with the AABB method. Should be pretty well suited for funny block game. - Remove Miniball from dependencies. --- build.gradle | 18 ----- .../flywheel/lib/model/ModelUtil.java | 66 +++++++++++-------- 2 files changed, 40 insertions(+), 44 deletions(-) diff --git a/build.gradle b/build.gradle index 9446108f3..0018618a2 100644 --- a/build.gradle +++ b/build.gradle @@ -74,21 +74,6 @@ repositories { mavenCentral() } -// Fix for loading non-mod libraries in dev-env, used for Miniball. -// https://gist.github.com/SizableShrimp/66b22f1b24c255e1491c8d98d3f11f83 -// v--------------------------------------------------------------------v -configurations { - library - implementation.extendsFrom library -} - -minecraft.runs.configureEach { - lazyToken('minecraft_classpath') { - configurations.library.copyRecursive().resolve().collect { it.absolutePath }.join(File.pathSeparator) - } -} -// ^--------------------------------------------------------------------^ - dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1' minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" @@ -96,9 +81,6 @@ dependencies { compileOnly fg.deobf("maven.modrinth:embeddium:0.2.10+mc1.20.1") compileOnly fg.deobf("maven.modrinth:oculus:1.20.1-1.6.9") - jarJar(group: 'com.dreizak', name: 'miniball', version: '[1.0.3,2.0.0)') - library 'com.dreizak:miniball:1.0.3' - // https://discord.com/channels/313125603924639766/725850371834118214/910619168821354497 // Prevent Mixin annotation processor from getting into IntelliJ's annotation processor settings // This allows 'Settings > Build, Execution, and Deployment > Build Tools > Gradle > Build and run using' set to IntelliJ to work correctly diff --git a/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java b/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java index b3d0dcfb9..76b23fd00 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java +++ b/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java @@ -4,12 +4,11 @@ import java.lang.reflect.Field; import java.nio.ByteBuffer; import org.jetbrains.annotations.Nullable; +import org.joml.Vector3f; import org.joml.Vector4f; import org.lwjgl.system.MemoryUtil; import org.slf4j.Logger; -import com.dreizak.miniball.highdim.Miniball; -import com.dreizak.miniball.model.PointSet; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.model.Mesh; import com.jozufozu.flywheel.api.vertex.VertexList; @@ -37,6 +36,7 @@ public final class ModelUtil { * Meant to be used for virtual rendering. */ public static final BlockRenderDispatcher VANILLA_RENDERER = createVanillaRenderer(); + private static final float BOUNDING_SPHERE_EPSILON = 1e-4f; private ModelUtil() { } @@ -128,33 +128,47 @@ public final class ModelUtil { } public static Vector4f computeBoundingSphere(VertexList vertexList) { - return computeBoundingSphere(new PointSet() { - @Override - public int size() { - return vertexList.vertexCount(); - } + var center = computeCenterOfAABBContaining(vertexList); - @Override - public int dimension() { - return 3; - } + var radius = computeMaxDistanceTo(vertexList, center) + BOUNDING_SPHERE_EPSILON; - @Override - public double coord(int i, int dim) { - return switch (dim) { - case 0 -> vertexList.x(i); - case 1 -> vertexList.y(i); - case 2 -> vertexList.z(i); - default -> throw new IllegalArgumentException("Invalid dimension: " + dim); - }; - } - }); + return new Vector4f(center, radius); } - public static Vector4f computeBoundingSphere(PointSet points) { - var miniball = new Miniball(points); - double[] center = miniball.center(); - double radius = miniball.radius(); - return new Vector4f((float) center[0], (float) center[1], (float) center[2], (float) radius); + private static float computeMaxDistanceTo(VertexList vertexList, Vector3f pos) { + float farthestDistanceSquared = -1; + + for (int i = 0; i < vertexList.vertexCount(); i++) { + var distanceSquared = pos.distanceSquared(vertexList.x(i), vertexList.y(i), vertexList.z(i)); + + if (distanceSquared > farthestDistanceSquared) { + farthestDistanceSquared = distanceSquared; + } + } + + return (float) Math.sqrt(farthestDistanceSquared); + } + + private static Vector3f computeCenterOfAABBContaining(VertexList vertexList) { + var min = new Vector3f(Float.MAX_VALUE); + var max = new Vector3f(Float.MIN_VALUE); + + for (int i = 0; i < vertexList.vertexCount(); i++) { + float x = vertexList.x(i); + float y = vertexList.y(i); + float z = vertexList.z(i); + + // JOML's min/max methods don't accept floats :whywheel: + min.x = Math.min(min.x, x); + min.y = Math.min(min.y, y); + min.z = Math.min(min.z, z); + + max.x = Math.max(max.x, x); + max.y = Math.max(max.y, y); + max.z = Math.max(max.z, z); + } + + return min.add(max) + .mul(0.5f); } }