北京经济技术开发区经开区虚拟城市项目-【前端】-移动端Web
lixuliang
2024-08-07 a916fc2df00bac9f8210b09fd7ee79f8b291e345
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
var FlattenIsDraw = false;
var FlattenWallArr = [];
var FlattenEntity = [];
var Flatten3DTileSetAnModels = [];
 
//地形压平
function CreateTerra_Flatten() {
    if (FlattenIsDraw) return;
    FlattenIsDraw = true;
    ClearAllClippingPlanes();
    var tooltip = sgworld.Core.CreateTooltip();
    var positions = [];
    var hierarchy = [];
    var polygon;
    var tempPosition;
    var bottomH = null;
    var clickNum = 0;
 
    var handle = new Cesium.ScreenSpaceEventHandler(Viewer.scene.canvas);
 
    setDepthTest('open');
    //鼠标左键单击
    handle.setInputAction(function (movement) {
        tempPosition = getPointFromWindowPoint(movement.position);
        var cartographic = Cesium.Cartographic.fromCartesian(tempPosition);
        var lng = Cesium.Math.toDegrees(cartographic.longitude);//经度值
        var lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度值
        positions.push(tempPosition);
        hierarchy.push(lng, lat, cartographic.height);
        if (clickNum === 0) {
            positions.push(tempPosition);
            hierarchy.push(lng, lat, cartographic.height);
        } else if (clickNum === 1) {
            polygon = Viewer.entities.add({
                name: null,
                polygon: {
                    hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy)),
                    material: Cesium.Color.YELLOW.withAlpha(0.3)
                }
            });
            polygon.polygon.hierarchy = new Cesium.CallbackProperty(getHierarchy, false);
            window.sgworld.ProjectTree.pushtemporaryItem(polygon);
        }
        clickNum++;
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
 
    //鼠标移动
    handle.setInputAction(function (movement) {
        if (clickNum === 0) {
            tooltip.showAt(movement.endPosition, "左键点击开始绘制点");
            return;
        } else if (clickNum === 1) {
            tooltip.showAt(movement.endPosition, "点击添加第二个点");
        } else {
            tooltip.showAt(movement.endPosition, "右键结束绘制");
        }
        tempPosition = getPointFromWindowPoint(movement.endPosition);
        var cartographic = Cesium.Cartographic.fromCartesian(tempPosition);
        var lng = Cesium.Math.toDegrees(cartographic.longitude);//经度值
        var lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度值
        hierarchy.splice(hierarchy.length - 3, 3);
        positions.splice(positions.length - 1, 1);
        positions.push(tempPosition);
        hierarchy.push(lng, lat, cartographic.height);
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
 
    //鼠标右键单击结束绘制
    handle.setInputAction(function (movement) {
        tooltip.show(false);
        handle.destroy();
        if (clickNum < 2) {
            clickNum = 0;
            return;
        }
        clickNum = 0;
        Viewer.entities.remove(polygon);
        polygon = Viewer.entities.add({
            name: null,
            polygon: {
                hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy)),
                material: new Cesium.ImageMaterialProperty({
                    image: SmartEarthRootUrl + 'Workers/image/bottomplane.jpg',
                    repeat: new Cesium.Cartesian2(1, 1)
                })
            }
        });
        //计算底部高度
        for (var i = 2; i < hierarchy.length; i += 3) {
            if (bottomH) {
                bottomH = bottomH > hierarchy[i] ? hierarchy[i] : bottomH;
            } else {
                bottomH = hierarchy[i];
            }
        }
        positions.push(positions[0]);
        var cartographic1, cartographic2, lng1, lng2, lat1, lat2;
        for (var i = 0; i < positions.length - 1; i++) {
            cartographic1 = Cesium.Cartographic.fromCartesian(positions[i]);
            cartographic2 = Cesium.Cartographic.fromCartesian(positions[i + 1]);
            lng1 = Cesium.Math.toDegrees(cartographic1.longitude);//经度值
            lat1 = Cesium.Math.toDegrees(cartographic1.latitude);//纬度值
 
            lng2 = Cesium.Math.toDegrees(cartographic2.longitude);//经度值
            lat2 = Cesium.Math.toDegrees(cartographic2.latitude);//纬度值
            getPoint(lng1, lat1, lng2, lat2, cartographic1.height, cartographic2.height, bottomH);
        }
        polygon.polygon.height = bottomH;
        FlattenEntity.push(polygon);
        groundClipping_Flatten(positions);
        FlattenIsDraw = false;
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
 
    //动态绘制多边形
    function getHierarchy() {
        return new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy));
    }
 
    function getPointFromWindowPoint(point) {
        var position;
        if (Viewer.scene.terrainProvider.constructor.name === "EllipsoidTerrainProvider") {
            position = Viewer.camera.pickEllipsoid(point, Viewer.scene.globe.ellipsoid);
        } else {
            var ray = Viewer.scene.camera.getPickRay(point);
            position = Viewer.scene.globe.pick(ray, Viewer.scene);
        }
        var feature = Viewer.scene.pick(point);
        if (feature) {
            var _cartesian = Viewer.scene.pickPosition(point);
            if (_cartesian) {
                position = _cartesian;
                var cartographic = Cesium.Cartographic.fromCartesian(position);
                var height = Viewer.scene.globe.getHeight(cartographic);
                cartographic.height = height;
                position = Cesium.Cartographic.toCartesian(cartographic);
            }
        }
 
        return position;
    }
 
    function groundClipping_Flatten(positions) {
        // Create clipping planes for polygon around area to be clipped.
        var globe = Viewer.scene.globe;
        var points = positions.concat();
        points.splice(points.length - 1, 1);
        //去除相同点
        for (var i = 0; i < points.length - 1; i++) {
            if (points[i].x === points[i + 1].x && points[i].y === points[i + 1].y) {
                points.splice(i, 1);
                i--;
            }
        }
        var pointsLength = points.length;
        //判断是否为顺时针
        if (PolygonIsClockwise(points)) {
            points.reverse();
        }
        // Create center points for each clipping plane
        var clippingPlanes = [];
        for (var i = 0; i < pointsLength; ++i) {
            var nextIndex = (i + 1) % pointsLength;
            var midpoint = Cesium.Cartesian3.add(points[i], points[nextIndex], new Cesium.Cartesian3());
            midpoint = Cesium.Cartesian3.multiplyByScalar(midpoint, 0.5, midpoint);
 
            var up = Cesium.Cartesian3.normalize(midpoint, new Cesium.Cartesian3());
            var right = Cesium.Cartesian3.subtract(points[nextIndex], midpoint, new Cesium.Cartesian3());
            right = Cesium.Cartesian3.normalize(right, right);
 
            var normal = Cesium.Cartesian3.cross(right, up, new Cesium.Cartesian3());
            normal = Cesium.Cartesian3.normalize(normal, normal);
 
            // 起始点为坐标原点
            var originCenteredPlane = new Cesium.Plane(normal, 0.0);
            var distance = Cesium.Plane.getPointDistance(originCenteredPlane, midpoint);
            clippingPlanes.push(new Cesium.ClippingPlane(normal, distance));
        }
        globe.clippingPlanes = new Cesium.ClippingPlaneCollection({
            planes: clippingPlanes
        });
    }
 
    //根据两点经纬度测算距离
    var PI = Math.PI;
    var EARTH_RADIUS = 6378137.0;
 
    function getGreatCircleDistance(lat1, lng1, lat2, lng2) {
        var radLat1 = getRad(lat1);
        var radLat2 = getRad(lat2);
 
        var a = radLat1 - radLat2;
        var b = getRad(lng1) - getRad(lng2);
 
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000.0;
        return s;
    }
 
    function getRad(d) {
        return d * PI / 180.0;
    }
 
    function drawFlattenWall(wallJwd, heightA, heightB) {
        var blueWall = Viewer.entities.add({
            name: null,
            wall: {
                //坐标点,不指定高度
                positions: Cesium.Cartesian3.fromDegreesArray(
                    wallJwd),
                // material : Cesium.Color.BLUE,
                maximumHeights: heightA,
                minimumHeights: heightB,
            }
 
        });
        var ellipse = blueWall.wall;
        ellipse.material = new Cesium.ImageMaterialProperty({
            image: SmartEarthRootUrl + 'Workers/image/ggy.jpg',
            repeat: new Cesium.Cartesian2(1, 1)
        });
        FlattenWallArr.push(blueWall);
    }
 
    function getPoint(lng1, lat1, lng2, lat2, height1, height2, bottomHeight) {
        //坐标转换
        // var ellipsoid = Viewer.scene.globe.ellipsoid;
        // var cartographic1 = Cesium.Cartographic.fromDegrees(lng1, lat1, height1);
        // var cartesian1 = ellipsoid.cartographicToCartesian(cartographic1);
        // var scene1 = Cesium.SceneTransforms.wgs84ToWindowCoordinates(Viewer.scene, cartesian1);
        // var cartographic2 = Cesium.Cartographic.fromDegrees(lng2, lat2, height2);
        // var cartesian2 = ellipsoid.cartographicToCartesian(cartographic2);
        // var scene2 = Cesium.SceneTransforms.wgs84ToWindowCoordinates(Viewer.scene, cartesian2);
        // var leftX = Math.round(scene1.x);
        // var leftY = Math.round(scene1.y);
        // var rightX = Math.round(scene2.x);
        // var rightY = Math.round(scene2.y);
        var pointSum = 160;  //取样点个数
        var addXTT = Cesium.Math.lerp(lng1, lng2, 1.0 / pointSum) - lng1;
        var addYTT = Cesium.Math.lerp(lat1, lat2, 1.0 / pointSum) - lat1;
        // var addX = Cesium.Math.lerp(leftX, rightX, 1.0 / pointSum) - leftX;
        // var addY = Cesium.Math.lerp(leftY, rightY, 1.0 / pointSum) - leftY;
        //经纬度高程数组
        // var longitudeArr = [];
        // var latitudeArr = [];
        // var heightArr = [];
        var wallJwd = [];
        var positions = [];
        var heightA = [];
        var Cartesian;
        wallJwd.push(lng1, lat1);
        Cartesian = Cesium.Cartesian3.fromDegrees(lng1, lat1);
        positions.push(Cesium.Cartographic.fromCartesian(Cartesian));
        for (var i = 0; i < pointSum; i++) {
            var longitude = lng1 + (i + 1) * addXTT;
            var latitude = lat1 + (i + 1) * addYTT;
            wallJwd.push(longitude, latitude);
            Cartesian = Cesium.Cartesian3.fromDegrees(longitude, latitude);
            positions.push(Cesium.Cartographic.fromCartesian(Cartesian));
            // var x = leftX + (i + 1) * addX;
            // var y = leftY + (i + 1) * addY;
            // var eventPosition = {x: x, y: y};
            //var ray = Viewer.camera.getPickRay(eventPosition);
            //var position = Viewer.scene.globe.pick(ray, Viewer.scene);
            // if (Cesium.defined(position)) {
            //     var cartographic = Cesium.Cartographic.fromCartesian(position);
            //     heightArr[i] = cartographic.height.toFixed(6);   //保留两位小数
            //     latitudeArr[i] = Cesium.Math.toDegrees(cartographic.latitude);
            //     longitudeArr[i] = Cesium.Math.toDegrees(cartographic.longitude);
            //     if (i == 0) {
            //         heightA.push(heightArr[0]);
            //     }
            //     wallJwd.push(longitudeArr[i]);
            //     wallJwd.push(latitudeArr[i]);
            //     heightA.push(heightArr[i]);
            //     if (i == pointSum - 1) {
            //         heightA.push(heightArr[i]);
            //     }
            // }
 
        }
        wallJwd.push(lng2, lat2);
        Cartesian = Cesium.Cartesian3.fromDegrees(lng2, lat2);
        positions.push(Cesium.Cartographic.fromCartesian(Cartesian));
        var heightB = [];
        //获取高度
        getHeightsFromLonLat(positions, function (data) {
            if (data) {
                heightA = data;
                heightA.forEach(function () {
                    heightB.push(bottomHeight);
                });
                drawFlattenWall(wallJwd, heightA, heightB);
            }
        });
    }
 
    //判断多边形是否为顺时针绘制
    function PolygonIsClockwise(points) {
        if (points.length >= 3) {
            //求向量积
            var type = (points[1].x - points[0].x) * (points[2].y - points[1].y) - (points[1].y - points[0].y) * (points[2].x - points[1].x);
            if (type > 0) {
                return false;
            }
            return true;
        }
    }
 
    //根据经纬度获取高度
    function getHeightsFromLonLat(positions, callback) {
        var camera = Viewer.camera;
        if (isHaveTerrain(Viewer)) {
            //根据经纬度计算出地形高度。
            var promise = Cesium.sampleTerrainMostDetailed(Viewer.terrainProvider, positions);
            var cameraHeight = camera.positionCartographic.height;
            Cesium.when(promise, function (updatedPositions) {
                var heights = [];
                updatedPositions.forEach(function (item) {
                    heights.push(item.height);
                });
                if (typeof callback === "function") {
                    callback(heights);
                }
            });
        } else {
            layer.msg('请添加地形数据!');
        }
    }
 
    //是否有地形
    function isHaveTerrain() {
        if (Viewer.scene && Viewer.scene.terrainProvider && (Viewer.scene.terrainProvider._layers || Viewer.scene.terrainProvider._urlTemplate)) {
            return true;
        }
        return false;
    }
}
 
