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