Fix and optimize model hacks

- Remove isVanillaAdapter check from CullingBakingModel.wrap
- Optimize model wrapping in ModelUtil.getBufferBuilderFromTemplate
This commit is contained in:
PepperCode1 2022-02-06 16:28:33 -08:00
parent 2b5e71e172
commit 59c4ac3cb8
4 changed files with 21 additions and 7 deletions

View file

@ -8,6 +8,7 @@ import java.util.function.Supplier;
import com.jozufozu.flywheel.core.virtual.VirtualEmptyBlockGetter; import com.jozufozu.flywheel.core.virtual.VirtualEmptyBlockGetter;
import com.jozufozu.flywheel.fabric.model.CullingBakedModel; import com.jozufozu.flywheel.fabric.model.CullingBakedModel;
import com.jozufozu.flywheel.fabric.model.DefaultLayerFilteringBakedModel; import com.jozufozu.flywheel.fabric.model.DefaultLayerFilteringBakedModel;
import com.jozufozu.flywheel.fabric.model.FabricModelUtil;
import com.jozufozu.flywheel.fabric.model.LayerFilteringBakedModel; import com.jozufozu.flywheel.fabric.model.LayerFilteringBakedModel;
import com.jozufozu.flywheel.util.Lazy; import com.jozufozu.flywheel.util.Lazy;
import com.jozufozu.flywheel.util.transform.TransformStack; import com.jozufozu.flywheel.util.transform.TransformStack;
@ -16,6 +17,7 @@ import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexFormat; import com.mojang.blaze3d.vertex.VertexFormat;
import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.BlockModelShaper; import net.minecraft.client.renderer.block.BlockModelShaper;
@ -68,13 +70,20 @@ public class ModelUtil {
if (state.getRenderShape() != RenderShape.MODEL) if (state.getRenderShape() != RenderShape.MODEL)
continue; continue;
BakedModel model = blockModels.getBlockModel(state);
if (((FabricBakedModel) model).isVanillaAdapter()) {
if (!FabricModelUtil.doesLayerMatch(state, layer)) {
continue;
}
} else {
model = CullingBakedModel.wrap(model);
model = LayerFilteringBakedModel.wrap(model, layer);
}
BlockPos pos = info.pos; BlockPos pos = info.pos;
ms.pushPose(); ms.pushPose();
ms.translate(pos.getX(), pos.getY(), pos.getZ()); ms.translate(pos.getX(), pos.getY(), pos.getZ());
BakedModel model = blockModels.getBlockModel(state);
model = CullingBakedModel.wrap(model);
model = LayerFilteringBakedModel.wrap(model, layer);
modelRenderer.tesselateBlock(renderWorld, model, state, pos, ms, builder, modelRenderer.tesselateBlock(renderWorld, model, state, pos, ms, builder,
true, random, 42, OverlayTexture.NO_OVERLAY); true, random, 42, OverlayTexture.NO_OVERLAY);
ms.popPose(); ms.popPose();

View file

@ -3,7 +3,6 @@ package com.jozufozu.flywheel.fabric.model;
import java.util.Random; import java.util.Random;
import java.util.function.Supplier; import java.util.function.Supplier;
import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;
import net.fabricmc.fabric.api.renderer.v1.model.ForwardingBakedModel; 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;
import net.minecraft.client.resources.model.BakedModel; import net.minecraft.client.resources.model.BakedModel;
@ -22,7 +21,7 @@ public class CullingBakedModel extends ForwardingBakedModel {
protected final BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos(); protected final BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
public static BakedModel wrap(BakedModel model) { public static BakedModel wrap(BakedModel model) {
if (!FabricModelUtil.FREX_LOADED && !((FabricBakedModel) model).isVanillaAdapter()) { if (!FabricModelUtil.FREX_LOADED) {
CullingBakedModel wrapper = THREAD_LOCAL.get(); CullingBakedModel wrapper = THREAD_LOCAL.get();
wrapper.wrapped = model; wrapper.wrapped = model;
return wrapper; return wrapper;

View file

@ -10,6 +10,9 @@ import net.fabricmc.fabric.api.renderer.v1.material.BlendMode;
import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial; import net.fabricmc.fabric.api.renderer.v1.material.RenderMaterial;
import net.fabricmc.fabric.impl.client.indigo.renderer.RenderMaterialImpl; import net.fabricmc.fabric.impl.client.indigo.renderer.RenderMaterialImpl;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.world.level.block.state.BlockState;
public class FabricModelUtil { public class FabricModelUtil {
public static final boolean INDIUM_LOADED = FabricLoader.getInstance().isModLoaded("indium"); public static final boolean INDIUM_LOADED = FabricLoader.getInstance().isModLoaded("indium");
@ -61,6 +64,10 @@ public class FabricModelUtil {
return BLEND_MODE_GETTER.getBlendMode(material); return BLEND_MODE_GETTER.getBlendMode(material);
} }
public static boolean doesLayerMatch(BlockState modelState, RenderType layer) {
return ItemBlockRenderTypes.getChunkRenderType(modelState) == layer;
}
private interface BlendModeGetter { private interface BlendModeGetter {
BlendMode getBlendMode(RenderMaterial material); BlendMode getBlendMode(RenderMaterial material);
} }

View file

@ -3,7 +3,6 @@ package com.jozufozu.flywheel.fabric.model;
import java.util.Random; import java.util.Random;
import java.util.function.Supplier; import java.util.function.Supplier;
import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;
import net.fabricmc.fabric.api.renderer.v1.model.ForwardingBakedModel; 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;
import net.minecraft.client.renderer.ItemBlockRenderTypes; import net.minecraft.client.renderer.ItemBlockRenderTypes;
@ -36,7 +35,7 @@ public class LayerFilteringBakedModel extends ForwardingBakedModel {
@Override @Override
public void emitBlockQuads(BlockAndTintGetter blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) { public void emitBlockQuads(BlockAndTintGetter blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) {
RenderType defaultLayer = ItemBlockRenderTypes.getChunkRenderType(state); RenderType defaultLayer = ItemBlockRenderTypes.getChunkRenderType(state);
if (((FabricBakedModel) wrapped).isVanillaAdapter()) { if (super.isVanillaAdapter()) {
if (defaultLayer == targetLayer) { if (defaultLayer == targetLayer) {
super.emitBlockQuads(blockView, state, pos, randomSupplier, context); super.emitBlockQuads(blockView, state, pos, randomSupplier, context);
} }