Cesium for Unity叠加行政区划线

先放上效果图

使用了插件 Shapes

https://assetstore.unity.com/packages/tools/particles-effects/shapes-173167

数据集文件

1、带十段线的

通过网盘分享的文件:100000_full.json

链接: https://pan.baidu.com/s/1gkpe8qEbpI9hfSFNWz8B-w?pwd=66ij 提取码: 66ij

2、不带十段线的

通过网盘分享的文件:中国.geoJson

链接: https://pan.baidu.com/s/1OuDg0MFIv3cRBi9qiIFdMA?pwd=raq5 提取码: raq5

来自 https://geojson.hxkj.vip/

代码

csharp 复制代码
using CesiumForUnity;
using Newtonsoft.Json;
using Shapes;
using System.Collections.Generic;
using System.IO;
using Unity.Mathematics;
using UnityEngine;

public class TestLine : MonoBehaviour
{
    public GameObject linePrefab;
    public CesiumGeoreference cg;

    // Start is called before the first frame update
    void Start()
    {
        string json = File.ReadAllText(Application.streamingAssetsPath + "/100000_full.json");
        Root root = JsonConvert.DeserializeObject<Root>(json);
        Debug.Log(root.name);
        GameObject lineRoot = new GameObject();
        lineRoot.name = "lineRoot";
        CesiumGlobeAnchor cgal = lineRoot.AddComponent<CesiumGlobeAnchor>();
        cgal.longitudeLatitudeHeight = new double3(0, 0, 0);
        lineRoot.transform.SetParent(transform);

        double3 temp = new double3();
        foreach (Feature f in root.features)
        {
            if (f.geometry.type == "MultiPolygon")
            {
                var multi = JsonConvert.DeserializeObject<List<List<List<List<double>>>>>(
                    JsonConvert.SerializeObject(f.geometry.coordinates)
                );
                Debug.Log($"{f.properties.name}: MultiPolygon with {multi.Count} polygons");

                for (int i = 0; i < multi.Count; i++)
                {
                    List<List<List<double>>> item = multi[i];

                    GameObject lineObject = Instantiate(linePrefab, lineRoot.transform);
                    lineObject.name = f.properties.name;
                    Polyline polyline = lineObject.GetComponent<Polyline>();
                    polyline.Thickness = 2000; 
                    List<Vector3> list = new List<Vector3>();

                    for (var j = 0; j < item[0].Count; j++)
                    {
                        double3 l = new double3();
                        l.x = item[0][j][0];
                        l.y = item[0][j][1];
                        l.z = 0;

                        if(temp.Equals(l))
                        {
                            //剔除重复的点,会导致画线中断,画线插件有bug
                            continue;
                        }
                        temp = l;
                        double3 t3 = CesiumWgs84Ellipsoid.LongitudeLatitudeHeightToEarthCenteredEarthFixed(l);
                        double3 u3 = cg.TransformEarthCenteredEarthFixedPositionToUnity(t3);
                        Vector3 v3 = new Vector3((float)u3.x, (float)u3.y, (float)u3.z);
                        polyline.AddPoint(v3); 
                    } 
                    polyline.Closed = true; 
                }
            }
            else if (f.geometry.type == "Polygon")
            {
                var poly = JsonConvert.DeserializeObject<List<List<List<double>>>>(
                    JsonConvert.SerializeObject(f.geometry.coordinates)
                );
                Debug.Log($"{f.properties.name}: Polygon with {poly.Count} rings");
            }
            else
            {
                Debug.LogWarning(f.geometry.type);
            }
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}
相关推荐
淡海水1 天前
【节点】[EvaluateTipThickness节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·evaluate·thickness
小贺儿开发2 天前
Unity3D 木胎雕刻
科技·unity·人机交互·互动·雕刻
HY小海2 天前
【Unity游戏创作】常见的设计模式
unity·设计模式·c#·游戏程序
淡海水3 天前
【节点】[EvaluateSimulationAdditionalData节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·simulation·evaluate
小贺儿开发4 天前
Unity3D 文物互动大屏
3d·unity·实时互动·udp·socket·网络通信
秦奈4 天前
Unity学习复习随笔(12):网络开发基础
网络·笔记·学习·unity
淡海水4 天前
【节点】[EvaluateRefractionData节点]原理解析与实际应用
unity·游戏引擎·shadergraph·data·图形·evaluate·refraction
淡海水5 天前
【节点】[EvaluateScatteringColor节点]原理解析与实际应用
unity·游戏引擎·shadergraph·color·图形·evaluate·scattering
lambda6 天前
游戏开发者的CMU动作数据使用指南:如何将AMC文件转Unity动画
unity·游戏开发·动作捕捉·cmu
淡海水6 天前
【节点】[ComputeVertexPosition节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·position·compute·vertex