CourtyardBackdrop.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace TowerDevil
  4. {
  5. // The castle and paving occupy one continuous painting. The two flexible margins
  6. // absorb aspect-ratio changes while the castle base stays aligned with the seal HUD.
  7. [RequireComponent(typeof(CanvasRenderer))]
  8. public sealed class CourtyardBackdrop : MaskableGraphic
  9. {
  10. const float Crown = .10f, Base = .29f;
  11. internal const float SealU = .5f, SealV = .235f;
  12. static readonly float[] columns = { 0, .12f, .23f, .36f, .5f, .64f, .77f, .88f, 1 };
  13. static readonly float[] rows = { 0, Crown * .5f, Crown, .16f, SealV, Base, Base + .055f, .70f, .79f, .88f, .95f, 1 };
  14. Texture2D painting;
  15. float crownDepth, baseDepth, foregroundWidth;
  16. Vector2 shake;
  17. public override Texture mainTexture => painting ? painting : base.mainTexture;
  18. internal void SetPainting(Texture2D art)
  19. {
  20. painting = art;
  21. raycastTarget = false;
  22. SetMaterialDirty();
  23. }
  24. internal void Fit(float crownY, float baseY, float visibleWidth)
  25. {
  26. crownDepth = crownY;
  27. baseDepth = baseY;
  28. foregroundWidth = visibleWidth;
  29. SetVerticesDirty();
  30. }
  31. internal Vector2 Point(float u, float v)
  32. {
  33. float depth;
  34. if (v <= Crown) depth = Mathf.Lerp(0, crownDepth, v / Crown);
  35. else if (v <= Base) depth = Mathf.Lerp(crownDepth, baseDepth, (v - Crown) / (Base - Crown));
  36. else depth = Mathf.Lerp(baseDepth, rectTransform.rect.height, (v - Base) / (1 - Base));
  37. float artworkWidth = rectTransform.rect.width;
  38. // Bring the foreground rock shoulders into view on narrow phones.
  39. float width = Mathf.SmoothStep(artworkWidth, foregroundWidth, Mathf.InverseLerp(.70f, .95f, v));
  40. return new Vector2(artworkWidth * .5f + (u - .5f) * width, -depth);
  41. }
  42. internal void MoveGate(Vector2 offset)
  43. {
  44. if (shake == offset) return;
  45. shake = offset;
  46. SetVerticesDirty();
  47. }
  48. internal void RegisterFractures(RectTransform overlay)
  49. {
  50. // Bounds of the central gatehouse within the backdrop, measured in art UVs from the top.
  51. Vector2 a = Point(.20f, .094f), b = Point(.80f, .272f);
  52. overlay.anchoredPosition = a;
  53. overlay.sizeDelta = new Vector2(b.x - a.x, a.y - b.y);
  54. }
  55. protected override void OnPopulateMesh(VertexHelper mesh)
  56. {
  57. mesh.Clear();
  58. for (int r = 0; r < rows.Length; r++)
  59. {
  60. float v = rows[r];
  61. float vertical = Mathf.InverseLerp(Crown * .5f, Crown, v) *
  62. (1 - Mathf.InverseLerp(Base, Base + .055f, v));
  63. for (int c = 0; c < columns.Length; c++)
  64. {
  65. float u = columns[c];
  66. float lateral = Mathf.InverseLerp(0, .20f, Mathf.Min(u, 1 - u));
  67. mesh.AddVert(Point(u,v) + shake * (vertical * lateral), color, new Vector2(u, 1 - v));
  68. if (r == 0 || c == 0) continue;
  69. int d = r * columns.Length + c;
  70. int a = d - columns.Length - 1;
  71. mesh.AddTriangle(a, a + 1, d);
  72. mesh.AddTriangle(a, d, d - 1);
  73. }
  74. }
  75. }
  76. }
  77. }