我在过去几天尝试了AndEngine(GLES2)。 我遇到了来自Examples项目的SpriteExample的问题。 在示例项目的SpriteExample中,face_box.png精灵看起来很漂亮。 但是,当我将相同的代码和face_box.png文件复制到我自己的单独项目时,精灵看起来像素化。
由于代码是一样的,我猜问题是一些配置设置,但是我无法弄明白。
我正在使用ICS在Galaxy S2上运行。
有没有人知道可能导致问题的原因?
这是代码,如果有人想知道 -
public class AndEngineMapActivity extends SimpleBaseGameActivity implements OnClickListener {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
// ===========================================================
// Fields
// ===========================================================
private ITexture mTexture;
private ITextureRegion mFaceTextureRegion;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
@Override
public void onCreateResources() {
try {
this.mTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/face_box.png");
}
});
this.mTexture.load();
this.mFaceTextureRegion = TextureRegionFactory.extractFromTexture(this.mTexture);
} catch (IOException e) {
Debug.e(e);
}
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
/* Calculate the coordinates for the face, so its centered on the camera. */
final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
/* Create the face and add it to the scene. */
final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
scene.attachChild(face);
return scene;
}
@Override
public void onClick(final ButtonSprite pButtonSprite, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AndEngineMapActivity.this, "Clicked", Toast.LENGTH_LONG).show();
}
});
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
**更新:**按照JoenEye的建议,我尝试使用
加载纹理@Override
public void onCreateResources() {
try {
this.mTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/face_box.png");
}
}, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mTexture.load();
this.mFaceTextureRegion = TextureRegionFactory.extractFromTexture(this.mTexture);
} catch (IOException e) {
Debug.e(e);
}
}
结果有所改善,笑脸图片看起来更好一点,但它仍然没有示例项目那么清晰。
****另一个更新:****
这些是我得到的结果的图像。
-
这是原始示例项目中的一个(最佳结果)
这是我的项目中没有TextureOptions.BILINEAR_PREMULTIPLYALPHA的那个
这是我的项目与TextureOptions.BILINEAR_PREMULTIPLYALPHA(当前结果)
顺便说一句 - 有趣的结果,一旦我创建了另一个只有这个类的空项目,它运行完美,看起来很好。
所以我想我的项目一定是某种配置问题。
我很高兴能得到更多的想法!
谢谢!
答案 0 :(得分:2)
我猜测示例项目和结果不好的项目之间的区别是精灵的子像素位置或者是稍微拉伸/倾斜。你是否小心精确地在像素边界上绘制精灵?
问题是当绘制一个片段时,如果片段与它正在采样的纹理没有完美对齐,那么它必须生成一个与纹理不完全相同的颜色。例如,如果你有一个10x10像素的纹理,并且你在屏幕坐标(0.5,0.5)处绘制它,那么每个像素或者只需要选择最近的纹素(NEAREST
采样),或者将附近的纹素混合在一起({ {1}}或LINEAR
抽样)。如果两个纹素与样本点的距离相等,则BILINEAR
可能会出现舍入问题(这可能会解释第一个错误结果中略显丑陋的图像)。如果您使用NEAREST
采样,那么它只是将最近的纹素混合在一起,这可能会使您在第二个结果中看到的图像略微模糊。
因此,要对具有微小特征的小图像进行补救,您需要始终确保绘制图像,使其与像素边界完全对齐。你可以在你的申请中检查你是否这样做了吗?
有关更多信息,我会进行一些谷歌搜索'像素完美采样/渲染',这将为您提供更多信息。
编辑,我也注意到了这一点,这可以解释:LINEAR
你在这里进行整数除法,如果图像宽度/高度是奇数,可能会导致你将“中心”的值减去半个像素。然后,当您将此值传递给AndEngine时,您的中心误差为半个像素,解释了子像素问题。
答案 1 :(得分:1)
尝试使用加载图像的标准方式,除非您出于某种原因使用您需要的方法。
mTexture = new BitmapTexture(1024, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
BitmapTextureRegionFactory.createFromAsset(mTexture, this, "face_box.png", 0, 0);
您可能最终会使用可构建的纹理自动将图像放置在纹理上,但这可以帮助您确定问题所在。
答案 2 :(得分:1)
最终我决定打开一个新项目并将我的所有代码移到新项目中。
有效。
我知道这是一个糟糕的解决方案,但它解决了这个问题。
如果我再次提出这个问题并找到真正的解决方案,我会在这里更新。