tutorial:blockentityrenderers
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:blockentityrenderers [2020/03/29 15:44] – Updated based on newer mappings jamieswhiteshirt | tutorial:blockentityrenderers [2024/08/27 04:42] (current) – 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 version of this tutorial. For the 1.14 version, see [[tutorial: | + | //This is the 1.15+ version of this tutorial. For the 1.14 version, see [[tutorial: |
Make sure you [[tutorial: | Make sure you [[tutorial: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Blocks by themselves aren't that interesting, | + | Blocks by themselves aren't that interesting, |
- | 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, 1); | + | private static |
- | + | ||
- | public MyBlockEntityRenderer(BlockEntityRenderDispatcher dispatcher) { | + | |
- | super(dispatcher); | + | |
- | } | + | |
| | ||
+ | public DemoBlockEntityRenderer(BlockEntityRendererFactory.Context ctx) {} | ||
+ | |||
@Override | @Override | ||
public void render(DemoBlockEntity blockEntity, | public void render(DemoBlockEntity blockEntity, | ||
Line 28: | Line 23: | ||
} | } | ||
</ | </ | ||
- | We're going to need to register our '' | + | We're going to need to register our '' |
- | This wouldn' | + | |
- | However, in a multiplayer setting, where the server runs in a different process than the client, the server code | + | |
- | has no concept of a " | + | |
- | To run initialization code only for the client, we need to setup a '' | + | |
- | Create a new class next to your main class that implements '' | + | In the entrypoint |
<code java> | <code java> | ||
+ | @Environment(EnvType.CLIENT) | ||
public class ExampleModClient implements ClientModInitializer { | public class ExampleModClient implements ClientModInitializer { | ||
@Override | @Override | ||
Line 44: | Line 36: | ||
</ | </ | ||
- | Set this class as the '' | + | Set this class as the '' |
<code javascript " | <code javascript " | ||
- | " | + | { |
+ | [...] | ||
+ | | ||
[...] | [...] | ||
" | " | ||
{ | { | ||
- | " | + | " |
} | } | ||
] | ] | ||
+ | }, | ||
+ | [...] | ||
} | } | ||
</ | </ | ||
- | And register the '' | + | And register the '' |
<code java> | <code java> | ||
- | @Override | + | |
- | public void onInitializeClient() { | + | public void onInitializeClient() { |
- | | + | |
- | } | + | } |
</ | </ | ||
We override the '' | We override the '' | ||
Line 70: | Line 66: | ||
} | } | ||
</ | </ | ||
- | We then perform the movement of the jukebox (matrices.translate) and rotation (matrices.multiply). | + | We then perform the movement of the jukebox ('' |
- | 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 88: | Line 81: | ||
// Rotate the item | // Rotate the item | ||
- | matrices.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion((blockEntity.getWorld().getTime() + tickDelta) * 4)); | + | matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees((blockEntity.getWorld().getTime() + tickDelta) * 4)); |
} | } | ||
</ | </ | ||
- | 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 | ||
Line 105: | Line 95: | ||
</ | </ | ||
- | 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 use it in '' | + | |
<code java> | <code java> | ||
@Override | @Override | ||
Line 119: | Line 104: | ||
| | ||
int lightAbove = WorldRenderer.getLightmapCoordinates(blockEntity.getWorld(), | int lightAbove = WorldRenderer.getLightmapCoordinates(blockEntity.getWorld(), | ||
- | MinecraftClient.getInstance().getItemRenderer().renderItem(stack, | + | MinecraftClient.getInstance().getItemRenderer().renderItem(stack, |
| | ||
[...] | [...] | ||
Line 126: | Line 111: | ||
The jukebox should now have the proper lighting. | The jukebox should now have the proper lighting. | ||
+ | |||
+ | ===== Rendering according to block entity data ===== | ||
+ | Sometimes you want 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.1585496689.txt.gz · Last modified: 2020/03/29 15:44 by jamieswhiteshirt