HeatMapRead.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**************************************************
  2. FileName:ObjectPool
  3. ****************************************************/
  4. //using DG.Tweening;
  5. using LitJson;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. public class HeatMapDate : MonoBehaviour
  11. {
  12. private int people;//最大人口
  13. private int range;//范围
  14. private int xpos;//x位置
  15. private int zpos;//z位置
  16. private int min;//最小人口
  17. public int People
  18. {
  19. get
  20. {
  21. return people;
  22. }
  23. set
  24. {
  25. people = value;
  26. }
  27. }
  28. public int Range
  29. {
  30. get
  31. {
  32. return range;
  33. }
  34. set
  35. {
  36. range = value;
  37. }
  38. }
  39. public int Xpos
  40. {
  41. get
  42. {
  43. return xpos;
  44. }
  45. set
  46. {
  47. xpos = value;
  48. }
  49. }
  50. public int Zpos
  51. {
  52. get
  53. {
  54. return zpos;
  55. }
  56. set
  57. {
  58. zpos = value;
  59. }
  60. }
  61. public int Min
  62. {
  63. get
  64. {
  65. return min;
  66. }
  67. set
  68. {
  69. min = value;
  70. }
  71. }
  72. }
  73. public class HeatMapRead : MonoBehaviour
  74. {
  75. public TextAsset HeatMapJson;
  76. JsonData heatMapJson;
  77. public string Layer = "";
  78. List<HeatMapDate> heatMapDate = new List<HeatMapDate>(); //数据集合
  79. public heatControl heatControl; //生成热力图算法代码
  80. public static HeatMapRead instance;
  81. private void Awake()
  82. {
  83. instance = this;
  84. Generate();
  85. }
  86. void Start()
  87. {
  88. StartR();
  89. }
  90. public int xSize, ySize;
  91. private Vector3[] vertices;
  92. private Mesh mesh;
  93. //生成Mesh
  94. private void Generate()
  95. {
  96. GetComponent<MeshFilter>().mesh = mesh = new Mesh();
  97. mesh.name = "Procedural Grid";
  98. vertices = new Vector3[(xSize + 1) * (ySize + 1)];
  99. for (int i = 0, y = 0; y <= ySize; y++)
  100. {
  101. for (int x = 0; x <= xSize; x++, i++)
  102. {
  103. vertices[i] = new Vector3(x,0,y);
  104. }
  105. }
  106. mesh.vertices = vertices;
  107. int[] triangles = new int[xSize * ySize * 6];
  108. for (int ti = 0, vi = 0, y = 0; y < ySize; y++, vi++)
  109. {
  110. for (int x = 0; x < xSize; x++, ti += 6, vi++)
  111. {
  112. triangles[ti] = vi;
  113. triangles[ti + 3] = triangles[ti + 2] = vi + 1;
  114. triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
  115. triangles[ti + 5] = vi + xSize + 2;
  116. }
  117. }
  118. mesh.triangles = triangles;
  119. // 网格自动计算法线向量
  120. mesh.RecalculateNormals();
  121. //Vector2[] uv = new Vector2[vertices.Length];
  122. //Vector4[] tangents = new Vector4[vertices.Length];//切线
  123. //Vector4 tangent = new Vector4(1f, 0f, 0f, -1f);
  124. //for (int i = 0, y = 0; y <= ySize; y++)
  125. //{
  126. // for (int x = 0; x <= xSize; x++, i++)
  127. // {
  128. // uv[i] = new Vector2((float)x / xSize, (float)y / ySize);
  129. // tangents[i] = tangent;
  130. // }
  131. //}
  132. //mesh.uv = uv;
  133. //mesh.tangents = tangents;
  134. }
  135. void StartR()
  136. {
  137. raedHeatMap(0);
  138. //this.GetComponent<heatControl>().enabled = false;
  139. //this.GetComponent<HeatMapRead>().enabled = false;
  140. }
  141. public void raedHeatMap(float High)
  142. {
  143. heatMapDate.Clear();//清空数据
  144. string subwayText = HeatMapJson.text;
  145. heatMapJson = JsonMapper.ToObject(subwayText);
  146. for (int i = 0; i < heatMapJson[Layer].Count; i++)
  147. {
  148. HeatMapDate Date = new HeatMapDate();
  149. Date.People = int.Parse(heatMapJson[Layer][i]["people"].ToString());
  150. Date.Range = int.Parse(heatMapJson[Layer][i]["range"].ToString());
  151. Date.Xpos = int.Parse(heatMapJson[Layer][i]["Xpos"].ToString());
  152. Date.Zpos = int.Parse(heatMapJson[Layer][i]["Zpos"].ToString());
  153. Date.Min = int.Parse(heatMapJson[Layer][i]["min"].ToString());
  154. heatMapDate.Add(Date);
  155. }
  156. heatControl.mapBigen(heatMapDate, High, gameObject);
  157. }
  158. }