Search in sources :

Example 1 with Rectanglef

use of org.terasology.joml.geom.Rectanglef in project Terasology by MovingBlocks.

the class AtlasFormat method process.

private void process(GridDefinition grid, Texture texture, Vector2ic size, Map<Name, SubtextureData> out) {
    if (grid.getTileSize() == null) {
        logger.error("Bad grid definition - missing mandatory property tileSize");
        return;
    }
    if (grid.getGridDimensions() == null) {
        logger.error("Bad grid definition - missing mandatory property gridDimensions");
        return;
    }
    Vector2f offset = new Vector2f(0, 0);
    if (grid.getGridOffset() != null) {
        offset.set((float) grid.getGridOffset().x / size.x(), (float) grid.getGridOffset().y / size.y());
    }
    Vector2f tileSize = new Vector2f((float) grid.getTileSize().x / size.x(), (float) grid.getTileSize().y / size.y());
    int tileX = 0;
    int tileY = 0;
    for (String name : grid.getTileNames()) {
        if (!name.isEmpty()) {
            Vector2f pos = new Vector2f(offset);
            pos.x += tileX * tileSize.x;
            pos.y += tileY * tileSize.y;
            Rectanglef tileLocation = new Rectanglef(offset.x + tileX * tileSize.x, offset.y + tileY * tileSize.y, 0.0f, 0.0f).setSize(tileSize.x, tileSize.y);
            out.put(new Name(name), new SubtextureData(texture, shrinkRegion(tileLocation)));
        }
        tileX++;
        if (tileX == grid.getGridDimensions().x) {
            tileX = 0;
            tileY++;
        }
    }
}
Also used : Vector2f(org.joml.Vector2f) Rectanglef(org.terasology.joml.geom.Rectanglef) SubtextureData(org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData) Name(org.terasology.gestalt.naming.Name)

Example 2 with Rectanglef

use of org.terasology.joml.geom.Rectanglef in project Terasology by MovingBlocks.

the class RectanglefTypeHandler method deserialize.

@Override
public Optional<Rectanglef> deserialize(PersistedData data) {
    if (!data.isNull() && data.isValueMap()) {
        PersistedDataMap map = data.getAsValueMap();
        PersistedDataArray minDataArr = map.get(MIN_FIELD).getAsArray();
        TFloatList minArr = minDataArr.getAsFloatArray();
        if (map.has(SIZE_FIELD)) {
            PersistedDataArray sizedataArray = map.get(SIZE_FIELD).getAsArray();
            TIntList sizeArr = sizedataArray.getAsIntegerArray();
            return Optional.of(new Rectanglef(minArr.get(0), minArr.get(1)).setSize(sizeArr.get(0), sizeArr.get(1)));
        }
        PersistedDataArray maxDataArr = map.get(MAX_FIELD).getAsArray();
        TFloatList maxArr = maxDataArr.getAsFloatArray();
        return Optional.of(new Rectanglef(minArr.get(0), minArr.get(1), maxArr.get(0), maxArr.get(1)));
    }
    return Optional.empty();
}
Also used : PersistedDataArray(org.terasology.persistence.typeHandling.PersistedDataArray) PersistedDataMap(org.terasology.persistence.typeHandling.PersistedDataMap) Rectanglef(org.terasology.joml.geom.Rectanglef) TFloatList(gnu.trove.list.TFloatList) TIntList(gnu.trove.list.TIntList)

Example 3 with Rectanglef

use of org.terasology.joml.geom.Rectanglef in project Terasology by MovingBlocks.

the class AtlasFormat method process.

private void process(FreeformDefinition freeform, Texture texture, Vector2ic size, Map<Name, SubtextureData> out) {
    if (freeform.getName() == null || freeform.getName().isEmpty()) {
        logger.error("Bad subimage definition - missing mandatory property name");
        return;
    }
    if (freeform.getMin() == null) {
        logger.error("Bad subimage definition '{}' - missing mandatory property min", freeform.getName());
        return;
    }
    if (freeform.getSize() == null && freeform.getMax() == null) {
        logger.error("Bad subimage definition '{}' - requires one of max or size", freeform.getName());
        return;
    }
    Vector2f min = new Vector2f((float) freeform.getMin().x / size.x(), (float) freeform.getMin().y / size.y());
    if (freeform.getSize() != null) {
        Vector2f itemSize = new Vector2f((float) freeform.getSize().x / size.x(), (float) freeform.getSize().y / size.y());
        out.put(new Name(freeform.getName()), new SubtextureData(texture, shrinkRegion(new Rectanglef(min, min).setSize(itemSize))));
    } else if (freeform.getMax() != null) {
        Vector2f max = new Vector2f((float) freeform.getMax().x / size.x(), (float) freeform.getMax().y / size.y());
        out.put(new Name(freeform.getName()), new SubtextureData(texture, shrinkRegion(new Rectanglef(min, max))));
    }
}
Also used : Vector2f(org.joml.Vector2f) SubtextureData(org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData) Rectanglef(org.terasology.joml.geom.Rectanglef) Name(org.terasology.gestalt.naming.Name)

