【问题标题】:Unity Convert Texture to Texture2d takes a lot of time in android deviceUnity Convert Texture to Texture2d在android设备上需要很多时间
【发布时间】:2020-11-12 16:28:18
【问题描述】:

我有一个函数可以将 RGB565 Texture2d 转换为 RGB24Texture2d。

我使用 Texture2d.ReadPixs() api 并成功实现,但是当它在 android 上运行时,在 Texture2d 上需要 50 毫秒 .ReadPixs() 读取的大小仅为640*480

我找到了一个解决方案 here(使用 glReadPixels),但是当从 Android Studio 编译 so 文件时,它无法统一运行。我不擅长c++和Android Studio...

还有其他解决办法吗?

提前谢谢!

 public static void textureToTexture2D(Texture texture, Texture2D texture2D)
 {
     if (texture == null)
         throw new ArgumentNullException("texture == null");
     if (texture2D == null)
         throw new ArgumentNullException("texture2D == null");
     if (texture.width != texture2D.width || texture.height != texture2D.height)
         throw new ArgumentException("texture and texture2D need to be the same size.");
     RenderTexture prevRT = RenderTexture.active;
     if (texture is RenderTexture)
     {
         RenderTexture.active = (RenderTexture)texture;
         
         texture2D.ReadPixels(new UnityEngine.Rect(0f, 0f, texture.width, texture.height), 0, 0, false);
         texture2D.Apply(false, false);
     }
     else
     {
         RenderTexture tempRT = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32);
         Graphics.Blit(texture, tempRT);
         RenderTexture.active = tempRT;
        //it takes the most time on the function about 50ms
         texture2D.ReadPixels(new UnityEngine.Rect(0f, 0f, texture.width, texture.height), 0, 0, false);
         texture2D.Apply(false, false);
         RenderTexture.ReleaseTemporary(tempRT);
     }
     RenderTexture.active = prevRT;
 }

【问题讨论】:

  • Texture2D texture2d = (Texture2D) texture; 完成 ;)。那么......你真的想在这里做什么?复制像素?在格式之间转换?我很困惑。
  • 对不起。我想要的是在格式之间进行转换(从 rgb565 到 rgb24)。过程是 Texture2d(rgb565)->Texture->RenderTexture->Texture2d(RGB24).ReadPixs() 但是 ReadPixs Cost too manytime。有用的方法是从 rgb565 textur2d 获取数据,然后重置数据位置,但是这个 texture2d 是由 Texture2d.CreateExternalTexture() 创建的,我无法从 Texture.GetPixs() 或 Texture.GetRawTextureData() 获取任何数据。
  • 转换小纹理的工作流程过于复杂。看看texture.GetPixels() 方法 - 它为您完成了大部分转换工作。

标签: android unity3d textures texture2d


【解决方案1】:

您也可以尝试使用Graphics.CopyTexture

public Texture2D toTexture2D(RenderTexture rTex)
{
    Texture2D dest = new Texture2D(rTex.width, rTex.height, TextureFormat.RGBA32, false);

    Graphics.CopyTexture(renderTexture, dest);

    return dest;
}

纹理格式应兼容(例如,TextureFormat.ARGB32RenderTextureFormat.ARGB32 兼容)。

您需要检查您的目标设备是否支持此功能。

某些平台可能不具备所有类型的纹理复制功能(例如,从渲染纹理复制到常规纹理)。查看CopyTextureSupport,并使用SystemInfo.copyTextureSupport查看。

【讨论】:

  • 这是一个很好的建议,但不幸的是我的应用程序正在电视上运行,SystemInfo.copyTextureSupport = none =-=。
【解决方案2】:

为什么不先尝试一些简单的事情:

Texture2D rgb565 = LoadMyTextureOrSomething();
Texture2D rgb24 = new Texture2D(
    width:          rgb565.width ,
    height:         rgb565.height ,
    textureFormat:  TextureFormat.RGB24 ,
    mipChain:       rgb565.mipmapCount!=1
);
rgb24.SetPixels( rgb565.GetPixels() );
rgb24.Apply();// sends changed texture to gpu

其中一些工作可以在工作线程(颜色转换部分)上完成,但需要手动byte[]NativeArray<byte> 处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 2015-02-19
    • 1970-01-01
    • 2020-03-19
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多