1.根据相机到模型的距离,显示或隐藏具体模型
/**
* @description: 模型的可见性设置
* @param entity {Entity} 隐藏的model实体
* @param distance {Number} 可见距离
* @return {*}
*/
function showOrHidden(entity, distance) {
let postRenderCallback = () => {
var cameraPosition = viewer.camera.positionWC; // 获取摄像机位置
var modelPosition = entity.position.getValue(Cesium.JulianDate.now()); // 获取模型位置
if (!Cesium.defined(modelPosition)) {
return; // 如果模型位置未定义,则退出函数
}
// 计算摄像机与模型之间的距离
var distance1 = Cesium.Cartesian3.distance(cameraPosition, modelPosition);
// 根据距离设置模型的可见性
if (distance1 < distance) {
entity.show = true; // 当距离小于distance米时,显示模型
} else {
entity.show = false; // 当距离大于等于1000米时,隐藏模型
}
}
viewer.scene.postRender.addEventListener(postRenderCallback)
}
2.根据相机到地面距离,显示或隐藏所有模型、粒子和图元
/**
* @description: 所有entity和primitive的可见性设置
* @param distance {Number} 可见距离
* @return {*}
*/
function showOrHiddenAll(distance) {
viewer.camera.changed.addEventListener(() => {
const height = viewer.scene.globe.ellipsoid.cartesianToCartographic(viewer.camera.position).height;;
if (height > distance) {
hidePrimitivesAndEntities();
} else {
showPrimitivesAndEntities();
}
});
function hidePrimitivesAndEntities() {
const primitives = viewer.scene.primitives;
for (let i = 0; i < primitives.length; i++) {
primitives.get(i).show = false;
}
const entities = viewer.entities;
entities.values.forEach(function(entity) {
entity.show = false;
});
}
function showPrimitivesAndEntities() {
const primitives = viewer.scene.primitives;
for (let i = 0; i < primitives.length; i++) {
primitives.get(i).show = true;
}
const entities = viewer.entities;
entities.values.forEach(function(entity) {
entity.show = true;
});
}
}