From 8526c5f3c437d25cc90eb8413fcfbb5bbb6c2e18 Mon Sep 17 00:00:00 2001
From: bartjuhhh <bart.knuiman@wur.nl>
Date: Wed, 27 Sep 2023 17:27:45 +0200
Subject: [PATCH] Improvement for when download failed. It no longe remains
 'not finished'.

---
 Runtime/VectorTile.cs | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/Runtime/VectorTile.cs b/Runtime/VectorTile.cs
index 19230f3..8ac514e 100644
--- a/Runtime/VectorTile.cs
+++ b/Runtime/VectorTile.cs
@@ -115,6 +115,10 @@ namespace Wander
                     parseTileTask = null;
                 }
             }
+            else // Done but not succesful.
+            { 
+                finished = true;
+            }
 
             return finished;
         }
@@ -239,11 +243,11 @@ namespace Wander
                     node.children = new QuadTreeNode[4];
                     var hs = (node.max - node.min) / 2;
                     Vector2 [] mins = new [] {
-                    node.min,
-                    new Vector2(node.min.x+hs.x, node.min.y),
-                    new Vector2(node.min.x,      node.min.y+hs.y),
-                    new Vector2(node.min.x+hs.x, node.min.y+hs.y)
-                };
+                        node.min,
+                        new Vector2(node.min.x+hs.x, node.min.y),
+                        new Vector2(node.min.x,      node.min.y+hs.y),
+                        new Vector2(node.min.x+hs.x, node.min.y+hs.y)
+                    };
                     for (int i = 0;i < 4;i++)
                     {
                         var n2   = new QuadTreeNode();
@@ -271,7 +275,7 @@ namespace Wander
                     }
                     node.triangles = null;
                 }
-                UnityEngine.Debug.Log( "Optimize for raytracing took " + sw.ElapsedMilliseconds );
+            //    UnityEngine.Debug.Log( "Optimize for raytracing took " + sw.ElapsedMilliseconds );
             }
             catch (Exception e)
             {
@@ -308,11 +312,8 @@ namespace Wander
         // If no polygon was hit, 15 is encoded (reserved).
         // If polygon was hit, but no feature was matched, also 15 is encoded.
         // Returns a list of failed to match pixels. This can be due to geometry not exactly matching or a layer not being found.
-        // Also outputs a remap of layer indices. For instance for a specific tile, only layerIdx 1, 6 and 3 are used.
-        // Then it applies a remapping of 1->0, 6->1, 3->2. So that in the shader only 3 textures are needed starting from 0, 1.. etc.
         public byte[] RenderToTextureSingle(
             int resolution,
-            out Dictionary<byte, byte> remappedLayerIndices,
             out List<Vector3Int> failedPixels,
             ref bool cancelToken )
         {
@@ -325,7 +326,6 @@ namespace Wander
 
             byte [] texture = new byte[resolution*resolution/2];
             failedPixels = new List<Vector3Int>();
-            remappedLayerIndices = new Dictionary<byte, byte>();
             float oneOverRes = 1.0f / resolution;
             int cachedTriIdx = 0;
             float fx = 4096 * oneOverRes; // TODO 4096 may be different in other implementations.
@@ -406,8 +406,18 @@ namespace Wander
                 }
             }
 
+
+         //   UnityEngine.Debug.Log( "Render to texture took " + sw.ElapsedMilliseconds );
+            return texture;
+        }
+
+        // Remaps layer indices. For instance for a specific tile, only layerIdx 1, 6 and 3 are used.
+        // Then it applies a remapping of 1->0, 6->1, 3->2. So that in the shader only 3 textures are needed starting from 0, 1.. etc.
+        public static Dictionary<byte, byte> FindLayersInTexture(byte[] texture, int resolution, ref bool cancelToken)
+        {
             // Determine number of different layers. For instance, if only layer 3, 8, 14 and 15 are used, we select
             // a material that only uses 4 textures and put 3 -> Albedo_0 -> 8 to Albedo_1, etc. in the shader.
+            Dictionary<byte, byte> remappedLayerIndices = new Dictionary<byte, byte>();
             byte cachedPixel = 15;
             int  remappedIndexCounter = -1;
             byte remappedPixel = 15;
@@ -454,12 +464,11 @@ namespace Wander
             }
 
             UnityEngine.Debug.Assert( remappedIndexCounter <= 15, "Exceeded layer count." ); // Invalid pixel is not added to map, so max must be 15, not 16.
-
-            UnityEngine.Debug.Log( "Render to texture took " + sw.ElapsedMilliseconds );
-            return texture;
+            return remappedLayerIndices;
         }
     }
 
+
     public static class VectorTileLoader
     {
         public static VectorTile LoadFromUrl( string url, bool autoStart = true )
-- 
GitLab