diff --git a/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/CustomBlendModeModel.java b/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/CustomBlendModeModel.java new file mode 100644 index 000000000..38f30842a --- /dev/null +++ b/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/CustomBlendModeModel.java @@ -0,0 +1,54 @@ +package io.github.fabricators_of_create.porting_lib.models; + +import java.util.function.Supplier; + +import net.fabricmc.fabric.api.renderer.v1.Renderer; +import net.fabricmc.fabric.api.renderer.v1.RendererAccess; +import net.fabricmc.fabric.api.renderer.v1.material.BlendMode; +import net.fabricmc.fabric.api.renderer.v1.material.MaterialFinder; +import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial; +import net.fabricmc.fabric.api.renderer.v1.model.ForwardingBakedModel; +import net.fabricmc.fabric.api.renderer.v1.render.RenderContext; +import net.fabricmc.fabric.api.renderer.v1.render.RenderContext.QuadTransform; +import net.minecraft.client.resources.model.BakedModel; +import net.minecraft.core.BlockPos; +import net.minecraft.util.RandomSource; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.BlockAndTintGetter; +import net.minecraft.world.level.block.state.BlockState; + +public class CustomBlendModeModel extends ForwardingBakedModel { + private final QuadTransform transform; + + + public CustomBlendModeModel(BakedModel owner, BlendMode blendMode) { + this.wrapped = owner; + + Renderer renderer = RendererAccess.INSTANCE.getRenderer(); + MaterialFinder finder = renderer == null ? null : renderer.materialFinder(); + this.transform = finder == null ? quad -> true : quad -> { + RenderMaterial newMaterial = finder.copyFrom(quad.material()).blendMode(blendMode).find(); + quad.material(newMaterial); + return true; + }; + } + + @Override + public boolean isVanillaAdapter() { + return false; + } + + @Override + public void emitBlockQuads(BlockAndTintGetter blockView, BlockState state, BlockPos pos, Supplier randomSupplier, RenderContext context) { + context.pushTransform(this.transform); + super.emitBlockQuads(blockView, state, pos, randomSupplier, context); + context.popTransform(); + } + + @Override + public void emitItemQuads(ItemStack stack, Supplier randomSupplier, RenderContext context) { + context.pushTransform(this.transform); + super.emitItemQuads(stack, randomSupplier, context); + context.popTransform(); + } +} diff --git a/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/extensions/BlockModelExtensions.java b/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/extensions/BlockModelExtensions.java index ea013d740..6f996b192 100644 --- a/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/extensions/BlockModelExtensions.java +++ b/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/extensions/BlockModelExtensions.java @@ -1,5 +1,7 @@ package io.github.fabricators_of_create.porting_lib.models.extensions; +import net.fabricmc.fabric.api.renderer.v1.material.BlendMode; + import org.jetbrains.annotations.ApiStatus; import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial; @@ -7,4 +9,5 @@ @ApiStatus.Internal public interface BlockModelExtensions { void port_lib$setRenderMaterial(RenderMaterial material); + void port_lib$setBlendMode(BlendMode blendMode); } diff --git a/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/mixin/client/BlockModelDeserializerMixin.java b/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/mixin/client/BlockModelDeserializerMixin.java index e3fca6efa..12e7a814c 100644 --- a/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/mixin/client/BlockModelDeserializerMixin.java +++ b/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/mixin/client/BlockModelDeserializerMixin.java @@ -38,8 +38,7 @@ private BlockModel addModelRenderType(BlockModel model, JsonElement jsonElement, if (renderer != null) { String typeName = GsonHelper.getAsString(modelJson, "render_type"); BlendMode blendMode = BlendMode.fromRenderLayer(RenderTypeUtil.get(new ResourceLocation(typeName))); - RenderMaterial material = renderer.materialFinder().blendMode(blendMode).find(); - ((BlockModelExtensions) model).port_lib$setRenderMaterial(material); + ((BlockModelExtensions) model).port_lib$setBlendMode(blendMode); } } diff --git a/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/mixin/client/BlockModelMixin.java b/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/mixin/client/BlockModelMixin.java index da69dbb47..2b28e8df8 100644 --- a/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/mixin/client/BlockModelMixin.java +++ b/modules/models/src/main/java/io/github/fabricators_of_create/porting_lib/models/mixin/client/BlockModelMixin.java @@ -2,6 +2,8 @@ import java.util.function.Function; +import io.github.fabricators_of_create.porting_lib.models.CustomBlendModeModel; +import net.fabricmc.fabric.api.renderer.v1.material.BlendMode; import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial; import org.spongepowered.asm.mixin.Mixin; @@ -24,18 +26,29 @@ public class BlockModelMixin implements BlockModelExtensions { @Unique private RenderMaterial material; + @Unique + private BlendMode blendMode; @Override public void port_lib$setRenderMaterial(RenderMaterial material) { this.material = material; } + @Override + public void port_lib$setBlendMode(BlendMode blendMode) { + this.blendMode = blendMode; + } + @ModifyReturnValue(method = "bake(Lnet/minecraft/client/resources/model/ModelBaker;Lnet/minecraft/client/renderer/block/model/BlockModel;Ljava/util/function/Function;Lnet/minecraft/client/resources/model/ModelState;Lnet/minecraft/resources/ResourceLocation;Z)Lnet/minecraft/client/resources/model/BakedModel;", at = @At("RETURN")) - private BakedModel useRenderMaterial(BakedModel model, ModelBaker modelBaker, BlockModel blockModel, - Function function, ModelState modelState, - ResourceLocation resourceLocation, boolean bl) { - if (material != null) - return new RenderMaterialModel(model, material); - return model; + private BakedModel useCustomRendering(BakedModel model, ModelBaker modelBaker, BlockModel blockModel, + Function function, ModelState modelState, + ResourceLocation resourceLocation, boolean bl) { + if (this.material != null) { + return new RenderMaterialModel(model, this.material); + } else if (blendMode != null) { + return new CustomBlendModeModel(model, blendMode); + } else { + return model; + } } } diff --git a/modules/models/src/testmod/java/io/github/fabricators_of_create/porting_lib/models/testmod/PortingLibModelsTestmod.java b/modules/models/src/testmod/java/io/github/fabricators_of_create/porting_lib/models/testmod/PortingLibModelsTestmod.java index 7e07fbe8f..d8a8d2dd6 100644 --- a/modules/models/src/testmod/java/io/github/fabricators_of_create/porting_lib/models/testmod/PortingLibModelsTestmod.java +++ b/modules/models/src/testmod/java/io/github/fabricators_of_create/porting_lib/models/testmod/PortingLibModelsTestmod.java @@ -3,18 +3,23 @@ import io.github.fabricators_of_create.porting_lib.core.PortingLib; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.item.v1.FabricItemSettings; +import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.minecraft.core.Registry; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.item.Item; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; public class PortingLibModelsTestmod implements ModInitializer { - public static Item DERPY_HELMET = new Item(new FabricItemSettings().equipmentSlot(stack -> EquipmentSlot.HEAD)); + public static final Item DERPY_HELMET = new Item(new FabricItemSettings().equipmentSlot(stack -> EquipmentSlot.HEAD)); public static final Item STONE_2 = new Item(new FabricItemSettings()); + public static final Block NOT_GLASS = new Block(FabricBlockSettings.copyOf(Blocks.GLASS)); @Override public void onInitialize() { Registry.register(BuiltInRegistries.ITEM, PortingLib.id("derp_helmet"), DERPY_HELMET); Registry.register(BuiltInRegistries.ITEM, PortingLib.id("stone_2"), STONE_2); + Registry.register(BuiltInRegistries.BLOCK, PortingLib.id("not_glass"), NOT_GLASS); } } diff --git a/modules/models/src/testmod/resources/assets/porting_lib/blockstates/not_glass.json b/modules/models/src/testmod/resources/assets/porting_lib/blockstates/not_glass.json new file mode 100644 index 000000000..01e50864b --- /dev/null +++ b/modules/models/src/testmod/resources/assets/porting_lib/blockstates/not_glass.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "porting_lib:block/not_glass" + } + } +} diff --git a/modules/models/src/testmod/resources/assets/porting_lib/lang/en_us.json b/modules/models/src/testmod/resources/assets/porting_lib/lang/en_us.json index 7b7c16799..a4985ae8a 100644 --- a/modules/models/src/testmod/resources/assets/porting_lib/lang/en_us.json +++ b/modules/models/src/testmod/resources/assets/porting_lib/lang/en_us.json @@ -1,3 +1,5 @@ { - "item.porting_lib.derp_helmet": "Derpy Helmet" + "item.porting_lib.derp_helmet": "Derpy Helmet", + "item.porting_lib.stone_2": "Stone 2", + "block.porting_lib.not_glass": "Not Glass" } diff --git a/modules/models/src/testmod/resources/assets/porting_lib/models/block/not_glass.json b/modules/models/src/testmod/resources/assets/porting_lib/models/block/not_glass.json new file mode 100644 index 000000000..3443c15af --- /dev/null +++ b/modules/models/src/testmod/resources/assets/porting_lib/models/block/not_glass.json @@ -0,0 +1,7 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { + "all": "minecraft:block/glass" + }, + "render_type": "cutout" +}