forked from zfedoran/pixel-sprite-generator
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSprite.hx
467 lines (397 loc) · 9.58 KB
/
Sprite.hx
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package psg;
import openfl.display.BitmapData;
import openfl.display.Bitmap;
class Sprite
{
public var width:Int;
public var height:Int;
public var data:Array<Int>;
public var mask:Mask;
public var isColored:Bool;
public var edgeBrightness:Float;
public var colorVariations:Float;
public var brightnessNoise:Float;
public var saturation:Float;
private var _bitmapData:BitmapData;
public var bitmap:Bitmap;
/**
* The Sprite class makes use of a Mask instance to generate a 2D sprite.
*
* colored : true, // boolean
* edgeBrightness : 0.3, // value from 0 to 1
* colorVariations : 0.2, // value from 0 to 1
* brightnessNoise : 0.3, // value from 0 to 1
* saturation : 0.5 // value from 0 to 1
*
* @class Sprite
* @param {mask}
* @param {options}
* @constructor
*/
public function new( mask_:Mask, ?isColored_:Bool = false,
?edgeBrightness_:Float = 0.3, ?colorVariations_:Float = 0.2,
?brightnessNoise_:Float = 0.3, ?saturation_:Float = 0.5
):Void
{
mask = mask_;
width = mask.width * (mask.mirrorX ? 2 : 1);
height = mask.height * (mask.mirrorY ? 2 : 1);
data = new Array<Int>();
isColored = isColored_;
edgeBrightness = edgeBrightness_;
colorVariations = colorVariations_;
brightnessNoise = brightnessNoise_;
saturation = saturation_;
init();
}
/**
* The init method calls all functions required to generate the sprite.
*/
private function init():Void
{
initBitmapdata();
initData();
applyMask();
generateRandomSample();
if (mask.mirrorX) {
mirrorX();
}
if (mask.mirrorY) {
mirrorY();
}
generateEdges();
renderPixelData();
initBitmap();
}
private function initBitmapdata():Void
{
_bitmapData = new BitmapData(width, height);
};
private function initBitmap():Void
{
bitmap = new Bitmap( _bitmapData, openfl.display.PixelSnapping.ALWAYS, false );
}
/**
* The getData method returns the sprite template data at location (x, y)
*
* -1 = Always border (black)
* 0 = Empty
* 1 = Randomly chosen Empty/Body
* 2 = Randomly chosen Border/Body
*
* @param x X position of pixel
* @param y Y position of pixel
* @return Value of pixel at position
*/
private function getData(x, y):Int
{
return data[y * width + x];
};
/**
* The setData method sets the sprite template data at location (x, y)
*
* -1 = Always border (black)
* 0 = Empty
* 1 = Randomly chosen Empty/Body
* 2 = Randomly chosen Border/Body
*
* @method setData
* @param {x}
* @param {y}
* @param {value}
* @returns {undefined}
*/
private function setData(x, y, value):Void
{
data[y * width + x] = value;
};
/**
* The initData method initializes the sprite data to completely solid.
*
* @method initData
* @returns {undefined}
*/
private function initData():Void
{
var h:Int = height;
var w:Int = width;
var x:Int = 0;
var y:Int = 0;
for (y in 0...h)
{
for (x in 0...w)
{
setData(x, y, -1);
}
}
};
/**
* The mirrorX method mirrors the template data horizontally.
*
* @method mirrorX
* @returns {undefined}
*/
private function mirrorX():Void
{
var h:Int = height;
var w:Int = Math.floor(width/2);
var x:Int = 0;
var y:Int = 0;
for (y in 0...h)
{
for (x in 0...w)
{
setData(width - x - 1, y, getData(x, y));
}
}
};
/**
* The mirrorY method mirrors the template data vertically.
*
* @method
* @returns {undefined}
*/
private function mirrorY():Void
{
var h:Int = Math.floor(height/2);
var w:Int = width;
var x:Int = 0;
var y:Int = 0;
for (y in 0...h)
{
for (x in 0...w)
{
setData(x, height - y - 1, getData(x, y));
}
}
};
/**
* The applyMask method copies the mask data into the template data array at
* location (0, 0).
*
* (note: the mask may be smaller than the template data array)
*
* @method applyMask
* @returns {undefined}
*/
private function applyMask():Void
{
var h:Int = mask.height;
var w:Int = mask.width;
var x:Int = 0;
var y:Int = 0;
for (y in 0...h)
{
for (x in 0...w)
{
setData(x, y, mask.data[y * w + x]);
}
}
};
/**
* Apply a random sample to the sprite template.
*
* If the template contains a 1 (internal body part) at location (x, y), then
* there is a 50% chance it will be turned empty. If there is a 2, then there
* is a 50% chance it will be turned into a body or border.
*
* (feel free to play with this logic for interesting results)
*
* @method generateRandomSample
* @returns {undefined}
*/
private function generateRandomSample():Void
{
var h:Int = height;
var w:Int = width;
var x:Int = 0;
var y:Int = 0;
var val:Int = 0;
for (y in 0...h)
{
for (x in 0...w)
{
val = getData(x, y);
if (val == 1)
{
val = val * Math.round( Math.random() );
}
else if (val == 2)
{
if (Math.random() > 0.5)
{
val = 1;
}
else
{
val = -1;
}
}
setData(x, y, val);
}
}
};
/**
* This method applies edges to any template location that is positive in
* value and is surrounded by empty (0) pixels.
*
* @method generateEdges
* @returns {undefined}
*/
private function generateEdges():Void
{
var h:Int = height;
var w:Int = width;
var x:Int = 0;
var y:Int = 0;
for (y in 0...h)
{
for (x in 0...w)
{
if (getData(x, y) > 0)
{
if (y - 1 >= 0 && getData(x, y-1) == 0)
{
setData(x, y-1, -1);
}
if (y + 1 < height && getData(x, y+1) == 0)
{
setData(x, y+1, -1);
}
if (x - 1 >= 0 && getData(x-1, y) == 0)
{
setData(x-1, y, -1);
}
if (x + 1 < width && getData(x+1, y) == 0)
{
setData(x+1, y, -1);
}
}
}
}
};
/**
* This method renders out the template data to a HTML canvas to finally
* create the sprite.
*
* (note: only template locations with the values of -1 (border) are rendered)
*
* @method renderPixelData
* @returns {undefined}
*/
private function renderPixelData():Void
{
// Prepare all the variables first
var isVerticalGradient:Bool = Math.random() > 0.5;
var saturation:Float = Math.max( Math.min( Math.random() * saturation, 1 ), 0);
var hue:Float = Math.random();
var u:Int = 0;
var v:Int = 0;
var ulen:Int = 0;
var vlen:Int = 0;
var isNewColor:Float = 0;
var val:Int = 0;
var index:Int = 0;
var color:Color = new Color(0,0,0);
var brightness:Float = 0;
// Target XY of BitmapData pixels
var x:Int = 0;
var y:Int = 0;
if (isVerticalGradient)
{
ulen = height;
vlen = width;
}
else
{
ulen = width;
vlen = height;
}
_bitmapData.lock();
for (u in 0...ulen)
{
// Create a non-uniform random number between 0 and 1 (lower numbers more likely)
isNewColor = Math.abs(((Math.random() * 2 - 1)
+ (Math.random() * 2 - 1)
+ (Math.random() * 2 - 1)) / 3);
// Only change the color sometimes (values above 0.8 are less likely than others)
if (isNewColor > (1 - colorVariations))
{
hue = Math.random();
}
for (v in 0...vlen)
{
if (isVerticalGradient)
{
val = getData(v, u);
index = (u * vlen + v) * 4;
x = v;
y = u;
}
else
{
val = getData(u, v);
index = (v * ulen + u) * 4;
x = u;
y = v;
}
color.setRGB(1,1,1);
if (val != 0)
{
if (isColored)
{
// Fade brightness away towards the edges
brightness = Math.sin((u / ulen) * Math.PI) * (1 - brightnessNoise)
+ Math.random() * brightnessNoise;
// Get the RGB color value
color.setHSL(hue, saturation, brightness);
// If this is an edge, then darken the pixel
if (val == -1)
{
color.r *= edgeBrightness;
color.g *= edgeBrightness;
color.b *= edgeBrightness;
}
}
else
{
// Not colored, simply output black
if (val == -1)
{
color.r = 0;
color.g = 0;
color.b = 0;
}
}
}
_bitmapData.setPixel( x, y , color.getRGB() );
}
}
_bitmapData.unlock();
};
/**
* This method converts the template data to a string value for debugging
* purposes.
*
* @method toString
* @returns {undefined}
*/
public function toString():String
{
var h:Int = height;
var w:Int = width;
var x:Int = 0;
var y:Int = 0;
var output:String = "";
for (y in 0...h)
{
for (x in 0...w)
{
var val = getData(x, y);
output += (val >= 0) ? " " + val : "" + val;
}
output += "\n";
}
return output;
};
}