| | 1 | | using DCL; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | public class CharacterPreviewController : MonoBehaviour |
| | 8 | | { |
| | 9 | | private const int SNAPSHOT_BODY_WIDTH_RES = 256; |
| | 10 | | private const int SNAPSHOT_BODY_HEIGHT_RES = 512; |
| | 11 | |
|
| | 12 | | private const int SNAPSHOT_FACE_WIDTH_RES = 512; |
| | 13 | | private const int SNAPSHOT_FACE_HEIGHT_RES = 512; |
| | 14 | |
|
| | 15 | | private const int SNAPSHOT_FACE_256_WIDTH_RES = 256; |
| | 16 | | private const int SNAPSHOT_FACE_256_HEIGHT_RES = 256; |
| | 17 | |
|
| | 18 | | private const int SNAPSHOT_FACE_128_WIDTH_RES = 128; |
| | 19 | | private const int SNAPSHOT_FACE_128_HEIGHT_RES = 128; |
| | 20 | |
|
| | 21 | | private const int SUPERSAMPLING = 1; |
| | 22 | | private const float CAMERA_TRANSITION_TIME = 0.5f; |
| | 23 | |
|
| | 24 | | public delegate void OnSnapshotsReady(Texture2D face, Texture2D face128, Texture2D face256, Texture2D body); |
| | 25 | |
|
| | 26 | | public enum CameraFocus |
| | 27 | | { |
| | 28 | | DefaultEditing, |
| | 29 | | FaceEditing, |
| | 30 | | FaceSnapshot, |
| | 31 | | BodySnapshot |
| | 32 | | } |
| | 33 | |
|
| | 34 | | private System.Collections.Generic.Dictionary<CameraFocus, Transform> cameraFocusLookUp; |
| | 35 | |
|
| | 36 | | public new Camera camera; |
| | 37 | | public AvatarRenderer avatarRenderer; |
| | 38 | |
|
| | 39 | | public Transform defaultEditingTemplate; |
| | 40 | | public Transform faceEditingTemplate; |
| | 41 | |
|
| | 42 | | public Transform faceSnapshotTemplate; |
| | 43 | | public Transform bodySnapshotTemplate; |
| | 44 | |
|
| | 45 | | private Coroutine updateModelRoutine; |
| | 46 | |
|
| | 47 | | private bool avatarLoadFailed = false; |
| | 48 | |
|
| | 49 | | private void Awake() |
| | 50 | | { |
| 44 | 51 | | cameraFocusLookUp = new Dictionary<CameraFocus, Transform>() |
| | 52 | | { |
| | 53 | | { CameraFocus.DefaultEditing, defaultEditingTemplate }, |
| | 54 | | { CameraFocus.FaceEditing, faceEditingTemplate }, |
| | 55 | | { CameraFocus.FaceSnapshot, faceSnapshotTemplate }, |
| | 56 | | { CameraFocus.BodySnapshot, bodySnapshotTemplate }, |
| | 57 | | }; |
| 44 | 58 | | } |
| | 59 | |
|
| 234 | 60 | | public void UpdateModel(AvatarModel newModel, Action onDone) { updateModelRoutine = CoroutineStarter.Start(UpdateMod |
| | 61 | |
|
| 88 | 62 | | private void OnDestroy() { CoroutineStarter.Stop(updateModelRoutine); } |
| | 63 | |
|
| | 64 | | private IEnumerator UpdateModelRoutine(AvatarModel newModel, Action onDone) |
| | 65 | | { |
| 117 | 66 | | bool avatarDone = false; |
| 117 | 67 | | avatarLoadFailed = false; |
| | 68 | |
|
| 160 | 69 | | avatarRenderer.ApplyModel(newModel, () => avatarDone = true, () => avatarLoadFailed = true); |
| | 70 | |
|
| 18141 | 71 | | yield return new DCL.WaitUntil(() => avatarDone || avatarLoadFailed); |
| 20 | 72 | | onDone?.Invoke(); |
| 20 | 73 | | } |
| | 74 | |
|
| | 75 | | public void TakeSnapshots(OnSnapshotsReady onSuccess, Action onFailed) |
| | 76 | | { |
| 0 | 77 | | if (avatarLoadFailed) |
| | 78 | | { |
| 0 | 79 | | onFailed?.Invoke(); |
| 0 | 80 | | return; |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | StartCoroutine(TakeSnapshots_Routine(onSuccess)); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | private IEnumerator TakeSnapshots_Routine(OnSnapshotsReady callback) |
| | 87 | | { |
| 0 | 88 | | DCL.Environment.i.platform.cullingController.Stop(); |
| | 89 | |
|
| 0 | 90 | | var current = camera.targetTexture; |
| 0 | 91 | | camera.targetTexture = null; |
| 0 | 92 | | var avatarAnimator = avatarRenderer.gameObject.GetComponent<AvatarAnimatorLegacy>(); |
| | 93 | |
|
| 0 | 94 | | SetFocus(CameraFocus.FaceSnapshot, false); |
| 0 | 95 | | avatarAnimator.Reset(); |
| 0 | 96 | | yield return null; |
| 0 | 97 | | Texture2D face = Snapshot(SNAPSHOT_FACE_WIDTH_RES, SNAPSHOT_FACE_HEIGHT_RES); |
| 0 | 98 | | Texture2D face128 = Snapshot(SNAPSHOT_FACE_128_WIDTH_RES, SNAPSHOT_FACE_128_HEIGHT_RES); |
| 0 | 99 | | Texture2D face256 = Snapshot(SNAPSHOT_FACE_256_WIDTH_RES, SNAPSHOT_FACE_256_HEIGHT_RES); |
| | 100 | |
|
| 0 | 101 | | SetFocus(CameraFocus.BodySnapshot, false); |
| 0 | 102 | | avatarAnimator.Reset(); |
| 0 | 103 | | yield return null; |
| 0 | 104 | | Texture2D body = Snapshot(SNAPSHOT_BODY_WIDTH_RES, SNAPSHOT_BODY_HEIGHT_RES); |
| | 105 | |
|
| 0 | 106 | | SetFocus(CameraFocus.DefaultEditing, false); |
| | 107 | |
|
| 0 | 108 | | camera.targetTexture = current; |
| | 109 | |
|
| 0 | 110 | | DCL.Environment.i.platform.cullingController.Start(); |
| 0 | 111 | | callback?.Invoke(face, face128, face256, body); |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | private Texture2D Snapshot(int width, int height) |
| | 115 | | { |
| 0 | 116 | | RenderTexture rt = new RenderTexture(width * SUPERSAMPLING, height * SUPERSAMPLING, 32); |
| 0 | 117 | | camera.targetTexture = rt; |
| 0 | 118 | | Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false); |
| 0 | 119 | | camera.Render(); |
| 0 | 120 | | RenderTexture.active = rt; |
| 0 | 121 | | screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); |
| 0 | 122 | | screenShot.Apply(); |
| | 123 | |
|
| 0 | 124 | | return screenShot; |
| | 125 | | } |
| | 126 | |
|
| | 127 | | private Coroutine cameraTransitionCoroutine; |
| | 128 | |
|
| 0 | 129 | | public void SetFocus(CameraFocus focus, bool useTransition = true) { SetFocus(cameraFocusLookUp[focus], useTransitio |
| | 130 | |
|
| | 131 | | private void SetFocus(Transform transform, bool useTransition = true) |
| | 132 | | { |
| 0 | 133 | | if (cameraTransitionCoroutine != null) |
| | 134 | | { |
| 0 | 135 | | StopCoroutine(cameraTransitionCoroutine); |
| | 136 | | } |
| | 137 | |
|
| 0 | 138 | | if (useTransition) |
| | 139 | | { |
| 0 | 140 | | cameraTransitionCoroutine = StartCoroutine(CameraTransition(camera.transform.position, transform.position, c |
| 0 | 141 | | } |
| | 142 | | else |
| | 143 | | { |
| 0 | 144 | | var cameraTransform = camera.transform; |
| 0 | 145 | | cameraTransform.position = transform.position; |
| 0 | 146 | | cameraTransform.rotation = transform.rotation; |
| | 147 | | } |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | private IEnumerator CameraTransition(Vector3 initPos, Vector3 endPos, Quaternion initRotation, Quaternion endRotatio |
| | 151 | | { |
| 0 | 152 | | var cameraTransform = camera.transform; |
| 0 | 153 | | float currentTime = 0; |
| | 154 | |
|
| 0 | 155 | | float inverseTime = 1 / time; |
| 0 | 156 | | while (currentTime < time) |
| | 157 | | { |
| 0 | 158 | | currentTime = Mathf.Clamp(currentTime + Time.deltaTime, 0, time); |
| 0 | 159 | | cameraTransform.position = Vector3.Lerp(initPos, endPos, currentTime * inverseTime); |
| 0 | 160 | | cameraTransform.rotation = Quaternion.Lerp(initRotation, endRotation, currentTime * inverseTime); |
| 0 | 161 | | yield return null; |
| | 162 | | } |
| | 163 | |
|
| 0 | 164 | | cameraTransitionCoroutine = null; |
| 0 | 165 | | } |
| | 166 | |
|
| 0 | 167 | | public void Rotate(float rotationVelocity) { avatarRenderer.transform.Rotate(Time.deltaTime * rotationVelocity * Vec |
| | 168 | | } |