├── Cell.cs ├── Grid_part1.cs ├── Grid_part2.cs ├── Grid_part3.cs ├── Grid_part4.cs ├── Grid_part5.cs ├── Grid_part6.cs └── Grid_part7.cs /Cell.cs: -------------------------------------------------------------------------------- 1 | public class Cell 2 | { 3 | public bool isWater; 4 | 5 | public Cell(bool isWater) { 6 | this.isWater = isWater; 7 | } 8 | } -------------------------------------------------------------------------------- /Grid_part1.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class Grid : MonoBehaviour 4 | { 5 | public float waterLevel = .4f; 6 | public float scale = .1f; 7 | public int size = 100; 8 | 9 | Cell[,] grid; 10 | 11 | void Start() { 12 | float[,] noiseMap = new float[size, size]; 13 | (float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f)); 14 | for(int y = 0; y < size; y++) { 15 | for(int x = 0; x < size; x++) { 16 | float noiseValue = Mathf.PerlinNoise(x * scale + xOffset, y * scale + yOffset); 17 | noiseMap[x, y] = noiseValue; 18 | } 19 | } 20 | 21 | float[,] falloffMap = new float[size, size]; 22 | for(int y = 0; y < size; y++) { 23 | for(int x = 0; x < size; x++) { 24 | float xv = x / (float)size * 2 - 1; 25 | float yv = y / (float)size * 2 - 1; 26 | float v = Mathf.Max(Mathf.Abs(xv), Mathf.Abs(yv)); 27 | falloffMap[x, y] = Mathf.Pow(v, 3f) / (Mathf.Pow(v, 3f) + Mathf.Pow(2.2f - 2.2f * v, 3f)); 28 | } 29 | } 30 | 31 | grid = new Cell[size, size]; 32 | for(int y = 0; y < size; y++) { 33 | for(int x = 0; x < size; x++) { 34 | float noiseValue = noiseMap[x, y]; 35 | noiseValue -= falloffMap[x, y]; 36 | bool isWater = noiseValue < waterLevel; 37 | Cell cell = new Cell(isWater); 38 | grid[x, y] = cell; 39 | } 40 | } 41 | } 42 | 43 | void OnDrawGizmos() { 44 | if(!Application.isPlaying) return; 45 | for(int y = 0; y < size; y++) { 46 | for(int x = 0; x < size; x++) { 47 | Cell cell = grid[x, y]; 48 | if(cell.isWater) 49 | Gizmos.color = Color.blue; 50 | else 51 | Gizmos.color = Color.green; 52 | Vector3 pos = new Vector3(x, 0, y); 53 | Gizmos.DrawCube(pos, Vector3.one); 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Grid_part2.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | public class Grid : MonoBehaviour 5 | { 6 | public float waterLevel = .4f; 7 | public float scale = .1f; 8 | public int size = 100; 9 | 10 | Cell[,] grid; 11 | 12 | void Start() { 13 | float[,] noiseMap = new float[size, size]; 14 | (float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f)); 15 | for(int y = 0; y < size; y++) { 16 | for(int x = 0; x < size; x++) { 17 | float noiseValue = Mathf.PerlinNoise(x * scale + xOffset, y * scale + yOffset); 18 | noiseMap[x, y] = noiseValue; 19 | } 20 | } 21 | 22 | float[,] falloffMap = new float[size, size]; 23 | for(int y = 0; y < size; y++) { 24 | for(int x = 0; x < size; x++) { 25 | float xv = x / (float)size * 2 - 1; 26 | float yv = y / (float)size * 2 - 1; 27 | float v = Mathf.Max(Mathf.Abs(xv), Mathf.Abs(yv)); 28 | falloffMap[x, y] = Mathf.Pow(v, 3f) / (Mathf.Pow(v, 3f) + Mathf.Pow(2.2f - 2.2f * v, 3f)); 29 | } 30 | } 31 | 32 | grid = new Cell[size, size]; 33 | for(int y = 0; y < size; y++) { 34 | for(int x = 0; x < size; x++) { 35 | float noiseValue = noiseMap[x, y]; 36 | noiseValue -= falloffMap[x, y]; 37 | bool isWater = noiseValue < waterLevel; 38 | Cell cell = new Cell(isWater); 39 | grid[x, y] = cell; 40 | } 41 | } 42 | 43 | DrawTerrainMesh(grid); 44 | } 45 | 46 | void DrawTerrainMesh(Cell[,] grid) { 47 | Mesh mesh = new Mesh(); 48 | List vertices = new List(); 49 | List triangles = new List(); 50 | List uvs = new List(); 51 | for(int y = 0; y < size; y++) { 52 | for(int x = 0; x < size; x++) { 53 | Cell cell = grid[x, y]; 54 | if(!cell.isWater) { 55 | Vector3 a = new Vector3(x - .5f, 0, y + .5f); 56 | Vector3 b = new Vector3(x + .5f, 0, y + .5f); 57 | Vector3 c = new Vector3(x - .5f, 0, y - .5f); 58 | Vector3 d = new Vector3(x + .5f, 0, y - .5f); 59 | Vector2 uvA = new Vector2(x / (float)size, y / (float)size); 60 | Vector2 uvB = new Vector2((x + 1) / (float)size, y / (float)size); 61 | Vector2 uvC = new Vector2(x / (float)size, (y + 1) / (float)size); 62 | Vector2 uvD = new Vector2((x + 1) / (float)size, (y + 1) / (float)size); 63 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 64 | Vector2[] uv = new Vector2[] { uvA, uvB, uvC, uvB, uvD, uvC }; 65 | for(int k = 0; k < 6; k++) { 66 | vertices.Add(v[k]); 67 | triangles.Add(triangles.Count); 68 | uvs.Add(uv[k]); 69 | } 70 | } 71 | } 72 | } 73 | mesh.vertices = vertices.ToArray(); 74 | mesh.triangles = triangles.ToArray(); 75 | mesh.uv = uvs.ToArray(); 76 | mesh.RecalculateNormals(); 77 | 78 | MeshFilter meshFilter = gameObject.AddComponent(); 79 | meshFilter.mesh = mesh; 80 | 81 | MeshRenderer meshRenderer = gameObject.AddComponent(); 82 | } 83 | 84 | void OnDrawGizmos() { 85 | if(!Application.isPlaying) return; 86 | for(int y = 0; y < size; y++) { 87 | for(int x = 0; x < size; x++) { 88 | Cell cell = grid[x, y]; 89 | if(cell.isWater) 90 | Gizmos.color = Color.blue; 91 | else 92 | Gizmos.color = Color.green; 93 | Vector3 pos = new Vector3(x, 0, y); 94 | Gizmos.DrawCube(pos, Vector3.one); 95 | } 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Grid_part3.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | public class Grid : MonoBehaviour 5 | { 6 | public Material terrainMaterial; 7 | public float waterLevel = .4f; 8 | public float scale = .1f; 9 | public int size = 100; 10 | 11 | Cell[,] grid; 12 | 13 | void Start() { 14 | float[,] noiseMap = new float[size, size]; 15 | (float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f)); 16 | for(int y = 0; y < size; y++) { 17 | for(int x = 0; x < size; x++) { 18 | float noiseValue = Mathf.PerlinNoise(x * scale + xOffset, y * scale + yOffset); 19 | noiseMap[x, y] = noiseValue; 20 | } 21 | } 22 | 23 | float[,] falloffMap = new float[size, size]; 24 | for(int y = 0; y < size; y++) { 25 | for(int x = 0; x < size; x++) { 26 | float xv = x / (float)size * 2 - 1; 27 | float yv = y / (float)size * 2 - 1; 28 | float v = Mathf.Max(Mathf.Abs(xv), Mathf.Abs(yv)); 29 | falloffMap[x, y] = Mathf.Pow(v, 3f) / (Mathf.Pow(v, 3f) + Mathf.Pow(2.2f - 2.2f * v, 3f)); 30 | } 31 | } 32 | 33 | grid = new Cell[size, size]; 34 | for(int y = 0; y < size; y++) { 35 | for(int x = 0; x < size; x++) { 36 | float noiseValue = noiseMap[x, y]; 37 | noiseValue -= falloffMap[x, y]; 38 | bool isWater = noiseValue < waterLevel; 39 | Cell cell = new Cell(isWater); 40 | grid[x, y] = cell; 41 | } 42 | } 43 | 44 | DrawTerrainMesh(grid); 45 | DrawTexture(grid); 46 | } 47 | 48 | void DrawTerrainMesh(Cell[,] grid) { 49 | Mesh mesh = new Mesh(); 50 | List vertices = new List(); 51 | List triangles = new List(); 52 | List uvs = new List(); 53 | for(int y = 0; y < size; y++) { 54 | for(int x = 0; x < size; x++) { 55 | Cell cell = grid[x, y]; 56 | /* if(!cell.isWater) { */ 57 | Vector3 a = new Vector3(x - .5f, 0, y + .5f); 58 | Vector3 b = new Vector3(x + .5f, 0, y + .5f); 59 | Vector3 c = new Vector3(x - .5f, 0, y - .5f); 60 | Vector3 d = new Vector3(x + .5f, 0, y - .5f); 61 | Vector2 uvA = new Vector2(x / (float)size, y / (float)size); 62 | Vector2 uvB = new Vector2((x + 1) / (float)size, y / (float)size); 63 | Vector2 uvC = new Vector2(x / (float)size, (y + 1) / (float)size); 64 | Vector2 uvD = new Vector2((x + 1) / (float)size, (y + 1) / (float)size); 65 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 66 | Vector2[] uv = new Vector2[] { uvA, uvB, uvC, uvB, uvD, uvC }; 67 | for(int k = 0; k < 6; k++) { 68 | vertices.Add(v[k]); 69 | triangles.Add(triangles.Count); 70 | uvs.Add(uv[k]); 71 | } 72 | /* } */ 73 | } 74 | } 75 | mesh.vertices = vertices.ToArray(); 76 | mesh.triangles = triangles.ToArray(); 77 | mesh.uv = uvs.ToArray(); 78 | mesh.RecalculateNormals(); 79 | 80 | MeshFilter meshFilter = gameObject.AddComponent(); 81 | meshFilter.mesh = mesh; 82 | 83 | MeshRenderer meshRenderer = gameObject.AddComponent(); 84 | } 85 | 86 | void DrawTexture(Cell[,] grid) { 87 | Texture2D texture = new Texture2D(size, size); 88 | Color[] colorMap = new Color[size * size]; 89 | for(int y = 0; y < size; y++) { 90 | for(int x = 0; x < size; x++) { 91 | Cell cell = grid[x, y]; 92 | if(cell.isWater) 93 | colorMap[y * size + x] = Color.blue; 94 | else 95 | colorMap[y * size + x] = Color.green; 96 | } 97 | } 98 | texture.filterMode = FilterMode.Point; 99 | texture.SetPixels(colorMap); 100 | texture.Apply(); 101 | 102 | MeshRenderer meshRenderer = gameObject.GetComponent(); 103 | meshRenderer.material = terrainMaterial; 104 | meshRenderer.material.mainTexture = texture; 105 | } 106 | 107 | void OnDrawGizmos() { 108 | if(!Application.isPlaying) return; 109 | for(int y = 0; y < size; y++) { 110 | for(int x = 0; x < size; x++) { 111 | Cell cell = grid[x, y]; 112 | if(cell.isWater) 113 | Gizmos.color = Color.blue; 114 | else 115 | Gizmos.color = Color.green; 116 | Vector3 pos = new Vector3(x, 0, y); 117 | Gizmos.DrawCube(pos, Vector3.one); 118 | } 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /Grid_part4.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | public class Grid : MonoBehaviour 5 | { 6 | public Material terrainMaterial; 7 | public Material edgeMaterial; 8 | public float waterLevel = .4f; 9 | public float scale = .1f; 10 | public int size = 100; 11 | 12 | Cell[,] grid; 13 | 14 | void Start() { 15 | float[,] noiseMap = new float[size, size]; 16 | (float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f)); 17 | for(int y = 0; y < size; y++) { 18 | for(int x = 0; x < size; x++) { 19 | float noiseValue = Mathf.PerlinNoise(x * scale + xOffset, y * scale + yOffset); 20 | noiseMap[x, y] = noiseValue; 21 | } 22 | } 23 | 24 | float[,] falloffMap = new float[size, size]; 25 | for(int y = 0; y < size; y++) { 26 | for(int x = 0; x < size; x++) { 27 | float xv = x / (float)size * 2 - 1; 28 | float yv = y / (float)size * 2 - 1; 29 | float v = Mathf.Max(Mathf.Abs(xv), Mathf.Abs(yv)); 30 | falloffMap[x, y] = Mathf.Pow(v, 3f) / (Mathf.Pow(v, 3f) + Mathf.Pow(2.2f - 2.2f * v, 3f)); 31 | } 32 | } 33 | 34 | grid = new Cell[size, size]; 35 | for(int y = 0; y < size; y++) { 36 | for(int x = 0; x < size; x++) { 37 | float noiseValue = noiseMap[x, y]; 38 | noiseValue -= falloffMap[x, y]; 39 | bool isWater = noiseValue < waterLevel; 40 | Cell cell = new Cell(isWater); 41 | grid[x, y] = cell; 42 | } 43 | } 44 | 45 | DrawTerrainMesh(grid); 46 | DrawEdgeMesh(grid); 47 | DrawTexture(grid); 48 | } 49 | 50 | void DrawTerrainMesh(Cell[,] grid) { 51 | Mesh mesh = new Mesh(); 52 | List vertices = new List(); 53 | List triangles = new List(); 54 | List uvs = new List(); 55 | for(int y = 0; y < size; y++) { 56 | for(int x = 0; x < size; x++) { 57 | Cell cell = grid[x, y]; 58 | if(!cell.isWater) { 59 | Vector3 a = new Vector3(x - .5f, 0, y + .5f); 60 | Vector3 b = new Vector3(x + .5f, 0, y + .5f); 61 | Vector3 c = new Vector3(x - .5f, 0, y - .5f); 62 | Vector3 d = new Vector3(x + .5f, 0, y - .5f); 63 | Vector2 uvA = new Vector2(x / (float)size, y / (float)size); 64 | Vector2 uvB = new Vector2((x + 1) / (float)size, y / (float)size); 65 | Vector2 uvC = new Vector2(x / (float)size, (y + 1) / (float)size); 66 | Vector2 uvD = new Vector2((x + 1) / (float)size, (y + 1) / (float)size); 67 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 68 | Vector2[] uv = new Vector2[] { uvA, uvB, uvC, uvB, uvD, uvC }; 69 | for(int k = 0; k < 6; k++) { 70 | vertices.Add(v[k]); 71 | triangles.Add(triangles.Count); 72 | uvs.Add(uv[k]); 73 | } 74 | } 75 | } 76 | } 77 | mesh.vertices = vertices.ToArray(); 78 | mesh.triangles = triangles.ToArray(); 79 | mesh.uv = uvs.ToArray(); 80 | mesh.RecalculateNormals(); 81 | 82 | MeshFilter meshFilter = gameObject.AddComponent(); 83 | meshFilter.mesh = mesh; 84 | 85 | MeshRenderer meshRenderer = gameObject.AddComponent(); 86 | } 87 | 88 | void DrawEdgeMesh(Cell[,] grid) { 89 | Mesh mesh = new Mesh(); 90 | List vertices = new List(); 91 | List triangles = new List(); 92 | for(int y = 0; y < size; y++) { 93 | for(int x = 0; x < size; x++) { 94 | Cell cell = grid[x, y]; 95 | if(!cell.isWater) { 96 | if(x > 0) { 97 | Cell left = grid[x - 1, y]; 98 | if(left.isWater) { 99 | Vector3 a = new Vector3(x - .5f, 0, y + .5f); 100 | Vector3 b = new Vector3(x - .5f, 0, y - .5f); 101 | Vector3 c = new Vector3(x - .5f, -1, y + .5f); 102 | Vector3 d = new Vector3(x - .5f, -1, y - .5f); 103 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 104 | for(int k = 0; k < 6; k++) { 105 | vertices.Add(v[k]); 106 | triangles.Add(triangles.Count); 107 | } 108 | } 109 | } 110 | if(x < size - 1) { 111 | Cell right = grid[x + 1, y]; 112 | if(right.isWater) { 113 | Vector3 a = new Vector3(x + .5f, 0, y - .5f); 114 | Vector3 b = new Vector3(x + .5f, 0, y + .5f); 115 | Vector3 c = new Vector3(x + .5f, -1, y - .5f); 116 | Vector3 d = new Vector3(x + .5f, -1, y + .5f); 117 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 118 | for(int k = 0; k < 6; k++) { 119 | vertices.Add(v[k]); 120 | triangles.Add(triangles.Count); 121 | } 122 | } 123 | } 124 | if(y > 0) { 125 | Cell down = grid[x, y - 1]; 126 | if(down.isWater) { 127 | Vector3 a = new Vector3(x - .5f, 0, y - .5f); 128 | Vector3 b = new Vector3(x + .5f, 0, y - .5f); 129 | Vector3 c = new Vector3(x - .5f, -1, y - .5f); 130 | Vector3 d = new Vector3(x + .5f, -1, y - .5f); 131 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 132 | for(int k = 0; k < 6; k++) { 133 | vertices.Add(v[k]); 134 | triangles.Add(triangles.Count); 135 | } 136 | } 137 | } 138 | if(y < size - 1) { 139 | Cell up = grid[x, y + 1]; 140 | if(up.isWater) { 141 | Vector3 a = new Vector3(x + .5f, 0, y + .5f); 142 | Vector3 b = new Vector3(x - .5f, 0, y + .5f); 143 | Vector3 c = new Vector3(x + .5f, -1, y + .5f); 144 | Vector3 d = new Vector3(x - .5f, -1, y + .5f); 145 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 146 | for(int k = 0; k < 6; k++) { 147 | vertices.Add(v[k]); 148 | triangles.Add(triangles.Count); 149 | } 150 | } 151 | } 152 | } 153 | } 154 | } 155 | mesh.vertices = vertices.ToArray(); 156 | mesh.triangles = triangles.ToArray(); 157 | mesh.RecalculateNormals(); 158 | 159 | GameObject edgeObj = new GameObject("Edge"); 160 | edgeObj.transform.SetParent(transform); 161 | 162 | MeshFilter meshFilter = edgeObj.AddComponent(); 163 | meshFilter.mesh = mesh; 164 | 165 | MeshRenderer meshRenderer = edgeObj.AddComponent(); 166 | meshRenderer.material = edgeMaterial; 167 | } 168 | 169 | void DrawTexture(Cell[,] grid) { 170 | Texture2D texture = new Texture2D(size, size); 171 | Color[] colorMap = new Color[size * size]; 172 | for(int y = 0; y < size; y++) { 173 | for(int x = 0; x < size; x++) { 174 | Cell cell = grid[x, y]; 175 | if(cell.isWater) 176 | colorMap[y * size + x] = Color.blue; 177 | else 178 | colorMap[y * size + x] = Color.green; 179 | } 180 | } 181 | texture.filterMode = FilterMode.Point; 182 | texture.SetPixels(colorMap); 183 | texture.Apply(); 184 | 185 | MeshRenderer meshRenderer = gameObject.GetComponent(); 186 | meshRenderer.material = terrainMaterial; 187 | meshRenderer.material.mainTexture = texture; 188 | } 189 | 190 | void OnDrawGizmos() { 191 | if(!Application.isPlaying) return; 192 | for(int y = 0; y < size; y++) { 193 | for(int x = 0; x < size; x++) { 194 | Cell cell = grid[x, y]; 195 | if(cell.isWater) 196 | Gizmos.color = Color.blue; 197 | else 198 | Gizmos.color = Color.green; 199 | Vector3 pos = new Vector3(x, 0, y); 200 | Gizmos.DrawCube(pos, Vector3.one); 201 | } 202 | } 203 | } 204 | } 205 | -------------------------------------------------------------------------------- /Grid_part5.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | public class Grid : MonoBehaviour 5 | { 6 | public GameObject[] treePrefabs; 7 | public Material terrainMaterial; 8 | public Material edgeMaterial; 9 | public float waterLevel = .4f; 10 | public float scale = .1f; 11 | public float treeNoiseScale = .05f; 12 | public float treeDensity = .5f; 13 | public int size = 100; 14 | 15 | Cell[,] grid; 16 | 17 | void Start() { 18 | float[,] noiseMap = new float[size, size]; 19 | (float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f)); 20 | for(int y = 0; y < size; y++) { 21 | for(int x = 0; x < size; x++) { 22 | float noiseValue = Mathf.PerlinNoise(x * scale + xOffset, y * scale + yOffset); 23 | noiseMap[x, y] = noiseValue; 24 | } 25 | } 26 | 27 | float[,] falloffMap = new float[size, size]; 28 | for(int y = 0; y < size; y++) { 29 | for(int x = 0; x < size; x++) { 30 | float xv = x / (float)size * 2 - 1; 31 | float yv = y / (float)size * 2 - 1; 32 | float v = Mathf.Max(Mathf.Abs(xv), Mathf.Abs(yv)); 33 | falloffMap[x, y] = Mathf.Pow(v, 3f) / (Mathf.Pow(v, 3f) + Mathf.Pow(2.2f - 2.2f * v, 3f)); 34 | } 35 | } 36 | 37 | grid = new Cell[size, size]; 38 | for(int y = 0; y < size; y++) { 39 | for(int x = 0; x < size; x++) { 40 | float noiseValue = noiseMap[x, y]; 41 | noiseValue -= falloffMap[x, y]; 42 | bool isWater = noiseValue < waterLevel; 43 | Cell cell = new Cell(isWater); 44 | grid[x, y] = cell; 45 | } 46 | } 47 | 48 | DrawTerrainMesh(grid); 49 | DrawEdgeMesh(grid); 50 | DrawTexture(grid); 51 | GenerateTrees(grid); 52 | } 53 | 54 | void DrawTerrainMesh(Cell[,] grid) { 55 | Mesh mesh = new Mesh(); 56 | List vertices = new List(); 57 | List triangles = new List(); 58 | List uvs = new List(); 59 | for(int y = 0; y < size; y++) { 60 | for(int x = 0; x < size; x++) { 61 | Cell cell = grid[x, y]; 62 | if(!cell.isWater) { 63 | Vector3 a = new Vector3(x - .5f, 0, y + .5f); 64 | Vector3 b = new Vector3(x + .5f, 0, y + .5f); 65 | Vector3 c = new Vector3(x - .5f, 0, y - .5f); 66 | Vector3 d = new Vector3(x + .5f, 0, y - .5f); 67 | Vector2 uvA = new Vector2(x / (float)size, y / (float)size); 68 | Vector2 uvB = new Vector2((x + 1) / (float)size, y / (float)size); 69 | Vector2 uvC = new Vector2(x / (float)size, (y + 1) / (float)size); 70 | Vector2 uvD = new Vector2((x + 1) / (float)size, (y + 1) / (float)size); 71 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 72 | Vector2[] uv = new Vector2[] { uvA, uvB, uvC, uvB, uvD, uvC }; 73 | for(int k = 0; k < 6; k++) { 74 | vertices.Add(v[k]); 75 | triangles.Add(triangles.Count); 76 | uvs.Add(uv[k]); 77 | } 78 | } 79 | } 80 | } 81 | mesh.vertices = vertices.ToArray(); 82 | mesh.triangles = triangles.ToArray(); 83 | mesh.uv = uvs.ToArray(); 84 | mesh.RecalculateNormals(); 85 | 86 | MeshFilter meshFilter = gameObject.AddComponent(); 87 | meshFilter.mesh = mesh; 88 | 89 | MeshRenderer meshRenderer = gameObject.AddComponent(); 90 | } 91 | 92 | void DrawEdgeMesh(Cell[,] grid) { 93 | Mesh mesh = new Mesh(); 94 | List vertices = new List(); 95 | List triangles = new List(); 96 | for(int y = 0; y < size; y++) { 97 | for(int x = 0; x < size; x++) { 98 | Cell cell = grid[x, y]; 99 | if(!cell.isWater) { 100 | if(x > 0) { 101 | Cell left = grid[x - 1, y]; 102 | if(left.isWater) { 103 | Vector3 a = new Vector3(x - .5f, 0, y + .5f); 104 | Vector3 b = new Vector3(x - .5f, 0, y - .5f); 105 | Vector3 c = new Vector3(x - .5f, -1, y + .5f); 106 | Vector3 d = new Vector3(x - .5f, -1, y - .5f); 107 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 108 | for(int k = 0; k < 6; k++) { 109 | vertices.Add(v[k]); 110 | triangles.Add(triangles.Count); 111 | } 112 | } 113 | } 114 | if(x < size - 1) { 115 | Cell right = grid[x + 1, y]; 116 | if(right.isWater) { 117 | Vector3 a = new Vector3(x + .5f, 0, y - .5f); 118 | Vector3 b = new Vector3(x + .5f, 0, y + .5f); 119 | Vector3 c = new Vector3(x + .5f, -1, y - .5f); 120 | Vector3 d = new Vector3(x + .5f, -1, y + .5f); 121 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 122 | for(int k = 0; k < 6; k++) { 123 | vertices.Add(v[k]); 124 | triangles.Add(triangles.Count); 125 | } 126 | } 127 | } 128 | if(y > 0) { 129 | Cell down = grid[x, y - 1]; 130 | if(down.isWater) { 131 | Vector3 a = new Vector3(x - .5f, 0, y - .5f); 132 | Vector3 b = new Vector3(x + .5f, 0, y - .5f); 133 | Vector3 c = new Vector3(x - .5f, -1, y - .5f); 134 | Vector3 d = new Vector3(x + .5f, -1, y - .5f); 135 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 136 | for(int k = 0; k < 6; k++) { 137 | vertices.Add(v[k]); 138 | triangles.Add(triangles.Count); 139 | } 140 | } 141 | } 142 | if(y < size - 1) { 143 | Cell up = grid[x, y + 1]; 144 | if(up.isWater) { 145 | Vector3 a = new Vector3(x + .5f, 0, y + .5f); 146 | Vector3 b = new Vector3(x - .5f, 0, y + .5f); 147 | Vector3 c = new Vector3(x + .5f, -1, y + .5f); 148 | Vector3 d = new Vector3(x - .5f, -1, y + .5f); 149 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 150 | for(int k = 0; k < 6; k++) { 151 | vertices.Add(v[k]); 152 | triangles.Add(triangles.Count); 153 | } 154 | } 155 | } 156 | } 157 | } 158 | } 159 | mesh.vertices = vertices.ToArray(); 160 | mesh.triangles = triangles.ToArray(); 161 | mesh.RecalculateNormals(); 162 | 163 | GameObject edgeObj = new GameObject("Edge"); 164 | edgeObj.transform.SetParent(transform); 165 | 166 | MeshFilter meshFilter = edgeObj.AddComponent(); 167 | meshFilter.mesh = mesh; 168 | 169 | MeshRenderer meshRenderer = edgeObj.AddComponent(); 170 | meshRenderer.material = edgeMaterial; 171 | } 172 | 173 | void DrawTexture(Cell[,] grid) { 174 | Texture2D texture = new Texture2D(size, size); 175 | Color[] colorMap = new Color[size * size]; 176 | for(int y = 0; y < size; y++) { 177 | for(int x = 0; x < size; x++) { 178 | Cell cell = grid[x, y]; 179 | if(cell.isWater) 180 | colorMap[y * size + x] = Color.blue; 181 | else 182 | colorMap[y * size + x] = Color.green; 183 | } 184 | } 185 | texture.filterMode = FilterMode.Point; 186 | texture.SetPixels(colorMap); 187 | texture.Apply(); 188 | 189 | MeshRenderer meshRenderer = gameObject.GetComponent(); 190 | meshRenderer.material = terrainMaterial; 191 | meshRenderer.material.mainTexture = texture; 192 | } 193 | 194 | void GenerateTrees(Cell[,] grid) { 195 | float[,] noiseMap = new float[size, size]; 196 | (float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f)); 197 | for(int y = 0; y < size; y++) { 198 | for(int x = 0; x < size; x++) { 199 | float noiseValue = Mathf.PerlinNoise(x * treeNoiseScale + xOffset, y * treeNoiseScale + yOffset); 200 | noiseMap[x, y] = noiseValue; 201 | } 202 | } 203 | 204 | for(int y = 0; y < size; y++) { 205 | for(int x = 0; x < size; x++) { 206 | Cell cell = grid[x, y]; 207 | if(!cell.isWater) { 208 | float v = Random.Range(0f, treeDensity); 209 | if(noiseMap[x, y] < v) { 210 | GameObject prefab = treePrefabs[Random.Range(0, treePrefabs.Length)]; 211 | GameObject tree = Instantiate(prefab, transform); 212 | tree.transform.position = new Vector3(x, 0, y); 213 | tree.transform.rotation = Quaternion.Euler(0, Random.Range(0, 360f), 0); 214 | tree.transform.localScale = Vector3.one * Random.Range(.8f, 1.2f); 215 | } 216 | } 217 | } 218 | } 219 | } 220 | 221 | void OnDrawGizmos() { 222 | if(!Application.isPlaying) return; 223 | for(int y = 0; y < size; y++) { 224 | for(int x = 0; x < size; x++) { 225 | Cell cell = grid[x, y]; 226 | if(cell.isWater) 227 | Gizmos.color = Color.blue; 228 | else 229 | Gizmos.color = Color.green; 230 | Vector3 pos = new Vector3(x, 0, y); 231 | Gizmos.DrawCube(pos, Vector3.one); 232 | } 233 | } 234 | } 235 | } 236 | -------------------------------------------------------------------------------- /Grid_part6.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | using Pathfinding; 4 | 5 | public class Grid : MonoBehaviour 6 | { 7 | public GameObject[] treePrefabs; 8 | public Material terrainMaterial; 9 | public Material edgeMaterial; 10 | public float waterLevel = .4f; 11 | public float scale = .1f; 12 | public float treeNoiseScale = .05f; 13 | public float treeDensity = .5f; 14 | public float riverNoiseScale = .06f; 15 | public int rivers = 5; 16 | public int size = 100; 17 | 18 | Cell[,] grid; 19 | 20 | void Start() { 21 | float[,] noiseMap = new float[size, size]; 22 | (float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f)); 23 | for(int y = 0; y < size; y++) { 24 | for(int x = 0; x < size; x++) { 25 | float noiseValue = Mathf.PerlinNoise(x * scale + xOffset, y * scale + yOffset); 26 | noiseMap[x, y] = noiseValue; 27 | } 28 | } 29 | 30 | float[,] falloffMap = new float[size, size]; 31 | for(int y = 0; y < size; y++) { 32 | for(int x = 0; x < size; x++) { 33 | float xv = x / (float)size * 2 - 1; 34 | float yv = y / (float)size * 2 - 1; 35 | float v = Mathf.Max(Mathf.Abs(xv), Mathf.Abs(yv)); 36 | falloffMap[x, y] = Mathf.Pow(v, 3f) / (Mathf.Pow(v, 3f) + Mathf.Pow(2.2f - 2.2f * v, 3f)); 37 | } 38 | } 39 | 40 | grid = new Cell[size, size]; 41 | for(int y = 0; y < size; y++) { 42 | for(int x = 0; x < size; x++) { 43 | float noiseValue = noiseMap[x, y]; 44 | noiseValue -= falloffMap[x, y]; 45 | bool isWater = noiseValue < waterLevel; 46 | Cell cell = new Cell(isWater); 47 | grid[x, y] = cell; 48 | } 49 | } 50 | 51 | GenerateRivers(grid); 52 | DrawTerrainMesh(grid); 53 | DrawEdgeMesh(grid); 54 | DrawTexture(grid); 55 | GenerateTrees(grid); 56 | } 57 | 58 | void GenerateRivers(Cell[,] grid) { 59 | float[,] noiseMap = new float[size, size]; 60 | (float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f)); 61 | for(int y = 0; y < size; y++) { 62 | for(int x = 0; x < size; x++) { 63 | float noiseValue = Mathf.PerlinNoise(x * riverNoiseScale + xOffset, y * riverNoiseScale + yOffset); 64 | noiseMap[x, y] = noiseValue; 65 | } 66 | } 67 | 68 | GridGraph gg = AstarData.active.graphs[0] as GridGraph; 69 | gg.center = new Vector3(size / 2f - .5f, 0, size / 2f - .5f); 70 | gg.SetDimensions(size, size, 1); 71 | AstarData.active.Scan(gg); 72 | AstarData.active.AddWorkItem(new AstarWorkItem(ctx => { 73 | for(int y = 0; y < size; y++) { 74 | for(int x = 0; x < size; x++) { 75 | GraphNode node = gg.GetNode(x, y); 76 | node.Walkable = noiseMap[x, y] > .4f; 77 | } 78 | } 79 | })); 80 | AstarData.active.FlushGraphUpdates(); 81 | 82 | int k = 0; 83 | for(int i = 0; i < rivers; i++) { 84 | GraphNode start = gg.nodes[Random.Range(16, size - 16)]; 85 | GraphNode end = gg.nodes[Random.Range(size * (size - 1) + 16, size * size - 16)]; 86 | ABPath path = ABPath.Construct((Vector3)start.position, (Vector3)end.position, (Path result) => { 87 | for(int j = 0; j < result.path.Count; j++) { 88 | GraphNode node = result.path[j]; 89 | int x = Mathf.RoundToInt(((Vector3)node.position).x); 90 | int y = Mathf.RoundToInt(((Vector3)node.position).z); 91 | grid[x, y].isWater = true; 92 | } 93 | k++; 94 | }); 95 | AstarPath.StartPath(path); 96 | AstarPath.BlockUntilCalculated(path); 97 | } 98 | } 99 | 100 | void DrawTerrainMesh(Cell[,] grid) { 101 | Mesh mesh = new Mesh(); 102 | List vertices = new List(); 103 | List triangles = new List(); 104 | List uvs = new List(); 105 | for(int y = 0; y < size; y++) { 106 | for(int x = 0; x < size; x++) { 107 | Cell cell = grid[x, y]; 108 | if(!cell.isWater) { 109 | Vector3 a = new Vector3(x - .5f, 0, y + .5f); 110 | Vector3 b = new Vector3(x + .5f, 0, y + .5f); 111 | Vector3 c = new Vector3(x - .5f, 0, y - .5f); 112 | Vector3 d = new Vector3(x + .5f, 0, y - .5f); 113 | Vector2 uvA = new Vector2(x / (float)size, y / (float)size); 114 | Vector2 uvB = new Vector2((x + 1) / (float)size, y / (float)size); 115 | Vector2 uvC = new Vector2(x / (float)size, (y + 1) / (float)size); 116 | Vector2 uvD = new Vector2((x + 1) / (float)size, (y + 1) / (float)size); 117 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 118 | Vector2[] uv = new Vector2[] { uvA, uvB, uvC, uvB, uvD, uvC }; 119 | for(int k = 0; k < 6; k++) { 120 | vertices.Add(v[k]); 121 | triangles.Add(triangles.Count); 122 | uvs.Add(uv[k]); 123 | } 124 | } 125 | } 126 | } 127 | mesh.vertices = vertices.ToArray(); 128 | mesh.triangles = triangles.ToArray(); 129 | mesh.uv = uvs.ToArray(); 130 | mesh.RecalculateNormals(); 131 | 132 | MeshFilter meshFilter = gameObject.AddComponent(); 133 | meshFilter.mesh = mesh; 134 | 135 | MeshRenderer meshRenderer = gameObject.AddComponent(); 136 | } 137 | 138 | void DrawEdgeMesh(Cell[,] grid) { 139 | Mesh mesh = new Mesh(); 140 | List vertices = new List(); 141 | List triangles = new List(); 142 | for(int y = 0; y < size; y++) { 143 | for(int x = 0; x < size; x++) { 144 | Cell cell = grid[x, y]; 145 | if(!cell.isWater) { 146 | if(x > 0) { 147 | Cell left = grid[x - 1, y]; 148 | if(left.isWater) { 149 | Vector3 a = new Vector3(x - .5f, 0, y + .5f); 150 | Vector3 b = new Vector3(x - .5f, 0, y - .5f); 151 | Vector3 c = new Vector3(x - .5f, -1, y + .5f); 152 | Vector3 d = new Vector3(x - .5f, -1, y - .5f); 153 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 154 | for(int k = 0; k < 6; k++) { 155 | vertices.Add(v[k]); 156 | triangles.Add(triangles.Count); 157 | } 158 | } 159 | } 160 | if(x < size - 1) { 161 | Cell right = grid[x + 1, y]; 162 | if(right.isWater) { 163 | Vector3 a = new Vector3(x + .5f, 0, y - .5f); 164 | Vector3 b = new Vector3(x + .5f, 0, y + .5f); 165 | Vector3 c = new Vector3(x + .5f, -1, y - .5f); 166 | Vector3 d = new Vector3(x + .5f, -1, y + .5f); 167 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 168 | for(int k = 0; k < 6; k++) { 169 | vertices.Add(v[k]); 170 | triangles.Add(triangles.Count); 171 | } 172 | } 173 | } 174 | if(y > 0) { 175 | Cell down = grid[x, y - 1]; 176 | if(down.isWater) { 177 | Vector3 a = new Vector3(x - .5f, 0, y - .5f); 178 | Vector3 b = new Vector3(x + .5f, 0, y - .5f); 179 | Vector3 c = new Vector3(x - .5f, -1, y - .5f); 180 | Vector3 d = new Vector3(x + .5f, -1, y - .5f); 181 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 182 | for(int k = 0; k < 6; k++) { 183 | vertices.Add(v[k]); 184 | triangles.Add(triangles.Count); 185 | } 186 | } 187 | } 188 | if(y < size - 1) { 189 | Cell up = grid[x, y + 1]; 190 | if(up.isWater) { 191 | Vector3 a = new Vector3(x + .5f, 0, y + .5f); 192 | Vector3 b = new Vector3(x - .5f, 0, y + .5f); 193 | Vector3 c = new Vector3(x + .5f, -1, y + .5f); 194 | Vector3 d = new Vector3(x - .5f, -1, y + .5f); 195 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 196 | for(int k = 0; k < 6; k++) { 197 | vertices.Add(v[k]); 198 | triangles.Add(triangles.Count); 199 | } 200 | } 201 | } 202 | } 203 | } 204 | } 205 | mesh.vertices = vertices.ToArray(); 206 | mesh.triangles = triangles.ToArray(); 207 | mesh.RecalculateNormals(); 208 | 209 | GameObject edgeObj = new GameObject("Edge"); 210 | edgeObj.transform.SetParent(transform); 211 | 212 | MeshFilter meshFilter = edgeObj.AddComponent(); 213 | meshFilter.mesh = mesh; 214 | 215 | MeshRenderer meshRenderer = edgeObj.AddComponent(); 216 | meshRenderer.material = edgeMaterial; 217 | } 218 | 219 | void DrawTexture(Cell[,] grid) { 220 | Texture2D texture = new Texture2D(size, size); 221 | Color[] colorMap = new Color[size * size]; 222 | for(int y = 0; y < size; y++) { 223 | for(int x = 0; x < size; x++) { 224 | Cell cell = grid[x, y]; 225 | if(cell.isWater) 226 | colorMap[y * size + x] = Color.blue; 227 | else 228 | colorMap[y * size + x] = Color.green; 229 | } 230 | } 231 | texture.filterMode = FilterMode.Point; 232 | texture.SetPixels(colorMap); 233 | texture.Apply(); 234 | 235 | MeshRenderer meshRenderer = gameObject.GetComponent(); 236 | meshRenderer.material = terrainMaterial; 237 | meshRenderer.material.mainTexture = texture; 238 | } 239 | 240 | void GenerateTrees(Cell[,] grid) { 241 | float[,] noiseMap = new float[size, size]; 242 | (float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f)); 243 | for(int y = 0; y < size; y++) { 244 | for(int x = 0; x < size; x++) { 245 | float noiseValue = Mathf.PerlinNoise(x * treeNoiseScale + xOffset, y * treeNoiseScale + yOffset); 246 | noiseMap[x, y] = noiseValue; 247 | } 248 | } 249 | 250 | for(int y = 0; y < size; y++) { 251 | for(int x = 0; x < size; x++) { 252 | Cell cell = grid[x, y]; 253 | if(!cell.isWater) { 254 | float v = Random.Range(0f, treeDensity); 255 | if(noiseMap[x, y] < v) { 256 | GameObject prefab = treePrefabs[Random.Range(0, treePrefabs.Length)]; 257 | GameObject tree = Instantiate(prefab, transform); 258 | tree.transform.position = new Vector3(x, 0, y); 259 | tree.transform.rotation = Quaternion.Euler(0, Random.Range(0, 360f), 0); 260 | tree.transform.localScale = Vector3.one * Random.Range(.8f, 1.2f); 261 | } 262 | } 263 | } 264 | } 265 | } 266 | } -------------------------------------------------------------------------------- /Grid_part7.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | using Pathfinding; 4 | 5 | public class Grid : MonoBehaviour 6 | { 7 | public GameObject[] treePrefabs; 8 | public Material terrainMaterial; 9 | public Material edgeMaterial; 10 | public float waterLevel = .4f; 11 | public float scale = .1f; 12 | public float treeNoiseScale = .05f; 13 | public float treeDensity = .5f; 14 | public float riverNoiseScale = .06f; 15 | public int rivers = 5; 16 | public int size = 100; 17 | 18 | Cell[,] grid; 19 | 20 | void Start() { 21 | float[,] noiseMap = new float[size, size]; 22 | (float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f)); 23 | for(int y = 0; y < size; y++) { 24 | for(int x = 0; x < size; x++) { 25 | float noiseValue = Mathf.PerlinNoise(x * scale + xOffset, y * scale + yOffset); 26 | noiseMap[x, y] = noiseValue; 27 | } 28 | } 29 | 30 | float[,] falloffMap = new float[size, size]; 31 | for(int y = 0; y < size; y++) { 32 | for(int x = 0; x < size; x++) { 33 | float xv = x / (float)size * 2 - 1; 34 | float yv = y / (float)size * 2 - 1; 35 | float v = Mathf.Max(Mathf.Abs(xv), Mathf.Abs(yv)); 36 | falloffMap[x, y] = Mathf.Pow(v, 3f) / (Mathf.Pow(v, 3f) + Mathf.Pow(2.2f - 2.2f * v, 3f)); 37 | } 38 | } 39 | 40 | grid = new Cell[size, size]; 41 | for(int y = 0; y < size; y++) { 42 | for(int x = 0; x < size; x++) { 43 | float noiseValue = noiseMap[x, y]; 44 | noiseValue -= falloffMap[x, y]; 45 | bool isWater = noiseValue < waterLevel; 46 | Cell cell = new Cell(isWater); 47 | grid[x, y] = cell; 48 | } 49 | } 50 | 51 | GenerateRivers(grid); 52 | DrawTerrainMesh(grid); 53 | DrawEdgeMesh(grid); 54 | DrawTexture(grid); 55 | GenerateTrees(grid); 56 | } 57 | 58 | void GenerateRivers(Cell[,] grid) { 59 | float[,] noiseMap = new float[size, size]; 60 | (float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f)); 61 | for(int y = 0; y < size; y++) { 62 | for(int x = 0; x < size; x++) { 63 | float noiseValue = Mathf.PerlinNoise(x * riverNoiseScale + xOffset, y * riverNoiseScale + yOffset); 64 | noiseMap[x, y] = noiseValue; 65 | } 66 | } 67 | 68 | GridGraph gg = AstarData.active.graphs[0] as GridGraph; 69 | gg.center = new Vector3(size / 2f - .5f, 0, size / 2f - .5f); 70 | gg.SetDimensions(size, size, 1); 71 | AstarData.active.Scan(gg); 72 | AstarData.active.AddWorkItem(new AstarWorkItem(ctx => { 73 | for(int y = 0; y < size; y++) { 74 | for(int x = 0; x < size; x++) { 75 | GraphNode node = gg.GetNode(x, y); 76 | node.Walkable = noiseMap[x, y] > .4f; 77 | } 78 | } 79 | })); 80 | AstarData.active.FlushGraphUpdates(); 81 | 82 | int k = 0; 83 | for(int i = 0; i < rivers; i++) { 84 | GraphNode start = gg.nodes[Random.Range(16, size - 16)]; 85 | GraphNode end = gg.nodes[Random.Range(size * (size - 1) + 16, size * size - 16)]; 86 | ABPath path = ABPath.Construct((Vector3)start.position, (Vector3)end.position, (Path result) => { 87 | for(int j = 0; j < result.path.Count; j++) { 88 | GraphNode node = result.path[j]; 89 | int x = Mathf.RoundToInt(((Vector3)node.position).x); 90 | int y = Mathf.RoundToInt(((Vector3)node.position).z); 91 | grid[x, y].isWater = true; 92 | } 93 | k++; 94 | }); 95 | AstarPath.StartPath(path); 96 | AstarPath.BlockUntilCalculated(path); 97 | } 98 | } 99 | 100 | void DrawTerrainMesh(Cell[,] grid) { 101 | Mesh mesh = new Mesh(); 102 | List vertices = new List(); 103 | List triangles = new List(); 104 | List uvs = new List(); 105 | for(int y = 0; y < size; y++) { 106 | for(int x = 0; x < size; x++) { 107 | Cell cell = grid[x, y]; 108 | if(!cell.isWater) { 109 | Vector3 a = new Vector3(x - .5f, 0, y + .5f); 110 | Vector3 b = new Vector3(x + .5f, 0, y + .5f); 111 | Vector3 c = new Vector3(x - .5f, 0, y - .5f); 112 | Vector3 d = new Vector3(x + .5f, 0, y - .5f); 113 | Vector2 uvA = new Vector2(x / (float)size, y / (float)size); 114 | Vector2 uvB = new Vector2((x + 1) / (float)size, y / (float)size); 115 | Vector2 uvC = new Vector2(x / (float)size, (y + 1) / (float)size); 116 | Vector2 uvD = new Vector2((x + 1) / (float)size, (y + 1) / (float)size); 117 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 118 | Vector2[] uv = new Vector2[] { uvA, uvB, uvC, uvB, uvD, uvC }; 119 | for(int k = 0; k < 6; k++) { 120 | vertices.Add(v[k]); 121 | triangles.Add(triangles.Count); 122 | uvs.Add(uv[k]); 123 | } 124 | } 125 | } 126 | } 127 | mesh.vertices = vertices.ToArray(); 128 | mesh.triangles = triangles.ToArray(); 129 | mesh.uv = uvs.ToArray(); 130 | mesh.RecalculateNormals(); 131 | 132 | MeshFilter meshFilter = gameObject.AddComponent(); 133 | meshFilter.mesh = mesh; 134 | 135 | MeshRenderer meshRenderer = gameObject.AddComponent(); 136 | } 137 | 138 | void DrawEdgeMesh(Cell[,] grid) { 139 | Mesh mesh = new Mesh(); 140 | List vertices = new List(); 141 | List triangles = new List(); 142 | for(int y = 0; y < size; y++) { 143 | for(int x = 0; x < size; x++) { 144 | Cell cell = grid[x, y]; 145 | if(!cell.isWater) { 146 | if(x > 0) { 147 | Cell left = grid[x - 1, y]; 148 | if(left.isWater) { 149 | Vector3 a = new Vector3(x - .5f, 0, y + .5f); 150 | Vector3 b = new Vector3(x - .5f, 0, y - .5f); 151 | Vector3 c = new Vector3(x - .5f, -1, y + .5f); 152 | Vector3 d = new Vector3(x - .5f, -1, y - .5f); 153 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 154 | for(int k = 0; k < 6; k++) { 155 | vertices.Add(v[k]); 156 | triangles.Add(triangles.Count); 157 | } 158 | } 159 | } 160 | if(x < size - 1) { 161 | Cell right = grid[x + 1, y]; 162 | if(right.isWater) { 163 | Vector3 a = new Vector3(x + .5f, 0, y - .5f); 164 | Vector3 b = new Vector3(x + .5f, 0, y + .5f); 165 | Vector3 c = new Vector3(x + .5f, -1, y - .5f); 166 | Vector3 d = new Vector3(x + .5f, -1, y + .5f); 167 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 168 | for(int k = 0; k < 6; k++) { 169 | vertices.Add(v[k]); 170 | triangles.Add(triangles.Count); 171 | } 172 | } 173 | } 174 | if(y > 0) { 175 | Cell down = grid[x, y - 1]; 176 | if(down.isWater) { 177 | Vector3 a = new Vector3(x - .5f, 0, y - .5f); 178 | Vector3 b = new Vector3(x + .5f, 0, y - .5f); 179 | Vector3 c = new Vector3(x - .5f, -1, y - .5f); 180 | Vector3 d = new Vector3(x + .5f, -1, y - .5f); 181 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 182 | for(int k = 0; k < 6; k++) { 183 | vertices.Add(v[k]); 184 | triangles.Add(triangles.Count); 185 | } 186 | } 187 | } 188 | if(y < size - 1) { 189 | Cell up = grid[x, y + 1]; 190 | if(up.isWater) { 191 | Vector3 a = new Vector3(x + .5f, 0, y + .5f); 192 | Vector3 b = new Vector3(x - .5f, 0, y + .5f); 193 | Vector3 c = new Vector3(x + .5f, -1, y + .5f); 194 | Vector3 d = new Vector3(x - .5f, -1, y + .5f); 195 | Vector3[] v = new Vector3[] { a, b, c, b, d, c }; 196 | for(int k = 0; k < 6; k++) { 197 | vertices.Add(v[k]); 198 | triangles.Add(triangles.Count); 199 | } 200 | } 201 | } 202 | } 203 | } 204 | } 205 | mesh.vertices = vertices.ToArray(); 206 | mesh.triangles = triangles.ToArray(); 207 | mesh.RecalculateNormals(); 208 | 209 | GameObject edgeObj = new GameObject("Edge"); 210 | edgeObj.transform.SetParent(transform); 211 | 212 | MeshFilter meshFilter = edgeObj.AddComponent(); 213 | meshFilter.mesh = mesh; 214 | 215 | MeshRenderer meshRenderer = edgeObj.AddComponent(); 216 | meshRenderer.material = edgeMaterial; 217 | } 218 | 219 | void DrawTexture(Cell[,] grid) { 220 | Texture2D texture = new Texture2D(size, size); 221 | Color[] colorMap = new Color[size * size]; 222 | for(int y = 0; y < size; y++) { 223 | for(int x = 0; x < size; x++) { 224 | Cell cell = grid[x, y]; 225 | if(cell.isWater) 226 | colorMap[y * size + x] = Color.blue; 227 | else 228 | colorMap[y * size + x] = new Color(153 / 255f, 191 / 255f, 115 / 255f); 229 | } 230 | } 231 | texture.filterMode = FilterMode.Point; 232 | texture.SetPixels(colorMap); 233 | texture.Apply(); 234 | 235 | MeshRenderer meshRenderer = gameObject.GetComponent(); 236 | meshRenderer.material = terrainMaterial; 237 | meshRenderer.material.mainTexture = texture; 238 | } 239 | 240 | void GenerateTrees(Cell[,] grid) { 241 | float[,] noiseMap = new float[size, size]; 242 | (float xOffset, float yOffset) = (Random.Range(-10000f, 10000f), Random.Range(-10000f, 10000f)); 243 | for(int y = 0; y < size; y++) { 244 | for(int x = 0; x < size; x++) { 245 | float noiseValue = Mathf.PerlinNoise(x * treeNoiseScale + xOffset, y * treeNoiseScale + yOffset); 246 | noiseMap[x, y] = noiseValue; 247 | } 248 | } 249 | 250 | for(int y = 0; y < size; y++) { 251 | for(int x = 0; x < size; x++) { 252 | Cell cell = grid[x, y]; 253 | if(!cell.isWater) { 254 | float v = Random.Range(0f, treeDensity); 255 | if(noiseMap[x, y] < v) { 256 | GameObject prefab = treePrefabs[Random.Range(0, treePrefabs.Length)]; 257 | GameObject tree = Instantiate(prefab, transform); 258 | tree.transform.position = new Vector3(x, 0, y); 259 | tree.transform.rotation = Quaternion.Euler(0, Random.Range(0, 360f), 0); 260 | tree.transform.localScale = Vector3.one * Random.Range(.8f, 1.2f); 261 | } 262 | } 263 | } 264 | } 265 | } 266 | } --------------------------------------------------------------------------------