Skip to main content
New Participant
July 7, 2014

IP Camera and 2D Texture

Hello,

I'm using Unity 4.5 and Oculus Rift DK1. My team and I have succeeded in importing a live IP cam view into unity using this code:

using UnityEngine;
using System.Collections;

public class SEMTEX : MonoBehaviour {

public string uri = "http://192.168.1.101/snapshot.cgi";
public string username = "admin";
public string password = "admin";


public Texture2D cam;


public void Start() {
cam=new Texture2D(1, 1, TextureFormat.RGB24, true);
StartCoroutine(Fetch());
}


public IEnumerator Fetch() {
while(true) {
Debug.Log("fetching... "+Time.realtimeSinceStartup);

WWWForm form = new WWWForm();
form.AddField("dummy", "field"); // required by WWWForm
WWW www = new WWW(uri, form.data, new System.Collections.Generic.Dictionary<string,string>() { // using www.headers is depreciated by some odd reason
{"Authorization", "Basic " + System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(username+":"+password))}
});
yield return www;

if(!string.IsNullOrEmpty(www.error))
throw new UnityException(www.error);

www.LoadImageIntoTexture(cam);
}
}


public void OnGUI() {
GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), cam);
}

}


The last thing we are trying to get is the Oculus Rift to look through the camera itself. But when we launch the .exe, it seems to override any settings we gave it to use the OVR. Any idea what is happening?
    This topic has been closed for replies.

    5 replies

    Inspiring
    July 7, 2014
    You can try drawing GUI textures to the left and right eye cameras or, better yet, draw it with OVRGUI.StereoDrawTexture(..). OVRMainMenu does something similar with OVRGUI.DrawStereoBox(..) to draw the FPS and other info to a stereo rectangle. Hope that helps.
    AMD Ryzen 7 1800X | MSI X370 Titanium | G.Skill 16GB DDR4 3200 | EVGA SuperNOVA 1000 | Corsair Hydro H110i Gigabyte RX Vega 64 x2 | Samsung 960 Evo M.2 500GB | Seagate FireCuda SSHD 2TB | Phanteks ENTHOO EVOLV
    Up and Coming
    July 12, 2014
    Frame rate is so important in the Rift and www.LoadImageIntoTexture() is very slow, why not update via native code using cam.GetNativeTextureID ?
    Check out my Unity VR development blog: http://talesfromtherift.com @peterept
    New Participant
    September 22, 2014
    Hi. I tried to use this code in order to get video from IP camera so I could use it in Unity. However, I get HTTP error 400 (Bad Request). Im using Unity 3D version 4.5.2f1.

    With WebCamTexture I can get video froma web cam, but IP camera seems to be a lot trickier. Any ideas what could be wrong?
    March 29, 2018
    Does my server feeding the video have to be set up a certain way? I am trying to do this as well but no success so far, also why are you adding a form field 
    Up and Coming
    March 31, 2018
    Well this thread is a blast from the past.

    You can certainly download image by image, which is pretty straight forward to code up using a download and Texture2D loading a PNG or JPG.

    But, these days you can use hardware decoding video to play it back instead. You could buy an asset like the fantastic AVPro, or similiar, Or, you could use a native Android Webview to open a website and render streaming videos (however, the current plugins all seem to have disappeared - but it's possible to roll your own using WebChromeClient and native texture into Unity3d).
    Check out my Unity VR development blog: http://talesfromtherift.com @peterept