-
Notifications
You must be signed in to change notification settings - Fork 1
/
Geometrize.cs
327 lines (276 loc) · 13.2 KB
/
Geometrize.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using UnityEditor;
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Tag
{
public string Name { get; set; }
}
public class Geometrize : MonoBehaviour
{
private static readonly HttpClient client = new HttpClient();
[HideInInspector]
public string versionString = "2.2"; // Current version of the software
public async Task<bool> newVersionAvailable()
{
try
{
string url = "https://api.github.com/repos/AlexInABox/mer-mosaic-generator/tags";
if (!client.DefaultRequestHeaders.Contains("User-Agent"))
{
client.DefaultRequestHeaders.Add("User-Agent", "C# console program");
}
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string jsonResponse = await response.Content.ReadAsStringAsync();
var tags = JsonConvert.DeserializeObject<List<Tag>>(jsonResponse);
string latestTag = tags[0].Name;
return CompareVersions(latestTag, versionString);
}
catch (Exception ex)
{
Debug.LogError($"Error fetching latest version tag: {ex.Message}");
return false;
}
}
private bool CompareVersions(string fetchedVersion, string currentVersion)
{
// Removing 'v' prefix commonly used in version tags if present
fetchedVersion = fetchedVersion.TrimStart('v');
currentVersion = currentVersion.TrimStart('v');
// Use System.Version to compare versions correctly
if (Version.TryParse(fetchedVersion, out Version fetched) && Version.TryParse(currentVersion, out Version current))
{
return fetched > current;
}
Debug.LogError("Failed to parse versions for comparison.");
return false;
}
// Input json file
[Header("Input")]
public TextAsset jsonFile;
[Space(10)]
[Header("Settings")]
[Tooltip("1 = one in-game door")]
[Range(0.01f, 1f)]
public float size = 1f;
public bool collidable = false;
[Tooltip("Automatically clear all children before generating a new image")]
public bool autoClear = true;
[HideInInspector]
[Header("Prefabs")]
public GameObject cubePrefab;
[HideInInspector]
public GameObject spherePrefab;
public void convert()
{
cubePrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Resources/Blocks/Primitives/Cube.prefab");
spherePrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Resources/Blocks/Primitives/Sphere.prefab");
if (autoClear) clearChildren();
//This is an example JSON file
/*
{"shapes":
[{"type":1, "data":[0,0,256,180],"color":[227,209,154,255],"score":0.19267},
{"type":8, "data":[126,91,74,72],"color":[211,153,3,128],"score":0.167187},
{"type":32, "data":[16,14,75],"color":[251,255,255,128],"score":0.152495},
{"type":1, "data":[186,0,255,97],"color":[238,255,255,128],"score":0.140048},
{"type":2, "data":[32,74,109,158,3],"color":[189,149,21,128],"score":0.133693},
{"type":16, "data":[135,49,40,51,267],"color":[255,200,0,128],"score":0.126617}
]}
*/
/*
type: 1 = rectangle, 2 = rotated rectangle, 8 = ellipse, 16 = rotated ellipse, 32 = circle
*/
//A rectangle is a cube prefab with a scale of (width, height, 1) and a position of (x, y, 0.01)
//A rotated rectangle is a cube prefab with a scale of (width, height, 1) and a position of (x, y, 0.01) and a rotation of (angle, 0, 0)
//A circle is a sphere prefab with a scale of (radius, radius, radius) and a position of (x, y, 0.01)
//An ellipse is a sphere prefab with a scale of (radius, radius, radius) and a position of (x, y, 0.01) and a rotation of (angle, 0, 0)
//A rotated ellipse is a sphere prefab with a scale of (radius, radius, radius) and a position of (x, y, 0.01) and a rotation of (angle, 0, 0)
//We only care about the type, data, and color
//In Unity these will be represented as GameObjects, Position and Scale, and Color
// Parse the JSON data
var shapeData = JsonConvert.DeserializeObject<ShapeData>(jsonFile.text);
if (shapeData.shapes == null || shapeData.shapes.Count == 0)
{
Debug.Log("No shapes found");
return;
}
var whichShape = 10;
foreach (var shape in shapeData.shapes)
{
// Now you can access shape properties as expected
if (whichShape == 10)
{
createCanvas(shape.data, shape.color);
}
else
{
switch (shape.type)
{
case 32:
createCircle(shape.data, shape.color, whichShape);
break;
case 8:
createEllipse(shape.data, shape.color, whichShape);
break;
case 16:
createRotatedEllipse(shape.data, shape.color, whichShape);
break;
case 1:
createCube(shape.data, shape.color, whichShape);
break;
case 2:
createRotatedCube(shape.data, shape.color, whichShape);
break;
default:
Debug.Log("Shape Type: " + shape.type + " not supported");
break;
}
}
whichShape++;
}
//trigger recompilation of all scripts
UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation();
}
private Transform canvas;
float globalSizeMultiplier; //the value by which to multiply so that the objects fit on the canvases new size
public void createCanvas(List<int> data, List<int> color)
{
//the canvas will have the height of 3. so we need to calculate the width and set the globalSizeMultiplier
globalSizeMultiplier = (size * 3f) / data[3]; //when the height is 300 (pixels) the multiplier will be 0.01
// Create a new cube
GameObject cube = Instantiate(cubePrefab, transform);
//Set parent
cube.transform.parent = transform;
// Set the position
cube.transform.localPosition = new Vector3(data[0], data[1], 0f);
// Set the scale
cube.transform.localScale = new Vector3(data[2] * globalSizeMultiplier, 3, 0.001f);
// Set the color
Color colorVar = new Color(color[0] / 255f, color[1] / 255f, color[2] / 255f);
colorVar.a = color[3] / 255f;
cube.GetComponent<PrimitiveComponent>().Color = colorVar;
cube.GetComponent<PrimitiveComponent>().Collidable = collidable;
canvas = cube.transform;
}
private void createCube(List<int> data, List<int> color, int position)
{
// Create a new cube
GameObject cube = Instantiate(cubePrefab, transform);
// Set the position
//data[0] and data[1] are the x and y coordinates of the top left corner of the rectangle
//data[2] and data[3] are the x and y coordinates of the bottom right corner of the rectangle
float middleOfXCorners = ((data[2] - data[0]) / 2) + data[0];
float middleOfYCorners = ((data[3] - data[1]) / 2) + data[1];
Vector3 vectorToMiddle = new Vector3(middleOfXCorners * globalSizeMultiplier, middleOfYCorners * globalSizeMultiplier, 0);
cube.transform.localPosition = new Vector3(canvas.localScale.x / 2, canvas.localScale.y / 2, 0) - vectorToMiddle + new Vector3(0, 0, 0.0001f * position);
// Set the scale
cube.transform.localScale = new Vector3((data[2] - data[0]) * globalSizeMultiplier, (data[3] - data[1]) * globalSizeMultiplier, 0.00001f);
// Set the color
Color colorVar = new Color(color[0] / 255f, color[1] / 255f, color[2] / 255f);
colorVar.a = color[3] / 255f;
cube.GetComponent<PrimitiveComponent>().Color = colorVar;
cube.GetComponent<PrimitiveComponent>().Collidable = collidable;
}
private void createRotatedCube(List<int> data, List<int> color, int position)
{
// Create a new cube
GameObject cube = Instantiate(cubePrefab, transform);
// Set the position
//data[0] and data[1] are the x and y coordinates of the top left corner of the rectangle
//data[2] and data[3] are the x and y coordinates of the bottom right corner of the rectangle
float middleOfXCorners = ((data[2] - data[0]) / 2) + data[0];
float middleOfYCorners = ((data[3] - data[1]) / 2) + data[1];
Vector3 vectorToMiddle = new Vector3(middleOfXCorners * globalSizeMultiplier, middleOfYCorners * globalSizeMultiplier, 0);
cube.transform.localPosition = new Vector3(canvas.localScale.x / 2, canvas.localScale.y / 2, 0) - vectorToMiddle + new Vector3(0, 0, 0.0001f * position);
// Set the scale
cube.transform.localScale = new Vector3((data[2] - data[0]) * globalSizeMultiplier, (data[3] - data[1]) * globalSizeMultiplier, 0.00001f);
//Set rotation
cube.transform.localRotation = Quaternion.Euler(0, 0, data[4]);
// Set the color
Color colorVar = new Color(color[0] / 255f, color[1] / 255f, color[2] / 255f);
colorVar.a = color[3] / 255f;
cube.GetComponent<PrimitiveComponent>().Color = colorVar;
cube.GetComponent<PrimitiveComponent>().Collidable = collidable;
}
private void createEllipse(List<int> data, List<int> color, int position)
{
// Create a new sphere
GameObject sphere = Instantiate(spherePrefab, transform);
// Set the position
sphere.transform.localPosition = new Vector3(canvas.localScale.x / 2, canvas.localScale.y / 2, 0) - new Vector3(data[0] * globalSizeMultiplier, data[1] * globalSizeMultiplier, -(0.0001f * position));
// Set the scale
sphere.transform.localScale = new Vector3(data[2] * 2 * globalSizeMultiplier, 0.00001f, data[3] * 2 * globalSizeMultiplier);
//Set rotation
sphere.transform.localRotation = Quaternion.Euler(90, 0, 0);
// Set the color
Color colorVar = new Color(color[0] / 255f, color[1] / 255f, color[2] / 255f);
colorVar.a = color[3] / 255f;
sphere.GetComponent<PrimitiveComponent>().Color = colorVar;
sphere.GetComponent<PrimitiveComponent>().Collidable = collidable;
}
private void createRotatedEllipse(List<int> data, List<int> color, int position)
{
// Create a new sphere
GameObject sphere = Instantiate(spherePrefab, transform);
// Set the position
sphere.transform.localPosition = new Vector3(canvas.localScale.x / 2, canvas.localScale.y / 2, 0) - new Vector3(data[0] * globalSizeMultiplier, data[1] * globalSizeMultiplier, -(0.0001f * position));
// Set the scale
sphere.transform.localScale = new Vector3(data[2] * 2 * globalSizeMultiplier, 0.00001f, data[3] * 2 * globalSizeMultiplier);
//Set rotation
sphere.transform.localRotation = Quaternion.Euler(90f - data[4], 90, 90);
// Set the color
Color colorVar = new Color(color[0] / 255f, color[1] / 255f, color[2] / 255f);
colorVar.a = color[3] / 255f;
sphere.GetComponent<PrimitiveComponent>().Color = colorVar;
sphere.GetComponent<PrimitiveComponent>().Collidable = collidable;
}
private void createCircle(List<int> data, List<int> color, int position)
{
// Create a new sphere
GameObject sphere = Instantiate(spherePrefab, transform);
// Set the position
sphere.transform.localPosition = new Vector3(canvas.localScale.x / 2, canvas.localScale.y / 2, 0) - new Vector3(data[0] * globalSizeMultiplier, data[1] * globalSizeMultiplier, -(0.0001f * position));
// Set the scale
sphere.transform.localScale = new Vector3(data[2] * 2 * globalSizeMultiplier, 0.00001f, data[2] * 2 * globalSizeMultiplier);
//Set rotation
sphere.transform.localRotation = Quaternion.Euler(90, 0, 0);
// Set the color
Color colorVar = new Color(color[0] / 255f, color[1] / 255f, color[2] / 255f);
colorVar.a = color[3] / 255f;
sphere.GetComponent<PrimitiveComponent>().Color = colorVar;
sphere.GetComponent<PrimitiveComponent>().Collidable = collidable;
}
public void clearChildren()
{
Transform parentTransform = transform;
// Copy the children into an array
Transform[] children = new Transform[parentTransform.childCount];
for (int i = 0; i < parentTransform.childCount; i++)
{
children[i] = parentTransform.GetChild(i);
}
// Destroy the children from the copied array
foreach (Transform child in children)
{
DestroyImmediate(child.gameObject);
}
}
[System.Serializable]
private class ShapeData
{
public List<Shape> shapes;
}
[System.Serializable]
private struct Shape
{
public int type;
public List<int> data;
public List<int> color;
public float score;
}
}