如何在 Unity3d 中将纹理格式从 Alpha8 更改为 RGBA? [英] How to change texture format from Alpha8 to RGBA in Unity3d?

查看:65
本文介绍了如何在 Unity3d 中将纹理格式从 Alpha8 更改为 RGBA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我试过的代码:

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

但图像已损坏且不可见.

But the image gets corrupted and isn't visible.

有谁知道如何将这个 Alpha8 更改为 RGBA 以便我可以像处理 OpenCV 中的任何其他图像一样处理它?<​​/p>

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

推荐答案

一个朋友给了我答案:

    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.

这篇关于如何在 Unity3d 中将纹理格式从 Alpha8 更改为 RGBA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