AR - World Tracking Camera (Premium Only)
CreatiCode last edited by creaticodeta2-mail
In Augmented Reality (AR) projects, you display the images from the real camera on your device, then overlay virtual 3D objects on top of it. It will look as though the virtual objects are part of the real scene.
This article will talk about a kind of AR called “World Tracking AR”, which means your program will try to keep track of the world as you move your device (phone or touchpad). For example, you can overlay a virtual tree on the camera view of an empty floor.
To use the world tracking AR camera, you need to initialize an empty 3D scene, then add this block:
The empty scene contains a default camera. This block will replace that camera with a new camera, which is linked to the real camera on the device you are using. After setting up this link, whenever you move or turn the device, the virtual camera in the 3D scene will move or turn in the same way.
There are 2 input parameters for the “switch to AR world camera” block:
Scale: this value controls the scaling ratio between the positions of the real camera and the virtual camera. For example, suppose the real camera is 2 feet away from the ground/floor. If the scale is 1, then the virtual camera will also be 2 units away from the ground in the 3D virtual scene. Specifically, its position will be (x=0, y=0, z=2). However, if the scale is 100, then the virtual camera will be 200 units away from the ground in the virtual scene, with a position of (x = 0, y = 0, z = 200).
Emulation Mode: the emulation mode is designed for debugging/development purposes. When you choose “Yes”, the real camera will not be connected, and you will still see the blue background of the empty scene. This makes it faster to develop and test your code. When you are ready to release your project for others to play with it, you can change this input to “No”, so that the images from the real camera will be shown in the background.
Now let’s walk through a simple example.
Create a new project with these 4 blocks. It will run in the emulation mode, so you can run it on any device (desktop computer, laptop, Chromebook, touchpad or smartphone).
Since the code is running in emulation mode, you will get an empty scene like this. As you drag the mouse pointer, the camera will turn to different angles.
The following animation illustrates the camera’s view point. You can see its position is X=0, Y=0, and Z=200 (because of the scale of 100), and it is looking down at a 45-degree angle.
Now let’s add a tree in front of the camera, in the same way as how you develop a normal non-AR project.
As you rotate the camera, the tree and the 3D axis will move in the opposite way, because they are staying where they are.
Lastly, we are ready to run the project on a mobile device with a rear-facing camera, such as a smartphone or a touchpad.
Change the “emulation mode” to “No”, then open this project in a browser on the mobile device.
Now you will see that the blue background is replaced with the images from the real camera. As you rotate or move the device, the tree will be automatically adjusted by the system in real-time, so that it looks as if the tree is staying on the same spot on the floor.