heatControl.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**************************************************
  2. FileName:ObjectPool
  3. Date:19/12/5
  4. Version:0.1
  5. Function List:热力图颜色赋值
  6. ****************************************************
  7. ****************************************************/
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. public class heatControl : MonoBehaviour
  13. {
  14. List<float> heatPointValue = new List<float>(); //所有顶点对应的颜色的值
  15. List<Vector3> heatPointVec = new List<Vector3>();//所有顶点
  16. MeshFilter meshFilter;
  17. public void mapBigen(List<HeatMapDate> heatMapDate, float High, GameObject self)
  18. {
  19. ReadMash(self); //得到所有的顶点
  20. Inject(HeatPointValue(), heatMapDate, High);
  21. }
  22. public void Inject(List<float> temperature, List<HeatMapDate> LayerDate, float High)
  23. {
  24. // 算法添加高温点
  25. for (int i = 0; i < LayerDate.Count; i++)
  26. {
  27. RandomTeamperatureone(LayerDate[i].People, LayerDate[i].Range, new Vector3(LayerDate[i].Xpos, 0, LayerDate[i].Zpos), ref temperature);
  28. }
  29. Generate(0.1f);
  30. Genereat(0.1f, LayerDate[0].Range, new Vector3(LayerDate[0].Xpos, 0, LayerDate[0].Zpos));
  31. //
  32. // 赋值颜色
  33. AddVertexColor(meshFilter);
  34. }
  35. public void CreateMesh()
  36. {
  37. }
  38. //得到所有的顶点
  39. void ReadMash(GameObject self)
  40. {
  41. meshFilter = self.transform.GetComponent<MeshFilter>();
  42. int Length = meshFilter.mesh.vertices.Length;
  43. for (int i = 0; i < Length; i++)
  44. {
  45. heatPointVec.Add(meshFilter.mesh.vertices[i]);
  46. //Debug.Log(heatPointVec[i]);
  47. }
  48. }
  49. // 初始化算法
  50. private List<float> HeatPointValue()
  51. {
  52. heatPointValue.Clear();
  53. for (int i = 0; i < heatPointVec.Count; i++)
  54. {
  55. // 假定正常人数为40人低于40人均为正常温度
  56. heatPointValue.Add(40);
  57. }
  58. return heatPointValue;
  59. }
  60. /// <summary>
  61. /// 得到点属性数据
  62. /// </summary>
  63. /// <param name="MaxPeopleNum">最大人数</param>
  64. /// <param name="range">影响范围</param>
  65. /// <param name="pox">x位置</param>
  66. /// <param name="temperatures">点属性数据</param>
  67. private void RandomTeamperatureone(float MaxPeopleNum, int range, Vector3 pos, ref List<float> temperatures)
  68. {
  69. for (int i = 0; i < temperatures.Count; i++)
  70. {
  71. int num = i;
  72. float distance = Mathf.Sqrt(Mathf.Pow(Vector3.Distance(heatPointVec[i], pos), 2)); //算距离
  73. if (distance <= range)
  74. {
  75. float ratio = 1 - (distance / range);//ratio等于0到1
  76. float temp = ratio * MaxPeopleNum;
  77. if (MaxPeopleNum > 160)
  78. {
  79. temp = ratio * MaxPeopleNum*1.5f ; //1.1f是基数,调整美观的
  80. }
  81. if (temp > MaxPeopleNum)
  82. {
  83. temp = MaxPeopleNum;
  84. }
  85. // 只有比当前点人数高才选择覆盖
  86. if (temp > heatPointValue[num])
  87. heatPointValue[num] = temp;
  88. }
  89. }
  90. //for (int i = 0; i < temperatures.Count; i++)
  91. //{
  92. // int num = i;
  93. // float distance = Mathf.Sqrt(Mathf.Pow(Vector3.Distance(heatPointVec[i], pos), 2)); //算距离
  94. // if (distance <= range * 1.5f && distance > range)
  95. // {
  96. // float ratio = 1 - (distance / range);//ratio等于0到1
  97. // float temp = ratio * MaxPeopleNum;
  98. // // 只有比当前点人数高才选择覆盖
  99. // if (temp > heatPointValue[num])
  100. // heatPointValue[num] = temp;
  101. // }
  102. // else if (distance < range)
  103. // {
  104. // float ratio = 1 - (distance / range);//ratio等于0到1
  105. // if (ratio < 0.7f)
  106. // {
  107. // ratio = 0.7f;
  108. // }
  109. // float temp = ratio * MaxPeopleNum;
  110. // // 只有比当前点人数高才选择覆盖
  111. // if (temp > heatPointValue[num])
  112. // heatPointValue[num] = temp;
  113. // }
  114. //}
  115. }
  116. /// <summary>
  117. /// 增加颜色数据
  118. /// </summary>
  119. /// <param name="meshFilter"></param>
  120. private void AddVertexColor(MeshFilter meshFilter)
  121. {
  122. Color[] colors = new Color[heatPointValue.Count];
  123. for (int i = 0; i < heatPointValue.Count; i++)
  124. {
  125. colors[i] = CalcColor(heatPointValue[i]);
  126. }
  127. meshFilter.mesh.colors = colors;
  128. }
  129. Color CalcColor(float temperature)
  130. {
  131. Color colorOne = new Color();
  132. if (temperature <=40)
  133. {
  134. //colorOne = Color.Lerp(new Color(1, 0, 1, 0), new Color(0, 1, 1, 0), (temperature) * 0.025f);//无
  135. colorOne = Color.Lerp(new Color(0, 1, 1, 0f), new Color(0, 1, 0), (temperature - 30) * 0.025f);//篮
  136. }
  137. else if (temperature >40 && temperature < 80)
  138. {
  139. colorOne = Color.Lerp(new Color(0, 1, 1, 0f), new Color(0, 1, 0), (temperature - 40) * 0.025f);//篮
  140. }
  141. else if (temperature >= 80 && temperature < 100)
  142. {
  143. // Debug.Log((temperature - 80) * 0.025f);
  144. colorOne = Color.Lerp(new Color(0, 1, 0), new Color(1, 1, 0), (temperature - 80) * 0.025f); //绿
  145. }
  146. else if (temperature >= 100 && temperature < 160)
  147. {
  148. colorOne = Color.Lerp(new Color(1f, 1f, 0f), new Color(1f, 0f, 0f), (temperature - 120) * 0.025f); //黄=>红
  149. }
  150. else
  151. {
  152. colorOne = new Color(1f, 0f, 0f, 1f); //红
  153. }
  154. return colorOne;
  155. }
  156. /// <summary>
  157. /// 生成mesh
  158. /// </summary>
  159. /// <param name="high">基本高度</param>
  160. public void Generate(float high)
  161. {
  162. Vector3[] vertices = new Vector3[heatPointVec.Count];
  163. for (int i = 0; i < heatPointVec.Count; i++)
  164. {
  165. vertices[i] = new Vector3(heatPointVec[i].x, heatPointValue[i] *high, heatPointVec[i].z);
  166. }
  167. meshFilter.mesh.vertices = vertices;
  168. }
  169. public void Genereat(float high, float range ,Vector3 pos)
  170. {
  171. Vector3[] vertices = new Vector3[heatPointVec.Count];
  172. for (int i = 0; i < heatPointVec.Count; i++)
  173. {
  174. vertices[i] = new Vector3(heatPointVec[i].x, heatPointValue[i] * high, heatPointVec[i].z);
  175. float distance = Mathf.Sqrt(Mathf.Pow(Vector3.Distance(heatPointVec[i], pos), 2)); //算距离
  176. if (heatPointValue[i]>40&& distance<= range)
  177. {
  178. // 生成球形的原理,是求出这个点在球体上的高度。 //range 球的半径(斜边) distance 是平面上点到圆心的距离(其中一条直角边) 求出高(另一条直角边)
  179. float y = Mathf.Sqrt((Mathf.Pow(range, 2) - Mathf.Pow(distance, 2)));
  180. vertices[i] = new Vector3(heatPointVec[i].x, y, heatPointVec[i].z);
  181. }
  182. }
  183. meshFilter.mesh.vertices = vertices;
  184. }
  185. }