Skip to content
Advertisement

LIBGdx – I get gray pixels in a sprite when saving as PNG

I’ve been trying to scale down a sprite and save it to the android local store for later use, but everything I’ve tried always results in a grey edge around the sprite.

Sprites

As you can see, the sprite batch blend function is set to GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA and rendering to the screen look fine. It’s only when the PNG is written that I get the dark edge. I’ve tried setting Pixmap blending to none and using a premultiplied alpha version of the original image (that’s why there are two images), but when I look in the emulator’s file system I get the same result. Here’s my code:

JavaScript

Advertisement

Answer

There was several issues and we found the solution together on Discord, so to sum things up to others :

  1. Blending function that preserve alpha : spriteBatch.setBlendFunctionSeparate(GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO)
  2. Clearing framebuffer before drawing : glClearColor(0,0,0,0); glClear(GL20.GL_COLOR_BUFFER_BIT)
  3. The Original PNG file has black RGB on transparent and half-transparent pixels instead of edges sprite color.

For the 3rd point, it has to be re-exported from Gimp like this :

  • add a mask layer with “transfer alpha channel” option
  • fill RGB with the sprite color (fixing edges color)
  • export as PNG with “save color values from transparent pixels” option on.
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement