source: binary-improvements2/MapRendering/src/MapRenderBlockBuffer.cs@ 391

Last change on this file since 391 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 7.4 KB
RevLine 
[224]1using System;
2using System.IO;
[325]3using AllocsFixes.FileCache;
[329]4using Unity.Collections;
[224]5using UnityEngine;
[329]6using UnityEngine.Profiling;
[224]7
[391]8namespace MapRendering {
[325]9 public class MapRenderBlockBuffer {
[391]10 private readonly Texture2D blockMap = new Texture2D (Constants.MapBlockSize, Constants.MapBlockSize, Constants.DefaultTextureFormat, false);
[325]11 private readonly MapTileCache cache;
[331]12 private readonly NativeArray<int> emptyImageData;
[391]13 private readonly Texture2D zoomBuffer = new Texture2D (Constants.MapBlockSize / 2, Constants.MapBlockSize / 2, Constants.DefaultTextureFormat, false);
[325]14 private readonly int zoomLevel;
[329]15 private readonly string folderBase;
16
[391]17 private Vector2i currentBlockMapPos = new Vector2i (int.MinValue, int.MinValue);
[329]18 private string currentBlockMapFolder = string.Empty;
[224]19
[351]20 public MapRenderBlockBuffer (int _level, MapTileCache _cache) {
21 zoomLevel = _level;
22 cache = _cache;
[391]23 folderBase = Constants.MapDirectory + "/" + zoomLevel + "/";
[331]24
25 {
26 // Initialize empty tile data
27 Color nullColor = new Color (0, 0, 0, 0);
[391]28 for (int x = 0; x < Constants.MapBlockSize; x++) {
29 for (int y = 0; y < Constants.MapBlockSize; y++) {
[331]30 blockMap.SetPixel (x, y, nullColor);
31 }
32 }
33
34 NativeArray<int> blockMapData = blockMap.GetRawTextureData<int> ();
35 emptyImageData = new NativeArray<int> (blockMapData.Length, Allocator.Persistent,
36 NativeArrayOptions.UninitializedMemory);
37 blockMapData.CopyTo (emptyImageData);
38 }
[224]39 }
40
[391]41 public TextureFormat FormatSelf => blockMap.format;
[329]42
[325]43 public void ResetBlock () {
[329]44 currentBlockMapFolder = string.Empty;
[391]45 currentBlockMapPos = new Vector2i (int.MinValue, int.MinValue);
[346]46 cache.ResetTile (zoomLevel);
[224]47 }
48
[325]49 public void SaveBlock () {
[331]50 Profiler.BeginSample ("SaveBlock");
[224]51 try {
[331]52 saveTextureToFile ();
[224]53 } catch (Exception e) {
[329]54 Log.Warning ("Exception in MapRenderBlockBuffer.SaveBlock(): " + e);
[224]55 }
[331]56 Profiler.EndSample ();
[224]57 }
58
[351]59 public bool LoadBlock (Vector2i _block) {
[331]60 Profiler.BeginSample ("LoadBlock");
[224]61 lock (blockMap) {
[351]62 if (currentBlockMapPos != _block) {
[329]63 Profiler.BeginSample ("LoadBlock.Strings");
64 string folder;
[351]65 if (currentBlockMapPos.x != _block.x) {
66 folder = folderBase + _block.x + '/';
[329]67
68 Profiler.BeginSample ("LoadBlock.Directory");
69 Directory.CreateDirectory (folder);
70 Profiler.EndSample ();
71 } else {
72 folder = currentBlockMapFolder;
73 }
74
[351]75 string fileName = folder + _block.y + ".png";
[329]76 Profiler.EndSample ();
77
[331]78 SaveBlock ();
79 loadTextureFromFile (fileName);
[329]80
81 currentBlockMapFolder = folder;
[351]82 currentBlockMapPos = _block;
[331]83
84 Profiler.EndSample ();
85 return true;
[224]86 }
87 }
[325]88
[331]89 Profiler.EndSample ();
90 return false;
[224]91 }
92
[351]93 public void SetPart (Vector2i _offset, int _partSize, Color32[] _pixels) {
[391]94 if (_offset.x + _partSize > Constants.MapBlockSize || _offset.y + _partSize > Constants.MapBlockSize) {
95 Log.Error (
96 $"MapBlockBuffer[{zoomLevel}].SetPart ({_offset}, {_partSize}, {_pixels.Length}) has blockMap.size ({Constants.MapBlockSize}/{Constants.MapBlockSize})");
[230]97 return;
98 }
[325]99
[329]100 Profiler.BeginSample ("SetPart");
[351]101 blockMap.SetPixels32 (_offset.x, _offset.y, _partSize, _partSize, _pixels);
[329]102 Profiler.EndSample ();
[224]103 }
104
[325]105 public Color32[] GetHalfScaled () {
[329]106 Profiler.BeginSample ("HalfScaled.ResizeBuffer");
[391]107 zoomBuffer.Resize (Constants.MapBlockSize, Constants.MapBlockSize);
[329]108 Profiler.EndSample ();
[224]109
[329]110 Profiler.BeginSample ("HalfScaled.CopyPixels");
111 if (blockMap.format == zoomBuffer.format) {
112 Profiler.BeginSample ("Native");
113 NativeArray<byte> dataSrc = blockMap.GetRawTextureData<byte> ();
114 NativeArray<byte> dataZoom = zoomBuffer.GetRawTextureData<byte> ();
115 dataSrc.CopyTo (dataZoom);
116 Profiler.EndSample ();
117 } else {
118 Profiler.BeginSample ("GetSetPixels");
119 zoomBuffer.SetPixels32 (blockMap.GetPixels32 ());
120 Profiler.EndSample ();
121 }
122 Profiler.EndSample ();
123
124 Profiler.BeginSample ("HalfScaled.Scale");
[391]125 TextureScale.Point (zoomBuffer, Constants.MapBlockSize / 2, Constants.MapBlockSize / 2);
[329]126 Profiler.EndSample ();
[224]127
[329]128 Profiler.BeginSample ("HalfScaled.Return");
129 Color32[] result = zoomBuffer.GetPixels32 ();
130 Profiler.EndSample ();
131
132 return result;
[224]133 }
134
[351]135 public void SetPartNative (Vector2i _offset, int _partSize, NativeArray<int> _pixels) {
[391]136 if (_offset.x + _partSize > Constants.MapBlockSize || _offset.y + _partSize > Constants.MapBlockSize) {
137 Log.Error (
138 $"MapBlockBuffer[{zoomLevel}].SetPart ({_offset}, {_partSize}, {_pixels.Length}) has blockMap.size ({Constants.MapBlockSize}/{Constants.MapBlockSize})");
[329]139 return;
140 }
141
142 Profiler.BeginSample ("SetPartNative");
143 NativeArray<int> destData = blockMap.GetRawTextureData<int> ();
144
[351]145 for (int y = 0; y < _partSize; y++) {
146 int srcLineStartIdx = _partSize * y;
147 int destLineStartIdx = blockMap.width * (_offset.y + y) + _offset.x;
148 for (int x = 0; x < _partSize; x++) {
149 destData [destLineStartIdx + x] = _pixels [srcLineStartIdx + x];
[329]150 }
151 }
152 Profiler.EndSample ();
153 }
154
155 public NativeArray<int> GetHalfScaledNative () {
156 Profiler.BeginSample ("HalfScaledNative.ResizeBuffer");
[391]157 if (zoomBuffer.format != blockMap.format || zoomBuffer.height != Constants.MapBlockSize / 2 || zoomBuffer.width != Constants.MapBlockSize / 2) {
158 zoomBuffer.Resize (Constants.MapBlockSize / 2, Constants.MapBlockSize / 2, blockMap.format, false);
[329]159 }
160 Profiler.EndSample ();
161
162 Profiler.BeginSample ("HalfScaledNative.Scale");
[331]163 ScaleNative (blockMap, zoomBuffer);
[329]164 Profiler.EndSample ();
165
166 return zoomBuffer.GetRawTextureData<int> ();
167 }
[331]168
169 private static void ScaleNative (Texture2D _sourceTex, Texture2D _targetTex) {
170 NativeArray<int> srcData = _sourceTex.GetRawTextureData<int> ();
171 NativeArray<int> targetData = _targetTex.GetRawTextureData<int> ();
172
173 int oldWidth = _sourceTex.width;
174 int oldHeight = _sourceTex.height;
175 int newWidth = _targetTex.width;
176 int newHeight = _targetTex.height;
177
[391]178 float ratioX = (float) oldWidth / newWidth;
179 float ratioY = (float) oldHeight / newHeight;
[329]180
[391]181 for (int y = 0; y < newHeight; y++) {
182 int oldLineStart = (int) (ratioY * y) * oldWidth;
183 int newLineStart = y * newWidth;
184 for (int x = 0; x < newWidth; x++) {
[331]185 targetData [newLineStart + x] = srcData [(int) (oldLineStart + ratioX * x)];
186 }
187 }
188 }
189
[325]190 private void loadTextureFromFile (string _fileName) {
[329]191 Profiler.BeginSample ("LoadTexture");
192
193 Profiler.BeginSample ("LoadFile");
[224]194 byte[] array = cache.LoadTile (zoomLevel, _fileName);
[329]195 Profiler.EndSample ();
196
197 Profiler.BeginSample ("LoadImage");
[391]198 if (array != null && blockMap.LoadImage (array) && blockMap.height == Constants.MapBlockSize &&
199 blockMap.width == Constants.MapBlockSize) {
[329]200 Profiler.EndSample ();
201
202 Profiler.EndSample ();
[326]203 return;
204 }
[329]205 Profiler.EndSample ();
[233]206
[326]207 if (array != null) {
208 Log.Error ("Map image tile " + _fileName + " has been corrupted, recreating tile");
209 }
[325]210
[391]211 if (blockMap.format != Constants.DefaultTextureFormat || blockMap.height != Constants.MapBlockSize ||
212 blockMap.width != Constants.MapBlockSize) {
213 blockMap.Resize (Constants.MapBlockSize, Constants.MapBlockSize, Constants.DefaultTextureFormat,
[331]214 false);
[326]215 }
216
[331]217 blockMap.LoadRawTextureData (emptyImageData);
218
[329]219 Profiler.EndSample ();
[224]220 }
221
[331]222 private void saveTextureToFile () {
[329]223 Profiler.BeginSample ("EncodePNG");
[224]224 byte[] array = blockMap.EncodeToPNG ();
[329]225 Profiler.EndSample ();
226
[224]227 cache.SaveTile (zoomLevel, array);
228 }
229 }
[325]230}
Note: See TracBrowser for help on using the repository browser.