Pre-Multiplied Alpha

Hi all

I’m a long time XNA user, and one thing I really like about XNA is that it uses pre-multiplied alpha. 2 main things I like about it - it reduces ugly edges around transparent graphics, and it enables “free” additive blending.

What is Unity’s support for pre-multiplied alpha like?

a) is there somewhere I can flip a switch to say "use pre-multiplied alpha?

b) can the pipeline take a non-pre-multiplied alpha PNG and convert it to pre-multiplied alpha for export?

Thanks!

There is a switch in the image import settings called ‘Alpha is Transparency’. I’m pretty sure that toggles pre-multiplied alpha.

Hmmm, I wonder if that really is pre-multiplied alpha or not… and I wonder if it is, why did they not just call it that?

An example of what I want to do…

  • Have an explosion graphic - transparent background, with a relatively low alpha on the explosion graphic
  • Set the color of this graphic to (255, 255, 255, 0)… or if the texture itself is white, maybe something like (255, 220, 170, 0)
  • Render multiple of these on top of each other, at random positions rotations
  • This will achieve a nice explosion look

Is this possible…?

Yes.

What’s the issue you’re having?

Just making sure Unity supports pre-multiplied alpha properly.

Do you know how it works, in terms of the content pipeline?

ie. does unity load the images, and then convert them to pre-multiplied upon export?
or perhaps it converts them on load?
or would you have to write a custom loader?

No, it doesn’t. As far as I can tell, it processes the texture to reduce ‘nasty fringes’ (by modifying the colour of zero-alpha pixels). If it was actually premultiplying, you’d need to use a different shader to render it correctly.

The ‘alpha is transparency’ button usually works well to fix fringes, and for most people it will be a satisfactory alternative to premultiplied alpha - but there are cases where it’s not as good as actual premultiplied alpha:

  1. Textures with sudden changes in colour and alpha at the same place - e.g. an opaque white circle with a low-alpha black drop-shadow. These can still exhibit nasty fringes.

  2. You can’t use clever premultiplied alpha tricks, such as regular alpha blending and additive blending in a the same draw call.

Unity doesn’t natively have a ‘premultiply alpha’ option (this would be a nice feature), but NGUI supports it for its texture atlases, and other 2D/UI systems might do too. (You could just write a script to load a texture, premultiply it, and save it elsewhere?)