| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DG.Tweening; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Assertions; |
| | 9 | |
|
| | 10 | | namespace AvatarSystem |
| | 11 | | { |
| | 12 | | public class BaseAvatar : IBaseAvatar |
| | 13 | | { |
| 1 | 14 | | internal static readonly int REVEAL_POSITION_ID = Shader.PropertyToID("_RevealPosition"); |
| 1 | 15 | | internal static readonly int REVEAL_NORMAL_ID = Shader.PropertyToID("_RevealNormal"); |
| 1 | 16 | | internal static readonly int COLOR_ID = Shader.PropertyToID("_Color"); |
| | 17 | |
|
| | 18 | | internal readonly IBaseAvatarReferences baseAvatarReferences; |
| 2 | 19 | | internal readonly List<Material> cachedMaterials = new (); |
| | 20 | | internal readonly Material ghostMaterial; |
| | 21 | |
|
| 0 | 22 | | public SkinnedMeshRenderer SkinnedMeshRenderer => baseAvatarReferences.SkinnedMeshRenderer; |
| 0 | 23 | | public GameObject ArmatureContainer => baseAvatarReferences.ArmatureContainer.gameObject; |
| | 24 | |
|
| 2 | 25 | | private CancellationTokenSource revealCts = new (); |
| 2 | 26 | | private CancellationTokenSource fadeInGhostCts = new (); |
| | 27 | |
|
| 2 | 28 | | public BaseAvatar(IBaseAvatarReferences baseAvatarReferences) |
| | 29 | | { |
| 2 | 30 | | Assert.IsNotNull(baseAvatarReferences.SkinnedMeshRenderer); |
| | 31 | |
|
| 2 | 32 | | this.baseAvatarReferences = baseAvatarReferences; |
| 2 | 33 | | ghostMaterial = baseAvatarReferences.SkinnedMeshRenderer.material; |
| 2 | 34 | | ghostMaterial.SetColor(COLOR_ID, Utils.GetRandomColorInGradient(baseAvatarReferences.GhostMinColor, this.bas |
| 2 | 35 | | } |
| | 36 | |
|
| | 37 | | public async UniTask FadeGhost(CancellationToken cancellationToken = default) |
| | 38 | | { |
| 0 | 39 | | fadeInGhostCts?.Cancel(); |
| 0 | 40 | | fadeInGhostCts?.Dispose(); |
| 0 | 41 | | fadeInGhostCts = new CancellationTokenSource(); |
| 0 | 42 | | var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(fadeInGhostCts.Token, cancellationToken); |
| | 43 | |
|
| | 44 | | //Reset revealing position for ghots material |
| 0 | 45 | | ghostMaterial.SetVector(REVEAL_NORMAL_ID, Vector3.up * -1); |
| 0 | 46 | | ghostMaterial.SetVector(REVEAL_POSITION_ID, Vector3.zero); |
| | 47 | |
|
| 0 | 48 | | await ghostMaterial |
| | 49 | | .DOFade(1, COLOR_ID, baseAvatarReferences.FadeGhostSpeed) |
| | 50 | | .SetSpeedBased(true) |
| | 51 | | .ToUniTaskInstantCancelation(cancellationToken: linkedCts.Token); |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | public async UniTask Reveal(Renderer targetRenderer, float avatarHeight, float completionHeight, CancellationTok |
| | 55 | | { |
| 0 | 56 | | revealCts?.Cancel(); |
| 0 | 57 | | revealCts?.Dispose(); |
| 0 | 58 | | revealCts = new CancellationTokenSource(); |
| 0 | 59 | | var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(revealCts.Token, cancellationToken); |
| | 60 | |
|
| | 61 | | // keep a list to gather materials to avoid allocations |
| 0 | 62 | | cachedMaterials.Clear(); |
| 0 | 63 | | targetRenderer?.GetMaterials(cachedMaterials); |
| | 64 | |
|
| | 65 | | UniTask GetRevealTask(Material material, float revealPosition, float completionPosition) |
| | 66 | | { |
| 0 | 67 | | material.SetVector(REVEAL_NORMAL_ID, Vector3.up * -1); |
| 0 | 68 | | material.SetVector(REVEAL_POSITION_ID, Vector3.zero); |
| | 69 | |
|
| 0 | 70 | | return material.DOVector(Vector3.up * revealPosition, REVEAL_POSITION_ID, baseAvatarReferences.RevealSpe |
| | 71 | | .SetSpeedBased(true) |
| | 72 | | .OnComplete(() => |
| | 73 | | { |
| 0 | 74 | | baseAvatarReferences.ParticlesContainer.SetActive(false); |
| 0 | 75 | | SetRevealPosition(material, completionPosition); |
| 0 | 76 | | }) |
| | 77 | | .ToUniTaskInstantCancelation(true, cancellationToken: linkedCts.Token); |
| | 78 | | } |
| | 79 | |
|
| | 80 | | try |
| | 81 | | { |
| 0 | 82 | | baseAvatarReferences.ParticlesContainer.SetActive(true); |
| 0 | 83 | | List<UniTask> tasks = new List<UniTask>(); |
| 0 | 84 | | tasks.Add(GetRevealTask(ghostMaterial, avatarHeight, completionHeight)); |
| | 85 | |
|
| 0 | 86 | | for (var index = 0; index < cachedMaterials.Count; index++) { tasks.Add(GetRevealTask(cachedMaterials[in |
| | 87 | |
|
| 0 | 88 | | await UniTask.WhenAll(tasks); |
| 0 | 89 | | } |
| | 90 | | finally |
| | 91 | | { |
| 0 | 92 | | baseAvatarReferences.ParticlesContainer.SetActive(false); |
| 0 | 93 | | FadeOutGhostMaterial(); |
| | 94 | | } |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public void RevealInstantly(Renderer targetRenderer, float avatarHeight) |
| | 98 | | { |
| 0 | 99 | | revealCts?.Cancel(); |
| 0 | 100 | | revealCts?.Dispose(); |
| 0 | 101 | | revealCts = null; |
| | 102 | |
|
| 0 | 103 | | cachedMaterials.Clear(); |
| 0 | 104 | | if(targetRenderer != null) |
| 0 | 105 | | targetRenderer.GetMaterials(cachedMaterials); |
| | 106 | |
|
| 0 | 107 | | ghostMaterial.SetVector(REVEAL_NORMAL_ID, Vector3.up * -1); |
| 0 | 108 | | SetRevealPosition(ghostMaterial, avatarHeight); |
| | 109 | |
|
| 0 | 110 | | for (var i = 0; i < cachedMaterials.Count; i++) |
| | 111 | | { |
| 0 | 112 | | cachedMaterials[i].SetVector(REVEAL_NORMAL_ID, Vector3.up * -1); |
| 0 | 113 | | SetRevealPosition(cachedMaterials[i], -avatarHeight); |
| | 114 | | } |
| 0 | 115 | | baseAvatarReferences.ParticlesContainer.SetActive(false); |
| 0 | 116 | | FadeOutGhostMaterial(); |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | private void FadeOutGhostMaterial() |
| | 120 | | { |
| 0 | 121 | | Color color = ghostMaterial.GetColor(COLOR_ID); |
| 0 | 122 | | color.a = 0; |
| 0 | 123 | | ghostMaterial.color = color; |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | internal static void SetRevealPosition(Material material, float height) |
| | 127 | | { |
| 0 | 128 | | material.SetVector(REVEAL_POSITION_ID, Vector3.up * height); |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | public void Dispose() |
| | 132 | | { |
| 0 | 133 | | revealCts?.Cancel(); |
| 0 | 134 | | revealCts?.Dispose(); |
| 0 | 135 | | revealCts = null; |
| 0 | 136 | | fadeInGhostCts?.Cancel(); |
| 0 | 137 | | fadeInGhostCts?.Dispose(); |
| 0 | 138 | | revealCts = fadeInGhostCts; |
| 0 | 139 | | } |
| | 140 | | } |
| | 141 | | } |