udProjectNodeUnity.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using udSDK;
  5. using System.Runtime.InteropServices;
  6. using JetBrains.Annotations;
  7. using System;
  8. using Microsoft.Win32;
  9. using UnityEngine.Networking;
  10. public class udProjectNodeUnity : MonoBehaviour
  11. {
  12. public udProjectNodeType itemType;
  13. public string itemTypeString;
  14. public UDProjectNode projectNode;
  15. public udProjectGeometryType geometryType;
  16. public string URI;
  17. GameObject firstChild;
  18. GameObject nextSibling;
  19. // keeps track of all coordinates in the nodedata
  20. double[] positions = new double[3];
  21. private UDProjectUnity project;
  22. Vector3 DoublesToVector3(double[] doubles)
  23. {
  24. return new Vector3((float)doubles[0], (float)doubles[1], (float)doubles[2]);
  25. }
  26. double[] GetReorderedPosition(double[] positions, int i)
  27. {
  28. double[] position = new double[3];
  29. position[0] = -positions[3 * i];
  30. position[2] = -positions[3 * i + 1];
  31. position[1] = -positions[3 * i + 2];
  32. return position;
  33. }
  34. IEnumerator LoadMediaImage(SpriteRenderer sprite)
  35. {
  36. string targetURI = URI;
  37. if (!URI.StartsWith("http") && !URI.StartsWith("www"))
  38. URI = "file://" + Application.dataPath + "/" + URI;
  39. // warning : this syntax changes somewhat in later versions of Unity
  40. using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(URI))
  41. {
  42. yield return uwr.SendWebRequest();
  43. if (uwr.isNetworkError || uwr.isHttpError)
  44. {
  45. Debug.Log(uwr.error);
  46. }
  47. else
  48. {
  49. // Get downloaded asset bundle
  50. var tex = DownloadHandlerTexture.GetContent(uwr);
  51. sprite.sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
  52. }
  53. }
  54. yield return null;
  55. }
  56. GameObject PlaceChildSphere(double[] position, float size)
  57. {
  58. Vector3 positionVector = DoublesToVector3(position);
  59. var sphereGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  60. var renderer = sphereGO.GetComponent<Renderer>();
  61. var shader = Shader.Find("udSDK/Demo/DepthOffsetSprite");
  62. if (shader == null)
  63. throw new Exception("Required shader is missing : udSDK/Demo/DepthOffsetSprite");
  64. renderer.material = new Material(shader);
  65. renderer.material.SetFloat("_ZOffset", 0 + size*0.01f);
  66. renderer.material.color = project.appearance.interestColor;
  67. sphereGO.transform.parent = transform;
  68. sphereGO.transform.position = positionVector;
  69. sphereGO.transform.localScale = Vector3.one * size;
  70. return sphereGO;
  71. }
  72. public void LoadTree(IntPtr pNode)
  73. {
  74. project = GetComponentInParent<UDProjectUnity>();
  75. projectNode = new UDProjectNode(pNode);
  76. if (projectNode.nodeData.pName != IntPtr.Zero)
  77. gameObject.name = Marshal.PtrToStringAnsi(projectNode.nodeData.pName);
  78. if (projectNode.nodeData.pURI != IntPtr.Zero)
  79. URI = Marshal.PtrToStringAnsi(projectNode.nodeData.pURI);
  80. if (projectNode.nodeData.pCoordinates != IntPtr.Zero)
  81. {
  82. if (!(projectNode.nodeData.geomCount == 0))
  83. {
  84. positions = new double[projectNode.nodeData.geomCount * 3];
  85. Marshal.Copy(projectNode.nodeData.pCoordinates, positions, 0, projectNode.nodeData.geomCount * 3);
  86. }
  87. else
  88. {
  89. positions = new double[3];
  90. Marshal.Copy(projectNode.nodeData.pCoordinates, positions, 0, 3);
  91. }
  92. }
  93. this.itemType = projectNode.nodeData.itemtype;
  94. this.geometryType = projectNode.nodeData.geomtype;
  95. switch (itemType)
  96. {
  97. case udProjectNodeType.udPNT_Custom://!<Need to check the itemtypeStr string manually.
  98. itemTypeString = new string(projectNode.nodeData.itemtypeStr);
  99. switch (itemTypeString)
  100. {
  101. //these are the custom types currently supported by udSDK Client:
  102. case "I3S":
  103. break;
  104. case ("Water"):
  105. break;
  106. case "ViewMap":
  107. break;
  108. case "Polygon":
  109. break;
  110. case "QFilter":
  111. break;
  112. case "Places":
  113. break;
  114. case "MHeight":
  115. break;
  116. }
  117. break;
  118. case udProjectNodeType.udPNT_PointCloud://!<A Euclideon Unlimited Detail Point Cloud file (“UDS”)
  119. UDSModel model = gameObject.AddComponent<UDSModel>();
  120. gameObject.tag = "UDSModel";
  121. model.path = this.URI;
  122. break;
  123. case udProjectNodeType.udPNT_PointOfInterest:
  124. if (geometryType != udProjectGeometryType.udPGT_LineString)
  125. {
  126. double[] pointPosition = GetReorderedPosition(positions, 0);
  127. pointPosition = project.CheckPosition(pointPosition);
  128. var sphereGO = PlaceChildSphere(pointPosition, project.appearance.pointSize);
  129. sphereGO.name = "Point of Interest";
  130. }
  131. break;
  132. case udProjectNodeType.udPNT_Folder: //!<A folder of other nodes (“Folder”)
  133. break;
  134. case udProjectNodeType.udPNT_GTFS: //!< A General Transit Feed Specification object ("GTFS")
  135. break;
  136. case udProjectNodeType.udPNT_Media: //!<An Image, Movie, Audio file or other media object (“Media”)
  137. // only supporting images presently
  138. var icon = new GameObject();
  139. var sprite = icon.AddComponent<SpriteRenderer>();
  140. var shader = Shader.Find("udSDK/Demo/SpriteBillboard");
  141. if (shader == null)
  142. throw new Exception("Required shader is missing : udSDK/Demo/SpriteBillboard");
  143. sprite.material = new Material(shader);
  144. sprite.material.SetFloat("_ZOffset", 0.5f);
  145. Texture2D tex = Texture2D.blackTexture;
  146. sprite.sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
  147. icon.transform.parent = transform;
  148. double[] spritePosition = GetReorderedPosition(positions, 0);
  149. spritePosition = project.CheckPosition(spritePosition);
  150. Vector3 positionVector = DoublesToVector3(spritePosition);
  151. icon.transform.position = positionVector;
  152. icon.transform.localScale = Vector3.one * project.appearance.imageMediaSize;
  153. // load the image in a coroutine
  154. icon.name = "Media : " + URI;
  155. StartCoroutine(LoadMediaImage(sprite));
  156. break;
  157. case udProjectNodeType.udPNT_Viewpoint:
  158. break;
  159. case udProjectNodeType.udPNT_VisualisationSettings: //!<Visualisation settings (itensity, map height etc) (“VizSet”)
  160. break;
  161. }
  162. switch (geometryType)
  163. {
  164. case(udProjectGeometryType.udPGT_None): //!<There is no geometry associated with this node.
  165. break;
  166. case(udProjectGeometryType.udPGT_Point): //!<pCoordinates is a single 3D position
  167. // we check the position here, so that it sets any required offset values
  168. project.CheckPosition(GetReorderedPosition(positions, 0));
  169. break;
  170. case(udProjectGeometryType.udPGT_MultiPoint): //!<Array of udPGT_Point, pCoordinates is an array of 3D positions.
  171. if (!(projectNode.nodeData.geomCount==0))
  172. {
  173. //create a child object for each geometry object
  174. for(int i = 0; i < projectNode.nodeData.geomCount; i++)
  175. {
  176. double[] position = GetReorderedPosition(positions, i);
  177. position = project.CheckPosition(position);
  178. var sphereGO = PlaceChildSphere(position, project.appearance.lineSize);
  179. sphereGO.name = "Point "+i;
  180. }
  181. }
  182. break;
  183. case(udProjectGeometryType.udPGT_LineString): //!<pCoordinates is an array of 3D positions forming an open line
  184. LineRenderer lr = gameObject.AddComponent<LineRenderer>();
  185. var shader = Shader.Find("udSDK/Demo/DepthOffsetSprite");
  186. if (shader == null)
  187. throw new Exception("Required shader is missing : udSDK/Demo/DepthOffsetSprite");
  188. lr.material = new Material(shader);
  189. lr.material.SetFloat("_ZOffset", 0 + project.appearance.lineSize * 0.01f);
  190. lr.material.color = project.appearance.interestColor;
  191. lr.useWorldSpace = false; // this gives us more tangible editor controls
  192. lr.positionCount = projectNode.nodeData.geomCount;
  193. Vector3[] verts = new Vector3[projectNode.nodeData.geomCount];
  194. if (!(projectNode.nodeData.geomCount==0))
  195. {
  196. //create a child object for each geometry object
  197. for(int i = 0; i < projectNode.nodeData.geomCount; i++)
  198. {
  199. double[] position = GetReorderedPosition(positions, i);
  200. position = project.CheckPosition(position);
  201. var sphereGO = PlaceChildSphere(position, project.appearance.lineSize);
  202. verts[i] = sphereGO.transform.position;
  203. sphereGO.name = "Point "+i;
  204. }
  205. }
  206. lr.SetPositions(verts);
  207. lr.startWidth = lr.endWidth = project.appearance.lineSize;
  208. break;
  209. case(udProjectGeometryType.udPGT_MultiLineString): //!<Array of udPGT_LineString; pCoordinates is NULL and children will be present.
  210. break;
  211. case(udProjectGeometryType.udPGT_Polygon): //!<pCoordinates will be a closed linear ring (the outside), there MAY be children that are interior as pChildren udPGT_MultiLineString items, these should be counted as islands of the external ring.
  212. break;
  213. case(udProjectGeometryType.udPGT_MultiPolygon): //!<pCoordinates is null, children will be udPGT_Polygon (which still may have internal islands)
  214. break;
  215. case(udProjectGeometryType.udPGT_GeometryCollection): //!<Array of geometries; pCoordinates is NULL and children may be present of any type.
  216. break;
  217. }
  218. //create sibling
  219. if(projectNode.nodeData.pNextSibling != System.IntPtr.Zero)
  220. {
  221. nextSibling = new GameObject();
  222. nextSibling.transform.parent = transform.parent;
  223. var nextSiblingData = nextSibling.AddComponent<udProjectNodeUnity>();
  224. nextSiblingData.LoadTree(projectNode.nodeData.pNextSibling);
  225. }
  226. //create a child (if exists)
  227. if(projectNode.nodeData.pFirstChild != System.IntPtr.Zero)
  228. {
  229. firstChild = new GameObject();
  230. firstChild.transform.parent = transform;
  231. var firstChildData = firstChild.AddComponent<udProjectNodeUnity>();
  232. firstChildData.LoadTree(projectNode.nodeData.pFirstChild);
  233. }
  234. }
  235. }