| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- namespace DevilLink2.UI
- {
- // Private logo rig: static brand lettering, two hinged wings, independent localized light passes.
- public sealed class LobbyLogoAnimation : MonoBehaviour
- {
- private const float Width = 472, Height = 223, IntroSeconds = .9f, LoopSeconds = 4.4f;
- private const string Folder = "LobbyLogoAnimation/";
- private UIScreen lobby;
- private RectTransform artSpace, body;
- private Image leftWing, rightWing, crystal, halo, metalA, metalB, magicA, magicB;
- private readonly Image[] stars = new Image[3];
- private Sprite[] metalFrames, magicFrames;
- private float clock, introTime;
- private bool ready, introCompleted;
- public static void Install(LobbyScreen owner, Image original)
- { Install((UIScreen)owner, original); }
- // Reuse the same rig on Start; existing Lobby calls retain their original entry point.
- public static void Install(UIScreen owner, Image original)
- {
- if (!original || original.GetComponent<LobbyLogoAnimation>()) return;
- string[] names = { "Wordmark_Core", "Wing_Left", "Wing_Right", "Crystal_Core", "Hinge_Covers", "Crystal_Halo", "Crystal_Star" };
- var sprites = new Sprite[names.Length];
- for (int i = 0; i < names.Length; i++) {
- sprites[i] = Resources.Load<Sprite>(Folder + names[i]);
- if (!sprites[i]) { Debug.LogWarning("Logo layers not imported; retaining original logo: " + names[i], original); return; }
- }
- var all = Resources.LoadAll<Sprite>(Folder + "Logo_LightLoops");
- var metal = Frames(all, "Metal_");
- var magic = Frames(all, "Magic_");
- if (metal.Length != 32 || magic.Length != 32) {
- Debug.LogWarning("Logo light atlas must contain 32 metal and 32 magic frames; retaining original logo.", original);
- return;
- }
- var rig = original.gameObject.AddComponent<LobbyLogoAnimation>();
- rig.lobby = owner; rig.metalFrames = metal; rig.magicFrames = magic;
- rig.artSpace = UIBuilder.Rect("LogoArtSpace", original.transform, 0, 0, Width, Height);
- rig.artSpace.anchorMin = rig.artSpace.anchorMax = rig.artSpace.pivot = new Vector2(.5f, .5f);
- rig.artSpace.anchoredPosition = Vector2.zero;
- rig.leftWing = rig.Layer("LeftHingedWing", sprites[1], rig.artSpace);
- rig.rightWing = rig.Layer("RightHingedWing", sprites[2], rig.artSpace);
- Hinge(rig.leftWing.rectTransform, 139, 81); Hinge(rig.rightWing.rectTransform, 340, 81);
- rig.body = UIBuilder.Rect("FixedBrandBody", rig.artSpace, 0, 0, Width, Height);
- rig.Layer("OriginalWordmark", sprites[0], rig.body);
- rig.Layer("OriginalWingMounts", sprites[4], rig.body);
- rig.halo = rig.Layer("CrystalHalo", sprites[5], rig.body);
- rig.crystal = rig.Layer("OriginalCrystal", sprites[3], rig.body);
- rig.magicA = rig.Layer("CrystalRibbonEnergyA", magic[0], rig.body);
- rig.magicB = rig.Layer("CrystalRibbonEnergyB", magic[1], rig.body);
- rig.metalA = rig.Layer("LetterSheenA", metal[0], rig.body);
- rig.metalB = rig.Layer("LetterSheenB", metal[1], rig.body);
- float[] x = { 235, 235, 363 }, y = { 39, 161, 174 };
- for (int i = 0; i < rig.stars.Length; i++) {
- var star = LobbyArtView.Picture("GemSpark" + i, rig.body, sprites[6], x[i] - 3, y[i] - 3, 6, 6);
- star.raycastTarget = false; star.material = null; rig.stars[i] = star;
- }
- rig.ready = true; rig.Fit(); rig.ApplyPose();
- // Original image is replaced only after the complete rig has its initial pose.
- original.enabled = false;
- }
- private static Sprite[] Frames(Sprite[] all, string prefix)
- {
- var result = new List<Sprite>();
- foreach (var sprite in all) if (sprite.name.StartsWith(prefix, StringComparison.Ordinal)) result.Add(sprite);
- result.Sort((a, b) => string.CompareOrdinal(a.name, b.name));
- return result.ToArray();
- }
- private Image Layer(string name, Sprite sprite, Transform parent)
- {
- var image = LobbyArtView.Picture(name, parent, sprite, 0, 0, Width, Height);
- image.preserveAspect = false; image.raycastTarget = false; image.material = null; image.color = Color.white;
- return image;
- }
- private static void Hinge(RectTransform r, float x, float y)
- {
- r.pivot = new Vector2(x / Width, 1 - y / Height);
- r.anchoredPosition = new Vector2(x, -y);
- }
- private void Fit()
- {
- if (!artSpace) return;
- var rect = ((RectTransform)transform).rect;
- // Static aspect fitting only; this scale is never an animation parameter.
- artSpace.localScale = Vector3.one * Mathf.Min(rect.width / Width, rect.height / Height);
- }
- private void OnRectTransformDimensionsChange() { if (ready) Fit(); }
- private void Update()
- {
- if (!ready || !lobby || !lobby.IsVisible) return;
- float dt = Time.unscaledDeltaTime;
- clock += dt;
- if (!introCompleted) {
- introTime = Mathf.Min(IntroSeconds, introTime + dt);
- if (introTime >= IntroSeconds) introCompleted = true;
- }
- ApplyPose();
- }
- private void ApplyPose()
- {
- if (!ready) return;
- float t = introCompleted ? 1 : Mathf.Clamp01(introTime / IntroSeconds);
- float unfold = 1 - Mathf.Pow(1 - t, 3);
- float phase = clock * Mathf.PI * 2 / LoopSeconds;
- float idleWeight = Mathf.SmoothStep(0, 1, Mathf.InverseLerp(.3f, 1, t));
- float wingAngle = 11 * (1 - unfold) + 3.2f * Mathf.Sin(phase) * idleWeight;
- leftWing.rectTransform.localRotation = Quaternion.Euler(0, 0, wingAngle);
- rightWing.rectTransform.localRotation = Quaternion.Euler(0, 0, -wingAngle);
- // Lettering only settles 2.5 source pixels (about 1.6 logical pixels), no alpha/scale reveal.
- body.anchoredPosition = new Vector2(0, -2.5f * (1 - unfold));
- float gemLevel = Mathf.Lerp(.38f, .94f + .06f * Mathf.Sin(phase), unfold);
- crystal.color = new Color(gemLevel, gemLevel, gemLevel, 1);
- halo.color = new Color(1, 1, 1, unfold * (.55f + .25f * Mathf.Sin(phase)));
- float metal = introCompleted ? Mathf.Repeat((clock - IntroSeconds) / LoopSeconds, 1) : Mathf.Clamp01((introTime - .16f) / .68f);
- Blend(metalA, metalB, metalFrames, metal);
- Blend(magicA, magicB, magicFrames, Mathf.Repeat(clock / LoopSeconds, 1));
- // Only local glints change opacity. Brand body, wings and crystal image alpha stay one.
- for (int i = 0; i < stars.Length; i++) {
- float glint = Mathf.Pow(Mathf.Max(0, Mathf.Sin(phase + i * 1.8f)), 12);
- stars[i].color = new Color(1, .85f, .62f, glint * .56f * unfold);
- }
- }
- private static void Blend(Image a, Image b, Sprite[] frames, float normalized)
- {
- float p = Mathf.Clamp01(normalized) * frames.Length;
- int frame = Mathf.Min(frames.Length - 1, Mathf.FloorToInt(p));
- float f = p - Mathf.Floor(p);
- if (normalized >= 1) f = 0;
- if (a.sprite != frames[frame]) a.sprite = frames[frame];
- if (b.sprite != frames[(frame + 1) % frames.Length]) b.sprite = frames[(frame + 1) % frames.Length];
- a.color = new Color(1, 1, 1, 1 - f); b.color = new Color(1, 1, 1, f);
- }
- private void OnEnable()
- {
- if (!ready) return;
- if (!introCompleted) { clock = 0; introTime = 0; }
- Fit(); ApplyPose();
- }
- private void OnDisable()
- {
- if (!ready) return;
- leftWing.rectTransform.localRotation = rightWing.rectTransform.localRotation = Quaternion.identity;
- body.anchoredPosition = Vector2.zero;
- // The clock is paused, not advanced off-screen. Interrupted first entrances restart cleanly.
- if (!introCompleted) { clock = 0; introTime = 0; }
- }
- }
- }
|