-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGrid_part4.cs
204 lines (188 loc) · 8.25 KB
/
Grid_part4.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
using UnityEngine;
using System.Collections.Generic;
public class Grid : MonoBehaviour
{
public Material terrainMaterial;
public Material edgeMaterial;
public float waterLevel = .4f;
public float scale = .1f;
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;
}
}
DrawTerrainMesh(grid);
DrawEdgeMesh(grid);
DrawTexture(grid);
}
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 OnDrawGizmos() {
if(!Application.isPlaying) return;
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
Cell cell = grid[x, y];
if(cell.isWater)
Gizmos.color = Color.blue;
else
Gizmos.color = Color.green;
Vector3 pos = new Vector3(x, 0, y);
Gizmos.DrawCube(pos, Vector3.one);
}
}
}
}