//模型压平
function CreateModel_Flatten() {
    if (FlattenIsDraw) return;
    if (Flatten3DTileSetAnModels.length === 0) {
        get3DTileSetAnModels_Flatten();
    }
    if (Flatten3DTileSetAnModels.length === 0) {
        layer.msg('无模型数据');
        return;
    }
    FlattenIsDraw = true;
    ClearAllClippingPlanes();
    var tooltip = sgworld.Core.CreateTooltip();
    var positions = [];
    var hierarchy = [];
    var polygon;
    var tempPosition;
    var bottomH = null;
    var clickNum = 0;
 
    var handle = new Cesium.ScreenSpaceEventHandler(Viewer.scene.canvas);
 
    setDepthTest('open');
 
    //修改
    //Flatten3DTileSetAnModels.forEach(function (tileset) {
    //    debugger
    //    //set3DtilesBug();
 
    //    //sgworld.Creator.createModifyMesh("aaaa", positions, 100000, tileset, {});
    //})
    //鼠标左键单击
    handle.setInputAction(function (movement) {
        tempPosition = getPointFromWindowPoint(movement.position);
        var cartographic = Cesium.Cartographic.fromCartesian(tempPosition);
        var lng = Cesium.Math.toDegrees(cartographic.longitude);//经度值
        var lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度值
        positions.push(tempPosition);
        hierarchy.push(lng, lat, cartographic.height);
        if (clickNum === 0) {
            positions.push(tempPosition);
            hierarchy.push(lng, lat, cartographic.height);
        } else if (clickNum === 1) {
            polygon = Viewer.entities.add({
                name: null,
                polygon: {
                    hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy)),
                    material: Cesium.Color.YELLOW.withAlpha(0.3)
                }
            });
            polygon.polygon.hierarchy = new Cesium.CallbackProperty(getHierarchy, false);
            window.sgworld.ProjectTree.pushtemporaryItem(polygon);
        }
        clickNum++;
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
 
    //鼠标移动
    handle.setInputAction(function (movement) {
        if (clickNum === 0) {
            tooltip.showAt(movement.endPosition, "左键点击开始绘制点");
            return;
        } else if (clickNum === 1) {
            tooltip.showAt(movement.endPosition, "点击添加第二个点");
        } else {
            tooltip.showAt(movement.endPosition, "右键结束绘制");
        }
        tempPosition = getPointFromWindowPoint(movement.endPosition);
        var cartographic = Cesium.Cartographic.fromCartesian(tempPosition);
        var lng = Cesium.Math.toDegrees(cartographic.longitude);//经度值
        var lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度值
        hierarchy.splice(hierarchy.length - 3, 3);
        positions.splice(positions.length - 1, 1);
        positions.push(tempPosition);
        hierarchy.push(lng, lat, cartographic.height);
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
 
    //鼠标右键单击结束绘制
    handle.setInputAction(function (movement) {
        tooltip.show(false);
        handle.destroy();
        if (clickNum < 2) {
            clickNum = 0;
            return;
        }
        clickNum = 0;
        Viewer.entities.remove(polygon);
 
        // polygon = Viewer.entities.add({
        //     name: null,
        //     polygon: {
        //         hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy)),
        //         material: new Cesium.ImageMaterialProperty({
        //             image: 'static/img/tt/bottomplane.jpg',
        //             repeat: new Cesium.Cartesian2(1, 1)
        //         })
        //     }
        // });
        //计算底部高度
        for (var i = 2; i < hierarchy.length; i += 3) {
            if (bottomH) {
                bottomH = bottomH > hierarchy[i] ? hierarchy[i] : bottomH;
            } else {
                bottomH = hierarchy[i];
            }
        }
        var pp = polygon;
        bottomH = -15;
        Flatten3DTileSetAnModels.forEach(function (tileset) {
            //set3DtilesBug();
            CreateModifyMesh("aaaa", positions, -bottomH, tileset, {});
            //CreateModifyMesh("aaaa", [{ x: -2614051.124732636, y: 4736955.336069073, z: 3366375.2614991767 }, { x: -2614075.364686353, y: 4736905.985810969, z: 3366424.333971074 }, { x: -2614050.302550419, y: 4736917.680342396, z: 3366428.1807405315 }, { x: -2614050.302550419, y: 4736917.680342396, z: 3366428.1807405315 }], 50, tileset, {});
            //sgworld.Creator.createModifyMesh("aaaa", positions, 100000, tileset, {});
        })
        //positions.push(positions[0]);
        // var cartographic1, cartographic2, lng1, lng2, lat1, lat2;
        // for (var i = 0; i < positions.length - 1; i++) {
        //     cartographic1 = Cesium.Cartographic.fromCartesian(positions[i]);
        //     cartographic2 = Cesium.Cartographic.fromCartesian(positions[i + 1]);
        //     lng1 = Cesium.Math.toDegrees(cartographic1.longitude);//经度值
        //     lat1 = Cesium.Math.toDegrees(cartographic1.latitude);//纬度值
 
        //     lng2 = Cesium.Math.toDegrees(cartographic2.longitude);//经度值
        //     lat2 = Cesium.Math.toDegrees(cartographic2.latitude);//纬度值
        //     getPoint(lng1, lat1, lng2, lat2, cartographic1.height, cartographic2.height, bottomH);
        // }
        // polygon.polygon.height = bottomH;
        // FlattenEntity.push(polygon);
        // groundClipping_Flatten(positions);
        window.ModelFlattenData = {
            height: bottomH,
            Flatten3DTileSetAnModels: Flatten3DTileSetAnModels,
            positions: positions
        };
        FlattenIsDraw = false;
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
 
    //动态绘制多边形
    function getHierarchy() {
        return new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy));
    }
 
    function getPointFromWindowPoint(point) {
        var position;
        if (Viewer.scene.terrainProvider.constructor.name === "EllipsoidTerrainProvider") {
            position = Viewer.camera.pickEllipsoid(point, Viewer.scene.globe.ellipsoid);
        } else {
            var ray = Viewer.scene.camera.getPickRay(point);
            position = Viewer.scene.globe.pick(ray, Viewer.scene);
        }
        var feature = Viewer.scene.pick(point);
        if (feature) {
            var _cartesian = Viewer.scene.pickPosition(point);
            if (_cartesian) {
                position = _cartesian;
                var cartographic = Cesium.Cartographic.fromCartesian(position);
                var height = Viewer.scene.globe.getHeight(cartographic);
                cartographic.height = height;
                position = Cesium.Cartographic.toCartesian(cartographic);
            }
        }
 
        return position;
    }
 
    //获取模型
    function get3DTileSetAnModels_Flatten() {
        Flatten3DTileSetAnModels = sgworld.Creator._D3Tilesets || [];
    }
}
 
function ClearAllClippingPlanes() {
    if (FlattenWallArr.length > 0) {
        for (var i = 0; i < FlattenWallArr.length; i++) {
            Viewer.entities.remove(FlattenWallArr[i])
        }
        FlattenWallArr = [];
    }
    if (Viewer.scene.globe.clippingPlanes) {
        Viewer.scene.globe.clippingPlanes.removeAll();
    }
    if (FlattenEntity && FlattenEntity.length > 0) {
        FlattenEntity.forEach(function (entity) {
            Viewer.entities.remove(entity);
        });
        FlattenEntity = [];
    }
    if (Flatten3DTileSetAnModels.length > 0) {
        Flatten3DTileSetAnModels.forEach(function (model) {
            for (; model.modifyB3dmObjects && model.modifyB3dmObjects.length;) {
                model.modifyB3dmObjects.pop()
                model.modifyMeshObject = undefined;
            }
        });
    }
    FlattenIsDraw = false;
}
 
 
 
var CreateModifyMesh = function (description, polygon, altitude, meshObject, options) {
    var len = polygon.length;
    var guid = Cesium.createGuid();
    var heights = new Array(len);
    heights.fill(altitude);
    var nowTime = Date.now();
    var rect = Cesium.Rectangle.fromCartesianArray(polygon);
    var west = rect.west * 180 / Math.PI;
    var south = rect.south * 180 / Math.PI;
    var east = rect.east * 180 / Math.PI;
    var north = rect.north * 180 / Math.PI;
 
    var minPoint = Cesium.Cartesian3.fromDegrees(east, south);
    var maxPoint = Cesium.Cartesian3.fromDegrees(west, north);
 
    var extrudedPolygon = new Cesium.PolygonGeometry({
        polygonHierarchy: new Cesium.PolygonHierarchy(polygon)
    });
    //var nowRect = new Cesium.Rectangle(west, south, east, north);
 
    if (meshObject.modifyB3dmObjects === undefined) {
        meshObject.modifyB3dmObjects = [];
    }
    var modifyMeshObject = {
        id: guid,
        show: true,
        name: description,
        rectangle: rect,
        polygonBounds: [minPoint.x, minPoint.y, minPoint.z, maxPoint.x, maxPoint.y, maxPoint.z],
        modelBounds: null,
        polygonGeometry: extrudedPolygon,
        vertices: polygon,
        polygon: polygon,
        height: altitude,
        meshObject: meshObject,
        heights: heights,
        timeStamp: nowTime,
        image: createTexture(polygon),
        texture: null
    };
    meshObject.modifyB3dmObjects.push(modifyMeshObject);
    meshObject.modifyMeshObject = modifyMeshObject;
};
 
 
 
 
var texturePixel = 1024;
function createTexture(polygon) {
    var rect = Cesium.Rectangle.fromCartesianArray(polygon);
    var west = rect.west * 180 / Math.PI;
    var south = rect.south * 180 / Math.PI;
    var east = rect.east * 180 / Math.PI;
    var north = rect.north * 180 / Math.PI;
    var nowRect = new Cesium.Rectangle(west, south, east, north);
    var canvasPolygon = [];
    polygon.forEach(function (position) {
        var point = position.longitude ? position : Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
        point = getPointRelativePosition(point, nowRect, texturePixel, texturePixel);
        canvasPolygon.push([point.normalizedLongitude, point.normalizedLatitude]);
    });
    var canvas = createCanvas(canvasPolygon, "#FF0000", texturePixel, texturePixel);
    //debug
    //document.body.appendChild(canvas);
    var image = new Image();
    image.width = texturePixel;
    image.height = texturePixel;
    image.src = canvas.toDataURL("image/png");
    return image;
 
    //return canvas;
 
}
function createCanvas(canvasPolygon, color, pixelX, pixelY) {
    var canvas = document.createElement("canvas");
    canvas.width = pixelX;
    canvas.height = pixelY;
    var ctx = canvas.getContext("2d");
    ctx.fillStyle = "#000";
    ctx.fillRect(0, 0, pixelX, pixelX);
    if (canvasPolygon.length <= 0) return;
    ctx.moveTo(canvasPolygon[0][0], canvasPolygon[0][1]);
    for (var i = 0; i < canvasPolygon.length; i++) {
        ctx.lineTo(canvasPolygon[i][0], canvasPolygon[i][1]);
    }
    ctx.fillStyle = color;
    ctx.fill();
    return canvas;
}
function getPointRelativePosition(point, rect, pixelX, pixelY) {
    var lonDelta = rect.east - rect.west;
    //var latDelta = rect.north - rect.south;
    var latDelta = rect.south - rect.north;
    var radianLon = point.longitude * 180 / Math.PI;
    var radianLat = point.latitude * 180 / Math.PI;
    point.normalizedLongitude = (radianLon - rect.west) / lonDelta * pixelX;
    point.normalizedLatitude = (radianLat - rect.north) / latDelta * pixelY;
    // point.normalizedLatitude = (rect.south-radianLat) / latDelta * pixelY;
    return point;
}
 
 
 
 
 
 
function set3DtilesBug() {
 
    var fixGltf = function (gltf) {
        if (!gltf.extensionsUsed) {
            return;
        }
        if (!gltf.extensionsRequired) {
            return;
        }
        var v = gltf.extensionsUsed.indexOf('KHR_technique_webgl');
        var t = gltf.extensionsRequired.indexOf('KHR_technique_webgl');
 
        if (v !== -1) {
            gltf.extensionsRequired.splice(t, 1, 'KHR_techniques_webgl');
            gltf.extensionsUsed.splice(v, 1, 'KHR_techniques_webgl');
            gltf.extensions = gltf.extensions || {};
            gltf.extensions['KHR_techniques_webgl'] = {};
            gltf.extensions['KHR_techniques_webgl'].programs = gltf.programs;
            gltf.extensions['KHR_techniques_webgl'].shaders = gltf.shaders;
            gltf.extensions['KHR_techniques_webgl'].techniques = gltf.techniques;
            var techniques = gltf.extensions['KHR_techniques_webgl'].techniques;
 
            gltf.materials.forEach(function (mat, index) {
                gltf.materials[index].extensions['KHR_technique_webgl'].values = gltf.materials[
                    index].values;
                gltf.materials[index].extensions['KHR_techniques_webgl'] = gltf.materials[index]
                    .extensions['KHR_technique_webgl'];
 
                var vtxfMaterialExtension = gltf.materials[index].extensions[
                    'KHR_techniques_webgl'];
 
                for (var value in vtxfMaterialExtension.values) {
                    var us = techniques[vtxfMaterialExtension.technique].uniforms;
                    for (var key in us) {
                        if (us[key] === value) {
                            vtxfMaterialExtension.values[key] = vtxfMaterialExtension.values[value];
                            delete vtxfMaterialExtension.values[value];
                            break;
                        }
                    }
                };
            });
 
            techniques.forEach(function (t) {
                for (var attribute in t.attributes) {
                    var name = t.attributes[attribute];
                    t.attributes[attribute] = t.parameters[name];
                };
 
                for (var uniform in t.uniforms) {
                    var name = t.uniforms[uniform];
                    t.uniforms[uniform] = t.parameters[name];
                };
            });
        }
    }
 
    Object.defineProperties(Cesium.Model.prototype, {
        _cachedGltf: {
            set: function (value) {
                this._vtxf_cachedGltf = value;
                if (this._vtxf_cachedGltf && this._vtxf_cachedGltf._gltf) {
                    fixGltf(this._vtxf_cachedGltf._gltf);
                }
            },
            get: function () {
                return this._vtxf_cachedGltf;
            }
        }
    });
 
 
}