-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCubeManager.cs
114 lines (102 loc) · 3.95 KB
/
CubeManager.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
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using CoherentNoise;
using CoherentNoise.Generation;
using CoherentNoise.Generation.Fractal;
using CoherentNoise.Generation.Combination;
using CoherentNoise.Generation.Voronoi;
public class CubeManager : MonoBehaviour {
public GameObject grass;
public float cubeSize = .5f; //size of each cube
public float surface = 1; //cutoff value of isosurface
public int size = 1;
public int max = 15;
public int min = -5;
public bool treeLeaves = false;
public float equilibrium = 1;
public bool done = false;
public bool caves = false;
public Hashtable perlinNoises = new Hashtable();
public List<GameObject> cubeList;
public int y_min = -5;
public int y_max = 15;
public GameObject TreeBuilding;
public GameObject tree;
private GameObject TreeBuilder;
public GameObject building;
public int treeDensity = 0;
public int buildingDensity = 0;
public int treeSize = 0;
public int buildingSize = 0;
public float leaf_density = 0;
private int [][] range = {new int[] {-10,10}, new int[] {0,25}, new int[] {-10,10}};
private CubeThreader a;
public bool smoothShade;
public GameObject object_prefab;
public Hashtable cubes = new Hashtable();
// Use this for initialization
public void Begin() {
TreeBuilder = Instantiate(TreeBuilding);
range [1] = new int[]{y_min, y_max};
if (size <= 0) size = 10;
//Generator s = new ValueNoise(Random.Range (-9000, 9000), null);
//int [] [] r = {new int[] {-size,size}, new int[] {-min,max}, new int[] {-size,size}};
//range = r;
Generator s = new GradientNoise (UnityEngine.Random.Range (-9000,9000));
a = new CubeThreader (cubeSize, s,range,surface,size,equilibrium,max,min,caves);
//Creates numCubes new marching cubes and adds them to the list
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++){
//Each cube should have a range of 30x30, with the cubes centered appropriately based on cubeNumber
//Height limits can be changed but for now are constant
//Range is not shifting properly when multiplied by i and j, it doent change inside range
//This enures a square of cubes centered around the origin.
Vector2 p = new Vector2 ( i,j);
cubes.Add (p,false);
a.addCubes(p);
}
}
Debug.Log ("CubeInfo");
a.Run();
int count = 0;
foreach (Vector2 posit in cubes.Keys) {
if((bool)cubes[posit] == false){
StartCoroutine(cube_Gen(posit));
}
}
//this.GetComponent<MeshRenderer> ().material.SetColor ("_Color", Color.red);
StartCoroutine(wait ());
//InvokeRepeating ("CheckAround", 10.0f, .5f);
perlinNoises = a.noises;
}
IEnumerator wait(){
while (cubeList.Count < size*size)
yield return new WaitForSeconds (0.1f);
done = true;
yield return null;
}
IEnumerator cube_Gen(Vector2 posit){
while (!(bool)(a.generated[posit])) yield return new WaitForSeconds (.1f);
GameObject cube = Instantiate(object_prefab);
List<int> tris = new List<int> ();
List<Vector3> vert = new List<Vector3> ();
tris = (List<int>)a.triangle[posit];
vert = (List<Vector3>)a.vertices[posit];
cube.GetComponent<MarchingCubes>().Go(tris,vert);
cube.transform.parent = gameObject.transform;
TreeAndBuilding tb = TreeBuilder.GetComponent<TreeAndBuilding> () as TreeAndBuilding;
TreeBuilder.GetComponent<TreeAndBuilding> ().Begin (posit,(Dictionary<Vector2,float>)perlinNoises[posit],
treeDensity,buildingDensity,cube,tree,building,cubeSize,
treeLeaves,treeSize,buildingSize,leaf_density);
GameObject grassObject = Instantiate (grass) as GameObject;
GrassScript gs = grassObject.GetComponent <GrassScript> () as GrassScript;
gs.Begin (cube, (Dictionary<Vector2,float>)perlinNoises [posit], cubeSize, posit);
while (!tb.done) yield return new WaitForSeconds (.1f);
while (!gs.done) yield return new WaitForSeconds (.1f);
cubeList.Add (cube);
cubes[posit] = true;
yield return null;
}
}