(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)

关于Draco压缩算法

Draco是一个开源的Lib库,提供3D图形mesh和点云的压缩和解压。它的目标是提升3D图形的存储和传输效率。

Draco is an open-source library for compressing and decompressing 3D geometric meshes and point clouds. It is intended to improve the storage and transmission of 3D graphics.https://google.github.io/draco/

git路径: https://github.com/google/draco

简单应用

Draco提供了针对性mesh压缩的算法,可以提供比gzip压缩更有针对性的压缩。

缺省支持了OBJ和PLY文件类型的压缩,其它格式的文件需要通过程序进行封装,并调用接口库进行压缩解压使用。

Draco提供的程序库文件有:

draco.lib

dracodec.lib

dracoenc.lib

draco_decoder.exe

draco_encoder.exe

如果输出的是obj/ply文件,

服务端采用压缩程序draco_encoder.exe对文件进行预压缩,当客户端请求时,向web客户端传输压缩后的文件。

web客户端侧,调用Draco提供的js解压代码,对obj文件解压,并渲染展示。

定制应用

对于非obj格式,需要写相关的格式压缩处理,调用接口:

包含头文件:

1
2
3
4
5
6


#include "draco/compression/encode.h"


#include "draco/core/encoder_buffer.h"

包装接口调用:

 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


// init encoder


draco::Encoder encoder;


encoder.SetSpeedOptions(7, 7);


encoder.SetAttributeQuantization(draco::GeometryAttribute::POSITION, 14);


encoder.SetAttributeQuantization(draco::GeometryAttribute::COLOR, 12);


encoder.SetAttributeQuantization(draco::GeometryAttribute::NORMAL, 10);


encoder.SetAttributeQuantization(draco::GeometryAttribute::TEX_COORD, 12);


encoder.SetAttributeQuantization(draco::GeometryAttribute::GENERIC, 12);





// init mesh


draco::Mesh mesh;


... put mesh info into draco mesh





// encode mesh to buffer


draco::EncoderBuffer buffer;


const draco::Status status = encoder.EncodeMeshToBuffer(mesh, &buffer);


// write buffer to file


...


out_file.write(buffer.data(), buffer.size())

c. mesh加载方法

参考Draco源码中 ObjDecoder::DecodeInternal 中的实现

 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


// 1. 设置三角形


	mesh->SetNumFaces


	// 逐个设置三角形的index值


	mesh->SetFace


// 2. 设置点的数量


	point->set_num_points


// 3. 定义VBO相关属性


	// 定义attribute, 把vertex/texcoor/normal/color放入attribute


	GeometryAttribute geoAttr;


	geoAttr.Init(GeometryAttribute::POSITION, nullptr, ...)


	int attrid = mesh->AddAttribute(geoAttr...)


	mesh.attribute(attrid)-SetAttributeValue(draco::AttributeValueIndex(i), value)

使用注意

注意:

Draco压缩算法对float数值有一定的要求, 要求是常规数字(Nan, Infinite都是不可接受的), 可以使用std::isnormal判定.(isnormal判定会识别Nan, infinite, 0, 0<abs(x)<mindouble)

Draco压缩算法会需要对shader进行修改

Draco压缩算法是有损压缩算法, 注意相关的压缩比设置

Draco实时解压会占用cpu, 需要关注改点

使用Draco的好处:

减少空间的占用, 减少网络传输量,该优点是它的核心好处

Draco的解压算法,网页端提供了wasm的解压算法,一定程度的降低了网页端解压消耗

使用Draco的缺憾:

可能性能提升也并没有期望中那么大,这个跟数据源的特点有关,不能得到保证。