Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.minecraft.client.model.geom;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.*;
- import java.util.*;
- import java.util.List;
- import javax.imageio.ImageIO;
- import net.minecraft.client.model.dragon.DragonModel;
- import util.Mth;
- public class ImageGenerator {
- private static final int WIDTH = 256;
- private static final int HEIGHT = 256;
- private class Region {
- public int x1, y1, x2, y2;
- public Region(int x1, int y1, int x2, int y2) {
- this.x1 = x1;
- this.y1 = y1;
- this.x2 = x2;
- this.y2 = y2;
- }
- public void move(int xo, int yo) {
- x1 += xo;
- y1 += yo;
- x2 += xo;
- y2 += yo;
- }
- public boolean overlaps(Region r2, int xo, int yo) {
- if (r2.x2 + xo <= x1) return false;
- if (r2.y2 + yo <= y1) return false;
- if (r2.x1 + xo >= x2) return false;
- if (r2.y1 + yo >= y2) return false;
- return true;
- }
- public void render(Graphics g) {
- g.fillRect(x1, y1, x2 - x1, y2 - y1);
- }
- }
- private class UnfoldedCube {
- public int x, y;
- public Region[] cubeRegions = new Region[6];
- public String id;
- public int width, height;
- public UnfoldedCube(Cube cube) {
- int xs = Mth.ceil(cube.x1 - cube.x0);
- int ys = Mth.ceil(cube.y1 - cube.y0);
- int zs = Mth.ceil(cube.z1 - cube.z0);
- width = xs * 2 + zs * 2;
- height = zs + ys;
- id = cube.id;
- cubeRegions[0] = new Region(0, zs, zs, zs + ys);
- cubeRegions[1] = new Region(zs, zs, zs + xs, zs + ys);
- cubeRegions[2] = new Region(zs + xs, zs, zs + xs + zs, zs + ys);
- cubeRegions[3] = new Region(zs + xs + zs, zs, zs + xs + zs + xs, zs + ys);
- cubeRegions[4] = new Region(zs, 0, zs + xs, zs);
- cubeRegions[5] = new Region(zs + xs, 0, zs + xs * 2, zs);
- }
- public boolean fits(int x, int y) {
- for (int i = 0; i < 6; i++) {
- if (!isFree(cubeRegions[i], x, y)) return false;
- }
- return true;
- }
- public void place(int x, int y) {
- this.x = x;
- this.y = y;
- for (int i = 0; i < 6; i++) {
- cubeRegions[i].move(x, y);
- occupiedRegions.add(cubeRegions[i]);
- }
- }
- public void render(Graphics g) {
- float hue = (float) Math.random();
- g.setColor(new Color(Color.HSBtoRGB(hue, 1, 0.7f)));
- cubeRegions[5].render(g);
- g.setColor(new Color(Color.HSBtoRGB(hue, 1, 1.0f)));
- cubeRegions[4].render(g);
- g.setColor(new Color(Color.HSBtoRGB(hue, 1, 0.9f)));
- cubeRegions[1].render(g);
- cubeRegions[3].render(g);
- g.setColor(new Color(Color.HSBtoRGB(hue, 1, 0.8f)));
- cubeRegions[0].render(g);
- cubeRegions[2].render(g);
- }
- }
- private List<Region> occupiedRegions = new ArrayList<Region>();
- private boolean isFree(Region region, int xo, int yo) {
- if (region.x1 + xo < 0) return false;
- if (region.y1 + yo < 0) return false;
- if (region.x2 + xo > WIDTH) return false;
- if (region.y2 + yo > HEIGHT) return false;
- for (int i = 0; i < occupiedRegions.size(); i++) {
- if (occupiedRegions.get(i).overlaps(region, xo, yo)) return false;
- }
- return true;
- }
- public void generateImage(List<ModelPart> parts) {
- List<Cube> cubes = new ArrayList<Cube>();
- for (ModelPart mp : parts) {
- getCubes(mp, cubes);
- }
- Collections.sort(cubes, new Comparator<Cube>() {
- @Override
- public int compare(Cube c0, Cube c1) {
- int xs = Mth.ceil(c0.x1 - c0.x0);
- int ys = Mth.ceil(c0.y1 - c0.y0);
- int zs = Mth.ceil(c0.z1 - c0.z0);
- int ww0 = xs * 2 + zs * 2;
- int hh0 = zs + ys;
- xs = Mth.ceil(c1.x1 - c1.x0);
- ys = Mth.ceil(c1.y1 - c1.y0);
- zs = Mth.ceil(c1.z1 - c1.z0);
- int ww1 = xs * 2 + zs * 2;
- int hh1 = zs + ys;
- if (ww0 * hh0 < ww1 * hh1) {
- return 1;
- } else if (ww0 * hh0 > ww1 * hh1) {
- return -1;
- }
- return 0;
- }
- });
- List<UnfoldedCube> unfolded = new ArrayList<UnfoldedCube>();
- for (Cube cube : cubes) {
- UnfoldedCube uc = new UnfoldedCube(cube);
- boolean placed = false;
- positionLoop: for (int y = 0; y < HEIGHT - uc.height; y++) {
- for (int x = 0; x < WIDTH - uc.width; x++) {
- if (uc.fits(x, y)) {
- uc.place(x, y);
- placed = true;
- break positionLoop;
- }
- }
- }
- if (!placed) {
- System.out.println("Failed to place " + uc.id);
- } else {
- unfolded.add(uc);
- }
- }
- BufferedImage img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
- Graphics g = img.getGraphics();
- System.out.println("----------------");
- for (UnfoldedCube uc : unfolded) {
- System.out.println("setMapTex(\"" + uc.id + "\", " + uc.x + ", " + uc.y + ");");
- uc.render(g);
- }
- try {
- ImageIO.write(img, "png", new File("output.png"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void getCubes(ModelPart mp, List<Cube> cubeList) {
- addLoop: for (Cube cube : mp.cubes) {
- if (cube.id != null) {
- for (Cube otherCube : cubeList) {
- if (otherCube.id != null) {
- if (otherCube.id.equals(cube.id)) {
- System.out.println("Duplicate " + cube.id);
- continue addLoop;
- }
- }
- }
- }
- cubeList.add(cube);
- }
- }
- public static void main(String[] args) {
- new ImageGenerator().generateImage(new DragonModel(0).cubes);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement