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++;
}
}
}
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;
}
}
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))));
}
}
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());
}
}
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);
}
}
Aggregations