use of org.terasology.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.rendering.assets.texture.subtexture.SubtextureData in project Terasology by MovingBlocks.
the class HeadlessGraphics method registerCoreAssetTypes.
@Override
public void registerCoreAssetTypes(ModuleAwareAssetTypeManager assetTypeManager) {
assetTypeManager.registerCoreAssetType(Font.class, (AssetFactory<Font, FontData>) FontImpl::new, "fonts");
assetTypeManager.registerCoreAssetType(Texture.class, (AssetFactory<Texture, TextureData>) HeadlessTexture::new, "textures", "fonts");
assetTypeManager.registerCoreFormat(Texture.class, new PNGTextureFormat(Texture.FilterMode.NEAREST, path -> path.getName(2).toString().equals("textures")));
assetTypeManager.registerCoreFormat(Texture.class, new PNGTextureFormat(Texture.FilterMode.LINEAR, path -> path.getName(2).toString().equals("fonts")));
assetTypeManager.registerCoreAssetType(Shader.class, (AssetFactory<Shader, ShaderData>) HeadlessShader::new, "shaders");
assetTypeManager.registerCoreAssetType(Material.class, (AssetFactory<Material, MaterialData>) HeadlessMaterial::new, "materials");
assetTypeManager.registerCoreAssetType(Mesh.class, (AssetFactory<Mesh, MeshData>) HeadlessMesh::new, "mesh");
assetTypeManager.registerCoreAssetType(SkeletalMesh.class, (AssetFactory<SkeletalMesh, SkeletalMeshData>) HeadlessSkeletalMesh::new, "skeletalMesh");
assetTypeManager.registerCoreAssetType(MeshAnimation.class, (AssetFactory<MeshAnimation, MeshAnimationData>) MeshAnimationImpl::new, "animations");
assetTypeManager.registerCoreAssetType(Atlas.class, (AssetFactory<Atlas, AtlasData>) Atlas::new, "atlas");
assetTypeManager.registerCoreAssetType(Subtexture.class, (AssetFactory<Subtexture, SubtextureData>) Subtexture::new);
}
use of org.terasology.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.rendering.assets.texture.subtexture.SubtextureData in project Terasology by MovingBlocks.
the class AtlasFormat method process.
private void process(GridDefinition grid, Texture texture, Vector2i 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;
Rect2f tileLocation = Rect2f.createFromMinAndSize(offset.x + tileX * tileSize.x, offset.y + tileY * tileSize.y, tileSize.x, tileSize.y);
out.put(new Name(name), new SubtextureData(texture, tileLocation));
}
tileX++;
if (tileX == grid.getGridDimensions().x) {
tileX = 0;
tileY++;
}
}
}
use of org.terasology.rendering.assets.texture.subtexture.SubtextureData in project Terasology by MovingBlocks.
the class AtlasFormat method process.
private void process(FreeformDefinition freeform, Texture texture, Vector2i 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, Rect2f.createFromMinAndSize(min, 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, Rect2f.createFromMinAndMax(min, max)));
}
}
Aggregations