| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using UnityEngine;
- using UnityEngine.UI;
- namespace TowerDevil
- {
- // The castle and paving occupy one continuous painting. The two flexible margins
- // absorb aspect-ratio changes while the castle base stays aligned with the seal HUD.
- [RequireComponent(typeof(CanvasRenderer))]
- public sealed class CourtyardBackdrop : MaskableGraphic
- {
- const float Crown = .10f, Base = .29f;
- internal const float SealU = .5f, SealV = .235f;
- static readonly float[] columns = { 0, .12f, .23f, .36f, .5f, .64f, .77f, .88f, 1 };
- static readonly float[] rows = { 0, Crown * .5f, Crown, .16f, SealV, Base, Base + .055f, .70f, .79f, .88f, .95f, 1 };
- Texture2D painting;
- float crownDepth, baseDepth, foregroundWidth;
- Vector2 shake;
- public override Texture mainTexture => painting ? painting : base.mainTexture;
- internal void SetPainting(Texture2D art)
- {
- painting = art;
- raycastTarget = false;
- SetMaterialDirty();
- }
- internal void Fit(float crownY, float baseY, float visibleWidth)
- {
- crownDepth = crownY;
- baseDepth = baseY;
- foregroundWidth = visibleWidth;
- SetVerticesDirty();
- }
- internal Vector2 Point(float u, float v)
- {
- float depth;
- if (v <= Crown) depth = Mathf.Lerp(0, crownDepth, v / Crown);
- else if (v <= Base) depth = Mathf.Lerp(crownDepth, baseDepth, (v - Crown) / (Base - Crown));
- else depth = Mathf.Lerp(baseDepth, rectTransform.rect.height, (v - Base) / (1 - Base));
- float artworkWidth = rectTransform.rect.width;
- // Bring the foreground rock shoulders into view on narrow phones.
- float width = Mathf.SmoothStep(artworkWidth, foregroundWidth, Mathf.InverseLerp(.70f, .95f, v));
- return new Vector2(artworkWidth * .5f + (u - .5f) * width, -depth);
- }
- internal void MoveGate(Vector2 offset)
- {
- if (shake == offset) return;
- shake = offset;
- SetVerticesDirty();
- }
- internal void RegisterFractures(RectTransform overlay)
- {
- // Bounds of the central gatehouse within the backdrop, measured in art UVs from the top.
- Vector2 a = Point(.20f, .094f), b = Point(.80f, .272f);
- overlay.anchoredPosition = a;
- overlay.sizeDelta = new Vector2(b.x - a.x, a.y - b.y);
- }
- protected override void OnPopulateMesh(VertexHelper mesh)
- {
- mesh.Clear();
- for (int r = 0; r < rows.Length; r++)
- {
- float v = rows[r];
- float vertical = Mathf.InverseLerp(Crown * .5f, Crown, v) *
- (1 - Mathf.InverseLerp(Base, Base + .055f, v));
- for (int c = 0; c < columns.Length; c++)
- {
- float u = columns[c];
- float lateral = Mathf.InverseLerp(0, .20f, Mathf.Min(u, 1 - u));
- mesh.AddVert(Point(u,v) + shake * (vertical * lateral), color, new Vector2(u, 1 - v));
- if (r == 0 || c == 0) continue;
- int d = r * columns.Length + c;
- int a = d - columns.Length - 1;
- mesh.AddTriangle(a, a + 1, d);
- mesh.AddTriangle(a, d, d - 1);
- }
- }
- }
- }
- }
|