Skip to content

Commit

Permalink
Prepare for renderer support
Browse files Browse the repository at this point in the history
When sodium has support for the renderer api, this can be enabled and the block entity renderer can be scrapped. Since the model is completely static, we can bake it to the chunk mesh rather than upload every frame
  • Loading branch information
dhyces committed Apr 6, 2024
1 parent 52ec602 commit 40b75db
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/client/java/moriyashiine/aylyth/client/AylythClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.terraformersmc.terraform.boat.api.client.TerraformBoatClientHelper;
import com.terraformersmc.terraform.sign.SpriteIdentifierRegistry;
import moriyashiine.aylyth.client.advancement.AdvancementIconRendererRegistry;
import moriyashiine.aylyth.client.model.block.SoulHearthBlockModel;
import moriyashiine.aylyth.client.model.entity.layer.CuirassModel;
import moriyashiine.aylyth.client.model.entity.layer.YmpeInfestationModel;
import moriyashiine.aylyth.client.model.entity.layer.YmpeThornRingModel;
Expand Down Expand Up @@ -236,6 +237,17 @@ public void onInitializeClient() {
});
});

if (false) {
ModelLoadingPlugin.register(pluginContext -> {
pluginContext.modifyModelAfterBake().register((model, context) -> {
if (context.id().getPath().contains("soul_hearth_charged")) {
return new SoulHearthBlockModel(model);
}
return model;
});
});
}

// ModelLoadingRegistry.INSTANCE.registerModelProvider((manager, out) -> out.accept(new ModelIdentifier(AylythUtil.id("%s_generated".formatted(Registries.ITEM.getId(ModItems.MYSTERIOUS_SKETCH).getPath())), "inventory")));
BuiltinItemRendererRegistry.INSTANCE.register(ModItems.WOODY_GROWTH_CACHE, new WoodyGrowthCacheItemRenderer());
// BuiltinItemRendererRegistry.INSTANCE.register(ModItems.MYSTERIOUS_SKETCH, new MysteriousSketchItemRenderer());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package moriyashiine.aylyth.client.model.block;

import com.mojang.blaze3d.systems.RenderSystem;
import moriyashiine.aylyth.common.block.SoulHearthBlock;
import moriyashiine.aylyth.common.registry.ModBlocks;
import moriyashiine.aylyth.common.registry.ModItems;
import moriyashiine.aylyth.common.util.AylythUtil;
import net.fabricmc.fabric.api.renderer.v1.model.ForwardingBakedModel;
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext;
import net.minecraft.block.BlockState;
import net.minecraft.block.enums.DoubleBlockHalf;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RotationAxis;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.BlockRenderView;
import org.joml.Vector3f;

import java.util.function.Supplier;

public class SoulHearthBlockModel extends ForwardingBakedModel {

public SoulHearthBlockModel(BakedModel model) {
this.wrapped = model;
}

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

@Override
public void emitBlockQuads(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) {
super.emitBlockQuads(blockView, state, pos, randomSupplier, context);
if (state.isOf(ModBlocks.SOUL_HEARTH) && state.get(SoulHearthBlock.HALF) == DoubleBlockHalf.LOWER) {
BakedModel model = MinecraftClient.getInstance().getBakedModelManager().getModel(new ModelIdentifier(AylythUtil.id("pomegranate"), "inventory"));
MatrixStack stack = new MatrixStack();
stack.push();
float scale = 0.4f;
stack.scale(scale, scale, scale);
stack.translate(0.3/scale, 0.75/scale, 0.3/scale);
for (int i = 0; i < state.get(SoulHearthBlock.CHARGES); i++) {
stack.translate(0, 0.06, 0);
stack.push();
stack.multiply(RotationAxis.POSITIVE_X.rotationDegrees(90));
stack.multiply(RotationAxis.POSITIVE_Z.rotationDegrees(90 * i), 0.5f, 0.5f, 0.5f);
context.pushTransform(quad -> {
Vector3f posVec = new Vector3f();
for (int j = 0; j < 4; j++) {
quad.copyPos(j, posVec);
posVec.mulProject(stack.peek().getPositionMatrix());
quad.pos(j, posVec);
}
return true;
});
model.emitItemQuads(new ItemStack(ModItems.POMEGRANATE), randomSupplier, context);
context.popTransform();
stack.pop();
}
stack.pop();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package moriyashiine.aylyth.client.util;

import com.mojang.blaze3d.systems.RenderSystem;
import me.shedaniel.math.Rectangle;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.client.render.DiffuseLighting;
import net.minecraft.client.render.LightmapTextureManager;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.EntityRenderDispatcher;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.util.math.RotationAxis;
import org.jetbrains.annotations.Nullable;

public class RenderUtils {

Expand Down

0 comments on commit 40b75db

Please sign in to comment.