// Copyright (c) Mira Labs, Inc., 2017. All rights reserved.
//
// Downloading and/or using this MIRA SDK is under license from MIRA,
// and subject to all terms and conditions of the Mira SDK License Agreement,
// found here: https://www.mirareality.com/Mira_SDK_License_Agreement.pdf
//
// By downloading this SDK, you agree to the Mira SDK License Agreement.
//
// This SDK may only be used in connection with the development of
// applications that are exclusively created for, and exclusively available
// for use with, MIRA hardware devices. This SDK may only be commercialized
// in the U.S. and Canada, subject to the terms of the License.
using Mira;
using System.Collections;
using UnityEngine;
using Wikitude;
using UnityEngine.SceneManagement;
using UnityEngine.XR.iOS;
///
/// All Mira settings for Wikitude Tracking, including positional offsets, tracking scaling, and image preview scaling
/// This Script should only be used in only when using Image Tracking.
/// The ArCamera should be accessed by the Instance.
///
public class MiraWikitudeManager : TransformOverride
{
#region Public Variables
///
/// refrence to the Wikitude Camera Script.
///
[HideInInspector]
public WikitudeCamera ArCam = null;
///
/// Set this to the gameObject that you would like to act as the Orientation Tracking Main Parent.
///
public GameObject headOrientation3DOF;
///
/// Attach the GameObject that contains the all elements in the scene that do not depend on Tracking.
///
public ImageTrackable imageTracker;
#endregion Public Variables
#region PrivateVariables
private static MiraWikitudeManager instance = null;
private float scaleMultiplier;
private Quaternion rotationalOffset;
private Vector3 positionalOffset;
private RotationalTrackingManager rotationalTracking;
private bool flag = false;
#endregion PrivateVariables
#region Properties
///
/// Any public method or variable should be accessed through the instance of the class.
///
/// The instance.
public static MiraWikitudeManager Instance
{
get
{
if (instance == null)
{
instance = Object.FindObjectOfType();
}
return instance;
}
}
#endregion Properties
#region Unity callbacks
public void OnEnable()
{
}
public void Awake()
{
}
public void Start()
{
scaleMultiplier = 1 / MiraArController.scaleMultiplier;
ArCam = gameObject.GetComponent();
if (MiraArController.Instance.isSpectator == false)
{
ArCam.DevicePosition = CaptureDevicePosition.Front;
Debug.Log("Using front facing camera");
Vector2 exposurepoint = new Vector2(0.15f, 0.2f);
ArCam.ExposeAtPointOfInterest(exposurepoint, CaptureExposureMode.ContinuousAutoExpose);
rotationalOffset = Quaternion.Euler(-30, 0, 0);
// positionalOffset = new Vector3(-5.2f, -0.93599f, -5.0f) * scaleMultiplier;
positionalOffset = new Vector3(-6.006f, -2.846f, -3.689f) * scaleMultiplier;
}
else
{
Camera specCam = gameObject.AddComponent ();
ArCam.DevicePosition = CaptureDevicePosition.Back;
specCam.nearClipPlane = MiraArController.Instance.nearClipPlane * (1/MiraArController.scaleMultiplier);
//ArCam.EnableCameraRendering = true;
positionalOffset = Vector3.zero;
rotationalOffset = Quaternion.identity;
ArCam.enabled = true;
imageTracker.AutoToggleVisibility = true;
}
InstaiateRotationalHandoffManager();
SanityCheck();
}
public void Update()
{
if(!gameObject.GetComponent ().EnableCameraRendering && MiraArController.Instance.isSpectator)
gameObject.GetComponent ().EnableCameraRendering = true;
}
#endregion Unity callbacks
#region Public Methods
///
/// /// Instaiates the rotational handoff manager required for state changes from Tracking founf -> Tracking Loast and vice versa.
///
public void InstaiateRotationalHandoffManager()
{
if (headOrientation3DOF != null) {
headOrientation3DOF.AddComponent();
rotationalTracking = headOrientation3DOF.AddComponent();
}
else
{
Debug.LogError("No 3DOF Head Orientation object set in the MiraWikitudeManager inspector");
}
}
///
/// Drawable Override is used to transalte th position and rotation of the Tracking Object according to the current state of the game.
/// Only be used if Static Camera is set true on Wikitude Camera
///
/// Image or Object Trackable.
/// Identified Image or Object Target.
/// Tracker Gameobject Position.
/// Tracker GameObject Rotation.
/// Tracker GameObject Scale.
public override void DrawableOverride(Trackable trackable, RecognizedTarget target, ref Vector3 position, ref Quaternion rotation, ref Vector3 scale)
{
var imageTarget = target as ImageTarget;
if (imageTarget != null)
{
// var augmentationToCamera = position - wikiCamera.transform.position;
// float length = augmentationToCamera.magnitude;
// Vector3 direction = augmentationToCamera.normalized;
position = imageTarget.PhysicalTargetHeight * scaleMultiplier * position * 0.1f;
}
}
///
/// Camera Overrise is used to transalte th position and rotation of the Mira Ar Camera according to the current state of the game.
///
/// Image or Object Trackable.
/// Mira Ar Camera Camera.
/// Identified Image or Object Target.
/// Mira Ar Camera Position.
/// Mira Ar Camera Rotation.
/// Mira Ar Camera Scale.
// Mira Remote Code
#if UNITY_EDITOR
float remoteTargetHeight;
public void RemoteTrackingFound(float foundTargetHeight)
{
remoteTargetHeight = foundTargetHeight;
// Simulate tracking found event (rotational handoff disabled)
rotationalTracking.RemoteTrackingEvents(true);
}
public void RemoteTrackingLost()
{
// Simulate tracking lost event (rotational handoff enabled)
rotationalTracking.RemoteTrackingEvents(false);
}
public void MiraRemoteTracking(Vector3 camPosition, Quaternion camRotation)
{
transform.position = (remoteTargetHeight * scaleMultiplier * camPosition * 0.1f) + camRotation * positionalOffset;
transform.rotation = camRotation * rotationalOffset;
}
#endif
public override void CameraOverride(Trackable trackable, Transform camera, RecognizedTarget target, ref Vector3 position, ref Quaternion rotation, ref Vector3 scale)
{
var imageTarget = target as ImageTarget;
if (imageTarget != null)
{
// position = imageTarget.PhysicalTargetHeight * scaleMultiplier * position * 0.1f + rotation * positionalOffset;
// rotation = rotation * rotationalOffset;
Vector3 relativePosition = position - trackable.transform.position;
position = trackable.transform.position + imageTarget.PhysicalTargetHeight * scaleMultiplier * relativePosition * 0.1f + rotation * positionalOffset;
rotation = rotation * rotationalOffset;
}
}
///
/// Rescales the Tracker preview according to the scale.
///
/// Trackable.
/// Image target height.
/// Position.
/// Rotation.
/// Scale.
public override void ImagePreviewOverride(Trackable trackable, float imageTargetHeight, ref Vector3 position, ref Quaternion rotation, ref Vector3 scale)
{
scale = new Vector3(imageTargetHeight, imageTargetHeight, imageTargetHeight) * 0.1f * 1 / MiraArController.Instance.setScaleMultiplier;
}
///
/// This Function can be used if you wish to load the tracking after the scene starts by disabling the MyImageTracker gameobject.
///
public IEnumerator ActivateTracking()
{
yield return new WaitForSeconds(5.0f);
if (imageTracker == null)
Debug.LogError("No imageTracker set in MiraWikitudeManager inspector");
else
imageTracker.gameObject.SetActive(true);
}
#endregion Public Methods
#region Private Variables
// Make sure there are no conflicting settings
private void SanityCheck()
{
if (MiraArController.Instance.isSpectator && ArCam.StaticCamera)
Debug.LogError("Incompatible WikitudeCamera Settings - cannot be Spectator with Static Camera");
}
#endregion Private Variables
}