-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunk.cs
198 lines (152 loc) · 4.55 KB
/
Chunk.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chunk
{
public GameObject chunkObject;
MeshFilter meshFilter;
MeshCollider meshCollider;
MeshRenderer meshRenderer;
Vector3Int chunkPosition;
TerrainPoint[,,] terrainMap;
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
List<Color> colors = new List<Color>();
int width { get { return GameData.ChunkWidth; } }
int height { get { return GameData.ChunkHeight; } }
float terrainSurface { get { return GameData.terrainSurface; } }
public Chunk(Vector3Int _position)
{
chunkObject = new GameObject();
chunkObject.name = string.Format("Chunk {0}, {1}", _position.x, _position.z);
chunkPosition = _position;
chunkObject.transform.position = chunkPosition;
meshFilter = chunkObject.AddComponent<MeshFilter>();
meshCollider = chunkObject.AddComponent<MeshCollider>();
meshRenderer = chunkObject.AddComponent<MeshRenderer>();
meshRenderer.material = Resources.Load<Material>("Materials/Terrain");
chunkObject.transform.tag = "Terrain";
terrainMap = new TerrainPoint[width + 1, height + 1, width + 1];
PopulateTerrainMap();
CreateMeshData();
}
void CreateMeshData()
{
ClearMeshData();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
for (int z = 0; z < width; z++)
{
MarchCube(new Vector3Int(x, y, z));
}
}
}
BuildMesh();
}
void PopulateTerrainMap()
{
for (int x = 0; x < width + 1; x++)
{
for (int z = 0; z < width + 1; z++)
{
for (int y = 0; y < height + 1; y++)
{
float thisHeight;
thisHeight = GameData.GetTerrainHeight(x + chunkPosition.x, z + chunkPosition.z);
terrainMap[x, y, z] = new TerrainPoint((float)y - thisHeight, Random.Range(0, 2));
}
}
}
}
void MarchCube(Vector3Int position)
{
float[] cube = new float[8];
for (int i = 0; i < 8; i++)
{
cube[i] = SampleTerrain(position + GameData.CornerTable[i]);
}
int configIndex = GetCubeConfiguration(cube);
if (configIndex == 0 || configIndex == 255)
return;
int edgeIndex = 0;
for (int i = 0; i < 5; i++)
{
for (int p = 0; p < 3; p++)
{
int indice = GameData.TriangleTable[configIndex, edgeIndex];
if (indice == -1)
return;
Vector3 vert1 = position + GameData.CornerTable[GameData.EdgeIndexes[indice, 0]];
Vector3 vert2 = position + GameData.CornerTable[GameData.EdgeIndexes[indice, 1]];
Vector3 vertPosition;
float vert1Sample = cube[GameData.EdgeIndexes[indice, 0]];
float vert2Sample = cube[GameData.EdgeIndexes[indice, 1]];
float difference = vert2Sample - vert1Sample;
if (difference == 0)
difference = terrainSurface;
else
difference = (terrainSurface - vert1Sample) / difference;
vertPosition = vert1 + ((vert2 - vert1) * difference);
triangles.Add(VertForIndice(vertPosition, position));
edgeIndex++;
}
}
}
int GetCubeConfiguration(float[] cube)
{
int configurationIndex = 0;
for (int i = 0; i < 8; i++)
{
if (cube[i] > terrainSurface)
configurationIndex |= 1 << i;
}
return configurationIndex;
}
public void PlaceTerrain(Vector3 pos)
{
Vector3Int v3Int = new Vector3Int(Mathf.CeilToInt(pos.x), Mathf.CeilToInt(pos.y), Mathf.CeilToInt(pos.z));
v3Int -= chunkPosition;
terrainMap[v3Int.x, v3Int.y, v3Int.z].dstToSurface = 0f;
CreateMeshData();
}
public void RemoveTerrain(Vector3 pos)
{
Vector3Int v3Int = new Vector3Int(Mathf.FloorToInt(pos.x), Mathf.FloorToInt(pos.y), Mathf.FloorToInt(pos.z));
v3Int -= chunkPosition;
terrainMap[v3Int.x, v3Int.y, v3Int.z].dstToSurface = 1f;
CreateMeshData();
}
float SampleTerrain(Vector3Int point)
{
return terrainMap[point.x, point.y, point.z].dstToSurface;
}
int VertForIndice(Vector3 vert, Vector3Int point)
{
for (int i = 0; i < vertices.Count; i++)
{
if (vertices[i] == vert)
return i;
}
vertices.Add(vert);
colors.Add((terrainMap[point.x, point.y, point.z].textureID == 1) ? Color.black : Color.red);
return vertices.Count - 1;
}
void ClearMeshData()
{
vertices.Clear();
triangles.Clear();
colors.Clear();
}
void BuildMesh()
{
Mesh mesh = new Mesh();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.colors = colors.ToArray();
mesh.RecalculateNormals();
meshFilter.mesh = mesh;
meshCollider.sharedMesh = mesh;
}
}