Mesh Manipulation - Mesh合并

做項目的時候,有的時候我們需要把多個Mesh合并起來,只是為了讓我們做起來的時候更方便些。下面是Mesh合并的代碼喻奥,支持動態(tài)合并和保存到文件。

using UnityEngine;
using System.Collections.Generic;

namespace LDFW.Model
{
    
    public class MeshCombiner : MonoBehaviour
    {
        public List<Transform>                  subMeshTransformList0;
        public List<Transform>                  subMeshTransformList1;
        public List<Transform>                  subMeshTransformList2;
        public List<Transform>                  subMeshTransformList3;
        public List<Transform>                  multiSubMeshTransformList;
        public string                           meshSavePath = "Assets/";



        public Mesh CombineMeshesWithSameUV(List<Transform> transformList)
        {
            if (transformList == null || transformList.Count <= 0)
            {
                Debug.LogError("Input transform list is null!");
                return null;
            }

            Mesh newMesh = new Mesh();
            
            List<Vector3> vertexList = new List<Vector3>();
            List<Vector2> uvList = new List<Vector2>();
            List<int> triangleList = new List<int>();
            int currentVertexListLength = 0;

            foreach (var trans in transformList)
            {
                if (trans == null)
                    continue;

                MeshFilter meshFilter = trans.GetComponent<MeshFilter>();
                if (meshFilter == null)
                    continue;

                Mesh mesh = meshFilter.mesh;
                if (mesh == null)
                    continue;

                currentVertexListLength = vertexList.Count;

                foreach (var vertex in mesh.vertices)
                    vertexList.Add(transform.InverseTransformPoint(trans.TransformPoint(vertex)));

                foreach (var uv in mesh.uv)
                    uvList.Add(uv);

                foreach (var triangleVertexIndex in mesh.triangles)
                    triangleList.Add(currentVertexListLength + triangleVertexIndex);

            }

            newMesh.vertices = vertexList.ToArray();
            newMesh.uv = uvList.ToArray();
            newMesh.triangles = triangleList.ToArray();

            return newMesh;
        }
        
        public Mesh CombineSubMeshes(List<Transform> subMesh0, List<Transform> subMesh1, List<Transform> subMesh2, List<Transform> subMesh3)
        {
            Mesh[] subMeshArray = new Mesh[4]
            {
                CombineMeshesWithSameUV(subMesh0),
                CombineMeshesWithSameUV(subMesh1),
                CombineMeshesWithSameUV(subMesh2),
                CombineMeshesWithSameUV(subMesh3),
            };
            

            Mesh newMesh = new Mesh();
            List<Vector3> vertexList = new List<Vector3>();
            List<Vector2> uvList = new List<Vector2>();
            List<int> triangleIndexList = new List<int>();
            int currentVertexCount = 0;

            Mesh currentMesh;
            Vector3[] currentMeshVertices;
            Vector2[] currentMeshUV;
            int[] currentMeshTriangleIndices;
            List<int>[] subMeshTriangleListArray = new List<int>[4];

            int subMeshCount = 0;


            for (int i = 0; i < 4; i++)
            {
                currentMesh = subMeshArray[i];
                subMeshTriangleListArray[i] = new List<int>();

                if (currentMesh == null)
                    continue;


                subMeshCount++;
                currentVertexCount = vertexList.Count;
                currentMeshVertices = currentMesh.vertices;
                currentMeshUV = currentMesh.uv;
                currentMeshTriangleIndices = currentMesh.triangles;

                foreach (var vertex in currentMeshVertices)
                    vertexList.Add(vertex);

                foreach (var uv in currentMeshUV)
                    uvList.Add(uv);

                foreach (var triangleIndex in currentMeshTriangleIndices)
                {
                    subMeshTriangleListArray[i].Add(currentVertexCount + triangleIndex);
                    triangleIndexList.Add(currentVertexCount + triangleIndex);
                }

            }

            newMesh.vertices = vertexList.ToArray();

            newMesh.subMeshCount = subMeshCount;
            if (subMeshCount >= 1)
            {
                newMesh.uv = uvList.ToArray();
                newMesh.SetIndices(subMeshTriangleListArray[0].ToArray(), MeshTopology.Triangles, 0);
            }
            if (subMeshCount >= 2)
            {
                newMesh.uv2 = uvList.ToArray();
                newMesh.SetIndices(subMeshTriangleListArray[1].ToArray(), MeshTopology.Triangles, 1);
            }
            if (subMeshCount >= 3)
            {
                newMesh.uv3 = uvList.ToArray();
                newMesh.SetIndices(subMeshTriangleListArray[2].ToArray(), MeshTopology.Triangles, 2);
            }
            if (subMeshCount >= 4)
            {
                newMesh.uv4 = uvList.ToArray();
                newMesh.SetIndices(subMeshTriangleListArray[3].ToArray(), MeshTopology.Triangles, 3);
            }
            
            newMesh.RecalculateBounds();
            newMesh.RecalculateNormals();

            System.GC.Collect();

            return newMesh;
        }

