I want to copy real-time video from one unity to another over the Photon network.
I managed to get it working. See picture 1.
I don’t have experience in Networking and hope someone can take a quick look at my code and give me some advice to get good performance. As of right now it is very slow, inverted, and doesn’t output the right texture.
I will have a look at setting up a stream with the bolt engine.
Thank you for taking a look at my thread.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class Send : MonoBehaviour
{
public Texture MyTexture;
public Texture2D receivedTexture;
public byte[] receivedBytes;
public byte[] N;
public PhotonView PV;
public Texture2D tex2D;
void Start()
{
MyTexture = null;
PV = GetComponent<PhotonView>();
}
static public Texture2D GetRTPixels(RenderTexture rt)
{
// Remember currently active render texture
RenderTexture currentActiveRT = RenderTexture.active;
// Set the supplied RenderTexture as the active one
RenderTexture.active = rt;
// Create a new Texture2D and read the RenderTexture image into it
Texture2D tex = new Texture2D(rt.width, rt.height);
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
// Restorie previously active render texture
RenderTexture.active = currentActiveRT;
return tex;
}
void Update()
{
PV = GetComponent<PhotonView>();
if (PhotonNetwork.IsMasterClient)
{
MyTexture = GetComponent<Renderer>().material.mainTexture;
//RenderTexture.active = myRenderTexture;
//myTexture2D.ReadPixels(new Rect(0, 0, myRenderTexture.width, myRenderTexture.height), 0, 0);
//myTexture2D.Apply();
Texture2D tex2D = (Texture2D)MyTexture;
tex2D = new Texture2D(tex2D.width, tex2D.height);
StartCoroutine(GetRenderTexturePixel(tex2D));
PV.RPC("UpdateMonitor", RpcTarget.Others, N);
return;
}
else if (!PhotonNetwork.IsMasterClient)
{
if (MyTexture != null)
{
tex2D = new Texture2D(MyTexture.width, MyTexture.height);
tex2D.LoadImage(N);
GetComponent<Renderer>().material.mainTexture = tex2D;
}
}
}
IEnumerator GetRenderTexturePixel(Texture2D tex)
{
Texture2D tempTex = new Texture2D(tex.width, tex.height);
yield return new WaitForEndOfFrame();
tempTex.ReadPixels(new Rect(0, 0, 400, 300), 0, 0);
tempTex.Apply();
N = tempTex.EncodeToPNG();
}
[PunRPC]
public void ReceiveWebcamPNG(byte[] receivedByte)
{
receivedTexture = null;
receivedTexture = new Texture2D(1, 1);
receivedTexture.LoadImage(receivedByte);
GetComponent<Renderer>().material.mainTexture = receivedTexture;
}
}