UDUtilities.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using System.Threading;
  6. namespace udSDK
  7. {
  8. public static class UDUtilities
  9. {
  10. public static Matrix4x4 UDtoGL =
  11. Matrix4x4.Scale(new Vector3(1, -1, 1)) *
  12. Matrix4x4.Rotate(Quaternion.Euler(90, 0, 0));
  13. /*
  14. * converts the z buffer value to a world space displacement
  15. */
  16. public static float zBufferToDepth(float z, float zNear, float zFar, bool ortho = true)
  17. {
  18. if (ortho)
  19. return (z * 0.5f + 0.5f) * (zFar - zNear) + zNear;
  20. else
  21. return (2 * zNear * zFar / (zNear - zFar)) / (z - (zFar + zNear) / (zFar - zNear));
  22. }
  23. /*
  24. *Converts matrix from Unity's left handed transformation convention ( y'=Ay) to
  25. * left handed system (y'=yA)
  26. */
  27. public static double[] GetUDMatrix(Matrix4x4 unityMat)
  28. {
  29. double[] udMat =
  30. {
  31. unityMat.m00,
  32. unityMat.m10,
  33. unityMat.m20,
  34. unityMat.m30,
  35. unityMat.m01,
  36. unityMat.m11,
  37. unityMat.m21,
  38. unityMat.m31,
  39. unityMat.m02,
  40. unityMat.m12,
  41. unityMat.m22,
  42. unityMat.m32,
  43. unityMat.m03,
  44. unityMat.m13,
  45. unityMat.m23,
  46. unityMat.m33
  47. };
  48. return udMat;
  49. }
  50. /*
  51. * attempts to load and returns all loaded UDS models in the scene
  52. */
  53. public static udRenderInstance[] getUDSInstances()
  54. {
  55. GameObject[] objects = GameObject.FindGameObjectsWithTag("UDSModel");
  56. int count = 0;
  57. udRenderInstance[] modelArray = new udRenderInstance[objects.Length];
  58. for (int i = 0; i < objects.Length; ++i)
  59. {
  60. UDSModel model = (UDSModel) objects[i].GetComponent("UDSModel");
  61. if (!model.isLoaded)
  62. model.LoadModel();
  63. if (model.isLoaded)
  64. {
  65. modelArray[count].pointCloud = model.udModel.pModel;
  66. Transform localTransform = objects[i].transform;
  67. modelArray[count].worldMatrix = UDUtilities.GetUDMatrix(
  68. Matrix4x4.TRS(model.transform.position, model.transform.rotation, model.transform.localScale) *
  69. model.modelToPivot
  70. );
  71. count++;
  72. }
  73. }
  74. return modelArray.Where(m => (m.pointCloud != System.IntPtr.Zero)).ToArray();
  75. }
  76. }
  77. /*
  78. *Class responsible for managing all threads related to VDK licensing
  79. */
  80. public class UDSessionThreadManager {
  81. bool logLicenseInformation = false;//this will print the license status every second to the log
  82. Thread licenseLogThread;
  83. List<Thread> activeThreads = new List<Thread>();
  84. public UDSessionThreadManager() {
  85. if (logLicenseInformation)
  86. {
  87. licenseLogThread = new Thread(new ThreadStart(LogLicenseStatus));
  88. licenseLogThread.Start();
  89. activeThreads.Add(licenseLogThread);
  90. }
  91. }
  92. /*
  93. *Logs the time until the license expires to the console every second
  94. */
  95. public void LogLicenseStatus() {
  96. while (true)
  97. {
  98. try
  99. {
  100. UDSessionInfo info = GlobalUDContext.uContext.GetSessionInfo();
  101. string message = string.Empty;
  102. if (info.isOffline)
  103. message += "Offline ";
  104. else
  105. message += "Online ";
  106. message += "License Expiry: " + info.expiresTimestamp.ToString();
  107. UnityEngine.Debug.Log(message);
  108. Thread.Sleep(1000);
  109. }
  110. catch {
  111. continue;
  112. }
  113. }
  114. }
  115. ~UDSessionThreadManager() {
  116. foreach (Thread thread in activeThreads)
  117. thread.Abort();
  118. }
  119. }
  120. }