From ca1d5b33109e2b6d317d4411fcd9375b680d7273 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Thu, 12 Dec 2024 12:21:02 +0100 Subject: [PATCH] Tweak zoomlevels to show a crispier image instead of using the calculations that match the 'official' zoom levels --- Assets/ATMDataController.cs | 35 +++++++++++++------ Assets/Scenes/Main.unity | 2 ++ .../CartesianTiles/ImageProjectionLayer.cs | 12 ++++--- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/Assets/ATMDataController.cs b/Assets/ATMDataController.cs index 1be00c9d..ff04a3ac 100644 --- a/Assets/ATMDataController.cs +++ b/Assets/ATMDataController.cs @@ -14,6 +14,11 @@ public class ATMDataController : MonoBehaviour private const double equatorialCircumference = 2 * Mathf.PI * earthRadius; private const double log2x = 0.30102999566d; + [Tooltip("The zoomlevels are scaled exponentially across this height range, tweak to get better tiling visuals")] + [SerializeField] private float minHeightForZoomLevels = 200f; + [Tooltip("The zoomlevels are scaled exponentially across this height range, tweak to get better tiling visuals")] + [SerializeField] private float maxHeightForZoomLevels = 40_000_000f; + private int currentYear; private int lastValidYear; public delegate void ATMDataHandler (int year); @@ -100,6 +105,13 @@ public int RoundDownYearMaps(int inputYear) { 20, 23 } }; + private Transform cameraTransform; + + private void Awake() + { + cameraTransform = Camera.main.transform; + } + public int GetTileSizeForZoomLevel(int zoomLevel) { return atmZoomTileSizes[zoomLevel]; @@ -115,18 +127,19 @@ public int GetTileSizeForZoomLevel(int zoomLevel) public int CalculateZoomLevel() { - Vector3 camPosition = Camera.main.transform.position; - var unityCoordinate = new Coordinate( - CoordinateSystem.Unity, - camPosition.x, - camPosition.y, - camPosition.z - ); - Coordinate coord = unityCoordinate.Convert(CoordinateSystem.WGS84); - double latitude = coord.northing; - double cosLatitude = Math.Cos(latitude * Mathf.Deg2Rad); - double zoomLevel = Math.Log(equatorialCircumference * cosLatitude / camPosition.y) / log2x; + Vector3 camPosition = cameraTransform.position; + var currentYearZoomBounds = GetZoomBounds(); + + float minimumZoomLevel = atmZoomTileSizes.Keys.Min(); + float maximumZoomLevel = atmZoomTileSizes.Keys.Max(); + float currentHeight = camPosition.y; + + var zoomLevel = maximumZoomLevel + - (Mathf.Log(currentHeight / minHeightForZoomLevels) / Mathf.Log(maxHeightForZoomLevels / minHeightForZoomLevels)) + * (maximumZoomLevel - minimumZoomLevel); + + // Clamp to the min and max of this specific zoom level. zoomLevel = Math.Clamp(zoomLevel, currentYearZoomBounds.minZoom, currentYearZoomBounds.maxZoom); return (int)zoomLevel; diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 0250aa18..d92eb4e4 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -1073,6 +1073,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 18e1f17bb2b6bc34c892ceb40d510e4e, type: 3} m_Name: m_EditorClassIdentifier: + minHeightForZoomLevels: 100 + maxHeightForZoomLevels: 10000 --- !u!4 &1444587089 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/CartesianTiles/ImageProjectionLayer.cs b/Assets/Scripts/CartesianTiles/ImageProjectionLayer.cs index 9cf850d1..a84603a5 100644 --- a/Assets/Scripts/CartesianTiles/ImageProjectionLayer.cs +++ b/Assets/Scripts/CartesianTiles/ImageProjectionLayer.cs @@ -54,12 +54,16 @@ public override void HandleTile(TileChange tileChange, Action callba tiles[tileKey].runningCoroutine = StartCoroutine(DownloadDataAndGenerateTexture(tileChange, callback)); break; case TileAction.Upgrade: - tiles[tileKey].unityLOD++; - tiles[tileKey].runningCoroutine = StartCoroutine(DownloadDataAndGenerateTexture(tileChange, callback)); + if (tiles.ContainsKey(tileKey)) { + tiles[tileKey].unityLOD++; + tiles[tileKey].runningCoroutine = StartCoroutine(DownloadDataAndGenerateTexture(tileChange, callback)); + } break; case TileAction.Downgrade: - tiles[tileKey].unityLOD--; - tiles[tileKey].runningCoroutine = StartCoroutine(DownloadDataAndGenerateTexture(tileChange, callback)); + if (tiles.ContainsKey(tileKey)) { + tiles[tileKey].unityLOD--; + tiles[tileKey].runningCoroutine = StartCoroutine(DownloadDataAndGenerateTexture(tileChange, callback)); + } break; case TileAction.Remove: InteruptRunningProcesses(tileKey);