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/13 15:43] – added more information for client entrypoint fudge | tutorial:blockentityrenderers [2019/12/14 10:43] – fix typo juuz | ||
---|---|---|---|
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: | ||
Make sure you [[tutorial: | Make sure you [[tutorial: | ||
Line 16: | Line 18: | ||
// A jukebox itemstack | // A jukebox itemstack | ||
private static ItemStack stack = new ItemStack(Items.JUKEBOX, | private static ItemStack stack = new ItemStack(Items.JUKEBOX, | ||
+ | | ||
+ | public MyBlockEntityRenderer(BlockEntityRenderDispatcher dispatcher) { | ||
+ | super(dispatcher); | ||
+ | } | ||
| | ||
@Override | @Override | ||
- | public void render(DemoBlockEntity blockEntity, | + | public void render(DemoBlockEntity blockEntity, |
} | } | ||
} | } | ||
Line 54: | Line 60: | ||
@Override | @Override | ||
public void onInitializeClient() { | public void onInitializeClient() { | ||
- | BlockEntityRendererRegistry.INSTANCE.register(DemoBlockEntity.class, | + | BlockEntityRendererRegistry.INSTANCE.register(DemoBlockEntity.class, |
} | } | ||
</ | </ | ||
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. | 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: | We recalculate this each time because we want it to be animating bouncing up and down. We calculate this by: | ||
Line 74: | Line 80: | ||
* 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 | ||
- | | + | |
} | } | ||
</ | </ | ||
Line 88: | Line 94: | ||
We also pass '' | We also pass '' | ||
an item lying on the ground. Try experimenting with this value and see what happens (it's an enum). | an item lying on the ground. Try experimenting with this value and see what happens (it's an enum). | ||
- | We also need to call '' | + | 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 111: | ||
To fix this, we will tell Minecraft to receive light from //one block above// the location of the block entity. | 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); | + | |
- | | + | |
| | ||
[...] | [...] |
tutorial/blockentityrenderers.txt · Last modified: 2023/02/09 13:14 by mschae23