LibGdx 纹理绘制为与 #getTextureData 给出的相反
我一直在尝试解决在我的 libgdx 桌面程序上正确显示纹理时遇到的这个问题.
I've been trying to resolve this issue I have been having with displaying a texture correctly on my libgdx desktop program.
我有一个正交相机,当我设置为:
I have an Orthographic camera with which when I set as:
camera.setOrtho(false);
我得到这张图片:
当我将其设置为:
camera.setOrtho(true);
我得到这张图片:
红色图像是用 SpriteBatch 绘制的:
The red image is drawn with a SpriteBatch:
batch.draw(texture, x, y, width, height);
而白色图像是从根据 alpha 值是否为 1 绘制的各个点绘制的.
While the white image is drawn from individual points plotted based on if their alpha value was 1.
TextureData td = texture.getTextureData();
td.prepare;
Pixmap map = td.consumePixmap();
for (int i = 0; i < map.getWidth(); i++)
for (int j = 0; j < map.getHeight(); j++)
if (new Color(map.getPixel(i, j)).a == 1)
points.add(new Point(i, j));
上面是用于获取所有不透明像素的代码.它们显示为白色图像,似乎与原始纹理相反.
Above is the code used to get all the non-transparent pixels. They are displayed as the white image, and seem to be the inverse of the original texture.
但是,绘制的点和图像本身始终是彼此的倒数.
However, the points plotted and the image itself is always an inverse of one another.
有没有办法解决这个问题?
Is there any way of resolving this issue?
谢谢.
PS:我尝试了多种方法来尝试解决此问题:
PS: I've tried multiple ways of trying to fix this:
- 使用 Sprite 并翻转它们
- 使用 TextureRegions 并翻转它们
似乎没有任何效果.
推荐答案
是的,这很讨厌,Libgdx 与它的坐标系不一致.就像 font.draw()
从上向下绘制.改变你在循环内绘制/绘图的迭代方式,基本上你必须手动翻转它.因此,当 j == 0
时,y 将成为最高值,如果 j == map.getHeight
y 将成为最低值.然后在这些值内线性插值.如果您在循环中显示了什么,我可以提供更多帮助.
Yes this is very anoying, Libgdx is not consistent with it's coordinate system. Like font.draw()
draws from top downward. Alter the way you iterate over you draw/plot inside your loop, basically you have to flip it manually. So when j == 0
then y would become the highest value and if j == map.getHeight
y would become the lowest value. Then interpolate linearly within these values. If you shown whats inside your loop I could help out a bit more.
相关文章