g33rt
Topic Author
Posts: 23
Joined: 14 Jan 2014, 16:02

Premultiplied Alpha

17 Apr 2014, 15:18

Hi,

I'm using a lot of images in my app, and the vast majority has transparency. I have to load and unload them dynamically to keep memory consumption reasonable. It kinda bothers me a bit that I have to loop through the pixels to get the transparency right to use them as an ImageSource. Especially, because I have to stick to the Unity GUI thread:


...
_texture = _www.texture;
_www.Dispose();
_www = null;

if (_alphaChannel)
{
var srcColors = _texture.GetPixels();

for (int i = 0; i < srcColors.Length; i++)
{
var srcColor = srcColors;

float multiplier = srcColor.a;

srcColors = new UnityEngine.Color(srcColor.r * multiplier, srcColor.g * multiplier, srcColor.b * multiplier, multiplier);
}

_texture.SetPixels(srcColors);
_texture.Apply();
}

_texture.Compress(false);
_image = _texture.ToImageSource();
NotifyPropertyChanged("Image");
...

Anyone has suggestions on doing this more efficiently?

Tnx
 
User avatar
jsantos
Site Admin
Posts: 4264
Joined: 20 Jan 2012, 17:18
Contact:

Re: Premultiplied Alpha

17 Apr 2014, 15:43

Are these textures being used in Unity? Because if they are not, you can avoid that path and use them directly in the xaml, we precompress them in premultiply alpha.
 
g33rt
Topic Author
Posts: 23
Joined: 14 Jan 2014, 16:02

Re: Premultiplied Alpha

17 Apr 2014, 15:52

Hi, tnx for the reply.

Well, the images are loaded dynamically. The app is skinnable, all images can either be downloaded in the app in a zip file on local storage, or directly referencing some images on the web.

Geert
 
User avatar
jsantos
Site Admin
Posts: 4264
Joined: 20 Jan 2012, 17:18
Contact:

Re: Premultiplied Alpha

17 Apr 2014, 15:58

I see, another option, can't you do the premultiply alpha step at art creation time? Tools like Adobe Photoshop can do that. If you disable texture compression in Unity, this should work, I don't know if texture compression could add artifacts to the premultiply alpha image.
 
g33rt
Topic Author
Posts: 23
Joined: 14 Jan 2014, 16:02

Re: Premultiplied Alpha

17 Apr 2014, 16:06

Hmmm. Interesting idea. We might be able to do some preprocessing server-side.

tnx

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 4 guests