我正在使用 Intel Real Sense 作为相机设备来捕获图片。捕获结果显示为RenderTexture
。由于我需要通过UDP
发送它,因此我需要将其转换为byte[]
,但它仅适用于Texture2D
。可以将RenderTexture
转换为Texture2D
并在2019年统一吗?
编辑: 现在,我正在使用此代码将RenderTexture转换为Texture2D:
Texture2D toTexture2D(RenderTexture rTex)
{
Texture2D tex = new Texture2D(rTex.width, rTex.width, TextureFormat.ARGB32, false);
RenderTexture.active = rTex;
tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
tex.Apply();
return tex;
}
我从here获得了这段代码,该代码不再适用于统一2019,因为如果我显示纹理,它只会给我白色纹理。
修改2: 在这里,我如何调用该函数:
//sender side
Texture2D WebCam;
public RawImage WebCamSender;
public RenderTexture tex;
Texture2D CurrentTexture;
//receiver side
public RawImage WebCamReceiver;
Texture2D Textur;
IEnumerator InitAndWaitForWebCamTexture()
{
WebCamSender.texture = tex;
CurrentTexture = new Texture2D(WebCamSender.texture.width,
WebCamSender.texture.height, TextureFormat.RGB24, false, false);
WebCam = toTexture2D(tex);
while (WebCamSender.texture.width < 100) //WebCam
{
yield return null;
}
StartCoroutine(SendUdpPacketVideo());
}
然后我将通过网络将其发送:
IEnumerator SendUdpPacketVideo()
{
...
CurrentTexture.SetPixels(WebCam.GetPixels());
byte[] PNGBytes = CurrentTexture.EncodeToPNG();
...
}
在接收方,我将对其解码并在原始图像上显示:
....
Textur.LoadImage(ReceivedVideo);
WebCamReceiver.texture = Textur;
...
答案 0 :(得分:0)
最优化的方法是:
public Texture2D toTexture2D(RenderTexture rTex)
{
Texture2D dest = new Texture2D(rTex.width, rTex.height, TextureFormat.RGBA32, false);
dest.Apply(false);
Graphics.CopyTexture(renderTexture, dest);
return dest;
}