First of all I’d like to thank the Wikitude company for the license key they provided to me. I’ll soon update the previous demo gifs to remove the trial watermarks!
In this tutorial we will use Wikitude’s image recognition feature to display a new image in our Ionic AR application.
It’s been a while since the last Wikitude tutorial so don’t hesitate to start from the beginning (here and there) if you didn’t follow the first tutorials.
Augmented Reality’s image recognition started with a very simple marker:
Nowadays we can use more complex images like the one we are going to use in this tutorial:
Wikitude Target Manager
Wikitude’s AR image recognition requires a target collection (.wtc file).
This file can be created using Wikitude’s target manager.
All we need is a new project:
Uploading the image:
And downloading the wtc file:
More images can be present in a target collection, for simplicity will only use one.
Our goal for the Ionic assets folder is to achieve the following architecture:
The images folder contains the images that will appear in our AR world. Wikitude’s image recognition only handles JPG and non transparent PNG files.
Unlike the previous Wikitude tutorials, we are going to work with TypeScript files.
Wikitude’s philosophy is all about placing the AR world’s files in the assets folder. This is great, however, Ionic will only directly copy those files without modifying them.
A new script named prerun.sh will be created to do the job:
This call will be done before Cordova runs the application. The config.xml file needs one new line:
A new Hook will automatically launch the prerun.sh script before running the application.
Wikitude AR World
We can now move on to the wc-image-recognition.ts file:
This is where our AR world starts. This file will create a new World instance and call its init method which starts as follow:
A new TargetCollectionResource object is created by using the wtc file we generated using Wikitude’s target manager.
When the camera will display the video stream, an ImageTracker will look for images present in this target collection.
The ImageResource is then created by using one JPG image, this image will be displayed in our world if the tracker finds a target.
Here are the last lines of our file:
The ImageDrawable helps us dictate how our image will be displayed relatively to the target. In our case we move it a bit and rotate it on the X axis, so it will be perpendicular to the target.
The ImageTrackable is finally created by associating the imageTracker with the drawable.
The image is displayed when the target is detected:
Wikitude’s Image Recognition is quite simple, we only need 30 lines of code. Before diving into it, a target collection is required.
We didn’t make many manipulations, however, the ImageDrawable Class offers more flexibility (z-index, opacity, etc.), don’t hesitate to read the official documentation, it’s full of content.