Skip to content

Commit

Permalink
actually use the render type hint
Browse files Browse the repository at this point in the history
  • Loading branch information
TropheusJ committed Mar 31, 2024
1 parent aa2a74b commit cc29dc1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.github.fabricators_of_create.porting_lib.model;

import java.util.function.Supplier;

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.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 RenderMaterialModel extends ForwardingBakedModel {
protected final RenderMaterial material;

public RenderMaterialModel(BakedModel owner, RenderMaterial material) {
this.wrapped = owner;
this.material = material;
}

@Override
public boolean isVanillaAdapter() {
return false;
}

@Override
public void emitBlockQuads(BlockAndTintGetter blockView, BlockState state, BlockPos pos, Supplier<RandomSource> randomSupplier, RenderContext context) {
context.pushTransform(quad -> {
quad.material(this.material);
return true;
});
super.emitBlockQuads(blockView, state, pos, randomSupplier, context);
context.popTransform();
}

@Override
public void emitItemQuads(ItemStack stack, Supplier<RandomSource> randomSupplier, RenderContext context) {
context.pushTransform(quad -> {
quad.material(this.material);
return true;
});
super.emitItemQuads(stack, randomSupplier, context);
context.popTransform();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.github.fabricators_of_create.porting_lib.client.RenderTypeGroup;
import io.github.fabricators_of_create.porting_lib.model.RenderMaterialModel;
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.RenderMaterial;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
Expand All @@ -19,6 +23,7 @@
import io.github.fabricators_of_create.porting_lib.model.IModelBuilder;
import io.github.fabricators_of_create.porting_lib.model.SimpleModelState;
import net.minecraft.Util;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.BlockElement;
import net.minecraft.client.renderer.block.model.BlockElementFace;
Expand Down Expand Up @@ -100,9 +105,21 @@ public static BakedModel bake(BlockModel blockModel, ModelBakery modelBakery, Bl
if (blockModel.getRootModel() == ModelBakery.GENERATION_MARKER)
return ITEM_MODEL_GENERATOR.generateBlockModel(spriteGetter, blockModel).bake(modelBakery, blockModel, spriteGetter, modelState, modelLocation, guiLight3d);

var renderTypeHint = blockModel.getGeometry().getRenderTypeHint();
var renderTypes = renderTypeHint != null ? blockModel.getGeometry().getRenderType(renderTypeHint) : RenderTypeGroup.EMPTY;
return blockModel.bake(modelBakery, owner, spriteGetter, modelState, modelLocation, guiLight3d);
BakedModel baked = blockModel.bake(modelBakery, owner, spriteGetter, modelState, modelLocation, guiLight3d);

ResourceLocation renderTypeHint = blockModel.getGeometry().getRenderTypeHint();
if (renderTypeHint != null) {
RenderType type = blockModel.getGeometry().getRenderType(renderTypeHint).block();
if (type != null) {
Renderer renderer = RendererAccess.INSTANCE.getRenderer();
if (renderer != null) {
RenderMaterial material = renderer.materialFinder().blendMode(0, BlendMode.fromRenderLayer(type)).find();
baked = new RenderMaterialModel(baked, material);
}
}
}

return baked;
}

/**
Expand Down

0 comments on commit cc29dc1

Please sign in to comment.