MrJul
Posts: 4
Joined: 25 Oct 2015, 17:04

Re: Noesis 2 png transparency

25 Apr 2017, 11:42

If that can help people, here's a small Unity Editor script to automatically pre-multiply alpha at import time:
https://gist.github.com/MrJul/1042aa754 ... a7d7d039c3

It doesn't change the original file, only the imported Unity asset, which is useful since the PNG display stays "correct" in non-Unity editors (Blend, etc).
 
User avatar
jsantos
Site Admin
Posts: 3905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Noesis 2 png transparency

25 Apr 2017, 20:52

Thanks for this! We will include it in our documentation!
 
ctgdavid
Posts: 2
Joined: 29 Dec 2022, 16:29

Re: Noesis 2 png transparency

04 Jan 2023, 05:26

Sometimes you (or rather the user) is bringing in a texture at runtime that you have no control over the authoring up. In which case, if the image has alpha you are going to have to get it in the right format so it looks correct. To wit, I submit the following extension to the Texture2D class which handles this:
public static class Texture2DExtensions
{
    public static void PreMultiplyAlpha(this Texture2D texture)
    {
        // Check if the texture has an alpha channel
        if (texture.format == TextureFormat.Alpha8 ||
            texture.format == TextureFormat.ARGB4444 ||
            texture.format == TextureFormat.RGBA32 ||
            texture.format == TextureFormat.RGBA4444 ||
            texture.format == TextureFormat.RGBAFloat ||
            texture.format == TextureFormat.RGBAHalf)
        {
            // Get the pixel data of the texture
            Color[] pixels = texture.GetPixels();

            // Pre-multiply the alpha values of the pixels
            for (int i = 0; i < pixels.Length; i++)
            {
                pixels[i] = new Color(
                    pixels[i].r * pixels[i].a,
                    pixels[i].g * pixels[i].a,
                    pixels[i].b * pixels[i].a,
                    pixels[i].a
                );
            }

            // Set the pixel data of the texture
            texture.SetPixels(pixels);
            texture.Apply();
        }
    }
}
Given Noesis' insistence on pre-multiplied Alpha, and Unity's insistence on not having pre-multiplied Alpha... might be good to have this in the Noesis API somewhere (if such a thing doesn't already exist).

David
 
User avatar
jsantos
Site Admin
Posts: 3905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Noesis 2 png transparency

04 Jan 2023, 12:55

Given Noesis' insistence on pre-multiplied Alpha, and Unity's insistence on not having pre-multiplied Alpha... might be good to have this in the Noesis API somewhere (if such a thing doesn't already exist).
It is impossible to get the right result without using pre-multiplied alpha, but we could do this internally in the shader. Slightly more inefficient but probably worth it. We have plans to implement support for premultiplied and non-premultiplied textures in the long-term.

Thanks for the contribution.
 
ctgdavid
Posts: 2
Joined: 29 Dec 2022, 16:29

Re: Noesis 2 png transparency

07 Jan 2023, 21:20

Yeah, I'm not complaining ... (that much)... but thought this would be useful.

Making this fact more obvious would help too. Noesis suffers from information overload trying to get started with it. And is saddled with all the complexities of xaml, which is a world of hurt all by itself.

As the saying goes:

Xaml makes the difficult easy, and the easy impossible.
 
User avatar
jsantos
Site Admin
Posts: 3905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Noesis 2 png transparency

10 Jan 2023, 15:01

Thanks for the feedback! There are also plans to automatically apply the "noesis" label to textures used by Noesis in Unity.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 21 guests