1

So, I've created a program with Kinect as its input. As you know, Kinect will send the data 30 frame per second.

I have a model that will mimic Kinect's input motion, so on Update() I read the motion from Kinect.

The problem is, on Update(), I've done a heavy computational algorithm. It makes the system lag and freezing. I've tried to do the calculation every 60 frames instead of each frame, but the system still got lag.

void Update(){
        ...
        //GET DATA FROM KINECT
        frame = new SkeletonRecorded();
        frame.root.rotation = skeletons[kinect2SensorID, playerID].root.rotation;
            ...
        frame.rightShoulder.position = skeletons[kinect2SensorID, playerID].rightShoulder.position;
        frames.Add(frame);
        if (frames.Count >= 60) {
            //CALLING THE CALCULATION METHOD (Here is the heavy part begin)
            comparatorClass.GetSkeletonData(frames);
            frames.Clear();
        }

}

The above code is just small parts of the Update() method (I cut it short for convenience). So, how should I call the GetSkeletonData() method without making the interface lag?

Richard
  • 11
  • 3
  • I'm not entirely sure but coroutines can be a solution for your problem. Here you can find details: http://docs.unity3d.com/Manual/Coroutines.html – Paweł Marecki May 05 '16 at 09:55
  • Coroutines are still executed in the main thread. So there won't be any performance gain since no parallelism takes place. But in general coroutines could be used to develop the every-second-instead-of-every-frame-thingy. Endless loop with waitforseconds(1) and the algo in between. – M156 May 05 '16 at 10:51
  • 2
    I've had similar issues using Kinect, though usually in my experience my bottleneck is in copying the video/depth frames into textures. You may find the approaches in the question "How to not freeze the main thread in Unity?" useful for this. Also, I notice you're using the older Kinect API. If you have access to the hardware to use the new Kinect (including Win8+, high-bandwidth USB3), I highly recommend it - both the hardware and software are much friendlier to work with! – DMGregory May 05 '16 at 11:47

1 Answers1

2

Handle it on another thread.

http://www.dotnetperls.com/thread

Extremely simple example. Can also look into Async and BackgroundWorker. But this would be a way to start.

using System;
using System.Threading;

bool skeletalComputationDone = false;
Thread skeletalComputationThread;

// main game update loop
Update()
{
    // ... other game stuff ...

    // if computation done handle it then start again
    if(skeletalComputationDone)
    {
        // .. handle it ...
        skeletalComputationDone = false;
        skeletalComputationThread = new Thread(new ThreadStart(SkeletalComputationThread));
        skeletalComputationThread .Start();
    }

    // ... more game stuff ...
}

// thread where to do skeletal computation
void SkeletalComputationThread()
{
     // ... massive computation

     skeletalComputationDone = true;
}
Shmoopy
  • 69
  • 3