unity開發(fā)之地圖分割(二)

本篇將繼續(xù)上篇的內容筹煮,對導出的數(shù)據(jù)進行讀取资昧,并還原編輯的區(qū)域塊,效果如下所示:

image.png

1.讀取導出數(shù)據(jù)

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.生成區(qū)域塊
根據(jù)得到的順時針結點list隙畜, 還原編輯的區(qū)域塊,當多邊形為凹多邊形且凹點(即角度大于180的頂點)不止一個時说贝,需分割成多個多邊形來生成mesh。若凹點只有一個慎颗,生成mesh時乡恕,將凹點作為固定點,即可生成所需mesh俯萎; 若凹點大于一個傲宜,則需要分割多邊形成凸多邊形或者只有一個凹點的凹多邊形,所以這里牽扯到了如何利用頂點信息分割凹多邊形夫啊。我的處理方法是函卒,記錄下凹點,連接兩個凹點撇眯,將圖形分割成兩塊报嵌,若兩個子塊滿足要求則不再分割,若不滿足熊榛,則重復之前操作锚国,將不滿足的子塊繼續(xù)分割直至滿足條件。當存在兩個相鄰且角度大于180的頂點時玄坦,若直接連接則無法分割血筑,所以這里和最近的非凹點連接進行強行分割。

meshList: 存儲生成的區(qū)域塊

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) //連續(xù)兩個凹點择懂,則移除并重置第一個凹點
                        {
                            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
                    {
                        //連接兩個滿足要求的頂點(故連續(xù)的兩個角度大于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);
        }
    }

當前方法我進行了測試還木發(fā)現(xiàn)什么問題,有可能我測試不足绑榴,或者有什么思考不足的方面哪轿,如果發(fā)現(xiàn)此方法有問題,歡迎大家指出翔怎。
源碼地址:https://github.com/gtgt154/MapSplit

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末窃诉,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子赤套,更是在濱河造成了極大的恐慌飘痛,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件容握,死亡現(xiàn)場離奇詭異宣脉,居然都是意外死亡,警方通過查閱死者的電腦和手機剔氏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門塑猖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人谈跛,你說我怎么就攤上這事羊苟。” “怎么了感憾?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵蜡励,是天一觀的道長。 經(jīng)常有香客問我阻桅,道長凉倚,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任嫂沉,我火速辦了婚禮占遥,結果婚禮上,老公的妹妹穿的比我還像新娘输瓜。我一直安慰自己瓦胎,他們只是感情好,可當我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布尤揣。 她就那樣靜靜地躺著搔啊,像睡著了一般。 火紅的嫁衣襯著肌膚如雪北戏。 梳的紋絲不亂的頭發(fā)上负芋,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機與錄音,去河邊找鬼旧蛾。 笑死莽龟,一個胖子當著我的面吹牛,可吹牛的內容都是我干的锨天。 我是一名探鬼主播毯盈,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼病袄!你這毒婦竟也來了搂赋?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤益缠,失蹤者是張志新(化名)和其女友劉穎脑奠,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體幅慌,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡宋欺,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了胰伍。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片迄靠。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖喇辽,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情雨席,我是刑警寧澤菩咨,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站陡厘,受9級特大地震影響抽米,放射性物質發(fā)生泄漏。R本人自食惡果不足惜糙置,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一云茸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧谤饭,春花似錦标捺、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至冤今,卻和暖如春闺兢,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背戏罢。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工屋谭, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留脚囊,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓桐磁,卻偏偏與公主長得像悔耘,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子所意,可洞房花燭夜當晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內容