-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGrid_part2.cs
98 lines (88 loc) · 3.63 KB
/
Grid_part2.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
using UnityEngine;
using System.Collections.Generic;
public class Grid : MonoBehaviour
{
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);
}
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 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);
}
}
}
}