1、通过版本自带API获取:
Cesium for Unity 1.13.1版本提供了方法SampleHeightMostDetailed,但也具有局限性,如果倾斜摄影数据是分多块的,则需要知道要定位的高程所属哪一块,通过Cesium3DTileset加载倾斜摄影数据,再去使用API方法SampleHeightMostDetailed ,示例如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CesiumForUnity;
using Unity.Mathematics;
using System.Threading.Tasks;
public class Test : MonoBehaviour
{
public Cesium3DTileset terrain;
public List<double3> locations;
// Start is called before the first frame update
void Start()
{
StartCoroutine(SampleTilesetHeights());
}
public IEnumerator SampleTilesetHeights()
{
if (terrain != null)
{
Task<CesiumSampleHeightResult> terrainTask =
terrain.SampleHeightMostDetailed(locations[1], locations[2], locations[0]);
yield return new WaitForTask(terrainTask);
Debug.Log("地形准备完毕");
CesiumSampleHeightResult result = terrainTask.Result;
if (result != null)
{
foreach (string warning in result.warnings)
{
Debug.Log(warning);
}
for (int i = 0; i < result.sampleSuccess.Length; i++)
{
if (result.sampleSuccess[i])
{
Debug.Log(result.longitudeLatitudeHeightPositions[i]);
}
else
{
Debug.Log("所给位置在当前地形上采样不到位置信息.");
}
}
}
}
else
{
Debug.LogError("地形未准备完毕");
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
其中locations 是你需要求取高程的经纬坐标点集合,double3结构,x 对应longitude,y对应latitude,z可以置为0,集合点列表可以是一个点,也可以是多个点,但是点要属于同一个tileset,经测试,即使Cesium3DTileset 不在main相机视口内,都能转换出经纬高,从而获取到高程
2、 通过坐标转换
在之前旧的版本中,还没提供直接的获取高程的方法,需要先把unity的世界坐标转化为ecef,然后ecef再去转经纬高
1、 Unity世界坐标转EarthCenteredEarthFixed
///
/// Unity世界坐标转EarthCenteredEarthFixed
///
///
///
public double3 Unity2ECEF(Vector3 unityPos)
{
return m_CesiumGeoreference.TransformUnityPositionToEarthCenteredEarthFixed(new double3(unityPos.x, unityPos.y, unityPos.z));
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2、 EarthCenteredEarthFixed 转Unity世界坐标
///
/// EarthCenteredEarthFixed 转Unity世界坐标
///
///
///
public Vector3 ECEF2Unity(double3 ecef)
{
double3 unityPosition = m_CesiumGeoreference.TransformEarthCenteredEarthFixedPositionToUnity(ecef);
return new Vector3((float)unityPosition.x, (float)unityPosition.y, (float)unityPosition.z);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3、接着我们可以拿到unity世界坐标转化的ecef坐标,调用CesiumWgs84Ellipsoid提供的ecef转经纬度方法EarthCenteredEarthFixedToLongitudeLatitudeHeight,代码如下:
///
/// 转换Unity世界坐标为经纬高
///
public double3 InverserUnityWorldPostionToLonLatHeight(Vector3 worldPostion)
{
double3 lonLatHeight = new double3();
double3 ecef = Unity2ECEF(worldPostion);
double3 lonlath = CesiumWgs84Ellipsoid.EarthCenteredEarthFixedToLongitudeLatitudeHeight(ecef);
lonLatHeight.x = lonlath.x;
lonLatHeight.y = lonlath.y;
lonLatHeight.z = lonlath.z;
return lonLatHeight;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
4、同样,我们也可以使用经纬度坐标转Unity世界坐标,代码如下:
///
/// 转换经纬高为世界坐标
///
///
///
public Vector3 InverLonLatHeightToUnityWorldPostion(double3 lonlatHeight)
{
double3 ecef = CesiumWgs84Ellipsoid.LongitudeLatitudeHeightToEarthCenteredEarthFixed(lonlatHeight);
Vector3 point = ECEF2Unity(ecef);
return point ;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
3、使用小结
如果对获取高程的需求比较频繁,个人还是推荐使用第二种方式
评论记录:
回复评论: