# cesium-添加地下模型

在加载模型的时候,可能会出现地下模型,这时就需要开启深度检测了,不然模型会在视图中乱飘

开启深度检测后,如果加载了图层,就有可能遮挡住地下模型

这是就需要使用根据距离改变地下透视程度

# 核心代码

            /**
             * 根据距离改变地下透视程度
             */
            undergroundState() {
                let viewer = this._viewer;
                let scene = viewer.scene;
                let globe = scene.globe;

                scene.screenSpaceCameraController.enableCollisionDetection = false;
                globe.translucency.frontFaceAlphaByDistance = new Cesium.NearFarScalar(
                    400.0,
                    0.0,
                    800.0,
                    1.0
                );
                globe.translucency.enabled = true;
                globe.translucency.frontFaceAlphaByDistance.nearValue = Cesium.Math.clamp(0.3, 0.0, 0.1);
                globe.translucency.frontFaceAlphaByDistance.farValue = 1.0;

            },
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Use correct character set. -->
    <meta charset="utf-8"/>
    <!-- Tell IE to use the latest, best version. -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <!-- Make the application on mobile take up the full browser screen and disable user scaling. -->
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"/>
    <title>cesium-操作面要素透明度</title>
    <script src="https://data.sunbt.ltd/lib/Cesium-1.89/Build/Cesium/Cesium.js"></script>
    <script src="../../static/lib/vue.min.js"></script>
    <script src="../../static/lib/jquery-3.6.0.min.js"></script>
    <script src="../../static/data/geodata.js"></script>
    <style>
        @import url(https://data.sunbt.ltd/lib/Cesium-1.89/Build/Cesium/Widgets/widgets.css);

        html,
        body, #temp {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }

        #cesiumContainer {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }

    </style>
</head>
<body>
<div id="temp">
    <div style="display: -webkit-flex;display: flex;width: 100%;height: 100%">
        <div style="width: 90%;height: 100%">
            <div id="cesiumContainer"></div>
        </div>
        <div style="width: 10%;height: 100%;background-color: #d3d3d3;padding: 30px">
            <button class="btn" @click="undergroundModel">地下模型</button>
        </div>
    </div>
</div>
<script>

    let EarthComp = new Vue({
        el: "#temp",
        data: {
            _viewer: undefined,
            model: null
        },
        mounted: function () {
            let that = this;
            this.earthInit();
        },
        methods: {
            /**
             * 地球初始化
             */
            earthInit() {
                let that=this;
                //天地图token
                let TDT_tk = "92e1a5834c7d3187b1dde67550b7a774";
                //Cesium token
                let cesium_tk = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJkYWRlMmQ1Ni1iNGMxLTRhY2YtYmExYi1jNjYyNTUxNDhjYzgiLCJpZCI6MjMwODAsInNjb3BlcyI6WyJhc2wiLCJhc3IiLCJhc3ciLCJnYyIsInByIl0sImlhdCI6MTU4OTE4NzE1NH0.QrUVmI13wKIqFwmnsGSR6aMr8FEtbO7jsTA0mqnvbdM";

                //标注
                let TDT_CIA_C = "http://{s}.tianditu.gov.cn/cia_c/wmts?service=wmts&request=GetTile&version=1.0.0" +
                    "&LAYER=cia&tileMatrixSet=c&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}" +
                    "&style=default&format=tiles&tk=" + TDT_tk;

                // 添加mapbox自定义地图实例
                let layer = new Cesium.MapboxStyleImageryProvider({
                    url: 'https://api.mapbox.com/styles/v1',
                    username: 'sungang',
                    styleId: 'cky44gl8m0rwn15pqt8ovyhrs',
                    accessToken: 'pk.eyJ1Ijoic3VuZ2FuZyIsImEiOiJja2VqaW5rbWwwdWc2MnlwaGVrMnBhbnRuIn0.UlFQNpPyJjiYfETSNo5Zkg',
                    scaleFactor: true
                });

                //初始页面加载
                Cesium.Ion.defaultAccessToken = cesium_tk;
                let viewer = new Cesium.Viewer('cesiumContainer', {
                    geocoder: false,   // 位置查找工具
                    baseLayerPicker: false,// 图层选择器(地形影像服务)
                    timeline: false, // 底部时间线
                    homeButton: false,// 视角返回初始位置
                    fullscreenButton: false, // 全屏
                    animation: false,   // 左下角仪表盘(动画器件)
                    sceneModePicker: false,// 选择视角的模式(球体、平铺、斜视平铺)
                    navigationHelpButton: false, //导航帮助按钮
                    imageryProvider: layer
                });

                //调用影响中文注记服务
                viewer.imageryLayers.addImageryProvider(new Cesium.WebMapTileServiceImageryProvider({
                    url: TDT_CIA_C,
                    layer: "tdtImg_c",
                    style: "default",
                    format: "tiles",
                    tileMatrixSetID: "c",
                    subdomains: ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"],
                    tilingScheme: new Cesium.GeographicTilingScheme(),
                    tileMatrixLabels: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"],
                    maximumLevel: 50,
                    show: false
                }))

                this._viewer = viewer;

                // 去除版权信息
                this._viewer._cesiumWidget._creditContainer.style.display = "none";

                let model = new Cesium.Cesium3DTileset({
                    url: 'http://njzy.natapp1.cc/res/data/3dtiles/tianjie/tileset.json',
                    modelMatrix: Cesium.Matrix4.fromArray(
                        [0.9972458032561666, 0.04372029028528979, 0.05991113506964879, 0,
                            -0.03623787897545647, 0.9920229449104262, -0.12073646051879428, 0,
                            -0.06471185374661931, 0.11823287609043515, 0.9908750491338749, 0,
                            -663.0794944260269, 1211.490494620055, 2974.1003134818748, 1]),
                });
                this.model=viewer.scene.primitives.add(model);
                this._viewer.flyTo(model, {
                    offset: {
                        heading: Cesium.Math.toRadians(20.0),//方向
                        pitch: Cesium.Math.toRadians(-25),//倾斜角度
                        range: 1000
                    }
                });
            },
            undergroundModel() {
                let that = this;
                //关闭深度检测
                let globe = this._viewer.scene.globe;
                globe.depthTestAgainstTerrain = false;

                this._viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
                    url: 'http://192.168.1.243:8088/data/3dtiles/tianjie/tileset.json',
                    modelMatrix: Cesium.Matrix4.fromArray([0.9972433273083445, 0.04373131442771638, 0.05994429311791549, 0, -0.03624054324752937, 0.9920147125640941, -0.12080328257751138, 0, -0.06474850704152399, 0.1182978537201852, 0.9908648992880364, 0, -640.4182153767906, 1170.067138605751, 2946.0255522700027, 1]),
                }));
                this.undergroundState()
            },
            /**
             * 根据距离改变地下透视程度
             */
            undergroundState() {
                let viewer = this._viewer;
                let scene = viewer.scene;
                let globe = scene.globe;

                scene.screenSpaceCameraController.enableCollisionDetection = false;
                globe.translucency.frontFaceAlphaByDistance = new Cesium.NearFarScalar(
                    400.0,
                    0.0,
                    800.0,
                    1.0
                );
                globe.translucency.enabled = true;
                globe.translucency.frontFaceAlphaByDistance.nearValue = Cesium.Math.clamp(0.3, 0.0, 0.1);
                globe.translucency.frontFaceAlphaByDistance.farValue = 1.0;
            },
        }
    })
</script>
</body>
</html>
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

# 示例效果

image-20220117201713486

image-20220117201753723

上次更新时间: 2022年5月20日星期五上午11点16分