| | 1 | | using DCLServices.MapRendererV2.MapCameraController; |
| | 2 | | using System; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | namespace DCLServices.MapRendererV2.ConsumerUtils |
| | 7 | | { |
| | 8 | | [RequireComponent(typeof(RawImage))] |
| | 9 | | [RequireComponent(typeof(RectTransform))] |
| | 10 | | public class PixelPerfectMapRendererTextureProvider : MonoBehaviour |
| | 11 | | { |
| | 12 | | [NonSerialized] |
| | 13 | | private RectTransform cachedRectTransform; |
| 20 | 14 | | private RectTransform rectTransform => cachedRectTransform ??= (RectTransform)transform; |
| | 15 | |
|
| | 16 | | private RawImage rawImage; |
| 0 | 17 | | private RawImage targetImage => rawImage ??= GetComponent<RawImage>(); |
| | 18 | |
|
| | 19 | | private IMapCameraController cameraController; |
| | 20 | | private Camera hudCamera; |
| | 21 | |
|
| 1 | 22 | | private static Vector3[] worldCorners = new Vector3[4]; |
| | 23 | |
|
| | 24 | | public void Activate(IMapCameraController cameraController) |
| | 25 | | { |
| 10 | 26 | | this.cameraController = cameraController; |
| 10 | 27 | | } |
| | 28 | |
|
| | 29 | | public void SetHudCamera(Camera hudCamera) |
| | 30 | | { |
| 11 | 31 | | this.hudCamera = hudCamera; |
| 11 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Deactivate() |
| | 35 | | { |
| 10 | 36 | | cameraController = null; |
| 10 | 37 | | } |
| | 38 | |
|
| | 39 | | public Vector2Int GetPixelPerfectTextureResolution() |
| | 40 | | { |
| | 41 | | // assumes CanvasScale Match Height = 1; |
| | 42 | |
|
| 10 | 43 | | var rectSize = rectTransform.rect.size; |
| 10 | 44 | | var ratio = rectSize.x / rectSize.y; |
| | 45 | |
|
| | 46 | | // translate rect to screen space |
| 10 | 47 | | rectTransform.GetWorldCorners(worldCorners); |
| | 48 | |
|
| 10 | 49 | | Vector2 bottomLeft = RectTransformUtility.WorldToScreenPoint(hudCamera, worldCorners[0]); |
| 10 | 50 | | Vector2 topRight = RectTransformUtility.WorldToScreenPoint(hudCamera, worldCorners[2]); |
| | 51 | |
|
| 10 | 52 | | var screenSize = topRight - bottomLeft; |
| 10 | 53 | | return new Vector2Int((int) screenSize.x, (int) screenSize.y); |
| | 54 | | } |
| | 55 | |
|
| | 56 | | private void OnRectTransformDimensionsChange() |
| | 57 | | { |
| 35 | 58 | | if (cameraController == null) |
| 35 | 59 | | return; |
| | 60 | |
|
| 0 | 61 | | cameraController.ResizeTexture(GetPixelPerfectTextureResolution()); |
| | 62 | |
|
| 0 | 63 | | targetImage.SetAllDirty(); |
| 0 | 64 | | } |
| | 65 | | } |
| | 66 | | } |