LobbyLogoAnimation.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace DevilLink2.UI
  6. {
  7. // Private logo rig: static brand lettering, two hinged wings, independent localized light passes.
  8. public sealed class LobbyLogoAnimation : MonoBehaviour
  9. {
  10. private const float Width = 472, Height = 223, IntroSeconds = .9f, LoopSeconds = 4.4f;
  11. private const string Folder = "LobbyLogoAnimation/";
  12. private UIScreen lobby;
  13. private RectTransform artSpace, body;
  14. private Image leftWing, rightWing, crystal, halo, metalA, metalB, magicA, magicB;
  15. private readonly Image[] stars = new Image[3];
  16. private Sprite[] metalFrames, magicFrames;
  17. private float clock, introTime;
  18. private bool ready, introCompleted;
  19. public static void Install(LobbyScreen owner, Image original)
  20. { Install((UIScreen)owner, original); }
  21. // Reuse the same rig on Start; existing Lobby calls retain their original entry point.
  22. public static void Install(UIScreen owner, Image original)
  23. {
  24. if (!original || original.GetComponent<LobbyLogoAnimation>()) return;
  25. string[] names = { "Wordmark_Core", "Wing_Left", "Wing_Right", "Crystal_Core", "Hinge_Covers", "Crystal_Halo", "Crystal_Star" };
  26. var sprites = new Sprite[names.Length];
  27. for (int i = 0; i < names.Length; i++) {
  28. sprites[i] = Resources.Load<Sprite>(Folder + names[i]);
  29. if (!sprites[i]) { Debug.LogWarning("Logo layers not imported; retaining original logo: " + names[i], original); return; }
  30. }
  31. var all = Resources.LoadAll<Sprite>(Folder + "Logo_LightLoops");
  32. var metal = Frames(all, "Metal_");
  33. var magic = Frames(all, "Magic_");
  34. if (metal.Length != 32 || magic.Length != 32) {
  35. Debug.LogWarning("Logo light atlas must contain 32 metal and 32 magic frames; retaining original logo.", original);
  36. return;
  37. }
  38. var rig = original.gameObject.AddComponent<LobbyLogoAnimation>();
  39. rig.lobby = owner; rig.metalFrames = metal; rig.magicFrames = magic;
  40. rig.artSpace = UIBuilder.Rect("LogoArtSpace", original.transform, 0, 0, Width, Height);
  41. rig.artSpace.anchorMin = rig.artSpace.anchorMax = rig.artSpace.pivot = new Vector2(.5f, .5f);
  42. rig.artSpace.anchoredPosition = Vector2.zero;
  43. rig.leftWing = rig.Layer("LeftHingedWing", sprites[1], rig.artSpace);
  44. rig.rightWing = rig.Layer("RightHingedWing", sprites[2], rig.artSpace);
  45. Hinge(rig.leftWing.rectTransform, 139, 81); Hinge(rig.rightWing.rectTransform, 340, 81);
  46. rig.body = UIBuilder.Rect("FixedBrandBody", rig.artSpace, 0, 0, Width, Height);
  47. rig.Layer("OriginalWordmark", sprites[0], rig.body);
  48. rig.Layer("OriginalWingMounts", sprites[4], rig.body);
  49. rig.halo = rig.Layer("CrystalHalo", sprites[5], rig.body);
  50. rig.crystal = rig.Layer("OriginalCrystal", sprites[3], rig.body);
  51. rig.magicA = rig.Layer("CrystalRibbonEnergyA", magic[0], rig.body);
  52. rig.magicB = rig.Layer("CrystalRibbonEnergyB", magic[1], rig.body);
  53. rig.metalA = rig.Layer("LetterSheenA", metal[0], rig.body);
  54. rig.metalB = rig.Layer("LetterSheenB", metal[1], rig.body);
  55. float[] x = { 235, 235, 363 }, y = { 39, 161, 174 };
  56. for (int i = 0; i < rig.stars.Length; i++) {
  57. var star = LobbyArtView.Picture("GemSpark" + i, rig.body, sprites[6], x[i] - 3, y[i] - 3, 6, 6);
  58. star.raycastTarget = false; star.material = null; rig.stars[i] = star;
  59. }
  60. rig.ready = true; rig.Fit(); rig.ApplyPose();
  61. // Original image is replaced only after the complete rig has its initial pose.
  62. original.enabled = false;
  63. }
  64. private static Sprite[] Frames(Sprite[] all, string prefix)
  65. {
  66. var result = new List<Sprite>();
  67. foreach (var sprite in all) if (sprite.name.StartsWith(prefix, StringComparison.Ordinal)) result.Add(sprite);
  68. result.Sort((a, b) => string.CompareOrdinal(a.name, b.name));
  69. return result.ToArray();
  70. }
  71. private Image Layer(string name, Sprite sprite, Transform parent)
  72. {
  73. var image = LobbyArtView.Picture(name, parent, sprite, 0, 0, Width, Height);
  74. image.preserveAspect = false; image.raycastTarget = false; image.material = null; image.color = Color.white;
  75. return image;
  76. }
  77. private static void Hinge(RectTransform r, float x, float y)
  78. {
  79. r.pivot = new Vector2(x / Width, 1 - y / Height);
  80. r.anchoredPosition = new Vector2(x, -y);
  81. }
  82. private void Fit()
  83. {
  84. if (!artSpace) return;
  85. var rect = ((RectTransform)transform).rect;
  86. // Static aspect fitting only; this scale is never an animation parameter.
  87. artSpace.localScale = Vector3.one * Mathf.Min(rect.width / Width, rect.height / Height);
  88. }
  89. private void OnRectTransformDimensionsChange() { if (ready) Fit(); }
  90. private void Update()
  91. {
  92. if (!ready || !lobby || !lobby.IsVisible) return;
  93. float dt = Time.unscaledDeltaTime;
  94. clock += dt;
  95. if (!introCompleted) {
  96. introTime = Mathf.Min(IntroSeconds, introTime + dt);
  97. if (introTime >= IntroSeconds) introCompleted = true;
  98. }
  99. ApplyPose();
  100. }
  101. private void ApplyPose()
  102. {
  103. if (!ready) return;
  104. float t = introCompleted ? 1 : Mathf.Clamp01(introTime / IntroSeconds);
  105. float unfold = 1 - Mathf.Pow(1 - t, 3);
  106. float phase = clock * Mathf.PI * 2 / LoopSeconds;
  107. float idleWeight = Mathf.SmoothStep(0, 1, Mathf.InverseLerp(.3f, 1, t));
  108. float wingAngle = 11 * (1 - unfold) + 3.2f * Mathf.Sin(phase) * idleWeight;
  109. leftWing.rectTransform.localRotation = Quaternion.Euler(0, 0, wingAngle);
  110. rightWing.rectTransform.localRotation = Quaternion.Euler(0, 0, -wingAngle);
  111. // Lettering only settles 2.5 source pixels (about 1.6 logical pixels), no alpha/scale reveal.
  112. body.anchoredPosition = new Vector2(0, -2.5f * (1 - unfold));
  113. float gemLevel = Mathf.Lerp(.38f, .94f + .06f * Mathf.Sin(phase), unfold);
  114. crystal.color = new Color(gemLevel, gemLevel, gemLevel, 1);
  115. halo.color = new Color(1, 1, 1, unfold * (.55f + .25f * Mathf.Sin(phase)));
  116. float metal = introCompleted ? Mathf.Repeat((clock - IntroSeconds) / LoopSeconds, 1) : Mathf.Clamp01((introTime - .16f) / .68f);
  117. Blend(metalA, metalB, metalFrames, metal);
  118. Blend(magicA, magicB, magicFrames, Mathf.Repeat(clock / LoopSeconds, 1));
  119. // Only local glints change opacity. Brand body, wings and crystal image alpha stay one.
  120. for (int i = 0; i < stars.Length; i++) {
  121. float glint = Mathf.Pow(Mathf.Max(0, Mathf.Sin(phase + i * 1.8f)), 12);
  122. stars[i].color = new Color(1, .85f, .62f, glint * .56f * unfold);
  123. }
  124. }
  125. private static void Blend(Image a, Image b, Sprite[] frames, float normalized)
  126. {
  127. float p = Mathf.Clamp01(normalized) * frames.Length;
  128. int frame = Mathf.Min(frames.Length - 1, Mathf.FloorToInt(p));
  129. float f = p - Mathf.Floor(p);
  130. if (normalized >= 1) f = 0;
  131. if (a.sprite != frames[frame]) a.sprite = frames[frame];
  132. if (b.sprite != frames[(frame + 1) % frames.Length]) b.sprite = frames[(frame + 1) % frames.Length];
  133. a.color = new Color(1, 1, 1, 1 - f); b.color = new Color(1, 1, 1, f);
  134. }
  135. private void OnEnable()
  136. {
  137. if (!ready) return;
  138. if (!introCompleted) { clock = 0; introTime = 0; }
  139. Fit(); ApplyPose();
  140. }
  141. private void OnDisable()
  142. {
  143. if (!ready) return;
  144. leftWing.rectTransform.localRotation = rightWing.rectTransform.localRotation = Quaternion.identity;
  145. body.anchoredPosition = Vector2.zero;
  146. // The clock is paused, not advanced off-screen. Interrupted first entrances restart cleanly.
  147. if (!introCompleted) { clock = 0; introTime = 0; }
  148. }
  149. }
  150. }