From 7c842499965c0ca94bc071805206462052e34dc8 Mon Sep 17 00:00:00 2001 From: Kovak Date: Wed, 19 Jul 2023 14:44:28 -0700 Subject: [PATCH] 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. --- .../SheetedDecalTextureGenerator.java.patch | 11 +++ .../client/ForgeHooksClient.java | 36 +++++++ .../minecraftforge/common/ForgeConfig.java | 7 ++ .../resources/assets/forge/lang/en_us.json | 2 + .../client/model/CalculateNormalsTest.java | 67 +++++++++++++ src/test/resources/META-INF/mods.toml | 3 + .../fire_accurate_normals_test.json | 34 +++++++ .../block/fire_accurate_normals_test.json | 94 +++++++++++++++++++ .../item/fire_accurate_normals_test.json | 6 ++ 9 files changed, 260 insertions(+) create mode 100644 patches/minecraft/com/mojang/blaze3d/vertex/SheetedDecalTextureGenerator.java.patch create mode 100644 src/test/java/net/minecraftforge/debug/client/model/CalculateNormalsTest.java create mode 100644 src/test/resources/assets/calculate_normals_test/blockstates/fire_accurate_normals_test.json create mode 100644 src/test/resources/assets/calculate_normals_test/models/block/fire_accurate_normals_test.json create mode 100644 src/test/resources/assets/calculate_normals_test/models/item/fire_accurate_normals_test.json diff --git a/patches/minecraft/com/mojang/blaze3d/vertex/SheetedDecalTextureGenerator.java.patch b/patches/minecraft/com/mojang/blaze3d/vertex/SheetedDecalTextureGenerator.java.patch new file mode 100644 index 0000000000..92f367c09b --- /dev/null +++ b/patches/minecraft/com/mojang/blaze3d/vertex/SheetedDecalTextureGenerator.java.patch @@ -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)); diff --git a/src/main/java/net/minecraftforge/client/ForgeHooksClient.java b/src/main/java/net/minecraftforge/client/ForgeHooksClient.java index 2538da64f0..eaa2bc39c5 100644 --- a/src/main/java/net/minecraftforge/client/ForgeHooksClient.java +++ b/src/main/java/net/minecraftforge/client/ForgeHooksClient.java @@ -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. diff --git a/src/main/java/net/minecraftforge/common/ForgeConfig.java b/src/main/java/net/minecraftforge/common/ForgeConfig.java index c76c7019e2..9607f01993 100644 --- a/src/main/java/net/minecraftforge/common/ForgeConfig.java +++ b/src/main/java/net/minecraftforge/common/ForgeConfig.java @@ -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(); } } diff --git a/src/main/resources/assets/forge/lang/en_us.json b/src/main/resources/assets/forge/lang/en_us.json index 95be069adf..23ddc25608 100644 --- a/src/main/resources/assets/forge/lang/en_us.json +++ b/src/main/resources/assets/forge/lang/en_us.json @@ -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", diff --git a/src/test/java/net/minecraftforge/debug/client/model/CalculateNormalsTest.java b/src/test/java/net/minecraftforge/debug/client/model/CalculateNormalsTest.java new file mode 100644 index 0000000000..1c5e705436 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/client/model/CalculateNormalsTest.java @@ -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 BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID); + public static final DeferredRegister ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID); + + public static RegistryObject 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 fire_accurate_normals_test_item = ITEMS.register("fire_accurate_normals_test", () -> + new BlockItem(fire_accurate_normals_test.get(), new Item.Properties())); + + private static ToIntFunction 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()); + } + } + +} diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index 98eb540d13..387a30d4b2 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -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. diff --git a/src/test/resources/assets/calculate_normals_test/blockstates/fire_accurate_normals_test.json b/src/test/resources/assets/calculate_normals_test/blockstates/fire_accurate_normals_test.json new file mode 100644 index 0000000000..4d4a79dd62 --- /dev/null +++ b/src/test/resources/assets/calculate_normals_test/blockstates/fire_accurate_normals_test.json @@ -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 + } + } +} diff --git a/src/test/resources/assets/calculate_normals_test/models/block/fire_accurate_normals_test.json b/src/test/resources/assets/calculate_normals_test/models/block/fire_accurate_normals_test.json new file mode 100644 index 0000000000..753834d683 --- /dev/null +++ b/src/test/resources/assets/calculate_normals_test/models/block/fire_accurate_normals_test.json @@ -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} + } + ] +} diff --git a/src/test/resources/assets/calculate_normals_test/models/item/fire_accurate_normals_test.json b/src/test/resources/assets/calculate_normals_test/models/item/fire_accurate_normals_test.json new file mode 100644 index 0000000000..a28fc7df9f --- /dev/null +++ b/src/test/resources/assets/calculate_normals_test/models/item/fire_accurate_normals_test.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:item/generated", + "textures": { + "layer0": "minecraft:item/campfire" + } +}