Advertisement
Guest User

ImageGenerator for MCP 5.0

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