[英]libGDX get pixel color from sprite or texture
I was searching it on net but i wasn't able to find any solution. 我在网上搜索它,但我找不到任何解决方案。 I have a sprite or a texture and when i touch it i want to get pixel color from touch coordinates.
我有一个精灵或纹理,当我触摸它时,我想从触摸坐标获得像素颜色。
so I have: 所以我有:
if (Gdx.input.isTouched()) {
Rectangle spriteBounds = sprite.getBoundingRectangle();
if (sprite.contains(Gdx.input.getX(), Gdx.input.getY())) {
//and here something like
//Color color = sprite.getPixelColor(Gdx.input.getX(), Gdx.input.getY());
}
}
Is it possible? 可能吗? Thank you :)
谢谢 :)
Something like the following might work, but is untested. 类似下面的东西可能会起作用,但未经测试。 You can get the color via the
Pixmap
of the sprite's Texture
. 您可以通过精灵
Texture
的Pixmap
获取颜色。 You need to make sure that you are converting the input (screen) coordinates properly to the local coordinates of the texture. 您需要确保正确地将输入(屏幕)坐标转换为纹理的局部坐标。
if (Gdx.input.isTouched()) {
Rectangle spriteBounds = sprite.getBoundingRectangle();
if (spriteBounds.contains(Gdx.input.getX(), Gdx.input.getY())) {
Texture texture = sprite.getTexture();
int spriteLocalX = (int) (Gdx.input.getX() - sprite.getX());
// we need to "invert" Y, because the screen coordinate origin is top-left
int spriteLocalY = (int) ((Gdx.graphics.getHeight() - Gdx.input.getY()) - sprite.getY());
int textureLocalX = sprite.getRegionX() + spriteLocalX;
int textureLocalY = sprite.getRegionY() + spriteLocalY;
if (!texture.getTextureData().isPrepared()) {
texture.getTextureData().prepare();
}
Pixmap pixmap = texture.getTextureData().consumePixmap();
return new Color(pixmap.getPixel(textureLocalX, textureLocalY));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.