ArcGIS Maps SDK for Unity 0.3旋转

​ArcGIS Maps SDK for Unity1.0版本已出
基础参考:

API:

https://developers.arcgis.com/unity-sdk/
基础

https://cloud.tencent.com/developer/news/717499

可以做什么

在这里插入图片描述

倾斜摄影 +BIM +标注 总之目的是用unity做出cesium的效果 超不超越之后再说

简单说说问题和解决办法

1.操作方式

左键移动 右键旋转相机 wsad

对于用过cesium开发的就会发现 很别扭 很难受 缺了个中键旋转
解决办法:自己写一个
如果你研究代码会发现 没有关于高程的算法 很难受
GetMouseRayCastDirection() //得到一条相机射线
Geometry.cs //用得到的射线 求与椭球的交点(对没有高程 可能是我没找到) 并返回交点与相机的距离
知道这些=》
得到鼠标点中的交点
private Vector3d getMousedownPosition(Vector3d cartesianPosition)
{
var worldRayDir = GetMouseRayCastDirection();
var ellipsoidScaling = 1.0 / (1.0 - GeoUtils.EarthFlattening);
// 缩放射线在y轴上的空间,使椭球体成为一个球体
var scaledRayOrig = new Vector3d(cartesianPosition.x, cartesianPosition.y * ellipsoidScaling, cartesianPosition.z);
var scaledRayDir = new Vector3d(worldRayDir.x, worldRayDir.y * ellipsoidScaling, worldRayDir.z);
double scaledLength;
var result = RaySphereIntersection(scaledRayOrig, scaledRayDir, Vector3d.zero, GeoUtils.EarthRadius + 750, out scaledLength);
if (result)
{
// 回到非缩放的世界坐标,计算hitPoint和rayOrig之间的真实长度
var scaledHitPoint = scaledRayOrig + scaledLength * scaledRayDir;
Vector3d hitPoint = new Vector3d(scaledHitPoint.x, scaledHitPoint.y / ellipsoidScaling, scaledHitPoint.z);
//var latLon = arcGISMapViewComponent.Scene.FromCartesianPosition(hitPoint);
return hitPoint;
}
return new Vector3d(0, 0, 0);
}
//判断射线是否与地球相交 并返回长度 (原代码有)
private bool RaySphereIntersection(Vector3d rayOrig, Vector3d rayDir, Vector3d sphereCenter, double sphereRadius, out double t)
{
double a = Vector3d.Dot(rayDir, rayDir);
Vector3d s0_r0 = rayOrig - sphereCenter;
double b = 2.0 * Vector3d.Dot(rayDir, s0_r0);
double c = Vector3d.Dot(s0_r0, s0_r0) - (sphereRadius * sphereRadius);

		if (b * b - 4.0 * a * c < 0.0)
		{
			t = 0;
			return false;
		}
		else
		{
			t = (-b - System.Math.Sqrt((b * b) - 4.0 * a * c)) / (2.0 * a);
			return true;
		}
	}

得到点后下一步旋转
原理:
我们要知道原理就不能用RotateAround 和Quaternion.AngleAxis(angle, axis)

利用 Quaternion.Euler自身旋转 Quaternion.AngleAxis 绕自定义轴旋转 得到旋转矩阵
Quaternion rotation = Quaternion.Euler(0, Input.GetAxis(“Mouse X”) * 5, 0);// 绕x y z轴的旋转
// Quaternion rotation = Quaternion.AngleAxis(Input.GetAxis(“Mouse X”) * 5, Vector3.up);
Quaternion rotation1 = Quaternion.AngleAxis(Input.GetAxis(“Mouse Y”) * 5,-transform.right);
Matrix4x4 m = Matrix4x4.TRS(new Vector3(0,0,0), rotation* rotation1, new Vector3(1,1,1));//设置 平移 大小 缩放
transform.position = m.MultiplyPoint3x4(transform.position - Model.Instance.Origin)+ Model.Instance.Origin;
//transform.rotation = m.rotation * transform.rotation;
transform.rotation = rotation* rotation1 * transform.rotation;

​然后就是:
//相机绕点旋转 ---------------------------------高度问题
void MyRotateAround(ref Vector3d cartesianPosition, ref Quaternion cartesianRotation, Vector3d center, Vector3d axis, double angle,int a)
{
Vector3d jilu = cartesianPosition;
//Quaternion rot = Quaternion.AngleAxis(angle, axis);
Vector3d dir = cartesianPosition - center; //计算从圆心指向摄像头的朝向向量
if (a==0)
{
dir = Matrix4x4d.Rotate(axis, angle).MultiplyPoint(dir);
cartesianPosition = center + dir;
cartesianRotation = Matrix4x4d.Rotate(axis, angle).ToQuaterniond().ToQuaternion() * cartesianRotation;//区别 前后位置乘的原因
}
else
{
dir = Matrix4x4d.Rotate(axis, -angle).MultiplyPoint(dir);
cartesianPosition = center + dir;
var right = Matrix4x4.Rotate(cartesianRotation).GetColumn(0);
var rotationX = Quaternion.AngleAxis(-(float)angle, right);
cartesianRotation = rotationX * cartesianRotation;
//cartesianRotation = Matrix4x4d.Rotate(axis, angle).ToQuaterniond().ToQuaternion() * cartesianRotation;
}
if (arcGISMapViewComponent.Scene.FromCartesianPosition(cartesianPosition).Altitude<700)
{
cartesianPosition = jilu;
}

	}

在DragMouseEvent()方法中插入
if (Input.GetMouseButtonDown(2))
{
RotationCenter = getMousedownPosition(cartesianPosition);
}
if (Input.GetMouseButton(2))
{
if (RotationCenter != Vector3d.Zero)
{
MyRotateAround(ref cartesianPosition, ref cartesianRotation, RotationCenter, Vector3d.Normalize(RotationCenter), Input.GetAxis(“Mouse X”) * 5, 0);
MyRotateAround(ref cartesianPosition, ref cartesianRotation, RotationCenter, (Vector3d)hpTransform.Right, Input.GetAxis(“Mouse Y”) * 5, 1);
}
}
2.相机跳转
this.transform.GetComponent().Rotation = new Rotator(356.499768212976f, 67.7412622911893f, 0.00028224651352337f);
this.transform.GetComponent().Position = newlatLon;
想要滑动跳转加个Lerp

猜你喜欢

转载自blog.csdn.net/WantFK/article/details/123909226