HeatMapVisual.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Utility;
  5. namespace KairoEngine.Grids
  6. {
  7. public class HeatMapVisual : MonoBehaviour
  8. {
  9. private Grid<HeatMapGridObject> grid;
  10. private Mesh mesh;
  11. private bool updateMesh = false;
  12. private void Awake()
  13. {
  14. mesh = new Mesh();
  15. GetComponent<MeshFilter>().mesh = mesh;
  16. }
  17. public void SetGrid(Grid<HeatMapGridObject> grid)
  18. {
  19. this.grid = grid;
  20. UpdateHeatMapVisual();
  21. grid.OnGridObjectChanged += Grid_OnGridObjectChanged;
  22. }
  23. private void UpdateHeatMapVisual()
  24. {
  25. MeshUtils.CreateEmptyMeshArrays(grid.GetWidth() * grid.GetHeight(), out Vector3[] vertices, out Vector2[] uv, out int[] triangles);
  26. for (int x = 0; x < grid.GetWidth(); x++)
  27. {
  28. for (int y = 0; y < grid.GetHeight(); y++)
  29. {
  30. int index = x * grid.GetHeight() + y;
  31. Vector3 quadSize = grid.GetQuadSize();
  32. HeatMapGridObject gridObject = grid.GetObject(x, y);
  33. Vector2 gridValueUV = new Vector2(gridObject.GetValueNormalized(), 0f);
  34. Vector3 pos = grid.GetWorldPosition(x, y) + (quadSize * 0.5f);
  35. float rot = 0f;
  36. MeshUtils.AddToMeshArrays(vertices, uv, triangles, index, pos, rot, quadSize, gridValueUV, gridValueUV);
  37. }
  38. }
  39. mesh.vertices = vertices;
  40. mesh.uv = uv;
  41. mesh.triangles = triangles;
  42. }
  43. private void Grid_OnGridObjectChanged(object sender, Grid<HeatMapGridObject>.OnGridObjectChangedEventArgs e) => updateMesh = true;
  44. private void LateUpdate()
  45. {
  46. if(updateMesh)
  47. {
  48. updateMesh = false;
  49. Debug.Log("Grid event fired");
  50. UpdateHeatMapVisual();
  51. }
  52. }
  53. public void AddValue(int x, int y, int value)
  54. {
  55. HeatMapGridObject obj = grid.GetObject(x, y);
  56. if(obj != null)
  57. {
  58. obj.AddValue(value);
  59. //grid.SetObject(x, y, obj);
  60. }
  61. }
  62. public void AddValue(Vector3 worldPosition, int value, int fullValueRange, int totalRange)
  63. {
  64. int lowerValueAmmount = Mathf.RoundToInt((float)value / (totalRange - fullValueRange));
  65. grid.GetGridPosition(worldPosition, out int originX, out int originY);
  66. for (int x = 0; x < totalRange; x++)
  67. {
  68. for (int y = 0; y < totalRange - x; y++)
  69. {
  70. int radius = x + y;
  71. int addValueAmmount = value;
  72. if(radius > fullValueRange) addValueAmmount -= lowerValueAmmount * (radius - fullValueRange);
  73. AddValue(originX + x, originY + y, addValueAmmount);
  74. if(x != 0) AddValue(originX - x, originY + y, addValueAmmount);
  75. if(y != 0) AddValue(originX + x, originY - y, addValueAmmount);
  76. if(y != 0 && x != 0) AddValue(originX - x, originY - y, addValueAmmount);
  77. }
  78. }
  79. }
  80. }
  81. public class HeatMapGridObject
  82. {
  83. private const int MIN = 0;
  84. private const int MAX = 100;
  85. private Grid<HeatMapGridObject> grid;
  86. private int x;
  87. private int y;
  88. public int value;
  89. public HeatMapGridObject(Grid<HeatMapGridObject> grid, int x, int y)
  90. {
  91. this.grid = grid;
  92. this.x = x;
  93. this.y = y;
  94. }
  95. public void AddValue(int addValue)
  96. {
  97. value += addValue;
  98. value = Mathf.Clamp(value, MIN, MAX);
  99. grid.TriggerGridObjectChanged(x, y);
  100. }
  101. public float GetValueNormalized() => (float)value / MAX;
  102. public override string ToString() => value.ToString();
  103. }
  104. }