|
- using System;
- using UnityEngine;
- using UnityEngine.Rendering;
-
- namespace SUISSEngine
- {
- public class GridOverlay : MonoBehaviour
- {
- public static Color TransparentColor
- {
- get
- {
- return GridOverlay.transparentColor;
- }
- }
-
- protected virtual void Awake()
- {
- this.InitializeColorsTexture();
- }
-
- protected virtual void Start()
- {
- if (this.grid == null)
- {
- return;
- }
- this.overlayObject = new GameObject("Overlay");
- this.overlayObject.transform.parent = base.transform;
- this.overlayObject.transform.localPosition = Vector3.zero;
- this.overlayMeshFilter = this.overlayObject.AddComponent<MeshFilter>();
- this.overlayMeshRenderer = this.overlayObject.AddComponent<MeshRenderer>();
- this.overlayMesh = this.CreateMesh(this.grid);
- this.overlayMeshFilter.sharedMesh = this.overlayMesh;
- this.overlayShader = this.CreateShader();
- this.overlayMaterial = this.CreateMaterial(this.overlayShader, this.grid, this.tileTexture, this.overlayColorsTexture);
- this.overlayMeshRenderer.material = this.overlayMaterial;
- this.overlayMeshRenderer.receiveShadows = false;
- this.overlayMeshRenderer.shadowCastingMode = ShadowCastingMode.Off;
- }
-
- protected virtual void LateUpdate()
- {
- if (this.overlayColorsChanged)
- {
- this.overlayColorsTexture.Apply(false, false);
- this.overlayColorsChanged = false;
- }
- }
-
- public Color GetColor(GridIndex index)
- {
- if (this.overlayColorsTexture == null && !this.InitializeColorsTexture())
- {
- return GridOverlay.transparentColor;
- }
- return this.overlayColorsTexture.GetPixel(index.u, index.v);
- }
-
- public void SetColor(GridIndex index, Color color)
- {
- if (this.overlayColorsTexture == null && !this.InitializeColorsTexture())
- {
- return;
- }
- this.overlayColorsTexture.SetPixel(index.u, index.v, color);
- this.overlayColorsChanged = true;
- }
-
- protected virtual Texture2D CreateColorsTexture(IsometricGrid grid)
- {
- Texture2D texture2D = new Texture2D(grid.Size.u, grid.Size.v, TextureFormat.ARGB32, false);
- texture2D.filterMode = FilterMode.Point;
- GridSize size = grid.Size;
- for (int i = 0; i < size.v; i++)
- {
- for (int j = 0; j < size.u; j++)
- {
- texture2D.SetPixel(j, i, GridOverlay.transparentColor);
- }
- }
- texture2D.Apply(false, false);
- return texture2D;
- }
-
- protected virtual Mesh CreateMesh(IsometricGrid grid)
- {
- Mesh mesh = new Mesh();
- Vector3[] vertices = new Vector3[]
- {
- new Vector3(0f, 0f, 0f),
- new Vector3((float)grid.Size.u * (grid.elementSize.x * 0.5f), (float)(-(float)grid.Size.u) * (grid.elementSize.y * 0.5f), 0f),
- 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),
- new Vector3((float)(-(float)grid.Size.v) * (grid.elementSize.x * 0.5f), (float)(-(float)grid.Size.v) * (grid.elementSize.y * 0.5f), 0f)
- };
- Vector2[] uv = new Vector2[]
- {
- new Vector2(0f, 0f),
- new Vector2(1f, 0f),
- new Vector2(1f, 1f),
- new Vector2(0f, 1f)
- };
- int[] triangles = new int[]
- {
- 0,
- 1,
- 2,
- 2,
- 3,
- 0
- };
- mesh.vertices = vertices;
- mesh.uv = uv;
- mesh.triangles = triangles;
- mesh.RecalculateNormals();
- return mesh;
- }
-
- protected virtual Shader CreateShader()
- {
- return Shader.Find("Engine/GridOverlay");
- }
-
- protected virtual Material CreateMaterial(Shader shader, IsometricGrid grid, Texture2D tileTexture, Texture2D colorsTexture)
- {
- Material material = new Material(shader);
- material.mainTexture = tileTexture;
- material.SetTexture("_ColorsTex", colorsTexture);
- material.SetFloat("_GridU", (float)grid.Size.u);
- material.SetFloat("_GridV", (float)grid.Size.v);
- return material;
- }
-
- private bool InitializeColorsTexture()
- {
- if (this.overlayColorsTexture == null)
- {
- if (this.grid == null)
- {
- UnityEngine.Debug.LogError("GridOverlay could not find an attached Grid.");
- return false;
- }
- this.overlayColorsTexture = this.CreateColorsTexture(this.grid);
- }
- return true;
- }
-
- private static Color transparentColor = new Color(0f, 0f, 0f, 0f);
-
- public Texture2D tileTexture;
-
- [SelfReference]
- public IsometricGrid grid;
-
- protected GameObject overlayObject;
-
- protected Texture2D overlayColorsTexture;
-
- protected bool overlayColorsChanged;
-
- protected MeshFilter overlayMeshFilter;
-
- protected MeshRenderer overlayMeshRenderer;
-
- protected Mesh overlayMesh;
-
- protected Shader overlayShader;
-
- protected Material overlayMaterial;
- }
- }
|