using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace TowerDevil { // All contact shadows share a mesh underneath the removable and animated pieces. [RequireComponent(typeof(CanvasRenderer))] public sealed class PieceShadows : MaskableGraphic { Dictionary figures; const int Segments = 16; static readonly Vector2[] circle = BuildCircle(); static Vector2[] BuildCircle() { var points = new Vector2[Segments]; for (int i = 0; i < Segments; i++) { float angle = i * 2 * Mathf.PI / Segments; points[i] = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)); } return points; } internal void Bind(Dictionary pieces) => figures = pieces; protected override void OnPopulateMesh(VertexHelper mesh) { mesh.Clear(); if (figures == null) return; foreach (var pair in figures) { RectTransform rt = pair.Value.rectTransform; float width = rt.rect.width * rt.localScale.x; Vector2 center = rt.anchoredPosition + new Vector2(width * .055f, width * .055f); Vector2 radius = new Vector2(width * .54f, width * .15f); int start = mesh.currentVertCount; mesh.AddVert(center, new Color32(12, 8, 13, 179), Vector2.zero); for (int i = 0; i < Segments; i++) { Vector2 d = Vector2.Scale(circle[i], radius); mesh.AddVert(center + d * .47f, new Color32(14, 10, 14, 142), Vector2.zero); mesh.AddVert(center + d, new Color32(14, 10, 14, 0), Vector2.zero); } for (int i = 0; i < Segments; i++) { int a = start + 1 + i * 2, b = start + 1 + ((i + 1) % Segments) * 2; mesh.AddTriangle(start, a, b); mesh.AddTriangle(a, a + 1, b); mesh.AddTriangle(a + 1, b + 1, b); } } } } }