-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGrid_part1.cs
57 lines (51 loc) · 1.85 KB
/
Grid_part1.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
using UnityEngine;
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;
}
}
}
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);
}
}
}
}