Changeset 391 for binary-improvements2/MapRendering/src/MapRenderer.cs
- Timestamp:
- Aug 7, 2022, 3:02:24 PM (2 years ago)
- Location:
- binary-improvements2/MapRendering/src
- Files:
-
- 1 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MapRendering/src/MapRenderer.cs
r390 r391 11 11 using Object = UnityEngine.Object; 12 12 13 namespace AllocsFixes.MapRendering {14 public class MapRender ing{15 private static MapRender inginstance;13 namespace MapRendering { 14 public class MapRenderer { 15 private static MapRenderer instance; 16 16 17 17 private static readonly object lockObject = new object (); 18 18 public static bool renderingEnabled = true; 19 private readonly MapTileCache cache = new MapTileCache (Constants.M AP_BLOCK_SIZE);19 private readonly MapTileCache cache = new MapTileCache (Constants.MapBlockSize); 20 20 private readonly Dictionary<Vector2i, Color32[]> dirtyChunks = new Dictionary<Vector2i, Color32[]> (); 21 21 private readonly MicroStopwatch msw = new MicroStopwatch (); … … 26 26 private bool shutdown; 27 27 28 private MapRender ing() {29 Constants.M AP_DIRECTORY= GameIO.GetSaveGameDir () + "/map";28 private MapRenderer () { 29 Constants.MapDirectory = GameIO.GetSaveGameDir () + "/map"; 30 30 31 31 lock (lockObject) { … … 35 35 } 36 36 37 cache.SetZoomCount (Constants.Z OOMLEVELS);38 39 zoomLevelBuffers = new MapRenderBlockBuffer[Constants.Z OOMLEVELS];40 for (int i = 0; i < Constants.Z OOMLEVELS; i++) {37 cache.SetZoomCount (Constants.Zoomlevels); 38 39 zoomLevelBuffers = new MapRenderBlockBuffer[Constants.Zoomlevels]; 40 for (int i = 0; i < Constants.Zoomlevels; i++) { 41 41 zoomLevelBuffers [i] = new MapRenderBlockBuffer (i, cache); 42 42 } … … 45 45 } 46 46 47 public static MapRendering Instance { 48 get { 49 if (instance == null) { 50 instance = new MapRendering (); 51 } 52 53 return instance; 54 } 55 } 47 public static MapRenderer Instance => instance ??= new MapRenderer (); 56 48 57 49 public static MapTileCache GetTileCache () { … … 77 69 ThreadPool.UnsafeQueueUserWorkItem (_o => { 78 70 try { 79 if (!Instance.renderingFullMap) { 80 lock (lockObject) { 81 Chunk c = (Chunk) _o; 82 Vector3i cPos = c.GetWorldPos (); 83 Vector2i cPos2 = new Vector2i (cPos.x / Constants.MAP_CHUNK_SIZE, 84 cPos.z / Constants.MAP_CHUNK_SIZE); 85 86 ushort[] mapColors = c.GetMapColors (); 87 if (mapColors != null) { 88 Color32[] realColors = 89 new Color32[Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE]; 90 for (int i_colors = 0; i_colors < mapColors.Length; i_colors++) { 91 realColors [i_colors] = shortColorToColor32 (mapColors [i_colors]); 92 } 93 94 Instance.dirtyChunks [cPos2] = realColors; 95 96 //Log.Out ("Add Dirty: " + cPos2); 97 } 71 if (Instance.renderingFullMap) { 72 return; 73 } 74 75 lock (lockObject) { 76 Chunk c = (Chunk) _o; 77 Vector3i cPos = c.GetWorldPos (); 78 Vector2i cPos2 = new Vector2i (cPos.x / Constants.MapChunkSize, 79 cPos.z / Constants.MapChunkSize); 80 81 ushort[] mapColors = c.GetMapColors (); 82 if (mapColors == null) { 83 return; 98 84 } 85 86 Color32[] realColors = 87 new Color32[Constants.MapChunkSize * Constants.MapChunkSize]; 88 for (int iColors = 0; iColors < mapColors.Length; iColors++) { 89 realColors [iColors] = shortColorToColor32 (mapColors [iColors]); 90 } 91 92 Instance.dirtyChunks [cPos2] = realColors; 93 94 //Log.Out ("Add Dirty: " + cPos2); 99 95 } 100 96 } catch (Exception e) { … … 112 108 Texture2D fullMapTexture = null; 113 109 114 Vector2i minChunk, maxChunk; 115 Vector2i minPos, maxPos; 116 int widthChunks, heightChunks, widthPix, heightPix; 117 getWorldExtent (rfm, out minChunk, out maxChunk, out minPos, out maxPos, out widthChunks, out heightChunks, 118 out widthPix, out heightPix); 119 120 Log.Out (string.Format ( 121 "RenderMap: min: {0}, max: {1}, minPos: {2}, maxPos: {3}, w/h: {4}/{5}, wP/hP: {6}/{7}", 122 minChunk.ToString (), maxChunk.ToString (), 123 minPos.ToString (), maxPos.ToString (), 124 widthChunks, heightChunks, 125 widthPix, heightPix) 110 getWorldExtent (rfm, out Vector2i minChunk, out Vector2i maxChunk, out Vector2i minPos, out Vector2i maxPos, out int widthChunks, out int heightChunks, 111 out int widthPix, out int heightPix); 112 113 Log.Out ( 114 $"RenderMap: min: {minChunk.ToString ()}, max: {maxChunk.ToString ()}, minPos: {minPos.ToString ()}, maxPos: {maxPos.ToString ()}, w/h: {widthChunks}/{heightChunks}, wP/hP: {widthPix}/{heightPix}" 126 115 ); 127 116 128 117 lock (lockObject) { 129 for (int i = 0; i < Constants.Z OOMLEVELS; i++) {118 for (int i = 0; i < Constants.Zoomlevels; i++) { 130 119 zoomLevelBuffers [i].ResetBlock (); 131 120 } 132 121 133 if (Directory.Exists (Constants.M AP_DIRECTORY)) {134 Directory.Delete (Constants.M AP_DIRECTORY, true);122 if (Directory.Exists (Constants.MapDirectory)) { 123 Directory.Delete (Constants.MapDirectory, true); 135 124 } 136 125 … … 143 132 } 144 133 145 Vector2i curFullMapPos = default (Vector2i);146 Vector2i curChunkPos = default (Vector2i);147 for (curFullMapPos.x = 0; curFullMapPos.x < widthPix; curFullMapPos.x += Constants.M AP_CHUNK_SIZE) {134 Vector2i curFullMapPos = default; 135 Vector2i curChunkPos = default; 136 for (curFullMapPos.x = 0; curFullMapPos.x < widthPix; curFullMapPos.x += Constants.MapChunkSize) { 148 137 for (curFullMapPos.y = 0; 149 138 curFullMapPos.y < heightPix; 150 curFullMapPos.y += Constants.M AP_CHUNK_SIZE) {151 curChunkPos.x = curFullMapPos.x / Constants.M AP_CHUNK_SIZE+ minChunk.x;152 curChunkPos.y = curFullMapPos.y / Constants.M AP_CHUNK_SIZE+ minChunk.y;139 curFullMapPos.y += Constants.MapChunkSize) { 140 curChunkPos.x = curFullMapPos.x / Constants.MapChunkSize + minChunk.x; 141 curChunkPos.y = curFullMapPos.y / Constants.MapChunkSize + minChunk.y; 153 142 154 143 try { … … 159 148 if (mapColors != null) { 160 149 Color32[] realColors = 161 new Color32[Constants.M AP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE];162 for (int i _colors = 0; i_colors < mapColors.Length; i_colors++) {163 realColors [i _colors] = shortColorToColor32 (mapColors [i_colors]);150 new Color32[Constants.MapChunkSize * Constants.MapChunkSize]; 151 for (int iColors = 0; iColors < mapColors.Length; iColors++) { 152 realColors [iColors] = shortColorToColor32 (mapColors [iColors]); 164 153 } 165 154 … … 167 156 if (fullMapTexture != null) { 168 157 fullMapTexture.SetPixels32 (curFullMapPos.x, curFullMapPos.y, 169 Constants.M AP_CHUNK_SIZE, Constants.MAP_CHUNK_SIZE, realColors);158 Constants.MapChunkSize, Constants.MapChunkSize, realColors); 170 159 } 171 160 } … … 180 169 } 181 170 182 Log.Out (string.Format ("RenderMap: {0}/{1} ({2}%)", curFullMapPos.x, widthPix, 183 (int) ((float) curFullMapPos.x / widthPix * 100))); 171 Log.Out ($"RenderMap: {curFullMapPos.x}/{widthPix} ({(int)((float)curFullMapPos.x / widthPix * 100)}%)"); 184 172 } 185 173 } … … 189 177 if (fullMapTexture != null) { 190 178 byte[] array = fullMapTexture.EncodeToPNG (); 191 File.WriteAllBytes (Constants.M AP_DIRECTORY+ "/map.png", array);179 File.WriteAllBytes (Constants.MapDirectory + "/map.png", array); 192 180 Object.Destroy (fullMapTexture); 193 181 } … … 200 188 201 189 private void SaveAllBlockMaps () { 202 for (int i = 0; i < Constants.Z OOMLEVELS; i++) {190 for (int i = 0; i < Constants.Zoomlevels; i++) { 203 191 zoomLevelBuffers [i].SaveBlock (); 204 192 } … … 246 234 //Log.Out ("Start Dirty: " + chunkPos); 247 235 248 getBlockNumber (chunkPos, out Vector2i block, out _, Constants.MAP_BLOCK_TO_CHUNK_DIV, Constants.M AP_CHUNK_SIZE);249 250 zoomLevelBuffers [Constants.Z OOMLEVELS- 1].LoadBlock (block);236 getBlockNumber (chunkPos, out Vector2i block, out _, Constants.MAP_BLOCK_TO_CHUNK_DIV, Constants.MapChunkSize); 237 238 zoomLevelBuffers [Constants.Zoomlevels - 1].LoadBlock (block); 251 239 Profiler.EndSample (); 252 240 253 241 Profiler.BeginSample ("RenderDirtyChunks.Work"); 254 242 // Write all chunks that are in the same image tile of the highest zoom level 255 Vector2i v_block, v_blockOffset;256 243 foreach (Vector2i v in chunksToRender) { 257 getBlockNumber (v, out v_block, out v_blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, 258 Constants.MAP_CHUNK_SIZE); 259 if (v_block.Equals (block)) { 260 //Log.Out ("Dirty: " + v + " render: true"); 261 chunksRendered.Add (v); 262 if (dirtyChunks [v].Length != Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE) { 263 Log.Error (string.Format ("Rendering chunk has incorrect data size of {0} instead of {1}", 264 dirtyChunks [v].Length, Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE)); 265 } 266 267 zoomLevelBuffers [Constants.ZOOMLEVELS - 1] 268 .SetPart (v_blockOffset, Constants.MAP_CHUNK_SIZE, dirtyChunks [v]); 269 } 244 getBlockNumber (v, out Vector2i vBlock, out Vector2i vBlockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, 245 Constants.MapChunkSize); 246 if (!vBlock.Equals (block)) { 247 continue; 248 } 249 250 //Log.Out ("Dirty: " + v + " render: true"); 251 chunksRendered.Add (v); 252 if (dirtyChunks [v].Length != Constants.MapChunkSize * Constants.MapChunkSize) { 253 Log.Error ( 254 $"Rendering chunk has incorrect data size of {dirtyChunks [v].Length} instead of {Constants.MapChunkSize * Constants.MapChunkSize}"); 255 } 256 257 zoomLevelBuffers [Constants.Zoomlevels - 1] 258 .SetPart (vBlockOffset, Constants.MapChunkSize, dirtyChunks [v]); 270 259 } 271 260 Profiler.EndSample (); … … 285 274 private void RenderZoomLevel (Vector2i _innerBlock) { 286 275 Profiler.BeginSample ("RenderZoomLevel"); 287 int level = Constants.Z OOMLEVELS- 1;276 int level = Constants.Zoomlevels - 1; 288 277 while (level > 0) { 289 Vector2i block, blockOffset; 290 getBlockNumber (_innerBlock, out block, out blockOffset, 2, Constants.MAP_BLOCK_SIZE / 2); 278 getBlockNumber (_innerBlock, out Vector2i block, out Vector2i blockOffset, 2, Constants.MapBlockSize / 2); 291 279 292 280 zoomLevelBuffers [level - 1].LoadBlock (block); … … 296 284 zoomLevelBuffers [level].FormatSelf == TextureFormat.RGBA32) && 297 285 zoomLevelBuffers [level].FormatSelf == zoomLevelBuffers [level - 1].FormatSelf) { 298 zoomLevelBuffers [level - 1].SetPartNative (blockOffset, Constants.M AP_BLOCK_SIZE/ 2, zoomLevelBuffers [level].GetHalfScaledNative ());286 zoomLevelBuffers [level - 1].SetPartNative (blockOffset, Constants.MapBlockSize / 2, zoomLevelBuffers [level].GetHalfScaledNative ()); 299 287 } else { 300 zoomLevelBuffers [level - 1].SetPart (blockOffset, Constants.M AP_BLOCK_SIZE/ 2, zoomLevelBuffers [level].GetHalfScaled ());288 zoomLevelBuffers [level - 1].SetPart (blockOffset, Constants.MapBlockSize / 2, zoomLevelBuffers [level].GetHalfScaled ()); 301 289 } 302 290 Profiler.EndSample (); … … 310 298 private void getBlockNumber (Vector2i _innerPos, out Vector2i _block, out Vector2i _blockOffset, int _scaleFactor, 311 299 int _offsetSize) { 312 _block = default (Vector2i);313 _blockOffset = default (Vector2i);300 _block = default; 301 _blockOffset = default; 314 302 _block.x = (_innerPos.x + 16777216) / _scaleFactor - 16777216 / _scaleFactor; 315 303 _block.y = (_innerPos.y + 16777216) / _scaleFactor - 16777216 / _scaleFactor; … … 319 307 320 308 private void WriteMapInfo () { 321 J SONObject mapInfo = new JSONObject ();322 mapInfo.Add ("blockSize", new J SONNumber (Constants.MAP_BLOCK_SIZE));323 mapInfo.Add ("maxZoom", new J SONNumber (Constants.ZOOMLEVELS- 1));324 325 Directory.CreateDirectory (Constants.M AP_DIRECTORY);326 File.WriteAllText (Constants.M AP_DIRECTORY+ "/mapinfo.json", mapInfo.ToString (), Encoding.UTF8);309 JsonObject mapInfo = new JsonObject (); 310 mapInfo.Add ("blockSize", new JsonNumber (Constants.MapBlockSize)); 311 mapInfo.Add ("maxZoom", new JsonNumber (Constants.Zoomlevels - 1)); 312 313 Directory.CreateDirectory (Constants.MapDirectory); 314 File.WriteAllText (Constants.MapDirectory + "/mapinfo.json", mapInfo.ToString (), Encoding.UTF8); 327 315 } 328 316 329 317 private bool LoadMapInfo () { 330 if (!File.Exists (Constants.M AP_DIRECTORY+ "/mapinfo.json")) {318 if (!File.Exists (Constants.MapDirectory + "/mapinfo.json")) { 331 319 return false; 332 320 } 333 321 334 string json = File.ReadAllText (Constants.M AP_DIRECTORY+ "/mapinfo.json", Encoding.UTF8);322 string json = File.ReadAllText (Constants.MapDirectory + "/mapinfo.json", Encoding.UTF8); 335 323 try { 336 JSONNode node = Parser.Parse (json); 337 if (node is JSONObject) { 338 JSONObject jo = (JSONObject) node; 324 JsonNode node = Parser.Parse (json); 325 if (node is JsonObject jo) { 339 326 if (jo.ContainsKey ("blockSize")) { 340 Constants.M AP_BLOCK_SIZE = ((JSONNumber) jo ["blockSize"]).GetInt ();327 Constants.MapBlockSize = ((JsonNumber) jo ["blockSize"]).GetInt (); 341 328 } 342 329 343 330 if (jo.ContainsKey ("maxZoom")) { 344 Constants.Z OOMLEVELS = ((JSONNumber) jo ["maxZoom"]).GetInt () + 1;331 Constants.Zoomlevels = ((JsonNumber) jo ["maxZoom"]).GetInt () + 1; 345 332 } 346 333 347 334 return true; 348 335 } 349 } catch (MalformedJ SONException e) {336 } catch (MalformedJsonException e) { 350 337 Log.Out ("Exception in LoadMapInfo: " + e); 351 338 } catch (InvalidCastException e) { … … 360 347 out int _widthChunks, out int _heightChunks, 361 348 out int _widthPix, out int _heightPix) { 362 _minChunk = default (Vector2i);363 _maxChunk = default (Vector2i);364 _minPos = default (Vector2i);365 _maxPos = default (Vector2i);349 _minChunk = default; 350 _maxChunk = default; 351 _minPos = default; 352 _maxPos = default; 366 353 367 354 long[] keys = _rfm.GetAllChunkKeys (); … … 397 384 _maxChunk.y = maxY; 398 385 399 _minPos.x = minX * Constants.M AP_CHUNK_SIZE;400 _minPos.y = minY * Constants.M AP_CHUNK_SIZE;401 402 _maxPos.x = maxX * Constants.M AP_CHUNK_SIZE;403 _maxPos.y = maxY * Constants.M AP_CHUNK_SIZE;386 _minPos.x = minX * Constants.MapChunkSize; 387 _minPos.y = minY * Constants.MapChunkSize; 388 389 _maxPos.x = maxX * Constants.MapChunkSize; 390 _maxPos.y = maxY * Constants.MapChunkSize; 404 391 405 392 _widthChunks = maxX - minX + 1; 406 393 _heightChunks = maxY - minY + 1; 407 394 408 _widthPix = _widthChunks * Constants.M AP_CHUNK_SIZE;409 _heightPix = _heightChunks * Constants.M AP_CHUNK_SIZE;395 _widthPix = _widthChunks * Constants.MapChunkSize; 396 _heightPix = _heightChunks * Constants.MapChunkSize; 410 397 } 411 398
Note:
See TracChangeset
for help on using the changeset viewer.