Search in sources :

Example 6 with Vector3

use of com.bergerkiller.bukkit.common.math.Vector3 in project BKCommonLib by bergerhealer.

the class MapResourcePack method getItemTexture.

/**
 * Renders the item gui slot texture of an item
 *
 * @param item to render
 * @param width of the produced icon image
 * @param height of the produced icon image
 * @return rendered item slot image
 */
public MapTexture getItemTexture(ItemStack item, int width, int height) {
    Model model = this.getItemModel(item);
    if (model == null || model.placeholder) {
        return createPlaceholderTexture(width, height);
    }
    MapTexture texture = MapTexture.createEmpty(width, height);
    Matrix4x4 transform = new Matrix4x4();
    if (width != 16 || height != 16) {
        transform.scale((double) width / 16.0, 1.0, (double) height / 16.0);
    }
    Model.Display display = model.display.get("gui");
    if (display != null) {
        display.apply(transform);
        texture.setLightOptions(0.0f, 1.0f, new Vector3(-1, 1, -1));
    } else {
    // System.out.println("GUI DISPLAY ELEMENT NOT FOUND");
    }
    texture.drawModel(model, transform);
    return texture;
}
Also used : Model(com.bergerkiller.bukkit.common.map.util.Model) GeneratedModel(com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel) Vector3(com.bergerkiller.bukkit.common.math.Vector3) Matrix4x4(com.bergerkiller.bukkit.common.math.Matrix4x4)

Example 7 with Vector3

use of com.bergerkiller.bukkit.common.math.Vector3 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 8 with Vector3

use of com.bergerkiller.bukkit.common.math.Vector3 in project BKCommonLib by bergerhealer.

the class MathUtilTest method testMatrixRotation.

@Test
public void testMatrixRotation() {
    Matrix4x4 transform = new Matrix4x4();
    transform.translateRotate(2.0, 3.0, 4.0, -12.0f, 34.0f);
    Vector3 vec = new Vector3();
    transform.transformPoint(vec);
    assertEquals(2.0, vec.x, 0.00001);
    assertEquals(3.0, vec.y, 0.00001);
    assertEquals(4.0, vec.z, 0.00001);
    Vector yawPitchRoll = transform.getYawPitchRoll();
    assertEquals(-12.0, yawPitchRoll.getX(), 0.001);
    assertEquals(34.0, yawPitchRoll.getY(), 0.001);
    assertEquals(0.0, yawPitchRoll.getZ(), 0.001);
    transform.rotateZ(30.0);
    yawPitchRoll = transform.getYawPitchRoll();
    assertEquals(-12.0, yawPitchRoll.getX(), 0.001);
    assertEquals(34.0, yawPitchRoll.getY(), 0.001);
    assertEquals(30.0, yawPitchRoll.getZ(), 0.001);
    transform.scale(1.5, 2.5, 3.5);
    yawPitchRoll = transform.getYawPitchRoll();
    assertEquals(-16.729, yawPitchRoll.getX(), 0.001);
    assertEquals(43.496, yawPitchRoll.getY(), 0.001);
    assertEquals(30.787, yawPitchRoll.getZ(), 0.001);
    // Try a bunch of random yaw/pitch/roll values and see that they all work
    Random rand = new Random();
    for (int i = 0; i < 1000; i++) {
        Vector result;
        Vector rotation = new Vector(180.0 * rand.nextDouble() - 90.0, 180.0 * rand.nextDouble() - 90.0, 180.0 * rand.nextDouble() - 90.0);
        transform = new Matrix4x4();
        transform.rotateYawPitchRoll(rotation);
        result = transform.getYawPitchRoll();
        assertEquals(rotation.getX(), result.getX(), 0.001);
        assertEquals(rotation.getY(), result.getY(), 0.001);
        assertEquals(rotation.getZ(), result.getZ(), 0.001);
    }
}
Also used : Random(java.util.Random) Vector3(com.bergerkiller.bukkit.common.math.Vector3) Vector(org.bukkit.util.Vector) Matrix4x4(com.bergerkiller.bukkit.common.math.Matrix4x4) Test(org.junit.Test)

Aggregations

Vector3 (com.bergerkiller.bukkit.common.math.Vector3)8 Matrix4x4 (com.bergerkiller.bukkit.common.math.Matrix4x4)6 MapTexture (com.bergerkiller.bukkit.common.map.MapTexture)3 Model (com.bergerkiller.bukkit.common.map.util.Model)2 GeneratedModel (com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel)1 Face (com.bergerkiller.bukkit.common.map.util.Model.Element.Face)1 BlockRenderOptions (com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions)1 Point (java.awt.Point)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1 BlockFace (org.bukkit.block.BlockFace)1 ItemStack (org.bukkit.inventory.ItemStack)1 Vector (org.bukkit.util.Vector)1 Test (org.junit.Test)1