- Set sprite size. Now i can change sprite only by setting scale. For example i have 2 textures one 64x64 and one 128x128. I set sprite size 1x1 and when change it texture in runtime, the sprite will be same size in world. I can change scale every time, but for that i need to save somewhere in lua, sizes of my texture regions.
- Set sprite pivot. Sprites use center pivot, which is not good,in some cases.
- Get texture regions size from atlas in runtime. If i can get texture regions size in runtime, i can implemet size and pivot by myself
If you offset the sprite in a game object and rotate the game object you can put the pivot wherever you want.
Yes, but if i use regions with different sizes it will break pivot. I can also change offset but i need to know texture region size.
You can use play_animation to set the desired texture in an atlas (i.e. an animation with only 1 frame). This will also change the size of the sprite. This will not work if you’ve changed the size of the actual texture resource though.
Ok, what are you trying to build?
Something like this. Use sprites in 3d to make walls.
1)I need pivot be at left bottom angle of sprite.(Sprite can have different sizes)
2)The texture region for wall can have different size.But in world, they must have same size.
To make that i need to know texture region size in runtime.
I think about making a script that will be create lua module with all my texture regions sizes from atlas.
But having such features in engine will be cool.
I made a jar to parse atlas, and create lua module with region sizes. So if someone need it your can use it.
local M = {
sprite1 = {texture = hash("sprite1"), width = 64, height = 64, aspect = 1.0},
sprite2 = {texture = hash("sprite2"), width = 64, height = 64, aspect = 1.0},
sprite3 = {texture = hash("sprite3"), width = 64, height = 64, aspect = 1.0},
colorstone = {texture = hash("colorstone"), width = 64, height = 64, aspect = 1.0},
eagle = {texture = hash("eagle"), width = 64, height = 64, aspect = 1.0},
mossy = {texture = hash("mossy"), width = 64, height = 64, aspect = 1.0},
purplestone = {texture = hash("purplestone"), width = 64, height = 64, aspect = 1.0},
wall1 = {texture = hash("wall1"), width = 64, height = 64, aspect = 1.0},
wall2 = {texture = hash("wall2"), width = 64, height = 64, aspect = 1.0},
wall3 = {texture = hash("wall3"), width = 64, height = 64, aspect = 1.0},
wall4 = {texture = hash("wall4"), width = 64, height = 64, aspect = 1.0}
}
return M
package com.d954mas.textures;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static final class ImageData {
public String name;
public int width, height;
public ImageData(String name, int width, int height) {
this.name = name;
this.width = width;
this.height = height;
}
@Override public String toString() {
return "ImageData{" +
"name='" + name + '\'' +
", width=" + width +
", height=" + height +
'}';
}
}
public static void main(String[] args) throws IOException {
File atlas = new File(args[0]);
String savePath = args[1];
List<String> imagesPathes = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(atlas))) {
while (reader.ready()) {
String line = reader.readLine();
if (line.matches(" {2}image: .*")) {
imagesPathes.add(line.split("image: ")[1]);
}
}
}
List<ImageData> imageDates = new ArrayList<>();
for (String path : imagesPathes) {
path = path.replaceAll("\"", "");
File input = new File("." + path);
BufferedImage img = ImageIO.read(input);
imageDates.add(new ImageData(input.getName().substring(0, input.getName().lastIndexOf(".")), img.getWidth(),
img.getHeight()));
}
StringBuilder luaBuilder = new StringBuilder();
luaBuilder.append("local M = {\n");
for (ImageData imageData : imageDates) {
System.out.println(imageData);
luaBuilder.append(" ").append(imageData.name).append(" = {");
luaBuilder.append("texture = hash(\"").append(imageData.name).append("\")").append(", ");
luaBuilder.append("width = ").append(imageData.width).append(", ");
luaBuilder.append("height = ").append(imageData.height).append(", ");
luaBuilder.append("aspect = ").append(imageData.width / (float)imageData.height);
luaBuilder.append("},\n");
}
luaBuilder.deleteCharAt(luaBuilder.length() - 2);
luaBuilder.append("}\n");
luaBuilder.append("return M");
String string = luaBuilder.toString();
System.out.println(string);
try (FileWriter writer = new FileWriter(new File(savePath))) {
writer.write(string);
}
}
}