liuyg
2021-07-02 6d4779a48a752c164e2fe7559dac6caed00f416f
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
define([
    'dojo/_base/declare',
    'dojo/_base/lang',
    'dojo/_base/array',
    'dojo/_base/html',
    'dojo/topic',
    'jimu/BaseWidget',
],
    function (declare,
        lang,
        array,
        html,
        topic,
        BaseWidget
    ) {
        return declare([BaseWidget], {
            baseClass: 'jimu-widget-visibleArea',
            name: 'visibleArea',
            flag: false,
            startup: function () {
                var that = this;
                var viewer = that.map;
                // viewer.imageryLayers.addImageryProvider(new Cesium.BingMapsImageryProvider({
                //     url: 'https://dev.virtualearth.net',
                //     mapStyle: Cesium.BingMapsStyle.AERIAL,
                //     key: 'AkOyUpgDCoz063AWW1WfHnxp5222UBdxLOp1XvRv0tuebQnr2S7UcZkiLgME7gX0'
                // }));
                var scene = viewer.scene;
                scene.shadowMap.darkness = 1.275; //设置第二重烘焙纹理的效果(明暗程度)
                scene.skyAtmosphere.brightnessShift = 0.4; //修改大气的亮度
                // scene.debugShowFramesPerSecond = true;
                scene.hdrEnabled = false;
                scene.sun.show = false;
                // 01设置环境光的强度-新处理CBD场景
                scene.lightSource.ambientLightColor = new Cesium.Color(0.65, 0.65, 0.65, 1);
                // 添加光源
                var position1 = new Cesium.Cartesian3.fromDegrees(116.261209157595, 39.3042238956531,
                    480);
                //光源方向点
 
                var targetPosition1 = new Cesium.Cartesian3.fromDegrees(116.261209157595,
                    39.3042238956531, 430);
                var dirLightOptions = {
                    targetPosition: targetPosition1,
                    color: new Cesium.Color(1.0, 1.0, 1.0, 1),
                    intensity: 0.55
                };
                // var URL_CONFIG = {
                //     SCENE_CBD: 'http://www.supermapol.com/realspace/services/3D-CBDCache20200416/rest/realspace',
                // }
                // var promise = scene.open(URL_CONFIG.SCENE_CBD);
                directionalLight_1 = new Cesium.DirectionalLight(position1, dirLightOptions);//加载光源
                scene.addLightSource(directionalLight_1);
                var viewPosition;
                $('#loadingbar').remove();
                $("#toolbar").show();
                if (!scene.pickPositionSupported) {
                    alert('不支持深度纹理,可视域分析功能无法使用(无法添加观测)!');
                }
                // 先将此标记置为true,不激活鼠标移动事件中对可视域分析对象的操作
                scene.viewFlag = true;
                var pointHandler = new Cesium.DrawHandler(viewer, Cesium.DrawMode.Point);
 
                // 创建可视域分析对象
                var viewshed3D = new Cesium.ViewShed3D(scene);
                var colorStr1 = viewshed3D.visibleAreaColor.toCssColorString();
                var colorStr2 = viewshed3D.hiddenAreaColor.toCssColorString();
                var palette = [
                    [
                        "rgb(0, 0, 0)",
                        "rgb(67, 67, 67)",
                        "rgb(102, 102, 102)",
                        "rgb(204, 204, 204)",
                        "rgb(217, 217, 217)",
                        "rgb(255, 255, 255)",
                    ],
                    [
                        "rgb(152, 0, 0)",
                        "rgb(255, 0, 0)",
                        "rgb(255, 153, 0)",
                        "rgb(255, 255, 0)",
                        "rgb(0, 255, 0)",
                        "rgb(0, 255, 255)",
                        "rgb(74, 134, 232)",
                        "rgb(0, 0, 255)",
                        "rgb(153, 0, 255)",
                        "rgb(255, 0, 255)"
                    ],
                    [
                        "rgb(230, 184, 175)",
                        "rgb(244, 204, 204)",
                        "rgb(252, 229, 205)",
                        "rgb(255, 242, 204)",
                        "rgb(217, 234, 211)",
                        "rgb(208, 224, 227)",
                        "rgb(201, 218, 248)",
                        "rgb(207, 226, 243)",
                        "rgb(217, 210, 233)",
                        "rgb(234, 209, 220)",
                        "rgb(221, 126, 107)",
                        "rgb(234, 153, 153)",
                        "rgb(249, 203, 156)",
                        "rgb(255, 229, 153)",
                        "rgb(182, 215, 168)",
                        "rgb(162, 196, 201)",
                        "rgb(164, 194, 244)",
                        "rgb(159, 197, 232)",
                        "rgb(180, 167, 214)",
                        "rgb(213, 166, 189)",
                        "rgb(204, 65, 37)",
                        "rgb(224, 102, 102)",
                        "rgb(246, 178, 107)",
                        "rgb(255, 217, 102)",
                        "rgb(147, 196, 125)",
                        "rgb(118, 165, 175)",
                        "rgb(109, 158, 235)",
                        "rgb(111, 168, 220)",
                        "rgb(142, 124, 195)",
                        "rgb(194, 123, 160)",
                        "rgb(166, 28, 0)",
                        "rgb(204, 0, 0)",
                        "rgb(230, 145, 56)",
                        "rgb(241, 194, 50)",
                        "rgb(106, 168, 79)",
                        "rgb(69, 129, 142)",
                        "rgb(60, 120, 216)",
                        "rgb(61, 133, 198)",
                        "rgb(103, 78, 167)",
                        "rgb(166, 77, 121)",
                        "rgb(91, 15, 0)",
                        "rgb(102, 0, 0)",
                        "rgb(120, 63, 4)",
                        "rgb(127, 96, 0)",
                        "rgb(39, 78, 19)",
                        "rgb(12, 52, 61)",
                        "rgb(28, 69, 135)",
                        "rgb(7, 55, 99)",
                        "rgb(32, 18, 77)",
                        "rgb(76, 17, 48)",
                    ]
                ]
                // console.log($("#colorPicker1"), 46343)
                // $("#colorPicker1").spectrum({
                //     color: colorStr1,
                //     showPalette: true,
                //     showAlpha: true,
                //     localStorageKey: "spectrum.demo",
                //     palette: palette
                // });
                // $('#colorPicker2').spectrum({
                //     color: colorStr2,
                //     showPalette: true,
                //     showAlpha: true,
                //     localStorageKey: "spectrum.demo",
                //     palette: palette
                // });
                $(".close").click(function () {
                    $("#wrapper").hide();
                });
                var widget = viewer.cesiumWidget;
                try {
                    //添加S3M图层
                    var promise = scene.open(URL_CONFIG.SCENE_CBD);
                    Cesium.when(promise, function (layers) {
                        // 图层加载完成,设置相机位置
                        scene.camera.setView({
                            destination: Cesium.Cartesian3.fromDegrees(
                                116.44366835831197, 39.907137217792666,
                                48.237028126511696),
                            orientation: {
                                heading: 1.6310555040487564,
                                pitch: 0.0017367269669030794,
                                roll: 3.007372129104624e-12
                            }
                        });
                        for (var i = 0; i < layers.length; i++) {
                            layers[i].selectEnabled = false;
                        }
                    }, function (e) {
                        if (widget._showRenderLoopErrors) {
                            var title = '加载SCP失败,请检查网络连接状态或者url地址是否正确?';
                            widget.showErrorPanel(title, undefined, e);
                        }
                    });
                } catch (e) {
                    if (widget._showRenderLoopErrors) {
                        var title = '渲染时发生错误,已停止渲染。';
                        widget.showErrorPanel(title, undefined, e);
                    }
                }
                var viewModel = {
                    direction: 1.0,
                    pitch: 1.0,
                    distance: 1.0,
                    verticalFov: 1.0,
                    horizontalFov: 1.0,
                    visibleAreaColor: '#ffffffff',
                    invisibleAreaColor: '#ffffffff'
                };
 
                Cesium.knockout.track(viewModel);
                var toolbar = document.getElementById('wrapper');
                Cesium.knockout.applyBindings(viewModel, toolbar);
                Cesium.knockout.getObservable(viewModel, 'direction').subscribe(
                    function (newValue) {
                        viewshed3D.direction = parseFloat(newValue);
                    }
                );
                Cesium.knockout.getObservable(viewModel, 'pitch').subscribe(
                    function (newValue) {
                        viewshed3D.pitch = parseFloat(newValue);
                    }
                );
                Cesium.knockout.getObservable(viewModel, 'distance').subscribe(
                    function (newValue) {
                        viewshed3D.distance = parseFloat(newValue);
                    }
                );
                Cesium.knockout.getObservable(viewModel, 'verticalFov').subscribe(
                    function (newValue) {
                        viewshed3D.verticalFov = parseFloat(newValue);
 
                    }
                );
                Cesium.knockout.getObservable(viewModel, 'horizontalFov').subscribe(
                    function (newValue) {
                        viewshed3D.horizontalFov = parseFloat(newValue);
                    }
                );
                Cesium.knockout.getObservable(viewModel, 'visibleAreaColor').subscribe(
                    function (newValue) {
                        var color = Cesium.Color.fromCssColorString(newValue);
                        viewshed3D.visibleAreaColor = color;
                    }
                );
                Cesium.knockout.getObservable(viewModel, 'invisibleAreaColor').subscribe(
                    function (newValue) {
                        var color = Cesium.Color.fromCssColorString(newValue);
                        viewshed3D.hiddenAreaColor = color;
                    }
                );
                var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
                // 鼠标移动时间回调
                handler.setInputAction(function (e) {
                    // 若此标记为false,则激活对可视域分析对象的操作
                    if (!scene.viewFlag) {
                        //获取鼠标屏幕坐标,并将其转化成笛卡尔坐标
                        var position = e.endPosition;
                        var last = scene.pickPosition(position);
 
                        //计算该点与视口位置点坐标的距离
                        var distance = Cesium.Cartesian3.distance(viewPosition, last);
 
                        if (distance > 0) {
                            // 将鼠标当前点坐标转化成经纬度
                            var cartographic = Cesium.Cartographic.fromCartesian(last);
                            var longitude = Cesium.Math.toDegrees(cartographic.longitude);
                            var latitude = Cesium.Math.toDegrees(cartographic.latitude);
                            var height = cartographic.height;
                            // 通过该点设置可视域分析对象的距离及方向
                            viewshed3D.setDistDirByPoint([longitude, latitude, height]);
                        }
                    }
                    // handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
                }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
 
                handler.setInputAction(function (e) {
                    //鼠标右键事件回调,不再执行鼠标移动事件中对可视域的操作
                    //判断主体是否激活
                    if ($(".jimu-widget-visibleArea").is(":visible")) {   //可见
                        //执行可见的操作
                        scene.viewFlag = true;
                        $("#wrapper").show();
                        viewModel.direction = viewshed3D.direction;
                        viewModel.pitch = viewshed3D.pitch;
                        viewModel.distance = viewshed3D.distance;
                        viewModel.horizontalFov = viewshed3D.horizontalFov;
                        viewModel.verticalFov = viewshed3D.verticalFov;
                    } else {
                        // console.log('没打开');
                    }
 
                }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
 
                document.getElementById("chooseView").onclick = function () {
                    if (pointHandler.active) {
                        return;
                    }
                    //先清除之前的可视域分析
                    viewer.entities.removeAll();
                    viewshed3D.distance = 0.1;
                    scene.viewFlag = true;
 
                    //激活绘制点类
                    pointHandler.activate();
                };
                pointHandler.drawEvt.addEventListener(function (result) {
                    var point = result.object;
                    var position = point.position;
                    viewPosition = position;
 
                    // 将获取的点的位置转化成经纬度
                    var cartographic = Cesium.Cartographic.fromCartesian(position);
                    var longitude = Cesium.Math.toDegrees(cartographic.longitude);
                    var latitude = Cesium.Math.toDegrees(cartographic.latitude);
                    var height = cartographic.height + 1.8;
                    point.position = Cesium.Cartesian3.fromDegrees(longitude, latitude, height);
 
                    if (scene.viewFlag) {
                        // 设置视口位置
                        viewshed3D.viewPosition = [longitude, latitude, height];
                        viewshed3D.build();
                        // 将标记置为false以激活鼠标移动回调里面的设置可视域操作
                        scene.viewFlag = false;
                    }
                });
                $("#clear").on("click", function () {
                    $("#wrapper").hide();
                    viewer.entities.removeAll();
                    viewshed3D.distance = 0.1;
                    scene.viewFlag = true;
                    pointHandler.clear();
                })
 
                $('.jimu-widget-visibleArea').find(".closes").click(function () {
                    $(".jimu-widget-visibleArea").hide();
                    $("#wrapper").hide();
                    viewer.entities.removeAll();
                    viewshed3D.distance = 0.1;
                    scene.viewFlag = true;
                    pointHandler.clear();
                })
                // console.log(scene)
                this.viewer = viewer;
                this.viewshed3D = viewshed3D;
                this.scene = scene;
                this.pointHandler = pointHandler;
 
                topic.subscribe("closev", lang.hitch(this, this.closev));
            },
            viewer: '',
            viewshed3D: '',
            scene: '',
            pointHandler: '',
            closev: function () {
                $(".jimu-widget-visibleArea").hide();
                $("#wrapper").hide();
                this.viewer.entities.removeAll();
                this.viewshed3D.distance = 0.1;
                this.scene.viewFlag = true;
                this.pointHandler.clear();
            },
            onOpen: function () {
 
            },
            onClose: function () {
                //面板关闭的时候触发 (when this panel is closed trigger)
            },
 
            onMinimize: function () {
                this.resize();
            },
 
            onMaximize: function () {
                this.resize();
            },
 
            resize: function () {
 
            },
 
            destroy: function () {
                //销毁的时候触发
                //todo
                //do something before this func
                this.inherited(arguments);
            }
 
        });
    });