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

完成了这就,就是这么的简单。
边栏推荐
- client-go gin的简单整合十一-Delete
- Configuration source code
- On the self-cultivation of an excellent red team member
- The era of copilot free is over! The official version is 67 yuan / month, and the student party and the defenders of popular open source projects can prostitute for nothing
- Jilin University 22 spring March "technical economics" assignment assessment-00073
- 墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库
- Xidian AI ranked higher than Qingbei in terms of AI majors, and Nantah ranked the first in China in 2022 in terms of soft science majors
- Maintenant, les oreilles vont entrer dans le métacosme.
- 俄罗斯AIRI研究院等 | SEMA:利用深度迁移学习进行抗原B细胞构象表征预测
- TC object structure and abbreviation
猜你喜欢

Lao Ye's blessing

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

Russian Airi Research Institute, etc. | SEMA: prediction of antigen B cell conformation characterization using deep transfer learning

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

Lu Qi invests in quantum computing for the first time

The era of copilot free is over! The official version is 67 yuan / month, and the student party and the defenders of popular open source projects can prostitute for nothing

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

Crawler crawls Sina Weibo data

【Rust投稿】捋捋 Rust 中的 impl Trait 和 dyn Trait

Why can banana be a random number generator? Because it is the "king of radiation" in the fruit industry
随机推荐
IE寿终正寝,网友们搞起了真·墓碑……
How to use ide to automatically sign and debug Hongmeng application
OpenSUSE environment PHP connection Oracle
Lu Qi invests in quantum computing for the first time
教你如何在winpe里安装win11系统
redis
Jilin University 22 spring March "career design" assignment assessment-00072
Redis related-03
谷歌创始人布林二婚破裂:被曝1月已提出与华裔妻子离婚,目前身家6314亿美元...
Standing at the center of the storm: how to change the engine of Tencent
AI writes its own code to let agents evolve! The big model of openai has the flavor of "human thought"
opencv最大能打开多少图像?
Musk: Twitter should learn from wechat and make 1billion people "live on it" into a super app
web3.eth. Getpendingtransactions() returns null?
OpenSUSE environment variable settings
Do you really need automated testing?
Lao Ye's blessing
Maybe it's the wrong reason
How to use crawlers to capture bullet screen and comment data of station B?
Break the memory wall with CPU scheme? Learn from PayPal stack to expand capacity, and the volume of missed fraud transactions can be reduced to 1/30