| | 1 | | using System; |
| | 2 | | using System.Threading; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using UnityEngine; |
| | 7 | | using Object = UnityEngine.Object; |
| | 8 | |
|
| | 9 | | namespace AvatarSystem |
| | 10 | | { |
| | 11 | | public class LOD : ILOD |
| | 12 | | { |
| | 13 | | public const float IMPOSTOR_MOVEMENT_INTERPOLATION = 1.79f; |
| | 14 | | private const float TRANSITION_DURATION = 0.75f; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// 0 = 3D Avatar, SSAO, Facial Features |
| | 18 | | /// 1 = 3D Avatar, NO SSAO, NO Facial Features |
| | 19 | | /// 2 = Impostor, NO 3D Avatar |
| | 20 | | /// </summary> |
| 699 | 21 | | public int lodIndex { get; private set; } = -1; |
| | 22 | |
|
| | 23 | | private readonly GameObject impostorContainer; |
| | 24 | | private readonly IVisibility visibility; |
| | 25 | | private readonly IAvatarMovementController avatarMovementController; |
| | 26 | |
|
| | 27 | | internal Renderer combinedAvatar; |
| | 28 | | internal Renderer impostorRenderer; |
| | 29 | | internal MeshFilter impostorMeshFilter; |
| | 30 | | private float avatarAlpha; |
| | 31 | |
|
| | 32 | | private CancellationTokenSource transitionCTS; |
| | 33 | | private CancellationTokenSource billboardLookAtCameraCTS; |
| 699 | 34 | | private string VISIBILITY_CONSTRAIN_IN_IMPOSTOR = "in_impostor"; |
| 699 | 35 | | private string VISIBILITY_CONSTRAIN_IN_LOD1 = "in_LOD1"; |
| | 36 | |
|
| 699 | 37 | | public LOD(GameObject impostorContainer, IVisibility visibility, IAvatarMovementController avatarMovementControl |
| | 38 | | { |
| 699 | 39 | | this.impostorContainer = impostorContainer; |
| 699 | 40 | | this.visibility = visibility; |
| | 41 | | // TODO Once the AvatarMovementController is completly ported into the AvatarSystem we can decouple it from |
| 699 | 42 | | this.avatarMovementController = avatarMovementController; |
| 699 | 43 | | } |
| | 44 | |
|
| | 45 | | internal void EnsureImpostor() |
| | 46 | | { |
| 26 | 47 | | if (impostorRenderer != null && impostorMeshFilter != null) |
| 16 | 48 | | return; |
| 10 | 49 | | impostorRenderer = CreateImpostor(); |
| 10 | 50 | | impostorMeshFilter = impostorRenderer.GetComponent<MeshFilter>(); |
| 10 | 51 | | SetImpostorTexture(null); |
| 10 | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Set the impostor texture (null will take a randomized one) |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="texture"></param> |
| | 58 | | public void SetImpostorTexture(Texture2D texture) |
| | 59 | | { |
| 10 | 60 | | EnsureImpostor(); |
| | 61 | |
|
| 10 | 62 | | if (texture == null) |
| 10 | 63 | | AvatarRendererHelpers.RandomizeAndApplyGenericImpostor(impostorMeshFilter.mesh, impostorRenderer.materia |
| | 64 | | else |
| 0 | 65 | | AvatarRendererHelpers.SetImpostorTexture(texture, impostorMeshFilter.mesh, impostorRenderer.material); |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public void SetImpostorTint(Color color) |
| | 69 | | { |
| 2 | 70 | | EnsureImpostor(); |
| | 71 | |
|
| 2 | 72 | | AvatarRendererHelpers.SetImpostorTintColor(impostorRenderer.material, color); |
| 2 | 73 | | } |
| | 74 | |
|
| | 75 | | public void Bind(Renderer combinedAvatar) |
| | 76 | | { |
| 0 | 77 | | this.combinedAvatar = combinedAvatar; |
| 0 | 78 | | SetLodIndex(lodIndex, true); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public void SetLodIndex(int lodIndex, bool inmediate = false) |
| | 82 | | { |
| 0 | 83 | | if (inmediate) |
| | 84 | | { |
| 0 | 85 | | avatarAlpha = lodIndex <= 1 ? 1f : 0f; |
| 0 | 86 | | UpdateSSAO(combinedAvatar, lodIndex); |
| 0 | 87 | | UpdateAlpha(avatarAlpha); |
| 0 | 88 | | UpdateMovementLerping(lodIndex); |
| | 89 | |
|
| 0 | 90 | | if (avatarAlpha > 0) |
| 0 | 91 | | visibility.RemoveCombinedRendererConstrain(VISIBILITY_CONSTRAIN_IN_IMPOSTOR); |
| | 92 | | else |
| 0 | 93 | | visibility.AddCombinedRendererConstrain(VISIBILITY_CONSTRAIN_IN_IMPOSTOR); |
| | 94 | |
|
| 0 | 95 | | if (lodIndex == 0) |
| 0 | 96 | | visibility.RemoveFacialFeaturesConstrain(VISIBILITY_CONSTRAIN_IN_LOD1); |
| | 97 | | else |
| 0 | 98 | | visibility.AddFacialFeaturesConstrain(VISIBILITY_CONSTRAIN_IN_LOD1); |
| | 99 | |
|
| 0 | 100 | | SetImpostorEnabled(avatarAlpha == 0); |
| 0 | 101 | | return; |
| | 102 | | } |
| | 103 | |
|
| 0 | 104 | | if (lodIndex == this.lodIndex) |
| 0 | 105 | | return; |
| | 106 | |
|
| 0 | 107 | | this.lodIndex = lodIndex; |
| | 108 | |
|
| 0 | 109 | | transitionCTS?.Cancel(); |
| 0 | 110 | | transitionCTS?.Dispose(); |
| 0 | 111 | | transitionCTS = new CancellationTokenSource(); |
| 0 | 112 | | Transition(transitionCTS.Token); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | private Renderer CreateImpostor() |
| | 116 | | { |
| 10 | 117 | | GameObject quadImpostorContainer = Object.Instantiate(Resources.Load<GameObject>("QuadImpostor"), impostorCo |
| 10 | 118 | | return quadImpostorContainer.GetComponent<Renderer>(); |
| | 119 | | } |
| | 120 | |
|
| | 121 | | private async UniTaskVoid Transition(CancellationToken ct) |
| | 122 | | { |
| 0 | 123 | | ct.ThrowIfCancellationRequested(); |
| 0 | 124 | | EnsureImpostor(); |
| | 125 | |
|
| | 126 | | try |
| | 127 | | { |
| 0 | 128 | | visibility.RemoveCombinedRendererConstrain(VISIBILITY_CONSTRAIN_IN_IMPOSTOR); |
| 0 | 129 | | impostorRenderer.enabled = true; |
| 0 | 130 | | float targetAvatarAlpha = lodIndex <= 1 ? 1f : 0f; |
| 0 | 131 | | while (!Mathf.Approximately(targetAvatarAlpha, avatarAlpha)) |
| | 132 | | { |
| 0 | 133 | | avatarAlpha = Mathf.MoveTowards(avatarAlpha, targetAvatarAlpha, (1f / TRANSITION_DURATION) * Time.de |
| 0 | 134 | | UpdateAlpha(avatarAlpha); |
| 0 | 135 | | await UniTask.NextFrame(ct); |
| | 136 | | } |
| | 137 | |
|
| 0 | 138 | | UpdateSSAO(combinedAvatar, lodIndex); |
| 0 | 139 | | UpdateMovementLerping(lodIndex); |
| | 140 | |
|
| 0 | 141 | | if (avatarAlpha > 0) |
| 0 | 142 | | visibility.RemoveCombinedRendererConstrain(VISIBILITY_CONSTRAIN_IN_IMPOSTOR); |
| | 143 | | else |
| 0 | 144 | | visibility.AddCombinedRendererConstrain(VISIBILITY_CONSTRAIN_IN_IMPOSTOR); |
| | 145 | |
|
| 0 | 146 | | if (lodIndex == 0) |
| 0 | 147 | | visibility.RemoveFacialFeaturesConstrain(VISIBILITY_CONSTRAIN_IN_LOD1); |
| | 148 | | else |
| 0 | 149 | | visibility.AddFacialFeaturesConstrain(VISIBILITY_CONSTRAIN_IN_LOD1); |
| | 150 | |
|
| 0 | 151 | | SetImpostorEnabled(avatarAlpha == 0); |
| 0 | 152 | | } |
| 0 | 153 | | catch (OperationCanceledException) |
| | 154 | | { |
| | 155 | | //No disposing required |
| 0 | 156 | | throw; |
| | 157 | | } |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | private void SetImpostorEnabled(bool enabled) |
| | 161 | | { |
| 0 | 162 | | EnsureImpostor(); |
| | 163 | |
|
| 0 | 164 | | impostorRenderer.enabled = enabled; |
| 0 | 165 | | billboardLookAtCameraCTS?.Cancel(); |
| 0 | 166 | | billboardLookAtCameraCTS?.Dispose(); |
| 0 | 167 | | if (enabled) |
| | 168 | | { |
| 0 | 169 | | billboardLookAtCameraCTS = new CancellationTokenSource(); |
| 0 | 170 | | BillboardLookAtCamera(billboardLookAtCameraCTS.Token); |
| 0 | 171 | | } |
| | 172 | | else |
| | 173 | | { |
| 0 | 174 | | billboardLookAtCameraCTS = null; |
| | 175 | | } |
| 0 | 176 | | } |
| | 177 | |
|
| | 178 | | internal void UpdateAlpha(float avatarAlpha) |
| | 179 | | { |
| 3 | 180 | | if (combinedAvatar == null) |
| 0 | 181 | | return; |
| | 182 | |
|
| 3 | 183 | | EnsureImpostor(); |
| | 184 | |
|
| 3 | 185 | | Material[] mats = combinedAvatar.sharedMaterials; |
| 12 | 186 | | for (int j = 0; j < mats.Length; j++) |
| | 187 | | { |
| 3 | 188 | | mats[j].SetFloat(ShaderUtils.DitherFade, avatarAlpha); |
| | 189 | | } |
| | 190 | |
|
| 3 | 191 | | Material impostorMaterial = impostorRenderer.material; |
| | 192 | | //TODO implement dither in Unlit shader |
| 3 | 193 | | Color current = impostorMaterial.GetColor(ShaderUtils.BaseColor); |
| 3 | 194 | | current.a = 1f - avatarAlpha; |
| 3 | 195 | | impostorMaterial.SetColor(ShaderUtils.BaseColor, current); |
| 3 | 196 | | } |
| | 197 | |
|
| | 198 | | internal static void UpdateSSAO(Renderer renderer, int lodIndex) |
| | 199 | | { |
| 3 | 200 | | if (renderer == null) |
| 0 | 201 | | return; |
| | 202 | |
|
| 3 | 203 | | Material[] mats = renderer.sharedMaterials; |
| 12 | 204 | | for (int j = 0; j < mats.Length; j++) |
| | 205 | | { |
| 3 | 206 | | if (lodIndex == 0) |
| 1 | 207 | | mats[j].DisableKeyword(ShaderUtils.SSAO_OFF_KEYWORD); |
| | 208 | | else |
| 2 | 209 | | mats[j].EnableKeyword(ShaderUtils.SSAO_OFF_KEYWORD); |
| | 210 | | } |
| 3 | 211 | | } |
| | 212 | |
|
| 6 | 213 | | internal void UpdateMovementLerping(int lodIndex) { avatarMovementController.SetMovementLerpWait(lodIndex >= 2 ? |
| | 214 | |
|
| | 215 | | private async UniTaskVoid BillboardLookAtCamera(CancellationToken ct) |
| | 216 | | { |
| 0 | 217 | | Camera camera = Camera.main; |
| 0 | 218 | | while (camera == null) |
| | 219 | | { |
| 0 | 220 | | await UniTask.WaitForEndOfFrame(ct).AttachExternalCancellation(ct); |
| 0 | 221 | | camera = Camera.main; |
| | 222 | | } |
| | 223 | |
|
| 0 | 224 | | while (true) |
| | 225 | | { |
| 0 | 226 | | SetBillboardRotation(camera.transform); |
| 0 | 227 | | await UniTask.WaitForEndOfFrame(ct).AttachExternalCancellation(ct); |
| | 228 | | } |
| | 229 | | } |
| | 230 | |
|
| | 231 | | internal void SetBillboardRotation(Transform lookAt) |
| | 232 | | { |
| 1 | 233 | | EnsureImpostor(); |
| | 234 | |
|
| 1 | 235 | | impostorRenderer.transform.LookAt(lookAt); |
| 1 | 236 | | impostorRenderer.transform.eulerAngles = Vector3.Scale(impostorContainer.transform.eulerAngles, Vector3.up); |
| 1 | 237 | | } |
| | 238 | |
|
| | 239 | | public void Dispose() |
| | 240 | | { |
| 690 | 241 | | transitionCTS?.Cancel(); |
| 690 | 242 | | transitionCTS?.Dispose(); |
| 690 | 243 | | transitionCTS = null; |
| | 244 | |
|
| 690 | 245 | | billboardLookAtCameraCTS?.Cancel(); |
| 690 | 246 | | billboardLookAtCameraCTS?.Dispose(); |
| 690 | 247 | | billboardLookAtCameraCTS = null; |
| | 248 | |
|
| 690 | 249 | | if (impostorRenderer != null) |
| 0 | 250 | | Object.Destroy(impostorRenderer.gameObject); |
| 690 | 251 | | } |
| | 252 | | } |
| | 253 | |
|
| | 254 | | public class NoLODs : ILOD |
| | 255 | | { |
| | 256 | | public int lodIndex { get; } |
| | 257 | |
|
| | 258 | | public void Bind(Renderer combinedAvatar) { } |
| | 259 | | public void SetLodIndex(int lodIndex, bool inmediate = false) { } |
| | 260 | | public void SetImpostorTexture(Texture2D texture) { } |
| | 261 | | public void SetImpostorTint(Color color) { } |
| | 262 | |
|
| | 263 | | public void Dispose() { } |
| | 264 | | } |
| | 265 | | } |