| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Threading; |
| | 6 | | using AvatarSystem; |
| | 7 | | using Cysharp.Threading.Tasks; |
| | 8 | | using DCL; |
| | 9 | | using GPUSkinning; |
| | 10 | | using UnityEngine; |
| | 11 | |
|
| | 12 | | public class CharacterPreviewController : MonoBehaviour |
| | 13 | | { |
| | 14 | | private const int SNAPSHOT_BODY_WIDTH_RES = 256; |
| | 15 | | private const int SNAPSHOT_BODY_HEIGHT_RES = 512; |
| | 16 | |
|
| | 17 | | private const int SNAPSHOT_FACE_256_WIDTH_RES = 256; |
| | 18 | | private const int SNAPSHOT_FACE_256_HEIGHT_RES = 256; |
| | 19 | |
|
| | 20 | | private const int SUPERSAMPLING = 1; |
| | 21 | | private const float CAMERA_TRANSITION_TIME = 0.5f; |
| | 22 | |
|
| | 23 | | public delegate void OnSnapshotsReady(Texture2D face256, Texture2D body); |
| | 24 | |
|
| | 25 | | public enum CameraFocus |
| | 26 | | { |
| | 27 | | DefaultEditing, |
| | 28 | | FaceEditing, |
| | 29 | | FaceSnapshot, |
| | 30 | | BodySnapshot |
| | 31 | | } |
| | 32 | |
|
| | 33 | | private System.Collections.Generic.Dictionary<CameraFocus, Transform> cameraFocusLookUp; |
| | 34 | |
|
| | 35 | | public new Camera camera; |
| | 36 | |
|
| | 37 | | public Transform defaultEditingTemplate; |
| | 38 | | public Transform faceEditingTemplate; |
| | 39 | |
|
| | 40 | | public Transform faceSnapshotTemplate; |
| | 41 | | public Transform bodySnapshotTemplate; |
| | 42 | |
|
| | 43 | | [SerializeField] private GameObject avatarContainer; |
| | 44 | | private IAvatar avatar; |
| 47 | 45 | | private readonly AvatarModel currentAvatarModel = new AvatarModel { wearables = new List<string>() }; |
| 47 | 46 | | private CancellationTokenSource loadingCts = new CancellationTokenSource(); |
| | 47 | |
|
| | 48 | | private void Awake() |
| | 49 | | { |
| 46 | 50 | | cameraFocusLookUp = new Dictionary<CameraFocus, Transform>() |
| | 51 | | { |
| | 52 | | { CameraFocus.DefaultEditing, defaultEditingTemplate }, |
| | 53 | | { CameraFocus.FaceEditing, faceEditingTemplate }, |
| | 54 | | { CameraFocus.FaceSnapshot, faceSnapshotTemplate }, |
| | 55 | | { CameraFocus.BodySnapshot, bodySnapshotTemplate }, |
| | 56 | | }; |
| 46 | 57 | | IAnimator animator = avatarContainer.gameObject.GetComponentInChildren<IAnimator>(); |
| 46 | 58 | | avatar = new AvatarSystem.Avatar( |
| | 59 | | new AvatarCurator(new WearableItemResolver()), |
| | 60 | | new Loader(new WearableLoaderFactory(), avatarContainer, new AvatarMeshCombinerHelper()), |
| | 61 | | animator, |
| | 62 | | new Visibility(), |
| | 63 | | new NoLODs(), |
| | 64 | | new SimpleGPUSkinning(), |
| | 65 | | new GPUSkinningThrottler(), |
| | 66 | | new EmoteAnimationEquipper(animator, DataStore.i.emotes) |
| | 67 | | ); |
| 46 | 68 | | } |
| | 69 | |
|
| | 70 | | public void UpdateModel(AvatarModel newModel, Action onDone) |
| | 71 | | { |
| 121 | 72 | | loadingCts?.Cancel(); |
| 121 | 73 | | loadingCts?.Dispose(); |
| 121 | 74 | | loadingCts = new CancellationTokenSource(); |
| 121 | 75 | | UpdateModelRoutine(newModel, onDone, loadingCts.Token); |
| 121 | 76 | | } |
| | 77 | |
|
| | 78 | | private void OnDestroy() |
| | 79 | | { |
| 46 | 80 | | loadingCts?.Cancel(); |
| 46 | 81 | | loadingCts?.Dispose(); |
| 46 | 82 | | loadingCts = null; |
| 46 | 83 | | avatar?.Dispose(); |
| 46 | 84 | | } |
| | 85 | |
|
| | 86 | | private async UniTaskVoid UpdateModelRoutine(AvatarModel newModel, Action onDone, CancellationToken ct) |
| | 87 | | { |
| 121 | 88 | | if (newModel.HaveSameWearablesAndColors(currentAvatarModel)) |
| | 89 | | { |
| 45 | 90 | | onDone?.Invoke(); |
| 45 | 91 | | return; |
| | 92 | | } |
| | 93 | |
|
| 76 | 94 | | currentAvatarModel.CopyFrom(newModel); |
| 76 | 95 | | List<string> wearables = new List<string>(newModel.wearables); |
| 76 | 96 | | wearables.Add(newModel.bodyShape); |
| 228 | 97 | | await avatar.Load(wearables, new AvatarSettings |
| | 98 | | { |
| | 99 | | bodyshapeId = newModel.bodyShape, |
| | 100 | | eyesColor = newModel.eyeColor, |
| | 101 | | hairColor = newModel.hairColor, |
| | 102 | | skinColor = newModel.skinColor |
| | 103 | |
|
| | 104 | | }, ct); |
| | 105 | |
|
| 76 | 106 | | onDone?.Invoke(); |
| 121 | 107 | | } |
| | 108 | |
|
| | 109 | | public void TakeSnapshots(OnSnapshotsReady onSuccess, Action onFailed) |
| | 110 | | { |
| 0 | 111 | | if (avatar.status != IAvatar.Status.Loaded) |
| | 112 | | { |
| 0 | 113 | | onFailed?.Invoke(); |
| 0 | 114 | | return; |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | StartCoroutine(TakeSnapshots_Routine(onSuccess)); |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | private IEnumerator TakeSnapshots_Routine(OnSnapshotsReady callback) |
| | 121 | | { |
| 0 | 122 | | DCL.Environment.i.platform.cullingController.Stop(); |
| | 123 | |
|
| 0 | 124 | | var current = camera.targetTexture; |
| 0 | 125 | | camera.targetTexture = null; |
| 0 | 126 | | var avatarAnimator = avatarContainer.gameObject.GetComponentInChildren<AvatarAnimatorLegacy>(); |
| | 127 | |
|
| 0 | 128 | | SetFocus(CameraFocus.FaceSnapshot, false); |
| 0 | 129 | | avatarAnimator.Reset(); |
| 0 | 130 | | yield return null; |
| 0 | 131 | | Texture2D face256 = Snapshot(SNAPSHOT_FACE_256_WIDTH_RES, SNAPSHOT_FACE_256_HEIGHT_RES); |
| | 132 | |
|
| 0 | 133 | | SetFocus(CameraFocus.BodySnapshot, false); |
| 0 | 134 | | avatarAnimator.Reset(); |
| 0 | 135 | | yield return null; |
| 0 | 136 | | Texture2D body = Snapshot(SNAPSHOT_BODY_WIDTH_RES, SNAPSHOT_BODY_HEIGHT_RES); |
| | 137 | |
|
| 0 | 138 | | SetFocus(CameraFocus.DefaultEditing, false); |
| | 139 | |
|
| 0 | 140 | | camera.targetTexture = current; |
| | 141 | |
|
| 0 | 142 | | DCL.Environment.i.platform.cullingController.Start(); |
| 0 | 143 | | callback?.Invoke(face256, body); |
| 0 | 144 | | } |
| | 145 | |
|
| | 146 | | private Texture2D Snapshot(int width, int height) |
| | 147 | | { |
| 0 | 148 | | RenderTexture rt = new RenderTexture(width * SUPERSAMPLING, height * SUPERSAMPLING, 32); |
| 0 | 149 | | camera.targetTexture = rt; |
| 0 | 150 | | Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false); |
| 0 | 151 | | camera.Render(); |
| 0 | 152 | | RenderTexture.active = rt; |
| 0 | 153 | | screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); |
| 0 | 154 | | screenShot.Apply(); |
| | 155 | |
|
| 0 | 156 | | return screenShot; |
| | 157 | | } |
| | 158 | |
|
| | 159 | | private Coroutine cameraTransitionCoroutine; |
| | 160 | |
|
| 90 | 161 | | public void SetFocus(CameraFocus focus, bool useTransition = true) { SetFocus(cameraFocusLookUp[focus], useTransitio |
| | 162 | |
|
| | 163 | | private void SetFocus(Transform transform, bool useTransition = true) |
| | 164 | | { |
| 45 | 165 | | if (cameraTransitionCoroutine != null) |
| | 166 | | { |
| 0 | 167 | | StopCoroutine(cameraTransitionCoroutine); |
| | 168 | | } |
| | 169 | |
|
| 45 | 170 | | if (useTransition) |
| | 171 | | { |
| 45 | 172 | | cameraTransitionCoroutine = StartCoroutine(CameraTransition(camera.transform.position, transform.position, c |
| 45 | 173 | | } |
| | 174 | | else |
| | 175 | | { |
| 0 | 176 | | var cameraTransform = camera.transform; |
| 0 | 177 | | cameraTransform.position = transform.position; |
| 0 | 178 | | cameraTransform.rotation = transform.rotation; |
| | 179 | | } |
| 0 | 180 | | } |
| | 181 | |
|
| | 182 | | private IEnumerator CameraTransition(Vector3 initPos, Vector3 endPos, Quaternion initRotation, Quaternion endRotatio |
| | 183 | | { |
| 45 | 184 | | var cameraTransform = camera.transform; |
| 45 | 185 | | float currentTime = 0; |
| | 186 | |
|
| 45 | 187 | | float inverseTime = 1 / time; |
| 45 | 188 | | while (currentTime < time) |
| | 189 | | { |
| 45 | 190 | | currentTime = Mathf.Clamp(currentTime + Time.deltaTime, 0, time); |
| 45 | 191 | | cameraTransform.position = Vector3.Lerp(initPos, endPos, currentTime * inverseTime); |
| 45 | 192 | | cameraTransform.rotation = Quaternion.Lerp(initRotation, endRotation, currentTime * inverseTime); |
| 45 | 193 | | yield return null; |
| | 194 | | } |
| | 195 | |
|
| 0 | 196 | | cameraTransitionCoroutine = null; |
| 0 | 197 | | } |
| | 198 | |
|
| 0 | 199 | | public void Rotate(float rotationVelocity) { avatarContainer.transform.Rotate(Time.deltaTime * rotationVelocity * Ve |
| | 200 | | } |