Cesium--获取当前相机中心与地面的射线焦点
创始人
2025-01-09 12:36:26
0

本文记录获取当前相机中心与地面的射线焦点的方法,可用于视角缩放过程中,控制视角自动平滑切换到二维等场景:

方法一定是视角中心能与地面有交集,如果对着地平线或对着天空肯定是没效果的。直接放代码:

//调整相机到正北方向 上帝视角 const limitViewerController=(viewer)=>{ 	try{ 		viewer.scene.screenSpaceCameraController.enableTilt = false; //禁止中键旋转视角 		// 将相机朝向正北方向(即Z轴指向正北,Y轴指向正东) 		var heading = Cesium.Math.toRadians(0.0); // 正北方向为90度 		var pitch = Cesium.Math.toRadians(0.0); // 保持平面视角 		var roll = 0.0; // 无滚动 		var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll); 		let camera_position = viewer.camera.position.clone(); 		let cameraHeight = viewer.camera.positionCartographic.height;  		let camera = viewer.scene.camera; 		const rayget = new Cesium.Ray(camera.position, camera.direction) 		 		//射线监测 不到目标时 无法设置视角 		const rayintersection = Cesium.IntersectionTests.rayEllipsoid(rayget, Cesium.Ellipsoid.WGS84); 		if(rayintersection){ 			const raypoint = Cesium.Ray.getPoint(rayget, rayintersection.start); 			 			// 获取当前使用的地球椭球体 			var ellipsoid = viewer.scene.globe.ellipsoid; 			// 将Cartesian3转换为经纬度 			var raycartographic = ellipsoid.cartesianToCartographic(raypoint); 			 var cameracartographic = ellipsoid.cartesianToCartographic(camera_position); 			  			// 获取经度、纬度和高度 			var raylongitude = Cesium.Math.toDegrees(raycartographic.longitude); 			var raylatitude = Cesium.Math.toDegrees(raycartographic.latitude); 			var rayheight = cameracartographic.height; 			 			const destinationPostion = Cesium.Cartesian3.fromDegrees(raylongitude, raylatitude, rayheight+100000); 			 			viewer.camera.flyTo({ 			    destination : destinationPostion, 				duration: 1.5, 			    orientation : { 			        heading : 6.283185307179586, 			        pitch : -1.5707963267948966, 			        roll : 0.0 			    } 			}); 		} 		 		 		//调整也可以通过TWEEN.js 实现过渡动画 		//import * as TWEEN from '@tweenjs/tween.js' 		// const coords = {x: viewer.camera.heading, y: viewer.camera.pitch,z:viewer.camera.roll} // Start at (0, 0) 		// const tween = new TWEEN.Tween(coords, false) // Create a new tween that modifies 'coords'. 		// .to({x: 6.283185307179586, y: -1.5707963267948966, z:0}, 1000)  		// .easing(TWEEN.Easing.Quadratic.InOut) 		// .onUpdate((object) => { 		// 	viewer.camera.setView({ 		// 		//destination : new Cesium.Cartesian3(raypoint.x, raypoint.y, camera_position.z), 		// 	    orientation: { 		// 			heading: object.x, // 方向的朝向,以弧度为单位 		// 			pitch: object.y, // 视角的高度,以弧度为单位 		// 	        roll: object.z // 绕着圆周运动的角度,一般设置为0 		// 	    } 		// 	}); 		// 	// viewer.scene.camera.setView({ 		// 	// 	 destination : new Cesium.Cartesian3(camera_position.x, camera_position.y, camera_position.z), 		// 	//     orientation: { 		// 	//         headingPitchRoll: new Cesium.HeadingPitchRoll(object.x, object.y, object.z) 		// 	//     } 		// 	// }); 		// }) 		// .start() // Start the tween immediately. 		// function animate() { 		// 	tween.update() 		// 	requestAnimationFrame(animate) 		// } 		// requestAnimationFrame(animate) 		 	 	}catch(err){ 		console.log(err); 	} }

 

相关内容

热门资讯

实测分享!微信小程序免费黑科技... 实测分享!微信小程序免费黑科技,蜜瓜大厅辅助免费下载窍门教程(有挂猫腻)1、玩家可以在微信小程序免费...
截至发稿!微乐小程序黑科技,蛮... 截至发稿!微乐小程序黑科技,蛮籽酉阳大贰辅助手册教程(有挂方法)1、进入到微乐小程序黑科技是否有挂之...
一分钟教会你!微信小程序免费黑... 一分钟教会你!微信小程序免费黑科技开挂,欢乐茶馆脚本方案教程(有挂细节)1、微信小程序免费黑科技透视...
热门推荐!微信小程序免费黑科技... 热门推荐!微信小程序免费黑科技辅助,朋友局辅助器绝活教程(发现有挂)热门推荐!微信小程序免费黑科技辅...
2024教程!微信小程序免费黑... 2024教程!微信小程序免费黑科技透视,卡五星辅助器逍遥模块教程(确实有挂)1、玩家可以在微信小程序...
现有关情况通报如下!微信小程序... 现有关情况通报如下!微信小程序免费黑科技开挂,潮汕木虱有辅助挂吗积累教程(有挂详细)1)微信小程序免...
据相关数据显示!微乐小程序黑科... 据相关数据显示!微乐小程序黑科技,途游手游辅助脚本平台教材教程(有挂方略)1、超多福利:超高返利,海...
今日焦点!微信小程序免费黑科技... 今日焦点!微信小程序免费黑科技,途游手游辅助秘籍教程(有挂技巧)1、下载好微信小程序免费黑科技正确养...
最新研发!微乐小程序黑科技,吉... 最新研发!微乐小程序黑科技,吉祥填大坑攻略窍门教程(新版有挂)1、每一步都需要思考,不同水平的挑战微...
三分钟了解!微乐小程序免费黑科... 三分钟了解!微乐小程序免费黑科技,闲逸碰胡辅助积累教程(有挂方法)1、许多玩家不知道微乐小程序免费黑...