在开发的过程,要求cesium加载的地球透明
只是地表透明还不能满足要求,只加载部分区域的方式来解决的
代码如下:
<template>
<div id="cesiumContainer" style="height: 100vh;"></div>
<div id="toolbar" style="position: fixed;top:20px;left:220px;">
<span style="color:#fff">球体透明</span>
</div>
<div class="slider-demo-block" style="width:200px;color:#fff;position: fixed;top:50px;left:220px;">
<span class="demonstration">透明度</span>
<el-slider v-model="value4" :format-tooltip="formatTooltip"/>
</div>
</template>
<script setup>
import {ref, onMounted, nextTick} from 'vue'
import * as Cesium from 'cesium'
let viewer = null;
let value4 = ref(100);
// 倾斜摄影
onMounted(async () => {
// token
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJlNWM5ZjhhYi1iNDJhLTQ0OTMtYmQyOC1kYzhhNWVmYmMyZjMiLCJpZCI6MTUzOTk1LCJpYXQiOjE2ODkzNDgxNjN9.ar6bCmmT63fP9nBriZMfbAfPlRDKqIoCCl-ioDSeVN0'
// 初始化
viewer = new Cesium.Viewer('cesiumContainer', {
orderIndependentTranslucency: false,
contextOptions: {
webgl: {
alpha: true,
}
}
});
viewer.imageryLayers.removeAll();
let imgLayer = new Cesium.UrlTemplateImageryProvider({
url: "https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}",
minimumLevel: 3,
maximumLevel: 18
})
viewer.imageryLayers.addImageryProvider(imgLayer);
// 地表透明
viewer.scene.backgroundColor = Cesium.Color.BLACK.withAlpha(0);
viewer.scene.globe.baseColor = Cesium.Color.BLACK.withAlpha(0); //修改地图蓝色背景
viewer.scene.skyAtmosphere.show = false;
viewer.scene.globe.translucency.enabled = true;
viewer.scene.globe.undergroundColor = undefined;
// 地表透明
// 只显示部分地图 开始
const globe = viewer.scene.globe;
let clippingPlanesEnabled = true;
let edgeStylingEnabled = false;
const position = Cesium.Cartographic.toCartesian(
new Cesium.Cartographic.fromDegrees(113.06785738392063, 22.643743971034342, 100)
);
const distance = 200000.0;
const boundingSphere11 = new Cesium.BoundingSphere(position, distance);
globe.clippingPlanes = new Cesium.ClippingPlaneCollection({
modelMatrix: Cesium.Transforms.eastNorthUpToFixedFrame(position),
planes: [
new Cesium.ClippingPlane(
new Cesium.Cartesian3(1.0, 0.0, 0.0),
distance
),
new Cesium.ClippingPlane(
new Cesium.Cartesian3(-1.0, 0.0, 0.0),
distance
),
new Cesium.ClippingPlane(
new Cesium.Cartesian3(0.0, 1.0, 0.0),
distance
),
new Cesium.ClippingPlane(
new Cesium.Cartesian3(0.0, -1.0, 0.0),
distance
),
],
unionClippingRegions: true,
edgeWidth: edgeStylingEnabled ? 1.0 : 0.0,
edgeColor: Cesium.Color.WHITE,
enabled: clippingPlanesEnabled,
});
globe.backFaceCulling = false;
globe.showSkirts = false;
// 只显示部分地图 结束
let tileset = await Cesium.Cesium3DTileset.fromUrl('/src/assets/tileset/12/tileset.json', {});
update3dtilesMaxtrix(tileset);
viewer.scene.primitives.add(tileset);
await viewer.flyTo(tileset)
//更新倾斜摄影位置
function update3dtilesMaxtrix(tileSet) {
//调整参数
let params = {
// tx: 113.06265738392063, //模型中心X轴坐标(经度,单位:十进制度)谷歌
// ty: 22.646803971034342, //模型中心Y轴坐标(纬度,单位:十进制度)谷歌
tx: 113.06785738392063, //模型中心X轴坐标(经度,单位:十进制度)高德
ty: 22.643743971034342, //模型中心Y轴坐标(纬度,单位:十进制度)高德
tz: 40, //模型中心Z轴坐标(高程,单位:米)
rx: 0, //X轴(经度)方向旋转角度(单位:度)
ry: 0, //Y轴(纬度)方向旋转角度(单位:度)
rz: 2, //Z轴(高程)方向旋转角度(单位:度)
scale: 1.3, //缩放比例
};
//旋转
const mx = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(params.rx));
const my = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(params.ry));
const mz = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(params.rz));
const rotationX = Cesium.Matrix4.fromRotationTranslation(mx);
const rotationY = Cesium.Matrix4.fromRotationTranslation(my);
const rotationZ = Cesium.Matrix4.fromRotationTranslation(mz);
//平移
const position = Cesium.Cartesian3.fromDegrees(
params.tx,
params.ty,
params.tz
);
const m = Cesium.Transforms.eastNorthUpToFixedFrame(position);
//旋转、平移矩阵相乘
Cesium.Matrix4.multiply(m, rotationX, m);
Cesium.Matrix4.multiply(m, rotationY, m);
Cesium.Matrix4.multiply(m, rotationZ, m);
//比例缩放
const scale = Cesium.Matrix4.fromUniformScale(params.scale);
Cesium.Matrix4.multiply(m, scale, m);
console.log("矩阵m:", m);
//赋值给tileset
tileSet._root.transform = m;
}
})
const formatTooltip = (val) => {
nextTick(() => {
// 地球球体透明
var _layers = viewer.imageryLayers._layers;
for (let i = 0, len = _layers.length; i < len; i++) {
_layers[i].alpha = val / 100;
}
})
// 地球球体透明
return val / 100
}
</script>
<style scoped>
#cesiumContainer {
overflow: hidden;
}
</style>
效果如图