UDSModel.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. #if UNITY_EDITOR
  7. using UnityEditor ;
  8. #endif
  9. using udSDK;
  10. public class UDSModel : MonoBehaviour
  11. {
  12. [System.NonSerialized]
  13. public udPointCloud udModel = new udPointCloud();
  14. [System.NonSerialized]
  15. public bool isLoaded = false;
  16. [System.NonSerialized]
  17. public Matrix4x4 modelScale;
  18. [System.NonSerialized]
  19. public Matrix4x4 modelToPivot; //This represents the transformation between the file representation and the coordinate system centred at pivot
  20. public udPointCloudHeader header = new udPointCloudHeader();
  21. [System.NonSerialized]
  22. public Matrix4x4 storedMatrix;
  23. [System.NonSerialized]
  24. public Vector3 fileScale;
  25. public Vector3 geolocationOffset = Vector3.zero;
  26. public string path = "";
  27. public bool geolocate = false;
  28. [HideInInspector]
  29. public UnityEvent m_OnLoaded;
  30. public string Path
  31. {
  32. get { return path; }
  33. set
  34. {
  35. path = value;
  36. if (isLoaded == true)
  37. udModel.Unload();
  38. isLoaded = false;
  39. }
  40. }
  41. private void Awake()
  42. {
  43. // adding this init in Awake so that it happens at the earliest possible time
  44. m_OnLoaded = new UnityEvent();
  45. }
  46. private void Update()
  47. {
  48. if (geolocate)
  49. {
  50. this.transform.localPosition = UDUtilities.UDtoGL *
  51. new Vector3(
  52. (float)(header.baseOffset[0] + header.pivot[0] * header.scaledRange),
  53. (float)(header.baseOffset[1] + header.pivot[1] * header.scaledRange),
  54. (float)(header.baseOffset[2] + header.pivot[2] * header.scaledRange)
  55. );
  56. transform.localPosition += geolocationOffset;
  57. geolocate = false;
  58. }
  59. }
  60. public Matrix4x4 getStoredMatrix() {
  61. return new Matrix4x4(
  62. new Vector4((float)header.storedMatrix[0], (float)header.storedMatrix[1], (float)header.storedMatrix[2], (float)header.storedMatrix[3]),
  63. new Vector4((float)header.storedMatrix[4], (float)header.storedMatrix[5], (float)header.storedMatrix[6], (float)header.storedMatrix[7]),
  64. new Vector4((float)header.storedMatrix[8], (float)header.storedMatrix[9], (float)header.storedMatrix[10], (float)header.storedMatrix[11]),
  65. new Vector4((float)header.storedMatrix[12], (float)header.storedMatrix[13], (float)header.storedMatrix[14], (float)header.storedMatrix[15])
  66. ) ;
  67. }
  68. // This gets called by getUDSInstances if it isn't loaded already
  69. public void LoadModel()
  70. {
  71. if (!GlobalUDContext.isCreated || isLoaded || Path == "" || Path == null)
  72. return;
  73. try
  74. {
  75. udModel.Load(GlobalUDContext.uContext, Path, ref header);
  76. storedMatrix = getStoredMatrix();
  77. double maxDim = 0;
  78. for (int i = 0; i < 3; i++) {
  79. if(maxDim<header.boundingBoxExtents[i])
  80. maxDim = header.boundingBoxExtents[i];
  81. }
  82. float s = (float)(1 / (2* maxDim));//bounding box extents are relative to centre -> size of model is double the extents
  83. fileScale = new Vector3(s,s,s);
  84. modelScale = Matrix4x4.Scale(new Vector3((float) header.scaledRange/s, (float) header.scaledRange/s, (float) header.scaledRange/s));
  85. modelToPivot = UDUtilities.UDtoGL *
  86. Matrix4x4.Scale(new Vector3(
  87. (float)header.scaledRange,
  88. (float)header.scaledRange,
  89. (float)header.scaledRange
  90. )
  91. ) *
  92. Matrix4x4.Translate(new Vector3(
  93. (float)(-header.pivot[0]),
  94. (float)(-header.pivot[1]),
  95. (float)(-header.pivot[2])
  96. )
  97. );
  98. isLoaded = true;
  99. m_OnLoaded.Invoke();
  100. }
  101. catch (Exception e)
  102. {
  103. Debug.LogError("Could not open UDS: " + Path + " " + e.Message);
  104. }
  105. }
  106. [ContextMenu("Reload")]
  107. public void Reload()
  108. {
  109. // we don't force a reload, only ensure reload happens at the next opportunity
  110. isLoaded = false;
  111. udModel.Unload();
  112. }
  113. }
  114. #if UNITY_EDITOR
  115. [CustomEditor(typeof(UDSModel))]
  116. public class UDSModelGUI : Editor
  117. {
  118. UDSModel script;
  119. GameObject gameObject ;
  120. void OnEnable() {
  121. script = (UDSModel) target;
  122. gameObject = script.gameObject;
  123. }
  124. public override void OnInspectorGUI()
  125. {
  126. if(gameObject.tag != "UDSModel")
  127. EditorGUILayout.HelpBox("This is not displayed because gameObject tag is not 'UDSModel'", MessageType.Warning);
  128. DrawDefaultInspector();
  129. }
  130. }
  131. #endif