| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using DCL; |
| | 8 | | using AvatarSystem; |
| | 9 | | using Cysharp.Threading.Tasks; |
| | 10 | | using Random = UnityEngine.Random; |
| | 11 | |
|
| | 12 | | public class BaseAvatarReveal : MonoBehaviour, IBaseAvatarRevealer |
| | 13 | | { |
| | 14 | | [SerializeField] private Animation animation; |
| | 15 | | [SerializeField] private List<GameObject> particleEffects; |
| | 16 | |
|
| | 17 | | [ColorUsage(true, true, 0f, 8f, 0.125f, 3f)] |
| | 18 | | public Color baseColor; |
| | 19 | | [ColorUsage(true, true, 0f, 8f, 0.125f, 3f)] |
| | 20 | | public Color maxColor; |
| | 21 | |
|
| | 22 | | public SkinnedMeshRenderer meshRenderer; |
| | 23 | | public bool avatarLoaded; |
| | 24 | | public GameObject revealer; |
| | 25 | |
|
| | 26 | | private ILOD lod; |
| | 27 | |
|
| | 28 | | public float fadeInSpeed; |
| | 29 | | Material _ghostMaterial; |
| | 30 | |
|
| | 31 | | private float startH; |
| | 32 | | private float startS; |
| | 33 | | private float startV; |
| | 34 | | private float endH; |
| | 35 | | private float endS; |
| | 36 | | private float endV; |
| | 37 | | private bool isRevealing; |
| | 38 | |
|
| 1 | 39 | | public List<Renderer> targets = new List<Renderer>(); |
| 1 | 40 | | List<Material> _materials = new List<Material>(); |
| | 41 | | private Coroutine updateAvatarRevealerRoutine = null; |
| | 42 | |
|
| | 43 | | private void Start() |
| | 44 | | { |
| 0 | 45 | | _ghostMaterial = meshRenderer.material; |
| 0 | 46 | | InitializeColorGradient(); |
| | 47 | |
|
| 0 | 48 | | foreach (Renderer r in targets) { _materials.Add(r.material); } |
| 0 | 49 | | FadeInGhostMaterial().Forget(); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | public SkinnedMeshRenderer GetMainRenderer() |
| | 53 | | { |
| 0 | 54 | | return meshRenderer; |
| | 55 | | } |
| | 56 | |
|
| | 57 | | private void InitializeColorGradient() |
| | 58 | | { |
| 0 | 59 | | Color.RGBToHSV(baseColor, out startH, out startS, out startV); |
| 0 | 60 | | Color.RGBToHSV(maxColor, out endH, out endS, out endV); |
| 0 | 61 | | Color newColor = Color.HSVToRGB(Random.Range(startH, endH), startS, startV); |
| 0 | 62 | | _ghostMaterial.SetColor("_Color", new Color(newColor.r, newColor.g, newColor.b, 0)); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public void InjectLodSystem(ILOD lod) |
| | 66 | | { |
| 0 | 67 | | this.lod = lod; |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | public void AddTarget(MeshRenderer newTarget) |
| | 71 | | { |
| 0 | 72 | | if (newTarget == null) |
| 0 | 73 | | return; |
| | 74 | |
|
| 0 | 75 | | targets.Add(newTarget); |
| 0 | 76 | | newTarget?.material.SetVector("_RevealPosition", Vector3.zero); |
| 0 | 77 | | newTarget?.material.SetVector("_RevealNormal", new Vector3(0, -1, 0)); |
| 0 | 78 | | _materials.Add(newTarget.material); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public async UniTask StartAvatarRevealAnimation(bool withTransition, CancellationToken cancellationToken) |
| | 82 | | { |
| 0 | 83 | | updateAvatarRevealerRoutine = StartCoroutine(UpdateRevealerPosition()); |
| | 84 | |
|
| | 85 | | try |
| | 86 | | { |
| 0 | 87 | | if (!withTransition || lod.lodIndex >= 2) |
| | 88 | | { |
| 0 | 89 | | SetFullRendered(); |
| 0 | 90 | | return; |
| | 91 | | } |
| | 92 | |
|
| 0 | 93 | | isRevealing = true; |
| 0 | 94 | | animation.Play(); |
| 0 | 95 | | await UniTask.WaitUntil(() => !isRevealing, cancellationToken: cancellationToken).AttachExternalCancellation |
| 0 | 96 | | } |
| 0 | 97 | | catch (OperationCanceledException) { SetFullRendered(); } |
| | 98 | | finally |
| | 99 | | { |
| 0 | 100 | | StopCoroutine(updateAvatarRevealerRoutine); |
| 0 | 101 | | SetFullRendered(); |
| | 102 | | } |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | private IEnumerator UpdateRevealerPosition() |
| | 106 | | { |
| 0 | 107 | | _ghostMaterial.SetVector("_RevealPosition", revealer.transform.localPosition); |
| | 108 | |
|
| 0 | 109 | | while (true) |
| | 110 | | { |
| 0 | 111 | | foreach (Material m in _materials) { m.SetVector("_RevealPosition", -revealer.transform.localPosition); } |
| | 112 | |
|
| 0 | 113 | | yield return new WaitForSeconds(0.01f); |
| | 114 | | } |
| | 115 | | } |
| | 116 | |
|
| | 117 | | private async UniTask FadeInGhostMaterial() |
| | 118 | | { |
| | 119 | | Color gColor; |
| | 120 | | Color tempColor; |
| | 121 | |
|
| 0 | 122 | | while (_ghostMaterial.GetColor("_Color").a < 0.9f) |
| | 123 | | { |
| 0 | 124 | | gColor = _ghostMaterial.GetColor("_Color"); |
| 0 | 125 | | tempColor = new Color(gColor.r, gColor.g, gColor.b, gColor.a + Time.deltaTime * fadeInSpeed); |
| 0 | 126 | | _ghostMaterial.SetColor("_Color", tempColor); |
| | 127 | |
|
| 0 | 128 | | await UniTask.Delay(50); |
| | 129 | | } |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | public void OnRevealAnimationEnd() |
| | 133 | | { |
| 0 | 134 | | isRevealing = false; |
| 0 | 135 | | meshRenderer.enabled = false; |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | private void SetFullRendered() |
| | 139 | | { |
| 0 | 140 | | meshRenderer.enabled = false; |
| 0 | 141 | | animation.Stop(); |
| | 142 | | const float REVEALED_POSITION = -10; |
| 0 | 143 | | revealer.transform.position = new Vector3(0, REVEALED_POSITION, 0); |
| | 144 | |
|
| 0 | 145 | | foreach (Material m in _materials) { m.SetVector("_RevealPosition", new Vector3(0, REVEALED_POSITION, 0)); } |
| | 146 | |
|
| 0 | 147 | | _ghostMaterial?.SetVector("_RevealPosition", new Vector3(0, 2.5f, 0)); |
| 0 | 148 | | DisableParticleEffects(); |
| 0 | 149 | | avatarLoaded = true; |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | public void Reset() |
| | 153 | | { |
| 0 | 154 | | Color gColor = _ghostMaterial.GetColor("_Color"); |
| 0 | 155 | | Color tempColor = new Color(gColor.r, gColor.g, gColor.b, 0); |
| 0 | 156 | | _ghostMaterial.SetColor("_Color", tempColor); |
| 0 | 157 | | avatarLoaded = false; |
| 0 | 158 | | meshRenderer.enabled = true; |
| 0 | 159 | | targets = new List<Renderer>(); |
| 0 | 160 | | _materials = new List<Material>(); |
| 0 | 161 | | revealer.transform.position = Vector3.zero; |
| 0 | 162 | | FadeInGhostMaterial().Forget(); |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | private void DisableParticleEffects() |
| | 166 | | { |
| 0 | 167 | | foreach (GameObject p in particleEffects) { p.SetActive(false); } |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | public void OnDisable() |
| | 171 | | { |
| 0 | 172 | | SetFullRendered(); |
| 0 | 173 | | } |
| | 174 | | } |