You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

172 lines
4.6 KiB

  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. namespace SUISSEngine
  5. {
  6. public class GridOverlay : MonoBehaviour
  7. {
  8. public static Color TransparentColor
  9. {
  10. get
  11. {
  12. return GridOverlay.transparentColor;
  13. }
  14. }
  15. protected virtual void Awake()
  16. {
  17. this.InitializeColorsTexture();
  18. }
  19. protected virtual void Start()
  20. {
  21. if (this.grid == null)
  22. {
  23. return;
  24. }
  25. this.overlayObject = new GameObject("Overlay");
  26. this.overlayObject.transform.parent = base.transform;
  27. this.overlayObject.transform.localPosition = Vector3.zero;
  28. this.overlayMeshFilter = this.overlayObject.AddComponent<MeshFilter>();
  29. this.overlayMeshRenderer = this.overlayObject.AddComponent<MeshRenderer>();
  30. this.overlayMesh = this.CreateMesh(this.grid);
  31. this.overlayMeshFilter.sharedMesh = this.overlayMesh;
  32. this.overlayShader = this.CreateShader();
  33. this.overlayMaterial = this.CreateMaterial(this.overlayShader, this.grid, this.tileTexture, this.overlayColorsTexture);
  34. this.overlayMeshRenderer.material = this.overlayMaterial;
  35. this.overlayMeshRenderer.receiveShadows = false;
  36. this.overlayMeshRenderer.shadowCastingMode = ShadowCastingMode.Off;
  37. }
  38. protected virtual void LateUpdate()
  39. {
  40. if (this.overlayColorsChanged)
  41. {
  42. this.overlayColorsTexture.Apply(false, false);
  43. this.overlayColorsChanged = false;
  44. }
  45. }
  46. public Color GetColor(GridIndex index)
  47. {
  48. if (this.overlayColorsTexture == null && !this.InitializeColorsTexture())
  49. {
  50. return GridOverlay.transparentColor;
  51. }
  52. return this.overlayColorsTexture.GetPixel(index.u, index.v);
  53. }
  54. public void SetColor(GridIndex index, Color color)
  55. {
  56. if (this.overlayColorsTexture == null && !this.InitializeColorsTexture())
  57. {
  58. return;
  59. }
  60. this.overlayColorsTexture.SetPixel(index.u, index.v, color);
  61. this.overlayColorsChanged = true;
  62. }
  63. protected virtual Texture2D CreateColorsTexture(IsometricGrid grid)
  64. {
  65. Texture2D texture2D = new Texture2D(grid.Size.u, grid.Size.v, TextureFormat.ARGB32, false);
  66. texture2D.filterMode = FilterMode.Point;
  67. GridSize size = grid.Size;
  68. for (int i = 0; i < size.v; i++)
  69. {
  70. for (int j = 0; j < size.u; j++)
  71. {
  72. texture2D.SetPixel(j, i, GridOverlay.transparentColor);
  73. }
  74. }
  75. texture2D.Apply(false, false);
  76. return texture2D;
  77. }
  78. protected virtual Mesh CreateMesh(IsometricGrid grid)
  79. {
  80. Mesh mesh = new Mesh();
  81. Vector3[] vertices = new Vector3[]
  82. {
  83. new Vector3(0f, 0f, 0f),
  84. new Vector3((float)grid.Size.u * (grid.elementSize.x * 0.5f), (float)(-(float)grid.Size.u) * (grid.elementSize.y * 0.5f), 0f),
  85. new Vector3((float)(grid.Size.u - grid.Size.v) * (grid.elementSize.x * 0.5f), (float)(-(float)(grid.Size.u + grid.Size.v)) * (grid.elementSize.y * 0.5f), 0f),
  86. new Vector3((float)(-(float)grid.Size.v) * (grid.elementSize.x * 0.5f), (float)(-(float)grid.Size.v) * (grid.elementSize.y * 0.5f), 0f)
  87. };
  88. Vector2[] uv = new Vector2[]
  89. {
  90. new Vector2(0f, 0f),
  91. new Vector2(1f, 0f),
  92. new Vector2(1f, 1f),
  93. new Vector2(0f, 1f)
  94. };
  95. int[] triangles = new int[]
  96. {
  97. 0,
  98. 1,
  99. 2,
  100. 2,
  101. 3,
  102. 0
  103. };
  104. mesh.vertices = vertices;
  105. mesh.uv = uv;
  106. mesh.triangles = triangles;
  107. mesh.RecalculateNormals();
  108. return mesh;
  109. }
  110. protected virtual Shader CreateShader()
  111. {
  112. return Shader.Find("Engine/GridOverlay");
  113. }
  114. protected virtual Material CreateMaterial(Shader shader, IsometricGrid grid, Texture2D tileTexture, Texture2D colorsTexture)
  115. {
  116. Material material = new Material(shader);
  117. material.mainTexture = tileTexture;
  118. material.SetTexture("_ColorsTex", colorsTexture);
  119. material.SetFloat("_GridU", (float)grid.Size.u);
  120. material.SetFloat("_GridV", (float)grid.Size.v);
  121. return material;
  122. }
  123. private bool InitializeColorsTexture()
  124. {
  125. if (this.overlayColorsTexture == null)
  126. {
  127. if (this.grid == null)
  128. {
  129. UnityEngine.Debug.LogError("GridOverlay could not find an attached Grid.");
  130. return false;
  131. }
  132. this.overlayColorsTexture = this.CreateColorsTexture(this.grid);
  133. }
  134. return true;
  135. }
  136. private static Color transparentColor = new Color(0f, 0f, 0f, 0f);
  137. public Texture2D tileTexture;
  138. [SelfReference]
  139. public IsometricGrid grid;
  140. protected GameObject overlayObject;
  141. protected Texture2D overlayColorsTexture;
  142. protected bool overlayColorsChanged;
  143. protected MeshFilter overlayMeshFilter;
  144. protected MeshRenderer overlayMeshRenderer;
  145. protected Mesh overlayMesh;
  146. protected Shader overlayShader;
  147. protected Material overlayMaterial;
  148. }
  149. }