当前位置:网站首页>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);
}

完成了这就,就是这么的简单。
边栏推荐
- MySQL modifies and deletes tables in batches according to the table prefix
- Tianshu night reading notes - 8.4 diskperf disassembly
- About PLSQL error initialization failure
- redis
- 【Harmony OS】【ARK UI】ETS 上下文基本操作
- js工具函数,自己封装一个节流函数
- Perfect shuffle problem
- Google founder brin's second marriage broke up: it was revealed that he had filed for divorce from his Chinese wife in January, and his current fortune is $631.4 billion
- 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
- Monitoring pancakeswap new token
猜你喜欢

Musk: Twitter should learn from wechat and make 1billion people "live on it" into a super app

PHP代码审计1—PHP.ini的那些事

程序猿职业发展9项必备软技能

About sizeof() and strlen in array

Tensorflow, danger! Google itself is the one who abandoned it

Apple's legendary design team disbanded after jobs refused to obey cook

老叶的祝福

墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库

【Harmony OS】【ArkUI】ets开发 图形与动画绘制

9 necessary soft skills for program ape career development
随机推荐
数学分析_笔记_第3章:极限
【Rust投稿】从零实现消息中间件(6)-CLIENT
存算一体芯片离普及还有多远?听听从业者怎么说 | 对撞派 x 后摩智能
Two common OEE monitoring methods for equipment utilization
Program. Launch (xxx) open file
北大换新校长!中国科学院院士龚旗煌接任,15岁考上北大物理系
Understand (DI) dependency injection in PHP
Comprehensive assignment of thesis writing instruction of Dongcai
中国天眼发现地外文明可疑信号,马斯克称星舰7月开始轨道试飞,网信办:APP不得强制要求用户同意处理个人信息,今日更多大新闻在此...
严重的PHP缺陷可导致QNAP NAS 设备遭RCE攻击
Maintenant, les oreilles vont entrer dans le métacosme.
Teach you how to install win11 system in winpe
On the self-cultivation of an excellent red team member
2022-06-21-flink-49 (I. SQL manual)
Why can banana be a random number generator? Because it is the "king of radiation" in the fruit industry
Perfect shuffle problem
How to use crawlers to capture bullet screen and comment data of station B?
How far is the memory computing integrated chip from popularization? Listen to what practitioners say | collision school x post friction intelligence
Comprehensive operation of financial risk management X of Dongcai
Sleep more, you can lose weight. According to the latest research from the University of Chicago, sleeping more than 1 hour a day is equivalent to eating less than one fried chicken leg