| | 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 | |
|
| | 11 | | namespace MainScripts.DCL.Controllers.HUD.CharacterPreview |
| | 12 | | { |
| | 13 | | public class CharacterPreviewController : MonoBehaviour, ICharacterPreviewController |
| | 14 | | { |
| | 15 | | private const int SNAPSHOT_BODY_WIDTH_RES = 256; |
| | 16 | | private const int SNAPSHOT_BODY_HEIGHT_RES = 512; |
| | 17 | |
|
| | 18 | | private const int SNAPSHOT_FACE_256_WIDTH_RES = 256; |
| | 19 | | private const int SNAPSHOT_FACE_256_HEIGHT_RES = 256; |
| | 20 | |
|
| | 21 | | private const int SUPERSAMPLING = 1; |
| | 22 | | private const float CAMERA_TRANSITION_TIME = 0.5f; |
| | 23 | |
|
| | 24 | | public delegate void OnSnapshotsReady(Texture2D face256, Texture2D body); |
| | 25 | |
|
| | 26 | | public enum CameraFocus |
| | 27 | | { |
| | 28 | | DefaultEditing, |
| | 29 | | FaceEditing, |
| | 30 | | FaceSnapshot, |
| | 31 | | BodySnapshot, |
| | 32 | | Preview |
| | 33 | | } |
| | 34 | |
|
| | 35 | | private Dictionary<CameraFocus, Transform> cameraFocusLookUp; |
| | 36 | |
|
| | 37 | | [SerializeField] private new Camera camera; |
| | 38 | | [SerializeField] private Transform defaultEditingTemplate; |
| | 39 | | [SerializeField] private Transform faceEditingTemplate; |
| | 40 | |
|
| | 41 | | [SerializeField] private Transform faceSnapshotTemplate; |
| | 42 | | [SerializeField] private Transform bodySnapshotTemplate; |
| | 43 | | [SerializeField] private Transform previewTemplate; |
| | 44 | |
|
| | 45 | | [SerializeField] private GameObject avatarContainer; |
| | 46 | | [SerializeField] private Transform avatarRevealContainer; |
| | 47 | |
|
| | 48 | | private Service<IAvatarFactory> avatarFactory; |
| | 49 | |
|
| | 50 | | private IAvatar avatar; |
| 49 | 51 | | private readonly AvatarModel currentAvatarModel = new () { wearables = new List<string>() }; |
| 49 | 52 | | private CancellationTokenSource loadingCts = new (); |
| | 53 | |
|
| | 54 | | private IAnimator animator; |
| | 55 | | private Quaternion avatarContainerDefaultRotation; |
| | 56 | |
|
| | 57 | | private void Awake() |
| | 58 | | { |
| 48 | 59 | | cameraFocusLookUp = new Dictionary<CameraFocus, Transform>() |
| | 60 | | { |
| | 61 | | { CameraFocus.DefaultEditing, defaultEditingTemplate }, |
| | 62 | | { CameraFocus.FaceEditing, faceEditingTemplate }, |
| | 63 | | { CameraFocus.FaceSnapshot, faceSnapshotTemplate }, |
| | 64 | | { CameraFocus.BodySnapshot, bodySnapshotTemplate }, |
| | 65 | | { CameraFocus.Preview, previewTemplate } |
| | 66 | | }; |
| | 67 | |
|
| 48 | 68 | | this.animator = GetComponentInChildren<IAnimator>(); |
| 48 | 69 | | avatarContainerDefaultRotation = avatarContainer.transform.rotation; |
| 48 | 70 | | } |
| | 71 | |
|
| | 72 | | public void SetEnabled(bool isEnabled) |
| | 73 | | { |
| 140 | 74 | | gameObject.SetActive(isEnabled); |
| 140 | 75 | | camera.enabled = isEnabled; |
| 140 | 76 | | } |
| | 77 | |
|
| | 78 | | public void Initialize(CharacterPreviewMode loadingMode, RenderTexture targetTexture) |
| | 79 | | { |
| 48 | 80 | | avatar?.Dispose(); |
| 48 | 81 | | avatar = loadingMode switch |
| | 82 | | { |
| 0 | 83 | | CharacterPreviewMode.WithHologram => CreateAvatarWithHologram(), |
| 48 | 84 | | CharacterPreviewMode.WithoutHologram => CreateAvatar(), |
| 0 | 85 | | _ => avatar, |
| | 86 | | }; |
| | 87 | |
|
| 48 | 88 | | camera.targetTexture = targetTexture; |
| 48 | 89 | | } |
| | 90 | |
|
| | 91 | | public async UniTask TryUpdateModelAsync(AvatarModel newModel, CancellationToken cancellationToken = default) |
| | 92 | | { |
| 0 | 93 | | if (newModel.HaveSameWearablesAndColors(currentAvatarModel) && avatar.status == IAvatar.Status.Loaded) |
| 0 | 94 | | return; |
| | 95 | |
|
| 0 | 96 | | loadingCts?.Cancel(); |
| 0 | 97 | | loadingCts?.Dispose(); |
| 0 | 98 | | loadingCts = new CancellationTokenSource(); |
| | 99 | |
|
| 0 | 100 | | cancellationToken = cancellationToken == default |
| | 101 | | ? loadingCts.Token |
| | 102 | | : CancellationTokenSource.CreateLinkedTokenSource(loadingCts.Token, cancellationToken).Token; |
| | 103 | |
|
| | 104 | |
|
| 0 | 105 | | await UpdateModelAsync(newModel, cancellationToken); |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | private void OnDestroy() |
| | 109 | | { |
| 48 | 110 | | loadingCts?.Cancel(); |
| 48 | 111 | | loadingCts?.Dispose(); |
| 48 | 112 | | loadingCts = null; |
| 48 | 113 | | avatar?.Dispose(); |
| 48 | 114 | | } |
| | 115 | |
|
| | 116 | | private IAvatar CreateAvatar() => |
| 48 | 117 | | avatarFactory.Ref.CreateAvatar(avatarContainer, this.animator, NoLODs.i, new Visibility()); |
| | 118 | |
|
| | 119 | | private IAvatar CreateAvatarWithHologram() => |
| 0 | 120 | | avatarFactory.Ref.CreateAvatarWithHologram(avatarContainer, avatarRevealContainer, avatarContainer, this.ani |
| | 121 | |
|
| | 122 | | private async UniTask UpdateModelAsync(AvatarModel newModel, CancellationToken ct) |
| | 123 | | { |
| 0 | 124 | | currentAvatarModel.CopyFrom(newModel); |
| | 125 | |
|
| | 126 | | try |
| | 127 | | { |
| 0 | 128 | | ct.ThrowIfCancellationRequested(); |
| 0 | 129 | | List<string> wearables = new List<string>(newModel.wearables); |
| 0 | 130 | | wearables.Add(newModel.bodyShape); |
| | 131 | |
|
| 0 | 132 | | await avatar.Load(wearables, newModel.emotes.Select(x => x.urn).ToList(), new AvatarSettings |
| | 133 | | { |
| | 134 | | bodyshapeId = newModel.bodyShape, |
| | 135 | | eyesColor = newModel.eyeColor, |
| | 136 | | hairColor = newModel.hairColor, |
| | 137 | | skinColor = newModel.skinColor |
| | 138 | | }, ct); |
| 0 | 139 | | } |
| 0 | 140 | | catch (Exception e) when (e is not OperationCanceledException) { Debug.LogException(e); } |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | public void TakeSnapshots(OnSnapshotsReady onSuccess, Action onFailed) |
| | 144 | | { |
| 0 | 145 | | if (avatar.status != IAvatar.Status.Loaded) |
| | 146 | | { |
| 0 | 147 | | onFailed?.Invoke(); |
| 0 | 148 | | return; |
| | 149 | | } |
| | 150 | |
|
| 0 | 151 | | StartCoroutine(TakeSnapshots_Routine(onSuccess)); |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | private IEnumerator TakeSnapshots_Routine(OnSnapshotsReady callback) |
| | 155 | | { |
| 0 | 156 | | global::DCL.Environment.i.platform.cullingController.Stop(); |
| | 157 | |
|
| 0 | 158 | | var current = camera.targetTexture; |
| 0 | 159 | | camera.targetTexture = null; |
| 0 | 160 | | var avatarAnimator = avatarContainer.gameObject.GetComponentInChildren<AvatarAnimatorLegacy>(); |
| | 161 | |
|
| 0 | 162 | | SetFocus(CameraFocus.FaceSnapshot, false); |
| 0 | 163 | | avatarAnimator.Reset(); |
| 0 | 164 | | yield return null; |
| 0 | 165 | | Texture2D face256 = Snapshot(SNAPSHOT_FACE_256_WIDTH_RES, SNAPSHOT_FACE_256_HEIGHT_RES); |
| | 166 | |
|
| 0 | 167 | | SetFocus(CameraFocus.BodySnapshot, false); |
| 0 | 168 | | avatarAnimator.Reset(); |
| 0 | 169 | | yield return null; |
| 0 | 170 | | Texture2D body = Snapshot(SNAPSHOT_BODY_WIDTH_RES, SNAPSHOT_BODY_HEIGHT_RES); |
| | 171 | |
|
| 0 | 172 | | SetFocus(CameraFocus.DefaultEditing, false); |
| | 173 | |
|
| 0 | 174 | | camera.targetTexture = current; |
| | 175 | |
|
| 0 | 176 | | global::DCL.Environment.i.platform.cullingController.Start(); |
| 0 | 177 | | callback?.Invoke(face256, body); |
| 0 | 178 | | } |
| | 179 | |
|
| | 180 | | private Texture2D Snapshot(int width, int height) |
| | 181 | | { |
| 0 | 182 | | RenderTexture rt = new RenderTexture(width * SUPERSAMPLING, height * SUPERSAMPLING, 32); |
| 0 | 183 | | camera.targetTexture = rt; |
| 0 | 184 | | Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false); |
| 0 | 185 | | camera.Render(); |
| 0 | 186 | | RenderTexture.active = rt; |
| 0 | 187 | | screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); |
| 0 | 188 | | screenShot.Apply(); |
| | 189 | |
|
| 0 | 190 | | return screenShot; |
| | 191 | | } |
| | 192 | |
|
| | 193 | | private Coroutine cameraTransitionCoroutine; |
| | 194 | |
|
| | 195 | | public void SetFocus(CameraFocus focus, bool useTransition = true) |
| | 196 | | { |
| 95 | 197 | | SetFocus(cameraFocusLookUp[focus], useTransition); |
| 95 | 198 | | } |
| | 199 | |
|
| | 200 | | private void SetFocus(Transform transform, bool useTransition = true) |
| | 201 | | { |
| 95 | 202 | | if (cameraTransitionCoroutine != null) { StopCoroutine(cameraTransitionCoroutine); } |
| | 203 | |
|
| 95 | 204 | | if (useTransition && gameObject.activeInHierarchy) |
| | 205 | | { |
| 1 | 206 | | cameraTransitionCoroutine = StartCoroutine(CameraTransition(camera.transform.position, transform.positio |
| | 207 | | } |
| | 208 | | else |
| | 209 | | { |
| 94 | 210 | | var cameraTransform = camera.transform; |
| 94 | 211 | | cameraTransform.position = transform.position; |
| 94 | 212 | | cameraTransform.rotation = transform.rotation; |
| | 213 | | } |
| 94 | 214 | | } |
| | 215 | |
|
| | 216 | | private IEnumerator CameraTransition(Vector3 initPos, Vector3 endPos, Quaternion initRotation, Quaternion endRot |
| | 217 | | { |
| 1 | 218 | | var cameraTransform = camera.transform; |
| 1 | 219 | | float currentTime = 0; |
| | 220 | |
|
| 1 | 221 | | float inverseTime = 1 / time; |
| | 222 | |
|
| 1 | 223 | | while (currentTime < time) |
| | 224 | | { |
| 1 | 225 | | currentTime = Mathf.Clamp(currentTime + Time.deltaTime, 0, time); |
| 1 | 226 | | cameraTransform.position = Vector3.Lerp(initPos, endPos, currentTime * inverseTime); |
| 1 | 227 | | cameraTransform.rotation = Quaternion.Lerp(initRotation, endRotation, currentTime * inverseTime); |
| 1 | 228 | | yield return null; |
| | 229 | | } |
| | 230 | |
|
| 0 | 231 | | cameraTransitionCoroutine = null; |
| 0 | 232 | | } |
| | 233 | |
|
| | 234 | | public void Rotate(float rotationVelocity) |
| | 235 | | { |
| 0 | 236 | | avatarContainer.transform.Rotate(Time.deltaTime * rotationVelocity * Vector3.up); |
| 0 | 237 | | } |
| | 238 | |
|
| | 239 | | public void ResetRotation() |
| | 240 | | { |
| 0 | 241 | | avatarContainer.transform.rotation = avatarContainerDefaultRotation; |
| 0 | 242 | | } |
| | 243 | |
|
| | 244 | | public void PlayEmote(string emoteId, long timestamp) |
| | 245 | | { |
| 1 | 246 | | avatar.PlayEmote(emoteId, timestamp); |
| 1 | 247 | | } |
| | 248 | |
|
| | 249 | | public void Dispose() |
| | 250 | | { |
| 48 | 251 | | Destroy(gameObject); |
| 48 | 252 | | } |
| | 253 | | } |
| | 254 | | } |