-
Notifications
You must be signed in to change notification settings - Fork 23
Laying out your AR scene and the role of ARXOrigin
artoolkitX for Unity has a flexible model of scene layout. While the ARXTrackable
component collects the raw transform between the physical camera and the marker, it is the combination of the ARXOrigin
, ARXTrackedObject
, and ARXCamera
components that apply this transform to move game objects and the Unity camera around in the Unity scene.
artoolkitX also supports a simplified model of scene construction which is suitable for use only with a single marker via the ARXTrackedCamera
component. It does not use the ARXOrigin
component. That will be discussed separately later, below.
A simple scene is constructed by placing an ARXOrigin
component at the root of the portion of the scene containing our AR content. E.g., as in the "SquareSample" scene provided with the "Samples" folder:
ARXOrigin
has one parameter configurable in the Unity Editor, "ARXTrackable find mode". In the default "Auto all" setting, the first trackable detected will be treated as the origin of the AR coordinate system for the purposes of representing the local pose (position and orientation) of tracked objects and the camera in the scene. If it is lost, and another trackable found, the second trackable will become the origin. This is the most simple and flexible behaviour.
ARXOrigin
by itself does nothing; it just designates the origin point of the scene and selects a trackable. The actual movement of the Unity camera around in the scene is achieved by attaching an ARXCamera
component to the Unity camera:
Each tracking frame, ARXCamera
updates the pose of the camera to which it is attached so that the spatial relationship between it and the ARXOrigin matches exactly the relationship between the real-world webcam and marker.
Usually, which trackable or marker is considered the origin for scene layout purposes is not critical; so long as at least one trackable is in view of the webcam, the desired behaviour is usually to update the virtual camera pose. However, in some circumstances, you might want a particular trackable to be the origin. E.g., you might be using a multi-marker (multiple square markers) as a base, as in this "Magic Lens" example:
When you wish only a particular trackable (or any one of a set of particular trackables) to be eligible to become the base marker for the purposes of camera positioning, you can change ARXOrigin
's "Find mode" to "auto by tag", and enter the string tag(s) of one or more ARXTrackable
s which can act as the origin. If more than one is provided, they will be searched for in order (so the second entry in the list will only be used if the first cannot be found, and so on).
Note that when "auto by tag" is selected, and none of the trackables can be seen, then other trackables visible to the webcam will not be updated.
There is a third mode for ARXOrigin
's find mode, "Manual". In this mode, it is up to the user to programmatically set the trackable using methods ARXOrigin.AddTrackable(ARXTrackable trackable, bool atHeadOfList = false)
some time after Start()
has been called.
Generally, the main thing you want to use AR tracking for is to move objects around in the scene, not just the camera. To have a game object's movement associated with the pose of an AR trackable, add an ARXTrackedObject
component to it:
Note the configuration of the ARXTrackedObject
; it will take its pose from the ARXTrackable
which it finds using the "tag" string (n.b. this "tag" just refers to the field of the same name in ARXTrackable
, and is unrelated to Unity's own gameobject tag system). In this case, this object will be linked to the ARXTrackable
which has the tag "hiro":
When there is a single tracked object in the scene, at runtime its pose will be set to 0 relative to the ARXOrigin.
The power of the ARXOrigin/ARXCamera/ARXTrackedObject combo comes from when there is more than one trackable in the scene. In this case, objects which are not the "base trackable" will have their pose set relative to the base trackable. This makes it simple to calculate their relative pose in the scene.
For simple scenes involving just a single tracking target, there is an alternative to using the ARXOrigin/ARXCamera/ARXTrackedObject combo. In such scenes, you can just attached an ARXTrackedCamera
component to the Unity camera and leave ARXOrigin and ARXTrackedObject out of the scene. In this case, the ARXTrackedCamera
will directly update the pose of the camera relative to the trackable linked by the "tag" parameter.
While this approach is less flexible, it might suit some simple projects.