Skip to content

Commit

Permalink
Merge pull request #1 from Netherlands3D/feature/performance-improvem…
Browse files Browse the repository at this point in the history
…ents

Feature/performance improvements
  • Loading branch information
sambaas authored Feb 13, 2024
2 parents 3786cf5 + 30ac12d commit 3106698
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 47 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this package will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.1.3] - 2024-02-13

- Performance fixes in higher zoomlevels

## [1.1.2]

### Removed
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripts/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private IEnumerator FadeInRawImage()
while(TextureTargetRawImage.color.a < 1.0f)
{
TextureTargetRawImage.color = new Color(TextureTargetRawImage.color.r, TextureTargetRawImage.color.g, TextureTargetRawImage.color.b, TextureTargetRawImage.color.a + fadeSpeed * Time.deltaTime);
yield return new WaitForEndOfFrame();
yield return null;
}
}

Expand Down
110 changes: 65 additions & 45 deletions Runtime/Scripts/WMTSMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public class WMTSMap : MonoBehaviour
[Tooltip("Defaults to Camera.main")]
[SerializeField] private Camera cameraMoveTarget;

/// <summary>
/// Cached sizeDelta of the minimap
/// </summary>
private Vector2 mapSizeDelta;
/// <summary>
/// The current index layer of tile layers
/// </summary>
Expand Down Expand Up @@ -103,27 +107,30 @@ public class WMTSMap : MonoBehaviour
/// <summary>
/// The minimap UI that handles all the UI
/// </summary>
private MinimapUI minimapUI;
private MinimapUI parentMinimapUI;
/// <summary>
/// The rect transform of the minimap
/// </summary>
private RectTransform rectTransformMinimapUI;
private RectTransform parentUIRectTransform;
/// <summary>
/// The rect transform
/// </summary>
private RectTransform rectTransform;
private RectTransform mapRectTransform;
/// <summary>
/// Contains the minimap layers with tiles
/// </summary>
private Dictionary<int, Dictionary<Vector2, Tile>> tileLayers = new Dictionary<int, Dictionary<Vector2, Tile>>();

private Vector2 lastMapSizeDelta;
private Vector3 lastMinimapPosition;

private void Awake()
{
if(!cameraMoveTarget) cameraMoveTarget = Camera.main;

minimapUI = GetComponentInParent<MinimapUI>();
rectTransform = GetComponent<RectTransform>();
rectTransformMinimapUI = (RectTransform)minimapUI.transform;
parentMinimapUI = GetComponentInParent<MinimapUI>();
parentUIRectTransform = (RectTransform)parentMinimapUI.transform;
mapRectTransform = GetComponent<RectTransform>();
}

private void Start()
Expand Down Expand Up @@ -156,10 +163,16 @@ private void Start()

private void Update()
{
Clamp();
mapSizeDelta = parentUIRectTransform.sizeDelta;

if (mapSizeDelta != lastMapSizeDelta || transform.localPosition != lastMinimapPosition)
{
Clamp();
UpdateLayerTiles(tileLayers[layerIndex]);
lastMapSizeDelta = mapSizeDelta;
lastMinimapPosition = transform.localPosition;
}

//Continiously check if tiles of the active layer identifier should be loaded
UpdateLayerTiles(tileLayers[layerIndex]);
MovePointer();
}

Expand All @@ -171,12 +184,12 @@ public void Clamp()
var maxPositionXInUnits = -(boundsInMeters.x / startMeterInPixels) * transform.localScale.x;
var maxPositionYInUnits = (boundsInMeters.y / startMeterInPixels) * transform.localScale.x;

var XPadding = rectTransformMinimapUI.sizeDelta.x*0.5f;
var YPadding = rectTransformMinimapUI.sizeDelta.y*0.5f;
var XPadding = mapSizeDelta.x*0.5f;
var YPadding = mapSizeDelta.y*0.5f;

this.transform.localPosition = new Vector3(
Mathf.Clamp(this.transform.localPosition.x, maxPositionXInUnits + rectTransformMinimapUI.sizeDelta.x - XPadding, XPadding),
Mathf.Clamp(this.transform.localPosition.y, rectTransformMinimapUI.sizeDelta.y - YPadding, maxPositionYInUnits + YPadding),
Mathf.Clamp(this.transform.localPosition.x, maxPositionXInUnits + mapSizeDelta.x - XPadding, XPadding),
Mathf.Clamp(this.transform.localPosition.y, mapSizeDelta.y - YPadding, maxPositionYInUnits + YPadding),
0
);
}
Expand Down Expand Up @@ -234,7 +247,7 @@ public Vector3 DeterminePositionOnMap(Vector3RD sourceRDPosition)
/// <param name="targetPosition">RD coordinate to place the object</param>
public void PositionObjectOnMap(RectTransform targetObject, Vector3RD targetPosition)
{
targetObject.transform.localScale = Vector3.one / rectTransform.localScale.x;
targetObject.transform.localScale = Vector3.one / mapRectTransform.localScale.x;
targetObject.transform.localPosition = DeterminePositionOnMap(targetPosition);
}

