TrailMesh2.js
5.1 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
import * as echarts from 'echarts/lib/echarts';
import graphicGL from '../../util/graphicGL';
import glmatrix from 'claygl/src/dep/glmatrix';
import Lines3DGeometry from '../../util/geometry/Lines3D';
import trail2GLSL from './shader/trail2.glsl.js';
import { getItemVisualColor, getItemVisualOpacity } from '../../util/visual';
var vec3 = glmatrix.vec3;
function sign(a) {
return a > 0 ? 1 : -1;
}
graphicGL.Shader.import(trail2GLSL);
export default graphicGL.Mesh.extend(function () {
var material = new graphicGL.Material({
shader: new graphicGL.Shader(
graphicGL.Shader.source('ecgl.trail2.vertex'),
graphicGL.Shader.source('ecgl.trail2.fragment')
),
transparent: true,
depthMask: false
});
var geometry = new Lines3DGeometry({
dynamic: true
});
geometry.createAttribute('dist', 'float', 1);
geometry.createAttribute('distAll', 'float', 1);
geometry.createAttribute('start', 'float', 1);
return {
geometry: geometry,
material: material,
culling: false,
$ignorePicking: true
};
}, {
updateData: function (data, api, lines3DGeometry) {
var seriesModel = data.hostModel;
var geometry = this.geometry;
var effectModel = seriesModel.getModel('effect');
var size = effectModel.get('trailWidth') * api.getDevicePixelRatio();
var trailLength = effectModel.get('trailLength');
var speed = seriesModel.get('effect.constantSpeed');
var period = seriesModel.get('effect.period') * 1000;
var useConstantSpeed = speed != null;
if (process.env.NODE_ENV !== 'production') {
if (!this.getScene()) {
console.error('TrailMesh must been add to scene before updateData');
}
}
useConstantSpeed
? this.material.set('speed', speed / 1000)
: this.material.set('period', period);
this.material[useConstantSpeed ? 'define' : 'undefine']('vertex', 'CONSTANT_SPEED');
var isPolyline = seriesModel.get('polyline');
geometry.trailLength = trailLength;
this.material.set('trailLength', trailLength);
geometry.resetOffset();
['position', 'positionPrev', 'positionNext'].forEach(function (attrName) {
geometry.attributes[attrName].value = lines3DGeometry.attributes[attrName].value;
});
var extraAttrs = ['dist', 'distAll', 'start', 'offset', 'color'];
extraAttrs.forEach(function (attrName) {
geometry.attributes[attrName].init(geometry.vertexCount);
});
geometry.indices = lines3DGeometry.indices;
var colorArr = [];
var effectColor = effectModel.get('trailColor');
var effectOpacity = effectModel.get('trailOpacity');
var hasEffectColor = effectColor != null;
var hasEffectOpacity = effectOpacity != null;
this.updateWorldTransform();
var xScale = this.worldTransform.x.len();
var yScale = this.worldTransform.y.len();
var zScale = this.worldTransform.z.len();
var vertexOffset = 0;
var maxDistance = 0;
data.each(function (idx) {
var pts = data.getItemLayout(idx);
var opacity = hasEffectOpacity ? effectOpacity : getItemVisualOpacity(data, idx);
var color = getItemVisualColor(data, idx);
if (opacity == null) {
opacity = 1;
}
colorArr = graphicGL.parseColor(hasEffectColor ? effectColor : color, colorArr);
colorArr[3] *= opacity;
var vertexCount = isPolyline
? lines3DGeometry.getPolylineVertexCount(pts)
: lines3DGeometry.getCubicCurveVertexCount(pts[0], pts[1], pts[2], pts[3])
var dist = 0;
var pos = [];
var posPrev = [];
for (var i = vertexOffset; i < vertexOffset + vertexCount; i++) {
geometry.attributes.position.get(i, pos);
pos[0] *= xScale;
pos[1] *= yScale;
pos[2] *= zScale;
if (i > vertexOffset) {
dist += vec3.dist(pos, posPrev);
}
geometry.attributes.dist.set(i, dist);
vec3.copy(posPrev, pos);
}
maxDistance = Math.max(maxDistance, dist);
var randomStart = Math.random() * (useConstantSpeed ? dist : period);
for (var i = vertexOffset; i < vertexOffset + vertexCount; i++) {
geometry.attributes.distAll.set(i, dist);
geometry.attributes.start.set(i, randomStart);
geometry.attributes.offset.set(
i, sign(lines3DGeometry.attributes.offset.get(i)) * size / 2
);
geometry.attributes.color.set(i, colorArr);
}
vertexOffset += vertexCount;
});
this.material.set('spotSize', maxDistance * 0.1 * trailLength);
this.material.set('spotIntensity', effectModel.get('spotIntensity'));
geometry.dirty();
},
setAnimationTime: function (time) {
this.material.set('time', time);
}
});