| | 1 | | using AvatarSystem; |
| | 2 | | using Cysharp.Threading.Tasks; |
| | 3 | | using DCL; |
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Threading; |
| | 9 | | using UnityEngine; |
| | 10 | | using Environment = DCL.Environment; |
| | 11 | |
|
| | 12 | | namespace MainScripts.DCL.Controllers.HUD.CharacterPreview |
| | 13 | | { |
| | 14 | | public class CharacterPreviewController : MonoBehaviour, ICharacterPreviewController |
| | 15 | | { |
| | 16 | | private const int SNAPSHOT_BODY_WIDTH_RES = 256; |
| | 17 | | private const int SNAPSHOT_BODY_HEIGHT_RES = 512; |
| | 18 | | private const int SNAPSHOT_FACE_256_WIDTH_RES = 256; |
| | 19 | | private const int SNAPSHOT_FACE_256_HEIGHT_RES = 256; |
| | 20 | |
|
| | 21 | | public delegate void OnSnapshotsReady(Texture2D face256, Texture2D body); |
| 0 | 22 | | public IReadOnlyList<SkinnedMeshRenderer> originalVisibleRenderers => avatar?.originalVisibleRenderers; |
| | 23 | |
|
| | 24 | | private Dictionary<PreviewCameraFocus, Transform> cameraFocusLookUp; |
| | 25 | |
|
| | 26 | | [SerializeField] private new Camera camera; |
| | 27 | | [SerializeField] private Transform defaultEditingTemplate; |
| | 28 | | [SerializeField] private Transform faceEditingTemplate; |
| | 29 | | [SerializeField] private Transform faceSnapshotTemplate; |
| | 30 | | [SerializeField] private Transform bodySnapshotTemplate; |
| | 31 | | [SerializeField] private Transform previewTemplate; |
| | 32 | | [SerializeField] private GameObject avatarContainer; |
| | 33 | | [SerializeField] private Transform baseAvatarContainer; |
| | 34 | | [SerializeField] private BaseAvatarReferences baseAvatarReferencesPrefab; |
| | 35 | | [SerializeField] private GameObject avatarShadow; |
| | 36 | | private Service<IAvatarFactory> avatarFactory; |
| | 37 | |
|
| | 38 | | private IAvatar avatar; |
| 2 | 39 | | private readonly AvatarModel currentAvatarModel = new () { wearables = new List<string>() }; |
| 2 | 40 | | private CancellationTokenSource loadingCts = new (); |
| | 41 | | private IAnimator animator; |
| | 42 | | private Quaternion avatarContainerDefaultRotation; |
| | 43 | | private Transform cameraTransform; |
| | 44 | | private IPreviewCameraController cameraController; |
| | 45 | |
|
| | 46 | | private void Awake() |
| | 47 | | { |
| 0 | 48 | | cameraFocusLookUp = new Dictionary<PreviewCameraFocus, Transform>() |
| | 49 | | { |
| | 50 | | { PreviewCameraFocus.DefaultEditing, defaultEditingTemplate }, |
| | 51 | | { PreviewCameraFocus.FaceEditing, faceEditingTemplate }, |
| | 52 | | { PreviewCameraFocus.FaceSnapshot, faceSnapshotTemplate }, |
| | 53 | | { PreviewCameraFocus.BodySnapshot, bodySnapshotTemplate }, |
| | 54 | | { PreviewCameraFocus.Preview, previewTemplate } |
| | 55 | | }; |
| | 56 | |
|
| 0 | 57 | | this.animator = GetComponentInChildren<IAnimator>(); |
| 0 | 58 | | avatarContainerDefaultRotation = avatarContainer.transform.rotation; |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | public void SetEnabled(bool isEnabled) |
| | 62 | | { |
| 0 | 63 | | gameObject.SetActive(isEnabled); |
| 0 | 64 | | cameraController.SetCameraEnabled(isEnabled); |
| 0 | 65 | | Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto); |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public void Initialize( |
| | 69 | | CharacterPreviewMode loadingMode, |
| | 70 | | RenderTexture targetTexture, |
| | 71 | | IPreviewCameraController previewCameraController) |
| | 72 | | { |
| 0 | 73 | | avatar?.Dispose(); |
| 0 | 74 | | avatar = loadingMode switch |
| | 75 | | { |
| 0 | 76 | | CharacterPreviewMode.WithHologram => CreateAvatarWithHologram(), |
| 0 | 77 | | CharacterPreviewMode.WithoutHologram => CreateAvatar(), |
| 0 | 78 | | _ => avatar, |
| | 79 | | }; |
| | 80 | |
|
| 0 | 81 | | this.cameraController = previewCameraController; |
| 0 | 82 | | cameraController.SetCamera(camera, targetTexture); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public async UniTask TryUpdateModelAsync(AvatarModel newModel, CancellationToken cancellationToken = default) |
| | 86 | | { |
| 0 | 87 | | if (newModel.HaveSameWearablesAndColors(currentAvatarModel) && avatar.status == IAvatar.Status.Loaded) |
| 0 | 88 | | return; |
| | 89 | |
|
| 0 | 90 | | loadingCts?.Cancel(); |
| 0 | 91 | | loadingCts?.Dispose(); |
| 0 | 92 | | loadingCts = new CancellationTokenSource(); |
| | 93 | |
|
| 0 | 94 | | cancellationToken = cancellationToken == default |
| | 95 | | ? loadingCts.Token |
| | 96 | | : CancellationTokenSource.CreateLinkedTokenSource(loadingCts.Token, cancellationToken).Token; |
| | 97 | |
|
| 0 | 98 | | await UpdateModelAsync(newModel, cancellationToken); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | private void OnDestroy() |
| | 102 | | { |
| 0 | 103 | | loadingCts?.Cancel(); |
| 0 | 104 | | loadingCts?.Dispose(); |
| 0 | 105 | | loadingCts = null; |
| 0 | 106 | | avatar?.Dispose(); |
| 0 | 107 | | cameraController.Dispose(); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | private IAvatar CreateAvatar() => |
| 0 | 111 | | avatarFactory.Ref.CreateAvatar(avatarContainer, this.animator, NoLODs.i, new Visibility()); |
| | 112 | |
|
| | 113 | | private IAvatar CreateAvatarWithHologram() |
| | 114 | | { |
| 0 | 115 | | var baseAvatarReferences = baseAvatarContainer.GetComponentInChildren<IBaseAvatarReferences>() ?? Instantiat |
| 0 | 116 | | return avatarFactory.Ref.CreateAvatarWithHologram(avatarContainer, new BaseAvatar(baseAvatarReferences), thi |
| | 117 | | } |
| | 118 | |
|
| | 119 | | private async UniTask UpdateModelAsync(AvatarModel newModel, CancellationToken ct) |
| | 120 | | { |
| 0 | 121 | | currentAvatarModel.CopyFrom(newModel); |
| | 122 | |
|
| | 123 | | try |
| | 124 | | { |
| 0 | 125 | | ct.ThrowIfCancellationRequested(); |
| 0 | 126 | | List<string> wearables = new List<string>(newModel.wearables) { newModel.bodyShape }; |
| | 127 | |
|
| 0 | 128 | | await avatar.Load(wearables, newModel.emotes.Select(x => x.urn).ToList(), new AvatarSettings |
| | 129 | | { |
| | 130 | | bodyshapeId = newModel.bodyShape, |
| | 131 | | eyesColor = newModel.eyeColor, |
| | 132 | | hairColor = newModel.hairColor, |
| | 133 | | skinColor = newModel.skinColor, |
| | 134 | | forceRender = new HashSet<string>(newModel.forceRender) |
| | 135 | | }, ct); |
| 0 | 136 | | } |
| 0 | 137 | | catch (Exception e) when (e is not OperationCanceledException) { Debug.LogException(e); } |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | public async UniTask<Texture2D> TakeBodySnapshotAsync() |
| | 141 | | { |
| 0 | 142 | | Environment.i.platform.cullingController.Stop(); |
| 0 | 143 | | if (avatar.status != IAvatar.Status.Loaded) |
| 0 | 144 | | return null; |
| | 145 | |
|
| 0 | 146 | | var current = cameraController.CurrentTargetTexture; |
| 0 | 147 | | cameraController.SetTargetTexture(null); |
| 0 | 148 | | var avatarAnimator = avatarContainer.gameObject.GetComponentInChildren<AvatarAnimatorLegacy>(); |
| | 149 | |
|
| 0 | 150 | | SetFocus(PreviewCameraFocus.BodySnapshot, false); |
| 0 | 151 | | avatarAnimator.Reset(); |
| 0 | 152 | | await UniTask.Yield(); |
| | 153 | |
|
| 0 | 154 | | Texture2D body = cameraController.TakeSnapshot(SNAPSHOT_BODY_WIDTH_RES, SNAPSHOT_BODY_HEIGHT_RES); |
| | 155 | |
|
| 0 | 156 | | SetFocus(PreviewCameraFocus.DefaultEditing, false); |
| | 157 | |
|
| 0 | 158 | | cameraController.SetTargetTexture(current); |
| 0 | 159 | | Environment.i.platform.cullingController.Start(); |
| | 160 | |
|
| 0 | 161 | | return body; |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | public void TakeSnapshots(OnSnapshotsReady onSuccess, Action onFailed) |
| | 165 | | { |
| 0 | 166 | | if (avatar.status != IAvatar.Status.Loaded) |
| | 167 | | { |
| 0 | 168 | | onFailed?.Invoke(); |
| 0 | 169 | | return; |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | StartCoroutine(TakeSnapshots_Routine(onSuccess)); |
| 0 | 173 | | } |
| | 174 | |
|
| | 175 | | private IEnumerator TakeSnapshots_Routine(OnSnapshotsReady callback) |
| | 176 | | { |
| 0 | 177 | | Environment.i.platform.cullingController.Stop(); |
| | 178 | |
|
| 0 | 179 | | var current = cameraController.CurrentTargetTexture; |
| 0 | 180 | | cameraController.SetTargetTexture(null); |
| 0 | 181 | | var avatarAnimator = avatarContainer.gameObject.GetComponentInChildren<AvatarAnimatorLegacy>(); |
| | 182 | |
|
| 0 | 183 | | SetFocus(PreviewCameraFocus.FaceSnapshot, false); |
| 0 | 184 | | avatarAnimator.Reset(); |
| 0 | 185 | | yield return null; |
| 0 | 186 | | Texture2D face256 = cameraController.TakeSnapshot(SNAPSHOT_FACE_256_WIDTH_RES, SNAPSHOT_FACE_256_HEIGHT_RES) |
| | 187 | |
|
| 0 | 188 | | SetFocus(PreviewCameraFocus.BodySnapshot, false); |
| 0 | 189 | | avatarAnimator.Reset(); |
| 0 | 190 | | yield return null; |
| 0 | 191 | | Texture2D body = cameraController.TakeSnapshot(SNAPSHOT_BODY_WIDTH_RES, SNAPSHOT_BODY_HEIGHT_RES); |
| | 192 | |
|
| 0 | 193 | | SetFocus(PreviewCameraFocus.DefaultEditing, false); |
| | 194 | |
|
| 0 | 195 | | cameraController.SetTargetTexture(current); |
| | 196 | |
|
| 0 | 197 | | Environment.i.platform.cullingController.Start(); |
| 0 | 198 | | callback?.Invoke(face256, body); |
| 0 | 199 | | } |
| | 200 | |
|
| | 201 | | public void SetFocus(PreviewCameraFocus focus, bool useTransition = true) => |
| 0 | 202 | | cameraController.SetFocus(cameraFocusLookUp[focus], useTransition); |
| | 203 | |
|
| | 204 | | public void Rotate(float rotationVelocity) => |
| 0 | 205 | | avatarContainer.transform.Rotate(Time.deltaTime * rotationVelocity * Vector3.up); |
| | 206 | |
|
| | 207 | | public void ResetRotation() => |
| 0 | 208 | | avatarContainer.transform.rotation = avatarContainerDefaultRotation; |
| | 209 | |
|
| | 210 | | public void MoveCamera(Vector3 positionDelta, bool changeYLimitsDependingOnZPosition) => |
| 0 | 211 | | cameraController.MoveCamera(positionDelta, changeYLimitsDependingOnZPosition); |
| | 212 | |
|
| | 213 | | public void SetCameraLimits(Bounds limits) => |
| 0 | 214 | | cameraController.SetCameraLimits(limits); |
| | 215 | |
|
| | 216 | | public void ConfigureZoom(float verticalCenterRef, float bottomMaxOffset, float topMaxOffset) => |
| 0 | 217 | | cameraController.ConfigureZoom(verticalCenterRef, bottomMaxOffset, topMaxOffset); |
| | 218 | |
|
| | 219 | | public void SetCharacterShadowActive(bool isActive) => |
| 0 | 220 | | avatarShadow.SetActive(isActive); |
| | 221 | |
|
| | 222 | | public IAvatarEmotesController GetEmotesController() => |
| 0 | 223 | | avatar.GetEmotesController(); |
| | 224 | |
|
| | 225 | | public void PlayEmote(string emoteId, long timestamp) |
| | 226 | | { |
| 0 | 227 | | avatar.GetEmotesController().PlayEmote(emoteId, timestamp, false, false, true); |
| 0 | 228 | | } |
| | 229 | |
|
| | 230 | | public void StopEmote() |
| | 231 | | { |
| 0 | 232 | | avatar.GetEmotesController().StopEmote(true); |
| 0 | 233 | | } |
| | 234 | |
|
| | 235 | | public void Dispose() => |
| 0 | 236 | | Destroy(gameObject); |
| | 237 | | } |
| | 238 | | } |