| | 1 | | using DCL.Helpers; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | public class MapChunk : MonoBehaviour |
| | 8 | | { |
| | 9 | | private bool VERBOSE = false; |
| | 10 | | const string MAP_API_BASE = "https://api.decentraland.org/v1/map.png"; |
| | 11 | |
|
| | 12 | | public RawImage targetImage; |
| | 13 | |
|
| | 14 | | [System.NonSerialized] public Vector2Int center; |
| | 15 | | [System.NonSerialized] public Vector2Int size; |
| | 16 | | [System.NonSerialized] public int tileSize; |
| | 17 | | [System.NonSerialized] public MapAtlas owner; |
| | 18 | | protected RectTransform rt; |
| | 19 | | protected bool isLoadingOrLoaded = false; |
| | 20 | | private WebRequestAsyncOperation loadOp; |
| | 21 | |
|
| | 22 | | public virtual WebRequestAsyncOperation LoadChunkImage() |
| | 23 | | { |
| 0 | 24 | | isLoadingOrLoaded = true; |
| | 25 | |
|
| 0 | 26 | | string url = $"{MAP_API_BASE}?center={center.x},{center.y}&width={size.x}&height={size.y}&size={tileSize}"; |
| | 27 | |
|
| 0 | 28 | | Texture2D result = null; |
| | 29 | |
|
| 0 | 30 | | return Utils.FetchTexture(url, true, (x) => |
| | 31 | | { |
| 0 | 32 | | result = x; |
| | 33 | |
|
| 0 | 34 | | if (result == null) |
| 0 | 35 | | return; |
| | 36 | |
|
| 0 | 37 | | var newTexture = new Texture2D(result.width, result.height, result.format, true); |
| 0 | 38 | | newTexture.SetPixels32(result.GetPixels32(0), 0); |
| 0 | 39 | | newTexture.Apply(true); |
| 0 | 40 | | Destroy(result); |
| | 41 | |
|
| 0 | 42 | | targetImage.texture = newTexture; |
| 0 | 43 | | targetImage.texture.wrapMode = TextureWrapMode.Clamp; |
| 0 | 44 | | targetImage.SetNativeSize(); |
| 0 | 45 | | targetImage.color = Color.white; |
| 0 | 46 | | }); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | public void UpdateCulling() |
| | 50 | | { |
| 0 | 51 | | if (owner == null) |
| 0 | 52 | | return; |
| | 53 | |
|
| 0 | 54 | | if (rt == null) |
| 0 | 55 | | rt = transform as RectTransform; |
| | 56 | |
|
| 0 | 57 | | Vector2 myMinCoords = rt.TransformPoint(new Vector3(rt.rect.xMin, rt.rect.yMin)); |
| 0 | 58 | | Vector2 myMaxCoords = rt.TransformPoint(new Vector3(rt.rect.xMax, rt.rect.yMax)); |
| | 59 | |
|
| 0 | 60 | | Vector2 viewMinCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMin, owner.viewport.r |
| 0 | 61 | | Vector2 viewMaxCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMax, owner.viewport.r |
| | 62 | |
|
| | 63 | | #if UNITY_EDITOR |
| 0 | 64 | | if (VERBOSE) |
| | 65 | | { |
| 0 | 66 | | var rtWorldRect = new Rect(myMinCoords.x, myMinCoords.y, myMaxCoords.x - myMinCoords.x, myMaxCoords.y - |
| 0 | 67 | | Utils.DrawRectGizmo(rtWorldRect, Color.red, 5f); |
| | 68 | | } |
| | 69 | | #endif |
| 0 | 70 | | float size = (viewMaxCoords - viewMinCoords).magnitude; |
| | 71 | |
|
| 0 | 72 | | Rect viewportRect = new Rect(viewMinCoords, viewMaxCoords - viewMinCoords); |
| 0 | 73 | | viewportRect.min -= Vector2.one * size; |
| 0 | 74 | | viewportRect.max += Vector2.one * size; |
| | 75 | |
|
| | 76 | | #if UNITY_EDITOR |
| 0 | 77 | | if (VERBOSE) |
| | 78 | | { |
| 0 | 79 | | Utils.DrawRectGizmo(viewportRect, Color.blue, 5f); |
| | 80 | | } |
| | 81 | | #endif |
| | 82 | |
|
| 0 | 83 | | Rect myRect = new Rect(myMinCoords, myMaxCoords - myMinCoords); |
| 0 | 84 | | bool visible = viewportRect.Overlaps(myRect, true); |
| | 85 | |
|
| 0 | 86 | | targetImage.enabled = visible; |
| | 87 | |
|
| 0 | 88 | | if (!isLoadingOrLoaded) |
| | 89 | | { |
| 0 | 90 | | loadOp = LoadChunkImage(); |
| | 91 | | } |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | private void OnDestroy() |
| | 95 | | { |
| 490 | 96 | | if (loadOp != null) |
| 0 | 97 | | loadOp.Dispose(); |
| 490 | 98 | | } |
| | 99 | | } |
| | 100 | | } |