首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Unity中使用Cesium如何通过经纬度获取高程

  • 25-04-24 15:41
  • 2236
  • 10407
blog.csdn.net

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、使用小结

如果对获取高程的需求比较频繁,个人还是推荐使用第二种方式

注:本文转载自blog.csdn.net的Col_20的文章"https://blog.csdn.net/colooooooo/article/details/144979366"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

134
游戏
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top