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