美文网首页
unity开发之地图分割(二)

unity开发之地图分割(二)

作者: gt154 | 来源:发表于2017-06-06 14:05 被阅读338次

    本篇将继续上篇的内容,对导出的数据进行读取,并还原编辑的区域块,效果如下所示:

    image.png

    1.读取导出数据

    private void ReadAndCreate()
        {
            if (meshList.Count > 0)
            {
                EditorUtility.DisplayDialog("提示", "请清空所有生成的面片!", "确定");
                return;
            }
    
            try
            {
                string filePath = outputPath + map.name + ".txt";
                if (File.Exists(Path.GetFullPath(filePath)))
                {
                    List<string> list = new List<string>(File.ReadAllLines(filePath));
                    List<Block> bList = new List<Block>();
                    int idx = 0;
                    while (idx < list.Count)
                    {
                        Block block = new Block();
                        if (block.linePosList == null)
                        {
                            block.linePosList = new List<Vector3>();
                        }
                        int cnt = int.Parse(list[idx]);
                        for (int i = idx + 1; i < idx + 1 + cnt; ++i)
                        {
                            string[] strs = list[i].Split(',');
                            Vector3 pos = new Vector3(float.Parse(strs[0]), float.Parse(strs[1]), float.Parse(strs[2]));
                            block.linePosList.Add(pos);
                        }
                        idx = idx + 1 + cnt;
                        bList.Add(block);
                    }
    
                    CreateMesh(bList);
                }
            }
            catch (Exception ex)
            {
                  LogMgr.LogError(ex); 
            }
        }
    

    2.生成区域块
    根据得到的顺时针结点list, 还原编辑的区域块,当多边形为凹多边形且凹点(即角度大于180的顶点)不止一个时,需分割成多个多边形来生成mesh。若凹点只有一个,生成mesh时,将凹点作为固定点,即可生成所需mesh; 若凹点大于一个,则需要分割多边形成凸多边形或者只有一个凹点的凹多边形,所以这里牵扯到了如何利用顶点信息分割凹多边形。我的处理方法是,记录下凹点,连接两个凹点,将图形分割成两块,若两个子块满足要求则不再分割,若不满足,则重复之前操作,将不满足的子块继续分割直至满足条件。当存在两个相邻且角度大于180的顶点时,若直接连接则无法分割,所以这里和最近的非凹点连接进行强行分割。

    meshList: 存储生成的区域块

    private void CreateMesh(List<Block> blist)
        {
            for (int k = 0; k < blist.Count; ++k)
            {
                List<Vector3> list = blist[k].linePosList;
                Vector3[] vertices = new Vector3[list.Count - 1];
                bool[] IsObtuse = new bool[list.Count - 1];  //标志该顶点两边夹角是否大于180
                int[] triangles = new int[3 * (list.Count - 3)];
    
                for (int i = 0; i < list.Count - 1; ++i)
                {
                    vertices[i] = list[i];
                }
    
                int startIdx = -1;  //记录第一个角度大于180的顶点索引
                for (int i = 0; i < list.Count - 1; ++i)
                {
                    Vector3 a = list[(i + list.Count - 2) % (list.Count - 1)] - list[i];
                    Vector3 b = list[(i + 1) % (list.Count - 1)] - list[i];
                    if (Vector3.Cross(a.normalized, b.normalized).y > 0)  //顺时针编辑顶点,故可如此判断
                    {
                        IsObtuse[i] = true;
                        if (startIdx == -1)
                        {
                            startIdx = i;
                        }
                    }
                    else
                    {
                        IsObtuse[i] = false;
                    }
                }
    
                startIdx = startIdx > 0 ? startIdx : 0;
                Queue<int> queue = new Queue<int>();  //顶点队列
                for (int i = 0; i < list.Count - 1; ++i)
                {
                    queue.Enqueue((startIdx + i) % (list.Count - 1));
                }
    
                int flagCnt = 0;
                int tIdx = 0;
                int lastIdx = -1;
                List<int> vlist = new List<int>();
                while (queue.Count > 0)
                {
                    int idx = queue.Dequeue();
                    if (IsObtuse[idx])
                    {
                        if (vlist.Count > 0 && flagCnt == 0) //若存在顶点角度大于180,保证vlist[0]顶点角度大于180
                        {
                            for (int q = 0; q < vlist.Count; q++)
                            {
                                queue.Enqueue(vlist[q]);
                            }
                            vlist.Clear();
                        }
    
                        queue.Enqueue(idx);
                        IsObtuse[idx] = false;
                        ++flagCnt;
    
                        if (flagCnt == 1)
                        {
                            lastIdx = idx;
                        }
                        else if (Math.Abs(idx - lastIdx) == 1)  //相邻凹点处理, 进行强行分割
                        {
                            if (vlist.Count == 1) //连续两个凹点,则移除并重置第一个凹点
                            {
                                IsObtuse[lastIdx] = true;
                                vlist.Clear();
                                flagCnt = 1;
                                lastIdx = idx;
                            }
                            else if(queue.Count == 2) //第二个凹点是绕了一圈后的点,理论上来说queue.Count = 2,这里就是加个保护
                            {
                                Vector3 a = list[idx] - list[lastIdx];
                                Vector3 b = list[(lastIdx + 2) % (list.Count - 1)] - list[lastIdx];
                                if (Vector3.Cross(a.normalized, b.normalized).y > 0)
                                {
                                    IsObtuse[lastIdx] = true;
                                }
                                IsObtuse[idx] = true;
                                
                                int aver = vlist[1];
                                int bver = vlist[2];
                                vlist.RemoveAt(1);
    
                                queue.Clear();
                                queue.Enqueue(lastIdx);
                                queue.Enqueue(aver);
                                queue.Enqueue(bver);
                            }
                        }
                        else
                        {
                            //连接两个满足要求的顶点(故连续的两个角度大于180的图形不能处理),对多边形进行分割,并重新标记被分割的顶点
                            Vector3 a = list[(lastIdx - 1) % (list.Count - 1)] - list[lastIdx];
                            Vector3 b = list[idx] - list[lastIdx];
                            if (Vector3.Cross(a.normalized, b.normalized).y > 0)
                            {
                                IsObtuse[lastIdx] = true;
                            }
    
                            a = list[lastIdx] - list[idx];
                            b = list[(idx + 1) % (list.Count - 1)] - list[idx];
                            if (Vector3.Cross(a.normalized, b.normalized).y > 0)
                            {
                                IsObtuse[idx] = true;
                            }
                        }
                    }
                    vlist.Add(idx);
    
                    if (flagCnt > 0 && flagCnt % 2 == 0 || queue.Count == 0)
                    {
                        //若最后只剩一个角度大于180的顶点,则移除开始时的重复添加
                        if (flagCnt == 1 && queue.Count == 0)
                        {
                            vlist.RemoveAt(0);
                        }
    
                        for (int i = 0; i < vlist.Count - 2; ++i)
                        {
                            triangles[3 * (i + tIdx)] = vlist[vlist.Count - 1];//固定第一个点
                            triangles[3 * (i + tIdx) + 1] = vlist[i];
                            triangles[3 * (i + tIdx) + 2] = vlist[i + 1];
                        }
                        tIdx += (vlist.Count - 2);
                        flagCnt = 0;
                        lastIdx = -1;
                        vlist.Clear(); 
                    }
                }
                
                GameObject target = new GameObject();
                target.name = "BlockMesh_" + k;
                target.transform.position = new Vector3(0, 30, 0);
    
                MeshFilter filter = target.AddComponent<MeshFilter>();
                target.AddComponent<MeshRenderer>();
                Material material = new Material(Shader.Find("Diffuse"));
                material.color = blockColors[k % 9];
                target.GetComponent<MeshRenderer>().material = material;
    
                Mesh mesh = new Mesh();
                mesh.Clear();
                mesh.vertices = vertices;
                mesh.triangles = triangles;
                List<Color> colors = new List<Color>();
                for (int i = 0; i < mesh.vertexCount; ++i)
                {
                    colors.Add(blockColors[k % 9]);
                }
                mesh.SetColors(colors);
                //mesh.uv = GetComponent<MeshFilter>().sharedMesh.uv;
                mesh.name = "mesh";
                mesh.RecalculateNormals();
                mesh.RecalculateBounds();
    
                filter.sharedMesh = mesh;
                meshList.Add(target);
            }
        }
    

    当前方法我进行了测试还木发现什么问题,有可能我测试不足,或者有什么思考不足的方面,如果发现此方法有问题,欢迎大家指出。
    源码地址:https://github.com/gtgt154/MapSplit

    相关文章

      网友评论

          本文标题:unity开发之地图分割(二)

          本文链接:https://www.haomeiwen.com/subject/rtmgfxtx.html