During one of the projects in my company I needed to build a native plugin to let Unity communicate with the Asus Xtion2, specifically to get its depth data. We used to be able to do this pretty easily with the Kinect but since Microsoft discontinued it, we need to start looking for alternatives.
Test Environment
- Windows 10 64 bit.
- Unity 2017.2.0f3 x64. Important! Choose x64 or x86 to match your Unity installation.
- OpenNI For Xtion2 SDK. The official SDK is somewhat different from the OpenNI SDK provided by Asus but it should behave the same. The one provided by Asus can be downloaded here. Make sure you choose the latest one.
- CMake 3.0 or higher
Who is it for
Someone who has been using Unity for some time and is comfortable with the concept of classes and objects. It will be very helpful if you know C++ and pointers too.
Steps
Since the code is provided, I will only go over the major steps. Let me know in the comments if I miss anything.
- Build and install OpenNI. Since there are already many tutorials on building and installing libraries I will not go into the details here.
- Assuming OpenNI is installed in {OPENNI_INSTALL_PATH}, make sure that you have {OPENNI_INSTALL_PATH}/lib/OpenNI2.lib and you can also run the samples located in {OPENNI_INSTALL_PATH}/Samples/Bin
- You can find the code for the native plugin here.
- The Unity project can be found here.
- Both lack documentation so in the meantime please check out the code and let me know in the comments if anything is unclear.
- You can find the CMakeList file for the native code under src/ folder.
- Adjust the install paths and parameters to suit your environment and build with CMake.
- If everything goes well with CMake you should see a solution (.sln) file in the build folder
- Open this solution file with Visual Studio and build the project.
- Don't forget to set the build settings to either Debug or Release and make sure to build x86 or x64 according to your architecture.
- If the build is successful, you should see an XtionCapture.dll in the build folder.
- Copy this dll to the Unity project in Assets/Plugins.
- You will also need to copy some file from the OpenNI SDK. Go to {OPENNI_INSTALL_PATH}/Redist and copy all the files there into the Assets/Plugins folder.
- If you managed to come this far then just press Play and enjoy!
Overview of plugin interface
The details can be found in the code but I'd like to share the idea behind the plugin.
- Instantiate a new capture class (xtion_capture) and let this class hold references to the Xtion Device. So pointers to device handlers, depth frames etc. are registered as this class' members.
- When xtion_capture is instantiated it returns a pointer to its object instance to Unity. This pointer is how Unity calls the capture class and retrieves data from its (member) pointers.
The above design is illustrated below.
Future work
I will definitely want to add documentations, and clean up the repository. If I have the time I'd try getting the color image too. Best of luck and I would be happy to help of anyone needs to have the same system setup.
Comments
Post a Comment