-
- ai_enabled
- Posts: 231
- Joined:
- Contact:
Using transparent images as TextureSource (Unity)
After release of the NoesisGUI 1.1 we was very happy to start using the TextureSource feature. But it has issue with the tranparent textures:
Converter usage example code (C#):
We asked the NoesisGUI team about it and they respond kindly:
So, the NoesisGUI require textures in premultiplied alpha format to render it correctly. Here is the C# code of texture converter class written for our game project. We decide to open source of this converter to support the NoesisGUI community!"We have been investigating this issue and found an explanation. Problem is that Unity doesn't use premultiply alpha (blogs.msdn.com/b/shawnhar/archive/2009/11/06/premultiplied-alpha.aspx) and NoesisGUI pipeline is entirely based in premultiply alpha. Premultiply alpha is the correct way to do the things.
Ideally the conversion from straight alpha to premultiplied alpha should be done at texture import time (as we do in Noesis), but there is no option to active it in Unity. The solution for you is doing in by hand. You have to multiply each color channel by the alpha channel."
Code: Select all
using UnityEngine;
public static class TextureConverter
{
public static Texture2D ConvertToPremultipliedAlpha(Texture2D source)
{
Color[] srcColors = source.GetPixels();
Color[] dstColors = new Color[srcColors.Length];
for (int i = 0; i < srcColors.Length; i++)
{
Color srcColor = srcColors[i];
float a = srcColor.a;
dstColors[i] = new Color(srcColor.r * a, srcColor.g * a, srcColor.b * a, a);
}
Texture2D result = new Texture2D(source.width, source.height, TextureFormat.ARGB32, false);
result.SetPixels(dstColors);
result.Apply();
return result;
}
}
Code: Select all
// texture is variable representing Texture2D
var convertedTexture = TextureConverter.ConvertToPremultipliedAlpha(texture);
// now you can use this texture in TextureSource
var textureSource = new TextureSource(convertedTexture);
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
-
- ai_enabled
- Posts: 231
- Joined:
- Contact:
Re: Using transparent images as TextureSource (Unity)
Here is correct result image:
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
-
- ai_enabled
- Posts: 231
- Joined:
- Contact:
Re: Using transparent images as TextureSource (Unity)
And one important note about it - if you import in the Unity Editor, please set texture type to "Advanced" and turn on checkbox "Read/write enabled":
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
-
-
sfernandez
Site Admin
- Posts: 3203
- Joined:
Re: Using transparent images as TextureSource (Unity)
Great job!!
Thanks for sharing with the community!

Thanks for sharing with the community!

Re: Using transparent images as TextureSource (Unity)
We are going to improve the documentation with all this and if you don't mind we are going to publish the source code to convert to premultiply-alpha in our github project: https://github.com/Noesis/noesisgui-contrib
thank!
thank!
-
- ai_enabled
- Posts: 231
- Joined:
- Contact:
Re: Using transparent images as TextureSource (Unity)
Feel free to use this source code anywhere.We are going to improve the documentation with all this and if you don't mind we are going to publish the source code to convert to premultiply-alpha in our github project: https://github.com/Noesis/noesisgui-contrib
thank!
Thank you for the great framework!
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
Re: Using transparent images as TextureSource (Unity)
Hi there,
I've got a large amount of transparent pictures being displayed in my app, and this preprocessing stage is very heavy and slow on mobile devices.
I was able to preprocess everything, saving the Texture2D raw data so I can load it quite quickly after.
But, for now, I haven't find any way to use a "compressed" file format with preprocessed value within (eg., TIFF, PNG, etc... with already processed colors so that I can avoid the multiplication stage).
Storing uncompressed data directly in the resources is not a good solution, for my apk size increases drastically. I'm building this cache on runtime, but it requires a lot of space (up to 100 Mo) on the user device.
Have anyone find a way to preprocess the multiplication but without the need to store uncompressed picture data ?
If Noesis VNext could manage unpremultiplied alpha, it would be a wonderful time savior
[Edit : Some clarifications]
I've got a large amount of transparent pictures being displayed in my app, and this preprocessing stage is very heavy and slow on mobile devices.
I was able to preprocess everything, saving the Texture2D raw data so I can load it quite quickly after.
But, for now, I haven't find any way to use a "compressed" file format with preprocessed value within (eg., TIFF, PNG, etc... with already processed colors so that I can avoid the multiplication stage).
Storing uncompressed data directly in the resources is not a good solution, for my apk size increases drastically. I'm building this cache on runtime, but it requires a lot of space (up to 100 Mo) on the user device.
Have anyone find a way to preprocess the multiplication but without the need to store uncompressed picture data ?
If Noesis VNext could manage unpremultiplied alpha, it would be a wonderful time savior

[Edit : Some clarifications]
Re: Using transparent images as TextureSource (Unity)
Hi!
Can't you do the premultiplication in the source image itself? (in the .PNG for example, just before Unity creates the internal texture). That way you can use compression without any kind of problem.
Problem with not using premultiply alpha is that the results are not correct. You can find more information about it in internet, but basically without premult-alpha you get halos around the edges. What I don't really understand is how Unity still doesn't support this step at import time.
http://answers.unity3d.com/questions/98 ... -pngs.html
Can't you do the premultiplication in the source image itself? (in the .PNG for example, just before Unity creates the internal texture). That way you can use compression without any kind of problem.
Problem with not using premultiply alpha is that the results are not correct. You can find more information about it in internet, but basically without premult-alpha you get halos around the edges. What I don't really understand is how Unity still doesn't support this step at import time.
http://answers.unity3d.com/questions/98 ... -pngs.html
Re: Using transparent images as TextureSource (Unity)
Hi !Hi!
Can't you do the premultiplication in the source image itself? (in the .PNG for example, just before Unity creates the internal texture). That way you can use compression without any kind of problem.
Problem with not using premultiply alpha is that the results are not correct. You can find more information about it in internet, but basically without premult-alpha you get halos around the edges. What I don't really understand is how Unity still doesn't support this step at import time.
http://answers.unity3d.com/questions/98 ... -pngs.html
In fact that was the first thing I tried :
* First, using Photoshop to perform the premultiplication => no success
* Then, creating an editor extension, taking a Texture2D, premultipliing it and saving it to PNG => no success
I guess the issue is related to the PNG format itself (either in terms of precision, or for its compression). And Unity can only save Texture2D as PNG, JPG, or Raw data.
With my workaround (building premultiplied cache) everything is now OK, i'm just concerned about the temp data folder size

Re: Using transparent images as TextureSource (Unity)
What do you mean with "no success" ? Could you elaborate?* First, using Photoshop to perform the premultiplication => no success
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 7 guests