【发布时间】: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