        public Mesh CombineMultiSubMeshes(List<Transform> multiSubMeshList)
        {

            Mesh newMesh = new Mesh();
            Mesh currentMesh = null;
            int totalSubMeshCount = 0;

            List<Vector3> vertices = new List<Vector3>();
            List<Vector2> uvs = new List<Vector2>();

            List<Vector2>[] uvList= new List<Vector2>[4];
            for (int i = 0; i < 4; i++)
                uvList[i] = new List<Vector2>();

            List<int>[] triangleList = new List<int>[4];
            for (int i = 0; i < 4; i++)
                triangleList[i] = new List<int>();


            // i:                   multiSubMeshList index
            // totalSubMeshCount:   total submesh count
            // 
            for (int i = 0; totalSubMeshCount < 4 && i < multiSubMeshList.Count; i++)
            {
                if (multiSubMeshList[i] == null)
                    continue;

                currentMesh = multiSubMeshList[i].GetComponent<MeshFilter>().mesh;
                if (currentMesh == null)
                    continue;

                Debug.Log(MeshInfoExtractor.GetMeshVerticesData(currentMesh));
                Debug.Log(MeshInfoExtractor.GetMeshUVData(currentMesh));
                Debug.Log(MeshInfoExtractor.GetTriangleData(currentMesh));
                Debug.Log(MeshInfoExtractor.GetSubmeshData(currentMesh));


                int currentSubMeshCount = currentMesh.subMeshCount;
                if (totalSubMeshCount + currentSubMeshCount > 4)
                    break;

                
                int currentVertexCount = vertices.Count;

                // concatenate vertices and uvs
                AddVertices(vertices, currentMesh.vertices, transform, multiSubMeshList[i]);
                AddUVs(uvs, currentMesh.uv);

                // Extract submesh triangles
                int[][] currentMeshTriangleArray = new int[currentSubMeshCount][];
                for (int j = 0; j < currentSubMeshCount; j++)
                    currentMeshTriangleArray[j] = currentMesh.GetTriangles(j);

                // Concatenate submesh triangles
                int currentMeshTriangleArrayIndex = 0;
                for (int j = totalSubMeshCount; j < totalSubMeshCount + currentSubMeshCount; j++)
                {
                    if (currentMeshTriangleArray[currentMeshTriangleArrayIndex] != null)
                        AddMeshTriangle(triangleList[j], currentMeshTriangleArray[currentMeshTriangleArrayIndex], currentVertexCount);

                    currentMeshTriangleArrayIndex++;
                }

                totalSubMeshCount += currentSubMeshCount;
            }

            Debug.Log("Vertices length = " + vertices.Count);
            Debug.Log("UVs length = " + uvList[0].Count + ", " + uvList[1].Count + ", " + uvList[2].Count + ", " + uvList[3].Count);
            Debug.Log("Triangles length = " + triangleList[0].Count + ", " + triangleList[1].Count + ", " + triangleList[2].Count + ", " + triangleList[3].Count);

            newMesh.SetVertices(vertices);
            newMesh.uv = uvs.ToArray();

            newMesh.subMeshCount = totalSubMeshCount;
            for (int i = 0; i < totalSubMeshCount; i++)
                newMesh.SetIndices(triangleList[i].ToArray(), MeshTopology.Triangles, i);
            
            newMesh.RecalculateBounds();
            newMesh.RecalculateNormals();

            System.GC.Collect();

            return newMesh;
        }

