Is there any way to remove the color from a Texture in LibGDX and be left with a grayscaled image?
If you don’t need it done in real-time, then you’re better off using Photoshop or GIMP to save two copies of your image.
If you need it in real-time or need to process a lot of sprites, using shaders is the ideal solution.
Shader Lesson 3: Grayscale and other effects
Code example
Another solution is to process the Pixmap before uploading it to a Texture:
Pixmaps & Textures
Yeah looking through the LWJGL Shader Tutorial now and that looks like the better solution.
Thanks a million for the code example as well. That will save me a ton of time lol.
Depending on what version of GL you’re using you can use glTexEnvf() + glTexEnvfv() + glColor() to draw pure white sprites. Or its shader counterpart.
Yeah it needs to be done in real time. So I’m going with Shaders. I found settings for a default shader as well.
My problem right now is that when I apply the grayscaled shader nothing renders… but when I set the shader to the default it renders everything after it fine.
I’m, obviously, very new to shaders so is there anything simple that I might be missing?
Note: I copied VERT and FRAG entirely from the code example.
This is what takes place in my render…
batch.setShader(shader);
time += Gdx.graphics.getDeltaTime();
grayscale = (float)Math.sin(time)/2f+0.5f;
shader.begin();
shader.setUniformf("grayscale", grayscale);
shader.end();
sprite.draw(batch);
batch.setShader(default_shader)
...
Figured it out.
Didn’t need to use
time += Gdx.graphics.getDeltaTime();
grayscale = (float)Math.sin(time)/2f+0.5f;
shader.begin();
shader.setUniformf("grayscale", grayscale);
shader.end();
It’s now just…
batch.setShader(shader);
sprite.draw(batch);
batch.setShader(default_shader)
And works as expected.
The “grayscale” uniform allows you to change the strength of the effect; i.e. in case you want some things grayscale and some things in color.
Read through lessons 1 - 3 for more info on using uniforms.
//start rendering your batch
batch.begin();
//enable grayscale
shader.setUniformf("grayscale", 1.0f);
//draw your grayscale entities
for (...)
batch.draw(...);
//now FLUSH the batch; same as using end() + begin()
batch.flush();
//now disable the grayscale effect
shader.setUniformf("grayscale", 0.0f);
//... render your color entities ...//
//finish rendering
shader.end();
This only affects the proceeding batch. So your code needs to look like this: