Premultiplied Alpha
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
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
Re: Premultiplied Alpha
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.
Re: Premultiplied Alpha
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
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
Re: Premultiplied Alpha
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.
Re: Premultiplied Alpha
Hmmm. Interesting idea. We might be able to do some preprocessing server-side.
tnx
tnx
Who is online
Users browsing this forum: Google [Bot], Semrush [Bot] and 4 guests