Bars3DGeometry.js
14.3 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* Geometry collecting bars data
*
* @module echarts-gl/chart/bars/BarsGeometry
* @author Yi Shen(http://github.com/pissang)
*/
import * as echarts from 'echarts/lib/echarts';
import dynamicConvertMixin from './dynamicConvertMixin';
import trianglesSortMixin from './trianglesSortMixin';
import Geometry from 'claygl/src/Geometry';
import glMatrix from 'claygl/src/dep/glmatrix';
var vec3 = glMatrix.vec3;
var mat3 = glMatrix.mat3;
/**
* @constructor
* @alias module:echarts-gl/chart/bars/BarsGeometry
* @extends clay.Geometry
*/
var BarsGeometry = Geometry.extend(function () {
return {
attributes: {
position: new Geometry.Attribute('position', 'float', 3, 'POSITION'),
normal: new Geometry.Attribute('normal', 'float', 3, 'NORMAL'),
color: new Geometry.Attribute('color', 'float', 4, 'COLOR'),
prevPosition: new Geometry.Attribute('prevPosition', 'float', 3),
prevNormal: new Geometry.Attribute('prevNormal', 'float', 3)
},
dynamic: true,
enableNormal: false,
bevelSize: 1,
bevelSegments: 0,
// Map from vertexIndex to dataIndex.
_dataIndices: null,
_vertexOffset: 0,
_triangleOffset: 0
};
},
/** @lends module:echarts-gl/chart/bars/BarsGeometry.prototype */
{
resetOffset: function () {
this._vertexOffset = 0;
this._triangleOffset = 0;
},
setBarCount: function (barCount) {
var enableNormal = this.enableNormal;
var vertexCount = this.getBarVertexCount() * barCount;
var triangleCount = this.getBarTriangleCount() * barCount;
if (this.vertexCount !== vertexCount) {
this.attributes.position.init(vertexCount);
if (enableNormal) {
this.attributes.normal.init(vertexCount);
}
else {
this.attributes.normal.value = null;
}
this.attributes.color.init(vertexCount);
}
if (this.triangleCount !== triangleCount) {
this.indices = vertexCount > 0xffff ? new Uint32Array(triangleCount * 3) : new Uint16Array(triangleCount * 3);
this._dataIndices = new Uint32Array(vertexCount);
}
},
getBarVertexCount: function () {
var bevelSegments = this.bevelSize > 0 ? this.bevelSegments : 0;
return bevelSegments > 0 ? this._getBevelBarVertexCount(bevelSegments)
: (this.enableNormal ? 24 : 8);
},
getBarTriangleCount: function () {
var bevelSegments = this.bevelSize > 0 ? this.bevelSegments : 0;
return bevelSegments > 0 ? this._getBevelBarTriangleCount(bevelSegments)
: 12;
},
_getBevelBarVertexCount: function (bevelSegments) {
return (bevelSegments + 1) * 4 * (bevelSegments + 1) * 2;
},
_getBevelBarTriangleCount: function (bevelSegments) {
var widthSegments = bevelSegments * 4 + 3;
var heightSegments = bevelSegments * 2 + 1;
return (widthSegments + 1) * heightSegments * 2 + 4;
},
setColor: function (idx, color) {
var vertexCount = this.getBarVertexCount();
var start = vertexCount * idx;
var end = vertexCount * (idx + 1);
for (var i = start; i < end; i++) {
this.attributes.color.set(i, color);
}
this.dirtyAttribute('color');
},
/**
* Get dataIndex of vertex.
* @param {number} vertexIndex
*/
getDataIndexOfVertex: function (vertexIndex) {
return this._dataIndices ? this._dataIndices[vertexIndex] : null;
},
/**
* Add a bar
* @param {Array.<number>} start
* @param {Array.<number>} end
* @param {Array.<number>} orient right direction
* @param {Array.<number>} size size on x and z
* @param {Array.<number>} color
*/
addBar: (function () {
var v3Create = vec3.create;
var v3ScaleAndAdd = vec3.scaleAndAdd;
var end = v3Create();
var px = v3Create();
var py = v3Create();
var pz = v3Create();
var nx = v3Create();
var ny = v3Create();
var nz = v3Create();
var pts = [];
var normals = [];
for (var i = 0; i < 8; i++) {
pts[i] = v3Create();
}
var cubeFaces4 = [
// PX
[0, 1, 5, 4],
// NX
[2, 3, 7, 6],
// PY
[4, 5, 6, 7],
// NY
[3, 2, 1, 0],
// PZ
[0, 4, 7, 3],
// NZ
[1, 2, 6, 5]
];
var face4To3 = [
0, 1, 2, 0, 2, 3
];
var cubeFaces3 = [];
for (var i = 0; i < cubeFaces4.length; i++) {
var face4 = cubeFaces4[i];
for (var j = 0; j < 2; j++) {
var face = [];
for (var k = 0; k < 3; k++) {
face.push(face4[face4To3[j * 3 + k]]);
}
cubeFaces3.push(face);
}
}
return function (start, dir, leftDir, size, color, dataIndex) {
// Use vertex, triangle maybe sorted.
var startVertex = this._vertexOffset;
if (this.bevelSize > 0 && this.bevelSegments > 0) {
this._addBevelBar(start, dir, leftDir, size, this.bevelSize, this.bevelSegments, color);
}
else {
vec3.copy(py, dir);
vec3.normalize(py, py);
// x * y => z
vec3.cross(pz, leftDir, py);
vec3.normalize(pz, pz);
// y * z => x
vec3.cross(px, py, pz);
vec3.normalize(pz, pz);
vec3.negate(nx, px);
vec3.negate(ny, py);
vec3.negate(nz, pz);
v3ScaleAndAdd(pts[0], start, px, size[0] / 2);
v3ScaleAndAdd(pts[0], pts[0], pz, size[2] / 2);
v3ScaleAndAdd(pts[1], start, px, size[0] / 2);
v3ScaleAndAdd(pts[1], pts[1], nz, size[2] / 2);
v3ScaleAndAdd(pts[2], start, nx, size[0] / 2);
v3ScaleAndAdd(pts[2], pts[2], nz, size[2] / 2);
v3ScaleAndAdd(pts[3], start, nx, size[0] / 2);
v3ScaleAndAdd(pts[3], pts[3], pz, size[2] / 2);
v3ScaleAndAdd(end, start, py, size[1]);
v3ScaleAndAdd(pts[4], end, px, size[0] / 2);
v3ScaleAndAdd(pts[4], pts[4], pz, size[2] / 2);
v3ScaleAndAdd(pts[5], end, px, size[0] / 2);
v3ScaleAndAdd(pts[5], pts[5], nz, size[2] / 2);
v3ScaleAndAdd(pts[6], end, nx, size[0] / 2);
v3ScaleAndAdd(pts[6], pts[6], nz, size[2] / 2);
v3ScaleAndAdd(pts[7], end, nx, size[0] / 2);
v3ScaleAndAdd(pts[7], pts[7], pz, size[2] / 2);
var attributes = this.attributes;
if (this.enableNormal) {
normals[0] = px;
normals[1] = nx;
normals[2] = py;
normals[3] = ny;
normals[4] = pz;
normals[5] = nz;
var vertexOffset = this._vertexOffset;
for (var i = 0; i < cubeFaces4.length; i++) {
var idx3 = this._triangleOffset * 3;
for (var k = 0; k < 6; k++) {
this.indices[idx3++] = vertexOffset + face4To3[k];
}
vertexOffset += 4;
this._triangleOffset += 2;
}
for (var i = 0; i < cubeFaces4.length; i++) {
var normal = normals[i];
for (var k = 0; k < 4; k++) {
var idx = cubeFaces4[i][k];
attributes.position.set(this._vertexOffset, pts[idx]);
attributes.normal.set(this._vertexOffset, normal);
attributes.color.set(this._vertexOffset++, color);
}
}
}
else {
for (var i = 0; i < cubeFaces3.length; i++) {
var idx3 = this._triangleOffset * 3;
for (var k = 0; k < 3; k++) {
this.indices[idx3 + k] = cubeFaces3[i][k] + this._vertexOffset;
}
this._triangleOffset++;
}
for (var i = 0; i < pts.length; i++) {
attributes.position.set(this._vertexOffset, pts[i]);
attributes.color.set(this._vertexOffset++, color);
}
}
}
var endVerex = this._vertexOffset;
for (var i = startVertex; i < endVerex; i++) {
this._dataIndices[i] = dataIndex;
}
};
})(),
/**
* Add a bar with bevel
* @param {Array.<number>} start
* @param {Array.<number>} end
* @param {Array.<number>} orient right direction
* @param {Array.<number>} size size on x and z
* @param {number} bevelSize
* @param {number} bevelSegments
* @param {Array.<number>} color
*/
_addBevelBar: (function () {
var px = vec3.create();
var py = vec3.create();
var pz = vec3.create();
var rotateMat = mat3.create();
var bevelStartSize = [];
var xOffsets = [1, -1, -1, 1];
var zOffsets = [1, 1, -1, -1];
var yOffsets = [2, 0];
return function (start, dir, leftDir, size, bevelSize, bevelSegments, color) {
vec3.copy(py, dir);
vec3.normalize(py, py);
// x * y => z
vec3.cross(pz, leftDir, py);
vec3.normalize(pz, pz);
// y * z => x
vec3.cross(px, py, pz);
vec3.normalize(pz, pz);
rotateMat[0] = px[0]; rotateMat[1] = px[1]; rotateMat[2] = px[2];
rotateMat[3] = py[0]; rotateMat[4] = py[1]; rotateMat[5] = py[2];
rotateMat[6] = pz[0]; rotateMat[7] = pz[1]; rotateMat[8] = pz[2];
bevelSize = Math.min(size[0], size[2]) / 2 * bevelSize;
for (var i = 0; i < 3; i++) {
bevelStartSize[i] = Math.max(size[i] - bevelSize * 2, 0);
}
var rx = (size[0] - bevelStartSize[0]) / 2;
var ry = (size[1] - bevelStartSize[1]) / 2;
var rz = (size[2] - bevelStartSize[2]) / 2;
var pos = [];
var normal = [];
var vertexOffset = this._vertexOffset;
var endIndices = [];
for (var i = 0; i < 2; i++) {
endIndices[i] = endIndices[i] = [];
for (var m = 0; m <= bevelSegments; m++) {
for (var j = 0; j < 4; j++) {
if ((m === 0 && i === 0) || (i === 1 && m === bevelSegments)) {
endIndices[i].push(vertexOffset);
}
for (var n = 0; n <= bevelSegments; n++) {
var phi = n / bevelSegments * Math.PI / 2 + Math.PI / 2 * j;
var theta = m / bevelSegments * Math.PI / 2 + Math.PI / 2 * i;
// var r = rx < ry ? (rz < rx ? rz : rx) : (rz < ry ? rz : ry);
normal[0] = rx * Math.cos(phi) * Math.sin(theta);
normal[1] = ry * Math.cos(theta);
normal[2] = rz * Math.sin(phi) * Math.sin(theta);
pos[0] = normal[0] + xOffsets[j] * bevelStartSize[0] / 2;
pos[1] = (normal[1] + ry) + yOffsets[i] * bevelStartSize[1] / 2;
pos[2] = normal[2] + zOffsets[j] * bevelStartSize[2] / 2;
// Normal is not right if rx, ry, rz not equal.
if (!(Math.abs(rx - ry) < 1e-6 && Math.abs(ry - rz) < 1e-6)) {
normal[0] /= rx * rx;
normal[1] /= ry * ry;
normal[2] /= rz * rz;
}
vec3.normalize(normal, normal);
vec3.transformMat3(pos, pos, rotateMat);
vec3.transformMat3(normal, normal, rotateMat);
vec3.add(pos, pos, start);
this.attributes.position.set(vertexOffset, pos);
if (this.enableNormal) {
this.attributes.normal.set(vertexOffset, normal);
}
this.attributes.color.set(vertexOffset, color);
vertexOffset++;
}
}
}
}
var widthSegments = bevelSegments * 4 + 3;
var heightSegments = bevelSegments * 2 + 1;
var len = widthSegments + 1;
for (var j = 0; j < heightSegments; j ++) {
for (var i = 0; i <= widthSegments; i ++) {
var i2 = j * len + i + this._vertexOffset;
var i1 = (j * len + (i + 1) % len) + this._vertexOffset;
var i4 = (j + 1) * len + (i + 1) % len + this._vertexOffset;
var i3 = (j + 1) * len + i + this._vertexOffset;
this.setTriangleIndices(this._triangleOffset++, [i4, i2, i1]);
this.setTriangleIndices(this._triangleOffset++, [i4, i3, i2]);
}
}
// Close top and bottom
this.setTriangleIndices(this._triangleOffset++, [endIndices[0][0], endIndices[0][2], endIndices[0][1]]);
this.setTriangleIndices(this._triangleOffset++, [endIndices[0][0], endIndices[0][3], endIndices[0][2]]);
this.setTriangleIndices(this._triangleOffset++, [endIndices[1][0], endIndices[1][1], endIndices[1][2]]);
this.setTriangleIndices(this._triangleOffset++, [endIndices[1][0], endIndices[1][2], endIndices[1][3]]);
this._vertexOffset = vertexOffset;
};
})()
});
echarts.util.defaults(BarsGeometry.prototype, dynamicConvertMixin);
echarts.util.defaults(BarsGeometry.prototype, trianglesSortMixin);
export default BarsGeometry;