Example 4 with Rectanglef

use of org.terasology.joml.geom.Rectanglef in project Terasology by MovingBlocks.

the class LwjglCanvasRenderer method drawTexture.

@Override
public void drawTexture(UITextureRegion texture, Colorc color, ScaleMode mode, Rectanglei absoluteRegionRectangle, float ux, float uy, float uw, float uh, float alpha) {
    if (!((TextureRegion) texture).getTexture().isLoaded()) {
        return;
    }
    Rectanglei absoluteRegion = new Rectanglei(absoluteRegionRectangle);
    if (!currentTextureCropRegion.equals(requestedCropRegion) && !(currentTextureCropRegion.containsRectangle(absoluteRegion) && requestedCropRegion.containsRectangle(absoluteRegion))) {
        textureMat.setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX, requestedCropRegion.maxX, requestedCropRegion.minY, requestedCropRegion.maxY);
        currentTextureCropRegion = requestedCropRegion;
    }
    Vector2f scale = mode.scaleForRegion(absoluteRegionRectangle, texture.getWidth(), texture.getHeight());
    Rectanglef textureArea = new Rectanglef(texture.getRegion());
    Mesh mesh = billboard;
    switch(mode) {
        case TILED:
            {
                Vector2i textureSize = texture.size();
                TextureCacheKey key = new TextureCacheKey(textureSize, new Vector2i(absoluteRegion.getSizeX(), absoluteRegion.getSizeY()));
                usedTextures.add(key);
                mesh = cachedTextures.get(key);
                if (mesh == null || mesh.isDisposed()) {
                    MeshBuilder builder = new MeshBuilder();
                    addTiles(builder, absoluteRegion, FULL_REGION, textureSize, FULL_REGION);
                    mesh = builder.build();
                    cachedTextures.put(key, mesh);
                }
                textureMat.setFloat2("scale", scale);
                textureMat.setFloat2("offset", absoluteRegion.minX, absoluteRegion.minY);
                textureMat.setFloat2("texOffset", textureArea.minX + ux * textureArea.getSizeX(), textureArea.minY + uy * textureArea.getSizeY());
                textureMat.setFloat2("texSize", uw * textureArea.getSizeX(), uh * textureArea.getSizeY());
                break;
            }
        case SCALE_FILL:
            {
                textureMat.setFloat2("offset", absoluteRegion.minX, absoluteRegion.minY);
                textureMat.setFloat2("scale", absoluteRegion.getSizeX(), absoluteRegion.getSizeY());
                float texBorderX = (scale.x - absoluteRegion.getSizeX()) / scale.x * uw;
                float texBorderY = (scale.y - absoluteRegion.getSizeY()) / scale.y * uh;
                textureMat.setFloat2("texOffset", textureArea.minX + (ux + 0.5f * texBorderX) * textureArea.getSizeX(), textureArea.minY + (uy + 0.5f * texBorderY) * textureArea.getSizeY());
                textureMat.setFloat2("texSize", (uw - texBorderX) * textureArea.getSizeX(), (uh - texBorderY) * textureArea.getSizeY());
                break;
            }
        default:
            {
                textureMat.setFloat2("scale", scale);
                textureMat.setFloat2("offset", absoluteRegion.minX + 0.5f * (absoluteRegion.getSizeX() - scale.x), absoluteRegion.minY + 0.5f * (absoluteRegion.getSizeY() - scale.y));
                textureMat.setFloat2("texOffset", textureArea.minX + ux * textureArea.getSizeX(), textureArea.minY + uy * textureArea.getSizeY());
                textureMat.setFloat2("texSize", uw * textureArea.getSizeX(), uh * textureArea.getSizeY());
                break;
            }
    }
    textureMat.setTexture("tex", ((TextureRegion) texture).getTexture());
    textureMat.setFloat4("color", color.rf(), color.gf(), color.bf(), color.af() * alpha);
    textureMat.setMatrix4("projectionMatrix", projMatrix);
    textureMat.setMatrix4("modelViewMatrix", modelMatrixStack);
    textureMat.bindTextures();
    mesh.render();
}
Also used : UITextureRegion(org.terasology.nui.UITextureRegion) TextureRegion(org.terasology.engine.rendering.assets.texture.TextureRegion) Vector2f(org.joml.Vector2f) Rectanglef(org.terasology.joml.geom.Rectanglef) MeshBuilder(org.terasology.engine.rendering.assets.mesh.MeshBuilder) FontMeshBuilder(org.terasology.engine.rendering.assets.font.FontMeshBuilder) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) Rectanglei(org.terasology.joml.geom.Rectanglei) Vector2i(org.joml.Vector2i)

