unity根据点生成mesh

生成Mesh的代码

public class SpwanMesh : MonoBehaviour
{
   
    
    
    public Material material;
    public bool createCube;
    private GameObject cubeParent;
    private Transform meshParent;
    [ContextMenu("生成模型")]
    public void Create()
    {
   
    
    
        var jsonPath = Application.dataPath + "\\填充面.json";
        var jsonData = JsonConvert.DeserializeObject<jsonData>(File.ReadAllText(jsonPath));
        try
        {
   
    
    
            jsonData.children = jsonData.children.OrderBy(x => int.Parse(x.czmObject.name)).ToList();
        }
        catch (Exception ex)
        {
   
    
    
            Debug.LogError("name不是整数类型的");
        }

        foreach (var item in jsonData.children)
        {
   
    
    
            var poss = item.czmObject.positions;
            Vector3[] vers = new Vector3[poss.Count];
            //poss = poss.OrderByDescending(x => x[2]).ToList();
            for (int i = 0; i < poss.Count; i++)
            {
   
    
    
                var x = poss[i][0] / Mathf.PI * 180.0;
                var y = poss[i][1] / Mathf.PI * 180.0;
                var z = poss[i][2];
                Vector3 newPos = GetPos(x, y, z);
                vers[i] = newPos;

                if (meshParent == null)
                {
   
    
    
                    meshParent = new GameObject("mesh").transform;
                }
                if (cubeParent == null)
                {
   
    
    
                    cubeParent = new GameObject("cubes");
                }
                if (createCube)
                    CreateCube(newPos, cubeParent, item.czmObject.name);
            }
            CreateMesh(vers, item.czmObject.name);
        }
    }
    [ContextMenu("清理")]
    public void Clear()
    {
   
    
    
        DestroyImmediate(meshParent.gameObject);
        DestroyImmediate(cubeParent);
    }

    private void CreateCube(Vector3 position, GameObject cubeParent, string _name)
    {
   
    
    
        

猜你喜欢

转载自blog.csdn.net/u010197227/article/details/130863788