Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display live camera feed in Unity

I have a question regarding Unity. I hope this hasn't been answered before. I want to connect a Camera (like a HD cam) to my computer and the video feed should be displayed inside my Unity scene. Think of it like a virtual television screen, that displays what the camera is seeing in realtime. How can i do this? Google didn't point me in the right direction, but maybe I'm just unable to get the query right ;)

I hope you understand what I'm going for.

like image 632
Sören Kampschroer Avatar asked Oct 20 '13 20:10

Sören Kampschroer


People also ask

How can I see what the camera sees in Unity?

In Unity: Select the Camera (GameObject) that you would like to look through while in the Scene view. Then go to the "GameObject" Menu and select "Align View to Selected."

How do I add a camera effect in Unity?

Import the Effects Package: In order to get camera effects working, you first need to import the Effects package from Unity's Standard Assets. You can either choose to import this package when you create your game, or you can add them to your existing project by going to Assets → Import Package → Effects.

How do I follow the camera in Unity?

Making the camera following the player is quite straight forward. Add this script to your main camera. Drag the reference of the player object to the script and then you are done. You can change the values in the Vector 3 depending on how far you want the camera to be from the player.


1 Answers

Yes that certainly is possible and luckily for you Unity3D actually supports it quite well out of the box. You can use a WebCamTexture to find the webcam and render it to a texture. From there you can choose to render the texture on anything in the 3D scene, including your virtual television screen of course.

It looks pretty self explanatory but the below code should start you off.

List and print out the connected devices it detects:

var devices : WebCamDevice[] = WebCamTexture.devices;
for( var i = 0 ; i < devices.length ; i++ )
    Debug.Log(devices[i].name);

Connect to an attached webcam and send the image data to a texture:

WebCamTexture webcam = WebCamTexture("NameOfDevice");
renderer.material.mainTexture = webcam;
webcam.Play();
like image 108
S.Richmond Avatar answered Sep 28 '22 08:09

S.Richmond