当前位置:网站首页>Cesium 拖拽3D模型
Cesium 拖拽3D模型
2022-06-25 03:44:00 【王+V】
Cesium 拖拽3D模型
这个地方是想实现一个什么效果呢?就是使用 cesium 在地图上添加一个3D模型,然后实现拖拽效果。
添加基站模型
然后这篇博文介绍的主要不是添加模型,但是也简单把代码直接粘贴过来吧,就不详细说了。
// 添加基站模型
function addSite() {
let position = Cesium.Cartesian3.fromDegrees(116.236393, 40.075119, 0);
// 设置模型方向
let hpRoll = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(0), 0, 0);
let orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpRoll);
// 添加模型
let model = viewer.entities.add({
id: 'site', // 模型id
position: position, // 模型位置
orientation: orientation, // 模型方向
model: {
uri: './models/siteModel/scene.gltf', // 模型路径,自己换成自己的模型
scale: 1,
show: true, // 模型是否可见
},
description: '基站模型' // 添加模型描述
});
viewer.trackedEntity = model; // 视角切换到模型
}
上边的代码就完成了在界面上添加模型功能。

拖拽
其实拖拽就是给这个cesium加一个监听事件。
比如说这个拖拽的简单逻辑分析哈:
1、鼠标按下事件,如果有模型的话,可以给模型设置一个颜色,知道按下选中的是哪个模型。
2、然后再监听鼠标拖拽事件,获取鼠标拖拽的位置,赋值给模型。
3、鼠标抬起事件,结束鼠标移动事件,然后把颜色改回去。
// 注册事件
function setHandler() {
handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
// 注册鼠标按下事件
handler.setInputAction((e) => {
const pick = viewer.scene.pick(e.position);
if (!Cesium.defined(pick)) {
return; };// 如果点击空白区域,则不往下执行
viewer.scene.screenSpaceCameraController.enableRotate = false;// 将相机锁定,不然后续移动实体时相机也会动
pick.id.model.color = Cesium.Color.fromAlpha(Cesium.Color.RED, 1) //设置颜色
// 注册鼠标拖拽事件
viewer.screenSpaceEventHandler.setInputAction((arg) => {
// 为viewer绑定MOUSE_MOVE事件监听器(执行函数,监听的事件)
const position = arg.endPosition;// arg有startPosition与endPosition两个属性,即移动前后的位置信息:Cartesian2对象
const cartesian = viewer.scene.globe.pick(viewer.camera.getPickRay(position), viewer.scene);//将Cartesian2转为Cartesian3
pick.id.position._value = cartesian
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
// 绑定鼠标抬起事件
viewer.screenSpaceEventHandler.setInputAction(({
position }) => {
//为viewer绑定LEFT_UP事件监听器(执行函数,监听的事件)
viewer.scene.screenSpaceCameraController.enableRotate = true;// 取消相机锁定
pick.id.model.color = null //设置颜色
viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);// 解除viewer的MOUSE_MOVE事件监听器
}, Cesium.ScreenSpaceEventType.LEFT_UP)
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
}

完成了这就,就是这么的简单。
边栏推荐
- The programmer reality show is coming again! Hulan, as the host, carried the lamp to fill the knowledge. The SSS boss had a bachelor's degree in pharmacy. Zhu Jun and Zhang Min from Tsinghua joined th
- Create SQLite table with shell script and add SQL statement -- General
- Windows 2003 64 bit system PHP running error: 1% is not a valid Win32 Application
- 亚马逊在中国的另一面
- opencv 红色区域在哪里?
- The sign in function completes 03 "IVX low code sign in system production"
- 9 necessary soft skills for program ape career development
- Collaboration + Security + storage, cloud box helps Shenzhen edetai restructure its data center
- How to quickly deliver high-value software
- Disassembly of Weima prospectus: the electric competition has ended and the intelligent qualifying has just begun
猜你喜欢
![[harmony OS] [ark UI] basic ETS context operations](/img/fb/a1b8463ba160e6c5aa23d671a0c245.png)
[harmony OS] [ark UI] basic ETS context operations

opencv是开源的吗?

Lao Ye's blessing

Intel 13代酷睿首次露出真面目:68MB缓存提升明显

Musk was sued for $258billion in MLM claims. TSMC announced the 2nm process. The Chinese Academy of Sciences found that the lunar soil contained water in the form of hydroxyl. Today, more big news is

2点睡10点起不算熬夜?除非你每天都能执行

China's SkyEye found suspicious signals of extraterrestrial civilization. Musk said that the Starship began its orbital test flight in July. Netinfo office: app should not force users to agree to proc

Maintenant, les oreilles vont entrer dans le métacosme.

IE寿终正寝,网友们搞起了真·墓碑……

client-go gin的简单整合十-Update
随机推荐
The release function completed 02 "IVX low code sign in system production"
How far is the memory computing integrated chip from popularization? Listen to what practitioners say | collision school x post friction intelligence
用CPU方案打破内存墙?学PayPal堆傲腾扩容量,漏查欺诈交易量可降至1/30
Teach you how to install win11 system in winpe
The more AI evolves, the more it resembles the human brain! Meta found the "prefrontal cortex" of the machine. AI scholars and neuroscientists were surprised
zabbix的安装避坑指南
谷歌创始人布林二婚破裂:被曝1月已提出与华裔妻子离婚,目前身家6314亿美元...
Lao Ye's blessing
Huawei failed to appeal and was prohibited from selling 5g equipment in Sweden; Apple regained the first place in the world in terms of market value; DeNO completes round a financing of USD 21million
虽然传统意义上的互联网早已不复存在,但这并不代表互联网早已消失不再
MySQL根据表前缀批量修改、删除表
About sizeof() and strlen in array
孙武玩《魔兽》?有图有真相
OpenSUSE installation pit log
Intel 13代酷睿首次露出真面目:68MB缓存提升明显
吴恩达机器学习新课程又来了!旁听免费,小白友好
中国天眼发现地外文明可疑信号,马斯克称星舰7月开始轨道试飞,网信办:APP不得强制要求用户同意处理个人信息,今日更多大新闻在此...
How to use crawlers to capture bullet screen and comment data of station B?
China's SkyEye found suspicious signals of extraterrestrial civilization. Musk said that the Starship began its orbital test flight in July. Netinfo office: app should not force users to agree to proc
论一个优秀红队人员的自我修养