Library Color to Material - Create images in using blocks

Discussion in 'Resources' started by Zombie_Striker, Mar 30, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Zombie_Striker

    What is this?
    This is a library I built for one of my upcoming plugins that allows you to turn a raw Color code into the closest matching material. Since there does not seem to be a library out there that does the same thing, I figured I would post it here for someone else to use.

    If you want a finished product for this library (if you just want to load images on your server without coding), you can check out the plugin I made using this library.
    http://dev.bukkit.org/bukkit-plugins/pixelprinter/

    Why should I use this?
    Since this is meant to just turn Color Values into Materials, you will only need this if you want to load Images/ gifs on your server or if you wanted to load pixel art.

    If you want to turn Materials into ChatColors or DyeColors, go to this library.


    Code:

    Since this page would go over the char limit, you can find my code at the following repo:
    https://github.com/ZombieStriker/Color-to-Material-lib/blob/master/RGBBlockColor.java

    Usage:

    Starting off small, this is a way to demonstrate how to use getClosestBlockValue if you are only looking for the corresponding color for one pixel. This would be used if you want a 1-to-1 scale version of an image.
    Code:
    // Lets try to set the material of Block "b" equal to the Red-est material. Bright red is R=255, G=0, B=0
    b.setType(   RGBBlockColor.getClosestBlockValue(new Color(255,0,0)).getMaterial());
    
    
    Automatically, the above will assume you are looking for the side of the block. If you want to get the texture for the top of the block, add "true" after supplying the color.


    Below is an example of how to use the four color ClosestBlockValue. This is usefull if you are trying to match full images, where the scale is not 1-1. Also note that the code below is built to display a full image, since that is what you will be most likely using this util for. "Bi" represents the buffered image that we want to display, and "getBlockAt" is a method I created for getting the location plus the height and width. What this will do is take four pixels of the image and try to find a material whose texture is the closest.
    Code:
    for (int width = 0; width < (bi.getWidth()); width += 2) {
                   for (int height = (bi.getHeight() - 1); height >= 0; height -= 2) {
                     Location b = .......getBlockAt(height, width,
                         bi.getHeight());
                     if (b == null)
                       continue;            
                     Color[] color = new Color[4];
                     for (int i = 0; i < 4; i++) {
                       int y = (height + 1 < result.length) ? height
                           + (i % 2)
                           : height;
                       int x = (width + 1 < result[y].length) ? width
                           + (i % 2)
                           : width;
                       color[i] = new Color(result[y][x].r,
                           result[y][x].g, result[y][x].b);
                     }
                     MaterialData m = RGBBlockColor
                         .getClosestBlockValue(
                             color,
                             (dir == Direction.FLAT_NORTHEAST
                                 || dir == Direction.FLAT_NORTHWEST
                                 || dir == Direction.FLAT_SOUTHEAST || dir == Direction.FLAT_SOUTHWEST));
                       b.setType(m.getM());
                       b.setData(m.getData());
                   }
                 }

    [edit] If you want to use this to create skins, you can use my Utility SkinCreator (found below):
    https://github.com/ZombieStriker/Color-to-Material-lib/blob/master/SkinCreator.java

    To use it, just use:
    Code:
    SkinCreator.createStatue(SkinCreator.getSkin(UUID#toString()), /*Where the statue should be created*/, Direction./*DIRECTION*/) {
    Images!
    [​IMG][​IMG][​IMG][​IMG][​IMG][​IMG]
    Old images (open)

    [​IMG][​IMG][​IMG][​IMG][​IMG][​IMG]
    Old images (open)


    [​IMG][​IMG][​IMG][​IMG][​IMG][​IMG]

    Updates:
    -2/11/2017: Added 1.12 blocks to github page. Will add 1.12 images when 1.12 is out. Referenced SkinCreator class as a resource.

    -1/2/2017: Fixed usage. Usage now includes code for building a full image. Code also has been updated so it will work on ALL versions of bukkit or spigot.
    -9/30/2016 : Added docs for each method. Added methods for saving an image of the color values represented by each block to a file.
    -9/29/2016 : Updated color finding formula. Your images should be more accurate using the new system.
    -7/28/2016: Added 1.7, 1.8, 1.9, and 1.10 support.
    -3/4/2/2016 : Revised the code. Released PixelPrinter. Added new images.
    -3/31/16 - Added images.
     
    Last edited: Feb 11, 2017
  2. Offline

    ChipDev

    This... LOOKS....... AMAZING!
     
  3. Offline

    ski23

    THIS IS TRULY REALLY HELPFUL!!! For one of my plugins I would like to create a 3d model of a player's skin in blocks and this will be very helpful in translating the colors of the pixels of the player's skin into correlating blocks. Furthermore, do you know any way that I can get a player's skin as some sort of file and then use some image processing to get the individual pixels to then convert them into blocks??
     
  4. Please include screenshots of a pixelart created by it :)
     
  5. Offline

    Zombie_Striker

    Last edited: Mar 31, 2016
  6. Offline

    Zombie_Striker

    @FisheyLP @ski23 @ChipDev @GamerzKing
    To everyone who may have used the code previously posted: I have just updated all of the data values. Now all the points are more accurate and include more blocks. If you have used my library, please use the new code.

    Also, I have uploaded new images with the updated library, along with my plugin that I made using this library.
     
    ChipDev likes this.
  7. Offline

    ChipDev

    WOAH. JUST. WOAH. How did you render the images from PNG?
     
  8. Offline

    Zombie_Striker

  9. Offline

    GamerzKing

    Very awesome! I love this class so much, and I love the images you added. Your work is amazing, and keep it up. Definitely going to be using this resource for some cool things. :)
     
  10. Offline

    Zombie_Striker

  11. @Zombie_Striker Jeez.... Very nice! How long did you spend making this? :D
     
  12. Offline

    Zombie_Striker

    @bwfcwalshy
    I think I spent two hours over this. I had to locate all the block textures, and go back-and-forth between Photoshop and Eclipse copying over the RGB values for each block.

    Time to add 1.10 blocks...
     
  13. Offline

    ArsenArsen

    Good luck. I know that was the hard part ;)
     
  14. Why don't you make a java program to do it? There are methods existing for archives.

    Just iterate through 1.10.jar\assets\minecraft\textures\blocks, maybe create an excluded list for blocks like bed, halfslab etc. then get the image with ImageIO.read(example.png"), loop through all pixels, getting the base color and directly saving it to a file.

    EDIT: Sounds so interesting and not hard that I actually try this now, I edit this post or post a new one when I'm done.
     
    Zombie_Striker likes this.
  15. Offline

    Zombie_Striker

    @FisheyLP
    If you can make it, share it. That will make my my life easier to not have to worry about updates.
     
  16. @Zombie_Striker
    Finally done :D

    Download: https://www.dropbox.com/s/yw3b9wt3o628qb4/Bukkit Texture BaseColor.jar?dl=0

    How to use it:
    1. Start cmd in the same directory, then run the command:
    Code:
    java -jar "Bukkit Texture BaseColor.jar"
    2. Enter the Minecraft version. For example 1.10.2
    3. Enter the craftbukkit jar location.

    How it works:
    1. Loading the selected minecraft version jar.
    2. Loading the craftbukkit jar you entered as a library during runtime using a ClassLoader. (It is needed because of Material.getMaterial(String name) to check if something exist (in 6. of this list)
    3. Looping through all files of the minecraft jar in the assets/minecraft/textures/blocks/ directory while saving all file names ending with .png to an ArrayList.
    4. Applying a blacklist to the ArrayList, filtering out blocks like beds, seeds, glass etc.
    5. Loading the BufferedImage for every directory.
    6. Generating the base color for every texture.
    7. Fixing all file names to match the bukkit material ones. Example: endstone > ENDER_STONE
    8. Formatting all gathered informations (material, rgb base color, data value) and writing it to a file.

    How to edit stuff:

    Just open this program with WinRAR, 7zip etc.
    In the directory com/FisheyLP/TextureBaseColor you can find 3 .txt files you can edit:
    • blacklist.txt: contains all blocked file names as regular expressions.
    • fixes.txt: contains all fixes for material names and data. (format: old value > new value:data)
    • format.txt: contains the format that will be saved. You can use %material, %r, %g, %b and %data as variables. I already configured it to match your code syntax :p

    Data about the program
    :
    • Took me a few hours.
    • Entirely in java, without external APIs (except loading craftbukkit).
    • 306 lines of code + 150 lines of the txt files = a total of 456 lines.
    • File size: 16kiB.
    • Duration for generating everything: ~1.5 seconds.

    Screenshots (open)


    [​IMG]
    [​IMG]
    You can replace
    Code:
    , 0);
    with
    Code:
    );
    if you don't want the data 0.


    Have fun :)

    EDIT:
    I think you understood wrong.. it is a program, that needs to have minecraft installed on the pc, not extending the functions of your library. But it's going to be helpful anyways for future updates ^^.
     
    Last edited: Jul 28, 2016
    I Al Istannen likes this.
  17. Offline

    Zombie_Striker

    Updated page: Posted link to repo with updated code.
     
  18. Offline

    timtower Administrator Administrator Moderator

    How did I miss this resource the first time.
    Is it also able to reverse the process?
     
  19. Offline

    Zombie_Striker

    Right now it cannot turn blocks into images, though I had not thought of that until now. Since it sounds like a neat idea, I may add that in the future.

    @timtower
    Updated page: The repo now has methods for saving blocks to an image files.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 30, 2016
    timtower likes this.
  20. All we need is a mp4 frame by frame interpreter and a powerful server then we're all set.

    EDIT: didn't read last post date, sorry for necro-posting
     
    Last edited: Jan 23, 2017
  21. Offline

    Zombie_Striker

    @TheEnderman
    It's not about a powerful server, but powerful clients. You clients are the ones that need to render the changes, so they all need a great CPU and GPU in order to render the frames (even for small gifs, I was not able to render a 40*100 image with a 750Nvida card at 7FPS without skipping frames.).

    Hopefully, with some restrictions to the amount of blocks changed, this could be fixed, but it will cause some of the image to not change properly.
     

  22. Server has to write blocks to world making lag, clients do have to be powerful as well.
     
  23. Offline

    ipodtouch0218

    Concrete and other 1.12 blocks are gonna make these images even more amazing!
     
  24. Yeah, it will help make it look real...

    Apart from terracotta. ugh..
     
    ipodtouch0218 likes this.
  25. Offline

    Zombie_Striker

    @ipodtouch0218 @TheEnderman
    IMHO, terracotta looks like it came straight from a poorly created mod. The textures are terrible. The only thing that is good about them is that they have multiple colors within the same block, so this lib (and PixelPrinter ) will produce better results.

    This update has added 48 new blocks, and brightened 16 others (wool blocks). For comparison, updates 1.8 through 1.11 only produced 29 new blocks. This will really create more accurate images.

    Updated page: Added 1.12 "support". I assumed the names would be the same as the ones in base MC. To anyone using this currently, make sure to come back after 1.12 spigot has been officially released to make sure all the values are correct.

    [Edit] Just saw this tweet. Since the coordination of each block is random, it seems I will just have to live with some inaccuracy when creating new images (either that, or I just remove those ugly blocks.)
     
    Last edited: Feb 11, 2017
  26. Offline

    ipodtouch0218

    @Zombie_Striker
    I mostly mean the new concrete blocks, the terracotta blocks do look bad. And also, they are only one texture. They the "facing" direction mechanic, which makes them able to have multiple textures.
     
  27. Offline

    Zombie_Striker

    @ski23
    Updated page: I have just added a link to my SkinCreator util. I will make a new thread just for that later on. Using that class, you can create skin statues of any player you want (as long as you know their uuid)
     
Thread Status:
Not open for further replies.

Share This Page