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/02/19 14:26] mcrafterzztutorial:biomecoloring [2019/10/25 17:41] – rework page draylar
Line 1: Line 1:
-====== Block Biome Coloring ====== +====== Color Providers ====== 
-In this tutorial we will show how to make blocks get affected by the biome color similar to leaves and grass. It's very important that it's done on client side in //onInitializeClient// as it otherwise will cause a crash. To register a custom block coloring use //ColorProviderRegistry.BLOCK.register// and for items //ColorProviderRegistry.ITEM.register//. The color could be any but in this tutorial the 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 providers, which allow you to hue and tint block & item model textures based on properties such as location, NBT, or 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? Well, the register method wants a color returned and in this case that color is taken from the grass blockusing the method's parameters blockposworld and layerColoring an item is very similar. Like blocks the returned color could be any //Color.black// for example but we will show how to get the default grass color.+The color provider is powerfulbut Mojang has opted to stick with individual textures for colored blocks such as concretewool, and 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.
  
-   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) -> + 
-         double temperature = 0.5D; // between 0 and 1 +==== Registering a Block Color Provider ==== 
-         double humidity 1.0D; // between 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.  
-         return GrassColorHandler.getColor(temperaturehumidity); +<code java [enable_line_numbers="true"]> 
-         }, block.getItem());+ColorProviderRegistry.BLOCK.register(new BlockColorProvider() { 
 +        @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 colorYou have BlockState, World, and BlockPos context, which is how you can change colors based on biome or position. The final int is the layereach 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 a 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!