1

I'm using Intel Real Sense as camera device to capture picture. The capture result is displayed as a RenderTexture. Since I need to sent it via UDP, I need to convert it to byte[], but it only work for Texture2D. Is it possible to convert RenderTexture into Texture2D in unity 2019?

Edit: Right now, I'm using this code to convert RenderTexture to 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;
}

I got this code from here, which doesn't work anymore for unity 2019 since if I display the texture it only give me white texture.

Edit 2: Here how i called that function:

//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());
}

then i'll send it via network like this :

IEnumerator SendUdpPacketVideo()
{
        ...
        CurrentTexture.SetPixels(WebCam.GetPixels());
        byte[] PNGBytes = CurrentTexture.EncodeToPNG();
        ...
}

On receiver side, i'm gonna decode it and display on raw image:

....
Textur.LoadImage(ReceivedVideo);
WebCamReceiver.texture = Textur;
...
4
  • What is your device? Nov 13, 2019 at 19:35
  • Could you add your code please?
    – derHugo
    Nov 13, 2019 at 19:36
  • @0xBFE1A8 i plan to create a windows program. Nov 13, 2019 at 19:40
  • @derHugo I haven't found a working code yet to convert rendertexture to texture2D. I'll post the nonworking one, wait. Nov 13, 2019 at 19:41

1 Answer 1

0

The most optimized way to do this is:

public Texture2D toTexture2D(RenderTexture rTex)
{
    Texture2D dest = new Texture2D(rTex.width, rTex.height, TextureFormat.RGBA32, false);
    dest.Apply(false);
    Graphics.CopyTexture(rTex, dest);
    return dest;
}
7
  • Thank you for your help. The code works better than before but it still did not display the texture. Only a grey display like this : i.imgur.com/2DUDMHy.jpg Nov 13, 2019 at 22:00
  • How exactly do you call this method? Nov 13, 2019 at 22:06
  • i'm using IEnumerator to generate frame by frame and set it on RawImage as texture. Nov 13, 2019 at 22:11
  • I've added it on the post. Nov 13, 2019 at 22:42
  • @0xBFE1A8 do you not maybe have to Apply after Graphics.Copy? Why doing it before? The docs also says the Data formats have to match .. but afaik by default RenderTexture is 24-bit RGB and has no alpha channel except OP changed that.
    – derHugo
    Nov 14, 2019 at 5:31

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.