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/05/26 16:21] – Code highlighting jamieswhiteshirttutorial: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
  
 +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 textures, such as the colored end of a tipped arrow.
 +
 +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.
 +
 +Remember that the color provider is a client-side mechanic. Make sure to put any code related to it inside a client initializer.
 +
 +==== Registering a Block Color Provider ====
 +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. 
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-public class ExampleModClient implements ClientModInitializer { +ColorProviderRegistry.BLOCK.register(new BlockColorProvider() { 
-    @Override +        @Override 
-    public void onInitializeClient() { + public int getColor(BlockState stateExtendedBlockView world, BlockPos pos, int layer) 
-        ColorProviderRegistry.BLOCK.register((block, pos, world, layer-> + return 0x3495eb
-            BlockColorMapper provider = ColorProviderRegistry.BLOCK.get(Blocks.GRASS); +
-            return provider == null ? -1 : provider.getColor(block, pos, world, layer); +}, MY_BLOCK);
-        }, YOUR_BLOCK_INSTANCE); +
-    } +
-}+
 </code> </code>
  
-Sowhat's happening here? The register method wants a color returned, and in this casethat color is taken from the grass blockColoring an item is very similar. Like blocks, the returned color could be anyand also remember to replace the final argument with an instance of your item.+All we do here is say, "Hi, ''MY_BLOCK'' should be colored 0x3495eb," which is blue color. You have BlockState, World, and BlockPos contextwhich is how you can change colors based on biome or positionThe final int is the layer; each one asks for a color individuallybut in this case, we're always returning blue.
  
-<code java [enable_line_numbers="true"]> +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: 
-public class ExampleModClient implements ClientModInitializer +<code json [enable_line_numbers="true"]> 
-    @Override +
-    public void onInitializeClient() +  "parent": "block/block", 
-        ColorProviderRegistry.ITEM.register((itemlayer) -> +  "textures": 
-            // These values are represented as temperature and humidityand used as coordinates for the color map +    "all": "block/white_concrete", 
-            double temperature = 0.5D; // a double value between and 1 +    "particle": "#all" 
-            double humidity = 1.0D; // a double value between and 1 +  }, 
-            return GrassColorHandler.getColor(temperaturehumidity); +  "elements": [ 
-        }, YOUR_ITEM_INSTANCE);+    {   "from": [ 0, 0, 0 ], 
 +      "to": [ 1616, 16 ], 
 +      "faces": 
 +        "down":  { "uv": [ 0, 0, 16, 16 ], "texture": "#all", "tintindex": 0, "cullface": "down" }
 +        "up":    { "uv":00, 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> </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!