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 01:45] – language fixes draylartutorial:biomecoloring [2019/10/25 17:41] – rework page draylar
Line 1: Line 1:
-====== Block Biome Coloring ====== +====== Color Providers ====== 
-In this tutorial, we will look at adding biome-dependent colors to new blocks. Remember to keep visual-related logic client-side(//onInitializeClient//or it will crash on a server. To register a custom block coloring, use //ColorProviderRegistry.BLOCK.register//, and for itemsuse //ColorProviderRegistry.ITEM.register//. In this tutorialthe grass biome color will be the one used.+Ever wonder how grass and leaves change hues depending on the biome, or how leather armor can have seemingly infinite color patterns? Meet color providerswhich allow you to hue and tint block & item model textures based on properties such as locationNBTor block states.
  
-   public class ExampleModClient implements ClientModInitializer { +=== Existing Examples === 
-      @Override +First, what existing vanilla content uses color providers? A few examples include: 
-      public void onInitializeClient() { +  * grass 
-         ColorProviderRegistry.BLOCK.register((block, pos, world, layer) -> { +  * leaves 
-         BlockColorMapper provider = ColorProviderRegistry.BLOCK.get(Blocks.GRASS); +  * leather armor dying 
-         return provider == null ? -1 : provider.getColor(blockposworld, layer); +  * redstone wire 
-         }, block); +  * plants such as melonssugarcaneand lilypads 
-      } +  * tipped arrows
-   }+
  
-So, what's happening here? The register method wants a 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, but we'll once again use grass blocks for the example.+The color provider is powerful, but Mojang has opted to stick with individual textures for colored blocks such as concrete, wool, and glass. The primary use case at this point is for biome shaded blocks and small tweaks to existing texturessuch as the colored end of a tipped arrow.
  
-   public class ExampleModClient implements ClientModInitializer { +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. 
-      @Override + 
-      public void onInitializeClient() { +Remember that the color provider is a client-side mechanic. Make sure to put any code related to it inside a client initializer. 
-         ColorProviderRegistry.ITEM.register((item, layer) -> + 
-         // These values are represented as temperature and humidity, and used as coordinates for the color map +==== Registering a Block Color Provider ==== 
-         double temperature = 0.5D; // a double value between 0 and 1 +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.  
-         double humidity 1.0D; // a double value between and 1 +<code java [enable_line_numbers="true"]> 
-         return GrassColorHandler.getColor(temperaturehumidity); +ColorProviderRegistry.BLOCK.register(new BlockColorProvider() { 
-         }, block.getItem());+        @Override 
 + public int getColor(BlockState stateExtendedBlockView world, BlockPos pos, int layer) { 
 + 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!