tutorial:blockentityrenderers
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:blockentityrenderers [2019/08/06 18:35] – fudge | tutorial:blockentityrenderers [2022/12/16 02:27] – solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Rendering blocks and items dynamically using block entity renderers ====== | ====== Rendering blocks and items dynamically using block entity renderers ====== | ||
+ | |||
+ | //This is the 1.15 & 1.16 version of this tutorial. For the 1.14 version, see [[tutorial: | ||
Make sure you [[tutorial: | Make sure you [[tutorial: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Some may say blocks | + | Blocks |
- | they just stay static at a certain location and a certain size until broken. | + | |
- | We can use block entity renderers to render items and blocks associated with a block entity far more dynamically - render multiple different items, | + | |
- | at differing locations and sizes, and more. | + | |
===== Example ===== | ===== Example ===== | ||
- | In this tutorial we'll build off the block entity we created by adding a '' | + | In this tutorial we'll build off the block entity we created by adding a '' |
- | The renderer will display a jukebox floating above the block, going up and down and spinning. | + | |
| | ||
The first thing we need to do is create our '' | The first thing we need to do is create our '' | ||
<code java> | <code java> | ||
- | public class MyBlockEntityRenderer extends | + | @Environment(EnvType.CLIENT) |
+ | public class DemoBlockEntityRenderer implements | ||
// A jukebox itemstack | // A jukebox itemstack | ||
private static ItemStack stack = new ItemStack(Items.JUKEBOX, | private static ItemStack stack = new ItemStack(Items.JUKEBOX, | ||
| | ||
+ | public DemoBlockEntityRenderer(BlockEntityRendererFactory.Context ctx) {} | ||
+ | |||
@Override | @Override | ||
- | public void render(DemoBlockEntity blockEntity, | + | public void render(DemoBlockEntity blockEntity, |
} | } | ||
} | } | ||
</ | </ | ||
- | And register | + | We're going to need to register |
+ | |||
+ | Create a new class next to your main class that implements '' | ||
<code java> | <code java> | ||
- | @Override | + | @Environment(EnvType.CLIENT) |
- | public void onInitialize() { | + | public class ExampleModClient implements ClientModInitializer { |
+ | | ||
+ | public void onInitializeClient() { | ||
+ | // Here we will put client-only registration code | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Set this class as the '' | ||
+ | <code javascript " | ||
+ | " | ||
[...] | [...] | ||
- | BlockEntityRendererRegistry.INSTANCE.register(DemoBlockEntity.class, new MyBlockEntityRenderer()); | + | |
+ | { | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | And register the '' | ||
+ | <code java> | ||
+ | @Override | ||
+ | public void onInitializeClient() { | ||
+ | | ||
} | } | ||
</ | </ | ||
We override the '' | We override the '' | ||
- | - for starters, call '' | + | - for starters, call '' |
<code java> | <code java> | ||
- | public void render(DemoBlockEntity blockEntity, | + | public void render(DemoBlockEntity blockEntity, |
- | GlStateManager.pushMatrix(); | + | matrices.push(); |
} | } | ||
</ | </ | ||
- | We then perform the movement of the jukebox (GlStateManager.translatef) and rotation (GlStateManager.rotatef). | + | We then perform the movement of the jukebox (matrices.translate) and rotation (matrices.multiply). There are two parts to the translation: |
- | There are two parts to the translation: | + | |
- | The second part is the part that changes: the offset in the y value. The offset is the height of the item for any given frame. | + | |
- | We recalculate this each time because we want it to be animating bouncing up and down. We calculate this by: | + | |
* Getting the current world time, which changes over time. | * Getting the current world time, which changes over time. | ||
* Adding the partial ticks. (The partial ticks is a fractional value representing the amount of time that’s passed between the last full tick and now. We use this because otherwise the animation would be jittery because there are fewer ticks per second than frames per second.) | * Adding the partial ticks. (The partial ticks is a fractional value representing the amount of time that’s passed between the last full tick and now. We use this because otherwise the animation would be jittery because there are fewer ticks per second than frames per second.) | ||
Line 47: | Line 69: | ||
* Dividing that by 4 to compress the sine wave vertically so the item doesn’t move up and down as much. | * Dividing that by 4 to compress the sine wave vertically so the item doesn’t move up and down as much. | ||
<code java> | <code java> | ||
- | public void render(DemoBlockEntity blockEntity, | + | public void render(DemoBlockEntity blockEntity, |
[...] | [...] | ||
// Calculate the current offset in the y value | // Calculate the current offset in the y value | ||
- | double offset = Math.sin((blockEntity.getWorld().getTime() + partialTicks) / 8.0) / 4.0; | + | double offset = Math.sin((blockEntity.getWorld().getTime() + tickDelta) / 8.0) / 4.0; |
// Move the item | // Move the item | ||
- | | + | |
// Rotate the item | // Rotate the item | ||
- | | + | |
} | } | ||
</ | </ | ||
- | Finally, we will get the Minecraft ' | + | Finally, we will get the Minecraft ' |
- | We also pass '' | + | |
- | an item lying on the ground. Try experimenting with this value and see what happens (it's an enum). | + | |
- | We also need to call '' | + | |
<code java> | <code java> | ||
- | public void render(DemoBlockEntity blockEntity, | + | public void render(DemoBlockEntity blockEntity, |
[...] | [...] | ||
- | MinecraftClient.getInstance().getItemRenderer().renderItem(stack, | + | MinecraftClient.getInstance().getItemRenderer().renderItem(stack, |
// Mandatory call after GL calls | // Mandatory call after GL calls | ||
- | | + | |
} | } | ||
</ | </ | ||
- | You can try your newly created block entity renderer right now. | + | You can try your newly created block entity renderer right now. However, if you didn't make your block transparent, |
- | However, if you didn't make your block transparent, | + | |
- | This is because by default, //whatever you render in the block entity, will receive light as if it's in the same position as the block entity//. | + | |
- | So the floating block receives light from //inside// our opaque block, which means it receives no light! | + | |
- | To fix this, we will tell Minecraft to receive light from //one block above// the location of the block entity. | + | |
- | To get the light, we call '' | + | To get the light, we call '' |
- | and to use the light we call '' | + | and to use the light we use it in '' |
<code java> | <code java> | ||
@Override | @Override | ||
- | public void render(DemoBlockEntity blockEntity, | + | public void render(DemoBlockEntity blockEntity, |
[...] | [...] | ||
| | ||
- | | + | |
- | int light = blockEntity.getWorld().getLightmapIndex(blockEntity.getPos().up(), 0); | + | |
- | | + | |
| | ||
[...] | [...] | ||
Line 94: | Line 108: | ||
The jukebox should now have the proper lighting. | The jukebox should now have the proper lighting. | ||
+ | |||
+ | ===== Rendering according to block entity data ===== | ||
+ | Sometimes you wants to render according to the block entity data (nbt), and you find they are all empty, even if you can access the data through ''/ |
tutorial/blockentityrenderers.txt · Last modified: 2024/08/27 04:42 by solidblock