- Home /
two webcams into a single rawimage side-by-side,How to have two webcams in the same rawimage side by side
Hello. I've been able to capture streams of different webcams simultaneously using the code found here https://github.com/Chamuth/unity-webcam/blob/master/MobileCam.cs and simply creating two rawimages, each one with attached a similar script. However I would need to have the two streams in just a single rawimage, side-by-side, for 3D purposes. I was thinking to copy the two rawimages in one but I don't think this is efficient and, beside, I'm new to Unity so not sure how to do it. Any suggestion?
Answer by highpockets · Feb 13, 2021 at 05:27 PM
Why can’t the WCTs be in 2 raw images right next to each other??
If you really need them to be displayed as one raw image, I think you will have to create that single texture2D at runtime every frame, but this seems like a very strange use case. Anyhow, this is untested, but I think more or less what you are trying to do:
Texture2D texture;
WebCamTexture wctOne;
WebCamTexture wctTwo;
RawImage rawCamImage;
void Start()
{
rawCamImage = GetComponent<RawImage>();
wctOne = new WebCamTexture( WebCamTexture.devices[0].name, Screen.width / 2, Screen.height, 30 );
wctTwo = new WebCamTexture( WebCamTexture.devices[1].name, Screen.width / 2, Screen.height, 30 );
StartCoroutine( StartCams() );
}
IEnumerator StartCams()
{
wctOne.Play();
wctTwo.Play();
while( wctOne.width < 100 || wctTwo.width < 100 )
yield return null;
texture = new Texture2D( wctOne.width + wctTwo.width, wctOne.height, TextureFormat.RGB24, false );
rawCamImage.texture = texture;
rawCamImage.material.mainTexture = texture;
while(true)
{
texture.SetPixels(0,0,wctOne.width,wctOne.height,wctOne.GetPixels());
texture.SetPixels(wctOne.width,0,wctTwo.width,wctTwo.height,wctTwo.GetPixels());
texture.Apply();
yield return null;
}
}
@highpockets Thank you: I will give it a try. I need the two side by side to be rendered in a single rawimage in order to exploit a Leia component that will automatically handle it to show it 3D in their 3D screens (Lume Pad for example). Normally any side-by-side image in their texture2D becomes 3D
Anyway there is something wrong (even if you comment the part about the second cam to get a single webcam working, the rawImage is empty)
I forgot to call Play().. Sorry, it was untested. I'll edit my code above
Just tested on a tablet (so there are for sure two cameras) but apparently it is not working. Even commenting the part for the second camera and testing on my pc (that has just one camera) I see the led of the camera on, but the rawImage is empty. I have tried to do as I had made in the past (assigning directly the webcam texture to the rawImage texture) and in this case of course it works. I don't know if I could have made errors since I have never used coroutines: in fact I have packed your code inside a C# script with some additional instructions to assign the rawImage and check the number of available cameras, the code I'm testing now is like that: (and attached to the rawImage, where I have also assigned the public property):
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class RenderOnRawImage : MonoBehaviour { Texture2D texture;
WebCamTexture wctOne;
WebCamTexture wctTwo;
public int numofcameras;
public RawImage rawCamImage;
void Start()
{
WebCamDevice[] devices = WebCamTexture.devices;
numofcameras = devices.Length;
GameObject.Find("Label1").GetComponentInChildren<Text>().text = "Number of cameras " + numofcameras;
wctOne = new WebCamTexture(WebCamTexture.devices[0].name, Screen.width/2, Screen.height);
wctTwo = new WebCamTexture(WebCamTexture.devices[1].name, Screen.width / 2, Screen.height, 30);
StartCoroutine(StartCams());
}
IEnumerator StartCams()
{
wctOne.Play();
wctTwo.Play();
while (wctOne.width < 100 || wctTwo.width < 100)
yield return null;
texture = new Texture2D(wctOne.width + wctTwo.width, wctOne.height, TextureFormat.RGB24, false);
rawCamImage.texture = texture;
rawCamImage.material.mainTexture = texture;
while (true)
{
texture.SetPixels(0, 0, wctOne.width, wctOne.height, wctOne.GetPixels());
texture.SetPixels(wctOne.width, 0, wctTwo.width, wctTwo.height, wctTwo.GetPixels());
yield return null;
}
}
}
Your answer
Follow this Question
Related Questions
how to check if webcam is used by another application 0 Answers
WebCamTexture does not stop 2 Answers
WebCamTexture problem 1 Answer