[英]Unity Convert Texture to Texture2d takes a lot of time in android device
我有一個 function 將 RGB565 Texture2d 轉換為 RGB24Texture2d。
我使用Texture2d.ReadPixs() api 並成功實現,但是當它在android上運行時, Texture2d.ReadPixs() 需要50 毫秒,並且讀取的大小僅為640*480 。
我在這里找到了一個解決方案(使用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;
}
您也可以嘗試使用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.ARGB32
和RenderTextureFormat.ARGB32
兼容)。
您需要檢查目標設備是否支持此功能。
某些平台可能沒有各種紋理復制的功能(例如,從渲染紋理復制到常規紋理)。 請參閱
CopyTextureSupport
,並使用SystemInfo.copyTextureSupport
進行檢查。
為什么不先嘗試一些簡單的事情:
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>
處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.