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

@@ -0,0 +1,11 @@
--- a/com/mojang/blaze3d/vertex/SheetedDecalTextureGenerator.java
+++ b/com/mojang/blaze3d/vertex/SheetedDecalTextureGenerator.java
@@ -46,7 +_,7 @@
public void m_5752_() {
Vector3f vector3f = this.f_85869_.transform(new Vector3f(this.f_85876_, this.f_85877_, this.f_85878_));
- Direction direction = Direction.m_122372_(vector3f.x(), vector3f.y(), vector3f.z());
+ Direction direction = net.minecraftforge.client.ForgeHooksClient.getNearestStable(vector3f.x(), vector3f.y(), vector3f.z());
Vector4f vector4f = this.f_85868_.transform(new Vector4f(this.f_85870_, this.f_85871_, this.f_85872_, 1.0F));
vector4f.rotateY((float)Math.PI);
vector4f.rotateX((-(float)Math.PI / 2F));
@@ -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();
}
}
@@ -195,6 +195,8 @@
"forge.configgui.indexVanillaPackCachesOnThread": "Index vanilla resource packs on thread",
"forge.configgui.indexModPackCachesOnThread.tooltip": "Set this to true to index mod resource and data packs on thread",
"forge.configgui.indexModPackCachesOnThread": "Index mod resource packs on thread",
"forge.configgui.stabilizeDirectionGetNearest": "Stabilize Direction Get Nearest",
"forge.configgui.stabilizeDirectionGetNearest.tooltip": "When enabled, a slightly biased Direction#getNearest calculation will be used to prevent normal fighting on 45 degree angle faces.",
"forge.controlsgui.shift": "SHIFT + %s",
"forge.controlsgui.control": "CTRL + %s",
@@ -0,0 +1,67 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.debug.client.model;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.CampfireBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
import net.minecraft.world.level.material.MapColor;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import java.util.function.ToIntFunction;
@Mod(CalculateNormalsTest.MODID)
public class CalculateNormalsTest {
public static final String MODID = "calculate_normals_test";
public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID);
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
public static RegistryObject<Block> fire_accurate_normals_test = BLOCKS.register("fire_accurate_normals_test", () ->
new CampfireBlock(false, 2, BlockBehaviour.Properties.of().mapColor(MapColor.PODZOL).instrument(NoteBlockInstrument.BASS)
.strength(2.0F).sound(SoundType.WOOD).lightLevel(litBlockEmission(10)).noOcclusion().ignitedByLava()));
public static RegistryObject<Item> fire_accurate_normals_test_item = ITEMS.register("fire_accurate_normals_test", () ->
new BlockItem(fire_accurate_normals_test.get(), new Item.Properties()));
private static ToIntFunction<BlockState> litBlockEmission(int val) {
return (blockState) -> {
return blockState.getValue(BlockStateProperties.LIT) ? val : 0;
};
}
public CalculateNormalsTest() {
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
BLOCKS.register(modEventBus);
ITEMS.register(modEventBus);
}
@Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public class ClientSetup {
@SubscribeEvent
public static void onClientSetup(final FMLClientSetupEvent event)
{
ItemBlockRenderTypes.setRenderLayer(fire_accurate_normals_test.get(), RenderType.cutout());
}
}
}
+3
View File
@@ -38,6 +38,9 @@ modId="mdk_datagen"
[[mods]]
modId="living_swap_items_event_test"
[[mods]]
modId="calculate_normals_test"
# LEGACY TEST CASES
###### The mods below are from the old test framework and need to be yeeted later again.
@@ -0,0 +1,34 @@
{
"variants": {
"facing=east,lit=false": {
"model": "calculate_normals_test:block/fire_accurate_normals_test",
"y": 270
},
"facing=east,lit=true": {
"model": "calculate_normals_test:block/fire_accurate_normals_test",
"y": 270
},
"facing=north,lit=false": {
"model": "calculate_normals_test:block/fire_accurate_normals_test",
"y": 180
},
"facing=north,lit=true": {
"model": "calculate_normals_test:block/fire_accurate_normals_test",
"y": 180
},
"facing=south,lit=false": {
"model": "calculate_normals_test:block/fire_accurate_normals_test"
},
"facing=south,lit=true": {
"model": "calculate_normals_test:block/fire_accurate_normals_test"
},
"facing=west,lit=false": {
"model": "calculate_normals_test:block/fire_accurate_normals_test",
"y": 90
},
"facing=west,lit=true": {
"model": "calculate_normals_test:block/fire_accurate_normals_test",
"y": 90
}
}
}
@@ -0,0 +1,94 @@
{
"parent": "block/block",
"display": {
"head": {
"translation": [ 0, 10.5, 0 ]
}
},
"textures": {
"particle": "block/campfire_log",
"log": "block/campfire_log",
"fire": "minecraft:block/campfire_fire",
"lit_log": "minecraft:block/campfire_log_lit"
},
"elements": [
{
"from": [ 1, 0, 0 ],
"to": [ 5, 4, 16 ],
"faces": {
"north": { "uv": [ 0, 4, 4, 8 ], "texture": "#log", "cullface": "north" },
"east": { "uv": [ 0, 1, 16, 5 ], "texture": "#lit_log" },
"south": { "uv": [ 0, 4, 4, 8 ], "texture": "#log", "cullface": "south" },
"west": { "uv": [ 16, 0, 0, 4 ], "texture": "#log" },
"up": { "uv": [ 0, 0, 16, 4 ], "rotation": 90, "texture": "#log" },
"down": { "uv": [ 0, 0, 16, 4 ], "rotation": 90, "texture": "#log", "cullface": "down" }
}
},
{
"from": [ 0, 3, 11 ],
"to": [ 16, 7, 15 ],
"faces": {
"north": { "uv": [ 16, 0, 0, 4 ], "texture": "#lit_log" },
"east": { "uv": [ 0, 4, 4, 8 ], "texture": "#log", "cullface": "east" },
"south": { "uv": [ 0, 0, 16, 4 ], "texture": "#lit_log" },
"west": { "uv": [ 0, 4, 4, 8 ], "texture": "#log", "cullface": "west" },
"up": { "uv": [ 0, 0, 16, 4 ], "rotation": 180, "texture": "#log" },
"down": { "uv": [ 0, 4, 16, 8 ], "texture": "#lit_log" }
}
},
{
"from": [ 11, 0, 0 ],
"to": [ 15, 4, 16 ],
"faces": {
"north": { "uv": [ 0, 4, 4, 8 ], "texture": "#log", "cullface": "north" },
"east": { "uv": [ 0, 0, 16, 4 ], "texture": "#log" },
"south": { "uv": [ 0, 4, 4, 8 ], "texture": "#log", "cullface": "south" },
"west": { "uv": [ 16, 1, 0, 5 ], "texture": "#lit_log" },
"up": { "uv": [ 0, 0, 16, 4 ], "rotation": 90, "texture": "#log" },
"down": { "uv": [ 0, 0, 16, 4 ], "rotation": 90, "texture": "#log", "cullface": "down" }
}
},
{
"from": [ 0, 3, 1 ],
"to": [ 16, 7, 5 ],
"faces": {
"north": { "uv": [ 0, 0, 16, 4 ], "texture": "#lit_log" },
"east": { "uv": [ 0, 4, 4, 8 ], "texture": "#log", "cullface": "east" },
"south": { "uv": [ 16, 0, 0, 4 ], "texture": "#lit_log" },
"west": { "uv": [ 0, 4, 4, 8 ], "texture": "#log", "cullface": "west" },
"up": { "uv": [ 0, 0, 16, 4 ], "rotation": 180, "texture": "#log" },
"down": { "uv": [ 0, 4, 16, 8 ], "texture": "#lit_log" }
}
},
{
"from": [ 5, 0, 0 ],
"to": [ 11, 1, 16 ],
"faces": {
"north": {"uv": [ 0, 15, 6, 16 ], "texture": "#log", "cullface": "north" },
"south": {"uv": [ 10, 15, 16, 16 ], "texture": "#log", "cullface": "south" },
"up": {"uv": [ 0, 8, 16, 14 ], "rotation": 90, "texture": "#lit_log" },
"down": {"uv": [ 0, 8, 16, 14 ], "rotation": 90, "texture": "#log", "cullface": "down" }
}
},
{ "from": [ 0.8, 1, 8 ],
"to": [ 15.2, 17, 8 ],
"rotation": { "origin": [ 8, 8, 8 ], "axis": "y", "angle": 45, "rescale": true },
"shade": false,
"faces": {
"north": { "uv": [ 0, 0, 16, 16 ], "texture": "#fire" },
"south": { "uv": [ 0, 0, 16, 16 ], "texture": "#fire" }
},
"forge_data": {"calculate_normals": true}
},
{ "from": [ 8, 1, 0.8 ],
"to": [ 8, 17, 15.2 ],
"rotation": { "origin": [ 8, 8, 8 ], "axis": "y", "angle": 45, "rescale": true },
"shade": false,
"faces": {
"west": { "uv": [ 0, 0, 16, 16 ], "texture": "#fire" },
"east": { "uv": [ 0, 0, 16, 16 ], "texture": "#fire" }
},
"forge_data": {"calculate_normals": true}
}
]
}
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "minecraft:item/campfire"
}
}