zhanmingkan
2022-05-14 0fc43fe898d14895c97427801293edfb3a0c5bf1
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
//the dynamic line from orgin to direction
(function (window) {
    'use strict';
 
    function define_CesiumODline(){
        //Main object
        var CesiumODline = {};
        
        function createFlightLinesApperance(color) {
            var polylineAppearance = new Cesium.PolylineMaterialAppearance({
                material: new Cesium.Material({
                    fabric : {
                        type : 'Color',
                        uniforms : {
                            color : color
                        }
                    },
                    translucent: true
                })
            });
            var renamedVS = Cesium.ShaderSource.replaceMain(polylineAppearance._vertexShaderSource, 'czm_twp_main');
            var twpMain =
                'varying vec4 v_twp; \n' +
                'void main() \n' +
                '{ \n' +
                '    czm_twp_main(); \n' +
                '    v_twp = czm_batchTable_twp(batchId); \n' +
                '}';
            polylineAppearance._vertexShaderSource = renamedVS + '\n' + twpMain;
            var renamedFS = Cesium.ShaderSource.replaceMain(polylineAppearance._fragmentShaderSource, 'czm_twp2_main');
            var twp2Main =
                'varying vec4 v_twp; \n' +
                'void main() \n' +
                '{ \n' +
                '    czm_twp2_main(); \n' +
                '    float t = v_twp.x;' +
                '    t *= 1.03;' +  //控制飞行速度
                '    float alpha = smoothstep(t - 0.055, t, v_st.s) * step(-t, -v_st.s); ' + //控制尾迹长度
                '    alpha += 0.0;' +  //控制轨迹底线的透明度
                '    alpha *= step(-0.1, -abs(0.5-v_st.t));  ' +
                '    gl_FragColor.a *= alpha;' +
                '}';
            polylineAppearance._fragmentShaderSource = renamedFS + '\n' + twp2Main;
            return polylineAppearance;
        }
        
        function createFlightLinesGeometryInstance(positions,width) {
            var gi = new Cesium.GeometryInstance({
                geometry: new Cesium.PolylineGeometry({
                    positions: positions,
                    width: width,
                    vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT
                }),
                attributes : {
                    twp: new Cesium.GeometryInstanceAttribute({
                        componentDatatype : Cesium.ComponentDatatype.FLOAT,
                        componentsPerAttribute : 4,
                        normalize : false,
                        value : [0.0, 0.5, 1.0, 1.0]
                    })
                },
            });
            return gi;
        }
    
        function createFlightLinesPrimitive(routePaths,color,tracewidth) {
            var geometryInstances = [];
            routePaths.forEach(function (route) {
                //线路样式在此分流:连续路径||只有起止点
                var positions = route.positions || createTransmitPolyline(route.startPos, route.endPos);
                var gi = createFlightLinesGeometryInstance(positions,tracewidth);
                geometryInstances.push(gi);
            });
            var flightLinesPrimitive = new Cesium.Primitive({
                geometryInstances: geometryInstances,
                appearance: createFlightLinesApperance(color)
            });
            return flightLinesPrimitive;
        }
        
        var scratchTWP;
        function getScratchTWP() {
            if (typeof scratchTWP === 'undefined') {
                scratchTWP = new Cesium.Cartesian4(0.0, 0.0, 0.0, 0.0);
            }
            return scratchTWP;
        }
        
        function setFlightLinesStatus(flightLinesPrimitive, statusCallback) {
            var scratchTWP = getScratchTWP();
            if (typeof flightLinesPrimitive._batchTable !== 'undefined') {
                //var attributeName = 'color';
                var attributeName = 'twp';
                var attributeIndex = flightLinesPrimitive._batchTableAttributeIndices[attributeName];
                var ni = flightLinesPrimitive._batchTable._numberOfInstances;
                for (var i = 0; i < ni; ++i) {
                    var instanceIndex = i;
                    statusCallback(instanceIndex, scratchTWP);
                    flightLinesPrimitive._batchTable.setBatchedAttribute(instanceIndex, attributeIndex, scratchTWP);
                }
            }
        }
        
        function addGlowLine(viewer,ODLines,color,linewidth,lineglow){
            var glowLines = [];
            ODLines.forEach(function (line) {
                var glowLine = viewer.entities.add({
                    polyline : {
                        positions : line.positions,
                        width : linewidth,
                        material : new Cesium.PolylineGlowMaterialProperty({
                            color : color,
                            glowPower : lineglow
                        })
                    }
                });
                glowLines.push(glowLine);
            });
            return glowLines;
        }
        
        /*Option = {
                viewer: viewer,
                geoJsonUrl: '../data/busLines.json',
                linecolor: Cesium.Color.GOLD,//ORANGE;//ORANGE;//GOLD;//底线颜色
                linewidth: 8.0,//底线宽度
                lineheight: 500.0,//底线高度
                lineglow: 0.15,//底线光晕宽度
                tracecolor: Cesium.Color.WHITE,//ORANGE;//GOLD;//尾迹线颜色
                tracewidth: 28.0,//尾迹线宽度
                positions: [[Cartesian3,Cartesian3,...],[Cartesian3,...],...],//坐标点数组
                timeDuration: 3.0,//各个尾迹线发出的时间间隔
                moveBaseDuration: 3.0,//各个尾迹线的速度,数值越大越慢
        };*/
        
        var jsonresult = [];
        CesiumODline.loadGeoJson = function(option){
            var glowlines;
            var flightLinesPrimitive;
            Cesium.Resource.fetchJson(option.geoJsonUrl).then(function(data) {
                var ODLines = [];
                data.features.map(function (dataArr, idx) {
                    var points = [];
                    var coords = dataArr.geometry.coordinates;
                    for (var i = 0; i < coords.length; i++) {
                        var cart = Cesium.Cartesian3.fromDegrees(
                                coords[i][0], coords[i][1], option.lineheight);
                        points.push(cart);
                    }
                    ODLines.push({
                        positions: points,
                        startTime: option.timeDuration * Math.random(),
                        duration: option.moveBaseDuration + 1.0 * Math.random(),
                    });
                });
                flightLinesPrimitive = createFlightLinesPrimitive(ODLines,
                        option.tracecolor,option.tracewidth);
                option.viewer.scene.primitives.add(flightLinesPrimitive);
                glowlines = addGlowLine(option.viewer,ODLines,option.linecolor,
                        option.linewidth,option.lineglow);
                
                var time = 0;
                var disposeListener = option.viewer.scene.preUpdate.addEventListener(function() {
                    time += 0.01;
                    if (time >= option.timeDuration) {
                        time = 0.0;
                    }
                    setFlightLinesStatus(flightLinesPrimitive, function(instanceIndex, result) {
                        var st = ODLines[instanceIndex].startTime;
                        var dr = ODLines[instanceIndex].duration;
                        var diff = time > st ? time - st : time + option.timeDuration - st;
                        var timeRatio = Math.min(diff / dr, 1.0);
 
                        Cesium.Cartesian4.fromElements(timeRatio, 0.3, 1.0, 1.0, result);
                    });
                });
                jsonresult.push(flightLinesPrimitive,glowlines);
            });
        };
        
        /*Option = {
                viewer: viewer,
                linecolor: Cesium.Color.GOLD,//ORANGE;//ORANGE;//GOLD;//底线颜色
                linewidth: 8.0,//底线宽度
                lineglow: 0.15,//底线光晕宽度
                tracecolor: Cesium.Color.WHITE,//ORANGE;//GOLD;//尾迹线颜色
                tracewidth: 28.0,//尾迹线宽度
                positions: [[Cartesian3,Cartesian3,...],[Cartesian3,...],...],//坐标点数组
                timeDuration: 3.0,//各个尾迹线发出的时间间隔
                moveBaseDuration: 3.0,//各个尾迹线的速度,数值越大越慢
        };*/
        CesiumODline.add = function(option){
            var ODLines = [];
            for ( var i = 0; i < option.positions.length; i++) {
                ODLines.push({
                    positions: option.positions[i],
                    startTime: option.timeDuration * Math.random(),
                    duration: option.moveBaseDuration + 1.0 * Math.random(),
                });
            }
                
            var flightLinesPrimitive = createFlightLinesPrimitive(ODLines,
                    option.tracecolor,option.tracewidth);
            option.viewer.scene.primitives.add(flightLinesPrimitive);
            var glowlines = addGlowLine(option.viewer,ODLines,option.linecolor,
                    option.linewidth,option.lineglow);
            
            var time = 0;
            var disposeListener = option.viewer.scene.preUpdate.addEventListener(function() {
                time += 0.01;
                if (time >= option.timeDuration) {
                    time = 0.0;
                }
                setFlightLinesStatus(flightLinesPrimitive, function(instanceIndex, result) {
                    var st = ODLines[instanceIndex].startTime;
                    var dr = ODLines[instanceIndex].duration;
                    var diff = time > st ? time - st : time + option.timeDuration - st;
                    var timeRatio = Math.min(diff / dr, 1.0);
 
                    Cesium.Cartesian4.fromElements(timeRatio, 0.3, 1.0, 1.0, result);
                });
            });
            
            return [flightLinesPrimitive, glowlines];
        };
        
        CesiumODline.remove = function(viewer, odlines){
            if(odlines.length!=0&&odlines!=undefined){
                viewer.scene.primitives.remove(odlines[0]);
                for ( var i = 0; i < odlines[1].length; i++) {
                    viewer.entities.remove(odlines[1][i]);
                }
            }
        };
        
        CesiumODline.removeJson = function(viewer){
            if(jsonresult.length!=0){
                viewer.scene.primitives.remove(jsonresult[0]);
                for ( var i = 0; i < jsonresult[1].length; i++) {
                    viewer.entities.remove(jsonresult[1][i]);
                }
                jsonresult = [];
            }
        };
        
        return CesiumODline;
    }
    
    if (typeof(CesiumODline) === 'undefined') {
        window.CesiumODline = define_CesiumODline();
    } else {
        console.log("CesiumODline already defined.");
    }
})(window);