-
Notifications
You must be signed in to change notification settings - Fork 3
/
VoxelMesh.cs
245 lines (211 loc) · 6.14 KB
/
VoxelMesh.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class VoxelMesh
{
Mesh mesh;
Material mat;
List<Vector3> Points;
List<Vector3> Verts;
List<int> Tris;
List<Vector2> UVs;
float size = 0.5f;
public GameObject gameObject;
public VoxelMesh(Vector3 pos, byte type)
{
gameObject = new GameObject();
gameObject.transform.position = pos;
Points = new List<Vector3>();
Points.Add(new Vector3(-size, size,-size));
Points.Add(new Vector3( size, size,-size));
Points.Add(new Vector3( size,-size,-size));
Points.Add(new Vector3(-size,-size,-size));
Points.Add(new Vector3( size, size, size));
Points.Add(new Vector3(-size, size, size));
Points.Add(new Vector3(-size,-size, size));
Points.Add(new Vector3( size,-size, size));
Verts = new List<Vector3>();
Tris = new List<int>();
UVs = new List<Vector2>();
CreateMesh(type);
}
private void CreateMesh(byte type)
{
gameObject.AddComponent("MeshFilter");
gameObject.AddComponent("MeshRenderer");
gameObject.AddComponent("MeshCollider");
switch(type)
{
case 0:
mat = Resources.Load("Materials/Air") as Material;
break;
case 1:
mat = Resources.Load("Materials/Dirt") as Material;
break;
case 2:
mat = Resources.Load("Materials/Stone") as Material;
break;
default:
Debug.LogWarning("Type not available, using default material.");
mat = Resources.Load("Materials/Default") as Material;
break;
}
if(mat == null)
{
Debug.LogError("Material not found!");
return;
}
MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
if(meshFilter == null)
{
Debug.LogError("MeshFilter not found!");
return;
}
mesh = meshFilter.sharedMesh;
if(mesh == null)
{
meshFilter.mesh = new Mesh();
mesh = meshFilter.sharedMesh;
}
MeshCollider meshCollider = gameObject.GetComponent<MeshCollider>();
if(meshCollider == null)
{
Debug.LogError("MeshCollider not found!");
return;
}
mesh.Clear();
UpdateMesh();
}
private void UpdateMesh()
{
// Front plane
Verts.Add(Points[0]); Verts.Add(Points[1]); Verts.Add(Points[2]); Verts.Add(Points[3]);
// Back plane
Verts.Add(Points[4]); Verts.Add(Points[5]); Verts.Add(Points[6]); Verts.Add(Points[7]);
// Left plane
Verts.Add(Points[5]); Verts.Add(Points[0]); Verts.Add(Points[3]); Verts.Add(Points[6]);
// Right plane
Verts.Add(Points[1]); Verts.Add(Points[4]); Verts.Add(Points[7]); Verts.Add(Points[2]);
// Top plane
Verts.Add(Points[5]); Verts.Add(Points[4]); Verts.Add(Points[1]); Verts.Add(Points[0]);
// Bottom plane
Verts.Add(Points[3]); Verts.Add(Points[2]); Verts.Add(Points[7]); Verts.Add(Points[6]);
// Front Plane
Tris.Add(0); Tris.Add(1); Tris.Add(2);
Tris.Add(2); Tris.Add(3); Tris.Add(0);
// Back Plane
Tris.Add(4); Tris.Add(5); Tris.Add(6);
Tris.Add(6); Tris.Add(7); Tris.Add(4);
// Left Plane
Tris.Add(8); Tris.Add(9); Tris.Add(10);
Tris.Add(10); Tris.Add(11); Tris.Add(8);
// Right Plane
Tris.Add(12); Tris.Add(13); Tris.Add(14);
Tris.Add(14); Tris.Add(15); Tris.Add(12);
// Top Plane
Tris.Add(16); Tris.Add(17); Tris.Add(18);
Tris.Add(18); Tris.Add(19); Tris.Add(16);
// Bottom Plane
Tris.Add(20); Tris.Add(21); Tris.Add(22);
Tris.Add(22); Tris.Add(23); Tris.Add(20);
// Front Plane
UVs.Add(new Vector2(0,1));
UVs.Add(new Vector2(1,1));
UVs.Add(new Vector2(1,0));
UVs.Add(new Vector2(0,0));
// Back Plane
UVs.Add(new Vector2(0,1));
UVs.Add(new Vector2(1,1));
UVs.Add(new Vector2(1,0));
UVs.Add(new Vector2(0,0));
// Left Plane
UVs.Add(new Vector2(0,1));
UVs.Add(new Vector2(1,1));
UVs.Add(new Vector2(1,0));
UVs.Add(new Vector2(0,0));
// Right Plane
UVs.Add(new Vector2(0,1));
UVs.Add(new Vector2(1,1));
UVs.Add(new Vector2(1,0));
UVs.Add(new Vector2(0,0));
// Top Plane
UVs.Add(new Vector2(0,1));
UVs.Add(new Vector2(1,1));
UVs.Add(new Vector2(1,0));
UVs.Add(new Vector2(0,0));
// Bottom Plane
UVs.Add(new Vector2(0,1));
UVs.Add(new Vector2(1,1));
UVs.Add(new Vector2(1,0));
UVs.Add(new Vector2(0,0));
mesh.vertices = Verts.ToArray();
mesh.triangles = Tris.ToArray();
mesh.uv = UVs.ToArray();
Verts.Clear();
Tris.Clear();
UVs.Clear();
MeshCollider meshCollider = gameObject.GetComponent<MeshCollider>();
mesh.RecalculateNormals();
mesh.RecalculateBounds();
RecalculateTangents(mesh);
meshCollider.sharedMesh = null;
meshCollider.sharedMesh = mesh;
gameObject.renderer.material = mat;
mesh.Optimize();
}
private static void RecalculateTangents(Mesh mesh)
{
int[] triangles = mesh.triangles;
Vector3[] vertices = mesh.vertices;
Vector2[] uv = mesh.uv;
Vector3[] normals = mesh.normals;
int triangleCount = triangles.Length;
int vertexCount = vertices.Length;
Vector3[] tan1 = new Vector3[vertexCount];
Vector3[] tan2 = new Vector3[vertexCount];
Vector4[] tangents = new Vector4[vertexCount];
for(long a = 0; a < triangleCount; a += 3)
{
long i1 = triangles[a];
long i2 = triangles[a + 1];
long i3 = triangles[a + 2];
Vector3 v1 = vertices[i1];
Vector3 v2 = vertices[i2];
Vector3 v3 = vertices[i3];
Vector2 w1 = uv[i1];
Vector2 w2 = uv[i2];
Vector2 w3 = uv[i3];
float x1 = v2.x - v1.x;
float x2 = v3.x - v1.x;
float y1 = v2.y - v1.y;
float y2 = v3.y - v1.y;
float z1 = v2.z - v1.z;
float z2 = v3.z - v1.z;
float s1 = w2.x = w1.x;
float s2 = w3.x = w1.x;
float t1 = w2.y = w1.y;
float t2 = w3.y = w1.y;
float div = s1 * t2 - s2 * t1;
float r = div == 0.0f ? 0.0f : 1.0f / div;
Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
tan1[i1] += sdir;
tan1[i2] += sdir;
tan1[i3] += sdir;
tan2[i1] += tdir;
tan2[i2] += tdir;
tan2[i3] += tdir;
}
for(long a = 0; a < vertexCount; ++a)
{
Vector3 n = normals[a];
Vector3 t = tan1[a];
Vector3.OrthoNormalize(ref n, ref t);
tangents[a].x = t.x;
tangents[a].y = t.y;
tangents[a].z = t.z;
tangents[a].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
}
mesh.tangents = tangents;
}
}