How to change texture format from Alpha8 to RGBA in Unity3d?

☆樱花仙子☆ 提交于 2021-01-29 17:47:06

问题


I have been trying to change the format from a camera that give a texture in Alpha8 to RGBA and have been unsuccessful so far.

This is the code I've tried:

   public static class TextureHelperClass
    {
        public static Texture2D ChangeFormat(this Texture2D oldTexture, TextureFormat newFormat)
        {
            //Create new empty Texture
            Texture2D newTex = new Texture2D(2, 2, newFormat, false);
            //Copy old texture pixels into new one
            newTex.SetPixels(oldTexture.GetPixels());
            //Apply
            newTex.Apply();

            return newTex;
        }
    }

And I'm calling the code like this:

  Texture imgTexture = Aplpha8Texture.ChangeFormat(TextureFormat.RGBA32);

But the image gets corrupted and isn't visible.

Does anyone know how to change this Alpha8 to RGBA so I can process it like any other image in OpenCV?


回答1:


A friend provided me with the answer:

    Color[] cs =oldTexture.GetPixels();
    for(int i = 0; i < cs.Length; i++){//we want to set the r g b values to a
        cs[i].r = cs[i].a;
        cs[i].g = cs[i].a;
        cs[i].b = cs[i].a;
        cs[i].a = 1.0f;                                                
    }
    //set the pixels in the new texture
    newTex.SetPixels(cs);
    //Apply
    newTex.Apply();

This will take alot of resources but it will work for sure. If you know a better way to make this change please add an answer to this thread.



来源:https://stackoverflow.com/questions/63211877/how-to-change-texture-format-from-alpha8-to-rgba-in-unity3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!