ansel0926
2022-05-14 cc021a0544a8c0b2a056cec3168da119515a8e38
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
(function(window) {
    'use strict';
 
    function define_CesiumCurvedTrail() {
        //Main object
        var CesiumCurvedTrail = {};
 
        /*
         流纹纹理线
        color 颜色
        duration 持续时间 毫秒
        */
        function PolylineTrailLinkMaterialProperty(color, duration) {
            this._definitionChanged = new Cesium.Event();
            this._color = undefined;
            this._colorSubscription = undefined;
            this.color = color;
            this.duration = duration;
            this._time = (new Date()).getTime();
        }
        Cesium.defineProperties(PolylineTrailLinkMaterialProperty.prototype, {
            isConstant : {
                get : function() {
                    return false;
                }
            },
            definitionChanged : {
                get : function() {
                    return this._definitionChanged;
                }
            },
            color : Cesium.createPropertyDescriptor('color')
        });
        PolylineTrailLinkMaterialProperty.prototype.getType = function(time) {
            return 'PolylineTrailLink';
        }
        PolylineTrailLinkMaterialProperty.prototype.getValue = function(time, result) {
            if (!Cesium.defined(result)) {
                result = {};
            }
            result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
            result.image = Cesium.Material.PolylineTrailLinkImage;
            result.time = (((new Date()).getTime() - this._time) % this.duration) / this.duration;
            return result;
        }
        PolylineTrailLinkMaterialProperty.prototype.equals = function(other) {
            return this === other ||
                (other instanceof PolylineTrailLinkMaterialProperty &&
                Cesium.Property.equals(this._color, other._color))
        }
        Cesium.PolylineTrailLinkMaterialProperty = PolylineTrailLinkMaterialProperty;
        Cesium.Material.PolylineTrailLinkType = 'PolylineTrailLink';
        Cesium.Material.PolylineTrailLinkImage = "../images/color1.png";
        Cesium.Material.PolylineTrailLinkSource = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
                                                     {\n\
                                                          czm_material material = czm_getDefaultMaterial(materialInput);\n\
                                                          vec2 st = materialInput.st;\n\
                                                          vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));\n\
                                                          material.alpha = colorImage.a * color.a;\n\
                                                          material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n\
                                                          return material;\n\
                                                      }";
        Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailLinkType, {
            fabric : {
                type : Cesium.Material.PolylineTrailLinkType,
                uniforms : {
                    color : new Cesium.Color(1.0, 0.0, 0.0, 0.5),
                    image : Cesium.Material.PolylineTrailLinkImage,
                    time : 0
                },
                source : Cesium.Material.PolylineTrailLinkSource
            },
            translucent : function(material) {
                return true;
            }
        });
 
        function parabolaEquation(options, resultOut) {
            //方程 y=-(4h/L^2)*x^2+h h:顶点高度 L:横纵间距较大者
            var h = options.height && options.height < 5000 ? options.height : 5000;
            var L = Math.abs(options.pt1.lon - options.pt2.lon) > Math.abs(options.pt1.lat - options.pt2.lat) ? Math.abs(options.pt1.lon - options.pt2.lon) : Math.abs(options.pt1.lat - options.pt2.lat);
            var num = options.num && options.num > 50 ? options.num : 50;
            var result = [];
            var dlt = L / num;
            if (Math.abs(options.pt1.lon - options.pt2.lon) > Math.abs(options.pt1.lat - options.pt2.lat)) { //以lon为基准
                var delLat = (options.pt2.lat - options.pt1.lat) / num;
                if (options.pt1.lon - options.pt2.lon > 0) {
                    dlt = -dlt;
                }
                for (var i = 0; i < num; i++) {
                    var tempH = options.baseheight + h - Math.pow((-0.5 * L + Math.abs(dlt) * i), 2) * 4 * h / Math.pow(L, 2);
                    var lon = options.pt1.lon + dlt * i;
                    var lat = options.pt1.lat + delLat * i;
                    result.push([ lon, lat, tempH ]);
                }
            } else { //以lat为基准
                var delLon = (options.pt2.lon - options.pt1.lon) / num;
                if (options.pt1.lat - options.pt2.lat > 0) {
                    dlt = -dlt;
                }
                for (var i = 0; i < num; i++) {
                    var tempH = options.baseheight + h - Math.pow((-0.5 * L + Math.abs(dlt) * i), 2) * 4 * h / Math.pow(L, 2);
                    var lon = options.pt1.lon + delLon * i;
                    var lat = options.pt1.lat + dlt * i;
                    result.push([ lon, lat, tempH ]);
                }
            }
            if (resultOut != undefined) {
                resultOut = result;
            }
            return result;
        }
 
 
        var trailArr = [];
        CesiumCurvedTrail.create = function(options) {
            var viewer = options.viewer;
            var isAdd = false;
            var material = null;
            var center = {
                lon : options.lon,
                lat : options.lat
            }
            var target = {
                "lon" : options.targetlon,
                "lat" : options.targetlat
            };
            if (!isAdd) {
                if (material != null) {
                } else {
                    material = new Cesium.PolylineTrailLinkMaterialProperty(Cesium.Color.ORANGE, 3000);
                }
                var points = parabolaEquation({
                    pt1 : center,
                    pt2 : target,
                    baseheight : options.alt,
                    height : options.curheight,
                    num : 100
                });
                var pointArr = [];
                for (var i = 0; i < points.length; i++) {
                    pointArr.push(points[i][0], points[i][1], points[i][2]);
                }
                trailArr.push(
                    viewer.entities.add({
                        id : 'PolylineTrailLink',
                        polyline : {
                            positions : Cesium.Cartesian3.fromDegreesArrayHeights(pointArr),
                            width : 2,
                            material : material
                        }
                    }),
                    viewer.entities.add({
                        position : Cesium.Cartesian3.fromDegrees(center.lon, center.lat, options.alt),
                        point : {
                            pixelSize : 6,
                            color : Cesium.Color.BLUE
                        }
                    }),
                    viewer.entities.add({
                        position : Cesium.Cartesian3.fromDegrees(target.lon, target.lat, options.alt),
                        point : {
                            pixelSize : 6,
                            color : Cesium.Color.RED
                        }
                    })
                );
                isAdd = true;
            }
        }
 
        CesiumCurvedTrail.clear = function(viewer) {
            for (var i = 0; i < trailArr.length; i++) {
                viewer.entities.remove(trailArr[i]);
            }
        }
 
        return CesiumCurvedTrail;
    }
 
    if (typeof (CesiumCurvedTrail) === 'undefined') {
        window.CesiumCurvedTrail = define_CesiumCurvedTrail();
    } else {
        console.log("CesiumCurvedTrail already defined.");
    }
})(window);