2个版本
0.1.1 | 2019年7月5日 |
---|---|
0.1.0 | 2019年7月5日 |
#80 in 数据格式
用于 renderling
2MB
660 行
生成形状后,将它们序列化到文件中,该文件包含顶点列表(positions
)和三角形面列表(cells
),该列表索引顶点列表。适合输入到 Three.js的BufferGeometry 或 regl。
二十面体的生成速度比JavaScript中的Three.js版本(我将它几乎全部复制到Rust中)要快得多。
截断的二十面体(我称之为六角球)生成速度稍慢,因为它们是通过生成二十面体然后将每个点截断为六边形和五边形面来制作的。
程序还可以通过为每个顶点分配颜色来为每个面添加颜色,但这会以在基础模型中重复共享顶点为代价,以便每个面都有唯一的顶点集,从而大大增加了网格的大小。
在WebGL中渲染细节级别为5及以上的六角球和细节级别为7及以上的二十面体时,请确保启用 OES_element_index_uint
扩展,因为顶点数可能会超出正常int索引。
屏幕截图
要生成一个详细的六角球,它从基本的二十面体开始
将其边细分为一个更详细的二十面体
然后,将二十面体中的每个点截断为六边形和12个五边形
达到任何细节级别
安装
安装,可以运行以下命令之一:cargo install icosahedron
或检出仓库并运行 cargo build --release
。
用法
使用以下选项运行它
icosahedron 0.1.1
Tyler Hallada <tyler@hallada.net>
Generates 3D icosahedra meshes
USAGE:
icosahedron [FLAGS] [OPTIONS] [OUTPUT]
FLAGS:
-c, --colored Assigns a random color to every face (increases vertices count).
-h, --help Prints help information
-t, --truncated Generate truncated icosahedra (hexspheres).
-V, --version Prints version information
OPTIONS:
-d, --detail <detail> Maximum detail level to generate. Each level multiplies the number of triangles by 4.
[default: 7]
-f, --format <format> Format to write the files in. [default: Bin] [possible values: Json, Bin]
-r, --radius <radius> Radius of the polyhedron, [default: 1.0]
ARGS:
<OUTPUT> Directory to write the output files to. [default: output/]
输出格式
输出为JSON或自定义二进制格式。二进制格式(全部为小端)布局如下
- 1 32位无符号整数指定顶点数(
V
) - 1 32位无符号整数指定三角形数(
T
) - 每个顶点的 x、y 和 z 坐标使用
V
* 3 个 32 位浮点数 - 每个顶点的法线使用
V
* 3 个 32 位浮点数 - 每个顶点的颜色使用
V
* 3 个 32 位浮点数 - 每个三角形使用
T
* 3 个 32 位无符号整数,这 3 个整数是对顶点数组的索引
JavaScript 中读取二进制格式的示例
fetch(binaryFile)
.then(response => response.arrayBuffer())
.then(buffer => {
let reader = new DataView(buffer);
let numVertices = reader.getUint32(0, true);
let numCells = reader.getUint32(4, true);
let shape = {
positions: new Float32Array(buffer, 8, numVertices * 3),
normals: new Float32Array(buffer, numVertices * 12 + 8, numVertices * 3),
colors: new Float32Array(buffer, numVertices * 24 + 8, numVertices * 3),
cells: new Uint32Array(buffer, numVertices * 36 + 8, numCells * 3),
})
依赖项
~2.3–3.5MB
~60K SLoC