前言

使用unity完成了obj的简单体素化,可以自定义体素精度。

效果示例

兔子

AK47

算法的大体思路就是把模型缩小到自己设置的体素网格坐标系下,然后计算出每个三角网格面的包围盒,把每个包围盒与自己体素网格相比较,根据值来计算是否应该在这个体素格中生成体素,设置三维bool数组来标记生成的位置。

源码示例:

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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using UnityEngine;
using System.IO;
using System.Collections.Generic;
using UnityEngine.UI;

public class VoxelizeOBJ : MonoBehaviour
{
public string fileName; // OBJ文件名
public int voxelResolution = 32; // 体素分辨率
public GameObject cubePrefab;

public GameObject Creat;

public InputField name;
public InputField pixel;
public Button confire;


void Start()
{
confire.onClick.AddListener(GetNameV);
VoxelizeOBJFile();
}

void VoxelizeOBJFile()
{
name.text = fileName;
pixel.text = voxelResolution.ToString();
// 获取 StreamingAssets 文件夹路径
string streamingAssetsPath = Application.streamingAssetsPath;

// 获取 StreamingAssets 文件夹中的所有文件
string[] files = Directory.GetFiles(streamingAssetsPath);
string firstFile;

if (files.Length > 0)
{
if (fileName != "")
firstFile = Path.Combine(streamingAssetsPath, fileName);
else
{
firstFile = files[0];
name.text = files[0].Replace(streamingAssetsPath + "\\", "");
}
}
else
{
return;
}

// 读取.obj文件的内容
string[] lines = File.ReadAllLines(firstFile);

// 存储顶点数据和面数据
List<Vector3> vertices = new List<Vector3>();
List<int[]> faces = new List<int[]>();

// 解析文件数据
foreach (string line in lines)
{
string[] parts = line.Split(' ');

if (parts[0] == "v")
{
ParseVector(vertices, parts);
}
else if (parts[0] == "f")
{
ParseFace(faces, parts);
}
}

// 进行体素化
bool[,,] voxelGrid = Voxelize(vertices, faces);

// 根据需要进行后续操作,比如可视化或保存体素化结果
InstantiateCubes(voxelGrid);
}

void ParseVector(List<Vector3> list, string[] parts)
{
float x = float.Parse(parts[1]);
float y = float.Parse(parts[2]);
float z = float.Parse(parts[3]);
list.Add(new Vector3(x, y, z));
}

void ParseFace(List<int[]> list, string[] parts)
{
int[] face = new int[parts.Length - 1];
for (int i = 1; i < parts.Length; i++)
{
string[] indices = parts[i].Split('/');
int vertexIndex = int.Parse(indices[0]) - 1;
face[i - 1] = vertexIndex;
}

list.Add(face);
}

bool[,,] Voxelize(List<Vector3> vertices, List<int[]> faces)
{
// 计算边界框
Vector3 minBound = CalculateMinBound(vertices);
Vector3 maxBound = CalculateMaxBound(vertices);

// 计算模型的长宽高
Vector3 modelSize = maxBound - minBound;

// 计算体素网格分辨率
int voxelResolutionX = Mathf.CeilToInt(modelSize.x); // 取模型长宽高中的最大值作为体素网格的分辨率
int voxelResolutionY = Mathf.CeilToInt(modelSize.y);
int voxelResolutionZ = Mathf.CeilToInt(modelSize.z);

int maxV = voxelResolution / Mathf.Max(voxelResolutionX, voxelResolutionY, voxelResolutionZ);

voxelResolutionX *= maxV;
voxelResolutionY *= maxV;
voxelResolutionZ *= maxV;

// 创建体素网格
bool[,,] voxelGrid = new bool[voxelResolutionX, voxelResolutionY, voxelResolutionZ];

// 进行体素化
foreach (int[] face in faces)
{
Vector3 v1 = vertices[face[0]];
Vector3 v2 = vertices[face[1]];
Vector3 v3 = vertices[face[2]];
VoxelizeTriangle(v1, v2, v3, voxelGrid, minBound, modelSize);
}

return voxelGrid;
}

void VoxelizeTriangle(Vector3 v1, Vector3 v2, Vector3 v3, bool[,,] voxelGrid, Vector3 minBound, Vector3 modelSize)
{
// 计算三角形边界框
Vector3 minVertex = Vector3.Min(Vector3.Min(v1, v2), v3);
Vector3 maxVertex = Vector3.Max(Vector3.Max(v1, v2), v3);

int minX = Mathf.Clamp(Mathf.FloorToInt((minVertex.x - minBound.x) / modelSize.x * voxelGrid.GetLength(0)), 0,
voxelGrid.GetLength(0) - 1);
int minY = Mathf.Clamp(Mathf.FloorToInt((minVertex.y - minBound.y) / modelSize.y * voxelGrid.GetLength(1)), 0,
voxelGrid.GetLength(1) - 1);
int minZ = Mathf.Clamp(Mathf.FloorToInt((minVertex.z - minBound.z) / modelSize.z * voxelGrid.GetLength(2)), 0,
voxelGrid.GetLength(2) - 1);
int maxX = Mathf.Clamp(Mathf.CeilToInt((maxVertex.x - minBound.x) / modelSize.x * voxelGrid.GetLength(0)), 0,
voxelGrid.GetLength(0) - 1);
int maxY = Mathf.Clamp(Mathf.CeilToInt((maxVertex.y - minBound.y) / modelSize.y * voxelGrid.GetLength(1)), 0,
voxelGrid.GetLength(1) - 1);
int maxZ = Mathf.Clamp(Mathf.CeilToInt((maxVertex.z - minBound.z) / modelSize.z * voxelGrid.GetLength(2)), 0,
voxelGrid.GetLength(2) - 1);

// 逐个体素进行检查
for (int x = minX; x <= maxX; x++)
{
for (int y = minY; y <= maxY; y++)
{
for (int z = minZ; z <= maxZ; z++)
{
Vector3 p = new Vector3(x + 0.5f, y + 0.5f, z + 0.5f);
if (PointInTriangle(p, v1, v2, v3))
{
voxelGrid[x, y, z] = true;
}
else if (PointOnTriangleBoundary(p.x, p.y, p.z, v1, v2, v3))
{
voxelGrid[x, y, z] = true;
}
}
}
}
}


// 判断点(px, py, pz)是否在三角形的边界上
bool PointOnTriangleBoundary(float px, float py, float pz, Vector3 v1, Vector3 v2, Vector3 v3)
{
bool IsBetween(float a, float b, float c)
{
return Mathf.Min(a, b) <= c && c <= Mathf.Max(a, b);
}

// 计算三角形的边
float edge1 = Vector3.Cross(v1 - v2, v1 - new Vector3(px, py, pz)).magnitude;
float edge2 = Vector3.Cross(v2 - v3, v2 - new Vector3(px, py, pz)).magnitude;
float edge3 = Vector3.Cross(v3 - v1, v3 - new Vector3(px, py, pz)).magnitude;

return IsBetween(0, 1, edge1 / (Vector3.Distance(v1, v2) + Vector3.Distance(v1, new Vector3(px, py, pz)))) &&
IsBetween(0, 1, edge2 / (Vector3.Distance(v2, v3) + Vector3.Distance(v2, new Vector3(px, py, pz)))) &&
IsBetween(0, 1, edge3 / (Vector3.Distance(v3, v1) + Vector3.Distance(v3, new Vector3(px, py, pz))));
}

// 判断点p是否在三角形内部
bool PointInTriangle(Vector3 p, Vector3 v1, Vector3 v2, Vector3 v3)
{
float Sign(Vector3 p1, Vector3 p2, Vector3 p3)
{
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}

bool b1 = Sign(p, v1, v2) < 0.0f;
bool b2 = Sign(p, v2, v3) < 0.0f;
bool b3 = Sign(p, v3, v1) < 0.0f;

return (b1 == b2) && (b2 == b3);
}

Vector3 CalculateMinBound(List<Vector3> vertices)
{
Vector3 minBound = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
foreach (Vector3 vertex in vertices)
{
minBound = Vector3.Min(minBound, vertex);
}

return minBound;
}

Vector3 CalculateMaxBound(List<Vector3> vertices)
{
Vector3 maxBound = new Vector3(float.MinValue, float.MinValue, float.MinValue);
foreach (Vector3 vertex in vertices)
{
maxBound = Vector3.Max(maxBound, vertex);
}

return maxBound;
}

void InstantiateCubes(bool[,,] voxelGrid)
{
// 遍历体素网格,生成cube
for (int x = 0; x < voxelGrid.GetLength(0); x++)
{
for (int y = 0; y < voxelGrid.GetLength(1); y++)
{
for (int z = 0; z < voxelGrid.GetLength(2); z++)
{
if (voxelGrid[x, y, z])
{
// 生成cube
Vector3 position = new Vector3(x + 0.5f, y + 0.5f, z + 0.5f);
GameObject cube = Instantiate(cubePrefab, position, Quaternion.identity);
cube.transform.localScale = Vector3.one;
cube.transform.SetParent(Creat.transform);
}
}
}
}
}

private void GetNameV()
{
if (name.text == "")
{
foreach (Transform child in Creat.transform)
{
Destroy(child.gameObject);
}

return;
}

fileName = name.text;
if (pixel.text != "")
voxelResolution = int.Parse(pixel.text);
// 删除所有子对象
foreach (Transform child in Creat.transform)
{
Destroy(child.gameObject);
}

VoxelizeOBJFile();
}
}