cesium实现相机绕地旋转效果

该博客介绍了如何在Cesium中实现相机绕地旋转的效果。通过动态更改相机的heading属性并结合Cesium的setView方法,创建了一个名为AroundView的类来实现这一功能。在核心代码中,定义了绑定和解除绑定事件、开始和停止旋转的方法,并在_onroundView函数中更新相机的朝向,实现了平滑的3D地球旋转效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Cesium实战系列文章总目录传送门

1. 实现效果

在这里插入图片描述

2. 实现方法

2.1 实现思路

相机绕地旋转,只需要动态更改相机的heading属性值即可。

不断使用camera类的setView方法进行更改,API:传送门
在这里插入图片描述

2.2 具体代码

(1)代码调用
引入下面的核心代码JS文件后,简单调用即可。

let aroundViewer = new AroundView(viewer, 0.2);
aroundViewer.start();

(2)核心代码
核心实现代码如下:

/*
 * @Description: 相机绕地旋转
 * @Version: 1.0
 * @Author: Julian
 * @Date: 2022-05-06 10:15:20
 * @LastEditors: Julian
 * @LastEditTime: 2022-05-06 10:43:02
 */
class AroundView {
    constructor(viewer, amount) {
        this._viewer = viewer;
        this._amount = amount;
    }

    // 绑定事件
    _bindEvent() {
        this._viewer.clock.onTick.addEventListener(this._aroundView, this);
    }

    // 解除绑定
    _unbindEvent() {
        this._viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
        this._viewer.clock.onTick.removeEventListener(this._aroundView, this);
    }

    // 开始
    start() {
        this._viewer.clock.shouldAnimate = true;
        this._unbindEvent();
        this._bindEvent();
        return this;
    }

    // 停止
    stop() {
        this._unbindEvent();
        return this;
    }


    // 相机绕地旋转函数
    _aroundView() {
        let heading = this._viewer.camera.heading;
        let pitch = this._viewer.camera.pitch;
        let roll = this._viewer.camera.roll;

        heading += Cesium.Math.toRadians(this._amount);
        if (heading >= Math.PI * 2 || heading <= -Math.PI * 2) {
            heading = 0;
        }

        this._viewer.camera.setView({
            orientation: {
                heading: heading,
                pitch: pitch,
                roll: roll
            }
        })
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

右弦GISer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值