【问题标题】:How to change texture format from Alpha8 to RGBA in Unity3d?如何在 Unity3d 中将纹理格式从 Alpha8 更改为 RGBA?
【发布时间】:2020-11-22 11:36:21
【问题描述】:

我一直在尝试将格式从在 Alpha8 中提供纹理的相机更改为 RGBA,但到目前为止没有成功。

这是我尝试过的代码:

   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;
        }
    }

我这样调用代码:

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

但图像已损坏且不可见。

有谁知道如何将此 Alpha8 更改为 RGBA 以便我可以像处理 OpenCV 中的任何其他图像一样处理它?

【问题讨论】:

    标签: unity3d texture2d


    【解决方案1】:

    朋友给了我答案:

        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();
    

    这将占用大量资源,但肯定会奏效。 如果您知道进行此更改的更好方法,请在此线程中添加答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      相关资源
      最近更新 更多