Create a BER
BER (BlockEntity Renderer) is responsible of custom rendering of a BlockEntity.
We are not going to go into details of how each rendering property works, but only teach you basics of the rendering.
Let’s say we have BlockEntity called ModBlockEntity and we want to make custom renderer for it.
For this part, we will need these things:
If you have those things set up, follow these steps:
- Create
ModBlockEntityRenderer.javaclassDirectorylibs/
- …
Directorysrc/main/java/your/namespace/your_mod_id/
Directorycommon/
Directoryblockentity/
- ModBlockEntity.java
Directoryregistry/
- ModBlockEntities.java
- ModRegistriesInit.java
Directoryclient/
Directoryrenderer/
Directoryblockentity/
- ModBlockEntityRenderer.java
- ExampleMod.java
- …
ModBlockEntityRenderer.java //... importspublic class ModBlockEntityRenderer implements BlockEntityRenderer<ModBlockEntity> {// this constructor will be necessary to register your renderer laterpublic ModBlockEntityRenderer(BlockEntityRendererProvider.Context ignored) {}// main method that is called to render our stuff every frame - NOT EVERY TICK!// (that means if you have 60fps, this method is called 60 times / second)@Override@ParametersAreNonnullByDefaultpublic void render(ModBlockEntity blockEntity, float partialTicks, PoseStack poseStack, MultiBufferSource bufferSource, int combinedLight, int combinedOverlay) {//...poseStack.pushPose(); // push the renderer to new "layer"// examples of render stuffposeStack.translate(x, y, z); // translate the layer (move the rendered object around)poseStack.scale(scaleX, scaleY, scaleZ); // scale the modelposeStack.mulPose(Axis.YP.rotationDegrees(45)); // rotate the model 45 degrees around Y axis// ---------// RENDER OUR MODEL// the easiest way to render the model// you can try experimenting on your own with other methodsModModels.MY_AWESOME_MODEL_WITH_TEXTURE // get the model.bindTexture() // bind the texture to the shader.render(poseStack, bufferSource, combinedLight); // render vertexes of the obj model by GPU// ---------poseStack.popPose(); // pop the renderer back to previous "layer"}} - Update
ModBlockEntities.javaDirectorylibs/
- …
Directorysrc/main/java/your/namespace/your_mod_id/
Directorycommon/
Directoryblockentity/
- ModBlockEntity.java
Directoryregistry/
- ModBlockEntities.java
- ModRegistriesInit.java
Directoryclient/
Directoryrenderer/
Directoryblockentity/
- ModBlockEntityRenderer.java
- ExampleMod.java
- …
ModBlockEntities.java public class ModBlockEntities {// ...public static final RegistryObject<BlockEntityType<ModBlockEntity>> MY_MOD_BE = ...// ...public static void init() {// register our created BERModRegistriesInit.REGISTRY_HELPER.beRenderers(() -> List.of(new RegistryHelper.BlockEntityRendererPair<>(MY_MOD_BE.get(), ModBlockEntityRenderer::new)// add more for each BER...));}} - That’s it! Try it running ingame!