-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
TextureE.cs
393 lines (346 loc) · 13.6 KB
/
TextureE.cs
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System;
using UnityEngine;
namespace ProceduralToolkit
{
/// <summary>
/// Texture extensions
/// </summary>
public static class TextureE
{
#region DrawLine
/// <summary>
/// Draws a line on the texture
/// </summary>
public static void DrawLine(this Texture2D texture, Vector2Int v0, Vector2Int v1, Color color)
{
DrawLine(texture, v0.x, v0.y, v1.x, v1.y, color);
}
/// <summary>
/// Draws a line on the texture
/// </summary>
public static void DrawLine(this Texture2D texture, int x0, int y0, int x1, int y1, Color color)
{
if (texture == null)
{
throw new ArgumentNullException(nameof(texture));
}
Draw.RasterLine(x0, y0, x1, y1, (x, y) => texture.SetPixel(x, y, color));
}
/// <summary>
/// Draws a line on pixels
/// </summary>
public static void DrawLine(this Color[] pixels, int textureWidth, Vector2Int v0, Vector2Int v1, Color color)
{
DrawLine(pixels, textureWidth, v0.x, v0.y, v1.x, v1.y, color);
}
/// <summary>
/// Draws a line on pixels
/// </summary>
public static void DrawLine(this Color[] pixels, int textureWidth, int x0, int y0, int x1, int y1, Color color)
{
if (pixels == null)
{
throw new ArgumentNullException(nameof(pixels));
}
Draw.RasterLine(x0, y0, x1, y1, (x, y) => pixels[x + y*textureWidth] = color);
}
#endregion DrawLine
#region DrawAALine
/// <summary>
/// Draws an anti-aliased line on the texture
/// </summary>
public static void DrawAALine(this Texture2D texture, Vector2Int v0, Vector2Int v1, Color color)
{
DrawAALine(texture, v0.x, v0.y, v1.x, v1.y, color);
}
/// <summary>
/// Draws an anti-aliased line on the texture
/// </summary>
public static void DrawAALine(this Texture2D texture, int x0, int y0, int x1, int y1, Color color)
{
if (texture == null)
{
throw new ArgumentNullException(nameof(texture));
}
Draw.RasterAALine(x0, y0, x1, y1, (x, y, t) => texture.SetPixel(x, y, Color.Lerp(texture.GetPixel(x, y), color, t)));
}
/// <summary>
/// Draws an anti-aliased line on pixels
/// </summary>
public static void DrawAALine(this Color[] pixels, int textureWidth, Vector2Int v0, Vector2Int v1, Color color)
{
DrawAALine(pixels, textureWidth, v0.x, v0.y, v1.x, v1.y, color);
}
/// <summary>
/// Draws an anti-aliased line on pixels
/// </summary>
public static void DrawAALine(this Color[] pixels, int textureWidth, int x0, int y0, int x1, int y1, Color color)
{
if (pixels == null)
{
throw new ArgumentNullException(nameof(pixels));
}
Draw.RasterAALine(x0, y0, x1, y1, (x, y, t) => pixels[x + y*textureWidth] = Color.Lerp(pixels[x + y*textureWidth], color, t));
}
#endregion DrawAALine
#region DrawCircle
/// <summary>
/// Draws a circle on the texture
/// </summary>
public static void DrawCircle(this Texture2D texture, Vector2Int center, int radius, Color color)
{
DrawCircle(texture, center.x, center.y, radius, color);
}
/// <summary>
/// Draws a circle on the texture
/// </summary>
public static void DrawCircle(this Texture2D texture, int centerX, int centerY, int radius, Color color)
{
if (texture == null)
{
throw new ArgumentNullException(nameof(texture));
}
Draw.RasterCircle(centerX, centerY, radius, (x, y) => texture.SetPixel(x, y, color));
}
/// <summary>
/// Draws a circle on pixels
/// </summary>
public static void DrawCircle(this Color[] pixels, int textureWidth, Vector2Int center, int radius, Color color)
{
DrawCircle(pixels, textureWidth, center.x, center.y, radius, color);
}
/// <summary>
/// Draws a circle on pixels
/// </summary>
public static void DrawCircle(this Color[] pixels, int textureWidth, int centerX, int centerY, int radius, Color color)
{
if (pixels == null)
{
throw new ArgumentNullException(nameof(pixels));
}
Draw.RasterCircle(centerX, centerY, radius, (x, y) => pixels[x + y*textureWidth] = color);
}
#endregion DrawCircle
#region DrawFilledCircle
/// <summary>
/// Draws a filled circle on the texture using Bresenham's algorithm
/// </summary>
public static void DrawFilledCircle(this Texture2D texture, Vector2Int center, int radius, Color color)
{
DrawFilledCircle(texture, center.x, center.y, radius, color);
}
/// <summary>
/// Draws a filled circle on the texture using Bresenham's algorithm
/// </summary>
public static void DrawFilledCircle(this Texture2D texture, int centerX, int centerY, int radius, Color color)
{
if (texture == null)
{
throw new ArgumentNullException(nameof(texture));
}
Draw.RasterFilledCircle(centerX, centerY, radius, (x, y) => texture.SetPixel(x, y, color));
}
/// <summary>
/// Draws a filled circle on pixels using Bresenham's algorithm
/// </summary>
public static void DrawFilledCircle(this Color[] pixels, int textureWidth, Vector2Int center, int radius, Color color)
{
DrawFilledCircle(pixels, textureWidth, center.x, center.y, radius, color);
}
/// <summary>
/// Draws a filled circle on pixels using Bresenham's algorithm
/// </summary>
public static void DrawFilledCircle(this Color[] pixels, int textureWidth, int centerX, int centerY, int radius, Color color)
{
if (pixels == null)
{
throw new ArgumentNullException(nameof(pixels));
}
Draw.RasterFilledCircle(centerX, centerY, radius, (x, y) => pixels[x + y*textureWidth] = color);
}
#endregion DrawFilledCircle
#region DrawRect
/// <summary>
/// Draws a filled rectangle on the texture
/// </summary>
public static void DrawRect(this Texture2D texture, RectInt rect, Color color)
{
DrawRect(texture, rect.x, rect.y, rect.width, rect.height, color);
}
/// <summary>
/// Draws a filled rectangle on the texture
/// </summary>
public static void DrawRect(this Texture2D texture, int x, int y, int blockWidth, int blockHeight, Color color)
{
if (texture == null)
{
throw new ArgumentNullException(nameof(texture));
}
var pixels = new Color[blockWidth*blockHeight];
for (int _y = 0; _y < blockHeight; _y++)
{
for (int _x = 0; _x < blockWidth; _x++)
{
pixels[_x + _y*blockWidth] = color;
}
}
texture.SetPixels(x, y, blockWidth, blockHeight, pixels);
}
/// <summary>
/// Draws a filled rectangle on pixels
/// </summary>
public static void DrawRect(this Color[] pixels, int textureWidth, RectInt rect, Color color)
{
DrawRect(pixels, textureWidth, rect.x, rect.y, rect.width, rect.height, color);
}
/// <summary>
/// Draws a filled rectangle on pixels
/// </summary>
public static void DrawRect(this Color[] pixels, int textureWidth, int x, int y, int blockWidth, int blockHeight, Color color)
{
if (pixels == null)
{
throw new ArgumentNullException(nameof(pixels));
}
for (int _y = y; _y < y + blockHeight; _y++)
{
for (int _x = x; _x < x + blockWidth; _x++)
{
pixels[_x + _y*textureWidth] = color;
}
}
}
#endregion DrawRect
#region DrawGradient
/// <summary>
/// Fills the texture with a gradient
/// </summary>
public static void DrawGradient(this Texture2D texture, Gradient gradient, Directions direction)
{
DrawGradient(texture, 0, 0, texture.width, texture.height, gradient, direction);
}
/// <summary>
/// Draws a gradient rectangle on the texture
/// </summary>
public static void DrawGradient(this Texture2D texture, RectInt rect, Gradient gradient, Directions direction)
{
DrawGradient(texture, rect.x, rect.y, rect.width, rect.height, gradient, direction);
}
/// <summary>
/// Draws a gradient rectangle on the texture
/// </summary>
public static void DrawGradient(this Texture2D texture, int x, int y, int blockWidth, int blockHeight, Gradient gradient,
Directions direction)
{
if (texture == null)
{
throw new ArgumentNullException(nameof(texture));
}
var pixels = new Color[blockWidth*blockHeight];
Gradient(pixels, blockWidth, 0, 0, blockWidth, blockHeight, gradient, direction);
texture.SetPixels(x, y, blockWidth, blockHeight, pixels);
}
/// <summary>
/// Fills pixels with a gradient
/// </summary>
public static void DrawGradient(this Color[] pixels, int textureWidth, int textureHeight, Gradient gradient, Directions direction)
{
DrawGradient(pixels, textureWidth, 0, 0, textureWidth, textureHeight, gradient, direction);
}
/// <summary>
/// Draws a gradient rectangle on pixels
/// </summary>
public static void DrawGradient(this Color[] pixels, int textureWidth, RectInt rect, Gradient gradient, Directions direction)
{
DrawGradient(pixels, textureWidth, rect.x, rect.y, rect.width, rect.height, gradient, direction);
}
/// <summary>
/// Draws a gradient rectangle on pixels
/// </summary>
public static void DrawGradient(this Color[] pixels, int textureWidth, int x, int y, int blockWidth, int blockHeight, Gradient gradient,
Directions direction)
{
if (pixels == null)
{
throw new ArgumentNullException(nameof(pixels));
}
Gradient(pixels, textureWidth, x, y, blockWidth, blockHeight, gradient, direction);
}
private static void Gradient(Color[] pixels, int textureWidth, int x, int y, int blockWidth, int blockHeight, Gradient gradient,
Directions direction)
{
Func<int, int, Color> getColor;
switch (direction)
{
case Directions.Left:
getColor = (_x, _y) => gradient.Evaluate(1 - (float) (_x - x)/(float) blockWidth);
break;
case Directions.Right:
getColor = (_x, _y) => gradient.Evaluate((float) (_x - x)/(float) blockWidth);
break;
case Directions.Down:
getColor = (_x, _y) => gradient.Evaluate(1 - (float) (_y - y)/(float) blockHeight);
break;
case Directions.Up:
getColor = (_x, _y) => gradient.Evaluate((float) (_y - y)/(float) blockHeight);
break;
default:
throw new ArgumentException("Not supported direction: " + direction, nameof(direction));
}
for (int _y = y; _y < y + blockHeight; _y++)
{
for (int _x = x; _x < x + blockWidth; _x++)
{
pixels[_x + _y*textureWidth] = getColor(_x, _y);
}
}
}
#endregion DrawGradient
#region Clear
/// <summary>
/// Fills the texture with white color
/// </summary>
public static void Clear(this Texture2D texture)
{
Clear(texture, Color.white);
}
/// <summary>
/// Fills the texture with specified color
/// </summary>
public static void Clear(this Texture2D texture, Color color)
{
if (texture == null)
{
throw new ArgumentNullException(nameof(texture));
}
var pixels = new Color[texture.width*texture.height];
for (var i = 0; i < pixels.Length; i++)
{
pixels[i] = color;
}
texture.SetPixels(pixels);
}
/// <summary>
/// Fills pixels with white color
/// </summary>
public static void Clear(this Color[] pixels)
{
Clear(pixels, Color.white);
}
/// <summary>
/// Fills pixels with specified color
/// </summary>
public static void Clear(this Color[] pixels, Color color)
{
if (pixels == null)
{
throw new ArgumentNullException(nameof(pixels));
}
for (var i = 0; i < pixels.Length; i++)
{
pixels[i] = color;
}
}
#endregion Clear
}
}