User Tools

Site Tools


tutorial:biomecoloring

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
Last revisionBoth sides next revision
tutorial:biomecoloring [2019/03/25 03:22] draylartutorial:biomecoloring [2019/10/25 17:41] – rework page draylar
Line 1: Line 1:
-====== Block Biome Coloring ====== +====== Color Providers ====== 
-In this tutorial, we'll look at adding biome-dependent colors to new blocks. To start, you'll need a block with a model that accounts for tintindex. To see an example of thisview the base leaves or grass_block model file+Ever wonder how grass and leaves change hues depending on the biome, or how leather armor can have seemingly infinite color patterns? Meet color providers, which allow you to hue and tint block & item model textures based on properties such as location, NBT, or block states.
  
-Remember to keep visual-related logic client-side(//onInitializeClient//) or it will crash on a server. To register a custom block coloringuse //ColorProviderRegistry.BLOCK.register//, and for items, use //ColorProviderRegistry.ITEM.register//. In this tutorial, the grass biome color will be the one used. Replace the final argument by passing in your block.+=== Existing Examples === 
 +Firstwhat existing vanilla content uses color providers? A few examples include: 
 +  * grass 
 +  * leaves 
 +  * leather armor dying 
 +  * redstone wire 
 +  * plants such as melonssugarcane, and lilypads 
 +  * tipped arrows
  
-   public class ExampleModClient implements ClientModInitializer { +The color provider is powerfulbut Mojang has opted to stick with individual textures for colored blocks such as concretewooland glassThe primary use case at this point is for biome shaded blocks and small tweaks to existing textures, such as the colored end of a tipped arrow.
-      @Override +
-      public void onInitializeClient() { +
-         ColorProviderRegistry.BLOCK.register((blockposworldlayer) -> { +
-         BlockColorMapper provider = ColorProviderRegistry.BLOCK.get(Blocks.GRASS); +
-         return provider == null ? -1 : provider.getColor(block, pos, world, layer); +
-         }, YOUR_BLOCK_INSTANCE); +
-      } +
-   }+
  
-So, what's happening here? The register method wants color returned, and in this case, that color is taken from the grass block. Coloring an item is very similar. Like blocks, the returned color could be any, and also remember to replace the final argument with an instance of your item.+The concept behind color providers is simple. You register a block or item to them, and when the block or item's model is rendered, the color provider applies a hue tweak to each layer of the texture. Both providers give you access to the layer of the model, which means you can hue each portion of a model separately, which is the case in leather armor & tipped arrows. This is useful for when you only want to change a few pixels, but not the entire texture.
  
-   public class ExampleModClient implements ClientModInitializer { +Remember that the color provider is a client-side mechanic. Make sure to put any code related to it inside a client initializer. 
-      @Override + 
-      public void onInitializeClient() { +==== Registering a Block Color Provider ==== 
-         ColorProviderRegistry.ITEM.register((item, layer) -> +To register a block to the block color provider, you'll need to use Fabric's ''ColorProviderRegistry''There is an instance of the ''BLOCK'' and ''ITEM'' provider inside this class, which you can call register on. The register method takes an instance of your color provider and a varargs of every block you want to color with the provider.  
-         // These values are represented as temperature and humidity, and used as coordinates for the color map +<code java [enable_line_numbers="true"]> 
-         double temperature = 0.5D; // a double value between 0 and 1 +ColorProviderRegistry.BLOCK.register(new BlockColorProvider() { 
-         double humidity 1.0D; // a double value between and 1 +        @Override 
-         return GrassColorHandler.getColor(temperaturehumidity); + public int getColor(BlockState stateExtendedBlockView world, BlockPos pos, int layer) { 
-         }, YOUR_ITEM_INSTANCE);+ return 0x3495eb; 
 +
 +}, MY_BLOCK); 
 +</code> 
 + 
 +All we do here is say, "Hi, ''MY_BLOCK'' should be colored 0x3495eb," which is a blue color. You have BlockState, World, and BlockPos context, which is how you can change colors based on biome or position. The final int is the layer; each one asks for a color individually, but in this case, we're always returning blue. 
 + 
 +The model is also important: the main note here is that you are //required// to define tintindex for each portion of the model you want to hue. To see an example of this, check out ''leaves.json'', which is the base model used for vanilla leaves. Here's the model used for our block: 
 +<code json [enable_line_numbers="true"]> 
 +
 +  "parent": "block/block", 
 +  "textures":
 +    "all": "block/white_concrete", 
 +    "particle": "#all" 
 +  }, 
 +  "elements":
 +    {   "from":0, 0, 0 ], 
 +      "to": [ 16, 16, 16 ]
 +      "faces":
 +        "down":  { "uv": [ 0, 0, 16, 16 ], "texture": "#all", "tintindex": 0, "cullface": "down" }, 
 +        "up":    { "uv": [ 0, 0, 16, 16 ], "texture": "#all", "tintindex": 0, "cullface": "up" }, 
 +        "north": { "uv": [ 0, 0, 16, 16 ], "texture": "#all", "tintindex": 0, "cullface": "north" }, 
 +        "south": { "uv": [ 0, 0, 16, 16 ], "texture": "#all", "tintindex": 0, "cullface": "south" }, 
 +        "west":  { "uv": [ 0, 0, 16, 16 ], "texture": "#all", "tintindex": 0, "cullface": "west" }, 
 +        "east":  { "uv": [ 0, 0, 16, 16 ], "texture": "#all", "tintindex": 0, "cullface": "east" }
       }       }
-   }+    } 
 +  ] 
 +
 +</code> 
 +In this instance, we're adding a single tintindex, which is what would appear in the `layer` parameter (layer 0). 
 + 
 +Here's the final result-- note that the original model used the ''white_concrete'' texture: 
 +{{https://i.imgur.com/fZLS10g.png}} 
 + 
  
-Finished!