Is it possible to use LibGDX SpriteBatch?

I gladly use the SpriteBatch class for the LibGDX platform. My goal is to change the presentation of the sprite through a shader.

batch = new SpriteBatch(2, shaderProgram);

I copied the default shader from the SpriteBatch class and added another uniform 2d sampler

+ "uniform sampler2D u_Texture2;\n"//

Is there a way to make the texture a shader. Doing this like this always ends on the ClearColor screen.

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();

Every texture works. Hand drawing using the Mesh class works as expected. So what can I do to use the convenience of SpriteBatch?

thanks for reference

+5
source share
1 answer

I assume the problem is with texture bindings. SpriteBatch claims that the active texture element will be 0, so it makes a call

lastTexture.bind(); lastTexture.bind(0);

, , , 1 ( texture2.bind(1); ). 0 , .

, Gdx.GL20.glActiveTexture(0); draw. , , !

EDIT: , !: 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();
+6

All Articles