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

}
相关推荐
happy_0x3f2 分钟前
前端应用的离线暂停更新策略
开发语言·前端·php
你驴我39 分钟前
WhatsApp 消息撤回与编辑的幂等性设计实践
java·服务器·前端·后端·python
新中地GIS开发老师1 小时前
零基础WebGIS开发入门 | GeoJSON数据持久化
前端·javascript·gis·webgis·三维gis开发
blns_yxl1 小时前
正则驱动实时表单验证(HTML+CSS+JS+正则)
javascript·css·html
sunywz2 小时前
【c#】 Web Deploy一键发布,IIS部署全流程
开发语言·前端·c#
宁&沉沦2 小时前
Chrome 扩展 Manifest 字段版本支持一览(全量)
前端·后端·编辑器
乖孩子2 小时前
Service Worker + IndexedDB 实践
前端
浮生望2 小时前
BFF架构实战:用Express+SSE为Vue3搭建安全的流式对话中间层
前端
Quz2 小时前
QML 与 JavaScript 交互方式:内联函数、外部文件、信号槽与工作线程
javascript·qt
先吃饱再说3 小时前
React 组件通信:从 Props 单向传递到自定义事件
前端·react.js·前端框架