Search in sources :

Example 1 with SubtextureData

use of org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData 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 SubtextureData

use of org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData in project Terasology by MovingBlocks.

the class AtlasFormat method load.

@Override
public AtlasData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
    try (Reader reader = new InputStreamReader(inputs.get(0).openStream(), Charsets.UTF_8)) {
        AtlasDefinition def = gson.fromJson(reader, AtlasDefinition.class);
        Optional<? extends Texture> texture = assetManager.getAsset(def.getTexture(), Texture.class);
        if (texture.isPresent()) {
            Vector2i size = def.getTextureSize();
            if (size == null) {
                size = new Vector2i(texture.get().getWidth(), texture.get().getHeight());
            }
            Map<Name, SubtextureData> result = Maps.newHashMap();
            if (def.getGrid() != null) {
                process(def.getGrid(), texture.get(), size, result);
            }
            if (def.getGrids() != null) {
                for (GridDefinition grid : def.getGrids()) {
                    process(grid, texture.get(), size, result);
                }
            }
            if (def.getSubimage() != null) {
                process(def.getSubimage(), texture.get(), size, result);
            }
            if (def.getSubimages() != null) {
                for (FreeformDefinition freeform : def.getSubimages()) {
                    process(freeform, texture.get(), size, result);
                }
            }
            return new AtlasData(result);
        }
        return null;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) SubtextureData(org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Vector2i(org.joml.Vector2i) Name(org.terasology.gestalt.naming.Name)

Example 3 with SubtextureData

use of org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData 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 SubtextureData

use of org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData in project Terasology by MovingBlocks.

the class Atlas method doReload.

@Override
protected void doReload(AtlasData data) {
    subtextures.clear();
    for (Map.Entry<Name, SubtextureData> entry : data.getSubtextures().entrySet()) {
        ResourceUrn subtextureUrn = new ResourceUrn(getUrn().getModuleName(), getUrn().getResourceName(), entry.getKey());
        subtextures.put(subtextureUrn, entry.getValue());
    }
}
Also used : SubtextureData(org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Map(java.util.Map) Name(org.terasology.gestalt.naming.Name)

Example 5 with SubtextureData

use of org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData in project Terasology by MovingBlocks.

the class WorldAtlasImpl method createTextureAtlas.

private void createTextureAtlas(final Texture texture) {
    final Map<Name, Map<Name, SubtextureData>> textureAtlases = Maps.newHashMap();
    final Vector2f texSize = new Vector2f(getRelativeTileSize(), getRelativeTileSize());
    tileIndexes.forEachEntry((tileUri, index) -> {
        Vector2f coords = getTexCoords(index);
        SubtextureData subtextureData = new SubtextureData(texture, new Rectanglef(coords, coords).setSize(texSize));
        Map<Name, SubtextureData> textureAtlas = textureAtlases.get(tileUri.getModuleName());
        if (textureAtlas == null) {
            textureAtlas = Maps.newHashMap();
            textureAtlases.put(tileUri.getModuleName(), textureAtlas);
        }
        textureAtlas.put(tileUri.getResourceName(), subtextureData);
        return true;
    });
    for (Map.Entry<Name, Map<Name, SubtextureData>> atlas : textureAtlases.entrySet()) {
        AtlasData data = new AtlasData(atlas.getValue());
        Assets.generateAsset(new ResourceUrn(atlas.getKey(), new Name("terrain")), data, Atlas.class);
    }
}
Also used : AtlasData(org.terasology.engine.rendering.assets.atlas.AtlasData) Vector2f(org.joml.Vector2f) SubtextureData(org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData) Rectanglef(org.terasology.joml.geom.Rectanglef) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) TObjectIntMap(gnu.trove.map.TObjectIntMap) Map(java.util.Map) TObjectIntHashMap(gnu.trove.map.hash.TObjectIntHashMap) Name(org.terasology.gestalt.naming.Name)

Aggregations

SubtextureData (org.terasology.engine.rendering.assets.texture.subtexture.SubtextureData)5 Name (org.terasology.gestalt.naming.Name)5 Vector2f (org.joml.Vector2f)3 Rectanglef (org.terasology.joml.geom.Rectanglef)3 Map (java.util.Map)2 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)2 TObjectIntMap (gnu.trove.map.TObjectIntMap)1 TObjectIntHashMap (gnu.trove.map.hash.TObjectIntHashMap)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Vector2i (org.joml.Vector2i)1 AtlasData (org.terasology.engine.rendering.assets.atlas.AtlasData)1