User Tools

Site Tools


tutorial:blockentityrenderers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:blockentityrenderers [2019/08/13 15:43] – added more information for client entrypoint fudgetutorial:blockentityrenderers [2020/03/29 15:44] – Updated based on newer mappings jamieswhiteshirt
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:1.14:blockentityrenderers|Rendering blocks and items dynamically using block entity renderers (1.14)]].//
  
 Make sure you [[tutorial:blockentity|added a block entity]] before reading this tutorial!  Make sure you [[tutorial:blockentity|added a block entity]] before reading this tutorial! 
Line 16: Line 18:
     // A jukebox itemstack     // A jukebox itemstack
     private static ItemStack stack = new ItemStack(Items.JUKEBOX, 1);     private static ItemStack stack = new ItemStack(Items.JUKEBOX, 1);
 +    
 +    public MyBlockEntityRenderer(BlockEntityRenderDispatcher dispatcher) {
 +        super(dispatcher);
 +    }
          
     @Override     @Override
-    public void render(DemoBlockEntity blockEntity, double xdouble ydouble zfloat partialTicks, int destroyStage) {+    public void render(DemoBlockEntity blockEntity, float tickDeltaMatrixStack matricesVertexConsumerProvider vertexConsumersint light, int overlay) {
     }     }
 } }
Line 54: Line 60:
 @Override @Override
 public void onInitializeClient() { public void onInitializeClient() {
-    BlockEntityRendererRegistry.INSTANCE.register(DemoBlockEntity.classnew MyBlockEntityRenderer());+    BlockEntityRendererRegistry.INSTANCE.register(DEMO_BLOCK_ENTITY, MyBlockEntityRenderer::new);
 } }
 </code> </code>
 We override the ''render'' method which gets called every frame(!), and in it we will do our rendering We override the ''render'' method which gets called every frame(!), and in it we will do our rendering
- - for starters, call ''GlStateManager.pushMatrix();'' which is mandatory when doing GL calls (we will doing those right after):+ - for starters, call ''matrices.push();'' which is mandatory when doing GL calls (we will doing those right after):
 <code java> <code java>
-    public void render(DemoBlockEntity blockEntity, double xdouble ydouble zfloat partialTicks, int destroyStage) { +    public void render(DemoBlockEntity blockEntity, float tickDeltaMatrixStack matricesVertexConsumerProvider vertexConsumersint light, int overlay) { 
-       GlStateManager.pushMatrix();+       matrices.push();
     }     }
 </code> </code>
-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: we translate it to x + 0.5, y + 1.25, and z + 0.5 which is above the center of our block. +There are two parts to the translation: we translate it to 0.5, 1.25, and 0.5 which is above the center of our block. 
 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, double xdouble ydouble zfloat partialTicks, int destroyStage) {+    public void render(DemoBlockEntity blockEntity, float tickDeltaMatrixStack matricesVertexConsumerProvider vertexConsumersint light, int overlay) {
         [...]         [...]
         // 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
-        GlStateManager.translated(x + 0.5, y + 1.25 + offset, z + 0.5);+        matrices.translate(0.5, 1.25 + offset, 0.5);
  
         // Rotate the item         // Rotate the item
-        GlStateManager.rotatef((blockEntity.getWorld().getTime() + partialTicks) * 4, 0, 1, 0);+        matrices.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion((blockEntity.getWorld().getTime() + tickDelta) * 4));
     }     }
 </code> </code>
Line 88: Line 94:
 We also pass ''ModelTransformation.Type.GROUND'' to ''renderItem'' because we want a similiar effect to  We also pass ''ModelTransformation.Type.GROUND'' to ''renderItem'' because we want a similiar effect to 
 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 ''GlStateManager.popMatrix();'' after these GL calls:+We also need to call ''matrices.pop();'' after these GL calls:
 <code java> <code java>
-    public void render(DemoBlockEntity blockEntity, double xdouble ydouble zfloat partialTicks, int destroyStage) {+    public void render(DemoBlockEntity blockEntity, float tickDeltaMatrixStack matricesVertexConsumerProvider vertexConsumersint light, int overlay) {
         [...]         [...]
-        MinecraftClient.getInstance().getItemRenderer().renderItem(stack, ModelTransformation.Type.GROUND);+        MinecraftClient.getInstance().getItemRenderer().renderItem(stack, ModelTransformation.Mode.GROUND, light, overlay, matrices, vertexConsumers);
  
         // Mandatory call after GL calls         // Mandatory call after GL calls
-        GlStateManager.popMatrix();+        matrices.pop();
     }     }
 </code> </code>
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 ''World#getLightmapIndex()'' on the position above our tile entity,  +To get the light, we call ''WorldRenderer#getLightmapCoordinates()'' on the position above our block entity,  
-and to use the light we call ''GLX.glMultiTexCoord2f'':+and to use the light we use it in ''renderItem()'':
 <code java> <code java>
     @Override     @Override
-    public void render(DemoBlockEntity blockEntity, double xdouble ydouble zfloat partialTicks, int destroyStage) {+    public void render(DemoBlockEntity blockEntity, float tickDeltaMatrixStack matricesVertexConsumerProvider vertexConsumersint light, int overlay) {
         [...]         [...]
                  
-        // Put this right above "MinecraftClient.getInstance().getItemRenderer().renderItem(stack, ModelTransformation.Type.GROUND);" +        int lightAbove = WorldRenderer.getLightmapCoordinates(blockEntity.getWorld()blockEntity.getPos().up()); 
-        int light = blockEntity.getWorld().getLightmapIndex(blockEntity.getPos().up(), 0); +        MinecraftClient.getInstance().getItemRenderer().renderItem(stackModelTransformation.Mode.GROUND, lightAbove, OverlayTexture.DEFAULT_UV, matrices, vertexConsumers);
-        GLX.glMultiTexCoord2f(GLX.GL_TEXTURE1, (float) (light & 0xFFFF)(float) ((light >> 16) & 0xFFFF));+
                  
         [...]         [...]
tutorial/blockentityrenderers.txt · Last modified: 2023/02/09 13:14 by mschae23