Search in sources :

Example 11 with MapTexture

use of com.bergerkiller.bukkit.common.map.MapTexture in project BKCommonLib by bergerhealer.

the class Model method build.

public void build(MapResourcePack resourcePack, RenderOptions options) {
    // Mostly for debug, but can be useful elsewhere perhaps?
    this.name = options.lookupModelName();
    // Build all textures, turning paths into absolute paths
    boolean hasChanges;
    do {
        hasChanges = false;
        for (Map.Entry<String, String> textureEntry : this.textures.entrySet()) {
            if (textureEntry.getValue().startsWith("#")) {
                String texture = this.textures.get(textureEntry.getValue().substring(1));
                if (texture != null) {
                    textureEntry.setValue(texture);
                    hasChanges = true;
                }
            }
        }
    } while (hasChanges);
    // This basically creates a small cube for every non-transparent pixel in the texture
    if (this.builtinType == BuiltinType.GENERATED) {
        this.elements.clear();
        MapTexture result = null;
        for (int i = 0; ; i++) {
            String layerKey = "layer" + i;
            String layerTexturePath = this.textures.get(layerKey);
            if (layerTexturePath == null) {
                break;
            }
            MapTexture texture = resourcePack.getTexture(layerTexturePath);
            // Item-specific layer render colors
            texture = applyTint(texture, options.get(layerKey + "tint"));
            if (result == null) {
                result = texture.clone();
            } else {
                result.draw(texture, 0, 0);
            }
        }
        if (result != null) {
            // We really cannot handle models like 600x600 - bad things really happen...
            if (result.getWidth() > 16 || result.getHeight() > 16) {
                MapTexture newTexture = MapTexture.createEmpty(16, 16);
                for (int x = 0; x < 16; x++) {
                    for (int y = 0; y < 16; y++) {
                        int px = (x * result.getWidth()) / 16;
                        int py = (y * result.getHeight()) / 16;
                        newTexture.writePixel(x, y, result.readPixel(px, py));
                    }
                }
                result = newTexture;
            }
            for (int y = 0; y < result.getHeight(); y++) {
                for (int x = 0; x < result.getWidth(); x++) {
                    byte color = result.readPixel(x, y);
                    if (color == MapColorPalette.COLOR_TRANSPARENT) {
                        continue;
                    }
                    Element element = new Element();
                    element.from = new Vector3(x, 0, y);
                    element.to = new Vector3(element.from.x + 1, element.from.y + 1, element.from.z + 1);
                    for (BlockFace bface : FaceUtil.BLOCK_SIDES) {
                        // If pixel on this face is not transparent, do not add one there
                        if (!FaceUtil.isVertical(bface)) {
                            int x2 = x - bface.getModX();
                            int y2 = y - bface.getModZ();
                            if (result.readPixel(x2, y2) == MapColorPalette.COLOR_TRANSPARENT) {
                                continue;
                            }
                        }
                        Face face = new Face();
                        MapTexture tex = MapTexture.createEmpty(1, 1);
                        tex.writePixel(0, 0, color);
                        face.texture = tex;
                        element.faces.put(bface, face);
                    }
                    this.elements.add(element);
                }
            }
        }
    }
    // Apply all textures to the model faces
    for (Element element : this.elements) {
        element.build(resourcePack, this.textures);
    }
}
Also used : MapTexture(com.bergerkiller.bukkit.common.map.MapTexture) BlockFace(org.bukkit.block.BlockFace) Vector3(com.bergerkiller.bukkit.common.math.Vector3) Face(com.bergerkiller.bukkit.common.map.util.Model.Element.Face) BlockFace(org.bukkit.block.BlockFace) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with MapTexture

use of com.bergerkiller.bukkit.common.map.MapTexture in project BKCommonLib by bergerhealer.

the class MapCanvasFunctionsTest method testPixelMove.

@Ignore
@Test
public void testPixelMove() {
    MapTexture map = MapTexture.createEmpty(200, 200);
    map.fillRectangle(0, 0, 50, 50, MapColorPalette.COLOR_RED);
    map.fillRectangle(20, 40, 100, 90, MapColorPalette.COLOR_BLUE);
    MapDebugWindow.showMap(map);
    map.movePixels(-10, -30);
    MapDebugWindow.showMapForever(map);
}
Also used : MapTexture(com.bergerkiller.bukkit.common.map.MapTexture) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with MapTexture

use of com.bergerkiller.bukkit.common.map.MapTexture in project BKCommonLib by bergerhealer.

the class MapResourcePackTest method testItemSlotTexture.

@Ignore
@Test
public void testItemSlotTexture() {
    Random rand = new Random();
    MapTexture map = MapTexture.createEmpty(128, 128);
    map.fill(MapColorPalette.getColor(128, 128, 128));
    // new MapResourcePack("https://www.dropbox.com/s/s77nz3zaclrdqog/TestPack.zip?dl=1");
    MapResourcePack pack = MapResourcePack.SERVER;
    pack.load();
    ItemStack item = ItemUtil.createItem(Material.DIAMOND_SWORD, 2, 1);
    ItemUtil.getMetaTag(item, true).putValue("Unbreakable", true);
    for (int x = 0; x < 128 - 16; x += 18) {
        for (int y = 0; y < 128 - 16; y += 18) {
            testDrawModel(map, pack, x, y, rand.nextInt(70));
        }
    }
    /*
        for (int x = 0; x < 128-16; x += 18) {
            for (int y = 0; y < 128-16; y += 18) {
                testDraw(map, pack, x, y, Material.values()[rand.nextInt(Material.values().length)]);
            }
        }
        */
    MapDebugWindow.showMapForeverAutoScale(map);
}
Also used : MapTexture(com.bergerkiller.bukkit.common.map.MapTexture) Random(java.util.Random) MapResourcePack(com.bergerkiller.bukkit.common.map.MapResourcePack) ItemStack(org.bukkit.inventory.ItemStack) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

MapTexture (com.bergerkiller.bukkit.common.map.MapTexture)13 Ignore (org.junit.Ignore)7 Test (org.junit.Test)7 MapResourcePack (com.bergerkiller.bukkit.common.map.MapResourcePack)4 Vector3 (com.bergerkiller.bukkit.common.math.Vector3)3 Model (com.bergerkiller.bukkit.common.map.util.Model)2 Face (com.bergerkiller.bukkit.common.map.util.Model.Element.Face)2 Matrix4x4 (com.bergerkiller.bukkit.common.math.Matrix4x4)2 BlockFace (org.bukkit.block.BlockFace)2 ItemStack (org.bukkit.inventory.ItemStack)2 MCSDFlat (com.bergerkiller.bukkit.common.map.MCSDFlat)1 MCSDGenCiede2000 (com.bergerkiller.bukkit.common.map.MCSDGenCiede2000)1 MCSDBubbleFormat (com.bergerkiller.bukkit.common.map.color.MCSDBubbleFormat)1 MapDebugWindow (com.bergerkiller.bukkit.common.map.util.MapDebugWindow)1 BlockRenderOptions (com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions)1 Dimension (java.awt.Dimension)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1