        private void AddVertices(List<Vector3> targetVertices, Vector3[] newVertices, Transform newParent, Transform oldParent)
        {
            if (newVertices != null)
            {
                foreach (var vec3 in newVertices)
                    targetVertices.Add(newParent.InverseTransformPoint(oldParent.TransformPoint(vec3)));
            }
        }

        private void AddUVs(List<Vector2> targetUVs, Vector2[] newUVs)
        {
            if (newUVs != null)
            {
                foreach (var vec2 in newUVs)
                    targetUVs.Add(vec2);
            }
        }

        private void AddMeshUV(List<Vector2> targetUV, Vector2[] uvArray)
        {
            if (uvArray != null)
            {
                foreach (var vec2 in uvArray)
                    targetUV.Add(vec2);
            }
        }

        private void AddMeshTriangle(List<int> targetTriangles, int[] triangleArray, int triangleIndexOffset)
        {
            if (triangleArray != null)
            {
                for (int i = 0; i < triangleArray.Length; i++)
                {
                    targetTriangles.Add(triangleArray[i] + triangleIndexOffset);
                }

            }
        }




    }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末捏悬,一起剝皮案震驚了整個濱河市撞蚕,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌过牙,老刑警劉巖甥厦,帶你破解...
    沈念sama閱讀 206,602評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異寇钉,居然都是意外死亡刀疙,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評論 2 382
  • 文/潘曉璐 我一進店門扫倡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谦秧,“玉大人,你說我怎么就攤上這事撵溃【卫穑” “怎么了?”我有些...
    開封第一講書人閱讀 152,878評論 0 344
  • 文/不壞的土叔 我叫張陵缘挑,是天一觀的道長集歇。 經(jīng)常有香客問我,道長语淘,這世上最難降的妖魔是什么诲宇? 我笑而不...
    開封第一講書人閱讀 55,306評論 1 279
  • 正文 為了忘掉前任际歼,我火速辦了婚禮,結(jié)果婚禮上焕窝,老公的妹妹穿的比我還像新娘蹬挺。我一直安慰自己,他們只是感情好它掂,可當我...
    茶點故事閱讀 64,330評論 5 373
  • 文/花漫 我一把揭開白布巴帮。 她就那樣靜靜地躺著,像睡著了一般虐秋。 火紅的嫁衣襯著肌膚如雪榕茧。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,071評論 1 285
  • 那天客给,我揣著相機與錄音用押,去河邊找鬼。 笑死靶剑,一個胖子當著我的面吹牛蜻拨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播桩引,決...
    沈念sama閱讀 38,382評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼缎讼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了坑匠?” 一聲冷哼從身側(cè)響起血崭,我...
    開封第一講書人閱讀 37,006評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎厘灼,沒想到半個月后夹纫,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,512評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡设凹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,965評論 2 325
  • 正文 我和宋清朗相戀三年舰讹,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片闪朱。...
    茶點故事閱讀 38,094評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡跺涤,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出监透,到底是詐尸還是另有隱情桶错,我是刑警寧澤,帶...
    沈念sama閱讀 33,732評論 4 323
  • 正文 年R本政府宣布胀蛮,位于F島的核電站院刁,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏粪狼。R本人自食惡果不足惜退腥,卻給世界環(huán)境...
    茶點故事閱讀 39,283評論 3 307
  • 文/蒙蒙 一任岸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧狡刘,春花似錦享潜、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,286評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至澜术,卻和暖如春艺蝴,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背鸟废。 一陣腳步聲響...
    開封第一講書人閱讀 31,512評論 1 262
  • 我被黑心中介騙來泰國打工猜敢, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人盒延。 一個月前我還...
    沈念sama閱讀 45,536評論 2 354
  • 正文 我出身青樓缩擂,卻偏偏與公主長得像,于是被迫代替她去往敵國和親添寺。 傳聞我的和親對象是個殘疾皇子胯盯,可洞房花燭夜當晚...
    茶點故事閱讀 42,828評論 2 345

推薦閱讀更多精彩內(nèi)容