| | 1 | | using AvatarSystem; |
| | 2 | | using Cysharp.Threading.Tasks; |
| | 3 | | using MainScripts.DCL.Controllers.HUD.CharacterPreview; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Rendering; |
| | 9 | | using UnityEngine.Rendering.Universal; |
| | 10 | | using UnityEngine.UI; |
| | 11 | |
|
| | 12 | | namespace DCL.Backpack |
| | 13 | | { |
| | 14 | | public class BackpackPreviewPanel : BaseComponentView |
| | 15 | | { |
| | 16 | | private const string RESET_PREVIEW_ANIMATION = "Idle"; |
| 1 | 17 | | private static readonly Bounds CAMERA_LIMITS = new ( |
| | 18 | | new Vector3(0f, 0.85f, 2.4f), |
| | 19 | | new Vector3(0f, 1.7f, 2.2f) |
| | 20 | | ); |
| | 21 | | private const float CAMERA_ZOOM_CENTER = 1.4f; |
| | 22 | | private const float CAMERA_ZOOM_BOTTOM_MAX_OFFSET = 1.1f; |
| | 23 | | private const float CAMERA_ZOOM_TOP_MAX_OFFSET = 0.3f; |
| | 24 | |
|
| | 25 | | [SerializeField] private RectTransform avatarPreviewPanel; |
| | 26 | | [SerializeField] private RawImage avatarPreviewImage; |
| | 27 | | [SerializeField] internal GameObject avatarPreviewLoadingSpinner; |
| | 28 | |
|
| | 29 | | [Header("MOUSE INPUT CONFIGURATION")] |
| | 30 | | [SerializeField] internal CharacterPreviewInputDetector characterPreviewInputDetector; |
| | 31 | | [SerializeField] internal InputAction_Hold firstClickAction; |
| | 32 | | [SerializeField] internal InputAction_Hold secondClickAction; |
| | 33 | | [SerializeField] internal InputAction_Hold middleClickAction; |
| | 34 | | [SerializeField] internal InputAction_Measurable mouseWheelAction; |
| | 35 | |
|
| | 36 | | [Header("ROTATE CONFIGURATION")] |
| 36 | 37 | | [SerializeField] internal float rotationFactor = -30f; |
| 36 | 38 | | [SerializeField] internal float slowDownTime = 0.5f; |
| | 39 | | [SerializeField] internal Texture2D rotateCursorTexture; |
| | 40 | |
|
| | 41 | | [Header("PANNING CONFIGURATION")] |
| 36 | 42 | | [SerializeField] internal float panSpeed = 0.2f; |
| 36 | 43 | | [SerializeField] internal bool allowVerticalPanning = true; |
| | 44 | | [SerializeField] internal bool allowHorizontalPanning = false; |
| 36 | 45 | | [SerializeField] internal float panningInertiaDuration = 0.5f; |
| | 46 | | [SerializeField] internal Texture2D panningCursorTexture; |
| | 47 | |
|
| | 48 | | [Header("ZOOM CONFIGURATION")] |
| 36 | 49 | | [SerializeField] internal float zoomSpeed = 5.0f; |
| 36 | 50 | | [SerializeField] internal float smoothTime = 0.2f; |
| | 51 | |
|
| | 52 | | public delegate void OnSnapshotsReady(Texture2D face256, Texture2D body); |
| 0 | 53 | | public IReadOnlyList<SkinnedMeshRenderer> originalVisibleRenderers => characterPreviewController?.originalVisibl |
| 0 | 54 | | public IAvatarEmotesController EmotesController => characterPreviewController.GetEmotesController(); |
| | 55 | |
|
| | 56 | | private ICharacterPreviewController characterPreviewController; |
| | 57 | | private IPreviewCameraRotationController avatarPreviewRotationController; |
| | 58 | | private IPreviewCameraPanningController avatarPreviewPanningController; |
| | 59 | | private IPreviewCameraZoomController avatarPreviewZoomController; |
| 36 | 60 | | private float prevRenderScale = 1.0f; |
| | 61 | |
|
| | 62 | | public void Initialize( |
| | 63 | | ICharacterPreviewFactory characterPreviewFactory, |
| | 64 | | IPreviewCameraRotationController avatarPreviewRotationController, |
| | 65 | | IPreviewCameraPanningController avatarPreviewPanningController, |
| | 66 | | IPreviewCameraZoomController avatarPreviewZoomController) |
| | 67 | | { |
| 34 | 68 | | characterPreviewController = characterPreviewFactory.Create( |
| | 69 | | loadingMode: CharacterPreviewMode.WithoutHologram, |
| | 70 | | renderTexture: (RenderTexture) avatarPreviewImage.texture, |
| | 71 | | isVisible: false, |
| | 72 | | previewCameraFocus: PreviewCameraFocus.DefaultEditing, |
| | 73 | | isAvatarShadowActive: true); |
| 34 | 74 | | characterPreviewController.SetCameraLimits(CAMERA_LIMITS); |
| 34 | 75 | | characterPreviewController.ConfigureZoom( |
| | 76 | | CAMERA_ZOOM_CENTER, CAMERA_ZOOM_BOTTOM_MAX_OFFSET, CAMERA_ZOOM_TOP_MAX_OFFSET); |
| 34 | 77 | | characterPreviewController.SetFocus(PreviewCameraFocus.DefaultEditing); |
| | 78 | |
|
| 34 | 79 | | this.avatarPreviewRotationController = avatarPreviewRotationController; |
| 34 | 80 | | this.avatarPreviewRotationController.Configure( |
| | 81 | | firstClickAction, |
| | 82 | | rotationFactor, |
| | 83 | | slowDownTime, |
| | 84 | | characterPreviewInputDetector, |
| | 85 | | rotateCursorTexture); |
| 34 | 86 | | this.avatarPreviewRotationController.OnHorizontalRotation += OnPreviewRotation; |
| | 87 | |
|
| 34 | 88 | | this.avatarPreviewPanningController = avatarPreviewPanningController; |
| 34 | 89 | | this.avatarPreviewPanningController.Configure( |
| | 90 | | secondClickAction, |
| | 91 | | middleClickAction, |
| | 92 | | panSpeed, |
| | 93 | | allowVerticalPanning, |
| | 94 | | allowHorizontalPanning, |
| | 95 | | panningInertiaDuration, |
| | 96 | | characterPreviewInputDetector, |
| | 97 | | panningCursorTexture); |
| 34 | 98 | | this.avatarPreviewPanningController.OnPanning += OnPreviewPanning; |
| | 99 | |
|
| 34 | 100 | | this.avatarPreviewZoomController = avatarPreviewZoomController; |
| 34 | 101 | | this.avatarPreviewZoomController.Configure( |
| | 102 | | mouseWheelAction, |
| | 103 | | zoomSpeed, |
| | 104 | | smoothTime, |
| | 105 | | characterPreviewInputDetector); |
| 34 | 106 | | this.avatarPreviewZoomController.OnZoom += OnPreviewZoom; |
| 34 | 107 | | } |
| | 108 | |
|
| | 109 | | public override void Dispose() |
| | 110 | | { |
| 85 | 111 | | base.Dispose(); |
| | 112 | |
|
| 85 | 113 | | avatarPreviewRotationController.OnHorizontalRotation -= OnPreviewRotation; |
| 85 | 114 | | avatarPreviewPanningController.OnPanning -= OnPreviewPanning; |
| 85 | 115 | | avatarPreviewZoomController.OnZoom -= OnPreviewZoom; |
| 85 | 116 | | avatarPreviewRotationController.Dispose(); |
| 85 | 117 | | avatarPreviewPanningController.Dispose(); |
| 85 | 118 | | avatarPreviewZoomController.Dispose(); |
| 85 | 119 | | } |
| | 120 | |
|
| 0 | 121 | | public override void RefreshControl() { } |
| | 122 | |
|
| | 123 | | public void SetPreviewEnabled(bool isEnabled) |
| | 124 | | { |
| 4 | 125 | | characterPreviewController.SetEnabled(isEnabled); |
| 4 | 126 | | FixThePreviewRenderingSomehowRelatedToTheRenderScale(isEnabled); |
| 4 | 127 | | } |
| | 128 | |
|
| | 129 | | public void PlayPreviewEmote(string emoteId) => |
| 2 | 130 | | characterPreviewController.PlayEmote(emoteId, (long)Time.realtimeSinceStartup); |
| | 131 | |
|
| | 132 | | public void PlayPreviewEmote(string emoteId, long timestamp) => |
| 2 | 133 | | characterPreviewController.PlayEmote(emoteId, timestamp); |
| | 134 | |
|
| | 135 | | public void ResetPreviewEmote() => |
| 5 | 136 | | characterPreviewController.StopEmote(); |
| | 137 | |
|
| | 138 | | public void ResetPreviewRotation() => |
| 5 | 139 | | characterPreviewController.ResetRotation(); |
| | 140 | |
|
| | 141 | | public async UniTask TryUpdatePreviewModelAsync(AvatarModel avatarModelToUpdate, CancellationToken ct) => |
| 2 | 142 | | await characterPreviewController.TryUpdateModelAsync(avatarModelToUpdate, ct); |
| | 143 | |
|
| | 144 | | public void SetLoadingActive(bool isActive) => |
| 21 | 145 | | avatarPreviewLoadingSpinner.SetActive(isActive); |
| | 146 | |
|
| | 147 | | public void TakeSnapshots(OnSnapshotsReady onSuccess, Action onFailed) => |
| 1 | 148 | | characterPreviewController.TakeSnapshots( |
| 0 | 149 | | (face256, body) => onSuccess?.Invoke(face256, body), |
| 0 | 150 | | () => onFailed?.Invoke()); |
| | 151 | |
|
| | 152 | | public void SetFocus(PreviewCameraFocus focus, bool useTransition = true) => |
| 8 | 153 | | characterPreviewController.SetFocus(focus, useTransition); |
| | 154 | |
|
| | 155 | | private void OnPreviewRotation(float angularVelocity) => |
| 2 | 156 | | characterPreviewController.Rotate(angularVelocity); |
| | 157 | |
|
| | 158 | | private void OnPreviewPanning(Vector3 positionDelta) => |
| 1 | 159 | | characterPreviewController.MoveCamera(positionDelta, true); |
| | 160 | |
|
| | 161 | | private void OnPreviewZoom(Vector3 delta) => |
| 1 | 162 | | characterPreviewController.MoveCamera(delta, true); |
| | 163 | |
|
| | 164 | | // TODO: We have to investigate why we have to use this workaround to fix the preview rendering |
| | 165 | | private void FixThePreviewRenderingSomehowRelatedToTheRenderScale(bool isEnabled) |
| | 166 | | { |
| | 167 | | // NOTE(Brian): SSAO doesn't work correctly with the offset avatar preview if the renderScale != 1.0 |
| 4 | 168 | | var asset = GraphicsSettings.renderPipelineAsset as UniversalRenderPipelineAsset; |
| | 169 | |
|
| 4 | 170 | | if (asset == null) |
| 0 | 171 | | return; |
| | 172 | |
|
| 4 | 173 | | if (isEnabled) |
| | 174 | | { |
| 2 | 175 | | prevRenderScale = asset.renderScale; |
| 2 | 176 | | asset.renderScale = 1.0f; |
| | 177 | | } |
| | 178 | | else |
| 2 | 179 | | asset.renderScale = prevRenderScale; |
| 2 | 180 | | } |
| | 181 | | } |
| | 182 | | } |