-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomize.cs
More file actions
510 lines (469 loc) · 14.5 KB
/
Randomize.cs
File metadata and controls
510 lines (469 loc) · 14.5 KB
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Randomize : MonoBehaviour {
public UIButton ResetButton;
//public int numberToSpawn = 100;
public int maxBoundsOfRandomization = 100;
//public Texture textureToApplyForTest;
//public Shader shaderToApplyForTest;
//public Material[] materialToApplyForTest;
public Transform centerOfRandomization;
public int rockCount;
public GameObject[] rocks;
public int treeCount;
public GameObject[] trees;
public int vegationCount;
public GameObject[] vegetation;
public int monsterCount;
public GameObject[] monsters;
List<GameObject> allThingsCreated;
// Use this for initialization
void Start ()
{
ResetButton.disabledColor = Color.black;
ResetButton.isEnabled = false;
allThingsCreated = new List<GameObject>();
StartCoroutine(SpacedProcess());
//allThingsCreated = new GameObject[ vegationCount + rockCount+ treeCount + monsterCount];
}
void Reset()
{
Destroy(allVeg);
Destroy(allRocks);
Destroy(allTrees);
//StartCoroutine(WaitBeforeContinuing());
StartCoroutine(SpacedProcess());
ResetButton.isEnabled = false;
}
Vector3 GetARandomPosition()
{
Vector3 randomizedPos;
randomizedPos = new Vector3(Random.Range(centerOfRandomization.position.x, maxBoundsOfRandomization), //x
centerOfRandomization.position.y, //y
Random.Range(centerOfRandomization.position.z, maxBoundsOfRandomization));
return randomizedPos;
}
GameObject thing;
// void CreateRandomObjects(GameObject[] things, int numTimes)
// {
// if(things.GetLength(0) > 0)
// for(int i = 0; i <= numTimes; i++)
// {
//
// thing = GameObject.Instantiate(things[Random.Range(0,things.GetLength(0))]) as GameObject;
// // Debug.Log(allThingsCreated.Count);
// //thing.AddComponent<SphereObject>();
// thing.AddComponent<RandomlyCreatedObject>();
// thing.isStatic = true;
// thing.transform.parent = transform;
// //thing.AddComponent<NavMeshObstacle>();
// thing.AddComponent<Rigidbody>();
// //Sphere1.renderer.material.mainTexture = textureToApplyForTest;
// //if(i % 2 == 0)
// //thing.renderer.material = materialToApplyForTest[0];
// //else thing.renderer.material = materialToApplyForTest[1];
// Vector3 random1 = GetARandomPosition();
//
// //Check if on navmesh, if not, randomize again
//
// thing.GetComponent<RandomlyCreatedObject>().creator = this;
// thing.transform.position = random1;
//
//
//
// }
// }
void CreateRandomObject(GameObject original, bool randomRotation, bool oneAxis, GameObject parent)
{
thing = GameObject.Instantiate(original) as GameObject;
Transform t = thing.transform;
thing.AddComponent<RandomlyCreatedObject>();
thing.isStatic = true;
t.parent = parent.transform;
thing.AddComponent<Rigidbody>();
Vector3 random1 = GetARandomPosition();
thing.GetComponent<RandomlyCreatedObject>().creator = this;
t.position = random1;
if(randomRotation && !oneAxis)
{
Quaternion randomRot = Random.rotation;
t.rotation = randomRot;
}else
{
Quaternion randomRot = Random.rotation;
// randomRot.w = 0;
randomRot.x = 0;
randomRot.z = 0;
t.rotation = randomRot;
}
allThingsCreated.Add(thing);
}
// void CreateRandomObjects(GameObject[] things, int numTimes, bool randomRotation, bool oneAxis, GameObject parent)
// {
// if(things.GetLength(0) > 0)
// for(int i = 0; i <= numTimes; i++)
// {
//
// thing = GameObject.Instantiate(things[Random.Range(0,things.GetLength(0))]) as GameObject;
// Transform t = thing.transform;
//
// thing.AddComponent<RandomlyCreatedObject>();
// thing.isStatic = true;
// t.parent = parent.transform;
//
// thing.AddComponent<Rigidbody>();
//
// Vector3 random1 = GetARandomPosition();
//
// thing.GetComponent<RandomlyCreatedObject>().creator = this;
// t.position = random1;
//
//
// if(randomRotation && !oneAxis)
// {
// Quaternion randomRot = Random.rotation;
// t.rotation = randomRot;
// }else
// {
// Quaternion randomRot = Random.rotation;
// // randomRot.w = 0;
// randomRot.x = 0;
// randomRot.z = 0;
// t.rotation = randomRot;
// }
//
// allThingsCreated.Add(thing);
//
// }
// }
//Called by randomized object if in a bad spot
public void ReRandomize(GameObject go)
{
go.transform.position = GetARandomPosition();
//Debug.Log("Randomized " + go + " to : " + go.transform.position);
}
GameObject allRocks;
GameObject allTrees;
GameObject allVeg;
// IEnumerator WaitBeforeContinuing()
// {
// allRocks = new GameObject();
// allRocks.name = "AllRocks";
// CreateRandomObjects(rocks, rockCount, true, false, allRocks);
// yield return new WaitForSeconds(1);
// Debug.Log(allThingsCreated.Count + " rocks created");
// allTrees = new GameObject();
// allTrees.name = "AllTrees";
// CreateRandomObjects(trees, treeCount, true, true , allTrees);
// yield return new WaitForSeconds(.5f);
// Debug.Log(allThingsCreated.Count + " trees created");
// allVeg = new GameObject();
// allVeg.name = "AllVeg";
// CreateRandomObjects(vegetation, vegationCount, false, false, allVeg);
// yield return new WaitForSeconds(.5f);
// Debug.Log(allThingsCreated.Count + " veg created");
//
// yield return new WaitForSeconds(4);
// foreach(GameObject go in allThingsCreated)
// {
//
// //if(go.GetComponent<Rigidbody>().IsSleeping() == false)
// if(go != null)
// {
// if(!go.GetComponent<RandomlyCreatedObject>().grounded )
// {
// //Debug.Log(go.name + " is not sleeping");
// Destroy(go);
//
// }
// else
// {
// go.isStatic = true;
// //Debug.Log(go.name + " is going static");
// }
// }
// }
// yield return new WaitForSeconds(1);
// //Debug.Log(allThingsCreated.Count + " all created");
// allRocks.AddComponent<CombineChildren>();
// allTrees.AddComponent<CombineChildren>();
// allVeg.AddComponent<CombineChildren>();
//
// allThingsCreated.Clear();
//
// ResetButton.isEnabled = true;
// }
//coroutine heavy process of controlling what gets randomly generated, when.
IEnumerator SpacedProcess()
{
allRocks = new GameObject();
allRocks.name = "AllRocks";
int rocksCreatedSoFar = 0;
int rocksPerGroup = 2;
int numberOfRockGroups = rockCount/2;
while(numberOfRockGroups > 20)
{
rocksPerGroup+=2;
numberOfRockGroups = rockCount/rocksPerGroup;
}
Debug.Log("Going to make : " + numberOfRockGroups + " groups");
for (int a = 0; a < numberOfRockGroups; a++) //groups 0 - 5
{
for(int i = 0; i < rocksPerGroup; i++) // 0 - 19
{
Debug.Log("Make rocks from group : " + a + " and item number is : " + i);
//CreateRandomObjects(rocks, rockCount, true, false, allRocks);
CreateRandomObject(rocks[ Random.Range(0,rocks.Length) ], true, false, allRocks);
rocksCreatedSoFar++;
}
yield return new WaitForEndOfFrame();
Debug.Log("processed so far, this message once per group : " + rocksCreatedSoFar);
}
Debug.Log("Total processed in all groups : " + rocksCreatedSoFar);
yield return new WaitForEndOfFrame();
//Trees
allTrees = new GameObject();
allTrees.name = "AllTrees";
int treesCreatedSoFar = 0;
int treesPerGroup = 2;
int numberOfTreeGroups = treeCount/2;
while(numberOfTreeGroups > 20)
{
treesPerGroup+=2;
numberOfTreeGroups = treeCount / treesPerGroup;
}
Debug.Log("Going to make : " + numberOfTreeGroups + " groups");
for (int a = 0; a < numberOfTreeGroups; a++) //groups 0 - 5
{
for(int i = 0; i < treesPerGroup; i++) // 0 - 19
{
Debug.Log("Make trees from group : " + a + " and item number is : " + i);
//CreateRandomObjects(rocks, rockCount, true, false, allRocks);
CreateRandomObject(trees[ Random.Range(0,trees.Length) ], true, true, allTrees);
treesCreatedSoFar++;
}
yield return new WaitForEndOfFrame();
Debug.Log("processed so far, this message once per group : " + treesCreatedSoFar);
}
Debug.Log("Total processed in all groups : " + treesCreatedSoFar);
yield return new WaitForEndOfFrame();
//Vegetation
allVeg = new GameObject();
allVeg.name = "AllVeg";
int vegCreatedSoFar = 0;
int vegPerGroup = 0;
int numberOfVegGroups = vegationCount/2;
while(numberOfVegGroups > 20)
{
vegPerGroup+= 2;
numberOfVegGroups = vegationCount / vegPerGroup;
}
Debug.Log("Going to make : " + numberOfVegGroups + " groups");
for (int a = 0; a < numberOfVegGroups; a++) //groups 0 - 5
{
for(int i = 0; i < vegPerGroup; i++) // 0 - 19
{
Debug.Log("Make vegetation from group : " + a + " and item number is : " + i);
//CreateRandomObjects(rocks, rockCount, true, false, allRocks);
CreateRandomObject(vegetation[ Random.Range(0,vegetation.Length) ], true, true, allVeg);
vegCreatedSoFar++;
}
yield return new WaitForEndOfFrame();
Debug.Log("processed so far, this message once per group : " + vegCreatedSoFar);
}
Debug.Log("Total processed in all groups : " + vegCreatedSoFar);
yield return new WaitForSeconds(4);
foreach(GameObject go in allThingsCreated)
{
//if(go.GetComponent<Rigidbody>().IsSleeping() == false)
if(go != null)
{
if(!go.GetComponent<RandomlyCreatedObject>().grounded )
{
//Debug.Log(go.name + " is not sleeping");
Destroy(go);
}
else
{
go.isStatic = true;
//Debug.Log(go.name + " is going static");
}
}
}
yield return new WaitForSeconds(1);
//Debug.Log(allThingsCreated.Count + " all created");
// allRocks.AddComponent<CombineChildren>();
// allTrees.AddComponent<CombineChildren>();
// allVeg.AddComponent<CombineChildren>();
// SplitAllThingsIntoGroupsAndAddCombineComponent();
SplitBasedOnVectorQuantity(allRocks);
SplitBasedOnVectorQuantity(allTrees);
SplitBasedOnVectorQuantity(allVeg);
ResetButton.isEnabled = true;
}
// bool SplitAgain(GameObject go)
// {
// int vertexCount = 0;
// foreach(transform t in go.transform)
// {
// //for if mesh is top of hierarchy
// if( t.gameObject.GetComponent<MeshFilter>())
// {
// vertexCount += t.gameObject.GetComponent<MeshFilter>().mesh.vertexCount;
// }else
// {
// foreach(MeshFilter m in t.gameObject.GetComponentsInChildren<MeshFilter>())
// {
// vertexCount += t.gameObject.GetComponent<MeshFilter>().mesh.vertexCount;
// }
// }
// if(vertexCount >= 64999)
// {
//
// Debug.Log("Vertex count is over.. " + vertexCount);
//
// }
// }
//
// return false;
// }
void SplitBasedOnVectorQuantity(GameObject go)
{
int maxVertCount = 64800;
int vertexCountForGroup = 0;
bool thisOneIsUnder = true;
int timesGoneOver = 0;
int vertexCount = 0;
GameObject go2 = new GameObject();
List<GameObject> Combinations = new List<GameObject>();
foreach(Transform t in go.transform)
{
if(t.gameObject.GetComponent<MeshFilter>())
{
vertexCount += t.gameObject.GetComponent<MeshFilter>().mesh.vertexCount;
}
foreach(MeshFilter m in t.gameObject.GetComponentsInChildren<MeshFilter>())
{
vertexCount += m.mesh.vertexCount;
}
if(vertexCount >= maxVertCount)
{
maxVertCount+= maxVertCount;
Destroy(go2);
go2 = new GameObject();
go2.name = go.name+timesGoneOver;
Debug.Log(go2.name + " is the new CombineHolder, and the new max vert count is : " + maxVertCount);
thisOneIsUnder = false;
Combinations.Add(go2);
timesGoneOver++;
}
if(!thisOneIsUnder)
{
Debug.Log("Vertex count : " + vertexCount);
t.parent = go2.transform;
}//else, do nothing - keep it under the original prefab
}
//go2.AddComponent<CombineChildren>();
go.AddComponent<CombineChildren>();
foreach(GameObject p in Combinations)
{
if(p)
{
Debug.Log("Adding Combine to : " + p.name);
p.AddComponent<CombineChildren>();
}
}
}
// //Solves the problem of combining 65,000 meshes
// void SplitAllThingsIntoGroupsAndAddCombineComponent()
// {
// Debug.Log("Splitting things by UV");
// GameObject[] CombineGroups = new GameObject[1];
// GameObject CombinationHeirarchy = new GameObject();
//
// //Make sure all items have unique names
// foreach(GameObject go in allThingsCreated)
// {
// if(go == null)
// {
// break;
// }
// Debug.Log("Processing : " + go.name);
// //check the uvs by name, and move object accordingly
// //string matName = go.GetComponentInChildren<MeshRenderer>().renderer.material.name;
// string matName;
// if(go.renderer)
// {
// matName = go.renderer.sharedMaterial.name;
//
// }
// else matName = go.GetComponentInChildren<MeshRenderer>().renderer.sharedMaterial.name;
//
//
// Debug.Log("Material name is : " + matName);
// //child object - this is where all the meshes with shared meshes will go
// GameObject ObjectToAddItemsTo = new GameObject();
// if(!CombineGroups[0])
// {
// GameObject starter = new GameObject();
// CombineGroups[0] = starter;
// //starter = CombineGroups[0];
// starter.name = matName;
// }
// foreach(GameObject a in CombineGroups)
// {
// if(a.name == matName)
// {
// ObjectToAddItemsTo = a;
// }
// }
// if(!ObjectToAddItemsTo) //There are no matches. Create a new go, add it to the combine groups, and attach.
// {
// ObjectToAddItemsTo = new GameObject();
// GameObject.Instantiate(ObjectToAddItemsTo);
// CombineGroups[CombineGroups.Length] = ObjectToAddItemsTo;
// go.transform.parent = ObjectToAddItemsTo.transform;
// }
// else //there is a match. parent the object
// {
// go.transform.parent = ObjectToAddItemsTo.transform;
// }
//
// //if the mesh.verts number more than 65,000, split into another gameobject, then add the component.
// }
//
// foreach(GameObject go in CombineGroups)
// {
// if(go == null)
// break;
// Debug.Log("Added CombineChildren to " + go.name);
// go.AddComponent<CombineChildren>();
// }
// allThingsCreated.Clear();
// }
//
// void CreateVegetation()
// {
// for(int i = 0; i <= numberToSpawn; i++)
// {
// GameObject Sphere1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
// Sphere1.AddComponent<SphereObject>();
// Sphere1.isStatic = true;
// Sphere1.transform.parent = transform;
// Sphere1.AddComponent<NavMeshObstacle>();
// //Sphere1.AddComponent<Rigidbody>();
// //Sphere1.renderer.material.mainTexture = textureToApplyForTest;
// if(i % 2 == 0)
// Sphere1.renderer.material = materialToApplyForTest[0];
// else Sphere1.renderer.material = materialToApplyForTest[1];
// Vector3 random1 = GetARandomPosition();
//
// //Check if on navmesh, if not, randomize again
//
// Sphere1.transform.position = random1;
// }
//
//
// }
}