-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathRadar.MemoryInteraction.cs
More file actions
227 lines (202 loc) · 9.87 KB
/
Radar.MemoryInteraction.cs
File metadata and controls
227 lines (202 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.Linq;
using System.Threading.Tasks;
using ExileCore.Shared.Helpers;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Convolution;
using SixLabors.ImageSharp.Processing.Processors.Transforms;
using Configuration = SixLabors.ImageSharp.Configuration;
using Vector4 = System.Numerics.Vector4;
namespace Radar;
public partial class Radar
{
private void GenerateMapTexture()
{
var gridHeightData = _heightData;
var maxX = _areaDimensions.Value.X;
var maxY = _areaDimensions.Value.Y;
var configuration = Configuration.Default.Clone();
configuration.PreferContiguousImageBuffers = true;
using var image = new Image<Rgba32>(configuration, maxX, maxY);
if (Settings.Debug.DrawHeightMap)
{
var minHeight = gridHeightData.Min(x => x.Min());
var maxHeight = gridHeightData.Max(x => x.Max());
image.Mutate(configuration, c => c.ProcessPixelRowsAsVector4((row, i) =>
{
for (var x = 0; x < row.Length - 1; x += 2)
{
var cellData = gridHeightData[i.Y][x];
for (var x_s = 0; x_s < 2; ++x_s)
{
row[x + x_s] = new Vector4(0, (cellData - minHeight) / (maxHeight - minHeight), 0, 1);
}
}
}));
}
else
{
if (Settings.Debug.AlternativeEdgeMethod)
{
static float Clamp(float value, float min, float max) => MathF.Min(MathF.Max(value, min), max);
static Vector4 Lerp(Vector4 a, Vector4 b, float t) => a + t * (b - a);
using var binaryMap = new Image<L8>(configuration, maxX, maxY);
if (!Settings.Debug.DisableHeightAdjust)
Parallel.For(
0, maxY, y =>
{
for (var x = 0; x < maxX; x++)
{
if (_processedTerrainData[y][x] != 1)
continue;
var cellData = gridHeightData[y][x];
var heightOffset = (int)(cellData / GridToWorldMultiplier / 2);
var adjustedX = x - heightOffset;
var adjustedY = y - heightOffset;
if (adjustedX >= 0 && adjustedX < maxX && adjustedY >= 0 && adjustedY < maxY)
binaryMap[adjustedX, adjustedY] = new L8(255);
}
});
else
Parallel.For(
0, maxY, y =>
{
for (var x = 0; x < maxX; x++)
if (_processedTerrainData[y][x] != 1)
binaryMap[x, y] = new L8(255);
});
var blurSigma = Settings.Debug.AlternativeEdgeSettings.OutlineBlurSigma.Value;
binaryMap.Mutate(ctx => ctx.GaussianBlur(blurSigma));
var eps = Settings.Debug.AlternativeEdgeSettings.OutlineTransitionThreshold.Value;
var aaWidth = Settings.Debug.AlternativeEdgeSettings.OutlineFeatherWidth.Value;
Parallel.For(
0, maxY, y =>
{
for (var x = 0; x < maxX; x++)
{
var t = binaryMap[x, y].PackedValue / 255f;
var walkableToEdge = Clamp((t - (eps - aaWidth)) / aaWidth, 0f, 1f);
var edgeToUnwalkable = Clamp((t - (1f - eps)) / aaWidth, 0f, 1f);
var walkableColor = new Vector4(1f, 1f, 1f, 0.00f);
var outlineTarget = Settings.TerrainColor.Value.ToVector4().ToVector4Num();
var color = Lerp(walkableColor, outlineTarget, walkableToEdge);
color = Lerp(color, color with { W = 0f }, edgeToUnwalkable);
color = new Vector4(Clamp(color.X, 0f, 1f), Clamp(color.Y, 0f, 1f), Clamp(color.Z, 0f, 1f), Clamp(color.W, 0f, 1f));
image[x, y] = new Rgba32(color.X, color.Y, color.Z, color.W);
}
});
}
else
{
var unwalkableMask = Vector4.UnitX + Vector4.UnitW;
var walkableMask = Vector4.UnitY + Vector4.UnitW;
if (Settings.Debug.DisableHeightAdjust)
{
Parallel.For(0, maxY, y =>
{
for (var x = 0; x < maxX; x++)
{
var terrainType = _processedTerrainData[y][x];
image[x, y] = new Rgba32(terrainType is 0 ? unwalkableMask : walkableMask);
}
});
}
else
{
Parallel.For(0, maxY, y =>
{
for (var x = 0; x < maxX; x++)
{
var cellData = gridHeightData[y][x / 2 * 2];
//basically, offset x and y by half the offset z would cause when rendering in 3d
var heightOffset = (int)(cellData / GridToWorldMultiplier / 2);
var offsetX = x - heightOffset;
var offsetY = y - heightOffset;
var terrainType = _processedTerrainData[y][x];
if (offsetX >= 0 && offsetX < maxX && offsetY >= 0 && offsetY < maxY)
{
image[offsetX, offsetY] = new Rgba32(terrainType is 0 ? unwalkableMask : walkableMask);
}
}
});
}
if (!Settings.Debug.StandardEdgeSettings.SkipNeighborFill)
{
Parallel.For(0, maxY, y =>
{
for (var x = 0; x < maxX; x++)
{
//this fills in the blanks that are left over from the height projection
if (image[x, y].ToVector4() == Vector4.Zero)
{
var countWalkable = 0;
var countUnwalkable = 0;
for (var xO = -1; xO < 2; xO++)
{
for (var yO = -1; yO < 2; yO++)
{
var xx = x + xO;
var yy = y + yO;
if (xx >= 0 && xx < maxX && yy >= 0 && yy < maxY)
{
var nPixel = image[x + xO, y + yO].ToVector4();
if (nPixel == walkableMask)
countWalkable++;
else if (nPixel == unwalkableMask)
countUnwalkable++;
}
}
}
image[x, y] = new Rgba32(countWalkable > countUnwalkable ? walkableMask : unwalkableMask);
}
}
});
}
if (!Settings.Debug.StandardEdgeSettings.SkipEdgeDetector)
{
var edgeDetector = new EdgeDetectorProcessor(EdgeDetectorKernel.Laplacian5x5, false)
.CreatePixelSpecificProcessor(configuration, image, image.Bounds());
edgeDetector.Execute();
}
if (!Settings.Debug.StandardEdgeSettings.SkipRecoloring)
{
image.Mutate(configuration, c => c.ProcessPixelRowsAsVector4((row, p) =>
{
for (var x = 0; x < row.Length - 0; x++)
{
row[x] = row[x] switch
{
{ X: 1 } => Settings.TerrainColor.Value.ToImguiVec4(),
{ X: 0 } => Vector4.Zero,
var s => s
};
}
}));
}
}
}
if (Math.Max(image.Height, image.Width) > Settings.MaximumMapTextureDimension)
{
var (newWidth, newHeight) = (image.Width, image.Height);
if (image.Height > image.Width)
{
newWidth = newWidth * Settings.MaximumMapTextureDimension / newHeight;
newHeight = Settings.MaximumMapTextureDimension;
}
else
{
newHeight = newHeight * Settings.MaximumMapTextureDimension / newWidth;
newWidth = Settings.MaximumMapTextureDimension;
}
var targetSize = new Size(newWidth, newHeight);
var resizer = new ResizeProcessor(new ResizeOptions { Size = targetSize }, image.Size())
.CreatePixelSpecificCloningProcessor(configuration, image, image.Bounds());
resizer.Execute();
}
//unfortunately the library doesn't respect our allocation settings above
using var imageCopy = image.Clone(configuration);
Graphics.LowLevel.AddOrUpdateTexture(TextureName, imageCopy);
}
}