Skip to content

Commit 7b78449

Browse files
committed
fix perf regressions
1 parent 9501dc3 commit 7b78449

File tree

6 files changed

+221
-124
lines changed

6 files changed

+221
-124
lines changed

Assets/Unity3DTiles/AbstractTilesetBehaviour.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public void LateUpdate()
6060

6161
_lateUpdate();
6262

63+
RequestManager.ForEachQueuedDownload(DerateUnusedTilePriority);
64+
RequestManager.ForEachActiveDownload(DerateUnusedTilePriority);
65+
TileCache.ForEach(DerateUnusedTilePriority);
66+
6367
int maxNewRequests = TileCache.HasMaxSize ? TileCache.MaxSize - TileCache.Count() : -1;
6468
RequestManager.Process(maxNewRequests);
6569

@@ -106,6 +110,23 @@ public void LateUpdate()
106110
UpdateStats();
107111
}
108112

113+
private void DerateUnusedTilePriority(Unity3DTile tile)
114+
{
115+
if (!tile.FrameState.IsUsedThisFrame && !tile.FrameState.UsedLastFrame)
116+
{
117+
if (tile.FrameState.LastVisitedFrame < 0)
118+
{
119+
tile.FrameState.Priority = float.MaxValue;
120+
}
121+
else
122+
{
123+
float maxFrames = 1000;
124+
float framesSinceUsed = Mathf.Min(Time.frameCount - tile.FrameState.LastVisitedFrame, maxFrames);
125+
tile.FrameState.Priority = (1 + (framesSinceUsed / maxFrames));
126+
}
127+
}
128+
}
129+
109130
protected virtual void UpdateStats()
110131
{
111132
//override in subclass

Assets/Unity3DTiles/MultiTilesetBehaviour.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,25 @@ protected override void _lateUpdate()
9090

9191
protected override void UpdateStats()
9292
{
93-
foreach (var tileset in tilesets)
93+
//this works but is very inefficient and drags down framerate significantly when many tilesets are loaded
94+
//foreach (var tileset in tilesets)
95+
//{
96+
// tileset.UpdateStats();
97+
//}
98+
RequestManager.ForEachQueuedDownload(t => { t.Tileset.Statistics.RequestQueueLength++; });
99+
RequestManager.ForEachActiveDownload(t => { t.Tileset.Statistics.ActiveDownloads++; });
100+
foreach (var tile in ProcessingQueue)
94101
{
95-
tileset.UpdateStats();
102+
tile.Tileset.Statistics.ProcessingQueueLength++;
96103
}
104+
TileCache.ForEach(t => {
105+
t.Tileset.Statistics.DownloadedTiles++;
106+
if (t.ContentState == Unity3DTileContentState.READY)
107+
{
108+
t.Tileset.Statistics.ReadyTiles++;
109+
}
110+
});
111+
Unity3DTilesetStatistics.MaxLoadedTiles = TileCache.MaxSize;
97112
Stats = Unity3DTilesetStatistics.Aggregate(tilesets.Select(t => t.Statistics).ToArray());
98113
}
99114

Assets/Unity3DTiles/RequestManager.cs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,6 @@ private set
9090
}
9191
}
9292

93-
public int Count(Func<Unity3DTile, bool> predicate = null)
94-
{
95-
if (predicate == null)
96-
{
97-
return queue.Count;
98-
}
99-
int num = 0;
100-
foreach (var request in queue)
101-
{
102-
if (predicate(request.Tile))
103-
{
104-
num++;
105-
}
106-
}
107-
return num;
108-
}
109-
11093
public IEnumerator<Unity3DTile> GetEnumerator()
11194
{
11295
foreach (var request in queue)
@@ -141,6 +124,23 @@ public RequestManager(Unity3DTilesetSceneOptions sceneOptions)
141124
this.sceneOptions = sceneOptions;
142125
}
143126

127+
public int Count(Func<Unity3DTile, bool> predicate = null)
128+
{
129+
if (predicate == null)
130+
{
131+
return queue.Count;
132+
}
133+
int num = 0;
134+
foreach (var request in queue)
135+
{
136+
if (predicate(request.Tile))
137+
{
138+
num++;
139+
}
140+
}
141+
return num;
142+
}
143+
144144
public int CountActiveDownloads(Func<Unity3DTile, bool> predicate = null)
145145
{
146146
if (predicate == null)
@@ -150,6 +150,22 @@ public int CountActiveDownloads(Func<Unity3DTile, bool> predicate = null)
150150
return activeDownloads.Count(predicate);
151151
}
152152

153+
public void ForEachQueuedDownload(Action<Unity3DTile> action)
154+
{
155+
foreach (var request in queue)
156+
{
157+
action(request.Tile);
158+
}
159+
}
160+
161+
public void ForEachActiveDownload(Action<Unity3DTile> action)
162+
{
163+
foreach (var tile in activeDownloads)
164+
{
165+
action(tile);
166+
}
167+
}
168+
153169
public void EnqueRequest(Request request)
154170
{
155171
request.Tile.ContentState = Unity3DTileContentState.LOADING;

Assets/Unity3DTiles/TileCache.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,6 @@ public class TileCache
3535

3636
private readonly List<Unity3DTile> tmpList = new List<Unity3DTile>();
3737

38-
public int Count(Func<Unity3DTile, bool> predicate = null)
39-
{
40-
if (predicate == null)
41-
{
42-
return list.Count - 1;
43-
}
44-
return list.Count(t => t != null && predicate(t));
45-
}
46-
4738
public bool HasMaxSize
4839
{
4940
get { return sceneOptions.CacheMaxSize > 0; }
@@ -100,9 +91,33 @@ public TileCache(Unity3DTilesetSceneOptions sceneOptions)
10091
list.AddFirst(sentinel);
10192
}
10293

94+
public int Count(Func<Unity3DTile, bool> predicate = null)
95+
{
96+
if (predicate == null)
97+
{
98+
return list.Count - 1;
99+
}
100+
return list.Count(t => t != null && predicate(t));
101+
}
102+
103+
public void ForEach(Action<Unity3DTile> action)
104+
{
105+
foreach (var tile in list)
106+
{
107+
if (tile != null)
108+
{
109+
action(tile);
110+
}
111+
}
112+
}
113+
103114
public bool Add(Unity3DTile tile, out bool duplicate)
104115
{
105116
duplicate = false;
117+
if (tile == null)
118+
{
119+
return false;
120+
}
106121
if (nodeLookup.ContainsKey(tile))
107122
{
108123
duplicate = true;

Assets/Unity3DTiles/Unity3DTile.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,21 @@ public void MarkUsed()
128128
InUsedSet = true;
129129
}
130130

131-
public bool IsUsedThisFrame(int frameCount)
131+
public bool IsUsedThisFrame
132132
{
133-
return InUsedSet && LastVisitedFrame == frameCount;
133+
get
134+
{
135+
return InUsedSet && LastVisitedFrame == Time.frameCount;
136+
}
134137
}
135138

136-
public void Reset(int frameCount)
139+
public void Reset()
137140
{
138-
if(LastVisitedFrame == frameCount)
141+
if (LastVisitedFrame == Time.frameCount)
139142
{
140143
return;
141144
}
142-
LastVisitedFrame = frameCount;
145+
LastVisitedFrame = Time.frameCount;
143146
InUsedSet = false;
144147
InFrustumSet = false;
145148
IsUsedSetLeaf = false;

0 commit comments

Comments
 (0)