Expand Down Expand Up @@ -326,7 +339,7 @@ private void MovePointer()

if(CenterPointerInView)
{
this.transform.localPosition = -pointer.localPosition * rectTransform.localScale.x + (Vector3)rectTransformMinimapUI.sizeDelta * 0.5f;
this.transform.localPosition = -pointer.localPosition * mapRectTransform.localScale.x + (Vector3)parentUIRectTransform.sizeDelta * 0.5f;
}
}

Expand Down Expand Up @@ -356,18 +369,34 @@ private void RemoveOtherLayers()
/// <param name="tileList"></param>
private void UpdateLayerTiles(Dictionary<Vector2, Tile> tileList)
{
for(int x = 0; x <= boundsTiles.x; x++)
mapSizeDelta = parentUIRectTransform.sizeDelta;
var localScale = mapRectTransform.localScale;
var localPosition = mapRectTransform.transform.localPosition;


var tileSizeX = tileSize * localScale.x;
var tileSizeY = tileSize * localScale.y;

var startX = Mathf.Max(0, Mathf.FloorToInt(-localPosition.x / tileSizeX));
var startY = Mathf.Max(0, Mathf.FloorToInt((localPosition.y-mapSizeDelta.y) / tileSizeY));

var endX = Mathf.CeilToInt((mapSizeDelta.x - localPosition.x) / tileSizeX);
var endY = Mathf.FloorToInt((mapSizeDelta.y + localPosition.y) / tileSizeY);

var tilesToRemove = new List<Vector2>(tileList.Keys);

for (int x = startX; x <= endX; x++)
{
for(int y = 0; y <= boundsTiles.y; y++)
{
for (int y = startY; y <= endY; y++)
{
Vector2 tileKey;

//Tile position within this container
float xPosition = (x * tileSize) - (layerTilesOffset.x * tileSize);
float yPosition = -((y * tileSize) - (layerTilesOffset.y * tileSize));
// Tile position within this container
float tileXPosition = (x * tileSize) - (layerTilesOffset.x * tileSize);
float tileYPosition = -((y * tileSize) - (layerTilesOffset.y * tileSize));

//Origin alignment determines the way we count our grid
switch(minimapConfig.TileMatrixSet.minimapOriginAlignment)
// Origin alignment determines the way we count our grid
switch (minimapConfig.TileMatrixSet.minimapOriginAlignment)
{
case TileMatrixSet.OriginAlignment.BottomLeft:
tileKey = new Vector2(x + tileOffset.x, (float)(divide - 1) - (y + tileOffset.y));
Expand All @@ -378,32 +407,23 @@ private void UpdateLayerTiles(Dictionary<Vector2, Tile> tileList)
break;
}

//Tile position to check if they are in viewer
float compareXPosition = xPosition * rectTransform.localScale.x + rectTransform.transform.localPosition.x;
float compareYPosition = yPosition * rectTransform.localScale.x + rectTransform.transform.localPosition.y;

//Is this tile within the viewer rectangle?
bool xWithinView = (compareXPosition + baseTileSize > 0 && compareXPosition < rectTransformMinimapUI.sizeDelta.x);
bool yWithinView = (compareYPosition > 0 && compareYPosition - baseTileSize < rectTransformMinimapUI.sizeDelta.y);

if(xWithinView && yWithinView)
{
if(!tileList.ContainsKey(tileKey))
{
var newTileObject = new GameObject();
var mapTile = newTileObject.AddComponent<Tile>();
mapTile.Initialize(this.transform, layerIndex, tileSize, xPosition, yPosition, tileKey, minimapConfig);

tileList.Add(tileKey, mapTile);
}
}
else if(tileList.ContainsKey(tileKey))
if (!tileList.TryGetValue(tileKey, out var existingTile))
{
Destroy(tileList[tileKey].gameObject);
tileList.Remove(tileKey);
var newTileObject = new GameObject();
var mapTile = newTileObject.AddComponent<Tile>();
mapTile.Initialize(this.transform, layerIndex, tileSize, tileXPosition, tileYPosition, tileKey, minimapConfig);

tileList.Add(tileKey, mapTile);
}
tilesToRemove.Remove(tileKey);
}
}

foreach (var tileKey in tilesToRemove)
{
Destroy(tileList[tileKey].gameObject);
tileList.Remove(tileKey);
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eu.netherlands3d.minimap",
"version": "1.1.2",
"version": "1.1.3",
"displayName": "Netherlands3D - Minimap",
"description": "A unity canvas minimap for WMTS services",
"unity": "2022.2",
Expand Down

0 comments on commit 3106698

Please sign in to comment.