سؤال

I'm happily using the SpriteBatch class of the LibGDX Framework. My aim is to modify the representation of the sprite through a shader.

batch = new SpriteBatch(2, shaderProgram);

I copied the default shader from the SpriteBatch Class and added another uniform Sampler 2d

+ "uniform sampler2D u_Texture2;\n"//

Is there a working way to give the texture to the shader. Doing it like this, allways ends up in a 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();

Each texture alone is working. Drawing manually with the help of the Mesh Class works as expected. So what can i do to use the convenience of SpriteBatch?

THX for Help

هل كانت مفيدة؟

المحلول

I guess the problem there is related to texture bindings. SpriteBatch asumes that the active texture unit will be 0, so it makes a call to

lastTexture.bind(); instead of lastTexture.bind(0);

The problem is that the active unit you are giving it is 1 (as you call texture2.bind(1); in your code). so, texture unit 0 is never bound, and may be thats causing the blank screen.

For instance, i would add a Gdx.GL20.glActiveTexture(0); before the draw calls. I'm not quite sure it will solve the problem, but it's a start!

EDIT: I try my suggested solution and it works! :D. To be clearer, you should be doing this:

      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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top