Thursday 25 December 2014

Block Rendering [1.8]

Block rendering has changed significantly from earlier versions.  This has made it significantly more complicated to understand, but on the flip side has introduced a great deal more flexibility for customising block appearances without using code.

There are a few key points in understanding how block rendering works:
  1. When minecraft is rendering a landscape, it first needs to ‘compile’ the block information (location, appearance, etc) into a list of drawing instructions that the graphics engine can execute.  Typically this means iterating through each block that the player might see and converting each part of the block into a series of textured rectangles.  A simple cube block like stone has six squares, one for each face.  More complicated blocks such as a torch or a piston are made up of a larger number of rectangular faces, perhaps at different angles.  After the render compilation phase is complete, minecraft ends up with a list of rendering instructions (a list of vertices with colour, texture coordinate, and lighting information) that can be executed by the graphics engine to draw the blocks in the landscape.
  2. Most of the blocks in a landscape are static, i.e. they don’t change their shape or position over time.  This allows minecraft to pre-compile a list of rendering instructions for drawing the blocks and then get the graphics engine to execute the list repeatedly, once per frame.  This is much faster than compiling the rendering instructions from scratch every frame.  In general, the drawing instructions only need to be recompiled when blocks in the landscape change.
  3. The blocks in a landscape are rendered in four different ways, depending on their desired appearance (see Picture below).  These are
  • SOLID – fully opaque (i.e. all texels render as opaque, ignoring alpha value).
  • CUTOUT_MIPPED – (alpha testing) - these block textures have holes in them (each texel is either fully opaque or fully transparent) and mip mapping is turned on (mip mapping is a way to improve the appearance of textures – see google for more information).
  • CUTOUT – (alpha testing) – as per CUTOUT_MIPPED except that mip mapping is turned off.
  • TRANSLUCENT – (alpha blending) – these blocks are partially transparent so that you can see other blocks through them – for example ice.  If the fancy graphics option is turned off, these become opaque.  TRANSLUCENT blocks are relatively expensive to render because they have to be pre-sorted in order of their distance from the player, otherwise they won’t render correctly.  TRANSLUCENT blocks can have holes in the texture same as for CUTOUT blocks, i.e. alpha testing is still turned on.

There are four different "layers" of Block Rendering.  The difference between CUTOUT and CUTOUT_MIPPED is too subtle to show here.

A landscape of blocks is rendered in layers – all of the SOLID blocks are drawn first, then the CUTOUT_MIPPED, then the CUTOUT, then the TRANSLUCENT.  A given block will only render in one of the four layers, specified by Block.getBlockLayer().
Each block also has a render type, specified by Block.getRenderType():
  • -1 = doesn’t render at all, eg BlockAir
  • 1 = renders as a flowing liquid (animated texture) using BlockFluidRenderer.renderFluid – eg lava and water.
  • 2 = doesn’t render anything in the block layers, but has an associated TileEntitySpecialRenderer which does draw something, eg BlockChest.
  • 3 = renders using an IBakedModel, BlockModelRenderer.renderModel.  This is described in more detail below.
There are two classes which implement IBakedModel; SimpleBakedModel and WeightedBakedModel.  They are both essentially identical except that WeightedBakedModel chooses a random model from a weighted list of alternative models, using the block position as a seed for the random number generator.  This allows for (eg) beds to have a different appearance depending on where they are placed.

The BakedModel format (used by Minecraft for nearly all its blocks) is described here.
There are some special considerations for rendering transparent blocks as described here.
Animation of blocks is described in more detail here #TBD#.
Lighting of blocks is described here.

Miscellaneous notes

Some few Blocks such as Flowers use the method Block.getOffsetType() to randomly move the rendering position of the block by a small amount (up to +/- 0.25 blocks).

1 comment:

  1. i want to change a solid block (stripped wood) to a cutout one (making it see through (like a carving)) a have made the texture, transparency and all, but were do i edit the block code so its no longer 'solid'?

    ReplyDelete