High video resolution and autofocus in Unity3D games built with ARToolKit

If you are creating a game with ARToolKit on Unity you most likely use the Unity 3D plugin from the ARToolKit.org website. Unfortunately, that release is a bit old and does not contain such features like autofocus on Android. Also, the default video resolution when you run your game is 320×240 which leads to a very bad user experience like jittering (shaking) of the 3D model you want to put on the ARMarker. So I’m going to show you how you can fix that very quick.

Autofocus

Luckily the autofocus feature has already been implemented into the plugin and is available on GitHub. Lucky again it was already merged to the master branch as well. So all we need to do is to clone the ARUnity5 Git repository and build the source code from there.

Build UnityARPlayer

To build the ARPlayer we need to follow these steps:

  1. Clone the repository into an empty directory
    1. git clone https://github.com/artoolkit/arunity5.git
  2. Start AndroidStudio (Tested with 2.2)
  3. Select Open an existing Android Studio project
    1. Screenshot on how to open ASPlayer project
    2. You may need to fix the Build Tools version depending on what version you have installed
  4. On the right of the Android Studio window there is a little Gradle-tab. You need to select that:
    1. Screenshot gradle tab
  5. Then you need to open the section for UnityARPlayer/Tasks/other/jarRelease
  6. The jar file is created into \[Git-repository-directory]/src/Android_Unity_Player_Source/AndroidStudioProj/UnityARPlayer/build/libs/UnityARPlayer.jar
  7. Copy that to your UnityProject/Assets/Plugins/Android and replace the existing version. (You might want to take a copy for safety 🙂 )

More detailed information about building the UnityARPlayer can be found here.

Video resolution

As for the video resolution, you would need to access the preference activity which comes with the ARPlayer. Sadly, due to the lack of the system menu button in the modern phones and the full-screen/immersive mode of the Unity games, that is hidden. Additionally, for a great game I can imagine that you do not want to bother the user with selecting a video resolution. To solve that I propose to set the video resolution to a fixed value that suits you best. For a simple example, I enhanced the UnityARPlayer code to select the highest video resolution available on the phone. That code can be found on my GitHub repository.

//Look for highest supported preview size and use that one.
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();

for (Camera.Size size: previewSizes) {
    Log.d("CameraSurface","PreviewSize: " + size.width + "x"+ size.height + "/n");
}

Camera.Size highestPreviewSize = parameters.getSupportedPreviewSizes().get(0);
Log.d("CameraSurface","PreviewSize: " + highestPreviewSize.width + "x"+ highestPreviewSize.height + "/n");
parameters.setPreviewSize(highestPreviewSize.width,highestPreviewSize.height);

If you now build the UnityARPlayer again and replace it in your project you should have a crystal clear video and the autofocus function.

Conclusion

Most of the features you need for a great game with ARToolKit, Unity3D and Android are already there, but somehow hidden and require manual build steps to enable them. You now have the knowledge to find and use those features which enables you to create great games with ARToolKit and Unity 3D.

Please let me know if that works for you as good as it works for me and tell me your tricks with Unity 3D and ARToolKit.

Bonus

If you read till the end here is a little bonus for you ;). I already created a UnityARPlayer.jar (deprecated see Update 1) that has the above features enabled. So go ahead and use that if you like.

 

Update 1:

Thanks to all the community feedback we found out that not all phone treat the video resolution the same way. Because of that, I added a for-each loop to loop over the available video resolutions and pick the one with the highest video-width. I hope that this solves the issues for all of you.

Thanks so much for your feedback. Here is the new UnityARPlayer.jar (Deprecated see Update 2) file for you.

 

Update 2:

I realized that the app sometimes crashes when put to the background and back to active again. UnityARPlayer.jar second update fixes this issue.

The code changes can be seen here: https://github.com/ThorstenBux/arunity5/commit/804741d437eb1b444ac6c553e25e2467971e7fe3

Happy coding

66 Comments

  1. sergeyko86 November 20, 2016
    • Thor_Bux November 20, 2016
      • sergeyko86 November 21, 2016
        • Thor_Bux November 24, 2016
  2. sergeyko86 November 21, 2016
    • Thor_Bux November 24, 2016
  3. sergeyko86 November 23, 2016
    • Thor_Bux November 24, 2016
  4. Hilo November 24, 2016
    • Thor_Bux November 24, 2016
  5. Sharbel Merheb November 25, 2016
    • Sharbel Merheb November 25, 2016
  6. Hilo November 27, 2016
    • Thor_Bux November 27, 2016
      • Hilo November 29, 2016
  7. Amit November 28, 2016
  8. Sharbel Merheb November 28, 2016
  9. Amit November 30, 2016
    • Thor_Bux November 30, 2016
    • Shadab January 5, 2017
  10. Amit December 1, 2016
  11. sergeyko86 December 4, 2016
  12. sergeyko86 December 4, 2016
  13. sergeyko86 December 4, 2016
    • kru_narin April 18, 2017
  14. sergeyko86 December 4, 2016
  15. Shadab January 6, 2017
  16. Destoo March 15, 2017
  17. zidaine March 31, 2017
    • Thor_Bux April 1, 2017
  18. Gerardo Ezequiel April 4, 2017
    • Shakipu April 5, 2017
    • Rupesh April 13, 2017
      • Thor_Bux April 14, 2017
  19. ridho May 4, 2017
  20. Thomas May 20, 2017
    • Thor_Bux May 22, 2017
  21. andy May 23, 2017
    • Thor_Bux May 23, 2017
      • andy May 24, 2017
        • Thor_Bux May 24, 2017
  22. andy May 23, 2017
    • Thor_Bux May 23, 2017
  23. MARKUS June 2, 2017
    • Thor_Bux June 7, 2017
  24. musyaffa gani June 7, 2017
  25. lbrandsaccess.com June 16, 2017
  26. Zuokun OUYANG July 4, 2017
  27. Yaroslav July 7, 2017
    • Thor_Bux July 9, 2017
      • Zuokun OUYANG July 10, 2017
        • lilyzhou November 23, 2017
  28. urfx August 5, 2017
    • Thor_Bux August 10, 2017
      • urfx August 12, 2017
        • urfx August 13, 2017