Example 5 with Rectanglef

use of org.terasology.joml.geom.Rectanglef in project Terasology by MovingBlocks.

the class LwjglCanvasRenderer method drawTextureBordered.

@Override
public void drawTextureBordered(UITextureRegion texture, Rectanglei regionRectangle, Border border, boolean tile, float ux, float uy, float uw, float uh, float alpha) {
    if (!((TextureRegion) texture).getTexture().isLoaded()) {
        return;
    }
    Rectanglei region = new Rectanglei(regionRectangle);
    if (!currentTextureCropRegion.equals(requestedCropRegion) && !(currentTextureCropRegion.containsRectangle(region) && requestedCropRegion.containsRectangle(region))) {
        textureMat.setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX, requestedCropRegion.maxX, requestedCropRegion.minY, requestedCropRegion.maxY);
        currentTextureCropRegion = requestedCropRegion;
    }
    Vector2i textureSize = new Vector2i(TeraMath.ceilToInt(texture.getWidth() * uw), TeraMath.ceilToInt(texture.getHeight() * uh));
    TextureCacheKey key = new TextureCacheKey(textureSize, new Vector2i(region.getSizeX(), region.getSizeY()), border, tile);
    usedTextures.add(key);
    Mesh mesh = cachedTextures.get(key);
    if (mesh == null || mesh.isDisposed()) {
        MeshBuilder builder = new MeshBuilder();
        float topTex = (float) border.getTop() / textureSize.y;
        float leftTex = (float) border.getLeft() / textureSize.x;
        float bottomTex = 1f - (float) border.getBottom() / textureSize.y;
        float rightTex = 1f - (float) border.getRight() / textureSize.x;
        int centerHoriz = region.getSizeX() - border.getTotalWidth();
        int centerVert = region.getSizeY() - border.getTotalHeight();
        float top = (float) border.getTop() / region.getSizeY();
        float left = (float) border.getLeft() / region.getSizeX();
        float bottom = 1f - (float) border.getBottom() / region.getSizeY();
        float right = 1f - (float) border.getRight() / region.getSizeX();
        if (border.getTop() != 0) {
            if (border.getLeft() != 0) {
                addRectPoly(builder, 0, 0, left, top, 0, 0, leftTex, topTex);
            }
            if (tile) {
                addTiles(builder, new Rectanglei(border.getLeft(), 0).setSize(centerHoriz, border.getTop()), new Rectanglef(left, 0, right, top), new Vector2i(textureSize.x - border.getTotalWidth(), border.getTop()), new Rectanglef(leftTex, 0, rightTex, topTex));
            } else {
                addRectPoly(builder, left, 0, right, top, leftTex, 0, rightTex, topTex);
            }
            if (border.getRight() != 0) {
                addRectPoly(builder, right, 0, 1, top, rightTex, 0, 1, topTex);
            }
        }
        if (border.getLeft() != 0) {
            if (tile) {
                addTiles(builder, new Rectanglei(0, border.getTop()).setSize(border.getLeft(), centerVert), new Rectanglef(0, top, left, bottom), new Vector2i(border.getLeft(), textureSize.y - border.getTotalHeight()), new Rectanglef(0, topTex, leftTex, bottomTex));
            } else {
                addRectPoly(builder, 0, top, left, bottom, 0, topTex, leftTex, bottomTex);
            }
        }
        if (tile) {
            addTiles(builder, new Rectanglei(border.getLeft(), border.getTop()).setSize(centerHoriz, centerVert), new Rectanglef(left, top, right, bottom), new Vector2i(textureSize.x - border.getTotalWidth(), textureSize.y - border.getTotalHeight()), new Rectanglef(leftTex, topTex, rightTex, bottomTex));
        } else {
            addRectPoly(builder, left, top, right, bottom, leftTex, topTex, rightTex, bottomTex);
        }
        if (border.getRight() != 0) {
            if (tile) {
                addTiles(builder, new Rectanglei(region.getSizeX() - border.getRight(), border.getTop()).setSize(border.getRight(), centerVert), new Rectanglef(right, top, 1, bottom), new Vector2i(border.getRight(), textureSize.y - border.getTotalHeight()), new Rectanglef(rightTex, topTex, 1, bottomTex));
            } else {
                addRectPoly(builder, right, top, 1, bottom, rightTex, topTex, 1, bottomTex);
            }
        }
        if (border.getBottom() != 0) {
            if (border.getLeft() != 0) {
                addRectPoly(builder, 0, bottom, left, 1, 0, bottomTex, leftTex, 1);
            }
            if (tile) {
                addTiles(builder, new Rectanglei(border.getLeft(), region.getSizeY() - border.getBottom()).setSize(centerHoriz, border.getBottom()), new Rectanglef(left, bottom, right, 1), new Vector2i(textureSize.x - border.getTotalWidth(), border.getBottom()), new Rectanglef(leftTex, bottomTex, rightTex, 1));
            } else {
                addRectPoly(builder, left, bottom, right, 1, leftTex, bottomTex, rightTex, 1);
            }
            if (border.getRight() != 0) {
                addRectPoly(builder, right, bottom, 1, 1, rightTex, bottomTex, 1, 1);
            }
        }
        mesh = builder.build();
        cachedTextures.put(key, mesh);
    }
    textureMat.setFloat2("scale", region.getSizeX(), region.getSizeY());
    textureMat.setFloat2("offset", region.minX, region.minY);
    Rectanglef textureArea = texture.getRegion();
    textureMat.setFloat2("texOffset", textureArea.minX + ux * textureArea.lengthX(), textureArea.minY + uy * textureArea.lengthY());
    textureMat.setFloat2("texSize", uw * textureArea.lengthX(), uh * textureArea.lengthY());
    textureMat.setTexture("tex", ((TextureRegion) texture).getTexture());
    textureMat.setFloat4("color", 1, 1, 1, alpha);
    textureMat.bindTextures();
    mesh.render();
}
Also used : UITextureRegion(org.terasology.nui.UITextureRegion) TextureRegion(org.terasology.engine.rendering.assets.texture.TextureRegion) MeshBuilder(org.terasology.engine.rendering.assets.mesh.MeshBuilder) FontMeshBuilder(org.terasology.engine.rendering.assets.font.FontMeshBuilder) Rectanglef(org.terasology.joml.geom.Rectanglef) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) Rectanglei(org.terasology.joml.geom.Rectanglei) Vector2i(org.joml.Vector2i)

