Search code examples
androidlibgdx

Libgdx shader on a shaperenderer's rectangle


I want to draw a 2d rectangle in the shaperenderer. That is ok. But I want to create on this rectangle a custom shader.

So I dont need texture on it, I only want a rectangle with custom shader. Is it possible? How?

My rectangle now is a gradient. I want to draw my shader on this gradient...


Solution

  • The ShapeRenderer class has a constructor where you can pass in your own ShaderProgram if you do not want to use the default. However, you cannot set the ShaderProgram on the fly like you can when using a SpriteBatch to render your stuff.

    View the source of ShapeRenderer and ImmediateModeRenderer20 class to see how the default shaders are applied. Then adjust to your needs an create an instance of ShapeRenderer with your adjusted ShaderProgram.