Fix rounding errors on models with 45 degree normals by favoring one Direction (#9669)

Should fix flickering issues when breaking custom models and having our vanilla solution disabled.
This commit is contained in:
Kovak authored and GitHub committed 2023-07-19 14:44:28 -07:00
1 parent 881725c7d5
commit 7c84249996
9 files changed
+260

No files matched your search

@@ -12,8 +12,10 @@ import com.mojang.blaze3d.shaders.FogShape;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.SheetedDecalTextureGenerator;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.datafixers.util.Either;
import com.mojang.math.Constants;
import net.minecraft.FileUtil;
import net.minecraft.client.Camera;
import net.minecraft.client.KeyMapping;
@@ -146,6 +148,7 @@ import net.minecraftforge.client.gui.overlay.GuiOverlayManager;
import net.minecraftforge.client.model.data.ModelData;
import net.minecraftforge.client.textures.ForgeTextureMetadata;
import net.minecraftforge.client.textures.TextureAtlasSpriteLoaderManager;
import net.minecraftforge.common.ForgeConfig;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.ForgeI18n;
import net.minecraftforge.common.ForgeMod;
@@ -1211,6 +1214,39 @@ public class ForgeHooksClient
ForgeHooks.onCreativeModeTabBuildContents(tab, tabKey, originalGenerator, params, output);
}
/**
* This function is a clone of {@link Direction#getNearest(float, float, float)} designed to return a consistent
* direction when the normal is at an inflection point (ie 45 degrees) rounding errors
* from associated matrix multiplication (such as during {@link SheetedDecalTextureGenerator#endVertex()}
* can cause the direction chosen to be unstable.
*
* The function will only take effect if the Forge Client config option "stabilizeDirectionGetNearest" is enabled.
*
* This is a port of the downstream changes from https://github.com/neoforged/NeoForge PR #26
*
* @param nX X component of the normal
* @param nY Y component of the normal
* @param nZ Z component of the normal
* @return the nearest Direction to the passed in normal, biased slightly in favor of the order of declaration
*/
public static Direction getNearestStable(float nX, float nY, float nZ)
{
if (ForgeConfig.CLIENT.stabilizeDirectionGetNearest.get()) {
Direction ret = Direction.NORTH;
float sum = Float.MIN_VALUE;
for(Direction dir : Direction.values()) {
float newSum = nX * (float)dir.getNormal().getX() + nY * (float)dir.getNormal().getY() + nZ * (float)dir.getNormal().getZ();
if (newSum > sum + Constants.EPSILON) {
sum = newSum;
ret = dir;
}
}
return ret;
} else {
return Direction.getNearest(nX, nY, nZ);
}
}
// Make sure the below method is only ever called once (by forge).
private static boolean initializedClientHooks = false;
// Runs during Minecraft construction, before initial resource loading.
@@ -106,6 +106,8 @@ public class ForgeConfig {
@Deprecated(since = "1.20.1", forRemoval = true) // Config option ignored.
public final BooleanValue compressLanIPv6Addresses;
public final BooleanValue stabilizeDirectionGetNearest;
Client(ForgeConfigSpec.Builder builder) {
builder.comment("Client only settings, mostly things related to rendering")
.push("client");
@@ -137,6 +139,11 @@ public class ForgeConfig {
.translation("forge.configgui.compressLanIPv6Addresses")
.define("compressLanIPv6Addresses", true);
stabilizeDirectionGetNearest = builder
.comment("When enabled, a slightly biased Direction#getNearest calculation will be used to prevent normal fighting on 45 degree angle faces.")
.translation("forge.configgui.stabilizeDirectionGetNearest")
.define("stabilizeDirectionGetNearest", true);
builder.pop();
}
}