stonstad
Topic Author
Posts: 54
Joined: 06 Jun 2016, 18:14

Offscreen Texture Alpha

12 Mar 2019, 15:10

I have asked so many questions here about Noesis and I feel bad for asking another question. :|

Today I am working with setting a rectangle fill to an offscreen rendered texture (Unity). The texture contains alpha.

The behavior seems to be that alpha is only partially respected -- i.e. if it's 100% alpha ("transparent") it works in the Noesis control but partial alpha appears black.

Here is the render target texture:
Image


And here is a rectangle with a background set to the same render target texture. You can see that alpha partially works.
Image


Here is the code I'm using which shows construction of the Noesis texture.
        private void InitializeComponent()
        {
            GUI.LoadComponent(this, "Assets/User Interface/Controls/Render Control/RenderControl.xaml");
            FrameworkElement root = this.Content as FrameworkElement;
            _RenderRectangle = root.FindName("_RenderRectangle") as Rectangle;

            _OffscreenCameraGameObject = this.FindInactiveGameObject("Offscreen Camera");
            _Camera = _OffscreenCameraGameObject.GetComponent<Camera>();

            // create render texture
            _RenderTexture = new RenderTexture((int)_Dimensions.x, (int)_Dimensions.y, 1, RenderTextureFormat.Default);
            RenderTexture.active = _RenderTexture;

            // create Noesis texture
            _RenderTexture.Create();
            NoesisTexture noesisTexture = NoesisTexture.WrapTexture(_RenderTexture, _RenderTexture.GetNativeTexturePtr(),
                _RenderTexture.width, _RenderTexture.height, 1);

            // create brush to store render texture and assign it to the rectangle
            _RenderRectangle.Fill = new ImageBrush()
            {
                ImageSource = new TextureSource(noesisTexture),
                Stretch = Stretch.None,
                Opacity = 1.0f
            };
        }
Does anyone have any thoughts on what could possibly cause this behavior? I have not ruled out Unity -- just not sure if this has been seen before. Thank you!
 
User avatar
jsantos
Site Admin
Posts: 2350
Joined: 20 Jan 2012, 17:18
Contact:

Re: Offscreen Texture Alpha

12 Mar 2019, 19:08

Does anyone have any thoughts on what could possibly cause this behavior? I have not ruled out Unity -- just not sure if this has been seen before. Thank you!
NoesisGUI expects textures in premultiplied alpha format. So, you need to multiply each channel by the alpha. We are doing this in our Unity AssetPostprocessor:
    private void OnPostprocessTexture(Texture2D texture)
    {
        if (AssetDatabase.GetLabels(assetImporter).Contains("Noesis"))
        {
            Color[] c = texture.GetPixels(0);

            // NoesisGUI needs premultipled alpha
            for (int i = 0; i < c.Length; i++)
            {
                c[i].r = c[i].r * c[i].a;
                c[i].g = c[i].g * c[i].a;
                c[i].b = c[i].b * c[i].a;
            }

            // Set new content and make the texture unreadable at runtime
            texture.SetPixels(c, 0);
            texture.Apply(true, true);
        }
    }
 
stonstad
Topic Author
Posts: 54
Joined: 06 Jun 2016, 18:14

Re: Offscreen Texture Alpha

12 Mar 2019, 20:06

The planet shown above rotates at about 30FPS. Would this approach still work, and if not, are there alternatives? i.e. would I need to write a custom shader?
 
stonstad
Topic Author
Posts: 54
Joined: 06 Jun 2016, 18:14

Re: Offscreen Texture Alpha

12 Mar 2019, 20:15

I'm thinking I might need to apply a post-processing effect to the camera creating a render texture -- so that the texture is premultiplied alpha. ... maybe a screenspace shader similar to: https://answers.unity.com/questions/764 ... alpha.html
 
User avatar
jsantos
Site Admin
Posts: 2350
Joined: 20 Jan 2012, 17:18
Contact:

Re: Offscreen Texture Alpha

12 Mar 2019, 20:41

Yes, sorry for the confusion. My code was pretending to show you how we are generating the premultiplied alpha for offline textures. For render targets you need to do that when rendering, using shaders or whatever similar in Unity. If you don't do it that way, you kill the performance as you observed.

Who is online

Users browsing this forum: No registered users and 3 guests