【Cesium】根据相机距离隐藏或显示模型

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;
        });
    }

}
相关推荐
人工智能训练师1 小时前
Ubuntu22.04如何安装新版本的Node.js和npm
linux·运维·前端·人工智能·ubuntu·npm·node.js
Seveny071 小时前
pnpm相对于npm,yarn的优势
前端·npm·node.js
yddddddy2 小时前
css的基本知识
前端·css
昔人'2 小时前
css `lh`单位
前端·css
前端君3 小时前
实现最大异步并发执行队列
javascript
Nicander4 小时前
上帝视角看 GPU 学习笔记
webgl·gpu
Nan_Shu_6144 小时前
Web前端面试题(2)
前端
知识分享小能手4 小时前
React学习教程,从入门到精通,React 组件核心语法知识点详解(类组件体系)(19)
前端·javascript·vue.js·学习·react.js·react·anti-design-vue
蚂蚁RichLab前端团队5 小时前
🚀🚀🚀 RichLab - 花呗前端团队招贤纳士 - 【转岗/内推/社招】
前端·javascript·人工智能
孩子 你要相信光5 小时前
css之一个元素可以同时应用多个动画效果
前端·css