forked from IndividualKex/ProceduralGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid_part6.cs
266 lines (246 loc) · 11 KB
/
Grid_part6.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
using UnityEngine;
using System.Collections.Generic;
using Pathfinding;
public class Grid : MonoBehaviour
{
public GameObject[] treePrefabs;
public Material terrainMaterial;
public Material edgeMaterial;
public float waterLevel = .4f;
public float scale = .1f;
public float treeNoiseScale = .05f;
public float treeDensity = .5f;
public float riverNoiseScale = .06f;
public int rivers = 5;
public int size = 100;
Cell[,] grid;
void Start() {
float[,] noiseMap = new float[size, size];
(float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f));
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
float noiseValue = Mathf.PerlinNoise(x * scale + xOffset, y * scale + yOffset);
noiseMap[x, y] = noiseValue;
}
}
float[,] falloffMap = new float[size, size];
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
float xv = x / (float)size * 2 - 1;
float yv = y / (float)size * 2 - 1;
float v = Mathf.Max(Mathf.Abs(xv), Mathf.Abs(yv));
falloffMap[x, y] = Mathf.Pow(v, 3f) / (Mathf.Pow(v, 3f) + Mathf.Pow(2.2f - 2.2f * v, 3f));
}
}
grid = new Cell[size, size];
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
float noiseValue = noiseMap[x, y];
noiseValue -= falloffMap[x, y];
bool isWater = noiseValue < waterLevel;
Cell cell = new Cell(isWater);
grid[x, y] = cell;
}
}
GenerateRivers(grid);
DrawTerrainMesh(grid);
DrawEdgeMesh(grid);
DrawTexture(grid);
GenerateTrees(grid);
}
void GenerateRivers(Cell[,] grid) {
float[,] noiseMap = new float[size, size];
(float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f));
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
float noiseValue = Mathf.PerlinNoise(x * riverNoiseScale + xOffset, y * riverNoiseScale + yOffset);
noiseMap[x, y] = noiseValue;
}
}
GridGraph gg = AstarData.active.graphs[0] as GridGraph;
gg.center = new Vector3(size / 2f - .5f, 0, size / 2f - .5f);
gg.SetDimensions(size, size, 1);
AstarData.active.Scan(gg);
AstarData.active.AddWorkItem(new AstarWorkItem(ctx => {
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
GraphNode node = gg.GetNode(x, y);
node.Walkable = noiseMap[x, y] > .4f;
}
}
}));
AstarData.active.FlushGraphUpdates();
int k = 0;
for(int i = 0; i < rivers; i++) {
GraphNode start = gg.nodes[Random.Range(16, size - 16)];
GraphNode end = gg.nodes[Random.Range(size * (size - 1) + 16, size * size - 16)];
ABPath path = ABPath.Construct((Vector3)start.position, (Vector3)end.position, (Path result) => {
for(int j = 0; j < result.path.Count; j++) {
GraphNode node = result.path[j];
int x = Mathf.RoundToInt(((Vector3)node.position).x);
int y = Mathf.RoundToInt(((Vector3)node.position).z);
grid[x, y].isWater = true;
}
k++;
});
AstarPath.StartPath(path);
AstarPath.BlockUntilCalculated(path);
}
}
void DrawTerrainMesh(Cell[,] grid) {
Mesh mesh = new Mesh();
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
List<Vector2> uvs = new List<Vector2>();
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
Cell cell = grid[x, y];
if(!cell.isWater) {
Vector3 a = new Vector3(x - .5f, 0, y + .5f);
Vector3 b = new Vector3(x + .5f, 0, y + .5f);
Vector3 c = new Vector3(x - .5f, 0, y - .5f);
Vector3 d = new Vector3(x + .5f, 0, y - .5f);
Vector2 uvA = new Vector2(x / (float)size, y / (float)size);
Vector2 uvB = new Vector2((x + 1) / (float)size, y / (float)size);
Vector2 uvC = new Vector2(x / (float)size, (y + 1) / (float)size);
Vector2 uvD = new Vector2((x + 1) / (float)size, (y + 1) / (float)size);
Vector3[] v = new Vector3[] { a, b, c, b, d, c };
Vector2[] uv = new Vector2[] { uvA, uvB, uvC, uvB, uvD, uvC };
for(int k = 0; k < 6; k++) {
vertices.Add(v[k]);
triangles.Add(triangles.Count);
uvs.Add(uv[k]);
}
}
}
}
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.uv = uvs.ToArray();
mesh.RecalculateNormals();
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
}
void DrawEdgeMesh(Cell[,] grid) {
Mesh mesh = new Mesh();
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
Cell cell = grid[x, y];
if(!cell.isWater) {
if(x > 0) {
Cell left = grid[x - 1, y];
if(left.isWater) {
Vector3 a = new Vector3(x - .5f, 0, y + .5f);
Vector3 b = new Vector3(x - .5f, 0, y - .5f);
Vector3 c = new Vector3(x - .5f, -1, y + .5f);
Vector3 d = new Vector3(x - .5f, -1, y - .5f);
Vector3[] v = new Vector3[] { a, b, c, b, d, c };
for(int k = 0; k < 6; k++) {
vertices.Add(v[k]);
triangles.Add(triangles.Count);
}
}
}
if(x < size - 1) {
Cell right = grid[x + 1, y];
if(right.isWater) {
Vector3 a = new Vector3(x + .5f, 0, y - .5f);
Vector3 b = new Vector3(x + .5f, 0, y + .5f);
Vector3 c = new Vector3(x + .5f, -1, y - .5f);
Vector3 d = new Vector3(x + .5f, -1, y + .5f);
Vector3[] v = new Vector3[] { a, b, c, b, d, c };
for(int k = 0; k < 6; k++) {
vertices.Add(v[k]);
triangles.Add(triangles.Count);
}
}
}
if(y > 0) {
Cell down = grid[x, y - 1];
if(down.isWater) {
Vector3 a = new Vector3(x - .5f, 0, y - .5f);
Vector3 b = new Vector3(x + .5f, 0, y - .5f);
Vector3 c = new Vector3(x - .5f, -1, y - .5f);
Vector3 d = new Vector3(x + .5f, -1, y - .5f);
Vector3[] v = new Vector3[] { a, b, c, b, d, c };
for(int k = 0; k < 6; k++) {
vertices.Add(v[k]);
triangles.Add(triangles.Count);
}
}
}
if(y < size - 1) {
Cell up = grid[x, y + 1];
if(up.isWater) {
Vector3 a = new Vector3(x + .5f, 0, y + .5f);
Vector3 b = new Vector3(x - .5f, 0, y + .5f);
Vector3 c = new Vector3(x + .5f, -1, y + .5f);
Vector3 d = new Vector3(x - .5f, -1, y + .5f);
Vector3[] v = new Vector3[] { a, b, c, b, d, c };
for(int k = 0; k < 6; k++) {
vertices.Add(v[k]);
triangles.Add(triangles.Count);
}
}
}
}
}
}
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals();
GameObject edgeObj = new GameObject("Edge");
edgeObj.transform.SetParent(transform);
MeshFilter meshFilter = edgeObj.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
MeshRenderer meshRenderer = edgeObj.AddComponent<MeshRenderer>();
meshRenderer.material = edgeMaterial;
}
void DrawTexture(Cell[,] grid) {
Texture2D texture = new Texture2D(size, size);
Color[] colorMap = new Color[size * size];
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
Cell cell = grid[x, y];
if(cell.isWater)
colorMap[y * size + x] = Color.blue;
else
colorMap[y * size + x] = Color.green;
}
}
texture.filterMode = FilterMode.Point;
texture.SetPixels(colorMap);
texture.Apply();
MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
meshRenderer.material = terrainMaterial;
meshRenderer.material.mainTexture = texture;
}
void GenerateTrees(Cell[,] grid) {
float[,] noiseMap = new float[size, size];
(float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f));
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
float noiseValue = Mathf.PerlinNoise(x * treeNoiseScale + xOffset, y * treeNoiseScale + yOffset);
noiseMap[x, y] = noiseValue;
}
}
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
Cell cell = grid[x, y];
if(!cell.isWater) {
float v = Random.Range(0f, treeDensity);
if(noiseMap[x, y] < v) {
GameObject prefab = treePrefabs[Random.Range(0, treePrefabs.Length)];
GameObject tree = Instantiate(prefab, transform);
tree.transform.position = new Vector3(x, 0, y);
tree.transform.rotation = Quaternion.Euler(0, Random.Range(0, 360f), 0);
tree.transform.localScale = Vector3.one * Random.Range(.8f, 1.2f);
}
}
}
}
}
}