【问题标题】:Libgdx update texture at runtimeLibgdx 在运行时更新纹理
【发布时间】:2014-02-08 20:45:09
【问题描述】:

我需要在运行时更新纹理,我的代码是:

    public void updateTexture(Bitmap bmp) {
        mTexture = new Texture( ImageHelper.bitmapToPixmap( bmp ) );
    }

但它不起作用。有人可以帮我吗?
谢谢

更新

    public void render(PerspectiveCamera camera) {

        mTexture.bind();
        mShaderProgram.begin();

            mShaderProgram.setUniformMatrix("u_worldView", camera.combined );
            mShaderProgram.setUniformi("u_texture", 0);
                mMesh.render(mShaderProgram, GL20.GL_TRIANGLES);

        mShaderProgram.end();
    }

新问题

我在所有对象上都获得了第一个旧纹理。

【问题讨论】:

  • 不工作具体是什么意思?旧纹理不会消失吗?你看到一个空白吗?您是否将mTexture 存储在您的任何其他对象中?
  • Wat P.T.说的也有道理,我认为您的 mTexture 被处理了两次....检查一下,看看是否有其他对象偶然引用了 mTexture,从而使其无法更新...
  • 我可以看到旧纹理。不是新的。纹理闪烁一次,仅此而已。我必须调用 .dispose();或不?我有一个带有自定义类的数组列表,其中包含 mTexture

标签: java android libgdx


【解决方案1】:

你的问题是你在打电话

mTexture.disose();

释放对象,然后您尝试将另一个对象分配给它。这永远不会奏效。 相反,您可以做的是:

public void updateTexture(Bitmap bmp) {
    mTexture = new Texture( ImageHelper.bitmapToPixmap( bmp ) );
}

这应该解决它。虽然确实不建议这样做,因为它会在同一个实例中创建另一个对象,但无论如何,对吧?

【讨论】:

    【解决方案2】:

    好的,我终于在这里找到了问题:

    Converting Android Bitmap to LibGdx's Texture

    工作代码:

    public void updateTexture(final Bitmap bmp) {
    
         Gdx.app.postRunnable(new Runnable() {
                @Override
                public void run() {            
                    mTexture    = new Texture( mImageHelper.bitmapToPixmap( bmp ) );
                    mSprite.setTextureRegion(new TextureRegion(mTexture));
                }
         });
    
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 2015-11-01
      • 1970-01-01
      • 2013-04-10
      • 2019-05-30
      • 2021-09-18
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      相关资源
      最近更新 更多