-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoxelClass.cs
444 lines (388 loc) · 13.3 KB
/
VoxelClass.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
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
using Godot;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static Godot.HttpRequest;
namespace Art2Voxel
{
public struct ImgStr
{
public Texture2D texture2D;
public String name;
public float rotation;
public Vector2 size;
}
internal static class VoxelClass
{
public static int maxXY = 0;
public static int maxZ = 0;
public static bool inited = false;
static Godot.Color[,,] voxArray;
public static List<ImgStr> listTexture;//+rotation
public static void Init()
{
listTexture = new List<ImgStr>();
}
public static ImgStr CreateImgStr(string filePath)
{
ImgStr imgstr=new ImgStr();
Texture2D icon = ResourceLoader.Load(filePath) as Texture2D;
imgstr.texture2D=icon;
imgstr.size.X = icon.GetWidth();
imgstr.size.Y = icon.GetHeight();
imgstr.name= filePath;
char[] delimiterChars = { '.' };
string[] words = filePath.Split(delimiterChars);
string str1 = words[words.Length - 2];
str1 = str1.Replace(",", ".");
//imgstr.rotation = (float)float.Parse(str1, System.Globalization.CultureInfo.GetCultureInfo("en-US"));
//var d = Convert.ToDecimal("1.2345", new CultureInfo("en-US"));
imgstr.rotation = (float)Convert.ToDouble(str1, new NumberFormatInfo() { NumberDecimalSeparator = "." });
return imgstr;
}
public static Vector2 GetPNGSize(string filePath)
{
//Image image = new Image();
//Texture2D icon = ResourceLoader.Load(filePath) as Texture2D;
ImgStr imgStr = CreateImgStr(filePath);
listTexture.Add(imgStr);
Vector2 result;
result = imgStr.size;
return result;
}
public static void AnalyzeImage(string file)
{
Vector2 pngSize = GetPNGSize(file);
if (pngSize.X > maxXY)
maxXY = (int)pngSize.X;
if (pngSize.Y > maxZ)
maxZ = (int)pngSize.Y;
}
public static void CreateVoxelArray()
{
voxArray = new Godot.Color[maxZ, maxXY, maxXY];
}
public static void FillByImage(int imgIndex, int size)
{
//Image image = new Image();
Texture2D icon = listTexture[imgIndex].texture2D;
Image image = icon.GetImage();
if (size > maxXY)
size = maxXY;
int imgWidth = image.GetWidth();
int imgHeight = image.GetHeight();
int addXY = (maxXY - imgWidth) / 2;
int addZ = (maxZ - imgHeight) / 2;
int addSXY = (maxXY - size) / 2;
//int addSY = (maxZ - size) / 2;
Godot.Color[,] imgCopy = new Godot.Color[maxZ, maxXY];
for (int y = 0; y < imgHeight; y++)
for (int x = 0; x < imgWidth; x++)
{
imgCopy[y + addZ, x + addXY] = image.GetPixel(x, y);
//GD.Print(x + "+" + y);
}
int Xmin = addXY;
if (Xmin < 0) Xmin = 0;
int Xmax = imgWidth + addXY;
if (Xmax > maxXY) Xmax = maxXY;
int Ymin = addSXY;
if (Ymin < 0) Ymin = 0;
int Ymax = size + addSXY;
if (Ymax > maxXY) Ymax = maxXY;
for (int x = Xmin; x < Xmax; x++)
for (int y = Ymin; y < Ymax; y++)
for (int z = 0; z < maxZ; z++)
{
//if (imgCopy[z, x] == new Godot.Color(0.5019608f, 0.5019608f, 0.5019608f, 1))
// imgCopy[z, x].A = 0;
voxArray[z, y, x] = imgCopy[z, x];
//voxArray[x, y, z] = image.GetPixel(x,z);
//voxArray[x, y, z].R = imgData[(x + z * image.GetWidth())*2 + 0] / (float)256;// image.GetPixel(x,z);
//voxArray[x, y, z].G = imgData[(x + z * image.GetWidth()) * 4 + 1] / (float)256;// image.GetPixel(x,z);
//voxArray[x, y, z].B = imgData[(x + z * image.GetWidth()) * 4 + 2] / (float)256;// image.GetPixel(x,z);
//voxArray[x, y, z].A = imgData[(x + z * image.GetWidth()) * 4 + 3] / (float)256;// image.GetPixel(x,z);
}
}
static public void AnalyzeFolder(string directoryPath)
{
//string[] files = Directory.GetFiles(directoryPath);
string[] files = Directory.GetFiles(ProjectSettings.GlobalizePath(directoryPath), "*.png");
foreach (string file in files)
{
AnalyzeImage(file);
}
}
internal static Godot.Color[,] GetImage(double rotation)
{
Godot.Color[,] imgCopy = new Godot.Color[maxZ, maxXY];
while (rotation < 0)
rotation += 360;
while (rotation > 360)
rotation -= 360;
double angle = Math.PI * rotation / 180.0;
double s = Math.Sin(angle);
double c = Math.Cos(angle);
int maxXYZ = maxXY * maxZ;
int zoom = 1;
int minX = 0;
int maxX = maxXY;
int addX = 1;
int minY = 0;
int maxY = maxXY;
int addY = 1;
if (rotation < 180)
{
minX = maxXY-1;
maxX = -1;
addX = -1;
}
if ((rotation > 90) && (rotation < 270))
{
minY = maxXY - 1;
maxY = -1;
addY = -1;
}
double tempAddCenter = ((maxXY - 1) * 0.5 * zoom);
for (int x = minX; x != maxX; x += addX)
for (int y = minY; y != maxY; y += addY)
{
int newX = (int)(((x - tempAddCenter) * c - (y - tempAddCenter) * s) + tempAddCenter + 0.0001);
int newY = (int)(((x - tempAddCenter) * s + (y - tempAddCenter) * c) + tempAddCenter + 0.0001);
if ((newX < 0) || (newY < 0))
continue;
if ((newX > maxXY - 1) || (newY > maxXY - 1))
continue;
//int tempIndex = newX * maxXYZ + newY * maxZ;
for (int z = 0; z < maxZ; z++)
{
if (voxArray[z, newY, newX].A == 1)
{
imgCopy[z, x] = voxArray[z, newY, newX];
}
}
}
return imgCopy;
}
internal static Vector2 GetSize()
{
return new Vector2(maxXY, maxZ);
}
internal static void Pressed(int pressed, Vector2 position, Vector2 scale, Vector2 size, Vector2 mouse)
{
Vector2 pressedPixel = ((mouse - position) / scale) / 3f;
Vector2 pressedPixel2;
pressedPixel2.X = pressedPixel.X;// + (int)((maxXY - size.X)/2);
pressedPixel2.Y = pressedPixel.Y;// + (int)((maxZ - size.Y)/ 2);
GD.Print("Pressed pixel:" + pressedPixel);
int x = (int)pressedPixel2.X;
int z = (int)pressedPixel2.Y;
if (pressed == 2)
{
FindBestAlpha(z,x);
}
else
{
int lastVoxel = -1;
for (int y = 0; y != maxXY; y += 1)
{
if (voxArray[z, y, x].A == 1)
{
lastVoxel = y;
break;
}
}
if (pressed == 0)
{
lastVoxel--;
if (lastVoxel >= 0)
voxArray[z, lastVoxel, x] = voxArray[z, lastVoxel + 1, x];
}
else
{
if (lastVoxel >= 0)
{
if (lastVoxel + 1 < maxXY)
{
if (voxArray[z, lastVoxel + 1, x].A == 0)
voxArray[z, lastVoxel + 1, x] = voxArray[z, lastVoxel, x];
}
voxArray[z, lastVoxel, x] = new Godot.Color(0, 0, 0, 0);
}
}
}
/*
//for (int x = 0; x != maxXY; x += 1)
for (int y = 0; y != maxXY; y += 1)
//for (int z = 0; z < maxZ; z++)
{
voxArray[z, y, x] = new Color(1,0,1,1);
}*/
}
internal static int GetDiffAlphaImg(double rotation, Texture2D texture2D)
{
Godot.Color[,] imgCopy = new Godot.Color[maxZ, maxXY];
while (rotation < 0)
rotation += 360;
while (rotation > 360)
rotation -= 360;
double angle = Math.PI * rotation / 180.0;
double s = Math.Sin(angle);
double c = Math.Cos(angle);
int maxXYZ = maxXY * maxZ;
int zoom = 1;
int minX = 0;
int maxX = maxXY;
int addX = 1;
int minY = 0;
int maxY = maxXY;
int addY = 1;
if (rotation < 180)
{
minX = maxXY - 1;
maxX = -1;
addX = -1;
}
if ((rotation > 90) && (rotation < 270))
{
minY = maxXY - 1;
maxY = -1;
addY = -1;
}
double tempAddCenter = ((maxXY - 1) * 0.5 * zoom);
for (int x = minX; x != maxX; x += addX)
for (int y = minY; y != maxY; y += addY)
{
int newX = (int)(((x - tempAddCenter) * c - (y - tempAddCenter) * s) + tempAddCenter + 0.0001);
int newY = (int)(((x - tempAddCenter) * s + (y - tempAddCenter) * c) + tempAddCenter + 0.0001);
if ((newX < 0) || (newY < 0))
continue;
if ((newX > maxXY - 1) || (newY > maxXY - 1))
continue;
//int tempIndex = newX * maxXYZ + newY * maxZ;
for (int z = 0; z < maxZ; z++)
{
if (voxArray[z, newY, newX].A == 1)
{
imgCopy[z, x] = voxArray[z, newY, newX];
}
}
}
//return imgCopy;
Image image = texture2D.GetImage();
//if (size > maxXY)
//int size = maxXY;
int imgWidth = image.GetWidth();
int imgHeight = image.GetHeight();
int addXY = (maxXY - imgWidth) / 2;
int addZ = (maxZ - imgHeight) / 2;
//int addSXY = (maxXY - size) / 2;
int Xmin = addXY;
if (Xmin < 0) Xmin = 0;
int Xmax = imgWidth + addXY;
if (Xmax > maxXY) Xmax = maxXY;
int Zmin = addZ;
if (Zmin < 0) Zmin = 0;
int Zmax = imgHeight + addZ;
if (Zmax > maxXY) Zmax = maxZ;
int result = 0;
for (int x = Xmin; x < Xmax; x++)
//for (int y = Ymin; y < Ymax; y++)
for (int z = Zmin; z < Zmax; z++)
{
var temp = image.GetPixel(x - Xmin, z - Zmin);
if (imgCopy[z, x].A != image.GetPixel(x - Xmin, z - Zmin).A)
{
result++;
}
}
return result;//fix it
}
internal static int GetDiffAlphaAll()
{
int sumaDiff = 0;
foreach (ImgStr imgStr in listTexture)
{
sumaDiff += GetDiffAlphaImg(imgStr.rotation, imgStr.texture2D);
}
return sumaDiff;
}
private static void FindBestAlpha(int z, int x)
{
for (int y = 0; y < maxXY; y++)
voxArray[z, y, x] = new Godot.Color(voxArray[z, y, x].R, voxArray[z, y, x].G, voxArray[z, y, x].B,1);
int lastScore = GetDiffAlphaAll();
for (int y = 0; y < maxXY; y++)
{
voxArray[z, y, x] = new Godot.Color(voxArray[z, y, x].R, voxArray[z, y, x].G, voxArray[z, y, x].B, 0);
int testScore = GetDiffAlphaAll();
if(testScore>=lastScore)
voxArray[z, y, x] = new Godot.Color(voxArray[z, y, x].R, voxArray[z, y, x].G, voxArray[z, y, x].B, 1);
}
}
/*
.(0st)
123
804
765
↑
765,20 21 22
Y+ X+-
.(45st)
123
804
765
↗
18765
Y+ X-,20 21 12 00 22
.(90st)
123
804
765
→
187
Y+- X-,00 10 20
.(135st)
123
804
765
↘
32187
Y- X-,00 01 10 02 20
.(180st)
123
804
765
↓
321
Y- X+-,02 01 00
.(225st)
123
804
765
↙
54321
Y- X+,02 01 12 00 22
.(270st)
123
804
765
←
543
Y+- X+,22 21 20
.(315st)
123
804
765
↖
76543,22 21 12 02 20
Y+ X+
*/
}
}