UDProjectUnity.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine;
  6. using udSDK;
  7. [System.Serializable]
  8. public struct UDProjectAppearance
  9. {
  10. [Tooltip("Control the color of lines and points.")]
  11. public Color interestColor;
  12. [Tooltip("Control the size of points.")]
  13. public float pointSize;
  14. [Tooltip("Control the size of image media.")]
  15. public float imageMediaSize;
  16. [Tooltip("Control the size of lines.")]
  17. public float lineSize;
  18. }
  19. public class UDProjectUnity : MonoBehaviour
  20. {
  21. public string path;
  22. public bool isLoaded = false;
  23. private string geoJSON;
  24. UDProject proj;
  25. // this retains an offset, to keep everything close to world origin
  26. private double[] positionOffset = new double[3];
  27. private bool positionSet = false;
  28. [Tooltip("Modify the visuals of a project when loaded.")]
  29. public UDProjectAppearance appearance = new UDProjectAppearance{
  30. interestColor = new Color(1f, 0.42f, 0f),
  31. pointSize = 50f,
  32. imageMediaSize = 100f,
  33. lineSize = 20f
  34. };
  35. public double[] CheckPosition(double[] position)
  36. {
  37. if(!positionSet)
  38. {
  39. SetPosition(position);
  40. return new double[] {0,0,0};
  41. }
  42. return new double[]
  43. {
  44. positionOffset[0] - position[0],
  45. positionOffset[1] - position[1],
  46. positionOffset[2] - position[2]
  47. };
  48. }
  49. public void SetPosition(double[] position)
  50. {
  51. positionOffset[0] = position[0];
  52. positionOffset[1] = position[1];
  53. positionOffset[2] = position[2];
  54. Debug.Log("Set project position : " + position[0] + ", " + position[1] + ", " + position[2]);
  55. positionSet = true;
  56. }
  57. void LoadFromFile(string path)
  58. {
  59. geoJSON = System.IO.File.ReadAllText(path);
  60. proj = new UDProject(geoJSON);
  61. this.path = path;
  62. print("loaded node!");
  63. }
  64. // Update is called once per frame
  65. void Update()
  66. {
  67. if (!isLoaded)
  68. {
  69. if(!GlobalUDContext.isCreated)
  70. {
  71. Debug.Log("Cannot load project before global context.");
  72. return;
  73. }
  74. try
  75. {
  76. LoadFromFile(path);
  77. GameObject rootNodeGO = new GameObject();
  78. rootNodeGO.transform.parent = transform;
  79. var pn = rootNodeGO.AddComponent<udProjectNodeUnity>();
  80. pn.LoadTree(proj.pRootNode);
  81. }
  82. catch(Exception e)
  83. {
  84. Debug.LogError("caught exception loading project: " + e.ToString());
  85. }
  86. isLoaded = true;
  87. }
  88. }
  89. }