exportGL2PLY.js
6.5 KB
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
(function () {
var CREDIT = 'comment ECharts v' + echarts.version + ' http://echarts.baidu.com/\n';
function quantizeArr(out, arr, precision) {
out[0] = Math.round(arr[0] * precision) / precision;
out[1] = Math.round(arr[1] * precision) / precision;
out[2] = Math.round(arr[2] * precision) / precision;
}
/**
* @param {ECharts} echartsInstance
* @param {Object} componentQuery
* @param {string} componentQuery.mainType
* @param {string} [componentQuery.subType]
* @param {number} [componentQuery.index]
* @param {string} [componentQuery.id]
* @param {string} [componentQuery.name]
* @param {Object} [opts]
*/
echarts.exportGL2PLY = function (chartInstance, componentQuery, opts) {
opts = opts || {};
var componentModel = chartInstance.getModel().queryComponents(componentQuery)[0];
if (!componentModel) {
throw new Error('Unkown component.');
}
var coordSys = componentModel.coordinateSystem;
var view = componentQuery.mainType === 'series'
? chartInstance.getViewOfSeriesModel(componentModel)
: chartInstance.getViewOfComponentModel(componentModel);
if (!view.__ecgl__) {
throw new Error('exportGL2PLY only support GL components.');
}
var viewGL = (coordSys && coordSys.viewGL) || view.viewGL;
var plyStr = 'ply\n'
+ 'format ascii 1.0\n'
+ CREDIT;
var headerStr = '';
var vertexStr = [];
var faceStr = [];
var vertexCount = 0;
var triangleCount = 0;
var needsNormal = false;
var needsColor = false;
var needsUv = false;
viewGL.scene.traverse(function (mesh) {
if (mesh.isRenderable() && mesh.geometry.vertexCount) {
var geometry = mesh.geometry;
var colorAttr = geometry.attributes.color;
var normalAttr = geometry.attributes.normal;
var texcoordAttr = geometry.attributes.texcoord0;
var hasUv = !!(texcoordAttr && texcoordAttr.value);
var hasNormal = !!(normalAttr && normalAttr.value);
var hasColor = !!(colorAttr && colorAttr.value);
needsNormal = needsNormal || hasNormal;
needsColor = needsColor || hasColor;
needsUv = needsUv || hasUv;
}
});
viewGL.scene.traverse(function (mesh) {
if (mesh.isRenderable() && mesh.geometry.vertexCount) {
var geometry = mesh.geometry;
var positionAttr = geometry.attributes.position;
var colorAttr = geometry.attributes.color;
var normalAttr = geometry.attributes.normal;
var texcoordAttr = geometry.attributes.texcoord0;
mesh.updateWorldTransform();
var normalMat = mesh.worldTransform.clone().invert().transpose();
var pos = new echarts.graphicGL.Vector3();
var nor = new echarts.graphicGL.Vector3();
var col = [];
var uv = [];
var hasUv = !!(texcoordAttr && texcoordAttr.value);
var hasNormal = !!(normalAttr && normalAttr.value);
var hasColor = !!(colorAttr && colorAttr.value);
var tmp = [];
for (var i = 0; i < geometry.vertexCount; i++) {
positionAttr.get(i, pos.array);
echarts.graphicGL.Vector3.transformMat4(pos, pos, mesh.worldTransform);
// PENDING
quantizeArr(tmp, pos.array, 1e5);
var vItem = tmp.join(' ');
if (needsNormal) {
if (hasNormal) {
normalAttr.get(i, nor.array);
echarts.graphicGL.Vector3.transformMat4(nor, nor, normalMat);
echarts.graphicGL.Vector3.normalize(nor, nor);
quantizeArr(tmp, nor.array, 1e3);
}
else {
tmp[0] = 0;
tmp[1] = 1;
tmp[2] = 0;
}
vItem += ' ' + tmp.join(' ');
}
if (needsColor) {
if (hasColor) {
colorAttr.get(i, col);
quantizeArr(col, col, 1e3);
}
else {
col[0] = 1;
col[1] = 1;
col[1] = 1;
}
vItem += ' ' + Math.round(col[0] * 255) + ' ' + Math.round(col[1] * 255) + ' ' + Math.round(col[2] * 255);
}
if (needsUv) {
if (hasUv) {
texcoordAttr.get(i, uv);
}
else {
uv[0] = 0;
uv[1] = 0;
}
vItem += ' ' + uv.join(' ');
}
vertexStr.push(vItem);
}
var indices = [];
for (var i = 0; i < geometry.triangleCount; i++) {
geometry.getTriangleIndices(i, indices);
// Start from 1
for (var k = 0; k < 3; k++) {
indices[k] += vertexCount;
}
faceStr.push('3 ' + indices.join(' '));
}
vertexCount += geometry.vertexCount;
triangleCount += geometry.triangleCount;
}
});
headerStr += 'element vertex ' + vertexCount + '\n';
headerStr += 'property float x\nproperty float y\nproperty float z\n';
if (needsNormal) {
headerStr += 'property float nx\nproperty float ny\nproperty float nz\n';
}
if (needsColor) {
headerStr += 'property uchar red\nproperty uchar green\nproperty uchar blue\n';
}
if (needsUv) {
headerStr += 'property float s\nproperty float t\n';
}
headerStr += 'element face ' + triangleCount + '\n';
headerStr += 'property list uchar uint vertex_indices\nend_header\n';
plyStr += headerStr;
plyStr += vertexStr.join('\n') + '\n';
plyStr += faceStr.join('\n');
return plyStr;
};
})();