Skip to content

kotlin voxel解析 三

Published:

上一篇中,我们得到了vox转换后的顶底数组和索引数组,可以将其保存成ply并使用3D查看器查看效果

ply格式就简单很多了,在header中定义顶点属性,然后放入顶点数组和索引数组就行了

		val header = StringBuilder()
        header
            .appendLine("ply")
            .appendLine("format ascii 1.0")
            .appendLine("element vertex ${vertices.size / 10}")
            .appendLine("property float x")
            .appendLine("property float y")
            .appendLine("property float z")
            .appendLine("property float nx")
            .appendLine("property float ny")
            .appendLine("property float nz")
            .appendLine("property uchar red")
            .appendLine("property uchar green")
            .appendLine("property uchar blue")
            .appendLine("property uchar alpha")
            .appendLine("element face ${indices.size / 3}")
            .appendLine("property list uchar int vertex_indices")
            .appendLine("end_header")
        for (p in vertices.indices step 10) {
            header.append(vertices[p]).append(' ')
                .append(vertices[p + 1]).append(' ')
                .append(vertices[p + 2]).append(' ')
                .append(vertices[p + 3]).append(' ')
                .append(vertices[p + 4]).append(' ')
                .append(vertices[p + 5]).append(' ')
                .append((vertices[p + 6]*255f).roundToInt()).append(' ')
                .append((vertices[p + 7]*255f).roundToInt()).append(' ')
                .append((vertices[p + 8]*255f).roundToInt()).append(' ')
                .append((vertices[p + 9]*255f).roundToInt()).appendLine()
        }
        for (p in indices.indices step 3) {
            header.append(3).append(' ').append(indices[p]).append(' ').append(indices[p + 1]).append(' ')
                .append(indices[p + 2]).appendLine()
        }

ply还有个二进制格式

		val header = StringBuilder()
        header
            .appendLine("ply")
            .appendLine("format binary_little_endian 1.0")
            .appendLine("element vertex ${vertices.size / 10}")
            .appendLine("property float x")
            .appendLine("property float y")
            .appendLine("property float z")
            .appendLine("property float nx")
            .appendLine("property float ny")
            .appendLine("property float nz")
            .appendLine("property uchar red")
            .appendLine("property uchar green")
            .appendLine("property uchar blue")
            .appendLine("property uchar alpha")
            .appendLine("element face ${indices.size / 3}")
            .appendLine("property list uchar int vertex_indices")
            .appendLine("end_header")
        val buffheader = header.toString().encodeToByteArray()
        val buffsize = (28 * vertices.size / 10) + (13 * indices.size / 3)
        val buff = ByteArray(buffheader.size+buffsize)
        buffheader.copyInto(buff)
        var index = buffheader.size
        for (p in vertices.indices step 10) {
            buff[index] = vertices[p]
            index += 4
            buff[index] = vertices[p+1]
            index += 4
            buff[index] = vertices[p+2]
            index += 4
            buff[index] = vertices[p+3]
            index += 4
            buff[index] = vertices[p+4]
            index += 4
            buff[index] = vertices[p+5]
            index += 4

            buff[index] = (vertices[p+6]*255f).roundToInt().toByte()
            index ++
            buff[index] = (vertices[p+7]*255f).roundToInt().toByte()
            index ++
            buff[index] = (vertices[p+8]*255f).roundToInt().toByte()
            index ++
            buff[index] = (vertices[p+9]*255f).roundToInt().toByte()
            index ++
        }
        for (p in indices.indices step 3) {
            buff[index] = (3).toByte()
            index++
            buff[index] = indices[p]
            index+=4
            buff[index] = indices[p+1]
            index+=4
            buff[index] = indices[p+2]
            index+=4
        }

嗯,看看转换后的效果

image-20240925171718184

13万个三角形数有些夸张了,而且生成的ply文件足足有4MB大小,原始的vox只有544KB,可以看到有很多重复使用的顶点,下一章对其进行优化

完整代码在github


Previous Post
kotlin voxel解析 四
Next Post
kotlin voxel解析 二