5

我很高兴使用 LibGDX 框架的 SpriteBatch 类。我的目标是通过着色器修改精灵的表示。

batch = new SpriteBatch(2, shaderProgram);

我从 SpriteBatch 类中复制了默认着色器并添加了另一个统一的 Sampler 2d

+ "uniform sampler2D u_Texture2;\n"//

有没有一种工作方法可以将纹理赋予着色器。这样做,总是以 ClearColor 屏幕结束。

batch.begin();
  texture2.bind(1);
  shaderProgram.setUniformi("u_Texture2", 1);
  batch.draw(spriteTexture,positions[0].x,positions[0].y);
  batch.draw(spriteTexture,positions[1].x,positions[1].y);
batch.end();

每个纹理单独工作。在 Mesh Class 的帮助下手动绘制可以按预期工作。那么我该怎么做才能使用 SpriteBatch 的便利性呢?

寻求帮助

4

1 回答 1

7

我想那里的问题与纹理绑定有关。SpriteBatch 假定活动纹理单元为 0,因此它调用

lastTexture.bind();代替lastTexture.bind(0);

问题是您给它的活动单位是 1(正如您texture2.bind(1);在代码中调用的那样)。因此,纹理单元 0 永远不会被绑定,并且可能是导致空白屏幕的原因。

例如,我会在通话Gdx.GL20.glActiveTexture(0);之前添加一个。draw我不太确定它会解决问题,但这是一个开始!

编辑: 我尝试了我建议的解决方案,它有效!:D。为了更清楚,你应该这样做:

      batch.begin();
      texture2.bind(1); 
      shaderProgram.setUniformi("u_Texture2", 1);
      Gdx.gl.glActiveTexture(GL10.GL_TEXTURE0);//This is required by the SpriteBatch!!
             //have the unit0 activated, so when it calls bind(), it has the desired effect.
      batch.draw(spriteTexture,positions[0].x,positions[0].y);
      batch.draw(spriteTexture,positions[1].x,positions[1].y);
    batch.end();
于 2012-09-05T13:10:36.823 回答