-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecoveredGenerateAtom.cs
384 lines (317 loc) · 14.2 KB
/
RecoveredGenerateAtom.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
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.Text;
using System;
public class GenerateAtom : MonoBehaviour
{
[Tooltip("The perovskite crystal lattice will be x units by x units")]
public int dimensions = 1;
[Tooltip("Size of unit cell")]
public int unit = 1;
private int unitTransformed = 1;
private GameObject[] OctahedraArray;
private GameObject[] AatomsArray = new GameObject[8];
private static GameObject[] XatomsArray = new GameObject[7];
[Tooltip("Atom coordinates")]
public Vector3[] XatomCoords = new Vector3[7];
public Vector3[] AatomCoords = new Vector3[8];
//public float testVar = 0.5f;
//public MeshFilter[] mf = new MeshFilter[8];
private Mesh[] meshArray = new Mesh[8];
private Mesh[] meshArrayCopy = new Mesh[8];
private GameObject[] planeArray = new GameObject[8];
//this array is necessary for rendering both sides of the mesh
private GameObject[] planeArrayCopy = new GameObject[8];
//2D array for atom arrangements (will be fed in)
private Vector3[][] meshVerts;
//variable for transparency
private static float alphaLevel = 0.6f;
private Color planeColor = new Color(0, 1, 1, alphaLevel);
[Tooltip("Atom Radius")]
//radius of atoms
public float XatomRadius = 0.1f;
public float AatomRadius = 0.5f;
public float BatomRadius = 0.5f;
[Tooltip("Transform whole octahedra by:")]
public float transformX = 0;
public float transformY = 0;
public float transformZ = 10;
private Vector3 transformOct = new Vector3();
public static GameObject center;
public static GenerateAtom Instance;
//UnityEngine.Object[] prefabArr = new UnityEngine.Object[7];
public int octCounter = 1;
// Use this for initialization
void Awake()
{
Instance = this;
}
void Start()
{
//generate initial Octahedra at origin
generateOctahedra();
/*
//generate Octahedra 2 at 200
transformX = 2;
generateOctahedra();
resetTransforms();
//generate at 002
transformZ = 12;
generateOctahedra();
resetTransforms();
//generate at 202
transformX = 2;
transformZ = 12;
generateOctahedra();
resetTransforms();
//generate at 020
transformY = 2;
transformZ = 10;
generateOctahedra();
resetTransforms();
//generate at 220
transformX = 2;
transformY = 2;
transformZ = 10;
generateOctahedra();
resetTransforms();
//generate at 022
transformY = 2;
transformZ = 12;
generateOctahedra();
resetTransforms();
//generate 222
transformX = 2;
transformY = 2;
transformZ = 12;
generateOctahedra();
resetTransforms();
*/
}
public void extendLattice()
{
//new transforming goes here
unitTransformed = 2 * unit;
//generate Octahedra 2 at 200
transformX += unitTransformed;
generateOctahedra();
resetTransforms();
//generate at 002
//transformZ = 12;
transformZ += unitTransformed;
generateOctahedra();
resetTransforms();
//generate at 202
transformX = 2;
transformZ = 12;
generateOctahedra();
resetTransforms();
//generate at 020
transformY = 2;
transformZ = 10;
generateOctahedra();
resetTransforms();
//generate at 220
transformX = 2;
transformY = 2;
transformZ = 10;
generateOctahedra();
resetTransforms();
//generate at 022
transformY = 2;
transformZ = 12;
generateOctahedra();
resetTransforms();
//generate 222
transformX = 2;
transformY = 2;
transformZ = 12;
generateOctahedra();
resetTransforms();
}
public void resetTransforms()
{
transformX = 0;
transformY = 0;
transformZ = 0;
}
//this generates one perovskite unit cell (rename this later
public void generateOctahedra()
{
for (int m = 0; m < dimensions*dimensions; m++)
{
OctahedraArray = new GameObject[dimensions*dimensions];
OctahedraArray[m] = new GameObject("Octahedra " + octCounter);
//EVENTUALLY REPLACE THESE WITH A UNIT VARIABLE?!?!
//hardcode X atom coords
XatomCoords[0] = new Vector3(0, -unit, 0);
XatomCoords[1] = new Vector3(0, unit, 0);
XatomCoords[2] = new Vector3(unit, 0, 0);
XatomCoords[3] = new Vector3(0, 0, unit);
XatomCoords[4] = new Vector3(0, 0, 0);
XatomCoords[5] = new Vector3(-unit, 0, 0);
XatomCoords[6] = new Vector3(0, 0, -unit);
//hardcode A atom coords
AatomCoords[0] = new Vector3(unit, -unit, unit);
AatomCoords[1] = new Vector3(unit, -unit, -unit);
AatomCoords[2] = new Vector3(-unit, -unit, -unit);
AatomCoords[3] = new Vector3(-unit, unit, -unit);
AatomCoords[4] = new Vector3(unit, unit, unit);
AatomCoords[5] = new Vector3(unit, unit, -unit);
AatomCoords[6] = new Vector3(-unit, unit, unit);
AatomCoords[7] = new Vector3(-unit, -unit, unit);
transformOct = new Vector3(transformX, transformY, transformZ);
//loop for creating atoms and assigning respective coordinates
for (int n = 0; n < AatomsArray.Length; n++)
{
if (n < XatomsArray.Length)
{
XatomsArray[n] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//apply change to XatomCoords if there is an input
XatomCoords[n] = XatomCoords[n] + transformOct;
XatomsArray[n].transform.position = XatomCoords[n];
//change color (of all atoms in the array D:)
XatomsArray[n].GetComponent<Renderer>().material.color = Color.green;
//Makes the current gameobject the parent of the atoms
//atomsArray[n].transform.parent = gameObject.transform;
XatomsArray[n].transform.parent = OctahedraArray[m].transform;
//change radius of sphere
XatomsArray[n].transform.localScale = new Vector3(XatomRadius, XatomRadius, XatomRadius);
//apply Interactible.cs script from Hololens ModelExplorer Demo
XatomsArray[n].AddComponent(Type.GetType("Interactible"));
//Debug.Log("asdf");
/*
//create empty prefab to prepare prefab asset
prefabArr[n] = PrefabUtility.CreatePrefab("Assets/prefabtestfolder/Whatever " + n + ".prefab", XatomsArray[n], ReplacePrefabOptions.ConnectToPrefab);
prefabArr[n].hideFlags = HideFlags.HideInHierarchy;*/
}
AatomsArray[n] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//apply change to AatomCoords if there is an input
AatomCoords[n] = AatomCoords[n] + transformOct;
AatomsArray[n].transform.position = AatomCoords[n];
//change color (of all atoms in the array D:)
AatomsArray[n].GetComponent<Renderer>().material.color = Color.white;
//Makes the current gameobject the parent of the atoms
//AatomsArray[n].transform.parent = Octahedra.transform;
AatomsArray[n].transform.parent = gameObject.transform;
//change radius of sphere
AatomsArray[n].transform.localScale = new Vector3(AatomRadius, AatomRadius, AatomRadius);
//apply Interactible.cs script from Hololens ModelExplorer Demo
AatomsArray[n].AddComponent(Type.GetType("Interactible"));
//Debug.Log(n);
}
//radius of center
XatomsArray[4].transform.localScale = new Vector3(BatomRadius, BatomRadius, BatomRadius);
/*
prefabArr[0] = PrefabUtility.CreatePrefab("Assets/prefabtestfolder/Whatever " + 1 + ".prefab", XatomsArray[0], ReplacePrefabOptions.ConnectToPrefab);
prefabArr[1] = PrefabUtility.CreatePrefab("Assets/prefabtestfolder/Whatever " + 2 + ".prefab", XatomsArray[1], ReplacePrefabOptions.ConnectToPrefab);*/
center = XatomsArray[4];
meshVerts = new Vector3[][] {
new Vector3[] { XatomCoords[0], XatomCoords[3], XatomCoords[2] },
new Vector3[] { XatomCoords[6], XatomCoords[2], XatomCoords[1] },
new Vector3[] { XatomCoords[5], XatomCoords[1], XatomCoords[6] },
new Vector3[] { XatomCoords[0], XatomCoords[5], XatomCoords[6] },
new Vector3[] { XatomCoords[0], XatomCoords[6], XatomCoords[2] },
new Vector3[] { XatomCoords[5], XatomCoords[3], XatomCoords[1] },
new Vector3[] { XatomCoords[2], XatomCoords[1], XatomCoords[3] },
new Vector3[] { XatomCoords[6], XatomCoords[5], XatomCoords[3] }
};
//loop for instantiating new plane array objects
for (int j = 0; j < meshArray.Length; j++)
{
planeArray[j] = new GameObject("plane " + (j + 1));
planeArrayCopy[j] = new GameObject("plane copy" + (j + 1));
planeArray[j].AddComponent<MeshFilter>();
planeArrayCopy[j].AddComponent<MeshFilter>();
planeArray[j].AddComponent<MeshRenderer>();
planeArrayCopy[j].AddComponent<MeshRenderer>();
meshArray[j] = new Mesh();
meshArrayCopy[j] = new Mesh();
meshArray[j] = planeArray[j].GetComponent<MeshFilter>().mesh;
meshArrayCopy[j] = planeArrayCopy[j].GetComponent<MeshFilter>().mesh;
meshArray[j].Clear();
meshArrayCopy[j].Clear();
meshArray[j].vertices = meshVerts[j];
meshArrayCopy[j].vertices = meshVerts[j];
meshArray[j].uv = new Vector2[] { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1) };
meshArrayCopy[j].uv = new Vector2[] { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1) };
meshArray[j].triangles = new int[] { 0, 2, 1 };
meshArrayCopy[j].triangles = swapTriIndices(meshArray[j].triangles);
//set plane color
planeArray[j].GetComponent<Renderer>().material.color = planeColor;
planeArrayCopy[j].GetComponent<Renderer>().material.color = planeColor;
//access rendering mode and enable transparency
planeArray[j].GetComponent<Renderer>().material.SetFloat("_Mode", 3);
planeArray[j].GetComponent<Renderer>().material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
planeArray[j].GetComponent<Renderer>().material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
planeArray[j].GetComponent<Renderer>().material.SetInt("_ZWrite", 0);
planeArray[j].GetComponent<Renderer>().material.DisableKeyword("_ALPHATEST_ON");
planeArray[j].GetComponent<Renderer>().material.EnableKeyword("_ALPHABLEND_ON");
planeArray[j].GetComponent<Renderer>().material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
planeArray[j].GetComponent<Renderer>().material.renderQueue = 3000;
planeArrayCopy[j].GetComponent<Renderer>().material.SetFloat("_Mode", 3);
planeArrayCopy[j].GetComponent<Renderer>().material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
planeArrayCopy[j].GetComponent<Renderer>().material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
planeArrayCopy[j].GetComponent<Renderer>().material.SetInt("_ZWrite", 0);
planeArrayCopy[j].GetComponent<Renderer>().material.DisableKeyword("_ALPHATEST_ON");
planeArrayCopy[j].GetComponent<Renderer>().material.EnableKeyword("_ALPHABLEND_ON");
planeArrayCopy[j].GetComponent<Renderer>().material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
planeArrayCopy[j].GetComponent<Renderer>().material.renderQueue = 3000;
//Makes the current gameobject the parent of the planes
planeArray[j].transform.parent = OctahedraArray[m].transform;
planeArrayCopy[j].transform.parent = OctahedraArray[m].transform;
}
//increment octahedra counter to update the name of gameobject
octCounter++;
//make octahedra child of current gameobject (Perovskite)
OctahedraArray[m].transform.parent = gameObject.transform;
}
}
// Update is called once per frame
void Update()
{
rotateObject();
}
public void rotateObject()
{
//make sure coordinates are correct
//OctahedraArray[0].transform.RotateAround(new Vector3(0, 0, 10), Vector3.up, 20 * Time.deltaTime);
}
//vector3[] toString method
public static string SerializeVector3Array(Vector3[] aVectors)
{
StringBuilder sb = new StringBuilder();
foreach (Vector3 v in aVectors)
{
sb.Append(v.x).Append(" ").Append(v.y).Append(" ").Append(v.z).Append("|");
}
if (sb.Length > 0) // remove last "|"
sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}
//toString method for debugging
public static string toString(int[] a)
{
String str = "";
for (var i = 0; i < a.Length; i++)
{
str = str + a[i].ToString();
}
return str;
}
//assumes int array only contains 3 points (because triangle)
public int[] swapTriIndices(int[] a)
{
int temp = 0;
a[0] = temp;
a[0] = a[1];
a[1] = temp;
return a;
}
public GameObject[] getGameObjectArr()
{
return XatomsArray;
}
}