-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureSynthesizer.cs
More file actions
251 lines (226 loc) · 10.5 KB
/
TextureSynthesizer.cs
File metadata and controls
251 lines (226 loc) · 10.5 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using PaintDotNet;
using System;
using System.Drawing;
using System.Runtime.CompilerServices;
namespace RedKnack.PainterlyPlugin
{
internal sealed class TextureSynthesizer
{
private readonly TextureType _type;
private readonly int _tileSize;
private readonly int _overlap;
private readonly float _intensity;
private readonly int _seed;
private float[] _heightMap;
private int _cachedW = -1, _cachedH = -1;
public TextureSynthesizer(TextureType type, int tileSize, double intensity, int seed = 42)
{
_type = type;
_tileSize = Math.Clamp(tileSize, 8, 128);
_overlap = Math.Max(2, _tileSize / 4);
_intensity = (float)Math.Clamp(intensity, 0.0, 1.0);
_seed = seed;
}
public float[] GetHeightMap(int width, int height)
{
if (_heightMap is null || _cachedW != width || _cachedH != height)
{
_heightMap = Synthesize(width, height);
_cachedW = width; _cachedH = height;
}
return _heightMap;
}
public void Apply(Surface dst, Rectangle roi, float[] heightMap)
{
if (_intensity <= 0.001f || _type == TextureType.None) return;
int w = dst.Width;
for (int y = roi.Top; y < roi.Bottom; y++)
for (int x = roi.Left; x < roi.Right; x++)
{
if (x < 0 || x >= dst.Width || y < 0 || y >= dst.Height) continue;
float h = heightMap[y * w + x];
float bump = (h - 0.5f) * 2f * _intensity * 0.4f;
ColorBgra c = dst[x, y];
dst[x, y] = ColorBgra.FromBgraClamped(
(int)Math.Clamp(c.B + bump * 255f, 0f, 255f),
(int)Math.Clamp(c.G + bump * 255f, 0f, 255f),
(int)Math.Clamp(c.R + bump * 255f, 0f, 255f),
c.A);
}
}
private float[] Synthesize(int width, int height)
{
float[] exemplar = BuildExemplar();
return QuiltTile(exemplar, width, height);
}
private float[] BuildExemplar() => _type switch
{
TextureType.Canvas => BuildCanvasExemplar(),
TextureType.OilImpasto => BuildImpastoExemplar(),
TextureType.BrushSurface => BuildBrushExemplar(),
_ => BuildCanvasExemplar(),
};
private float[] BuildCanvasExemplar()
{
int s = _tileSize; var ex = new float[s * s]; var rng = new Random(_seed);
for (int y = 0; y < s; y++)
for (int x = 0; x < s; x++)
{
float freq = 2f * MathF.PI / (s * 0.25f);
float warp = MathF.Abs(MathF.Sin(y * freq * 2.1f)) + MathF.Abs(MathF.Sin(x * freq * 2.1f));
float grain = (float)(rng.NextDouble() * 0.06 - 0.03);
ex[y * s + x] = Math.Clamp(warp * 0.6f + 0.2f + grain, 0f, 1f);
}
return ex;
}
private float[] BuildImpastoExemplar()
{
int s = _tileSize; var ex = new float[s * s]; var rng = new Random(_seed);
for (int y = 0; y < s; y++)
for (int x = 0; x < s; x++)
{
float v = 0f;
v += 0.4f * MathF.Sin(x * MathF.PI * 1.3f / s + 0.5f) * MathF.Sin(y * MathF.PI * 1.1f / s);
v += 0.3f * MathF.Sin(x * MathF.PI * 2.7f / s + 1.2f) * MathF.Cos(y * MathF.PI * 2.3f / s);
v += 0.15f * MathF.Sin(x * MathF.PI * 5f / s) * MathF.Sin(y * MathF.PI * 4.5f / s + 0.8f);
v += (float)(rng.NextDouble() * 0.1 - 0.05);
ex[y * s + x] = Math.Clamp(v * 0.5f + 0.5f, 0f, 1f);
}
return ex;
}
private float[] BuildBrushExemplar()
{
int s = _tileSize; var ex = new float[s * s]; var rng = new Random(_seed);
int numBristles = s / 2;
var bristleY = new float[numBristles];
for (int b = 0; b < numBristles; b++) bristleY[b] = (float)(rng.NextDouble() * s);
for (int y = 0; y < s; y++)
for (int x = 0; x < s; x++)
{
float v = 0.1f, phase = x * MathF.PI * 2f / s;
for (int b = 0; b < numBristles; b++)
{
float dy = y - (bristleY[b] + MathF.Sin(phase + b * 0.7f) * s * 0.05f);
v += MathF.Exp(-dy * dy * 0.5f) * 0.6f;
}
v += (float)(rng.NextDouble() * 0.04 - 0.02);
ex[y * s + x] = Math.Clamp(v, 0f, 1f);
}
return ex;
}
private float[] QuiltTile(float[] exemplar, int width, int height)
{
var result = new float[width * height];
int s = _tileSize, step = s - _overlap;
for (int tileY = 0; tileY * step < height + s; tileY++)
for (int tileX = 0; tileX * step < width + s; tileX++)
PlaceTileWithSeam(result, exemplar, tileX * step, tileY * step, width, height);
return result;
}
private void PlaceTileWithSeam(float[] result, float[] exemplar,
int dstX, int dstY, int width, int height)
{
int s = _tileSize, ov = _overlap;
int[] vSeam = ComputeVerticalSeam (result, exemplar, dstX, dstY, ov, s, width, height);
int[] hSeam = ComputeHorizontalSeam(result, exemplar, dstX, dstY, ov, s, width, height);
for (int ty = 0; ty < s; ty++)
for (int tx = 0; tx < s; tx++)
{
int py = dstY + ty, px = dstX + tx;
if (px < 0 || px >= width || py < 0 || py >= height) continue;
bool inV = dstX > 0 && tx < ov;
bool inH = dstY > 0 && ty < ov;
bool useEx;
if (inV && inH) useEx = tx >= vSeam[ty] && ty >= hSeam[tx];
else if (inV) useEx = tx >= vSeam[ty];
else if (inH) useEx = ty >= hSeam[tx];
else useEx = true;
float exVal = exemplar[(ty % s) * s + (tx % s)];
if (useEx)
{
result[py * width + px] = exVal;
}
else
{
float blend = inV
? Math.Clamp((float)(tx - vSeam[ty]) / Math.Max(1, ov - vSeam[ty]), 0f, 1f)
: Math.Clamp((float)(ty - hSeam[tx]) / Math.Max(1, ov - hSeam[tx]), 0f, 1f);
float existing = result[py * width + px];
result[py * width + px] = existing + (exVal - existing) * blend;
}
}
}
private int[] ComputeVerticalSeam(float[] result, float[] exemplar,
int dstX, int dstY, int overlap, int tileH, int width, int height)
{
var seam = new int[tileH];
if (dstX <= 0) return seam;
var cost = new float[tileH * overlap];
for (int ty = 0; ty < tileH; ty++)
for (int tx = 0; tx < overlap; tx++)
{
int py = dstY + ty, px = dstX + tx;
float existing = (py >= 0 && py < height && px >= 0 && px < width)
? result[py * width + px] : 0f;
float diff = existing - exemplar[(ty % _tileSize) * _tileSize + (tx % _tileSize)];
float c = diff * diff;
if (ty == 0) { cost[ty * overlap + tx] = c; }
else
{
float above = cost[(ty-1) * overlap + tx];
float aboveLeft = tx > 0 ? cost[(ty-1) * overlap + tx-1] : float.MaxValue;
float aboveRight = tx < overlap - 1 ? cost[(ty-1) * overlap + tx+1] : float.MaxValue;
cost[ty * overlap + tx] = c + Math.Min(above, Math.Min(aboveLeft, aboveRight));
}
}
int cur = 0; float minC = float.MaxValue;
for (int tx = 0; tx < overlap; tx++)
if (cost[(tileH-1) * overlap + tx] < minC) { minC = cost[(tileH-1) * overlap + tx]; cur = tx; }
seam[tileH - 1] = cur;
for (int ty = tileH - 2; ty >= 0; ty--)
{
int best = cur; float bC = cost[ty * overlap + cur];
if (cur > 0 && cost[ty * overlap + cur-1] < bC) { bC = cost[ty * overlap + cur-1]; best = cur-1; }
if (cur < overlap - 1 && cost[ty * overlap + cur+1] < bC) { best = cur+1; }
cur = best; seam[ty] = cur;
}
return seam;
}
private int[] ComputeHorizontalSeam(float[] result, float[] exemplar,
int dstX, int dstY, int overlap, int tileW, int width, int height)
{
var seam = new int[tileW];
if (dstY <= 0) return seam;
var cost = new float[overlap * tileW];
for (int ty = 0; ty < overlap; ty++)
for (int tx = 0; tx < tileW; tx++)
{
int py = dstY + ty, px = dstX + tx;
float existing = (py >= 0 && py < height && px >= 0 && px < width)
? result[py * width + px] : 0f;
float diff = existing - exemplar[(ty % _tileSize) * _tileSize + (tx % _tileSize)];
float c = diff * diff;
if (tx == 0) { cost[ty * tileW + tx] = c; }
else
{
float left = cost[ty * tileW + tx-1];
float leftAbove = ty > 0 ? cost[(ty-1) * tileW + tx-1] : float.MaxValue;
float leftBelow = ty < overlap - 1 ? cost[(ty+1) * tileW + tx-1] : float.MaxValue;
cost[ty * tileW + tx] = c + Math.Min(left, Math.Min(leftAbove, leftBelow));
}
}
int cur = 0; float minC = float.MaxValue;
for (int ty = 0; ty < overlap; ty++)
if (cost[ty * tileW + tileW-1] < minC) { minC = cost[ty * tileW + tileW-1]; cur = ty; }
seam[tileW - 1] = cur;
for (int tx = tileW - 2; tx >= 0; tx--)
{
int best = cur; float bC = cost[cur * tileW + tx];
if (cur > 0 && cost[(cur-1) * tileW + tx] < bC) { bC = cost[(cur-1) * tileW + tx]; best = cur-1; }
if (cur < overlap - 1 && cost[(cur+1) * tileW + tx] < bC) { best = cur+1; }
cur = best; seam[tx] = cur;
}
return seam;
}
}
}