- Home /
WebcamTexture not refreshing
Hey everyone,
This is an extremely frustrating issue because it is such a simple setup.
When working with a webcam I noticed that its refresh rate was much lower than the 30FPS I requested in code. Not a big surprise, but I've determined that the frame rate is somehow connected to the amount of code running in the scene.
Not like how you are thinking either... when I remove all other code from the scene and just start up a webcamtexture, like this:
WebCamTexture curentView = new WebCamTexture(WebCamTexture.devices[0], 1280, 720, 30);
currentView.Play();
renderer.material.mainTexture = currentView;
I get a reasonable refresh rate on the view for about 0.5-1 seconds and then it freezes. After it freezes it sometimes updates once every 5-10 seconds and sometimes never again. As I said, this is with no other code running in the scene at all.
Now when I add the following update function:
void Update() {
int j = 0;
for(int i = 0; i < 10000000; i++) {
j += i;
}
}
I get between 3 and 8 FPS from the webcam (I used another piece of code added later to calculate those values from currentView.didUpdateThisFrame being true or false). This was tested with 2x an external camera (same model) and 1x the camera included above my laptop screen. I've also had this issue on more than one computer.
Any ideas on how to get a more consistent frame rate from webcams? Better yet, any ideas on how to not have your webcam freeze up when not running much code?
Thanks in advance.
Using Unity Pro 4.3.3f1
This problem seemed to go away in our code, but it had only disappeared because we were doing a few things really inefficiently. Now that we've optimized things and are getting very high FPS the webcam stuff is freezing up again.
Have you tried changing the target FPS of the webcam texture to see if it effects the freezing at all?
Good question. Yes I have, however it does not offer any improvement that I noticed. I'll try again later this afternoon to see if the actual delays (5-10 seconds) are the same or different... meaning I will collect actual ti$$anonymous$$g numbers for comparison.
Alright, took me a few days but I put together a program that starts the camera, watches for 30 seconds and counts the number of frames where didUpdateThisFrame was true, and then divided by the capture time to get FPS. I had it run this routine 5 times for each of the following framerates (none- didn't pass the FPS argument, 1, 5, 15, 30)
These are the numbers (actual frame rates from the camera) that it found: None, didn't pass FPS argument - 7.2999 0.0331 0.0333 0 4.2105
1 FPS requested - 0 0.0333 0 0.0662 0
5 FPS requested - 0 0 0 0 0
15 FPS requested - 0.0333 0 0 0 0
30 FPS requested - 0 0.0333 0 0 0
I will also try running the same routine but include the counting for loop in each Update call to slow things down.
Hah, this is funny... When I run it with the following loop in the Update routine: for(int i = 0; i < 1000000; i++) { i = i + 1 - 1; }
I get the following actual framerates:
None req, didn't pass arg - 7.465 7.397 7.398 7.365 7.364
1 FPS requested - 7.364 7.398 7.399 7.399 7.365
5 FPS requested - 7.398 7.398 7.398 7.265 7.365
15 FPS requested - 7.399 7.366 7.399 7.399 7.366
30 FPS requested - 7.498 7.366 7.365 7.399 7.398