< Summary

Class:DCL.MapChunk
Assembly:MapRenderer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/MapChunk.cs
Covered lines:27
Uncovered lines:13
Coverable lines:40
Total lines:100
Line coverage:67.5% (27 of 40)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Start()0%110100%
LoadChunkImage()0%110100%
UpdateCulling()0%6.196082.61%
OnDestroy()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/MapChunk.cs

#LineLine coverage
 1using DCL.Helpers;
 2using UnityEngine;
 3using UnityEngine.UI;
 4
 5namespace 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
 1097622        private void Start() { targetImage.color = Color.clear; }
 23
 24        public virtual WebRequestAsyncOperation LoadChunkImage()
 25        {
 4926            isLoadingOrLoaded = true;
 27
 4928            string url = $"{MAP_API_BASE}?center={center.x},{center.y}&width={size.x}&height={size.y}&size={tileSize}";
 29
 4930            Texture result = null;
 31
 4932            return Utils.FetchTexture(url, (x) =>
 33            {
 034                result = x;
 35
 036                if (result != null)
 37                {
 038                    result.filterMode = FilterMode.Trilinear;
 039                    result.wrapMode = TextureWrapMode.Clamp;
 040                    result.anisoLevel = 16;
 41
 042                    targetImage.texture = result;
 043                    targetImage.SetNativeSize();
 044                    targetImage.color = Color.white;
 45                }
 046            });
 47        }
 48
 49        public void UpdateCulling()
 50        {
 9851            if (owner == null)
 052                return;
 53
 9854            if (rt == null)
 4955                rt = transform as RectTransform;
 56
 9857            Vector2 myMinCoords = rt.TransformPoint(new Vector3(rt.rect.xMin, rt.rect.yMin));
 9858            Vector2 myMaxCoords = rt.TransformPoint(new Vector3(rt.rect.xMax, rt.rect.yMax));
 59
 9860            Vector2 viewMinCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMin, owner.viewport.r
 9861            Vector2 viewMaxCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMax, owner.viewport.r
 62
 63#if UNITY_EDITOR
 9864            if (VERBOSE)
 65            {
 066                var rtWorldRect = new Rect(myMinCoords.x, myMinCoords.y, myMaxCoords.x - myMinCoords.x, myMaxCoords.y - 
 067                Utils.DrawRectGizmo(rtWorldRect, Color.red, 5f);
 68            }
 69#endif
 9870            float size = (viewMaxCoords - viewMinCoords).magnitude;
 71
 9872            Rect viewportRect = new Rect(viewMinCoords, viewMaxCoords - viewMinCoords);
 9873            viewportRect.min -= Vector2.one * size;
 9874            viewportRect.max += Vector2.one * size;
 75
 76#if UNITY_EDITOR
 9877            if (VERBOSE)
 78            {
 079                Utils.DrawRectGizmo(viewportRect, Color.blue, 5f);
 80            }
 81#endif
 82
 9883            Rect myRect = new Rect(myMinCoords, myMaxCoords - myMinCoords);
 9884            bool visible = viewportRect.Overlaps(myRect, true);
 85
 9886            targetImage.enabled = visible;
 87
 9888            if (!isLoadingOrLoaded)
 89            {
 4990                loadOp = LoadChunkImage();
 91            }
 9892        }
 93
 94        private void OnDestroy()
 95        {
 548896            if (loadOp != null)
 4997                loadOp.Dispose();
 548898        }
 99    }
 100}