Advertisement
Lupins

Texture Program by Notch

Oct 9th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.56 KB | None | 0 0
  1. //Código do Notch
  2.  
  3. package net.minecraft.client.model.geom;
  4.  
  5. import java.awt.*;
  6. import java.awt.image.BufferedImage;
  7. import java.io.*;
  8. import java.util.*;
  9. import java.util.List;
  10.  
  11. import javax.imageio.ImageIO;
  12.  
  13. import net.minecraft.client.model.dragon.DragonModel;
  14. import util.Mth;
  15.  
  16. public class ImageGenerator {
  17.     private static final int WIDTH = 256;
  18.     private static final int HEIGHT = 256;
  19.  
  20.     private class Region {
  21.         public int x1, y1, x2, y2;
  22.  
  23.         public Region(int x1, int y1, int x2, int y2) {
  24.             this.x1 = x1;
  25.             this.y1 = y1;
  26.             this.x2 = x2;
  27.             this.y2 = y2;
  28.         }
  29.  
  30.         public void move(int xo, int yo) {
  31.             x1 += xo;
  32.             y1 += yo;
  33.             x2 += xo;
  34.             y2 += yo;
  35.         }
  36.  
  37.         public boolean overlaps(Region r2, int xo, int yo) {
  38.             if (r2.x2 + xo <= x1) return false;
  39.             if (r2.y2 + yo <= y1) return false;
  40.             if (r2.x1 + xo >= x2) return false;
  41.             if (r2.y1 + yo >= y2) return false;
  42.             return true;
  43.         }
  44.  
  45.         public void render(Graphics g) {
  46.             g.fillRect(x1, y1, x2 - x1, y2 - y1);
  47.         }
  48.     }
  49.  
  50.     private class UnfoldedCube {
  51.         public int x, y;
  52.         public Region[] cubeRegions = new Region[6];
  53.         public String id;
  54.         public int width, height;
  55.  
  56.         public UnfoldedCube(Cube cube) {
  57.             int xs = Mth.ceil(cube.x1 - cube.x0);
  58.             int ys = Mth.ceil(cube.y1 - cube.y0);
  59.             int zs = Mth.ceil(cube.z1 - cube.z0);
  60.  
  61.             width = xs * 2 + zs * 2;
  62.             height = zs + ys;
  63.  
  64.             id = cube.id;
  65.  
  66.             cubeRegions[0] = new Region(0, zs, zs, zs + ys);
  67.             cubeRegions[1] = new Region(zs, zs, zs + xs, zs + ys);
  68.             cubeRegions[2] = new Region(zs + xs, zs, zs + xs + zs, zs + ys);
  69.             cubeRegions[3] = new Region(zs + xs + zs, zs, zs + xs + zs + xs, zs + ys);
  70.             cubeRegions[4] = new Region(zs, 0, zs + xs, zs);
  71.             cubeRegions[5] = new Region(zs + xs, 0, zs + xs * 2, zs);
  72.         }
  73.  
  74.         public boolean fits(int x, int y) {
  75.             for (int i = 0; i < 6; i++) {
  76.                 if (!isFree(cubeRegions[i], x, y)) return false;
  77.             }
  78.             return true;
  79.         }
  80.  
  81.         public void place(int x, int y) {
  82.             this.x = x;
  83.             this.y = y;
  84.             for (int i = 0; i < 6; i++) {
  85.                 cubeRegions[i].move(x, y);
  86.                 occupiedRegions.add(cubeRegions[i]);
  87.             }
  88.         }
  89.  
  90.         public void render(Graphics g) {
  91.             float hue = (float) Math.random();
  92.             g.setColor(new Color(Color.HSBtoRGB(hue, 1, 0.7f)));
  93.             cubeRegions[5].render(g);
  94.             g.setColor(new Color(Color.HSBtoRGB(hue, 1, 1.0f)));
  95.             cubeRegions[4].render(g);
  96.             g.setColor(new Color(Color.HSBtoRGB(hue, 1, 0.9f)));
  97.             cubeRegions[1].render(g);
  98.             cubeRegions[3].render(g);
  99.             g.setColor(new Color(Color.HSBtoRGB(hue, 1, 0.8f)));
  100.             cubeRegions[0].render(g);
  101.             cubeRegions[2].render(g);
  102.         }
  103.     }
  104.  
  105.     private List<Region> occupiedRegions = new ArrayList<Region>();
  106.  
  107.     private boolean isFree(Region region, int xo, int yo) {
  108.         if (region.x1 + xo < 0) return false;
  109.         if (region.y1 + yo < 0) return false;
  110.         if (region.x2 + xo > WIDTH) return false;
  111.         if (region.y2 + yo > HEIGHT) return false;
  112.  
  113.         for (int i = 0; i < occupiedRegions.size(); i++) {
  114.             if (occupiedRegions.get(i).overlaps(region, xo, yo)) return false;
  115.         }
  116.  
  117.         return true;
  118.     }
  119.  
  120.     public void generateImage(List<ModelPart> parts) {
  121.  
  122.         List<Cube> cubes = new ArrayList<Cube>();
  123.         for (ModelPart mp : parts) {
  124.             getCubes(mp, cubes);
  125.         }
  126.  
  127.         Collections.sort(cubes, new Comparator<Cube>() {
  128.             @Override
  129.             public int compare(Cube c0, Cube c1) {
  130.                 int xs = Mth.ceil(c0.x1 - c0.x0);
  131.                 int ys = Mth.ceil(c0.y1 - c0.y0);
  132.                 int zs = Mth.ceil(c0.z1 - c0.z0);
  133.  
  134.                 int ww0 = xs * 2 + zs * 2;
  135.                 int hh0 = zs + ys;
  136.  
  137.                 xs = Mth.ceil(c1.x1 - c1.x0);
  138.                 ys = Mth.ceil(c1.y1 - c1.y0);
  139.                 zs = Mth.ceil(c1.z1 - c1.z0);
  140.  
  141.                 int ww1 = xs * 2 + zs * 2;
  142.                 int hh1 = zs + ys;
  143.  
  144.                 if (ww0 * hh0 < ww1 * hh1) {
  145.                     return 1;
  146.                 } else if (ww0 * hh0 > ww1 * hh1) {
  147.                     return -1;
  148.                 }
  149.  
  150.                 return 0;
  151.             }
  152.         });
  153.  
  154.         List<UnfoldedCube> unfolded = new ArrayList<UnfoldedCube>();
  155.         for (Cube cube : cubes) {
  156.             UnfoldedCube uc = new UnfoldedCube(cube);
  157.  
  158.             boolean placed = false;
  159.             positionLoop: for (int y = 0; y < HEIGHT - uc.height; y++) {
  160.                 for (int x = 0; x < WIDTH - uc.width; x++) {
  161.                     if (uc.fits(x, y)) {
  162.                         uc.place(x, y);
  163.                         placed = true;
  164.                         break positionLoop;
  165.                     }
  166.                 }
  167.             }
  168.             if (!placed) {
  169.                 System.out.println("Failed to place " + uc.id);
  170.             } else {
  171.                 unfolded.add(uc);
  172.             }
  173.         }
  174.  
  175.         BufferedImage img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
  176.         Graphics g = img.getGraphics();
  177.         System.out.println("----------------");
  178.         for (UnfoldedCube uc : unfolded) {
  179.             System.out.println("setMapTex(\"" + uc.id + "\", " + uc.x + ", " + uc.y + ");");
  180.             uc.render(g);
  181.         }
  182.         try {
  183.             ImageIO.write(img, "png", new File("output.png"));
  184.         } catch (IOException e) {
  185.             e.printStackTrace();
  186.         }
  187.     }
  188.  
  189.     public void getCubes(ModelPart mp, List<Cube> cubeList) {
  190.         addLoop: for (Cube cube : mp.cubes) {
  191.             if (cube.id != null) {
  192.                 for (Cube otherCube : cubeList) {
  193.                     if (otherCube.id != null) {
  194.                         if (otherCube.id.equals(cube.id)) {
  195.                             System.out.println("Duplicate " + cube.id);
  196.                             continue addLoop;
  197.                         }
  198.                     }
  199.                 }
  200.             }
  201.             cubeList.add(cube);
  202.         }
  203.     }
  204.  
  205.     public static void main(String[] args) {
  206.         new ImageGenerator().generateImage(new DragonModel(0).cubes);
  207.     }
  208. }
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement