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

完成了这就,就是这么的简单。
边栏推荐
- Zuckerberg's latest VR prototype is coming. It is necessary to confuse virtual reality with reality
- 9 necessary soft skills for program ape career development
- Why can banana be a random number generator? Because it is the "king of radiation" in the fruit industry
- JS tool function, self encapsulating a throttling function
- JSP cannot be resolved to a type error reporting solution
- [rust contribution] implement Message Oriented Middleware (6) -client from zero
- (超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写
- 一文搞懂php中的(DI)依赖注入
- ICML 2022 | ByteDance AI Lab proposes a multimodal model: x-vlm, learning multi granularity alignment of vision and language
- Work assessment of Biopharmaceutics of Jilin University in March of the 22nd spring -00031
猜你喜欢

北大换新校长!中国科学院院士龚旗煌接任,15岁考上北大物理系

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

ICML 2022 | ByteDance AI Lab proposes a multimodal model: x-vlm, learning multi granularity alignment of vision and language

Crawler grabs the idea of reading on wechat

zabbix的安装避坑指南

“语法糖”——我的编程新知

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

完美洗牌问题

Peking University has a new president! Gongqihuang, academician of the Chinese Academy of Sciences, took over and was admitted to the Physics Department of Peking University at the age of 15

用CPU方案打破内存墙?学PayPal堆傲腾扩容量,漏查欺诈交易量可降至1/30
随机推荐
Maybe it's the wrong reason
JS tool function, self encapsulating a throttling function
【Harmony OS】【ARK UI】ETS 上下文基本操作
Lao Ye's blessing
How to quickly deliver high-value software
Tai Chi graphics 60 lines of code to achieve classic papers, 0.7 seconds to get Poisson disk sampling, 100 times faster than numpy
DevEco Studio 3.0编辑器配置技巧篇
Crawler grabs the idea of reading on wechat
[rust contribution] implement Message Oriented Middleware (6) -client from zero
PHP代码审计2—这些函数必知必会
Peking University has a new president! Gongqihuang, academician of the Chinese Academy of Sciences, took over and was admitted to the Physics Department of Peking University at the age of 15
Musk: Twitter should learn from wechat and make 1billion people "live on it" into a super app
亚马逊在中国的另一面
数学分析_笔记_第3章:极限
程序员真人秀又来了!呼兰当主持挑灯狂补知识,SSS大佬本科竟是药学,清华朱军张敏等加入导师团...
教你如何在winpe里安装win11系统
uniapp 制作手机app程序, 使用uni.chooseVideo录制视频,视频播放模糊分辨率低的原因
About sizeof() and strlen in array
How far is the memory computing integrated chip from popularization? Listen to what practitioners say | collision school x post friction intelligence
Redis related-01