Aggregations

Rectanglef (org.terasology.joml.geom.Rectanglef)6 Vector2f (org.joml.Vector2f)4 SubtextureData (org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData)3 Name (org.terasology.gestalt.naming.Name)3 Vector2i (org.joml.Vector2i)2 FontMeshBuilder (org.terasology.engine.rendering.assets.font.FontMeshBuilder)2 Mesh (org.terasology.engine.rendering.assets.mesh.Mesh)2 MeshBuilder (org.terasology.engine.rendering.assets.mesh.MeshBuilder)2 TextureRegion (org.terasology.engine.rendering.assets.texture.TextureRegion)2 Rectanglei (org.terasology.joml.geom.Rectanglei)2 UITextureRegion (org.terasology.nui.UITextureRegion)2 TFloatList (gnu.trove.list.TFloatList)1 TIntList (gnu.trove.list.TIntList)1 TObjectIntMap (gnu.trove.map.TObjectIntMap)1 TObjectIntHashMap (gnu.trove.map.hash.TObjectIntHashMap)1 Map (java.util.Map)1 AtlasData (org.terasology.engine.rendering.assets.atlas.AtlasData)1 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)1 PersistedDataArray (org.terasology.persistence.typeHandling.PersistedDataArray)1 PersistedDataMap (org.terasology.persistence.typeHandling.PersistedDataMap)1