Use one webcamtexture on multiple gameobject

300 Views Asked by At

I was trying This Page to map Theta S camera to the sphere.

enter image description here

There are two half-spheres. The mapping is done by shaders, so I don't have to care about this. Both piece of sphere has same code: public int cameraNumber = 0; private WebCamTexture webcamTexture;

void Start() 
 {
     WebCamDevice[] devices = WebCamTexture.devices;
     if (devices.Length > cameraNumber) {
         webcamTexture = new WebCamTexture(devices[0].name, 1280, 720);
         GetComponent<Renderer>().material.mainTexture = webcamTexture;
         webcamTexture.Play();
     } else {
         Debug.Log("no camera");
     }
 }

What's the problem is, only "sphere1" renders WebCamTexture. I tested with additional test plane, and only the plane rendered WebCamTexture. (Both spheres didn't).
It seems only one gameobject can render one WebCamTexture. How can I render one WebCamTexture on multiple gameobject?

I already tried This and This, but both didn't work.
In This Page, it suggested a solution in one-line: enter image description here Which I couldn't understand. Can anyone help me?

1

There are 1 best solutions below

0
On

If I understand your code correctly, you are creating twice a WebcamTexture (one for each Sphere object). Webcams can in general only be used as a single instance.

I would try to create a WebCamTexture object in a different script such that you get a single texture. Then you can try to assign that to the materials of your sphere.

I am not sure why one of the links you give use Resources.Load to get a reference to the material; which could also have been assigned through the inspector. (But you are not using that right now).

You could also try 'isPlaying' on the WebCamTextures to see if both return true.