CameraAnchor.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Handles the ZED camera's offset from its tracked object within the MR calibration scene.
  6. /// Provides interfaces for translating/rotating gradually or instantly, along with related
  7. /// functions like undo/redo and loading/saving.
  8. /// </summary>
  9. public class CameraAnchor : MonoBehaviour
  10. {
  11. /// <summary>
  12. /// The scene's ZEDManager instance. Should be a child of this object's transform.
  13. /// If not assigned, will be set to the ZEDManager for camera index 1.
  14. /// </summary>
  15. [Tooltip("The scene's ZEDManager instance. Should be a child of this object's transform. " +
  16. "If not assigned, will be set to the ZEDManager for camera index 1.")]
  17. public ZEDManager zedManager;
  18. /// <summary>
  19. /// ZEDControllerTracker assigned to this object. That's what keeps the object's position in sync with the real-world object.
  20. /// </summary>
  21. [HideInInspector]
  22. public ZEDControllerTracker controllerTracker;
  23. /// <summary>
  24. /// Max speed the ZED will move when calling TranslateZEDIncrementally(). This happens when using the manual translation arrows.
  25. /// </summary>
  26. [Space(5)]
  27. [Tooltip("Max speed the ZED will move when calling TranslateZEDIncrementally(). This happens when using the manual translation arrows.")]
  28. public float maxTranslateMPS = 0.05f;
  29. /// <summary>
  30. /// Max speed the ZED will move when calling RotateZEDIncrementally(). This happens when using the manual rotation rings.
  31. /// </summary>
  32. [Tooltip("Max speed the ZED will move when calling RotateZEDIncrementally(). This happens when using the manual rotation rings.")]
  33. public float maxRotateDPS = 30f;
  34. /// <summary>
  35. /// Delegate that provides a reference for a CameraAnchor, indended to be to this one.
  36. /// </summary>
  37. public delegate void CameraAnchorCreatedDelegate(CameraAnchor anchor);
  38. /// <summary>
  39. /// Event called in Start(), confirming that the anchor is ready to be used.
  40. /// </summary>
  41. public static event CameraAnchorCreatedDelegate OnCameraAnchorCreated;
  42. /// <summary>
  43. /// Index of the layer hidden from the ZED camera, preventing it from being visible in the 2D view.
  44. /// This is NOT enforced in the camera itself via script; you must exclude it from the camera's layer mask manually.
  45. /// </summary>
  46. public const int HIDE_FROM_ZED_LAYER = 17;
  47. /// <summary>
  48. /// File path to where the app loads and saves the calibrated offset values - the whole point of the MR calibration scene existing.
  49. /// </summary>
  50. private string filePath
  51. {
  52. get
  53. {
  54. string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
  55. string specificFolder = System.IO.Path.Combine(folder, @"Stereolabs\steamvr\ZED_Position_Offset.conf");
  56. return specificFolder;
  57. }
  58. }
  59. /// <summary>
  60. /// How many undo/redo poses to save before we start discarding them.
  61. /// </summary>
  62. private const int MAX_UNDO_HISTORY = 50;
  63. private CappedStack<AnchorPose> undoStack = new CappedStack<AnchorPose>(MAX_UNDO_HISTORY);
  64. private CappedStack<AnchorPose> redoStack = new CappedStack<AnchorPose>(MAX_UNDO_HISTORY);
  65. //When we call TranslateZEDIncrementally or the Rotate version, we only want to register an undoable pose
  66. //when you first call it. So we have a timer we start after registering the pose that blocks re-registering
  67. //until neither incremental function has been called for MIN_TIME_ect seconds.
  68. private const float MIN_TIME_BETWEEN_INCREMENTAL_UNDOS = 0.25f;
  69. private float incrementalUndoTimer = 0f;
  70. private bool canRegisterIncrementalUndo
  71. {
  72. get
  73. {
  74. return incrementalUndoTimer <= 0f;
  75. }
  76. }
  77. /// <summary>
  78. /// Get necessary references and handle startup events.
  79. /// </summary>
  80. void Start()
  81. {
  82. if (!controllerTracker) controllerTracker = GetComponent<ZEDControllerTracker>();
  83. if (!zedManager) zedManager = controllerTracker.zedManager;
  84. zedManager.OnZEDReady += LoadCalibFile;
  85. //Dispatch event that this anchor is ready.
  86. if (OnCameraAnchorCreated != null) OnCameraAnchorCreated.Invoke(this);
  87. }
  88. /// <summary>
  89. /// Sets the ZED position and rotation to the specified values, instantly.
  90. /// Also stores the pose for undoing later.
  91. /// </summary>
  92. public void SetNewZEDPose(Vector3 localpos, Quaternion localrot)
  93. {
  94. RegisterUndoablePose();
  95. zedManager.transform.localPosition = localpos;
  96. zedManager.transform.localRotation = localrot;
  97. }
  98. /// <summary>
  99. /// Adds the specified translation and rotation to the current ZED transform values.
  100. /// </summary>
  101. /// <param name="posoffset">Direction and amount to translate the ZED.</param>
  102. /// <param name="rotoffset">Direction and amount to rotate the ZED.</param>
  103. /// <param name="uselocal">If true, applies to localPosition/localRotation. Otherwise, applies to world-space values.</param>
  104. public void MoveZEDPose(Vector3 posoffset, Quaternion rotoffset, bool uselocal)
  105. {
  106. RegisterUndoablePose();
  107. if (uselocal) //Local space.
  108. {
  109. zedManager.transform.localPosition += posoffset;
  110. zedManager.transform.localRotation = rotoffset * zedManager.transform.localRotation;
  111. }
  112. else //World space.
  113. {
  114. zedManager.transform.position += posoffset;
  115. zedManager.transform.rotation = rotoffset * zedManager.transform.rotation;
  116. }
  117. }
  118. /// <summary>
  119. /// Move the ZED in a direction by an amount governed by maxTranslateMPS.
  120. /// Provided values should be clamped to -1 and 1, representing the multiple of that max speed used to translate.
  121. /// For instance, if max speed is 1 meter per second and this is called with 0, 0.5f, 0, for 1 second, it'll
  122. /// move 0.5 meters over the course of that second.
  123. /// Meant to be called every frame while dragging a control (like the translate arrows) to slide the ZED gradually.
  124. /// </summary>
  125. /// <param name="translation">What percentage (-1 to 1) of the max speed to move the ZED.</param>
  126. public void TranslateZEDIncrementally(Vector3 translation) //Input should be clamped to -1 and 1. 1 moves in max direction speed.
  127. {
  128. if (canRegisterIncrementalUndo) //Clear to register the undo.
  129. {
  130. RegisterUndoablePose();
  131. StartCoroutine(BlockIncrementalUndoRegister());
  132. }
  133. else //Timer already started. Don't start a new coroutine, but reset the timer.
  134. {
  135. incrementalUndoTimer = MIN_TIME_BETWEEN_INCREMENTAL_UNDOS;
  136. }
  137. Vector3 velocity = translation * maxTranslateMPS * Time.deltaTime;
  138. zedManager.transform.localPosition += zedManager.transform.localRotation * velocity;
  139. }
  140. /// <summary>
  141. /// Rotates the ZED in a direction by an amount governed by maxRotateDPS.
  142. /// Provided values should be clamped to -1 and 1, representing the multiple of that max speed used to rotate.
  143. /// For instance, if max speed is 100 degrees per second, and this is called with 0,0.5f,0 for 1 second, it'll
  144. /// rotate 50 degrees on the Y axis over the course of that second.
  145. /// Meant to be called every frame while dragging a control (like the translate arrows) to slide the ZED gradually.
  146. /// </summary>
  147. /// <param name="rotation"></param>
  148. public void RotateZEDIncrementally(Vector3 rotation)//Input should be clamped to -1 and 1. 1 moves in max rotation speed.
  149. {
  150. if (canRegisterIncrementalUndo) //Clear to register the undo.
  151. {
  152. RegisterUndoablePose();
  153. StartCoroutine(BlockIncrementalUndoRegister());
  154. }
  155. else //Timer already started. Don't start a new coroutine, but reset the timer.
  156. {
  157. incrementalUndoTimer = MIN_TIME_BETWEEN_INCREMENTAL_UNDOS;
  158. }
  159. Vector3 angvelocity = rotation * maxRotateDPS * Time.deltaTime;
  160. zedManager.transform.localRotation *= Quaternion.Euler(angvelocity);
  161. //zedManager.transform.localEulerAngles += angvelocity;
  162. }
  163. /// <summary>
  164. /// Temporarily blocks an undoable position from being registered from the 'incremental' move functions.
  165. /// This is done so that when you first start sliding/rotating the ZED over time, it only saves the position
  166. /// in the first frame, and not repeatedly or at any time until you finish thta action.
  167. /// </summary>
  168. private IEnumerator BlockIncrementalUndoRegister()
  169. {
  170. incrementalUndoTimer = MAX_UNDO_HISTORY;
  171. while (incrementalUndoTimer > 0)
  172. {
  173. incrementalUndoTimer -= Time.deltaTime;
  174. yield return null;
  175. }
  176. }
  177. /// <summary>
  178. /// Moves the ZED to the top position on the Undo stack, undoing the last change.
  179. /// Also adds the current position to the Redo stack.
  180. /// </summary>
  181. public void Undo()
  182. {
  183. if (undoStack.Count == 0) return; //No action to undo.
  184. AnchorPose undoPose = undoStack.Pop(); //Get last pose from the stack and remove it.
  185. //Save the current pose to the redo stack.
  186. AnchorPose pose = new AnchorPose(zedManager.transform.localPosition, zedManager.transform.localRotation);
  187. redoStack.Push(pose);
  188. //Apply historical pose to the ZED.
  189. zedManager.transform.localPosition = undoPose.position;
  190. zedManager.transform.localRotation = undoPose.rotation;
  191. //redoStack.Push(undoPose); //Remember what you undid so it can be redone.
  192. }
  193. /// <summary>
  194. /// Moves the ZED to the top position on the Redo stack, if any, which is added after
  195. /// the user calls Undo, so long as another Undo is not registered in the meantime.
  196. /// </summary>
  197. public void Redo()
  198. {
  199. if (redoStack.Count == 0) return; //No action to redo.
  200. AnchorPose redoPose = redoStack.Pop(); //Get last pose from the stack and remove it.
  201. //Re-apply that pose to the ZED.
  202. zedManager.transform.localPosition = redoPose.position;
  203. zedManager.transform.localRotation = redoPose.rotation;
  204. undoStack.Push(redoPose); //Put that action back on the undo stack so you could repeat this process again if you wanted.
  205. }
  206. /// <summary>
  207. /// Call before the ZED is moved to make it so you can go back to this position by pressing Undo.
  208. /// If you are moving it gradually, call before the gradual movement starts and don't update during.
  209. /// Also clears the Redo stack as you've now branched away from it.
  210. /// </summary>
  211. private void RegisterUndoablePose()
  212. {
  213. RegisterUndoablePose(zedManager.transform.localPosition, zedManager.transform.localRotation);
  214. }
  215. /// <summary>
  216. /// Call before the ZED is moved to make it so you can go back to this position by pressing Undo.
  217. /// If you are moving it gradually, call before the gradual movement starts and don't update during.
  218. /// Also clears the Redo stack as you've now branched away from it.
  219. /// </summary>
  220. private void RegisterUndoablePose(Vector3 pos, Quaternion rot)
  221. {
  222. AnchorPose pose = new AnchorPose(pos, rot);
  223. undoStack.Push(pose);
  224. //Clear the Redo stack as we've now branched away from whatever history it had.
  225. redoStack.Clear();
  226. }
  227. /// <summary>
  228. /// Saves the ZED's offset from the tracked object as a file to be loaded within Unity or from any ZED application
  229. /// built to load such a calibration file.
  230. /// <para>Destination directory defined by the CALIB_FILE_PATH constant.</para>
  231. /// </summary>
  232. public void SaveCalibFile()
  233. {
  234. int slashindex = filePath.LastIndexOf('\\');
  235. string directory = filePath.Substring(0, slashindex);
  236. print(directory);
  237. if (!System.IO.Directory.Exists(directory))
  238. {
  239. System.IO.Directory.CreateDirectory(directory);
  240. }
  241. using (System.IO.StreamWriter file = new System.IO.StreamWriter(filePath))
  242. {
  243. Vector3 localpos = zedManager.transform.localPosition; //Shorthand.
  244. Vector3 localrot = zedManager.transform.localRotation.eulerAngles; //Shorthand.
  245. file.WriteLine("x=" + localpos.x);
  246. file.WriteLine("y=" + localpos.y);
  247. file.WriteLine("z=" + localpos.z);
  248. file.WriteLine("rx=" + localrot.x);
  249. file.WriteLine("ry=" + localrot.y);
  250. file.WriteLine("rz=" + localrot.z);
  251. #if ZED_STEAM_VR
  252. string indexstring = "indexController=";
  253. if (controllerTracker.index > 0)
  254. {
  255. var snerror = Valve.VR.ETrackedPropertyError.TrackedProp_Success;
  256. var snresult = new System.Text.StringBuilder((int)64);
  257. indexstring += Valve.VR.OpenVR.System.GetStringTrackedDeviceProperty((uint)controllerTracker.index,
  258. Valve.VR.ETrackedDeviceProperty.Prop_SerialNumber_String, snresult, 64, ref snerror);
  259. }
  260. else
  261. {
  262. indexstring += "NONE";
  263. }
  264. file.WriteLine(indexstring);
  265. #endif
  266. file.Close();
  267. print("Calibration saved to " + filePath);
  268. MessageDisplay.DisplayTemporaryMessageAll("Saved calibration file to:\r\n" + filePath);
  269. }
  270. }
  271. /// <summary>
  272. /// Loads a previously-saved calibration file, if any, and applies the position to the ZED.
  273. /// Called when the ZED is first initialized, so you left off where you started.
  274. /// </summary>
  275. public void LoadCalibFile()
  276. {
  277. if (!System.IO.File.Exists(filePath))
  278. {
  279. print("Did not load values as no previously-saved file was found: " + filePath);
  280. return;
  281. }
  282. string[] lines = null;
  283. try
  284. {
  285. lines = System.IO.File.ReadAllLines(filePath);
  286. }
  287. catch (System.Exception e)
  288. {
  289. Debug.LogError(e.ToString());
  290. }
  291. if (lines == null)
  292. {
  293. print("Loaded calibration file was empty: " + filePath);
  294. return;
  295. }
  296. Vector3 localpos = Vector3.zero;
  297. Vector3 localrot = Vector3.zero; //Euler angles.
  298. foreach (string line in lines)
  299. {
  300. string[] splitline = line.Split('=');
  301. if (splitline != null && splitline.Length >= 2)
  302. {
  303. string key = splitline[0];
  304. string value = splitline[1].Split(' ')[0].ToLower(); //Removed space after values if present.
  305. if (key == "indexController") continue; //We don't need to load this.
  306. //We'll parse the field ahead of time for simplicity, but this only works because all needed values are floats.
  307. //This needs to be amended if we ever need to load other values.
  308. float parsedval = float.Parse(value, System.Globalization.CultureInfo.InvariantCulture);
  309. if (key == "x")
  310. {
  311. localpos.x = parsedval;
  312. }
  313. else if (key == "y")
  314. {
  315. localpos.y = parsedval;
  316. }
  317. else if (key == "z")
  318. {
  319. localpos.z = parsedval;
  320. }
  321. else if (key == "rx")
  322. {
  323. localrot.x = parsedval;
  324. }
  325. else if (key == "ry")
  326. {
  327. localrot.y = parsedval;
  328. }
  329. else if (key == "rz")
  330. {
  331. localrot.z = parsedval;
  332. }
  333. }
  334. }
  335. zedManager.transform.localPosition = localpos;
  336. zedManager.transform.localRotation = Quaternion.Euler(localrot);
  337. print("Loaded past calibration from file: " + filePath);
  338. }
  339. /// <summary>
  340. /// Very simple version of UnityEngine's Pose class, as it doesn't exist in older versions of Unity.
  341. /// </summary>
  342. internal class AnchorPose
  343. {
  344. internal Vector3 position;
  345. internal Quaternion rotation;
  346. internal AnchorPose(Vector3 pos, Quaternion rot)
  347. {
  348. position = pos;
  349. rotation = rot;
  350. }
  351. }
  352. }